zlib.h revision 206924
117651Speter/* zlib.h -- interface of the 'zlib' general purpose compression library 2206924Sdelphij version 1.2.5, April 19th, 2010 317651Speter 4205471Sdelphij Copyright (C) 1995-2010 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 40206924Sdelphij#define ZLIB_VERSION "1.2.5" 41206924Sdelphij#define ZLIB_VERNUM 0x1250 42205471Sdelphij#define ZLIB_VER_MAJOR 1 43205471Sdelphij#define ZLIB_VER_MINOR 2 44206924Sdelphij#define ZLIB_VER_REVISION 5 45206924Sdelphij#define ZLIB_VER_SUBREVISION 0 4617651Speter 47131377Stjr/* 48205471Sdelphij The 'zlib' compression library provides in-memory compression and 49205471Sdelphij decompression functions, including integrity checks of the uncompressed data. 50205471Sdelphij This version of the library supports only one compression method (deflation) 51205471Sdelphij but other algorithms will be added later and will have the same stream 52205471Sdelphij interface. 5317651Speter 54205471Sdelphij Compression can be done in a single step if the buffers are large enough, 55205471Sdelphij or can be done by repeated calls of the compression function. In the latter 56205471Sdelphij case, the application must provide more input and/or consume the output 5717651Speter (providing more output space) before each call. 5817651Speter 59205471Sdelphij The compressed data format used by default by the in-memory functions is 60145474Skientzle the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped 61145474Skientzle around a deflate stream, which is itself documented in RFC 1951. 62131377Stjr 63205471Sdelphij The library also supports reading and writing files in gzip (.gz) format 64131377Stjr with an interface similar to that of stdio using the functions that start 65131377Stjr with "gz". The gzip format is different from the zlib format. gzip is a 66131377Stjr gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. 6733904Ssteve 68205471Sdelphij This library can optionally read and write gzip streams in memory as well. 69145474Skientzle 70205471Sdelphij The zlib format was designed to be compact and fast for use in memory 71131377Stjr and on communications channels. The gzip format was designed for single- 72131377Stjr file compression on file systems, has a larger header than zlib to maintain 73131377Stjr directory information, and uses a different, slower check method than zlib. 74131377Stjr 75205471Sdelphij The library does not install any signal handler. The decoder checks 76205471Sdelphij the consistency of the compressed data, so the library should never crash 77205471Sdelphij even in case of corrupted input. 7817651Speter*/ 7917651Speter 8017651Spetertypedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); 8117651Spetertypedef void (*free_func) OF((voidpf opaque, voidpf address)); 8217651Speter 8317651Speterstruct internal_state; 8417651Speter 8517651Spetertypedef struct z_stream_s { 8617651Speter Bytef *next_in; /* next input byte */ 8717651Speter uInt avail_in; /* number of bytes available at next_in */ 8817651Speter uLong total_in; /* total nb of input bytes read so far */ 8917651Speter 9017651Speter Bytef *next_out; /* next output byte should be put there */ 9117651Speter uInt avail_out; /* remaining free space at next_out */ 9217651Speter uLong total_out; /* total nb of bytes output so far */ 9317651Speter 9417651Speter char *msg; /* last error message, NULL if no error */ 9517651Speter struct internal_state FAR *state; /* not visible by applications */ 9617651Speter 9717651Speter alloc_func zalloc; /* used to allocate the internal state */ 9817651Speter free_func zfree; /* used to free the internal state */ 9917651Speter voidpf opaque; /* private data object passed to zalloc and zfree */ 10017651Speter 101157043Sdes int data_type; /* best guess about the data type: binary or text */ 10217651Speter uLong adler; /* adler32 value of the uncompressed data */ 10317651Speter uLong reserved; /* reserved for future use */ 10417651Speter} z_stream; 10517651Speter 10617651Spetertypedef z_stream FAR *z_streamp; 10717651Speter 10817651Speter/* 109157043Sdes gzip header information passed to and from zlib routines. See RFC 1952 110157043Sdes for more details on the meanings of these fields. 111157043Sdes*/ 112157043Sdestypedef struct gz_header_s { 113157043Sdes int text; /* true if compressed data believed to be text */ 114157043Sdes uLong time; /* modification time */ 115157043Sdes int xflags; /* extra flags (not used when writing a gzip file) */ 116157043Sdes int os; /* operating system */ 117157043Sdes Bytef *extra; /* pointer to extra field or Z_NULL if none */ 118157043Sdes uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ 119157043Sdes uInt extra_max; /* space at extra (only when reading header) */ 120157043Sdes Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ 121157043Sdes uInt name_max; /* space at name (only when reading header) */ 122157043Sdes Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ 123157043Sdes uInt comm_max; /* space at comment (only when reading header) */ 124157043Sdes int hcrc; /* true if there was or will be a header crc */ 125157043Sdes int done; /* true when done reading gzip header (not used 126157043Sdes when writing a gzip file) */ 127157043Sdes} gz_header; 128157043Sdes 129157043Sdestypedef gz_header FAR *gz_headerp; 130157043Sdes 131157043Sdes/* 132205471Sdelphij The application must update next_in and avail_in when avail_in has dropped 133205471Sdelphij to zero. It must update next_out and avail_out when avail_out has dropped 134205471Sdelphij to zero. The application must initialize zalloc, zfree and opaque before 135205471Sdelphij calling the init function. All other fields are set by the compression 136205471Sdelphij library and must not be updated by the application. 13717651Speter 138205471Sdelphij The opaque value provided by the application will be passed as the first 139205471Sdelphij parameter for calls of zalloc and zfree. This can be useful for custom 140205471Sdelphij memory management. The compression library attaches no meaning to the 14117651Speter opaque value. 14217651Speter 143205471Sdelphij zalloc must return Z_NULL if there is not enough memory for the object. 14433904Ssteve If zlib is used in a multi-threaded application, zalloc and zfree must be 14533904Ssteve thread safe. 14633904Ssteve 147205471Sdelphij On 16-bit systems, the functions zalloc and zfree must be able to allocate 148205471Sdelphij exactly 65536 bytes, but will not be required to allocate more than this if 149205471Sdelphij the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers 150205471Sdelphij returned by zalloc for objects of exactly 65536 bytes *must* have their 151205471Sdelphij offset normalized to zero. The default allocation function provided by this 152205471Sdelphij library ensures this (see zutil.c). To reduce memory requirements and avoid 153205471Sdelphij any allocation of 64K objects, at the expense of compression ratio, compile 154205471Sdelphij the library with -DMAX_WBITS=14 (see zconf.h). 15517651Speter 156205471Sdelphij The fields total_in and total_out can be used for statistics or progress 157205471Sdelphij reports. After compression, total_in holds the total size of the 158205471Sdelphij uncompressed data and may be saved for use in the decompressor (particularly 159205471Sdelphij if the decompressor wants to decompress everything in a single step). 16017651Speter*/ 16117651Speter 16217651Speter /* constants */ 16317651Speter 16417651Speter#define Z_NO_FLUSH 0 165205471Sdelphij#define Z_PARTIAL_FLUSH 1 16617651Speter#define Z_SYNC_FLUSH 2 16717651Speter#define Z_FULL_FLUSH 3 16817651Speter#define Z_FINISH 4 169131377Stjr#define Z_BLOCK 5 170205471Sdelphij#define Z_TREES 6 171131377Stjr/* Allowed flush values; see deflate() and inflate() below for details */ 17217651Speter 17317651Speter#define Z_OK 0 17417651Speter#define Z_STREAM_END 1 17517651Speter#define Z_NEED_DICT 2 17617651Speter#define Z_ERRNO (-1) 17717651Speter#define Z_STREAM_ERROR (-2) 17817651Speter#define Z_DATA_ERROR (-3) 17917651Speter#define Z_MEM_ERROR (-4) 18017651Speter#define Z_BUF_ERROR (-5) 18117651Speter#define Z_VERSION_ERROR (-6) 182205471Sdelphij/* Return codes for the compression/decompression functions. Negative values 183205471Sdelphij * are errors, positive values are used for special but normal events. 18417651Speter */ 18517651Speter 18617651Speter#define Z_NO_COMPRESSION 0 18717651Speter#define Z_BEST_SPEED 1 18817651Speter#define Z_BEST_COMPRESSION 9 18917651Speter#define Z_DEFAULT_COMPRESSION (-1) 19017651Speter/* compression levels */ 19117651Speter 19217651Speter#define Z_FILTERED 1 19317651Speter#define Z_HUFFMAN_ONLY 2 194131377Stjr#define Z_RLE 3 195157043Sdes#define Z_FIXED 4 19617651Speter#define Z_DEFAULT_STRATEGY 0 19717651Speter/* compression strategy; see deflateInit2() below for details */ 19817651Speter 19917651Speter#define Z_BINARY 0 200157043Sdes#define Z_TEXT 1 201157043Sdes#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ 20217651Speter#define Z_UNKNOWN 2 203131377Stjr/* Possible values of the data_type field (though see inflate()) */ 20417651Speter 20517651Speter#define Z_DEFLATED 8 20617651Speter/* The deflate compression method (the only one supported in this version) */ 20717651Speter 20817651Speter#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 20917651Speter 21017651Speter#define zlib_version zlibVersion() 21117651Speter/* for compatibility with versions < 1.0.2 */ 21217651Speter 213205471Sdelphij 21417651Speter /* basic functions */ 21517651Speter 21642468SpeterZEXTERN const char * ZEXPORT zlibVersion OF((void)); 21717651Speter/* The application can compare zlibVersion and ZLIB_VERSION for consistency. 218205471Sdelphij If the first character differs, the library code actually used is not 219205471Sdelphij compatible with the zlib.h header file used by the application. This check 220205471Sdelphij is automatically made by deflateInit and inflateInit. 22117651Speter */ 22217651Speter 223131377Stjr/* 22442468SpeterZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); 22517651Speter 226205471Sdelphij Initializes the internal stream state for compression. The fields 227205471Sdelphij zalloc, zfree and opaque must be initialized before by the caller. If 228205471Sdelphij zalloc and zfree are set to Z_NULL, deflateInit updates them to use default 229205471Sdelphij allocation functions. 23017651Speter 23117651Speter The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 232205471Sdelphij 1 gives best speed, 9 gives best compression, 0 gives no compression at all 233205471Sdelphij (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION 234205471Sdelphij requests a default compromise between speed and compression (currently 235205471Sdelphij equivalent to level 6). 23617651Speter 237205471Sdelphij deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 238205471Sdelphij memory, Z_STREAM_ERROR if level is not a valid compression level, or 23917651Speter Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible 240205471Sdelphij with the version assumed by the caller (ZLIB_VERSION). msg is set to null 241205471Sdelphij if there is no error message. deflateInit does not perform any compression: 242205471Sdelphij this will be done by deflate(). 24317651Speter*/ 24417651Speter 24517651Speter 24642468SpeterZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); 24717651Speter/* 24833904Ssteve deflate compresses as much data as possible, and stops when the input 249205471Sdelphij buffer becomes empty or the output buffer becomes full. It may introduce 250205471Sdelphij some output latency (reading input without producing any output) except when 25133904Ssteve forced to flush. 25217651Speter 253205471Sdelphij The detailed semantics are as follows. deflate performs one or both of the 25433904Ssteve following actions: 25533904Ssteve 25617651Speter - Compress more input starting at next_in and update next_in and avail_in 257205471Sdelphij accordingly. If not all input can be processed (because there is not 25817651Speter enough room in the output buffer), next_in and avail_in are updated and 25917651Speter processing will resume at this point for the next call of deflate(). 26017651Speter 26117651Speter - Provide more output starting at next_out and update next_out and avail_out 262205471Sdelphij accordingly. This action is forced if the parameter flush is non zero. 26317651Speter Forcing flush frequently degrades the compression ratio, so this parameter 264205471Sdelphij should be set only when necessary (in interactive applications). Some 265205471Sdelphij output may be provided even if flush is not set. 26617651Speter 267205471Sdelphij Before the call of deflate(), the application should ensure that at least 268205471Sdelphij one of the actions is possible, by providing more input and/or consuming more 269205471Sdelphij output, and updating avail_in or avail_out accordingly; avail_out should 270205471Sdelphij never be zero before the call. The application can consume the compressed 271205471Sdelphij output when it wants, for example when the output buffer is full (avail_out 272205471Sdelphij == 0), or after each call of deflate(). If deflate returns Z_OK and with 273205471Sdelphij zero avail_out, it must be called again after making room in the output 274205471Sdelphij buffer because there might be more output pending. 27517651Speter 276157043Sdes Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to 277205471Sdelphij decide how much data to accumulate before producing output, in order to 278157043Sdes maximize compression. 279157043Sdes 28033904Ssteve If the parameter flush is set to Z_SYNC_FLUSH, all pending output is 28133904Ssteve flushed to the output buffer and the output is aligned on a byte boundary, so 282205471Sdelphij that the decompressor can get all input data available so far. (In 283205471Sdelphij particular avail_in is zero after the call if enough output space has been 284205471Sdelphij provided before the call.) Flushing may degrade compression for some 285205471Sdelphij compression algorithms and so it should be used only when necessary. This 286205471Sdelphij completes the current deflate block and follows it with an empty stored block 287205471Sdelphij that is three bits plus filler bits to the next byte, followed by four bytes 288205471Sdelphij (00 00 ff ff). 28917651Speter 290205471Sdelphij If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the 291205471Sdelphij output buffer, but the output is not aligned to a byte boundary. All of the 292205471Sdelphij input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. 293205471Sdelphij This completes the current deflate block and follows it with an empty fixed 294205471Sdelphij codes block that is 10 bits long. This assures that enough bytes are output 295205471Sdelphij in order for the decompressor to finish the block before the empty fixed code 296205471Sdelphij block. 297205471Sdelphij 298205471Sdelphij If flush is set to Z_BLOCK, a deflate block is completed and emitted, as 299205471Sdelphij for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to 300205471Sdelphij seven bits of the current block are held to be written as the next byte after 301205471Sdelphij the next deflate block is completed. In this case, the decompressor may not 302205471Sdelphij be provided enough bits at this point in order to complete decompression of 303205471Sdelphij the data provided so far to the compressor. It may need to wait for the next 304205471Sdelphij block to be emitted. This is for advanced applications that need to control 305205471Sdelphij the emission of deflate blocks. 306205471Sdelphij 30733904Ssteve If flush is set to Z_FULL_FLUSH, all output is flushed as with 30833904Ssteve Z_SYNC_FLUSH, and the compression state is reset so that decompression can 30933904Ssteve restart from this point if previous compressed data has been damaged or if 310205471Sdelphij random access is desired. Using Z_FULL_FLUSH too often can seriously degrade 311157043Sdes compression. 31233904Ssteve 31333904Ssteve If deflate returns with avail_out == 0, this function must be called again 31433904Ssteve with the same value of the flush parameter and more output space (updated 31533904Ssteve avail_out), until the flush is complete (deflate returns with non-zero 316205471Sdelphij avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that 317131377Stjr avail_out is greater than six to avoid repeated flush markers due to 318131377Stjr avail_out == 0 on return. 31933904Ssteve 32017651Speter If the parameter flush is set to Z_FINISH, pending input is processed, 321205471Sdelphij pending output is flushed and deflate returns with Z_STREAM_END if there was 322205471Sdelphij enough output space; if deflate returns with Z_OK, this function must be 32317651Speter called again with Z_FINISH and more output space (updated avail_out) but no 324205471Sdelphij more input data, until it returns with Z_STREAM_END or an error. After 325205471Sdelphij deflate has returned Z_STREAM_END, the only possible operations on the stream 326205471Sdelphij are deflateReset or deflateEnd. 327131377Stjr 32817651Speter Z_FINISH can be used immediately after deflateInit if all the compression 329205471Sdelphij is to be done in a single step. In this case, avail_out must be at least the 330205471Sdelphij value returned by deflateBound (see below). If deflate does not return 33117651Speter Z_STREAM_END, then it must be called again as described above. 33217651Speter 33333904Ssteve deflate() sets strm->adler to the adler32 checksum of all input read 33433904Ssteve so far (that is, total_in bytes). 33533904Ssteve 336157043Sdes deflate() may update strm->data_type if it can make a good guess about 337205471Sdelphij the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered 338205471Sdelphij binary. This field is only for information purposes and does not affect the 339205471Sdelphij compression algorithm in any manner. 34017651Speter 34117651Speter deflate() returns Z_OK if some progress has been made (more input 34217651Speter processed or more output produced), Z_STREAM_END if all input has been 34317651Speter consumed and all output has been produced (only when flush is set to 34417651Speter Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 345205471Sdelphij if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible 346205471Sdelphij (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not 347131377Stjr fatal, and deflate() can be called again with more input and more output 348131377Stjr space to continue compressing. 34917651Speter*/ 35017651Speter 35117651Speter 35242468SpeterZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); 35317651Speter/* 35417651Speter All dynamically allocated data structures for this stream are freed. 355205471Sdelphij This function discards any unprocessed input and does not flush any pending 356205471Sdelphij output. 35717651Speter 35817651Speter deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 35917651Speter stream state was inconsistent, Z_DATA_ERROR if the stream was freed 360205471Sdelphij prematurely (some input or output was discarded). In the error case, msg 361205471Sdelphij may be set but then points to a static string (which must not be 36217651Speter deallocated). 36317651Speter*/ 36417651Speter 36517651Speter 366131377Stjr/* 36742468SpeterZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); 36817651Speter 369205471Sdelphij Initializes the internal stream state for decompression. The fields 37033904Ssteve next_in, avail_in, zalloc, zfree and opaque must be initialized before by 371205471Sdelphij the caller. If next_in is not Z_NULL and avail_in is large enough (the 372205471Sdelphij exact value depends on the compression method), inflateInit determines the 37333904Ssteve compression method from the zlib header and allocates all data structures 37433904Ssteve accordingly; otherwise the allocation will be deferred to the first call of 37533904Ssteve inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to 37633904Ssteve use default allocation functions. 37717651Speter 37833904Ssteve inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 37933904Ssteve memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 380205471Sdelphij version assumed by the caller, or Z_STREAM_ERROR if the parameters are 381205471Sdelphij invalid, such as a null pointer to the structure. msg is set to null if 382205471Sdelphij there is no error message. inflateInit does not perform any decompression 383205471Sdelphij apart from possibly reading the zlib header if present: actual decompression 384205471Sdelphij will be done by inflate(). (So next_in and avail_in may be modified, but 385205471Sdelphij next_out and avail_out are unused and unchanged.) The current implementation 386205471Sdelphij of inflateInit() does not process any header information -- that is deferred 387205471Sdelphij until inflate() is called. 38817651Speter*/ 38917651Speter 39017651Speter 39142468SpeterZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); 39217651Speter/* 39333904Ssteve inflate decompresses as much data as possible, and stops when the input 394205471Sdelphij buffer becomes empty or the output buffer becomes full. It may introduce 395131377Stjr some output latency (reading input without producing any output) except when 396131377Stjr forced to flush. 39717651Speter 398205471Sdelphij The detailed semantics are as follows. inflate performs one or both of the 39933904Ssteve following actions: 40033904Ssteve 40117651Speter - Decompress more input starting at next_in and update next_in and avail_in 402205471Sdelphij accordingly. If not all input can be processed (because there is not 403205471Sdelphij enough room in the output buffer), next_in is updated and processing will 404205471Sdelphij resume at this point for the next call of inflate(). 40517651Speter 40617651Speter - Provide more output starting at next_out and update next_out and avail_out 407205471Sdelphij accordingly. inflate() provides as much output as possible, until there is 408205471Sdelphij no more input data or no more space in the output buffer (see below about 409205471Sdelphij the flush parameter). 41017651Speter 411205471Sdelphij Before the call of inflate(), the application should ensure that at least 412205471Sdelphij one of the actions is possible, by providing more input and/or consuming more 413205471Sdelphij output, and updating the next_* and avail_* values accordingly. The 414205471Sdelphij application can consume the uncompressed output when it wants, for example 415205471Sdelphij when the output buffer is full (avail_out == 0), or after each call of 416205471Sdelphij inflate(). If inflate returns Z_OK and with zero avail_out, it must be 417205471Sdelphij called again after making room in the output buffer because there might be 418205471Sdelphij more output pending. 41917651Speter 420205471Sdelphij The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, 421205471Sdelphij Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much 422205471Sdelphij output as possible to the output buffer. Z_BLOCK requests that inflate() 423205471Sdelphij stop if and when it gets to the next deflate block boundary. When decoding 424205471Sdelphij the zlib or gzip format, this will cause inflate() to return immediately 425205471Sdelphij after the header and before the first block. When doing a raw inflate, 426205471Sdelphij inflate() will go ahead and process the first block, and will return when it 427205471Sdelphij gets to the end of that block, or when it runs out of data. 42817651Speter 429131377Stjr The Z_BLOCK option assists in appending to or combining deflate streams. 430131377Stjr Also to assist in this, on return inflate() will set strm->data_type to the 431205471Sdelphij number of unused bits in the last byte taken from strm->next_in, plus 64 if 432205471Sdelphij inflate() is currently decoding the last block in the deflate stream, plus 433205471Sdelphij 128 if inflate() returned immediately after decoding an end-of-block code or 434205471Sdelphij decoding the complete header up to just before the first byte of the deflate 435205471Sdelphij stream. The end-of-block will not be indicated until all of the uncompressed 436205471Sdelphij data from that block has been written to strm->next_out. The number of 437205471Sdelphij unused bits may in general be greater than seven, except when bit 7 of 438205471Sdelphij data_type is set, in which case the number of unused bits will be less than 439205471Sdelphij eight. data_type is set as noted here every time inflate() returns for all 440205471Sdelphij flush options, and so can be used to determine the amount of currently 441205471Sdelphij consumed input in bits. 442131377Stjr 443205471Sdelphij The Z_TREES option behaves as Z_BLOCK does, but it also returns when the 444205471Sdelphij end of each deflate block header is reached, before any actual data in that 445205471Sdelphij block is decoded. This allows the caller to determine the length of the 446205471Sdelphij deflate block header for later use in random access within a deflate block. 447205471Sdelphij 256 is added to the value of strm->data_type when inflate() returns 448205471Sdelphij immediately after reaching the end of the deflate block header. 449205471Sdelphij 45017651Speter inflate() should normally be called until it returns Z_STREAM_END or an 451205471Sdelphij error. However if all decompression is to be performed in a single step (a 452205471Sdelphij single call of inflate), the parameter flush should be set to Z_FINISH. In 453205471Sdelphij this case all pending input is processed and all pending output is flushed; 454205471Sdelphij avail_out must be large enough to hold all the uncompressed data. (The size 455205471Sdelphij of the uncompressed data may have been saved by the compressor for this 456205471Sdelphij purpose.) The next operation on this stream must be inflateEnd to deallocate 457205471Sdelphij the decompression state. The use of Z_FINISH is never required, but can be 458205471Sdelphij used to inform inflate that a faster approach may be used for the single 459205471Sdelphij inflate() call. 46017651Speter 461131377Stjr In this implementation, inflate() always flushes as much output as 462131377Stjr possible to the output buffer, and always uses the faster approach on the 463205471Sdelphij first call. So the only effect of the flush parameter in this implementation 464131377Stjr is on the return value of inflate(), as noted below, or when it returns early 465205471Sdelphij because Z_BLOCK or Z_TREES is used. 46633904Ssteve 467131377Stjr If a preset dictionary is needed after this call (see inflateSetDictionary 468145474Skientzle below), inflate sets strm->adler to the adler32 checksum of the dictionary 469131377Stjr chosen by the compressor and returns Z_NEED_DICT; otherwise it sets 470131377Stjr strm->adler to the adler32 checksum of all output produced so far (that is, 471131377Stjr total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described 472205471Sdelphij below. At the end of the stream, inflate() checks that its computed adler32 473131377Stjr checksum is equal to that saved by the compressor and returns Z_STREAM_END 474131377Stjr only if the checksum is correct. 475131377Stjr 476205471Sdelphij inflate() can decompress and check either zlib-wrapped or gzip-wrapped 477205471Sdelphij deflate data. The header type is detected automatically, if requested when 478205471Sdelphij initializing with inflateInit2(). Any information contained in the gzip 479205471Sdelphij header is not retained, so applications that need that information should 480205471Sdelphij instead use raw inflate, see inflateInit2() below, or inflateBack() and 481205471Sdelphij perform their own processing of the gzip header and trailer. 482131377Stjr 48333904Ssteve inflate() returns Z_OK if some progress has been made (more input processed 48433904Ssteve or more output produced), Z_STREAM_END if the end of the compressed data has 48533904Ssteve been reached and all uncompressed output has been produced, Z_NEED_DICT if a 48633904Ssteve preset dictionary is needed at this point, Z_DATA_ERROR if the input data was 487131377Stjr corrupted (input stream not conforming to the zlib format or incorrect check 488131377Stjr value), Z_STREAM_ERROR if the stream structure was inconsistent (for example 489205471Sdelphij next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory, 490131377Stjr Z_BUF_ERROR if no progress is possible or if there was not enough room in the 491205471Sdelphij output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and 492131377Stjr inflate() can be called again with more input and more output space to 493205471Sdelphij continue decompressing. If Z_DATA_ERROR is returned, the application may 494205471Sdelphij then call inflateSync() to look for a good compression block if a partial 495205471Sdelphij recovery of the data is desired. 49617651Speter*/ 49717651Speter 49817651Speter 49942468SpeterZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); 50017651Speter/* 50117651Speter All dynamically allocated data structures for this stream are freed. 502205471Sdelphij This function discards any unprocessed input and does not flush any pending 503205471Sdelphij output. 50417651Speter 50517651Speter inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state 506205471Sdelphij was inconsistent. In the error case, msg may be set but then points to a 50717651Speter static string (which must not be deallocated). 50817651Speter*/ 50917651Speter 510205471Sdelphij 51117651Speter /* Advanced functions */ 51217651Speter 51317651Speter/* 51417651Speter The following functions are needed only in some special applications. 51517651Speter*/ 51617651Speter 517131377Stjr/* 51842468SpeterZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, 51942468Speter int level, 52042468Speter int method, 52142468Speter int windowBits, 52242468Speter int memLevel, 52342468Speter int strategy)); 52417651Speter 525205471Sdelphij This is another version of deflateInit with more compression options. The 526205471Sdelphij fields next_in, zalloc, zfree and opaque must be initialized before by the 527205471Sdelphij caller. 52817651Speter 529205471Sdelphij The method parameter is the compression method. It must be Z_DEFLATED in 53033904Ssteve this version of the library. 53117651Speter 53217651Speter The windowBits parameter is the base two logarithm of the window size 533205471Sdelphij (the size of the history buffer). It should be in the range 8..15 for this 534205471Sdelphij version of the library. Larger values of this parameter result in better 535205471Sdelphij compression at the expense of memory usage. The default value is 15 if 53633904Ssteve deflateInit is used instead. 53717651Speter 538205471Sdelphij windowBits can also be -8..-15 for raw deflate. In this case, -windowBits 539205471Sdelphij determines the window size. deflate() will then generate raw deflate data 540131377Stjr with no zlib header or trailer, and will not compute an adler32 check value. 541131377Stjr 542205471Sdelphij windowBits can also be greater than 15 for optional gzip encoding. Add 543131377Stjr 16 to windowBits to write a simple gzip header and trailer around the 544205471Sdelphij compressed data instead of a zlib wrapper. The gzip header will have no 545205471Sdelphij file name, no extra data, no comment, no modification time (set to zero), no 546205471Sdelphij header crc, and the operating system will be set to 255 (unknown). If a 547145474Skientzle gzip stream is being written, strm->adler is a crc32 instead of an adler32. 548131377Stjr 54917651Speter The memLevel parameter specifies how much memory should be allocated 550205471Sdelphij for the internal compression state. memLevel=1 uses minimum memory but is 551205471Sdelphij slow and reduces compression ratio; memLevel=9 uses maximum memory for 552205471Sdelphij optimal speed. The default value is 8. See zconf.h for total memory usage 553205471Sdelphij as a function of windowBits and memLevel. 55417651Speter 555205471Sdelphij The strategy parameter is used to tune the compression algorithm. Use the 55617651Speter value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a 557131377Stjr filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no 558131377Stjr string match), or Z_RLE to limit match distances to one (run-length 559205471Sdelphij encoding). Filtered data consists mostly of small values with a somewhat 560205471Sdelphij random distribution. In this case, the compression algorithm is tuned to 561205471Sdelphij compress them better. The effect of Z_FILTERED is to force more Huffman 562131377Stjr coding and less string matching; it is somewhat intermediate between 563205471Sdelphij Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as 564205471Sdelphij fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The 565205471Sdelphij strategy parameter only affects the compression ratio but not the 566205471Sdelphij correctness of the compressed output even if it is not set appropriately. 567205471Sdelphij Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler 568205471Sdelphij decoder for special applications. 56917651Speter 570205471Sdelphij deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 571205471Sdelphij memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid 572205471Sdelphij method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is 573205471Sdelphij incompatible with the version assumed by the caller (ZLIB_VERSION). msg is 574205471Sdelphij set to null if there is no error message. deflateInit2 does not perform any 575205471Sdelphij compression: this will be done by deflate(). 57617651Speter*/ 577131377Stjr 57842468SpeterZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, 57942468Speter const Bytef *dictionary, 58042468Speter uInt dictLength)); 58117651Speter/* 58233904Ssteve Initializes the compression dictionary from the given byte sequence 583205471Sdelphij without producing any compressed output. This function must be called 584205471Sdelphij immediately after deflateInit, deflateInit2 or deflateReset, before any call 585205471Sdelphij of deflate. The compressor and decompressor must use exactly the same 58617651Speter dictionary (see inflateSetDictionary). 58733904Ssteve 58817651Speter The dictionary should consist of strings (byte sequences) that are likely 58917651Speter to be encountered later in the data to be compressed, with the most commonly 590205471Sdelphij used strings preferably put towards the end of the dictionary. Using a 59133904Ssteve dictionary is most useful when the data to be compressed is short and can be 59233904Ssteve predicted with good accuracy; the data can then be compressed better than 59333904Ssteve with the default empty dictionary. 59433904Ssteve 59533904Ssteve Depending on the size of the compression data structures selected by 59633904Ssteve deflateInit or deflateInit2, a part of the dictionary may in effect be 597205471Sdelphij discarded, for example if the dictionary is larger than the window size 598205471Sdelphij provided in deflateInit or deflateInit2. Thus the strings most likely to be 599205471Sdelphij useful should be put at the end of the dictionary, not at the front. In 600205471Sdelphij addition, the current implementation of deflate will use at most the window 601205471Sdelphij size minus 262 bytes of the provided dictionary. 60233904Ssteve 603131377Stjr Upon return of this function, strm->adler is set to the adler32 value 60417651Speter of the dictionary; the decompressor may later use this value to determine 605205471Sdelphij which dictionary has been used by the compressor. (The adler32 value 60617651Speter applies to the whole dictionary even if only a subset of the dictionary is 607131377Stjr actually used by the compressor.) If a raw deflate was requested, then the 608131377Stjr adler32 value is not computed and strm->adler is not set. 60917651Speter 61017651Speter deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a 611205471Sdelphij parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is 61233904Ssteve inconsistent (for example if deflate has already been called for this stream 613205471Sdelphij or if the compression method is bsort). deflateSetDictionary does not 61433904Ssteve perform any compression: this will be done by deflate(). 61517651Speter*/ 61617651Speter 61742468SpeterZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, 61842468Speter z_streamp source)); 61917651Speter/* 62033904Ssteve Sets the destination stream as a complete copy of the source stream. 62117651Speter 62217651Speter This function can be useful when several compression strategies will be 62317651Speter tried, for example when there are several ways of pre-processing the input 624205471Sdelphij data with a filter. The streams that will be discarded should then be freed 62517651Speter by calling deflateEnd. Note that deflateCopy duplicates the internal 626205471Sdelphij compression state which can be quite large, so this strategy is slow and can 627205471Sdelphij consume lots of memory. 62817651Speter 62917651Speter deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 63017651Speter enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 631205471Sdelphij (such as zalloc being Z_NULL). msg is left unchanged in both source and 63217651Speter destination. 63317651Speter*/ 63417651Speter 63542468SpeterZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); 63617651Speter/* 63717651Speter This function is equivalent to deflateEnd followed by deflateInit, 638205471Sdelphij but does not free and reallocate all the internal compression state. The 639205471Sdelphij stream will keep the same compression level and any other attributes that 640205471Sdelphij may have been set by deflateInit2. 64117651Speter 642205471Sdelphij deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 643205471Sdelphij stream state was inconsistent (such as zalloc or state being Z_NULL). 64417651Speter*/ 64517651Speter 64642468SpeterZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, 647131377Stjr int level, 648131377Stjr int strategy)); 64917651Speter/* 65033904Ssteve Dynamically update the compression level and compression strategy. The 65133904Ssteve interpretation of level and strategy is as in deflateInit2. This can be 65233904Ssteve used to switch between compression and straight copy of the input data, or 653205471Sdelphij to switch to a different kind of input data requiring a different strategy. 654205471Sdelphij If the compression level is changed, the input available so far is 655205471Sdelphij compressed with the old level (and may be flushed); the new level will take 656205471Sdelphij effect only at the next call of deflate(). 65717651Speter 65817651Speter Before the call of deflateParams, the stream state must be set as for 659205471Sdelphij a call of deflate(), since the currently available input may have to be 660205471Sdelphij compressed and flushed. In particular, strm->avail_out must be non-zero. 66117651Speter 66217651Speter deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source 663205471Sdelphij stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if 664205471Sdelphij strm->avail_out was zero. 66517651Speter*/ 66617651Speter 667157043SdesZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, 668157043Sdes int good_length, 669157043Sdes int max_lazy, 670157043Sdes int nice_length, 671157043Sdes int max_chain)); 672157043Sdes/* 673157043Sdes Fine tune deflate's internal compression parameters. This should only be 674157043Sdes used by someone who understands the algorithm used by zlib's deflate for 675157043Sdes searching for the best matching string, and even then only by the most 676157043Sdes fanatic optimizer trying to squeeze out the last compressed bit for their 677157043Sdes specific input data. Read the deflate.c source code for the meaning of the 678157043Sdes max_lazy, good_length, nice_length, and max_chain parameters. 679157043Sdes 680157043Sdes deflateTune() can be called after deflateInit() or deflateInit2(), and 681157043Sdes returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. 682157043Sdes */ 683157043Sdes 684131377StjrZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, 685131377Stjr uLong sourceLen)); 686131377Stjr/* 687131377Stjr deflateBound() returns an upper bound on the compressed size after 688205471Sdelphij deflation of sourceLen bytes. It must be called after deflateInit() or 689205471Sdelphij deflateInit2(), and after deflateSetHeader(), if used. This would be used 690205471Sdelphij to allocate an output buffer for deflation in a single pass, and so would be 691205471Sdelphij called before deflate(). 692131377Stjr*/ 693131377Stjr 694131377StjrZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, 695131377Stjr int bits, 696131377Stjr int value)); 697131377Stjr/* 698131377Stjr deflatePrime() inserts bits in the deflate output stream. The intent 699205471Sdelphij is that this function is used to start off the deflate output with the bits 700205471Sdelphij leftover from a previous deflate stream when appending to it. As such, this 701205471Sdelphij function can only be used for raw deflate, and must be used before the first 702205471Sdelphij deflate() call after a deflateInit2() or deflateReset(). bits must be less 703205471Sdelphij than or equal to 16, and that many of the least significant bits of value 704205471Sdelphij will be inserted in the output. 705131377Stjr 706205471Sdelphij deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source 707131377Stjr stream state was inconsistent. 708131377Stjr*/ 709131377Stjr 710157043SdesZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, 711157043Sdes gz_headerp head)); 712131377Stjr/* 713205471Sdelphij deflateSetHeader() provides gzip header information for when a gzip 714157043Sdes stream is requested by deflateInit2(). deflateSetHeader() may be called 715157043Sdes after deflateInit2() or deflateReset() and before the first call of 716157043Sdes deflate(). The text, time, os, extra field, name, and comment information 717157043Sdes in the provided gz_header structure are written to the gzip header (xflag is 718157043Sdes ignored -- the extra flags are set according to the compression level). The 719157043Sdes caller must assure that, if not Z_NULL, name and comment are terminated with 720157043Sdes a zero byte, and that if extra is not Z_NULL, that extra_len bytes are 721157043Sdes available there. If hcrc is true, a gzip header crc is included. Note that 722157043Sdes the current versions of the command-line version of gzip (up through version 723157043Sdes 1.3.x) do not support header crc's, and will report that it is a "multi-part 724157043Sdes gzip file" and give up. 725157043Sdes 726205471Sdelphij If deflateSetHeader is not used, the default gzip header has text false, 727157043Sdes the time set to zero, and os set to 255, with no extra, name, or comment 728157043Sdes fields. The gzip header is returned to the default state by deflateReset(). 729157043Sdes 730205471Sdelphij deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source 731157043Sdes stream state was inconsistent. 732157043Sdes*/ 733157043Sdes 734157043Sdes/* 73542468SpeterZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, 73642468Speter int windowBits)); 73717651Speter 738205471Sdelphij This is another version of inflateInit with an extra parameter. The 73933904Ssteve fields next_in, avail_in, zalloc, zfree and opaque must be initialized 74033904Ssteve before by the caller. 74117651Speter 74217651Speter The windowBits parameter is the base two logarithm of the maximum window 74317651Speter size (the size of the history buffer). It should be in the range 8..15 for 744205471Sdelphij this version of the library. The default value is 15 if inflateInit is used 745205471Sdelphij instead. windowBits must be greater than or equal to the windowBits value 746131377Stjr provided to deflateInit2() while compressing, or it must be equal to 15 if 747205471Sdelphij deflateInit2() was not used. If a compressed stream with a larger window 748131377Stjr size is given as input, inflate() will return with the error code 749131377Stjr Z_DATA_ERROR instead of trying to allocate a larger window. 75017651Speter 751205471Sdelphij windowBits can also be zero to request that inflate use the window size in 752205471Sdelphij the zlib header of the compressed stream. 753205471Sdelphij 754205471Sdelphij windowBits can also be -8..-15 for raw inflate. In this case, -windowBits 755205471Sdelphij determines the window size. inflate() will then process raw deflate data, 756131377Stjr not looking for a zlib or gzip header, not generating a check value, and not 757205471Sdelphij looking for any check values for comparison at the end of the stream. This 758131377Stjr is for use with other formats that use the deflate compressed data format 759205471Sdelphij such as zip. Those formats provide their own check values. If a custom 760131377Stjr format is developed using the raw deflate format for compressed data, it is 761131377Stjr recommended that a check value such as an adler32 or a crc32 be applied to 762131377Stjr the uncompressed data as is done in the zlib, gzip, and zip formats. For 763205471Sdelphij most applications, the zlib format should be used as is. Note that comments 764131377Stjr above on the use in deflateInit2() applies to the magnitude of windowBits. 765131377Stjr 766205471Sdelphij windowBits can also be greater than 15 for optional gzip decoding. Add 767131377Stjr 32 to windowBits to enable zlib and gzip decoding with automatic header 768131377Stjr detection, or add 16 to decode only the gzip format (the zlib format will 769205471Sdelphij return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a 770205471Sdelphij crc32 instead of an adler32. 771131377Stjr 772131377Stjr inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 773205471Sdelphij memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 774205471Sdelphij version assumed by the caller, or Z_STREAM_ERROR if the parameters are 775205471Sdelphij invalid, such as a null pointer to the structure. msg is set to null if 776205471Sdelphij there is no error message. inflateInit2 does not perform any decompression 777205471Sdelphij apart from possibly reading the zlib header if present: actual decompression 778205471Sdelphij will be done by inflate(). (So next_in and avail_in may be modified, but 779205471Sdelphij next_out and avail_out are unused and unchanged.) The current implementation 780205471Sdelphij of inflateInit2() does not process any header information -- that is 781205471Sdelphij deferred until inflate() is called. 78217651Speter*/ 78317651Speter 78442468SpeterZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, 78542468Speter const Bytef *dictionary, 78642468Speter uInt dictLength)); 78717651Speter/* 78833904Ssteve Initializes the decompression dictionary from the given uncompressed byte 789205471Sdelphij sequence. This function must be called immediately after a call of inflate, 790205471Sdelphij if that call returned Z_NEED_DICT. The dictionary chosen by the compressor 791157043Sdes can be determined from the adler32 value returned by that call of inflate. 792157043Sdes The compressor and decompressor must use exactly the same dictionary (see 793157043Sdes deflateSetDictionary). For raw inflate, this function can be called 794157043Sdes immediately after inflateInit2() or inflateReset() and before any call of 795157043Sdes inflate() to set the dictionary. The application must insure that the 796157043Sdes dictionary that was used for compression is provided. 79717651Speter 79817651Speter inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a 799205471Sdelphij parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is 80017651Speter inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the 801205471Sdelphij expected one (incorrect adler32 value). inflateSetDictionary does not 80217651Speter perform any decompression: this will be done by subsequent calls of 80317651Speter inflate(). 80417651Speter*/ 80517651Speter 80642468SpeterZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); 807131377Stjr/* 808205471Sdelphij Skips invalid compressed data until a full flush point (see above the 809205471Sdelphij description of deflate with Z_FULL_FLUSH) can be found, or until all 810205471Sdelphij available input is skipped. No output is provided. 81117651Speter 812205471Sdelphij inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR 813205471Sdelphij if no more input was provided, Z_DATA_ERROR if no flush point has been 814205471Sdelphij found, or Z_STREAM_ERROR if the stream structure was inconsistent. In the 815205471Sdelphij success case, the application may save the current current value of total_in 816205471Sdelphij which indicates where valid compressed data was found. In the error case, 817205471Sdelphij the application may repeatedly call inflateSync, providing more input each 818205471Sdelphij time, until success or end of the input data. 81917651Speter*/ 82017651Speter 821131377StjrZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, 822131377Stjr z_streamp source)); 823131377Stjr/* 824131377Stjr Sets the destination stream as a complete copy of the source stream. 825131377Stjr 826131377Stjr This function can be useful when randomly accessing a large stream. The 827131377Stjr first pass through the stream can periodically record the inflate state, 828131377Stjr allowing restarting inflate at those points when randomly accessing the 829131377Stjr stream. 830131377Stjr 831131377Stjr inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 832131377Stjr enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 833205471Sdelphij (such as zalloc being Z_NULL). msg is left unchanged in both source and 834131377Stjr destination. 835131377Stjr*/ 836131377Stjr 83742468SpeterZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); 83817651Speter/* 83917651Speter This function is equivalent to inflateEnd followed by inflateInit, 840205471Sdelphij but does not free and reallocate all the internal decompression state. The 841205471Sdelphij stream will keep attributes that may have been set by inflateInit2. 84217651Speter 843205471Sdelphij inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 844205471Sdelphij stream state was inconsistent (such as zalloc or state being Z_NULL). 84517651Speter*/ 84617651Speter 847205471SdelphijZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, 848205471Sdelphij int windowBits)); 849205471Sdelphij/* 850205471Sdelphij This function is the same as inflateReset, but it also permits changing 851205471Sdelphij the wrap and window size requests. The windowBits parameter is interpreted 852205471Sdelphij the same as it is for inflateInit2. 853205471Sdelphij 854205471Sdelphij inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source 855205471Sdelphij stream state was inconsistent (such as zalloc or state being Z_NULL), or if 856205471Sdelphij the windowBits parameter is invalid. 857205471Sdelphij*/ 858205471Sdelphij 859157043SdesZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, 860157043Sdes int bits, 861157043Sdes int value)); 862131377Stjr/* 863157043Sdes This function inserts bits in the inflate input stream. The intent is 864205471Sdelphij that this function is used to start inflating at a bit position in the 865205471Sdelphij middle of a byte. The provided bits will be used before any bytes are used 866205471Sdelphij from next_in. This function should only be used with raw inflate, and 867205471Sdelphij should be used before the first inflate() call after inflateInit2() or 868205471Sdelphij inflateReset(). bits must be less than or equal to 16, and that many of the 869205471Sdelphij least significant bits of value will be inserted in the input. 870157043Sdes 871205471Sdelphij If bits is negative, then the input stream bit buffer is emptied. Then 872205471Sdelphij inflatePrime() can be called again to put bits in the buffer. This is used 873205471Sdelphij to clear out bits leftover after feeding inflate a block description prior 874205471Sdelphij to feeding inflate codes. 875205471Sdelphij 876205471Sdelphij inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source 877157043Sdes stream state was inconsistent. 878157043Sdes*/ 879157043Sdes 880205471SdelphijZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); 881205471Sdelphij/* 882205471Sdelphij This function returns two values, one in the lower 16 bits of the return 883205471Sdelphij value, and the other in the remaining upper bits, obtained by shifting the 884205471Sdelphij return value down 16 bits. If the upper value is -1 and the lower value is 885205471Sdelphij zero, then inflate() is currently decoding information outside of a block. 886205471Sdelphij If the upper value is -1 and the lower value is non-zero, then inflate is in 887205471Sdelphij the middle of a stored block, with the lower value equaling the number of 888205471Sdelphij bytes from the input remaining to copy. If the upper value is not -1, then 889205471Sdelphij it is the number of bits back from the current bit position in the input of 890205471Sdelphij the code (literal or length/distance pair) currently being processed. In 891205471Sdelphij that case the lower value is the number of bytes already emitted for that 892205471Sdelphij code. 893205471Sdelphij 894205471Sdelphij A code is being processed if inflate is waiting for more input to complete 895205471Sdelphij decoding of the code, or if it has completed decoding but is waiting for 896205471Sdelphij more output space to write the literal or match data. 897205471Sdelphij 898205471Sdelphij inflateMark() is used to mark locations in the input data for random 899205471Sdelphij access, which may be at bit positions, and to note those cases where the 900205471Sdelphij output of a code may span boundaries of random access blocks. The current 901205471Sdelphij location in the input stream can be determined from avail_in and data_type 902205471Sdelphij as noted in the description for the Z_BLOCK flush parameter for inflate. 903205471Sdelphij 904205471Sdelphij inflateMark returns the value noted above or -1 << 16 if the provided 905205471Sdelphij source stream state was inconsistent. 906205471Sdelphij*/ 907205471Sdelphij 908157043SdesZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, 909157043Sdes gz_headerp head)); 910157043Sdes/* 911205471Sdelphij inflateGetHeader() requests that gzip header information be stored in the 912157043Sdes provided gz_header structure. inflateGetHeader() may be called after 913157043Sdes inflateInit2() or inflateReset(), and before the first call of inflate(). 914157043Sdes As inflate() processes the gzip stream, head->done is zero until the header 915157043Sdes is completed, at which time head->done is set to one. If a zlib stream is 916157043Sdes being decoded, then head->done is set to -1 to indicate that there will be 917205471Sdelphij no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be 918205471Sdelphij used to force inflate() to return immediately after header processing is 919205471Sdelphij complete and before any actual data is decompressed. 920157043Sdes 921205471Sdelphij The text, time, xflags, and os fields are filled in with the gzip header 922157043Sdes contents. hcrc is set to true if there is a header CRC. (The header CRC 923205471Sdelphij was valid if done is set to one.) If extra is not Z_NULL, then extra_max 924157043Sdes contains the maximum number of bytes to write to extra. Once done is true, 925157043Sdes extra_len contains the actual extra field length, and extra contains the 926157043Sdes extra field, or that field truncated if extra_max is less than extra_len. 927157043Sdes If name is not Z_NULL, then up to name_max characters are written there, 928157043Sdes terminated with a zero unless the length is greater than name_max. If 929157043Sdes comment is not Z_NULL, then up to comm_max characters are written there, 930205471Sdelphij terminated with a zero unless the length is greater than comm_max. When any 931205471Sdelphij of extra, name, or comment are not Z_NULL and the respective field is not 932205471Sdelphij present in the header, then that field is set to Z_NULL to signal its 933157043Sdes absence. This allows the use of deflateSetHeader() with the returned 934157043Sdes structure to duplicate the header. However if those fields are set to 935157043Sdes allocated memory, then the application will need to save those pointers 936157043Sdes elsewhere so that they can be eventually freed. 937157043Sdes 938205471Sdelphij If inflateGetHeader is not used, then the header information is simply 939157043Sdes discarded. The header is always checked for validity, including the header 940157043Sdes CRC if present. inflateReset() will reset the process to discard the header 941157043Sdes information. The application would need to call inflateGetHeader() again to 942157043Sdes retrieve the header from the next gzip stream. 943157043Sdes 944205471Sdelphij inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source 945157043Sdes stream state was inconsistent. 946157043Sdes*/ 947157043Sdes 948157043Sdes/* 949157043SdesZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, 950131377Stjr unsigned char FAR *window)); 95117651Speter 952131377Stjr Initialize the internal stream state for decompression using inflateBack() 953131377Stjr calls. The fields zalloc, zfree and opaque in strm must be initialized 954131377Stjr before the call. If zalloc and zfree are Z_NULL, then the default library- 955131377Stjr derived memory allocation routines are used. windowBits is the base two 956131377Stjr logarithm of the window size, in the range 8..15. window is a caller 957131377Stjr supplied buffer of that size. Except for special applications where it is 958131377Stjr assured that deflate was used with small window sizes, windowBits must be 15 959131377Stjr and a 32K byte window must be supplied to be able to decompress general 960131377Stjr deflate streams. 961131377Stjr 962131377Stjr See inflateBack() for the usage of these routines. 963131377Stjr 964131377Stjr inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of 965205471Sdelphij the paramaters are invalid, Z_MEM_ERROR if the internal state could not be 966205471Sdelphij allocated, or Z_VERSION_ERROR if the version of the library does not match 967205471Sdelphij the version of the header file. 968131377Stjr*/ 969131377Stjr 970131377Stjrtypedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); 971131377Stjrtypedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); 972131377Stjr 973157043SdesZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, 974131377Stjr in_func in, void FAR *in_desc, 975131377Stjr out_func out, void FAR *out_desc)); 976131377Stjr/* 977131377Stjr inflateBack() does a raw inflate with a single call using a call-back 978131377Stjr interface for input and output. This is more efficient than inflate() for 979131377Stjr file i/o applications in that it avoids copying between the output and the 980131377Stjr sliding window by simply making the window itself the output buffer. This 981131377Stjr function trusts the application to not change the output buffer passed by 982131377Stjr the output function, at least until inflateBack() returns. 983131377Stjr 984131377Stjr inflateBackInit() must be called first to allocate the internal state 985131377Stjr and to initialize the state with the user-provided window buffer. 986131377Stjr inflateBack() may then be used multiple times to inflate a complete, raw 987205471Sdelphij deflate stream with each call. inflateBackEnd() is then called to free the 988205471Sdelphij allocated state. 989131377Stjr 990131377Stjr A raw deflate stream is one with no zlib or gzip header or trailer. 991131377Stjr This routine would normally be used in a utility that reads zip or gzip 992131377Stjr files and writes out uncompressed files. The utility would decode the 993205471Sdelphij header and process the trailer on its own, hence this routine expects only 994205471Sdelphij the raw deflate stream to decompress. This is different from the normal 995205471Sdelphij behavior of inflate(), which expects either a zlib or gzip header and 996131377Stjr trailer around the deflate stream. 997131377Stjr 998131377Stjr inflateBack() uses two subroutines supplied by the caller that are then 999131377Stjr called by inflateBack() for input and output. inflateBack() calls those 1000131377Stjr routines until it reads a complete deflate stream and writes out all of the 1001131377Stjr uncompressed data, or until it encounters an error. The function's 1002131377Stjr parameters and return types are defined above in the in_func and out_func 1003131377Stjr typedefs. inflateBack() will call in(in_desc, &buf) which should return the 1004131377Stjr number of bytes of provided input, and a pointer to that input in buf. If 1005131377Stjr there is no input available, in() must return zero--buf is ignored in that 1006131377Stjr case--and inflateBack() will return a buffer error. inflateBack() will call 1007131377Stjr out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() 1008131377Stjr should return zero on success, or non-zero on failure. If out() returns 1009131377Stjr non-zero, inflateBack() will return with an error. Neither in() nor out() 1010131377Stjr are permitted to change the contents of the window provided to 1011131377Stjr inflateBackInit(), which is also the buffer that out() uses to write from. 1012131377Stjr The length written by out() will be at most the window size. Any non-zero 1013131377Stjr amount of input may be provided by in(). 1014131377Stjr 1015131377Stjr For convenience, inflateBack() can be provided input on the first call by 1016131377Stjr setting strm->next_in and strm->avail_in. If that input is exhausted, then 1017131377Stjr in() will be called. Therefore strm->next_in must be initialized before 1018131377Stjr calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called 1019131377Stjr immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in 1020131377Stjr must also be initialized, and then if strm->avail_in is not zero, input will 1021205471Sdelphij initially be taken from strm->next_in[0 .. strm->avail_in - 1]. 1022131377Stjr 1023131377Stjr The in_desc and out_desc parameters of inflateBack() is passed as the 1024131377Stjr first parameter of in() and out() respectively when they are called. These 1025131377Stjr descriptors can be optionally used to pass any information that the caller- 1026131377Stjr supplied in() and out() functions need to do their job. 1027131377Stjr 1028131377Stjr On return, inflateBack() will set strm->next_in and strm->avail_in to 1029131377Stjr pass back any unused input that was provided by the last in() call. The 1030131377Stjr return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR 1031205471Sdelphij if in() or out() returned an error, Z_DATA_ERROR if there was a format error 1032205471Sdelphij in the deflate stream (in which case strm->msg is set to indicate the nature 1033205471Sdelphij of the error), or Z_STREAM_ERROR if the stream was not properly initialized. 1034205471Sdelphij In the case of Z_BUF_ERROR, an input or output error can be distinguished 1035205471Sdelphij using strm->next_in which will be Z_NULL only if in() returned an error. If 1036205471Sdelphij strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning 1037205471Sdelphij non-zero. (in() will always be called before out(), so strm->next_in is 1038205471Sdelphij assured to be defined if out() returns non-zero.) Note that inflateBack() 1039205471Sdelphij cannot return Z_OK. 1040131377Stjr*/ 1041131377Stjr 1042157043SdesZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); 1043131377Stjr/* 1044131377Stjr All memory allocated by inflateBackInit() is freed. 1045131377Stjr 1046131377Stjr inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream 1047131377Stjr state was inconsistent. 1048131377Stjr*/ 1049131377Stjr 1050131377StjrZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); 1051131377Stjr/* Return flags indicating compile-time options. 1052131377Stjr 1053131377Stjr Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: 1054131377Stjr 1.0: size of uInt 1055131377Stjr 3.2: size of uLong 1056131377Stjr 5.4: size of voidpf (pointer) 1057131377Stjr 7.6: size of z_off_t 1058131377Stjr 1059131377Stjr Compiler, assembler, and debug options: 1060131377Stjr 8: DEBUG 1061131377Stjr 9: ASMV or ASMINF -- use ASM code 1062131377Stjr 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention 1063131377Stjr 11: 0 (reserved) 1064131377Stjr 1065131377Stjr One-time table building (smaller code, but not thread-safe if true): 1066131377Stjr 12: BUILDFIXED -- build static block decoding tables when needed 1067131377Stjr 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed 1068131377Stjr 14,15: 0 (reserved) 1069131377Stjr 1070131377Stjr Library content (indicates missing functionality): 1071131377Stjr 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking 1072131377Stjr deflate code when not needed) 1073131377Stjr 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect 1074131377Stjr and decode gzip streams (to avoid linking crc code) 1075131377Stjr 18-19: 0 (reserved) 1076131377Stjr 1077131377Stjr Operation variations (changes in library functionality): 1078131377Stjr 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate 1079131377Stjr 21: FASTEST -- deflate algorithm with only one, lowest compression level 1080131377Stjr 22,23: 0 (reserved) 1081131377Stjr 1082131377Stjr The sprintf variant used by gzprintf (zero is best): 1083131377Stjr 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format 1084131377Stjr 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! 1085131377Stjr 26: 0 = returns value, 1 = void -- 1 means inferred string length returned 1086131377Stjr 1087131377Stjr Remainder: 1088131377Stjr 27-31: 0 (reserved) 1089131377Stjr */ 1090131377Stjr 1091131377Stjr 109217651Speter /* utility functions */ 109317651Speter 109417651Speter/* 1095205471Sdelphij The following utility functions are implemented on top of the basic 1096205471Sdelphij stream-oriented functions. To simplify the interface, some default options 1097205471Sdelphij are assumed (compression level and memory usage, standard memory allocation 1098205471Sdelphij functions). The source code of these utility functions can be modified if 1099205471Sdelphij you need special options. 110017651Speter*/ 110117651Speter 110242468SpeterZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, 110342468Speter const Bytef *source, uLong sourceLen)); 110417651Speter/* 110517651Speter Compresses the source buffer into the destination buffer. sourceLen is 1106205471Sdelphij the byte length of the source buffer. Upon entry, destLen is the total size 1107205471Sdelphij of the destination buffer, which must be at least the value returned by 1108205471Sdelphij compressBound(sourceLen). Upon exit, destLen is the actual size of the 110917651Speter compressed buffer. 1110205471Sdelphij 111117651Speter compress returns Z_OK if success, Z_MEM_ERROR if there was not 111217651Speter enough memory, Z_BUF_ERROR if there was not enough room in the output 111317651Speter buffer. 111417651Speter*/ 111517651Speter 111642468SpeterZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, 111742468Speter const Bytef *source, uLong sourceLen, 111842468Speter int level)); 111917651Speter/* 1120205471Sdelphij Compresses the source buffer into the destination buffer. The level 112133904Ssteve parameter has the same meaning as in deflateInit. sourceLen is the byte 1122205471Sdelphij length of the source buffer. Upon entry, destLen is the total size of the 1123131377Stjr destination buffer, which must be at least the value returned by 1124205471Sdelphij compressBound(sourceLen). Upon exit, destLen is the actual size of the 1125131377Stjr compressed buffer. 112633904Ssteve 112733904Ssteve compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 112833904Ssteve memory, Z_BUF_ERROR if there was not enough room in the output buffer, 112933904Ssteve Z_STREAM_ERROR if the level parameter is invalid. 113033904Ssteve*/ 113133904Ssteve 1132131377StjrZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); 1133131377Stjr/* 1134131377Stjr compressBound() returns an upper bound on the compressed size after 1135205471Sdelphij compress() or compress2() on sourceLen bytes. It would be used before a 1136205471Sdelphij compress() or compress2() call to allocate the destination buffer. 1137131377Stjr*/ 1138131377Stjr 113942468SpeterZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, 114042468Speter const Bytef *source, uLong sourceLen)); 114133904Ssteve/* 114217651Speter Decompresses the source buffer into the destination buffer. sourceLen is 1143205471Sdelphij the byte length of the source buffer. Upon entry, destLen is the total size 1144205471Sdelphij of the destination buffer, which must be large enough to hold the entire 1145205471Sdelphij uncompressed data. (The size of the uncompressed data must have been saved 1146205471Sdelphij previously by the compressor and transmitted to the decompressor by some 1147205471Sdelphij mechanism outside the scope of this compression library.) Upon exit, destLen 1148205471Sdelphij is the actual size of the uncompressed buffer. 114917651Speter 115017651Speter uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 115117651Speter enough memory, Z_BUF_ERROR if there was not enough room in the output 1152131377Stjr buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. 115317651Speter*/ 115417651Speter 115517651Speter 1156205471Sdelphij /* gzip file access functions */ 115717651Speter 115817651Speter/* 1159205471Sdelphij This library supports reading and writing files in gzip (.gz) format with 1160205471Sdelphij an interface similar to that of stdio, using the functions that start with 1161205471Sdelphij "gz". The gzip format is different from the zlib format. gzip is a gzip 1162205471Sdelphij wrapper, documented in RFC 1952, wrapped around a deflate stream. 1163205471Sdelphij*/ 116433904Ssteve 1165205471Sdelphijtypedef voidp gzFile; /* opaque gzip file descriptor */ 1166205471Sdelphij 1167205471Sdelphij/* 1168205471SdelphijZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); 1169205471Sdelphij 1170205471Sdelphij Opens a gzip (.gz) file for reading or writing. The mode parameter is as 1171205471Sdelphij in fopen ("rb" or "wb") but can also include a compression level ("wb9") or 1172205471Sdelphij a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only 1173205471Sdelphij compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' 1174205471Sdelphij for fixed code compression as in "wb9F". (See the description of 1175205471Sdelphij deflateInit2 for more information about the strategy parameter.) Also "a" 1176205471Sdelphij can be used instead of "w" to request that the gzip stream that will be 1177205471Sdelphij written be appended to the file. "+" will result in an error, since reading 1178205471Sdelphij and writing to the same gzip file is not supported. 1179205471Sdelphij 118033904Ssteve gzopen can be used to read a file which is not in gzip format; in this 118133904Ssteve case gzread will directly read from the file without decompression. 118233904Ssteve 1183205471Sdelphij gzopen returns NULL if the file could not be opened, if there was 1184205471Sdelphij insufficient memory to allocate the gzFile state, or if an invalid mode was 1185205471Sdelphij specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). 1186205471Sdelphij errno can be checked to determine if the reason gzopen failed was that the 1187205471Sdelphij file could not be opened. 1188205471Sdelphij*/ 118917651Speter 1190205471SdelphijZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); 119117651Speter/* 1192205471Sdelphij gzdopen associates a gzFile with the file descriptor fd. File descriptors 1193205471Sdelphij are obtained from calls like open, dup, creat, pipe or fileno (if the file 1194205471Sdelphij has been previously opened with fopen). The mode parameter is as in gzopen. 1195205471Sdelphij 1196205471Sdelphij The next call of gzclose on the returned gzFile will also close the file 1197205471Sdelphij descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor 1198205471Sdelphij fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, 1199205471Sdelphij mode);. The duplicated descriptor should be saved to avoid a leak, since 1200205471Sdelphij gzdopen does not close fd if it fails. 1201205471Sdelphij 1202205471Sdelphij gzdopen returns NULL if there was insufficient memory to allocate the 1203205471Sdelphij gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not 1204205471Sdelphij provided, or '+' was provided), or if fd is -1. The file descriptor is not 1205205471Sdelphij used until the next gz* read, write, seek, or close operation, so gzdopen 1206205471Sdelphij will not detect if fd is invalid (unless fd is -1). 120717651Speter*/ 120817651Speter 1209205471SdelphijZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); 1210205471Sdelphij/* 1211205471Sdelphij Set the internal buffer size used by this library's functions. The 1212205471Sdelphij default buffer size is 8192 bytes. This function must be called after 1213205471Sdelphij gzopen() or gzdopen(), and before any other calls that read or write the 1214205471Sdelphij file. The buffer memory allocation is always deferred to the first read or 1215205471Sdelphij write. Two buffers are allocated, either both of the specified size when 1216205471Sdelphij writing, or one of the specified size and the other twice that size when 1217205471Sdelphij reading. A larger buffer size of, for example, 64K or 128K bytes will 1218205471Sdelphij noticeably increase the speed of decompression (reading). 1219205471Sdelphij 1220205471Sdelphij The new buffer size also affects the maximum length for gzprintf(). 1221205471Sdelphij 1222205471Sdelphij gzbuffer() returns 0 on success, or -1 on failure, such as being called 1223205471Sdelphij too late. 1224205471Sdelphij*/ 1225205471Sdelphij 122642468SpeterZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); 122717651Speter/* 1228205471Sdelphij Dynamically update the compression level or strategy. See the description 122933904Ssteve of deflateInit2 for the meaning of these parameters. 1230205471Sdelphij 123133904Ssteve gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not 123233904Ssteve opened for writing. 123333904Ssteve*/ 123433904Ssteve 1235205471SdelphijZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); 123633904Ssteve/* 1237205471Sdelphij Reads the given number of uncompressed bytes from the compressed file. If 1238205471Sdelphij the input file was not in gzip format, gzread copies the given number of 1239205471Sdelphij bytes into the buffer. 124017651Speter 1241205471Sdelphij After reaching the end of a gzip stream in the input, gzread will continue 1242205471Sdelphij to read, looking for another gzip stream, or failing that, reading the rest 1243205471Sdelphij of the input file directly without decompression. The entire input file 1244205471Sdelphij will be read if gzread is called until it returns less than the requested 1245205471Sdelphij len. 1246205471Sdelphij 1247205471Sdelphij gzread returns the number of uncompressed bytes actually read, less than 1248205471Sdelphij len for end of file, or -1 for error. 1249205471Sdelphij*/ 1250205471Sdelphij 1251205471SdelphijZEXTERN int ZEXPORT gzwrite OF((gzFile file, 1252205471Sdelphij voidpc buf, unsigned len)); 125317651Speter/* 125417651Speter Writes the given number of uncompressed bytes into the compressed file. 1255205471Sdelphij gzwrite returns the number of uncompressed bytes written or 0 in case of 1256205471Sdelphij error. 125717651Speter*/ 125817651Speter 1259205471SdelphijZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); 126017651Speter/* 1261205471Sdelphij Converts, formats, and writes the arguments to the compressed file under 1262205471Sdelphij control of the format string, as in fprintf. gzprintf returns the number of 1263205471Sdelphij uncompressed bytes actually written, or 0 in case of error. The number of 1264205471Sdelphij uncompressed bytes written is limited to 8191, or one less than the buffer 1265205471Sdelphij size given to gzbuffer(). The caller should assure that this limit is not 1266205471Sdelphij exceeded. If it is exceeded, then gzprintf() will return an error (0) with 1267205471Sdelphij nothing written. In this case, there may also be a buffer overflow with 1268205471Sdelphij unpredictable consequences, which is possible only if zlib was compiled with 1269205471Sdelphij the insecure functions sprintf() or vsprintf() because the secure snprintf() 1270205471Sdelphij or vsnprintf() functions were not available. This can be determined using 1271205471Sdelphij zlibCompileFlags(). 127233904Ssteve*/ 127333904Ssteve 127442468SpeterZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); 127533904Ssteve/* 1276205471Sdelphij Writes the given null-terminated string to the compressed file, excluding 127733904Ssteve the terminating null character. 1278205471Sdelphij 1279205471Sdelphij gzputs returns the number of characters written, or -1 in case of error. 128033904Ssteve*/ 128133904Ssteve 128242468SpeterZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); 128333904Ssteve/* 1284205471Sdelphij Reads bytes from the compressed file until len-1 characters are read, or a 1285205471Sdelphij newline character is read and transferred to buf, or an end-of-file 1286205471Sdelphij condition is encountered. If any characters are read or if len == 1, the 1287205471Sdelphij string is terminated with a null character. If no characters are read due 1288205471Sdelphij to an end-of-file or len < 1, then the buffer is left untouched. 1289205471Sdelphij 1290205471Sdelphij gzgets returns buf which is a null-terminated string, or it returns NULL 1291205471Sdelphij for end-of-file or in case of error. If there was an error, the contents at 1292205471Sdelphij buf are indeterminate. 129333904Ssteve*/ 129433904Ssteve 1295205471SdelphijZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); 129633904Ssteve/* 1297205471Sdelphij Writes c, converted to an unsigned char, into the compressed file. gzputc 1298205471Sdelphij returns the value that was written, or -1 in case of error. 129933904Ssteve*/ 130033904Ssteve 1301205471SdelphijZEXTERN int ZEXPORT gzgetc OF((gzFile file)); 130233904Ssteve/* 1303205471Sdelphij Reads one byte from the compressed file. gzgetc returns this byte or -1 1304205471Sdelphij in case of end of file or error. 130533904Ssteve*/ 130633904Ssteve 1307205471SdelphijZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); 1308131377Stjr/* 1309205471Sdelphij Push one character back onto the stream to be read as the first character 1310205471Sdelphij on the next read. At least one character of push-back is allowed. 1311205471Sdelphij gzungetc() returns the character pushed, or -1 on failure. gzungetc() will 1312205471Sdelphij fail if c is -1, and may fail if a character has been pushed but not read 1313205471Sdelphij yet. If gzungetc is used immediately after gzopen or gzdopen, at least the 1314205471Sdelphij output buffer size of pushed characters is allowed. (See gzbuffer above.) 1315205471Sdelphij The pushed character will be discarded if the stream is repositioned with 1316205471Sdelphij gzseek() or gzrewind(). 1317131377Stjr*/ 1318131377Stjr 1319205471SdelphijZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); 132033904Ssteve/* 1321205471Sdelphij Flushes all pending output into the compressed file. The parameter flush 1322205471Sdelphij is as in the deflate() function. The return value is the zlib error number 1323205471Sdelphij (see function gzerror below). gzflush is only permitted when writing. 1324205471Sdelphij 1325205471Sdelphij If the flush parameter is Z_FINISH, the remaining data is written and the 1326205471Sdelphij gzip stream is completed in the output. If gzwrite() is called again, a new 1327205471Sdelphij gzip stream will be started in the output. gzread() is able to read such 1328205471Sdelphij concatented gzip streams. 1329205471Sdelphij 1330205471Sdelphij gzflush should be called only when strictly necessary because it will 1331205471Sdelphij degrade compression if called too often. 133217651Speter*/ 133317651Speter 1334131377Stjr/* 1335205471SdelphijZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, 1336205471Sdelphij z_off_t offset, int whence)); 1337205471Sdelphij 1338205471Sdelphij Sets the starting position for the next gzread or gzwrite on the given 1339205471Sdelphij compressed file. The offset represents a number of bytes in the 1340205471Sdelphij uncompressed data stream. The whence parameter is defined as in lseek(2); 134133904Ssteve the value SEEK_END is not supported. 1342205471Sdelphij 134333904Ssteve If the file is opened for reading, this function is emulated but can be 1344205471Sdelphij extremely slow. If the file is opened for writing, only forward seeks are 134533904Ssteve supported; gzseek then compresses a sequence of zeroes up to the new 134633904Ssteve starting position. 134733904Ssteve 1348205471Sdelphij gzseek returns the resulting offset location as measured in bytes from 134933904Ssteve the beginning of the uncompressed stream, or -1 in case of error, in 135033904Ssteve particular if the file is opened for writing and the new starting position 135133904Ssteve would be before the current position. 135233904Ssteve*/ 135333904Ssteve 135442468SpeterZEXTERN int ZEXPORT gzrewind OF((gzFile file)); 135517651Speter/* 135633904Ssteve Rewinds the given file. This function is supported only for reading. 135733904Ssteve 1358205471Sdelphij gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) 135933904Ssteve*/ 136033904Ssteve 1361205471Sdelphij/* 136242468SpeterZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); 1363205471Sdelphij 1364205471Sdelphij Returns the starting position for the next gzread or gzwrite on the given 1365205471Sdelphij compressed file. This position represents a number of bytes in the 1366205471Sdelphij uncompressed data stream, and is zero when starting, even if appending or 1367205471Sdelphij reading a gzip stream from the middle of a file using gzdopen(). 1368205471Sdelphij 1369205471Sdelphij gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) 1370205471Sdelphij*/ 1371205471Sdelphij 137233904Ssteve/* 1373205471SdelphijZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); 137433904Ssteve 1375205471Sdelphij Returns the current offset in the file being read or written. This offset 1376205471Sdelphij includes the count of bytes that precede the gzip stream, for example when 1377205471Sdelphij appending or when using gzdopen() for reading. When reading, the offset 1378205471Sdelphij does not include as yet unused buffered input. This information can be used 1379205471Sdelphij for a progress indicator. On error, gzoffset() returns -1. 138033904Ssteve*/ 138133904Ssteve 138242468SpeterZEXTERN int ZEXPORT gzeof OF((gzFile file)); 138333904Ssteve/* 1384205471Sdelphij Returns true (1) if the end-of-file indicator has been set while reading, 1385205471Sdelphij false (0) otherwise. Note that the end-of-file indicator is set only if the 1386205471Sdelphij read tried to go past the end of the input, but came up short. Therefore, 1387205471Sdelphij just like feof(), gzeof() may return false even if there is no more data to 1388205471Sdelphij read, in the event that the last read request was for the exact number of 1389205471Sdelphij bytes remaining in the input file. This will happen if the input file size 1390205471Sdelphij is an exact multiple of the buffer size. 1391205471Sdelphij 1392205471Sdelphij If gzeof() returns true, then the read functions will return no more data, 1393205471Sdelphij unless the end-of-file indicator is reset by gzclearerr() and the input file 1394205471Sdelphij has grown since the previous end of file was detected. 139533904Ssteve*/ 139633904Ssteve 1397157043SdesZEXTERN int ZEXPORT gzdirect OF((gzFile file)); 1398157043Sdes/* 1399205471Sdelphij Returns true (1) if file is being copied directly while reading, or false 1400205471Sdelphij (0) if file is a gzip stream being decompressed. This state can change from 1401205471Sdelphij false to true while reading the input file if the end of a gzip stream is 1402205471Sdelphij reached, but is followed by data that is not another gzip stream. 1403205471Sdelphij 1404205471Sdelphij If the input file is empty, gzdirect() will return true, since the input 1405205471Sdelphij does not contain a gzip stream. 1406205471Sdelphij 1407205471Sdelphij If gzdirect() is used immediately after gzopen() or gzdopen() it will 1408205471Sdelphij cause buffers to be allocated to allow reading the file to determine if it 1409205471Sdelphij is a gzip file. Therefore if gzbuffer() is used, it should be called before 1410205471Sdelphij gzdirect(). 1411157043Sdes*/ 1412157043Sdes 141342468SpeterZEXTERN int ZEXPORT gzclose OF((gzFile file)); 141433904Ssteve/* 1415205471Sdelphij Flushes all pending output if necessary, closes the compressed file and 1416205471Sdelphij deallocates the (de)compression state. Note that once file is closed, you 1417205471Sdelphij cannot call gzerror with file, since its structures have been deallocated. 1418205471Sdelphij gzclose must not be called more than once on the same file, just as free 1419205471Sdelphij must not be called more than once on the same allocation. 1420205471Sdelphij 1421205471Sdelphij gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a 1422205471Sdelphij file operation error, or Z_OK on success. 142317651Speter*/ 142417651Speter 1425205471SdelphijZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); 1426205471SdelphijZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); 1427205471Sdelphij/* 1428205471Sdelphij Same as gzclose(), but gzclose_r() is only for use when reading, and 1429205471Sdelphij gzclose_w() is only for use when writing or appending. The advantage to 1430205471Sdelphij using these instead of gzclose() is that they avoid linking in zlib 1431205471Sdelphij compression or decompression code that is not used when only reading or only 1432205471Sdelphij writing respectively. If gzclose() is used, then both compression and 1433205471Sdelphij decompression code will be included the application when linking to a static 1434205471Sdelphij zlib library. 1435205471Sdelphij*/ 1436205471Sdelphij 143742468SpeterZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); 143817651Speter/* 1439205471Sdelphij Returns the error message for the last error which occurred on the given 1440205471Sdelphij compressed file. errnum is set to zlib error number. If an error occurred 1441205471Sdelphij in the file system and not in the compression library, errnum is set to 1442205471Sdelphij Z_ERRNO and the application may consult errno to get the exact error code. 1443205471Sdelphij 1444205471Sdelphij The application must not modify the returned string. Future calls to 1445205471Sdelphij this function may invalidate the previously returned string. If file is 1446205471Sdelphij closed, then the string previously returned by gzerror will no longer be 1447205471Sdelphij available. 1448205471Sdelphij 1449205471Sdelphij gzerror() should be used to distinguish errors from end-of-file for those 1450205471Sdelphij functions above that do not distinguish those cases in their return values. 145117651Speter*/ 145217651Speter 1453131377StjrZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); 1454131377Stjr/* 1455205471Sdelphij Clears the error and end-of-file flags for file. This is analogous to the 1456205471Sdelphij clearerr() function in stdio. This is useful for continuing to read a gzip 1457131377Stjr file that is being written concurrently. 1458131377Stjr*/ 1459131377Stjr 1460205471Sdelphij 146117651Speter /* checksum functions */ 146217651Speter 146317651Speter/* 146417651Speter These functions are not related to compression but are exported 1465205471Sdelphij anyway because they might be useful in applications using the compression 1466205471Sdelphij library. 146717651Speter*/ 146817651Speter 146942468SpeterZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); 147017651Speter/* 147117651Speter Update a running Adler-32 checksum with the bytes buf[0..len-1] and 1472205471Sdelphij return the updated checksum. If buf is Z_NULL, this function returns the 1473205471Sdelphij required initial value for the checksum. 147417651Speter 1475205471Sdelphij An Adler-32 checksum is almost as reliable as a CRC32 but can be computed 1476205471Sdelphij much faster. 1477205471Sdelphij 1478205471Sdelphij Usage example: 1479205471Sdelphij 148017651Speter uLong adler = adler32(0L, Z_NULL, 0); 148117651Speter 148217651Speter while (read_buffer(buffer, length) != EOF) { 148317651Speter adler = adler32(adler, buffer, length); 148417651Speter } 148517651Speter if (adler != original_adler) error(); 148617651Speter*/ 148717651Speter 1488205471Sdelphij/* 1489157043SdesZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, 1490157043Sdes z_off_t len2)); 1491205471Sdelphij 1492157043Sdes Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 1493157043Sdes and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for 1494157043Sdes each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of 1495157043Sdes seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. 1496157043Sdes*/ 1497157043Sdes 149842468SpeterZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); 149917651Speter/* 1500157043Sdes Update a running CRC-32 with the bytes buf[0..len-1] and return the 1501205471Sdelphij updated CRC-32. If buf is Z_NULL, this function returns the required 1502205471Sdelphij initial value for the for the crc. Pre- and post-conditioning (one's 1503205471Sdelphij complement) is performed within this function so it shouldn't be done by the 1504205471Sdelphij application. 1505205471Sdelphij 150617651Speter Usage example: 150717651Speter 150817651Speter uLong crc = crc32(0L, Z_NULL, 0); 150917651Speter 151017651Speter while (read_buffer(buffer, length) != EOF) { 151117651Speter crc = crc32(crc, buffer, length); 151217651Speter } 151317651Speter if (crc != original_crc) error(); 151417651Speter*/ 151517651Speter 1516205471Sdelphij/* 1517157043SdesZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); 151817651Speter 1519157043Sdes Combine two CRC-32 check values into one. For two sequences of bytes, 1520157043Sdes seq1 and seq2 with lengths len1 and len2, CRC-32 check values were 1521157043Sdes calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 1522157043Sdes check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and 1523157043Sdes len2. 1524157043Sdes*/ 1525157043Sdes 1526157043Sdes 152717651Speter /* various hacks, don't look :) */ 152817651Speter 152917651Speter/* deflateInit and inflateInit are macros to allow checking the zlib version 153017651Speter * and the compiler's view of z_stream: 153117651Speter */ 153242468SpeterZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, 153333904Ssteve const char *version, int stream_size)); 153442468SpeterZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, 153542468Speter const char *version, int stream_size)); 153642468SpeterZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, 153742468Speter int windowBits, int memLevel, 153842468Speter int strategy, const char *version, 153942468Speter int stream_size)); 154042468SpeterZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, 154142468Speter const char *version, int stream_size)); 1542157043SdesZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, 1543131377Stjr unsigned char FAR *window, 1544131377Stjr const char *version, 1545131377Stjr int stream_size)); 154617651Speter#define deflateInit(strm, level) \ 154717651Speter deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) 154817651Speter#define inflateInit(strm) \ 154917651Speter inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) 155017651Speter#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ 155117651Speter deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ 155233904Ssteve (strategy), ZLIB_VERSION, sizeof(z_stream)) 155317651Speter#define inflateInit2(strm, windowBits) \ 155417651Speter inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) 1555131377Stjr#define inflateBackInit(strm, windowBits, window) \ 1556131377Stjr inflateBackInit_((strm), (windowBits), (window), \ 1557205471Sdelphij ZLIB_VERSION, sizeof(z_stream)) 155817651Speter 1559206708Sdelphij/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or 1560206708Sdelphij * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if 1561206708Sdelphij * both are true, the application gets the *64 functions, and the regular 1562206708Sdelphij * functions are changed to 64 bits) -- in case these are set on systems 1563206708Sdelphij * without large file support, _LFS64_LARGEFILE must also be true 1564206708Sdelphij */ 1565206708Sdelphij#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 1566206708Sdelphij ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 1567206708Sdelphij ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 1568206708Sdelphij ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 1569206708Sdelphij ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 1570206708Sdelphij ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); 1571206708Sdelphij ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); 1572206708Sdelphij#endif 157333904Ssteve 1574206708Sdelphij#if !defined(ZLIB_INTERNAL) && _FILE_OFFSET_BITS-0 == 64 && _LFS64_LARGEFILE-0 1575206708Sdelphij# define gzopen gzopen64 1576206708Sdelphij# define gzseek gzseek64 1577206708Sdelphij# define gztell gztell64 1578206708Sdelphij# define gzoffset gzoffset64 1579206708Sdelphij# define adler32_combine adler32_combine64 1580206708Sdelphij# define crc32_combine crc32_combine64 1581206708Sdelphij# ifdef _LARGEFILE64_SOURCE 1582206708Sdelphij ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 1583206708Sdelphij ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); 1584206708Sdelphij ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); 1585206708Sdelphij ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); 1586206708Sdelphij ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); 1587206708Sdelphij ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); 1588206708Sdelphij# endif 1589206708Sdelphij#else 1590205471Sdelphij ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); 1591205471Sdelphij ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); 1592205471Sdelphij ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); 1593205471Sdelphij ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); 1594205471Sdelphij ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); 1595205471Sdelphij ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); 1596206708Sdelphij#endif 1597205471Sdelphij 1598206708Sdelphij/* hack for buggy compilers */ 1599131377Stjr#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) 1600206708Sdelphij struct internal_state {int dummy;}; 160117651Speter#endif 160217651Speter 1603206708Sdelphij/* undocumented functions */ 1604145474SkientzleZEXTERN const char * ZEXPORT zError OF((int)); 1605205471SdelphijZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); 160642468SpeterZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); 1607205471SdelphijZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); 160817651Speter 160917651Speter#ifdef __cplusplus 161017651Speter} 161117651Speter#endif 161217651Speter 1613131377Stjr#endif /* ZLIB_H */ 1614