zlib.ads 13.3 KB
Newer Older
M
Mark Adler 已提交
1 2 3
------------------------------------------------------------------------------
--                      ZLib for Ada thick binding.                         --
--                                                                          --
M
Mark Adler 已提交
4
--              Copyright (C) 2002-2004 Dmitriy Anisimkov                   --
M
Mark Adler 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
--                                                                          --
--  This library is free software; you can redistribute it and/or modify    --
--  it under the terms of the GNU General Public License as published by    --
--  the Free Software Foundation; either version 2 of the License, or (at   --
--  your option) any later version.                                         --
--                                                                          --
--  This library is distributed in the hope that it will be useful, but     --
--  WITHOUT ANY WARRANTY; without even the implied warranty of              --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       --
--  General Public License for more details.                                --
--                                                                          --
--  You should have received a copy of the GNU General Public License       --
--  along with this library; if not, write to the Free Software Foundation, --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file  might be  --
--  covered by the  GNU Public License.                                     --
------------------------------------------------------------------------------

M
Mark Adler 已提交
28
--  $Id: zlib.ads,v 1.26 2004/09/06 06:53:19 vagul Exp $
M
Mark Adler 已提交
29 30 31 32 33 34 35

with Ada.Streams;

with Interfaces;

package ZLib is

M
Mark Adler 已提交
36 37
   ZLib_Error   : exception;
   Status_Error : exception;
M
Mark Adler 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

   type Compression_Level is new Integer range -1 .. 9;

   type Flush_Mode is private;

   type Compression_Method is private;

   type Window_Bits_Type is new Integer range 8 .. 15;

   type Memory_Level_Type is new Integer range 1 .. 9;

   type Unsigned_32 is new Interfaces.Unsigned_32;

   type Strategy_Type is private;

   type Header_Type is (None, Auto, Default, GZip);
   --  Header type usage have a some limitation for inflate.
   --  See comment for Inflate_Init.

   subtype Count is Ada.Streams.Stream_Element_Count;

M
Mark Adler 已提交
59 60 61
   Default_Memory_Level : constant Memory_Level_Type := 8;
   Default_Window_Bits  : constant Window_Bits_Type  := 15;

M
Mark Adler 已提交
62 63 64 65 66
   ----------------------------------
   -- Compression method constants --
   ----------------------------------

   Deflated : constant Compression_Method;
M
Mark Adler 已提交
67
   --  Only one method allowed in this ZLib version
M
Mark Adler 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

   ---------------------------------
   -- Compression level constants --
   ---------------------------------

   No_Compression      : constant Compression_Level := 0;
   Best_Speed          : constant Compression_Level := 1;
   Best_Compression    : constant Compression_Level := 9;
   Default_Compression : constant Compression_Level := -1;

   --------------------------
   -- Flush mode constants --
   --------------------------

   No_Flush      : constant Flush_Mode;
   --  Regular way for compression, no flush

   Partial_Flush : constant Flush_Mode;
M
Mark Adler 已提交
86
   --  Will be removed, use Z_SYNC_FLUSH instead
M
Mark Adler 已提交
87 88

   Sync_Flush    : constant Flush_Mode;
M
Mark Adler 已提交
89
   --  All pending output is flushed to the output buffer and the output
M
Mark Adler 已提交
90 91 92 93 94 95
   --  is aligned on a byte boundary, so that the decompressor can get all
   --  input data available so far. (In particular avail_in is zero after the
   --  call if enough output space has been provided  before the call.)
   --  Flushing may degrade compression for some compression algorithms and so
   --  it should be used only when necessary.

M
Mark Adler 已提交
96 97 98 99 100 101 102 103
   Block_Flush   : constant Flush_Mode;
   --  Z_BLOCK requests that inflate() stop
   --  if and when it get to the next deflate block boundary. When decoding the
   --  zlib or gzip format, this will cause inflate() to return immediately
   --  after the header and before the first block. When doing a raw inflate,
   --  inflate() will go ahead and process the first block, and will return
   --  when it gets to the end of that block, or when it runs out of data.

M
Mark Adler 已提交
104
   Full_Flush    : constant Flush_Mode;
M
Mark Adler 已提交
105
   --  All output is flushed as with SYNC_FLUSH, and the compression state
M
Mark Adler 已提交
106 107
   --  is reset so that decompression can restart from this point if previous
   --  compressed data has been damaged or if random access is desired. Using
M
Mark Adler 已提交
108
   --  Full_Flush too often can seriously degrade the compression.
M
Mark Adler 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

   Finish        : constant Flush_Mode;
   --  Just for tell the compressor that input data is complete.

   ------------------------------------
   -- Compression strategy constants --
   ------------------------------------

   --  RLE stategy could be used only in version 1.2.0 and later.

   Filtered         : constant Strategy_Type;
   Huffman_Only     : constant Strategy_Type;
   RLE              : constant Strategy_Type;
   Default_Strategy : constant Strategy_Type;

   Default_Buffer_Size : constant := 4096;

M
Mark Adler 已提交
126
   type Filter_Type is tagged limited private;
M
Mark Adler 已提交
127 128 129 130 131 132 133 134 135 136 137 138
   --  The filter is for compression and for decompression.
   --  The usage of the type is depend of its initialization.

   function Version return String;
   pragma Inline (Version);
   --  Return string representation of the ZLib version.

   procedure Deflate_Init
     (Filter       : in out Filter_Type;
      Level        : in     Compression_Level  := Default_Compression;
      Strategy     : in     Strategy_Type      := Default_Strategy;
      Method       : in     Compression_Method := Deflated;
M
Mark Adler 已提交
139 140
      Window_Bits  : in     Window_Bits_Type   := Default_Window_Bits;
      Memory_Level : in     Memory_Level_Type  := Default_Memory_Level;
M
Mark Adler 已提交
141 142 143 144 145 146 147 148 149 150
      Header       : in     Header_Type        := Default);
   --  Compressor initialization.
   --  When Header parameter is Auto or Default, then default zlib header
   --  would be provided for compressed data.
   --  When Header is GZip, then gzip header would be set instead of
   --  default header.
   --  When Header is None, no header would be set for compressed data.

   procedure Inflate_Init
     (Filter      : in out Filter_Type;
M
Mark Adler 已提交
151
      Window_Bits : in     Window_Bits_Type := Default_Window_Bits;
M
Mark Adler 已提交
152 153 154 155 156 157 158 159 160
      Header      : in     Header_Type      := Default);
   --  Decompressor initialization.
   --  Default header type mean that ZLib default header is expecting in the
   --  input compressed stream.
   --  Header type None mean that no header is expecting in the input stream.
   --  GZip header type mean that GZip header is expecting in the
   --  input compressed stream.
   --  Auto header type mean that header type (GZip or Native) would be
   --  detected automatically in the input stream.
M
Mark Adler 已提交
161 162
   --  Note that header types parameter values None, GZip and Auto are
   --  supported for inflate routine only in ZLib versions 1.2.0.2 and later.
M
Mark Adler 已提交
163 164
   --  Deflate_Init is supporting all header types.

M
Mark Adler 已提交
165 166 167 168
   function Is_Open (Filter : in Filter_Type) return Boolean;
   pragma Inline (Is_Open);
   --  Is the filter opened for compression or decompression.

M
Mark Adler 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
   procedure Close
     (Filter       : in out Filter_Type;
      Ignore_Error : in     Boolean := False);
   --  Closing the compression or decompressor.
   --  If stream is closing before the complete and Ignore_Error is False,
   --  The exception would be raised.

   generic
      with procedure Data_In
        (Item : out Ada.Streams.Stream_Element_Array;
         Last : out Ada.Streams.Stream_Element_Offset);
      with procedure Data_Out
        (Item : in Ada.Streams.Stream_Element_Array);
   procedure Generic_Translate
     (Filter          : in out Filter_Type;
      In_Buffer_Size  : in     Integer := Default_Buffer_Size;
      Out_Buffer_Size : in     Integer := Default_Buffer_Size);
M
Mark Adler 已提交
186
   --  Compress/decompress data fetch from Data_In routine and pass the result
M
Mark Adler 已提交
187 188
   --  to the Data_Out routine. User should provide Data_In and Data_Out
   --  for compression/decompression data flow.
M
Mark Adler 已提交
189
   --  Compression or decompression depend on Filter initialization.
M
Mark Adler 已提交
190 191 192

   function Total_In (Filter : in Filter_Type) return Count;
   pragma Inline (Total_In);
M
Mark Adler 已提交
193
   --  Returns total number of input bytes read so far
M
Mark Adler 已提交
194 195 196

   function Total_Out (Filter : in Filter_Type) return Count;
   pragma Inline (Total_Out);
M
Mark Adler 已提交
197
   --  Returns total number of bytes output so far
M
Mark Adler 已提交
198 199 200 201 202 203

   function CRC32
     (CRC    : in Unsigned_32;
      Data   : in Ada.Streams.Stream_Element_Array)
      return Unsigned_32;
   pragma Inline (CRC32);
M
Mark Adler 已提交
204
   --  Compute CRC32, it could be necessary for make gzip format
M
Mark Adler 已提交
205 206 207 208 209

   procedure CRC32
     (CRC  : in out Unsigned_32;
      Data : in     Ada.Streams.Stream_Element_Array);
   pragma Inline (CRC32);
M
Mark Adler 已提交
210
   --  Compute CRC32, it could be necessary for make gzip format
M
Mark Adler 已提交
211 212 213 214 215 216 217 218 219 220 221 222

   -------------------------------------------------
   --  Below is more complex low level routines.  --
   -------------------------------------------------

   procedure Translate
     (Filter    : in out Filter_Type;
      In_Data   : in     Ada.Streams.Stream_Element_Array;
      In_Last   :    out Ada.Streams.Stream_Element_Offset;
      Out_Data  :    out Ada.Streams.Stream_Element_Array;
      Out_Last  :    out Ada.Streams.Stream_Element_Offset;
      Flush     : in     Flush_Mode);
M
Mark Adler 已提交
223 224 225 226 227
   --  Compress/decompress the In_Data buffer and place the result into
   --  Out_Data. In_Last is the index of last element from In_Data accepted by
   --  the Filter. Out_Last is the last element of the received data from
   --  Filter. To tell the filter that incoming data are complete put the
   --  Flush parameter to Finish.
M
Mark Adler 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

   function Stream_End (Filter : in Filter_Type) return Boolean;
   pragma Inline (Stream_End);
   --  Return the true when the stream is complete.

   procedure Flush
     (Filter    : in out Filter_Type;
      Out_Data  :    out Ada.Streams.Stream_Element_Array;
      Out_Last  :    out Ada.Streams.Stream_Element_Offset;
      Flush     : in     Flush_Mode);
   pragma Inline (Flush);
   --  Flushing the data from the compressor.

   generic
      with procedure Write
        (Item : in Ada.Streams.Stream_Element_Array);
      --  User should provide this routine for accept
      --  compressed/decompressed data.

      Buffer_Size : in Ada.Streams.Stream_Element_Offset
         := Default_Buffer_Size;
      --  Buffer size for Write user routine.

   procedure Write
     (Filter  : in out Filter_Type;
      Item    : in     Ada.Streams.Stream_Element_Array;
M
Mark Adler 已提交
254 255 256
      Flush   : in     Flush_Mode := No_Flush);
   --  Compress/Decompress data from Item to the generic parameter procedure
   --  Write. Output buffer size could be set in Buffer_Size generic parameter.
M
Mark Adler 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270

   generic
      with procedure Read
        (Item : out Ada.Streams.Stream_Element_Array;
         Last : out Ada.Streams.Stream_Element_Offset);
      --  User should provide data for compression/decompression
      --  thru this routine.

      Buffer : in out Ada.Streams.Stream_Element_Array;
      --  Buffer for keep remaining data from the previous
      --  back read.

      Rest_First, Rest_Last : in out Ada.Streams.Stream_Element_Offset;
      --  Rest_First have to be initialized to Buffer'Last + 1
M
Mark Adler 已提交
271
      --  Rest_Last have to be initialized to Buffer'Last
M
Mark Adler 已提交
272 273
      --  before usage.

M
Mark Adler 已提交
274 275 276
      Allow_Read_Some : in Boolean := False;
      --  Is it allowed to return Last < Item'Last before end of data.

M
Mark Adler 已提交
277 278 279
   procedure Read
     (Filter : in out Filter_Type;
      Item   :    out Ada.Streams.Stream_Element_Array;
M
Mark Adler 已提交
280 281 282 283 284 285
      Last   :    out Ada.Streams.Stream_Element_Offset;
      Flush  : in     Flush_Mode := No_Flush);
   --  Compress/Decompress data from generic parameter procedure Read to the
   --  Item. User should provide Buffer and initialized Rest_First, Rest_Last
   --  indicators. If Allow_Read_Some is True, Read routines could return
   --  Last < Item'Last only at end of stream.
M
Mark Adler 已提交
286 287 288 289 290

private

   use Ada.Streams;

M
Mark Adler 已提交
291 292 293 294
   pragma Assert (Ada.Streams.Stream_Element'Size    =    8);
   pragma Assert (Ada.Streams.Stream_Element'Modulus = 2**8);

   type Flush_Mode is new Integer range 0 .. 5;
M
Mark Adler 已提交
295 296 297 298 299 300

   type Compression_Method is new Integer range 8 .. 8;

   type Strategy_Type is new Integer range 0 .. 3;

   No_Flush      : constant Flush_Mode := 0;
M
Mark Adler 已提交
301
   Partial_Flush : constant Flush_Mode := 1;
M
Mark Adler 已提交
302 303 304
   Sync_Flush    : constant Flush_Mode := 2;
   Full_Flush    : constant Flush_Mode := 3;
   Finish        : constant Flush_Mode := 4;
M
Mark Adler 已提交
305
   Block_Flush   : constant Flush_Mode := 5;
M
Mark Adler 已提交
306 307 308 309 310 311 312 313 314 315 316 317

   Filtered         : constant Strategy_Type := 1;
   Huffman_Only     : constant Strategy_Type := 2;
   RLE              : constant Strategy_Type := 3;
   Default_Strategy : constant Strategy_Type := 0;

   Deflated : constant Compression_Method := 8;

   type Z_Stream;

   type Z_Stream_Access is access all Z_Stream;

M
Mark Adler 已提交
318
   type Filter_Type is tagged limited record
M
Mark Adler 已提交
319 320 321 322 323 324 325 326 327 328
      Strm        : Z_Stream_Access;
      Compression : Boolean;
      Stream_End  : Boolean;
      Header      : Header_Type;
      CRC         : Unsigned_32;
      Offset      : Stream_Element_Offset;
      --  Offset for gzip header/footer output.
   end record;

end ZLib;