Chris@4
|
1 ------------------------------------------------------------------------------
|
Chris@4
|
2 -- ZLib for Ada thick binding. --
|
Chris@4
|
3 -- --
|
Chris@4
|
4 -- Copyright (C) 2002-2004 Dmitriy Anisimkov --
|
Chris@4
|
5 -- --
|
Chris@4
|
6 -- This library is free software; you can redistribute it and/or modify --
|
Chris@4
|
7 -- it under the terms of the GNU General Public License as published by --
|
Chris@4
|
8 -- the Free Software Foundation; either version 2 of the License, or (at --
|
Chris@4
|
9 -- your option) any later version. --
|
Chris@4
|
10 -- --
|
Chris@4
|
11 -- This library is distributed in the hope that it will be useful, but --
|
Chris@4
|
12 -- WITHOUT ANY WARRANTY; without even the implied warranty of --
|
Chris@4
|
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
|
Chris@4
|
14 -- General Public License for more details. --
|
Chris@4
|
15 -- --
|
Chris@4
|
16 -- You should have received a copy of the GNU General Public License --
|
Chris@4
|
17 -- along with this library; if not, write to the Free Software Foundation, --
|
Chris@4
|
18 -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
|
Chris@4
|
19 -- --
|
Chris@4
|
20 -- As a special exception, if other files instantiate generics from this --
|
Chris@4
|
21 -- unit, or you link this unit with other files to produce an executable, --
|
Chris@4
|
22 -- this unit does not by itself cause the resulting executable to be --
|
Chris@4
|
23 -- covered by the GNU General Public License. This exception does not --
|
Chris@4
|
24 -- however invalidate any other reasons why the executable file might be --
|
Chris@4
|
25 -- covered by the GNU Public License. --
|
Chris@4
|
26 ------------------------------------------------------------------------------
|
Chris@4
|
27
|
Chris@4
|
28 -- $Id: zlib.ads,v 1.26 2004/09/06 06:53:19 vagul Exp $
|
Chris@4
|
29
|
Chris@4
|
30 with Ada.Streams;
|
Chris@4
|
31
|
Chris@4
|
32 with Interfaces;
|
Chris@4
|
33
|
Chris@4
|
34 package ZLib is
|
Chris@4
|
35
|
Chris@4
|
36 ZLib_Error : exception;
|
Chris@4
|
37 Status_Error : exception;
|
Chris@4
|
38
|
Chris@4
|
39 type Compression_Level is new Integer range -1 .. 9;
|
Chris@4
|
40
|
Chris@4
|
41 type Flush_Mode is private;
|
Chris@4
|
42
|
Chris@4
|
43 type Compression_Method is private;
|
Chris@4
|
44
|
Chris@4
|
45 type Window_Bits_Type is new Integer range 8 .. 15;
|
Chris@4
|
46
|
Chris@4
|
47 type Memory_Level_Type is new Integer range 1 .. 9;
|
Chris@4
|
48
|
Chris@4
|
49 type Unsigned_32 is new Interfaces.Unsigned_32;
|
Chris@4
|
50
|
Chris@4
|
51 type Strategy_Type is private;
|
Chris@4
|
52
|
Chris@4
|
53 type Header_Type is (None, Auto, Default, GZip);
|
Chris@4
|
54 -- Header type usage have a some limitation for inflate.
|
Chris@4
|
55 -- See comment for Inflate_Init.
|
Chris@4
|
56
|
Chris@4
|
57 subtype Count is Ada.Streams.Stream_Element_Count;
|
Chris@4
|
58
|
Chris@4
|
59 Default_Memory_Level : constant Memory_Level_Type := 8;
|
Chris@4
|
60 Default_Window_Bits : constant Window_Bits_Type := 15;
|
Chris@4
|
61
|
Chris@4
|
62 ----------------------------------
|
Chris@4
|
63 -- Compression method constants --
|
Chris@4
|
64 ----------------------------------
|
Chris@4
|
65
|
Chris@4
|
66 Deflated : constant Compression_Method;
|
Chris@4
|
67 -- Only one method allowed in this ZLib version
|
Chris@4
|
68
|
Chris@4
|
69 ---------------------------------
|
Chris@4
|
70 -- Compression level constants --
|
Chris@4
|
71 ---------------------------------
|
Chris@4
|
72
|
Chris@4
|
73 No_Compression : constant Compression_Level := 0;
|
Chris@4
|
74 Best_Speed : constant Compression_Level := 1;
|
Chris@4
|
75 Best_Compression : constant Compression_Level := 9;
|
Chris@4
|
76 Default_Compression : constant Compression_Level := -1;
|
Chris@4
|
77
|
Chris@4
|
78 --------------------------
|
Chris@4
|
79 -- Flush mode constants --
|
Chris@4
|
80 --------------------------
|
Chris@4
|
81
|
Chris@4
|
82 No_Flush : constant Flush_Mode;
|
Chris@4
|
83 -- Regular way for compression, no flush
|
Chris@4
|
84
|
Chris@4
|
85 Partial_Flush : constant Flush_Mode;
|
Chris@4
|
86 -- Will be removed, use Z_SYNC_FLUSH instead
|
Chris@4
|
87
|
Chris@4
|
88 Sync_Flush : constant Flush_Mode;
|
Chris@4
|
89 -- All pending output is flushed to the output buffer and the output
|
Chris@4
|
90 -- is aligned on a byte boundary, so that the decompressor can get all
|
Chris@4
|
91 -- input data available so far. (In particular avail_in is zero after the
|
Chris@4
|
92 -- call if enough output space has been provided before the call.)
|
Chris@4
|
93 -- Flushing may degrade compression for some compression algorithms and so
|
Chris@4
|
94 -- it should be used only when necessary.
|
Chris@4
|
95
|
Chris@4
|
96 Block_Flush : constant Flush_Mode;
|
Chris@4
|
97 -- Z_BLOCK requests that inflate() stop
|
Chris@4
|
98 -- if and when it get to the next deflate block boundary. When decoding the
|
Chris@4
|
99 -- zlib or gzip format, this will cause inflate() to return immediately
|
Chris@4
|
100 -- after the header and before the first block. When doing a raw inflate,
|
Chris@4
|
101 -- inflate() will go ahead and process the first block, and will return
|
Chris@4
|
102 -- when it gets to the end of that block, or when it runs out of data.
|
Chris@4
|
103
|
Chris@4
|
104 Full_Flush : constant Flush_Mode;
|
Chris@4
|
105 -- All output is flushed as with SYNC_FLUSH, and the compression state
|
Chris@4
|
106 -- is reset so that decompression can restart from this point if previous
|
Chris@4
|
107 -- compressed data has been damaged or if random access is desired. Using
|
Chris@4
|
108 -- Full_Flush too often can seriously degrade the compression.
|
Chris@4
|
109
|
Chris@4
|
110 Finish : constant Flush_Mode;
|
Chris@4
|
111 -- Just for tell the compressor that input data is complete.
|
Chris@4
|
112
|
Chris@4
|
113 ------------------------------------
|
Chris@4
|
114 -- Compression strategy constants --
|
Chris@4
|
115 ------------------------------------
|
Chris@4
|
116
|
Chris@4
|
117 -- RLE stategy could be used only in version 1.2.0 and later.
|
Chris@4
|
118
|
Chris@4
|
119 Filtered : constant Strategy_Type;
|
Chris@4
|
120 Huffman_Only : constant Strategy_Type;
|
Chris@4
|
121 RLE : constant Strategy_Type;
|
Chris@4
|
122 Default_Strategy : constant Strategy_Type;
|
Chris@4
|
123
|
Chris@4
|
124 Default_Buffer_Size : constant := 4096;
|
Chris@4
|
125
|
Chris@4
|
126 type Filter_Type is tagged limited private;
|
Chris@4
|
127 -- The filter is for compression and for decompression.
|
Chris@4
|
128 -- The usage of the type is depend of its initialization.
|
Chris@4
|
129
|
Chris@4
|
130 function Version return String;
|
Chris@4
|
131 pragma Inline (Version);
|
Chris@4
|
132 -- Return string representation of the ZLib version.
|
Chris@4
|
133
|
Chris@4
|
134 procedure Deflate_Init
|
Chris@4
|
135 (Filter : in out Filter_Type;
|
Chris@4
|
136 Level : in Compression_Level := Default_Compression;
|
Chris@4
|
137 Strategy : in Strategy_Type := Default_Strategy;
|
Chris@4
|
138 Method : in Compression_Method := Deflated;
|
Chris@4
|
139 Window_Bits : in Window_Bits_Type := Default_Window_Bits;
|
Chris@4
|
140 Memory_Level : in Memory_Level_Type := Default_Memory_Level;
|
Chris@4
|
141 Header : in Header_Type := Default);
|
Chris@4
|
142 -- Compressor initialization.
|
Chris@4
|
143 -- When Header parameter is Auto or Default, then default zlib header
|
Chris@4
|
144 -- would be provided for compressed data.
|
Chris@4
|
145 -- When Header is GZip, then gzip header would be set instead of
|
Chris@4
|
146 -- default header.
|
Chris@4
|
147 -- When Header is None, no header would be set for compressed data.
|
Chris@4
|
148
|
Chris@4
|
149 procedure Inflate_Init
|
Chris@4
|
150 (Filter : in out Filter_Type;
|
Chris@4
|
151 Window_Bits : in Window_Bits_Type := Default_Window_Bits;
|
Chris@4
|
152 Header : in Header_Type := Default);
|
Chris@4
|
153 -- Decompressor initialization.
|
Chris@4
|
154 -- Default header type mean that ZLib default header is expecting in the
|
Chris@4
|
155 -- input compressed stream.
|
Chris@4
|
156 -- Header type None mean that no header is expecting in the input stream.
|
Chris@4
|
157 -- GZip header type mean that GZip header is expecting in the
|
Chris@4
|
158 -- input compressed stream.
|
Chris@4
|
159 -- Auto header type mean that header type (GZip or Native) would be
|
Chris@4
|
160 -- detected automatically in the input stream.
|
Chris@4
|
161 -- Note that header types parameter values None, GZip and Auto are
|
Chris@4
|
162 -- supported for inflate routine only in ZLib versions 1.2.0.2 and later.
|
Chris@4
|
163 -- Deflate_Init is supporting all header types.
|
Chris@4
|
164
|
Chris@4
|
165 function Is_Open (Filter : in Filter_Type) return Boolean;
|
Chris@4
|
166 pragma Inline (Is_Open);
|
Chris@4
|
167 -- Is the filter opened for compression or decompression.
|
Chris@4
|
168
|
Chris@4
|
169 procedure Close
|
Chris@4
|
170 (Filter : in out Filter_Type;
|
Chris@4
|
171 Ignore_Error : in Boolean := False);
|
Chris@4
|
172 -- Closing the compression or decompressor.
|
Chris@4
|
173 -- If stream is closing before the complete and Ignore_Error is False,
|
Chris@4
|
174 -- The exception would be raised.
|
Chris@4
|
175
|
Chris@4
|
176 generic
|
Chris@4
|
177 with procedure Data_In
|
Chris@4
|
178 (Item : out Ada.Streams.Stream_Element_Array;
|
Chris@4
|
179 Last : out Ada.Streams.Stream_Element_Offset);
|
Chris@4
|
180 with procedure Data_Out
|
Chris@4
|
181 (Item : in Ada.Streams.Stream_Element_Array);
|
Chris@4
|
182 procedure Generic_Translate
|
Chris@4
|
183 (Filter : in out Filter_Type;
|
Chris@4
|
184 In_Buffer_Size : in Integer := Default_Buffer_Size;
|
Chris@4
|
185 Out_Buffer_Size : in Integer := Default_Buffer_Size);
|
Chris@4
|
186 -- Compress/decompress data fetch from Data_In routine and pass the result
|
Chris@4
|
187 -- to the Data_Out routine. User should provide Data_In and Data_Out
|
Chris@4
|
188 -- for compression/decompression data flow.
|
Chris@4
|
189 -- Compression or decompression depend on Filter initialization.
|
Chris@4
|
190
|
Chris@4
|
191 function Total_In (Filter : in Filter_Type) return Count;
|
Chris@4
|
192 pragma Inline (Total_In);
|
Chris@4
|
193 -- Returns total number of input bytes read so far
|
Chris@4
|
194
|
Chris@4
|
195 function Total_Out (Filter : in Filter_Type) return Count;
|
Chris@4
|
196 pragma Inline (Total_Out);
|
Chris@4
|
197 -- Returns total number of bytes output so far
|
Chris@4
|
198
|
Chris@4
|
199 function CRC32
|
Chris@4
|
200 (CRC : in Unsigned_32;
|
Chris@4
|
201 Data : in Ada.Streams.Stream_Element_Array)
|
Chris@4
|
202 return Unsigned_32;
|
Chris@4
|
203 pragma Inline (CRC32);
|
Chris@4
|
204 -- Compute CRC32, it could be necessary for make gzip format
|
Chris@4
|
205
|
Chris@4
|
206 procedure CRC32
|
Chris@4
|
207 (CRC : in out Unsigned_32;
|
Chris@4
|
208 Data : in Ada.Streams.Stream_Element_Array);
|
Chris@4
|
209 pragma Inline (CRC32);
|
Chris@4
|
210 -- Compute CRC32, it could be necessary for make gzip format
|
Chris@4
|
211
|
Chris@4
|
212 -------------------------------------------------
|
Chris@4
|
213 -- Below is more complex low level routines. --
|
Chris@4
|
214 -------------------------------------------------
|
Chris@4
|
215
|
Chris@4
|
216 procedure Translate
|
Chris@4
|
217 (Filter : in out Filter_Type;
|
Chris@4
|
218 In_Data : in Ada.Streams.Stream_Element_Array;
|
Chris@4
|
219 In_Last : out Ada.Streams.Stream_Element_Offset;
|
Chris@4
|
220 Out_Data : out Ada.Streams.Stream_Element_Array;
|
Chris@4
|
221 Out_Last : out Ada.Streams.Stream_Element_Offset;
|
Chris@4
|
222 Flush : in Flush_Mode);
|
Chris@4
|
223 -- Compress/decompress the In_Data buffer and place the result into
|
Chris@4
|
224 -- Out_Data. In_Last is the index of last element from In_Data accepted by
|
Chris@4
|
225 -- the Filter. Out_Last is the last element of the received data from
|
Chris@4
|
226 -- Filter. To tell the filter that incoming data are complete put the
|
Chris@4
|
227 -- Flush parameter to Finish.
|
Chris@4
|
228
|
Chris@4
|
229 function Stream_End (Filter : in Filter_Type) return Boolean;
|
Chris@4
|
230 pragma Inline (Stream_End);
|
Chris@4
|
231 -- Return the true when the stream is complete.
|
Chris@4
|
232
|
Chris@4
|
233 procedure Flush
|
Chris@4
|
234 (Filter : in out Filter_Type;
|
Chris@4
|
235 Out_Data : out Ada.Streams.Stream_Element_Array;
|
Chris@4
|
236 Out_Last : out Ada.Streams.Stream_Element_Offset;
|
Chris@4
|
237 Flush : in Flush_Mode);
|
Chris@4
|
238 pragma Inline (Flush);
|
Chris@4
|
239 -- Flushing the data from the compressor.
|
Chris@4
|
240
|
Chris@4
|
241 generic
|
Chris@4
|
242 with procedure Write
|
Chris@4
|
243 (Item : in Ada.Streams.Stream_Element_Array);
|
Chris@4
|
244 -- User should provide this routine for accept
|
Chris@4
|
245 -- compressed/decompressed data.
|
Chris@4
|
246
|
Chris@4
|
247 Buffer_Size : in Ada.Streams.Stream_Element_Offset
|
Chris@4
|
248 := Default_Buffer_Size;
|
Chris@4
|
249 -- Buffer size for Write user routine.
|
Chris@4
|
250
|
Chris@4
|
251 procedure Write
|
Chris@4
|
252 (Filter : in out Filter_Type;
|
Chris@4
|
253 Item : in Ada.Streams.Stream_Element_Array;
|
Chris@4
|
254 Flush : in Flush_Mode := No_Flush);
|
Chris@4
|
255 -- Compress/Decompress data from Item to the generic parameter procedure
|
Chris@4
|
256 -- Write. Output buffer size could be set in Buffer_Size generic parameter.
|
Chris@4
|
257
|
Chris@4
|
258 generic
|
Chris@4
|
259 with procedure Read
|
Chris@4
|
260 (Item : out Ada.Streams.Stream_Element_Array;
|
Chris@4
|
261 Last : out Ada.Streams.Stream_Element_Offset);
|
Chris@4
|
262 -- User should provide data for compression/decompression
|
Chris@4
|
263 -- thru this routine.
|
Chris@4
|
264
|
Chris@4
|
265 Buffer : in out Ada.Streams.Stream_Element_Array;
|
Chris@4
|
266 -- Buffer for keep remaining data from the previous
|
Chris@4
|
267 -- back read.
|
Chris@4
|
268
|
Chris@4
|
269 Rest_First, Rest_Last : in out Ada.Streams.Stream_Element_Offset;
|
Chris@4
|
270 -- Rest_First have to be initialized to Buffer'Last + 1
|
Chris@4
|
271 -- Rest_Last have to be initialized to Buffer'Last
|
Chris@4
|
272 -- before usage.
|
Chris@4
|
273
|
Chris@4
|
274 Allow_Read_Some : in Boolean := False;
|
Chris@4
|
275 -- Is it allowed to return Last < Item'Last before end of data.
|
Chris@4
|
276
|
Chris@4
|
277 procedure Read
|
Chris@4
|
278 (Filter : in out Filter_Type;
|
Chris@4
|
279 Item : out Ada.Streams.Stream_Element_Array;
|
Chris@4
|
280 Last : out Ada.Streams.Stream_Element_Offset;
|
Chris@4
|
281 Flush : in Flush_Mode := No_Flush);
|
Chris@4
|
282 -- Compress/Decompress data from generic parameter procedure Read to the
|
Chris@4
|
283 -- Item. User should provide Buffer and initialized Rest_First, Rest_Last
|
Chris@4
|
284 -- indicators. If Allow_Read_Some is True, Read routines could return
|
Chris@4
|
285 -- Last < Item'Last only at end of stream.
|
Chris@4
|
286
|
Chris@4
|
287 private
|
Chris@4
|
288
|
Chris@4
|
289 use Ada.Streams;
|
Chris@4
|
290
|
Chris@4
|
291 pragma Assert (Ada.Streams.Stream_Element'Size = 8);
|
Chris@4
|
292 pragma Assert (Ada.Streams.Stream_Element'Modulus = 2**8);
|
Chris@4
|
293
|
Chris@4
|
294 type Flush_Mode is new Integer range 0 .. 5;
|
Chris@4
|
295
|
Chris@4
|
296 type Compression_Method is new Integer range 8 .. 8;
|
Chris@4
|
297
|
Chris@4
|
298 type Strategy_Type is new Integer range 0 .. 3;
|
Chris@4
|
299
|
Chris@4
|
300 No_Flush : constant Flush_Mode := 0;
|
Chris@4
|
301 Partial_Flush : constant Flush_Mode := 1;
|
Chris@4
|
302 Sync_Flush : constant Flush_Mode := 2;
|
Chris@4
|
303 Full_Flush : constant Flush_Mode := 3;
|
Chris@4
|
304 Finish : constant Flush_Mode := 4;
|
Chris@4
|
305 Block_Flush : constant Flush_Mode := 5;
|
Chris@4
|
306
|
Chris@4
|
307 Filtered : constant Strategy_Type := 1;
|
Chris@4
|
308 Huffman_Only : constant Strategy_Type := 2;
|
Chris@4
|
309 RLE : constant Strategy_Type := 3;
|
Chris@4
|
310 Default_Strategy : constant Strategy_Type := 0;
|
Chris@4
|
311
|
Chris@4
|
312 Deflated : constant Compression_Method := 8;
|
Chris@4
|
313
|
Chris@4
|
314 type Z_Stream;
|
Chris@4
|
315
|
Chris@4
|
316 type Z_Stream_Access is access all Z_Stream;
|
Chris@4
|
317
|
Chris@4
|
318 type Filter_Type is tagged limited record
|
Chris@4
|
319 Strm : Z_Stream_Access;
|
Chris@4
|
320 Compression : Boolean;
|
Chris@4
|
321 Stream_End : Boolean;
|
Chris@4
|
322 Header : Header_Type;
|
Chris@4
|
323 CRC : Unsigned_32;
|
Chris@4
|
324 Offset : Stream_Element_Offset;
|
Chris@4
|
325 -- Offset for gzip header/footer output.
|
Chris@4
|
326 end record;
|
Chris@4
|
327
|
Chris@4
|
328 end ZLib;
|