annotate src/zlib-1.2.8/contrib/ada/zlib.ads @ 43:5ea0608b923f

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