xref: /freebsd-src/sys/contrib/zlib/zlib.h (revision 6255c67c3d1a268535c50de74d3300fd86d8f15d)
1c9083b85SXin LI /* zlib.h -- interface of the 'zlib' general purpose compression library
2*6255c67cSXin LI   version 1.3.1, January 22nd, 2024
3c9083b85SXin LI 
4*6255c67cSXin LI   Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
5c9083b85SXin LI 
6c9083b85SXin LI   This software is provided 'as-is', without any express or implied
7c9083b85SXin LI   warranty.  In no event will the authors be held liable for any damages
8c9083b85SXin LI   arising from the use of this software.
9c9083b85SXin LI 
10c9083b85SXin LI   Permission is granted to anyone to use this software for any purpose,
11c9083b85SXin LI   including commercial applications, and to alter it and redistribute it
12c9083b85SXin LI   freely, subject to the following restrictions:
13c9083b85SXin LI 
14c9083b85SXin LI   1. The origin of this software must not be misrepresented; you must not
15c9083b85SXin LI      claim that you wrote the original software. If you use this software
16c9083b85SXin LI      in a product, an acknowledgment in the product documentation would be
17c9083b85SXin LI      appreciated but is not required.
18c9083b85SXin LI   2. Altered source versions must be plainly marked as such, and must not be
19c9083b85SXin LI      misrepresented as being the original software.
20c9083b85SXin LI   3. This notice may not be removed or altered from any source distribution.
21c9083b85SXin LI 
22c9083b85SXin LI   Jean-loup Gailly        Mark Adler
23c9083b85SXin LI   jloup@gzip.org          madler@alumni.caltech.edu
24c9083b85SXin LI 
25c9083b85SXin LI 
26c9083b85SXin LI   The data format used by the zlib library is described by RFCs (Request for
27c9083b85SXin LI   Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
28c9083b85SXin LI   (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
29c9083b85SXin LI */
30c9083b85SXin LI 
31c9083b85SXin LI #ifndef ZLIB_H
32c9083b85SXin LI #define ZLIB_H
33c9083b85SXin LI 
34c9083b85SXin LI #include "zconf.h"
35c9083b85SXin LI 
36c9083b85SXin LI #ifdef __cplusplus
37c9083b85SXin LI extern "C" {
38c9083b85SXin LI #endif
39c9083b85SXin LI 
40*6255c67cSXin LI #define ZLIB_VERSION "1.3.1"
41*6255c67cSXin LI #define ZLIB_VERNUM 0x1310
42c9083b85SXin LI #define ZLIB_VER_MAJOR 1
434717628eSXin LI #define ZLIB_VER_MINOR 3
44*6255c67cSXin LI #define ZLIB_VER_REVISION 1
45c9083b85SXin LI #define ZLIB_VER_SUBREVISION 0
46c9083b85SXin LI 
47c9083b85SXin LI /*
48c9083b85SXin LI     The 'zlib' compression library provides in-memory compression and
49c9083b85SXin LI   decompression functions, including integrity checks of the uncompressed data.
50c9083b85SXin LI   This version of the library supports only one compression method (deflation)
51c9083b85SXin LI   but other algorithms will be added later and will have the same stream
52c9083b85SXin LI   interface.
53c9083b85SXin LI 
54c9083b85SXin LI     Compression can be done in a single step if the buffers are large enough,
55c9083b85SXin LI   or can be done by repeated calls of the compression function.  In the latter
56c9083b85SXin LI   case, the application must provide more input and/or consume the output
57c9083b85SXin LI   (providing more output space) before each call.
58c9083b85SXin LI 
59c9083b85SXin LI     The compressed data format used by default by the in-memory functions is
60c9083b85SXin LI   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
61c9083b85SXin LI   around a deflate stream, which is itself documented in RFC 1951.
62c9083b85SXin LI 
63c9083b85SXin LI     The library also supports reading and writing files in gzip (.gz) format
64c9083b85SXin LI   with an interface similar to that of stdio using the functions that start
65c9083b85SXin LI   with "gz".  The gzip format is different from the zlib format.  gzip is a
66c9083b85SXin LI   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
67c9083b85SXin LI 
68c9083b85SXin LI     This library can optionally read and write gzip and raw deflate streams in
69c9083b85SXin LI   memory as well.
70c9083b85SXin LI 
71c9083b85SXin LI     The zlib format was designed to be compact and fast for use in memory
72c9083b85SXin LI   and on communications channels.  The gzip format was designed for single-
73c9083b85SXin LI   file compression on file systems, has a larger header than zlib to maintain
74c9083b85SXin LI   directory information, and uses a different, slower check method than zlib.
75c9083b85SXin LI 
76c9083b85SXin LI     The library does not install any signal handler.  The decoder checks
77c9083b85SXin LI   the consistency of the compressed data, so the library should never crash
78c9083b85SXin LI   even in the case of corrupted input.
79c9083b85SXin LI */
80c9083b85SXin LI 
814717628eSXin LI typedef voidpf (*alloc_func)(voidpf opaque, uInt items, uInt size);
824717628eSXin LI typedef void   (*free_func)(voidpf opaque, voidpf address);
83c9083b85SXin LI 
84c9083b85SXin LI struct internal_state;
85c9083b85SXin LI 
86c9083b85SXin LI typedef struct z_stream_s {
87c9083b85SXin LI     z_const Bytef *next_in;     /* next input byte */
88c9083b85SXin LI     uInt     avail_in;  /* number of bytes available at next_in */
89c9083b85SXin LI     uLong    total_in;  /* total number of input bytes read so far */
90c9083b85SXin LI 
91c9083b85SXin LI     Bytef    *next_out; /* next output byte will go here */
92c9083b85SXin LI     uInt     avail_out; /* remaining free space at next_out */
93c9083b85SXin LI     uLong    total_out; /* total number of bytes output so far */
94c9083b85SXin LI 
95c9083b85SXin LI     z_const char *msg;  /* last error message, NULL if no error */
96c9083b85SXin LI     struct internal_state FAR *state; /* not visible by applications */
97c9083b85SXin LI 
98c9083b85SXin LI     alloc_func zalloc;  /* used to allocate the internal state */
99c9083b85SXin LI     free_func  zfree;   /* used to free the internal state */
100c9083b85SXin LI     voidpf     opaque;  /* private data object passed to zalloc and zfree */
101c9083b85SXin LI 
102c9083b85SXin LI     int     data_type;  /* best guess about the data type: binary or text
103c9083b85SXin LI                            for deflate, or the decoding state for inflate */
104c9083b85SXin LI     uLong   adler;      /* Adler-32 or CRC-32 value of the uncompressed data */
105c9083b85SXin LI     uLong   reserved;   /* reserved for future use */
106c9083b85SXin LI } z_stream;
107c9083b85SXin LI 
108c9083b85SXin LI typedef z_stream FAR *z_streamp;
109c9083b85SXin LI 
110c9083b85SXin LI /*
111c9083b85SXin LI      gzip header information passed to and from zlib routines.  See RFC 1952
112c9083b85SXin LI   for more details on the meanings of these fields.
113c9083b85SXin LI */
114c9083b85SXin LI typedef struct gz_header_s {
115c9083b85SXin LI     int     text;       /* true if compressed data believed to be text */
116c9083b85SXin LI     uLong   time;       /* modification time */
117c9083b85SXin LI     int     xflags;     /* extra flags (not used when writing a gzip file) */
118c9083b85SXin LI     int     os;         /* operating system */
119c9083b85SXin LI     Bytef   *extra;     /* pointer to extra field or Z_NULL if none */
120c9083b85SXin LI     uInt    extra_len;  /* extra field length (valid if extra != Z_NULL) */
121c9083b85SXin LI     uInt    extra_max;  /* space at extra (only when reading header) */
122c9083b85SXin LI     Bytef   *name;      /* pointer to zero-terminated file name or Z_NULL */
123c9083b85SXin LI     uInt    name_max;   /* space at name (only when reading header) */
124c9083b85SXin LI     Bytef   *comment;   /* pointer to zero-terminated comment or Z_NULL */
125c9083b85SXin LI     uInt    comm_max;   /* space at comment (only when reading header) */
126c9083b85SXin LI     int     hcrc;       /* true if there was or will be a header crc */
127c9083b85SXin LI     int     done;       /* true when done reading gzip header (not used
128c9083b85SXin LI                            when writing a gzip file) */
129c9083b85SXin LI } gz_header;
130c9083b85SXin LI 
131c9083b85SXin LI typedef gz_header FAR *gz_headerp;
132c9083b85SXin LI 
133c9083b85SXin LI /*
134c9083b85SXin LI      The application must update next_in and avail_in when avail_in has dropped
135c9083b85SXin LI    to zero.  It must update next_out and avail_out when avail_out has dropped
136c9083b85SXin LI    to zero.  The application must initialize zalloc, zfree and opaque before
137c9083b85SXin LI    calling the init function.  All other fields are set by the compression
138c9083b85SXin LI    library and must not be updated by the application.
139c9083b85SXin LI 
140c9083b85SXin LI      The opaque value provided by the application will be passed as the first
141c9083b85SXin LI    parameter for calls of zalloc and zfree.  This can be useful for custom
142c9083b85SXin LI    memory management.  The compression library attaches no meaning to the
143c9083b85SXin LI    opaque value.
144c9083b85SXin LI 
145c9083b85SXin LI      zalloc must return Z_NULL if there is not enough memory for the object.
146c9083b85SXin LI    If zlib is used in a multi-threaded application, zalloc and zfree must be
147c9083b85SXin LI    thread safe.  In that case, zlib is thread-safe.  When zalloc and zfree are
148c9083b85SXin LI    Z_NULL on entry to the initialization function, they are set to internal
149c9083b85SXin LI    routines that use the standard library functions malloc() and free().
150c9083b85SXin LI 
151c9083b85SXin LI      On 16-bit systems, the functions zalloc and zfree must be able to allocate
152c9083b85SXin LI    exactly 65536 bytes, but will not be required to allocate more than this if
153c9083b85SXin LI    the symbol MAXSEG_64K is defined (see zconf.h).  WARNING: On MSDOS, pointers
154c9083b85SXin LI    returned by zalloc for objects of exactly 65536 bytes *must* have their
155c9083b85SXin LI    offset normalized to zero.  The default allocation function provided by this
156c9083b85SXin LI    library ensures this (see zutil.c).  To reduce memory requirements and avoid
157c9083b85SXin LI    any allocation of 64K objects, at the expense of compression ratio, compile
158c9083b85SXin LI    the library with -DMAX_WBITS=14 (see zconf.h).
159c9083b85SXin LI 
160c9083b85SXin LI      The fields total_in and total_out can be used for statistics or progress
161c9083b85SXin LI    reports.  After compression, total_in holds the total size of the
162c9083b85SXin LI    uncompressed data and may be saved for use by the decompressor (particularly
163c9083b85SXin LI    if the decompressor wants to decompress everything in a single step).
164c9083b85SXin LI */
165c9083b85SXin LI 
166c9083b85SXin LI                         /* constants */
167c9083b85SXin LI 
168c9083b85SXin LI #define Z_NO_FLUSH      0
169c9083b85SXin LI #define Z_PARTIAL_FLUSH 1
170c9083b85SXin LI #define Z_SYNC_FLUSH    2
171c9083b85SXin LI #define Z_FULL_FLUSH    3
172c9083b85SXin LI #define Z_FINISH        4
173c9083b85SXin LI #define Z_BLOCK         5
174c9083b85SXin LI #define Z_TREES         6
175c9083b85SXin LI /* Allowed flush values; see deflate() and inflate() below for details */
176c9083b85SXin LI 
177c9083b85SXin LI #define Z_OK            0
178c9083b85SXin LI #define Z_STREAM_END    1
179c9083b85SXin LI #define Z_NEED_DICT     2
180c9083b85SXin LI #define Z_ERRNO        (-1)
181c9083b85SXin LI #define Z_STREAM_ERROR (-2)
182c9083b85SXin LI #define Z_DATA_ERROR   (-3)
183c9083b85SXin LI #define Z_MEM_ERROR    (-4)
184c9083b85SXin LI #define Z_BUF_ERROR    (-5)
185c9083b85SXin LI #define Z_VERSION_ERROR (-6)
186c9083b85SXin LI /* Return codes for the compression/decompression functions. Negative values
187c9083b85SXin LI  * are errors, positive values are used for special but normal events.
188c9083b85SXin LI  */
189c9083b85SXin LI 
190c9083b85SXin LI #define Z_NO_COMPRESSION         0
191c9083b85SXin LI #define Z_BEST_SPEED             1
192c9083b85SXin LI #define Z_BEST_COMPRESSION       9
193c9083b85SXin LI #define Z_DEFAULT_COMPRESSION  (-1)
194c9083b85SXin LI /* compression levels */
195c9083b85SXin LI 
196c9083b85SXin LI #define Z_FILTERED            1
197c9083b85SXin LI #define Z_HUFFMAN_ONLY        2
198c9083b85SXin LI #define Z_RLE                 3
199c9083b85SXin LI #define Z_FIXED               4
200c9083b85SXin LI #define Z_DEFAULT_STRATEGY    0
201c9083b85SXin LI /* compression strategy; see deflateInit2() below for details */
202c9083b85SXin LI 
203c9083b85SXin LI #define Z_BINARY   0
204c9083b85SXin LI #define Z_TEXT     1
205c9083b85SXin LI #define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
206c9083b85SXin LI #define Z_UNKNOWN  2
207c9083b85SXin LI /* Possible values of the data_type field for deflate() */
208c9083b85SXin LI 
209c9083b85SXin LI #define Z_DEFLATED   8
210c9083b85SXin LI /* The deflate compression method (the only one supported in this version) */
211c9083b85SXin LI 
212c9083b85SXin LI #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
213c9083b85SXin LI 
214c9083b85SXin LI #define zlib_version zlibVersion()
215c9083b85SXin LI /* for compatibility with versions < 1.0.2 */
216c9083b85SXin LI 
217c9083b85SXin LI 
218c9083b85SXin LI                         /* basic functions */
219c9083b85SXin LI 
2204717628eSXin LI ZEXTERN const char * ZEXPORT zlibVersion(void);
221c9083b85SXin LI /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
222c9083b85SXin LI    If the first character differs, the library code actually used is not
223c9083b85SXin LI    compatible with the zlib.h header file used by the application.  This check
224c9083b85SXin LI    is automatically made by deflateInit and inflateInit.
225c9083b85SXin LI  */
226c9083b85SXin LI 
227c9083b85SXin LI /*
2284717628eSXin LI ZEXTERN int ZEXPORT deflateInit(z_streamp strm, int level);
229c9083b85SXin LI 
230c9083b85SXin LI      Initializes the internal stream state for compression.  The fields
231c9083b85SXin LI    zalloc, zfree and opaque must be initialized before by the caller.  If
232c9083b85SXin LI    zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
2334717628eSXin LI    allocation functions.  total_in, total_out, adler, and msg are initialized.
234c9083b85SXin LI 
235c9083b85SXin LI      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
236c9083b85SXin LI    1 gives best speed, 9 gives best compression, 0 gives no compression at all
237c9083b85SXin LI    (the input data is simply copied a block at a time).  Z_DEFAULT_COMPRESSION
238c9083b85SXin LI    requests a default compromise between speed and compression (currently
239c9083b85SXin LI    equivalent to level 6).
240c9083b85SXin LI 
241c9083b85SXin LI      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
242c9083b85SXin LI    memory, Z_STREAM_ERROR if level is not a valid compression level, or
243c9083b85SXin LI    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
244c9083b85SXin LI    with the version assumed by the caller (ZLIB_VERSION).  msg is set to null
245c9083b85SXin LI    if there is no error message.  deflateInit does not perform any compression:
246c9083b85SXin LI    this will be done by deflate().
247c9083b85SXin LI */
248c9083b85SXin LI 
249c9083b85SXin LI 
2504717628eSXin LI ZEXTERN int ZEXPORT deflate(z_streamp strm, int flush);
251c9083b85SXin LI /*
252c9083b85SXin LI     deflate compresses as much data as possible, and stops when the input
253c9083b85SXin LI   buffer becomes empty or the output buffer becomes full.  It may introduce
254c9083b85SXin LI   some output latency (reading input without producing any output) except when
255c9083b85SXin LI   forced to flush.
256c9083b85SXin LI 
257c9083b85SXin LI     The detailed semantics are as follows.  deflate performs one or both of the
258c9083b85SXin LI   following actions:
259c9083b85SXin LI 
260c9083b85SXin LI   - Compress more input starting at next_in and update next_in and avail_in
261c9083b85SXin LI     accordingly.  If not all input can be processed (because there is not
262c9083b85SXin LI     enough room in the output buffer), next_in and avail_in are updated and
263c9083b85SXin LI     processing will resume at this point for the next call of deflate().
264c9083b85SXin LI 
265c9083b85SXin LI   - Generate more output starting at next_out and update next_out and avail_out
266c9083b85SXin LI     accordingly.  This action is forced if the parameter flush is non zero.
267c9083b85SXin LI     Forcing flush frequently degrades the compression ratio, so this parameter
268c9083b85SXin LI     should be set only when necessary.  Some output may be provided even if
269c9083b85SXin LI     flush is zero.
270c9083b85SXin LI 
271c9083b85SXin LI     Before the call of deflate(), the application should ensure that at least
272c9083b85SXin LI   one of the actions is possible, by providing more input and/or consuming more
273c9083b85SXin LI   output, and updating avail_in or avail_out accordingly; avail_out should
274c9083b85SXin LI   never be zero before the call.  The application can consume the compressed
275c9083b85SXin LI   output when it wants, for example when the output buffer is full (avail_out
276c9083b85SXin LI   == 0), or after each call of deflate().  If deflate returns Z_OK and with
277c9083b85SXin LI   zero avail_out, it must be called again after making room in the output
278c9083b85SXin LI   buffer because there might be more output pending. See deflatePending(),
279e37bb444SXin LI   which can be used if desired to determine whether or not there is more output
280c9083b85SXin LI   in that case.
281c9083b85SXin LI 
282c9083b85SXin LI     Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
283c9083b85SXin LI   decide how much data to accumulate before producing output, in order to
284c9083b85SXin LI   maximize compression.
285c9083b85SXin LI 
286c9083b85SXin LI     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
287c9083b85SXin LI   flushed to the output buffer and the output is aligned on a byte boundary, so
288c9083b85SXin LI   that the decompressor can get all input data available so far.  (In
289c9083b85SXin LI   particular avail_in is zero after the call if enough output space has been
290c9083b85SXin LI   provided before the call.) Flushing may degrade compression for some
291c9083b85SXin LI   compression algorithms and so it should be used only when necessary.  This
292c9083b85SXin LI   completes the current deflate block and follows it with an empty stored block
293c9083b85SXin LI   that is three bits plus filler bits to the next byte, followed by four bytes
294c9083b85SXin LI   (00 00 ff ff).
295c9083b85SXin LI 
296c9083b85SXin LI     If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
297c9083b85SXin LI   output buffer, but the output is not aligned to a byte boundary.  All of the
298c9083b85SXin LI   input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
299c9083b85SXin LI   This completes the current deflate block and follows it with an empty fixed
300c9083b85SXin LI   codes block that is 10 bits long.  This assures that enough bytes are output
301c9083b85SXin LI   in order for the decompressor to finish the block before the empty fixed
302c9083b85SXin LI   codes block.
303c9083b85SXin LI 
304c9083b85SXin LI     If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
305c9083b85SXin LI   for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
306c9083b85SXin LI   seven bits of the current block are held to be written as the next byte after
307c9083b85SXin LI   the next deflate block is completed.  In this case, the decompressor may not
308c9083b85SXin LI   be provided enough bits at this point in order to complete decompression of
309c9083b85SXin LI   the data provided so far to the compressor.  It may need to wait for the next
310c9083b85SXin LI   block to be emitted.  This is for advanced applications that need to control
311c9083b85SXin LI   the emission of deflate blocks.
312c9083b85SXin LI 
313c9083b85SXin LI     If flush is set to Z_FULL_FLUSH, all output is flushed as with
314c9083b85SXin LI   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
315c9083b85SXin LI   restart from this point if previous compressed data has been damaged or if
316c9083b85SXin LI   random access is desired.  Using Z_FULL_FLUSH too often can seriously degrade
317c9083b85SXin LI   compression.
318c9083b85SXin LI 
319c9083b85SXin LI     If deflate returns with avail_out == 0, this function must be called again
320c9083b85SXin LI   with the same value of the flush parameter and more output space (updated
321c9083b85SXin LI   avail_out), until the flush is complete (deflate returns with non-zero
322c9083b85SXin LI   avail_out).  In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
3234717628eSXin LI   avail_out is greater than six when the flush marker begins, in order to avoid
3244717628eSXin LI   repeated flush markers upon calling deflate() again when avail_out == 0.
325c9083b85SXin LI 
326c9083b85SXin LI     If the parameter flush is set to Z_FINISH, pending input is processed,
327c9083b85SXin LI   pending output is flushed and deflate returns with Z_STREAM_END if there was
328c9083b85SXin LI   enough output space.  If deflate returns with Z_OK or Z_BUF_ERROR, this
329c9083b85SXin LI   function must be called again with Z_FINISH and more output space (updated
330c9083b85SXin LI   avail_out) but no more input data, until it returns with Z_STREAM_END or an
331c9083b85SXin LI   error.  After deflate has returned Z_STREAM_END, the only possible operations
332c9083b85SXin LI   on the stream are deflateReset or deflateEnd.
333c9083b85SXin LI 
334c9083b85SXin LI     Z_FINISH can be used in the first deflate call after deflateInit if all the
335c9083b85SXin LI   compression is to be done in a single step.  In order to complete in one
336c9083b85SXin LI   call, avail_out must be at least the value returned by deflateBound (see
337c9083b85SXin LI   below).  Then deflate is guaranteed to return Z_STREAM_END.  If not enough
338c9083b85SXin LI   output space is provided, deflate will not return Z_STREAM_END, and it must
339c9083b85SXin LI   be called again as described above.
340c9083b85SXin LI 
341c9083b85SXin LI     deflate() sets strm->adler to the Adler-32 checksum of all input read
342c9083b85SXin LI   so far (that is, total_in bytes).  If a gzip stream is being generated, then
343c9083b85SXin LI   strm->adler will be the CRC-32 checksum of the input read so far.  (See
344c9083b85SXin LI   deflateInit2 below.)
345c9083b85SXin LI 
346c9083b85SXin LI     deflate() may update strm->data_type if it can make a good guess about
347c9083b85SXin LI   the input data type (Z_BINARY or Z_TEXT).  If in doubt, the data is
348c9083b85SXin LI   considered binary.  This field is only for information purposes and does not
349c9083b85SXin LI   affect the compression algorithm in any manner.
350c9083b85SXin LI 
351c9083b85SXin LI     deflate() returns Z_OK if some progress has been made (more input
352c9083b85SXin LI   processed or more output produced), Z_STREAM_END if all input has been
353c9083b85SXin LI   consumed and all output has been produced (only when flush is set to
354c9083b85SXin LI   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
355c9083b85SXin LI   if next_in or next_out was Z_NULL or the state was inadvertently written over
356c9083b85SXin LI   by the application), or Z_BUF_ERROR if no progress is possible (for example
357c9083b85SXin LI   avail_in or avail_out was zero).  Note that Z_BUF_ERROR is not fatal, and
358c9083b85SXin LI   deflate() can be called again with more input and more output space to
359c9083b85SXin LI   continue compressing.
360c9083b85SXin LI */
361c9083b85SXin LI 
362c9083b85SXin LI 
3634717628eSXin LI ZEXTERN int ZEXPORT deflateEnd(z_streamp strm);
364c9083b85SXin LI /*
365c9083b85SXin LI      All dynamically allocated data structures for this stream are freed.
366c9083b85SXin LI    This function discards any unprocessed input and does not flush any pending
367c9083b85SXin LI    output.
368c9083b85SXin LI 
369c9083b85SXin LI      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
370c9083b85SXin LI    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
371c9083b85SXin LI    prematurely (some input or output was discarded).  In the error case, msg
372c9083b85SXin LI    may be set but then points to a static string (which must not be
373c9083b85SXin LI    deallocated).
374c9083b85SXin LI */
375c9083b85SXin LI 
376c9083b85SXin LI 
377c9083b85SXin LI /*
3784717628eSXin LI ZEXTERN int ZEXPORT inflateInit(z_streamp strm);
379c9083b85SXin LI 
380c9083b85SXin LI      Initializes the internal stream state for decompression.  The fields
381c9083b85SXin LI    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
382c9083b85SXin LI    the caller.  In the current version of inflate, the provided input is not
383c9083b85SXin LI    read or consumed.  The allocation of a sliding window will be deferred to
384c9083b85SXin LI    the first call of inflate (if the decompression does not complete on the
385c9083b85SXin LI    first call).  If zalloc and zfree are set to Z_NULL, inflateInit updates
3864717628eSXin LI    them to use default allocation functions.  total_in, total_out, adler, and
3874717628eSXin LI    msg are initialized.
388c9083b85SXin LI 
389c9083b85SXin LI      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
390c9083b85SXin LI    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
391c9083b85SXin LI    version assumed by the caller, or Z_STREAM_ERROR if the parameters are
392c9083b85SXin LI    invalid, such as a null pointer to the structure.  msg is set to null if
393c9083b85SXin LI    there is no error message.  inflateInit does not perform any decompression.
394c9083b85SXin LI    Actual decompression will be done by inflate().  So next_in, and avail_in,
395c9083b85SXin LI    next_out, and avail_out are unused and unchanged.  The current
396c9083b85SXin LI    implementation of inflateInit() does not process any header information --
397c9083b85SXin LI    that is deferred until inflate() is called.
398c9083b85SXin LI */
399c9083b85SXin LI 
400c9083b85SXin LI 
4014717628eSXin LI ZEXTERN int ZEXPORT inflate(z_streamp strm, int flush);
402c9083b85SXin LI /*
403c9083b85SXin LI     inflate decompresses as much data as possible, and stops when the input
404c9083b85SXin LI   buffer becomes empty or the output buffer becomes full.  It may introduce
405c9083b85SXin LI   some output latency (reading input without producing any output) except when
406c9083b85SXin LI   forced to flush.
407c9083b85SXin LI 
408c9083b85SXin LI   The detailed semantics are as follows.  inflate performs one or both of the
409c9083b85SXin LI   following actions:
410c9083b85SXin LI 
411c9083b85SXin LI   - Decompress more input starting at next_in and update next_in and avail_in
412c9083b85SXin LI     accordingly.  If not all input can be processed (because there is not
413c9083b85SXin LI     enough room in the output buffer), then next_in and avail_in are updated
414c9083b85SXin LI     accordingly, and processing will resume at this point for the next call of
415c9083b85SXin LI     inflate().
416c9083b85SXin LI 
417c9083b85SXin LI   - Generate more output starting at next_out and update next_out and avail_out
418c9083b85SXin LI     accordingly.  inflate() provides as much output as possible, until there is
419c9083b85SXin LI     no more input data or no more space in the output buffer (see below about
420c9083b85SXin LI     the flush parameter).
421c9083b85SXin LI 
422c9083b85SXin LI     Before the call of inflate(), the application should ensure that at least
423c9083b85SXin LI   one of the actions is possible, by providing more input and/or consuming more
424c9083b85SXin LI   output, and updating the next_* and avail_* values accordingly.  If the
425c9083b85SXin LI   caller of inflate() does not provide both available input and available
426c9083b85SXin LI   output space, it is possible that there will be no progress made.  The
427c9083b85SXin LI   application can consume the uncompressed output when it wants, for example
428c9083b85SXin LI   when the output buffer is full (avail_out == 0), or after each call of
429c9083b85SXin LI   inflate().  If inflate returns Z_OK and with zero avail_out, it must be
430c9083b85SXin LI   called again after making room in the output buffer because there might be
431c9083b85SXin LI   more output pending.
432c9083b85SXin LI 
433c9083b85SXin LI     The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
434c9083b85SXin LI   Z_BLOCK, or Z_TREES.  Z_SYNC_FLUSH requests that inflate() flush as much
435c9083b85SXin LI   output as possible to the output buffer.  Z_BLOCK requests that inflate()
436c9083b85SXin LI   stop if and when it gets to the next deflate block boundary.  When decoding
437c9083b85SXin LI   the zlib or gzip format, this will cause inflate() to return immediately
438c9083b85SXin LI   after the header and before the first block.  When doing a raw inflate,
439c9083b85SXin LI   inflate() will go ahead and process the first block, and will return when it
440c9083b85SXin LI   gets to the end of that block, or when it runs out of data.
441c9083b85SXin LI 
442c9083b85SXin LI     The Z_BLOCK option assists in appending to or combining deflate streams.
443c9083b85SXin LI   To assist in this, on return inflate() always sets strm->data_type to the
444c9083b85SXin LI   number of unused bits in the last byte taken from strm->next_in, plus 64 if
445c9083b85SXin LI   inflate() is currently decoding the last block in the deflate stream, plus
446c9083b85SXin LI   128 if inflate() returned immediately after decoding an end-of-block code or
447c9083b85SXin LI   decoding the complete header up to just before the first byte of the deflate
448c9083b85SXin LI   stream.  The end-of-block will not be indicated until all of the uncompressed
449c9083b85SXin LI   data from that block has been written to strm->next_out.  The number of
450c9083b85SXin LI   unused bits may in general be greater than seven, except when bit 7 of
451c9083b85SXin LI   data_type is set, in which case the number of unused bits will be less than
452c9083b85SXin LI   eight.  data_type is set as noted here every time inflate() returns for all
453c9083b85SXin LI   flush options, and so can be used to determine the amount of currently
454c9083b85SXin LI   consumed input in bits.
455c9083b85SXin LI 
456c9083b85SXin LI     The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
457c9083b85SXin LI   end of each deflate block header is reached, before any actual data in that
458c9083b85SXin LI   block is decoded.  This allows the caller to determine the length of the
459c9083b85SXin LI   deflate block header for later use in random access within a deflate block.
460c9083b85SXin LI   256 is added to the value of strm->data_type when inflate() returns
461c9083b85SXin LI   immediately after reaching the end of the deflate block header.
462c9083b85SXin LI 
463c9083b85SXin LI     inflate() should normally be called until it returns Z_STREAM_END or an
464c9083b85SXin LI   error.  However if all decompression is to be performed in a single step (a
465c9083b85SXin LI   single call of inflate), the parameter flush should be set to Z_FINISH.  In
466c9083b85SXin LI   this case all pending input is processed and all pending output is flushed;
467c9083b85SXin LI   avail_out must be large enough to hold all of the uncompressed data for the
468c9083b85SXin LI   operation to complete.  (The size of the uncompressed data may have been
469c9083b85SXin LI   saved by the compressor for this purpose.)  The use of Z_FINISH is not
470c9083b85SXin LI   required to perform an inflation in one step.  However it may be used to
471c9083b85SXin LI   inform inflate that a faster approach can be used for the single inflate()
472c9083b85SXin LI   call.  Z_FINISH also informs inflate to not maintain a sliding window if the
473c9083b85SXin LI   stream completes, which reduces inflate's memory footprint.  If the stream
474c9083b85SXin LI   does not complete, either because not all of the stream is provided or not
475c9083b85SXin LI   enough output space is provided, then a sliding window will be allocated and
476c9083b85SXin LI   inflate() can be called again to continue the operation as if Z_NO_FLUSH had
477c9083b85SXin LI   been used.
478c9083b85SXin LI 
479c9083b85SXin LI      In this implementation, inflate() always flushes as much output as
480c9083b85SXin LI   possible to the output buffer, and always uses the faster approach on the
481c9083b85SXin LI   first call.  So the effects of the flush parameter in this implementation are
482c9083b85SXin LI   on the return value of inflate() as noted below, when inflate() returns early
483c9083b85SXin LI   when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of
484c9083b85SXin LI   memory for a sliding window when Z_FINISH is used.
485c9083b85SXin LI 
486c9083b85SXin LI      If a preset dictionary is needed after this call (see inflateSetDictionary
487c9083b85SXin LI   below), inflate sets strm->adler to the Adler-32 checksum of the dictionary
488c9083b85SXin LI   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
489c9083b85SXin LI   strm->adler to the Adler-32 checksum of all output produced so far (that is,
490c9083b85SXin LI   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
491c9083b85SXin LI   below.  At the end of the stream, inflate() checks that its computed Adler-32
492c9083b85SXin LI   checksum is equal to that saved by the compressor and returns Z_STREAM_END
493c9083b85SXin LI   only if the checksum is correct.
494c9083b85SXin LI 
495c9083b85SXin LI     inflate() can decompress and check either zlib-wrapped or gzip-wrapped
496c9083b85SXin LI   deflate data.  The header type is detected automatically, if requested when
497c9083b85SXin LI   initializing with inflateInit2().  Any information contained in the gzip
498c9083b85SXin LI   header is not retained unless inflateGetHeader() is used.  When processing
499c9083b85SXin LI   gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
500c9083b85SXin LI   produced so far.  The CRC-32 is checked against the gzip trailer, as is the
501c9083b85SXin LI   uncompressed length, modulo 2^32.
502c9083b85SXin LI 
503c9083b85SXin LI     inflate() returns Z_OK if some progress has been made (more input processed
504c9083b85SXin LI   or more output produced), Z_STREAM_END if the end of the compressed data has
505c9083b85SXin LI   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
506c9083b85SXin LI   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
507c9083b85SXin LI   corrupted (input stream not conforming to the zlib format or incorrect check
508c9083b85SXin LI   value, in which case strm->msg points to a string with a more specific
509c9083b85SXin LI   error), Z_STREAM_ERROR if the stream structure was inconsistent (for example
510c9083b85SXin LI   next_in or next_out was Z_NULL, or the state was inadvertently written over
511c9083b85SXin LI   by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR
512c9083b85SXin LI   if no progress was possible or if there was not enough room in the output
513c9083b85SXin LI   buffer when Z_FINISH is used.  Note that Z_BUF_ERROR is not fatal, and
514c9083b85SXin LI   inflate() can be called again with more input and more output space to
515c9083b85SXin LI   continue decompressing.  If Z_DATA_ERROR is returned, the application may
516c9083b85SXin LI   then call inflateSync() to look for a good compression block if a partial
517c9083b85SXin LI   recovery of the data is to be attempted.
518c9083b85SXin LI */
519c9083b85SXin LI 
520c9083b85SXin LI 
5214717628eSXin LI ZEXTERN int ZEXPORT inflateEnd(z_streamp strm);
522c9083b85SXin LI /*
523c9083b85SXin LI      All dynamically allocated data structures for this stream are freed.
524c9083b85SXin LI    This function discards any unprocessed input and does not flush any pending
525c9083b85SXin LI    output.
526c9083b85SXin LI 
527c9083b85SXin LI      inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state
528c9083b85SXin LI    was inconsistent.
529c9083b85SXin LI */
530c9083b85SXin LI 
531c9083b85SXin LI 
532c9083b85SXin LI                         /* Advanced functions */
533c9083b85SXin LI 
534c9083b85SXin LI /*
535c9083b85SXin LI     The following functions are needed only in some special applications.
536c9083b85SXin LI */
537c9083b85SXin LI 
538c9083b85SXin LI /*
5394717628eSXin LI ZEXTERN int ZEXPORT deflateInit2(z_streamp strm,
540c9083b85SXin LI                                  int level,
541c9083b85SXin LI                                  int method,
542c9083b85SXin LI                                  int windowBits,
543c9083b85SXin LI                                  int memLevel,
5444717628eSXin LI                                  int strategy);
545c9083b85SXin LI 
546c9083b85SXin LI      This is another version of deflateInit with more compression options.  The
547cd882207SXin LI    fields zalloc, zfree and opaque must be initialized before by the caller.
548c9083b85SXin LI 
549c9083b85SXin LI      The method parameter is the compression method.  It must be Z_DEFLATED in
550c9083b85SXin LI    this version of the library.
551c9083b85SXin LI 
552c9083b85SXin LI      The windowBits parameter is the base two logarithm of the window size
553c9083b85SXin LI    (the size of the history buffer).  It should be in the range 8..15 for this
554c9083b85SXin LI    version of the library.  Larger values of this parameter result in better
555c9083b85SXin LI    compression at the expense of memory usage.  The default value is 15 if
556c9083b85SXin LI    deflateInit is used instead.
557c9083b85SXin LI 
558c9083b85SXin LI      For the current implementation of deflate(), a windowBits value of 8 (a
559c9083b85SXin LI    window size of 256 bytes) is not supported.  As a result, a request for 8
560c9083b85SXin LI    will result in 9 (a 512-byte window).  In that case, providing 8 to
561c9083b85SXin LI    inflateInit2() will result in an error when the zlib header with 9 is
562c9083b85SXin LI    checked against the initialization of inflate().  The remedy is to not use 8
563c9083b85SXin LI    with deflateInit2() with this initialization, or at least in that case use 9
564c9083b85SXin LI    with inflateInit2().
565c9083b85SXin LI 
566c9083b85SXin LI      windowBits can also be -8..-15 for raw deflate.  In this case, -windowBits
567c9083b85SXin LI    determines the window size.  deflate() will then generate raw deflate data
568c9083b85SXin LI    with no zlib header or trailer, and will not compute a check value.
569c9083b85SXin LI 
570c9083b85SXin LI      windowBits can also be greater than 15 for optional gzip encoding.  Add
571c9083b85SXin LI    16 to windowBits to write a simple gzip header and trailer around the
572c9083b85SXin LI    compressed data instead of a zlib wrapper.  The gzip header will have no
573c9083b85SXin LI    file name, no extra data, no comment, no modification time (set to zero), no
574c9083b85SXin LI    header crc, and the operating system will be set to the appropriate value,
575c9083b85SXin LI    if the operating system was determined at compile time.  If a gzip stream is
576c9083b85SXin LI    being written, strm->adler is a CRC-32 instead of an Adler-32.
577c9083b85SXin LI 
578c9083b85SXin LI      For raw deflate or gzip encoding, a request for a 256-byte window is
579c9083b85SXin LI    rejected as invalid, since only the zlib header provides a means of
580c9083b85SXin LI    transmitting the window size to the decompressor.
581c9083b85SXin LI 
582c9083b85SXin LI      The memLevel parameter specifies how much memory should be allocated
583c9083b85SXin LI    for the internal compression state.  memLevel=1 uses minimum memory but is
584c9083b85SXin LI    slow and reduces compression ratio; memLevel=9 uses maximum memory for
585c9083b85SXin LI    optimal speed.  The default value is 8.  See zconf.h for total memory usage
586c9083b85SXin LI    as a function of windowBits and memLevel.
587c9083b85SXin LI 
588c9083b85SXin LI      The strategy parameter is used to tune the compression algorithm.  Use the
589c9083b85SXin LI    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
590c9083b85SXin LI    filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
591c9083b85SXin LI    string match), or Z_RLE to limit match distances to one (run-length
592c9083b85SXin LI    encoding).  Filtered data consists mostly of small values with a somewhat
593c9083b85SXin LI    random distribution.  In this case, the compression algorithm is tuned to
594c9083b85SXin LI    compress them better.  The effect of Z_FILTERED is to force more Huffman
595c9083b85SXin LI    coding and less string matching; it is somewhat intermediate between
596c9083b85SXin LI    Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY.  Z_RLE is designed to be almost as
597c9083b85SXin LI    fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data.  The
598c9083b85SXin LI    strategy parameter only affects the compression ratio but not the
599c9083b85SXin LI    correctness of the compressed output even if it is not set appropriately.
600c9083b85SXin LI    Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
601c9083b85SXin LI    decoder for special applications.
602c9083b85SXin LI 
603c9083b85SXin LI      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
604c9083b85SXin LI    memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
605c9083b85SXin LI    method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
606c9083b85SXin LI    incompatible with the version assumed by the caller (ZLIB_VERSION).  msg is
607c9083b85SXin LI    set to null if there is no error message.  deflateInit2 does not perform any
608c9083b85SXin LI    compression: this will be done by deflate().
609c9083b85SXin LI */
610c9083b85SXin LI 
6114717628eSXin LI ZEXTERN int ZEXPORT deflateSetDictionary(z_streamp strm,
612c9083b85SXin LI                                          const Bytef *dictionary,
6134717628eSXin LI                                          uInt  dictLength);
614c9083b85SXin LI /*
615c9083b85SXin LI      Initializes the compression dictionary from the given byte sequence
616c9083b85SXin LI    without producing any compressed output.  When using the zlib format, this
617c9083b85SXin LI    function must be called immediately after deflateInit, deflateInit2 or
618c9083b85SXin LI    deflateReset, and before any call of deflate.  When doing raw deflate, this
619c9083b85SXin LI    function must be called either before any call of deflate, or immediately
620c9083b85SXin LI    after the completion of a deflate block, i.e. after all input has been
621c9083b85SXin LI    consumed and all output has been delivered when using any of the flush
622c9083b85SXin LI    options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH.  The
623c9083b85SXin LI    compressor and decompressor must use exactly the same dictionary (see
624c9083b85SXin LI    inflateSetDictionary).
625c9083b85SXin LI 
626c9083b85SXin LI      The dictionary should consist of strings (byte sequences) that are likely
627c9083b85SXin LI    to be encountered later in the data to be compressed, with the most commonly
628c9083b85SXin LI    used strings preferably put towards the end of the dictionary.  Using a
629c9083b85SXin LI    dictionary is most useful when the data to be compressed is short and can be
630c9083b85SXin LI    predicted with good accuracy; the data can then be compressed better than
631c9083b85SXin LI    with the default empty dictionary.
632c9083b85SXin LI 
633c9083b85SXin LI      Depending on the size of the compression data structures selected by
634c9083b85SXin LI    deflateInit or deflateInit2, a part of the dictionary may in effect be
635c9083b85SXin LI    discarded, for example if the dictionary is larger than the window size
636c9083b85SXin LI    provided in deflateInit or deflateInit2.  Thus the strings most likely to be
637c9083b85SXin LI    useful should be put at the end of the dictionary, not at the front.  In
638c9083b85SXin LI    addition, the current implementation of deflate will use at most the window
639c9083b85SXin LI    size minus 262 bytes of the provided dictionary.
640c9083b85SXin LI 
641c9083b85SXin LI      Upon return of this function, strm->adler is set to the Adler-32 value
642c9083b85SXin LI    of the dictionary; the decompressor may later use this value to determine
643c9083b85SXin LI    which dictionary has been used by the compressor.  (The Adler-32 value
644c9083b85SXin LI    applies to the whole dictionary even if only a subset of the dictionary is
645c9083b85SXin LI    actually used by the compressor.) If a raw deflate was requested, then the
646c9083b85SXin LI    Adler-32 value is not computed and strm->adler is not set.
647c9083b85SXin LI 
648c9083b85SXin LI      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
649c9083b85SXin LI    parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
650c9083b85SXin LI    inconsistent (for example if deflate has already been called for this stream
651c9083b85SXin LI    or if not at a block boundary for raw deflate).  deflateSetDictionary does
652c9083b85SXin LI    not perform any compression: this will be done by deflate().
653c9083b85SXin LI */
654c9083b85SXin LI 
6554717628eSXin LI ZEXTERN int ZEXPORT deflateGetDictionary(z_streamp strm,
656c9083b85SXin LI                                          Bytef *dictionary,
6574717628eSXin LI                                          uInt  *dictLength);
658c9083b85SXin LI /*
659c9083b85SXin LI      Returns the sliding dictionary being maintained by deflate.  dictLength is
660c9083b85SXin LI    set to the number of bytes in the dictionary, and that many bytes are copied
661c9083b85SXin LI    to dictionary.  dictionary must have enough space, where 32768 bytes is
662c9083b85SXin LI    always enough.  If deflateGetDictionary() is called with dictionary equal to
663c9083b85SXin LI    Z_NULL, then only the dictionary length is returned, and nothing is copied.
664e37bb444SXin LI    Similarly, if dictLength is Z_NULL, then it is not set.
665c9083b85SXin LI 
666c9083b85SXin LI      deflateGetDictionary() may return a length less than the window size, even
667c9083b85SXin LI    when more than the window size in input has been provided. It may return up
668c9083b85SXin LI    to 258 bytes less in that case, due to how zlib's implementation of deflate
669c9083b85SXin LI    manages the sliding window and lookahead for matches, where matches can be
670c9083b85SXin LI    up to 258 bytes long. If the application needs the last window-size bytes of
671c9083b85SXin LI    input, then that would need to be saved by the application outside of zlib.
672c9083b85SXin LI 
673c9083b85SXin LI      deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
674c9083b85SXin LI    stream state is inconsistent.
675c9083b85SXin LI */
676c9083b85SXin LI 
6774717628eSXin LI ZEXTERN int ZEXPORT deflateCopy(z_streamp dest,
6784717628eSXin LI                                 z_streamp source);
679c9083b85SXin LI /*
680c9083b85SXin LI      Sets the destination stream as a complete copy of the source stream.
681c9083b85SXin LI 
682c9083b85SXin LI      This function can be useful when several compression strategies will be
683c9083b85SXin LI    tried, for example when there are several ways of pre-processing the input
684c9083b85SXin LI    data with a filter.  The streams that will be discarded should then be freed
685c9083b85SXin LI    by calling deflateEnd.  Note that deflateCopy duplicates the internal
686c9083b85SXin LI    compression state which can be quite large, so this strategy is slow and can
687c9083b85SXin LI    consume lots of memory.
688c9083b85SXin LI 
689c9083b85SXin LI      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
690c9083b85SXin LI    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
691c9083b85SXin LI    (such as zalloc being Z_NULL).  msg is left unchanged in both source and
692c9083b85SXin LI    destination.
693c9083b85SXin LI */
694c9083b85SXin LI 
6954717628eSXin LI ZEXTERN int ZEXPORT deflateReset(z_streamp strm);
696c9083b85SXin LI /*
697c9083b85SXin LI      This function is equivalent to deflateEnd followed by deflateInit, but
698c9083b85SXin LI    does not free and reallocate the internal compression state.  The stream
699c9083b85SXin LI    will leave the compression level and any other attributes that may have been
7004717628eSXin LI    set unchanged.  total_in, total_out, adler, and msg are initialized.
701c9083b85SXin LI 
702c9083b85SXin LI      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
703c9083b85SXin LI    stream state was inconsistent (such as zalloc or state being Z_NULL).
704c9083b85SXin LI */
705c9083b85SXin LI 
7064717628eSXin LI ZEXTERN int ZEXPORT deflateParams(z_streamp strm,
707c9083b85SXin LI                                   int level,
7084717628eSXin LI                                   int strategy);
709c9083b85SXin LI /*
710c9083b85SXin LI      Dynamically update the compression level and compression strategy.  The
711c9083b85SXin LI    interpretation of level and strategy is as in deflateInit2().  This can be
712c9083b85SXin LI    used to switch between compression and straight copy of the input data, or
713c9083b85SXin LI    to switch to a different kind of input data requiring a different strategy.
714c9083b85SXin LI    If the compression approach (which is a function of the level) or the
715c9083b85SXin LI    strategy is changed, and if there have been any deflate() calls since the
716c9083b85SXin LI    state was initialized or reset, then the input available so far is
717c9083b85SXin LI    compressed with the old level and strategy using deflate(strm, Z_BLOCK).
718c9083b85SXin LI    There are three approaches for the compression levels 0, 1..3, and 4..9
719c9083b85SXin LI    respectively.  The new level and strategy will take effect at the next call
720c9083b85SXin LI    of deflate().
721c9083b85SXin LI 
722c9083b85SXin LI      If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
723c9083b85SXin LI    not have enough output space to complete, then the parameter change will not
724c9083b85SXin LI    take effect.  In this case, deflateParams() can be called again with the
725c9083b85SXin LI    same parameters and more output space to try again.
726c9083b85SXin LI 
727c9083b85SXin LI      In order to assure a change in the parameters on the first try, the
728c9083b85SXin LI    deflate stream should be flushed using deflate() with Z_BLOCK or other flush
729c9083b85SXin LI    request until strm.avail_out is not zero, before calling deflateParams().
730c9083b85SXin LI    Then no more input data should be provided before the deflateParams() call.
731c9083b85SXin LI    If this is done, the old level and strategy will be applied to the data
732c9083b85SXin LI    compressed before deflateParams(), and the new level and strategy will be
7334717628eSXin LI    applied to the data compressed after deflateParams().
734c9083b85SXin LI 
735c9083b85SXin LI      deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream
736c9083b85SXin LI    state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if
737c9083b85SXin LI    there was not enough output space to complete the compression of the
738c9083b85SXin LI    available input data before a change in the strategy or approach.  Note that
739c9083b85SXin LI    in the case of a Z_BUF_ERROR, the parameters are not changed.  A return
740c9083b85SXin LI    value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be
741c9083b85SXin LI    retried with more output space.
742c9083b85SXin LI */
743c9083b85SXin LI 
7444717628eSXin LI ZEXTERN int ZEXPORT deflateTune(z_streamp strm,
745c9083b85SXin LI                                 int good_length,
746c9083b85SXin LI                                 int max_lazy,
747c9083b85SXin LI                                 int nice_length,
7484717628eSXin LI                                 int max_chain);
749c9083b85SXin LI /*
750c9083b85SXin LI      Fine tune deflate's internal compression parameters.  This should only be
751c9083b85SXin LI    used by someone who understands the algorithm used by zlib's deflate for
752c9083b85SXin LI    searching for the best matching string, and even then only by the most
753c9083b85SXin LI    fanatic optimizer trying to squeeze out the last compressed bit for their
754c9083b85SXin LI    specific input data.  Read the deflate.c source code for the meaning of the
755c9083b85SXin LI    max_lazy, good_length, nice_length, and max_chain parameters.
756c9083b85SXin LI 
757c9083b85SXin LI      deflateTune() can be called after deflateInit() or deflateInit2(), and
758c9083b85SXin LI    returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
759c9083b85SXin LI  */
760c9083b85SXin LI 
7614717628eSXin LI ZEXTERN uLong ZEXPORT deflateBound(z_streamp strm,
7624717628eSXin LI                                    uLong sourceLen);
763c9083b85SXin LI /*
764c9083b85SXin LI      deflateBound() returns an upper bound on the compressed size after
765c9083b85SXin LI    deflation of sourceLen bytes.  It must be called after deflateInit() or
766c9083b85SXin LI    deflateInit2(), and after deflateSetHeader(), if used.  This would be used
767c9083b85SXin LI    to allocate an output buffer for deflation in a single pass, and so would be
768c9083b85SXin LI    called before deflate().  If that first deflate() call is provided the
769c9083b85SXin LI    sourceLen input bytes, an output buffer allocated to the size returned by
770c9083b85SXin LI    deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed
771c9083b85SXin LI    to return Z_STREAM_END.  Note that it is possible for the compressed size to
772c9083b85SXin LI    be larger than the value returned by deflateBound() if flush options other
773c9083b85SXin LI    than Z_FINISH or Z_NO_FLUSH are used.
774c9083b85SXin LI */
775c9083b85SXin LI 
7764717628eSXin LI ZEXTERN int ZEXPORT deflatePending(z_streamp strm,
777c9083b85SXin LI                                    unsigned *pending,
7784717628eSXin LI                                    int *bits);
779c9083b85SXin LI /*
780c9083b85SXin LI      deflatePending() returns the number of bytes and bits of output that have
781c9083b85SXin LI    been generated, but not yet provided in the available output.  The bytes not
782c9083b85SXin LI    provided would be due to the available output space having being consumed.
783c9083b85SXin LI    The number of bits of output not provided are between 0 and 7, where they
784c9083b85SXin LI    await more bits to join them in order to fill out a full byte.  If pending
785c9083b85SXin LI    or bits are Z_NULL, then those values are not set.
786c9083b85SXin LI 
787c9083b85SXin LI      deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
788c9083b85SXin LI    stream state was inconsistent.
789c9083b85SXin LI  */
790c9083b85SXin LI 
7914717628eSXin LI ZEXTERN int ZEXPORT deflatePrime(z_streamp strm,
792c9083b85SXin LI                                  int bits,
7934717628eSXin LI                                  int value);
794c9083b85SXin LI /*
795c9083b85SXin LI      deflatePrime() inserts bits in the deflate output stream.  The intent
796c9083b85SXin LI    is that this function is used to start off the deflate output with the bits
797c9083b85SXin LI    leftover from a previous deflate stream when appending to it.  As such, this
798c9083b85SXin LI    function can only be used for raw deflate, and must be used before the first
799c9083b85SXin LI    deflate() call after a deflateInit2() or deflateReset().  bits must be less
800c9083b85SXin LI    than or equal to 16, and that many of the least significant bits of value
801c9083b85SXin LI    will be inserted in the output.
802c9083b85SXin LI 
803c9083b85SXin LI      deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough
804c9083b85SXin LI    room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the
805c9083b85SXin LI    source stream state was inconsistent.
806c9083b85SXin LI */
807c9083b85SXin LI 
8084717628eSXin LI ZEXTERN int ZEXPORT deflateSetHeader(z_streamp strm,
8094717628eSXin LI                                      gz_headerp head);
810c9083b85SXin LI /*
811c9083b85SXin LI      deflateSetHeader() provides gzip header information for when a gzip
812c9083b85SXin LI    stream is requested by deflateInit2().  deflateSetHeader() may be called
813c9083b85SXin LI    after deflateInit2() or deflateReset() and before the first call of
814c9083b85SXin LI    deflate().  The text, time, os, extra field, name, and comment information
815c9083b85SXin LI    in the provided gz_header structure are written to the gzip header (xflag is
816c9083b85SXin LI    ignored -- the extra flags are set according to the compression level).  The
817c9083b85SXin LI    caller must assure that, if not Z_NULL, name and comment are terminated with
818c9083b85SXin LI    a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
819c9083b85SXin LI    available there.  If hcrc is true, a gzip header crc is included.  Note that
820c9083b85SXin LI    the current versions of the command-line version of gzip (up through version
821c9083b85SXin LI    1.3.x) do not support header crc's, and will report that it is a "multi-part
822c9083b85SXin LI    gzip file" and give up.
823c9083b85SXin LI 
824c9083b85SXin LI      If deflateSetHeader is not used, the default gzip header has text false,
8254717628eSXin LI    the time set to zero, and os set to the current operating system, with no
8264717628eSXin LI    extra, name, or comment fields.  The gzip header is returned to the default
8274717628eSXin LI    state by deflateReset().
828c9083b85SXin LI 
829c9083b85SXin LI      deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
830c9083b85SXin LI    stream state was inconsistent.
831c9083b85SXin LI */
832c9083b85SXin LI 
833c9083b85SXin LI /*
8344717628eSXin LI ZEXTERN int ZEXPORT inflateInit2(z_streamp strm,
8354717628eSXin LI                                  int windowBits);
836c9083b85SXin LI 
837c9083b85SXin LI      This is another version of inflateInit with an extra parameter.  The
838c9083b85SXin LI    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
839c9083b85SXin LI    before by the caller.
840c9083b85SXin LI 
841c9083b85SXin LI      The windowBits parameter is the base two logarithm of the maximum window
842c9083b85SXin LI    size (the size of the history buffer).  It should be in the range 8..15 for
843c9083b85SXin LI    this version of the library.  The default value is 15 if inflateInit is used
844c9083b85SXin LI    instead.  windowBits must be greater than or equal to the windowBits value
845c9083b85SXin LI    provided to deflateInit2() while compressing, or it must be equal to 15 if
846c9083b85SXin LI    deflateInit2() was not used.  If a compressed stream with a larger window
847c9083b85SXin LI    size is given as input, inflate() will return with the error code
848c9083b85SXin LI    Z_DATA_ERROR instead of trying to allocate a larger window.
849c9083b85SXin LI 
850c9083b85SXin LI      windowBits can also be zero to request that inflate use the window size in
851c9083b85SXin LI    the zlib header of the compressed stream.
852c9083b85SXin LI 
853c9083b85SXin LI      windowBits can also be -8..-15 for raw inflate.  In this case, -windowBits
854c9083b85SXin LI    determines the window size.  inflate() will then process raw deflate data,
855c9083b85SXin LI    not looking for a zlib or gzip header, not generating a check value, and not
856c9083b85SXin LI    looking for any check values for comparison at the end of the stream.  This
857c9083b85SXin LI    is for use with other formats that use the deflate compressed data format
858c9083b85SXin LI    such as zip.  Those formats provide their own check values.  If a custom
859c9083b85SXin LI    format is developed using the raw deflate format for compressed data, it is
860c9083b85SXin LI    recommended that a check value such as an Adler-32 or a CRC-32 be applied to
861c9083b85SXin LI    the uncompressed data as is done in the zlib, gzip, and zip formats.  For
862c9083b85SXin LI    most applications, the zlib format should be used as is.  Note that comments
863c9083b85SXin LI    above on the use in deflateInit2() applies to the magnitude of windowBits.
864c9083b85SXin LI 
865c9083b85SXin LI      windowBits can also be greater than 15 for optional gzip decoding.  Add
866c9083b85SXin LI    32 to windowBits to enable zlib and gzip decoding with automatic header
867c9083b85SXin LI    detection, or add 16 to decode only the gzip format (the zlib format will
868c9083b85SXin LI    return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is a
869c9083b85SXin LI    CRC-32 instead of an Adler-32.  Unlike the gunzip utility and gzread() (see
870cd882207SXin LI    below), inflate() will *not* automatically decode concatenated gzip members.
871cd882207SXin LI    inflate() will return Z_STREAM_END at the end of the gzip member.  The state
872cd882207SXin LI    would need to be reset to continue decoding a subsequent gzip member.  This
873cd882207SXin LI    *must* be done if there is more data after a gzip member, in order for the
874cd882207SXin LI    decompression to be compliant with the gzip standard (RFC 1952).
875c9083b85SXin LI 
876c9083b85SXin LI      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
877c9083b85SXin LI    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
878c9083b85SXin LI    version assumed by the caller, or Z_STREAM_ERROR if the parameters are
879c9083b85SXin LI    invalid, such as a null pointer to the structure.  msg is set to null if
880c9083b85SXin LI    there is no error message.  inflateInit2 does not perform any decompression
881c9083b85SXin LI    apart from possibly reading the zlib header if present: actual decompression
882c9083b85SXin LI    will be done by inflate().  (So next_in and avail_in may be modified, but
883c9083b85SXin LI    next_out and avail_out are unused and unchanged.) The current implementation
884c9083b85SXin LI    of inflateInit2() does not process any header information -- that is
885c9083b85SXin LI    deferred until inflate() is called.
886c9083b85SXin LI */
887c9083b85SXin LI 
8884717628eSXin LI ZEXTERN int ZEXPORT inflateSetDictionary(z_streamp strm,
889c9083b85SXin LI                                          const Bytef *dictionary,
8904717628eSXin LI                                          uInt  dictLength);
891c9083b85SXin LI /*
892c9083b85SXin LI      Initializes the decompression dictionary from the given uncompressed byte
893c9083b85SXin LI    sequence.  This function must be called immediately after a call of inflate,
894c9083b85SXin LI    if that call returned Z_NEED_DICT.  The dictionary chosen by the compressor
895c9083b85SXin LI    can be determined from the Adler-32 value returned by that call of inflate.
896c9083b85SXin LI    The compressor and decompressor must use exactly the same dictionary (see
897c9083b85SXin LI    deflateSetDictionary).  For raw inflate, this function can be called at any
898c9083b85SXin LI    time to set the dictionary.  If the provided dictionary is smaller than the
899c9083b85SXin LI    window and there is already data in the window, then the provided dictionary
900c9083b85SXin LI    will amend what's there.  The application must insure that the dictionary
901c9083b85SXin LI    that was used for compression is provided.
902c9083b85SXin LI 
903c9083b85SXin LI      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
904c9083b85SXin LI    parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
905c9083b85SXin LI    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
906c9083b85SXin LI    expected one (incorrect Adler-32 value).  inflateSetDictionary does not
907c9083b85SXin LI    perform any decompression: this will be done by subsequent calls of
908c9083b85SXin LI    inflate().
909c9083b85SXin LI */
910c9083b85SXin LI 
9114717628eSXin LI ZEXTERN int ZEXPORT inflateGetDictionary(z_streamp strm,
912c9083b85SXin LI                                          Bytef *dictionary,
9134717628eSXin LI                                          uInt  *dictLength);
914c9083b85SXin LI /*
915c9083b85SXin LI      Returns the sliding dictionary being maintained by inflate.  dictLength is
916c9083b85SXin LI    set to the number of bytes in the dictionary, and that many bytes are copied
917c9083b85SXin LI    to dictionary.  dictionary must have enough space, where 32768 bytes is
918c9083b85SXin LI    always enough.  If inflateGetDictionary() is called with dictionary equal to
919c9083b85SXin LI    Z_NULL, then only the dictionary length is returned, and nothing is copied.
920e37bb444SXin LI    Similarly, if dictLength is Z_NULL, then it is not set.
921c9083b85SXin LI 
922c9083b85SXin LI      inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
923c9083b85SXin LI    stream state is inconsistent.
924c9083b85SXin LI */
925c9083b85SXin LI 
9264717628eSXin LI ZEXTERN int ZEXPORT inflateSync(z_streamp strm);
927c9083b85SXin LI /*
928c9083b85SXin LI      Skips invalid compressed data until a possible full flush point (see above
929c9083b85SXin LI    for the description of deflate with Z_FULL_FLUSH) can be found, or until all
930c9083b85SXin LI    available input is skipped.  No output is provided.
931c9083b85SXin LI 
932c9083b85SXin LI      inflateSync searches for a 00 00 FF FF pattern in the compressed data.
933c9083b85SXin LI    All full flush points have this pattern, but not all occurrences of this
934c9083b85SXin LI    pattern are full flush points.
935c9083b85SXin LI 
936c9083b85SXin LI      inflateSync returns Z_OK if a possible full flush point has been found,
937c9083b85SXin LI    Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point
938c9083b85SXin LI    has been found, or Z_STREAM_ERROR if the stream structure was inconsistent.
939*6255c67cSXin LI    In the success case, the application may save the current value of total_in
940*6255c67cSXin LI    which indicates where valid compressed data was found.  In the error case,
941*6255c67cSXin LI    the application may repeatedly call inflateSync, providing more input each
942*6255c67cSXin LI    time, until success or end of the input data.
943c9083b85SXin LI */
944c9083b85SXin LI 
9454717628eSXin LI ZEXTERN int ZEXPORT inflateCopy(z_streamp dest,
9464717628eSXin LI                                 z_streamp source);
947c9083b85SXin LI /*
948c9083b85SXin LI      Sets the destination stream as a complete copy of the source stream.
949c9083b85SXin LI 
950c9083b85SXin LI      This function can be useful when randomly accessing a large stream.  The
951c9083b85SXin LI    first pass through the stream can periodically record the inflate state,
952c9083b85SXin LI    allowing restarting inflate at those points when randomly accessing the
953c9083b85SXin LI    stream.
954c9083b85SXin LI 
955c9083b85SXin LI      inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
956c9083b85SXin LI    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
957c9083b85SXin LI    (such as zalloc being Z_NULL).  msg is left unchanged in both source and
958c9083b85SXin LI    destination.
959c9083b85SXin LI */
960c9083b85SXin LI 
9614717628eSXin LI ZEXTERN int ZEXPORT inflateReset(z_streamp strm);
962c9083b85SXin LI /*
963c9083b85SXin LI      This function is equivalent to inflateEnd followed by inflateInit,
964c9083b85SXin LI    but does not free and reallocate the internal decompression state.  The
965c9083b85SXin LI    stream will keep attributes that may have been set by inflateInit2.
9664717628eSXin LI    total_in, total_out, adler, and msg are initialized.
967c9083b85SXin LI 
968c9083b85SXin LI      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
969c9083b85SXin LI    stream state was inconsistent (such as zalloc or state being Z_NULL).
970c9083b85SXin LI */
971c9083b85SXin LI 
9724717628eSXin LI ZEXTERN int ZEXPORT inflateReset2(z_streamp strm,
9734717628eSXin LI                                   int windowBits);
974c9083b85SXin LI /*
975c9083b85SXin LI      This function is the same as inflateReset, but it also permits changing
976c9083b85SXin LI    the wrap and window size requests.  The windowBits parameter is interpreted
977c9083b85SXin LI    the same as it is for inflateInit2.  If the window size is changed, then the
978c9083b85SXin LI    memory allocated for the window is freed, and the window will be reallocated
979c9083b85SXin LI    by inflate() if needed.
980c9083b85SXin LI 
981c9083b85SXin LI      inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
982c9083b85SXin LI    stream state was inconsistent (such as zalloc or state being Z_NULL), or if
983c9083b85SXin LI    the windowBits parameter is invalid.
984c9083b85SXin LI */
985c9083b85SXin LI 
9864717628eSXin LI ZEXTERN int ZEXPORT inflatePrime(z_streamp strm,
987c9083b85SXin LI                                  int bits,
9884717628eSXin LI                                  int value);
989c9083b85SXin LI /*
990c9083b85SXin LI      This function inserts bits in the inflate input stream.  The intent is
991c9083b85SXin LI    that this function is used to start inflating at a bit position in the
992c9083b85SXin LI    middle of a byte.  The provided bits will be used before any bytes are used
993c9083b85SXin LI    from next_in.  This function should only be used with raw inflate, and
994c9083b85SXin LI    should be used before the first inflate() call after inflateInit2() or
995c9083b85SXin LI    inflateReset().  bits must be less than or equal to 16, and that many of the
996c9083b85SXin LI    least significant bits of value will be inserted in the input.
997c9083b85SXin LI 
998c9083b85SXin LI      If bits is negative, then the input stream bit buffer is emptied.  Then
999c9083b85SXin LI    inflatePrime() can be called again to put bits in the buffer.  This is used
1000c9083b85SXin LI    to clear out bits leftover after feeding inflate a block description prior
1001c9083b85SXin LI    to feeding inflate codes.
1002c9083b85SXin LI 
1003c9083b85SXin LI      inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
1004c9083b85SXin LI    stream state was inconsistent.
1005c9083b85SXin LI */
1006c9083b85SXin LI 
10074717628eSXin LI ZEXTERN long ZEXPORT inflateMark(z_streamp strm);
1008c9083b85SXin LI /*
1009c9083b85SXin LI      This function returns two values, one in the lower 16 bits of the return
1010c9083b85SXin LI    value, and the other in the remaining upper bits, obtained by shifting the
1011c9083b85SXin LI    return value down 16 bits.  If the upper value is -1 and the lower value is
1012c9083b85SXin LI    zero, then inflate() is currently decoding information outside of a block.
1013c9083b85SXin LI    If the upper value is -1 and the lower value is non-zero, then inflate is in
1014c9083b85SXin LI    the middle of a stored block, with the lower value equaling the number of
1015c9083b85SXin LI    bytes from the input remaining to copy.  If the upper value is not -1, then
1016c9083b85SXin LI    it is the number of bits back from the current bit position in the input of
1017c9083b85SXin LI    the code (literal or length/distance pair) currently being processed.  In
1018c9083b85SXin LI    that case the lower value is the number of bytes already emitted for that
1019c9083b85SXin LI    code.
1020c9083b85SXin LI 
1021c9083b85SXin LI      A code is being processed if inflate is waiting for more input to complete
1022c9083b85SXin LI    decoding of the code, or if it has completed decoding but is waiting for
1023c9083b85SXin LI    more output space to write the literal or match data.
1024c9083b85SXin LI 
1025c9083b85SXin LI      inflateMark() is used to mark locations in the input data for random
1026c9083b85SXin LI    access, which may be at bit positions, and to note those cases where the
1027c9083b85SXin LI    output of a code may span boundaries of random access blocks.  The current
1028c9083b85SXin LI    location in the input stream can be determined from avail_in and data_type
1029c9083b85SXin LI    as noted in the description for the Z_BLOCK flush parameter for inflate.
1030c9083b85SXin LI 
1031c9083b85SXin LI      inflateMark returns the value noted above, or -65536 if the provided
1032c9083b85SXin LI    source stream state was inconsistent.
1033c9083b85SXin LI */
1034c9083b85SXin LI 
10354717628eSXin LI ZEXTERN int ZEXPORT inflateGetHeader(z_streamp strm,
10364717628eSXin LI                                      gz_headerp head);
1037c9083b85SXin LI /*
1038c9083b85SXin LI      inflateGetHeader() requests that gzip header information be stored in the
1039c9083b85SXin LI    provided gz_header structure.  inflateGetHeader() may be called after
1040c9083b85SXin LI    inflateInit2() or inflateReset(), and before the first call of inflate().
1041c9083b85SXin LI    As inflate() processes the gzip stream, head->done is zero until the header
1042c9083b85SXin LI    is completed, at which time head->done is set to one.  If a zlib stream is
1043c9083b85SXin LI    being decoded, then head->done is set to -1 to indicate that there will be
1044c9083b85SXin LI    no gzip header information forthcoming.  Note that Z_BLOCK or Z_TREES can be
1045c9083b85SXin LI    used to force inflate() to return immediately after header processing is
1046c9083b85SXin LI    complete and before any actual data is decompressed.
1047c9083b85SXin LI 
1048c9083b85SXin LI      The text, time, xflags, and os fields are filled in with the gzip header
1049c9083b85SXin LI    contents.  hcrc is set to true if there is a header CRC.  (The header CRC
1050c9083b85SXin LI    was valid if done is set to one.) If extra is not Z_NULL, then extra_max
1051c9083b85SXin LI    contains the maximum number of bytes to write to extra.  Once done is true,
1052c9083b85SXin LI    extra_len contains the actual extra field length, and extra contains the
1053c9083b85SXin LI    extra field, or that field truncated if extra_max is less than extra_len.
1054c9083b85SXin LI    If name is not Z_NULL, then up to name_max characters are written there,
1055c9083b85SXin LI    terminated with a zero unless the length is greater than name_max.  If
1056c9083b85SXin LI    comment is not Z_NULL, then up to comm_max characters are written there,
1057c9083b85SXin LI    terminated with a zero unless the length is greater than comm_max.  When any
1058c9083b85SXin LI    of extra, name, or comment are not Z_NULL and the respective field is not
1059c9083b85SXin LI    present in the header, then that field is set to Z_NULL to signal its
1060c9083b85SXin LI    absence.  This allows the use of deflateSetHeader() with the returned
1061c9083b85SXin LI    structure to duplicate the header.  However if those fields are set to
1062c9083b85SXin LI    allocated memory, then the application will need to save those pointers
1063c9083b85SXin LI    elsewhere so that they can be eventually freed.
1064c9083b85SXin LI 
1065c9083b85SXin LI      If inflateGetHeader is not used, then the header information is simply
1066c9083b85SXin LI    discarded.  The header is always checked for validity, including the header
1067c9083b85SXin LI    CRC if present.  inflateReset() will reset the process to discard the header
1068c9083b85SXin LI    information.  The application would need to call inflateGetHeader() again to
1069c9083b85SXin LI    retrieve the header from the next gzip stream.
1070c9083b85SXin LI 
1071c9083b85SXin LI      inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
1072c9083b85SXin LI    stream state was inconsistent.
1073c9083b85SXin LI */
1074c9083b85SXin LI 
1075c9083b85SXin LI /*
10764717628eSXin LI ZEXTERN int ZEXPORT inflateBackInit(z_streamp strm, int windowBits,
10774717628eSXin LI                                     unsigned char FAR *window);
1078c9083b85SXin LI 
1079c9083b85SXin LI      Initialize the internal stream state for decompression using inflateBack()
1080c9083b85SXin LI    calls.  The fields zalloc, zfree and opaque in strm must be initialized
1081c9083b85SXin LI    before the call.  If zalloc and zfree are Z_NULL, then the default library-
1082c9083b85SXin LI    derived memory allocation routines are used.  windowBits is the base two
1083c9083b85SXin LI    logarithm of the window size, in the range 8..15.  window is a caller
1084c9083b85SXin LI    supplied buffer of that size.  Except for special applications where it is
1085c9083b85SXin LI    assured that deflate was used with small window sizes, windowBits must be 15
1086c9083b85SXin LI    and a 32K byte window must be supplied to be able to decompress general
1087c9083b85SXin LI    deflate streams.
1088c9083b85SXin LI 
1089c9083b85SXin LI      See inflateBack() for the usage of these routines.
1090c9083b85SXin LI 
1091c9083b85SXin LI      inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
1092c9083b85SXin LI    the parameters are invalid, Z_MEM_ERROR if the internal state could not be
1093c9083b85SXin LI    allocated, or Z_VERSION_ERROR if the version of the library does not match
1094c9083b85SXin LI    the version of the header file.
1095c9083b85SXin LI */
1096c9083b85SXin LI 
10974717628eSXin LI typedef unsigned (*in_func)(void FAR *,
10984717628eSXin LI                             z_const unsigned char FAR * FAR *);
10994717628eSXin LI typedef int (*out_func)(void FAR *, unsigned char FAR *, unsigned);
1100c9083b85SXin LI 
11014717628eSXin LI ZEXTERN int ZEXPORT inflateBack(z_streamp strm,
1102c9083b85SXin LI                                 in_func in, void FAR *in_desc,
11034717628eSXin LI                                 out_func out, void FAR *out_desc);
1104c9083b85SXin LI /*
1105c9083b85SXin LI      inflateBack() does a raw inflate with a single call using a call-back
1106c9083b85SXin LI    interface for input and output.  This is potentially more efficient than
1107c9083b85SXin LI    inflate() for file i/o applications, in that it avoids copying between the
1108c9083b85SXin LI    output and the sliding window by simply making the window itself the output
1109c9083b85SXin LI    buffer.  inflate() can be faster on modern CPUs when used with large
1110c9083b85SXin LI    buffers.  inflateBack() trusts the application to not change the output
1111c9083b85SXin LI    buffer passed by the output function, at least until inflateBack() returns.
1112c9083b85SXin LI 
1113c9083b85SXin LI      inflateBackInit() must be called first to allocate the internal state
1114c9083b85SXin LI    and to initialize the state with the user-provided window buffer.
1115c9083b85SXin LI    inflateBack() may then be used multiple times to inflate a complete, raw
1116c9083b85SXin LI    deflate stream with each call.  inflateBackEnd() is then called to free the
1117c9083b85SXin LI    allocated state.
1118c9083b85SXin LI 
1119c9083b85SXin LI      A raw deflate stream is one with no zlib or gzip header or trailer.
1120c9083b85SXin LI    This routine would normally be used in a utility that reads zip or gzip
1121c9083b85SXin LI    files and writes out uncompressed files.  The utility would decode the
1122c9083b85SXin LI    header and process the trailer on its own, hence this routine expects only
1123c9083b85SXin LI    the raw deflate stream to decompress.  This is different from the default
1124c9083b85SXin LI    behavior of inflate(), which expects a zlib header and trailer around the
1125c9083b85SXin LI    deflate stream.
1126c9083b85SXin LI 
1127c9083b85SXin LI      inflateBack() uses two subroutines supplied by the caller that are then
1128c9083b85SXin LI    called by inflateBack() for input and output.  inflateBack() calls those
1129c9083b85SXin LI    routines until it reads a complete deflate stream and writes out all of the
1130c9083b85SXin LI    uncompressed data, or until it encounters an error.  The function's
1131c9083b85SXin LI    parameters and return types are defined above in the in_func and out_func
1132c9083b85SXin LI    typedefs.  inflateBack() will call in(in_desc, &buf) which should return the
1133c9083b85SXin LI    number of bytes of provided input, and a pointer to that input in buf.  If
1134c9083b85SXin LI    there is no input available, in() must return zero -- buf is ignored in that
1135c9083b85SXin LI    case -- and inflateBack() will return a buffer error.  inflateBack() will
1136c9083b85SXin LI    call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].
1137c9083b85SXin LI    out() should return zero on success, or non-zero on failure.  If out()
1138c9083b85SXin LI    returns non-zero, inflateBack() will return with an error.  Neither in() nor
1139c9083b85SXin LI    out() are permitted to change the contents of the window provided to
1140c9083b85SXin LI    inflateBackInit(), which is also the buffer that out() uses to write from.
1141c9083b85SXin LI    The length written by out() will be at most the window size.  Any non-zero
1142c9083b85SXin LI    amount of input may be provided by in().
1143c9083b85SXin LI 
1144c9083b85SXin LI      For convenience, inflateBack() can be provided input on the first call by
1145c9083b85SXin LI    setting strm->next_in and strm->avail_in.  If that input is exhausted, then
1146c9083b85SXin LI    in() will be called.  Therefore strm->next_in must be initialized before
1147c9083b85SXin LI    calling inflateBack().  If strm->next_in is Z_NULL, then in() will be called
1148c9083b85SXin LI    immediately for input.  If strm->next_in is not Z_NULL, then strm->avail_in
1149c9083b85SXin LI    must also be initialized, and then if strm->avail_in is not zero, input will
1150c9083b85SXin LI    initially be taken from strm->next_in[0 ..  strm->avail_in - 1].
1151c9083b85SXin LI 
1152c9083b85SXin LI      The in_desc and out_desc parameters of inflateBack() is passed as the
1153c9083b85SXin LI    first parameter of in() and out() respectively when they are called.  These
1154c9083b85SXin LI    descriptors can be optionally used to pass any information that the caller-
1155c9083b85SXin LI    supplied in() and out() functions need to do their job.
1156c9083b85SXin LI 
1157c9083b85SXin LI      On return, inflateBack() will set strm->next_in and strm->avail_in to
1158c9083b85SXin LI    pass back any unused input that was provided by the last in() call.  The
1159c9083b85SXin LI    return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
1160c9083b85SXin LI    if in() or out() returned an error, Z_DATA_ERROR if there was a format error
1161c9083b85SXin LI    in the deflate stream (in which case strm->msg is set to indicate the nature
1162c9083b85SXin LI    of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
1163c9083b85SXin LI    In the case of Z_BUF_ERROR, an input or output error can be distinguished
1164c9083b85SXin LI    using strm->next_in which will be Z_NULL only if in() returned an error.  If
1165c9083b85SXin LI    strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
1166c9083b85SXin LI    non-zero.  (in() will always be called before out(), so strm->next_in is
1167c9083b85SXin LI    assured to be defined if out() returns non-zero.)  Note that inflateBack()
1168c9083b85SXin LI    cannot return Z_OK.
1169c9083b85SXin LI */
1170c9083b85SXin LI 
11714717628eSXin LI ZEXTERN int ZEXPORT inflateBackEnd(z_streamp strm);
1172c9083b85SXin LI /*
1173c9083b85SXin LI      All memory allocated by inflateBackInit() is freed.
1174c9083b85SXin LI 
1175c9083b85SXin LI      inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
1176c9083b85SXin LI    state was inconsistent.
1177c9083b85SXin LI */
1178c9083b85SXin LI 
11794717628eSXin LI ZEXTERN uLong ZEXPORT zlibCompileFlags(void);
1180c9083b85SXin LI /* Return flags indicating compile-time options.
1181c9083b85SXin LI 
1182c9083b85SXin LI     Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
1183c9083b85SXin LI      1.0: size of uInt
1184c9083b85SXin LI      3.2: size of uLong
1185c9083b85SXin LI      5.4: size of voidpf (pointer)
1186c9083b85SXin LI      7.6: size of z_off_t
1187c9083b85SXin LI 
1188c9083b85SXin LI     Compiler, assembler, and debug options:
1189c9083b85SXin LI      8: ZLIB_DEBUG
1190c9083b85SXin LI      9: ASMV or ASMINF -- use ASM code
1191c9083b85SXin LI      10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
1192c9083b85SXin LI      11: 0 (reserved)
1193c9083b85SXin LI 
1194c9083b85SXin LI     One-time table building (smaller code, but not thread-safe if true):
1195c9083b85SXin LI      12: BUILDFIXED -- build static block decoding tables when needed
1196c9083b85SXin LI      13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
1197c9083b85SXin LI      14,15: 0 (reserved)
1198c9083b85SXin LI 
1199c9083b85SXin LI     Library content (indicates missing functionality):
1200c9083b85SXin LI      16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
1201c9083b85SXin LI                           deflate code when not needed)
1202c9083b85SXin LI      17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
1203c9083b85SXin LI                     and decode gzip streams (to avoid linking crc code)
1204c9083b85SXin LI      18-19: 0 (reserved)
1205c9083b85SXin LI 
1206c9083b85SXin LI     Operation variations (changes in library functionality):
1207c9083b85SXin LI      20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
1208c9083b85SXin LI      21: FASTEST -- deflate algorithm with only one, lowest compression level
1209c9083b85SXin LI      22,23: 0 (reserved)
1210c9083b85SXin LI 
1211c9083b85SXin LI     The sprintf variant used by gzprintf (zero is best):
1212c9083b85SXin LI      24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
1213c9083b85SXin LI      25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
1214c9083b85SXin LI      26: 0 = returns value, 1 = void -- 1 means inferred string length returned
1215c9083b85SXin LI 
1216c9083b85SXin LI     Remainder:
1217c9083b85SXin LI      27-31: 0 (reserved)
1218c9083b85SXin LI  */
1219c9083b85SXin LI 
1220a15cb219SXin LI #if !defined(Z_SOLO) || defined(_KERNEL)
1221c9083b85SXin LI 
1222c9083b85SXin LI                         /* utility functions */
1223c9083b85SXin LI 
1224c9083b85SXin LI /*
1225c9083b85SXin LI      The following utility functions are implemented on top of the basic
1226c9083b85SXin LI    stream-oriented functions.  To simplify the interface, some default options
1227c9083b85SXin LI    are assumed (compression level and memory usage, standard memory allocation
1228c9083b85SXin LI    functions).  The source code of these utility functions can be modified if
1229c9083b85SXin LI    you need special options.
1230c9083b85SXin LI */
1231c9083b85SXin LI 
12324717628eSXin LI ZEXTERN int ZEXPORT compress(Bytef *dest,   uLongf *destLen,
12334717628eSXin LI                              const Bytef *source, uLong sourceLen);
1234c9083b85SXin LI /*
1235c9083b85SXin LI      Compresses the source buffer into the destination buffer.  sourceLen is
1236c9083b85SXin LI    the byte length of the source buffer.  Upon entry, destLen is the total size
1237c9083b85SXin LI    of the destination buffer, which must be at least the value returned by
1238c9083b85SXin LI    compressBound(sourceLen).  Upon exit, destLen is the actual size of the
1239c9083b85SXin LI    compressed data.  compress() is equivalent to compress2() with a level
1240c9083b85SXin LI    parameter of Z_DEFAULT_COMPRESSION.
1241c9083b85SXin LI 
1242c9083b85SXin LI      compress returns Z_OK if success, Z_MEM_ERROR if there was not
1243c9083b85SXin LI    enough memory, Z_BUF_ERROR if there was not enough room in the output
1244c9083b85SXin LI    buffer.
1245c9083b85SXin LI */
1246c9083b85SXin LI 
12474717628eSXin LI ZEXTERN int ZEXPORT compress2(Bytef *dest,   uLongf *destLen,
1248c9083b85SXin LI                               const Bytef *source, uLong sourceLen,
12494717628eSXin LI                               int level);
1250c9083b85SXin LI /*
1251c9083b85SXin LI      Compresses the source buffer into the destination buffer.  The level
1252c9083b85SXin LI    parameter has the same meaning as in deflateInit.  sourceLen is the byte
1253c9083b85SXin LI    length of the source buffer.  Upon entry, destLen is the total size of the
1254c9083b85SXin LI    destination buffer, which must be at least the value returned by
1255c9083b85SXin LI    compressBound(sourceLen).  Upon exit, destLen is the actual size of the
1256c9083b85SXin LI    compressed data.
1257c9083b85SXin LI 
1258c9083b85SXin LI      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1259c9083b85SXin LI    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
1260c9083b85SXin LI    Z_STREAM_ERROR if the level parameter is invalid.
1261c9083b85SXin LI */
1262c9083b85SXin LI 
12634717628eSXin LI ZEXTERN uLong ZEXPORT compressBound(uLong sourceLen);
1264c9083b85SXin LI /*
1265c9083b85SXin LI      compressBound() returns an upper bound on the compressed size after
1266c9083b85SXin LI    compress() or compress2() on sourceLen bytes.  It would be used before a
1267c9083b85SXin LI    compress() or compress2() call to allocate the destination buffer.
1268c9083b85SXin LI */
1269c9083b85SXin LI 
12704717628eSXin LI ZEXTERN int ZEXPORT uncompress(Bytef *dest,   uLongf *destLen,
12714717628eSXin LI                                const Bytef *source, uLong sourceLen);
1272c9083b85SXin LI /*
1273c9083b85SXin LI      Decompresses the source buffer into the destination buffer.  sourceLen is
1274c9083b85SXin LI    the byte length of the source buffer.  Upon entry, destLen is the total size
1275c9083b85SXin LI    of the destination buffer, which must be large enough to hold the entire
1276c9083b85SXin LI    uncompressed data.  (The size of the uncompressed data must have been saved
1277c9083b85SXin LI    previously by the compressor and transmitted to the decompressor by some
1278c9083b85SXin LI    mechanism outside the scope of this compression library.) Upon exit, destLen
1279c9083b85SXin LI    is the actual size of the uncompressed data.
1280c9083b85SXin LI 
1281c9083b85SXin LI      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
1282c9083b85SXin LI    enough memory, Z_BUF_ERROR if there was not enough room in the output
1283c9083b85SXin LI    buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.  In
1284c9083b85SXin LI    the case where there is not enough room, uncompress() will fill the output
1285c9083b85SXin LI    buffer with the uncompressed data up to that point.
1286c9083b85SXin LI */
1287c9083b85SXin LI 
12884717628eSXin LI ZEXTERN int ZEXPORT uncompress2(Bytef *dest,   uLongf *destLen,
12894717628eSXin LI                                 const Bytef *source, uLong *sourceLen);
1290c9083b85SXin LI /*
1291c9083b85SXin LI      Same as uncompress, except that sourceLen is a pointer, where the
1292c9083b85SXin LI    length of the source is *sourceLen.  On return, *sourceLen is the number of
1293c9083b85SXin LI    source bytes consumed.
1294c9083b85SXin LI */
1295a15cb219SXin LI #endif /* !Z_SOLO || _KERNEL */
1296a15cb219SXin LI 
1297a15cb219SXin LI #ifndef Z_SOLO
1298c9083b85SXin LI 
1299c9083b85SXin LI                         /* gzip file access functions */
1300c9083b85SXin LI 
1301c9083b85SXin LI /*
1302c9083b85SXin LI      This library supports reading and writing files in gzip (.gz) format with
1303c9083b85SXin LI    an interface similar to that of stdio, using the functions that start with
1304c9083b85SXin LI    "gz".  The gzip format is different from the zlib format.  gzip is a gzip
1305c9083b85SXin LI    wrapper, documented in RFC 1952, wrapped around a deflate stream.
1306c9083b85SXin LI */
1307c9083b85SXin LI 
1308c9083b85SXin LI typedef struct gzFile_s *gzFile;    /* semi-opaque gzip file descriptor */
1309c9083b85SXin LI 
1310c9083b85SXin LI /*
13114717628eSXin LI ZEXTERN gzFile ZEXPORT gzopen(const char *path, const char *mode);
1312c9083b85SXin LI 
1313cd882207SXin LI      Open the gzip (.gz) file at path for reading and decompressing, or
1314cd882207SXin LI    compressing and writing.  The mode parameter is as in fopen ("rb" or "wb")
1315cd882207SXin LI    but can also include a compression level ("wb9") or a strategy: 'f' for
1316cd882207SXin LI    filtered data as in "wb6f", 'h' for Huffman-only compression as in "wb1h",
1317cd882207SXin LI    'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression
1318cd882207SXin LI    as in "wb9F".  (See the description of deflateInit2 for more information
1319cd882207SXin LI    about the strategy parameter.)  'T' will request transparent writing or
1320cd882207SXin LI    appending with no compression and not using the gzip format.
1321c9083b85SXin LI 
1322c9083b85SXin LI      "a" can be used instead of "w" to request that the gzip stream that will
1323c9083b85SXin LI    be written be appended to the file.  "+" will result in an error, since
1324c9083b85SXin LI    reading and writing to the same gzip file is not supported.  The addition of
1325c9083b85SXin LI    "x" when writing will create the file exclusively, which fails if the file
1326c9083b85SXin LI    already exists.  On systems that support it, the addition of "e" when
1327c9083b85SXin LI    reading or writing will set the flag to close the file on an execve() call.
1328c9083b85SXin LI 
1329c9083b85SXin LI      These functions, as well as gzip, will read and decode a sequence of gzip
1330c9083b85SXin LI    streams in a file.  The append function of gzopen() can be used to create
1331c9083b85SXin LI    such a file.  (Also see gzflush() for another way to do this.)  When
1332c9083b85SXin LI    appending, gzopen does not test whether the file begins with a gzip stream,
1333c9083b85SXin LI    nor does it look for the end of the gzip streams to begin appending.  gzopen
1334c9083b85SXin LI    will simply append a gzip stream to the existing file.
1335c9083b85SXin LI 
1336c9083b85SXin LI      gzopen can be used to read a file which is not in gzip format; in this
1337c9083b85SXin LI    case gzread will directly read from the file without decompression.  When
1338c9083b85SXin LI    reading, this will be detected automatically by looking for the magic two-
1339c9083b85SXin LI    byte gzip header.
1340c9083b85SXin LI 
1341c9083b85SXin LI      gzopen returns NULL if the file could not be opened, if there was
1342c9083b85SXin LI    insufficient memory to allocate the gzFile state, or if an invalid mode was
1343c9083b85SXin LI    specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
1344c9083b85SXin LI    errno can be checked to determine if the reason gzopen failed was that the
1345c9083b85SXin LI    file could not be opened.
1346c9083b85SXin LI */
1347c9083b85SXin LI 
13484717628eSXin LI ZEXTERN gzFile ZEXPORT gzdopen(int fd, const char *mode);
1349c9083b85SXin LI /*
1350cd882207SXin LI      Associate a gzFile with the file descriptor fd.  File descriptors are
1351cd882207SXin LI    obtained from calls like open, dup, creat, pipe or fileno (if the file has
1352cd882207SXin LI    been previously opened with fopen).  The mode parameter is as in gzopen.
1353c9083b85SXin LI 
1354c9083b85SXin LI      The next call of gzclose on the returned gzFile will also close the file
1355c9083b85SXin LI    descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
1356c9083b85SXin LI    fd.  If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
1357c9083b85SXin LI    mode);.  The duplicated descriptor should be saved to avoid a leak, since
1358c9083b85SXin LI    gzdopen does not close fd if it fails.  If you are using fileno() to get the
1359c9083b85SXin LI    file descriptor from a FILE *, then you will have to use dup() to avoid
1360c9083b85SXin LI    double-close()ing the file descriptor.  Both gzclose() and fclose() will
1361c9083b85SXin LI    close the associated file descriptor, so they need to have different file
1362c9083b85SXin LI    descriptors.
1363c9083b85SXin LI 
1364c9083b85SXin LI      gzdopen returns NULL if there was insufficient memory to allocate the
1365c9083b85SXin LI    gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
1366c9083b85SXin LI    provided, or '+' was provided), or if fd is -1.  The file descriptor is not
1367c9083b85SXin LI    used until the next gz* read, write, seek, or close operation, so gzdopen
1368c9083b85SXin LI    will not detect if fd is invalid (unless fd is -1).
1369c9083b85SXin LI */
1370c9083b85SXin LI 
13714717628eSXin LI ZEXTERN int ZEXPORT gzbuffer(gzFile file, unsigned size);
1372c9083b85SXin LI /*
1373cd882207SXin LI      Set the internal buffer size used by this library's functions for file to
1374cd882207SXin LI    size.  The default buffer size is 8192 bytes.  This function must be called
1375cd882207SXin LI    after gzopen() or gzdopen(), and before any other calls that read or write
1376cd882207SXin LI    the file.  The buffer memory allocation is always deferred to the first read
1377cd882207SXin LI    or write.  Three times that size in buffer space is allocated.  A larger
1378cd882207SXin LI    buffer size of, for example, 64K or 128K bytes will noticeably increase the
1379cd882207SXin LI    speed of decompression (reading).
1380c9083b85SXin LI 
1381c9083b85SXin LI      The new buffer size also affects the maximum length for gzprintf().
1382c9083b85SXin LI 
1383c9083b85SXin LI      gzbuffer() returns 0 on success, or -1 on failure, such as being called
1384c9083b85SXin LI    too late.
1385c9083b85SXin LI */
1386c9083b85SXin LI 
13874717628eSXin LI ZEXTERN int ZEXPORT gzsetparams(gzFile file, int level, int strategy);
1388c9083b85SXin LI /*
1389cd882207SXin LI      Dynamically update the compression level and strategy for file.  See the
1390cd882207SXin LI    description of deflateInit2 for the meaning of these parameters. Previously
1391cd882207SXin LI    provided data is flushed before applying the parameter changes.
1392c9083b85SXin LI 
1393c9083b85SXin LI      gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not
1394c9083b85SXin LI    opened for writing, Z_ERRNO if there is an error writing the flushed data,
1395c9083b85SXin LI    or Z_MEM_ERROR if there is a memory allocation error.
1396c9083b85SXin LI */
1397c9083b85SXin LI 
13984717628eSXin LI ZEXTERN int ZEXPORT gzread(gzFile file, voidp buf, unsigned len);
1399c9083b85SXin LI /*
1400cd882207SXin LI      Read and decompress up to len uncompressed bytes from file into buf.  If
1401c9083b85SXin LI    the input file is not in gzip format, gzread copies the given number of
1402c9083b85SXin LI    bytes into the buffer directly from the file.
1403c9083b85SXin LI 
1404c9083b85SXin LI      After reaching the end of a gzip stream in the input, gzread will continue
1405c9083b85SXin LI    to read, looking for another gzip stream.  Any number of gzip streams may be
1406c9083b85SXin LI    concatenated in the input file, and will all be decompressed by gzread().
1407c9083b85SXin LI    If something other than a gzip stream is encountered after a gzip stream,
1408c9083b85SXin LI    that remaining trailing garbage is ignored (and no error is returned).
1409c9083b85SXin LI 
1410c9083b85SXin LI      gzread can be used to read a gzip file that is being concurrently written.
1411c9083b85SXin LI    Upon reaching the end of the input, gzread will return with the available
1412c9083b85SXin LI    data.  If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then
1413c9083b85SXin LI    gzclearerr can be used to clear the end of file indicator in order to permit
1414c9083b85SXin LI    gzread to be tried again.  Z_OK indicates that a gzip stream was completed
1415c9083b85SXin LI    on the last gzread.  Z_BUF_ERROR indicates that the input file ended in the
1416c9083b85SXin LI    middle of a gzip stream.  Note that gzread does not return -1 in the event
1417c9083b85SXin LI    of an incomplete gzip stream.  This error is deferred until gzclose(), which
1418c9083b85SXin LI    will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip
1419c9083b85SXin LI    stream.  Alternatively, gzerror can be used before gzclose to detect this
1420c9083b85SXin LI    case.
1421c9083b85SXin LI 
1422c9083b85SXin LI      gzread returns the number of uncompressed bytes actually read, less than
1423c9083b85SXin LI    len for end of file, or -1 for error.  If len is too large to fit in an int,
1424c9083b85SXin LI    then nothing is read, -1 is returned, and the error state is set to
1425c9083b85SXin LI    Z_STREAM_ERROR.
1426c9083b85SXin LI */
1427c9083b85SXin LI 
14284717628eSXin LI ZEXTERN z_size_t ZEXPORT gzfread(voidp buf, z_size_t size, z_size_t nitems,
14294717628eSXin LI                                  gzFile file);
1430c9083b85SXin LI /*
1431cd882207SXin LI      Read and decompress up to nitems items of size size from file into buf,
1432cd882207SXin LI    otherwise operating as gzread() does.  This duplicates the interface of
1433cd882207SXin LI    stdio's fread(), with size_t request and return types.  If the library
1434cd882207SXin LI    defines size_t, then z_size_t is identical to size_t.  If not, then z_size_t
1435cd882207SXin LI    is an unsigned integer type that can contain a pointer.
1436c9083b85SXin LI 
1437c9083b85SXin LI      gzfread() returns the number of full items read of size size, or zero if
1438c9083b85SXin LI    the end of the file was reached and a full item could not be read, or if
1439c9083b85SXin LI    there was an error.  gzerror() must be consulted if zero is returned in
1440c9083b85SXin LI    order to determine if there was an error.  If the multiplication of size and
1441c9083b85SXin LI    nitems overflows, i.e. the product does not fit in a z_size_t, then nothing
1442c9083b85SXin LI    is read, zero is returned, and the error state is set to Z_STREAM_ERROR.
1443c9083b85SXin LI 
1444c9083b85SXin LI      In the event that the end of file is reached and only a partial item is
1445c9083b85SXin LI    available at the end, i.e. the remaining uncompressed data length is not a
1446e37bb444SXin LI    multiple of size, then the final partial item is nevertheless read into buf
1447c9083b85SXin LI    and the end-of-file flag is set.  The length of the partial item read is not
1448c9083b85SXin LI    provided, but could be inferred from the result of gztell().  This behavior
1449c9083b85SXin LI    is the same as the behavior of fread() implementations in common libraries,
1450c9083b85SXin LI    but it prevents the direct use of gzfread() to read a concurrently written
1451e37bb444SXin LI    file, resetting and retrying on end-of-file, when size is not 1.
1452c9083b85SXin LI */
1453c9083b85SXin LI 
14544717628eSXin LI ZEXTERN int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len);
1455c9083b85SXin LI /*
1456cd882207SXin LI      Compress and write the len uncompressed bytes at buf to file. gzwrite
1457cd882207SXin LI    returns the number of uncompressed bytes written or 0 in case of error.
1458c9083b85SXin LI */
1459c9083b85SXin LI 
14604717628eSXin LI ZEXTERN z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size,
14614717628eSXin LI                                   z_size_t nitems, gzFile file);
1462c9083b85SXin LI /*
1463cd882207SXin LI      Compress and write nitems items of size size from buf to file, duplicating
1464c9083b85SXin LI    the interface of stdio's fwrite(), with size_t request and return types.  If
1465c9083b85SXin LI    the library defines size_t, then z_size_t is identical to size_t.  If not,
1466c9083b85SXin LI    then z_size_t is an unsigned integer type that can contain a pointer.
1467c9083b85SXin LI 
1468c9083b85SXin LI      gzfwrite() returns the number of full items written of size size, or zero
1469c9083b85SXin LI    if there was an error.  If the multiplication of size and nitems overflows,
1470c9083b85SXin LI    i.e. the product does not fit in a z_size_t, then nothing is written, zero
1471c9083b85SXin LI    is returned, and the error state is set to Z_STREAM_ERROR.
1472c9083b85SXin LI */
1473c9083b85SXin LI 
14744717628eSXin LI ZEXTERN int ZEXPORTVA gzprintf(gzFile file, const char *format, ...);
1475c9083b85SXin LI /*
1476cd882207SXin LI      Convert, format, compress, and write the arguments (...) to file under
1477cd882207SXin LI    control of the string format, as in fprintf.  gzprintf returns the number of
1478c9083b85SXin LI    uncompressed bytes actually written, or a negative zlib error code in case
1479c9083b85SXin LI    of error.  The number of uncompressed bytes written is limited to 8191, or
1480c9083b85SXin LI    one less than the buffer size given to gzbuffer().  The caller should assure
1481c9083b85SXin LI    that this limit is not exceeded.  If it is exceeded, then gzprintf() will
1482c9083b85SXin LI    return an error (0) with nothing written.  In this case, there may also be a
1483c9083b85SXin LI    buffer overflow with unpredictable consequences, which is possible only if
1484cd882207SXin LI    zlib was compiled with the insecure functions sprintf() or vsprintf(),
1485c9083b85SXin LI    because the secure snprintf() or vsnprintf() functions were not available.
1486c9083b85SXin LI    This can be determined using zlibCompileFlags().
1487c9083b85SXin LI */
1488c9083b85SXin LI 
14894717628eSXin LI ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s);
1490c9083b85SXin LI /*
1491cd882207SXin LI      Compress and write the given null-terminated string s to file, excluding
1492c9083b85SXin LI    the terminating null character.
1493c9083b85SXin LI 
1494c9083b85SXin LI      gzputs returns the number of characters written, or -1 in case of error.
1495c9083b85SXin LI */
1496c9083b85SXin LI 
14974717628eSXin LI ZEXTERN char * ZEXPORT gzgets(gzFile file, char *buf, int len);
1498c9083b85SXin LI /*
1499cd882207SXin LI      Read and decompress bytes from file into buf, until len-1 characters are
1500cd882207SXin LI    read, or until a newline character is read and transferred to buf, or an
1501cd882207SXin LI    end-of-file condition is encountered.  If any characters are read or if len
1502cd882207SXin LI    is one, the string is terminated with a null character.  If no characters
1503cd882207SXin LI    are read due to an end-of-file or len is less than one, then the buffer is
1504cd882207SXin LI    left untouched.
1505c9083b85SXin LI 
1506c9083b85SXin LI      gzgets returns buf which is a null-terminated string, or it returns NULL
1507c9083b85SXin LI    for end-of-file or in case of error.  If there was an error, the contents at
1508c9083b85SXin LI    buf are indeterminate.
1509c9083b85SXin LI */
1510c9083b85SXin LI 
15114717628eSXin LI ZEXTERN int ZEXPORT gzputc(gzFile file, int c);
1512c9083b85SXin LI /*
1513cd882207SXin LI      Compress and write c, converted to an unsigned char, into file.  gzputc
1514c9083b85SXin LI    returns the value that was written, or -1 in case of error.
1515c9083b85SXin LI */
1516c9083b85SXin LI 
15174717628eSXin LI ZEXTERN int ZEXPORT gzgetc(gzFile file);
1518c9083b85SXin LI /*
1519cd882207SXin LI      Read and decompress one byte from file.  gzgetc returns this byte or -1
1520c9083b85SXin LI    in case of end of file or error.  This is implemented as a macro for speed.
1521c9083b85SXin LI    As such, it does not do all of the checking the other functions do.  I.e.
1522c9083b85SXin LI    it does not check to see if file is NULL, nor whether the structure file
1523c9083b85SXin LI    points to has been clobbered or not.
1524c9083b85SXin LI */
1525c9083b85SXin LI 
15264717628eSXin LI ZEXTERN int ZEXPORT gzungetc(int c, gzFile file);
1527c9083b85SXin LI /*
1528cd882207SXin LI      Push c back onto the stream for file to be read as the first character on
1529cd882207SXin LI    the next read.  At least one character of push-back is always allowed.
1530c9083b85SXin LI    gzungetc() returns the character pushed, or -1 on failure.  gzungetc() will
1531c9083b85SXin LI    fail if c is -1, and may fail if a character has been pushed but not read
1532c9083b85SXin LI    yet.  If gzungetc is used immediately after gzopen or gzdopen, at least the
1533c9083b85SXin LI    output buffer size of pushed characters is allowed.  (See gzbuffer above.)
1534c9083b85SXin LI    The pushed character will be discarded if the stream is repositioned with
1535c9083b85SXin LI    gzseek() or gzrewind().
1536c9083b85SXin LI */
1537c9083b85SXin LI 
15384717628eSXin LI ZEXTERN int ZEXPORT gzflush(gzFile file, int flush);
1539c9083b85SXin LI /*
1540cd882207SXin LI      Flush all pending output to file.  The parameter flush is as in the
1541cd882207SXin LI    deflate() function.  The return value is the zlib error number (see function
1542cd882207SXin LI    gzerror below).  gzflush is only permitted when writing.
1543c9083b85SXin LI 
1544c9083b85SXin LI      If the flush parameter is Z_FINISH, the remaining data is written and the
1545c9083b85SXin LI    gzip stream is completed in the output.  If gzwrite() is called again, a new
1546c9083b85SXin LI    gzip stream will be started in the output.  gzread() is able to read such
1547c9083b85SXin LI    concatenated gzip streams.
1548c9083b85SXin LI 
1549c9083b85SXin LI      gzflush should be called only when strictly necessary because it will
1550c9083b85SXin LI    degrade compression if called too often.
1551c9083b85SXin LI */
1552c9083b85SXin LI 
1553c9083b85SXin LI /*
15544717628eSXin LI ZEXTERN z_off_t ZEXPORT gzseek(gzFile file,
15554717628eSXin LI                                z_off_t offset, int whence);
1556c9083b85SXin LI 
1557cd882207SXin LI      Set the starting position to offset relative to whence for the next gzread
1558cd882207SXin LI    or gzwrite on file.  The offset represents a number of bytes in the
1559c9083b85SXin LI    uncompressed data stream.  The whence parameter is defined as in lseek(2);
1560c9083b85SXin LI    the value SEEK_END is not supported.
1561c9083b85SXin LI 
1562c9083b85SXin LI      If the file is opened for reading, this function is emulated but can be
1563c9083b85SXin LI    extremely slow.  If the file is opened for writing, only forward seeks are
1564c9083b85SXin LI    supported; gzseek then compresses a sequence of zeroes up to the new
1565c9083b85SXin LI    starting position.
1566c9083b85SXin LI 
1567c9083b85SXin LI      gzseek returns the resulting offset location as measured in bytes from
1568c9083b85SXin LI    the beginning of the uncompressed stream, or -1 in case of error, in
1569c9083b85SXin LI    particular if the file is opened for writing and the new starting position
1570c9083b85SXin LI    would be before the current position.
1571c9083b85SXin LI */
1572c9083b85SXin LI 
15734717628eSXin LI ZEXTERN int ZEXPORT    gzrewind(gzFile file);
1574c9083b85SXin LI /*
1575cd882207SXin LI      Rewind file. This function is supported only for reading.
1576c9083b85SXin LI 
1577cd882207SXin LI      gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET).
1578c9083b85SXin LI */
1579c9083b85SXin LI 
1580c9083b85SXin LI /*
15814717628eSXin LI ZEXTERN z_off_t ZEXPORT    gztell(gzFile file);
1582c9083b85SXin LI 
1583cd882207SXin LI      Return the starting position for the next gzread or gzwrite on file.
1584cd882207SXin LI    This position represents a number of bytes in the uncompressed data stream,
1585cd882207SXin LI    and is zero when starting, even if appending or reading a gzip stream from
1586cd882207SXin LI    the middle of a file using gzdopen().
1587c9083b85SXin LI 
1588c9083b85SXin LI      gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
1589c9083b85SXin LI */
1590c9083b85SXin LI 
1591c9083b85SXin LI /*
15924717628eSXin LI ZEXTERN z_off_t ZEXPORT gzoffset(gzFile file);
1593c9083b85SXin LI 
1594cd882207SXin LI      Return the current compressed (actual) read or write offset of file.  This
1595cd882207SXin LI    offset includes the count of bytes that precede the gzip stream, for example
1596cd882207SXin LI    when appending or when using gzdopen() for reading.  When reading, the
1597cd882207SXin LI    offset does not include as yet unused buffered input.  This information can
1598cd882207SXin LI    be used for a progress indicator.  On error, gzoffset() returns -1.
1599c9083b85SXin LI */
1600c9083b85SXin LI 
16014717628eSXin LI ZEXTERN int ZEXPORT gzeof(gzFile file);
1602c9083b85SXin LI /*
1603cd882207SXin LI      Return true (1) if the end-of-file indicator for file has been set while
1604cd882207SXin LI    reading, false (0) otherwise.  Note that the end-of-file indicator is set
1605cd882207SXin LI    only if the read tried to go past the end of the input, but came up short.
1606cd882207SXin LI    Therefore, just like feof(), gzeof() may return false even if there is no
1607cd882207SXin LI    more data to read, in the event that the last read request was for the exact
1608cd882207SXin LI    number of bytes remaining in the input file.  This will happen if the input
1609cd882207SXin LI    file size is an exact multiple of the buffer size.
1610c9083b85SXin LI 
1611c9083b85SXin LI      If gzeof() returns true, then the read functions will return no more data,
1612c9083b85SXin LI    unless the end-of-file indicator is reset by gzclearerr() and the input file
1613c9083b85SXin LI    has grown since the previous end of file was detected.
1614c9083b85SXin LI */
1615c9083b85SXin LI 
16164717628eSXin LI ZEXTERN int ZEXPORT gzdirect(gzFile file);
1617c9083b85SXin LI /*
1618cd882207SXin LI      Return true (1) if file is being copied directly while reading, or false
1619c9083b85SXin LI    (0) if file is a gzip stream being decompressed.
1620c9083b85SXin LI 
1621c9083b85SXin LI      If the input file is empty, gzdirect() will return true, since the input
1622c9083b85SXin LI    does not contain a gzip stream.
1623c9083b85SXin LI 
1624c9083b85SXin LI      If gzdirect() is used immediately after gzopen() or gzdopen() it will
1625c9083b85SXin LI    cause buffers to be allocated to allow reading the file to determine if it
1626c9083b85SXin LI    is a gzip file.  Therefore if gzbuffer() is used, it should be called before
1627c9083b85SXin LI    gzdirect().
1628c9083b85SXin LI 
1629c9083b85SXin LI      When writing, gzdirect() returns true (1) if transparent writing was
1630c9083b85SXin LI    requested ("wT" for the gzopen() mode), or false (0) otherwise.  (Note:
1631c9083b85SXin LI    gzdirect() is not needed when writing.  Transparent writing must be
1632c9083b85SXin LI    explicitly requested, so the application already knows the answer.  When
1633c9083b85SXin LI    linking statically, using gzdirect() will include all of the zlib code for
1634c9083b85SXin LI    gzip file reading and decompression, which may not be desired.)
1635c9083b85SXin LI */
1636c9083b85SXin LI 
16374717628eSXin LI ZEXTERN int ZEXPORT    gzclose(gzFile file);
1638c9083b85SXin LI /*
1639cd882207SXin LI      Flush all pending output for file, if necessary, close file and
1640cd882207SXin LI    deallocate the (de)compression state.  Note that once file is closed, you
1641c9083b85SXin LI    cannot call gzerror with file, since its structures have been deallocated.
1642c9083b85SXin LI    gzclose must not be called more than once on the same file, just as free
1643c9083b85SXin LI    must not be called more than once on the same allocation.
1644c9083b85SXin LI 
1645c9083b85SXin LI      gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
1646c9083b85SXin LI    file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the
1647c9083b85SXin LI    last read ended in the middle of a gzip stream, or Z_OK on success.
1648c9083b85SXin LI */
1649c9083b85SXin LI 
16504717628eSXin LI ZEXTERN int ZEXPORT gzclose_r(gzFile file);
16514717628eSXin LI ZEXTERN int ZEXPORT gzclose_w(gzFile file);
1652c9083b85SXin LI /*
1653c9083b85SXin LI      Same as gzclose(), but gzclose_r() is only for use when reading, and
1654c9083b85SXin LI    gzclose_w() is only for use when writing or appending.  The advantage to
1655c9083b85SXin LI    using these instead of gzclose() is that they avoid linking in zlib
1656c9083b85SXin LI    compression or decompression code that is not used when only reading or only
1657c9083b85SXin LI    writing respectively.  If gzclose() is used, then both compression and
1658c9083b85SXin LI    decompression code will be included the application when linking to a static
1659c9083b85SXin LI    zlib library.
1660c9083b85SXin LI */
1661c9083b85SXin LI 
16624717628eSXin LI ZEXTERN const char * ZEXPORT gzerror(gzFile file, int *errnum);
1663c9083b85SXin LI /*
1664cd882207SXin LI      Return the error message for the last error which occurred on file.
1665cd882207SXin LI    errnum is set to zlib error number.  If an error occurred in the file system
1666cd882207SXin LI    and not in the compression library, errnum is set to Z_ERRNO and the
1667cd882207SXin LI    application may consult errno to get the exact error code.
1668c9083b85SXin LI 
1669c9083b85SXin LI      The application must not modify the returned string.  Future calls to
1670c9083b85SXin LI    this function may invalidate the previously returned string.  If file is
1671c9083b85SXin LI    closed, then the string previously returned by gzerror will no longer be
1672c9083b85SXin LI    available.
1673c9083b85SXin LI 
1674c9083b85SXin LI      gzerror() should be used to distinguish errors from end-of-file for those
1675c9083b85SXin LI    functions above that do not distinguish those cases in their return values.
1676c9083b85SXin LI */
1677c9083b85SXin LI 
16784717628eSXin LI ZEXTERN void ZEXPORT gzclearerr(gzFile file);
1679c9083b85SXin LI /*
1680cd882207SXin LI      Clear the error and end-of-file flags for file.  This is analogous to the
1681c9083b85SXin LI    clearerr() function in stdio.  This is useful for continuing to read a gzip
1682c9083b85SXin LI    file that is being written concurrently.
1683c9083b85SXin LI */
1684c9083b85SXin LI 
1685c9083b85SXin LI #endif /* !Z_SOLO */
1686c9083b85SXin LI 
1687c9083b85SXin LI                         /* checksum functions */
1688c9083b85SXin LI 
1689c9083b85SXin LI /*
1690c9083b85SXin LI      These functions are not related to compression but are exported
1691c9083b85SXin LI    anyway because they might be useful in applications using the compression
1692c9083b85SXin LI    library.
1693c9083b85SXin LI */
1694c9083b85SXin LI 
16954717628eSXin LI ZEXTERN uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len);
1696c9083b85SXin LI /*
1697c9083b85SXin LI      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1698cd882207SXin LI    return the updated checksum. An Adler-32 value is in the range of a 32-bit
1699cd882207SXin LI    unsigned integer. If buf is Z_NULL, this function returns the required
1700cd882207SXin LI    initial value for the checksum.
1701c9083b85SXin LI 
1702c9083b85SXin LI      An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
1703c9083b85SXin LI    much faster.
1704c9083b85SXin LI 
1705c9083b85SXin LI    Usage example:
1706c9083b85SXin LI 
1707c9083b85SXin LI      uLong adler = adler32(0L, Z_NULL, 0);
1708c9083b85SXin LI 
1709c9083b85SXin LI      while (read_buffer(buffer, length) != EOF) {
1710c9083b85SXin LI        adler = adler32(adler, buffer, length);
1711c9083b85SXin LI      }
1712c9083b85SXin LI      if (adler != original_adler) error();
1713c9083b85SXin LI */
1714c9083b85SXin LI 
17154717628eSXin LI ZEXTERN uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf,
17164717628eSXin LI                                 z_size_t len);
1717c9083b85SXin LI /*
1718c9083b85SXin LI      Same as adler32(), but with a size_t length.
1719c9083b85SXin LI */
1720c9083b85SXin LI 
1721c9083b85SXin LI /*
17224717628eSXin LI ZEXTERN uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2,
17234717628eSXin LI                                       z_off_t len2);
1724c9083b85SXin LI 
1725c9083b85SXin LI      Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
1726c9083b85SXin LI    and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
1727c9083b85SXin LI    each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
1728c9083b85SXin LI    seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.  Note
1729c9083b85SXin LI    that the z_off_t type (like off_t) is a signed integer.  If len2 is
1730c9083b85SXin LI    negative, the result has no meaning or utility.
1731c9083b85SXin LI */
1732c9083b85SXin LI 
17334717628eSXin LI ZEXTERN uLong ZEXPORT crc32(uLong crc, const Bytef *buf, uInt len);
1734c9083b85SXin LI /*
1735c9083b85SXin LI      Update a running CRC-32 with the bytes buf[0..len-1] and return the
1736cd882207SXin LI    updated CRC-32. A CRC-32 value is in the range of a 32-bit unsigned integer.
1737cd882207SXin LI    If buf is Z_NULL, this function returns the required initial value for the
1738cd882207SXin LI    crc. Pre- and post-conditioning (one's complement) is performed within this
1739cd882207SXin LI    function so it shouldn't be done by the application.
1740c9083b85SXin LI 
1741c9083b85SXin LI    Usage example:
1742c9083b85SXin LI 
1743c9083b85SXin LI      uLong crc = crc32(0L, Z_NULL, 0);
1744c9083b85SXin LI 
1745c9083b85SXin LI      while (read_buffer(buffer, length) != EOF) {
1746c9083b85SXin LI        crc = crc32(crc, buffer, length);
1747c9083b85SXin LI      }
1748c9083b85SXin LI      if (crc != original_crc) error();
1749c9083b85SXin LI */
1750c9083b85SXin LI 
17514717628eSXin LI ZEXTERN uLong ZEXPORT crc32_z(uLong crc, const Bytef *buf,
17524717628eSXin LI                               z_size_t len);
1753c9083b85SXin LI /*
1754c9083b85SXin LI      Same as crc32(), but with a size_t length.
1755c9083b85SXin LI */
1756c9083b85SXin LI 
1757c9083b85SXin LI /*
17584717628eSXin LI ZEXTERN uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2);
1759c9083b85SXin LI 
1760c9083b85SXin LI      Combine two CRC-32 check values into one.  For two sequences of bytes,
1761c9083b85SXin LI    seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
1762c9083b85SXin LI    calculated for each, crc1 and crc2.  crc32_combine() returns the CRC-32
1763c9083b85SXin LI    check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
1764*6255c67cSXin LI    len2. len2 must be non-negative.
1765c9083b85SXin LI */
1766c9083b85SXin LI 
1767cd882207SXin LI /*
17684717628eSXin LI ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t len2);
1769cd882207SXin LI 
1770cd882207SXin LI      Return the operator corresponding to length len2, to be used with
1771*6255c67cSXin LI    crc32_combine_op(). len2 must be non-negative.
1772cd882207SXin LI */
1773cd882207SXin LI 
17744717628eSXin LI ZEXTERN uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op);
1775cd882207SXin LI /*
1776cd882207SXin LI      Give the same result as crc32_combine(), using op in place of len2. op is
1777cd882207SXin LI    is generated from len2 by crc32_combine_gen(). This will be faster than
1778cd882207SXin LI    crc32_combine() if the generated op is used more than once.
1779cd882207SXin LI */
1780cd882207SXin LI 
1781cd882207SXin LI 
1782cd882207SXin LI ZEXTERN uLong ZEXPORT crc32_combine_gen64 OF((z_off64_t));
1783c9083b85SXin LI 
1784c9083b85SXin LI                         /* various hacks, don't look :) */
1785c9083b85SXin LI 
1786c9083b85SXin LI /* deflateInit and inflateInit are macros to allow checking the zlib version
1787c9083b85SXin LI  * and the compiler's view of z_stream:
1788c9083b85SXin LI  */
17894717628eSXin LI ZEXTERN int ZEXPORT deflateInit_(z_streamp strm, int level,
17904717628eSXin LI                                  const char *version, int stream_size);
17914717628eSXin LI ZEXTERN int ZEXPORT inflateInit_(z_streamp strm,
17924717628eSXin LI                                  const char *version, int stream_size);
17934717628eSXin LI ZEXTERN int ZEXPORT deflateInit2_(z_streamp strm, int  level, int  method,
1794c9083b85SXin LI                                   int windowBits, int memLevel,
1795c9083b85SXin LI                                   int strategy, const char *version,
17964717628eSXin LI                                   int stream_size);
17974717628eSXin LI ZEXTERN int ZEXPORT inflateInit2_(z_streamp strm, int  windowBits,
17984717628eSXin LI                                   const char *version, int stream_size);
17994717628eSXin LI ZEXTERN int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits,
1800c9083b85SXin LI                                      unsigned char FAR *window,
1801c9083b85SXin LI                                      const char *version,
18024717628eSXin LI                                      int stream_size);
1803c9083b85SXin LI #ifdef Z_PREFIX_SET
1804c9083b85SXin LI #  define z_deflateInit(strm, level) \
1805c9083b85SXin LI           deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
1806c9083b85SXin LI #  define z_inflateInit(strm) \
1807c9083b85SXin LI           inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
1808c9083b85SXin LI #  define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1809c9083b85SXin LI           deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1810c9083b85SXin LI                         (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
1811c9083b85SXin LI #  define z_inflateInit2(strm, windowBits) \
1812c9083b85SXin LI           inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
1813c9083b85SXin LI                         (int)sizeof(z_stream))
1814c9083b85SXin LI #  define z_inflateBackInit(strm, windowBits, window) \
1815c9083b85SXin LI           inflateBackInit_((strm), (windowBits), (window), \
1816c9083b85SXin LI                            ZLIB_VERSION, (int)sizeof(z_stream))
1817c9083b85SXin LI #else
1818c9083b85SXin LI #  define deflateInit(strm, level) \
1819c9083b85SXin LI           deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
1820c9083b85SXin LI #  define inflateInit(strm) \
1821c9083b85SXin LI           inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
1822c9083b85SXin LI #  define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1823c9083b85SXin LI           deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1824c9083b85SXin LI                         (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
1825c9083b85SXin LI #  define inflateInit2(strm, windowBits) \
1826c9083b85SXin LI           inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
1827c9083b85SXin LI                         (int)sizeof(z_stream))
1828c9083b85SXin LI #  define inflateBackInit(strm, windowBits, window) \
1829c9083b85SXin LI           inflateBackInit_((strm), (windowBits), (window), \
1830c9083b85SXin LI                            ZLIB_VERSION, (int)sizeof(z_stream))
1831c9083b85SXin LI #endif
1832c9083b85SXin LI 
1833c9083b85SXin LI #ifndef Z_SOLO
1834c9083b85SXin LI 
1835c9083b85SXin LI /* gzgetc() macro and its supporting function and exposed data structure.  Note
1836c9083b85SXin LI  * that the real internal state is much larger than the exposed structure.
1837c9083b85SXin LI  * This abbreviated structure exposes just enough for the gzgetc() macro.  The
1838c9083b85SXin LI  * user should not mess with these exposed elements, since their names or
1839c9083b85SXin LI  * behavior could change in the future, perhaps even capriciously.  They can
1840c9083b85SXin LI  * only be used by the gzgetc() macro.  You have been warned.
1841c9083b85SXin LI  */
1842c9083b85SXin LI struct gzFile_s {
1843c9083b85SXin LI     unsigned have;
1844c9083b85SXin LI     unsigned char *next;
1845c9083b85SXin LI     z_off64_t pos;
1846c9083b85SXin LI };
18474717628eSXin LI ZEXTERN int ZEXPORT gzgetc_(gzFile file);       /* backward compatibility */
1848c9083b85SXin LI #ifdef Z_PREFIX_SET
1849c9083b85SXin LI #  undef z_gzgetc
1850c9083b85SXin LI #  define z_gzgetc(g) \
1851c9083b85SXin LI           ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
1852c9083b85SXin LI #else
1853c9083b85SXin LI #  define gzgetc(g) \
1854c9083b85SXin LI           ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g))
1855c9083b85SXin LI #endif
1856c9083b85SXin LI 
1857c9083b85SXin LI /* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
1858c9083b85SXin LI  * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
1859c9083b85SXin LI  * both are true, the application gets the *64 functions, and the regular
1860c9083b85SXin LI  * functions are changed to 64 bits) -- in case these are set on systems
1861c9083b85SXin LI  * without large file support, _LFS64_LARGEFILE must also be true
1862c9083b85SXin LI  */
1863c9083b85SXin LI #ifdef Z_LARGE64
18644717628eSXin LI    ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *);
18654717628eSXin LI    ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int);
18664717628eSXin LI    ZEXTERN z_off64_t ZEXPORT gztell64(gzFile);
18674717628eSXin LI    ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile);
18684717628eSXin LI    ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off64_t);
18694717628eSXin LI    ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off64_t);
18704717628eSXin LI    ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off64_t);
1871c9083b85SXin LI #endif
1872c9083b85SXin LI 
1873c9083b85SXin LI #if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
1874c9083b85SXin LI #  ifdef Z_PREFIX_SET
1875c9083b85SXin LI #    define z_gzopen z_gzopen64
1876c9083b85SXin LI #    define z_gzseek z_gzseek64
1877c9083b85SXin LI #    define z_gztell z_gztell64
1878c9083b85SXin LI #    define z_gzoffset z_gzoffset64
1879c9083b85SXin LI #    define z_adler32_combine z_adler32_combine64
1880c9083b85SXin LI #    define z_crc32_combine z_crc32_combine64
1881cd882207SXin LI #    define z_crc32_combine_gen z_crc32_combine_gen64
1882c9083b85SXin LI #  else
1883c9083b85SXin LI #    define gzopen gzopen64
1884c9083b85SXin LI #    define gzseek gzseek64
1885c9083b85SXin LI #    define gztell gztell64
1886c9083b85SXin LI #    define gzoffset gzoffset64
1887c9083b85SXin LI #    define adler32_combine adler32_combine64
1888c9083b85SXin LI #    define crc32_combine crc32_combine64
1889cd882207SXin LI #    define crc32_combine_gen crc32_combine_gen64
1890c9083b85SXin LI #  endif
1891c9083b85SXin LI #  ifndef Z_LARGE64
18924717628eSXin LI      ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *);
18934717628eSXin LI      ZEXTERN z_off_t ZEXPORT gzseek64(gzFile, z_off_t, int);
18944717628eSXin LI      ZEXTERN z_off_t ZEXPORT gztell64(gzFile);
18954717628eSXin LI      ZEXTERN z_off_t ZEXPORT gzoffset64(gzFile);
18964717628eSXin LI      ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t);
18974717628eSXin LI      ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t);
18984717628eSXin LI      ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t);
1899c9083b85SXin LI #  endif
1900c9083b85SXin LI #else
19014717628eSXin LI    ZEXTERN gzFile ZEXPORT gzopen(const char *, const char *);
19024717628eSXin LI    ZEXTERN z_off_t ZEXPORT gzseek(gzFile, z_off_t, int);
19034717628eSXin LI    ZEXTERN z_off_t ZEXPORT gztell(gzFile);
19044717628eSXin LI    ZEXTERN z_off_t ZEXPORT gzoffset(gzFile);
19054717628eSXin LI    ZEXTERN uLong ZEXPORT adler32_combine(uLong, uLong, z_off_t);
19064717628eSXin LI    ZEXTERN uLong ZEXPORT crc32_combine(uLong, uLong, z_off_t);
19074717628eSXin LI    ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t);
1908c9083b85SXin LI #endif
1909c9083b85SXin LI 
1910c9083b85SXin LI #else /* Z_SOLO */
1911c9083b85SXin LI 
19124717628eSXin LI    ZEXTERN uLong ZEXPORT adler32_combine(uLong, uLong, z_off_t);
19134717628eSXin LI    ZEXTERN uLong ZEXPORT crc32_combine(uLong, uLong, z_off_t);
19144717628eSXin LI    ZEXTERN uLong ZEXPORT crc32_combine_gen(z_off_t);
1915c9083b85SXin LI 
1916c9083b85SXin LI #endif /* !Z_SOLO */
1917c9083b85SXin LI 
1918c9083b85SXin LI /* undocumented functions */
19194717628eSXin LI ZEXTERN const char   * ZEXPORT zError(int);
19204717628eSXin LI ZEXTERN int            ZEXPORT inflateSyncPoint(z_streamp);
19214717628eSXin LI ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table(void);
19224717628eSXin LI ZEXTERN int            ZEXPORT inflateUndermine(z_streamp, int);
19234717628eSXin LI ZEXTERN int            ZEXPORT inflateValidate(z_streamp, int);
19244717628eSXin LI ZEXTERN unsigned long  ZEXPORT inflateCodesUsed(z_streamp);
19254717628eSXin LI ZEXTERN int            ZEXPORT inflateResetKeep(z_streamp);
19264717628eSXin LI ZEXTERN int            ZEXPORT deflateResetKeep(z_streamp);
1927cd882207SXin LI #if defined(_WIN32) && !defined(Z_SOLO)
19284717628eSXin LI ZEXTERN gzFile         ZEXPORT gzopen_w(const wchar_t *path,
19294717628eSXin LI                                         const char *mode);
1930c9083b85SXin LI #endif
1931c9083b85SXin LI #if defined(STDC) || defined(Z_HAVE_STDARG_H)
1932c9083b85SXin LI #  ifndef Z_SOLO
19334717628eSXin LI ZEXTERN int            ZEXPORTVA gzvprintf(gzFile file,
1934c9083b85SXin LI                                            const char *format,
19354717628eSXin LI                                            va_list va);
1936c9083b85SXin LI #  endif
1937c9083b85SXin LI #endif
1938c9083b85SXin LI 
1939c9083b85SXin LI #ifdef __cplusplus
1940c9083b85SXin LI }
1941c9083b85SXin LI #endif
1942c9083b85SXin LI 
1943c9083b85SXin LI #endif /* ZLIB_H */
1944