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