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