zlib.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * This file is derived from zlib.h and zconf.h from the zlib-0.95
  3. * distribution by Jean-loup Gailly and Mark Adler, with some additions
  4. * by Paul Mackerras to aid in implementing Deflate compression and
  5. * decompression for PPP packets.
  6. */
  7. /*
  8. * ==FILEVERSION 960122==
  9. *
  10. * This marker is used by the Linux installation script to determine
  11. * whether an up-to-date version of this file is already installed.
  12. */
  13. /* zlib.h -- interface of the 'zlib' general purpose compression library
  14. version 0.95, Aug 16th, 1995.
  15. Copyright (C) 1995 Jean-loup Gailly and Mark Adler
  16. This software is provided 'as-is', without any express or implied
  17. warranty. In no event will the authors be held liable for any damages
  18. arising from the use of this software.
  19. Permission is granted to anyone to use this software for any purpose,
  20. including commercial applications, and to alter it and redistribute it
  21. freely, subject to the following restrictions:
  22. 1. The origin of this software must not be misrepresented; you must not
  23. claim that you wrote the original software. If you use this software
  24. in a product, an acknowledgment in the product documentation would be
  25. appreciated but is not required.
  26. 2. Altered source versions must be plainly marked as such, and must not be
  27. misrepresented as being the original software.
  28. 3. This notice may not be removed or altered from any source distribution.
  29. Jean-loup Gailly Mark Adler
  30. gzip@prep.ai.mit.edu madler@alumni.caltech.edu
  31. */
  32. #ifndef _ZLIB_H
  33. #define _ZLIB_H
  34. /* #include "zconf.h" */ /* included directly here */
  35. /* zconf.h -- configuration of the zlib compression library
  36. * Copyright (C) 1995 Jean-loup Gailly.
  37. * For conditions of distribution and use, see copyright notice in zlib.h
  38. */
  39. /* From: zconf.h,v 1.12 1995/05/03 17:27:12 jloup Exp */
  40. /*
  41. The library does not install any signal handler. It is recommended to
  42. add at least a handler for SIGSEGV when decompressing; the library checks
  43. the consistency of the input data whenever possible but may go nuts
  44. for some forms of corrupted input.
  45. */
  46. /*
  47. * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  48. * than 64k bytes at a time (needed on systems with 16-bit int).
  49. * Compile with -DUNALIGNED_OK if it is OK to access shorts or ints
  50. * at addresses which are not a multiple of their size.
  51. * Under DOS, -DFAR=far or -DFAR=__far may be needed.
  52. */
  53. #ifndef STDC
  54. # if defined(MSDOS) || defined(__STDC__) || defined(__cplusplus)
  55. # define STDC
  56. # endif
  57. #endif
  58. #ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */
  59. # include <unix.h>
  60. #endif
  61. /* Maximum value for memLevel in deflateInit2 */
  62. #ifndef MAX_MEM_LEVEL
  63. # ifdef MAXSEG_64K
  64. # define MAX_MEM_LEVEL 8
  65. # else
  66. # define MAX_MEM_LEVEL 9
  67. # endif
  68. #endif
  69. #ifndef FAR
  70. # define FAR
  71. #endif
  72. /* Maximum value for windowBits in deflateInit2 and inflateInit2 */
  73. #ifndef MAX_WBITS
  74. # define MAX_WBITS 15 /* 32K LZ77 window */
  75. #endif
  76. /* The memory requirements for deflate are (in bytes):
  77. 1 << (windowBits+2) + 1 << (memLevel+9)
  78. that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  79. plus a few kilobytes for small objects. For example, if you want to reduce
  80. the default memory requirements from 256K to 128K, compile with
  81. make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
  82. Of course this will generally degrade compression (there's no free lunch).
  83. The memory requirements for inflate are (in bytes) 1 << windowBits
  84. that is, 32K for windowBits=15 (default value) plus a few kilobytes
  85. for small objects.
  86. */
  87. /* Type declarations */
  88. #ifndef OF /* function prototypes */
  89. # ifdef STDC
  90. # define OF(args) args
  91. # else
  92. # define OF(args) ()
  93. # endif
  94. #endif
  95. typedef unsigned char Byte; /* 8 bits */
  96. typedef unsigned int uInt; /* 16 bits or more */
  97. typedef unsigned long uLong; /* 32 bits or more */
  98. typedef Byte FAR Bytef;
  99. typedef char FAR charf;
  100. typedef int FAR intf;
  101. typedef uInt FAR uIntf;
  102. typedef uLong FAR uLongf;
  103. #ifdef STDC
  104. typedef void FAR *voidpf;
  105. typedef void *voidp;
  106. #else
  107. typedef Byte FAR *voidpf;
  108. typedef Byte *voidp;
  109. #endif
  110. /* end of original zconf.h */
  111. #define ZLIB_VERSION "0.95P"
  112. /*
  113. The 'zlib' compression library provides in-memory compression and
  114. decompression functions, including integrity checks of the uncompressed
  115. data. This version of the library supports only one compression method
  116. (deflation) but other algorithms may be added later and will have the same
  117. stream interface.
  118. For compression the application must provide the output buffer and
  119. may optionally provide the input buffer for optimization. For decompression,
  120. the application must provide the input buffer and may optionally provide
  121. the output buffer for optimization.
  122. Compression can be done in a single step if the buffers are large
  123. enough (for example if an input file is mmap'ed), or can be done by
  124. repeated calls of the compression function. In the latter case, the
  125. application must provide more input and/or consume the output
  126. (providing more output space) before each call.
  127. */
  128. typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
  129. typedef void (*free_func) OF((voidpf opaque, voidpf address, uInt nbytes));
  130. typedef void (*cb_func) OF((Bytef *buf, uInt len));
  131. struct internal_state;
  132. typedef struct z_stream_s {
  133. Bytef *next_in; /* next input byte */
  134. uInt avail_in; /* number of bytes available at next_in */
  135. uLong total_in; /* total nb of input bytes read so far */
  136. Bytef *next_out; /* next output byte should be put there */
  137. uInt avail_out; /* remaining free space at next_out */
  138. uLong total_out; /* total nb of bytes output so far */
  139. char *msg; /* last error message, NULL if no error */
  140. struct internal_state FAR *state; /* not visible by applications */
  141. alloc_func zalloc; /* used to allocate the internal state */
  142. free_func zfree; /* used to free the internal state */
  143. voidp opaque; /* private data object passed to zalloc and zfree */
  144. Byte data_type; /* best guess about the data type: ascii or binary */
  145. cb_func outcb; /* called regularly just before blocks of output */
  146. } z_stream;
  147. /*
  148. The application must update next_in and avail_in when avail_in has
  149. dropped to zero. It must update next_out and avail_out when avail_out
  150. has dropped to zero. The application must initialize zalloc, zfree and
  151. opaque before calling the init function. All other fields are set by the
  152. compression library and must not be updated by the application.
  153. The opaque value provided by the application will be passed as the first
  154. parameter for calls of zalloc and zfree. This can be useful for custom
  155. memory management. The compression library attaches no meaning to the
  156. opaque value.
  157. zalloc must return Z_NULL if there is not enough memory for the object.
  158. On 16-bit systems, the functions zalloc and zfree must be able to allocate
  159. exactly 65536 bytes, but will not be required to allocate more than this
  160. if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  161. pointers returned by zalloc for objects of exactly 65536 bytes *must*
  162. have their offset normalized to zero. The default allocation function
  163. provided by this library ensures this (see zutil.c). To reduce memory
  164. requirements and avoid any allocation of 64K objects, at the expense of
  165. compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  166. The fields total_in and total_out can be used for statistics or
  167. progress reports. After compression, total_in holds the total size of
  168. the uncompressed data and may be saved for use in the decompressor
  169. (particularly if the decompressor wants to decompress everything in
  170. a single step).
  171. */
  172. /* constants */
  173. #define Z_NO_FLUSH 0
  174. #define Z_PARTIAL_FLUSH 1
  175. #define Z_FULL_FLUSH 2
  176. #define Z_SYNC_FLUSH 3 /* experimental: partial_flush + byte align */
  177. #define Z_FINISH 4
  178. #define Z_PACKET_FLUSH 5
  179. /* See deflate() below for the usage of these constants */
  180. #define Z_OK 0
  181. #define Z_STREAM_END 1
  182. #define Z_ERRNO (-1)
  183. #define Z_STREAM_ERROR (-2)
  184. #define Z_DATA_ERROR (-3)
  185. #define Z_MEM_ERROR (-4)
  186. #define Z_BUF_ERROR (-5)
  187. /* error codes for the compression/decompression functions */
  188. #define Z_BEST_SPEED 1
  189. #define Z_BEST_COMPRESSION 9
  190. #define Z_DEFAULT_COMPRESSION (-1)
  191. /* compression levels */
  192. #define Z_FILTERED 1
  193. #define Z_HUFFMAN_ONLY 2
  194. #define Z_DEFAULT_STRATEGY 0
  195. #define Z_BINARY 0
  196. #define Z_ASCII 1
  197. #define Z_UNKNOWN 2
  198. /* Used to set the data_type field */
  199. #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
  200. extern char *zlib_version;
  201. /* The application can compare zlib_version and ZLIB_VERSION for consistency.
  202. If the first character differs, the library code actually used is
  203. not compatible with the zlib.h header file used by the application.
  204. */
  205. /* basic functions */
  206. extern int inflateInit OF((z_stream *strm));
  207. /*
  208. Initializes the internal stream state for decompression. The fields
  209. zalloc and zfree must be initialized before by the caller. If zalloc and
  210. zfree are set to Z_NULL, inflateInit updates them to use default allocation
  211. functions.
  212. inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  213. enough memory. msg is set to null if there is no error message.
  214. inflateInit does not perform any decompression: this will be done by
  215. inflate().
  216. */
  217. extern int inflate OF((z_stream *strm, int flush));
  218. /*
  219. Performs one or both of the following actions:
  220. - Decompress more input starting at next_in and update next_in and avail_in
  221. accordingly. If not all input can be processed (because there is not
  222. enough room in the output buffer), next_in is updated and processing
  223. will resume at this point for the next call of inflate().
  224. - Provide more output starting at next_out and update next_out and avail_out
  225. accordingly. inflate() always provides as much output as possible
  226. (until there is no more input data or no more space in the output buffer).
  227. Before the call of inflate(), the application should ensure that at least
  228. one of the actions is possible, by providing more input and/or consuming
  229. more output, and updating the next_* and avail_* values accordingly.
  230. The application can consume the uncompressed output when it wants, for
  231. example when the output buffer is full (avail_out == 0), or after each
  232. call of inflate().
  233. If the parameter flush is set to Z_PARTIAL_FLUSH or Z_PACKET_FLUSH,
  234. inflate flushes as much output as possible to the output buffer. The
  235. flushing behavior of inflate is not specified for values of the flush
  236. parameter other than Z_PARTIAL_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the
  237. current implementation actually flushes as much output as possible
  238. anyway. For Z_PACKET_FLUSH, inflate checks that once all the input data
  239. has been consumed, it is expecting to see the length field of a stored
  240. block; if not, it returns Z_DATA_ERROR.
  241. inflate() should normally be called until it returns Z_STREAM_END or an
  242. error. However if all decompression is to be performed in a single step
  243. (a single call of inflate), the parameter flush should be set to
  244. Z_FINISH. In this case all pending input is processed and all pending
  245. output is flushed; avail_out must be large enough to hold all the
  246. uncompressed data. (The size of the uncompressed data may have been saved
  247. by the compressor for this purpose.) The next operation on this stream must
  248. be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  249. is never required, but can be used to inform inflate that a faster routine
  250. may be used for the single inflate() call.
  251. inflate() returns Z_OK if some progress has been made (more input
  252. processed or more output produced), Z_STREAM_END if the end of the
  253. compressed data has been reached and all uncompressed output has been
  254. produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if
  255. the stream structure was inconsistent (for example if next_in or next_out
  256. was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no
  257. progress is possible or if there was not enough room in the output buffer
  258. when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then
  259. call inflateSync to look for a good compression block. */
  260. extern int inflateEnd OF((z_stream *strm));
  261. /*
  262. All dynamically allocated data structures for this stream are freed.
  263. This function discards any unprocessed input and does not flush any
  264. pending output.
  265. inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  266. was inconsistent. In the error case, msg may be set but then points to a
  267. static string (which must not be deallocated).
  268. */
  269. /* advanced functions */
  270. extern int inflateInit2 OF((z_stream *strm,
  271. int windowBits));
  272. /*
  273. This is another version of inflateInit with more compression options. The
  274. fields next_out, zalloc and zfree must be initialized before by the caller.
  275. The windowBits parameter is the base two logarithm of the maximum window
  276. size (the size of the history buffer). It should be in the range 8..15 for
  277. this version of the library (the value 16 will be allowed soon). The
  278. default value is 15 if inflateInit is used instead. If a compressed stream
  279. with a larger window size is given as input, inflate() will return with
  280. the error code Z_DATA_ERROR instead of trying to allocate a larger window.
  281. If next_out is not null, the library will use this buffer for the history
  282. buffer; the buffer must either be large enough to hold the entire output
  283. data, or have at least 1<<windowBits bytes. If next_out is null, the
  284. library will allocate its own buffer (and leave next_out null). next_in
  285. need not be provided here but must be provided by the application for the
  286. next call of inflate().
  287. If the history buffer is provided by the application, next_out must
  288. never be changed by the application since the decompressor maintains
  289. history information inside this buffer from call to call; the application
  290. can only reset next_out to the beginning of the history buffer when
  291. avail_out is zero and all output has been consumed.
  292. inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
  293. not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
  294. windowBits < 8). msg is set to null if there is no error message.
  295. inflateInit2 does not perform any decompression: this will be done by
  296. inflate().
  297. */
  298. extern int inflateSync OF((z_stream *strm));
  299. /*
  300. Skips invalid compressed data until the special marker (see deflate()
  301. above) can be found, or until all available input is skipped. No output
  302. is provided.
  303. inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR
  304. if no more input was provided, Z_DATA_ERROR if no marker has been found,
  305. or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  306. case, the application may save the current current value of total_in which
  307. indicates where valid compressed data was found. In the error case, the
  308. application may repeatedly call inflateSync, providing more input each time,
  309. until success or end of the input data.
  310. */
  311. extern int inflateReset OF((z_stream *strm));
  312. /*
  313. This function is equivalent to inflateEnd followed by inflateInit,
  314. but does not free and reallocate all the internal decompression state.
  315. The stream will keep attributes that may have been set by inflateInit2.
  316. inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  317. stream state was inconsistent (such as zalloc or state being NULL).
  318. */
  319. extern int inflateIncomp OF((z_stream *strm));
  320. /*
  321. This function adds the data at next_in (avail_in bytes) to the output
  322. history without performing any output. There must be no pending output,
  323. and the decompressor must be expecting to see the start of a block.
  324. Calling this function is equivalent to decompressing a stored block
  325. containing the data at next_in (except that the data is not output).
  326. */
  327. /* checksum functions */
  328. /*
  329. This function is not related to compression but is exported
  330. anyway because it might be useful in applications using the
  331. compression library.
  332. */
  333. extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len));
  334. /*
  335. Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  336. return the updated checksum. If buf is NULL, this function returns
  337. the required initial value for the checksum.
  338. An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  339. much faster. Usage example:
  340. uLong adler = adler32(0L, Z_NULL, 0);
  341. while (read_buffer(buffer, length) != EOF) {
  342. adler = adler32(adler, buffer, length);
  343. }
  344. if (adler != original_adler) error();
  345. */
  346. #ifndef _Z_UTIL_H
  347. struct internal_state {int dummy;}; /* hack for buggy compilers */
  348. #endif
  349. #endif /* _ZLIB_H */