1*96c32821Schristos /* $NetBSD: zlib.h,v 1.8 2024/09/22 19:12:27 christos Exp $ */ 2aaf4ece6Schristos 3aaf4ece6Schristos /* zlib.h -- interface of the 'zlib' general purpose compression library 4*96c32821Schristos version 1.3.1, January 22nd, 2024 5aaf4ece6Schristos 6*96c32821Schristos Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler 7aaf4ece6Schristos 8aaf4ece6Schristos This software is provided 'as-is', without any express or implied 9aaf4ece6Schristos warranty. In no event will the authors be held liable for any damages 10aaf4ece6Schristos arising from the use of this software. 11aaf4ece6Schristos 12aaf4ece6Schristos Permission is granted to anyone to use this software for any purpose, 13aaf4ece6Schristos including commercial applications, and to alter it and redistribute it 14aaf4ece6Schristos freely, subject to the following restrictions: 15aaf4ece6Schristos 16aaf4ece6Schristos 1. The origin of this software must not be misrepresented; you must not 17aaf4ece6Schristos claim that you wrote the original software. If you use this software 18aaf4ece6Schristos in a product, an acknowledgment in the product documentation would be 19aaf4ece6Schristos appreciated but is not required. 20aaf4ece6Schristos 2. Altered source versions must be plainly marked as such, and must not be 21aaf4ece6Schristos misrepresented as being the original software. 22aaf4ece6Schristos 3. This notice may not be removed or altered from any source distribution. 23aaf4ece6Schristos 24aaf4ece6Schristos Jean-loup Gailly Mark Adler 25aaf4ece6Schristos jloup@gzip.org madler@alumni.caltech.edu 26aaf4ece6Schristos 27aaf4ece6Schristos 28aaf4ece6Schristos The data format used by the zlib library is described by RFCs (Request for 296db8c6e9Schristos Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 306db8c6e9Schristos (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). 31aaf4ece6Schristos */ 32aaf4ece6Schristos 33aaf4ece6Schristos #ifndef ZLIB_H 34aaf4ece6Schristos #define ZLIB_H 35aaf4ece6Schristos 36aaf4ece6Schristos #include "zconf.h" 37aaf4ece6Schristos 38aaf4ece6Schristos #ifdef __cplusplus 39aaf4ece6Schristos extern "C" { 40aaf4ece6Schristos #endif 41aaf4ece6Schristos 42*96c32821Schristos #define ZLIB_VERSION "1.3.1" 43*96c32821Schristos #define ZLIB_VERNUM 0x1310 446db8c6e9Schristos #define ZLIB_VER_MAJOR 1 45*96c32821Schristos #define ZLIB_VER_MINOR 3 46*96c32821Schristos #define ZLIB_VER_REVISION 1 476db8c6e9Schristos #define ZLIB_VER_SUBREVISION 0 48aaf4ece6Schristos 49aaf4ece6Schristos /* 50aaf4ece6Schristos The 'zlib' compression library provides in-memory compression and 516db8c6e9Schristos decompression functions, including integrity checks of the uncompressed data. 526db8c6e9Schristos This version of the library supports only one compression method (deflation) 536db8c6e9Schristos but other algorithms will be added later and will have the same stream 546db8c6e9Schristos interface. 55aaf4ece6Schristos 566db8c6e9Schristos Compression can be done in a single step if the buffers are large enough, 576db8c6e9Schristos or can be done by repeated calls of the compression function. In the latter 586db8c6e9Schristos case, the application must provide more input and/or consume the output 59aaf4ece6Schristos (providing more output space) before each call. 60aaf4ece6Schristos 61aaf4ece6Schristos The compressed data format used by default by the in-memory functions is 62aaf4ece6Schristos the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped 63aaf4ece6Schristos around a deflate stream, which is itself documented in RFC 1951. 64aaf4ece6Schristos 65aaf4ece6Schristos The library also supports reading and writing files in gzip (.gz) format 66aaf4ece6Schristos with an interface similar to that of stdio using the functions that start 67aaf4ece6Schristos with "gz". The gzip format is different from the zlib format. gzip is a 68aaf4ece6Schristos gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. 69aaf4ece6Schristos 706db8c6e9Schristos This library can optionally read and write gzip and raw deflate streams in 716db8c6e9Schristos memory as well. 72aaf4ece6Schristos 73aaf4ece6Schristos The zlib format was designed to be compact and fast for use in memory 74aaf4ece6Schristos and on communications channels. The gzip format was designed for single- 75aaf4ece6Schristos file compression on file systems, has a larger header than zlib to maintain 76aaf4ece6Schristos directory information, and uses a different, slower check method than zlib. 77aaf4ece6Schristos 78aaf4ece6Schristos The library does not install any signal handler. The decoder checks 796db8c6e9Schristos the consistency of the compressed data, so the library should never crash 806db8c6e9Schristos even in the case of corrupted input. 81aaf4ece6Schristos */ 82aaf4ece6Schristos 83*96c32821Schristos typedef voidpf (*alloc_func)(voidpf opaque, uInt items, uInt size); 84*96c32821Schristos typedef void (*free_func)(voidpf opaque, voidpf address); 85aaf4ece6Schristos 86aaf4ece6Schristos struct internal_state; 87aaf4ece6Schristos 88aaf4ece6Schristos typedef struct z_stream_s { 896db8c6e9Schristos z_const Bytef *next_in; /* next input byte */ 90aaf4ece6Schristos uInt avail_in; /* number of bytes available at next_in */ 916db8c6e9Schristos uLong total_in; /* total number of input bytes read so far */ 92aaf4ece6Schristos 936db8c6e9Schristos Bytef *next_out; /* next output byte will go here */ 94aaf4ece6Schristos uInt avail_out; /* remaining free space at next_out */ 956db8c6e9Schristos uLong total_out; /* total number of bytes output so far */ 96aaf4ece6Schristos 976db8c6e9Schristos z_const char *msg; /* last error message, NULL if no error */ 98aaf4ece6Schristos struct internal_state FAR *state; /* not visible by applications */ 99aaf4ece6Schristos 100aaf4ece6Schristos alloc_func zalloc; /* used to allocate the internal state */ 101aaf4ece6Schristos free_func zfree; /* used to free the internal state */ 102aaf4ece6Schristos voidpf opaque; /* private data object passed to zalloc and zfree */ 103aaf4ece6Schristos 1046db8c6e9Schristos int data_type; /* best guess about the data type: binary or text 1056db8c6e9Schristos for deflate, or the decoding state for inflate */ 1066db8c6e9Schristos uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */ 107aaf4ece6Schristos uLong reserved; /* reserved for future use */ 108aaf4ece6Schristos } z_stream; 109aaf4ece6Schristos 110aaf4ece6Schristos typedef z_stream FAR *z_streamp; 111aaf4ece6Schristos 112aaf4ece6Schristos /* 113aaf4ece6Schristos gzip header information passed to and from zlib routines. See RFC 1952 114aaf4ece6Schristos for more details on the meanings of these fields. 115aaf4ece6Schristos */ 116aaf4ece6Schristos typedef struct gz_header_s { 117aaf4ece6Schristos int text; /* true if compressed data believed to be text */ 118aaf4ece6Schristos uLong time; /* modification time */ 119aaf4ece6Schristos int xflags; /* extra flags (not used when writing a gzip file) */ 120aaf4ece6Schristos int os; /* operating system */ 121aaf4ece6Schristos Bytef *extra; /* pointer to extra field or Z_NULL if none */ 122aaf4ece6Schristos uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ 123aaf4ece6Schristos uInt extra_max; /* space at extra (only when reading header) */ 124aaf4ece6Schristos Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ 125aaf4ece6Schristos uInt name_max; /* space at name (only when reading header) */ 126aaf4ece6Schristos Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ 127aaf4ece6Schristos uInt comm_max; /* space at comment (only when reading header) */ 128aaf4ece6Schristos int hcrc; /* true if there was or will be a header crc */ 129aaf4ece6Schristos int done; /* true when done reading gzip header (not used 130aaf4ece6Schristos when writing a gzip file) */ 131aaf4ece6Schristos } gz_header; 132aaf4ece6Schristos 133aaf4ece6Schristos typedef gz_header FAR *gz_headerp; 134aaf4ece6Schristos 135aaf4ece6Schristos /* 1366db8c6e9Schristos The application must update next_in and avail_in when avail_in has dropped 1376db8c6e9Schristos to zero. It must update next_out and avail_out when avail_out has dropped 1386db8c6e9Schristos to zero. The application must initialize zalloc, zfree and opaque before 1396db8c6e9Schristos calling the init function. All other fields are set by the compression 1406db8c6e9Schristos library and must not be updated by the application. 141aaf4ece6Schristos 142aaf4ece6Schristos The opaque value provided by the application will be passed as the first 143aaf4ece6Schristos parameter for calls of zalloc and zfree. This can be useful for custom 144aaf4ece6Schristos memory management. The compression library attaches no meaning to the 145aaf4ece6Schristos opaque value. 146aaf4ece6Schristos 147aaf4ece6Schristos zalloc must return Z_NULL if there is not enough memory for the object. 148aaf4ece6Schristos If zlib is used in a multi-threaded application, zalloc and zfree must be 1496db8c6e9Schristos thread safe. In that case, zlib is thread-safe. When zalloc and zfree are 1506db8c6e9Schristos Z_NULL on entry to the initialization function, they are set to internal 1516db8c6e9Schristos routines that use the standard library functions malloc() and free(). 152aaf4ece6Schristos 153aaf4ece6Schristos On 16-bit systems, the functions zalloc and zfree must be able to allocate 1546db8c6e9Schristos exactly 65536 bytes, but will not be required to allocate more than this if 1556db8c6e9Schristos the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers 1566db8c6e9Schristos returned by zalloc for objects of exactly 65536 bytes *must* have their 1576db8c6e9Schristos offset normalized to zero. The default allocation function provided by this 1586db8c6e9Schristos library ensures this (see zutil.c). To reduce memory requirements and avoid 1596db8c6e9Schristos any allocation of 64K objects, at the expense of compression ratio, compile 1606db8c6e9Schristos the library with -DMAX_WBITS=14 (see zconf.h). 161aaf4ece6Schristos 1626db8c6e9Schristos The fields total_in and total_out can be used for statistics or progress 1636db8c6e9Schristos reports. After compression, total_in holds the total size of the 1646db8c6e9Schristos uncompressed data and may be saved for use by the decompressor (particularly 1656db8c6e9Schristos if the decompressor wants to decompress everything in a single step). 166aaf4ece6Schristos */ 167aaf4ece6Schristos 168aaf4ece6Schristos /* constants */ 169aaf4ece6Schristos 170aaf4ece6Schristos #define Z_NO_FLUSH 0 1716db8c6e9Schristos #define Z_PARTIAL_FLUSH 1 172aaf4ece6Schristos #define Z_SYNC_FLUSH 2 173aaf4ece6Schristos #define Z_FULL_FLUSH 3 174aaf4ece6Schristos #define Z_FINISH 4 175aaf4ece6Schristos #define Z_BLOCK 5 1766db8c6e9Schristos #define Z_TREES 6 177aaf4ece6Schristos /* Allowed flush values; see deflate() and inflate() below for details */ 178aaf4ece6Schristos 179aaf4ece6Schristos #define Z_OK 0 180aaf4ece6Schristos #define Z_STREAM_END 1 181aaf4ece6Schristos #define Z_NEED_DICT 2 182aaf4ece6Schristos #define Z_ERRNO (-1) 183aaf4ece6Schristos #define Z_STREAM_ERROR (-2) 184aaf4ece6Schristos #define Z_DATA_ERROR (-3) 185aaf4ece6Schristos #define Z_MEM_ERROR (-4) 186aaf4ece6Schristos #define Z_BUF_ERROR (-5) 187aaf4ece6Schristos #define Z_VERSION_ERROR (-6) 1886db8c6e9Schristos /* Return codes for the compression/decompression functions. Negative values 1896db8c6e9Schristos * are errors, positive values are used for special but normal events. 190aaf4ece6Schristos */ 191aaf4ece6Schristos 192aaf4ece6Schristos #define Z_NO_COMPRESSION 0 193aaf4ece6Schristos #define Z_BEST_SPEED 1 194aaf4ece6Schristos #define Z_BEST_COMPRESSION 9 195aaf4ece6Schristos #define Z_DEFAULT_COMPRESSION (-1) 196aaf4ece6Schristos /* compression levels */ 197aaf4ece6Schristos 198aaf4ece6Schristos #define Z_FILTERED 1 199aaf4ece6Schristos #define Z_HUFFMAN_ONLY 2 200aaf4ece6Schristos #define Z_RLE 3 201aaf4ece6Schristos #define Z_FIXED 4 202aaf4ece6Schristos #define Z_DEFAULT_STRATEGY 0 203aaf4ece6Schristos /* compression strategy; see deflateInit2() below for details */ 204aaf4ece6Schristos 205aaf4ece6Schristos #define Z_BINARY 0 206aaf4ece6Schristos #define Z_TEXT 1 207aaf4ece6Schristos #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ 208aaf4ece6Schristos #define Z_UNKNOWN 2 2096db8c6e9Schristos /* Possible values of the data_type field for deflate() */ 210aaf4ece6Schristos 211aaf4ece6Schristos #define Z_DEFLATED 8 212aaf4ece6Schristos /* The deflate compression method (the only one supported in this version) */ 213aaf4ece6Schristos 214aaf4ece6Schristos #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 215aaf4ece6Schristos 216aaf4ece6Schristos #define zlib_version zlibVersion() 217aaf4ece6Schristos /* for compatibility with versions < 1.0.2 */ 218aaf4ece6Schristos 2196db8c6e9Schristos 220aaf4ece6Schristos /* basic functions */ 221aaf4ece6Schristos 222*96c32821Schristos ZEXTERN const char * ZEXPORT zlibVersion(void); 223aaf4ece6Schristos /* The application can compare zlibVersion and ZLIB_VERSION for consistency. 2246db8c6e9Schristos If the first character differs, the library code actually used is not 2256db8c6e9Schristos compatible with the zlib.h header file used by the application. This check 2266db8c6e9Schristos is automatically made by deflateInit and inflateInit. 227aaf4ece6Schristos */ 228aaf4ece6Schristos 229aaf4ece6Schristos /* 230*96c32821Schristos ZEXTERN int ZEXPORT deflateInit(z_streamp strm, int level); 231aaf4ece6Schristos 232aaf4ece6Schristos Initializes the internal stream state for compression. The fields 2336db8c6e9Schristos zalloc, zfree and opaque must be initialized before by the caller. If 2346db8c6e9Schristos zalloc and zfree are set to Z_NULL, deflateInit updates them to use default 235*96c32821Schristos allocation functions. total_in, total_out, adler, and msg are initialized. 236aaf4ece6Schristos 237aaf4ece6Schristos The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 2386db8c6e9Schristos 1 gives best speed, 9 gives best compression, 0 gives no compression at all 2396db8c6e9Schristos (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION 2406db8c6e9Schristos requests a default compromise between speed and compression (currently 2416db8c6e9Schristos equivalent to level 6). 242aaf4ece6Schristos 2436db8c6e9Schristos deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 2446db8c6e9Schristos memory, Z_STREAM_ERROR if level is not a valid compression level, or 245aaf4ece6Schristos Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible 2466db8c6e9Schristos with the version assumed by the caller (ZLIB_VERSION). msg is set to null 2476db8c6e9Schristos if there is no error message. deflateInit does not perform any compression: 2486db8c6e9Schristos this will be done by deflate(). 249aaf4ece6Schristos */ 250aaf4ece6Schristos 251aaf4ece6Schristos 252*96c32821Schristos ZEXTERN int ZEXPORT deflate(z_streamp strm, int flush); 253aaf4ece6Schristos /* 254aaf4ece6Schristos deflate compresses as much data as possible, and stops when the input 2556db8c6e9Schristos buffer becomes empty or the output buffer becomes full. It may introduce 2566db8c6e9Schristos some output latency (reading input without producing any output) except when 257aaf4ece6Schristos forced to flush. 258aaf4ece6Schristos 259aaf4ece6Schristos The detailed semantics are as follows. deflate performs one or both of the 260aaf4ece6Schristos following actions: 261aaf4ece6Schristos 262aaf4ece6Schristos - Compress more input starting at next_in and update next_in and avail_in 263aaf4ece6Schristos accordingly. If not all input can be processed (because there is not 264aaf4ece6Schristos enough room in the output buffer), next_in and avail_in are updated and 265aaf4ece6Schristos processing will resume at this point for the next call of deflate(). 266aaf4ece6Schristos 2676db8c6e9Schristos - Generate more output starting at next_out and update next_out and avail_out 268aaf4ece6Schristos accordingly. This action is forced if the parameter flush is non zero. 269aaf4ece6Schristos Forcing flush frequently degrades the compression ratio, so this parameter 2706db8c6e9Schristos should be set only when necessary. Some output may be provided even if 2716db8c6e9Schristos flush is zero. 272aaf4ece6Schristos 273aaf4ece6Schristos Before the call of deflate(), the application should ensure that at least 2746db8c6e9Schristos one of the actions is possible, by providing more input and/or consuming more 2756db8c6e9Schristos output, and updating avail_in or avail_out accordingly; avail_out should 2766db8c6e9Schristos never be zero before the call. The application can consume the compressed 2776db8c6e9Schristos output when it wants, for example when the output buffer is full (avail_out 2786db8c6e9Schristos == 0), or after each call of deflate(). If deflate returns Z_OK and with 2796db8c6e9Schristos zero avail_out, it must be called again after making room in the output 2806db8c6e9Schristos buffer because there might be more output pending. See deflatePending(), 2817991f5a7Sandvar which can be used if desired to determine whether or not there is more output 2826db8c6e9Schristos in that case. 283aaf4ece6Schristos 284aaf4ece6Schristos Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to 2856db8c6e9Schristos decide how much data to accumulate before producing output, in order to 286aaf4ece6Schristos maximize compression. 287aaf4ece6Schristos 288aaf4ece6Schristos If the parameter flush is set to Z_SYNC_FLUSH, all pending output is 289aaf4ece6Schristos flushed to the output buffer and the output is aligned on a byte boundary, so 2906db8c6e9Schristos that the decompressor can get all input data available so far. (In 2916db8c6e9Schristos particular avail_in is zero after the call if enough output space has been 2926db8c6e9Schristos provided before the call.) Flushing may degrade compression for some 2936db8c6e9Schristos compression algorithms and so it should be used only when necessary. This 2946db8c6e9Schristos completes the current deflate block and follows it with an empty stored block 2956db8c6e9Schristos that is three bits plus filler bits to the next byte, followed by four bytes 2966db8c6e9Schristos (00 00 ff ff). 2976db8c6e9Schristos 2986db8c6e9Schristos If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the 2996db8c6e9Schristos output buffer, but the output is not aligned to a byte boundary. All of the 3006db8c6e9Schristos input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. 3016db8c6e9Schristos This completes the current deflate block and follows it with an empty fixed 3026db8c6e9Schristos codes block that is 10 bits long. This assures that enough bytes are output 3036db8c6e9Schristos in order for the decompressor to finish the block before the empty fixed 3046db8c6e9Schristos codes block. 3056db8c6e9Schristos 3066db8c6e9Schristos If flush is set to Z_BLOCK, a deflate block is completed and emitted, as 3076db8c6e9Schristos for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to 3086db8c6e9Schristos seven bits of the current block are held to be written as the next byte after 3096db8c6e9Schristos the next deflate block is completed. In this case, the decompressor may not 3106db8c6e9Schristos be provided enough bits at this point in order to complete decompression of 3116db8c6e9Schristos the data provided so far to the compressor. It may need to wait for the next 3126db8c6e9Schristos block to be emitted. This is for advanced applications that need to control 3136db8c6e9Schristos the emission of deflate blocks. 314aaf4ece6Schristos 315aaf4ece6Schristos If flush is set to Z_FULL_FLUSH, all output is flushed as with 316aaf4ece6Schristos Z_SYNC_FLUSH, and the compression state is reset so that decompression can 317aaf4ece6Schristos restart from this point if previous compressed data has been damaged or if 318aaf4ece6Schristos random access is desired. Using Z_FULL_FLUSH too often can seriously degrade 319aaf4ece6Schristos compression. 320aaf4ece6Schristos 321aaf4ece6Schristos If deflate returns with avail_out == 0, this function must be called again 322aaf4ece6Schristos with the same value of the flush parameter and more output space (updated 323aaf4ece6Schristos avail_out), until the flush is complete (deflate returns with non-zero 324aaf4ece6Schristos avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that 325*96c32821Schristos avail_out is greater than six when the flush marker begins, in order to avoid 326*96c32821Schristos repeated flush markers upon calling deflate() again when avail_out == 0. 327aaf4ece6Schristos 328aaf4ece6Schristos If the parameter flush is set to Z_FINISH, pending input is processed, 3296db8c6e9Schristos pending output is flushed and deflate returns with Z_STREAM_END if there was 3306db8c6e9Schristos enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this 3316db8c6e9Schristos function must be called again with Z_FINISH and more output space (updated 3326db8c6e9Schristos avail_out) but no more input data, until it returns with Z_STREAM_END or an 3336db8c6e9Schristos error. After deflate has returned Z_STREAM_END, the only possible operations 3346db8c6e9Schristos on the stream are deflateReset or deflateEnd. 335aaf4ece6Schristos 3366db8c6e9Schristos Z_FINISH can be used in the first deflate call after deflateInit if all the 3376db8c6e9Schristos compression is to be done in a single step. In order to complete in one 3386db8c6e9Schristos call, avail_out must be at least the value returned by deflateBound (see 3396db8c6e9Schristos below). Then deflate is guaranteed to return Z_STREAM_END. If not enough 3406db8c6e9Schristos output space is provided, deflate will not return Z_STREAM_END, and it must 3416db8c6e9Schristos be called again as described above. 342aaf4ece6Schristos 3436db8c6e9Schristos deflate() sets strm->adler to the Adler-32 checksum of all input read 3446db8c6e9Schristos so far (that is, total_in bytes). If a gzip stream is being generated, then 3456db8c6e9Schristos strm->adler will be the CRC-32 checksum of the input read so far. (See 3466db8c6e9Schristos deflateInit2 below.) 347aaf4ece6Schristos 348aaf4ece6Schristos deflate() may update strm->data_type if it can make a good guess about 3496db8c6e9Schristos the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is 3506db8c6e9Schristos considered binary. This field is only for information purposes and does not 3516db8c6e9Schristos affect the compression algorithm in any manner. 352aaf4ece6Schristos 353aaf4ece6Schristos deflate() returns Z_OK if some progress has been made (more input 354aaf4ece6Schristos processed or more output produced), Z_STREAM_END if all input has been 355aaf4ece6Schristos consumed and all output has been produced (only when flush is set to 356aaf4ece6Schristos Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 3576db8c6e9Schristos if next_in or next_out was Z_NULL or the state was inadvertently written over 3586db8c6e9Schristos by the application), or Z_BUF_ERROR if no progress is possible (for example 3596db8c6e9Schristos avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and 3606db8c6e9Schristos deflate() can be called again with more input and more output space to 3616db8c6e9Schristos continue compressing. 362aaf4ece6Schristos */ 363aaf4ece6Schristos 364aaf4ece6Schristos 365*96c32821Schristos ZEXTERN int ZEXPORT deflateEnd(z_streamp strm); 366aaf4ece6Schristos /* 367aaf4ece6Schristos All dynamically allocated data structures for this stream are freed. 3686db8c6e9Schristos This function discards any unprocessed input and does not flush any pending 3696db8c6e9Schristos output. 370aaf4ece6Schristos 371aaf4ece6Schristos deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 372aaf4ece6Schristos stream state was inconsistent, Z_DATA_ERROR if the stream was freed 3736db8c6e9Schristos prematurely (some input or output was discarded). In the error case, msg 3746db8c6e9Schristos may be set but then points to a static string (which must not be 375aaf4ece6Schristos deallocated). 376aaf4ece6Schristos */ 377aaf4ece6Schristos 378aaf4ece6Schristos 379aaf4ece6Schristos /* 380*96c32821Schristos ZEXTERN int ZEXPORT inflateInit(z_streamp strm); 381aaf4ece6Schristos 382aaf4ece6Schristos Initializes the internal stream state for decompression. The fields 383aaf4ece6Schristos next_in, avail_in, zalloc, zfree and opaque must be initialized before by 3846db8c6e9Schristos the caller. In the current version of inflate, the provided input is not 3856db8c6e9Schristos read or consumed. The allocation of a sliding window will be deferred to 3866db8c6e9Schristos the first call of inflate (if the decompression does not complete on the 3876db8c6e9Schristos first call). If zalloc and zfree are set to Z_NULL, inflateInit updates 388*96c32821Schristos them to use default allocation functions. total_in, total_out, adler, and 389*96c32821Schristos msg are initialized. 390aaf4ece6Schristos 391aaf4ece6Schristos inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 392aaf4ece6Schristos memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 3936db8c6e9Schristos version assumed by the caller, or Z_STREAM_ERROR if the parameters are 3946db8c6e9Schristos invalid, such as a null pointer to the structure. msg is set to null if 3956db8c6e9Schristos there is no error message. inflateInit does not perform any decompression. 3966db8c6e9Schristos Actual decompression will be done by inflate(). So next_in, and avail_in, 3976db8c6e9Schristos next_out, and avail_out are unused and unchanged. The current 3986db8c6e9Schristos implementation of inflateInit() does not process any header information -- 3996db8c6e9Schristos that is deferred until inflate() is called. 400aaf4ece6Schristos */ 401aaf4ece6Schristos 402aaf4ece6Schristos 403*96c32821Schristos ZEXTERN int ZEXPORT inflate(z_streamp strm, int flush); 404aaf4ece6Schristos /* 405aaf4ece6Schristos inflate decompresses as much data as possible, and stops when the input 406aaf4ece6Schristos buffer becomes empty or the output buffer becomes full. It may introduce 407aaf4ece6Schristos some output latency (reading input without producing any output) except when 408aaf4ece6Schristos forced to flush. 409aaf4ece6Schristos 410aaf4ece6Schristos The detailed semantics are as follows. inflate performs one or both of the 411aaf4ece6Schristos following actions: 412aaf4ece6Schristos 413aaf4ece6Schristos - Decompress more input starting at next_in and update next_in and avail_in 414aaf4ece6Schristos accordingly. If not all input can be processed (because there is not 4156db8c6e9Schristos enough room in the output buffer), then next_in and avail_in are updated 4166db8c6e9Schristos accordingly, and processing will resume at this point for the next call of 4176db8c6e9Schristos inflate(). 418aaf4ece6Schristos 4196db8c6e9Schristos - Generate more output starting at next_out and update next_out and avail_out 4206db8c6e9Schristos accordingly. inflate() provides as much output as possible, until there is 4216db8c6e9Schristos no more input data or no more space in the output buffer (see below about 4226db8c6e9Schristos the flush parameter). 423aaf4ece6Schristos 424aaf4ece6Schristos Before the call of inflate(), the application should ensure that at least 4256db8c6e9Schristos one of the actions is possible, by providing more input and/or consuming more 4266db8c6e9Schristos output, and updating the next_* and avail_* values accordingly. If the 4276db8c6e9Schristos caller of inflate() does not provide both available input and available 4286db8c6e9Schristos output space, it is possible that there will be no progress made. The 4296db8c6e9Schristos application can consume the uncompressed output when it wants, for example 4306db8c6e9Schristos when the output buffer is full (avail_out == 0), or after each call of 4316db8c6e9Schristos inflate(). If inflate returns Z_OK and with zero avail_out, it must be 4326db8c6e9Schristos called again after making room in the output buffer because there might be 4336db8c6e9Schristos more output pending. 434aaf4ece6Schristos 4356db8c6e9Schristos The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, 4366db8c6e9Schristos Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much 4376db8c6e9Schristos output as possible to the output buffer. Z_BLOCK requests that inflate() 4386db8c6e9Schristos stop if and when it gets to the next deflate block boundary. When decoding 4396db8c6e9Schristos the zlib or gzip format, this will cause inflate() to return immediately 4406db8c6e9Schristos after the header and before the first block. When doing a raw inflate, 4416db8c6e9Schristos inflate() will go ahead and process the first block, and will return when it 4426db8c6e9Schristos gets to the end of that block, or when it runs out of data. 443aaf4ece6Schristos 444aaf4ece6Schristos The Z_BLOCK option assists in appending to or combining deflate streams. 4456db8c6e9Schristos To assist in this, on return inflate() always sets strm->data_type to the 4466db8c6e9Schristos number of unused bits in the last byte taken from strm->next_in, plus 64 if 4476db8c6e9Schristos inflate() is currently decoding the last block in the deflate stream, plus 4486db8c6e9Schristos 128 if inflate() returned immediately after decoding an end-of-block code or 4496db8c6e9Schristos decoding the complete header up to just before the first byte of the deflate 4506db8c6e9Schristos stream. The end-of-block will not be indicated until all of the uncompressed 4516db8c6e9Schristos data from that block has been written to strm->next_out. The number of 4526db8c6e9Schristos unused bits may in general be greater than seven, except when bit 7 of 4536db8c6e9Schristos data_type is set, in which case the number of unused bits will be less than 4546db8c6e9Schristos eight. data_type is set as noted here every time inflate() returns for all 4556db8c6e9Schristos flush options, and so can be used to determine the amount of currently 4566db8c6e9Schristos consumed input in bits. 4576db8c6e9Schristos 4586db8c6e9Schristos The Z_TREES option behaves as Z_BLOCK does, but it also returns when the 4596db8c6e9Schristos end of each deflate block header is reached, before any actual data in that 4606db8c6e9Schristos block is decoded. This allows the caller to determine the length of the 4616db8c6e9Schristos deflate block header for later use in random access within a deflate block. 4626db8c6e9Schristos 256 is added to the value of strm->data_type when inflate() returns 4636db8c6e9Schristos immediately after reaching the end of the deflate block header. 464aaf4ece6Schristos 465aaf4ece6Schristos inflate() should normally be called until it returns Z_STREAM_END or an 4666db8c6e9Schristos error. However if all decompression is to be performed in a single step (a 4676db8c6e9Schristos single call of inflate), the parameter flush should be set to Z_FINISH. In 4686db8c6e9Schristos this case all pending input is processed and all pending output is flushed; 4696db8c6e9Schristos avail_out must be large enough to hold all of the uncompressed data for the 4706db8c6e9Schristos operation to complete. (The size of the uncompressed data may have been 4716db8c6e9Schristos saved by the compressor for this purpose.) The use of Z_FINISH is not 4726db8c6e9Schristos required to perform an inflation in one step. However it may be used to 4736db8c6e9Schristos inform inflate that a faster approach can be used for the single inflate() 4746db8c6e9Schristos call. Z_FINISH also informs inflate to not maintain a sliding window if the 4756db8c6e9Schristos stream completes, which reduces inflate's memory footprint. If the stream 4766db8c6e9Schristos does not complete, either because not all of the stream is provided or not 4776db8c6e9Schristos enough output space is provided, then a sliding window will be allocated and 4786db8c6e9Schristos inflate() can be called again to continue the operation as if Z_NO_FLUSH had 4796db8c6e9Schristos been used. 480aaf4ece6Schristos 481aaf4ece6Schristos In this implementation, inflate() always flushes as much output as 482aaf4ece6Schristos possible to the output buffer, and always uses the faster approach on the 4836db8c6e9Schristos first call. So the effects of the flush parameter in this implementation are 4846db8c6e9Schristos on the return value of inflate() as noted below, when inflate() returns early 4856db8c6e9Schristos when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of 4866db8c6e9Schristos memory for a sliding window when Z_FINISH is used. 487aaf4ece6Schristos 488aaf4ece6Schristos If a preset dictionary is needed after this call (see inflateSetDictionary 4896db8c6e9Schristos below), inflate sets strm->adler to the Adler-32 checksum of the dictionary 490aaf4ece6Schristos chosen by the compressor and returns Z_NEED_DICT; otherwise it sets 4916db8c6e9Schristos strm->adler to the Adler-32 checksum of all output produced so far (that is, 492aaf4ece6Schristos total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described 4936db8c6e9Schristos below. At the end of the stream, inflate() checks that its computed Adler-32 494aaf4ece6Schristos checksum is equal to that saved by the compressor and returns Z_STREAM_END 495aaf4ece6Schristos only if the checksum is correct. 496aaf4ece6Schristos 4976db8c6e9Schristos inflate() can decompress and check either zlib-wrapped or gzip-wrapped 4986db8c6e9Schristos deflate data. The header type is detected automatically, if requested when 4996db8c6e9Schristos initializing with inflateInit2(). Any information contained in the gzip 5006db8c6e9Schristos header is not retained unless inflateGetHeader() is used. When processing 5016db8c6e9Schristos gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output 5026db8c6e9Schristos produced so far. The CRC-32 is checked against the gzip trailer, as is the 5036db8c6e9Schristos uncompressed length, modulo 2^32. 504aaf4ece6Schristos 505aaf4ece6Schristos inflate() returns Z_OK if some progress has been made (more input processed 506aaf4ece6Schristos or more output produced), Z_STREAM_END if the end of the compressed data has 507aaf4ece6Schristos been reached and all uncompressed output has been produced, Z_NEED_DICT if a 508aaf4ece6Schristos preset dictionary is needed at this point, Z_DATA_ERROR if the input data was 509aaf4ece6Schristos corrupted (input stream not conforming to the zlib format or incorrect check 5106db8c6e9Schristos value, in which case strm->msg points to a string with a more specific 5116db8c6e9Schristos error), Z_STREAM_ERROR if the stream structure was inconsistent (for example 5126db8c6e9Schristos next_in or next_out was Z_NULL, or the state was inadvertently written over 5136db8c6e9Schristos by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR 5146db8c6e9Schristos if no progress was possible or if there was not enough room in the output 5156db8c6e9Schristos buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and 516aaf4ece6Schristos inflate() can be called again with more input and more output space to 5176db8c6e9Schristos continue decompressing. If Z_DATA_ERROR is returned, the application may 5186db8c6e9Schristos then call inflateSync() to look for a good compression block if a partial 5196db8c6e9Schristos recovery of the data is to be attempted. 520aaf4ece6Schristos */ 521aaf4ece6Schristos 522aaf4ece6Schristos 523*96c32821Schristos ZEXTERN int ZEXPORT inflateEnd(z_streamp strm); 524aaf4ece6Schristos /* 525aaf4ece6Schristos All dynamically allocated data structures for this stream are freed. 5266db8c6e9Schristos This function discards any unprocessed input and does not flush any pending 5276db8c6e9Schristos output. 528aaf4ece6Schristos 5296db8c6e9Schristos inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state 5306db8c6e9Schristos was inconsistent. 531aaf4ece6Schristos */ 532aaf4ece6Schristos 5336db8c6e9Schristos 534aaf4ece6Schristos /* Advanced functions */ 535aaf4ece6Schristos 536aaf4ece6Schristos /* 537aaf4ece6Schristos The following functions are needed only in some special applications. 538aaf4ece6Schristos */ 539aaf4ece6Schristos 540aaf4ece6Schristos /* 541*96c32821Schristos ZEXTERN int ZEXPORT deflateInit2(z_streamp strm, 542aaf4ece6Schristos int level, 543aaf4ece6Schristos int method, 544aaf4ece6Schristos int windowBits, 545aaf4ece6Schristos int memLevel, 546*96c32821Schristos int strategy); 547aaf4ece6Schristos 548aaf4ece6Schristos This is another version of deflateInit with more compression options. The 5493b1253e8Schristos fields zalloc, zfree and opaque must be initialized before by the caller. 550aaf4ece6Schristos 551aaf4ece6Schristos The method parameter is the compression method. It must be Z_DEFLATED in 552aaf4ece6Schristos this version of the library. 553aaf4ece6Schristos 554aaf4ece6Schristos The windowBits parameter is the base two logarithm of the window size 555aaf4ece6Schristos (the size of the history buffer). It should be in the range 8..15 for this 556aaf4ece6Schristos version of the library. Larger values of this parameter result in better 557aaf4ece6Schristos compression at the expense of memory usage. The default value is 15 if 558aaf4ece6Schristos deflateInit is used instead. 559aaf4ece6Schristos 5606db8c6e9Schristos For the current implementation of deflate(), a windowBits value of 8 (a 5616db8c6e9Schristos window size of 256 bytes) is not supported. As a result, a request for 8 5626db8c6e9Schristos will result in 9 (a 512-byte window). In that case, providing 8 to 5636db8c6e9Schristos inflateInit2() will result in an error when the zlib header with 9 is 5646db8c6e9Schristos checked against the initialization of inflate(). The remedy is to not use 8 5656db8c6e9Schristos with deflateInit2() with this initialization, or at least in that case use 9 5666db8c6e9Schristos with inflateInit2(). 5676db8c6e9Schristos 568aaf4ece6Schristos windowBits can also be -8..-15 for raw deflate. In this case, -windowBits 569aaf4ece6Schristos determines the window size. deflate() will then generate raw deflate data 5706db8c6e9Schristos with no zlib header or trailer, and will not compute a check value. 571aaf4ece6Schristos 572aaf4ece6Schristos windowBits can also be greater than 15 for optional gzip encoding. Add 573aaf4ece6Schristos 16 to windowBits to write a simple gzip header and trailer around the 574aaf4ece6Schristos compressed data instead of a zlib wrapper. The gzip header will have no 5756db8c6e9Schristos file name, no extra data, no comment, no modification time (set to zero), no 5766db8c6e9Schristos header crc, and the operating system will be set to the appropriate value, 5776db8c6e9Schristos if the operating system was determined at compile time. If a gzip stream is 5786db8c6e9Schristos being written, strm->adler is a CRC-32 instead of an Adler-32. 5796db8c6e9Schristos 5806db8c6e9Schristos For raw deflate or gzip encoding, a request for a 256-byte window is 5816db8c6e9Schristos rejected as invalid, since only the zlib header provides a means of 5826db8c6e9Schristos transmitting the window size to the decompressor. 583aaf4ece6Schristos 584aaf4ece6Schristos The memLevel parameter specifies how much memory should be allocated 5856db8c6e9Schristos for the internal compression state. memLevel=1 uses minimum memory but is 5866db8c6e9Schristos slow and reduces compression ratio; memLevel=9 uses maximum memory for 5876db8c6e9Schristos optimal speed. The default value is 8. See zconf.h for total memory usage 5886db8c6e9Schristos as a function of windowBits and memLevel. 589aaf4ece6Schristos 590aaf4ece6Schristos The strategy parameter is used to tune the compression algorithm. Use the 591aaf4ece6Schristos value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a 592aaf4ece6Schristos filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no 593aaf4ece6Schristos string match), or Z_RLE to limit match distances to one (run-length 594aaf4ece6Schristos encoding). Filtered data consists mostly of small values with a somewhat 595aaf4ece6Schristos random distribution. In this case, the compression algorithm is tuned to 596aaf4ece6Schristos compress them better. The effect of Z_FILTERED is to force more Huffman 597aaf4ece6Schristos coding and less string matching; it is somewhat intermediate between 5986db8c6e9Schristos Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as 5996db8c6e9Schristos fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The 6006db8c6e9Schristos strategy parameter only affects the compression ratio but not the 6016db8c6e9Schristos correctness of the compressed output even if it is not set appropriately. 6026db8c6e9Schristos Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler 6036db8c6e9Schristos decoder for special applications. 604aaf4ece6Schristos 605aaf4ece6Schristos deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 6066db8c6e9Schristos memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid 6076db8c6e9Schristos method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is 6086db8c6e9Schristos incompatible with the version assumed by the caller (ZLIB_VERSION). msg is 6096db8c6e9Schristos set to null if there is no error message. deflateInit2 does not perform any 6106db8c6e9Schristos compression: this will be done by deflate(). 611aaf4ece6Schristos */ 612aaf4ece6Schristos 613*96c32821Schristos ZEXTERN int ZEXPORT deflateSetDictionary(z_streamp strm, 614aaf4ece6Schristos const Bytef *dictionary, 615*96c32821Schristos uInt dictLength); 616aaf4ece6Schristos /* 617aaf4ece6Schristos Initializes the compression dictionary from the given byte sequence 6186db8c6e9Schristos without producing any compressed output. When using the zlib format, this 6196db8c6e9Schristos function must be called immediately after deflateInit, deflateInit2 or 6206db8c6e9Schristos deflateReset, and before any call of deflate. When doing raw deflate, this 6216db8c6e9Schristos function must be called either before any call of deflate, or immediately 6226db8c6e9Schristos after the completion of a deflate block, i.e. after all input has been 6236db8c6e9Schristos consumed and all output has been delivered when using any of the flush 6246db8c6e9Schristos options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The 6256db8c6e9Schristos compressor and decompressor must use exactly the same dictionary (see 6266db8c6e9Schristos inflateSetDictionary). 627aaf4ece6Schristos 628aaf4ece6Schristos The dictionary should consist of strings (byte sequences) that are likely 629aaf4ece6Schristos to be encountered later in the data to be compressed, with the most commonly 630aaf4ece6Schristos used strings preferably put towards the end of the dictionary. Using a 631aaf4ece6Schristos dictionary is most useful when the data to be compressed is short and can be 632aaf4ece6Schristos predicted with good accuracy; the data can then be compressed better than 633aaf4ece6Schristos with the default empty dictionary. 634aaf4ece6Schristos 635aaf4ece6Schristos Depending on the size of the compression data structures selected by 636aaf4ece6Schristos deflateInit or deflateInit2, a part of the dictionary may in effect be 6376db8c6e9Schristos discarded, for example if the dictionary is larger than the window size 6386db8c6e9Schristos provided in deflateInit or deflateInit2. Thus the strings most likely to be 6396db8c6e9Schristos useful should be put at the end of the dictionary, not at the front. In 6406db8c6e9Schristos addition, the current implementation of deflate will use at most the window 6416db8c6e9Schristos size minus 262 bytes of the provided dictionary. 642aaf4ece6Schristos 6436db8c6e9Schristos Upon return of this function, strm->adler is set to the Adler-32 value 644aaf4ece6Schristos of the dictionary; the decompressor may later use this value to determine 6456db8c6e9Schristos which dictionary has been used by the compressor. (The Adler-32 value 646aaf4ece6Schristos applies to the whole dictionary even if only a subset of the dictionary is 647aaf4ece6Schristos actually used by the compressor.) If a raw deflate was requested, then the 6486db8c6e9Schristos Adler-32 value is not computed and strm->adler is not set. 649aaf4ece6Schristos 650aaf4ece6Schristos deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a 6516db8c6e9Schristos parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is 652aaf4ece6Schristos inconsistent (for example if deflate has already been called for this stream 6536db8c6e9Schristos or if not at a block boundary for raw deflate). deflateSetDictionary does 6546db8c6e9Schristos not perform any compression: this will be done by deflate(). 6556db8c6e9Schristos */ 6566db8c6e9Schristos 657*96c32821Schristos ZEXTERN int ZEXPORT deflateGetDictionary(z_streamp strm, 6586db8c6e9Schristos Bytef *dictionary, 659*96c32821Schristos uInt *dictLength); 6606db8c6e9Schristos /* 6616db8c6e9Schristos Returns the sliding dictionary being maintained by deflate. dictLength is 6626db8c6e9Schristos set to the number of bytes in the dictionary, and that many bytes are copied 6636db8c6e9Schristos to dictionary. dictionary must have enough space, where 32768 bytes is 6646db8c6e9Schristos always enough. If deflateGetDictionary() is called with dictionary equal to 6656db8c6e9Schristos Z_NULL, then only the dictionary length is returned, and nothing is copied. 6663b1253e8Schristos Similarly, if dictLength is Z_NULL, then it is not set. 6676db8c6e9Schristos 6686db8c6e9Schristos deflateGetDictionary() may return a length less than the window size, even 6696db8c6e9Schristos when more than the window size in input has been provided. It may return up 6706db8c6e9Schristos to 258 bytes less in that case, due to how zlib's implementation of deflate 6716db8c6e9Schristos manages the sliding window and lookahead for matches, where matches can be 6726db8c6e9Schristos up to 258 bytes long. If the application needs the last window-size bytes of 6736db8c6e9Schristos input, then that would need to be saved by the application outside of zlib. 6746db8c6e9Schristos 6756db8c6e9Schristos deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the 6766db8c6e9Schristos stream state is inconsistent. 677aaf4ece6Schristos */ 678aaf4ece6Schristos 679*96c32821Schristos ZEXTERN int ZEXPORT deflateCopy(z_streamp dest, 680*96c32821Schristos z_streamp source); 681aaf4ece6Schristos /* 682aaf4ece6Schristos Sets the destination stream as a complete copy of the source stream. 683aaf4ece6Schristos 684aaf4ece6Schristos This function can be useful when several compression strategies will be 685aaf4ece6Schristos tried, for example when there are several ways of pre-processing the input 686aaf4ece6Schristos data with a filter. The streams that will be discarded should then be freed 687aaf4ece6Schristos by calling deflateEnd. Note that deflateCopy duplicates the internal 6886db8c6e9Schristos compression state which can be quite large, so this strategy is slow and can 6896db8c6e9Schristos consume lots of memory. 690aaf4ece6Schristos 691aaf4ece6Schristos deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 692aaf4ece6Schristos enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 6936db8c6e9Schristos (such as zalloc being Z_NULL). msg is left unchanged in both source and 694aaf4ece6Schristos destination. 695aaf4ece6Schristos */ 696aaf4ece6Schristos 697*96c32821Schristos ZEXTERN int ZEXPORT deflateReset(z_streamp strm); 698aaf4ece6Schristos /* 6996db8c6e9Schristos This function is equivalent to deflateEnd followed by deflateInit, but 7006db8c6e9Schristos does not free and reallocate the internal compression state. The stream 7016db8c6e9Schristos will leave the compression level and any other attributes that may have been 702*96c32821Schristos set unchanged. total_in, total_out, adler, and msg are initialized. 703aaf4ece6Schristos 704aaf4ece6Schristos deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 7056db8c6e9Schristos stream state was inconsistent (such as zalloc or state being Z_NULL). 706aaf4ece6Schristos */ 707aaf4ece6Schristos 708*96c32821Schristos ZEXTERN int ZEXPORT deflateParams(z_streamp strm, 709aaf4ece6Schristos int level, 710*96c32821Schristos int strategy); 711aaf4ece6Schristos /* 712aaf4ece6Schristos Dynamically update the compression level and compression strategy. The 7136db8c6e9Schristos interpretation of level and strategy is as in deflateInit2(). This can be 714aaf4ece6Schristos used to switch between compression and straight copy of the input data, or 7156db8c6e9Schristos to switch to a different kind of input data requiring a different strategy. 7166db8c6e9Schristos If the compression approach (which is a function of the level) or the 7173b1253e8Schristos strategy is changed, and if there have been any deflate() calls since the 7183b1253e8Schristos state was initialized or reset, then the input available so far is 7193b1253e8Schristos compressed with the old level and strategy using deflate(strm, Z_BLOCK). 7203b1253e8Schristos There are three approaches for the compression levels 0, 1..3, and 4..9 7213b1253e8Schristos respectively. The new level and strategy will take effect at the next call 7223b1253e8Schristos of deflate(). 723aaf4ece6Schristos 7246db8c6e9Schristos If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does 7256db8c6e9Schristos not have enough output space to complete, then the parameter change will not 7266db8c6e9Schristos take effect. In this case, deflateParams() can be called again with the 7276db8c6e9Schristos same parameters and more output space to try again. 728aaf4ece6Schristos 7296db8c6e9Schristos In order to assure a change in the parameters on the first try, the 7306db8c6e9Schristos deflate stream should be flushed using deflate() with Z_BLOCK or other flush 7316db8c6e9Schristos request until strm.avail_out is not zero, before calling deflateParams(). 7326db8c6e9Schristos Then no more input data should be provided before the deflateParams() call. 7336db8c6e9Schristos If this is done, the old level and strategy will be applied to the data 7346db8c6e9Schristos compressed before deflateParams(), and the new level and strategy will be 7357991f5a7Sandvar applied to the data compressed after deflateParams(). 7366db8c6e9Schristos 7376db8c6e9Schristos deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream 7386db8c6e9Schristos state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if 7396db8c6e9Schristos there was not enough output space to complete the compression of the 7406db8c6e9Schristos available input data before a change in the strategy or approach. Note that 7416db8c6e9Schristos in the case of a Z_BUF_ERROR, the parameters are not changed. A return 7426db8c6e9Schristos value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be 7436db8c6e9Schristos retried with more output space. 744aaf4ece6Schristos */ 745aaf4ece6Schristos 746*96c32821Schristos ZEXTERN int ZEXPORT deflateTune(z_streamp strm, 747aaf4ece6Schristos int good_length, 748aaf4ece6Schristos int max_lazy, 749aaf4ece6Schristos int nice_length, 750*96c32821Schristos int max_chain); 751aaf4ece6Schristos /* 752aaf4ece6Schristos Fine tune deflate's internal compression parameters. This should only be 753aaf4ece6Schristos used by someone who understands the algorithm used by zlib's deflate for 754aaf4ece6Schristos searching for the best matching string, and even then only by the most 755aaf4ece6Schristos fanatic optimizer trying to squeeze out the last compressed bit for their 756aaf4ece6Schristos specific input data. Read the deflate.c source code for the meaning of the 757aaf4ece6Schristos max_lazy, good_length, nice_length, and max_chain parameters. 758aaf4ece6Schristos 759aaf4ece6Schristos deflateTune() can be called after deflateInit() or deflateInit2(), and 760aaf4ece6Schristos returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. 761aaf4ece6Schristos */ 762aaf4ece6Schristos 763*96c32821Schristos ZEXTERN uLong ZEXPORT deflateBound(z_streamp strm, 764*96c32821Schristos uLong sourceLen); 765aaf4ece6Schristos /* 766aaf4ece6Schristos deflateBound() returns an upper bound on the compressed size after 7676db8c6e9Schristos deflation of sourceLen bytes. It must be called after deflateInit() or 7686db8c6e9Schristos deflateInit2(), and after deflateSetHeader(), if used. This would be used 7696db8c6e9Schristos to allocate an output buffer for deflation in a single pass, and so would be 7706db8c6e9Schristos called before deflate(). If that first deflate() call is provided the 7716db8c6e9Schristos sourceLen input bytes, an output buffer allocated to the size returned by 7726db8c6e9Schristos deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed 7736db8c6e9Schristos to return Z_STREAM_END. Note that it is possible for the compressed size to 7746db8c6e9Schristos be larger than the value returned by deflateBound() if flush options other 7756db8c6e9Schristos than Z_FINISH or Z_NO_FLUSH are used. 7766db8c6e9Schristos */ 7776db8c6e9Schristos 778*96c32821Schristos ZEXTERN int ZEXPORT deflatePending(z_streamp strm, 7796db8c6e9Schristos unsigned *pending, 780*96c32821Schristos int *bits); 7816db8c6e9Schristos /* 7826db8c6e9Schristos deflatePending() returns the number of bytes and bits of output that have 7836db8c6e9Schristos been generated, but not yet provided in the available output. The bytes not 7846db8c6e9Schristos provided would be due to the available output space having being consumed. 7856db8c6e9Schristos The number of bits of output not provided are between 0 and 7, where they 7866db8c6e9Schristos await more bits to join them in order to fill out a full byte. If pending 7876db8c6e9Schristos or bits are Z_NULL, then those values are not set. 7886db8c6e9Schristos 7896db8c6e9Schristos deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source 7906db8c6e9Schristos stream state was inconsistent. 791aaf4ece6Schristos */ 792aaf4ece6Schristos 793*96c32821Schristos ZEXTERN int ZEXPORT deflatePrime(z_streamp strm, 794aaf4ece6Schristos int bits, 795*96c32821Schristos int value); 796aaf4ece6Schristos /* 797aaf4ece6Schristos deflatePrime() inserts bits in the deflate output stream. The intent 7986db8c6e9Schristos is that this function is used to start off the deflate output with the bits 7996db8c6e9Schristos leftover from a previous deflate stream when appending to it. As such, this 8006db8c6e9Schristos function can only be used for raw deflate, and must be used before the first 8016db8c6e9Schristos deflate() call after a deflateInit2() or deflateReset(). bits must be less 8026db8c6e9Schristos than or equal to 16, and that many of the least significant bits of value 8036db8c6e9Schristos will be inserted in the output. 804aaf4ece6Schristos 8056db8c6e9Schristos deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough 8066db8c6e9Schristos room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the 8076db8c6e9Schristos source stream state was inconsistent. 808aaf4ece6Schristos */ 809aaf4ece6Schristos 810*96c32821Schristos ZEXTERN int ZEXPORT deflateSetHeader(z_streamp strm, 811*96c32821Schristos gz_headerp head); 812aaf4ece6Schristos /* 813aaf4ece6Schristos deflateSetHeader() provides gzip header information for when a gzip 814aaf4ece6Schristos stream is requested by deflateInit2(). deflateSetHeader() may be called 815aaf4ece6Schristos after deflateInit2() or deflateReset() and before the first call of 816aaf4ece6Schristos deflate(). The text, time, os, extra field, name, and comment information 817aaf4ece6Schristos in the provided gz_header structure are written to the gzip header (xflag is 818aaf4ece6Schristos ignored -- the extra flags are set according to the compression level). The 819aaf4ece6Schristos caller must assure that, if not Z_NULL, name and comment are terminated with 820aaf4ece6Schristos a zero byte, and that if extra is not Z_NULL, that extra_len bytes are 821aaf4ece6Schristos available there. If hcrc is true, a gzip header crc is included. Note that 822aaf4ece6Schristos the current versions of the command-line version of gzip (up through version 823aaf4ece6Schristos 1.3.x) do not support header crc's, and will report that it is a "multi-part 824aaf4ece6Schristos gzip file" and give up. 825aaf4ece6Schristos 826aaf4ece6Schristos If deflateSetHeader is not used, the default gzip header has text false, 827*96c32821Schristos the time set to zero, and os set to the current operating system, with no 828*96c32821Schristos extra, name, or comment fields. The gzip header is returned to the default 829*96c32821Schristos state by deflateReset(). 830aaf4ece6Schristos 831aaf4ece6Schristos deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source 832aaf4ece6Schristos stream state was inconsistent. 833aaf4ece6Schristos */ 834aaf4ece6Schristos 835aaf4ece6Schristos /* 836*96c32821Schristos ZEXTERN int ZEXPORT inflateInit2(z_streamp strm, 837*96c32821Schristos int windowBits); 838aaf4ece6Schristos 839aaf4ece6Schristos This is another version of inflateInit with an extra parameter. The 840aaf4ece6Schristos fields next_in, avail_in, zalloc, zfree and opaque must be initialized 841aaf4ece6Schristos before by the caller. 842aaf4ece6Schristos 843aaf4ece6Schristos The windowBits parameter is the base two logarithm of the maximum window 844aaf4ece6Schristos size (the size of the history buffer). It should be in the range 8..15 for 845aaf4ece6Schristos this version of the library. The default value is 15 if inflateInit is used 846aaf4ece6Schristos instead. windowBits must be greater than or equal to the windowBits value 847aaf4ece6Schristos provided to deflateInit2() while compressing, or it must be equal to 15 if 848aaf4ece6Schristos deflateInit2() was not used. If a compressed stream with a larger window 849aaf4ece6Schristos size is given as input, inflate() will return with the error code 850aaf4ece6Schristos Z_DATA_ERROR instead of trying to allocate a larger window. 851aaf4ece6Schristos 8526db8c6e9Schristos windowBits can also be zero to request that inflate use the window size in 8536db8c6e9Schristos the zlib header of the compressed stream. 8546db8c6e9Schristos 855aaf4ece6Schristos windowBits can also be -8..-15 for raw inflate. In this case, -windowBits 856aaf4ece6Schristos determines the window size. inflate() will then process raw deflate data, 857aaf4ece6Schristos not looking for a zlib or gzip header, not generating a check value, and not 858aaf4ece6Schristos looking for any check values for comparison at the end of the stream. This 859aaf4ece6Schristos is for use with other formats that use the deflate compressed data format 860aaf4ece6Schristos such as zip. Those formats provide their own check values. If a custom 861aaf4ece6Schristos format is developed using the raw deflate format for compressed data, it is 8626db8c6e9Schristos recommended that a check value such as an Adler-32 or a CRC-32 be applied to 863aaf4ece6Schristos the uncompressed data as is done in the zlib, gzip, and zip formats. For 864aaf4ece6Schristos most applications, the zlib format should be used as is. Note that comments 865aaf4ece6Schristos above on the use in deflateInit2() applies to the magnitude of windowBits. 866aaf4ece6Schristos 867aaf4ece6Schristos windowBits can also be greater than 15 for optional gzip decoding. Add 868aaf4ece6Schristos 32 to windowBits to enable zlib and gzip decoding with automatic header 869aaf4ece6Schristos detection, or add 16 to decode only the gzip format (the zlib format will 8706db8c6e9Schristos return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a 8716db8c6e9Schristos CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see 8723b1253e8Schristos below), inflate() will *not* automatically decode concatenated gzip members. 8733b1253e8Schristos inflate() will return Z_STREAM_END at the end of the gzip member. The state 8743b1253e8Schristos would need to be reset to continue decoding a subsequent gzip member. This 8753b1253e8Schristos *must* be done if there is more data after a gzip member, in order for the 8763b1253e8Schristos decompression to be compliant with the gzip standard (RFC 1952). 877aaf4ece6Schristos 878aaf4ece6Schristos inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 8796db8c6e9Schristos memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 8806db8c6e9Schristos version assumed by the caller, or Z_STREAM_ERROR if the parameters are 8816db8c6e9Schristos invalid, such as a null pointer to the structure. msg is set to null if 8826db8c6e9Schristos there is no error message. inflateInit2 does not perform any decompression 8836db8c6e9Schristos apart from possibly reading the zlib header if present: actual decompression 8846db8c6e9Schristos will be done by inflate(). (So next_in and avail_in may be modified, but 8856db8c6e9Schristos next_out and avail_out are unused and unchanged.) The current implementation 8866db8c6e9Schristos of inflateInit2() does not process any header information -- that is 8876db8c6e9Schristos deferred until inflate() is called. 888aaf4ece6Schristos */ 889aaf4ece6Schristos 890*96c32821Schristos ZEXTERN int ZEXPORT inflateSetDictionary(z_streamp strm, 891aaf4ece6Schristos const Bytef *dictionary, 892*96c32821Schristos uInt dictLength); 893aaf4ece6Schristos /* 894aaf4ece6Schristos Initializes the decompression dictionary from the given uncompressed byte 895aaf4ece6Schristos sequence. This function must be called immediately after a call of inflate, 896aaf4ece6Schristos if that call returned Z_NEED_DICT. The dictionary chosen by the compressor 8976db8c6e9Schristos can be determined from the Adler-32 value returned by that call of inflate. 898aaf4ece6Schristos The compressor and decompressor must use exactly the same dictionary (see 8996db8c6e9Schristos deflateSetDictionary). For raw inflate, this function can be called at any 9006db8c6e9Schristos time to set the dictionary. If the provided dictionary is smaller than the 9016db8c6e9Schristos window and there is already data in the window, then the provided dictionary 9026db8c6e9Schristos will amend what's there. The application must insure that the dictionary 9036db8c6e9Schristos that was used for compression is provided. 904aaf4ece6Schristos 905aaf4ece6Schristos inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a 9066db8c6e9Schristos parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is 907aaf4ece6Schristos inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the 9086db8c6e9Schristos expected one (incorrect Adler-32 value). inflateSetDictionary does not 909aaf4ece6Schristos perform any decompression: this will be done by subsequent calls of 910aaf4ece6Schristos inflate(). 911aaf4ece6Schristos */ 912aaf4ece6Schristos 913*96c32821Schristos ZEXTERN int ZEXPORT inflateGetDictionary(z_streamp strm, 9146db8c6e9Schristos Bytef *dictionary, 915*96c32821Schristos uInt *dictLength); 9166db8c6e9Schristos /* 9176db8c6e9Schristos Returns the sliding dictionary being maintained by inflate. dictLength is 9186db8c6e9Schristos set to the number of bytes in the dictionary, and that many bytes are copied 9196db8c6e9Schristos to dictionary. dictionary must have enough space, where 32768 bytes is 9206db8c6e9Schristos always enough. If inflateGetDictionary() is called with dictionary equal to 9216db8c6e9Schristos Z_NULL, then only the dictionary length is returned, and nothing is copied. 9223b1253e8Schristos Similarly, if dictLength is Z_NULL, then it is not set. 9236db8c6e9Schristos 9246db8c6e9Schristos inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the 9256db8c6e9Schristos stream state is inconsistent. 9266db8c6e9Schristos */ 9276db8c6e9Schristos 928*96c32821Schristos ZEXTERN int ZEXPORT inflateSync(z_streamp strm); 929aaf4ece6Schristos /* 9306db8c6e9Schristos Skips invalid compressed data until a possible full flush point (see above 9316db8c6e9Schristos for the description of deflate with Z_FULL_FLUSH) can be found, or until all 932aaf4ece6Schristos available input is skipped. No output is provided. 933aaf4ece6Schristos 9346db8c6e9Schristos inflateSync searches for a 00 00 FF FF pattern in the compressed data. 9356db8c6e9Schristos All full flush points have this pattern, but not all occurrences of this 9366db8c6e9Schristos pattern are full flush points. 9376db8c6e9Schristos 9386db8c6e9Schristos inflateSync returns Z_OK if a possible full flush point has been found, 9396db8c6e9Schristos Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point 9406db8c6e9Schristos has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. 941*96c32821Schristos In the success case, the application may save the current value of total_in 942*96c32821Schristos which indicates where valid compressed data was found. In the error case, 943*96c32821Schristos the application may repeatedly call inflateSync, providing more input each 944*96c32821Schristos time, until success or end of the input data. 945aaf4ece6Schristos */ 946aaf4ece6Schristos 947*96c32821Schristos ZEXTERN int ZEXPORT inflateCopy(z_streamp dest, 948*96c32821Schristos z_streamp source); 949aaf4ece6Schristos /* 950aaf4ece6Schristos Sets the destination stream as a complete copy of the source stream. 951aaf4ece6Schristos 952aaf4ece6Schristos This function can be useful when randomly accessing a large stream. The 953aaf4ece6Schristos first pass through the stream can periodically record the inflate state, 954aaf4ece6Schristos allowing restarting inflate at those points when randomly accessing the 955aaf4ece6Schristos stream. 956aaf4ece6Schristos 957aaf4ece6Schristos inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 958aaf4ece6Schristos enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 9596db8c6e9Schristos (such as zalloc being Z_NULL). msg is left unchanged in both source and 960aaf4ece6Schristos destination. 961aaf4ece6Schristos */ 962aaf4ece6Schristos 963*96c32821Schristos ZEXTERN int ZEXPORT inflateReset(z_streamp strm); 964aaf4ece6Schristos /* 965aaf4ece6Schristos This function is equivalent to inflateEnd followed by inflateInit, 9666db8c6e9Schristos but does not free and reallocate the internal decompression state. The 9676db8c6e9Schristos stream will keep attributes that may have been set by inflateInit2. 968*96c32821Schristos total_in, total_out, adler, and msg are initialized. 969aaf4ece6Schristos 970aaf4ece6Schristos inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 9716db8c6e9Schristos stream state was inconsistent (such as zalloc or state being Z_NULL). 9726db8c6e9Schristos */ 9736db8c6e9Schristos 974*96c32821Schristos ZEXTERN int ZEXPORT inflateReset2(z_streamp strm, 975*96c32821Schristos int windowBits); 9766db8c6e9Schristos /* 9776db8c6e9Schristos This function is the same as inflateReset, but it also permits changing 9786db8c6e9Schristos the wrap and window size requests. The windowBits parameter is interpreted 9796db8c6e9Schristos the same as it is for inflateInit2. If the window size is changed, then the 9806db8c6e9Schristos memory allocated for the window is freed, and the window will be reallocated 9816db8c6e9Schristos by inflate() if needed. 9826db8c6e9Schristos 9836db8c6e9Schristos inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source 9846db8c6e9Schristos stream state was inconsistent (such as zalloc or state being Z_NULL), or if 9856db8c6e9Schristos the windowBits parameter is invalid. 986aaf4ece6Schristos */ 987aaf4ece6Schristos 988*96c32821Schristos ZEXTERN int ZEXPORT inflatePrime(z_streamp strm, 989aaf4ece6Schristos int bits, 990*96c32821Schristos int value); 991aaf4ece6Schristos /* 992aaf4ece6Schristos This function inserts bits in the inflate input stream. The intent is 993aaf4ece6Schristos that this function is used to start inflating at a bit position in the 994aaf4ece6Schristos middle of a byte. The provided bits will be used before any bytes are used 995aaf4ece6Schristos from next_in. This function should only be used with raw inflate, and 996aaf4ece6Schristos should be used before the first inflate() call after inflateInit2() or 997aaf4ece6Schristos inflateReset(). bits must be less than or equal to 16, and that many of the 998aaf4ece6Schristos least significant bits of value will be inserted in the input. 999aaf4ece6Schristos 10006db8c6e9Schristos If bits is negative, then the input stream bit buffer is emptied. Then 10016db8c6e9Schristos inflatePrime() can be called again to put bits in the buffer. This is used 10026db8c6e9Schristos to clear out bits leftover after feeding inflate a block description prior 10036db8c6e9Schristos to feeding inflate codes. 10046db8c6e9Schristos 1005aaf4ece6Schristos inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source 1006aaf4ece6Schristos stream state was inconsistent. 1007aaf4ece6Schristos */ 1008aaf4ece6Schristos 1009*96c32821Schristos ZEXTERN long ZEXPORT inflateMark(z_streamp strm); 10106db8c6e9Schristos /* 10116db8c6e9Schristos This function returns two values, one in the lower 16 bits of the return 10126db8c6e9Schristos value, and the other in the remaining upper bits, obtained by shifting the 10136db8c6e9Schristos return value down 16 bits. If the upper value is -1 and the lower value is 10146db8c6e9Schristos zero, then inflate() is currently decoding information outside of a block. 10156db8c6e9Schristos If the upper value is -1 and the lower value is non-zero, then inflate is in 10166db8c6e9Schristos the middle of a stored block, with the lower value equaling the number of 10176db8c6e9Schristos bytes from the input remaining to copy. If the upper value is not -1, then 10186db8c6e9Schristos it is the number of bits back from the current bit position in the input of 10196db8c6e9Schristos the code (literal or length/distance pair) currently being processed. In 10206db8c6e9Schristos that case the lower value is the number of bytes already emitted for that 10216db8c6e9Schristos code. 10226db8c6e9Schristos 10236db8c6e9Schristos A code is being processed if inflate is waiting for more input to complete 10246db8c6e9Schristos decoding of the code, or if it has completed decoding but is waiting for 10256db8c6e9Schristos more output space to write the literal or match data. 10266db8c6e9Schristos 10276db8c6e9Schristos inflateMark() is used to mark locations in the input data for random 10286db8c6e9Schristos access, which may be at bit positions, and to note those cases where the 10296db8c6e9Schristos output of a code may span boundaries of random access blocks. The current 10306db8c6e9Schristos location in the input stream can be determined from avail_in and data_type 10316db8c6e9Schristos as noted in the description for the Z_BLOCK flush parameter for inflate. 10326db8c6e9Schristos 10336db8c6e9Schristos inflateMark returns the value noted above, or -65536 if the provided 10346db8c6e9Schristos source stream state was inconsistent. 10356db8c6e9Schristos */ 10366db8c6e9Schristos 1037*96c32821Schristos ZEXTERN int ZEXPORT inflateGetHeader(z_streamp strm, 1038*96c32821Schristos gz_headerp head); 1039aaf4ece6Schristos /* 1040aaf4ece6Schristos inflateGetHeader() requests that gzip header information be stored in the 1041aaf4ece6Schristos provided gz_header structure. inflateGetHeader() may be called after 1042aaf4ece6Schristos inflateInit2() or inflateReset(), and before the first call of inflate(). 1043aaf4ece6Schristos As inflate() processes the gzip stream, head->done is zero until the header 1044aaf4ece6Schristos is completed, at which time head->done is set to one. If a zlib stream is 1045aaf4ece6Schristos being decoded, then head->done is set to -1 to indicate that there will be 10466db8c6e9Schristos no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be 10476db8c6e9Schristos used to force inflate() to return immediately after header processing is 10486db8c6e9Schristos complete and before any actual data is decompressed. 1049aaf4ece6Schristos 1050aaf4ece6Schristos The text, time, xflags, and os fields are filled in with the gzip header 1051aaf4ece6Schristos contents. hcrc is set to true if there is a header CRC. (The header CRC 1052aaf4ece6Schristos was valid if done is set to one.) If extra is not Z_NULL, then extra_max 1053aaf4ece6Schristos contains the maximum number of bytes to write to extra. Once done is true, 1054aaf4ece6Schristos extra_len contains the actual extra field length, and extra contains the 1055aaf4ece6Schristos extra field, or that field truncated if extra_max is less than extra_len. 1056aaf4ece6Schristos If name is not Z_NULL, then up to name_max characters are written there, 1057aaf4ece6Schristos terminated with a zero unless the length is greater than name_max. If 1058aaf4ece6Schristos comment is not Z_NULL, then up to comm_max characters are written there, 10596db8c6e9Schristos terminated with a zero unless the length is greater than comm_max. When any 10606db8c6e9Schristos of extra, name, or comment are not Z_NULL and the respective field is not 10616db8c6e9Schristos present in the header, then that field is set to Z_NULL to signal its 1062aaf4ece6Schristos absence. This allows the use of deflateSetHeader() with the returned 1063aaf4ece6Schristos structure to duplicate the header. However if those fields are set to 1064aaf4ece6Schristos allocated memory, then the application will need to save those pointers 1065aaf4ece6Schristos elsewhere so that they can be eventually freed. 1066aaf4ece6Schristos 1067aaf4ece6Schristos If inflateGetHeader is not used, then the header information is simply 1068aaf4ece6Schristos discarded. The header is always checked for validity, including the header 1069aaf4ece6Schristos CRC if present. inflateReset() will reset the process to discard the header 1070aaf4ece6Schristos information. The application would need to call inflateGetHeader() again to 1071aaf4ece6Schristos retrieve the header from the next gzip stream. 1072aaf4ece6Schristos 1073aaf4ece6Schristos inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source 1074aaf4ece6Schristos stream state was inconsistent. 1075aaf4ece6Schristos */ 1076aaf4ece6Schristos 1077aaf4ece6Schristos /* 1078*96c32821Schristos ZEXTERN int ZEXPORT inflateBackInit(z_streamp strm, int windowBits, 1079*96c32821Schristos unsigned char FAR *window); 1080aaf4ece6Schristos 1081aaf4ece6Schristos Initialize the internal stream state for decompression using inflateBack() 1082aaf4ece6Schristos calls. The fields zalloc, zfree and opaque in strm must be initialized 1083aaf4ece6Schristos before the call. If zalloc and zfree are Z_NULL, then the default library- 1084aaf4ece6Schristos derived memory allocation routines are used. windowBits is the base two 1085aaf4ece6Schristos logarithm of the window size, in the range 8..15. window is a caller 1086aaf4ece6Schristos supplied buffer of that size. Except for special applications where it is 1087aaf4ece6Schristos assured that deflate was used with small window sizes, windowBits must be 15 1088aaf4ece6Schristos and a 32K byte window must be supplied to be able to decompress general 1089aaf4ece6Schristos deflate streams. 1090aaf4ece6Schristos 1091aaf4ece6Schristos See inflateBack() for the usage of these routines. 1092aaf4ece6Schristos 1093aaf4ece6Schristos inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of 10946db8c6e9Schristos the parameters are invalid, Z_MEM_ERROR if the internal state could not be 10956db8c6e9Schristos allocated, or Z_VERSION_ERROR if the version of the library does not match 10966db8c6e9Schristos the version of the header file. 1097aaf4ece6Schristos */ 1098aaf4ece6Schristos 1099*96c32821Schristos typedef unsigned (*in_func)(void FAR *, 1100*96c32821Schristos z_const unsigned char FAR * FAR *); 1101*96c32821Schristos typedef int (*out_func)(void FAR *, unsigned char FAR *, unsigned); 1102aaf4ece6Schristos 1103*96c32821Schristos ZEXTERN int ZEXPORT inflateBack(z_streamp strm, 1104aaf4ece6Schristos in_func in, void FAR *in_desc, 1105*96c32821Schristos out_func out, void FAR *out_desc); 1106aaf4ece6Schristos /* 1107aaf4ece6Schristos inflateBack() does a raw inflate with a single call using a call-back 11086db8c6e9Schristos interface for input and output. This is potentially more efficient than 11096db8c6e9Schristos inflate() for file i/o applications, in that it avoids copying between the 11106db8c6e9Schristos output and the sliding window by simply making the window itself the output 11116db8c6e9Schristos buffer. inflate() can be faster on modern CPUs when used with large 11126db8c6e9Schristos buffers. inflateBack() trusts the application to not change the output 11136db8c6e9Schristos buffer passed by the output function, at least until inflateBack() returns. 1114aaf4ece6Schristos 1115aaf4ece6Schristos inflateBackInit() must be called first to allocate the internal state 1116aaf4ece6Schristos and to initialize the state with the user-provided window buffer. 1117aaf4ece6Schristos inflateBack() may then be used multiple times to inflate a complete, raw 11186db8c6e9Schristos deflate stream with each call. inflateBackEnd() is then called to free the 11196db8c6e9Schristos allocated state. 1120aaf4ece6Schristos 1121aaf4ece6Schristos A raw deflate stream is one with no zlib or gzip header or trailer. 1122aaf4ece6Schristos This routine would normally be used in a utility that reads zip or gzip 1123aaf4ece6Schristos files and writes out uncompressed files. The utility would decode the 11246db8c6e9Schristos header and process the trailer on its own, hence this routine expects only 11256db8c6e9Schristos the raw deflate stream to decompress. This is different from the default 11266db8c6e9Schristos behavior of inflate(), which expects a zlib header and trailer around the 11276db8c6e9Schristos deflate stream. 1128aaf4ece6Schristos 1129aaf4ece6Schristos inflateBack() uses two subroutines supplied by the caller that are then 1130aaf4ece6Schristos called by inflateBack() for input and output. inflateBack() calls those 1131aaf4ece6Schristos routines until it reads a complete deflate stream and writes out all of the 1132aaf4ece6Schristos uncompressed data, or until it encounters an error. The function's 1133aaf4ece6Schristos parameters and return types are defined above in the in_func and out_func 1134aaf4ece6Schristos typedefs. inflateBack() will call in(in_desc, &buf) which should return the 1135aaf4ece6Schristos number of bytes of provided input, and a pointer to that input in buf. If 1136aaf4ece6Schristos there is no input available, in() must return zero -- buf is ignored in that 11376db8c6e9Schristos case -- and inflateBack() will return a buffer error. inflateBack() will 11386db8c6e9Schristos call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. 11396db8c6e9Schristos out() should return zero on success, or non-zero on failure. If out() 11406db8c6e9Schristos returns non-zero, inflateBack() will return with an error. Neither in() nor 11416db8c6e9Schristos out() are permitted to change the contents of the window provided to 1142aaf4ece6Schristos inflateBackInit(), which is also the buffer that out() uses to write from. 1143aaf4ece6Schristos The length written by out() will be at most the window size. Any non-zero 1144aaf4ece6Schristos amount of input may be provided by in(). 1145aaf4ece6Schristos 1146aaf4ece6Schristos For convenience, inflateBack() can be provided input on the first call by 1147aaf4ece6Schristos setting strm->next_in and strm->avail_in. If that input is exhausted, then 1148aaf4ece6Schristos in() will be called. Therefore strm->next_in must be initialized before 1149aaf4ece6Schristos calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called 1150aaf4ece6Schristos immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in 1151aaf4ece6Schristos must also be initialized, and then if strm->avail_in is not zero, input will 1152aaf4ece6Schristos initially be taken from strm->next_in[0 .. strm->avail_in - 1]. 1153aaf4ece6Schristos 1154aaf4ece6Schristos The in_desc and out_desc parameters of inflateBack() is passed as the 1155aaf4ece6Schristos first parameter of in() and out() respectively when they are called. These 1156aaf4ece6Schristos descriptors can be optionally used to pass any information that the caller- 1157aaf4ece6Schristos supplied in() and out() functions need to do their job. 1158aaf4ece6Schristos 1159aaf4ece6Schristos On return, inflateBack() will set strm->next_in and strm->avail_in to 1160aaf4ece6Schristos pass back any unused input that was provided by the last in() call. The 1161aaf4ece6Schristos return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR 11626db8c6e9Schristos if in() or out() returned an error, Z_DATA_ERROR if there was a format error 11636db8c6e9Schristos in the deflate stream (in which case strm->msg is set to indicate the nature 11646db8c6e9Schristos of the error), or Z_STREAM_ERROR if the stream was not properly initialized. 11656db8c6e9Schristos In the case of Z_BUF_ERROR, an input or output error can be distinguished 11666db8c6e9Schristos using strm->next_in which will be Z_NULL only if in() returned an error. If 11676db8c6e9Schristos strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning 11686db8c6e9Schristos non-zero. (in() will always be called before out(), so strm->next_in is 11696db8c6e9Schristos assured to be defined if out() returns non-zero.) Note that inflateBack() 11706db8c6e9Schristos cannot return Z_OK. 1171aaf4ece6Schristos */ 1172aaf4ece6Schristos 1173*96c32821Schristos ZEXTERN int ZEXPORT inflateBackEnd(z_streamp strm); 1174aaf4ece6Schristos /* 1175aaf4ece6Schristos All memory allocated by inflateBackInit() is freed. 1176aaf4ece6Schristos 1177aaf4ece6Schristos inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream 1178aaf4ece6Schristos state was inconsistent. 1179aaf4ece6Schristos */ 1180aaf4ece6Schristos 1181*96c32821Schristos ZEXTERN uLong ZEXPORT zlibCompileFlags(void); 1182aaf4ece6Schristos /* Return flags indicating compile-time options. 1183aaf4ece6Schristos 1184aaf4ece6Schristos Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: 1185aaf4ece6Schristos 1.0: size of uInt 1186aaf4ece6Schristos 3.2: size of uLong 1187aaf4ece6Schristos 5.4: size of voidpf (pointer) 1188aaf4ece6Schristos 7.6: size of z_off_t 1189aaf4ece6Schristos 1190aaf4ece6Schristos Compiler, assembler, and debug options: 1191a1f9f4c0Schristos 8: ZLIB_DEBUG 1192aaf4ece6Schristos 9: ASMV or ASMINF -- use ASM code 1193aaf4ece6Schristos 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention 1194aaf4ece6Schristos 11: 0 (reserved) 1195aaf4ece6Schristos 1196aaf4ece6Schristos One-time table building (smaller code, but not thread-safe if true): 1197aaf4ece6Schristos 12: BUILDFIXED -- build static block decoding tables when needed 1198aaf4ece6Schristos 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed 1199aaf4ece6Schristos 14,15: 0 (reserved) 1200aaf4ece6Schristos 1201aaf4ece6Schristos Library content (indicates missing functionality): 1202aaf4ece6Schristos 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking 1203aaf4ece6Schristos deflate code when not needed) 1204aaf4ece6Schristos 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect 1205aaf4ece6Schristos and decode gzip streams (to avoid linking crc code) 1206aaf4ece6Schristos 18-19: 0 (reserved) 1207aaf4ece6Schristos 1208aaf4ece6Schristos Operation variations (changes in library functionality): 1209aaf4ece6Schristos 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate 1210aaf4ece6Schristos 21: FASTEST -- deflate algorithm with only one, lowest compression level 1211aaf4ece6Schristos 22,23: 0 (reserved) 1212aaf4ece6Schristos 1213aaf4ece6Schristos The sprintf variant used by gzprintf (zero is best): 1214aaf4ece6Schristos 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format 1215aaf4ece6Schristos 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! 1216aaf4ece6Schristos 26: 0 = returns value, 1 = void -- 1 means inferred string length returned 1217aaf4ece6Schristos 1218aaf4ece6Schristos Remainder: 1219aaf4ece6Schristos 27-31: 0 (reserved) 1220aaf4ece6Schristos */ 1221aaf4ece6Schristos 12226db8c6e9Schristos #ifndef Z_SOLO 1223aaf4ece6Schristos 1224aaf4ece6Schristos /* utility functions */ 1225aaf4ece6Schristos 1226aaf4ece6Schristos /* 12276db8c6e9Schristos The following utility functions are implemented on top of the basic 12286db8c6e9Schristos stream-oriented functions. To simplify the interface, some default options 12296db8c6e9Schristos are assumed (compression level and memory usage, standard memory allocation 12306db8c6e9Schristos functions). The source code of these utility functions can be modified if 12316db8c6e9Schristos you need special options. 1232aaf4ece6Schristos */ 1233aaf4ece6Schristos 1234*96c32821Schristos ZEXTERN int ZEXPORT compress(Bytef *dest, uLongf *destLen, 1235*96c32821Schristos const Bytef *source, uLong sourceLen); 1236aaf4ece6Schristos /* 1237aaf4ece6Schristos Compresses the source buffer into the destination buffer. sourceLen is 12386db8c6e9Schristos the byte length of the source buffer. Upon entry, destLen is the total size 12396db8c6e9Schristos of the destination buffer, which must be at least the value returned by 12406db8c6e9Schristos compressBound(sourceLen). Upon exit, destLen is the actual size of the 12416db8c6e9Schristos compressed data. compress() is equivalent to compress2() with a level 12426db8c6e9Schristos parameter of Z_DEFAULT_COMPRESSION. 12436db8c6e9Schristos 1244aaf4ece6Schristos compress returns Z_OK if success, Z_MEM_ERROR if there was not 1245aaf4ece6Schristos enough memory, Z_BUF_ERROR if there was not enough room in the output 1246aaf4ece6Schristos buffer. 1247aaf4ece6Schristos */ 1248aaf4ece6Schristos 1249*96c32821Schristos ZEXTERN int ZEXPORT compress2(Bytef *dest, uLongf *destLen, 1250aaf4ece6Schristos const Bytef *source, uLong sourceLen, 1251*96c32821Schristos int level); 1252aaf4ece6Schristos /* 1253aaf4ece6Schristos Compresses the source buffer into the destination buffer. The level 1254aaf4ece6Schristos parameter has the same meaning as in deflateInit. sourceLen is the byte 1255aaf4ece6Schristos length of the source buffer. Upon entry, destLen is the total size of the 1256aaf4ece6Schristos destination buffer, which must be at least the value returned by 1257aaf4ece6Schristos compressBound(sourceLen). Upon exit, destLen is the actual size of the 12586db8c6e9Schristos compressed data. 1259aaf4ece6Schristos 1260aaf4ece6Schristos compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 1261aaf4ece6Schristos memory, Z_BUF_ERROR if there was not enough room in the output buffer, 1262aaf4ece6Schristos Z_STREAM_ERROR if the level parameter is invalid. 1263aaf4ece6Schristos */ 1264aaf4ece6Schristos 1265*96c32821Schristos ZEXTERN uLong ZEXPORT compressBound(uLong sourceLen); 1266aaf4ece6Schristos /* 1267aaf4ece6Schristos compressBound() returns an upper bound on the compressed size after 12686db8c6e9Schristos compress() or compress2() on sourceLen bytes. It would be used before a 12696db8c6e9Schristos compress() or compress2() call to allocate the destination buffer. 1270aaf4ece6Schristos */ 1271aaf4ece6Schristos 1272*96c32821Schristos ZEXTERN int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, 1273*96c32821Schristos const Bytef *source, uLong sourceLen); 1274aaf4ece6Schristos /* 1275aaf4ece6Schristos Decompresses the source buffer into the destination buffer. sourceLen is 12766db8c6e9Schristos the byte length of the source buffer. Upon entry, destLen is the total size 12776db8c6e9Schristos of the destination buffer, which must be large enough to hold the entire 12786db8c6e9Schristos uncompressed data. (The size of the uncompressed data must have been saved 12796db8c6e9Schristos previously by the compressor and transmitted to the decompressor by some 12806db8c6e9Schristos mechanism outside the scope of this compression library.) Upon exit, destLen 12816db8c6e9Schristos is the actual size of the uncompressed data. 1282aaf4ece6Schristos 1283aaf4ece6Schristos uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 1284aaf4ece6Schristos enough memory, Z_BUF_ERROR if there was not enough room in the output 12856db8c6e9Schristos buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In 12866db8c6e9Schristos the case where there is not enough room, uncompress() will fill the output 12876db8c6e9Schristos buffer with the uncompressed data up to that point. 1288aaf4ece6Schristos */ 1289aaf4ece6Schristos 1290*96c32821Schristos ZEXTERN int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, 1291*96c32821Schristos const Bytef *source, uLong *sourceLen); 1292aaf4ece6Schristos /* 12936db8c6e9Schristos Same as uncompress, except that sourceLen is a pointer, where the 12946db8c6e9Schristos length of the source is *sourceLen. On return, *sourceLen is the number of 12956db8c6e9Schristos source bytes consumed. 12966db8c6e9Schristos */ 12976db8c6e9Schristos 12986db8c6e9Schristos /* gzip file access functions */ 12996db8c6e9Schristos 13006db8c6e9Schristos /* 13016db8c6e9Schristos This library supports reading and writing files in gzip (.gz) format with 13026db8c6e9Schristos an interface similar to that of stdio, using the functions that start with 13036db8c6e9Schristos "gz". The gzip format is different from the zlib format. gzip is a gzip 13046db8c6e9Schristos wrapper, documented in RFC 1952, wrapped around a deflate stream. 13056db8c6e9Schristos */ 13066db8c6e9Schristos 13076db8c6e9Schristos typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ 13086db8c6e9Schristos 13096db8c6e9Schristos /* 1310*96c32821Schristos ZEXTERN gzFile ZEXPORT gzopen(const char *path, const char *mode); 13116db8c6e9Schristos 13123b1253e8Schristos Open the gzip (.gz) file at path for reading and decompressing, or 13133b1253e8Schristos compressing and writing. The mode parameter is as in fopen ("rb" or "wb") 13143b1253e8Schristos but can also include a compression level ("wb9") or a strategy: 'f' for 13153b1253e8Schristos filtered data as in "wb6f", 'h' for Huffman-only compression as in "wb1h", 13163b1253e8Schristos 'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression 13173b1253e8Schristos as in "wb9F". (See the description of deflateInit2 for more information 13183b1253e8Schristos about the strategy parameter.) 'T' will request transparent writing or 13193b1253e8Schristos appending with no compression and not using the gzip format. 13206db8c6e9Schristos 13216db8c6e9Schristos "a" can be used instead of "w" to request that the gzip stream that will 13226db8c6e9Schristos be written be appended to the file. "+" will result in an error, since 13236db8c6e9Schristos reading and writing to the same gzip file is not supported. The addition of 13246db8c6e9Schristos "x" when writing will create the file exclusively, which fails if the file 13256db8c6e9Schristos already exists. On systems that support it, the addition of "e" when 13266db8c6e9Schristos reading or writing will set the flag to close the file on an execve() call. 13276db8c6e9Schristos 13286db8c6e9Schristos These functions, as well as gzip, will read and decode a sequence of gzip 13296db8c6e9Schristos streams in a file. The append function of gzopen() can be used to create 13306db8c6e9Schristos such a file. (Also see gzflush() for another way to do this.) When 13316db8c6e9Schristos appending, gzopen does not test whether the file begins with a gzip stream, 13326db8c6e9Schristos nor does it look for the end of the gzip streams to begin appending. gzopen 13336db8c6e9Schristos will simply append a gzip stream to the existing file. 1334aaf4ece6Schristos 1335aaf4ece6Schristos gzopen can be used to read a file which is not in gzip format; in this 13366db8c6e9Schristos case gzread will directly read from the file without decompression. When 13376db8c6e9Schristos reading, this will be detected automatically by looking for the magic two- 13386db8c6e9Schristos byte gzip header. 1339aaf4ece6Schristos 13406db8c6e9Schristos gzopen returns NULL if the file could not be opened, if there was 13416db8c6e9Schristos insufficient memory to allocate the gzFile state, or if an invalid mode was 13426db8c6e9Schristos specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). 13436db8c6e9Schristos errno can be checked to determine if the reason gzopen failed was that the 13446db8c6e9Schristos file could not be opened. 13456db8c6e9Schristos */ 1346aaf4ece6Schristos 1347*96c32821Schristos ZEXTERN gzFile ZEXPORT gzdopen(int fd, const char *mode); 1348aaf4ece6Schristos /* 13493b1253e8Schristos Associate a gzFile with the file descriptor fd. File descriptors are 13503b1253e8Schristos obtained from calls like open, dup, creat, pipe or fileno (if the file has 13513b1253e8Schristos been previously opened with fopen). The mode parameter is as in gzopen. 13526db8c6e9Schristos 13536db8c6e9Schristos The next call of gzclose on the returned gzFile will also close the file 13546db8c6e9Schristos descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor 13556db8c6e9Schristos fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, 13566db8c6e9Schristos mode);. The duplicated descriptor should be saved to avoid a leak, since 13576db8c6e9Schristos gzdopen does not close fd if it fails. If you are using fileno() to get the 13586db8c6e9Schristos file descriptor from a FILE *, then you will have to use dup() to avoid 13596db8c6e9Schristos double-close()ing the file descriptor. Both gzclose() and fclose() will 13606db8c6e9Schristos close the associated file descriptor, so they need to have different file 13616db8c6e9Schristos descriptors. 13626db8c6e9Schristos 13636db8c6e9Schristos gzdopen returns NULL if there was insufficient memory to allocate the 13646db8c6e9Schristos gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not 13656db8c6e9Schristos provided, or '+' was provided), or if fd is -1. The file descriptor is not 13666db8c6e9Schristos used until the next gz* read, write, seek, or close operation, so gzdopen 13676db8c6e9Schristos will not detect if fd is invalid (unless fd is -1). 13686db8c6e9Schristos */ 13696db8c6e9Schristos 1370*96c32821Schristos ZEXTERN int ZEXPORT gzbuffer(gzFile file, unsigned size); 13716db8c6e9Schristos /* 13723b1253e8Schristos Set the internal buffer size used by this library's functions for file to 13733b1253e8Schristos size. The default buffer size is 8192 bytes. This function must be called 13743b1253e8Schristos after gzopen() or gzdopen(), and before any other calls that read or write 13753b1253e8Schristos the file. The buffer memory allocation is always deferred to the first read 13763b1253e8Schristos or write. Three times that size in buffer space is allocated. A larger 13773b1253e8Schristos buffer size of, for example, 64K or 128K bytes will noticeably increase the 13783b1253e8Schristos speed of decompression (reading). 13796db8c6e9Schristos 13806db8c6e9Schristos The new buffer size also affects the maximum length for gzprintf(). 13816db8c6e9Schristos 13826db8c6e9Schristos gzbuffer() returns 0 on success, or -1 on failure, such as being called 13836db8c6e9Schristos too late. 1384aaf4ece6Schristos */ 1385aaf4ece6Schristos 1386*96c32821Schristos ZEXTERN int ZEXPORT gzsetparams(gzFile file, int level, int strategy); 1387aaf4ece6Schristos /* 13883b1253e8Schristos Dynamically update the compression level and strategy for file. See the 13893b1253e8Schristos description of deflateInit2 for the meaning of these parameters. Previously 13903b1253e8Schristos provided data is flushed before applying the parameter changes. 13916db8c6e9Schristos 13926db8c6e9Schristos gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not 13936db8c6e9Schristos opened for writing, Z_ERRNO if there is an error writing the flushed data, 13946db8c6e9Schristos or Z_MEM_ERROR if there is a memory allocation error. 1395aaf4ece6Schristos */ 1396aaf4ece6Schristos 1397*96c32821Schristos ZEXTERN int ZEXPORT gzread(gzFile file, voidp buf, unsigned len); 1398aaf4ece6Schristos /* 13993b1253e8Schristos Read and decompress up to len uncompressed bytes from file into buf. If 14006db8c6e9Schristos the input file is not in gzip format, gzread copies the given number of 14016db8c6e9Schristos bytes into the buffer directly from the file. 14026db8c6e9Schristos 14036db8c6e9Schristos After reaching the end of a gzip stream in the input, gzread will continue 14046db8c6e9Schristos to read, looking for another gzip stream. Any number of gzip streams may be 14056db8c6e9Schristos concatenated in the input file, and will all be decompressed by gzread(). 14066db8c6e9Schristos If something other than a gzip stream is encountered after a gzip stream, 14076db8c6e9Schristos that remaining trailing garbage is ignored (and no error is returned). 14086db8c6e9Schristos 14096db8c6e9Schristos gzread can be used to read a gzip file that is being concurrently written. 14106db8c6e9Schristos Upon reaching the end of the input, gzread will return with the available 14116db8c6e9Schristos data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then 14126db8c6e9Schristos gzclearerr can be used to clear the end of file indicator in order to permit 14136db8c6e9Schristos gzread to be tried again. Z_OK indicates that a gzip stream was completed 14146db8c6e9Schristos on the last gzread. Z_BUF_ERROR indicates that the input file ended in the 14156db8c6e9Schristos middle of a gzip stream. Note that gzread does not return -1 in the event 14166db8c6e9Schristos of an incomplete gzip stream. This error is deferred until gzclose(), which 14176db8c6e9Schristos will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip 14186db8c6e9Schristos stream. Alternatively, gzerror can be used before gzclose to detect this 14196db8c6e9Schristos case. 14206db8c6e9Schristos 14216db8c6e9Schristos gzread returns the number of uncompressed bytes actually read, less than 14226db8c6e9Schristos len for end of file, or -1 for error. If len is too large to fit in an int, 14236db8c6e9Schristos then nothing is read, -1 is returned, and the error state is set to 14246db8c6e9Schristos Z_STREAM_ERROR. 14256db8c6e9Schristos */ 14266db8c6e9Schristos 1427*96c32821Schristos ZEXTERN z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems, 1428*96c32821Schristos gzFile file); 14296db8c6e9Schristos /* 14303b1253e8Schristos Read and decompress up to nitems items of size size from file into buf, 14313b1253e8Schristos otherwise operating as gzread() does. This duplicates the interface of 14323b1253e8Schristos stdio's fread(), with size_t request and return types. If the library 14333b1253e8Schristos defines size_t, then z_size_t is identical to size_t. If not, then z_size_t 14343b1253e8Schristos is an unsigned integer type that can contain a pointer. 14356db8c6e9Schristos 14366db8c6e9Schristos gzfread() returns the number of full items read of size size, or zero if 14376db8c6e9Schristos the end of the file was reached and a full item could not be read, or if 14386db8c6e9Schristos there was an error. gzerror() must be consulted if zero is returned in 14396db8c6e9Schristos order to determine if there was an error. If the multiplication of size and 14406db8c6e9Schristos nitems overflows, i.e. the product does not fit in a z_size_t, then nothing 14416db8c6e9Schristos is read, zero is returned, and the error state is set to Z_STREAM_ERROR. 14426db8c6e9Schristos 14436db8c6e9Schristos In the event that the end of file is reached and only a partial item is 14446db8c6e9Schristos available at the end, i.e. the remaining uncompressed data length is not a 14457991f5a7Sandvar multiple of size, then the final partial item is nevertheless read into buf 14466db8c6e9Schristos and the end-of-file flag is set. The length of the partial item read is not 14476db8c6e9Schristos provided, but could be inferred from the result of gztell(). This behavior 14486db8c6e9Schristos is the same as the behavior of fread() implementations in common libraries, 14496db8c6e9Schristos but it prevents the direct use of gzfread() to read a concurrently written 14507991f5a7Sandvar file, resetting and retrying on end-of-file, when size is not 1. 14516db8c6e9Schristos */ 1452aaf4ece6Schristos 1453*96c32821Schristos ZEXTERN int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len); 1454aaf4ece6Schristos /* 14553b1253e8Schristos Compress and write the len uncompressed bytes at buf to file. gzwrite 14563b1253e8Schristos returns the number of uncompressed bytes written or 0 in case of error. 1457aaf4ece6Schristos */ 1458aaf4ece6Schristos 1459*96c32821Schristos ZEXTERN z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, 1460*96c32821Schristos z_size_t nitems, gzFile file); 1461aaf4ece6Schristos /* 14623b1253e8Schristos Compress and write nitems items of size size from buf to file, duplicating 14636db8c6e9Schristos the interface of stdio's fwrite(), with size_t request and return types. If 14646db8c6e9Schristos the library defines size_t, then z_size_t is identical to size_t. If not, 14656db8c6e9Schristos then z_size_t is an unsigned integer type that can contain a pointer. 14666db8c6e9Schristos 14676db8c6e9Schristos gzfwrite() returns the number of full items written of size size, or zero 14686db8c6e9Schristos if there was an error. If the multiplication of size and nitems overflows, 14696db8c6e9Schristos i.e. the product does not fit in a z_size_t, then nothing is written, zero 14706db8c6e9Schristos is returned, and the error state is set to Z_STREAM_ERROR. 14716db8c6e9Schristos */ 14726db8c6e9Schristos 1473*96c32821Schristos ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...); 14746db8c6e9Schristos /* 14753b1253e8Schristos Convert, format, compress, and write the arguments (...) to file under 14763b1253e8Schristos control of the string format, as in fprintf. gzprintf returns the number of 14776db8c6e9Schristos uncompressed bytes actually written, or a negative zlib error code in case 14786db8c6e9Schristos of error. The number of uncompressed bytes written is limited to 8191, or 14796db8c6e9Schristos one less than the buffer size given to gzbuffer(). The caller should assure 14806db8c6e9Schristos that this limit is not exceeded. If it is exceeded, then gzprintf() will 1481aaf4ece6Schristos return an error (0) with nothing written. In this case, there may also be a 1482aaf4ece6Schristos buffer overflow with unpredictable consequences, which is possible only if 14833b1253e8Schristos zlib was compiled with the insecure functions sprintf() or vsprintf(), 1484aaf4ece6Schristos because the secure snprintf() or vsnprintf() functions were not available. 14856db8c6e9Schristos This can be determined using zlibCompileFlags(). 1486aaf4ece6Schristos */ 1487aaf4ece6Schristos 1488*96c32821Schristos ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); 1489aaf4ece6Schristos /* 14903b1253e8Schristos Compress and write the given null-terminated string s to file, excluding 1491aaf4ece6Schristos the terminating null character. 14926db8c6e9Schristos 1493aaf4ece6Schristos gzputs returns the number of characters written, or -1 in case of error. 1494aaf4ece6Schristos */ 1495aaf4ece6Schristos 1496*96c32821Schristos ZEXTERN char * ZEXPORT gzgets(gzFile file, char *buf, int len); 1497aaf4ece6Schristos /* 14983b1253e8Schristos Read and decompress bytes from file into buf, until len-1 characters are 14993b1253e8Schristos read, or until a newline character is read and transferred to buf, or an 15003b1253e8Schristos end-of-file condition is encountered. If any characters are read or if len 15013b1253e8Schristos is one, the string is terminated with a null character. If no characters 15023b1253e8Schristos are read due to an end-of-file or len is less than one, then the buffer is 15033b1253e8Schristos left untouched. 15046db8c6e9Schristos 15056db8c6e9Schristos gzgets returns buf which is a null-terminated string, or it returns NULL 15066db8c6e9Schristos for end-of-file or in case of error. If there was an error, the contents at 15076db8c6e9Schristos buf are indeterminate. 1508aaf4ece6Schristos */ 1509aaf4ece6Schristos 1510*96c32821Schristos ZEXTERN int ZEXPORT gzputc(gzFile file, int c); 1511aaf4ece6Schristos /* 15123b1253e8Schristos Compress and write c, converted to an unsigned char, into file. gzputc 15136db8c6e9Schristos returns the value that was written, or -1 in case of error. 1514aaf4ece6Schristos */ 1515aaf4ece6Schristos 1516*96c32821Schristos ZEXTERN int ZEXPORT gzgetc(gzFile file); 1517aaf4ece6Schristos /* 15183b1253e8Schristos Read and decompress one byte from file. gzgetc returns this byte or -1 15196db8c6e9Schristos in case of end of file or error. This is implemented as a macro for speed. 15206db8c6e9Schristos As such, it does not do all of the checking the other functions do. I.e. 15216db8c6e9Schristos it does not check to see if file is NULL, nor whether the structure file 15226db8c6e9Schristos points to has been clobbered or not. 1523aaf4ece6Schristos */ 1524aaf4ece6Schristos 1525*96c32821Schristos ZEXTERN int ZEXPORT gzungetc(int c, gzFile file); 1526aaf4ece6Schristos /* 15273b1253e8Schristos Push c back onto the stream for file to be read as the first character on 15283b1253e8Schristos the next read. At least one character of push-back is always allowed. 15296db8c6e9Schristos gzungetc() returns the character pushed, or -1 on failure. gzungetc() will 15306db8c6e9Schristos fail if c is -1, and may fail if a character has been pushed but not read 15316db8c6e9Schristos yet. If gzungetc is used immediately after gzopen or gzdopen, at least the 15326db8c6e9Schristos output buffer size of pushed characters is allowed. (See gzbuffer above.) 15336db8c6e9Schristos The pushed character will be discarded if the stream is repositioned with 15346db8c6e9Schristos gzseek() or gzrewind(). 1535aaf4ece6Schristos */ 1536aaf4ece6Schristos 1537*96c32821Schristos ZEXTERN int ZEXPORT gzflush(gzFile file, int flush); 1538aaf4ece6Schristos /* 15393b1253e8Schristos Flush all pending output to file. The parameter flush is as in the 15403b1253e8Schristos deflate() function. The return value is the zlib error number (see function 15413b1253e8Schristos gzerror below). gzflush is only permitted when writing. 15426db8c6e9Schristos 15436db8c6e9Schristos If the flush parameter is Z_FINISH, the remaining data is written and the 15446db8c6e9Schristos gzip stream is completed in the output. If gzwrite() is called again, a new 15456db8c6e9Schristos gzip stream will be started in the output. gzread() is able to read such 15466db8c6e9Schristos concatenated gzip streams. 15476db8c6e9Schristos 15486db8c6e9Schristos gzflush should be called only when strictly necessary because it will 15496db8c6e9Schristos degrade compression if called too often. 1550aaf4ece6Schristos */ 1551aaf4ece6Schristos 15526db8c6e9Schristos /* 1553*96c32821Schristos ZEXTERN z_off_t ZEXPORT gzseek(gzFile file, 1554*96c32821Schristos z_off_t offset, int whence); 15556db8c6e9Schristos 15563b1253e8Schristos Set the starting position to offset relative to whence for the next gzread 15573b1253e8Schristos or gzwrite on file. The offset represents a number of bytes in the 1558aaf4ece6Schristos uncompressed data stream. The whence parameter is defined as in lseek(2); 1559aaf4ece6Schristos the value SEEK_END is not supported. 15606db8c6e9Schristos 1561aaf4ece6Schristos If the file is opened for reading, this function is emulated but can be 1562aaf4ece6Schristos extremely slow. If the file is opened for writing, only forward seeks are 1563aaf4ece6Schristos supported; gzseek then compresses a sequence of zeroes up to the new 1564aaf4ece6Schristos starting position. 1565aaf4ece6Schristos 1566aaf4ece6Schristos gzseek returns the resulting offset location as measured in bytes from 1567aaf4ece6Schristos the beginning of the uncompressed stream, or -1 in case of error, in 1568aaf4ece6Schristos particular if the file is opened for writing and the new starting position 1569aaf4ece6Schristos would be before the current position. 1570aaf4ece6Schristos */ 1571aaf4ece6Schristos 1572*96c32821Schristos ZEXTERN int ZEXPORT gzrewind(gzFile file); 1573aaf4ece6Schristos /* 15743b1253e8Schristos Rewind file. This function is supported only for reading. 1575aaf4ece6Schristos 15763b1253e8Schristos gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET). 1577aaf4ece6Schristos */ 1578aaf4ece6Schristos 1579aaf4ece6Schristos /* 1580*96c32821Schristos ZEXTERN z_off_t ZEXPORT gztell(gzFile file); 15816db8c6e9Schristos 15823b1253e8Schristos Return the starting position for the next gzread or gzwrite on file. 15833b1253e8Schristos This position represents a number of bytes in the uncompressed data stream, 15843b1253e8Schristos and is zero when starting, even if appending or reading a gzip stream from 15853b1253e8Schristos the middle of a file using gzdopen(). 1586aaf4ece6Schristos 1587aaf4ece6Schristos gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) 1588aaf4ece6Schristos */ 1589aaf4ece6Schristos 15906db8c6e9Schristos /* 1591*96c32821Schristos ZEXTERN z_off_t ZEXPORT gzoffset(gzFile file); 15926db8c6e9Schristos 15933b1253e8Schristos Return the current compressed (actual) read or write offset of file. This 15943b1253e8Schristos offset includes the count of bytes that precede the gzip stream, for example 15953b1253e8Schristos when appending or when using gzdopen() for reading. When reading, the 15963b1253e8Schristos offset does not include as yet unused buffered input. This information can 15973b1253e8Schristos be used for a progress indicator. On error, gzoffset() returns -1. 15986db8c6e9Schristos */ 15996db8c6e9Schristos 1600*96c32821Schristos ZEXTERN int ZEXPORT gzeof(gzFile file); 1601aaf4ece6Schristos /* 16023b1253e8Schristos Return true (1) if the end-of-file indicator for file has been set while 16033b1253e8Schristos reading, false (0) otherwise. Note that the end-of-file indicator is set 16043b1253e8Schristos only if the read tried to go past the end of the input, but came up short. 16053b1253e8Schristos Therefore, just like feof(), gzeof() may return false even if there is no 16063b1253e8Schristos more data to read, in the event that the last read request was for the exact 16073b1253e8Schristos number of bytes remaining in the input file. This will happen if the input 16083b1253e8Schristos file size is an exact multiple of the buffer size. 16096db8c6e9Schristos 16106db8c6e9Schristos If gzeof() returns true, then the read functions will return no more data, 16116db8c6e9Schristos unless the end-of-file indicator is reset by gzclearerr() and the input file 16126db8c6e9Schristos has grown since the previous end of file was detected. 1613aaf4ece6Schristos */ 1614aaf4ece6Schristos 1615*96c32821Schristos ZEXTERN int ZEXPORT gzdirect(gzFile file); 1616aaf4ece6Schristos /* 16173b1253e8Schristos Return true (1) if file is being copied directly while reading, or false 16186db8c6e9Schristos (0) if file is a gzip stream being decompressed. 16196db8c6e9Schristos 16206db8c6e9Schristos If the input file is empty, gzdirect() will return true, since the input 16216db8c6e9Schristos does not contain a gzip stream. 16226db8c6e9Schristos 16236db8c6e9Schristos If gzdirect() is used immediately after gzopen() or gzdopen() it will 16246db8c6e9Schristos cause buffers to be allocated to allow reading the file to determine if it 16256db8c6e9Schristos is a gzip file. Therefore if gzbuffer() is used, it should be called before 16266db8c6e9Schristos gzdirect(). 16276db8c6e9Schristos 16286db8c6e9Schristos When writing, gzdirect() returns true (1) if transparent writing was 16296db8c6e9Schristos requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: 16306db8c6e9Schristos gzdirect() is not needed when writing. Transparent writing must be 16316db8c6e9Schristos explicitly requested, so the application already knows the answer. When 16326db8c6e9Schristos linking statically, using gzdirect() will include all of the zlib code for 16336db8c6e9Schristos gzip file reading and decompression, which may not be desired.) 1634aaf4ece6Schristos */ 1635aaf4ece6Schristos 1636*96c32821Schristos ZEXTERN int ZEXPORT gzclose(gzFile file); 1637aaf4ece6Schristos /* 16383b1253e8Schristos Flush all pending output for file, if necessary, close file and 16393b1253e8Schristos deallocate the (de)compression state. Note that once file is closed, you 16406db8c6e9Schristos cannot call gzerror with file, since its structures have been deallocated. 16416db8c6e9Schristos gzclose must not be called more than once on the same file, just as free 16426db8c6e9Schristos must not be called more than once on the same allocation. 16436db8c6e9Schristos 16446db8c6e9Schristos gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a 16456db8c6e9Schristos file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the 16466db8c6e9Schristos last read ended in the middle of a gzip stream, or Z_OK on success. 16476db8c6e9Schristos */ 16486db8c6e9Schristos 1649*96c32821Schristos ZEXTERN int ZEXPORT gzclose_r(gzFile file); 1650*96c32821Schristos ZEXTERN int ZEXPORT gzclose_w(gzFile file); 16516db8c6e9Schristos /* 16526db8c6e9Schristos Same as gzclose(), but gzclose_r() is only for use when reading, and 16536db8c6e9Schristos gzclose_w() is only for use when writing or appending. The advantage to 16546db8c6e9Schristos using these instead of gzclose() is that they avoid linking in zlib 16556db8c6e9Schristos compression or decompression code that is not used when only reading or only 16566db8c6e9Schristos writing respectively. If gzclose() is used, then both compression and 16576db8c6e9Schristos decompression code will be included the application when linking to a static 16586db8c6e9Schristos zlib library. 1659aaf4ece6Schristos */ 1660aaf4ece6Schristos 1661*96c32821Schristos ZEXTERN const char * ZEXPORT gzerror(gzFile file, int *errnum); 1662aaf4ece6Schristos /* 16633b1253e8Schristos Return the error message for the last error which occurred on file. 16643b1253e8Schristos errnum is set to zlib error number. If an error occurred in the file system 16653b1253e8Schristos and not in the compression library, errnum is set to Z_ERRNO and the 16663b1253e8Schristos application may consult errno to get the exact error code. 16676db8c6e9Schristos 16686db8c6e9Schristos The application must not modify the returned string. Future calls to 16696db8c6e9Schristos this function may invalidate the previously returned string. If file is 16706db8c6e9Schristos closed, then the string previously returned by gzerror will no longer be 16716db8c6e9Schristos available. 16726db8c6e9Schristos 16736db8c6e9Schristos gzerror() should be used to distinguish errors from end-of-file for those 16746db8c6e9Schristos functions above that do not distinguish those cases in their return values. 1675aaf4ece6Schristos */ 1676aaf4ece6Schristos 1677*96c32821Schristos ZEXTERN void ZEXPORT gzclearerr(gzFile file); 1678aaf4ece6Schristos /* 16793b1253e8Schristos Clear the error and end-of-file flags for file. This is analogous to the 1680aaf4ece6Schristos clearerr() function in stdio. This is useful for continuing to read a gzip 1681aaf4ece6Schristos file that is being written concurrently. 1682aaf4ece6Schristos */ 1683aaf4ece6Schristos 16846db8c6e9Schristos #endif /* !Z_SOLO */ 16856db8c6e9Schristos 1686aaf4ece6Schristos /* checksum functions */ 1687aaf4ece6Schristos 1688aaf4ece6Schristos /* 1689aaf4ece6Schristos These functions are not related to compression but are exported 16906db8c6e9Schristos anyway because they might be useful in applications using the compression 16916db8c6e9Schristos library. 1692aaf4ece6Schristos */ 1693aaf4ece6Schristos 1694*96c32821Schristos ZEXTERN uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len); 1695aaf4ece6Schristos /* 1696aaf4ece6Schristos Update a running Adler-32 checksum with the bytes buf[0..len-1] and 16973b1253e8Schristos return the updated checksum. An Adler-32 value is in the range of a 32-bit 16983b1253e8Schristos unsigned integer. If buf is Z_NULL, this function returns the required 16993b1253e8Schristos initial value for the checksum. 17006db8c6e9Schristos 17016db8c6e9Schristos An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed 17026db8c6e9Schristos much faster. 17036db8c6e9Schristos 17046db8c6e9Schristos Usage example: 1705aaf4ece6Schristos 1706aaf4ece6Schristos uLong adler = adler32(0L, Z_NULL, 0); 1707aaf4ece6Schristos 1708aaf4ece6Schristos while (read_buffer(buffer, length) != EOF) { 1709aaf4ece6Schristos adler = adler32(adler, buffer, length); 1710aaf4ece6Schristos } 1711aaf4ece6Schristos if (adler != original_adler) error(); 1712aaf4ece6Schristos */ 1713aaf4ece6Schristos 1714*96c32821Schristos ZEXTERN uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, 1715*96c32821Schristos z_size_t len); 17166db8c6e9Schristos /* 17176db8c6e9Schristos Same as adler32(), but with a size_t length. 17186db8c6e9Schristos */ 17196db8c6e9Schristos 17206db8c6e9Schristos /* 1721*96c32821Schristos ZEXTERN uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, 1722*96c32821Schristos z_off_t len2); 17236db8c6e9Schristos 1724aaf4ece6Schristos Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 1725aaf4ece6Schristos and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for 1726aaf4ece6Schristos each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of 17276db8c6e9Schristos seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note 17286db8c6e9Schristos that the z_off_t type (like off_t) is a signed integer. If len2 is 17296db8c6e9Schristos negative, the result has no meaning or utility. 1730aaf4ece6Schristos */ 1731aaf4ece6Schristos 1732eb31298aSchristos #if !defined(_KERNEL) && !defined(_STANDALONE) 1733*96c32821Schristos ZEXTERN uLong ZEXPORT crc32(uLong crc, const Bytef *buf, uInt len); 1734eb31298aSchristos #endif 1735aaf4ece6Schristos /* 1736aaf4ece6Schristos Update a running CRC-32 with the bytes buf[0..len-1] and return the 17373b1253e8Schristos updated CRC-32. A CRC-32 value is in the range of a 32-bit unsigned integer. 17383b1253e8Schristos If buf is Z_NULL, this function returns the required initial value for the 17393b1253e8Schristos crc. Pre- and post-conditioning (one's complement) is performed within this 17403b1253e8Schristos function so it shouldn't be done by the application. 17416db8c6e9Schristos 1742aaf4ece6Schristos Usage example: 1743aaf4ece6Schristos 1744aaf4ece6Schristos uLong crc = crc32(0L, Z_NULL, 0); 1745aaf4ece6Schristos 1746aaf4ece6Schristos while (read_buffer(buffer, length) != EOF) { 1747aaf4ece6Schristos crc = crc32(crc, buffer, length); 1748aaf4ece6Schristos } 1749aaf4ece6Schristos if (crc != original_crc) error(); 1750aaf4ece6Schristos */ 1751aaf4ece6Schristos 1752*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_z(uLong crc, const Bytef *buf, 1753*96c32821Schristos z_size_t len); 17546db8c6e9Schristos /* 17556db8c6e9Schristos Same as crc32(), but with a size_t length. 17566db8c6e9Schristos */ 1757aaf4ece6Schristos 1758aaf4ece6Schristos /* 1759*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2); 17606db8c6e9Schristos 1761aaf4ece6Schristos Combine two CRC-32 check values into one. For two sequences of bytes, 1762aaf4ece6Schristos seq1 and seq2 with lengths len1 and len2, CRC-32 check values were 1763aaf4ece6Schristos calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 1764aaf4ece6Schristos check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and 1765*96c32821Schristos len2. len2 must be non-negative. 1766aaf4ece6Schristos */ 1767aaf4ece6Schristos 17683b1253e8Schristos /* 1769*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t len2); 17703b1253e8Schristos 17713b1253e8Schristos Return the operator corresponding to length len2, to be used with 1772*96c32821Schristos crc32_combine_op(). len2 must be non-negative. 17733b1253e8Schristos */ 17743b1253e8Schristos 1775*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op); 17763b1253e8Schristos /* 17773b1253e8Schristos Give the same result as crc32_combine(), using op in place of len2. op is 17783b1253e8Schristos is generated from len2 by crc32_combine_gen(). This will be faster than 17793b1253e8Schristos crc32_combine() if the generated op is used more than once. 17803b1253e8Schristos */ 17813b1253e8Schristos 1782aaf4ece6Schristos 1783aaf4ece6Schristos /* various hacks, don't look :) */ 1784aaf4ece6Schristos 1785aaf4ece6Schristos /* deflateInit and inflateInit are macros to allow checking the zlib version 1786aaf4ece6Schristos * and the compiler's view of z_stream: 1787aaf4ece6Schristos */ 1788*96c32821Schristos ZEXTERN int ZEXPORT deflateInit_(z_streamp strm, int level, 1789*96c32821Schristos const char *version, int stream_size); 1790*96c32821Schristos ZEXTERN int ZEXPORT inflateInit_(z_streamp strm, 1791*96c32821Schristos const char *version, int stream_size); 1792*96c32821Schristos ZEXTERN int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, 1793aaf4ece6Schristos int windowBits, int memLevel, 1794aaf4ece6Schristos int strategy, const char *version, 1795*96c32821Schristos int stream_size); 1796*96c32821Schristos ZEXTERN int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, 1797*96c32821Schristos const char *version, int stream_size); 1798*96c32821Schristos ZEXTERN int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits, 1799aaf4ece6Schristos unsigned char FAR *window, 1800aaf4ece6Schristos const char *version, 1801*96c32821Schristos int stream_size); 18026db8c6e9Schristos #ifdef Z_PREFIX_SET 18036db8c6e9Schristos # define z_deflateInit(strm, level) \ 18046db8c6e9Schristos deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) 18056db8c6e9Schristos # define z_inflateInit(strm) \ 18066db8c6e9Schristos inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) 18076db8c6e9Schristos # define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ 18086db8c6e9Schristos deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ 18096db8c6e9Schristos (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) 18106db8c6e9Schristos # define z_inflateInit2(strm, windowBits) \ 18116db8c6e9Schristos inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ 18126db8c6e9Schristos (int)sizeof(z_stream)) 18136db8c6e9Schristos # define z_inflateBackInit(strm, windowBits, window) \ 18146db8c6e9Schristos inflateBackInit_((strm), (windowBits), (window), \ 18156db8c6e9Schristos ZLIB_VERSION, (int)sizeof(z_stream)) 18166db8c6e9Schristos #else 1817aaf4ece6Schristos # define deflateInit(strm, level) \ 18186db8c6e9Schristos deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) 1819aaf4ece6Schristos # define inflateInit(strm) \ 18206db8c6e9Schristos inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) 1821aaf4ece6Schristos # define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ 1822aaf4ece6Schristos deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ 18236db8c6e9Schristos (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) 1824aaf4ece6Schristos # define inflateInit2(strm, windowBits) \ 18256db8c6e9Schristos inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ 18266db8c6e9Schristos (int)sizeof(z_stream)) 1827aaf4ece6Schristos # define inflateBackInit(strm, windowBits, window) \ 1828aaf4ece6Schristos inflateBackInit_((strm), (windowBits), (window), \ 18296db8c6e9Schristos ZLIB_VERSION, (int)sizeof(z_stream)) 1830aaf4ece6Schristos #endif 1831aaf4ece6Schristos 18326db8c6e9Schristos #ifndef Z_SOLO 18336db8c6e9Schristos 18346db8c6e9Schristos /* gzgetc() macro and its supporting function and exposed data structure. Note 18356db8c6e9Schristos * that the real internal state is much larger than the exposed structure. 18366db8c6e9Schristos * This abbreviated structure exposes just enough for the gzgetc() macro. The 18376db8c6e9Schristos * user should not mess with these exposed elements, since their names or 18386db8c6e9Schristos * behavior could change in the future, perhaps even capriciously. They can 18396db8c6e9Schristos * only be used by the gzgetc() macro. You have been warned. 18406db8c6e9Schristos */ 18416db8c6e9Schristos struct gzFile_s { 18426db8c6e9Schristos unsigned have; 18436db8c6e9Schristos unsigned char *next; 18446db8c6e9Schristos z_off64_t pos; 18456db8c6e9Schristos }; 1846*96c32821Schristos ZEXTERN int ZEXPORT gzgetc_(gzFile file); /* backward compatibility */ 18476db8c6e9Schristos #ifdef Z_PREFIX_SET 18486db8c6e9Schristos # undef z_gzgetc 18496db8c6e9Schristos # define z_gzgetc(g) \ 18506db8c6e9Schristos ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) 18516db8c6e9Schristos #else 18526db8c6e9Schristos # define gzgetc(g) \ 18536db8c6e9Schristos ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) 18546db8c6e9Schristos #endif 18556db8c6e9Schristos 18566db8c6e9Schristos /* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or 18576db8c6e9Schristos * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if 18586db8c6e9Schristos * both are true, the application gets the *64 functions, and the regular 18596db8c6e9Schristos * functions are changed to 64 bits) -- in case these are set on systems 18606db8c6e9Schristos * without large file support, _LFS64_LARGEFILE must also be true 18616db8c6e9Schristos */ 18626db8c6e9Schristos #ifdef Z_LARGE64 1863*96c32821Schristos ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); 1864*96c32821Schristos ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int); 1865*96c32821Schristos ZEXTERN z_off64_t ZEXPORT gztell64(gzFile); 1866*96c32821Schristos ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile); 1867*96c32821Schristos ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off64_t); 1868*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off64_t); 1869*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off64_t); 18706db8c6e9Schristos #endif 18716db8c6e9Schristos 18726db8c6e9Schristos #if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) 18736db8c6e9Schristos # ifdef Z_PREFIX_SET 18746db8c6e9Schristos # define z_gzopen z_gzopen64 18756db8c6e9Schristos # define z_gzseek z_gzseek64 18766db8c6e9Schristos # define z_gztell z_gztell64 18776db8c6e9Schristos # define z_gzoffset z_gzoffset64 18786db8c6e9Schristos # define z_adler32_combine z_adler32_combine64 18796db8c6e9Schristos # define z_crc32_combine z_crc32_combine64 18803b1253e8Schristos # define z_crc32_combine_gen z_crc32_combine_gen64 18816db8c6e9Schristos # else 18826db8c6e9Schristos # define gzopen gzopen64 18836db8c6e9Schristos # define gzseek gzseek64 18846db8c6e9Schristos # define gztell gztell64 18856db8c6e9Schristos # define gzoffset gzoffset64 18866db8c6e9Schristos # define adler32_combine adler32_combine64 18876db8c6e9Schristos # define crc32_combine crc32_combine64 18883b1253e8Schristos # define crc32_combine_gen crc32_combine_gen64 18896db8c6e9Schristos # endif 18906db8c6e9Schristos # ifndef Z_LARGE64 1891*96c32821Schristos ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); 1892*96c32821Schristos ZEXTERN z_off_t ZEXPORT gzseek64(gzFile, z_off_t, int); 1893*96c32821Schristos ZEXTERN z_off_t ZEXPORT gztell64(gzFile); 1894*96c32821Schristos ZEXTERN z_off_t ZEXPORT gzoffset64(gzFile); 1895*96c32821Schristos ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t); 1896*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t); 1897*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t); 18986db8c6e9Schristos # endif 18996db8c6e9Schristos #else 1900*96c32821Schristos ZEXTERN gzFile ZEXPORT gzopen(const char *, const char *); 1901*96c32821Schristos ZEXTERN z_off_t ZEXPORT gzseek(gzFile, z_off_t, int); 1902*96c32821Schristos ZEXTERN z_off_t ZEXPORT gztell(gzFile); 1903*96c32821Schristos ZEXTERN z_off_t ZEXPORT gzoffset(gzFile); 1904*96c32821Schristos ZEXTERN uLong ZEXPORT adler32_combine(uLong, uLong, z_off_t); 1905*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine(uLong, uLong, z_off_t); 1906*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t); 19076db8c6e9Schristos #endif 19086db8c6e9Schristos 19096db8c6e9Schristos #else /* Z_SOLO */ 19106db8c6e9Schristos 1911*96c32821Schristos ZEXTERN uLong ZEXPORT adler32_combine(uLong, uLong, z_off_t); 1912*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine(uLong, uLong, z_off_t); 1913*96c32821Schristos ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t); 19146db8c6e9Schristos 19156db8c6e9Schristos #endif /* !Z_SOLO */ 19166db8c6e9Schristos 19176db8c6e9Schristos /* undocumented functions */ 1918*96c32821Schristos ZEXTERN const char * ZEXPORT zError(int); 1919*96c32821Schristos ZEXTERN int ZEXPORT inflateSyncPoint(z_streamp); 1920*96c32821Schristos ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table(void); 1921*96c32821Schristos ZEXTERN int ZEXPORT inflateUndermine(z_streamp, int); 1922*96c32821Schristos ZEXTERN int ZEXPORT inflateValidate(z_streamp, int); 1923*96c32821Schristos ZEXTERN unsigned long ZEXPORT inflateCodesUsed(z_streamp); 1924*96c32821Schristos ZEXTERN int ZEXPORT inflateResetKeep(z_streamp); 1925*96c32821Schristos ZEXTERN int ZEXPORT deflateResetKeep(z_streamp); 19263b1253e8Schristos #if defined(_WIN32) && !defined(Z_SOLO) 1927*96c32821Schristos ZEXTERN gzFile ZEXPORT gzopen_w(const wchar_t *path, 1928*96c32821Schristos const char *mode); 19296db8c6e9Schristos #endif 19306db8c6e9Schristos #if defined(STDC) || defined(Z_HAVE_STDARG_H) 19316db8c6e9Schristos # ifndef Z_SOLO 1932*96c32821Schristos ZEXTERN int ZEXPORTVA gzvprintf(gzFile file, 19336db8c6e9Schristos const char *format, 1934*96c32821Schristos va_list va); 19356db8c6e9Schristos # endif 19366db8c6e9Schristos #endif 1937aaf4ece6Schristos 1938aaf4ece6Schristos #ifdef __cplusplus 1939aaf4ece6Schristos } 1940aaf4ece6Schristos #endif 1941aaf4ece6Schristos 1942aaf4ece6Schristos #endif /* ZLIB_H */ 1943