zlib.pp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. {$ifndef NO_SMART_LINK}
  2. {$smartlink on}
  3. {$endif}
  4. unit zlib;
  5. interface
  6. { Needed for array of const }
  7. {$mode objfpc}
  8. { for linux for linking with libc }
  9. {$ifdef unix}
  10. {$linklib c}
  11. {$endif}
  12. {$packrecords c}
  13. uses
  14. ctypes;
  15. const
  16. {$ifdef netware} {zlib.nlm comes with netware6}
  17. libz='zlib';
  18. {$else}
  19. {$ifdef windows}
  20. libz='zlib1';
  21. {$else windows}
  22. libz='z';
  23. {$endif windows}
  24. {$endif}
  25. {$ifndef windows}
  26. {$linklib libz}
  27. {$endif windows}
  28. (*
  29. The 'zlib' compression library provides in-memory compression and
  30. decompression functions, including integrity checks of the uncompressed
  31. data. This version of the library supports only one compression method
  32. (deflation) but other algorithms will be added later and will have the same
  33. stream interface.
  34. Compression can be done in a single step if the buffers are large
  35. enough (for example if an input file is mmap'ed), or can be done by
  36. repeated calls of the compression function. In the latter case, the
  37. application must provide more input and/or consume the output
  38. (providing more output space) before each call.
  39. The compressed data format used by default by the in-memory functions is
  40. the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
  41. around a deflate stream, which is itself documented in RFC 1951.
  42. The library also supports reading and writing files in gzip (.gz) format
  43. with an interface similar to that of stdio using the functions that start
  44. with "gz". The gzip format is different from the zlib format. gzip is a
  45. gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
  46. This library can optionally read and write gzip streams in memory as well.
  47. The zlib format was designed to be compact and fast for use in memory
  48. and on communications channels. The gzip format was designed for single-
  49. file compression on file systems, has a larger header than zlib to maintain
  50. directory information, and uses a different, slower check method than zlib.
  51. The library does not install any signal handler. The decoder checks
  52. the consistency of the compressed data, so the library should never
  53. crash even in case of corrupted input.
  54. *)
  55. const
  56. ZLIB_VERSION = '1.2.3';
  57. ZLIB_VERNUM = $1230;
  58. type
  59. uInt = cuint;
  60. puIntf = ^uIntf;
  61. uIntf = uInt;
  62. uLong = culong;
  63. puLongf = ^uLongf;
  64. uLongf = uLong;
  65. pBytef = ^Bytef;
  66. Bytef = cchar;
  67. alloc_func = function(opaque: pointer; items: uInt; size: uInt): pointer; cdecl;
  68. TAllocfunc = alloc_func;
  69. free_func = procedure(opaque: pointer; items: uInt; size: uInt); cdecl;
  70. TFreeFunc = free_func;
  71. internal_statep = ^internal_state;
  72. internal_state = record end;
  73. TInternalState = internal_state;
  74. PInternalState = internal_statep;
  75. z_off_t = coff_t;
  76. z_streamp = ^z_stream;
  77. z_stream = record
  78. next_in : pBytef; // next input byte
  79. avail_in : uInt; // number of bytes available at next_in
  80. total_in : uLong; // total nb of input bytes read so far
  81. next_out : pBytef; // next output byte should be put there
  82. avail_out : uInt; // remaining free space at next_out
  83. total_out : uLong; // total nb of bytes output so far
  84. msg : pBytef; // last error message, NULL if no error
  85. state : internal_statep; // not visible by applications
  86. zalloc : alloc_func; // used to allocate the internal state
  87. zfree : free_func; // used to free the internal state
  88. opaque : pointer; // private data object passed to zalloc and zfree
  89. data_type : cint; // best guess about the data type: binary or text
  90. adler : uLong; // adler32 value of the uncompressed data
  91. reserved : uLong; // reserved for future use
  92. end;
  93. TZStreamRec = z_stream;
  94. TZStream = z_stream;
  95. PZStream = z_streamp;
  96. (*
  97. gzip header information passed to and from zlib routines. See RFC 1952
  98. for more details on the meanings of these fields.
  99. *)
  100. gz_headerp = ^gz_header;
  101. gz_header = record
  102. text : cint; // true if compressed data believed to be text
  103. time : uLong; // modification time
  104. xflags : cint; // extra flags (not used when writing a gzip file)
  105. os : cint; // operating system
  106. extra : pBytef; // pointer to extra field or Z_NULL if none
  107. extra_len : uInt; // extra field length (valid if extra != Z_NULL)
  108. extra_max : uInt; // space at extra (only when reading header)
  109. name : pBytef; // pointer to zero-terminated file name or Z_NULL
  110. name_max : uInt; // space at name (only when reading header)
  111. comment : Bytef; // pointer to zero-terminated comment or Z_NULL
  112. comm_max : uInt; // space at comment (only when reading header)
  113. hcrc : cint; // true if there was or will be a header crc
  114. done : cint; // true when done reading gzip header (not used when writing a gzip file)
  115. end;
  116. (*
  117. The application must update next_in and avail_in when avail_in has
  118. dropped to zero. It must update next_out and avail_out when avail_out
  119. has dropped to zero. The application must initialize zalloc, zfree and
  120. opaque before calling the init function. All other fields are set by the
  121. compression library and must not be updated by the application.
  122. The opaque value provided by the application will be passed as the first
  123. parameter for calls of zalloc and zfree. This can be useful for custom
  124. memory management. The compression library attaches no meaning to the
  125. opaque value.
  126. zalloc must return Z_NULL if there is not enough memory for the object.
  127. If zlib is used in a multi-threaded application, zalloc and zfree must be
  128. thread safe.
  129. On 16-bit systems, the functions zalloc and zfree must be able to allocate
  130. exactly 65536 bytes, but will not be required to allocate more than this
  131. if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  132. pointers returned by zalloc for objects of exactly 65536 bytes *must*
  133. have their offset normalized to zero. The default allocation function
  134. provided by this library ensures this (see zutil.c). To reduce memory
  135. requirements and avoid any allocation of 64K objects, at the expense of
  136. compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  137. The fields total_in and total_out can be used for statistics or
  138. progress reports. After compression, total_in holds the total size of
  139. the uncompressed data and may be saved for use in the decompressor
  140. (particularly if the decompressor wants to decompress everything in
  141. a single step).
  142. *)
  143. const
  144. Z_NO_FLUSH = 0;
  145. Z_PARTIAL_FLUSH = 1; // will be removed, use Z_SYNC_FLUSH instead
  146. Z_SYNC_FLUSH = 2;
  147. Z_FULL_FLUSH = 3;
  148. Z_FINISH = 4;
  149. Z_BLOCK = 5;
  150. (* Allowed flush values; see deflate() and inflate() below for details *)
  151. Z_OK = 0;
  152. Z_STREAM_END = 1;
  153. Z_NEED_DICT = 2;
  154. Z_ERRNO = (-1);
  155. Z_STREAM_ERROR = (-2);
  156. Z_DATA_ERROR = (-3);
  157. Z_MEM_ERROR = (-4);
  158. Z_BUF_ERROR = (-5);
  159. Z_VERSION_ERROR = (-6);
  160. (* Return codes for the compression/decompression functions. Negative
  161. * values are errors, positive values are used for special but normal events.
  162. *)
  163. Z_NO_COMPRESSION = 0;
  164. Z_BEST_SPEED = 1;
  165. Z_BEST_COMPRESSION = 9;
  166. Z_DEFAULT_COMPRESSION = -(1);
  167. (* compression levels *)
  168. Z_FILTERED = 1;
  169. Z_HUFFMAN_ONLY = 2;
  170. Z_RLE = 3;
  171. Z_FIXED = 4;
  172. Z_DEFAULT_STRATEGY = 0;
  173. (* compression strategy; see deflateInit2() below for details *)
  174. Z_BINARY = 0;
  175. Z_TEXT = 1;
  176. Z_ASCII = Z_TEXT; // for compatibility with 1.2.2 and earlier
  177. Z_UNKNOWN = 2;
  178. (* Possible values of the data_type field (though see inflate()) *)
  179. Z_DEFLATED = 8;
  180. (* The deflate compression method (the only one supported in this version) *)
  181. Z_NULL = 0;
  182. (* for initializing zalloc, zfree, opaque *)
  183. function zlibVersion:pchar; cdecl; external libz name 'zlibVersion';
  184. (*
  185. The application can compare zlibVersion and ZLIB_VERSION for consistency.
  186. If the first character differs, the library code actually used is
  187. not compatible with the zlib.h header file used by the application.
  188. This check is automatically made by deflateInit and inflateInit.
  189. *)
  190. function deflateInit_(var strm: z_stream; level: cint; version: pchar; stream_size: cint): cint; cdecl; external libz name 'deflateInit_';
  191. function deflateInit(var strm: z_stream; level: cint): cint;
  192. (*
  193. Initializes the internal stream state for compression. The fields
  194. zalloc, zfree and opaque must be initialized before by the caller.
  195. If zalloc and zfree are set to Z_NULL, deflateInit updates them to
  196. use default allocation functions.
  197. The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
  198. 1 gives best speed, 9 gives best compression, 0 gives no compression at
  199. all (the input data is simply copied a block at a time).
  200. Z_DEFAULT_COMPRESSION requests a default compromise between speed and
  201. compression (currently equivalent to level 6).
  202. deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  203. enough memory, Z_STREAM_ERROR if level is not a valid compression level,
  204. Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
  205. with the version assumed by the caller (ZLIB_VERSION).
  206. msg is set to null if there is no error message. deflateInit does not
  207. perform any compression: this will be done by deflate().
  208. *)
  209. function deflate(var strm: z_stream; flush: cint): cint; cdecl; external libz name 'deflate';
  210. (*
  211. deflate compresses as much data as possible, and stops when the input
  212. buffer becomes empty or the output buffer becomes full. It may introduce some
  213. output latency (reading input without producing any output) except when
  214. forced to flush.
  215. The detailed semantics are as follows. deflate performs one or both of the
  216. following actions:
  217. - Compress more input starting at next_in and update next_in and avail_in
  218. accordingly. If not all input can be processed (because there is not
  219. enough room in the output buffer), next_in and avail_in are updated and
  220. processing will resume at this point for the next call of deflate().
  221. - Provide more output starting at next_out and update next_out and avail_out
  222. accordingly. This action is forced if the parameter flush is non zero.
  223. Forcing flush frequently degrades the compression ratio, so this parameter
  224. should be set only when necessary (in interactive applications).
  225. Some output may be provided even if flush is not set.
  226. Before the call of deflate(), the application should ensure that at least
  227. one of the actions is possible, by providing more input and/or consuming
  228. more output, and updating avail_in or avail_out accordingly; avail_out
  229. should never be zero before the call. The application can consume the
  230. compressed output when it wants, for example when the output buffer is full
  231. (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
  232. and with zero avail_out, it must be called again after making room in the
  233. output buffer because there might be more output pending.
  234. Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
  235. decide how much data to accumualte before producing output, in order to
  236. maximize compression.
  237. If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
  238. flushed to the output buffer and the output is aligned on a byte boundary, so
  239. that the decompressor can get all input data available so far. (In particular
  240. avail_in is zero after the call if enough output space has been provided
  241. before the call.) Flushing may degrade compression for some compression
  242. algorithms and so it should be used only when necessary.
  243. If flush is set to Z_FULL_FLUSH, all output is flushed as with
  244. Z_SYNC_FLUSH, and the compression state is reset so that decompression can
  245. restart from this point if previous compressed data has been damaged or if
  246. random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
  247. compression.
  248. If deflate returns with avail_out == 0, this function must be called again
  249. with the same value of the flush parameter and more output space (updated
  250. avail_out), until the flush is complete (deflate returns with non-zero
  251. avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
  252. avail_out is greater than six to avoid repeated flush markers due to
  253. avail_out == 0 on return.
  254. If the parameter flush is set to Z_FINISH, pending input is processed,
  255. pending output is flushed and deflate returns with Z_STREAM_END if there
  256. was enough output space; if deflate returns with Z_OK, this function must be
  257. called again with Z_FINISH and more output space (updated avail_out) but no
  258. more input data, until it returns with Z_STREAM_END or an error. After
  259. deflate has returned Z_STREAM_END, the only possible operations on the
  260. stream are deflateReset or deflateEnd.
  261. Z_FINISH can be used immediately after deflateInit if all the compression
  262. is to be done in a single step. In this case, avail_out must be at least
  263. the value returned by deflateBound (see below). If deflate does not return
  264. Z_STREAM_END, then it must be called again as described above.
  265. deflate() sets strm->adler to the adler32 checksum of all input read
  266. so far (that is, total_in bytes).
  267. deflate() may update strm->data_type if it can make a good guess about
  268. the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
  269. binary. This field is only for information purposes and does not affect
  270. the compression algorithm in any manner.
  271. deflate() returns Z_OK if some progress has been made (more input
  272. processed or more output produced), Z_STREAM_END if all input has been
  273. consumed and all output has been produced (only when flush is set to
  274. Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
  275. if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
  276. (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
  277. fatal, and deflate() can be called again with more input and more output
  278. space to continue compressing.
  279. *)
  280. function deflateEnd(var strm: z_stream): cint; cdecl; external libz name 'deflateEnd';
  281. (*
  282. All dynamically allocated data structures for this stream are freed.
  283. This function discards any unprocessed input and does not flush any
  284. pending output.
  285. deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
  286. stream state was inconsistent, Z_DATA_ERROR if the stream was freed
  287. prematurely (some input or output was discarded). In the error case,
  288. msg may be set but then points to a static string (which must not be
  289. deallocated).
  290. *)
  291. function inflateInit_(var strm: z_stream; version: pchar; stream_size: cint): cint; cdecl; external libz name 'inflateInit_';
  292. function inflateInit(var strm: z_stream): cint;
  293. (*
  294. Initializes the internal stream state for decompression. The fields
  295. next_in, avail_in, zalloc, zfree and opaque must be initialized before by
  296. the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
  297. value depends on the compression method), inflateInit determines the
  298. compression method from the zlib header and allocates all data structures
  299. accordingly; otherwise the allocation will be deferred to the first call of
  300. inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
  301. use default allocation functions.
  302. inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
  303. memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
  304. version assumed by the caller. msg is set to null if there is no error
  305. message. inflateInit does not perform any decompression apart from reading
  306. the zlib header if present: this will be done by inflate(). (So next_in and
  307. avail_in may be modified, but next_out and avail_out are unchanged.)
  308. *)
  309. function inflate(var strm: z_stream; flush: cint): cint; cdecl; external libz name 'inflate';
  310. (*
  311. inflate decompresses as much data as possible, and stops when the input
  312. buffer becomes empty or the output buffer becomes full. It may introduce
  313. some output latency (reading input without producing any output) except when
  314. forced to flush.
  315. The detailed semantics are as follows. inflate performs one or both of the
  316. following actions:
  317. - Decompress more input starting at next_in and update next_in and avail_in
  318. accordingly. If not all input can be processed (because there is not
  319. enough room in the output buffer), next_in is updated and processing
  320. will resume at this point for the next call of inflate().
  321. - Provide more output starting at next_out and update next_out and avail_out
  322. accordingly. inflate() provides as much output as possible, until there
  323. is no more input data or no more space in the output buffer (see below
  324. about the flush parameter).
  325. Before the call of inflate(), the application should ensure that at least
  326. one of the actions is possible, by providing more input and/or consuming
  327. more output, and updating the next_* and avail_* values accordingly.
  328. The application can consume the uncompressed output when it wants, for
  329. example when the output buffer is full (avail_out == 0), or after each
  330. call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  331. must be called again after making room in the output buffer because there
  332. might be more output pending.
  333. The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
  334. Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
  335. output as possible to the output buffer. Z_BLOCK requests that inflate() stop
  336. if and when it gets to the next deflate block boundary. When decoding the
  337. zlib or gzip format, this will cause inflate() to return immediately after
  338. the header and before the first block. When doing a raw inflate, inflate()
  339. will go ahead and process the first block, and will return when it gets to
  340. the end of that block, or when it runs out of data.
  341. The Z_BLOCK option assists in appending to or combining deflate streams.
  342. Also to assist in this, on return inflate() will set strm->data_type to the
  343. number of unused bits in the last byte taken from strm->next_in, plus 64
  344. if inflate() is currently decoding the last block in the deflate stream,
  345. plus 128 if inflate() returned immediately after decoding an end-of-block
  346. code or decoding the complete header up to just before the first byte of the
  347. deflate stream. The end-of-block will not be indicated until all of the
  348. uncompressed data from that block has been written to strm->next_out. The
  349. number of unused bits may in general be greater than seven, except when
  350. bit 7 of data_type is set, in which case the number of unused bits will be
  351. less than eight.
  352. inflate() should normally be called until it returns Z_STREAM_END or an
  353. error. However if all decompression is to be performed in a single step
  354. (a single call of inflate), the parameter flush should be set to
  355. Z_FINISH. In this case all pending input is processed and all pending
  356. output is flushed; avail_out must be large enough to hold all the
  357. uncompressed data. (The size of the uncompressed data may have been saved
  358. by the compressor for this purpose.) The next operation on this stream must
  359. be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  360. is never required, but can be used to inform inflate that a faster approach
  361. may be used for the single inflate() call.
  362. In this implementation, inflate() always flushes as much output as
  363. possible to the output buffer, and always uses the faster approach on the
  364. first call. So the only effect of the flush parameter in this implementation
  365. is on the return value of inflate(), as noted below, or when it returns early
  366. because Z_BLOCK is used.
  367. If a preset dictionary is needed after this call (see inflateSetDictionary
  368. below), inflate sets strm->adler to the adler32 checksum of the dictionary
  369. chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
  370. strm->adler to the adler32 checksum of all output produced so far (that is,
  371. total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
  372. below. At the end of the stream, inflate() checks that its computed adler32
  373. checksum is equal to that saved by the compressor and returns Z_STREAM_END
  374. only if the checksum is correct.
  375. inflate() will decompress and check either zlib-wrapped or gzip-wrapped
  376. deflate data. The header type is detected automatically. Any information
  377. contained in the gzip header is not retained, so applications that need that
  378. information should instead use raw inflate, see inflateInit2() below, or
  379. inflateBack() and perform their own processing of the gzip header and
  380. trailer.
  381. inflate() returns Z_OK if some progress has been made (more input processed
  382. or more output produced), Z_STREAM_END if the end of the compressed data has
  383. been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  384. preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  385. corrupted (input stream not conforming to the zlib format or incorrect check
  386. value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
  387. if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
  388. Z_BUF_ERROR if no progress is possible or if there was not enough room in the
  389. output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
  390. inflate() can be called again with more input and more output space to
  391. continue decompressing. If Z_DATA_ERROR is returned, the application may then
  392. call inflateSync() to look for a good compression block if a partial recovery
  393. of the data is desired.
  394. *)
  395. function inflateEnd(var strm: z_stream): cint; cdecl; external libz name 'inflateEnd';
  396. (*
  397. All dynamically allocated data structures for this stream are freed.
  398. This function discards any unprocessed input and does not flush any
  399. pending output.
  400. inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  401. was inconsistent. In the error case, msg may be set but then points to a
  402. static string (which must not be deallocated).
  403. *)
  404. function deflateInit2_(var strm: z_stream; level, method, windowBits, memLevel, strategy: cint; version: pchar; stream_size: cint): cint; cdecl; external libz name 'deflateInit2_';
  405. function deflateInit2(var strm: z_stream; level, method, windowBits, memLevel, strategy: cint): longint;
  406. (*
  407. This is another version of deflateInit with more compression options. The
  408. fields next_in, zalloc, zfree and opaque must be initialized before by
  409. the caller.
  410. The method parameter is the compression method. It must be Z_DEFLATED in
  411. this version of the library.
  412. The windowBits parameter is the base two logarithm of the window size
  413. (the size of the history buffer). It should be in the range 8..15 for this
  414. version of the library. Larger values of this parameter result in better
  415. compression at the expense of memory usage. The default value is 15 if
  416. deflateInit is used instead.
  417. windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
  418. determines the window size. deflate() will then generate raw deflate data
  419. with no zlib header or trailer, and will not compute an adler32 check value.
  420. windowBits can also be greater than 15 for optional gzip encoding. Add
  421. 16 to windowBits to write a simple gzip header and trailer around the
  422. compressed data instead of a zlib wrapper. The gzip header will have no
  423. file name, no extra data, no comment, no modification time (set to zero),
  424. no header crc, and the operating system will be set to 255 (unknown). If a
  425. gzip stream is being written, strm->adler is a crc32 instead of an adler32.
  426. The memLevel parameter specifies how much memory should be allocated
  427. for the internal compression state. memLevel=1 uses minimum memory but
  428. is slow and reduces compression ratio; memLevel=9 uses maximum memory
  429. for optimal speed. The default value is 8. See zconf.h for total memory
  430. usage as a function of windowBits and memLevel.
  431. The strategy parameter is used to tune the compression algorithm. Use the
  432. value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
  433. filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
  434. string match), or Z_RLE to limit match distances to one (run-length
  435. encoding). Filtered data consists mostly of small values with a somewhat
  436. random distribution. In this case, the compression algorithm is tuned to
  437. compress them better. The effect of Z_FILTERED is to force more Huffman
  438. coding and less string matching; it is somewhat intermediate between
  439. Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
  440. Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
  441. parameter only affects the compression ratio but not the correctness of the
  442. compressed output even if it is not set appropriately. Z_FIXED prevents the
  443. use of dynamic Huffman codes, allowing for a simpler decoder for special
  444. applications.
  445. deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  446. memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
  447. method). msg is set to null if there is no error message. deflateInit2 does
  448. not perform any compression: this will be done by deflate().
  449. *)
  450. function deflateSetDictionary(var strm: z_stream; dictionary: pbytef; dictLength: uInt): cint; cdecl; external libz name 'deflateSetDictionary';
  451. (*
  452. Initializes the compression dictionary from the given byte sequence
  453. without producing any compressed output. This function must be called
  454. immediately after deflateInit, deflateInit2 or deflateReset, before any
  455. call of deflate. The compressor and decompressor must use exactly the same
  456. dictionary (see inflateSetDictionary).
  457. The dictionary should consist of strings (byte sequences) that are likely
  458. to be encountered later in the data to be compressed, with the most commonly
  459. used strings preferably put towards the end of the dictionary. Using a
  460. dictionary is most useful when the data to be compressed is short and can be
  461. predicted with good accuracy; the data can then be compressed better than
  462. with the default empty dictionary.
  463. Depending on the size of the compression data structures selected by
  464. deflateInit or deflateInit2, a part of the dictionary may in effect be
  465. discarded, for example if the dictionary is larger than the window size in
  466. deflate or deflate2. Thus the strings most likely to be useful should be
  467. put at the end of the dictionary, not at the front. In addition, the
  468. current implementation of deflate will use at most the window size minus
  469. 262 bytes of the provided dictionary.
  470. Upon return of this function, strm->adler is set to the adler32 value
  471. of the dictionary; the decompressor may later use this value to determine
  472. which dictionary has been used by the compressor. (The adler32 value
  473. applies to the whole dictionary even if only a subset of the dictionary is
  474. actually used by the compressor.) If a raw deflate was requested, then the
  475. adler32 value is not computed and strm->adler is not set.
  476. deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
  477. parameter is invalid (such as NULL dictionary) or the stream state is
  478. inconsistent (for example if deflate has already been called for this stream
  479. or if the compression method is bsort). deflateSetDictionary does not
  480. perform any compression: this will be done by deflate().
  481. *)
  482. function deflateCopy(var dest, source: z_stream): cint; cdecl; external libz name 'deflateCopy';
  483. (*
  484. Sets the destination stream as a complete copy of the source stream.
  485. This function can be useful when several compression strategies will be
  486. tried, for example when there are several ways of pre-processing the input
  487. data with a filter. The streams that will be discarded should then be freed
  488. by calling deflateEnd. Note that deflateCopy duplicates the internal
  489. compression state which can be quite large, so this strategy is slow and
  490. can consume lots of memory.
  491. deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  492. enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  493. (such as zalloc being NULL). msg is left unchanged in both source and
  494. destination.
  495. *)
  496. function deflateReset(var strm: z_stream): cint; cdecl; external libz name 'deflateReset';
  497. (*
  498. This function is equivalent to deflateEnd followed by deflateInit,
  499. but does not free and reallocate all the internal compression state.
  500. The stream will keep the same compression level and any other attributes
  501. that may have been set by deflateInit2.
  502. deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  503. stream state was inconsistent (such as zalloc or state being NULL).
  504. *)
  505. function deflateParams(var strm: z_stream; level: cint; strategy: cint): cint; cdecl; external libz name 'deflateParams';
  506. (*
  507. Dynamically update the compression level and compression strategy. The
  508. interpretation of level and strategy is as in deflateInit2. This can be
  509. used to switch between compression and straight copy of the input data, or
  510. to switch to a different kind of input data requiring a different
  511. strategy. If the compression level is changed, the input available so far
  512. is compressed with the old level (and may be flushed); the new level will
  513. take effect only at the next call of deflate().
  514. Before the call of deflateParams, the stream state must be set as for
  515. a call of deflate(), since the currently available input may have to
  516. be compressed and flushed. In particular, strm->avail_out must be non-zero.
  517. deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
  518. stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
  519. if strm->avail_out was zero.
  520. *)
  521. function deflateTune(var strm: z_stream; good_length, max_lazy, nice_length, max_chain: cint): cint; cdecl; external libz name 'deflateTune';
  522. (*
  523. Fine tune deflate's internal compression parameters. This should only be
  524. used by someone who understands the algorithm used by zlib's deflate for
  525. searching for the best matching string, and even then only by the most
  526. fanatic optimizer trying to squeeze out the last compressed bit for their
  527. specific input data. Read the deflate.c source code for the meaning of the
  528. max_lazy, good_length, nice_length, and max_chain parameters.
  529. deflateTune() can be called after deflateInit() or deflateInit2(), and
  530. returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
  531. *)
  532. function deflateBound(var strm: z_stream; sourceLen: uLong): uLong; cdecl; external libz name 'deflateBound';
  533. (*
  534. deflateBound() returns an upper bound on the compressed size after
  535. deflation of sourceLen bytes. It must be called after deflateInit()
  536. or deflateInit2(). This would be used to allocate an output buffer
  537. for deflation in a single pass, and so would be called before deflate().
  538. *)
  539. function deflatePrime(var strm: z_stream; bits, value: cint): cint; cdecl; external libz name 'deflatePrime';
  540. (*
  541. deflatePrime() inserts bits in the deflate output stream. The intent
  542. is that this function is used to start off the deflate output with the
  543. bits leftover from a previous deflate stream when appending to it. As such,
  544. this function can only be used for raw deflate, and must be used before the
  545. first deflate() call after a deflateInit2() or deflateReset(). bits must be
  546. less than or equal to 16, and that many of the least significant bits of
  547. value will be inserted in the output.
  548. deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
  549. stream state was inconsistent.
  550. *)
  551. function deflateSetHeader(var strm: z_stream; var head: gz_header): cint; cdecl; external libz name 'deflateSetHeader';
  552. (*
  553. deflateSetHeader() provides gzip header information for when a gzip
  554. stream is requested by deflateInit2(). deflateSetHeader() may be called
  555. after deflateInit2() or deflateReset() and before the first call of
  556. deflate(). The text, time, os, extra field, name, and comment information
  557. in the provided gz_header structure are written to the gzip header (xflag is
  558. ignored -- the extra flags are set according to the compression level). The
  559. caller must assure that, if not Z_NULL, name and comment are terminated with
  560. a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
  561. available there. If hcrc is true, a gzip header crc is included. Note that
  562. the current versions of the command-line version of gzip (up through version
  563. 1.3.x) do not support header crc's, and will report that it is a "multi-part
  564. gzip file" and give up.
  565. If deflateSetHeader is not used, the default gzip header has text false,
  566. the time set to zero, and os set to 255, with no extra, name, or comment
  567. fields. The gzip header is returned to the default state by deflateReset().
  568. deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
  569. stream state was inconsistent.
  570. *)
  571. function inflateInit2_(var strm: z_stream; windowBits: cint; version:pchar; stream_size: cint): cint; cdecl; external libz name 'inflateInit2_';
  572. function inflateInit2(var strm: z_stream; windowBits: cint): cint;
  573. (*
  574. This is another version of inflateInit with an extra parameter. The
  575. fields next_in, avail_in, zalloc, zfree and opaque must be initialized
  576. before by the caller.
  577. The windowBits parameter is the base two logarithm of the maximum window
  578. size (the size of the history buffer). It should be in the range 8..15 for
  579. this version of the library. The default value is 15 if inflateInit is used
  580. instead. windowBits must be greater than or equal to the windowBits value
  581. provided to deflateInit2() while compressing, or it must be equal to 15 if
  582. deflateInit2() was not used. If a compressed stream with a larger window
  583. size is given as input, inflate() will return with the error code
  584. Z_DATA_ERROR instead of trying to allocate a larger window.
  585. windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
  586. determines the window size. inflate() will then process raw deflate data,
  587. not looking for a zlib or gzip header, not generating a check value, and not
  588. looking for any check values for comparison at the end of the stream. This
  589. is for use with other formats that use the deflate compressed data format
  590. such as zip. Those formats provide their own check values. If a custom
  591. format is developed using the raw deflate format for compressed data, it is
  592. recommended that a check value such as an adler32 or a crc32 be applied to
  593. the uncompressed data as is done in the zlib, gzip, and zip formats. For
  594. most applications, the zlib format should be used as is. Note that comments
  595. above on the use in deflateInit2() applies to the magnitude of windowBits.
  596. windowBits can also be greater than 15 for optional gzip decoding. Add
  597. 32 to windowBits to enable zlib and gzip decoding with automatic header
  598. detection, or add 16 to decode only the gzip format (the zlib format will
  599. return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is
  600. a crc32 instead of an adler32.
  601. inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  602. memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
  603. is set to null if there is no error message. inflateInit2 does not perform
  604. any decompression apart from reading the zlib header if present: this will
  605. be done by inflate(). (So next_in and avail_in may be modified, but next_out
  606. and avail_out are unchanged.)
  607. *)
  608. function inflateSetDictionary(var strm: z_stream; dictionary: pbytef; dictLength: uInt): cint; cdecl; external libz name 'inflateSetDictionary';
  609. (*
  610. Initializes the decompression dictionary from the given uncompressed byte
  611. sequence. This function must be called immediately after a call of inflate,
  612. if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
  613. can be determined from the adler32 value returned by that call of inflate.
  614. The compressor and decompressor must use exactly the same dictionary (see
  615. deflateSetDictionary). For raw inflate, this function can be called
  616. immediately after inflateInit2() or inflateReset() and before any call of
  617. inflate() to set the dictionary. The application must insure that the
  618. dictionary that was used for compression is provided.
  619. inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  620. parameter is invalid (such as NULL dictionary) or the stream state is
  621. inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  622. expected one (incorrect adler32 value). inflateSetDictionary does not
  623. perform any decompression: this will be done by subsequent calls of
  624. inflate().
  625. *)
  626. function inflateSync(var strm: z_stream): cint; cdecl; external libz name 'inflateSync';
  627. (*
  628. Skips invalid compressed data until a full flush point (see above the
  629. description of deflate with Z_FULL_FLUSH) can be found, or until all
  630. available input is skipped. No output is provided.
  631. inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  632. if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  633. or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  634. case, the application may save the current current value of total_in which
  635. indicates where valid compressed data was found. In the error case, the
  636. application may repeatedly call inflateSync, providing more input each time,
  637. until success or end of the input data.
  638. *)
  639. function inflateCopy(var dest, source: z_stream): cint; cdecl; external libz name 'inflateCopy';
  640. (*
  641. Sets the destination stream as a complete copy of the source stream.
  642. This function can be useful when randomly accessing a large stream. The
  643. first pass through the stream can periodically record the inflate state,
  644. allowing restarting inflate at those points when randomly accessing the
  645. stream.
  646. inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  647. enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  648. (such as zalloc being NULL). msg is left unchanged in both source and
  649. destination.
  650. *)
  651. function inflateReset(var strm: z_stream): cint; cdecl; external libz name 'inflateReset';
  652. (*
  653. This function is equivalent to inflateEnd followed by inflateInit,
  654. but does not free and reallocate all the internal decompression state.
  655. The stream will keep attributes that may have been set by inflateInit2.
  656. inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  657. stream state was inconsistent (such as zalloc or state being NULL).
  658. *)
  659. function inflatePrime(var strm: z_stream; bits, value: cint): cint; cdecl; external libz name 'inflateReset';
  660. (*
  661. This function inserts bits in the inflate input stream. The intent is
  662. that this function is used to start inflating at a bit position in the
  663. middle of a byte. The provided bits will be used before any bytes are used
  664. from next_in. This function should only be used with raw inflate, and
  665. should be used before the first inflate() call after inflateInit2() or
  666. inflateReset(). bits must be less than or equal to 16, and that many of the
  667. least significant bits of value will be inserted in the input.
  668. inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
  669. stream state was inconsistent.
  670. *)
  671. function inflateGetHeader(var strm: z_stream; var head: gz_header): cint; cdecl; external libz name 'inflateGetHeader';
  672. (*
  673. inflateGetHeader() requests that gzip header information be stored in the
  674. provided gz_header structure. inflateGetHeader() may be called after
  675. inflateInit2() or inflateReset(), and before the first call of inflate().
  676. As inflate() processes the gzip stream, head->done is zero until the header
  677. is completed, at which time head->done is set to one. If a zlib stream is
  678. being decoded, then head->done is set to -1 to indicate that there will be
  679. no gzip header information forthcoming. Note that Z_BLOCK can be used to
  680. force inflate() to return immediately after header processing is complete
  681. and before any actual data is decompressed.
  682. The text, time, xflags, and os fields are filled in with the gzip header
  683. contents. hcrc is set to true if there is a header CRC. (The header CRC
  684. was valid if done is set to one.) If extra is not Z_NULL, then extra_max
  685. contains the maximum number of bytes to write to extra. Once done is true,
  686. extra_len contains the actual extra field length, and extra contains the
  687. extra field, or that field truncated if extra_max is less than extra_len.
  688. If name is not Z_NULL, then up to name_max characters are written there,
  689. terminated with a zero unless the length is greater than name_max. If
  690. comment is not Z_NULL, then up to comm_max characters are written there,
  691. terminated with a zero unless the length is greater than comm_max. When
  692. any of extra, name, or comment are not Z_NULL and the respective field is
  693. not present in the header, then that field is set to Z_NULL to signal its
  694. absence. This allows the use of deflateSetHeader() with the returned
  695. structure to duplicate the header. However if those fields are set to
  696. allocated memory, then the application will need to save those pointers
  697. elsewhere so that they can be eventually freed.
  698. If inflateGetHeader is not used, then the header information is simply
  699. discarded. The header is always checked for validity, including the header
  700. CRC if present. inflateReset() will reset the process to discard the header
  701. information. The application would need to call inflateGetHeader() again to
  702. retrieve the header from the next gzip stream.
  703. inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
  704. stream state was inconsistent.
  705. *)
  706. function inflateBackInit_(var strm: z_stream; windowBits: cint; window: pointer; version: pchar; stream_size: cint): cint; cdecl; external libz name 'inflateBackInit_';
  707. function inflateBackInit(var strm: z_stream; windowBits: cint; window: pointer): cint;
  708. (*
  709. Initialize the internal stream state for decompression using inflateBack()
  710. calls. The fields zalloc, zfree and opaque in strm must be initialized
  711. before the call. If zalloc and zfree are Z_NULL, then the default library-
  712. derived memory allocation routines are used. windowBits is the base two
  713. logarithm of the window size, in the range 8..15. window is a caller
  714. supplied buffer of that size. Except for special applications where it is
  715. assured that deflate was used with small window sizes, windowBits must be 15
  716. and a 32K byte window must be supplied to be able to decompress general
  717. deflate streams.
  718. See inflateBack() for the usage of these routines.
  719. inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
  720. the paramaters are invalid, Z_MEM_ERROR if the internal state could not
  721. be allocated, or Z_VERSION_ERROR if the version of the library does not
  722. match the version of the header file.
  723. *)
  724. type
  725. in_func = function(in_desc: pointer; var c: pcuchar): cuint; cdecl;
  726. out_func = function(out_desc: pointer; c: pcuchar; i: cuint): cint; cdecl;
  727. //typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
  728. //typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
  729. function inflateBack(var strm: z_stream; inf: in_func; in_desc: pointer; outf: out_func; out_desc: pointer): cint; cdecl; external libz name 'inflateBack';
  730. (*
  731. inflateBack() does a raw inflate with a single call using a call-back
  732. interface for input and output. This is more efficient than inflate() for
  733. file i/o applications in that it avoids copying between the output and the
  734. sliding window by simply making the window itself the output buffer. This
  735. function trusts the application to not change the output buffer passed by
  736. the output function, at least until inflateBack() returns.
  737. inflateBackInit() must be called first to allocate the internal state
  738. and to initialize the state with the user-provided window buffer.
  739. inflateBack() may then be used multiple times to inflate a complete, raw
  740. deflate stream with each call. inflateBackEnd() is then called to free
  741. the allocated state.
  742. A raw deflate stream is one with no zlib or gzip header or trailer.
  743. This routine would normally be used in a utility that reads zip or gzip
  744. files and writes out uncompressed files. The utility would decode the
  745. header and process the trailer on its own, hence this routine expects
  746. only the raw deflate stream to decompress. This is different from the
  747. normal behavior of inflate(), which expects either a zlib or gzip header and
  748. trailer around the deflate stream.
  749. inflateBack() uses two subroutines supplied by the caller that are then
  750. called by inflateBack() for input and output. inflateBack() calls those
  751. routines until it reads a complete deflate stream and writes out all of the
  752. uncompressed data, or until it encounters an error. The function's
  753. parameters and return types are defined above in the in_func and out_func
  754. typedefs. inflateBack() will call in(in_desc, &buf) which should return the
  755. number of bytes of provided input, and a pointer to that input in buf. If
  756. there is no input available, in() must return zero--buf is ignored in that
  757. case--and inflateBack() will return a buffer error. inflateBack() will call
  758. out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
  759. should return zero on success, or non-zero on failure. If out() returns
  760. non-zero, inflateBack() will return with an error. Neither in() nor out()
  761. are permitted to change the contents of the window provided to
  762. inflateBackInit(), which is also the buffer that out() uses to write from.
  763. The length written by out() will be at most the window size. Any non-zero
  764. amount of input may be provided by in().
  765. For convenience, inflateBack() can be provided input on the first call by
  766. setting strm->next_in and strm->avail_in. If that input is exhausted, then
  767. in() will be called. Therefore strm->next_in must be initialized before
  768. calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
  769. immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
  770. must also be initialized, and then if strm->avail_in is not zero, input will
  771. initially be taken from strm->next_in[0 .. strm->avail_in - 1].
  772. The in_desc and out_desc parameters of inflateBack() is passed as the
  773. first parameter of in() and out() respectively when they are called. These
  774. descriptors can be optionally used to pass any information that the caller-
  775. supplied in() and out() functions need to do their job.
  776. On return, inflateBack() will set strm->next_in and strm->avail_in to
  777. pass back any unused input that was provided by the last in() call. The
  778. return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
  779. if in() or out() returned an error, Z_DATA_ERROR if there was a format
  780. error in the deflate stream (in which case strm->msg is set to indicate the
  781. nature of the error), or Z_STREAM_ERROR if the stream was not properly
  782. initialized. In the case of Z_BUF_ERROR, an input or output error can be
  783. distinguished using strm->next_in which will be Z_NULL only if in() returned
  784. an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
  785. out() returning non-zero. (in() will always be called before out(), so
  786. strm->next_in is assured to be defined if out() returns non-zero.) Note
  787. that inflateBack() cannot return Z_OK.
  788. *)
  789. function inflateBackEnd(var strm: z_stream): cint; cdecl; external libz name 'inflateBackEnd';
  790. (*
  791. All memory allocated by inflateBackInit() is freed.
  792. inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
  793. state was inconsistent.
  794. *)
  795. function compress(dest: pbytef; destLen: puLongf; source: pbytef; sourceLen: uLong): cint; cdecl; external libz name 'compress';
  796. (*
  797. Compresses the source buffer into the destination buffer. sourceLen is
  798. the byte length of the source buffer. Upon entry, destLen is the total
  799. size of the destination buffer, which must be at least the value returned
  800. by compressBound(sourceLen). Upon exit, destLen is the actual size of the
  801. compressed buffer.
  802. This function can be used to compress a whole file at once if the
  803. input file is mmap'ed.
  804. compress returns Z_OK if success, Z_MEM_ERROR if there was not
  805. enough memory, Z_BUF_ERROR if there was not enough room in the output
  806. buffer.
  807. *)
  808. function compress2(dest: pbytef; destLen: puLongf; source: pbytef; sourceLen: uLong; level: cint): cint; cdecl; external libz name 'compress2';
  809. (*
  810. Compresses the source buffer into the destination buffer. The level
  811. parameter has the same meaning as in deflateInit. sourceLen is the byte
  812. length of the source buffer. Upon entry, destLen is the total size of the
  813. destination buffer, which must be at least the value returned by
  814. compressBound(sourceLen). Upon exit, destLen is the actual size of the
  815. compressed buffer.
  816. compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  817. memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  818. Z_STREAM_ERROR if the level parameter is invalid.
  819. *)
  820. function compressBound(sourceLen: uLong): uLong; cdecl; external libz name 'compressBound';
  821. (*
  822. compressBound() returns an upper bound on the compressed size after
  823. compress() or compress2() on sourceLen bytes. It would be used before
  824. a compress() or compress2() call to allocate the destination buffer.
  825. *)
  826. function uncompress(dest: pbytef; destLen: puLongf; source: pbytef; sourceLen: uLong):cint; cdecl; external libz name 'uncompress';
  827. (*
  828. Decompresses the source buffer into the destination buffer. sourceLen is
  829. the byte length of the source buffer. Upon entry, destLen is the total
  830. size of the destination buffer, which must be large enough to hold the
  831. entire uncompressed data. (The size of the uncompressed data must have
  832. been saved previously by the compressor and transmitted to the decompressor
  833. by some mechanism outside the scope of this compression library.)
  834. Upon exit, destLen is the actual size of the compressed buffer.
  835. This function can be used to decompress a whole file at once if the
  836. input file is mmap'ed.
  837. uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  838. enough memory, Z_BUF_ERROR if there was not enough room in the output
  839. buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
  840. *)
  841. type
  842. gzFile = pointer;
  843. function gzopen(path: pchar; mode: pchar): gzFile; cdecl; external libz name 'gzopen';
  844. (*
  845. Opens a gzip (.gz) file for reading or writing. The mode parameter
  846. is as in fopen ("rb" or "wb") but can also include a compression level
  847. ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
  848. Huffman only compression as in "wb1h", or 'R' for run-length encoding
  849. as in "wb1R". (See the description of deflateInit2 for more information
  850. about the strategy parameter.)
  851. gzopen can be used to read a file which is not in gzip format; in this
  852. case gzread will directly read from the file without decompression.
  853. gzopen returns NULL if the file could not be opened or if there was
  854. insufficient memory to allocate the (de)compression state; errno
  855. can be checked to distinguish the two cases (if errno is zero, the
  856. zlib error is Z_MEM_ERROR).
  857. *)
  858. function gzdopen(fd: cint; mode:pchar):gzFile; cdecl; external libz name 'gzdopen';
  859. (*
  860. gzdopen() associates a gzFile with the file descriptor fd. File
  861. descriptors are obtained from calls like open, dup, creat, pipe or
  862. fileno (in the file has been previously opened with fopen).
  863. The mode parameter is as in gzopen.
  864. The next call of gzclose on the returned gzFile will also close the
  865. file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
  866. descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
  867. gzdopen returns NULL if there was insufficient memory to allocate
  868. the (de)compression state.
  869. *)
  870. function gzsetparams(thefile:gzFile; level: cint; strategy: cint): cint; cdecl; external libz name 'gzsetparams';
  871. (*
  872. Dynamically update the compression level or strategy. See the description
  873. of deflateInit2 for the meaning of these parameters.
  874. gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
  875. opened for writing.
  876. *)
  877. function gzread(thefile:gzFile; buf:pointer; len:cardinal): cint; cdecl; external libz name 'gzread';
  878. (*
  879. Reads the given number of uncompressed bytes from the compressed file.
  880. If the input file was not in gzip format, gzread copies the given number
  881. of bytes into the buffer.
  882. gzread returns the number of uncompressed bytes actually read (0 for
  883. end of file, -1 for error).
  884. *)
  885. function gzwrite(thefile:gzFile; buf:pointer; len:cardinal): cint; cdecl; external libz name 'gzwrite';
  886. (*
  887. Writes the given number of uncompressed bytes into the compressed file.
  888. gzwrite returns the number of uncompressed bytes actually written
  889. (0 in case of error).
  890. *)
  891. function gzprintf(thefile:gzFile; format:pbytef; args:array of const): cint; cdecl; external libz name 'gzprintf';
  892. (*
  893. Converts, formats, and writes the args to the compressed file under
  894. control of the format string, as in fprintf. gzprintf returns the number of
  895. uncompressed bytes actually written (0 in case of error). The number of
  896. uncompressed bytes written is limited to 4095. The caller should assure that
  897. this limit is not exceeded. If it is exceeded, then gzprintf() will return
  898. return an error (0) with nothing written. In this case, there may also be a
  899. buffer overflow with unpredictable consequences, which is possible only if
  900. zlib was compiled with the insecure functions sprintf() or vsprintf()
  901. because the secure snprintf() or vsnprintf() functions were not available.
  902. *)
  903. function gzputs(thefile:gzFile; s:pbytef): cint; cdecl; external libz name 'gzputs';
  904. (*
  905. Writes the given null-terminated string to the compressed file, excluding
  906. the terminating null character.
  907. gzputs returns the number of characters written, or -1 in case of error.
  908. *)
  909. function gzgets(thefile:gzFile; buf:pbytef; len: cint):pbytef; cdecl; external libz name 'gzgets';
  910. (*
  911. Reads bytes from the compressed file until len-1 characters are read, or
  912. a newline character is read and transferred to buf, or an end-of-file
  913. condition is encountered. The string is then terminated with a null
  914. character.
  915. gzgets returns buf, or Z_NULL in case of error.
  916. *)
  917. function gzputc(thefile:gzFile; c:char):char; cdecl; external libz name 'gzputc';
  918. (*
  919. Writes c, converted to an unsigned char, into the compressed file.
  920. gzputc returns the value that was written, or -1 in case of error.
  921. *)
  922. function gzgetc(thefile:gzFile):char; cdecl; external libz name 'gzgetc';
  923. (*
  924. Reads one byte from the compressed file. gzgetc returns this byte
  925. or -1 in case of end of file or error.
  926. *)
  927. function gzflush(thefile:gzFile; flush: cint): cint; cdecl; external libz name 'gzflush';
  928. (*
  929. Push one character back onto the stream to be read again later.
  930. Only one character of push-back is allowed. gzungetc() returns the
  931. character pushed, or -1 on failure. gzungetc() will fail if a
  932. character has been pushed but not read yet, or if c is -1. The pushed
  933. character will be discarded if the stream is repositioned with gzseek()
  934. or gzrewind().
  935. *)
  936. function gzseek(thefile:gzFile; offset:z_off_t; whence: cint):z_off_t; cdecl; external libz name 'gzseek';
  937. (*
  938. Sets the starting position for the next gzread or gzwrite on the
  939. given compressed file. The offset represents a number of bytes in the
  940. uncompressed data stream. The whence parameter is defined as in lseek(2);
  941. the value SEEK_END is not supported.
  942. If the file is opened for reading, this function is emulated but can be
  943. extremely slow. If the file is opened for writing, only forward seeks are
  944. supported; gzseek then compresses a sequence of zeroes up to the new
  945. starting position.
  946. gzseek returns the resulting offset location as measured in bytes from
  947. the beginning of the uncompressed stream, or -1 in case of error, in
  948. particular if the file is opened for writing and the new starting position
  949. would be before the current position.
  950. *)
  951. function gzrewind(thefile:gzFile): cint; cdecl; external libz name 'gzrewind';
  952. (*
  953. Rewinds the given file. This function is supported only for reading.
  954. gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
  955. *)
  956. function gztell(thefile:gzFile):z_off_t; cdecl; external libz name 'gztell';
  957. (*
  958. Returns the starting position for the next gzread or gzwrite on the
  959. given compressed file. This position represents a number of bytes in the
  960. uncompressed data stream.
  961. gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
  962. *)
  963. function gzeof(thefile:gzFile):longbool; cdecl; external libz name 'gzeof';
  964. (*
  965. Returns 1 when EOF has previously been detected reading the given
  966. input stream, otherwise zero.
  967. *)
  968. function gzclose(thefile:gzFile): cint; cdecl; external libz name 'gzclose';
  969. (*
  970. Flushes all pending output if necessary, closes the compressed file
  971. and deallocates all the (de)compression state. The return value is the zlib
  972. error number (see function gzerror below).
  973. *)
  974. function gzerror(thefile:gzFile; var errnum: cint):pbytef; cdecl; external libz name 'gzerror';
  975. (*
  976. Returns the error message for the last error which occurred on the
  977. given compressed file. errnum is set to zlib error number. If an
  978. error occurred in the file system and not in the compression library,
  979. errnum is set to Z_ERRNO and the application may consult errno
  980. to get the exact error code.
  981. *)
  982. procedure gzclearerr(thefile: gzFile); cdecl; external libz name 'gzclearerr';
  983. (*
  984. Clears the error and end-of-file flags for file. This is analogous to the
  985. clearerr() function in stdio. This is useful for continuing to read a gzip
  986. file that is being written concurrently.
  987. *)
  988. function adler32(adler: uLong; buf: pbytef; len: uInt): uLong; cdecl; external libz name 'adler32';
  989. (*
  990. Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  991. return the updated checksum. If buf is NULL, this function returns
  992. the required initial value for the checksum.
  993. An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  994. much faster. Usage example:
  995. uLong adler = adler32(0L, Z_NULL, 0);
  996. while (read_buffer(buffer, length) != EOF) {
  997. adler = adler32(adler, buffer, length);
  998. }
  999. if (adler != original_adler) error();
  1000. *)
  1001. function adler32_combine(adler1, adler2: uLong; len2: z_off_t): uLong; cdecl; external libz name 'adler32_combine';
  1002. (*
  1003. Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
  1004. and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
  1005. each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
  1006. seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
  1007. *)
  1008. function crc32(crc: uLong; buf: pbytef; len: uInt): uLong; cdecl; external libz name 'crc32';
  1009. (*
  1010. Update a running CRC-32 with the bytes buf[0..len-1] and return the
  1011. updated CRC-32. If buf is NULL, this function returns the required initial
  1012. value for the for the crc. Pre- and post-conditioning (one's complement) is
  1013. performed within this function so it shouldn't be done by the application.
  1014. Usage example:
  1015. uLong crc = crc32(0L, Z_NULL, 0);
  1016. while (read_buffer(buffer, length) != EOF) {
  1017. crc = crc32(crc, buffer, length);
  1018. }
  1019. if (crc != original_crc) error();
  1020. *)
  1021. function crc32_combine(crc1, crc2: uLong; len2: z_off_t): uLong; cdecl; external libz name 'crc32_combine';
  1022. (*
  1023. Combine two CRC-32 check values into one. For two sequences of bytes,
  1024. seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
  1025. calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
  1026. check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
  1027. len2.
  1028. *)
  1029. function zError(err: cint): pchar; cdecl; external libz name 'zError';
  1030. function inflateSyncPoint(var z: z_stream): cint; cdecl; external libz name 'inflateSyncPoint';
  1031. function get_crc_table: pointer; cdecl; external libz name 'get_crc_table';
  1032. function zlibAllocMem(AppData: Pointer; Items, Size: UInt): Pointer; cdecl;
  1033. procedure zlibFreeMem(AppData, Block: Pointer); cdecl;
  1034. implementation
  1035. function deflateInit(var strm: z_stream; level: cint): cint;
  1036. begin
  1037. Result := deflateInit_(strm, level, ZLIB_VERSION, sizeof(z_stream));
  1038. end;
  1039. function inflateInit(var strm: z_stream): cint;
  1040. begin
  1041. Result := inflateInit_(strm, ZLIB_VERSION, sizeof(z_stream));
  1042. end;
  1043. function deflateInit2(var strm: z_stream; level, method, windowBits, memLevel, strategy: cint): cint;
  1044. begin
  1045. Result := deflateInit2_(strm, level, method, windowBits, memLevel, strategy, ZLIB_VERSION, sizeof(z_stream));
  1046. end;
  1047. function inflateInit2(var strm: z_stream; windowBits: cint): cint;
  1048. begin
  1049. Result := inflateInit2_(strm, windowBits, ZLIB_VERSION, sizeof(z_stream));
  1050. end;
  1051. function inflateBackInit(var strm: z_stream; windowBits: cint; window: pointer): cint;
  1052. begin
  1053. Result := inflateBackInit_(strm, windowBits, window, ZLIB_VERSION, sizeof(z_stream));
  1054. end;
  1055. function zlibAllocMem(AppData: Pointer; Items, Size: UInt): Pointer; cdecl;
  1056. begin
  1057. Result := AllocMem(Items * Size);
  1058. end;
  1059. procedure zlibFreeMem(AppData, Block: Pointer); cdecl;
  1060. begin
  1061. FreeMem(Block);
  1062. end;
  1063. end.