zlib.ads 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ------------------------------------------------------------------------------
  2. -- ZLib for Ada thick binding. --
  3. -- --
  4. -- Copyright (C) 2002-2004 Dmitriy Anisimkov --
  5. -- --
  6. -- This library is free software; you can redistribute it and/or modify --
  7. -- it under the terms of the GNU General Public License as published by --
  8. -- the Free Software Foundation; either version 2 of the License, or (at --
  9. -- your option) any later version. --
  10. -- --
  11. -- This library is distributed in the hope that it will be useful, but --
  12. -- WITHOUT ANY WARRANTY; without even the implied warranty of --
  13. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
  14. -- General Public License for more details. --
  15. -- --
  16. -- You should have received a copy of the GNU General Public License --
  17. -- along with this library; if not, write to the Free Software Foundation, --
  18. -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
  19. -- --
  20. -- As a special exception, if other files instantiate generics from this --
  21. -- unit, or you link this unit with other files to produce an executable, --
  22. -- this unit does not by itself cause the resulting executable to be --
  23. -- covered by the GNU General Public License. This exception does not --
  24. -- however invalidate any other reasons why the executable file might be --
  25. -- covered by the GNU Public License. --
  26. ------------------------------------------------------------------------------
  27. -- $Id: zlib.ads,v 1.26 2004/09/06 06:53:19 vagul Exp $
  28. with Ada.Streams;
  29. with Interfaces;
  30. package ZLib is
  31. ZLib_Error : exception;
  32. Status_Error : exception;
  33. type Compression_Level is new Integer range -1 .. 9;
  34. type Flush_Mode is private;
  35. type Compression_Method is private;
  36. type Window_Bits_Type is new Integer range 8 .. 15;
  37. type Memory_Level_Type is new Integer range 1 .. 9;
  38. type Unsigned_32 is new Interfaces.Unsigned_32;
  39. type Strategy_Type is private;
  40. type Header_Type is (None, Auto, Default, GZip);
  41. -- Header type usage have a some limitation for inflate.
  42. -- See comment for Inflate_Init.
  43. subtype Count is Ada.Streams.Stream_Element_Count;
  44. Default_Memory_Level : constant Memory_Level_Type := 8;
  45. Default_Window_Bits : constant Window_Bits_Type := 15;
  46. ----------------------------------
  47. -- Compression method constants --
  48. ----------------------------------
  49. Deflated : constant Compression_Method;
  50. -- Only one method allowed in this ZLib version
  51. ---------------------------------
  52. -- Compression level constants --
  53. ---------------------------------
  54. No_Compression : constant Compression_Level := 0;
  55. Best_Speed : constant Compression_Level := 1;
  56. Best_Compression : constant Compression_Level := 9;
  57. Default_Compression : constant Compression_Level := -1;
  58. --------------------------
  59. -- Flush mode constants --
  60. --------------------------
  61. No_Flush : constant Flush_Mode;
  62. -- Regular way for compression, no flush
  63. Partial_Flush : constant Flush_Mode;
  64. -- Will be removed, use Z_SYNC_FLUSH instead
  65. Sync_Flush : constant Flush_Mode;
  66. -- All pending output is flushed to the output buffer and the output
  67. -- is aligned on a byte boundary, so that the decompressor can get all
  68. -- input data available so far. (In particular avail_in is zero after the
  69. -- call if enough output space has been provided before the call.)
  70. -- Flushing may degrade compression for some compression algorithms and so
  71. -- it should be used only when necessary.
  72. Block_Flush : constant Flush_Mode;
  73. -- Z_BLOCK requests that inflate() stop
  74. -- if and when it get to the next deflate block boundary. When decoding the
  75. -- zlib or gzip format, this will cause inflate() to return immediately
  76. -- after the header and before the first block. When doing a raw inflate,
  77. -- inflate() will go ahead and process the first block, and will return
  78. -- when it gets to the end of that block, or when it runs out of data.
  79. Full_Flush : constant Flush_Mode;
  80. -- All output is flushed as with SYNC_FLUSH, and the compression state
  81. -- is reset so that decompression can restart from this point if previous
  82. -- compressed data has been damaged or if random access is desired. Using
  83. -- Full_Flush too often can seriously degrade the compression.
  84. Finish : constant Flush_Mode;
  85. -- Just for tell the compressor that input data is complete.
  86. ------------------------------------
  87. -- Compression strategy constants --
  88. ------------------------------------
  89. -- RLE stategy could be used only in version 1.2.0 and later.
  90. Filtered : constant Strategy_Type;
  91. Huffman_Only : constant Strategy_Type;
  92. RLE : constant Strategy_Type;
  93. Default_Strategy : constant Strategy_Type;
  94. Default_Buffer_Size : constant := 4096;
  95. type Filter_Type is tagged limited private;
  96. -- The filter is for compression and for decompression.
  97. -- The usage of the type is depend of its initialization.
  98. function Version return String;
  99. pragma Inline (Version);
  100. -- Return string representation of the ZLib version.
  101. procedure Deflate_Init
  102. (Filter : in out Filter_Type;
  103. Level : in Compression_Level := Default_Compression;
  104. Strategy : in Strategy_Type := Default_Strategy;
  105. Method : in Compression_Method := Deflated;
  106. Window_Bits : in Window_Bits_Type := Default_Window_Bits;
  107. Memory_Level : in Memory_Level_Type := Default_Memory_Level;
  108. Header : in Header_Type := Default);
  109. -- Compressor initialization.
  110. -- When Header parameter is Auto or Default, then default zlib header
  111. -- would be provided for compressed data.
  112. -- When Header is GZip, then gzip header would be set instead of
  113. -- default header.
  114. -- When Header is None, no header would be set for compressed data.
  115. procedure Inflate_Init
  116. (Filter : in out Filter_Type;
  117. Window_Bits : in Window_Bits_Type := Default_Window_Bits;
  118. Header : in Header_Type := Default);
  119. -- Decompressor initialization.
  120. -- Default header type mean that ZLib default header is expecting in the
  121. -- input compressed stream.
  122. -- Header type None mean that no header is expecting in the input stream.
  123. -- GZip header type mean that GZip header is expecting in the
  124. -- input compressed stream.
  125. -- Auto header type mean that header type (GZip or Native) would be
  126. -- detected automatically in the input stream.
  127. -- Note that header types parameter values None, GZip and Auto are
  128. -- supported for inflate routine only in ZLib versions 1.2.0.2 and later.
  129. -- Deflate_Init is supporting all header types.
  130. function Is_Open (Filter : in Filter_Type) return Boolean;
  131. pragma Inline (Is_Open);
  132. -- Is the filter opened for compression or decompression.
  133. procedure Close
  134. (Filter : in out Filter_Type;
  135. Ignore_Error : in Boolean := False);
  136. -- Closing the compression or decompressor.
  137. -- If stream is closing before the complete and Ignore_Error is False,
  138. -- The exception would be raised.
  139. generic
  140. with procedure Data_In
  141. (Item : out Ada.Streams.Stream_Element_Array;
  142. Last : out Ada.Streams.Stream_Element_Offset);
  143. with procedure Data_Out
  144. (Item : in Ada.Streams.Stream_Element_Array);
  145. procedure Generic_Translate
  146. (Filter : in out Filter_Type;
  147. In_Buffer_Size : in Integer := Default_Buffer_Size;
  148. Out_Buffer_Size : in Integer := Default_Buffer_Size);
  149. -- Compress/decompress data fetch from Data_In routine and pass the result
  150. -- to the Data_Out routine. User should provide Data_In and Data_Out
  151. -- for compression/decompression data flow.
  152. -- Compression or decompression depend on Filter initialization.
  153. function Total_In (Filter : in Filter_Type) return Count;
  154. pragma Inline (Total_In);
  155. -- Returns total number of input bytes read so far
  156. function Total_Out (Filter : in Filter_Type) return Count;
  157. pragma Inline (Total_Out);
  158. -- Returns total number of bytes output so far
  159. function CRC32
  160. (CRC : in Unsigned_32;
  161. Data : in Ada.Streams.Stream_Element_Array)
  162. return Unsigned_32;
  163. pragma Inline (CRC32);
  164. -- Compute CRC32, it could be necessary for make gzip format
  165. procedure CRC32
  166. (CRC : in out Unsigned_32;
  167. Data : in Ada.Streams.Stream_Element_Array);
  168. pragma Inline (CRC32);
  169. -- Compute CRC32, it could be necessary for make gzip format
  170. -------------------------------------------------
  171. -- Below is more complex low level routines. --
  172. -------------------------------------------------
  173. procedure Translate
  174. (Filter : in out Filter_Type;
  175. In_Data : in Ada.Streams.Stream_Element_Array;
  176. In_Last : out Ada.Streams.Stream_Element_Offset;
  177. Out_Data : out Ada.Streams.Stream_Element_Array;
  178. Out_Last : out Ada.Streams.Stream_Element_Offset;
  179. Flush : in Flush_Mode);
  180. -- Compress/decompress the In_Data buffer and place the result into
  181. -- Out_Data. In_Last is the index of last element from In_Data accepted by
  182. -- the Filter. Out_Last is the last element of the received data from
  183. -- Filter. To tell the filter that incoming data are complete put the
  184. -- Flush parameter to Finish.
  185. function Stream_End (Filter : in Filter_Type) return Boolean;
  186. pragma Inline (Stream_End);
  187. -- Return the true when the stream is complete.
  188. procedure Flush
  189. (Filter : in out Filter_Type;
  190. Out_Data : out Ada.Streams.Stream_Element_Array;
  191. Out_Last : out Ada.Streams.Stream_Element_Offset;
  192. Flush : in Flush_Mode);
  193. pragma Inline (Flush);
  194. -- Flushing the data from the compressor.
  195. generic
  196. with procedure Write
  197. (Item : in Ada.Streams.Stream_Element_Array);
  198. -- User should provide this routine for accept
  199. -- compressed/decompressed data.
  200. Buffer_Size : in Ada.Streams.Stream_Element_Offset
  201. := Default_Buffer_Size;
  202. -- Buffer size for Write user routine.
  203. procedure Write
  204. (Filter : in out Filter_Type;
  205. Item : in Ada.Streams.Stream_Element_Array;
  206. Flush : in Flush_Mode := No_Flush);
  207. -- Compress/Decompress data from Item to the generic parameter procedure
  208. -- Write. Output buffer size could be set in Buffer_Size generic parameter.
  209. generic
  210. with procedure Read
  211. (Item : out Ada.Streams.Stream_Element_Array;
  212. Last : out Ada.Streams.Stream_Element_Offset);
  213. -- User should provide data for compression/decompression
  214. -- thru this routine.
  215. Buffer : in out Ada.Streams.Stream_Element_Array;
  216. -- Buffer for keep remaining data from the previous
  217. -- back read.
  218. Rest_First, Rest_Last : in out Ada.Streams.Stream_Element_Offset;
  219. -- Rest_First have to be initialized to Buffer'Last + 1
  220. -- Rest_Last have to be initialized to Buffer'Last
  221. -- before usage.
  222. Allow_Read_Some : in Boolean := False;
  223. -- Is it allowed to return Last < Item'Last before end of data.
  224. procedure Read
  225. (Filter : in out Filter_Type;
  226. Item : out Ada.Streams.Stream_Element_Array;
  227. Last : out Ada.Streams.Stream_Element_Offset;
  228. Flush : in Flush_Mode := No_Flush);
  229. -- Compress/Decompress data from generic parameter procedure Read to the
  230. -- Item. User should provide Buffer and initialized Rest_First, Rest_Last
  231. -- indicators. If Allow_Read_Some is True, Read routines could return
  232. -- Last < Item'Last only at end of stream.
  233. private
  234. use Ada.Streams;
  235. pragma Assert (Ada.Streams.Stream_Element'Size = 8);
  236. pragma Assert (Ada.Streams.Stream_Element'Modulus = 2**8);
  237. type Flush_Mode is new Integer range 0 .. 5;
  238. type Compression_Method is new Integer range 8 .. 8;
  239. type Strategy_Type is new Integer range 0 .. 3;
  240. No_Flush : constant Flush_Mode := 0;
  241. Partial_Flush : constant Flush_Mode := 1;
  242. Sync_Flush : constant Flush_Mode := 2;
  243. Full_Flush : constant Flush_Mode := 3;
  244. Finish : constant Flush_Mode := 4;
  245. Block_Flush : constant Flush_Mode := 5;
  246. Filtered : constant Strategy_Type := 1;
  247. Huffman_Only : constant Strategy_Type := 2;
  248. RLE : constant Strategy_Type := 3;
  249. Default_Strategy : constant Strategy_Type := 0;
  250. Deflated : constant Compression_Method := 8;
  251. type Z_Stream;
  252. type Z_Stream_Access is access all Z_Stream;
  253. type Filter_Type is tagged limited record
  254. Strm : Z_Stream_Access;
  255. Compression : Boolean;
  256. Stream_End : Boolean;
  257. Header : Header_Type;
  258. CRC : Unsigned_32;
  259. Offset : Stream_Element_Offset;
  260. -- Offset for gzip header/footer output.
  261. end record;
  262. end ZLib;