kusano fc6ab3
------------------------------------------------------------------------------
kusano fc6ab3
--                      ZLib for Ada thick binding.                         --
kusano fc6ab3
--                                                                          --
kusano fc6ab3
--              Copyright (C) 2002-2004 Dmitriy Anisimkov                   --
kusano fc6ab3
--                                                                          --
kusano fc6ab3
--  This library is free software; you can redistribute it and/or modify    --
kusano fc6ab3
--  it under the terms of the GNU General Public License as published by    --
kusano fc6ab3
--  the Free Software Foundation; either version 2 of the License, or (at   --
kusano fc6ab3
--  your option) any later version.                                         --
kusano fc6ab3
--                                                                          --
kusano fc6ab3
--  This library is distributed in the hope that it will be useful, but     --
kusano fc6ab3
--  WITHOUT ANY WARRANTY; without even the implied warranty of              --
kusano fc6ab3
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       --
kusano fc6ab3
--  General Public License for more details.                                --
kusano fc6ab3
--                                                                          --
kusano fc6ab3
--  You should have received a copy of the GNU General Public License       --
kusano fc6ab3
--  along with this library; if not, write to the Free Software Foundation, --
kusano fc6ab3
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
kusano fc6ab3
--                                                                          --
kusano fc6ab3
--  As a special exception, if other files instantiate generics from this   --
kusano fc6ab3
--  unit, or you link this unit with other files to produce an executable,  --
kusano fc6ab3
--  this  unit  does not  by itself cause  the resulting executable to be   --
kusano fc6ab3
--  covered by the GNU General Public License. This exception does not      --
kusano fc6ab3
--  however invalidate any other reasons why the executable file  might be  --
kusano fc6ab3
--  covered by the  GNU Public License.                                     --
kusano fc6ab3
------------------------------------------------------------------------------
kusano fc6ab3
kusano fc6ab3
--  $Id: zlib.ads,v 1.26 2004/09/06 06:53:19 vagul Exp $
kusano fc6ab3
kusano fc6ab3
with Ada.Streams;
kusano fc6ab3
kusano fc6ab3
with Interfaces;
kusano fc6ab3
kusano fc6ab3
package ZLib is
kusano fc6ab3
kusano fc6ab3
   ZLib_Error   : exception;
kusano fc6ab3
   Status_Error : exception;
kusano fc6ab3
kusano fc6ab3
   type Compression_Level is new Integer range -1 .. 9;
kusano fc6ab3
kusano fc6ab3
   type Flush_Mode is private;
kusano fc6ab3
kusano fc6ab3
   type Compression_Method is private;
kusano fc6ab3
kusano fc6ab3
   type Window_Bits_Type is new Integer range 8 .. 15;
kusano fc6ab3
kusano fc6ab3
   type Memory_Level_Type is new Integer range 1 .. 9;
kusano fc6ab3
kusano fc6ab3
   type Unsigned_32 is new Interfaces.Unsigned_32;
kusano fc6ab3
kusano fc6ab3
   type Strategy_Type is private;
kusano fc6ab3
kusano fc6ab3
   type Header_Type is (None, Auto, Default, GZip);
kusano fc6ab3
   --  Header type usage have a some limitation for inflate.
kusano fc6ab3
   --  See comment for Inflate_Init.
kusano fc6ab3
kusano fc6ab3
   subtype Count is Ada.Streams.Stream_Element_Count;
kusano fc6ab3
kusano fc6ab3
   Default_Memory_Level : constant Memory_Level_Type := 8;
kusano fc6ab3
   Default_Window_Bits  : constant Window_Bits_Type  := 15;
kusano fc6ab3
kusano fc6ab3
   ----------------------------------
kusano fc6ab3
   -- Compression method constants --
kusano fc6ab3
   ----------------------------------
kusano fc6ab3
kusano fc6ab3
   Deflated : constant Compression_Method;
kusano fc6ab3
   --  Only one method allowed in this ZLib version
kusano fc6ab3
kusano fc6ab3
   ---------------------------------
kusano fc6ab3
   -- Compression level constants --
kusano fc6ab3
   ---------------------------------
kusano fc6ab3
kusano fc6ab3
   No_Compression      : constant Compression_Level := 0;
kusano fc6ab3
   Best_Speed          : constant Compression_Level := 1;
kusano fc6ab3
   Best_Compression    : constant Compression_Level := 9;
kusano fc6ab3
   Default_Compression : constant Compression_Level := -1;
kusano fc6ab3
kusano fc6ab3
   --------------------------
kusano fc6ab3
   -- Flush mode constants --
kusano fc6ab3
   --------------------------
kusano fc6ab3
kusano fc6ab3
   No_Flush      : constant Flush_Mode;
kusano fc6ab3
   --  Regular way for compression, no flush
kusano fc6ab3
kusano fc6ab3
   Partial_Flush : constant Flush_Mode;
kusano fc6ab3
   --  Will be removed, use Z_SYNC_FLUSH instead
kusano fc6ab3
kusano fc6ab3
   Sync_Flush    : constant Flush_Mode;
kusano fc6ab3
   --  All pending output is flushed to the output buffer and the output
kusano fc6ab3
   --  is aligned on a byte boundary, so that the decompressor can get all
kusano fc6ab3
   --  input data available so far. (In particular avail_in is zero after the
kusano fc6ab3
   --  call if enough output space has been provided  before the call.)
kusano fc6ab3
   --  Flushing may degrade compression for some compression algorithms and so
kusano fc6ab3
   --  it should be used only when necessary.
kusano fc6ab3
kusano fc6ab3
   Block_Flush   : constant Flush_Mode;
kusano fc6ab3
   --  Z_BLOCK requests that inflate() stop
kusano fc6ab3
   --  if and when it get to the next deflate block boundary. When decoding the
kusano fc6ab3
   --  zlib or gzip format, this will cause inflate() to return immediately
kusano fc6ab3
   --  after the header and before the first block. When doing a raw inflate,
kusano fc6ab3
   --  inflate() will go ahead and process the first block, and will return
kusano fc6ab3
   --  when it gets to the end of that block, or when it runs out of data.
kusano fc6ab3
kusano fc6ab3
   Full_Flush    : constant Flush_Mode;
kusano fc6ab3
   --  All output is flushed as with SYNC_FLUSH, and the compression state
kusano fc6ab3
   --  is reset so that decompression can restart from this point if previous
kusano fc6ab3
   --  compressed data has been damaged or if random access is desired. Using
kusano fc6ab3
   --  Full_Flush too often can seriously degrade the compression.
kusano fc6ab3
kusano fc6ab3
   Finish        : constant Flush_Mode;
kusano fc6ab3
   --  Just for tell the compressor that input data is complete.
kusano fc6ab3
kusano fc6ab3
   ------------------------------------
kusano fc6ab3
   -- Compression strategy constants --
kusano fc6ab3
   ------------------------------------
kusano fc6ab3
kusano fc6ab3
   --  RLE stategy could be used only in version 1.2.0 and later.
kusano fc6ab3
kusano fc6ab3
   Filtered         : constant Strategy_Type;
kusano fc6ab3
   Huffman_Only     : constant Strategy_Type;
kusano fc6ab3
   RLE              : constant Strategy_Type;
kusano fc6ab3
   Default_Strategy : constant Strategy_Type;
kusano fc6ab3
kusano fc6ab3
   Default_Buffer_Size : constant := 4096;
kusano fc6ab3
kusano fc6ab3
   type Filter_Type is tagged limited private;
kusano fc6ab3
   --  The filter is for compression and for decompression.
kusano fc6ab3
   --  The usage of the type is depend of its initialization.
kusano fc6ab3
kusano fc6ab3
   function Version return String;
kusano fc6ab3
   pragma Inline (Version);
kusano fc6ab3
   --  Return string representation of the ZLib version.
kusano fc6ab3
kusano fc6ab3
   procedure Deflate_Init
kusano fc6ab3
     (Filter       : in out Filter_Type;
kusano fc6ab3
      Level        : in     Compression_Level  := Default_Compression;
kusano fc6ab3
      Strategy     : in     Strategy_Type      := Default_Strategy;
kusano fc6ab3
      Method       : in     Compression_Method := Deflated;
kusano fc6ab3
      Window_Bits  : in     Window_Bits_Type   := Default_Window_Bits;
kusano fc6ab3
      Memory_Level : in     Memory_Level_Type  := Default_Memory_Level;
kusano fc6ab3
      Header       : in     Header_Type        := Default);
kusano fc6ab3
   --  Compressor initialization.
kusano fc6ab3
   --  When Header parameter is Auto or Default, then default zlib header
kusano fc6ab3
   --  would be provided for compressed data.
kusano fc6ab3
   --  When Header is GZip, then gzip header would be set instead of
kusano fc6ab3
   --  default header.
kusano fc6ab3
   --  When Header is None, no header would be set for compressed data.
kusano fc6ab3
kusano fc6ab3
   procedure Inflate_Init
kusano fc6ab3
     (Filter      : in out Filter_Type;
kusano fc6ab3
      Window_Bits : in     Window_Bits_Type := Default_Window_Bits;
kusano fc6ab3
      Header      : in     Header_Type      := Default);
kusano fc6ab3
   --  Decompressor initialization.
kusano fc6ab3
   --  Default header type mean that ZLib default header is expecting in the
kusano fc6ab3
   --  input compressed stream.
kusano fc6ab3
   --  Header type None mean that no header is expecting in the input stream.
kusano fc6ab3
   --  GZip header type mean that GZip header is expecting in the
kusano fc6ab3
   --  input compressed stream.
kusano fc6ab3
   --  Auto header type mean that header type (GZip or Native) would be
kusano fc6ab3
   --  detected automatically in the input stream.
kusano fc6ab3
   --  Note that header types parameter values None, GZip and Auto are
kusano fc6ab3
   --  supported for inflate routine only in ZLib versions 1.2.0.2 and later.
kusano fc6ab3
   --  Deflate_Init is supporting all header types.
kusano fc6ab3
kusano fc6ab3
   function Is_Open (Filter : in Filter_Type) return Boolean;
kusano fc6ab3
   pragma Inline (Is_Open);
kusano fc6ab3
   --  Is the filter opened for compression or decompression.
kusano fc6ab3
kusano fc6ab3
   procedure Close
kusano fc6ab3
     (Filter       : in out Filter_Type;
kusano fc6ab3
      Ignore_Error : in     Boolean := False);
kusano fc6ab3
   --  Closing the compression or decompressor.
kusano fc6ab3
   --  If stream is closing before the complete and Ignore_Error is False,
kusano fc6ab3
   --  The exception would be raised.
kusano fc6ab3
kusano fc6ab3
   generic
kusano fc6ab3
      with procedure Data_In
kusano fc6ab3
        (Item : out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
         Last : out Ada.Streams.Stream_Element_Offset);
kusano fc6ab3
      with procedure Data_Out
kusano fc6ab3
        (Item : in Ada.Streams.Stream_Element_Array);
kusano fc6ab3
   procedure Generic_Translate
kusano fc6ab3
     (Filter          : in out Filter_Type;
kusano fc6ab3
      In_Buffer_Size  : in     Integer := Default_Buffer_Size;
kusano fc6ab3
      Out_Buffer_Size : in     Integer := Default_Buffer_Size);
kusano fc6ab3
   --  Compress/decompress data fetch from Data_In routine and pass the result
kusano fc6ab3
   --  to the Data_Out routine. User should provide Data_In and Data_Out
kusano fc6ab3
   --  for compression/decompression data flow.
kusano fc6ab3
   --  Compression or decompression depend on Filter initialization.
kusano fc6ab3
kusano fc6ab3
   function Total_In (Filter : in Filter_Type) return Count;
kusano fc6ab3
   pragma Inline (Total_In);
kusano fc6ab3
   --  Returns total number of input bytes read so far
kusano fc6ab3
kusano fc6ab3
   function Total_Out (Filter : in Filter_Type) return Count;
kusano fc6ab3
   pragma Inline (Total_Out);
kusano fc6ab3
   --  Returns total number of bytes output so far
kusano fc6ab3
kusano fc6ab3
   function CRC32
kusano fc6ab3
     (CRC    : in Unsigned_32;
kusano fc6ab3
      Data   : in Ada.Streams.Stream_Element_Array)
kusano fc6ab3
      return Unsigned_32;
kusano fc6ab3
   pragma Inline (CRC32);
kusano fc6ab3
   --  Compute CRC32, it could be necessary for make gzip format
kusano fc6ab3
kusano fc6ab3
   procedure CRC32
kusano fc6ab3
     (CRC  : in out Unsigned_32;
kusano fc6ab3
      Data : in     Ada.Streams.Stream_Element_Array);
kusano fc6ab3
   pragma Inline (CRC32);
kusano fc6ab3
   --  Compute CRC32, it could be necessary for make gzip format
kusano fc6ab3
kusano fc6ab3
   -------------------------------------------------
kusano fc6ab3
   --  Below is more complex low level routines.  --
kusano fc6ab3
   -------------------------------------------------
kusano fc6ab3
kusano fc6ab3
   procedure Translate
kusano fc6ab3
     (Filter    : in out Filter_Type;
kusano fc6ab3
      In_Data   : in     Ada.Streams.Stream_Element_Array;
kusano fc6ab3
      In_Last   :    out Ada.Streams.Stream_Element_Offset;
kusano fc6ab3
      Out_Data  :    out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
      Out_Last  :    out Ada.Streams.Stream_Element_Offset;
kusano fc6ab3
      Flush     : in     Flush_Mode);
kusano fc6ab3
   --  Compress/decompress the In_Data buffer and place the result into
kusano fc6ab3
   --  Out_Data. In_Last is the index of last element from In_Data accepted by
kusano fc6ab3
   --  the Filter. Out_Last is the last element of the received data from
kusano fc6ab3
   --  Filter. To tell the filter that incoming data are complete put the
kusano fc6ab3
   --  Flush parameter to Finish.
kusano fc6ab3
kusano fc6ab3
   function Stream_End (Filter : in Filter_Type) return Boolean;
kusano fc6ab3
   pragma Inline (Stream_End);
kusano fc6ab3
   --  Return the true when the stream is complete.
kusano fc6ab3
kusano fc6ab3
   procedure Flush
kusano fc6ab3
     (Filter    : in out Filter_Type;
kusano fc6ab3
      Out_Data  :    out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
      Out_Last  :    out Ada.Streams.Stream_Element_Offset;
kusano fc6ab3
      Flush     : in     Flush_Mode);
kusano fc6ab3
   pragma Inline (Flush);
kusano fc6ab3
   --  Flushing the data from the compressor.
kusano fc6ab3
kusano fc6ab3
   generic
kusano fc6ab3
      with procedure Write
kusano fc6ab3
        (Item : in Ada.Streams.Stream_Element_Array);
kusano fc6ab3
      --  User should provide this routine for accept
kusano fc6ab3
      --  compressed/decompressed data.
kusano fc6ab3
kusano fc6ab3
      Buffer_Size : in Ada.Streams.Stream_Element_Offset
kusano fc6ab3
         := Default_Buffer_Size;
kusano fc6ab3
      --  Buffer size for Write user routine.
kusano fc6ab3
kusano fc6ab3
   procedure Write
kusano fc6ab3
     (Filter  : in out Filter_Type;
kusano fc6ab3
      Item    : in     Ada.Streams.Stream_Element_Array;
kusano fc6ab3
      Flush   : in     Flush_Mode := No_Flush);
kusano fc6ab3
   --  Compress/Decompress data from Item to the generic parameter procedure
kusano fc6ab3
   --  Write. Output buffer size could be set in Buffer_Size generic parameter.
kusano fc6ab3
kusano fc6ab3
   generic
kusano fc6ab3
      with procedure Read
kusano fc6ab3
        (Item : out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
         Last : out Ada.Streams.Stream_Element_Offset);
kusano fc6ab3
      --  User should provide data for compression/decompression
kusano fc6ab3
      --  thru this routine.
kusano fc6ab3
kusano fc6ab3
      Buffer : in out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
      --  Buffer for keep remaining data from the previous
kusano fc6ab3
      --  back read.
kusano fc6ab3
kusano fc6ab3
      Rest_First, Rest_Last : in out Ada.Streams.Stream_Element_Offset;
kusano fc6ab3
      --  Rest_First have to be initialized to Buffer'Last + 1
kusano fc6ab3
      --  Rest_Last have to be initialized to Buffer'Last
kusano fc6ab3
      --  before usage.
kusano fc6ab3
kusano fc6ab3
      Allow_Read_Some : in Boolean := False;
kusano fc6ab3
      --  Is it allowed to return Last < Item'Last before end of data.
kusano fc6ab3
kusano fc6ab3
   procedure Read
kusano fc6ab3
     (Filter : in out Filter_Type;
kusano fc6ab3
      Item   :    out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
      Last   :    out Ada.Streams.Stream_Element_Offset;
kusano fc6ab3
      Flush  : in     Flush_Mode := No_Flush);
kusano fc6ab3
   --  Compress/Decompress data from generic parameter procedure Read to the
kusano fc6ab3
   --  Item. User should provide Buffer and initialized Rest_First, Rest_Last
kusano fc6ab3
   --  indicators. If Allow_Read_Some is True, Read routines could return
kusano fc6ab3
   --  Last < Item'Last only at end of stream.
kusano fc6ab3
kusano fc6ab3
private
kusano fc6ab3
kusano fc6ab3
   use Ada.Streams;
kusano fc6ab3
kusano fc6ab3
   pragma Assert (Ada.Streams.Stream_Element'Size    =    8);
kusano fc6ab3
   pragma Assert (Ada.Streams.Stream_Element'Modulus = 2**8);
kusano fc6ab3
kusano fc6ab3
   type Flush_Mode is new Integer range 0 .. 5;
kusano fc6ab3
kusano fc6ab3
   type Compression_Method is new Integer range 8 .. 8;
kusano fc6ab3
kusano fc6ab3
   type Strategy_Type is new Integer range 0 .. 3;
kusano fc6ab3
kusano fc6ab3
   No_Flush      : constant Flush_Mode := 0;
kusano fc6ab3
   Partial_Flush : constant Flush_Mode := 1;
kusano fc6ab3
   Sync_Flush    : constant Flush_Mode := 2;
kusano fc6ab3
   Full_Flush    : constant Flush_Mode := 3;
kusano fc6ab3
   Finish        : constant Flush_Mode := 4;
kusano fc6ab3
   Block_Flush   : constant Flush_Mode := 5;
kusano fc6ab3
kusano fc6ab3
   Filtered         : constant Strategy_Type := 1;
kusano fc6ab3
   Huffman_Only     : constant Strategy_Type := 2;
kusano fc6ab3
   RLE              : constant Strategy_Type := 3;
kusano fc6ab3
   Default_Strategy : constant Strategy_Type := 0;
kusano fc6ab3
kusano fc6ab3
   Deflated : constant Compression_Method := 8;
kusano fc6ab3
kusano fc6ab3
   type Z_Stream;
kusano fc6ab3
kusano fc6ab3
   type Z_Stream_Access is access all Z_Stream;
kusano fc6ab3
kusano fc6ab3
   type Filter_Type is tagged limited record
kusano fc6ab3
      Strm        : Z_Stream_Access;
kusano fc6ab3
      Compression : Boolean;
kusano fc6ab3
      Stream_End  : Boolean;
kusano fc6ab3
      Header      : Header_Type;
kusano fc6ab3
      CRC         : Unsigned_32;
kusano fc6ab3
      Offset      : Stream_Element_Offset;
kusano fc6ab3
      --  Offset for gzip header/footer output.
kusano fc6ab3
   end record;
kusano fc6ab3
kusano fc6ab3
end ZLib;