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