zlib.h 36 KB

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