17dd7cddfSDavid du Colombier /* zlib.h -- interface of the 'zlib' general purpose compression library 2*593dc095SDavid du Colombier version 1.2.2, October 3rd, 2004 37dd7cddfSDavid du Colombier 4*593dc095SDavid du Colombier Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler 57dd7cddfSDavid du Colombier 67dd7cddfSDavid du Colombier This software is provided 'as-is', without any express or implied 77dd7cddfSDavid du Colombier warranty. In no event will the authors be held liable for any damages 87dd7cddfSDavid du Colombier arising from the use of this software. 97dd7cddfSDavid du Colombier 107dd7cddfSDavid du Colombier Permission is granted to anyone to use this software for any purpose, 117dd7cddfSDavid du Colombier including commercial applications, and to alter it and redistribute it 127dd7cddfSDavid du Colombier freely, subject to the following restrictions: 137dd7cddfSDavid du Colombier 147dd7cddfSDavid du Colombier 1. The origin of this software must not be misrepresented; you must not 157dd7cddfSDavid du Colombier claim that you wrote the original software. If you use this software 167dd7cddfSDavid du Colombier in a product, an acknowledgment in the product documentation would be 177dd7cddfSDavid du Colombier appreciated but is not required. 187dd7cddfSDavid du Colombier 2. Altered source versions must be plainly marked as such, and must not be 197dd7cddfSDavid du Colombier misrepresented as being the original software. 207dd7cddfSDavid du Colombier 3. This notice may not be removed or altered from any source distribution. 217dd7cddfSDavid du Colombier 227dd7cddfSDavid du Colombier Jean-loup Gailly Mark Adler 23*593dc095SDavid du Colombier jloup@gzip.org madler@alumni.caltech.edu 247dd7cddfSDavid du Colombier 257dd7cddfSDavid du Colombier 267dd7cddfSDavid du Colombier The data format used by the zlib library is described by RFCs (Request for 27*593dc095SDavid du Colombier Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt 287dd7cddfSDavid du Colombier (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). 297dd7cddfSDavid du Colombier */ 307dd7cddfSDavid du Colombier 31*593dc095SDavid du Colombier #ifndef ZLIB_H 32*593dc095SDavid du Colombier #define ZLIB_H 33*593dc095SDavid du Colombier 34*593dc095SDavid du Colombier #include "zconf.h" 357dd7cddfSDavid du Colombier 367dd7cddfSDavid du Colombier #ifdef __cplusplus 377dd7cddfSDavid du Colombier extern "C" { 387dd7cddfSDavid du Colombier #endif 397dd7cddfSDavid du Colombier 40*593dc095SDavid du Colombier #define ZLIB_VERSION "1.2.2" 41*593dc095SDavid du Colombier #define ZLIB_VERNUM 0x1220 427dd7cddfSDavid du Colombier 437dd7cddfSDavid du Colombier /* 447dd7cddfSDavid du Colombier The 'zlib' compression library provides in-memory compression and 457dd7cddfSDavid du Colombier decompression functions, including integrity checks of the uncompressed 467dd7cddfSDavid du Colombier data. This version of the library supports only one compression method 47*593dc095SDavid du Colombier (deflation) but other algorithms will be added later and will have the same 487dd7cddfSDavid du Colombier stream interface. 497dd7cddfSDavid du Colombier 507dd7cddfSDavid du Colombier Compression can be done in a single step if the buffers are large 517dd7cddfSDavid du Colombier enough (for example if an input file is mmap'ed), or can be done by 527dd7cddfSDavid du Colombier repeated calls of the compression function. In the latter case, the 537dd7cddfSDavid du Colombier application must provide more input and/or consume the output 547dd7cddfSDavid du Colombier (providing more output space) before each call. 557dd7cddfSDavid du Colombier 56*593dc095SDavid du Colombier The compressed data format used by default by the in-memory functions is 57*593dc095SDavid du Colombier the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped 58*593dc095SDavid du Colombier around a deflate stream, which is itself documented in RFC 1951. 59*593dc095SDavid du Colombier 60*593dc095SDavid du Colombier The library also supports reading and writing files in gzip (.gz) format 61*593dc095SDavid du Colombier with an interface similar to that of stdio using the functions that start 62*593dc095SDavid du Colombier with "gz". The gzip format is different from the zlib format. gzip is a 63*593dc095SDavid du Colombier gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. 64*593dc095SDavid du Colombier 65*593dc095SDavid du Colombier This library can optionally read and write gzip streams in memory as well. 66*593dc095SDavid du Colombier 67*593dc095SDavid du Colombier The zlib format was designed to be compact and fast for use in memory 68*593dc095SDavid du Colombier and on communications channels. The gzip format was designed for single- 69*593dc095SDavid du Colombier file compression on file systems, has a larger header than zlib to maintain 70*593dc095SDavid du Colombier directory information, and uses a different, slower check method than zlib. 71*593dc095SDavid du Colombier 72*593dc095SDavid du Colombier The library does not install any signal handler. The decoder checks 73*593dc095SDavid du Colombier the consistency of the compressed data, so the library should never 74*593dc095SDavid du Colombier crash even in case of corrupted input. 757dd7cddfSDavid du Colombier */ 767dd7cddfSDavid du Colombier 777dd7cddfSDavid du Colombier typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); 787dd7cddfSDavid du Colombier typedef void (*free_func) OF((voidpf opaque, voidpf address)); 797dd7cddfSDavid du Colombier 807dd7cddfSDavid du Colombier struct internal_state; 817dd7cddfSDavid du Colombier 827dd7cddfSDavid du Colombier typedef struct z_stream_s { 837dd7cddfSDavid du Colombier Bytef *next_in; /* next input byte */ 847dd7cddfSDavid du Colombier uInt avail_in; /* number of bytes available at next_in */ 857dd7cddfSDavid du Colombier uLong total_in; /* total nb of input bytes read so far */ 867dd7cddfSDavid du Colombier 877dd7cddfSDavid du Colombier Bytef *next_out; /* next output byte should be put there */ 887dd7cddfSDavid du Colombier uInt avail_out; /* remaining free space at next_out */ 897dd7cddfSDavid du Colombier uLong total_out; /* total nb of bytes output so far */ 907dd7cddfSDavid du Colombier 917dd7cddfSDavid du Colombier char *msg; /* last error message, NULL if no error */ 927dd7cddfSDavid du Colombier struct internal_state FAR *state; /* not visible by applications */ 937dd7cddfSDavid du Colombier 947dd7cddfSDavid du Colombier alloc_func zalloc; /* used to allocate the internal state */ 957dd7cddfSDavid du Colombier free_func zfree; /* used to free the internal state */ 967dd7cddfSDavid du Colombier voidpf opaque; /* private data object passed to zalloc and zfree */ 977dd7cddfSDavid du Colombier 987dd7cddfSDavid du Colombier int data_type; /* best guess about the data type: ascii or binary */ 997dd7cddfSDavid du Colombier uLong adler; /* adler32 value of the uncompressed data */ 1007dd7cddfSDavid du Colombier uLong reserved; /* reserved for future use */ 1017dd7cddfSDavid du Colombier } z_stream; 1027dd7cddfSDavid du Colombier 1037dd7cddfSDavid du Colombier typedef z_stream FAR *z_streamp; 1047dd7cddfSDavid du Colombier 1057dd7cddfSDavid du Colombier /* 1067dd7cddfSDavid du Colombier The application must update next_in and avail_in when avail_in has 1077dd7cddfSDavid du Colombier dropped to zero. It must update next_out and avail_out when avail_out 1087dd7cddfSDavid du Colombier has dropped to zero. The application must initialize zalloc, zfree and 1097dd7cddfSDavid du Colombier opaque before calling the init function. All other fields are set by the 1107dd7cddfSDavid du Colombier compression library and must not be updated by the application. 1117dd7cddfSDavid du Colombier 1127dd7cddfSDavid du Colombier The opaque value provided by the application will be passed as the first 1137dd7cddfSDavid du Colombier parameter for calls of zalloc and zfree. This can be useful for custom 1147dd7cddfSDavid du Colombier memory management. The compression library attaches no meaning to the 1157dd7cddfSDavid du Colombier opaque value. 1167dd7cddfSDavid du Colombier 1177dd7cddfSDavid du Colombier zalloc must return Z_NULL if there is not enough memory for the object. 118*593dc095SDavid du Colombier If zlib is used in a multi-threaded application, zalloc and zfree must be 119*593dc095SDavid du Colombier thread safe. 120*593dc095SDavid du Colombier 1217dd7cddfSDavid du Colombier On 16-bit systems, the functions zalloc and zfree must be able to allocate 1227dd7cddfSDavid du Colombier exactly 65536 bytes, but will not be required to allocate more than this 1237dd7cddfSDavid du Colombier if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, 1247dd7cddfSDavid du Colombier pointers returned by zalloc for objects of exactly 65536 bytes *must* 1257dd7cddfSDavid du Colombier have their offset normalized to zero. The default allocation function 1267dd7cddfSDavid du Colombier provided by this library ensures this (see zutil.c). To reduce memory 1277dd7cddfSDavid du Colombier requirements and avoid any allocation of 64K objects, at the expense of 1287dd7cddfSDavid du Colombier compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). 1297dd7cddfSDavid du Colombier 1307dd7cddfSDavid du Colombier The fields total_in and total_out can be used for statistics or 1317dd7cddfSDavid du Colombier progress reports. After compression, total_in holds the total size of 1327dd7cddfSDavid du Colombier the uncompressed data and may be saved for use in the decompressor 1337dd7cddfSDavid du Colombier (particularly if the decompressor wants to decompress everything in 1347dd7cddfSDavid du Colombier a single step). 1357dd7cddfSDavid du Colombier */ 1367dd7cddfSDavid du Colombier 1377dd7cddfSDavid du Colombier /* constants */ 1387dd7cddfSDavid du Colombier 1397dd7cddfSDavid du Colombier #define Z_NO_FLUSH 0 140*593dc095SDavid du Colombier #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ 1417dd7cddfSDavid du Colombier #define Z_SYNC_FLUSH 2 1427dd7cddfSDavid du Colombier #define Z_FULL_FLUSH 3 1437dd7cddfSDavid du Colombier #define Z_FINISH 4 144*593dc095SDavid du Colombier #define Z_BLOCK 5 145*593dc095SDavid du Colombier /* Allowed flush values; see deflate() and inflate() below for details */ 1467dd7cddfSDavid du Colombier 1477dd7cddfSDavid du Colombier #define Z_OK 0 1487dd7cddfSDavid du Colombier #define Z_STREAM_END 1 1497dd7cddfSDavid du Colombier #define Z_NEED_DICT 2 1507dd7cddfSDavid du Colombier #define Z_ERRNO (-1) 1517dd7cddfSDavid du Colombier #define Z_STREAM_ERROR (-2) 1527dd7cddfSDavid du Colombier #define Z_DATA_ERROR (-3) 1537dd7cddfSDavid du Colombier #define Z_MEM_ERROR (-4) 1547dd7cddfSDavid du Colombier #define Z_BUF_ERROR (-5) 1557dd7cddfSDavid du Colombier #define Z_VERSION_ERROR (-6) 1567dd7cddfSDavid du Colombier /* Return codes for the compression/decompression functions. Negative 1577dd7cddfSDavid du Colombier * values are errors, positive values are used for special but normal events. 1587dd7cddfSDavid du Colombier */ 1597dd7cddfSDavid du Colombier 1607dd7cddfSDavid du Colombier #define Z_NO_COMPRESSION 0 1617dd7cddfSDavid du Colombier #define Z_BEST_SPEED 1 1627dd7cddfSDavid du Colombier #define Z_BEST_COMPRESSION 9 1637dd7cddfSDavid du Colombier #define Z_DEFAULT_COMPRESSION (-1) 1647dd7cddfSDavid du Colombier /* compression levels */ 1657dd7cddfSDavid du Colombier 1667dd7cddfSDavid du Colombier #define Z_FILTERED 1 1677dd7cddfSDavid du Colombier #define Z_HUFFMAN_ONLY 2 168*593dc095SDavid du Colombier #define Z_RLE 3 1697dd7cddfSDavid du Colombier #define Z_DEFAULT_STRATEGY 0 1707dd7cddfSDavid du Colombier /* compression strategy; see deflateInit2() below for details */ 1717dd7cddfSDavid du Colombier 1727dd7cddfSDavid du Colombier #define Z_BINARY 0 1737dd7cddfSDavid du Colombier #define Z_ASCII 1 1747dd7cddfSDavid du Colombier #define Z_UNKNOWN 2 175*593dc095SDavid du Colombier /* Possible values of the data_type field (though see inflate()) */ 1767dd7cddfSDavid du Colombier 1777dd7cddfSDavid du Colombier #define Z_DEFLATED 8 1787dd7cddfSDavid du Colombier /* The deflate compression method (the only one supported in this version) */ 1797dd7cddfSDavid du Colombier 1807dd7cddfSDavid du Colombier #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 1817dd7cddfSDavid du Colombier 1827dd7cddfSDavid du Colombier #define zlib_version zlibVersion() 1837dd7cddfSDavid du Colombier /* for compatibility with versions < 1.0.2 */ 1847dd7cddfSDavid du Colombier 1857dd7cddfSDavid du Colombier /* basic functions */ 1867dd7cddfSDavid du Colombier 187*593dc095SDavid du Colombier ZEXTERN const char * ZEXPORT zlibVersion OF((void)); 1887dd7cddfSDavid du Colombier /* The application can compare zlibVersion and ZLIB_VERSION for consistency. 1897dd7cddfSDavid du Colombier If the first character differs, the library code actually used is 1907dd7cddfSDavid du Colombier not compatible with the zlib.h header file used by the application. 1917dd7cddfSDavid du Colombier This check is automatically made by deflateInit and inflateInit. 1927dd7cddfSDavid du Colombier */ 1937dd7cddfSDavid du Colombier 1947dd7cddfSDavid du Colombier /* 195*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); 1967dd7cddfSDavid du Colombier 1977dd7cddfSDavid du Colombier Initializes the internal stream state for compression. The fields 1987dd7cddfSDavid du Colombier zalloc, zfree and opaque must be initialized before by the caller. 1997dd7cddfSDavid du Colombier If zalloc and zfree are set to Z_NULL, deflateInit updates them to 2007dd7cddfSDavid du Colombier use default allocation functions. 2017dd7cddfSDavid du Colombier 2027dd7cddfSDavid du Colombier The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 2037dd7cddfSDavid du Colombier 1 gives best speed, 9 gives best compression, 0 gives no compression at 2047dd7cddfSDavid du Colombier all (the input data is simply copied a block at a time). 2057dd7cddfSDavid du Colombier Z_DEFAULT_COMPRESSION requests a default compromise between speed and 2067dd7cddfSDavid du Colombier compression (currently equivalent to level 6). 2077dd7cddfSDavid du Colombier 2087dd7cddfSDavid du Colombier deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 2097dd7cddfSDavid du Colombier enough memory, Z_STREAM_ERROR if level is not a valid compression level, 2107dd7cddfSDavid du Colombier Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible 2117dd7cddfSDavid du Colombier with the version assumed by the caller (ZLIB_VERSION). 2127dd7cddfSDavid du Colombier msg is set to null if there is no error message. deflateInit does not 2137dd7cddfSDavid du Colombier perform any compression: this will be done by deflate(). 2147dd7cddfSDavid du Colombier */ 2157dd7cddfSDavid du Colombier 2167dd7cddfSDavid du Colombier 217*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); 2187dd7cddfSDavid du Colombier /* 219*593dc095SDavid du Colombier deflate compresses as much data as possible, and stops when the input 220*593dc095SDavid du Colombier buffer becomes empty or the output buffer becomes full. It may introduce some 221*593dc095SDavid du Colombier output latency (reading input without producing any output) except when 222*593dc095SDavid du Colombier forced to flush. 223*593dc095SDavid du Colombier 224*593dc095SDavid du Colombier The detailed semantics are as follows. deflate performs one or both of the 225*593dc095SDavid du Colombier following actions: 2267dd7cddfSDavid du Colombier 2277dd7cddfSDavid du Colombier - Compress more input starting at next_in and update next_in and avail_in 2287dd7cddfSDavid du Colombier accordingly. If not all input can be processed (because there is not 2297dd7cddfSDavid du Colombier enough room in the output buffer), next_in and avail_in are updated and 2307dd7cddfSDavid du Colombier processing will resume at this point for the next call of deflate(). 2317dd7cddfSDavid du Colombier 2327dd7cddfSDavid du Colombier - Provide more output starting at next_out and update next_out and avail_out 2337dd7cddfSDavid du Colombier accordingly. This action is forced if the parameter flush is non zero. 2347dd7cddfSDavid du Colombier Forcing flush frequently degrades the compression ratio, so this parameter 2357dd7cddfSDavid du Colombier should be set only when necessary (in interactive applications). 2367dd7cddfSDavid du Colombier Some output may be provided even if flush is not set. 2377dd7cddfSDavid du Colombier 2387dd7cddfSDavid du Colombier Before the call of deflate(), the application should ensure that at least 2397dd7cddfSDavid du Colombier one of the actions is possible, by providing more input and/or consuming 2407dd7cddfSDavid du Colombier more output, and updating avail_in or avail_out accordingly; avail_out 2417dd7cddfSDavid du Colombier should never be zero before the call. The application can consume the 2427dd7cddfSDavid du Colombier compressed output when it wants, for example when the output buffer is full 2437dd7cddfSDavid du Colombier (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK 2447dd7cddfSDavid du Colombier and with zero avail_out, it must be called again after making room in the 2457dd7cddfSDavid du Colombier output buffer because there might be more output pending. 2467dd7cddfSDavid du Colombier 247*593dc095SDavid du Colombier If the parameter flush is set to Z_SYNC_FLUSH, all pending output is 248*593dc095SDavid du Colombier flushed to the output buffer and the output is aligned on a byte boundary, so 249*593dc095SDavid du Colombier that the decompressor can get all input data available so far. (In particular 250*593dc095SDavid du Colombier avail_in is zero after the call if enough output space has been provided 251*593dc095SDavid du Colombier before the call.) Flushing may degrade compression for some compression 252*593dc095SDavid du Colombier algorithms and so it should be used only when necessary. 253*593dc095SDavid du Colombier 254*593dc095SDavid du Colombier If flush is set to Z_FULL_FLUSH, all output is flushed as with 255*593dc095SDavid du Colombier Z_SYNC_FLUSH, and the compression state is reset so that decompression can 256*593dc095SDavid du Colombier restart from this point if previous compressed data has been damaged or if 257*593dc095SDavid du Colombier random access is desired. Using Z_FULL_FLUSH too often can seriously degrade 258*593dc095SDavid du Colombier the compression. 259*593dc095SDavid du Colombier 260*593dc095SDavid du Colombier If deflate returns with avail_out == 0, this function must be called again 261*593dc095SDavid du Colombier with the same value of the flush parameter and more output space (updated 262*593dc095SDavid du Colombier avail_out), until the flush is complete (deflate returns with non-zero 263*593dc095SDavid du Colombier avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that 264*593dc095SDavid du Colombier avail_out is greater than six to avoid repeated flush markers due to 265*593dc095SDavid du Colombier avail_out == 0 on return. 2667dd7cddfSDavid du Colombier 2677dd7cddfSDavid du Colombier If the parameter flush is set to Z_FINISH, pending input is processed, 2687dd7cddfSDavid du Colombier pending output is flushed and deflate returns with Z_STREAM_END if there 2697dd7cddfSDavid du Colombier was enough output space; if deflate returns with Z_OK, this function must be 2707dd7cddfSDavid du Colombier called again with Z_FINISH and more output space (updated avail_out) but no 2717dd7cddfSDavid du Colombier more input data, until it returns with Z_STREAM_END or an error. After 2727dd7cddfSDavid du Colombier deflate has returned Z_STREAM_END, the only possible operations on the 2737dd7cddfSDavid du Colombier stream are deflateReset or deflateEnd. 2747dd7cddfSDavid du Colombier 2757dd7cddfSDavid du Colombier Z_FINISH can be used immediately after deflateInit if all the compression 2767dd7cddfSDavid du Colombier is to be done in a single step. In this case, avail_out must be at least 277*593dc095SDavid du Colombier the value returned by deflateBound (see below). If deflate does not return 2787dd7cddfSDavid du Colombier Z_STREAM_END, then it must be called again as described above. 2797dd7cddfSDavid du Colombier 280*593dc095SDavid du Colombier deflate() sets strm->adler to the adler32 checksum of all input read 281*593dc095SDavid du Colombier so far (that is, total_in bytes). 282*593dc095SDavid du Colombier 2837dd7cddfSDavid du Colombier deflate() may update data_type if it can make a good guess about 2847dd7cddfSDavid du Colombier the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered 2857dd7cddfSDavid du Colombier binary. This field is only for information purposes and does not affect 2867dd7cddfSDavid du Colombier the compression algorithm in any manner. 2877dd7cddfSDavid du Colombier 2887dd7cddfSDavid du Colombier deflate() returns Z_OK if some progress has been made (more input 2897dd7cddfSDavid du Colombier processed or more output produced), Z_STREAM_END if all input has been 2907dd7cddfSDavid du Colombier consumed and all output has been produced (only when flush is set to 2917dd7cddfSDavid du Colombier Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 292*593dc095SDavid du Colombier if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible 293*593dc095SDavid du Colombier (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not 294*593dc095SDavid du Colombier fatal, and deflate() can be called again with more input and more output 295*593dc095SDavid du Colombier space to continue compressing. 2967dd7cddfSDavid du Colombier */ 2977dd7cddfSDavid du Colombier 2987dd7cddfSDavid du Colombier 299*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); 3007dd7cddfSDavid du Colombier /* 3017dd7cddfSDavid du Colombier All dynamically allocated data structures for this stream are freed. 3027dd7cddfSDavid du Colombier This function discards any unprocessed input and does not flush any 3037dd7cddfSDavid du Colombier pending output. 3047dd7cddfSDavid du Colombier 3057dd7cddfSDavid du Colombier deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 3067dd7cddfSDavid du Colombier stream state was inconsistent, Z_DATA_ERROR if the stream was freed 3077dd7cddfSDavid du Colombier prematurely (some input or output was discarded). In the error case, 3087dd7cddfSDavid du Colombier msg may be set but then points to a static string (which must not be 3097dd7cddfSDavid du Colombier deallocated). 3107dd7cddfSDavid du Colombier */ 3117dd7cddfSDavid du Colombier 3127dd7cddfSDavid du Colombier 3137dd7cddfSDavid du Colombier /* 314*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); 3157dd7cddfSDavid du Colombier 3167dd7cddfSDavid du Colombier Initializes the internal stream state for decompression. The fields 317*593dc095SDavid du Colombier next_in, avail_in, zalloc, zfree and opaque must be initialized before by 318*593dc095SDavid du Colombier the caller. If next_in is not Z_NULL and avail_in is large enough (the exact 319*593dc095SDavid du Colombier value depends on the compression method), inflateInit determines the 320*593dc095SDavid du Colombier compression method from the zlib header and allocates all data structures 321*593dc095SDavid du Colombier accordingly; otherwise the allocation will be deferred to the first call of 322*593dc095SDavid du Colombier inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to 323*593dc095SDavid du Colombier use default allocation functions. 3247dd7cddfSDavid du Colombier 325*593dc095SDavid du Colombier inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 326*593dc095SDavid du Colombier memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 327*593dc095SDavid du Colombier version assumed by the caller. msg is set to null if there is no error 328*593dc095SDavid du Colombier message. inflateInit does not perform any decompression apart from reading 329*593dc095SDavid du Colombier the zlib header if present: this will be done by inflate(). (So next_in and 330*593dc095SDavid du Colombier avail_in may be modified, but next_out and avail_out are unchanged.) 3317dd7cddfSDavid du Colombier */ 3327dd7cddfSDavid du Colombier 3337dd7cddfSDavid du Colombier 334*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); 3357dd7cddfSDavid du Colombier /* 336*593dc095SDavid du Colombier inflate decompresses as much data as possible, and stops when the input 337*593dc095SDavid du Colombier buffer becomes empty or the output buffer becomes full. It may introduce 338*593dc095SDavid du Colombier some output latency (reading input without producing any output) except when 339*593dc095SDavid du Colombier forced to flush. 340*593dc095SDavid du Colombier 341*593dc095SDavid du Colombier The detailed semantics are as follows. inflate performs one or both of the 342*593dc095SDavid du Colombier following actions: 3437dd7cddfSDavid du Colombier 3447dd7cddfSDavid du Colombier - Decompress more input starting at next_in and update next_in and avail_in 3457dd7cddfSDavid du Colombier accordingly. If not all input can be processed (because there is not 3467dd7cddfSDavid du Colombier enough room in the output buffer), next_in is updated and processing 3477dd7cddfSDavid du Colombier will resume at this point for the next call of inflate(). 3487dd7cddfSDavid du Colombier 3497dd7cddfSDavid du Colombier - Provide more output starting at next_out and update next_out and avail_out 3507dd7cddfSDavid du Colombier accordingly. inflate() provides as much output as possible, until there 3517dd7cddfSDavid du Colombier is no more input data or no more space in the output buffer (see below 3527dd7cddfSDavid du Colombier about the flush parameter). 3537dd7cddfSDavid du Colombier 3547dd7cddfSDavid du Colombier Before the call of inflate(), the application should ensure that at least 3557dd7cddfSDavid du Colombier one of the actions is possible, by providing more input and/or consuming 3567dd7cddfSDavid du Colombier more output, and updating the next_* and avail_* values accordingly. 3577dd7cddfSDavid du Colombier The application can consume the uncompressed output when it wants, for 3587dd7cddfSDavid du Colombier example when the output buffer is full (avail_out == 0), or after each 3597dd7cddfSDavid du Colombier call of inflate(). If inflate returns Z_OK and with zero avail_out, it 3607dd7cddfSDavid du Colombier must be called again after making room in the output buffer because there 3617dd7cddfSDavid du Colombier might be more output pending. 3627dd7cddfSDavid du Colombier 363*593dc095SDavid du Colombier The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, 364*593dc095SDavid du Colombier Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much 365*593dc095SDavid du Colombier output as possible to the output buffer. Z_BLOCK requests that inflate() stop 366*593dc095SDavid du Colombier if and when it get to the next deflate block boundary. When decoding the zlib 367*593dc095SDavid du Colombier or gzip format, this will cause inflate() to return immediately after the 368*593dc095SDavid du Colombier header and before the first block. When doing a raw inflate, inflate() will 369*593dc095SDavid du Colombier go ahead and process the first block, and will return when it gets to the end 370*593dc095SDavid du Colombier of that block, or when it runs out of data. 371*593dc095SDavid du Colombier 372*593dc095SDavid du Colombier The Z_BLOCK option assists in appending to or combining deflate streams. 373*593dc095SDavid du Colombier Also to assist in this, on return inflate() will set strm->data_type to the 374*593dc095SDavid du Colombier number of unused bits in the last byte taken from strm->next_in, plus 64 375*593dc095SDavid du Colombier if inflate() is currently decoding the last block in the deflate stream, 376*593dc095SDavid du Colombier plus 128 if inflate() returned immediately after decoding an end-of-block 377*593dc095SDavid du Colombier code or decoding the complete header up to just before the first byte of the 378*593dc095SDavid du Colombier deflate stream. The end-of-block will not be indicated until all of the 379*593dc095SDavid du Colombier uncompressed data from that block has been written to strm->next_out. The 380*593dc095SDavid du Colombier number of unused bits may in general be greater than seven, except when 381*593dc095SDavid du Colombier bit 7 of data_type is set, in which case the number of unused bits will be 382*593dc095SDavid du Colombier less than eight. 3837dd7cddfSDavid du Colombier 3847dd7cddfSDavid du Colombier inflate() should normally be called until it returns Z_STREAM_END or an 3857dd7cddfSDavid du Colombier error. However if all decompression is to be performed in a single step 3867dd7cddfSDavid du Colombier (a single call of inflate), the parameter flush should be set to 3877dd7cddfSDavid du Colombier Z_FINISH. In this case all pending input is processed and all pending 3887dd7cddfSDavid du Colombier output is flushed; avail_out must be large enough to hold all the 3897dd7cddfSDavid du Colombier uncompressed data. (The size of the uncompressed data may have been saved 3907dd7cddfSDavid du Colombier by the compressor for this purpose.) The next operation on this stream must 3917dd7cddfSDavid du Colombier be inflateEnd to deallocate the decompression state. The use of Z_FINISH 392*593dc095SDavid du Colombier is never required, but can be used to inform inflate that a faster approach 3937dd7cddfSDavid du Colombier may be used for the single inflate() call. 3947dd7cddfSDavid du Colombier 395*593dc095SDavid du Colombier In this implementation, inflate() always flushes as much output as 396*593dc095SDavid du Colombier possible to the output buffer, and always uses the faster approach on the 397*593dc095SDavid du Colombier first call. So the only effect of the flush parameter in this implementation 398*593dc095SDavid du Colombier is on the return value of inflate(), as noted below, or when it returns early 399*593dc095SDavid du Colombier because Z_BLOCK is used. 400*593dc095SDavid du Colombier 401*593dc095SDavid du Colombier If a preset dictionary is needed after this call (see inflateSetDictionary 402*593dc095SDavid du Colombier below), inflate sets strm->adler to the adler32 checksum of the dictionary 403*593dc095SDavid du Colombier chosen by the compressor and returns Z_NEED_DICT; otherwise it sets 404*593dc095SDavid du Colombier strm->adler to the adler32 checksum of all output produced so far (that is, 405*593dc095SDavid du Colombier total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described 406*593dc095SDavid du Colombier below. At the end of the stream, inflate() checks that its computed adler32 407*593dc095SDavid du Colombier checksum is equal to that saved by the compressor and returns Z_STREAM_END 408*593dc095SDavid du Colombier only if the checksum is correct. 409*593dc095SDavid du Colombier 410*593dc095SDavid du Colombier inflate() will decompress and check either zlib-wrapped or gzip-wrapped 411*593dc095SDavid du Colombier deflate data. The header type is detected automatically. Any information 412*593dc095SDavid du Colombier contained in the gzip header is not retained, so applications that need that 413*593dc095SDavid du Colombier information should instead use raw inflate, see inflateInit2() below, or 414*593dc095SDavid du Colombier inflateBack() and perform their own processing of the gzip header and 415*593dc095SDavid du Colombier trailer. 416*593dc095SDavid du Colombier 417*593dc095SDavid du Colombier inflate() returns Z_OK if some progress has been made (more input processed 418*593dc095SDavid du Colombier or more output produced), Z_STREAM_END if the end of the compressed data has 419*593dc095SDavid du Colombier been reached and all uncompressed output has been produced, Z_NEED_DICT if a 420*593dc095SDavid du Colombier preset dictionary is needed at this point, Z_DATA_ERROR if the input data was 421*593dc095SDavid du Colombier corrupted (input stream not conforming to the zlib format or incorrect check 422*593dc095SDavid du Colombier value), Z_STREAM_ERROR if the stream structure was inconsistent (for example 423*593dc095SDavid du Colombier if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, 424*593dc095SDavid du Colombier Z_BUF_ERROR if no progress is possible or if there was not enough room in the 425*593dc095SDavid du Colombier output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and 426*593dc095SDavid du Colombier inflate() can be called again with more input and more output space to 427*593dc095SDavid du Colombier continue decompressing. If Z_DATA_ERROR is returned, the application may then 428*593dc095SDavid du Colombier call inflateSync() to look for a good compression block if a partial recovery 429*593dc095SDavid du Colombier of the data is desired. 4307dd7cddfSDavid du Colombier */ 4317dd7cddfSDavid du Colombier 4327dd7cddfSDavid du Colombier 433*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); 4347dd7cddfSDavid du Colombier /* 4357dd7cddfSDavid du Colombier All dynamically allocated data structures for this stream are freed. 4367dd7cddfSDavid du Colombier This function discards any unprocessed input and does not flush any 4377dd7cddfSDavid du Colombier pending output. 4387dd7cddfSDavid du Colombier 4397dd7cddfSDavid du Colombier inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state 4407dd7cddfSDavid du Colombier was inconsistent. In the error case, msg may be set but then points to a 4417dd7cddfSDavid du Colombier static string (which must not be deallocated). 4427dd7cddfSDavid du Colombier */ 4437dd7cddfSDavid du Colombier 4447dd7cddfSDavid du Colombier /* Advanced functions */ 4457dd7cddfSDavid du Colombier 4467dd7cddfSDavid du Colombier /* 4477dd7cddfSDavid du Colombier The following functions are needed only in some special applications. 4487dd7cddfSDavid du Colombier */ 4497dd7cddfSDavid du Colombier 4507dd7cddfSDavid du Colombier /* 451*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, 4527dd7cddfSDavid du Colombier int level, 4537dd7cddfSDavid du Colombier int method, 4547dd7cddfSDavid du Colombier int windowBits, 4557dd7cddfSDavid du Colombier int memLevel, 4567dd7cddfSDavid du Colombier int strategy)); 4577dd7cddfSDavid du Colombier 4587dd7cddfSDavid du Colombier This is another version of deflateInit with more compression options. The 4597dd7cddfSDavid du Colombier fields next_in, zalloc, zfree and opaque must be initialized before by 4607dd7cddfSDavid du Colombier the caller. 4617dd7cddfSDavid du Colombier 4627dd7cddfSDavid du Colombier The method parameter is the compression method. It must be Z_DEFLATED in 463*593dc095SDavid du Colombier this version of the library. 4647dd7cddfSDavid du Colombier 4657dd7cddfSDavid du Colombier The windowBits parameter is the base two logarithm of the window size 4667dd7cddfSDavid du Colombier (the size of the history buffer). It should be in the range 8..15 for this 467*593dc095SDavid du Colombier version of the library. Larger values of this parameter result in better 468*593dc095SDavid du Colombier compression at the expense of memory usage. The default value is 15 if 469*593dc095SDavid du Colombier deflateInit is used instead. 470*593dc095SDavid du Colombier 471*593dc095SDavid du Colombier windowBits can also be -8..-15 for raw deflate. In this case, -windowBits 472*593dc095SDavid du Colombier determines the window size. deflate() will then generate raw deflate data 473*593dc095SDavid du Colombier with no zlib header or trailer, and will not compute an adler32 check value. 474*593dc095SDavid du Colombier 475*593dc095SDavid du Colombier windowBits can also be greater than 15 for optional gzip encoding. Add 476*593dc095SDavid du Colombier 16 to windowBits to write a simple gzip header and trailer around the 477*593dc095SDavid du Colombier compressed data instead of a zlib wrapper. The gzip header will have no 478*593dc095SDavid du Colombier file name, no extra data, no comment, no modification time (set to zero), 479*593dc095SDavid du Colombier no header crc, and the operating system will be set to 255 (unknown). If a 480*593dc095SDavid du Colombier gzip stream is being written, strm->adler is a crc32 instead of an adler32. 4817dd7cddfSDavid du Colombier 4827dd7cddfSDavid du Colombier The memLevel parameter specifies how much memory should be allocated 4837dd7cddfSDavid du Colombier for the internal compression state. memLevel=1 uses minimum memory but 4847dd7cddfSDavid du Colombier is slow and reduces compression ratio; memLevel=9 uses maximum memory 4857dd7cddfSDavid du Colombier for optimal speed. The default value is 8. See zconf.h for total memory 4867dd7cddfSDavid du Colombier usage as a function of windowBits and memLevel. 4877dd7cddfSDavid du Colombier 4887dd7cddfSDavid du Colombier The strategy parameter is used to tune the compression algorithm. Use the 4897dd7cddfSDavid du Colombier value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a 490*593dc095SDavid du Colombier filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no 491*593dc095SDavid du Colombier string match), or Z_RLE to limit match distances to one (run-length 492*593dc095SDavid du Colombier encoding). Filtered data consists mostly of small values with a somewhat 493*593dc095SDavid du Colombier random distribution. In this case, the compression algorithm is tuned to 494*593dc095SDavid du Colombier compress them better. The effect of Z_FILTERED is to force more Huffman 495*593dc095SDavid du Colombier coding and less string matching; it is somewhat intermediate between 496*593dc095SDavid du Colombier Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as 497*593dc095SDavid du Colombier Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy 498*593dc095SDavid du Colombier parameter only affects the compression ratio but not the correctness of the 499*593dc095SDavid du Colombier compressed output even if it is not set appropriately. 5007dd7cddfSDavid du Colombier 501*593dc095SDavid du Colombier deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 502*593dc095SDavid du Colombier memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid 503*593dc095SDavid du Colombier method). msg is set to null if there is no error message. deflateInit2 does 504*593dc095SDavid du Colombier not perform any compression: this will be done by deflate(). 5057dd7cddfSDavid du Colombier */ 5067dd7cddfSDavid du Colombier 507*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, 5087dd7cddfSDavid du Colombier const Bytef *dictionary, 5097dd7cddfSDavid du Colombier uInt dictLength)); 5107dd7cddfSDavid du Colombier /* 511*593dc095SDavid du Colombier Initializes the compression dictionary from the given byte sequence 512*593dc095SDavid du Colombier without producing any compressed output. This function must be called 513*593dc095SDavid du Colombier immediately after deflateInit, deflateInit2 or deflateReset, before any 514*593dc095SDavid du Colombier call of deflate. The compressor and decompressor must use exactly the same 5157dd7cddfSDavid du Colombier dictionary (see inflateSetDictionary). 516*593dc095SDavid du Colombier 5177dd7cddfSDavid du Colombier The dictionary should consist of strings (byte sequences) that are likely 5187dd7cddfSDavid du Colombier to be encountered later in the data to be compressed, with the most commonly 5197dd7cddfSDavid du Colombier used strings preferably put towards the end of the dictionary. Using a 520*593dc095SDavid du Colombier dictionary is most useful when the data to be compressed is short and can be 521*593dc095SDavid du Colombier predicted with good accuracy; the data can then be compressed better than 522*593dc095SDavid du Colombier with the default empty dictionary. 523*593dc095SDavid du Colombier 524*593dc095SDavid du Colombier Depending on the size of the compression data structures selected by 525*593dc095SDavid du Colombier deflateInit or deflateInit2, a part of the dictionary may in effect be 526*593dc095SDavid du Colombier discarded, for example if the dictionary is larger than the window size in 527*593dc095SDavid du Colombier deflate or deflate2. Thus the strings most likely to be useful should be 528*593dc095SDavid du Colombier put at the end of the dictionary, not at the front. 529*593dc095SDavid du Colombier 530*593dc095SDavid du Colombier Upon return of this function, strm->adler is set to the adler32 value 5317dd7cddfSDavid du Colombier of the dictionary; the decompressor may later use this value to determine 532*593dc095SDavid du Colombier which dictionary has been used by the compressor. (The adler32 value 5337dd7cddfSDavid du Colombier applies to the whole dictionary even if only a subset of the dictionary is 534*593dc095SDavid du Colombier actually used by the compressor.) If a raw deflate was requested, then the 535*593dc095SDavid du Colombier adler32 value is not computed and strm->adler is not set. 5367dd7cddfSDavid du Colombier 5377dd7cddfSDavid du Colombier deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a 538*593dc095SDavid du Colombier parameter is invalid (such as NULL dictionary) or the stream state is 539*593dc095SDavid du Colombier inconsistent (for example if deflate has already been called for this stream 540*593dc095SDavid du Colombier or if the compression method is bsort). deflateSetDictionary does not 541*593dc095SDavid du Colombier perform any compression: this will be done by deflate(). 5427dd7cddfSDavid du Colombier */ 5437dd7cddfSDavid du Colombier 544*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, 5457dd7cddfSDavid du Colombier z_streamp source)); 5467dd7cddfSDavid du Colombier /* 547*593dc095SDavid du Colombier Sets the destination stream as a complete copy of the source stream. 5487dd7cddfSDavid du Colombier 5497dd7cddfSDavid du Colombier This function can be useful when several compression strategies will be 5507dd7cddfSDavid du Colombier tried, for example when there are several ways of pre-processing the input 5517dd7cddfSDavid du Colombier data with a filter. The streams that will be discarded should then be freed 5527dd7cddfSDavid du Colombier by calling deflateEnd. Note that deflateCopy duplicates the internal 5537dd7cddfSDavid du Colombier compression state which can be quite large, so this strategy is slow and 5547dd7cddfSDavid du Colombier can consume lots of memory. 5557dd7cddfSDavid du Colombier 5567dd7cddfSDavid du Colombier deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 5577dd7cddfSDavid du Colombier enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 5587dd7cddfSDavid du Colombier (such as zalloc being NULL). msg is left unchanged in both source and 5597dd7cddfSDavid du Colombier destination. 5607dd7cddfSDavid du Colombier */ 5617dd7cddfSDavid du Colombier 562*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); 5637dd7cddfSDavid du Colombier /* 5647dd7cddfSDavid du Colombier This function is equivalent to deflateEnd followed by deflateInit, 5657dd7cddfSDavid du Colombier but does not free and reallocate all the internal compression state. 5667dd7cddfSDavid du Colombier The stream will keep the same compression level and any other attributes 5677dd7cddfSDavid du Colombier that may have been set by deflateInit2. 5687dd7cddfSDavid du Colombier 5697dd7cddfSDavid du Colombier deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 5707dd7cddfSDavid du Colombier stream state was inconsistent (such as zalloc or state being NULL). 5717dd7cddfSDavid du Colombier */ 5727dd7cddfSDavid du Colombier 573*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, 574*593dc095SDavid du Colombier int level, 575*593dc095SDavid du Colombier int strategy)); 5767dd7cddfSDavid du Colombier /* 577*593dc095SDavid du Colombier Dynamically update the compression level and compression strategy. The 578*593dc095SDavid du Colombier interpretation of level and strategy is as in deflateInit2. This can be 579*593dc095SDavid du Colombier used to switch between compression and straight copy of the input data, or 580*593dc095SDavid du Colombier to switch to a different kind of input data requiring a different 581*593dc095SDavid du Colombier strategy. If the compression level is changed, the input available so far 582*593dc095SDavid du Colombier is compressed with the old level (and may be flushed); the new level will 583*593dc095SDavid du Colombier take effect only at the next call of deflate(). 5847dd7cddfSDavid du Colombier 5857dd7cddfSDavid du Colombier Before the call of deflateParams, the stream state must be set as for 5867dd7cddfSDavid du Colombier a call of deflate(), since the currently available input may have to 5877dd7cddfSDavid du Colombier be compressed and flushed. In particular, strm->avail_out must be non-zero. 5887dd7cddfSDavid du Colombier 5897dd7cddfSDavid du Colombier deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source 5907dd7cddfSDavid du Colombier stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR 5917dd7cddfSDavid du Colombier if strm->avail_out was zero. 5927dd7cddfSDavid du Colombier */ 5937dd7cddfSDavid du Colombier 594*593dc095SDavid du Colombier ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, 595*593dc095SDavid du Colombier uLong sourceLen)); 5967dd7cddfSDavid du Colombier /* 597*593dc095SDavid du Colombier deflateBound() returns an upper bound on the compressed size after 598*593dc095SDavid du Colombier deflation of sourceLen bytes. It must be called after deflateInit() 599*593dc095SDavid du Colombier or deflateInit2(). This would be used to allocate an output buffer 600*593dc095SDavid du Colombier for deflation in a single pass, and so would be called before deflate(). 601*593dc095SDavid du Colombier */ 602*593dc095SDavid du Colombier 603*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, 604*593dc095SDavid du Colombier int bits, 605*593dc095SDavid du Colombier int value)); 606*593dc095SDavid du Colombier /* 607*593dc095SDavid du Colombier deflatePrime() inserts bits in the deflate output stream. The intent 608*593dc095SDavid du Colombier is that this function is used to start off the deflate output with the 609*593dc095SDavid du Colombier bits leftover from a previous deflate stream when appending to it. As such, 610*593dc095SDavid du Colombier this function can only be used for raw deflate, and must be used before the 611*593dc095SDavid du Colombier first deflate() call after a deflateInit2() or deflateReset(). bits must be 612*593dc095SDavid du Colombier less than or equal to 16, and that many of the least significant bits of 613*593dc095SDavid du Colombier value will be inserted in the output. 614*593dc095SDavid du Colombier 615*593dc095SDavid du Colombier deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source 616*593dc095SDavid du Colombier stream state was inconsistent. 617*593dc095SDavid du Colombier */ 618*593dc095SDavid du Colombier 619*593dc095SDavid du Colombier /* 620*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, 6217dd7cddfSDavid du Colombier int windowBits)); 6227dd7cddfSDavid du Colombier 623*593dc095SDavid du Colombier This is another version of inflateInit with an extra parameter. The 624*593dc095SDavid du Colombier fields next_in, avail_in, zalloc, zfree and opaque must be initialized 625*593dc095SDavid du Colombier before by the caller. 6267dd7cddfSDavid du Colombier 6277dd7cddfSDavid du Colombier The windowBits parameter is the base two logarithm of the maximum window 6287dd7cddfSDavid du Colombier size (the size of the history buffer). It should be in the range 8..15 for 629*593dc095SDavid du Colombier this version of the library. The default value is 15 if inflateInit is used 630*593dc095SDavid du Colombier instead. windowBits must be greater than or equal to the windowBits value 631*593dc095SDavid du Colombier provided to deflateInit2() while compressing, or it must be equal to 15 if 632*593dc095SDavid du Colombier deflateInit2() was not used. If a compressed stream with a larger window 633*593dc095SDavid du Colombier size is given as input, inflate() will return with the error code 634*593dc095SDavid du Colombier Z_DATA_ERROR instead of trying to allocate a larger window. 6357dd7cddfSDavid du Colombier 636*593dc095SDavid du Colombier windowBits can also be -8..-15 for raw inflate. In this case, -windowBits 637*593dc095SDavid du Colombier determines the window size. inflate() will then process raw deflate data, 638*593dc095SDavid du Colombier not looking for a zlib or gzip header, not generating a check value, and not 639*593dc095SDavid du Colombier looking for any check values for comparison at the end of the stream. This 640*593dc095SDavid du Colombier is for use with other formats that use the deflate compressed data format 641*593dc095SDavid du Colombier such as zip. Those formats provide their own check values. If a custom 642*593dc095SDavid du Colombier format is developed using the raw deflate format for compressed data, it is 643*593dc095SDavid du Colombier recommended that a check value such as an adler32 or a crc32 be applied to 644*593dc095SDavid du Colombier the uncompressed data as is done in the zlib, gzip, and zip formats. For 645*593dc095SDavid du Colombier most applications, the zlib format should be used as is. Note that comments 646*593dc095SDavid du Colombier above on the use in deflateInit2() applies to the magnitude of windowBits. 6477dd7cddfSDavid du Colombier 648*593dc095SDavid du Colombier windowBits can also be greater than 15 for optional gzip decoding. Add 649*593dc095SDavid du Colombier 32 to windowBits to enable zlib and gzip decoding with automatic header 650*593dc095SDavid du Colombier detection, or add 16 to decode only the gzip format (the zlib format will 651*593dc095SDavid du Colombier return a Z_DATA_ERROR. If a gzip stream is being decoded, strm->adler is 652*593dc095SDavid du Colombier a crc32 instead of an adler32. 6537dd7cddfSDavid du Colombier 654*593dc095SDavid du Colombier inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 655*593dc095SDavid du Colombier memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative 656*593dc095SDavid du Colombier memLevel). msg is set to null if there is no error message. inflateInit2 657*593dc095SDavid du Colombier does not perform any decompression apart from reading the zlib header if 658*593dc095SDavid du Colombier present: this will be done by inflate(). (So next_in and avail_in may be 659*593dc095SDavid du Colombier modified, but next_out and avail_out are unchanged.) 6607dd7cddfSDavid du Colombier */ 6617dd7cddfSDavid du Colombier 662*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, 6637dd7cddfSDavid du Colombier const Bytef *dictionary, 6647dd7cddfSDavid du Colombier uInt dictLength)); 6657dd7cddfSDavid du Colombier /* 666*593dc095SDavid du Colombier Initializes the decompression dictionary from the given uncompressed byte 667*593dc095SDavid du Colombier sequence. This function must be called immediately after a call of inflate 668*593dc095SDavid du Colombier if this call returned Z_NEED_DICT. The dictionary chosen by the compressor 669*593dc095SDavid du Colombier can be determined from the adler32 value returned by this call of 670*593dc095SDavid du Colombier inflate. The compressor and decompressor must use exactly the same 6717dd7cddfSDavid du Colombier dictionary (see deflateSetDictionary). 6727dd7cddfSDavid du Colombier 6737dd7cddfSDavid du Colombier inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a 6747dd7cddfSDavid du Colombier parameter is invalid (such as NULL dictionary) or the stream state is 6757dd7cddfSDavid du Colombier inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the 676*593dc095SDavid du Colombier expected one (incorrect adler32 value). inflateSetDictionary does not 6777dd7cddfSDavid du Colombier perform any decompression: this will be done by subsequent calls of 6787dd7cddfSDavid du Colombier inflate(). 6797dd7cddfSDavid du Colombier */ 6807dd7cddfSDavid du Colombier 681*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); 6827dd7cddfSDavid du Colombier /* 683*593dc095SDavid du Colombier Skips invalid compressed data until a full flush point (see above the 684*593dc095SDavid du Colombier description of deflate with Z_FULL_FLUSH) can be found, or until all 685*593dc095SDavid du Colombier available input is skipped. No output is provided. 6867dd7cddfSDavid du Colombier 687*593dc095SDavid du Colombier inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR 688*593dc095SDavid du Colombier if no more input was provided, Z_DATA_ERROR if no flush point has been found, 6897dd7cddfSDavid du Colombier or Z_STREAM_ERROR if the stream structure was inconsistent. In the success 6907dd7cddfSDavid du Colombier case, the application may save the current current value of total_in which 6917dd7cddfSDavid du Colombier indicates where valid compressed data was found. In the error case, the 6927dd7cddfSDavid du Colombier application may repeatedly call inflateSync, providing more input each time, 6937dd7cddfSDavid du Colombier until success or end of the input data. 6947dd7cddfSDavid du Colombier */ 6957dd7cddfSDavid du Colombier 696*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, 697*593dc095SDavid du Colombier z_streamp source)); 698*593dc095SDavid du Colombier /* 699*593dc095SDavid du Colombier Sets the destination stream as a complete copy of the source stream. 700*593dc095SDavid du Colombier 701*593dc095SDavid du Colombier This function can be useful when randomly accessing a large stream. The 702*593dc095SDavid du Colombier first pass through the stream can periodically record the inflate state, 703*593dc095SDavid du Colombier allowing restarting inflate at those points when randomly accessing the 704*593dc095SDavid du Colombier stream. 705*593dc095SDavid du Colombier 706*593dc095SDavid du Colombier inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 707*593dc095SDavid du Colombier enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 708*593dc095SDavid du Colombier (such as zalloc being NULL). msg is left unchanged in both source and 709*593dc095SDavid du Colombier destination. 710*593dc095SDavid du Colombier */ 711*593dc095SDavid du Colombier 712*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); 7137dd7cddfSDavid du Colombier /* 7147dd7cddfSDavid du Colombier This function is equivalent to inflateEnd followed by inflateInit, 7157dd7cddfSDavid du Colombier but does not free and reallocate all the internal decompression state. 7167dd7cddfSDavid du Colombier The stream will keep attributes that may have been set by inflateInit2. 7177dd7cddfSDavid du Colombier 7187dd7cddfSDavid du Colombier inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 7197dd7cddfSDavid du Colombier stream state was inconsistent (such as zalloc or state being NULL). 7207dd7cddfSDavid du Colombier */ 7217dd7cddfSDavid du Colombier 722*593dc095SDavid du Colombier /* 723*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateBackInit OF((z_stream FAR *strm, int windowBits, 724*593dc095SDavid du Colombier unsigned char FAR *window)); 725*593dc095SDavid du Colombier 726*593dc095SDavid du Colombier Initialize the internal stream state for decompression using inflateBack() 727*593dc095SDavid du Colombier calls. The fields zalloc, zfree and opaque in strm must be initialized 728*593dc095SDavid du Colombier before the call. If zalloc and zfree are Z_NULL, then the default library- 729*593dc095SDavid du Colombier derived memory allocation routines are used. windowBits is the base two 730*593dc095SDavid du Colombier logarithm of the window size, in the range 8..15. window is a caller 731*593dc095SDavid du Colombier supplied buffer of that size. Except for special applications where it is 732*593dc095SDavid du Colombier assured that deflate was used with small window sizes, windowBits must be 15 733*593dc095SDavid du Colombier and a 32K byte window must be supplied to be able to decompress general 734*593dc095SDavid du Colombier deflate streams. 735*593dc095SDavid du Colombier 736*593dc095SDavid du Colombier See inflateBack() for the usage of these routines. 737*593dc095SDavid du Colombier 738*593dc095SDavid du Colombier inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of 739*593dc095SDavid du Colombier the paramaters are invalid, Z_MEM_ERROR if the internal state could not 740*593dc095SDavid du Colombier be allocated, or Z_VERSION_ERROR if the version of the library does not 741*593dc095SDavid du Colombier match the version of the header file. 742*593dc095SDavid du Colombier */ 743*593dc095SDavid du Colombier 744*593dc095SDavid du Colombier typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); 745*593dc095SDavid du Colombier typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); 746*593dc095SDavid du Colombier 747*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateBack OF((z_stream FAR *strm, 748*593dc095SDavid du Colombier in_func in, void FAR *in_desc, 749*593dc095SDavid du Colombier out_func out, void FAR *out_desc)); 750*593dc095SDavid du Colombier /* 751*593dc095SDavid du Colombier inflateBack() does a raw inflate with a single call using a call-back 752*593dc095SDavid du Colombier interface for input and output. This is more efficient than inflate() for 753*593dc095SDavid du Colombier file i/o applications in that it avoids copying between the output and the 754*593dc095SDavid du Colombier sliding window by simply making the window itself the output buffer. This 755*593dc095SDavid du Colombier function trusts the application to not change the output buffer passed by 756*593dc095SDavid du Colombier the output function, at least until inflateBack() returns. 757*593dc095SDavid du Colombier 758*593dc095SDavid du Colombier inflateBackInit() must be called first to allocate the internal state 759*593dc095SDavid du Colombier and to initialize the state with the user-provided window buffer. 760*593dc095SDavid du Colombier inflateBack() may then be used multiple times to inflate a complete, raw 761*593dc095SDavid du Colombier deflate stream with each call. inflateBackEnd() is then called to free 762*593dc095SDavid du Colombier the allocated state. 763*593dc095SDavid du Colombier 764*593dc095SDavid du Colombier A raw deflate stream is one with no zlib or gzip header or trailer. 765*593dc095SDavid du Colombier This routine would normally be used in a utility that reads zip or gzip 766*593dc095SDavid du Colombier files and writes out uncompressed files. The utility would decode the 767*593dc095SDavid du Colombier header and process the trailer on its own, hence this routine expects 768*593dc095SDavid du Colombier only the raw deflate stream to decompress. This is different from the 769*593dc095SDavid du Colombier normal behavior of inflate(), which expects either a zlib or gzip header and 770*593dc095SDavid du Colombier trailer around the deflate stream. 771*593dc095SDavid du Colombier 772*593dc095SDavid du Colombier inflateBack() uses two subroutines supplied by the caller that are then 773*593dc095SDavid du Colombier called by inflateBack() for input and output. inflateBack() calls those 774*593dc095SDavid du Colombier routines until it reads a complete deflate stream and writes out all of the 775*593dc095SDavid du Colombier uncompressed data, or until it encounters an error. The function's 776*593dc095SDavid du Colombier parameters and return types are defined above in the in_func and out_func 777*593dc095SDavid du Colombier typedefs. inflateBack() will call in(in_desc, &buf) which should return the 778*593dc095SDavid du Colombier number of bytes of provided input, and a pointer to that input in buf. If 779*593dc095SDavid du Colombier there is no input available, in() must return zero--buf is ignored in that 780*593dc095SDavid du Colombier case--and inflateBack() will return a buffer error. inflateBack() will call 781*593dc095SDavid du Colombier out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() 782*593dc095SDavid du Colombier should return zero on success, or non-zero on failure. If out() returns 783*593dc095SDavid du Colombier non-zero, inflateBack() will return with an error. Neither in() nor out() 784*593dc095SDavid du Colombier are permitted to change the contents of the window provided to 785*593dc095SDavid du Colombier inflateBackInit(), which is also the buffer that out() uses to write from. 786*593dc095SDavid du Colombier The length written by out() will be at most the window size. Any non-zero 787*593dc095SDavid du Colombier amount of input may be provided by in(). 788*593dc095SDavid du Colombier 789*593dc095SDavid du Colombier For convenience, inflateBack() can be provided input on the first call by 790*593dc095SDavid du Colombier setting strm->next_in and strm->avail_in. If that input is exhausted, then 791*593dc095SDavid du Colombier in() will be called. Therefore strm->next_in must be initialized before 792*593dc095SDavid du Colombier calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called 793*593dc095SDavid du Colombier immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in 794*593dc095SDavid du Colombier must also be initialized, and then if strm->avail_in is not zero, input will 795*593dc095SDavid du Colombier initially be taken from strm->next_in[0 .. strm->avail_in - 1]. 796*593dc095SDavid du Colombier 797*593dc095SDavid du Colombier The in_desc and out_desc parameters of inflateBack() is passed as the 798*593dc095SDavid du Colombier first parameter of in() and out() respectively when they are called. These 799*593dc095SDavid du Colombier descriptors can be optionally used to pass any information that the caller- 800*593dc095SDavid du Colombier supplied in() and out() functions need to do their job. 801*593dc095SDavid du Colombier 802*593dc095SDavid du Colombier On return, inflateBack() will set strm->next_in and strm->avail_in to 803*593dc095SDavid du Colombier pass back any unused input that was provided by the last in() call. The 804*593dc095SDavid du Colombier return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR 805*593dc095SDavid du Colombier if in() or out() returned an error, Z_DATA_ERROR if there was a format 806*593dc095SDavid du Colombier error in the deflate stream (in which case strm->msg is set to indicate the 807*593dc095SDavid du Colombier nature of the error), or Z_STREAM_ERROR if the stream was not properly 808*593dc095SDavid du Colombier initialized. In the case of Z_BUF_ERROR, an input or output error can be 809*593dc095SDavid du Colombier distinguished using strm->next_in which will be Z_NULL only if in() returned 810*593dc095SDavid du Colombier an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to 811*593dc095SDavid du Colombier out() returning non-zero. (in() will always be called before out(), so 812*593dc095SDavid du Colombier strm->next_in is assured to be defined if out() returns non-zero.) Note 813*593dc095SDavid du Colombier that inflateBack() cannot return Z_OK. 814*593dc095SDavid du Colombier */ 815*593dc095SDavid du Colombier 816*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateBackEnd OF((z_stream FAR *strm)); 817*593dc095SDavid du Colombier /* 818*593dc095SDavid du Colombier All memory allocated by inflateBackInit() is freed. 819*593dc095SDavid du Colombier 820*593dc095SDavid du Colombier inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream 821*593dc095SDavid du Colombier state was inconsistent. 822*593dc095SDavid du Colombier */ 823*593dc095SDavid du Colombier 824*593dc095SDavid du Colombier ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); 825*593dc095SDavid du Colombier /* Return flags indicating compile-time options. 826*593dc095SDavid du Colombier 827*593dc095SDavid du Colombier Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: 828*593dc095SDavid du Colombier 1.0: size of uInt 829*593dc095SDavid du Colombier 3.2: size of uLong 830*593dc095SDavid du Colombier 5.4: size of voidpf (pointer) 831*593dc095SDavid du Colombier 7.6: size of z_off_t 832*593dc095SDavid du Colombier 833*593dc095SDavid du Colombier Compiler, assembler, and debug options: 834*593dc095SDavid du Colombier 8: DEBUG 835*593dc095SDavid du Colombier 9: ASMV or ASMINF -- use ASM code 836*593dc095SDavid du Colombier 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention 837*593dc095SDavid du Colombier 11: 0 (reserved) 838*593dc095SDavid du Colombier 839*593dc095SDavid du Colombier One-time table building (smaller code, but not thread-safe if true): 840*593dc095SDavid du Colombier 12: BUILDFIXED -- build static block decoding tables when needed 841*593dc095SDavid du Colombier 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed 842*593dc095SDavid du Colombier 14,15: 0 (reserved) 843*593dc095SDavid du Colombier 844*593dc095SDavid du Colombier Library content (indicates missing functionality): 845*593dc095SDavid du Colombier 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking 846*593dc095SDavid du Colombier deflate code when not needed) 847*593dc095SDavid du Colombier 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect 848*593dc095SDavid du Colombier and decode gzip streams (to avoid linking crc code) 849*593dc095SDavid du Colombier 18-19: 0 (reserved) 850*593dc095SDavid du Colombier 851*593dc095SDavid du Colombier Operation variations (changes in library functionality): 852*593dc095SDavid du Colombier 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate 853*593dc095SDavid du Colombier 21: FASTEST -- deflate algorithm with only one, lowest compression level 854*593dc095SDavid du Colombier 22,23: 0 (reserved) 855*593dc095SDavid du Colombier 856*593dc095SDavid du Colombier The sprintf variant used by gzprintf (zero is best): 857*593dc095SDavid du Colombier 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format 858*593dc095SDavid du Colombier 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! 859*593dc095SDavid du Colombier 26: 0 = returns value, 1 = void -- 1 means inferred string length returned 860*593dc095SDavid du Colombier 861*593dc095SDavid du Colombier Remainder: 862*593dc095SDavid du Colombier 27-31: 0 (reserved) 863*593dc095SDavid du Colombier */ 864*593dc095SDavid du Colombier 8657dd7cddfSDavid du Colombier 8667dd7cddfSDavid du Colombier /* utility functions */ 8677dd7cddfSDavid du Colombier 8687dd7cddfSDavid du Colombier /* 8697dd7cddfSDavid du Colombier The following utility functions are implemented on top of the 8707dd7cddfSDavid du Colombier basic stream-oriented functions. To simplify the interface, some 871*593dc095SDavid du Colombier default options are assumed (compression level and memory usage, 8727dd7cddfSDavid du Colombier standard memory allocation functions). The source code of these 8737dd7cddfSDavid du Colombier utility functions can easily be modified if you need special options. 8747dd7cddfSDavid du Colombier */ 8757dd7cddfSDavid du Colombier 876*593dc095SDavid du Colombier ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, 8777dd7cddfSDavid du Colombier const Bytef *source, uLong sourceLen)); 8787dd7cddfSDavid du Colombier /* 8797dd7cddfSDavid du Colombier Compresses the source buffer into the destination buffer. sourceLen is 8807dd7cddfSDavid du Colombier the byte length of the source buffer. Upon entry, destLen is the total 881*593dc095SDavid du Colombier size of the destination buffer, which must be at least the value returned 882*593dc095SDavid du Colombier by compressBound(sourceLen). Upon exit, destLen is the actual size of the 8837dd7cddfSDavid du Colombier compressed buffer. 8847dd7cddfSDavid du Colombier This function can be used to compress a whole file at once if the 8857dd7cddfSDavid du Colombier input file is mmap'ed. 8867dd7cddfSDavid du Colombier compress returns Z_OK if success, Z_MEM_ERROR if there was not 8877dd7cddfSDavid du Colombier enough memory, Z_BUF_ERROR if there was not enough room in the output 8887dd7cddfSDavid du Colombier buffer. 8897dd7cddfSDavid du Colombier */ 8907dd7cddfSDavid du Colombier 891*593dc095SDavid du Colombier ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, 892*593dc095SDavid du Colombier const Bytef *source, uLong sourceLen, 893*593dc095SDavid du Colombier int level)); 894*593dc095SDavid du Colombier /* 895*593dc095SDavid du Colombier Compresses the source buffer into the destination buffer. The level 896*593dc095SDavid du Colombier parameter has the same meaning as in deflateInit. sourceLen is the byte 897*593dc095SDavid du Colombier length of the source buffer. Upon entry, destLen is the total size of the 898*593dc095SDavid du Colombier destination buffer, which must be at least the value returned by 899*593dc095SDavid du Colombier compressBound(sourceLen). Upon exit, destLen is the actual size of the 900*593dc095SDavid du Colombier compressed buffer. 901*593dc095SDavid du Colombier 902*593dc095SDavid du Colombier compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 903*593dc095SDavid du Colombier memory, Z_BUF_ERROR if there was not enough room in the output buffer, 904*593dc095SDavid du Colombier Z_STREAM_ERROR if the level parameter is invalid. 905*593dc095SDavid du Colombier */ 906*593dc095SDavid du Colombier 907*593dc095SDavid du Colombier ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); 908*593dc095SDavid du Colombier /* 909*593dc095SDavid du Colombier compressBound() returns an upper bound on the compressed size after 910*593dc095SDavid du Colombier compress() or compress2() on sourceLen bytes. It would be used before 911*593dc095SDavid du Colombier a compress() or compress2() call to allocate the destination buffer. 912*593dc095SDavid du Colombier */ 913*593dc095SDavid du Colombier 914*593dc095SDavid du Colombier ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, 9157dd7cddfSDavid du Colombier const Bytef *source, uLong sourceLen)); 9167dd7cddfSDavid du Colombier /* 9177dd7cddfSDavid du Colombier Decompresses the source buffer into the destination buffer. sourceLen is 9187dd7cddfSDavid du Colombier the byte length of the source buffer. Upon entry, destLen is the total 9197dd7cddfSDavid du Colombier size of the destination buffer, which must be large enough to hold the 9207dd7cddfSDavid du Colombier entire uncompressed data. (The size of the uncompressed data must have 9217dd7cddfSDavid du Colombier been saved previously by the compressor and transmitted to the decompressor 9227dd7cddfSDavid du Colombier by some mechanism outside the scope of this compression library.) 9237dd7cddfSDavid du Colombier Upon exit, destLen is the actual size of the compressed buffer. 9247dd7cddfSDavid du Colombier This function can be used to decompress a whole file at once if the 9257dd7cddfSDavid du Colombier input file is mmap'ed. 9267dd7cddfSDavid du Colombier 9277dd7cddfSDavid du Colombier uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 9287dd7cddfSDavid du Colombier enough memory, Z_BUF_ERROR if there was not enough room in the output 929*593dc095SDavid du Colombier buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. 9307dd7cddfSDavid du Colombier */ 9317dd7cddfSDavid du Colombier 9327dd7cddfSDavid du Colombier 9337dd7cddfSDavid du Colombier typedef voidp gzFile; 9347dd7cddfSDavid du Colombier 935*593dc095SDavid du Colombier ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); 9367dd7cddfSDavid du Colombier /* 9377dd7cddfSDavid du Colombier Opens a gzip (.gz) file for reading or writing. The mode parameter 9387dd7cddfSDavid du Colombier is as in fopen ("rb" or "wb") but can also include a compression level 939*593dc095SDavid du Colombier ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for 940*593dc095SDavid du Colombier Huffman only compression as in "wb1h", or 'R' for run-length encoding 941*593dc095SDavid du Colombier as in "wb1R". (See the description of deflateInit2 for more information 942*593dc095SDavid du Colombier about the strategy parameter.) 943*593dc095SDavid du Colombier 944*593dc095SDavid du Colombier gzopen can be used to read a file which is not in gzip format; in this 945*593dc095SDavid du Colombier case gzread will directly read from the file without decompression. 946*593dc095SDavid du Colombier 9477dd7cddfSDavid du Colombier gzopen returns NULL if the file could not be opened or if there was 9487dd7cddfSDavid du Colombier insufficient memory to allocate the (de)compression state; errno 9497dd7cddfSDavid du Colombier can be checked to distinguish the two cases (if errno is zero, the 950*593dc095SDavid du Colombier zlib error is Z_MEM_ERROR). */ 9517dd7cddfSDavid du Colombier 952*593dc095SDavid du Colombier ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); 9537dd7cddfSDavid du Colombier /* 9547dd7cddfSDavid du Colombier gzdopen() associates a gzFile with the file descriptor fd. File 9557dd7cddfSDavid du Colombier descriptors are obtained from calls like open, dup, creat, pipe or 9567dd7cddfSDavid du Colombier fileno (in the file has been previously opened with fopen). 9577dd7cddfSDavid du Colombier The mode parameter is as in gzopen. 9587dd7cddfSDavid du Colombier The next call of gzclose on the returned gzFile will also close the 9597dd7cddfSDavid du Colombier file descriptor fd, just like fclose(fdopen(fd), mode) closes the file 9607dd7cddfSDavid du Colombier descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). 9617dd7cddfSDavid du Colombier gzdopen returns NULL if there was insufficient memory to allocate 9627dd7cddfSDavid du Colombier the (de)compression state. 9637dd7cddfSDavid du Colombier */ 9647dd7cddfSDavid du Colombier 965*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); 966*593dc095SDavid du Colombier /* 967*593dc095SDavid du Colombier Dynamically update the compression level or strategy. See the description 968*593dc095SDavid du Colombier of deflateInit2 for the meaning of these parameters. 969*593dc095SDavid du Colombier gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not 970*593dc095SDavid du Colombier opened for writing. 971*593dc095SDavid du Colombier */ 972*593dc095SDavid du Colombier 973*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); 9747dd7cddfSDavid du Colombier /* 9757dd7cddfSDavid du Colombier Reads the given number of uncompressed bytes from the compressed file. 9767dd7cddfSDavid du Colombier If the input file was not in gzip format, gzread copies the given number 9777dd7cddfSDavid du Colombier of bytes into the buffer. 9787dd7cddfSDavid du Colombier gzread returns the number of uncompressed bytes actually read (0 for 9797dd7cddfSDavid du Colombier end of file, -1 for error). */ 9807dd7cddfSDavid du Colombier 981*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzwrite OF((gzFile file, 982*593dc095SDavid du Colombier voidpc buf, unsigned len)); 9837dd7cddfSDavid du Colombier /* 9847dd7cddfSDavid du Colombier Writes the given number of uncompressed bytes into the compressed file. 9857dd7cddfSDavid du Colombier gzwrite returns the number of uncompressed bytes actually written 9867dd7cddfSDavid du Colombier (0 in case of error). 9877dd7cddfSDavid du Colombier */ 9887dd7cddfSDavid du Colombier 989*593dc095SDavid du Colombier ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); 990*593dc095SDavid du Colombier /* 991*593dc095SDavid du Colombier Converts, formats, and writes the args to the compressed file under 992*593dc095SDavid du Colombier control of the format string, as in fprintf. gzprintf returns the number of 993*593dc095SDavid du Colombier uncompressed bytes actually written (0 in case of error). The number of 994*593dc095SDavid du Colombier uncompressed bytes written is limited to 4095. The caller should assure that 995*593dc095SDavid du Colombier this limit is not exceeded. If it is exceeded, then gzprintf() will return 996*593dc095SDavid du Colombier return an error (0) with nothing written. In this case, there may also be a 997*593dc095SDavid du Colombier buffer overflow with unpredictable consequences, which is possible only if 998*593dc095SDavid du Colombier zlib was compiled with the insecure functions sprintf() or vsprintf() 999*593dc095SDavid du Colombier because the secure snprintf() or vsnprintf() functions were not available. 1000*593dc095SDavid du Colombier */ 1001*593dc095SDavid du Colombier 1002*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); 1003*593dc095SDavid du Colombier /* 1004*593dc095SDavid du Colombier Writes the given null-terminated string to the compressed file, excluding 1005*593dc095SDavid du Colombier the terminating null character. 1006*593dc095SDavid du Colombier gzputs returns the number of characters written, or -1 in case of error. 1007*593dc095SDavid du Colombier */ 1008*593dc095SDavid du Colombier 1009*593dc095SDavid du Colombier ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); 1010*593dc095SDavid du Colombier /* 1011*593dc095SDavid du Colombier Reads bytes from the compressed file until len-1 characters are read, or 1012*593dc095SDavid du Colombier a newline character is read and transferred to buf, or an end-of-file 1013*593dc095SDavid du Colombier condition is encountered. The string is then terminated with a null 1014*593dc095SDavid du Colombier character. 1015*593dc095SDavid du Colombier gzgets returns buf, or Z_NULL in case of error. 1016*593dc095SDavid du Colombier */ 1017*593dc095SDavid du Colombier 1018*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); 1019*593dc095SDavid du Colombier /* 1020*593dc095SDavid du Colombier Writes c, converted to an unsigned char, into the compressed file. 1021*593dc095SDavid du Colombier gzputc returns the value that was written, or -1 in case of error. 1022*593dc095SDavid du Colombier */ 1023*593dc095SDavid du Colombier 1024*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); 1025*593dc095SDavid du Colombier /* 1026*593dc095SDavid du Colombier Reads one byte from the compressed file. gzgetc returns this byte 1027*593dc095SDavid du Colombier or -1 in case of end of file or error. 1028*593dc095SDavid du Colombier */ 1029*593dc095SDavid du Colombier 1030*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); 1031*593dc095SDavid du Colombier /* 1032*593dc095SDavid du Colombier Push one character back onto the stream to be read again later. 1033*593dc095SDavid du Colombier Only one character of push-back is allowed. gzungetc() returns the 1034*593dc095SDavid du Colombier character pushed, or -1 on failure. gzungetc() will fail if a 1035*593dc095SDavid du Colombier character has been pushed but not read yet, or if c is -1. The pushed 1036*593dc095SDavid du Colombier character will be discarded if the stream is repositioned with gzseek() 1037*593dc095SDavid du Colombier or gzrewind(). 1038*593dc095SDavid du Colombier */ 1039*593dc095SDavid du Colombier 1040*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); 10417dd7cddfSDavid du Colombier /* 10427dd7cddfSDavid du Colombier Flushes all pending output into the compressed file. The parameter 10437dd7cddfSDavid du Colombier flush is as in the deflate() function. The return value is the zlib 10447dd7cddfSDavid du Colombier error number (see function gzerror below). gzflush returns Z_OK if 10457dd7cddfSDavid du Colombier the flush parameter is Z_FINISH and all output could be flushed. 10467dd7cddfSDavid du Colombier gzflush should be called only when strictly necessary because it can 10477dd7cddfSDavid du Colombier degrade compression. 10487dd7cddfSDavid du Colombier */ 10497dd7cddfSDavid du Colombier 1050*593dc095SDavid du Colombier ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, 1051*593dc095SDavid du Colombier z_off_t offset, int whence)); 1052*593dc095SDavid du Colombier /* 1053*593dc095SDavid du Colombier Sets the starting position for the next gzread or gzwrite on the 1054*593dc095SDavid du Colombier given compressed file. The offset represents a number of bytes in the 1055*593dc095SDavid du Colombier uncompressed data stream. The whence parameter is defined as in lseek(2); 1056*593dc095SDavid du Colombier the value SEEK_END is not supported. 1057*593dc095SDavid du Colombier If the file is opened for reading, this function is emulated but can be 1058*593dc095SDavid du Colombier extremely slow. If the file is opened for writing, only forward seeks are 1059*593dc095SDavid du Colombier supported; gzseek then compresses a sequence of zeroes up to the new 1060*593dc095SDavid du Colombier starting position. 1061*593dc095SDavid du Colombier 1062*593dc095SDavid du Colombier gzseek returns the resulting offset location as measured in bytes from 1063*593dc095SDavid du Colombier the beginning of the uncompressed stream, or -1 in case of error, in 1064*593dc095SDavid du Colombier particular if the file is opened for writing and the new starting position 1065*593dc095SDavid du Colombier would be before the current position. 1066*593dc095SDavid du Colombier */ 1067*593dc095SDavid du Colombier 1068*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); 1069*593dc095SDavid du Colombier /* 1070*593dc095SDavid du Colombier Rewinds the given file. This function is supported only for reading. 1071*593dc095SDavid du Colombier 1072*593dc095SDavid du Colombier gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) 1073*593dc095SDavid du Colombier */ 1074*593dc095SDavid du Colombier 1075*593dc095SDavid du Colombier ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); 1076*593dc095SDavid du Colombier /* 1077*593dc095SDavid du Colombier Returns the starting position for the next gzread or gzwrite on the 1078*593dc095SDavid du Colombier given compressed file. This position represents a number of bytes in the 1079*593dc095SDavid du Colombier uncompressed data stream. 1080*593dc095SDavid du Colombier 1081*593dc095SDavid du Colombier gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) 1082*593dc095SDavid du Colombier */ 1083*593dc095SDavid du Colombier 1084*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzeof OF((gzFile file)); 1085*593dc095SDavid du Colombier /* 1086*593dc095SDavid du Colombier Returns 1 when EOF has previously been detected reading the given 1087*593dc095SDavid du Colombier input stream, otherwise zero. 1088*593dc095SDavid du Colombier */ 1089*593dc095SDavid du Colombier 1090*593dc095SDavid du Colombier ZEXTERN int ZEXPORT gzclose OF((gzFile file)); 10917dd7cddfSDavid du Colombier /* 10927dd7cddfSDavid du Colombier Flushes all pending output if necessary, closes the compressed file 10937dd7cddfSDavid du Colombier and deallocates all the (de)compression state. The return value is the zlib 10947dd7cddfSDavid du Colombier error number (see function gzerror below). 10957dd7cddfSDavid du Colombier */ 10967dd7cddfSDavid du Colombier 1097*593dc095SDavid du Colombier ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); 10987dd7cddfSDavid du Colombier /* 10997dd7cddfSDavid du Colombier Returns the error message for the last error which occurred on the 11007dd7cddfSDavid du Colombier given compressed file. errnum is set to zlib error number. If an 11017dd7cddfSDavid du Colombier error occurred in the file system and not in the compression library, 11027dd7cddfSDavid du Colombier errnum is set to Z_ERRNO and the application may consult errno 11037dd7cddfSDavid du Colombier to get the exact error code. 11047dd7cddfSDavid du Colombier */ 11057dd7cddfSDavid du Colombier 1106*593dc095SDavid du Colombier ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); 1107*593dc095SDavid du Colombier /* 1108*593dc095SDavid du Colombier Clears the error and end-of-file flags for file. This is analogous to the 1109*593dc095SDavid du Colombier clearerr() function in stdio. This is useful for continuing to read a gzip 1110*593dc095SDavid du Colombier file that is being written concurrently. 1111*593dc095SDavid du Colombier */ 1112*593dc095SDavid du Colombier 11137dd7cddfSDavid du Colombier /* checksum functions */ 11147dd7cddfSDavid du Colombier 11157dd7cddfSDavid du Colombier /* 11167dd7cddfSDavid du Colombier These functions are not related to compression but are exported 11177dd7cddfSDavid du Colombier anyway because they might be useful in applications using the 11187dd7cddfSDavid du Colombier compression library. 11197dd7cddfSDavid du Colombier */ 11207dd7cddfSDavid du Colombier 1121*593dc095SDavid du Colombier ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); 11227dd7cddfSDavid du Colombier 11237dd7cddfSDavid du Colombier /* 11247dd7cddfSDavid du Colombier Update a running Adler-32 checksum with the bytes buf[0..len-1] and 11257dd7cddfSDavid du Colombier return the updated checksum. If buf is NULL, this function returns 11267dd7cddfSDavid du Colombier the required initial value for the checksum. 11277dd7cddfSDavid du Colombier An Adler-32 checksum is almost as reliable as a CRC32 but can be computed 11287dd7cddfSDavid du Colombier much faster. Usage example: 11297dd7cddfSDavid du Colombier 11307dd7cddfSDavid du Colombier uLong adler = adler32(0L, Z_NULL, 0); 11317dd7cddfSDavid du Colombier 11327dd7cddfSDavid du Colombier while (read_buffer(buffer, length) != EOF) { 11337dd7cddfSDavid du Colombier adler = adler32(adler, buffer, length); 11347dd7cddfSDavid du Colombier } 11357dd7cddfSDavid du Colombier if (adler != original_adler) error(); 11367dd7cddfSDavid du Colombier */ 11377dd7cddfSDavid du Colombier 1138*593dc095SDavid du Colombier ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); 11397dd7cddfSDavid du Colombier /* 11407dd7cddfSDavid du Colombier Update a running crc with the bytes buf[0..len-1] and return the updated 11417dd7cddfSDavid du Colombier crc. If buf is NULL, this function returns the required initial value 11427dd7cddfSDavid du Colombier for the crc. Pre- and post-conditioning (one's complement) is performed 11437dd7cddfSDavid du Colombier within this function so it shouldn't be done by the application. 11447dd7cddfSDavid du Colombier Usage example: 11457dd7cddfSDavid du Colombier 11467dd7cddfSDavid du Colombier uLong crc = crc32(0L, Z_NULL, 0); 11477dd7cddfSDavid du Colombier 11487dd7cddfSDavid du Colombier while (read_buffer(buffer, length) != EOF) { 11497dd7cddfSDavid du Colombier crc = crc32(crc, buffer, length); 11507dd7cddfSDavid du Colombier } 11517dd7cddfSDavid du Colombier if (crc != original_crc) error(); 11527dd7cddfSDavid du Colombier */ 11537dd7cddfSDavid du Colombier 11547dd7cddfSDavid du Colombier 11557dd7cddfSDavid du Colombier /* various hacks, don't look :) */ 11567dd7cddfSDavid du Colombier 11577dd7cddfSDavid du Colombier /* deflateInit and inflateInit are macros to allow checking the zlib version 11587dd7cddfSDavid du Colombier * and the compiler's view of z_stream: 11597dd7cddfSDavid du Colombier */ 1160*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, 11617dd7cddfSDavid du Colombier const char *version, int stream_size)); 1162*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, 11637dd7cddfSDavid du Colombier const char *version, int stream_size)); 1164*593dc095SDavid du Colombier ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, 1165*593dc095SDavid du Colombier int windowBits, int memLevel, 1166*593dc095SDavid du Colombier int strategy, const char *version, 1167*593dc095SDavid du Colombier int stream_size)); 1168*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, 11697dd7cddfSDavid du Colombier const char *version, int stream_size)); 1170*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateBackInit_ OF((z_stream FAR *strm, int windowBits, 1171*593dc095SDavid du Colombier unsigned char FAR *window, 1172*593dc095SDavid du Colombier const char *version, 1173*593dc095SDavid du Colombier int stream_size)); 11747dd7cddfSDavid du Colombier #define deflateInit(strm, level) \ 11757dd7cddfSDavid du Colombier deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) 11767dd7cddfSDavid du Colombier #define inflateInit(strm) \ 11777dd7cddfSDavid du Colombier inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) 11787dd7cddfSDavid du Colombier #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ 11797dd7cddfSDavid du Colombier deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ 11807dd7cddfSDavid du Colombier (strategy), ZLIB_VERSION, sizeof(z_stream)) 11817dd7cddfSDavid du Colombier #define inflateInit2(strm, windowBits) \ 11827dd7cddfSDavid du Colombier inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) 1183*593dc095SDavid du Colombier #define inflateBackInit(strm, windowBits, window) \ 1184*593dc095SDavid du Colombier inflateBackInit_((strm), (windowBits), (window), \ 1185*593dc095SDavid du Colombier ZLIB_VERSION, sizeof(z_stream)) 11867dd7cddfSDavid du Colombier 1187*593dc095SDavid du Colombier 1188*593dc095SDavid du Colombier #if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) 11897dd7cddfSDavid du Colombier struct internal_state {int dummy;}; /* hack for buggy compilers */ 11907dd7cddfSDavid du Colombier #endif 11917dd7cddfSDavid du Colombier 1192*593dc095SDavid du Colombier ZEXTERN const char * ZEXPORT zError OF((int)); 1193*593dc095SDavid du Colombier ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z)); 1194*593dc095SDavid du Colombier ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); 11957dd7cddfSDavid du Colombier 11967dd7cddfSDavid du Colombier #ifdef __cplusplus 11977dd7cddfSDavid du Colombier } 11987dd7cddfSDavid du Colombier #endif 11997dd7cddfSDavid du Colombier 1200*593dc095SDavid du Colombier #endif /* ZLIB_H */ 1201