zlib.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /* zlib.h -- interface of the 'zlib' general purpose compression library
  2. version 1.0.4, Jul 24th, 1996.
  3. Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. Jean-loup Gailly Mark Adler
  18. gzip@prep.ai.mit.edu madler@alumni.caltech.edu
  19. The data format used by the zlib library is described by RFCs (Request for
  20. Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  21. (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  22. */
  23. #ifndef _ZLIB_H
  24. #define _ZLIB_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #include "zconf.h"
  29. #define ZLIB_VERSION "1.0.4"
  30. /*
  31. The 'zlib' compression library provides in-memory compression and
  32. decompression functions, including integrity checks of the uncompressed
  33. data. This version of the library supports only one compression method
  34. (deflation) but other algorithms may be added later and will have the same
  35. stream interface.
  36. For compression the application must provide the output buffer and
  37. may optionally provide the input buffer for optimization. For decompression,
  38. the application must provide the input buffer and may optionally provide
  39. the output buffer for optimization.
  40. Compression can be done in a single step if the buffers are large
  41. enough (for example if an input file is mmap'ed), or can be done by
  42. repeated calls of the compression function. In the latter case, the
  43. application must provide more input and/or consume the output
  44. (providing more output space) before each call.
  45. The library does not install any signal handler. It is recommended to
  46. add at least a handler for SIGSEGV when decompressing; the library checks
  47. the consistency of the input data whenever possible but may go nuts
  48. for some forms of corrupted input.
  49. */
  50. typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
  51. typedef void (*free_func) OF((voidpf opaque, voidpf address));
  52. struct internal_state;
  53. typedef struct z_stream_s {
  54. Bytef *next_in; /* next input byte */
  55. uInt avail_in; /* number of bytes available at next_in */
  56. uLong total_in; /* total nb of input bytes read so far */
  57. Bytef *next_out; /* next output byte should be put there */
  58. uInt avail_out; /* remaining free space at next_out */
  59. uLong total_out; /* total nb of bytes output so far */
  60. char *msg; /* last error message, NULL if no error */
  61. struct internal_state FAR *state; /* not visible by applications */
  62. alloc_func zalloc; /* used to allocate the internal state */
  63. free_func zfree; /* used to free the internal state */
  64. voidpf opaque; /* private data object passed to zalloc and zfree */
  65. int data_type; /* best guess about the data type: ascii or binary */
  66. uLong adler; /* adler32 value of the uncompressed data */
  67. uLong reserved; /* reserved for future use */
  68. } z_stream;
  69. typedef z_stream FAR *z_streamp;
  70. /*
  71. The application must update next_in and avail_in when avail_in has
  72. dropped to zero. It must update next_out and avail_out when avail_out
  73. has dropped to zero. The application must initialize zalloc, zfree and
  74. opaque before calling the init function. All other fields are set by the
  75. compression library and must not be updated by the application.
  76. The opaque value provided by the application will be passed as the first
  77. parameter for calls of zalloc and zfree. This can be useful for custom
  78. memory management. The compression library attaches no meaning to the
  79. opaque value.
  80. zalloc must return Z_NULL if there is not enough memory for the object.
  81. On 16-bit systems, the functions zalloc and zfree must be able to allocate
  82. exactly 65536 bytes, but will not be required to allocate more than this
  83. if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  84. pointers returned by zalloc for objects of exactly 65536 bytes *must*
  85. have their offset normalized to zero. The default allocation function
  86. provided by this library ensures this (see zutil.c). To reduce memory
  87. requirements and avoid any allocation of 64K objects, at the expense of
  88. compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  89. The fields total_in and total_out can be used for statistics or
  90. progress reports. After compression, total_in holds the total size of
  91. the uncompressed data and may be saved for use in the decompressor
  92. (particularly if the decompressor wants to decompress everything in
  93. a single step).
  94. */
  95. /* constants */
  96. #define Z_NO_FLUSH 0
  97. #define Z_PARTIAL_FLUSH 1
  98. #define Z_SYNC_FLUSH 2
  99. #define Z_FULL_FLUSH 3
  100. #define Z_FINISH 4
  101. /* Allowed flush values; see deflate() below for details */
  102. #define Z_OK 0
  103. #define Z_STREAM_END 1
  104. #define Z_NEED_DICT 2
  105. #define Z_ERRNO (-1)
  106. #define Z_STREAM_ERROR (-2)
  107. #define Z_DATA_ERROR (-3)
  108. #define Z_MEM_ERROR (-4)
  109. #define Z_BUF_ERROR (-5)
  110. #define Z_VERSION_ERROR (-6)
  111. /* Return codes for the compression/decompression functions. Negative
  112. * values are errors, positive values are used for special but normal events.
  113. */
  114. #define Z_NO_COMPRESSION 0
  115. #define Z_BEST_SPEED 1
  116. #define Z_BEST_COMPRESSION 9
  117. #define Z_DEFAULT_COMPRESSION (-1)
  118. /* compression levels */
  119. #define Z_FILTERED 1
  120. #define Z_HUFFMAN_ONLY 2
  121. #define Z_DEFAULT_STRATEGY 0
  122. /* compression strategy; see deflateInit2() below for details */
  123. #define Z_BINARY 0
  124. #define Z_ASCII 1
  125. #define Z_UNKNOWN 2
  126. /* Possible values of the data_type field */
  127. #define Z_DEFLATED 8
  128. /* The deflate compression method (the only one supported in this version) */
  129. #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
  130. #define zlib_version zlibVersion()
  131. /* for compatibility with versions < 1.0.2 */
  132. /* basic functions */
  133. extern const char * EXPORT zlibVersion OF((void));
  134. /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
  135. If the first character differs, the library code actually used is
  136. not compatible with the zlib.h header file used by the application.
  137. This check is automatically made by deflateInit and inflateInit.
  138. */
  139. /*
  140. extern int EXPORT deflateInit OF((z_streamp strm, int level));
  141. Initializes the internal stream state for compression. The fields
  142. zalloc, zfree and opaque must be initialized before by the caller.
  143. If zalloc and zfree are set to Z_NULL, deflateInit updates them to
  144. use default allocation functions.
  145. The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
  146. 1 gives best speed, 9 gives best compression, 0 gives no compression at
  147. all (the input data is simply copied a block at a time).
  148. Z_DEFAULT_COMPRESSION requests a default compromise between speed and
  149. compression (currently equivalent to level 6).
  150. deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  151. enough memory, Z_STREAM_ERROR if level is not a valid compression level,
  152. Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
  153. with the version assumed by the caller (ZLIB_VERSION).
  154. msg is set to null if there is no error message. deflateInit does not
  155. perform any compression: this will be done by deflate().
  156. */
  157. extern int EXPORT deflate OF((z_streamp strm, int flush));
  158. /*
  159. Performs one or both of the following actions:
  160. - Compress more input starting at next_in and update next_in and avail_in
  161. accordingly. If not all input can be processed (because there is not
  162. enough room in the output buffer), next_in and avail_in are updated and
  163. processing will resume at this point for the next call of deflate().
  164. - Provide more output starting at next_out and update next_out and avail_out
  165. accordingly. This action is forced if the parameter flush is non zero.
  166. Forcing flush frequently degrades the compression ratio, so this parameter
  167. should be set only when necessary (in interactive applications).
  168. Some output may be provided even if flush is not set.
  169. Before the call of deflate(), the application should ensure that at least
  170. one of the actions is possible, by providing more input and/or consuming
  171. more output, and updating avail_in or avail_out accordingly; avail_out
  172. should never be zero before the call. The application can consume the
  173. compressed output when it wants, for example when the output buffer is full
  174. (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
  175. and with zero avail_out, it must be called again after making room in the
  176. output buffer because there might be more output pending.
  177. If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression
  178. block is terminated and flushed to the output buffer so that the
  179. decompressor can get all input data available so far. For method 9, a future
  180. variant on method 8, the current block will be flushed but not terminated.
  181. Z_SYNC_FLUSH has the same effect as partial flush except that the compressed
  182. output is byte aligned (the compressor can clear its internal bit buffer)
  183. and the current block is always terminated; this can be useful if the
  184. compressor has to be restarted from scratch after an interruption (in which
  185. case the internal state of the compressor may be lost).
  186. If flush is set to Z_FULL_FLUSH, the compression block is terminated, a
  187. special marker is output and the compression dictionary is discarded; this
  188. is useful to allow the decompressor to synchronize if one compressed block
  189. has been damaged (see inflateSync below). Flushing degrades compression and
  190. so should be used only when necessary. Using Z_FULL_FLUSH too often can
  191. seriously degrade the compression. If deflate returns with avail_out == 0,
  192. this function must be called again with the same value of the flush
  193. parameter and more output space (updated avail_out), until the flush is
  194. complete (deflate returns with non-zero avail_out).
  195. If the parameter flush is set to Z_FINISH, pending input is processed,
  196. pending output is flushed and deflate returns with Z_STREAM_END if there
  197. was enough output space; if deflate returns with Z_OK, this function must be
  198. called again with Z_FINISH and more output space (updated avail_out) but no
  199. more input data, until it returns with Z_STREAM_END or an error. After
  200. deflate has returned Z_STREAM_END, the only possible operations on the
  201. stream are deflateReset or deflateEnd.
  202. Z_FINISH can be used immediately after deflateInit if all the compression
  203. is to be done in a single step. In this case, avail_out must be at least
  204. 0.1% larger than avail_in plus 12 bytes. If deflate does not return
  205. Z_STREAM_END, then it must be called again as described above.
  206. deflate() may update data_type if it can make a good guess about
  207. the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
  208. binary. This field is only for information purposes and does not affect
  209. the compression algorithm in any manner.
  210. deflate() returns Z_OK if some progress has been made (more input
  211. processed or more output produced), Z_STREAM_END if all input has been
  212. consumed and all output has been produced (only when flush is set to
  213. Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
  214. if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible.
  215. */
  216. extern int EXPORT deflateEnd OF((z_streamp strm));
  217. /*
  218. All dynamically allocated data structures for this stream are freed.
  219. This function discards any unprocessed input and does not flush any
  220. pending output.
  221. deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
  222. stream state was inconsistent, Z_DATA_ERROR if the stream was freed
  223. prematurely (some input or output was discarded). In the error case,
  224. msg may be set but then points to a static string (which must not be
  225. deallocated).
  226. */
  227. /*
  228. extern int EXPORT inflateInit OF((z_streamp strm));
  229. Initializes the internal stream state for decompression. The fields
  230. zalloc, zfree and opaque must be initialized before by the caller. If
  231. zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
  232. allocation functions.
  233. inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  234. enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
  235. with the version assumed by the caller. msg is set to null if there is no
  236. error message. inflateInit does not perform any decompression: this will be
  237. done by inflate().
  238. */
  239. extern int EXPORT inflate OF((z_streamp strm, int flush));
  240. /*
  241. Performs one or both of the following actions:
  242. - Decompress more input starting at next_in and update next_in and avail_in
  243. accordingly. If not all input can be processed (because there is not
  244. enough room in the output buffer), next_in is updated and processing
  245. will resume at this point for the next call of inflate().
  246. - Provide more output starting at next_out and update next_out and avail_out
  247. accordingly. inflate() provides as much output as possible, until there
  248. is no more input data or no more space in the output buffer (see below
  249. about the flush parameter).
  250. Before the call of inflate(), the application should ensure that at least
  251. one of the actions is possible, by providing more input and/or consuming
  252. more output, and updating the next_* and avail_* values accordingly.
  253. The application can consume the uncompressed output when it wants, for
  254. example when the output buffer is full (avail_out == 0), or after each
  255. call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  256. must be called again after making room in the output buffer because there
  257. might be more output pending.
  258. If the parameter flush is set to Z_PARTIAL_FLUSH, inflate flushes as much
  259. output as possible to the output buffer. The flushing behavior of inflate is
  260. not specified for values of the flush parameter other than Z_PARTIAL_FLUSH
  261. and Z_FINISH, but the current implementation actually flushes as much output
  262. as possible anyway.
  263. inflate() should normally be called until it returns Z_STREAM_END or an
  264. error. However if all decompression is to be performed in a single step
  265. (a single call of inflate), the parameter flush should be set to
  266. Z_FINISH. In this case all pending input is processed and all pending
  267. output is flushed; avail_out must be large enough to hold all the
  268. uncompressed data. (The size of the uncompressed data may have been saved
  269. by the compressor for this purpose.) The next operation on this stream must
  270. be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  271. is never required, but can be used to inform inflate that a faster routine
  272. may be used for the single inflate() call.
  273. inflate() returns Z_OK if some progress has been made (more input
  274. processed or more output produced), Z_STREAM_END if the end of the
  275. compressed data has been reached and all uncompressed output has been
  276. produced, Z_NEED_DICT if a preset dictionary is needed at this point (see
  277. inflateSetDictionary below), Z_DATA_ERROR if the input data was corrupted,
  278. Z_STREAM_ERROR if the stream structure was inconsistent (for example if
  279. next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
  280. Z_BUF_ERROR if no progress is possible or if there was not enough room in
  281. the output buffer when Z_FINISH is used. In the Z_DATA_ERROR case, the
  282. application may then call inflateSync to look for a good compression block.
  283. In the Z_NEED_DICT case, strm->adler is set to the Adler32 value of the
  284. dictionary chosen by the compressor.
  285. */
  286. extern int EXPORT inflateEnd OF((z_streamp strm));
  287. /*
  288. All dynamically allocated data structures for this stream are freed.
  289. This function discards any unprocessed input and does not flush any
  290. pending output.
  291. inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  292. was inconsistent. In the error case, msg may be set but then points to a
  293. static string (which must not be deallocated).
  294. */
  295. /* Advanced functions */
  296. /*
  297. The following functions are needed only in some special applications.
  298. */
  299. /*
  300. extern int EXPORT deflateInit2 OF((z_streamp strm,
  301. int level,
  302. int method,
  303. int windowBits,
  304. int memLevel,
  305. int strategy));
  306. This is another version of deflateInit with more compression options. The
  307. fields next_in, zalloc, zfree and opaque must be initialized before by
  308. the caller.
  309. The method parameter is the compression method. It must be Z_DEFLATED in
  310. this version of the library. (Method 9 will allow a 64K history buffer and
  311. partial block flushes.)
  312. The windowBits parameter is the base two logarithm of the window size
  313. (the size of the history buffer). It should be in the range 8..15 for this
  314. version of the library (the value 16 will be allowed for method 9). Larger
  315. values of this parameter result in better compression at the expense of
  316. memory usage. The default value is 15 if deflateInit is used instead.
  317. The memLevel parameter specifies how much memory should be allocated
  318. for the internal compression state. memLevel=1 uses minimum memory but
  319. is slow and reduces compression ratio; memLevel=9 uses maximum memory
  320. for optimal speed. The default value is 8. See zconf.h for total memory
  321. usage as a function of windowBits and memLevel.
  322. The strategy parameter is used to tune the compression algorithm. Use the
  323. value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
  324. filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
  325. string match). Filtered data consists mostly of small values with a
  326. somewhat random distribution. In this case, the compression algorithm is
  327. tuned to compress them better. The effect of Z_FILTERED is to force more
  328. Huffman coding and less string matching; it is somewhat intermediate
  329. between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
  330. the compression ratio but not the correctness of the compressed output even
  331. if it is not set appropriately.
  332. If next_in is not null, the library will use this buffer to hold also
  333. some history information; the buffer must either hold the entire input
  334. data, or have at least 1<<(windowBits+1) bytes and be writable. If next_in
  335. is null, the library will allocate its own history buffer (and leave next_in
  336. null). next_out need not be provided here but must be provided by the
  337. application for the next call of deflate().
  338. If the history buffer is provided by the application, next_in must
  339. must never be changed by the application since the compressor maintains
  340. information inside this buffer from call to call; the application
  341. must provide more input only by increasing avail_in. next_in is always
  342. reset by the library in this case.
  343. deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
  344. not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
  345. an invalid method). msg is set to null if there is no error message.
  346. deflateInit2 does not perform any compression: this will be done by
  347. deflate().
  348. */
  349. extern int EXPORT deflateSetDictionary OF((z_streamp strm,
  350. const Bytef *dictionary,
  351. uInt dictLength));
  352. /*
  353. Initializes the compression dictionary (history buffer) from the given
  354. byte sequence without producing any compressed output. This function must
  355. be called immediately after deflateInit or deflateInit2, before any call
  356. of deflate. The compressor and decompressor must use exactly the same
  357. dictionary (see inflateSetDictionary).
  358. The dictionary should consist of strings (byte sequences) that are likely
  359. to be encountered later in the data to be compressed, with the most commonly
  360. used strings preferably put towards the end of the dictionary. Using a
  361. dictionary is most useful when the data to be compressed is short and
  362. can be predicted with good accuracy; the data can then be compressed better
  363. than with the default empty dictionary. In this version of the library,
  364. only the last 32K bytes of the dictionary are used.
  365. Upon return of this function, strm->adler is set to the Adler32 value
  366. of the dictionary; the decompressor may later use this value to determine
  367. which dictionary has been used by the compressor. (The Adler32 value
  368. applies to the whole dictionary even if only a subset of the dictionary is
  369. actually used by the compressor.)
  370. deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
  371. parameter is invalid (such as NULL dictionary) or the stream state
  372. is inconsistent (for example if deflate has already been called for this
  373. stream). deflateSetDictionary does not perform any compression: this will
  374. be done by deflate().
  375. */
  376. extern int EXPORT deflateCopy OF((z_streamp dest,
  377. z_streamp source));
  378. /*
  379. Sets the destination stream as a complete copy of the source stream. If
  380. the source stream is using an application-supplied history buffer, a new
  381. buffer is allocated for the destination stream. The compressed output
  382. buffer is always application-supplied. It's the responsibility of the
  383. application to provide the correct values of next_out and avail_out for the
  384. next call of deflate.
  385. This function can be useful when several compression strategies will be
  386. tried, for example when there are several ways of pre-processing the input
  387. data with a filter. The streams that will be discarded should then be freed
  388. by calling deflateEnd. Note that deflateCopy duplicates the internal
  389. compression state which can be quite large, so this strategy is slow and
  390. can consume lots of memory.
  391. deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  392. enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  393. (such as zalloc being NULL). msg is left unchanged in both source and
  394. destination.
  395. */
  396. extern int EXPORT deflateReset OF((z_streamp strm));
  397. /*
  398. This function is equivalent to deflateEnd followed by deflateInit,
  399. but does not free and reallocate all the internal compression state.
  400. The stream will keep the same compression level and any other attributes
  401. that may have been set by deflateInit2.
  402. deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  403. stream state was inconsistent (such as zalloc or state being NULL).
  404. */
  405. extern int EXPORT deflateParams OF((z_streamp strm, int level, int strategy));
  406. /*
  407. Dynamically update the compression level and compression strategy.
  408. This can be used to switch between compression and straight copy of
  409. the input data, or to switch to a different kind of input data requiring
  410. a different strategy. If the compression level is changed, the input
  411. available so far is compressed with the old level (and may be flushed);
  412. the new level will take effect only at the next call of deflate().
  413. Before the call of deflateParams, the stream state must be set as for
  414. a call of deflate(), since the currently available input may have to
  415. be compressed and flushed. In particular, strm->avail_out must be non-zero.
  416. deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
  417. stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
  418. if strm->avail_out was zero.
  419. */
  420. /*
  421. extern int EXPORT inflateInit2 OF((z_streamp strm,
  422. int windowBits));
  423. This is another version of inflateInit with more compression options. The
  424. fields next_out, zalloc, zfree and opaque must be initialized before by
  425. the caller.
  426. The windowBits parameter is the base two logarithm of the maximum window
  427. size (the size of the history buffer). It should be in the range 8..15 for
  428. this version of the library (the value 16 will be allowed soon). The
  429. default value is 15 if inflateInit is used instead. If a compressed stream
  430. with a larger window size is given as input, inflate() will return with
  431. the error code Z_DATA_ERROR instead of trying to allocate a larger window.
  432. If next_out is not null, the library will use this buffer for the history
  433. buffer; the buffer must either be large enough to hold the entire output
  434. data, or have at least 1<<windowBits bytes. If next_out is null, the
  435. library will allocate its own buffer (and leave next_out null). next_in
  436. need not be provided here but must be provided by the application for the
  437. next call of inflate().
  438. If the history buffer is provided by the application, next_out must
  439. never be changed by the application since the decompressor maintains
  440. history information inside this buffer from call to call; the application
  441. can only reset next_out to the beginning of the history buffer when
  442. avail_out is zero and all output has been consumed.
  443. inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
  444. not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
  445. windowBits < 8). msg is set to null if there is no error message.
  446. inflateInit2 does not perform any decompression: this will be done by
  447. inflate().
  448. */
  449. extern int EXPORT inflateSetDictionary OF((z_streamp strm,
  450. const Bytef *dictionary,
  451. uInt dictLength));
  452. /*
  453. Initializes the decompression dictionary (history buffer) from the given
  454. uncompressed byte sequence. This function must be called immediately after
  455. a call of inflate if this call returned Z_NEED_DICT. The dictionary chosen
  456. by the compressor can be determined from the Adler32 value returned by this
  457. call of inflate. The compressor and decompressor must use exactly the same
  458. dictionary (see deflateSetDictionary).
  459. inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  460. parameter is invalid (such as NULL dictionary) or the stream state is
  461. inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  462. expected one (incorrect Adler32 value). inflateSetDictionary does not
  463. perform any decompression: this will be done by subsequent calls of
  464. inflate().
  465. */
  466. extern int EXPORT inflateSync OF((z_streamp strm));
  467. /*
  468. Skips invalid compressed data until the special marker (see deflate()
  469. above) can be found, or until all available input is skipped. No output
  470. is provided.
  471. inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR
  472. if no more input was provided, Z_DATA_ERROR if no marker has been found,
  473. or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  474. case, the application may save the current current value of total_in which
  475. indicates where valid compressed data was found. In the error case, the
  476. application may repeatedly call inflateSync, providing more input each time,
  477. until success or end of the input data.
  478. */
  479. extern int EXPORT inflateReset OF((z_streamp strm));
  480. /*
  481. This function is equivalent to inflateEnd followed by inflateInit,
  482. but does not free and reallocate all the internal decompression state.
  483. The stream will keep attributes that may have been set by inflateInit2.
  484. inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  485. stream state was inconsistent (such as zalloc or state being NULL).
  486. */
  487. /* utility functions */
  488. /*
  489. The following utility functions are implemented on top of the
  490. basic stream-oriented functions. To simplify the interface, some
  491. default options are assumed (compression level, window size,
  492. standard memory allocation functions). The source code of these
  493. utility functions can easily be modified if you need special options.
  494. */
  495. extern int EXPORT compress OF((Bytef *dest, uLongf *destLen,
  496. const Bytef *source, uLong sourceLen));
  497. /*
  498. Compresses the source buffer into the destination buffer. sourceLen is
  499. the byte length of the source buffer. Upon entry, destLen is the total
  500. size of the destination buffer, which must be at least 0.1% larger than
  501. sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
  502. compressed buffer.
  503. This function can be used to compress a whole file at once if the
  504. input file is mmap'ed.
  505. compress returns Z_OK if success, Z_MEM_ERROR if there was not
  506. enough memory, Z_BUF_ERROR if there was not enough room in the output
  507. buffer.
  508. */
  509. extern int EXPORT uncompress OF((Bytef *dest, uLongf *destLen,
  510. const Bytef *source, uLong sourceLen));
  511. /*
  512. Decompresses the source buffer into the destination buffer. sourceLen is
  513. the byte length of the source buffer. Upon entry, destLen is the total
  514. size of the destination buffer, which must be large enough to hold the
  515. entire uncompressed data. (The size of the uncompressed data must have
  516. been saved previously by the compressor and transmitted to the decompressor
  517. by some mechanism outside the scope of this compression library.)
  518. Upon exit, destLen is the actual size of the compressed buffer.
  519. This function can be used to decompress a whole file at once if the
  520. input file is mmap'ed.
  521. uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  522. enough memory, Z_BUF_ERROR if there was not enough room in the output
  523. buffer, or Z_DATA_ERROR if the input data was corrupted.
  524. */
  525. typedef voidp gzFile;
  526. extern gzFile EXPORT gzopen OF((const char *path, const char *mode));
  527. /*
  528. Opens a gzip (.gz) file for reading or writing. The mode parameter
  529. is as in fopen ("rb" or "wb") but can also include a compression level
  530. ("wb9"). gzopen can be used to read a file which is not in gzip format;
  531. in this case gzread will directly read from the file without decompression.
  532. gzopen returns NULL if the file could not be opened or if there was
  533. insufficient memory to allocate the (de)compression state; errno
  534. can be checked to distinguish the two cases (if errno is zero, the
  535. zlib error is Z_MEM_ERROR).
  536. */
  537. extern gzFile EXPORT gzdopen OF((int fd, const char *mode));
  538. /*
  539. gzdopen() associates a gzFile with the file descriptor fd. File
  540. descriptors are obtained from calls like open, dup, creat, pipe or
  541. fileno (in the file has been previously opened with fopen).
  542. The mode parameter is as in gzopen.
  543. The next call of gzclose on the returned gzFile will also close the
  544. file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
  545. descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
  546. gzdopen returns NULL if there was insufficient memory to allocate
  547. the (de)compression state.
  548. */
  549. extern int EXPORT gzread OF((gzFile file, voidp buf, unsigned len));
  550. /*
  551. Reads the given number of uncompressed bytes from the compressed file.
  552. If the input file was not in gzip format, gzread copies the given number
  553. of bytes into the buffer.
  554. gzread returns the number of uncompressed bytes actually read (0 for
  555. end of file, -1 for error). */
  556. extern int EXPORT gzwrite OF((gzFile file, const voidp buf, unsigned len));
  557. /*
  558. Writes the given number of uncompressed bytes into the compressed file.
  559. gzwrite returns the number of uncompressed bytes actually written
  560. (0 in case of error).
  561. */
  562. extern int EXPORT gzflush OF((gzFile file, int flush));
  563. /*
  564. Flushes all pending output into the compressed file. The parameter
  565. flush is as in the deflate() function. The return value is the zlib
  566. error number (see function gzerror below). gzflush returns Z_OK if
  567. the flush parameter is Z_FINISH and all output could be flushed.
  568. gzflush should be called only when strictly necessary because it can
  569. degrade compression.
  570. */
  571. extern int EXPORT gzclose OF((gzFile file));
  572. /*
  573. Flushes all pending output if necessary, closes the compressed file
  574. and deallocates all the (de)compression state. The return value is the zlib
  575. error number (see function gzerror below).
  576. */
  577. extern const char * EXPORT gzerror OF((gzFile file, int *errnum));
  578. /*
  579. Returns the error message for the last error which occurred on the
  580. given compressed file. errnum is set to zlib error number. If an
  581. error occurred in the file system and not in the compression library,
  582. errnum is set to Z_ERRNO and the application may consult errno
  583. to get the exact error code.
  584. */
  585. /* checksum functions */
  586. /*
  587. These functions are not related to compression but are exported
  588. anyway because they might be useful in applications using the
  589. compression library.
  590. */
  591. extern uLong EXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
  592. /*
  593. Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  594. return the updated checksum. If buf is NULL, this function returns
  595. the required initial value for the checksum.
  596. An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  597. much faster. Usage example:
  598. uLong adler = adler32(0L, Z_NULL, 0);
  599. while (read_buffer(buffer, length) != EOF) {
  600. adler = adler32(adler, buffer, length);
  601. }
  602. if (adler != original_adler) error();
  603. */
  604. extern uLong EXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
  605. /*
  606. Update a running crc with the bytes buf[0..len-1] and return the updated
  607. crc. If buf is NULL, this function returns the required initial value
  608. for the crc. Pre- and post-conditioning (one's complement) is performed
  609. within this function so it shouldn't be done by the application.
  610. Usage example:
  611. uLong crc = crc32(0L, Z_NULL, 0);
  612. while (read_buffer(buffer, length) != EOF) {
  613. crc = crc32(crc, buffer, length);
  614. }
  615. if (crc != original_crc) error();
  616. */
  617. /* various hacks, don't look :) */
  618. /* deflateInit and inflateInit are macros to allow checking the zlib version
  619. * and the compiler's view of z_stream:
  620. */
  621. extern int EXPORT deflateInit_ OF((z_streamp strm, int level,
  622. const char *version, int stream_size));
  623. extern int EXPORT inflateInit_ OF((z_streamp strm,
  624. const char *version, int stream_size));
  625. extern int EXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
  626. int windowBits, int memLevel, int strategy,
  627. const char *version, int stream_size));
  628. extern int EXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
  629. const char *version, int stream_size));
  630. #define deflateInit(strm, level) \
  631. deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
  632. #define inflateInit(strm) \
  633. inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
  634. #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
  635. deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
  636. (strategy), ZLIB_VERSION, sizeof(z_stream))
  637. #define inflateInit2(strm, windowBits) \
  638. inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
  639. #if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
  640. struct internal_state {int dummy;}; /* hack for buggy compilers */
  641. #endif
  642. uLongf *get_crc_table OF((void)); /* can be used by asm versions of crc32() */
  643. #ifdef __cplusplus
  644. }
  645. #endif
  646. #endif /* _ZLIB_H */