zbase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. unit zbase;
  2. { Original:
  3. zlib.h -- interface of the 'zlib' general purpose compression library
  4. version 1.1.0, Feb 24th, 1998
  5. Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
  6. This software is provided 'as-is', without any express or implied
  7. warranty. In no event will the authors be held liable for any damages
  8. arising from the use of this software.
  9. Permission is granted to anyone to use this software for any purpose,
  10. including commercial applications, and to alter it and redistribute it
  11. freely, subject to the following restrictions:
  12. 1. The origin of this software must not be misrepresented; you must not
  13. claim that you wrote the original software. If you use this software
  14. in a product, an acknowledgment in the product documentation would be
  15. appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be
  17. misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. Jean-loup Gailly Mark Adler
  20. [email protected] [email protected]
  21. The data format used by the zlib library is described by RFCs (Request for
  22. Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  23. (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  24. Pascal tranlastion
  25. Copyright (C) 1998 by Jacques Nomssi Nzali
  26. For conditions of distribution and use, see copyright notice in readme.txt
  27. }
  28. interface
  29. {$I zconf.inc}
  30. { zconf.h -- configuration of the zlib compression library }
  31. { zutil.c -- target dependent utility functions for the compression library }
  32. { The 'zlib' compression library provides in-memory compression and
  33. decompression functions, including integrity checks of the uncompressed
  34. data. This version of the library supports only one compression method
  35. (deflation) but other algorithms will be added later and will have the same
  36. stream interface.
  37. Compression can be done in a single step if the buffers are large
  38. enough (for example if an input file is mmap'ed), or can be done by
  39. repeated calls of the compression function. In the latter case, the
  40. application must provide more input and/or consume the output
  41. (providing more output space) before each call.
  42. The library also supports reading and writing files in gzip (.gz) format
  43. with an interface similar to that of stdio.
  44. The library does not install any signal handler. The decoder checks
  45. the consistency of the compressed data, so the library should never
  46. crash even in case of corrupted input. }
  47. { Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  48. than 64k bytes at a time (needed on systems with 16-bit integer). }
  49. { Maximum value for memLevel in deflateInit2 }
  50. {$ifdef MAXSEG_64K}
  51. {$IFDEF TP}
  52. const
  53. MAX_MEM_LEVEL = 7;
  54. DEF_MEM_LEVEL = MAX_MEM_LEVEL; { default memLevel }
  55. {$ELSE}
  56. const
  57. MAX_MEM_LEVEL = 8;
  58. DEF_MEM_LEVEL = MAX_MEM_LEVEL; { default memLevel }
  59. {$ENDIF}
  60. {$else}
  61. const
  62. MAX_MEM_LEVEL = 9;
  63. DEF_MEM_LEVEL = 8; { if MAX_MEM_LEVEL > 8 }
  64. {$endif}
  65. { Maximum value for windowBits in deflateInit2 and inflateInit2 }
  66. const
  67. {$IFDEF TP}
  68. MAX_WBITS = 14; { 32K LZ77 window }
  69. {$ELSE}
  70. MAX_WBITS = 15; { 32K LZ77 window }
  71. {$ENDIF}
  72. { default windowBits for decompression. MAX_WBITS is for compression only }
  73. const
  74. DEF_WBITS = MAX_WBITS;
  75. type Pbytearray=^Tbytearray;
  76. Pwordarray=^Twordarray;
  77. Pcardinalarray=^Tcardinalarray;
  78. Tbytearray = array [0..maxint div sizeof(byte)-1] of byte;
  79. Twordarray = array [0..maxint div sizeof(word)-1] of word;
  80. Tintegerarray = array [0..maxint div sizeof(integer)-1] of integer;
  81. Tcardinalarray = array [0..maxint div sizeof(cardinal)-1] of cardinal;
  82. { The memory requirements for deflate are (in bytes):
  83. 1 shl (windowBits+2) + 1 shl (memLevel+9)
  84. that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  85. plus a few kilobytes for small objects. For example, if you want to reduce
  86. the default memory requirements from 256K to 128K, compile with
  87. DMAX_WBITS=14 DMAX_MEM_LEVEL=7
  88. Of course this will generally degrade compression (there's no free lunch).
  89. The memory requirements for inflate are (in bytes) 1 shl windowBits
  90. that is, 32K for windowBits=15 (default value) plus a few kilobytes
  91. for small objects. }
  92. { Huffman code lookup table entry--this entry is four bytes for machines
  93. that have 16-bit pointers (e.g. PC's in the small or medium model). }
  94. type
  95. pInflate_huft = ^inflate_huft;
  96. inflate_huft = Record
  97. Exop, { number of extra bits or operation }
  98. bits : Byte; { number of bits in this code or subcode }
  99. {pad : cardinal;} { pad structure to a power of 2 (4 bytes for }
  100. { 16-bit, 8 bytes for 32-bit integer's) }
  101. base : cardinal; { literal, length base, or distance base }
  102. { or table offset }
  103. End;
  104. type
  105. huft_field = Array[0..(maxint div SizeOf(inflate_huft))-1] of inflate_huft;
  106. huft_ptr = ^huft_field;
  107. type
  108. ppInflate_huft = ^pInflate_huft;
  109. type
  110. inflate_codes_mode = ( { waiting for "i:"=input, "o:"=output, "x:"=nothing }
  111. START, { x: set up for LEN }
  112. LEN, { i: get length/literal/eob next }
  113. LENEXT, { i: getting length extra (have base) }
  114. DIST, { i: get distance next }
  115. DISTEXT, { i: getting distance extra }
  116. COPY, { o: copying bytes in window, waiting for space }
  117. LIT, { o: got literal, waiting for output space }
  118. WASH, { o: got eob, possibly still output waiting }
  119. ZEND, { x: got eob and all data flushed }
  120. BADCODE); { x: got error }
  121. { inflate codes private state }
  122. type
  123. pInflate_codes_state = ^inflate_codes_state;
  124. inflate_codes_state = record
  125. mode : inflate_codes_mode; { current inflate_codes mode }
  126. { mode dependent information }
  127. len : cardinal;
  128. sub : record { submode }
  129. Case Byte of
  130. 0:(code : record { if LEN or DIST, where in tree }
  131. tree : pInflate_huft; { pointer into tree }
  132. need : cardinal; { bits needed }
  133. end);
  134. 1:(lit : cardinal); { if LIT, literal }
  135. 2:(copy: record { if EXT or COPY, where and how much }
  136. get : cardinal; { bits to get for extra }
  137. dist : cardinal; { distance back to copy from }
  138. end);
  139. end;
  140. { mode independent information }
  141. lbits : Byte; { ltree bits decoded per branch }
  142. dbits : Byte; { dtree bits decoder per branch }
  143. ltree : pInflate_huft; { literal/length/eob tree }
  144. dtree : pInflate_huft; { distance tree }
  145. end;
  146. type
  147. check_func = function(check : cardinal;
  148. buf : Pbyte;
  149. {const buf : array of byte;}
  150. len : cardinal) : cardinal;
  151. type
  152. inflate_block_mode =
  153. (ZTYPE, { get type bits (3, including end bit) }
  154. LENS, { get lengths for stored }
  155. STORED, { processing stored block }
  156. TABLE, { get table lengths }
  157. BTREE, { get bit lengths tree for a dynamic block }
  158. DTREE, { get length, distance trees for a dynamic block }
  159. CODES, { processing fixed or dynamic block }
  160. DRY, { output remaining window bytes }
  161. BLKDONE, { finished last block, done }
  162. BLKBAD); { got a data error--stuck here }
  163. type
  164. pInflate_blocks_state = ^inflate_blocks_state;
  165. { inflate blocks semi-private state }
  166. inflate_blocks_state = record
  167. mode : inflate_block_mode; { current inflate_block mode }
  168. { mode dependent information }
  169. sub : record { submode }
  170. case Byte of
  171. 0:(left : cardinal); { if STORED, bytes left to copy }
  172. 1:(trees : record { if DTREE, decoding info for trees }
  173. table : cardinal; { table lengths (14 bits) }
  174. index : cardinal; { index into blens (or border) }
  175. blens : Pcardinalarray; { bit lengths of codes }
  176. bb : cardinal; { bit length tree depth }
  177. tb : pInflate_huft; { bit length decoding tree }
  178. end);
  179. 2:(decode : record { if CODES, current state }
  180. tl : pInflate_huft;
  181. td : pInflate_huft; { trees to free }
  182. codes : pInflate_codes_state;
  183. end);
  184. end;
  185. last : boolean; { true if this block is the last block }
  186. { mode independent information }
  187. bitk : cardinal; { bits in bit buffer }
  188. bitb : cardinal; { bit buffer }
  189. hufts : huft_ptr; {pInflate_huft;} { single malloc for tree space }
  190. window : Pbyte; { sliding window }
  191. zend : Pbyte; { one byte after sliding window }
  192. read : Pbyte; { window read pointer }
  193. write : Pbyte; { window write pointer }
  194. checkfn : check_func; { check function }
  195. check : cardinal; { check on output }
  196. end;
  197. type
  198. inflate_mode = (
  199. METHOD, { waiting for method byte }
  200. FLAG, { waiting for flag byte }
  201. DICT4, { four dictionary check bytes to go }
  202. DICT3, { three dictionary check bytes to go }
  203. DICT2, { two dictionary check bytes to go }
  204. DICT1, { one dictionary check byte to go }
  205. DICT0, { waiting for inflateSetDictionary }
  206. BLOCKS, { decompressing blocks }
  207. CHECK4, { four check bytes to go }
  208. CHECK3, { three check bytes to go }
  209. CHECK2, { two check bytes to go }
  210. CHECK1, { one check byte to go }
  211. DONE, { finished check, done }
  212. BAD); { got an error--stay here }
  213. { inflate private state }
  214. type
  215. pInternal_state = ^internal_state; { or point to a deflate_state record }
  216. internal_state = record
  217. mode : inflate_mode; { current inflate mode }
  218. { mode dependent information }
  219. sub : record { submode }
  220. case byte of
  221. 0:(method : cardinal); { if FLAGS, method byte }
  222. 1:(check : record { if CHECK, check values to compare }
  223. was : cardinal; { computed check value }
  224. need : cardinal; { stream check value }
  225. end);
  226. 2:(marker : cardinal); { if BAD, inflateSync's marker bytes count }
  227. end;
  228. { mode independent information }
  229. nowrap : boolean; { flag for no wrapper }
  230. wbits : cardinal; { log2(window size) (8..15, defaults to 15) }
  231. blocks : pInflate_blocks_state; { current inflate_blocks state }
  232. end;
  233. type
  234. z_streamp = ^z_stream;
  235. z_stream = record
  236. next_in : Pbyte; { next input byte }
  237. avail_in : cardinal; { number of bytes available at next_in }
  238. total_in : cardinal; { total nb of input bytes read so far }
  239. next_out : Pbyte; { next output byte should be put there }
  240. avail_out : cardinal; { remaining free space at next_out }
  241. total_out : cardinal; { total nb of bytes output so far }
  242. msg : string[255]; { last error message, '' if no error }
  243. state : pInternal_state; { not visible by applications }
  244. data_type : integer; { best guess about the data type: ascii or binary }
  245. adler : cardinal; { adler32 value of the uncompressed data }
  246. reserved : cardinal; { reserved for future use }
  247. end;
  248. { The application must update next_in and avail_in when avail_in has
  249. dropped to zero. It must update next_out and avail_out when avail_out
  250. has dropped to zero. The application must initialize zalloc, zfree and
  251. opaque before calling the init function. All other fields are set by the
  252. compression library and must not be updated by the application.
  253. The fields total_in and total_out can be used for statistics or
  254. progress reports. After compression, total_in holds the total size of
  255. the uncompressed data and may be saved for use in the decompressor
  256. (particularly if the decompressor wants to decompress everything in
  257. a single step). }
  258. const { constants }
  259. Z_NO_FLUSH = 0;
  260. Z_PARTIAL_FLUSH = 1;
  261. Z_SYNC_FLUSH = 2;
  262. Z_FULL_FLUSH = 3;
  263. Z_FINISH = 4;
  264. { Allowed flush values; see deflate() below for details }
  265. Z_OK = 0;
  266. Z_STREAM_END = 1;
  267. Z_NEED_DICT = 2;
  268. Z_ERRNO = (-1);
  269. Z_STREAM_ERROR = (-2);
  270. Z_DATA_ERROR = (-3);
  271. Z_MEM_ERROR = (-4);
  272. Z_BUF_ERROR = (-5);
  273. Z_VERSION_ERROR = (-6);
  274. { Return codes for the compression/decompression functions. Negative
  275. values are errors, positive values are used for special but normal events.}
  276. Z_NO_COMPRESSION = 0;
  277. Z_BEST_SPEED = 1;
  278. Z_BEST_COMPRESSION = 9;
  279. Z_DEFAULT_COMPRESSION = (-1);
  280. { compression levels }
  281. Z_FILTERED = 1;
  282. Z_HUFFMAN_ONLY = 2;
  283. Z_DEFAULT_STRATEGY = 0;
  284. { compression strategy; see deflateInit2() below for details }
  285. Z_BINARY = 0;
  286. Z_ASCII = 1;
  287. Z_UNKNOWN = 2;
  288. { Possible values of the data_type field }
  289. Z_DEFLATED = 8;
  290. { The deflate compression method (the only one supported in this version) }
  291. {$IFDEF GZIO}
  292. var
  293. errno : integer;
  294. {$ENDIF}
  295. { common constants }
  296. { The three kinds of block type }
  297. const
  298. STORED_BLOCK = 0;
  299. STATIC_TREES = 1;
  300. DYN_TREES = 2;
  301. { The minimum and maximum match lengths }
  302. const
  303. MIN_MATCH = 3;
  304. {$ifdef MAX_MATCH_IS_258}
  305. MAX_MATCH = 258;
  306. {$else}
  307. MAX_MATCH = ??; { deliberate syntax error }
  308. {$endif}
  309. const
  310. PRESET_DICT = $20; { preset dictionary flag in zlib header }
  311. {$IFDEF ZLIB_DEBUG}
  312. procedure Assert(cond : boolean; msg : string);
  313. {$ENDIF}
  314. procedure Trace(x : string);
  315. procedure Tracev(x : string);
  316. procedure Tracevv(x : string);
  317. procedure Tracevvv(x : string);
  318. procedure Tracec(c : boolean; x : string);
  319. procedure Tracecv(c : boolean; x : string);
  320. function zlibVersion : string;
  321. { The application can compare zlibVersion and ZLIB_VERSION for consistency.
  322. If the first character differs, the library code actually used is
  323. not compatible with the zlib.h header file used by the application.
  324. This check is automatically made by deflateInit and inflateInit. }
  325. function zError(err : integer) : string;
  326. const
  327. ZLIB_VERSION : string[10] = '1.1.2';
  328. const
  329. z_errbase = Z_NEED_DICT;
  330. z_errmsg : Array[0..9] of string[21] = { indexed by 2-zlib_error }
  331. ('need dictionary', { Z_NEED_DICT 2 }
  332. 'stream end', { Z_STREAM_END 1 }
  333. '', { Z_OK 0 }
  334. 'file error', { Z_ERRNO (-1) }
  335. 'stream error', { Z_STREAM_ERROR (-2) }
  336. 'data error', { Z_DATA_ERROR (-3) }
  337. 'insufficient memory', { Z_MEM_ERROR (-4) }
  338. 'buffer error', { Z_BUF_ERROR (-5) }
  339. 'incompatible version',{ Z_VERSION_ERROR (-6) }
  340. '');
  341. const
  342. z_verbose : integer = 1;
  343. {$IFDEF ZLIB_DEBUG}
  344. procedure z_error (m : string);
  345. {$ENDIF}
  346. implementation
  347. function zError(err : integer) : string;
  348. begin
  349. zError := z_errmsg[Z_NEED_DICT-err];
  350. end;
  351. function zlibVersion : string;
  352. begin
  353. zlibVersion := ZLIB_VERSION;
  354. end;
  355. procedure z_error (m : string);
  356. begin
  357. WriteLn(output, m);
  358. Write('Zlib - Halt...');
  359. ReadLn;
  360. Halt(1);
  361. end;
  362. procedure Assert(cond : boolean; msg : string);
  363. begin
  364. if not cond then
  365. z_error(msg);
  366. end;
  367. procedure Trace(x : string);
  368. begin
  369. WriteLn(x);
  370. end;
  371. procedure Tracev(x : string);
  372. begin
  373. if (z_verbose>0) then
  374. WriteLn(x);
  375. end;
  376. procedure Tracevv(x : string);
  377. begin
  378. if (z_verbose>1) then
  379. WriteLn(x);
  380. end;
  381. procedure Tracevvv(x : string);
  382. begin
  383. if (z_verbose>2) then
  384. WriteLn(x);
  385. end;
  386. procedure Tracec(c : boolean; x : string);
  387. begin
  388. if (z_verbose>0) and (c) then
  389. WriteLn(x);
  390. end;
  391. procedure Tracecv(c : boolean; x : string);
  392. begin
  393. if (z_verbose>1) and c then
  394. WriteLn(x);
  395. end;
  396. end.