xref: /netbsd-src/external/gpl3/binutils.old/dist/zlib/zlib.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* zlib.h -- interface of the 'zlib' general purpose compression library
2*e992f068Schristos   version 1.2.12, March 11th, 2022
375fd0b74Schristos 
4*e992f068Schristos   Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
575fd0b74Schristos 
675fd0b74Schristos   This software is provided 'as-is', without any express or implied
775fd0b74Schristos   warranty.  In no event will the authors be held liable for any damages
875fd0b74Schristos   arising from the use of this software.
975fd0b74Schristos 
1075fd0b74Schristos   Permission is granted to anyone to use this software for any purpose,
1175fd0b74Schristos   including commercial applications, and to alter it and redistribute it
1275fd0b74Schristos   freely, subject to the following restrictions:
1375fd0b74Schristos 
1475fd0b74Schristos   1. The origin of this software must not be misrepresented; you must not
1575fd0b74Schristos      claim that you wrote the original software. If you use this software
1675fd0b74Schristos      in a product, an acknowledgment in the product documentation would be
1775fd0b74Schristos      appreciated but is not required.
1875fd0b74Schristos   2. Altered source versions must be plainly marked as such, and must not be
1975fd0b74Schristos      misrepresented as being the original software.
2075fd0b74Schristos   3. This notice may not be removed or altered from any source distribution.
2175fd0b74Schristos 
2275fd0b74Schristos   Jean-loup Gailly        Mark Adler
2375fd0b74Schristos   jloup@gzip.org          madler@alumni.caltech.edu
2475fd0b74Schristos 
2575fd0b74Schristos 
2675fd0b74Schristos   The data format used by the zlib library is described by RFCs (Request for
2775fd0b74Schristos   Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
2875fd0b74Schristos   (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
2975fd0b74Schristos */
3075fd0b74Schristos 
3175fd0b74Schristos #ifndef ZLIB_H
3275fd0b74Schristos #define ZLIB_H
3375fd0b74Schristos 
3475fd0b74Schristos #include "zconf.h"
3575fd0b74Schristos 
3675fd0b74Schristos #ifdef __cplusplus
3775fd0b74Schristos extern "C" {
3875fd0b74Schristos #endif
3975fd0b74Schristos 
40*e992f068Schristos #define ZLIB_VERSION "1.2.12"
41*e992f068Schristos #define ZLIB_VERNUM 0x12c0
4275fd0b74Schristos #define ZLIB_VER_MAJOR 1
4375fd0b74Schristos #define ZLIB_VER_MINOR 2
44*e992f068Schristos #define ZLIB_VER_REVISION 12
4575fd0b74Schristos #define ZLIB_VER_SUBREVISION 0
4675fd0b74Schristos 
4775fd0b74Schristos /*
4875fd0b74Schristos     The 'zlib' compression library provides in-memory compression and
4975fd0b74Schristos   decompression functions, including integrity checks of the uncompressed data.
5075fd0b74Schristos   This version of the library supports only one compression method (deflation)
5175fd0b74Schristos   but other algorithms will be added later and will have the same stream
5275fd0b74Schristos   interface.
5375fd0b74Schristos 
5475fd0b74Schristos     Compression can be done in a single step if the buffers are large enough,
5575fd0b74Schristos   or can be done by repeated calls of the compression function.  In the latter
5675fd0b74Schristos   case, the application must provide more input and/or consume the output
5775fd0b74Schristos   (providing more output space) before each call.
5875fd0b74Schristos 
5975fd0b74Schristos     The compressed data format used by default by the in-memory functions is
6075fd0b74Schristos   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
6175fd0b74Schristos   around a deflate stream, which is itself documented in RFC 1951.
6275fd0b74Schristos 
6375fd0b74Schristos     The library also supports reading and writing files in gzip (.gz) format
6475fd0b74Schristos   with an interface similar to that of stdio using the functions that start
6575fd0b74Schristos   with "gz".  The gzip format is different from the zlib format.  gzip is a
6675fd0b74Schristos   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
6775fd0b74Schristos 
68ede78133Schristos     This library can optionally read and write gzip and raw deflate streams in
69ede78133Schristos   memory as well.
7075fd0b74Schristos 
7175fd0b74Schristos     The zlib format was designed to be compact and fast for use in memory
7275fd0b74Schristos   and on communications channels.  The gzip format was designed for single-
7375fd0b74Schristos   file compression on file systems, has a larger header than zlib to maintain
7475fd0b74Schristos   directory information, and uses a different, slower check method than zlib.
7575fd0b74Schristos 
7675fd0b74Schristos     The library does not install any signal handler.  The decoder checks
7775fd0b74Schristos   the consistency of the compressed data, so the library should never crash
78ede78133Schristos   even in the case of corrupted input.
7975fd0b74Schristos */
8075fd0b74Schristos 
8175fd0b74Schristos typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
8275fd0b74Schristos typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
8375fd0b74Schristos 
8475fd0b74Schristos struct internal_state;
8575fd0b74Schristos 
8675fd0b74Schristos typedef struct z_stream_s {
8775fd0b74Schristos     z_const Bytef *next_in;     /* next input byte */
8875fd0b74Schristos     uInt     avail_in;  /* number of bytes available at next_in */
8975fd0b74Schristos     uLong    total_in;  /* total number of input bytes read so far */
9075fd0b74Schristos 
91ede78133Schristos     Bytef    *next_out; /* next output byte will go here */
9275fd0b74Schristos     uInt     avail_out; /* remaining free space at next_out */
9375fd0b74Schristos     uLong    total_out; /* total number of bytes output so far */
9475fd0b74Schristos 
9575fd0b74Schristos     z_const char *msg;  /* last error message, NULL if no error */
9675fd0b74Schristos     struct internal_state FAR *state; /* not visible by applications */
9775fd0b74Schristos 
9875fd0b74Schristos     alloc_func zalloc;  /* used to allocate the internal state */
9975fd0b74Schristos     free_func  zfree;   /* used to free the internal state */
10075fd0b74Schristos     voidpf     opaque;  /* private data object passed to zalloc and zfree */
10175fd0b74Schristos 
102ede78133Schristos     int     data_type;  /* best guess about the data type: binary or text
103ede78133Schristos                            for deflate, or the decoding state for inflate */
104ede78133Schristos     uLong   adler;      /* Adler-32 or CRC-32 value of the uncompressed data */
10575fd0b74Schristos     uLong   reserved;   /* reserved for future use */
10675fd0b74Schristos } z_stream;
10775fd0b74Schristos 
10875fd0b74Schristos typedef z_stream FAR *z_streamp;
10975fd0b74Schristos 
11075fd0b74Schristos /*
11175fd0b74Schristos      gzip header information passed to and from zlib routines.  See RFC 1952
11275fd0b74Schristos   for more details on the meanings of these fields.
11375fd0b74Schristos */
11475fd0b74Schristos typedef struct gz_header_s {
11575fd0b74Schristos     int     text;       /* true if compressed data believed to be text */
11675fd0b74Schristos     uLong   time;       /* modification time */
11775fd0b74Schristos     int     xflags;     /* extra flags (not used when writing a gzip file) */
11875fd0b74Schristos     int     os;         /* operating system */
11975fd0b74Schristos     Bytef   *extra;     /* pointer to extra field or Z_NULL if none */
12075fd0b74Schristos     uInt    extra_len;  /* extra field length (valid if extra != Z_NULL) */
12175fd0b74Schristos     uInt    extra_max;  /* space at extra (only when reading header) */
12275fd0b74Schristos     Bytef   *name;      /* pointer to zero-terminated file name or Z_NULL */
12375fd0b74Schristos     uInt    name_max;   /* space at name (only when reading header) */
12475fd0b74Schristos     Bytef   *comment;   /* pointer to zero-terminated comment or Z_NULL */
12575fd0b74Schristos     uInt    comm_max;   /* space at comment (only when reading header) */
12675fd0b74Schristos     int     hcrc;       /* true if there was or will be a header crc */
12775fd0b74Schristos     int     done;       /* true when done reading gzip header (not used
12875fd0b74Schristos                            when writing a gzip file) */
12975fd0b74Schristos } gz_header;
13075fd0b74Schristos 
13175fd0b74Schristos typedef gz_header FAR *gz_headerp;
13275fd0b74Schristos 
13375fd0b74Schristos /*
13475fd0b74Schristos      The application must update next_in and avail_in when avail_in has dropped
13575fd0b74Schristos    to zero.  It must update next_out and avail_out when avail_out has dropped
13675fd0b74Schristos    to zero.  The application must initialize zalloc, zfree and opaque before
13775fd0b74Schristos    calling the init function.  All other fields are set by the compression
13875fd0b74Schristos    library and must not be updated by the application.
13975fd0b74Schristos 
14075fd0b74Schristos      The opaque value provided by the application will be passed as the first
14175fd0b74Schristos    parameter for calls of zalloc and zfree.  This can be useful for custom
14275fd0b74Schristos    memory management.  The compression library attaches no meaning to the
14375fd0b74Schristos    opaque value.
14475fd0b74Schristos 
14575fd0b74Schristos      zalloc must return Z_NULL if there is not enough memory for the object.
14675fd0b74Schristos    If zlib is used in a multi-threaded application, zalloc and zfree must be
147ede78133Schristos    thread safe.  In that case, zlib is thread-safe.  When zalloc and zfree are
148ede78133Schristos    Z_NULL on entry to the initialization function, they are set to internal
149ede78133Schristos    routines that use the standard library functions malloc() and free().
15075fd0b74Schristos 
15175fd0b74Schristos      On 16-bit systems, the functions zalloc and zfree must be able to allocate
15275fd0b74Schristos    exactly 65536 bytes, but will not be required to allocate more than this if
15375fd0b74Schristos    the symbol MAXSEG_64K is defined (see zconf.h).  WARNING: On MSDOS, pointers
15475fd0b74Schristos    returned by zalloc for objects of exactly 65536 bytes *must* have their
15575fd0b74Schristos    offset normalized to zero.  The default allocation function provided by this
15675fd0b74Schristos    library ensures this (see zutil.c).  To reduce memory requirements and avoid
15775fd0b74Schristos    any allocation of 64K objects, at the expense of compression ratio, compile
15875fd0b74Schristos    the library with -DMAX_WBITS=14 (see zconf.h).
15975fd0b74Schristos 
16075fd0b74Schristos      The fields total_in and total_out can be used for statistics or progress
16175fd0b74Schristos    reports.  After compression, total_in holds the total size of the
162ede78133Schristos    uncompressed data and may be saved for use by the decompressor (particularly
16375fd0b74Schristos    if the decompressor wants to decompress everything in a single step).
16475fd0b74Schristos */
16575fd0b74Schristos 
16675fd0b74Schristos                         /* constants */
16775fd0b74Schristos 
16875fd0b74Schristos #define Z_NO_FLUSH      0
16975fd0b74Schristos #define Z_PARTIAL_FLUSH 1
17075fd0b74Schristos #define Z_SYNC_FLUSH    2
17175fd0b74Schristos #define Z_FULL_FLUSH    3
17275fd0b74Schristos #define Z_FINISH        4
17375fd0b74Schristos #define Z_BLOCK         5
17475fd0b74Schristos #define Z_TREES         6
17575fd0b74Schristos /* Allowed flush values; see deflate() and inflate() below for details */
17675fd0b74Schristos 
17775fd0b74Schristos #define Z_OK            0
17875fd0b74Schristos #define Z_STREAM_END    1
17975fd0b74Schristos #define Z_NEED_DICT     2
18075fd0b74Schristos #define Z_ERRNO        (-1)
18175fd0b74Schristos #define Z_STREAM_ERROR (-2)
18275fd0b74Schristos #define Z_DATA_ERROR   (-3)
18375fd0b74Schristos #define Z_MEM_ERROR    (-4)
18475fd0b74Schristos #define Z_BUF_ERROR    (-5)
18575fd0b74Schristos #define Z_VERSION_ERROR (-6)
18675fd0b74Schristos /* Return codes for the compression/decompression functions. Negative values
18775fd0b74Schristos  * are errors, positive values are used for special but normal events.
18875fd0b74Schristos  */
18975fd0b74Schristos 
19075fd0b74Schristos #define Z_NO_COMPRESSION         0
19175fd0b74Schristos #define Z_BEST_SPEED             1
19275fd0b74Schristos #define Z_BEST_COMPRESSION       9
19375fd0b74Schristos #define Z_DEFAULT_COMPRESSION  (-1)
19475fd0b74Schristos /* compression levels */
19575fd0b74Schristos 
19675fd0b74Schristos #define Z_FILTERED            1
19775fd0b74Schristos #define Z_HUFFMAN_ONLY        2
19875fd0b74Schristos #define Z_RLE                 3
19975fd0b74Schristos #define Z_FIXED               4
20075fd0b74Schristos #define Z_DEFAULT_STRATEGY    0
20175fd0b74Schristos /* compression strategy; see deflateInit2() below for details */
20275fd0b74Schristos 
20375fd0b74Schristos #define Z_BINARY   0
20475fd0b74Schristos #define Z_TEXT     1
20575fd0b74Schristos #define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
20675fd0b74Schristos #define Z_UNKNOWN  2
207ede78133Schristos /* Possible values of the data_type field for deflate() */
20875fd0b74Schristos 
20975fd0b74Schristos #define Z_DEFLATED   8
21075fd0b74Schristos /* The deflate compression method (the only one supported in this version) */
21175fd0b74Schristos 
21275fd0b74Schristos #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
21375fd0b74Schristos 
21475fd0b74Schristos #define zlib_version zlibVersion()
21575fd0b74Schristos /* for compatibility with versions < 1.0.2 */
21675fd0b74Schristos 
21775fd0b74Schristos 
21875fd0b74Schristos                         /* basic functions */
21975fd0b74Schristos 
22075fd0b74Schristos ZEXTERN const char * ZEXPORT zlibVersion OF((void));
22175fd0b74Schristos /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
22275fd0b74Schristos    If the first character differs, the library code actually used is not
22375fd0b74Schristos    compatible with the zlib.h header file used by the application.  This check
22475fd0b74Schristos    is automatically made by deflateInit and inflateInit.
22575fd0b74Schristos  */
22675fd0b74Schristos 
22775fd0b74Schristos /*
22875fd0b74Schristos ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
22975fd0b74Schristos 
23075fd0b74Schristos      Initializes the internal stream state for compression.  The fields
23175fd0b74Schristos    zalloc, zfree and opaque must be initialized before by the caller.  If
23275fd0b74Schristos    zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
23375fd0b74Schristos    allocation functions.
23475fd0b74Schristos 
23575fd0b74Schristos      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
23675fd0b74Schristos    1 gives best speed, 9 gives best compression, 0 gives no compression at all
23775fd0b74Schristos    (the input data is simply copied a block at a time).  Z_DEFAULT_COMPRESSION
23875fd0b74Schristos    requests a default compromise between speed and compression (currently
23975fd0b74Schristos    equivalent to level 6).
24075fd0b74Schristos 
24175fd0b74Schristos      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
24275fd0b74Schristos    memory, Z_STREAM_ERROR if level is not a valid compression level, or
24375fd0b74Schristos    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
24475fd0b74Schristos    with the version assumed by the caller (ZLIB_VERSION).  msg is set to null
24575fd0b74Schristos    if there is no error message.  deflateInit does not perform any compression:
24675fd0b74Schristos    this will be done by deflate().
24775fd0b74Schristos */
24875fd0b74Schristos 
24975fd0b74Schristos 
25075fd0b74Schristos ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
25175fd0b74Schristos /*
25275fd0b74Schristos     deflate compresses as much data as possible, and stops when the input
25375fd0b74Schristos   buffer becomes empty or the output buffer becomes full.  It may introduce
25475fd0b74Schristos   some output latency (reading input without producing any output) except when
25575fd0b74Schristos   forced to flush.
25675fd0b74Schristos 
25775fd0b74Schristos     The detailed semantics are as follows.  deflate performs one or both of the
25875fd0b74Schristos   following actions:
25975fd0b74Schristos 
26075fd0b74Schristos   - Compress more input starting at next_in and update next_in and avail_in
26175fd0b74Schristos     accordingly.  If not all input can be processed (because there is not
26275fd0b74Schristos     enough room in the output buffer), next_in and avail_in are updated and
26375fd0b74Schristos     processing will resume at this point for the next call of deflate().
26475fd0b74Schristos 
265ede78133Schristos   - Generate more output starting at next_out and update next_out and avail_out
26675fd0b74Schristos     accordingly.  This action is forced if the parameter flush is non zero.
26775fd0b74Schristos     Forcing flush frequently degrades the compression ratio, so this parameter
268ede78133Schristos     should be set only when necessary.  Some output may be provided even if
269ede78133Schristos     flush is zero.
27075fd0b74Schristos 
27175fd0b74Schristos     Before the call of deflate(), the application should ensure that at least
27275fd0b74Schristos   one of the actions is possible, by providing more input and/or consuming more
27375fd0b74Schristos   output, and updating avail_in or avail_out accordingly; avail_out should
27475fd0b74Schristos   never be zero before the call.  The application can consume the compressed
27575fd0b74Schristos   output when it wants, for example when the output buffer is full (avail_out
27675fd0b74Schristos   == 0), or after each call of deflate().  If deflate returns Z_OK and with
27775fd0b74Schristos   zero avail_out, it must be called again after making room in the output
278ede78133Schristos   buffer because there might be more output pending. See deflatePending(),
279ede78133Schristos   which can be used if desired to determine whether or not there is more ouput
280ede78133Schristos   in that case.
28175fd0b74Schristos 
28275fd0b74Schristos     Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
28375fd0b74Schristos   decide how much data to accumulate before producing output, in order to
28475fd0b74Schristos   maximize compression.
28575fd0b74Schristos 
28675fd0b74Schristos     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
28775fd0b74Schristos   flushed to the output buffer and the output is aligned on a byte boundary, so
28875fd0b74Schristos   that the decompressor can get all input data available so far.  (In
28975fd0b74Schristos   particular avail_in is zero after the call if enough output space has been
29075fd0b74Schristos   provided before the call.) Flushing may degrade compression for some
29175fd0b74Schristos   compression algorithms and so it should be used only when necessary.  This
29275fd0b74Schristos   completes the current deflate block and follows it with an empty stored block
29375fd0b74Schristos   that is three bits plus filler bits to the next byte, followed by four bytes
29475fd0b74Schristos   (00 00 ff ff).
29575fd0b74Schristos 
29675fd0b74Schristos     If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
29775fd0b74Schristos   output buffer, but the output is not aligned to a byte boundary.  All of the
29875fd0b74Schristos   input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
29975fd0b74Schristos   This completes the current deflate block and follows it with an empty fixed
30075fd0b74Schristos   codes block that is 10 bits long.  This assures that enough bytes are output
301ede78133Schristos   in order for the decompressor to finish the block before the empty fixed
302ede78133Schristos   codes block.
30375fd0b74Schristos 
30475fd0b74Schristos     If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
30575fd0b74Schristos   for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
30675fd0b74Schristos   seven bits of the current block are held to be written as the next byte after
30775fd0b74Schristos   the next deflate block is completed.  In this case, the decompressor may not
30875fd0b74Schristos   be provided enough bits at this point in order to complete decompression of
30975fd0b74Schristos   the data provided so far to the compressor.  It may need to wait for the next
31075fd0b74Schristos   block to be emitted.  This is for advanced applications that need to control
31175fd0b74Schristos   the emission of deflate blocks.
31275fd0b74Schristos 
31375fd0b74Schristos     If flush is set to Z_FULL_FLUSH, all output is flushed as with
31475fd0b74Schristos   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
31575fd0b74Schristos   restart from this point if previous compressed data has been damaged or if
31675fd0b74Schristos   random access is desired.  Using Z_FULL_FLUSH too often can seriously degrade
31775fd0b74Schristos   compression.
31875fd0b74Schristos 
31975fd0b74Schristos     If deflate returns with avail_out == 0, this function must be called again
32075fd0b74Schristos   with the same value of the flush parameter and more output space (updated
32175fd0b74Schristos   avail_out), until the flush is complete (deflate returns with non-zero
32275fd0b74Schristos   avail_out).  In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
32375fd0b74Schristos   avail_out is greater than six to avoid repeated flush markers due to
32475fd0b74Schristos   avail_out == 0 on return.
32575fd0b74Schristos 
32675fd0b74Schristos     If the parameter flush is set to Z_FINISH, pending input is processed,
32775fd0b74Schristos   pending output is flushed and deflate returns with Z_STREAM_END if there was
328ede78133Schristos   enough output space.  If deflate returns with Z_OK or Z_BUF_ERROR, this
329ede78133Schristos   function must be called again with Z_FINISH and more output space (updated
330ede78133Schristos   avail_out) but no more input data, until it returns with Z_STREAM_END or an
331ede78133Schristos   error.  After deflate has returned Z_STREAM_END, the only possible operations
332ede78133Schristos   on the stream are deflateReset or deflateEnd.
33375fd0b74Schristos 
334ede78133Schristos     Z_FINISH can be used in the first deflate call after deflateInit if all the
335ede78133Schristos   compression is to be done in a single step.  In order to complete in one
336ede78133Schristos   call, avail_out must be at least the value returned by deflateBound (see
337ede78133Schristos   below).  Then deflate is guaranteed to return Z_STREAM_END.  If not enough
338ede78133Schristos   output space is provided, deflate will not return Z_STREAM_END, and it must
339ede78133Schristos   be called again as described above.
34075fd0b74Schristos 
341ede78133Schristos     deflate() sets strm->adler to the Adler-32 checksum of all input read
342ede78133Schristos   so far (that is, total_in bytes).  If a gzip stream is being generated, then
343ede78133Schristos   strm->adler will be the CRC-32 checksum of the input read so far.  (See
344ede78133Schristos   deflateInit2 below.)
34575fd0b74Schristos 
34675fd0b74Schristos     deflate() may update strm->data_type if it can make a good guess about
347ede78133Schristos   the input data type (Z_BINARY or Z_TEXT).  If in doubt, the data is
348ede78133Schristos   considered binary.  This field is only for information purposes and does not
349ede78133Schristos   affect the compression algorithm in any manner.
35075fd0b74Schristos 
35175fd0b74Schristos     deflate() returns Z_OK if some progress has been made (more input
35275fd0b74Schristos   processed or more output produced), Z_STREAM_END if all input has been
35375fd0b74Schristos   consumed and all output has been produced (only when flush is set to
35475fd0b74Schristos   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
355ede78133Schristos   if next_in or next_out was Z_NULL or the state was inadvertently written over
356ede78133Schristos   by the application), or Z_BUF_ERROR if no progress is possible (for example
357ede78133Schristos   avail_in or avail_out was zero).  Note that Z_BUF_ERROR is not fatal, and
358ede78133Schristos   deflate() can be called again with more input and more output space to
359ede78133Schristos   continue compressing.
36075fd0b74Schristos */
36175fd0b74Schristos 
36275fd0b74Schristos 
36375fd0b74Schristos ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
36475fd0b74Schristos /*
36575fd0b74Schristos      All dynamically allocated data structures for this stream are freed.
36675fd0b74Schristos    This function discards any unprocessed input and does not flush any pending
36775fd0b74Schristos    output.
36875fd0b74Schristos 
36975fd0b74Schristos      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
37075fd0b74Schristos    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
37175fd0b74Schristos    prematurely (some input or output was discarded).  In the error case, msg
37275fd0b74Schristos    may be set but then points to a static string (which must not be
37375fd0b74Schristos    deallocated).
37475fd0b74Schristos */
37575fd0b74Schristos 
37675fd0b74Schristos 
37775fd0b74Schristos /*
37875fd0b74Schristos ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
37975fd0b74Schristos 
38075fd0b74Schristos      Initializes the internal stream state for decompression.  The fields
38175fd0b74Schristos    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
382ede78133Schristos    the caller.  In the current version of inflate, the provided input is not
383ede78133Schristos    read or consumed.  The allocation of a sliding window will be deferred to
384ede78133Schristos    the first call of inflate (if the decompression does not complete on the
385ede78133Schristos    first call).  If zalloc and zfree are set to Z_NULL, inflateInit updates
386ede78133Schristos    them to use default allocation functions.
38775fd0b74Schristos 
38875fd0b74Schristos      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
38975fd0b74Schristos    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
39075fd0b74Schristos    version assumed by the caller, or Z_STREAM_ERROR if the parameters are
39175fd0b74Schristos    invalid, such as a null pointer to the structure.  msg is set to null if
392ede78133Schristos    there is no error message.  inflateInit does not perform any decompression.
393ede78133Schristos    Actual decompression will be done by inflate().  So next_in, and avail_in,
394ede78133Schristos    next_out, and avail_out are unused and unchanged.  The current
395ede78133Schristos    implementation of inflateInit() does not process any header information --
396ede78133Schristos    that is deferred until inflate() is called.
39775fd0b74Schristos */
39875fd0b74Schristos 
39975fd0b74Schristos 
40075fd0b74Schristos ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
40175fd0b74Schristos /*
40275fd0b74Schristos     inflate decompresses as much data as possible, and stops when the input
40375fd0b74Schristos   buffer becomes empty or the output buffer becomes full.  It may introduce
40475fd0b74Schristos   some output latency (reading input without producing any output) except when
40575fd0b74Schristos   forced to flush.
40675fd0b74Schristos 
40775fd0b74Schristos   The detailed semantics are as follows.  inflate performs one or both of the
40875fd0b74Schristos   following actions:
40975fd0b74Schristos 
41075fd0b74Schristos   - Decompress more input starting at next_in and update next_in and avail_in
41175fd0b74Schristos     accordingly.  If not all input can be processed (because there is not
412ede78133Schristos     enough room in the output buffer), then next_in and avail_in are updated
413ede78133Schristos     accordingly, and processing will resume at this point for the next call of
414ede78133Schristos     inflate().
41575fd0b74Schristos 
416ede78133Schristos   - Generate more output starting at next_out and update next_out and avail_out
41775fd0b74Schristos     accordingly.  inflate() provides as much output as possible, until there is
41875fd0b74Schristos     no more input data or no more space in the output buffer (see below about
41975fd0b74Schristos     the flush parameter).
42075fd0b74Schristos 
42175fd0b74Schristos     Before the call of inflate(), the application should ensure that at least
42275fd0b74Schristos   one of the actions is possible, by providing more input and/or consuming more
423ede78133Schristos   output, and updating the next_* and avail_* values accordingly.  If the
424ede78133Schristos   caller of inflate() does not provide both available input and available
425ede78133Schristos   output space, it is possible that there will be no progress made.  The
42675fd0b74Schristos   application can consume the uncompressed output when it wants, for example
42775fd0b74Schristos   when the output buffer is full (avail_out == 0), or after each call of
42875fd0b74Schristos   inflate().  If inflate returns Z_OK and with zero avail_out, it must be
42975fd0b74Schristos   called again after making room in the output buffer because there might be
43075fd0b74Schristos   more output pending.
43175fd0b74Schristos 
43275fd0b74Schristos     The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
43375fd0b74Schristos   Z_BLOCK, or Z_TREES.  Z_SYNC_FLUSH requests that inflate() flush as much
43475fd0b74Schristos   output as possible to the output buffer.  Z_BLOCK requests that inflate()
43575fd0b74Schristos   stop if and when it gets to the next deflate block boundary.  When decoding
43675fd0b74Schristos   the zlib or gzip format, this will cause inflate() to return immediately
43775fd0b74Schristos   after the header and before the first block.  When doing a raw inflate,
43875fd0b74Schristos   inflate() will go ahead and process the first block, and will return when it
43975fd0b74Schristos   gets to the end of that block, or when it runs out of data.
44075fd0b74Schristos 
44175fd0b74Schristos     The Z_BLOCK option assists in appending to or combining deflate streams.
442ede78133Schristos   To assist in this, on return inflate() always sets strm->data_type to the
44375fd0b74Schristos   number of unused bits in the last byte taken from strm->next_in, plus 64 if
44475fd0b74Schristos   inflate() is currently decoding the last block in the deflate stream, plus
44575fd0b74Schristos   128 if inflate() returned immediately after decoding an end-of-block code or
44675fd0b74Schristos   decoding the complete header up to just before the first byte of the deflate
44775fd0b74Schristos   stream.  The end-of-block will not be indicated until all of the uncompressed
44875fd0b74Schristos   data from that block has been written to strm->next_out.  The number of
44975fd0b74Schristos   unused bits may in general be greater than seven, except when bit 7 of
45075fd0b74Schristos   data_type is set, in which case the number of unused bits will be less than
45175fd0b74Schristos   eight.  data_type is set as noted here every time inflate() returns for all
45275fd0b74Schristos   flush options, and so can be used to determine the amount of currently
45375fd0b74Schristos   consumed input in bits.
45475fd0b74Schristos 
45575fd0b74Schristos     The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
45675fd0b74Schristos   end of each deflate block header is reached, before any actual data in that
45775fd0b74Schristos   block is decoded.  This allows the caller to determine the length of the
45875fd0b74Schristos   deflate block header for later use in random access within a deflate block.
45975fd0b74Schristos   256 is added to the value of strm->data_type when inflate() returns
46075fd0b74Schristos   immediately after reaching the end of the deflate block header.
46175fd0b74Schristos 
46275fd0b74Schristos     inflate() should normally be called until it returns Z_STREAM_END or an
46375fd0b74Schristos   error.  However if all decompression is to be performed in a single step (a
46475fd0b74Schristos   single call of inflate), the parameter flush should be set to Z_FINISH.  In
46575fd0b74Schristos   this case all pending input is processed and all pending output is flushed;
46675fd0b74Schristos   avail_out must be large enough to hold all of the uncompressed data for the
46775fd0b74Schristos   operation to complete.  (The size of the uncompressed data may have been
46875fd0b74Schristos   saved by the compressor for this purpose.)  The use of Z_FINISH is not
46975fd0b74Schristos   required to perform an inflation in one step.  However it may be used to
47075fd0b74Schristos   inform inflate that a faster approach can be used for the single inflate()
47175fd0b74Schristos   call.  Z_FINISH also informs inflate to not maintain a sliding window if the
47275fd0b74Schristos   stream completes, which reduces inflate's memory footprint.  If the stream
47375fd0b74Schristos   does not complete, either because not all of the stream is provided or not
47475fd0b74Schristos   enough output space is provided, then a sliding window will be allocated and
47575fd0b74Schristos   inflate() can be called again to continue the operation as if Z_NO_FLUSH had
47675fd0b74Schristos   been used.
47775fd0b74Schristos 
47875fd0b74Schristos      In this implementation, inflate() always flushes as much output as
47975fd0b74Schristos   possible to the output buffer, and always uses the faster approach on the
48075fd0b74Schristos   first call.  So the effects of the flush parameter in this implementation are
48175fd0b74Schristos   on the return value of inflate() as noted below, when inflate() returns early
48275fd0b74Schristos   when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of
48375fd0b74Schristos   memory for a sliding window when Z_FINISH is used.
48475fd0b74Schristos 
48575fd0b74Schristos      If a preset dictionary is needed after this call (see inflateSetDictionary
48675fd0b74Schristos   below), inflate sets strm->adler to the Adler-32 checksum of the dictionary
48775fd0b74Schristos   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
48875fd0b74Schristos   strm->adler to the Adler-32 checksum of all output produced so far (that is,
48975fd0b74Schristos   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
490ede78133Schristos   below.  At the end of the stream, inflate() checks that its computed Adler-32
49175fd0b74Schristos   checksum is equal to that saved by the compressor and returns Z_STREAM_END
49275fd0b74Schristos   only if the checksum is correct.
49375fd0b74Schristos 
49475fd0b74Schristos     inflate() can decompress and check either zlib-wrapped or gzip-wrapped
49575fd0b74Schristos   deflate data.  The header type is detected automatically, if requested when
49675fd0b74Schristos   initializing with inflateInit2().  Any information contained in the gzip
497ede78133Schristos   header is not retained unless inflateGetHeader() is used.  When processing
49875fd0b74Schristos   gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
499ede78133Schristos   produced so far.  The CRC-32 is checked against the gzip trailer, as is the
500ede78133Schristos   uncompressed length, modulo 2^32.
50175fd0b74Schristos 
50275fd0b74Schristos     inflate() returns Z_OK if some progress has been made (more input processed
50375fd0b74Schristos   or more output produced), Z_STREAM_END if the end of the compressed data has
50475fd0b74Schristos   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
50575fd0b74Schristos   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
50675fd0b74Schristos   corrupted (input stream not conforming to the zlib format or incorrect check
507ede78133Schristos   value, in which case strm->msg points to a string with a more specific
508ede78133Schristos   error), Z_STREAM_ERROR if the stream structure was inconsistent (for example
509ede78133Schristos   next_in or next_out was Z_NULL, or the state was inadvertently written over
510ede78133Schristos   by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR
511ede78133Schristos   if no progress was possible or if there was not enough room in the output
512ede78133Schristos   buffer when Z_FINISH is used.  Note that Z_BUF_ERROR is not fatal, and
51375fd0b74Schristos   inflate() can be called again with more input and more output space to
51475fd0b74Schristos   continue decompressing.  If Z_DATA_ERROR is returned, the application may
51575fd0b74Schristos   then call inflateSync() to look for a good compression block if a partial
516ede78133Schristos   recovery of the data is to be attempted.
51775fd0b74Schristos */
51875fd0b74Schristos 
51975fd0b74Schristos 
52075fd0b74Schristos ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
52175fd0b74Schristos /*
52275fd0b74Schristos      All dynamically allocated data structures for this stream are freed.
52375fd0b74Schristos    This function discards any unprocessed input and does not flush any pending
52475fd0b74Schristos    output.
52575fd0b74Schristos 
526ede78133Schristos      inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state
527ede78133Schristos    was inconsistent.
52875fd0b74Schristos */
52975fd0b74Schristos 
53075fd0b74Schristos 
53175fd0b74Schristos                         /* Advanced functions */
53275fd0b74Schristos 
53375fd0b74Schristos /*
53475fd0b74Schristos     The following functions are needed only in some special applications.
53575fd0b74Schristos */
53675fd0b74Schristos 
53775fd0b74Schristos /*
53875fd0b74Schristos ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
53975fd0b74Schristos                                      int  level,
54075fd0b74Schristos                                      int  method,
54175fd0b74Schristos                                      int  windowBits,
54275fd0b74Schristos                                      int  memLevel,
54375fd0b74Schristos                                      int  strategy));
54475fd0b74Schristos 
54575fd0b74Schristos      This is another version of deflateInit with more compression options.  The
546*e992f068Schristos    fields zalloc, zfree and opaque must be initialized before by the caller.
54775fd0b74Schristos 
54875fd0b74Schristos      The method parameter is the compression method.  It must be Z_DEFLATED in
54975fd0b74Schristos    this version of the library.
55075fd0b74Schristos 
55175fd0b74Schristos      The windowBits parameter is the base two logarithm of the window size
55275fd0b74Schristos    (the size of the history buffer).  It should be in the range 8..15 for this
55375fd0b74Schristos    version of the library.  Larger values of this parameter result in better
55475fd0b74Schristos    compression at the expense of memory usage.  The default value is 15 if
55575fd0b74Schristos    deflateInit is used instead.
55675fd0b74Schristos 
557ede78133Schristos      For the current implementation of deflate(), a windowBits value of 8 (a
558ede78133Schristos    window size of 256 bytes) is not supported.  As a result, a request for 8
559ede78133Schristos    will result in 9 (a 512-byte window).  In that case, providing 8 to
560ede78133Schristos    inflateInit2() will result in an error when the zlib header with 9 is
561ede78133Schristos    checked against the initialization of inflate().  The remedy is to not use 8
562ede78133Schristos    with deflateInit2() with this initialization, or at least in that case use 9
563ede78133Schristos    with inflateInit2().
564ede78133Schristos 
56575fd0b74Schristos      windowBits can also be -8..-15 for raw deflate.  In this case, -windowBits
56675fd0b74Schristos    determines the window size.  deflate() will then generate raw deflate data
567ede78133Schristos    with no zlib header or trailer, and will not compute a check value.
56875fd0b74Schristos 
56975fd0b74Schristos      windowBits can also be greater than 15 for optional gzip encoding.  Add
57075fd0b74Schristos    16 to windowBits to write a simple gzip header and trailer around the
57175fd0b74Schristos    compressed data instead of a zlib wrapper.  The gzip header will have no
57275fd0b74Schristos    file name, no extra data, no comment, no modification time (set to zero), no
573ede78133Schristos    header crc, and the operating system will be set to the appropriate value,
574ede78133Schristos    if the operating system was determined at compile time.  If a gzip stream is
575ede78133Schristos    being written, strm->adler is a CRC-32 instead of an Adler-32.
576ede78133Schristos 
577ede78133Schristos      For raw deflate or gzip encoding, a request for a 256-byte window is
578ede78133Schristos    rejected as invalid, since only the zlib header provides a means of
579ede78133Schristos    transmitting the window size to the decompressor.
58075fd0b74Schristos 
58175fd0b74Schristos      The memLevel parameter specifies how much memory should be allocated
58275fd0b74Schristos    for the internal compression state.  memLevel=1 uses minimum memory but is
58375fd0b74Schristos    slow and reduces compression ratio; memLevel=9 uses maximum memory for
58475fd0b74Schristos    optimal speed.  The default value is 8.  See zconf.h for total memory usage
58575fd0b74Schristos    as a function of windowBits and memLevel.
58675fd0b74Schristos 
58775fd0b74Schristos      The strategy parameter is used to tune the compression algorithm.  Use the
58875fd0b74Schristos    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
58975fd0b74Schristos    filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
59075fd0b74Schristos    string match), or Z_RLE to limit match distances to one (run-length
59175fd0b74Schristos    encoding).  Filtered data consists mostly of small values with a somewhat
59275fd0b74Schristos    random distribution.  In this case, the compression algorithm is tuned to
59375fd0b74Schristos    compress them better.  The effect of Z_FILTERED is to force more Huffman
59475fd0b74Schristos    coding and less string matching; it is somewhat intermediate between
59575fd0b74Schristos    Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY.  Z_RLE is designed to be almost as
59675fd0b74Schristos    fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data.  The
59775fd0b74Schristos    strategy parameter only affects the compression ratio but not the
59875fd0b74Schristos    correctness of the compressed output even if it is not set appropriately.
59975fd0b74Schristos    Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
60075fd0b74Schristos    decoder for special applications.
60175fd0b74Schristos 
60275fd0b74Schristos      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
60375fd0b74Schristos    memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
60475fd0b74Schristos    method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
60575fd0b74Schristos    incompatible with the version assumed by the caller (ZLIB_VERSION).  msg is
60675fd0b74Schristos    set to null if there is no error message.  deflateInit2 does not perform any
60775fd0b74Schristos    compression: this will be done by deflate().
60875fd0b74Schristos */
60975fd0b74Schristos 
61075fd0b74Schristos ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
61175fd0b74Schristos                                              const Bytef *dictionary,
61275fd0b74Schristos                                              uInt  dictLength));
61375fd0b74Schristos /*
61475fd0b74Schristos      Initializes the compression dictionary from the given byte sequence
61575fd0b74Schristos    without producing any compressed output.  When using the zlib format, this
61675fd0b74Schristos    function must be called immediately after deflateInit, deflateInit2 or
61775fd0b74Schristos    deflateReset, and before any call of deflate.  When doing raw deflate, this
61875fd0b74Schristos    function must be called either before any call of deflate, or immediately
61975fd0b74Schristos    after the completion of a deflate block, i.e. after all input has been
62075fd0b74Schristos    consumed and all output has been delivered when using any of the flush
62175fd0b74Schristos    options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH.  The
62275fd0b74Schristos    compressor and decompressor must use exactly the same dictionary (see
62375fd0b74Schristos    inflateSetDictionary).
62475fd0b74Schristos 
62575fd0b74Schristos      The dictionary should consist of strings (byte sequences) that are likely
62675fd0b74Schristos    to be encountered later in the data to be compressed, with the most commonly
62775fd0b74Schristos    used strings preferably put towards the end of the dictionary.  Using a
62875fd0b74Schristos    dictionary is most useful when the data to be compressed is short and can be
62975fd0b74Schristos    predicted with good accuracy; the data can then be compressed better than
63075fd0b74Schristos    with the default empty dictionary.
63175fd0b74Schristos 
63275fd0b74Schristos      Depending on the size of the compression data structures selected by
63375fd0b74Schristos    deflateInit or deflateInit2, a part of the dictionary may in effect be
63475fd0b74Schristos    discarded, for example if the dictionary is larger than the window size
63575fd0b74Schristos    provided in deflateInit or deflateInit2.  Thus the strings most likely to be
63675fd0b74Schristos    useful should be put at the end of the dictionary, not at the front.  In
63775fd0b74Schristos    addition, the current implementation of deflate will use at most the window
63875fd0b74Schristos    size minus 262 bytes of the provided dictionary.
63975fd0b74Schristos 
640ede78133Schristos      Upon return of this function, strm->adler is set to the Adler-32 value
64175fd0b74Schristos    of the dictionary; the decompressor may later use this value to determine
642ede78133Schristos    which dictionary has been used by the compressor.  (The Adler-32 value
64375fd0b74Schristos    applies to the whole dictionary even if only a subset of the dictionary is
64475fd0b74Schristos    actually used by the compressor.) If a raw deflate was requested, then the
645ede78133Schristos    Adler-32 value is not computed and strm->adler is not set.
64675fd0b74Schristos 
64775fd0b74Schristos      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
64875fd0b74Schristos    parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
64975fd0b74Schristos    inconsistent (for example if deflate has already been called for this stream
65075fd0b74Schristos    or if not at a block boundary for raw deflate).  deflateSetDictionary does
65175fd0b74Schristos    not perform any compression: this will be done by deflate().
65275fd0b74Schristos */
65375fd0b74Schristos 
654ede78133Schristos ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm,
655ede78133Schristos                                              Bytef *dictionary,
656ede78133Schristos                                              uInt  *dictLength));
657ede78133Schristos /*
658ede78133Schristos      Returns the sliding dictionary being maintained by deflate.  dictLength is
659ede78133Schristos    set to the number of bytes in the dictionary, and that many bytes are copied
660ede78133Schristos    to dictionary.  dictionary must have enough space, where 32768 bytes is
661ede78133Schristos    always enough.  If deflateGetDictionary() is called with dictionary equal to
662ede78133Schristos    Z_NULL, then only the dictionary length is returned, and nothing is copied.
663ede78133Schristos    Similary, if dictLength is Z_NULL, then it is not set.
664ede78133Schristos 
665ede78133Schristos      deflateGetDictionary() may return a length less than the window size, even
666ede78133Schristos    when more than the window size in input has been provided. It may return up
667ede78133Schristos    to 258 bytes less in that case, due to how zlib's implementation of deflate
668ede78133Schristos    manages the sliding window and lookahead for matches, where matches can be
669ede78133Schristos    up to 258 bytes long. If the application needs the last window-size bytes of
670ede78133Schristos    input, then that would need to be saved by the application outside of zlib.
671ede78133Schristos 
672ede78133Schristos      deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
673ede78133Schristos    stream state is inconsistent.
674ede78133Schristos */
675ede78133Schristos 
67675fd0b74Schristos ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
67775fd0b74Schristos                                     z_streamp source));
67875fd0b74Schristos /*
67975fd0b74Schristos      Sets the destination stream as a complete copy of the source stream.
68075fd0b74Schristos 
68175fd0b74Schristos      This function can be useful when several compression strategies will be
68275fd0b74Schristos    tried, for example when there are several ways of pre-processing the input
68375fd0b74Schristos    data with a filter.  The streams that will be discarded should then be freed
68475fd0b74Schristos    by calling deflateEnd.  Note that deflateCopy duplicates the internal
68575fd0b74Schristos    compression state which can be quite large, so this strategy is slow and can
68675fd0b74Schristos    consume lots of memory.
68775fd0b74Schristos 
68875fd0b74Schristos      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
68975fd0b74Schristos    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
69075fd0b74Schristos    (such as zalloc being Z_NULL).  msg is left unchanged in both source and
69175fd0b74Schristos    destination.
69275fd0b74Schristos */
69375fd0b74Schristos 
69475fd0b74Schristos ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
69575fd0b74Schristos /*
696ede78133Schristos      This function is equivalent to deflateEnd followed by deflateInit, but
697ede78133Schristos    does not free and reallocate the internal compression state.  The stream
698ede78133Schristos    will leave the compression level and any other attributes that may have been
699ede78133Schristos    set unchanged.
70075fd0b74Schristos 
70175fd0b74Schristos      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
70275fd0b74Schristos    stream state was inconsistent (such as zalloc or state being Z_NULL).
70375fd0b74Schristos */
70475fd0b74Schristos 
70575fd0b74Schristos ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
70675fd0b74Schristos                                       int level,
70775fd0b74Schristos                                       int strategy));
70875fd0b74Schristos /*
70975fd0b74Schristos      Dynamically update the compression level and compression strategy.  The
710ede78133Schristos    interpretation of level and strategy is as in deflateInit2().  This can be
71175fd0b74Schristos    used to switch between compression and straight copy of the input data, or
71275fd0b74Schristos    to switch to a different kind of input data requiring a different strategy.
713ede78133Schristos    If the compression approach (which is a function of the level) or the
714*e992f068Schristos    strategy is changed, and if there have been any deflate() calls since the
715*e992f068Schristos    state was initialized or reset, then the input available so far is
716*e992f068Schristos    compressed with the old level and strategy using deflate(strm, Z_BLOCK).
717*e992f068Schristos    There are three approaches for the compression levels 0, 1..3, and 4..9
718*e992f068Schristos    respectively.  The new level and strategy will take effect at the next call
719*e992f068Schristos    of deflate().
72075fd0b74Schristos 
721ede78133Schristos      If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
722ede78133Schristos    not have enough output space to complete, then the parameter change will not
723ede78133Schristos    take effect.  In this case, deflateParams() can be called again with the
724ede78133Schristos    same parameters and more output space to try again.
72575fd0b74Schristos 
726ede78133Schristos      In order to assure a change in the parameters on the first try, the
727ede78133Schristos    deflate stream should be flushed using deflate() with Z_BLOCK or other flush
728ede78133Schristos    request until strm.avail_out is not zero, before calling deflateParams().
729ede78133Schristos    Then no more input data should be provided before the deflateParams() call.
730ede78133Schristos    If this is done, the old level and strategy will be applied to the data
731ede78133Schristos    compressed before deflateParams(), and the new level and strategy will be
732ede78133Schristos    applied to the the data compressed after deflateParams().
733ede78133Schristos 
734ede78133Schristos      deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream
735ede78133Schristos    state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if
736ede78133Schristos    there was not enough output space to complete the compression of the
737ede78133Schristos    available input data before a change in the strategy or approach.  Note that
738ede78133Schristos    in the case of a Z_BUF_ERROR, the parameters are not changed.  A return
739ede78133Schristos    value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be
740ede78133Schristos    retried with more output space.
74175fd0b74Schristos */
74275fd0b74Schristos 
74375fd0b74Schristos ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
74475fd0b74Schristos                                     int good_length,
74575fd0b74Schristos                                     int max_lazy,
74675fd0b74Schristos                                     int nice_length,
74775fd0b74Schristos                                     int max_chain));
74875fd0b74Schristos /*
74975fd0b74Schristos      Fine tune deflate's internal compression parameters.  This should only be
75075fd0b74Schristos    used by someone who understands the algorithm used by zlib's deflate for
75175fd0b74Schristos    searching for the best matching string, and even then only by the most
75275fd0b74Schristos    fanatic optimizer trying to squeeze out the last compressed bit for their
75375fd0b74Schristos    specific input data.  Read the deflate.c source code for the meaning of the
75475fd0b74Schristos    max_lazy, good_length, nice_length, and max_chain parameters.
75575fd0b74Schristos 
75675fd0b74Schristos      deflateTune() can be called after deflateInit() or deflateInit2(), and
75775fd0b74Schristos    returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
75875fd0b74Schristos  */
75975fd0b74Schristos 
76075fd0b74Schristos ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
76175fd0b74Schristos                                        uLong sourceLen));
76275fd0b74Schristos /*
76375fd0b74Schristos      deflateBound() returns an upper bound on the compressed size after
76475fd0b74Schristos    deflation of sourceLen bytes.  It must be called after deflateInit() or
76575fd0b74Schristos    deflateInit2(), and after deflateSetHeader(), if used.  This would be used
76675fd0b74Schristos    to allocate an output buffer for deflation in a single pass, and so would be
76775fd0b74Schristos    called before deflate().  If that first deflate() call is provided the
76875fd0b74Schristos    sourceLen input bytes, an output buffer allocated to the size returned by
76975fd0b74Schristos    deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed
77075fd0b74Schristos    to return Z_STREAM_END.  Note that it is possible for the compressed size to
77175fd0b74Schristos    be larger than the value returned by deflateBound() if flush options other
77275fd0b74Schristos    than Z_FINISH or Z_NO_FLUSH are used.
77375fd0b74Schristos */
77475fd0b74Schristos 
77575fd0b74Schristos ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm,
77675fd0b74Schristos                                        unsigned *pending,
77775fd0b74Schristos                                        int *bits));
77875fd0b74Schristos /*
77975fd0b74Schristos      deflatePending() returns the number of bytes and bits of output that have
78075fd0b74Schristos    been generated, but not yet provided in the available output.  The bytes not
78175fd0b74Schristos    provided would be due to the available output space having being consumed.
78275fd0b74Schristos    The number of bits of output not provided are between 0 and 7, where they
78375fd0b74Schristos    await more bits to join them in order to fill out a full byte.  If pending
78475fd0b74Schristos    or bits are Z_NULL, then those values are not set.
78575fd0b74Schristos 
78675fd0b74Schristos      deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
78775fd0b74Schristos    stream state was inconsistent.
78875fd0b74Schristos  */
78975fd0b74Schristos 
79075fd0b74Schristos ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
79175fd0b74Schristos                                      int bits,
79275fd0b74Schristos                                      int value));
79375fd0b74Schristos /*
79475fd0b74Schristos      deflatePrime() inserts bits in the deflate output stream.  The intent
79575fd0b74Schristos    is that this function is used to start off the deflate output with the bits
79675fd0b74Schristos    leftover from a previous deflate stream when appending to it.  As such, this
79775fd0b74Schristos    function can only be used for raw deflate, and must be used before the first
79875fd0b74Schristos    deflate() call after a deflateInit2() or deflateReset().  bits must be less
79975fd0b74Schristos    than or equal to 16, and that many of the least significant bits of value
80075fd0b74Schristos    will be inserted in the output.
80175fd0b74Schristos 
80275fd0b74Schristos      deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough
80375fd0b74Schristos    room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the
80475fd0b74Schristos    source stream state was inconsistent.
80575fd0b74Schristos */
80675fd0b74Schristos 
80775fd0b74Schristos ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
80875fd0b74Schristos                                          gz_headerp head));
80975fd0b74Schristos /*
81075fd0b74Schristos      deflateSetHeader() provides gzip header information for when a gzip
81175fd0b74Schristos    stream is requested by deflateInit2().  deflateSetHeader() may be called
81275fd0b74Schristos    after deflateInit2() or deflateReset() and before the first call of
81375fd0b74Schristos    deflate().  The text, time, os, extra field, name, and comment information
81475fd0b74Schristos    in the provided gz_header structure are written to the gzip header (xflag is
81575fd0b74Schristos    ignored -- the extra flags are set according to the compression level).  The
81675fd0b74Schristos    caller must assure that, if not Z_NULL, name and comment are terminated with
81775fd0b74Schristos    a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
81875fd0b74Schristos    available there.  If hcrc is true, a gzip header crc is included.  Note that
81975fd0b74Schristos    the current versions of the command-line version of gzip (up through version
82075fd0b74Schristos    1.3.x) do not support header crc's, and will report that it is a "multi-part
82175fd0b74Schristos    gzip file" and give up.
82275fd0b74Schristos 
82375fd0b74Schristos      If deflateSetHeader is not used, the default gzip header has text false,
82475fd0b74Schristos    the time set to zero, and os set to 255, with no extra, name, or comment
82575fd0b74Schristos    fields.  The gzip header is returned to the default state by deflateReset().
82675fd0b74Schristos 
82775fd0b74Schristos      deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
82875fd0b74Schristos    stream state was inconsistent.
82975fd0b74Schristos */
83075fd0b74Schristos 
83175fd0b74Schristos /*
83275fd0b74Schristos ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
83375fd0b74Schristos                                      int  windowBits));
83475fd0b74Schristos 
83575fd0b74Schristos      This is another version of inflateInit with an extra parameter.  The
83675fd0b74Schristos    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
83775fd0b74Schristos    before by the caller.
83875fd0b74Schristos 
83975fd0b74Schristos      The windowBits parameter is the base two logarithm of the maximum window
84075fd0b74Schristos    size (the size of the history buffer).  It should be in the range 8..15 for
84175fd0b74Schristos    this version of the library.  The default value is 15 if inflateInit is used
84275fd0b74Schristos    instead.  windowBits must be greater than or equal to the windowBits value
84375fd0b74Schristos    provided to deflateInit2() while compressing, or it must be equal to 15 if
84475fd0b74Schristos    deflateInit2() was not used.  If a compressed stream with a larger window
84575fd0b74Schristos    size is given as input, inflate() will return with the error code
84675fd0b74Schristos    Z_DATA_ERROR instead of trying to allocate a larger window.
84775fd0b74Schristos 
84875fd0b74Schristos      windowBits can also be zero to request that inflate use the window size in
84975fd0b74Schristos    the zlib header of the compressed stream.
85075fd0b74Schristos 
85175fd0b74Schristos      windowBits can also be -8..-15 for raw inflate.  In this case, -windowBits
85275fd0b74Schristos    determines the window size.  inflate() will then process raw deflate data,
85375fd0b74Schristos    not looking for a zlib or gzip header, not generating a check value, and not
85475fd0b74Schristos    looking for any check values for comparison at the end of the stream.  This
85575fd0b74Schristos    is for use with other formats that use the deflate compressed data format
85675fd0b74Schristos    such as zip.  Those formats provide their own check values.  If a custom
85775fd0b74Schristos    format is developed using the raw deflate format for compressed data, it is
858ede78133Schristos    recommended that a check value such as an Adler-32 or a CRC-32 be applied to
85975fd0b74Schristos    the uncompressed data as is done in the zlib, gzip, and zip formats.  For
86075fd0b74Schristos    most applications, the zlib format should be used as is.  Note that comments
86175fd0b74Schristos    above on the use in deflateInit2() applies to the magnitude of windowBits.
86275fd0b74Schristos 
86375fd0b74Schristos      windowBits can also be greater than 15 for optional gzip decoding.  Add
86475fd0b74Schristos    32 to windowBits to enable zlib and gzip decoding with automatic header
86575fd0b74Schristos    detection, or add 16 to decode only the gzip format (the zlib format will
86675fd0b74Schristos    return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is a
867ede78133Schristos    CRC-32 instead of an Adler-32.  Unlike the gunzip utility and gzread() (see
868*e992f068Schristos    below), inflate() will *not* automatically decode concatenated gzip members.
869*e992f068Schristos    inflate() will return Z_STREAM_END at the end of the gzip member.  The state
870*e992f068Schristos    would need to be reset to continue decoding a subsequent gzip member.  This
871*e992f068Schristos    *must* be done if there is more data after a gzip member, in order for the
872*e992f068Schristos    decompression to be compliant with the gzip standard (RFC 1952).
87375fd0b74Schristos 
87475fd0b74Schristos      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
87575fd0b74Schristos    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
87675fd0b74Schristos    version assumed by the caller, or Z_STREAM_ERROR if the parameters are
87775fd0b74Schristos    invalid, such as a null pointer to the structure.  msg is set to null if
87875fd0b74Schristos    there is no error message.  inflateInit2 does not perform any decompression
87975fd0b74Schristos    apart from possibly reading the zlib header if present: actual decompression
88075fd0b74Schristos    will be done by inflate().  (So next_in and avail_in may be modified, but
88175fd0b74Schristos    next_out and avail_out are unused and unchanged.) The current implementation
88275fd0b74Schristos    of inflateInit2() does not process any header information -- that is
88375fd0b74Schristos    deferred until inflate() is called.
88475fd0b74Schristos */
88575fd0b74Schristos 
88675fd0b74Schristos ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
88775fd0b74Schristos                                              const Bytef *dictionary,
88875fd0b74Schristos                                              uInt  dictLength));
88975fd0b74Schristos /*
89075fd0b74Schristos      Initializes the decompression dictionary from the given uncompressed byte
89175fd0b74Schristos    sequence.  This function must be called immediately after a call of inflate,
89275fd0b74Schristos    if that call returned Z_NEED_DICT.  The dictionary chosen by the compressor
893ede78133Schristos    can be determined from the Adler-32 value returned by that call of inflate.
89475fd0b74Schristos    The compressor and decompressor must use exactly the same dictionary (see
89575fd0b74Schristos    deflateSetDictionary).  For raw inflate, this function can be called at any
89675fd0b74Schristos    time to set the dictionary.  If the provided dictionary is smaller than the
89775fd0b74Schristos    window and there is already data in the window, then the provided dictionary
89875fd0b74Schristos    will amend what's there.  The application must insure that the dictionary
89975fd0b74Schristos    that was used for compression is provided.
90075fd0b74Schristos 
90175fd0b74Schristos      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
90275fd0b74Schristos    parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
90375fd0b74Schristos    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
904ede78133Schristos    expected one (incorrect Adler-32 value).  inflateSetDictionary does not
90575fd0b74Schristos    perform any decompression: this will be done by subsequent calls of
90675fd0b74Schristos    inflate().
90775fd0b74Schristos */
90875fd0b74Schristos 
90975fd0b74Schristos ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm,
91075fd0b74Schristos                                              Bytef *dictionary,
91175fd0b74Schristos                                              uInt  *dictLength));
91275fd0b74Schristos /*
91375fd0b74Schristos      Returns the sliding dictionary being maintained by inflate.  dictLength is
91475fd0b74Schristos    set to the number of bytes in the dictionary, and that many bytes are copied
91575fd0b74Schristos    to dictionary.  dictionary must have enough space, where 32768 bytes is
91675fd0b74Schristos    always enough.  If inflateGetDictionary() is called with dictionary equal to
91775fd0b74Schristos    Z_NULL, then only the dictionary length is returned, and nothing is copied.
91875fd0b74Schristos    Similary, if dictLength is Z_NULL, then it is not set.
91975fd0b74Schristos 
92075fd0b74Schristos      inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
92175fd0b74Schristos    stream state is inconsistent.
92275fd0b74Schristos */
92375fd0b74Schristos 
92475fd0b74Schristos ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
92575fd0b74Schristos /*
92675fd0b74Schristos      Skips invalid compressed data until a possible full flush point (see above
92775fd0b74Schristos    for the description of deflate with Z_FULL_FLUSH) can be found, or until all
92875fd0b74Schristos    available input is skipped.  No output is provided.
92975fd0b74Schristos 
93075fd0b74Schristos      inflateSync searches for a 00 00 FF FF pattern in the compressed data.
93175fd0b74Schristos    All full flush points have this pattern, but not all occurrences of this
93275fd0b74Schristos    pattern are full flush points.
93375fd0b74Schristos 
93475fd0b74Schristos      inflateSync returns Z_OK if a possible full flush point has been found,
93575fd0b74Schristos    Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point
93675fd0b74Schristos    has been found, or Z_STREAM_ERROR if the stream structure was inconsistent.
93775fd0b74Schristos    In the success case, the application may save the current current value of
93875fd0b74Schristos    total_in which indicates where valid compressed data was found.  In the
93975fd0b74Schristos    error case, the application may repeatedly call inflateSync, providing more
94075fd0b74Schristos    input each time, until success or end of the input data.
94175fd0b74Schristos */
94275fd0b74Schristos 
94375fd0b74Schristos ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
94475fd0b74Schristos                                     z_streamp source));
94575fd0b74Schristos /*
94675fd0b74Schristos      Sets the destination stream as a complete copy of the source stream.
94775fd0b74Schristos 
94875fd0b74Schristos      This function can be useful when randomly accessing a large stream.  The
94975fd0b74Schristos    first pass through the stream can periodically record the inflate state,
95075fd0b74Schristos    allowing restarting inflate at those points when randomly accessing the
95175fd0b74Schristos    stream.
95275fd0b74Schristos 
95375fd0b74Schristos      inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
95475fd0b74Schristos    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
95575fd0b74Schristos    (such as zalloc being Z_NULL).  msg is left unchanged in both source and
95675fd0b74Schristos    destination.
95775fd0b74Schristos */
95875fd0b74Schristos 
95975fd0b74Schristos ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
96075fd0b74Schristos /*
96175fd0b74Schristos      This function is equivalent to inflateEnd followed by inflateInit,
962ede78133Schristos    but does not free and reallocate the internal decompression state.  The
96375fd0b74Schristos    stream will keep attributes that may have been set by inflateInit2.
96475fd0b74Schristos 
96575fd0b74Schristos      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
96675fd0b74Schristos    stream state was inconsistent (such as zalloc or state being Z_NULL).
96775fd0b74Schristos */
96875fd0b74Schristos 
96975fd0b74Schristos ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
97075fd0b74Schristos                                       int windowBits));
97175fd0b74Schristos /*
97275fd0b74Schristos      This function is the same as inflateReset, but it also permits changing
97375fd0b74Schristos    the wrap and window size requests.  The windowBits parameter is interpreted
974ede78133Schristos    the same as it is for inflateInit2.  If the window size is changed, then the
975ede78133Schristos    memory allocated for the window is freed, and the window will be reallocated
976ede78133Schristos    by inflate() if needed.
97775fd0b74Schristos 
97875fd0b74Schristos      inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
97975fd0b74Schristos    stream state was inconsistent (such as zalloc or state being Z_NULL), or if
98075fd0b74Schristos    the windowBits parameter is invalid.
98175fd0b74Schristos */
98275fd0b74Schristos 
98375fd0b74Schristos ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
98475fd0b74Schristos                                      int bits,
98575fd0b74Schristos                                      int value));
98675fd0b74Schristos /*
98775fd0b74Schristos      This function inserts bits in the inflate input stream.  The intent is
98875fd0b74Schristos    that this function is used to start inflating at a bit position in the
98975fd0b74Schristos    middle of a byte.  The provided bits will be used before any bytes are used
99075fd0b74Schristos    from next_in.  This function should only be used with raw inflate, and
99175fd0b74Schristos    should be used before the first inflate() call after inflateInit2() or
99275fd0b74Schristos    inflateReset().  bits must be less than or equal to 16, and that many of the
99375fd0b74Schristos    least significant bits of value will be inserted in the input.
99475fd0b74Schristos 
99575fd0b74Schristos      If bits is negative, then the input stream bit buffer is emptied.  Then
99675fd0b74Schristos    inflatePrime() can be called again to put bits in the buffer.  This is used
99775fd0b74Schristos    to clear out bits leftover after feeding inflate a block description prior
99875fd0b74Schristos    to feeding inflate codes.
99975fd0b74Schristos 
100075fd0b74Schristos      inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
100175fd0b74Schristos    stream state was inconsistent.
100275fd0b74Schristos */
100375fd0b74Schristos 
100475fd0b74Schristos ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
100575fd0b74Schristos /*
100675fd0b74Schristos      This function returns two values, one in the lower 16 bits of the return
100775fd0b74Schristos    value, and the other in the remaining upper bits, obtained by shifting the
100875fd0b74Schristos    return value down 16 bits.  If the upper value is -1 and the lower value is
100975fd0b74Schristos    zero, then inflate() is currently decoding information outside of a block.
101075fd0b74Schristos    If the upper value is -1 and the lower value is non-zero, then inflate is in
101175fd0b74Schristos    the middle of a stored block, with the lower value equaling the number of
101275fd0b74Schristos    bytes from the input remaining to copy.  If the upper value is not -1, then
101375fd0b74Schristos    it is the number of bits back from the current bit position in the input of
101475fd0b74Schristos    the code (literal or length/distance pair) currently being processed.  In
101575fd0b74Schristos    that case the lower value is the number of bytes already emitted for that
101675fd0b74Schristos    code.
101775fd0b74Schristos 
101875fd0b74Schristos      A code is being processed if inflate is waiting for more input to complete
101975fd0b74Schristos    decoding of the code, or if it has completed decoding but is waiting for
102075fd0b74Schristos    more output space to write the literal or match data.
102175fd0b74Schristos 
102275fd0b74Schristos      inflateMark() is used to mark locations in the input data for random
102375fd0b74Schristos    access, which may be at bit positions, and to note those cases where the
102475fd0b74Schristos    output of a code may span boundaries of random access blocks.  The current
102575fd0b74Schristos    location in the input stream can be determined from avail_in and data_type
102675fd0b74Schristos    as noted in the description for the Z_BLOCK flush parameter for inflate.
102775fd0b74Schristos 
1028ede78133Schristos      inflateMark returns the value noted above, or -65536 if the provided
102975fd0b74Schristos    source stream state was inconsistent.
103075fd0b74Schristos */
103175fd0b74Schristos 
103275fd0b74Schristos ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
103375fd0b74Schristos                                          gz_headerp head));
103475fd0b74Schristos /*
103575fd0b74Schristos      inflateGetHeader() requests that gzip header information be stored in the
103675fd0b74Schristos    provided gz_header structure.  inflateGetHeader() may be called after
103775fd0b74Schristos    inflateInit2() or inflateReset(), and before the first call of inflate().
103875fd0b74Schristos    As inflate() processes the gzip stream, head->done is zero until the header
103975fd0b74Schristos    is completed, at which time head->done is set to one.  If a zlib stream is
104075fd0b74Schristos    being decoded, then head->done is set to -1 to indicate that there will be
104175fd0b74Schristos    no gzip header information forthcoming.  Note that Z_BLOCK or Z_TREES can be
104275fd0b74Schristos    used to force inflate() to return immediately after header processing is
104375fd0b74Schristos    complete and before any actual data is decompressed.
104475fd0b74Schristos 
104575fd0b74Schristos      The text, time, xflags, and os fields are filled in with the gzip header
104675fd0b74Schristos    contents.  hcrc is set to true if there is a header CRC.  (The header CRC
104775fd0b74Schristos    was valid if done is set to one.) If extra is not Z_NULL, then extra_max
104875fd0b74Schristos    contains the maximum number of bytes to write to extra.  Once done is true,
104975fd0b74Schristos    extra_len contains the actual extra field length, and extra contains the
105075fd0b74Schristos    extra field, or that field truncated if extra_max is less than extra_len.
105175fd0b74Schristos    If name is not Z_NULL, then up to name_max characters are written there,
105275fd0b74Schristos    terminated with a zero unless the length is greater than name_max.  If
105375fd0b74Schristos    comment is not Z_NULL, then up to comm_max characters are written there,
105475fd0b74Schristos    terminated with a zero unless the length is greater than comm_max.  When any
105575fd0b74Schristos    of extra, name, or comment are not Z_NULL and the respective field is not
105675fd0b74Schristos    present in the header, then that field is set to Z_NULL to signal its
105775fd0b74Schristos    absence.  This allows the use of deflateSetHeader() with the returned
105875fd0b74Schristos    structure to duplicate the header.  However if those fields are set to
105975fd0b74Schristos    allocated memory, then the application will need to save those pointers
106075fd0b74Schristos    elsewhere so that they can be eventually freed.
106175fd0b74Schristos 
106275fd0b74Schristos      If inflateGetHeader is not used, then the header information is simply
106375fd0b74Schristos    discarded.  The header is always checked for validity, including the header
106475fd0b74Schristos    CRC if present.  inflateReset() will reset the process to discard the header
106575fd0b74Schristos    information.  The application would need to call inflateGetHeader() again to
106675fd0b74Schristos    retrieve the header from the next gzip stream.
106775fd0b74Schristos 
106875fd0b74Schristos      inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
106975fd0b74Schristos    stream state was inconsistent.
107075fd0b74Schristos */
107175fd0b74Schristos 
107275fd0b74Schristos /*
107375fd0b74Schristos ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
107475fd0b74Schristos                                         unsigned char FAR *window));
107575fd0b74Schristos 
107675fd0b74Schristos      Initialize the internal stream state for decompression using inflateBack()
107775fd0b74Schristos    calls.  The fields zalloc, zfree and opaque in strm must be initialized
107875fd0b74Schristos    before the call.  If zalloc and zfree are Z_NULL, then the default library-
107975fd0b74Schristos    derived memory allocation routines are used.  windowBits is the base two
108075fd0b74Schristos    logarithm of the window size, in the range 8..15.  window is a caller
108175fd0b74Schristos    supplied buffer of that size.  Except for special applications where it is
108275fd0b74Schristos    assured that deflate was used with small window sizes, windowBits must be 15
108375fd0b74Schristos    and a 32K byte window must be supplied to be able to decompress general
108475fd0b74Schristos    deflate streams.
108575fd0b74Schristos 
108675fd0b74Schristos      See inflateBack() for the usage of these routines.
108775fd0b74Schristos 
108875fd0b74Schristos      inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
108975fd0b74Schristos    the parameters are invalid, Z_MEM_ERROR if the internal state could not be
109075fd0b74Schristos    allocated, or Z_VERSION_ERROR if the version of the library does not match
109175fd0b74Schristos    the version of the header file.
109275fd0b74Schristos */
109375fd0b74Schristos 
109475fd0b74Schristos typedef unsigned (*in_func) OF((void FAR *,
109575fd0b74Schristos                                 z_const unsigned char FAR * FAR *));
109675fd0b74Schristos typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
109775fd0b74Schristos 
109875fd0b74Schristos ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
109975fd0b74Schristos                                     in_func in, void FAR *in_desc,
110075fd0b74Schristos                                     out_func out, void FAR *out_desc));
110175fd0b74Schristos /*
110275fd0b74Schristos      inflateBack() does a raw inflate with a single call using a call-back
110375fd0b74Schristos    interface for input and output.  This is potentially more efficient than
110475fd0b74Schristos    inflate() for file i/o applications, in that it avoids copying between the
110575fd0b74Schristos    output and the sliding window by simply making the window itself the output
110675fd0b74Schristos    buffer.  inflate() can be faster on modern CPUs when used with large
110775fd0b74Schristos    buffers.  inflateBack() trusts the application to not change the output
110875fd0b74Schristos    buffer passed by the output function, at least until inflateBack() returns.
110975fd0b74Schristos 
111075fd0b74Schristos      inflateBackInit() must be called first to allocate the internal state
111175fd0b74Schristos    and to initialize the state with the user-provided window buffer.
111275fd0b74Schristos    inflateBack() may then be used multiple times to inflate a complete, raw
111375fd0b74Schristos    deflate stream with each call.  inflateBackEnd() is then called to free the
111475fd0b74Schristos    allocated state.
111575fd0b74Schristos 
111675fd0b74Schristos      A raw deflate stream is one with no zlib or gzip header or trailer.
111775fd0b74Schristos    This routine would normally be used in a utility that reads zip or gzip
111875fd0b74Schristos    files and writes out uncompressed files.  The utility would decode the
111975fd0b74Schristos    header and process the trailer on its own, hence this routine expects only
1120ede78133Schristos    the raw deflate stream to decompress.  This is different from the default
1121ede78133Schristos    behavior of inflate(), which expects a zlib header and trailer around the
1122ede78133Schristos    deflate stream.
112375fd0b74Schristos 
112475fd0b74Schristos      inflateBack() uses two subroutines supplied by the caller that are then
112575fd0b74Schristos    called by inflateBack() for input and output.  inflateBack() calls those
112675fd0b74Schristos    routines until it reads a complete deflate stream and writes out all of the
112775fd0b74Schristos    uncompressed data, or until it encounters an error.  The function's
112875fd0b74Schristos    parameters and return types are defined above in the in_func and out_func
112975fd0b74Schristos    typedefs.  inflateBack() will call in(in_desc, &buf) which should return the
113075fd0b74Schristos    number of bytes of provided input, and a pointer to that input in buf.  If
113175fd0b74Schristos    there is no input available, in() must return zero -- buf is ignored in that
1132ede78133Schristos    case -- and inflateBack() will return a buffer error.  inflateBack() will
1133ede78133Schristos    call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].
1134ede78133Schristos    out() should return zero on success, or non-zero on failure.  If out()
1135ede78133Schristos    returns non-zero, inflateBack() will return with an error.  Neither in() nor
1136ede78133Schristos    out() are permitted to change the contents of the window provided to
113775fd0b74Schristos    inflateBackInit(), which is also the buffer that out() uses to write from.
113875fd0b74Schristos    The length written by out() will be at most the window size.  Any non-zero
113975fd0b74Schristos    amount of input may be provided by in().
114075fd0b74Schristos 
114175fd0b74Schristos      For convenience, inflateBack() can be provided input on the first call by
114275fd0b74Schristos    setting strm->next_in and strm->avail_in.  If that input is exhausted, then
114375fd0b74Schristos    in() will be called.  Therefore strm->next_in must be initialized before
114475fd0b74Schristos    calling inflateBack().  If strm->next_in is Z_NULL, then in() will be called
114575fd0b74Schristos    immediately for input.  If strm->next_in is not Z_NULL, then strm->avail_in
114675fd0b74Schristos    must also be initialized, and then if strm->avail_in is not zero, input will
114775fd0b74Schristos    initially be taken from strm->next_in[0 ..  strm->avail_in - 1].
114875fd0b74Schristos 
114975fd0b74Schristos      The in_desc and out_desc parameters of inflateBack() is passed as the
115075fd0b74Schristos    first parameter of in() and out() respectively when they are called.  These
115175fd0b74Schristos    descriptors can be optionally used to pass any information that the caller-
115275fd0b74Schristos    supplied in() and out() functions need to do their job.
115375fd0b74Schristos 
115475fd0b74Schristos      On return, inflateBack() will set strm->next_in and strm->avail_in to
115575fd0b74Schristos    pass back any unused input that was provided by the last in() call.  The
115675fd0b74Schristos    return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
115775fd0b74Schristos    if in() or out() returned an error, Z_DATA_ERROR if there was a format error
115875fd0b74Schristos    in the deflate stream (in which case strm->msg is set to indicate the nature
115975fd0b74Schristos    of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
116075fd0b74Schristos    In the case of Z_BUF_ERROR, an input or output error can be distinguished
116175fd0b74Schristos    using strm->next_in which will be Z_NULL only if in() returned an error.  If
116275fd0b74Schristos    strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
116375fd0b74Schristos    non-zero.  (in() will always be called before out(), so strm->next_in is
116475fd0b74Schristos    assured to be defined if out() returns non-zero.)  Note that inflateBack()
116575fd0b74Schristos    cannot return Z_OK.
116675fd0b74Schristos */
116775fd0b74Schristos 
116875fd0b74Schristos ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
116975fd0b74Schristos /*
117075fd0b74Schristos      All memory allocated by inflateBackInit() is freed.
117175fd0b74Schristos 
117275fd0b74Schristos      inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
117375fd0b74Schristos    state was inconsistent.
117475fd0b74Schristos */
117575fd0b74Schristos 
117675fd0b74Schristos ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
117775fd0b74Schristos /* Return flags indicating compile-time options.
117875fd0b74Schristos 
117975fd0b74Schristos     Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
118075fd0b74Schristos      1.0: size of uInt
118175fd0b74Schristos      3.2: size of uLong
118275fd0b74Schristos      5.4: size of voidpf (pointer)
118375fd0b74Schristos      7.6: size of z_off_t
118475fd0b74Schristos 
118575fd0b74Schristos     Compiler, assembler, and debug options:
1186ede78133Schristos      8: ZLIB_DEBUG
118775fd0b74Schristos      9: ASMV or ASMINF -- use ASM code
118875fd0b74Schristos      10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
118975fd0b74Schristos      11: 0 (reserved)
119075fd0b74Schristos 
119175fd0b74Schristos     One-time table building (smaller code, but not thread-safe if true):
119275fd0b74Schristos      12: BUILDFIXED -- build static block decoding tables when needed
119375fd0b74Schristos      13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
119475fd0b74Schristos      14,15: 0 (reserved)
119575fd0b74Schristos 
119675fd0b74Schristos     Library content (indicates missing functionality):
119775fd0b74Schristos      16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
119875fd0b74Schristos                           deflate code when not needed)
119975fd0b74Schristos      17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
120075fd0b74Schristos                     and decode gzip streams (to avoid linking crc code)
120175fd0b74Schristos      18-19: 0 (reserved)
120275fd0b74Schristos 
120375fd0b74Schristos     Operation variations (changes in library functionality):
120475fd0b74Schristos      20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
120575fd0b74Schristos      21: FASTEST -- deflate algorithm with only one, lowest compression level
120675fd0b74Schristos      22,23: 0 (reserved)
120775fd0b74Schristos 
120875fd0b74Schristos     The sprintf variant used by gzprintf (zero is best):
120975fd0b74Schristos      24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
121075fd0b74Schristos      25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
121175fd0b74Schristos      26: 0 = returns value, 1 = void -- 1 means inferred string length returned
121275fd0b74Schristos 
121375fd0b74Schristos     Remainder:
121475fd0b74Schristos      27-31: 0 (reserved)
121575fd0b74Schristos  */
121675fd0b74Schristos 
121775fd0b74Schristos #ifndef Z_SOLO
121875fd0b74Schristos 
121975fd0b74Schristos                         /* utility functions */
122075fd0b74Schristos 
122175fd0b74Schristos /*
122275fd0b74Schristos      The following utility functions are implemented on top of the basic
122375fd0b74Schristos    stream-oriented functions.  To simplify the interface, some default options
122475fd0b74Schristos    are assumed (compression level and memory usage, standard memory allocation
122575fd0b74Schristos    functions).  The source code of these utility functions can be modified if
122675fd0b74Schristos    you need special options.
122775fd0b74Schristos */
122875fd0b74Schristos 
122975fd0b74Schristos ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
123075fd0b74Schristos                                  const Bytef *source, uLong sourceLen));
123175fd0b74Schristos /*
123275fd0b74Schristos      Compresses the source buffer into the destination buffer.  sourceLen is
123375fd0b74Schristos    the byte length of the source buffer.  Upon entry, destLen is the total size
123475fd0b74Schristos    of the destination buffer, which must be at least the value returned by
123575fd0b74Schristos    compressBound(sourceLen).  Upon exit, destLen is the actual size of the
1236ede78133Schristos    compressed data.  compress() is equivalent to compress2() with a level
1237ede78133Schristos    parameter of Z_DEFAULT_COMPRESSION.
123875fd0b74Schristos 
123975fd0b74Schristos      compress returns Z_OK if success, Z_MEM_ERROR if there was not
124075fd0b74Schristos    enough memory, Z_BUF_ERROR if there was not enough room in the output
124175fd0b74Schristos    buffer.
124275fd0b74Schristos */
124375fd0b74Schristos 
124475fd0b74Schristos ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
124575fd0b74Schristos                                   const Bytef *source, uLong sourceLen,
124675fd0b74Schristos                                   int level));
124775fd0b74Schristos /*
124875fd0b74Schristos      Compresses the source buffer into the destination buffer.  The level
124975fd0b74Schristos    parameter has the same meaning as in deflateInit.  sourceLen is the byte
125075fd0b74Schristos    length of the source buffer.  Upon entry, destLen is the total size of the
125175fd0b74Schristos    destination buffer, which must be at least the value returned by
125275fd0b74Schristos    compressBound(sourceLen).  Upon exit, destLen is the actual size of the
1253ede78133Schristos    compressed data.
125475fd0b74Schristos 
125575fd0b74Schristos      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
125675fd0b74Schristos    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
125775fd0b74Schristos    Z_STREAM_ERROR if the level parameter is invalid.
125875fd0b74Schristos */
125975fd0b74Schristos 
126075fd0b74Schristos ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
126175fd0b74Schristos /*
126275fd0b74Schristos      compressBound() returns an upper bound on the compressed size after
126375fd0b74Schristos    compress() or compress2() on sourceLen bytes.  It would be used before a
126475fd0b74Schristos    compress() or compress2() call to allocate the destination buffer.
126575fd0b74Schristos */
126675fd0b74Schristos 
126775fd0b74Schristos ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
126875fd0b74Schristos                                    const Bytef *source, uLong sourceLen));
126975fd0b74Schristos /*
127075fd0b74Schristos      Decompresses the source buffer into the destination buffer.  sourceLen is
127175fd0b74Schristos    the byte length of the source buffer.  Upon entry, destLen is the total size
127275fd0b74Schristos    of the destination buffer, which must be large enough to hold the entire
127375fd0b74Schristos    uncompressed data.  (The size of the uncompressed data must have been saved
127475fd0b74Schristos    previously by the compressor and transmitted to the decompressor by some
127575fd0b74Schristos    mechanism outside the scope of this compression library.) Upon exit, destLen
1276ede78133Schristos    is the actual size of the uncompressed data.
127775fd0b74Schristos 
127875fd0b74Schristos      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
127975fd0b74Schristos    enough memory, Z_BUF_ERROR if there was not enough room in the output
128075fd0b74Schristos    buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.  In
128175fd0b74Schristos    the case where there is not enough room, uncompress() will fill the output
128275fd0b74Schristos    buffer with the uncompressed data up to that point.
128375fd0b74Schristos */
128475fd0b74Schristos 
1285ede78133Schristos ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest,   uLongf *destLen,
1286ede78133Schristos                                     const Bytef *source, uLong *sourceLen));
1287ede78133Schristos /*
1288ede78133Schristos      Same as uncompress, except that sourceLen is a pointer, where the
1289ede78133Schristos    length of the source is *sourceLen.  On return, *sourceLen is the number of
1290ede78133Schristos    source bytes consumed.
1291ede78133Schristos */
1292ede78133Schristos 
129375fd0b74Schristos                         /* gzip file access functions */
129475fd0b74Schristos 
129575fd0b74Schristos /*
129675fd0b74Schristos      This library supports reading and writing files in gzip (.gz) format with
129775fd0b74Schristos    an interface similar to that of stdio, using the functions that start with
129875fd0b74Schristos    "gz".  The gzip format is different from the zlib format.  gzip is a gzip
129975fd0b74Schristos    wrapper, documented in RFC 1952, wrapped around a deflate stream.
130075fd0b74Schristos */
130175fd0b74Schristos 
130275fd0b74Schristos typedef struct gzFile_s *gzFile;    /* semi-opaque gzip file descriptor */
130375fd0b74Schristos 
130475fd0b74Schristos /*
130575fd0b74Schristos ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
130675fd0b74Schristos 
1307*e992f068Schristos      Open the gzip (.gz) file at path for reading and decompressing, or
1308*e992f068Schristos    compressing and writing.  The mode parameter is as in fopen ("rb" or "wb")
1309*e992f068Schristos    but can also include a compression level ("wb9") or a strategy: 'f' for
1310*e992f068Schristos    filtered data as in "wb6f", 'h' for Huffman-only compression as in "wb1h",
1311*e992f068Schristos    'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression
1312*e992f068Schristos    as in "wb9F".  (See the description of deflateInit2 for more information
1313*e992f068Schristos    about the strategy parameter.)  'T' will request transparent writing or
1314*e992f068Schristos    appending with no compression and not using the gzip format.
131575fd0b74Schristos 
131675fd0b74Schristos      "a" can be used instead of "w" to request that the gzip stream that will
131775fd0b74Schristos    be written be appended to the file.  "+" will result in an error, since
131875fd0b74Schristos    reading and writing to the same gzip file is not supported.  The addition of
131975fd0b74Schristos    "x" when writing will create the file exclusively, which fails if the file
132075fd0b74Schristos    already exists.  On systems that support it, the addition of "e" when
132175fd0b74Schristos    reading or writing will set the flag to close the file on an execve() call.
132275fd0b74Schristos 
132375fd0b74Schristos      These functions, as well as gzip, will read and decode a sequence of gzip
132475fd0b74Schristos    streams in a file.  The append function of gzopen() can be used to create
132575fd0b74Schristos    such a file.  (Also see gzflush() for another way to do this.)  When
132675fd0b74Schristos    appending, gzopen does not test whether the file begins with a gzip stream,
132775fd0b74Schristos    nor does it look for the end of the gzip streams to begin appending.  gzopen
132875fd0b74Schristos    will simply append a gzip stream to the existing file.
132975fd0b74Schristos 
133075fd0b74Schristos      gzopen can be used to read a file which is not in gzip format; in this
133175fd0b74Schristos    case gzread will directly read from the file without decompression.  When
133275fd0b74Schristos    reading, this will be detected automatically by looking for the magic two-
133375fd0b74Schristos    byte gzip header.
133475fd0b74Schristos 
133575fd0b74Schristos      gzopen returns NULL if the file could not be opened, if there was
133675fd0b74Schristos    insufficient memory to allocate the gzFile state, or if an invalid mode was
133775fd0b74Schristos    specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
133875fd0b74Schristos    errno can be checked to determine if the reason gzopen failed was that the
133975fd0b74Schristos    file could not be opened.
134075fd0b74Schristos */
134175fd0b74Schristos 
134275fd0b74Schristos ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
134375fd0b74Schristos /*
1344*e992f068Schristos      Associate a gzFile with the file descriptor fd.  File descriptors are
1345*e992f068Schristos    obtained from calls like open, dup, creat, pipe or fileno (if the file has
1346*e992f068Schristos    been previously opened with fopen).  The mode parameter is as in gzopen.
134775fd0b74Schristos 
134875fd0b74Schristos      The next call of gzclose on the returned gzFile will also close the file
134975fd0b74Schristos    descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
135075fd0b74Schristos    fd.  If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
135175fd0b74Schristos    mode);.  The duplicated descriptor should be saved to avoid a leak, since
135275fd0b74Schristos    gzdopen does not close fd if it fails.  If you are using fileno() to get the
135375fd0b74Schristos    file descriptor from a FILE *, then you will have to use dup() to avoid
135475fd0b74Schristos    double-close()ing the file descriptor.  Both gzclose() and fclose() will
135575fd0b74Schristos    close the associated file descriptor, so they need to have different file
135675fd0b74Schristos    descriptors.
135775fd0b74Schristos 
135875fd0b74Schristos      gzdopen returns NULL if there was insufficient memory to allocate the
135975fd0b74Schristos    gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
136075fd0b74Schristos    provided, or '+' was provided), or if fd is -1.  The file descriptor is not
136175fd0b74Schristos    used until the next gz* read, write, seek, or close operation, so gzdopen
136275fd0b74Schristos    will not detect if fd is invalid (unless fd is -1).
136375fd0b74Schristos */
136475fd0b74Schristos 
136575fd0b74Schristos ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
136675fd0b74Schristos /*
1367*e992f068Schristos      Set the internal buffer size used by this library's functions for file to
1368*e992f068Schristos    size.  The default buffer size is 8192 bytes.  This function must be called
1369*e992f068Schristos    after gzopen() or gzdopen(), and before any other calls that read or write
1370*e992f068Schristos    the file.  The buffer memory allocation is always deferred to the first read
1371*e992f068Schristos    or write.  Three times that size in buffer space is allocated.  A larger
1372*e992f068Schristos    buffer size of, for example, 64K or 128K bytes will noticeably increase the
1373*e992f068Schristos    speed of decompression (reading).
137475fd0b74Schristos 
137575fd0b74Schristos      The new buffer size also affects the maximum length for gzprintf().
137675fd0b74Schristos 
137775fd0b74Schristos      gzbuffer() returns 0 on success, or -1 on failure, such as being called
137875fd0b74Schristos    too late.
137975fd0b74Schristos */
138075fd0b74Schristos 
138175fd0b74Schristos ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
138275fd0b74Schristos /*
1383*e992f068Schristos      Dynamically update the compression level and strategy for file.  See the
1384*e992f068Schristos    description of deflateInit2 for the meaning of these parameters. Previously
1385*e992f068Schristos    provided data is flushed before applying the parameter changes.
138675fd0b74Schristos 
1387ede78133Schristos      gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
1388ede78133Schristos    opened for writing, Z_ERRNO if there is an error writing the flushed data,
1389ede78133Schristos    or Z_MEM_ERROR if there is a memory allocation error.
139075fd0b74Schristos */
139175fd0b74Schristos 
139275fd0b74Schristos ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
139375fd0b74Schristos /*
1394*e992f068Schristos      Read and decompress up to len uncompressed bytes from file into buf.  If
139575fd0b74Schristos    the input file is not in gzip format, gzread copies the given number of
139675fd0b74Schristos    bytes into the buffer directly from the file.
139775fd0b74Schristos 
139875fd0b74Schristos      After reaching the end of a gzip stream in the input, gzread will continue
139975fd0b74Schristos    to read, looking for another gzip stream.  Any number of gzip streams may be
140075fd0b74Schristos    concatenated in the input file, and will all be decompressed by gzread().
140175fd0b74Schristos    If something other than a gzip stream is encountered after a gzip stream,
140275fd0b74Schristos    that remaining trailing garbage is ignored (and no error is returned).
140375fd0b74Schristos 
140475fd0b74Schristos      gzread can be used to read a gzip file that is being concurrently written.
140575fd0b74Schristos    Upon reaching the end of the input, gzread will return with the available
140675fd0b74Schristos    data.  If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then
140775fd0b74Schristos    gzclearerr can be used to clear the end of file indicator in order to permit
140875fd0b74Schristos    gzread to be tried again.  Z_OK indicates that a gzip stream was completed
140975fd0b74Schristos    on the last gzread.  Z_BUF_ERROR indicates that the input file ended in the
141075fd0b74Schristos    middle of a gzip stream.  Note that gzread does not return -1 in the event
141175fd0b74Schristos    of an incomplete gzip stream.  This error is deferred until gzclose(), which
141275fd0b74Schristos    will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip
141375fd0b74Schristos    stream.  Alternatively, gzerror can be used before gzclose to detect this
141475fd0b74Schristos    case.
141575fd0b74Schristos 
141675fd0b74Schristos      gzread returns the number of uncompressed bytes actually read, less than
1417ede78133Schristos    len for end of file, or -1 for error.  If len is too large to fit in an int,
1418ede78133Schristos    then nothing is read, -1 is returned, and the error state is set to
1419ede78133Schristos    Z_STREAM_ERROR.
1420ede78133Schristos */
1421ede78133Schristos 
1422ede78133Schristos ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems,
1423ede78133Schristos                                      gzFile file));
1424ede78133Schristos /*
1425*e992f068Schristos      Read and decompress up to nitems items of size size from file into buf,
1426*e992f068Schristos    otherwise operating as gzread() does.  This duplicates the interface of
1427*e992f068Schristos    stdio's fread(), with size_t request and return types.  If the library
1428*e992f068Schristos    defines size_t, then z_size_t is identical to size_t.  If not, then z_size_t
1429*e992f068Schristos    is an unsigned integer type that can contain a pointer.
1430ede78133Schristos 
1431ede78133Schristos      gzfread() returns the number of full items read of size size, or zero if
1432ede78133Schristos    the end of the file was reached and a full item could not be read, or if
1433ede78133Schristos    there was an error.  gzerror() must be consulted if zero is returned in
1434ede78133Schristos    order to determine if there was an error.  If the multiplication of size and
1435ede78133Schristos    nitems overflows, i.e. the product does not fit in a z_size_t, then nothing
1436ede78133Schristos    is read, zero is returned, and the error state is set to Z_STREAM_ERROR.
1437ede78133Schristos 
1438ede78133Schristos      In the event that the end of file is reached and only a partial item is
1439ede78133Schristos    available at the end, i.e. the remaining uncompressed data length is not a
1440ede78133Schristos    multiple of size, then the final partial item is nevetheless read into buf
1441ede78133Schristos    and the end-of-file flag is set.  The length of the partial item read is not
1442ede78133Schristos    provided, but could be inferred from the result of gztell().  This behavior
1443ede78133Schristos    is the same as the behavior of fread() implementations in common libraries,
1444ede78133Schristos    but it prevents the direct use of gzfread() to read a concurrently written
1445ede78133Schristos    file, reseting and retrying on end-of-file, when size is not 1.
144675fd0b74Schristos */
144775fd0b74Schristos 
1448*e992f068Schristos ZEXTERN int ZEXPORT gzwrite OF((gzFile file, voidpc buf, unsigned len));
144975fd0b74Schristos /*
1450*e992f068Schristos      Compress and write the len uncompressed bytes at buf to file. gzwrite
1451*e992f068Schristos    returns the number of uncompressed bytes written or 0 in case of error.
145275fd0b74Schristos */
145375fd0b74Schristos 
1454ede78133Schristos ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size,
1455ede78133Schristos                                       z_size_t nitems, gzFile file));
1456ede78133Schristos /*
1457*e992f068Schristos      Compress and write nitems items of size size from buf to file, duplicating
1458ede78133Schristos    the interface of stdio's fwrite(), with size_t request and return types.  If
1459ede78133Schristos    the library defines size_t, then z_size_t is identical to size_t.  If not,
1460ede78133Schristos    then z_size_t is an unsigned integer type that can contain a pointer.
1461ede78133Schristos 
1462ede78133Schristos      gzfwrite() returns the number of full items written of size size, or zero
1463ede78133Schristos    if there was an error.  If the multiplication of size and nitems overflows,
1464ede78133Schristos    i.e. the product does not fit in a z_size_t, then nothing is written, zero
1465ede78133Schristos    is returned, and the error state is set to Z_STREAM_ERROR.
1466ede78133Schristos */
1467ede78133Schristos 
146875fd0b74Schristos ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
146975fd0b74Schristos /*
1470*e992f068Schristos      Convert, format, compress, and write the arguments (...) to file under
1471*e992f068Schristos    control of the string format, as in fprintf.  gzprintf returns the number of
1472ede78133Schristos    uncompressed bytes actually written, or a negative zlib error code in case
1473ede78133Schristos    of error.  The number of uncompressed bytes written is limited to 8191, or
1474ede78133Schristos    one less than the buffer size given to gzbuffer().  The caller should assure
1475ede78133Schristos    that this limit is not exceeded.  If it is exceeded, then gzprintf() will
1476ede78133Schristos    return an error (0) with nothing written.  In this case, there may also be a
1477ede78133Schristos    buffer overflow with unpredictable consequences, which is possible only if
1478*e992f068Schristos    zlib was compiled with the insecure functions sprintf() or vsprintf(),
1479ede78133Schristos    because the secure snprintf() or vsnprintf() functions were not available.
1480ede78133Schristos    This can be determined using zlibCompileFlags().
148175fd0b74Schristos */
148275fd0b74Schristos 
148375fd0b74Schristos ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
148475fd0b74Schristos /*
1485*e992f068Schristos      Compress and write the given null-terminated string s to file, excluding
148675fd0b74Schristos    the terminating null character.
148775fd0b74Schristos 
148875fd0b74Schristos      gzputs returns the number of characters written, or -1 in case of error.
148975fd0b74Schristos */
149075fd0b74Schristos 
149175fd0b74Schristos ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
149275fd0b74Schristos /*
1493*e992f068Schristos      Read and decompress bytes from file into buf, until len-1 characters are
1494*e992f068Schristos    read, or until a newline character is read and transferred to buf, or an
1495*e992f068Schristos    end-of-file condition is encountered.  If any characters are read or if len
1496*e992f068Schristos    is one, the string is terminated with a null character.  If no characters
1497*e992f068Schristos    are read due to an end-of-file or len is less than one, then the buffer is
1498*e992f068Schristos    left untouched.
149975fd0b74Schristos 
150075fd0b74Schristos      gzgets returns buf which is a null-terminated string, or it returns NULL
150175fd0b74Schristos    for end-of-file or in case of error.  If there was an error, the contents at
150275fd0b74Schristos    buf are indeterminate.
150375fd0b74Schristos */
150475fd0b74Schristos 
150575fd0b74Schristos ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
150675fd0b74Schristos /*
1507*e992f068Schristos      Compress and write c, converted to an unsigned char, into file.  gzputc
150875fd0b74Schristos    returns the value that was written, or -1 in case of error.
150975fd0b74Schristos */
151075fd0b74Schristos 
151175fd0b74Schristos ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
151275fd0b74Schristos /*
1513*e992f068Schristos      Read and decompress one byte from file.  gzgetc returns this byte or -1
151475fd0b74Schristos    in case of end of file or error.  This is implemented as a macro for speed.
151575fd0b74Schristos    As such, it does not do all of the checking the other functions do.  I.e.
151675fd0b74Schristos    it does not check to see if file is NULL, nor whether the structure file
151775fd0b74Schristos    points to has been clobbered or not.
151875fd0b74Schristos */
151975fd0b74Schristos 
152075fd0b74Schristos ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
152175fd0b74Schristos /*
1522*e992f068Schristos      Push c back onto the stream for file to be read as the first character on
1523*e992f068Schristos    the next read.  At least one character of push-back is always allowed.
152475fd0b74Schristos    gzungetc() returns the character pushed, or -1 on failure.  gzungetc() will
152575fd0b74Schristos    fail if c is -1, and may fail if a character has been pushed but not read
152675fd0b74Schristos    yet.  If gzungetc is used immediately after gzopen or gzdopen, at least the
152775fd0b74Schristos    output buffer size of pushed characters is allowed.  (See gzbuffer above.)
152875fd0b74Schristos    The pushed character will be discarded if the stream is repositioned with
152975fd0b74Schristos    gzseek() or gzrewind().
153075fd0b74Schristos */
153175fd0b74Schristos 
153275fd0b74Schristos ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
153375fd0b74Schristos /*
1534*e992f068Schristos      Flush all pending output to file.  The parameter flush is as in the
1535*e992f068Schristos    deflate() function.  The return value is the zlib error number (see function
1536*e992f068Schristos    gzerror below).  gzflush is only permitted when writing.
153775fd0b74Schristos 
153875fd0b74Schristos      If the flush parameter is Z_FINISH, the remaining data is written and the
153975fd0b74Schristos    gzip stream is completed in the output.  If gzwrite() is called again, a new
154075fd0b74Schristos    gzip stream will be started in the output.  gzread() is able to read such
1541ede78133Schristos    concatenated gzip streams.
154275fd0b74Schristos 
154375fd0b74Schristos      gzflush should be called only when strictly necessary because it will
154475fd0b74Schristos    degrade compression if called too often.
154575fd0b74Schristos */
154675fd0b74Schristos 
154775fd0b74Schristos /*
154875fd0b74Schristos ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
154975fd0b74Schristos                                    z_off_t offset, int whence));
155075fd0b74Schristos 
1551*e992f068Schristos      Set the starting position to offset relative to whence for the next gzread
1552*e992f068Schristos    or gzwrite on file.  The offset represents a number of bytes in the
155375fd0b74Schristos    uncompressed data stream.  The whence parameter is defined as in lseek(2);
155475fd0b74Schristos    the value SEEK_END is not supported.
155575fd0b74Schristos 
155675fd0b74Schristos      If the file is opened for reading, this function is emulated but can be
155775fd0b74Schristos    extremely slow.  If the file is opened for writing, only forward seeks are
155875fd0b74Schristos    supported; gzseek then compresses a sequence of zeroes up to the new
155975fd0b74Schristos    starting position.
156075fd0b74Schristos 
156175fd0b74Schristos      gzseek returns the resulting offset location as measured in bytes from
156275fd0b74Schristos    the beginning of the uncompressed stream, or -1 in case of error, in
156375fd0b74Schristos    particular if the file is opened for writing and the new starting position
156475fd0b74Schristos    would be before the current position.
156575fd0b74Schristos */
156675fd0b74Schristos 
156775fd0b74Schristos ZEXTERN int ZEXPORT    gzrewind OF((gzFile file));
156875fd0b74Schristos /*
1569*e992f068Schristos      Rewind file. This function is supported only for reading.
157075fd0b74Schristos 
1571*e992f068Schristos      gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET).
157275fd0b74Schristos */
157375fd0b74Schristos 
157475fd0b74Schristos /*
157575fd0b74Schristos ZEXTERN z_off_t ZEXPORT    gztell OF((gzFile file));
157675fd0b74Schristos 
1577*e992f068Schristos      Return the starting position for the next gzread or gzwrite on file.
1578*e992f068Schristos    This position represents a number of bytes in the uncompressed data stream,
1579*e992f068Schristos    and is zero when starting, even if appending or reading a gzip stream from
1580*e992f068Schristos    the middle of a file using gzdopen().
158175fd0b74Schristos 
158275fd0b74Schristos      gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
158375fd0b74Schristos */
158475fd0b74Schristos 
158575fd0b74Schristos /*
158675fd0b74Schristos ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
158775fd0b74Schristos 
1588*e992f068Schristos      Return the current compressed (actual) read or write offset of file.  This
1589*e992f068Schristos    offset includes the count of bytes that precede the gzip stream, for example
1590*e992f068Schristos    when appending or when using gzdopen() for reading.  When reading, the
1591*e992f068Schristos    offset does not include as yet unused buffered input.  This information can
1592*e992f068Schristos    be used for a progress indicator.  On error, gzoffset() returns -1.
159375fd0b74Schristos */
159475fd0b74Schristos 
159575fd0b74Schristos ZEXTERN int ZEXPORT gzeof OF((gzFile file));
159675fd0b74Schristos /*
1597*e992f068Schristos      Return true (1) if the end-of-file indicator for file has been set while
1598*e992f068Schristos    reading, false (0) otherwise.  Note that the end-of-file indicator is set
1599*e992f068Schristos    only if the read tried to go past the end of the input, but came up short.
1600*e992f068Schristos    Therefore, just like feof(), gzeof() may return false even if there is no
1601*e992f068Schristos    more data to read, in the event that the last read request was for the exact
1602*e992f068Schristos    number of bytes remaining in the input file.  This will happen if the input
1603*e992f068Schristos    file size is an exact multiple of the buffer size.
160475fd0b74Schristos 
160575fd0b74Schristos      If gzeof() returns true, then the read functions will return no more data,
160675fd0b74Schristos    unless the end-of-file indicator is reset by gzclearerr() and the input file
160775fd0b74Schristos    has grown since the previous end of file was detected.
160875fd0b74Schristos */
160975fd0b74Schristos 
161075fd0b74Schristos ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
161175fd0b74Schristos /*
1612*e992f068Schristos      Return true (1) if file is being copied directly while reading, or false
161375fd0b74Schristos    (0) if file is a gzip stream being decompressed.
161475fd0b74Schristos 
161575fd0b74Schristos      If the input file is empty, gzdirect() will return true, since the input
161675fd0b74Schristos    does not contain a gzip stream.
161775fd0b74Schristos 
161875fd0b74Schristos      If gzdirect() is used immediately after gzopen() or gzdopen() it will
161975fd0b74Schristos    cause buffers to be allocated to allow reading the file to determine if it
162075fd0b74Schristos    is a gzip file.  Therefore if gzbuffer() is used, it should be called before
162175fd0b74Schristos    gzdirect().
162275fd0b74Schristos 
162375fd0b74Schristos      When writing, gzdirect() returns true (1) if transparent writing was
162475fd0b74Schristos    requested ("wT" for the gzopen() mode), or false (0) otherwise.  (Note:
162575fd0b74Schristos    gzdirect() is not needed when writing.  Transparent writing must be
162675fd0b74Schristos    explicitly requested, so the application already knows the answer.  When
162775fd0b74Schristos    linking statically, using gzdirect() will include all of the zlib code for
162875fd0b74Schristos    gzip file reading and decompression, which may not be desired.)
162975fd0b74Schristos */
163075fd0b74Schristos 
163175fd0b74Schristos ZEXTERN int ZEXPORT    gzclose OF((gzFile file));
163275fd0b74Schristos /*
1633*e992f068Schristos      Flush all pending output for file, if necessary, close file and
1634*e992f068Schristos    deallocate the (de)compression state.  Note that once file is closed, you
163575fd0b74Schristos    cannot call gzerror with file, since its structures have been deallocated.
163675fd0b74Schristos    gzclose must not be called more than once on the same file, just as free
163775fd0b74Schristos    must not be called more than once on the same allocation.
163875fd0b74Schristos 
163975fd0b74Schristos      gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
164075fd0b74Schristos    file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the
164175fd0b74Schristos    last read ended in the middle of a gzip stream, or Z_OK on success.
164275fd0b74Schristos */
164375fd0b74Schristos 
164475fd0b74Schristos ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
164575fd0b74Schristos ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
164675fd0b74Schristos /*
164775fd0b74Schristos      Same as gzclose(), but gzclose_r() is only for use when reading, and
164875fd0b74Schristos    gzclose_w() is only for use when writing or appending.  The advantage to
164975fd0b74Schristos    using these instead of gzclose() is that they avoid linking in zlib
165075fd0b74Schristos    compression or decompression code that is not used when only reading or only
165175fd0b74Schristos    writing respectively.  If gzclose() is used, then both compression and
165275fd0b74Schristos    decompression code will be included the application when linking to a static
165375fd0b74Schristos    zlib library.
165475fd0b74Schristos */
165575fd0b74Schristos 
165675fd0b74Schristos ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
165775fd0b74Schristos /*
1658*e992f068Schristos      Return the error message for the last error which occurred on file.
1659*e992f068Schristos    errnum is set to zlib error number.  If an error occurred in the file system
1660*e992f068Schristos    and not in the compression library, errnum is set to Z_ERRNO and the
1661*e992f068Schristos    application may consult errno to get the exact error code.
166275fd0b74Schristos 
166375fd0b74Schristos      The application must not modify the returned string.  Future calls to
166475fd0b74Schristos    this function may invalidate the previously returned string.  If file is
166575fd0b74Schristos    closed, then the string previously returned by gzerror will no longer be
166675fd0b74Schristos    available.
166775fd0b74Schristos 
166875fd0b74Schristos      gzerror() should be used to distinguish errors from end-of-file for those
166975fd0b74Schristos    functions above that do not distinguish those cases in their return values.
167075fd0b74Schristos */
167175fd0b74Schristos 
167275fd0b74Schristos ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
167375fd0b74Schristos /*
1674*e992f068Schristos      Clear the error and end-of-file flags for file.  This is analogous to the
167575fd0b74Schristos    clearerr() function in stdio.  This is useful for continuing to read a gzip
167675fd0b74Schristos    file that is being written concurrently.
167775fd0b74Schristos */
167875fd0b74Schristos 
167975fd0b74Schristos #endif /* !Z_SOLO */
168075fd0b74Schristos 
168175fd0b74Schristos                         /* checksum functions */
168275fd0b74Schristos 
168375fd0b74Schristos /*
168475fd0b74Schristos      These functions are not related to compression but are exported
168575fd0b74Schristos    anyway because they might be useful in applications using the compression
168675fd0b74Schristos    library.
168775fd0b74Schristos */
168875fd0b74Schristos 
168975fd0b74Schristos ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
169075fd0b74Schristos /*
169175fd0b74Schristos      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1692*e992f068Schristos    return the updated checksum. An Adler-32 value is in the range of a 32-bit
1693*e992f068Schristos    unsigned integer. If buf is Z_NULL, this function returns the required
1694*e992f068Schristos    initial value for the checksum.
169575fd0b74Schristos 
1696ede78133Schristos      An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
169775fd0b74Schristos    much faster.
169875fd0b74Schristos 
169975fd0b74Schristos    Usage example:
170075fd0b74Schristos 
170175fd0b74Schristos      uLong adler = adler32(0L, Z_NULL, 0);
170275fd0b74Schristos 
170375fd0b74Schristos      while (read_buffer(buffer, length) != EOF) {
170475fd0b74Schristos        adler = adler32(adler, buffer, length);
170575fd0b74Schristos      }
170675fd0b74Schristos      if (adler != original_adler) error();
170775fd0b74Schristos */
170875fd0b74Schristos 
1709ede78133Schristos ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf,
1710ede78133Schristos                                     z_size_t len));
1711ede78133Schristos /*
1712ede78133Schristos      Same as adler32(), but with a size_t length.
1713ede78133Schristos */
1714ede78133Schristos 
171575fd0b74Schristos /*
171675fd0b74Schristos ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
171775fd0b74Schristos                                           z_off_t len2));
171875fd0b74Schristos 
171975fd0b74Schristos      Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
172075fd0b74Schristos    and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
172175fd0b74Schristos    each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
172275fd0b74Schristos    seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.  Note
172375fd0b74Schristos    that the z_off_t type (like off_t) is a signed integer.  If len2 is
172475fd0b74Schristos    negative, the result has no meaning or utility.
172575fd0b74Schristos */
172675fd0b74Schristos 
172775fd0b74Schristos ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
172875fd0b74Schristos /*
172975fd0b74Schristos      Update a running CRC-32 with the bytes buf[0..len-1] and return the
1730*e992f068Schristos    updated CRC-32. A CRC-32 value is in the range of a 32-bit unsigned integer.
1731*e992f068Schristos    If buf is Z_NULL, this function returns the required initial value for the
1732*e992f068Schristos    crc. Pre- and post-conditioning (one's complement) is performed within this
1733*e992f068Schristos    function so it shouldn't be done by the application.
173475fd0b74Schristos 
173575fd0b74Schristos    Usage example:
173675fd0b74Schristos 
173775fd0b74Schristos      uLong crc = crc32(0L, Z_NULL, 0);
173875fd0b74Schristos 
173975fd0b74Schristos      while (read_buffer(buffer, length) != EOF) {
174075fd0b74Schristos        crc = crc32(crc, buffer, length);
174175fd0b74Schristos      }
174275fd0b74Schristos      if (crc != original_crc) error();
174375fd0b74Schristos */
174475fd0b74Schristos 
1745*e992f068Schristos ZEXTERN uLong ZEXPORT crc32_z OF((uLong crc, const Bytef *buf,
1746ede78133Schristos                                   z_size_t len));
1747ede78133Schristos /*
1748ede78133Schristos      Same as crc32(), but with a size_t length.
1749ede78133Schristos */
1750ede78133Schristos 
175175fd0b74Schristos /*
175275fd0b74Schristos ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
175375fd0b74Schristos 
175475fd0b74Schristos      Combine two CRC-32 check values into one.  For two sequences of bytes,
175575fd0b74Schristos    seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
175675fd0b74Schristos    calculated for each, crc1 and crc2.  crc32_combine() returns the CRC-32
175775fd0b74Schristos    check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
175875fd0b74Schristos    len2.
175975fd0b74Schristos */
176075fd0b74Schristos 
1761*e992f068Schristos /*
1762*e992f068Schristos ZEXTERN uLong ZEXPORT crc32_combine_gen OF((z_off_t len2));
1763*e992f068Schristos 
1764*e992f068Schristos      Return the operator corresponding to length len2, to be used with
1765*e992f068Schristos    crc32_combine_op().
1766*e992f068Schristos */
1767*e992f068Schristos 
1768*e992f068Schristos ZEXTERN uLong ZEXPORT crc32_combine_op OF((uLong crc1, uLong crc2, uLong op));
1769*e992f068Schristos /*
1770*e992f068Schristos      Give the same result as crc32_combine(), using op in place of len2. op is
1771*e992f068Schristos    is generated from len2 by crc32_combine_gen(). This will be faster than
1772*e992f068Schristos    crc32_combine() if the generated op is used more than once.
1773*e992f068Schristos */
1774*e992f068Schristos 
177575fd0b74Schristos 
177675fd0b74Schristos                         /* various hacks, don't look :) */
177775fd0b74Schristos 
177875fd0b74Schristos /* deflateInit and inflateInit are macros to allow checking the zlib version
177975fd0b74Schristos  * and the compiler's view of z_stream:
178075fd0b74Schristos  */
178175fd0b74Schristos ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
178275fd0b74Schristos                                      const char *version, int stream_size));
178375fd0b74Schristos ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
178475fd0b74Schristos                                      const char *version, int stream_size));
178575fd0b74Schristos ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int  level, int  method,
178675fd0b74Schristos                                       int windowBits, int memLevel,
178775fd0b74Schristos                                       int strategy, const char *version,
178875fd0b74Schristos                                       int stream_size));
178975fd0b74Schristos ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int  windowBits,
179075fd0b74Schristos                                       const char *version, int stream_size));
179175fd0b74Schristos ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
179275fd0b74Schristos                                          unsigned char FAR *window,
179375fd0b74Schristos                                          const char *version,
179475fd0b74Schristos                                          int stream_size));
1795ede78133Schristos #ifdef Z_PREFIX_SET
1796ede78133Schristos #  define z_deflateInit(strm, level) \
1797ede78133Schristos           deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
1798ede78133Schristos #  define z_inflateInit(strm) \
1799ede78133Schristos           inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
1800ede78133Schristos #  define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1801ede78133Schristos           deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1802ede78133Schristos                         (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
1803ede78133Schristos #  define z_inflateInit2(strm, windowBits) \
1804ede78133Schristos           inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
1805ede78133Schristos                         (int)sizeof(z_stream))
1806ede78133Schristos #  define z_inflateBackInit(strm, windowBits, window) \
1807ede78133Schristos           inflateBackInit_((strm), (windowBits), (window), \
1808ede78133Schristos                            ZLIB_VERSION, (int)sizeof(z_stream))
1809ede78133Schristos #else
181075fd0b74Schristos #  define deflateInit(strm, level) \
181175fd0b74Schristos           deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
181275fd0b74Schristos #  define inflateInit(strm) \
181375fd0b74Schristos           inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
181475fd0b74Schristos #  define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
181575fd0b74Schristos           deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
181675fd0b74Schristos                         (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
181775fd0b74Schristos #  define inflateInit2(strm, windowBits) \
181875fd0b74Schristos           inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
181975fd0b74Schristos                         (int)sizeof(z_stream))
182075fd0b74Schristos #  define inflateBackInit(strm, windowBits, window) \
182175fd0b74Schristos           inflateBackInit_((strm), (windowBits), (window), \
182275fd0b74Schristos                            ZLIB_VERSION, (int)sizeof(z_stream))
1823ede78133Schristos #endif
182475fd0b74Schristos 
182575fd0b74Schristos #ifndef Z_SOLO
182675fd0b74Schristos 
182775fd0b74Schristos /* gzgetc() macro and its supporting function and exposed data structure.  Note
182875fd0b74Schristos  * that the real internal state is much larger than the exposed structure.
182975fd0b74Schristos  * This abbreviated structure exposes just enough for the gzgetc() macro.  The
183075fd0b74Schristos  * user should not mess with these exposed elements, since their names or
183175fd0b74Schristos  * behavior could change in the future, perhaps even capriciously.  They can
183275fd0b74Schristos  * only be used by the gzgetc() macro.  You have been warned.
183375fd0b74Schristos  */
183475fd0b74Schristos struct gzFile_s {
183575fd0b74Schristos     unsigned have;
183675fd0b74Schristos     unsigned char *next;
183775fd0b74Schristos     z_off64_t pos;
183875fd0b74Schristos };
183975fd0b74Schristos ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file));  /* backward compatibility */
184075fd0b74Schristos #ifdef Z_PREFIX_SET
184175fd0b74Schristos #  undef z_gzgetc
184275fd0b74Schristos #  define z_gzgetc(g) \
1843ede78133Schristos           ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
184475fd0b74Schristos #else
184575fd0b74Schristos #  define gzgetc(g) \
1846ede78133Schristos           ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
184775fd0b74Schristos #endif
184875fd0b74Schristos 
184975fd0b74Schristos /* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
185075fd0b74Schristos  * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
185175fd0b74Schristos  * both are true, the application gets the *64 functions, and the regular
185275fd0b74Schristos  * functions are changed to 64 bits) -- in case these are set on systems
185375fd0b74Schristos  * without large file support, _LFS64_LARGEFILE must also be true
185475fd0b74Schristos  */
185575fd0b74Schristos #ifdef Z_LARGE64
185675fd0b74Schristos    ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
185775fd0b74Schristos    ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
185875fd0b74Schristos    ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
185975fd0b74Schristos    ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
186075fd0b74Schristos    ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
186175fd0b74Schristos    ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
1862*e992f068Schristos    ZEXTERN uLong ZEXPORT crc32_combine_gen64 OF((z_off64_t));
186375fd0b74Schristos #endif
186475fd0b74Schristos 
186575fd0b74Schristos #if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
186675fd0b74Schristos #  ifdef Z_PREFIX_SET
186775fd0b74Schristos #    define z_gzopen z_gzopen64
186875fd0b74Schristos #    define z_gzseek z_gzseek64
186975fd0b74Schristos #    define z_gztell z_gztell64
187075fd0b74Schristos #    define z_gzoffset z_gzoffset64
187175fd0b74Schristos #    define z_adler32_combine z_adler32_combine64
187275fd0b74Schristos #    define z_crc32_combine z_crc32_combine64
1873*e992f068Schristos #    define z_crc32_combine_gen z_crc32_combine_gen64
187475fd0b74Schristos #  else
187575fd0b74Schristos #    define gzopen gzopen64
187675fd0b74Schristos #    define gzseek gzseek64
187775fd0b74Schristos #    define gztell gztell64
187875fd0b74Schristos #    define gzoffset gzoffset64
187975fd0b74Schristos #    define adler32_combine adler32_combine64
188075fd0b74Schristos #    define crc32_combine crc32_combine64
1881*e992f068Schristos #    define crc32_combine_gen crc32_combine_gen64
188275fd0b74Schristos #  endif
188375fd0b74Schristos #  ifndef Z_LARGE64
188475fd0b74Schristos      ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
188575fd0b74Schristos      ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
188675fd0b74Schristos      ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
188775fd0b74Schristos      ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
188875fd0b74Schristos      ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
188975fd0b74Schristos      ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
1890*e992f068Schristos      ZEXTERN uLong ZEXPORT crc32_combine_gen64 OF((z_off_t));
189175fd0b74Schristos #  endif
189275fd0b74Schristos #else
189375fd0b74Schristos    ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
189475fd0b74Schristos    ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
189575fd0b74Schristos    ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
189675fd0b74Schristos    ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
189775fd0b74Schristos    ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
189875fd0b74Schristos    ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
1899*e992f068Schristos    ZEXTERN uLong ZEXPORT crc32_combine_gen OF((z_off_t));
190075fd0b74Schristos #endif
190175fd0b74Schristos 
190275fd0b74Schristos #else /* Z_SOLO */
190375fd0b74Schristos 
190475fd0b74Schristos    ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
190575fd0b74Schristos    ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
1906*e992f068Schristos    ZEXTERN uLong ZEXPORT crc32_combine_gen OF((z_off_t));
190775fd0b74Schristos 
190875fd0b74Schristos #endif /* !Z_SOLO */
190975fd0b74Schristos 
191075fd0b74Schristos /* undocumented functions */
191175fd0b74Schristos ZEXTERN const char   * ZEXPORT zError           OF((int));
191275fd0b74Schristos ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp));
191375fd0b74Schristos ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table    OF((void));
191475fd0b74Schristos ZEXTERN int            ZEXPORT inflateUndermine OF((z_streamp, int));
1915ede78133Schristos ZEXTERN int            ZEXPORT inflateValidate OF((z_streamp, int));
1916ede78133Schristos ZEXTERN unsigned long  ZEXPORT inflateCodesUsed OF ((z_streamp));
191775fd0b74Schristos ZEXTERN int            ZEXPORT inflateResetKeep OF((z_streamp));
191875fd0b74Schristos ZEXTERN int            ZEXPORT deflateResetKeep OF((z_streamp));
191975fd0b74Schristos #if defined(_WIN32) && !defined(Z_SOLO)
192075fd0b74Schristos ZEXTERN gzFile         ZEXPORT gzopen_w OF((const wchar_t *path,
192175fd0b74Schristos                                             const char *mode));
192275fd0b74Schristos #endif
192375fd0b74Schristos #if defined(STDC) || defined(Z_HAVE_STDARG_H)
192475fd0b74Schristos #  ifndef Z_SOLO
192575fd0b74Schristos ZEXTERN int            ZEXPORTVA gzvprintf Z_ARG((gzFile file,
192675fd0b74Schristos                                                   const char *format,
192775fd0b74Schristos                                                   va_list va));
192875fd0b74Schristos #  endif
192975fd0b74Schristos #endif
193075fd0b74Schristos 
193175fd0b74Schristos #ifdef __cplusplus
193275fd0b74Schristos }
193375fd0b74Schristos #endif
193475fd0b74Schristos 
193575fd0b74Schristos #endif /* ZLIB_H */
1936