1275793eaSopenharmony_ci------------------------------------------------------------------------------
2275793eaSopenharmony_ci--                      ZLib for Ada thick binding.                         --
3275793eaSopenharmony_ci--                                                                          --
4275793eaSopenharmony_ci--              Copyright (C) 2002-2004 Dmitriy Anisimkov                   --
5275793eaSopenharmony_ci--                                                                          --
6275793eaSopenharmony_ci--  This library is free software; you can redistribute it and/or modify    --
7275793eaSopenharmony_ci--  it under the terms of the GNU General Public License as published by    --
8275793eaSopenharmony_ci--  the Free Software Foundation; either version 2 of the License, or (at   --
9275793eaSopenharmony_ci--  your option) any later version.                                         --
10275793eaSopenharmony_ci--                                                                          --
11275793eaSopenharmony_ci--  This library is distributed in the hope that it will be useful, but     --
12275793eaSopenharmony_ci--  WITHOUT ANY WARRANTY; without even the implied warranty of              --
13275793eaSopenharmony_ci--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       --
14275793eaSopenharmony_ci--  General Public License for more details.                                --
15275793eaSopenharmony_ci--                                                                          --
16275793eaSopenharmony_ci--  You should have received a copy of the GNU General Public License       --
17275793eaSopenharmony_ci--  along with this library; if not, write to the Free Software Foundation, --
18275793eaSopenharmony_ci--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
19275793eaSopenharmony_ci--                                                                          --
20275793eaSopenharmony_ci--  As a special exception, if other files instantiate generics from this   --
21275793eaSopenharmony_ci--  unit, or you link this unit with other files to produce an executable,  --
22275793eaSopenharmony_ci--  this  unit  does not  by itself cause  the resulting executable to be   --
23275793eaSopenharmony_ci--  covered by the GNU General Public License. This exception does not      --
24275793eaSopenharmony_ci--  however invalidate any other reasons why the executable file  might be  --
25275793eaSopenharmony_ci--  covered by the  GNU Public License.                                     --
26275793eaSopenharmony_ci------------------------------------------------------------------------------
27275793eaSopenharmony_ci
28275793eaSopenharmony_ci--  $Id: zlib.ads,v 1.26 2004/09/06 06:53:19 vagul Exp $
29275793eaSopenharmony_ci
30275793eaSopenharmony_ciwith Ada.Streams;
31275793eaSopenharmony_ci
32275793eaSopenharmony_ciwith Interfaces;
33275793eaSopenharmony_ci
34275793eaSopenharmony_cipackage ZLib is
35275793eaSopenharmony_ci
36275793eaSopenharmony_ci   ZLib_Error   : exception;
37275793eaSopenharmony_ci   Status_Error : exception;
38275793eaSopenharmony_ci
39275793eaSopenharmony_ci   type Compression_Level is new Integer range -1 .. 9;
40275793eaSopenharmony_ci
41275793eaSopenharmony_ci   type Flush_Mode is private;
42275793eaSopenharmony_ci
43275793eaSopenharmony_ci   type Compression_Method is private;
44275793eaSopenharmony_ci
45275793eaSopenharmony_ci   type Window_Bits_Type is new Integer range 8 .. 15;
46275793eaSopenharmony_ci
47275793eaSopenharmony_ci   type Memory_Level_Type is new Integer range 1 .. 9;
48275793eaSopenharmony_ci
49275793eaSopenharmony_ci   type Unsigned_32 is new Interfaces.Unsigned_32;
50275793eaSopenharmony_ci
51275793eaSopenharmony_ci   type Strategy_Type is private;
52275793eaSopenharmony_ci
53275793eaSopenharmony_ci   type Header_Type is (None, Auto, Default, GZip);
54275793eaSopenharmony_ci   --  Header type usage have a some limitation for inflate.
55275793eaSopenharmony_ci   --  See comment for Inflate_Init.
56275793eaSopenharmony_ci
57275793eaSopenharmony_ci   subtype Count is Ada.Streams.Stream_Element_Count;
58275793eaSopenharmony_ci
59275793eaSopenharmony_ci   Default_Memory_Level : constant Memory_Level_Type := 8;
60275793eaSopenharmony_ci   Default_Window_Bits  : constant Window_Bits_Type  := 15;
61275793eaSopenharmony_ci
62275793eaSopenharmony_ci   ----------------------------------
63275793eaSopenharmony_ci   -- Compression method constants --
64275793eaSopenharmony_ci   ----------------------------------
65275793eaSopenharmony_ci
66275793eaSopenharmony_ci   Deflated : constant Compression_Method;
67275793eaSopenharmony_ci   --  Only one method allowed in this ZLib version
68275793eaSopenharmony_ci
69275793eaSopenharmony_ci   ---------------------------------
70275793eaSopenharmony_ci   -- Compression level constants --
71275793eaSopenharmony_ci   ---------------------------------
72275793eaSopenharmony_ci
73275793eaSopenharmony_ci   No_Compression      : constant Compression_Level := 0;
74275793eaSopenharmony_ci   Best_Speed          : constant Compression_Level := 1;
75275793eaSopenharmony_ci   Best_Compression    : constant Compression_Level := 9;
76275793eaSopenharmony_ci   Default_Compression : constant Compression_Level := -1;
77275793eaSopenharmony_ci
78275793eaSopenharmony_ci   --------------------------
79275793eaSopenharmony_ci   -- Flush mode constants --
80275793eaSopenharmony_ci   --------------------------
81275793eaSopenharmony_ci
82275793eaSopenharmony_ci   No_Flush      : constant Flush_Mode;
83275793eaSopenharmony_ci   --  Regular way for compression, no flush
84275793eaSopenharmony_ci
85275793eaSopenharmony_ci   Partial_Flush : constant Flush_Mode;
86275793eaSopenharmony_ci   --  Will be removed, use Z_SYNC_FLUSH instead
87275793eaSopenharmony_ci
88275793eaSopenharmony_ci   Sync_Flush    : constant Flush_Mode;
89275793eaSopenharmony_ci   --  All pending output is flushed to the output buffer and the output
90275793eaSopenharmony_ci   --  is aligned on a byte boundary, so that the decompressor can get all
91275793eaSopenharmony_ci   --  input data available so far. (In particular avail_in is zero after the
92275793eaSopenharmony_ci   --  call if enough output space has been provided  before the call.)
93275793eaSopenharmony_ci   --  Flushing may degrade compression for some compression algorithms and so
94275793eaSopenharmony_ci   --  it should be used only when necessary.
95275793eaSopenharmony_ci
96275793eaSopenharmony_ci   Block_Flush   : constant Flush_Mode;
97275793eaSopenharmony_ci   --  Z_BLOCK requests that inflate() stop
98275793eaSopenharmony_ci   --  if and when it get to the next deflate block boundary. When decoding the
99275793eaSopenharmony_ci   --  zlib or gzip format, this will cause inflate() to return immediately
100275793eaSopenharmony_ci   --  after the header and before the first block. When doing a raw inflate,
101275793eaSopenharmony_ci   --  inflate() will go ahead and process the first block, and will return
102275793eaSopenharmony_ci   --  when it gets to the end of that block, or when it runs out of data.
103275793eaSopenharmony_ci
104275793eaSopenharmony_ci   Full_Flush    : constant Flush_Mode;
105275793eaSopenharmony_ci   --  All output is flushed as with SYNC_FLUSH, and the compression state
106275793eaSopenharmony_ci   --  is reset so that decompression can restart from this point if previous
107275793eaSopenharmony_ci   --  compressed data has been damaged or if random access is desired. Using
108275793eaSopenharmony_ci   --  Full_Flush too often can seriously degrade the compression.
109275793eaSopenharmony_ci
110275793eaSopenharmony_ci   Finish        : constant Flush_Mode;
111275793eaSopenharmony_ci   --  Just for tell the compressor that input data is complete.
112275793eaSopenharmony_ci
113275793eaSopenharmony_ci   ------------------------------------
114275793eaSopenharmony_ci   -- Compression strategy constants --
115275793eaSopenharmony_ci   ------------------------------------
116275793eaSopenharmony_ci
117275793eaSopenharmony_ci   --  RLE strategy could be used only in version 1.2.0 and later.
118275793eaSopenharmony_ci
119275793eaSopenharmony_ci   Filtered         : constant Strategy_Type;
120275793eaSopenharmony_ci   Huffman_Only     : constant Strategy_Type;
121275793eaSopenharmony_ci   RLE              : constant Strategy_Type;
122275793eaSopenharmony_ci   Default_Strategy : constant Strategy_Type;
123275793eaSopenharmony_ci
124275793eaSopenharmony_ci   Default_Buffer_Size : constant := 4096;
125275793eaSopenharmony_ci
126275793eaSopenharmony_ci   type Filter_Type is tagged limited private;
127275793eaSopenharmony_ci   --  The filter is for compression and for decompression.
128275793eaSopenharmony_ci   --  The usage of the type is depend of its initialization.
129275793eaSopenharmony_ci
130275793eaSopenharmony_ci   function Version return String;
131275793eaSopenharmony_ci   pragma Inline (Version);
132275793eaSopenharmony_ci   --  Return string representation of the ZLib version.
133275793eaSopenharmony_ci
134275793eaSopenharmony_ci   procedure Deflate_Init
135275793eaSopenharmony_ci     (Filter       : in out Filter_Type;
136275793eaSopenharmony_ci      Level        : in     Compression_Level  := Default_Compression;
137275793eaSopenharmony_ci      Strategy     : in     Strategy_Type      := Default_Strategy;
138275793eaSopenharmony_ci      Method       : in     Compression_Method := Deflated;
139275793eaSopenharmony_ci      Window_Bits  : in     Window_Bits_Type   := Default_Window_Bits;
140275793eaSopenharmony_ci      Memory_Level : in     Memory_Level_Type  := Default_Memory_Level;
141275793eaSopenharmony_ci      Header       : in     Header_Type        := Default);
142275793eaSopenharmony_ci   --  Compressor initialization.
143275793eaSopenharmony_ci   --  When Header parameter is Auto or Default, then default zlib header
144275793eaSopenharmony_ci   --  would be provided for compressed data.
145275793eaSopenharmony_ci   --  When Header is GZip, then gzip header would be set instead of
146275793eaSopenharmony_ci   --  default header.
147275793eaSopenharmony_ci   --  When Header is None, no header would be set for compressed data.
148275793eaSopenharmony_ci
149275793eaSopenharmony_ci   procedure Inflate_Init
150275793eaSopenharmony_ci     (Filter      : in out Filter_Type;
151275793eaSopenharmony_ci      Window_Bits : in     Window_Bits_Type := Default_Window_Bits;
152275793eaSopenharmony_ci      Header      : in     Header_Type      := Default);
153275793eaSopenharmony_ci   --  Decompressor initialization.
154275793eaSopenharmony_ci   --  Default header type mean that ZLib default header is expecting in the
155275793eaSopenharmony_ci   --  input compressed stream.
156275793eaSopenharmony_ci   --  Header type None mean that no header is expecting in the input stream.
157275793eaSopenharmony_ci   --  GZip header type mean that GZip header is expecting in the
158275793eaSopenharmony_ci   --  input compressed stream.
159275793eaSopenharmony_ci   --  Auto header type mean that header type (GZip or Native) would be
160275793eaSopenharmony_ci   --  detected automatically in the input stream.
161275793eaSopenharmony_ci   --  Note that header types parameter values None, GZip and Auto are
162275793eaSopenharmony_ci   --  supported for inflate routine only in ZLib versions 1.2.0.2 and later.
163275793eaSopenharmony_ci   --  Deflate_Init is supporting all header types.
164275793eaSopenharmony_ci
165275793eaSopenharmony_ci   function Is_Open (Filter : in Filter_Type) return Boolean;
166275793eaSopenharmony_ci   pragma Inline (Is_Open);
167275793eaSopenharmony_ci   --  Is the filter opened for compression or decompression.
168275793eaSopenharmony_ci
169275793eaSopenharmony_ci   procedure Close
170275793eaSopenharmony_ci     (Filter       : in out Filter_Type;
171275793eaSopenharmony_ci      Ignore_Error : in     Boolean := False);
172275793eaSopenharmony_ci   --  Closing the compression or decompressor.
173275793eaSopenharmony_ci   --  If stream is closing before the complete and Ignore_Error is False,
174275793eaSopenharmony_ci   --  The exception would be raised.
175275793eaSopenharmony_ci
176275793eaSopenharmony_ci   generic
177275793eaSopenharmony_ci      with procedure Data_In
178275793eaSopenharmony_ci        (Item : out Ada.Streams.Stream_Element_Array;
179275793eaSopenharmony_ci         Last : out Ada.Streams.Stream_Element_Offset);
180275793eaSopenharmony_ci      with procedure Data_Out
181275793eaSopenharmony_ci        (Item : in Ada.Streams.Stream_Element_Array);
182275793eaSopenharmony_ci   procedure Generic_Translate
183275793eaSopenharmony_ci     (Filter          : in out Filter_Type;
184275793eaSopenharmony_ci      In_Buffer_Size  : in     Integer := Default_Buffer_Size;
185275793eaSopenharmony_ci      Out_Buffer_Size : in     Integer := Default_Buffer_Size);
186275793eaSopenharmony_ci   --  Compress/decompress data fetch from Data_In routine and pass the result
187275793eaSopenharmony_ci   --  to the Data_Out routine. User should provide Data_In and Data_Out
188275793eaSopenharmony_ci   --  for compression/decompression data flow.
189275793eaSopenharmony_ci   --  Compression or decompression depend on Filter initialization.
190275793eaSopenharmony_ci
191275793eaSopenharmony_ci   function Total_In (Filter : in Filter_Type) return Count;
192275793eaSopenharmony_ci   pragma Inline (Total_In);
193275793eaSopenharmony_ci   --  Returns total number of input bytes read so far
194275793eaSopenharmony_ci
195275793eaSopenharmony_ci   function Total_Out (Filter : in Filter_Type) return Count;
196275793eaSopenharmony_ci   pragma Inline (Total_Out);
197275793eaSopenharmony_ci   --  Returns total number of bytes output so far
198275793eaSopenharmony_ci
199275793eaSopenharmony_ci   function CRC32
200275793eaSopenharmony_ci     (CRC    : in Unsigned_32;
201275793eaSopenharmony_ci      Data   : in Ada.Streams.Stream_Element_Array)
202275793eaSopenharmony_ci      return Unsigned_32;
203275793eaSopenharmony_ci   pragma Inline (CRC32);
204275793eaSopenharmony_ci   --  Compute CRC32, it could be necessary for make gzip format
205275793eaSopenharmony_ci
206275793eaSopenharmony_ci   procedure CRC32
207275793eaSopenharmony_ci     (CRC  : in out Unsigned_32;
208275793eaSopenharmony_ci      Data : in     Ada.Streams.Stream_Element_Array);
209275793eaSopenharmony_ci   pragma Inline (CRC32);
210275793eaSopenharmony_ci   --  Compute CRC32, it could be necessary for make gzip format
211275793eaSopenharmony_ci
212275793eaSopenharmony_ci   -------------------------------------------------
213275793eaSopenharmony_ci   --  Below is more complex low level routines.  --
214275793eaSopenharmony_ci   -------------------------------------------------
215275793eaSopenharmony_ci
216275793eaSopenharmony_ci   procedure Translate
217275793eaSopenharmony_ci     (Filter    : in out Filter_Type;
218275793eaSopenharmony_ci      In_Data   : in     Ada.Streams.Stream_Element_Array;
219275793eaSopenharmony_ci      In_Last   :    out Ada.Streams.Stream_Element_Offset;
220275793eaSopenharmony_ci      Out_Data  :    out Ada.Streams.Stream_Element_Array;
221275793eaSopenharmony_ci      Out_Last  :    out Ada.Streams.Stream_Element_Offset;
222275793eaSopenharmony_ci      Flush     : in     Flush_Mode);
223275793eaSopenharmony_ci   --  Compress/decompress the In_Data buffer and place the result into
224275793eaSopenharmony_ci   --  Out_Data. In_Last is the index of last element from In_Data accepted by
225275793eaSopenharmony_ci   --  the Filter. Out_Last is the last element of the received data from
226275793eaSopenharmony_ci   --  Filter. To tell the filter that incoming data are complete put the
227275793eaSopenharmony_ci   --  Flush parameter to Finish.
228275793eaSopenharmony_ci
229275793eaSopenharmony_ci   function Stream_End (Filter : in Filter_Type) return Boolean;
230275793eaSopenharmony_ci   pragma Inline (Stream_End);
231275793eaSopenharmony_ci   --  Return the true when the stream is complete.
232275793eaSopenharmony_ci
233275793eaSopenharmony_ci   procedure Flush
234275793eaSopenharmony_ci     (Filter    : in out Filter_Type;
235275793eaSopenharmony_ci      Out_Data  :    out Ada.Streams.Stream_Element_Array;
236275793eaSopenharmony_ci      Out_Last  :    out Ada.Streams.Stream_Element_Offset;
237275793eaSopenharmony_ci      Flush     : in     Flush_Mode);
238275793eaSopenharmony_ci   pragma Inline (Flush);
239275793eaSopenharmony_ci   --  Flushing the data from the compressor.
240275793eaSopenharmony_ci
241275793eaSopenharmony_ci   generic
242275793eaSopenharmony_ci      with procedure Write
243275793eaSopenharmony_ci        (Item : in Ada.Streams.Stream_Element_Array);
244275793eaSopenharmony_ci      --  User should provide this routine for accept
245275793eaSopenharmony_ci      --  compressed/decompressed data.
246275793eaSopenharmony_ci
247275793eaSopenharmony_ci      Buffer_Size : in Ada.Streams.Stream_Element_Offset
248275793eaSopenharmony_ci         := Default_Buffer_Size;
249275793eaSopenharmony_ci      --  Buffer size for Write user routine.
250275793eaSopenharmony_ci
251275793eaSopenharmony_ci   procedure Write
252275793eaSopenharmony_ci     (Filter  : in out Filter_Type;
253275793eaSopenharmony_ci      Item    : in     Ada.Streams.Stream_Element_Array;
254275793eaSopenharmony_ci      Flush   : in     Flush_Mode := No_Flush);
255275793eaSopenharmony_ci   --  Compress/Decompress data from Item to the generic parameter procedure
256275793eaSopenharmony_ci   --  Write. Output buffer size could be set in Buffer_Size generic parameter.
257275793eaSopenharmony_ci
258275793eaSopenharmony_ci   generic
259275793eaSopenharmony_ci      with procedure Read
260275793eaSopenharmony_ci        (Item : out Ada.Streams.Stream_Element_Array;
261275793eaSopenharmony_ci         Last : out Ada.Streams.Stream_Element_Offset);
262275793eaSopenharmony_ci      --  User should provide data for compression/decompression
263275793eaSopenharmony_ci      --  thru this routine.
264275793eaSopenharmony_ci
265275793eaSopenharmony_ci      Buffer : in out Ada.Streams.Stream_Element_Array;
266275793eaSopenharmony_ci      --  Buffer for keep remaining data from the previous
267275793eaSopenharmony_ci      --  back read.
268275793eaSopenharmony_ci
269275793eaSopenharmony_ci      Rest_First, Rest_Last : in out Ada.Streams.Stream_Element_Offset;
270275793eaSopenharmony_ci      --  Rest_First have to be initialized to Buffer'Last + 1
271275793eaSopenharmony_ci      --  Rest_Last have to be initialized to Buffer'Last
272275793eaSopenharmony_ci      --  before usage.
273275793eaSopenharmony_ci
274275793eaSopenharmony_ci      Allow_Read_Some : in Boolean := False;
275275793eaSopenharmony_ci      --  Is it allowed to return Last < Item'Last before end of data.
276275793eaSopenharmony_ci
277275793eaSopenharmony_ci   procedure Read
278275793eaSopenharmony_ci     (Filter : in out Filter_Type;
279275793eaSopenharmony_ci      Item   :    out Ada.Streams.Stream_Element_Array;
280275793eaSopenharmony_ci      Last   :    out Ada.Streams.Stream_Element_Offset;
281275793eaSopenharmony_ci      Flush  : in     Flush_Mode := No_Flush);
282275793eaSopenharmony_ci   --  Compress/Decompress data from generic parameter procedure Read to the
283275793eaSopenharmony_ci   --  Item. User should provide Buffer and initialized Rest_First, Rest_Last
284275793eaSopenharmony_ci   --  indicators. If Allow_Read_Some is True, Read routines could return
285275793eaSopenharmony_ci   --  Last < Item'Last only at end of stream.
286275793eaSopenharmony_ci
287275793eaSopenharmony_ciprivate
288275793eaSopenharmony_ci
289275793eaSopenharmony_ci   use Ada.Streams;
290275793eaSopenharmony_ci
291275793eaSopenharmony_ci   pragma Assert (Ada.Streams.Stream_Element'Size    =    8);
292275793eaSopenharmony_ci   pragma Assert (Ada.Streams.Stream_Element'Modulus = 2**8);
293275793eaSopenharmony_ci
294275793eaSopenharmony_ci   type Flush_Mode is new Integer range 0 .. 5;
295275793eaSopenharmony_ci
296275793eaSopenharmony_ci   type Compression_Method is new Integer range 8 .. 8;
297275793eaSopenharmony_ci
298275793eaSopenharmony_ci   type Strategy_Type is new Integer range 0 .. 3;
299275793eaSopenharmony_ci
300275793eaSopenharmony_ci   No_Flush      : constant Flush_Mode := 0;
301275793eaSopenharmony_ci   Partial_Flush : constant Flush_Mode := 1;
302275793eaSopenharmony_ci   Sync_Flush    : constant Flush_Mode := 2;
303275793eaSopenharmony_ci   Full_Flush    : constant Flush_Mode := 3;
304275793eaSopenharmony_ci   Finish        : constant Flush_Mode := 4;
305275793eaSopenharmony_ci   Block_Flush   : constant Flush_Mode := 5;
306275793eaSopenharmony_ci
307275793eaSopenharmony_ci   Filtered         : constant Strategy_Type := 1;
308275793eaSopenharmony_ci   Huffman_Only     : constant Strategy_Type := 2;
309275793eaSopenharmony_ci   RLE              : constant Strategy_Type := 3;
310275793eaSopenharmony_ci   Default_Strategy : constant Strategy_Type := 0;
311275793eaSopenharmony_ci
312275793eaSopenharmony_ci   Deflated : constant Compression_Method := 8;
313275793eaSopenharmony_ci
314275793eaSopenharmony_ci   type Z_Stream;
315275793eaSopenharmony_ci
316275793eaSopenharmony_ci   type Z_Stream_Access is access all Z_Stream;
317275793eaSopenharmony_ci
318275793eaSopenharmony_ci   type Filter_Type is tagged limited record
319275793eaSopenharmony_ci      Strm        : Z_Stream_Access;
320275793eaSopenharmony_ci      Compression : Boolean;
321275793eaSopenharmony_ci      Stream_End  : Boolean;
322275793eaSopenharmony_ci      Header      : Header_Type;
323275793eaSopenharmony_ci      CRC         : Unsigned_32;
324275793eaSopenharmony_ci      Offset      : Stream_Element_Offset;
325275793eaSopenharmony_ci      --  Offset for gzip header/footer output.
326275793eaSopenharmony_ci   end record;
327275793eaSopenharmony_ci
328275793eaSopenharmony_ciend ZLib;
329