zlib.h 65 KB

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