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