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