zlib.h revision 131377
117651Speter/* zlib.h -- interface of the 'zlib' general purpose compression library 2131377Stjr version 1.2.1, November 17th, 2003 317651Speter 4131377Stjr Copyright (C) 1995-2003 Jean-loup Gailly and Mark Adler 517651Speter 617651Speter This software is provided 'as-is', without any express or implied 717651Speter warranty. In no event will the authors be held liable for any damages 817651Speter arising from the use of this software. 917651Speter 1017651Speter Permission is granted to anyone to use this software for any purpose, 1117651Speter including commercial applications, and to alter it and redistribute it 1217651Speter freely, subject to the following restrictions: 1317651Speter 1417651Speter 1. The origin of this software must not be misrepresented; you must not 1517651Speter claim that you wrote the original software. If you use this software 1617651Speter in a product, an acknowledgment in the product documentation would be 1717651Speter appreciated but is not required. 1817651Speter 2. Altered source versions must be plainly marked as such, and must not be 1917651Speter misrepresented as being the original software. 2017651Speter 3. This notice may not be removed or altered from any source distribution. 2117651Speter 2217651Speter Jean-loup Gailly Mark Adler 2333904Ssteve jloup@gzip.org madler@alumni.caltech.edu 2417651Speter 2517651Speter 2617651Speter The data format used by the zlib library is described by RFCs (Request for 27131377Stjr Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt 2817651Speter (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). 2917651Speter*/ 3017651Speter 31131377Stjr#ifndef ZLIB_H 32131377Stjr#define ZLIB_H 3317651Speter 3442468Speter#include "zconf.h" 3542468Speter 3617651Speter#ifdef __cplusplus 3717651Speterextern "C" { 3817651Speter#endif 3917651Speter 40131377Stjr#define ZLIB_VERSION "1.2.1" 41131377Stjr#define ZLIB_VERNUM 0x1210 4217651Speter 43131377Stjr/* 4417651Speter The 'zlib' compression library provides in-memory compression and 4517651Speter decompression functions, including integrity checks of the uncompressed 4617651Speter data. This version of the library supports only one compression method 4733904Ssteve (deflation) but other algorithms will be added later and will have the same 4817651Speter stream interface. 4917651Speter 5017651Speter Compression can be done in a single step if the buffers are large 5117651Speter enough (for example if an input file is mmap'ed), or can be done by 5217651Speter repeated calls of the compression function. In the latter case, the 5317651Speter application must provide more input and/or consume the output 5417651Speter (providing more output space) before each call. 5517651Speter 56131377Stjr The compressed data format used by the in-memory functions is the zlib 57131377Stjr format, which is a zlib wrapper documented in RFC 1950, wrapped around a 58131377Stjr deflate stream, which is itself documented in RFC 1951. 59131377Stjr 6033904Ssteve The library also supports reading and writing files in gzip (.gz) format 61131377Stjr with an interface similar to that of stdio using the functions that start 62131377Stjr with "gz". The gzip format is different from the zlib format. gzip is a 63131377Stjr gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. 6433904Ssteve 65131377Stjr The zlib format was designed to be compact and fast for use in memory 66131377Stjr and on communications channels. The gzip format was designed for single- 67131377Stjr file compression on file systems, has a larger header than zlib to maintain 68131377Stjr directory information, and uses a different, slower check method than zlib. 69131377Stjr 70131377Stjr This library does not provide any functions to write gzip files in memory. 71131377Stjr However such functions could be easily written using zlib's deflate function, 72131377Stjr the documentation in the gzip RFC, and the examples in gzio.c. 73131377Stjr 7433904Ssteve The library does not install any signal handler. The decoder checks 7533904Ssteve the consistency of the compressed data, so the library should never 7633904Ssteve crash even in case of corrupted input. 7717651Speter*/ 7817651Speter 7917651Spetertypedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); 8017651Spetertypedef void (*free_func) OF((voidpf opaque, voidpf address)); 8117651Speter 8217651Speterstruct internal_state; 8317651Speter 8417651Spetertypedef struct z_stream_s { 8517651Speter Bytef *next_in; /* next input byte */ 8617651Speter uInt avail_in; /* number of bytes available at next_in */ 8717651Speter uLong total_in; /* total nb of input bytes read so far */ 8817651Speter 8917651Speter Bytef *next_out; /* next output byte should be put there */ 9017651Speter uInt avail_out; /* remaining free space at next_out */ 9117651Speter uLong total_out; /* total nb of bytes output so far */ 9217651Speter 9317651Speter char *msg; /* last error message, NULL if no error */ 9417651Speter struct internal_state FAR *state; /* not visible by applications */ 9517651Speter 9617651Speter alloc_func zalloc; /* used to allocate the internal state */ 9717651Speter free_func zfree; /* used to free the internal state */ 9817651Speter voidpf opaque; /* private data object passed to zalloc and zfree */ 9917651Speter 10017651Speter int data_type; /* best guess about the data type: ascii or binary */ 10117651Speter uLong adler; /* adler32 value of the uncompressed data */ 10217651Speter uLong reserved; /* reserved for future use */ 10317651Speter} z_stream; 10417651Speter 10517651Spetertypedef z_stream FAR *z_streamp; 10617651Speter 10717651Speter/* 10817651Speter The application must update next_in and avail_in when avail_in has 10917651Speter dropped to zero. It must update next_out and avail_out when avail_out 11017651Speter has dropped to zero. The application must initialize zalloc, zfree and 11117651Speter opaque before calling the init function. All other fields are set by the 11217651Speter compression library and must not be updated by the application. 11317651Speter 11417651Speter The opaque value provided by the application will be passed as the first 11517651Speter parameter for calls of zalloc and zfree. This can be useful for custom 11617651Speter memory management. The compression library attaches no meaning to the 11717651Speter opaque value. 11817651Speter 11917651Speter zalloc must return Z_NULL if there is not enough memory for the object. 12033904Ssteve If zlib is used in a multi-threaded application, zalloc and zfree must be 12133904Ssteve thread safe. 12233904Ssteve 12317651Speter On 16-bit systems, the functions zalloc and zfree must be able to allocate 12417651Speter exactly 65536 bytes, but will not be required to allocate more than this 12517651Speter if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, 12617651Speter pointers returned by zalloc for objects of exactly 65536 bytes *must* 12717651Speter have their offset normalized to zero. The default allocation function 12817651Speter provided by this library ensures this (see zutil.c). To reduce memory 12917651Speter requirements and avoid any allocation of 64K objects, at the expense of 13017651Speter compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). 13117651Speter 13217651Speter The fields total_in and total_out can be used for statistics or 13317651Speter progress reports. After compression, total_in holds the total size of 13417651Speter the uncompressed data and may be saved for use in the decompressor 13517651Speter (particularly if the decompressor wants to decompress everything in 13617651Speter a single step). 13717651Speter*/ 13817651Speter 13917651Speter /* constants */ 14017651Speter 14117651Speter#define Z_NO_FLUSH 0 14233904Ssteve#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ 14317651Speter#define Z_SYNC_FLUSH 2 14417651Speter#define Z_FULL_FLUSH 3 14517651Speter#define Z_FINISH 4 146131377Stjr#define Z_BLOCK 5 147131377Stjr/* Allowed flush values; see deflate() and inflate() below for details */ 14817651Speter 14917651Speter#define Z_OK 0 15017651Speter#define Z_STREAM_END 1 15117651Speter#define Z_NEED_DICT 2 15217651Speter#define Z_ERRNO (-1) 15317651Speter#define Z_STREAM_ERROR (-2) 15417651Speter#define Z_DATA_ERROR (-3) 15517651Speter#define Z_MEM_ERROR (-4) 15617651Speter#define Z_BUF_ERROR (-5) 15717651Speter#define Z_VERSION_ERROR (-6) 15817651Speter/* Return codes for the compression/decompression functions. Negative 15917651Speter * values are errors, positive values are used for special but normal events. 16017651Speter */ 16117651Speter 16217651Speter#define Z_NO_COMPRESSION 0 16317651Speter#define Z_BEST_SPEED 1 16417651Speter#define Z_BEST_COMPRESSION 9 16517651Speter#define Z_DEFAULT_COMPRESSION (-1) 16617651Speter/* compression levels */ 16717651Speter 16817651Speter#define Z_FILTERED 1 16917651Speter#define Z_HUFFMAN_ONLY 2 170131377Stjr#define Z_RLE 3 17117651Speter#define Z_DEFAULT_STRATEGY 0 17217651Speter/* compression strategy; see deflateInit2() below for details */ 17317651Speter 17417651Speter#define Z_BINARY 0 17517651Speter#define Z_ASCII 1 17617651Speter#define Z_UNKNOWN 2 177131377Stjr/* Possible values of the data_type field (though see inflate()) */ 17817651Speter 17917651Speter#define Z_DEFLATED 8 18017651Speter/* The deflate compression method (the only one supported in this version) */ 18117651Speter 18217651Speter#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 18317651Speter 18417651Speter#define zlib_version zlibVersion() 18517651Speter/* for compatibility with versions < 1.0.2 */ 18617651Speter 18717651Speter /* basic functions */ 18817651Speter 18942468SpeterZEXTERN const char * ZEXPORT zlibVersion OF((void)); 19017651Speter/* The application can compare zlibVersion and ZLIB_VERSION for consistency. 19117651Speter If the first character differs, the library code actually used is 19217651Speter not compatible with the zlib.h header file used by the application. 19317651Speter This check is automatically made by deflateInit and inflateInit. 19417651Speter */ 19517651Speter 196131377Stjr/* 19742468SpeterZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); 19817651Speter 19917651Speter Initializes the internal stream state for compression. The fields 20017651Speter zalloc, zfree and opaque must be initialized before by the caller. 20117651Speter If zalloc and zfree are set to Z_NULL, deflateInit updates them to 20217651Speter use default allocation functions. 20317651Speter 20417651Speter The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 20517651Speter 1 gives best speed, 9 gives best compression, 0 gives no compression at 20617651Speter all (the input data is simply copied a block at a time). 20717651Speter Z_DEFAULT_COMPRESSION requests a default compromise between speed and 20817651Speter compression (currently equivalent to level 6). 20917651Speter 21017651Speter deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 21117651Speter enough memory, Z_STREAM_ERROR if level is not a valid compression level, 21217651Speter Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible 21317651Speter with the version assumed by the caller (ZLIB_VERSION). 21417651Speter msg is set to null if there is no error message. deflateInit does not 21517651Speter perform any compression: this will be done by deflate(). 21617651Speter*/ 21717651Speter 21817651Speter 21942468SpeterZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); 22017651Speter/* 22133904Ssteve deflate compresses as much data as possible, and stops when the input 22233904Ssteve buffer becomes empty or the output buffer becomes full. It may introduce some 22333904Ssteve output latency (reading input without producing any output) except when 22433904Ssteve forced to flush. 22517651Speter 22633904Ssteve The detailed semantics are as follows. deflate performs one or both of the 22733904Ssteve following actions: 22833904Ssteve 22917651Speter - Compress more input starting at next_in and update next_in and avail_in 23017651Speter accordingly. If not all input can be processed (because there is not 23117651Speter enough room in the output buffer), next_in and avail_in are updated and 23217651Speter processing will resume at this point for the next call of deflate(). 23317651Speter 23417651Speter - Provide more output starting at next_out and update next_out and avail_out 23517651Speter accordingly. This action is forced if the parameter flush is non zero. 23617651Speter Forcing flush frequently degrades the compression ratio, so this parameter 23717651Speter should be set only when necessary (in interactive applications). 23817651Speter Some output may be provided even if flush is not set. 23917651Speter 24017651Speter Before the call of deflate(), the application should ensure that at least 24117651Speter one of the actions is possible, by providing more input and/or consuming 24217651Speter more output, and updating avail_in or avail_out accordingly; avail_out 24317651Speter should never be zero before the call. The application can consume the 24417651Speter compressed output when it wants, for example when the output buffer is full 24517651Speter (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK 24617651Speter and with zero avail_out, it must be called again after making room in the 24717651Speter output buffer because there might be more output pending. 24817651Speter 24933904Ssteve If the parameter flush is set to Z_SYNC_FLUSH, all pending output is 25033904Ssteve flushed to the output buffer and the output is aligned on a byte boundary, so 25133904Ssteve that the decompressor can get all input data available so far. (In particular 25233904Ssteve avail_in is zero after the call if enough output space has been provided 25333904Ssteve before the call.) Flushing may degrade compression for some compression 25433904Ssteve algorithms and so it should be used only when necessary. 25517651Speter 25633904Ssteve If flush is set to Z_FULL_FLUSH, all output is flushed as with 25733904Ssteve Z_SYNC_FLUSH, and the compression state is reset so that decompression can 25833904Ssteve restart from this point if previous compressed data has been damaged or if 25933904Ssteve random access is desired. Using Z_FULL_FLUSH too often can seriously degrade 26033904Ssteve the compression. 26133904Ssteve 26233904Ssteve If deflate returns with avail_out == 0, this function must be called again 26333904Ssteve with the same value of the flush parameter and more output space (updated 26433904Ssteve avail_out), until the flush is complete (deflate returns with non-zero 265131377Stjr avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that 266131377Stjr avail_out is greater than six to avoid repeated flush markers due to 267131377Stjr avail_out == 0 on return. 26833904Ssteve 26917651Speter If the parameter flush is set to Z_FINISH, pending input is processed, 27017651Speter pending output is flushed and deflate returns with Z_STREAM_END if there 27117651Speter was enough output space; if deflate returns with Z_OK, this function must be 27217651Speter called again with Z_FINISH and more output space (updated avail_out) but no 27317651Speter more input data, until it returns with Z_STREAM_END or an error. After 27417651Speter deflate has returned Z_STREAM_END, the only possible operations on the 27517651Speter stream are deflateReset or deflateEnd. 276131377Stjr 27717651Speter Z_FINISH can be used immediately after deflateInit if all the compression 27817651Speter is to be done in a single step. In this case, avail_out must be at least 279131377Stjr the value returned by deflateBound (see below). If deflate does not return 28017651Speter Z_STREAM_END, then it must be called again as described above. 28117651Speter 28233904Ssteve deflate() sets strm->adler to the adler32 checksum of all input read 28333904Ssteve so far (that is, total_in bytes). 28433904Ssteve 28517651Speter deflate() may update data_type if it can make a good guess about 28617651Speter the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered 28717651Speter binary. This field is only for information purposes and does not affect 28817651Speter the compression algorithm in any manner. 28917651Speter 29017651Speter deflate() returns Z_OK if some progress has been made (more input 29117651Speter processed or more output produced), Z_STREAM_END if all input has been 29217651Speter consumed and all output has been produced (only when flush is set to 29317651Speter Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 29442468Speter if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible 295131377Stjr (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not 296131377Stjr fatal, and deflate() can be called again with more input and more output 297131377Stjr space to continue compressing. 29817651Speter*/ 29917651Speter 30017651Speter 30142468SpeterZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); 30217651Speter/* 30317651Speter All dynamically allocated data structures for this stream are freed. 30417651Speter This function discards any unprocessed input and does not flush any 30517651Speter pending output. 30617651Speter 30717651Speter deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 30817651Speter stream state was inconsistent, Z_DATA_ERROR if the stream was freed 30917651Speter prematurely (some input or output was discarded). In the error case, 31017651Speter msg may be set but then points to a static string (which must not be 31117651Speter deallocated). 31217651Speter*/ 31317651Speter 31417651Speter 315131377Stjr/* 31642468SpeterZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); 31717651Speter 31817651Speter Initializes the internal stream state for decompression. The fields 31933904Ssteve next_in, avail_in, zalloc, zfree and opaque must be initialized before by 32033904Ssteve the caller. If next_in is not Z_NULL and avail_in is large enough (the exact 32133904Ssteve value depends on the compression method), inflateInit determines the 32233904Ssteve compression method from the zlib header and allocates all data structures 32333904Ssteve accordingly; otherwise the allocation will be deferred to the first call of 32433904Ssteve inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to 32533904Ssteve use default allocation functions. 32617651Speter 32733904Ssteve inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 32833904Ssteve memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 32933904Ssteve version assumed by the caller. msg is set to null if there is no error 33033904Ssteve message. inflateInit does not perform any decompression apart from reading 33133904Ssteve the zlib header if present: this will be done by inflate(). (So next_in and 33233904Ssteve avail_in may be modified, but next_out and avail_out are unchanged.) 33317651Speter*/ 33417651Speter 33517651Speter 33642468SpeterZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); 33717651Speter/* 33833904Ssteve inflate decompresses as much data as possible, and stops when the input 339131377Stjr buffer becomes empty or the output buffer becomes full. It may introduce 340131377Stjr some output latency (reading input without producing any output) except when 341131377Stjr forced to flush. 34217651Speter 34333904Ssteve The detailed semantics are as follows. inflate performs one or both of the 34433904Ssteve following actions: 34533904Ssteve 34617651Speter - Decompress more input starting at next_in and update next_in and avail_in 34717651Speter accordingly. If not all input can be processed (because there is not 34817651Speter enough room in the output buffer), next_in is updated and processing 34917651Speter will resume at this point for the next call of inflate(). 35017651Speter 35117651Speter - Provide more output starting at next_out and update next_out and avail_out 35217651Speter accordingly. inflate() provides as much output as possible, until there 35317651Speter is no more input data or no more space in the output buffer (see below 35417651Speter about the flush parameter). 35517651Speter 35617651Speter Before the call of inflate(), the application should ensure that at least 35717651Speter one of the actions is possible, by providing more input and/or consuming 35817651Speter more output, and updating the next_* and avail_* values accordingly. 35917651Speter The application can consume the uncompressed output when it wants, for 36017651Speter example when the output buffer is full (avail_out == 0), or after each 36117651Speter call of inflate(). If inflate returns Z_OK and with zero avail_out, it 36217651Speter must be called again after making room in the output buffer because there 36317651Speter might be more output pending. 36417651Speter 365131377Stjr The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, 366131377Stjr Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much 367131377Stjr output as possible to the output buffer. Z_BLOCK requests that inflate() stop 368131377Stjr if and when it get to the next deflate block boundary. When decoding the zlib 369131377Stjr or gzip format, this will cause inflate() to return immediately after the 370131377Stjr header and before the first block. When doing a raw inflate, inflate() will 371131377Stjr go ahead and process the first block, and will return when it gets to the end 372131377Stjr of that block, or when it runs out of data. 37317651Speter 374131377Stjr The Z_BLOCK option assists in appending to or combining deflate streams. 375131377Stjr Also to assist in this, on return inflate() will set strm->data_type to the 376131377Stjr number of unused bits in the last byte taken from strm->next_in, plus 64 377131377Stjr if inflate() is currently decoding the last block in the deflate stream, 378131377Stjr plus 128 if inflate() returned immediately after decoding an end-of-block 379131377Stjr code or decoding the complete header up to just before the first byte of the 380131377Stjr deflate stream. The end-of-block will not be indicated until all of the 381131377Stjr uncompressed data from that block has been written to strm->next_out. The 382131377Stjr number of unused bits may in general be greater than seven, except when 383131377Stjr bit 7 of data_type is set, in which case the number of unused bits will be 384131377Stjr less than eight. 385131377Stjr 38617651Speter inflate() should normally be called until it returns Z_STREAM_END or an 38717651Speter error. However if all decompression is to be performed in a single step 38817651Speter (a single call of inflate), the parameter flush should be set to 38917651Speter Z_FINISH. In this case all pending input is processed and all pending 39017651Speter output is flushed; avail_out must be large enough to hold all the 39117651Speter uncompressed data. (The size of the uncompressed data may have been saved 39217651Speter by the compressor for this purpose.) The next operation on this stream must 39317651Speter be inflateEnd to deallocate the decompression state. The use of Z_FINISH 394131377Stjr is never required, but can be used to inform inflate that a faster approach 39517651Speter may be used for the single inflate() call. 39617651Speter 397131377Stjr In this implementation, inflate() always flushes as much output as 398131377Stjr possible to the output buffer, and always uses the faster approach on the 399131377Stjr first call. So the only effect of the flush parameter in this implementation 400131377Stjr is on the return value of inflate(), as noted below, or when it returns early 401131377Stjr because Z_BLOCK is used. 40233904Ssteve 403131377Stjr If a preset dictionary is needed after this call (see inflateSetDictionary 404131377Stjr below), inflate sets strm-adler to the adler32 checksum of the dictionary 405131377Stjr chosen by the compressor and returns Z_NEED_DICT; otherwise it sets 406131377Stjr strm->adler to the adler32 checksum of all output produced so far (that is, 407131377Stjr total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described 408131377Stjr below. At the end of the stream, inflate() checks that its computed adler32 409131377Stjr checksum is equal to that saved by the compressor and returns Z_STREAM_END 410131377Stjr only if the checksum is correct. 411131377Stjr 412131377Stjr inflate() will decompress and check either zlib-wrapped or gzip-wrapped 413131377Stjr deflate data. The header type is detected automatically. Any information 414131377Stjr contained in the gzip header is not retained, so applications that need that 415131377Stjr information should instead use raw inflate, see inflateInit2() below, or 416131377Stjr inflateBack() and perform their own processing of the gzip header and 417131377Stjr trailer. 418131377Stjr 41933904Ssteve inflate() returns Z_OK if some progress has been made (more input processed 42033904Ssteve or more output produced), Z_STREAM_END if the end of the compressed data has 42133904Ssteve been reached and all uncompressed output has been produced, Z_NEED_DICT if a 42233904Ssteve preset dictionary is needed at this point, Z_DATA_ERROR if the input data was 423131377Stjr corrupted (input stream not conforming to the zlib format or incorrect check 424131377Stjr value), Z_STREAM_ERROR if the stream structure was inconsistent (for example 425131377Stjr if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, 426131377Stjr Z_BUF_ERROR if no progress is possible or if there was not enough room in the 427131377Stjr output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and 428131377Stjr inflate() can be called again with more input and more output space to 429131377Stjr continue decompressing. If Z_DATA_ERROR is returned, the application may then 430131377Stjr call inflateSync() to look for a good compression block if a partial recovery 431131377Stjr of the data is desired. 43217651Speter*/ 43317651Speter 43417651Speter 43542468SpeterZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); 43617651Speter/* 43717651Speter All dynamically allocated data structures for this stream are freed. 43817651Speter This function discards any unprocessed input and does not flush any 43917651Speter pending output. 44017651Speter 44117651Speter inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state 44217651Speter was inconsistent. In the error case, msg may be set but then points to a 44317651Speter static string (which must not be deallocated). 44417651Speter*/ 44517651Speter 44617651Speter /* Advanced functions */ 44717651Speter 44817651Speter/* 44917651Speter The following functions are needed only in some special applications. 45017651Speter*/ 45117651Speter 452131377Stjr/* 45342468SpeterZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, 45442468Speter int level, 45542468Speter int method, 45642468Speter int windowBits, 45742468Speter int memLevel, 45842468Speter int strategy)); 45917651Speter 46017651Speter This is another version of deflateInit with more compression options. The 46117651Speter fields next_in, zalloc, zfree and opaque must be initialized before by 46217651Speter the caller. 46317651Speter 46417651Speter The method parameter is the compression method. It must be Z_DEFLATED in 46533904Ssteve this version of the library. 46617651Speter 46717651Speter The windowBits parameter is the base two logarithm of the window size 468131377Stjr (the size of the history buffer). It should be in the range 8..15 for this 46933904Ssteve version of the library. Larger values of this parameter result in better 47033904Ssteve compression at the expense of memory usage. The default value is 15 if 47133904Ssteve deflateInit is used instead. 47217651Speter 473131377Stjr windowBits can also be -8..-15 for raw deflate. In this case, -windowBits 474131377Stjr determines the window size. deflate() will then generate raw deflate data 475131377Stjr with no zlib header or trailer, and will not compute an adler32 check value. 476131377Stjr 477131377Stjr windowBits can also be greater than 15 for optional gzip encoding. Add 478131377Stjr 16 to windowBits to write a simple gzip header and trailer around the 479131377Stjr compressed data instead of a zlib wrapper. The gzip header will have no 480131377Stjr file name, no extra data, no comment, no modification time (set to zero), 481131377Stjr no header crc, and the operating system will be set to 255 (unknown). 482131377Stjr 48317651Speter The memLevel parameter specifies how much memory should be allocated 48417651Speter for the internal compression state. memLevel=1 uses minimum memory but 48517651Speter is slow and reduces compression ratio; memLevel=9 uses maximum memory 48617651Speter for optimal speed. The default value is 8. See zconf.h for total memory 48717651Speter usage as a function of windowBits and memLevel. 48817651Speter 48917651Speter The strategy parameter is used to tune the compression algorithm. Use the 49017651Speter value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a 491131377Stjr filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no 492131377Stjr string match), or Z_RLE to limit match distances to one (run-length 493131377Stjr encoding). Filtered data consists mostly of small values with a somewhat 494131377Stjr random distribution. In this case, the compression algorithm is tuned to 495131377Stjr compress them better. The effect of Z_FILTERED is to force more Huffman 496131377Stjr coding and less string matching; it is somewhat intermediate between 497131377Stjr Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as 498131377Stjr Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy 499131377Stjr parameter only affects the compression ratio but not the correctness of the 500131377Stjr compressed output even if it is not set appropriately. 50117651Speter 50233904Ssteve deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 50333904Ssteve memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid 50433904Ssteve method). msg is set to null if there is no error message. deflateInit2 does 50533904Ssteve not perform any compression: this will be done by deflate(). 50617651Speter*/ 507131377Stjr 50842468SpeterZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, 50942468Speter const Bytef *dictionary, 51042468Speter uInt dictLength)); 51117651Speter/* 51233904Ssteve Initializes the compression dictionary from the given byte sequence 51333904Ssteve without producing any compressed output. This function must be called 51442468Speter immediately after deflateInit, deflateInit2 or deflateReset, before any 51542468Speter call of deflate. The compressor and decompressor must use exactly the same 51617651Speter dictionary (see inflateSetDictionary). 51733904Ssteve 51817651Speter The dictionary should consist of strings (byte sequences) that are likely 51917651Speter to be encountered later in the data to be compressed, with the most commonly 52017651Speter used strings preferably put towards the end of the dictionary. Using a 52133904Ssteve dictionary is most useful when the data to be compressed is short and can be 52233904Ssteve predicted with good accuracy; the data can then be compressed better than 52333904Ssteve with the default empty dictionary. 52433904Ssteve 52533904Ssteve Depending on the size of the compression data structures selected by 52633904Ssteve deflateInit or deflateInit2, a part of the dictionary may in effect be 52733904Ssteve discarded, for example if the dictionary is larger than the window size in 52833904Ssteve deflate or deflate2. Thus the strings most likely to be useful should be 52933904Ssteve put at the end of the dictionary, not at the front. 53033904Ssteve 531131377Stjr Upon return of this function, strm->adler is set to the adler32 value 53217651Speter of the dictionary; the decompressor may later use this value to determine 533131377Stjr which dictionary has been used by the compressor. (The adler32 value 53417651Speter applies to the whole dictionary even if only a subset of the dictionary is 535131377Stjr actually used by the compressor.) If a raw deflate was requested, then the 536131377Stjr adler32 value is not computed and strm->adler is not set. 53717651Speter 53817651Speter deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a 53933904Ssteve parameter is invalid (such as NULL dictionary) or the stream state is 54033904Ssteve inconsistent (for example if deflate has already been called for this stream 54133904Ssteve or if the compression method is bsort). deflateSetDictionary does not 54233904Ssteve perform any compression: this will be done by deflate(). 54317651Speter*/ 54417651Speter 54542468SpeterZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, 54642468Speter z_streamp source)); 54717651Speter/* 54833904Ssteve Sets the destination stream as a complete copy of the source stream. 54917651Speter 55017651Speter This function can be useful when several compression strategies will be 55117651Speter tried, for example when there are several ways of pre-processing the input 55217651Speter data with a filter. The streams that will be discarded should then be freed 55317651Speter by calling deflateEnd. Note that deflateCopy duplicates the internal 55417651Speter compression state which can be quite large, so this strategy is slow and 55517651Speter can consume lots of memory. 55617651Speter 55717651Speter deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 55817651Speter enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 55917651Speter (such as zalloc being NULL). msg is left unchanged in both source and 56017651Speter destination. 56117651Speter*/ 56217651Speter 56342468SpeterZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); 56417651Speter/* 56517651Speter This function is equivalent to deflateEnd followed by deflateInit, 56617651Speter but does not free and reallocate all the internal compression state. 56717651Speter The stream will keep the same compression level and any other attributes 56817651Speter that may have been set by deflateInit2. 56917651Speter 57017651Speter deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 57117651Speter stream state was inconsistent (such as zalloc or state being NULL). 57217651Speter*/ 57317651Speter 57442468SpeterZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, 575131377Stjr int level, 576131377Stjr int strategy)); 57717651Speter/* 57833904Ssteve Dynamically update the compression level and compression strategy. The 57933904Ssteve interpretation of level and strategy is as in deflateInit2. This can be 58033904Ssteve used to switch between compression and straight copy of the input data, or 58133904Ssteve to switch to a different kind of input data requiring a different 58233904Ssteve strategy. If the compression level is changed, the input available so far 58333904Ssteve is compressed with the old level (and may be flushed); the new level will 58433904Ssteve take effect only at the next call of deflate(). 58517651Speter 58617651Speter Before the call of deflateParams, the stream state must be set as for 58717651Speter a call of deflate(), since the currently available input may have to 58817651Speter be compressed and flushed. In particular, strm->avail_out must be non-zero. 58917651Speter 59017651Speter deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source 59117651Speter stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR 59217651Speter if strm->avail_out was zero. 59317651Speter*/ 59417651Speter 595131377StjrZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, 596131377Stjr uLong sourceLen)); 597131377Stjr/* 598131377Stjr deflateBound() returns an upper bound on the compressed size after 599131377Stjr deflation of sourceLen bytes. It must be called after deflateInit() 600131377Stjr or deflateInit2(). This would be used to allocate an output buffer 601131377Stjr for deflation in a single pass, and so would be called before deflate(). 602131377Stjr*/ 603131377Stjr 604131377StjrZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, 605131377Stjr int bits, 606131377Stjr int value)); 607131377Stjr/* 608131377Stjr deflatePrime() inserts bits in the deflate output stream. The intent 609131377Stjr is that this function is used to start off the deflate output with the 610131377Stjr bits leftover from a previous deflate stream when appending to it. As such, 611131377Stjr this function can only be used for raw deflate, and must be used before the 612131377Stjr first deflate() call after a deflateInit2() or deflateReset(). bits must be 613131377Stjr less than or equal to 16, and that many of the least significant bits of 614131377Stjr value will be inserted in the output. 615131377Stjr 616131377Stjr deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source 617131377Stjr stream state was inconsistent. 618131377Stjr*/ 619131377Stjr 620131377Stjr/* 62142468SpeterZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, 62242468Speter int windowBits)); 62317651Speter 62433904Ssteve This is another version of inflateInit with an extra parameter. The 62533904Ssteve fields next_in, avail_in, zalloc, zfree and opaque must be initialized 62633904Ssteve before by the caller. 62717651Speter 62817651Speter The windowBits parameter is the base two logarithm of the maximum window 62917651Speter size (the size of the history buffer). It should be in the range 8..15 for 63033904Ssteve this version of the library. The default value is 15 if inflateInit is used 631131377Stjr instead. windowBits must be greater than or equal to the windowBits value 632131377Stjr provided to deflateInit2() while compressing, or it must be equal to 15 if 633131377Stjr deflateInit2() was not used. If a compressed stream with a larger window 634131377Stjr size is given as input, inflate() will return with the error code 635131377Stjr Z_DATA_ERROR instead of trying to allocate a larger window. 63617651Speter 637131377Stjr windowBits can also be -8..-15 for raw inflate. In this case, -windowBits 638131377Stjr determines the window size. inflate() will then process raw deflate data, 639131377Stjr not looking for a zlib or gzip header, not generating a check value, and not 640131377Stjr looking for any check values for comparison at the end of the stream. This 641131377Stjr is for use with other formats that use the deflate compressed data format 642131377Stjr such as zip. Those formats provide their own check values. If a custom 643131377Stjr format is developed using the raw deflate format for compressed data, it is 644131377Stjr recommended that a check value such as an adler32 or a crc32 be applied to 645131377Stjr the uncompressed data as is done in the zlib, gzip, and zip formats. For 646131377Stjr most applications, the zlib format should be used as is. Note that comments 647131377Stjr above on the use in deflateInit2() applies to the magnitude of windowBits. 648131377Stjr 649131377Stjr windowBits can also be greater than 15 for optional gzip decoding. Add 650131377Stjr 32 to windowBits to enable zlib and gzip decoding with automatic header 651131377Stjr detection, or add 16 to decode only the gzip format (the zlib format will 652131377Stjr return a Z_DATA_ERROR). 653131377Stjr 654131377Stjr inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 65533904Ssteve memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative 65633904Ssteve memLevel). msg is set to null if there is no error message. inflateInit2 65733904Ssteve does not perform any decompression apart from reading the zlib header if 65833904Ssteve present: this will be done by inflate(). (So next_in and avail_in may be 65933904Ssteve modified, but next_out and avail_out are unchanged.) 66017651Speter*/ 66117651Speter 66242468SpeterZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, 66342468Speter const Bytef *dictionary, 66442468Speter uInt dictLength)); 66517651Speter/* 66633904Ssteve Initializes the decompression dictionary from the given uncompressed byte 66733904Ssteve sequence. This function must be called immediately after a call of inflate 66833904Ssteve if this call returned Z_NEED_DICT. The dictionary chosen by the compressor 669131377Stjr can be determined from the adler32 value returned by this call of 67033904Ssteve inflate. The compressor and decompressor must use exactly the same 67117651Speter dictionary (see deflateSetDictionary). 67217651Speter 67317651Speter inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a 67417651Speter parameter is invalid (such as NULL dictionary) or the stream state is 67517651Speter inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the 676131377Stjr expected one (incorrect adler32 value). inflateSetDictionary does not 67717651Speter perform any decompression: this will be done by subsequent calls of 67817651Speter inflate(). 67917651Speter*/ 68017651Speter 68142468SpeterZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); 682131377Stjr/* 68333904Ssteve Skips invalid compressed data until a full flush point (see above the 68433904Ssteve description of deflate with Z_FULL_FLUSH) can be found, or until all 68533904Ssteve available input is skipped. No output is provided. 68617651Speter 68733904Ssteve inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR 68833904Ssteve if no more input was provided, Z_DATA_ERROR if no flush point has been found, 68917651Speter or Z_STREAM_ERROR if the stream structure was inconsistent. In the success 69017651Speter case, the application may save the current current value of total_in which 69117651Speter indicates where valid compressed data was found. In the error case, the 69217651Speter application may repeatedly call inflateSync, providing more input each time, 69317651Speter until success or end of the input data. 69417651Speter*/ 69517651Speter 696131377StjrZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, 697131377Stjr z_streamp source)); 698131377Stjr/* 699131377Stjr Sets the destination stream as a complete copy of the source stream. 700131377Stjr 701131377Stjr This function can be useful when randomly accessing a large stream. The 702131377Stjr first pass through the stream can periodically record the inflate state, 703131377Stjr allowing restarting inflate at those points when randomly accessing the 704131377Stjr stream. 705131377Stjr 706131377Stjr inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 707131377Stjr enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 708131377Stjr (such as zalloc being NULL). msg is left unchanged in both source and 709131377Stjr destination. 710131377Stjr*/ 711131377Stjr 71242468SpeterZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); 71317651Speter/* 71417651Speter This function is equivalent to inflateEnd followed by inflateInit, 71517651Speter but does not free and reallocate all the internal decompression state. 71617651Speter The stream will keep attributes that may have been set by inflateInit2. 71717651Speter 71817651Speter inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 71917651Speter stream state was inconsistent (such as zalloc or state being NULL). 72017651Speter*/ 72117651Speter 722131377Stjr/* 723131377StjrZEXTERN int ZEXPORT inflateBackInit OF((z_stream FAR *strm, int windowBits, 724131377Stjr unsigned char FAR *window)); 72517651Speter 726131377Stjr Initialize the internal stream state for decompression using inflateBack() 727131377Stjr calls. The fields zalloc, zfree and opaque in strm must be initialized 728131377Stjr before the call. If zalloc and zfree are Z_NULL, then the default library- 729131377Stjr derived memory allocation routines are used. windowBits is the base two 730131377Stjr logarithm of the window size, in the range 8..15. window is a caller 731131377Stjr supplied buffer of that size. Except for special applications where it is 732131377Stjr assured that deflate was used with small window sizes, windowBits must be 15 733131377Stjr and a 32K byte window must be supplied to be able to decompress general 734131377Stjr deflate streams. 735131377Stjr 736131377Stjr See inflateBack() for the usage of these routines. 737131377Stjr 738131377Stjr inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of 739131377Stjr the paramaters are invalid, Z_MEM_ERROR if the internal state could not 740131377Stjr be allocated, or Z_VERSION_ERROR if the version of the library does not 741131377Stjr match the version of the header file. 742131377Stjr*/ 743131377Stjr 744131377Stjrtypedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); 745131377Stjrtypedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); 746131377Stjr 747131377StjrZEXTERN int ZEXPORT inflateBack OF((z_stream FAR *strm, 748131377Stjr in_func in, void FAR *in_desc, 749131377Stjr out_func out, void FAR *out_desc)); 750131377Stjr/* 751131377Stjr inflateBack() does a raw inflate with a single call using a call-back 752131377Stjr interface for input and output. This is more efficient than inflate() for 753131377Stjr file i/o applications in that it avoids copying between the output and the 754131377Stjr sliding window by simply making the window itself the output buffer. This 755131377Stjr function trusts the application to not change the output buffer passed by 756131377Stjr the output function, at least until inflateBack() returns. 757131377Stjr 758131377Stjr inflateBackInit() must be called first to allocate the internal state 759131377Stjr and to initialize the state with the user-provided window buffer. 760131377Stjr inflateBack() may then be used multiple times to inflate a complete, raw 761131377Stjr deflate stream with each call. inflateBackEnd() is then called to free 762131377Stjr the allocated state. 763131377Stjr 764131377Stjr A raw deflate stream is one with no zlib or gzip header or trailer. 765131377Stjr This routine would normally be used in a utility that reads zip or gzip 766131377Stjr files and writes out uncompressed files. The utility would decode the 767131377Stjr header and process the trailer on its own, hence this routine expects 768131377Stjr only the raw deflate stream to decompress. This is different from the 769131377Stjr normal behavior of inflate(), which expects either a zlib or gzip header and 770131377Stjr trailer around the deflate stream. 771131377Stjr 772131377Stjr inflateBack() uses two subroutines supplied by the caller that are then 773131377Stjr called by inflateBack() for input and output. inflateBack() calls those 774131377Stjr routines until it reads a complete deflate stream and writes out all of the 775131377Stjr uncompressed data, or until it encounters an error. The function's 776131377Stjr parameters and return types are defined above in the in_func and out_func 777131377Stjr typedefs. inflateBack() will call in(in_desc, &buf) which should return the 778131377Stjr number of bytes of provided input, and a pointer to that input in buf. If 779131377Stjr there is no input available, in() must return zero--buf is ignored in that 780131377Stjr case--and inflateBack() will return a buffer error. inflateBack() will call 781131377Stjr out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() 782131377Stjr should return zero on success, or non-zero on failure. If out() returns 783131377Stjr non-zero, inflateBack() will return with an error. Neither in() nor out() 784131377Stjr are permitted to change the contents of the window provided to 785131377Stjr inflateBackInit(), which is also the buffer that out() uses to write from. 786131377Stjr The length written by out() will be at most the window size. Any non-zero 787131377Stjr amount of input may be provided by in(). 788131377Stjr 789131377Stjr For convenience, inflateBack() can be provided input on the first call by 790131377Stjr setting strm->next_in and strm->avail_in. If that input is exhausted, then 791131377Stjr in() will be called. Therefore strm->next_in must be initialized before 792131377Stjr calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called 793131377Stjr immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in 794131377Stjr must also be initialized, and then if strm->avail_in is not zero, input will 795131377Stjr initially be taken from strm->next_in[0 .. strm->avail_in - 1]. 796131377Stjr 797131377Stjr The in_desc and out_desc parameters of inflateBack() is passed as the 798131377Stjr first parameter of in() and out() respectively when they are called. These 799131377Stjr descriptors can be optionally used to pass any information that the caller- 800131377Stjr supplied in() and out() functions need to do their job. 801131377Stjr 802131377Stjr On return, inflateBack() will set strm->next_in and strm->avail_in to 803131377Stjr pass back any unused input that was provided by the last in() call. The 804131377Stjr return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR 805131377Stjr if in() or out() returned an error, Z_DATA_ERROR if there was a format 806131377Stjr error in the deflate stream (in which case strm->msg is set to indicate the 807131377Stjr nature of the error), or Z_STREAM_ERROR if the stream was not properly 808131377Stjr initialized. In the case of Z_BUF_ERROR, an input or output error can be 809131377Stjr distinguished using strm->next_in which will be Z_NULL only if in() returned 810131377Stjr an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to 811131377Stjr out() returning non-zero. (in() will always be called before out(), so 812131377Stjr strm->next_in is assured to be defined if out() returns non-zero.) Note 813131377Stjr that inflateBack() cannot return Z_OK. 814131377Stjr*/ 815131377Stjr 816131377StjrZEXTERN int ZEXPORT inflateBackEnd OF((z_stream FAR *strm)); 817131377Stjr/* 818131377Stjr All memory allocated by inflateBackInit() is freed. 819131377Stjr 820131377Stjr inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream 821131377Stjr state was inconsistent. 822131377Stjr*/ 823131377Stjr 824131377StjrZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); 825131377Stjr/* Return flags indicating compile-time options. 826131377Stjr 827131377Stjr Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: 828131377Stjr 1.0: size of uInt 829131377Stjr 3.2: size of uLong 830131377Stjr 5.4: size of voidpf (pointer) 831131377Stjr 7.6: size of z_off_t 832131377Stjr 833131377Stjr Compiler, assembler, and debug options: 834131377Stjr 8: DEBUG 835131377Stjr 9: ASMV or ASMINF -- use ASM code 836131377Stjr 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention 837131377Stjr 11: 0 (reserved) 838131377Stjr 839131377Stjr One-time table building (smaller code, but not thread-safe if true): 840131377Stjr 12: BUILDFIXED -- build static block decoding tables when needed 841131377Stjr 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed 842131377Stjr 14,15: 0 (reserved) 843131377Stjr 844131377Stjr Library content (indicates missing functionality): 845131377Stjr 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking 846131377Stjr deflate code when not needed) 847131377Stjr 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect 848131377Stjr and decode gzip streams (to avoid linking crc code) 849131377Stjr 18-19: 0 (reserved) 850131377Stjr 851131377Stjr Operation variations (changes in library functionality): 852131377Stjr 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate 853131377Stjr 21: FASTEST -- deflate algorithm with only one, lowest compression level 854131377Stjr 22,23: 0 (reserved) 855131377Stjr 856131377Stjr The sprintf variant used by gzprintf (zero is best): 857131377Stjr 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format 858131377Stjr 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! 859131377Stjr 26: 0 = returns value, 1 = void -- 1 means inferred string length returned 860131377Stjr 861131377Stjr Remainder: 862131377Stjr 27-31: 0 (reserved) 863131377Stjr */ 864131377Stjr 865131377Stjr 86617651Speter /* utility functions */ 86717651Speter 86817651Speter/* 86917651Speter The following utility functions are implemented on top of the 87017651Speter basic stream-oriented functions. To simplify the interface, some 87133904Ssteve default options are assumed (compression level and memory usage, 87217651Speter standard memory allocation functions). The source code of these 87317651Speter utility functions can easily be modified if you need special options. 87417651Speter*/ 87517651Speter 87642468SpeterZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, 87742468Speter const Bytef *source, uLong sourceLen)); 87817651Speter/* 87917651Speter Compresses the source buffer into the destination buffer. sourceLen is 88017651Speter the byte length of the source buffer. Upon entry, destLen is the total 881131377Stjr size of the destination buffer, which must be at least the value returned 882131377Stjr by compressBound(sourceLen). Upon exit, destLen is the actual size of the 88317651Speter compressed buffer. 88417651Speter This function can be used to compress a whole file at once if the 88517651Speter input file is mmap'ed. 88617651Speter compress returns Z_OK if success, Z_MEM_ERROR if there was not 88717651Speter enough memory, Z_BUF_ERROR if there was not enough room in the output 88817651Speter buffer. 88917651Speter*/ 89017651Speter 89142468SpeterZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, 89242468Speter const Bytef *source, uLong sourceLen, 89342468Speter int level)); 89417651Speter/* 89533904Ssteve Compresses the source buffer into the destination buffer. The level 89633904Ssteve parameter has the same meaning as in deflateInit. sourceLen is the byte 89733904Ssteve length of the source buffer. Upon entry, destLen is the total size of the 898131377Stjr destination buffer, which must be at least the value returned by 899131377Stjr compressBound(sourceLen). Upon exit, destLen is the actual size of the 900131377Stjr compressed buffer. 90133904Ssteve 90233904Ssteve compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 90333904Ssteve memory, Z_BUF_ERROR if there was not enough room in the output buffer, 90433904Ssteve Z_STREAM_ERROR if the level parameter is invalid. 90533904Ssteve*/ 90633904Ssteve 907131377StjrZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); 908131377Stjr/* 909131377Stjr compressBound() returns an upper bound on the compressed size after 910131377Stjr compress() or compress2() on sourceLen bytes. It would be used before 911131377Stjr a compress() or compress2() call to allocate the destination buffer. 912131377Stjr*/ 913131377Stjr 91442468SpeterZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, 91542468Speter const Bytef *source, uLong sourceLen)); 91633904Ssteve/* 91717651Speter Decompresses the source buffer into the destination buffer. sourceLen is 91817651Speter the byte length of the source buffer. Upon entry, destLen is the total 91917651Speter size of the destination buffer, which must be large enough to hold the 92017651Speter entire uncompressed data. (The size of the uncompressed data must have 92117651Speter been saved previously by the compressor and transmitted to the decompressor 92217651Speter by some mechanism outside the scope of this compression library.) 92317651Speter Upon exit, destLen is the actual size of the compressed buffer. 92417651Speter This function can be used to decompress a whole file at once if the 92517651Speter input file is mmap'ed. 92617651Speter 92717651Speter uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 92817651Speter enough memory, Z_BUF_ERROR if there was not enough room in the output 929131377Stjr buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. 93017651Speter*/ 93117651Speter 93217651Speter 93317651Spetertypedef voidp gzFile; 93417651Speter 93542468SpeterZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); 93617651Speter/* 93717651Speter Opens a gzip (.gz) file for reading or writing. The mode parameter 93817651Speter is as in fopen ("rb" or "wb") but can also include a compression level 93933904Ssteve ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for 940131377Stjr Huffman only compression as in "wb1h", or 'R' for run-length encoding 941131377Stjr as in "wb1R". (See the description of deflateInit2 for more information 942131377Stjr about the strategy parameter.) 94333904Ssteve 94433904Ssteve gzopen can be used to read a file which is not in gzip format; in this 94533904Ssteve case gzread will directly read from the file without decompression. 94633904Ssteve 94717651Speter gzopen returns NULL if the file could not be opened or if there was 94817651Speter insufficient memory to allocate the (de)compression state; errno 94917651Speter can be checked to distinguish the two cases (if errno is zero, the 95033904Ssteve zlib error is Z_MEM_ERROR). */ 95117651Speter 95242468SpeterZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); 95317651Speter/* 95417651Speter gzdopen() associates a gzFile with the file descriptor fd. File 95517651Speter descriptors are obtained from calls like open, dup, creat, pipe or 95617651Speter fileno (in the file has been previously opened with fopen). 95717651Speter The mode parameter is as in gzopen. 95817651Speter The next call of gzclose on the returned gzFile will also close the 95917651Speter file descriptor fd, just like fclose(fdopen(fd), mode) closes the file 96017651Speter descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). 96117651Speter gzdopen returns NULL if there was insufficient memory to allocate 96217651Speter the (de)compression state. 96317651Speter*/ 96417651Speter 96542468SpeterZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); 96617651Speter/* 96733904Ssteve Dynamically update the compression level or strategy. See the description 96833904Ssteve of deflateInit2 for the meaning of these parameters. 96933904Ssteve gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not 97033904Ssteve opened for writing. 97133904Ssteve*/ 97233904Ssteve 97342468SpeterZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); 97433904Ssteve/* 97517651Speter Reads the given number of uncompressed bytes from the compressed file. 97617651Speter If the input file was not in gzip format, gzread copies the given number 97717651Speter of bytes into the buffer. 97817651Speter gzread returns the number of uncompressed bytes actually read (0 for 97917651Speter end of file, -1 for error). */ 98017651Speter 981131377StjrZEXTERN int ZEXPORT gzwrite OF((gzFile file, 982131377Stjr voidpc buf, unsigned len)); 98317651Speter/* 98417651Speter Writes the given number of uncompressed bytes into the compressed file. 98517651Speter gzwrite returns the number of uncompressed bytes actually written 98617651Speter (0 in case of error). 98717651Speter*/ 98817651Speter 98942468SpeterZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); 99017651Speter/* 99133904Ssteve Converts, formats, and writes the args to the compressed file under 99233904Ssteve control of the format string, as in fprintf. gzprintf returns the number of 993131377Stjr uncompressed bytes actually written (0 in case of error). The number of 994131377Stjr uncompressed bytes written is limited to 4095. The caller should assure that 995131377Stjr this limit is not exceeded. If it is exceeded, then gzprintf() will return 996131377Stjr return an error (0) with nothing written. In this case, there may also be a 997131377Stjr buffer overflow with unpredictable consequences, which is possible only if 998131377Stjr zlib was compiled with the insecure functions sprintf() or vsprintf() 999131377Stjr because the secure snprintf() or vsnprintf() functions were not available. 100033904Ssteve*/ 100133904Ssteve 100242468SpeterZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); 100333904Ssteve/* 100433904Ssteve Writes the given null-terminated string to the compressed file, excluding 100533904Ssteve the terminating null character. 100633904Ssteve gzputs returns the number of characters written, or -1 in case of error. 100733904Ssteve*/ 100833904Ssteve 100942468SpeterZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); 101033904Ssteve/* 101133904Ssteve Reads bytes from the compressed file until len-1 characters are read, or 101233904Ssteve a newline character is read and transferred to buf, or an end-of-file 101333904Ssteve condition is encountered. The string is then terminated with a null 101433904Ssteve character. 101533904Ssteve gzgets returns buf, or Z_NULL in case of error. 101633904Ssteve*/ 101733904Ssteve 101842468SpeterZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); 101933904Ssteve/* 102033904Ssteve Writes c, converted to an unsigned char, into the compressed file. 102133904Ssteve gzputc returns the value that was written, or -1 in case of error. 102233904Ssteve*/ 102333904Ssteve 102442468SpeterZEXTERN int ZEXPORT gzgetc OF((gzFile file)); 102533904Ssteve/* 102633904Ssteve Reads one byte from the compressed file. gzgetc returns this byte 102733904Ssteve or -1 in case of end of file or error. 102833904Ssteve*/ 102933904Ssteve 1030131377StjrZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); 1031131377Stjr/* 1032131377Stjr Push one character back onto the stream to be read again later. 1033131377Stjr Only one character of push-back is allowed. gzungetc() returns the 1034131377Stjr character pushed, or -1 on failure. gzungetc() will fail if a 1035131377Stjr character has been pushed but not read yet, or if c is -1. The pushed 1036131377Stjr character will be discarded if the stream is repositioned with gzseek() 1037131377Stjr or gzrewind(). 1038131377Stjr*/ 1039131377Stjr 104042468SpeterZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); 104133904Ssteve/* 104217651Speter Flushes all pending output into the compressed file. The parameter 104317651Speter flush is as in the deflate() function. The return value is the zlib 104417651Speter error number (see function gzerror below). gzflush returns Z_OK if 104517651Speter the flush parameter is Z_FINISH and all output could be flushed. 104617651Speter gzflush should be called only when strictly necessary because it can 104717651Speter degrade compression. 104817651Speter*/ 104917651Speter 105042468SpeterZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, 1051131377Stjr z_off_t offset, int whence)); 1052131377Stjr/* 105342468Speter Sets the starting position for the next gzread or gzwrite on the 105442468Speter given compressed file. The offset represents a number of bytes in the 105533904Ssteve uncompressed data stream. The whence parameter is defined as in lseek(2); 105633904Ssteve the value SEEK_END is not supported. 105733904Ssteve If the file is opened for reading, this function is emulated but can be 105833904Ssteve extremely slow. If the file is opened for writing, only forward seeks are 105933904Ssteve supported; gzseek then compresses a sequence of zeroes up to the new 106033904Ssteve starting position. 106133904Ssteve 106233904Ssteve gzseek returns the resulting offset location as measured in bytes from 106333904Ssteve the beginning of the uncompressed stream, or -1 in case of error, in 106433904Ssteve particular if the file is opened for writing and the new starting position 106533904Ssteve would be before the current position. 106633904Ssteve*/ 106733904Ssteve 106842468SpeterZEXTERN int ZEXPORT gzrewind OF((gzFile file)); 106917651Speter/* 107033904Ssteve Rewinds the given file. This function is supported only for reading. 107133904Ssteve 107233904Ssteve gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) 107333904Ssteve*/ 107433904Ssteve 107542468SpeterZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); 107633904Ssteve/* 107733904Ssteve Returns the starting position for the next gzread or gzwrite on the 107833904Ssteve given compressed file. This position represents a number of bytes in the 107933904Ssteve uncompressed data stream. 108033904Ssteve 108133904Ssteve gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) 108233904Ssteve*/ 108333904Ssteve 108442468SpeterZEXTERN int ZEXPORT gzeof OF((gzFile file)); 108533904Ssteve/* 108633904Ssteve Returns 1 when EOF has previously been detected reading the given 108733904Ssteve input stream, otherwise zero. 108833904Ssteve*/ 108933904Ssteve 109042468SpeterZEXTERN int ZEXPORT gzclose OF((gzFile file)); 109133904Ssteve/* 109217651Speter Flushes all pending output if necessary, closes the compressed file 109317651Speter and deallocates all the (de)compression state. The return value is the zlib 109417651Speter error number (see function gzerror below). 109517651Speter*/ 109617651Speter 109742468SpeterZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); 109817651Speter/* 109917651Speter Returns the error message for the last error which occurred on the 110017651Speter given compressed file. errnum is set to zlib error number. If an 110117651Speter error occurred in the file system and not in the compression library, 110217651Speter errnum is set to Z_ERRNO and the application may consult errno 110317651Speter to get the exact error code. 110417651Speter*/ 110517651Speter 1106131377StjrZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); 1107131377Stjr/* 1108131377Stjr Clears the error and end-of-file flags for file. This is analogous to the 1109131377Stjr clearerr() function in stdio. This is useful for continuing to read a gzip 1110131377Stjr file that is being written concurrently. 1111131377Stjr*/ 1112131377Stjr 111317651Speter /* checksum functions */ 111417651Speter 111517651Speter/* 111617651Speter These functions are not related to compression but are exported 111717651Speter anyway because they might be useful in applications using the 111817651Speter compression library. 111917651Speter*/ 112017651Speter 112142468SpeterZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); 112217651Speter 112317651Speter/* 112417651Speter Update a running Adler-32 checksum with the bytes buf[0..len-1] and 112517651Speter return the updated checksum. If buf is NULL, this function returns 112617651Speter the required initial value for the checksum. 112717651Speter An Adler-32 checksum is almost as reliable as a CRC32 but can be computed 112817651Speter much faster. Usage example: 112917651Speter 113017651Speter uLong adler = adler32(0L, Z_NULL, 0); 113117651Speter 113217651Speter while (read_buffer(buffer, length) != EOF) { 113317651Speter adler = adler32(adler, buffer, length); 113417651Speter } 113517651Speter if (adler != original_adler) error(); 113617651Speter*/ 113717651Speter 113842468SpeterZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); 113917651Speter/* 114017651Speter Update a running crc with the bytes buf[0..len-1] and return the updated 114117651Speter crc. If buf is NULL, this function returns the required initial value 114217651Speter for the crc. Pre- and post-conditioning (one's complement) is performed 114317651Speter within this function so it shouldn't be done by the application. 114417651Speter Usage example: 114517651Speter 114617651Speter uLong crc = crc32(0L, Z_NULL, 0); 114717651Speter 114817651Speter while (read_buffer(buffer, length) != EOF) { 114917651Speter crc = crc32(crc, buffer, length); 115017651Speter } 115117651Speter if (crc != original_crc) error(); 115217651Speter*/ 115317651Speter 115417651Speter 115517651Speter /* various hacks, don't look :) */ 115617651Speter 115717651Speter/* deflateInit and inflateInit are macros to allow checking the zlib version 115817651Speter * and the compiler's view of z_stream: 115917651Speter */ 116042468SpeterZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, 116133904Ssteve const char *version, int stream_size)); 116242468SpeterZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, 116342468Speter const char *version, int stream_size)); 116442468SpeterZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, 116542468Speter int windowBits, int memLevel, 116642468Speter int strategy, const char *version, 116742468Speter int stream_size)); 116842468SpeterZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, 116942468Speter const char *version, int stream_size)); 1170131377StjrZEXTERN int ZEXPORT inflateBackInit_ OF((z_stream FAR *strm, int windowBits, 1171131377Stjr unsigned char FAR *window, 1172131377Stjr const char *version, 1173131377Stjr int stream_size)); 117417651Speter#define deflateInit(strm, level) \ 117517651Speter deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) 117617651Speter#define inflateInit(strm) \ 117717651Speter inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) 117817651Speter#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ 117917651Speter deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ 118033904Ssteve (strategy), ZLIB_VERSION, sizeof(z_stream)) 118117651Speter#define inflateInit2(strm, windowBits) \ 118217651Speter inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) 1183131377Stjr#define inflateBackInit(strm, windowBits, window) \ 1184131377Stjr inflateBackInit_((strm), (windowBits), (window), \ 1185131377Stjr ZLIB_VERSION, sizeof(z_stream)) 118617651Speter 118733904Ssteve 1188131377Stjr#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) 118917651Speter struct internal_state {int dummy;}; /* hack for buggy compilers */ 119017651Speter#endif 119117651Speter 119242468SpeterZEXTERN const char * ZEXPORT zError OF((int err)); 119342468SpeterZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z)); 119442468SpeterZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); 119517651Speter 119617651Speter#ifdef __cplusplus 119717651Speter} 119817651Speter#endif 119917651Speter 1200131377Stjr#endif /* ZLIB_H */ 1201