xref: /OK3568_Linux_fs/kernel/include/linux/zlib.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* zlib.h -- interface of the 'zlib' general purpose compression library
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun   This software is provided 'as-is', without any express or implied
6*4882a593Smuzhiyun   warranty.  In no event will the authors be held liable for any damages
7*4882a593Smuzhiyun   arising from the use of this software.
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun   Permission is granted to anyone to use this software for any purpose,
10*4882a593Smuzhiyun   including commercial applications, and to alter it and redistribute it
11*4882a593Smuzhiyun   freely, subject to the following restrictions:
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun   1. The origin of this software must not be misrepresented; you must not
14*4882a593Smuzhiyun      claim that you wrote the original software. If you use this software
15*4882a593Smuzhiyun      in a product, an acknowledgment in the product documentation would be
16*4882a593Smuzhiyun      appreciated but is not required.
17*4882a593Smuzhiyun   2. Altered source versions must be plainly marked as such, and must not be
18*4882a593Smuzhiyun      misrepresented as being the original software.
19*4882a593Smuzhiyun   3. This notice may not be removed or altered from any source distribution.
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun   Jean-loup Gailly        Mark Adler
22*4882a593Smuzhiyun   jloup@gzip.org          madler@alumni.caltech.edu
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun   The data format used by the zlib library is described by RFCs (Request for
26*4882a593Smuzhiyun   Comments) 1950 to 1952 in the files https://www.ietf.org/rfc/rfc1950.txt
27*4882a593Smuzhiyun   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #ifndef _ZLIB_H
31*4882a593Smuzhiyun #define _ZLIB_H
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/zconf.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* zlib deflate based on ZLIB_VERSION "1.1.3" */
36*4882a593Smuzhiyun /* zlib inflate based on ZLIB_VERSION "1.2.3" */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun   This is a modified version of zlib for use inside the Linux kernel.
40*4882a593Smuzhiyun   The main changes are to perform all memory allocation in advance.
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun   Inflation Changes:
43*4882a593Smuzhiyun     * Z_PACKET_FLUSH is added and used by ppp_deflate. Before returning
44*4882a593Smuzhiyun       this checks there is no more input data available and the next data
45*4882a593Smuzhiyun       is a STORED block. It also resets the mode to be read for the next
46*4882a593Smuzhiyun       data, all as per PPP requirements.
47*4882a593Smuzhiyun     * Addition of zlib_inflateIncomp which copies incompressible data into
48*4882a593Smuzhiyun       the history window and adjusts the accoutning without calling
49*4882a593Smuzhiyun       zlib_inflate itself to inflate the data.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun      The 'zlib' compression library provides in-memory compression and
54*4882a593Smuzhiyun   decompression functions, including integrity checks of the uncompressed
55*4882a593Smuzhiyun   data.  This version of the library supports only one compression method
56*4882a593Smuzhiyun   (deflation) but other algorithms will be added later and will have the same
57*4882a593Smuzhiyun   stream interface.
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun      Compression can be done in a single step if the buffers are large
60*4882a593Smuzhiyun   enough (for example if an input file is mmap'ed), or can be done by
61*4882a593Smuzhiyun   repeated calls of the compression function.  In the latter case, the
62*4882a593Smuzhiyun   application must provide more input and/or consume the output
63*4882a593Smuzhiyun   (providing more output space) before each call.
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun      The compressed data format used by default by the in-memory functions is
66*4882a593Smuzhiyun   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
67*4882a593Smuzhiyun   around a deflate stream, which is itself documented in RFC 1951.
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun      The library also supports reading and writing files in gzip (.gz) format
70*4882a593Smuzhiyun   with an interface similar to that of stdio.
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun      The zlib format was designed to be compact and fast for use in memory
73*4882a593Smuzhiyun   and on communications channels.  The gzip format was designed for single-
74*4882a593Smuzhiyun   file compression on file systems, has a larger header than zlib to maintain
75*4882a593Smuzhiyun   directory information, and uses a different, slower check method than zlib.
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun      The library does not install any signal handler. The decoder checks
78*4882a593Smuzhiyun   the consistency of the compressed data, so the library should never
79*4882a593Smuzhiyun   crash even in case of corrupted input.
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct internal_state;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun typedef struct z_stream_s {
85*4882a593Smuzhiyun     const Byte *next_in;   /* next input byte */
86*4882a593Smuzhiyun 	uLong avail_in;  /* number of bytes available at next_in */
87*4882a593Smuzhiyun     uLong    total_in;  /* total nb of input bytes read so far */
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun     Byte    *next_out;  /* next output byte should be put there */
90*4882a593Smuzhiyun 	uLong avail_out; /* remaining free space at next_out */
91*4882a593Smuzhiyun     uLong    total_out; /* total nb of bytes output so far */
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun     char     *msg;      /* last error message, NULL if no error */
94*4882a593Smuzhiyun     struct internal_state *state; /* not visible by applications */
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun     void     *workspace; /* memory allocated for this stream */
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun     int     data_type;  /* best guess about the data type: ascii or binary */
99*4882a593Smuzhiyun     uLong   adler;      /* adler32 value of the uncompressed data */
100*4882a593Smuzhiyun     uLong   reserved;   /* reserved for future use */
101*4882a593Smuzhiyun } z_stream;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun typedef z_stream *z_streamp;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun    The application must update next_in and avail_in when avail_in has
107*4882a593Smuzhiyun    dropped to zero. It must update next_out and avail_out when avail_out
108*4882a593Smuzhiyun    has dropped to zero. The application must initialize zalloc, zfree and
109*4882a593Smuzhiyun    opaque before calling the init function. All other fields are set by the
110*4882a593Smuzhiyun    compression library and must not be updated by the application.
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun    The opaque value provided by the application will be passed as the first
113*4882a593Smuzhiyun    parameter for calls of zalloc and zfree. This can be useful for custom
114*4882a593Smuzhiyun    memory management. The compression library attaches no meaning to the
115*4882a593Smuzhiyun    opaque value.
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun    zalloc must return NULL if there is not enough memory for the object.
118*4882a593Smuzhiyun    If zlib is used in a multi-threaded application, zalloc and zfree must be
119*4882a593Smuzhiyun    thread safe.
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun    On 16-bit systems, the functions zalloc and zfree must be able to allocate
122*4882a593Smuzhiyun    exactly 65536 bytes, but will not be required to allocate more than this
123*4882a593Smuzhiyun    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
124*4882a593Smuzhiyun    pointers returned by zalloc for objects of exactly 65536 bytes *must*
125*4882a593Smuzhiyun    have their offset normalized to zero. The default allocation function
126*4882a593Smuzhiyun    provided by this library ensures this (see zutil.c). To reduce memory
127*4882a593Smuzhiyun    requirements and avoid any allocation of 64K objects, at the expense of
128*4882a593Smuzhiyun    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun    The fields total_in and total_out can be used for statistics or
131*4882a593Smuzhiyun    progress reports. After compression, total_in holds the total size of
132*4882a593Smuzhiyun    the uncompressed data and may be saved for use in the decompressor
133*4882a593Smuzhiyun    (particularly if the decompressor wants to decompress everything in
134*4882a593Smuzhiyun    a single step).
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun                         /* constants */
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun #define Z_NO_FLUSH      0
140*4882a593Smuzhiyun #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
141*4882a593Smuzhiyun #define Z_PACKET_FLUSH  2
142*4882a593Smuzhiyun #define Z_SYNC_FLUSH    3
143*4882a593Smuzhiyun #define Z_FULL_FLUSH    4
144*4882a593Smuzhiyun #define Z_FINISH        5
145*4882a593Smuzhiyun #define Z_BLOCK         6 /* Only for inflate at present */
146*4882a593Smuzhiyun /* Allowed flush values; see deflate() and inflate() below for details */
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun #define Z_OK            0
149*4882a593Smuzhiyun #define Z_STREAM_END    1
150*4882a593Smuzhiyun #define Z_NEED_DICT     2
151*4882a593Smuzhiyun #define Z_ERRNO        (-1)
152*4882a593Smuzhiyun #define Z_STREAM_ERROR (-2)
153*4882a593Smuzhiyun #define Z_DATA_ERROR   (-3)
154*4882a593Smuzhiyun #define Z_MEM_ERROR    (-4)
155*4882a593Smuzhiyun #define Z_BUF_ERROR    (-5)
156*4882a593Smuzhiyun #define Z_VERSION_ERROR (-6)
157*4882a593Smuzhiyun /* Return codes for the compression/decompression functions. Negative
158*4882a593Smuzhiyun  * values are errors, positive values are used for special but normal events.
159*4882a593Smuzhiyun  */
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun #define Z_NO_COMPRESSION         0
162*4882a593Smuzhiyun #define Z_BEST_SPEED             1
163*4882a593Smuzhiyun #define Z_BEST_COMPRESSION       9
164*4882a593Smuzhiyun #define Z_DEFAULT_COMPRESSION  (-1)
165*4882a593Smuzhiyun /* compression levels */
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun #define Z_FILTERED            1
168*4882a593Smuzhiyun #define Z_HUFFMAN_ONLY        2
169*4882a593Smuzhiyun #define Z_DEFAULT_STRATEGY    0
170*4882a593Smuzhiyun /* compression strategy; see deflateInit2() below for details */
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun #define Z_BINARY   0
173*4882a593Smuzhiyun #define Z_ASCII    1
174*4882a593Smuzhiyun #define Z_UNKNOWN  2
175*4882a593Smuzhiyun /* Possible values of the data_type field */
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun #define Z_DEFLATED   8
178*4882a593Smuzhiyun /* The deflate compression method (the only one supported in this version) */
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun                         /* basic functions */
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun extern int zlib_deflate_workspacesize (int windowBits, int memLevel);
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun    Returns the number of bytes that needs to be allocated for a per-
185*4882a593Smuzhiyun    stream workspace with the specified parameters.  A pointer to this
186*4882a593Smuzhiyun    number of bytes should be returned in stream->workspace before
187*4882a593Smuzhiyun    you call zlib_deflateInit() or zlib_deflateInit2().  If you call
188*4882a593Smuzhiyun    zlib_deflateInit(), specify windowBits = MAX_WBITS and memLevel =
189*4882a593Smuzhiyun    MAX_MEM_LEVEL here.  If you call zlib_deflateInit2(), the windowBits
190*4882a593Smuzhiyun    and memLevel parameters passed to zlib_deflateInit2() must not
191*4882a593Smuzhiyun    exceed those passed here.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun extern int zlib_deflate_dfltcc_enabled (void);
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun    Returns 1 if Deflate-Conversion facility is installed and enabled,
197*4882a593Smuzhiyun    otherwise 0.
198*4882a593Smuzhiyun */
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun extern int deflateInit (z_streamp strm, int level);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun      Initializes the internal stream state for compression. The fields
204*4882a593Smuzhiyun    zalloc, zfree and opaque must be initialized before by the caller.
205*4882a593Smuzhiyun    If zalloc and zfree are set to NULL, deflateInit updates them to
206*4882a593Smuzhiyun    use default allocation functions.
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
209*4882a593Smuzhiyun    1 gives best speed, 9 gives best compression, 0 gives no compression at
210*4882a593Smuzhiyun    all (the input data is simply copied a block at a time).
211*4882a593Smuzhiyun    Z_DEFAULT_COMPRESSION requests a default compromise between speed and
212*4882a593Smuzhiyun    compression (currently equivalent to level 6).
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
215*4882a593Smuzhiyun    enough memory, Z_STREAM_ERROR if level is not a valid compression level,
216*4882a593Smuzhiyun    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
217*4882a593Smuzhiyun    with the version assumed by the caller (ZLIB_VERSION).
218*4882a593Smuzhiyun    msg is set to null if there is no error message.  deflateInit does not
219*4882a593Smuzhiyun    perform any compression: this will be done by deflate().
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun extern int zlib_deflate (z_streamp strm, int flush);
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun     deflate compresses as much data as possible, and stops when the input
226*4882a593Smuzhiyun   buffer becomes empty or the output buffer becomes full. It may introduce some
227*4882a593Smuzhiyun   output latency (reading input without producing any output) except when
228*4882a593Smuzhiyun   forced to flush.
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun     The detailed semantics are as follows. deflate performs one or both of the
231*4882a593Smuzhiyun   following actions:
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun   - Compress more input starting at next_in and update next_in and avail_in
234*4882a593Smuzhiyun     accordingly. If not all input can be processed (because there is not
235*4882a593Smuzhiyun     enough room in the output buffer), next_in and avail_in are updated and
236*4882a593Smuzhiyun     processing will resume at this point for the next call of deflate().
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun   - Provide more output starting at next_out and update next_out and avail_out
239*4882a593Smuzhiyun     accordingly. This action is forced if the parameter flush is non zero.
240*4882a593Smuzhiyun     Forcing flush frequently degrades the compression ratio, so this parameter
241*4882a593Smuzhiyun     should be set only when necessary (in interactive applications).
242*4882a593Smuzhiyun     Some output may be provided even if flush is not set.
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun   Before the call of deflate(), the application should ensure that at least
245*4882a593Smuzhiyun   one of the actions is possible, by providing more input and/or consuming
246*4882a593Smuzhiyun   more output, and updating avail_in or avail_out accordingly; avail_out
247*4882a593Smuzhiyun   should never be zero before the call. The application can consume the
248*4882a593Smuzhiyun   compressed output when it wants, for example when the output buffer is full
249*4882a593Smuzhiyun   (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
250*4882a593Smuzhiyun   and with zero avail_out, it must be called again after making room in the
251*4882a593Smuzhiyun   output buffer because there might be more output pending.
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
254*4882a593Smuzhiyun   flushed to the output buffer and the output is aligned on a byte boundary, so
255*4882a593Smuzhiyun   that the decompressor can get all input data available so far. (In particular
256*4882a593Smuzhiyun   avail_in is zero after the call if enough output space has been provided
257*4882a593Smuzhiyun   before the call.)  Flushing may degrade compression for some compression
258*4882a593Smuzhiyun   algorithms and so it should be used only when necessary.
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun     If flush is set to Z_FULL_FLUSH, all output is flushed as with
261*4882a593Smuzhiyun   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
262*4882a593Smuzhiyun   restart from this point if previous compressed data has been damaged or if
263*4882a593Smuzhiyun   random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
264*4882a593Smuzhiyun   the compression.
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun     If deflate returns with avail_out == 0, this function must be called again
267*4882a593Smuzhiyun   with the same value of the flush parameter and more output space (updated
268*4882a593Smuzhiyun   avail_out), until the flush is complete (deflate returns with non-zero
269*4882a593Smuzhiyun   avail_out).
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun     If the parameter flush is set to Z_FINISH, pending input is processed,
272*4882a593Smuzhiyun   pending output is flushed and deflate returns with Z_STREAM_END if there
273*4882a593Smuzhiyun   was enough output space; if deflate returns with Z_OK, this function must be
274*4882a593Smuzhiyun   called again with Z_FINISH and more output space (updated avail_out) but no
275*4882a593Smuzhiyun   more input data, until it returns with Z_STREAM_END or an error. After
276*4882a593Smuzhiyun   deflate has returned Z_STREAM_END, the only possible operations on the
277*4882a593Smuzhiyun   stream are deflateReset or deflateEnd.
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun     Z_FINISH can be used immediately after deflateInit if all the compression
280*4882a593Smuzhiyun   is to be done in a single step. In this case, avail_out must be at least
281*4882a593Smuzhiyun   0.1% larger than avail_in plus 12 bytes.  If deflate does not return
282*4882a593Smuzhiyun   Z_STREAM_END, then it must be called again as described above.
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun     deflate() sets strm->adler to the adler32 checksum of all input read
285*4882a593Smuzhiyun   so far (that is, total_in bytes).
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun     deflate() may update data_type if it can make a good guess about
288*4882a593Smuzhiyun   the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
289*4882a593Smuzhiyun   binary. This field is only for information purposes and does not affect
290*4882a593Smuzhiyun   the compression algorithm in any manner.
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun     deflate() returns Z_OK if some progress has been made (more input
293*4882a593Smuzhiyun   processed or more output produced), Z_STREAM_END if all input has been
294*4882a593Smuzhiyun   consumed and all output has been produced (only when flush is set to
295*4882a593Smuzhiyun   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
296*4882a593Smuzhiyun   if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
297*4882a593Smuzhiyun   (for example avail_in or avail_out was zero).
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun extern int zlib_deflateEnd (z_streamp strm);
302*4882a593Smuzhiyun /*
303*4882a593Smuzhiyun      All dynamically allocated data structures for this stream are freed.
304*4882a593Smuzhiyun    This function discards any unprocessed input and does not flush any
305*4882a593Smuzhiyun    pending output.
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
308*4882a593Smuzhiyun    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
309*4882a593Smuzhiyun    prematurely (some input or output was discarded). In the error case,
310*4882a593Smuzhiyun    msg may be set but then points to a static string (which must not be
311*4882a593Smuzhiyun    deallocated).
312*4882a593Smuzhiyun */
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun extern int zlib_inflate_workspacesize (void);
316*4882a593Smuzhiyun /*
317*4882a593Smuzhiyun    Returns the number of bytes that needs to be allocated for a per-
318*4882a593Smuzhiyun    stream workspace.  A pointer to this number of bytes should be
319*4882a593Smuzhiyun    returned in stream->workspace before calling zlib_inflateInit().
320*4882a593Smuzhiyun */
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun /*
323*4882a593Smuzhiyun extern int zlib_inflateInit (z_streamp strm);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun      Initializes the internal stream state for decompression. The fields
326*4882a593Smuzhiyun    next_in, avail_in, and workspace must be initialized before by
327*4882a593Smuzhiyun    the caller. If next_in is not NULL and avail_in is large enough (the exact
328*4882a593Smuzhiyun    value depends on the compression method), inflateInit determines the
329*4882a593Smuzhiyun    compression method from the zlib header and allocates all data structures
330*4882a593Smuzhiyun    accordingly; otherwise the allocation will be deferred to the first call of
331*4882a593Smuzhiyun    inflate.  If zalloc and zfree are set to NULL, inflateInit updates them to
332*4882a593Smuzhiyun    use default allocation functions.
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
335*4882a593Smuzhiyun    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
336*4882a593Smuzhiyun    version assumed by the caller.  msg is set to null if there is no error
337*4882a593Smuzhiyun    message. inflateInit does not perform any decompression apart from reading
338*4882a593Smuzhiyun    the zlib header if present: this will be done by inflate().  (So next_in and
339*4882a593Smuzhiyun    avail_in may be modified, but next_out and avail_out are unchanged.)
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun extern int zlib_inflate (z_streamp strm, int flush);
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun     inflate decompresses as much data as possible, and stops when the input
346*4882a593Smuzhiyun   buffer becomes empty or the output buffer becomes full. It may introduce
347*4882a593Smuzhiyun   some output latency (reading input without producing any output) except when
348*4882a593Smuzhiyun   forced to flush.
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun   The detailed semantics are as follows. inflate performs one or both of the
351*4882a593Smuzhiyun   following actions:
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun   - Decompress more input starting at next_in and update next_in and avail_in
354*4882a593Smuzhiyun     accordingly. If not all input can be processed (because there is not
355*4882a593Smuzhiyun     enough room in the output buffer), next_in is updated and processing
356*4882a593Smuzhiyun     will resume at this point for the next call of inflate().
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun   - Provide more output starting at next_out and update next_out and avail_out
359*4882a593Smuzhiyun     accordingly.  inflate() provides as much output as possible, until there
360*4882a593Smuzhiyun     is no more input data or no more space in the output buffer (see below
361*4882a593Smuzhiyun     about the flush parameter).
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun   Before the call of inflate(), the application should ensure that at least
364*4882a593Smuzhiyun   one of the actions is possible, by providing more input and/or consuming
365*4882a593Smuzhiyun   more output, and updating the next_* and avail_* values accordingly.
366*4882a593Smuzhiyun   The application can consume the uncompressed output when it wants, for
367*4882a593Smuzhiyun   example when the output buffer is full (avail_out == 0), or after each
368*4882a593Smuzhiyun   call of inflate(). If inflate returns Z_OK and with zero avail_out, it
369*4882a593Smuzhiyun   must be called again after making room in the output buffer because there
370*4882a593Smuzhiyun   might be more output pending.
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun     The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
373*4882a593Smuzhiyun   Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
374*4882a593Smuzhiyun   output as possible to the output buffer. Z_BLOCK requests that inflate() stop
375*4882a593Smuzhiyun   if and when it gets to the next deflate block boundary. When decoding the
376*4882a593Smuzhiyun   zlib or gzip format, this will cause inflate() to return immediately after
377*4882a593Smuzhiyun   the header and before the first block. When doing a raw inflate, inflate()
378*4882a593Smuzhiyun   will go ahead and process the first block, and will return when it gets to
379*4882a593Smuzhiyun   the end of that block, or when it runs out of data.
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun     The Z_BLOCK option assists in appending to or combining deflate streams.
382*4882a593Smuzhiyun   Also to assist in this, on return inflate() will set strm->data_type to the
383*4882a593Smuzhiyun   number of unused bits in the last byte taken from strm->next_in, plus 64
384*4882a593Smuzhiyun   if inflate() is currently decoding the last block in the deflate stream,
385*4882a593Smuzhiyun   plus 128 if inflate() returned immediately after decoding an end-of-block
386*4882a593Smuzhiyun   code or decoding the complete header up to just before the first byte of the
387*4882a593Smuzhiyun   deflate stream. The end-of-block will not be indicated until all of the
388*4882a593Smuzhiyun   uncompressed data from that block has been written to strm->next_out.  The
389*4882a593Smuzhiyun   number of unused bits may in general be greater than seven, except when
390*4882a593Smuzhiyun   bit 7 of data_type is set, in which case the number of unused bits will be
391*4882a593Smuzhiyun   less than eight.
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun     inflate() should normally be called until it returns Z_STREAM_END or an
394*4882a593Smuzhiyun   error. However if all decompression is to be performed in a single step
395*4882a593Smuzhiyun   (a single call of inflate), the parameter flush should be set to
396*4882a593Smuzhiyun   Z_FINISH. In this case all pending input is processed and all pending
397*4882a593Smuzhiyun   output is flushed; avail_out must be large enough to hold all the
398*4882a593Smuzhiyun   uncompressed data. (The size of the uncompressed data may have been saved
399*4882a593Smuzhiyun   by the compressor for this purpose.) The next operation on this stream must
400*4882a593Smuzhiyun   be inflateEnd to deallocate the decompression state. The use of Z_FINISH
401*4882a593Smuzhiyun   is never required, but can be used to inform inflate that a faster approach
402*4882a593Smuzhiyun   may be used for the single inflate() call.
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun      In this implementation, inflate() always flushes as much output as
405*4882a593Smuzhiyun   possible to the output buffer, and always uses the faster approach on the
406*4882a593Smuzhiyun   first call. So the only effect of the flush parameter in this implementation
407*4882a593Smuzhiyun   is on the return value of inflate(), as noted below, or when it returns early
408*4882a593Smuzhiyun   because Z_BLOCK is used.
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun      If a preset dictionary is needed after this call (see inflateSetDictionary
411*4882a593Smuzhiyun   below), inflate sets strm->adler to the adler32 checksum of the dictionary
412*4882a593Smuzhiyun   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
413*4882a593Smuzhiyun   strm->adler to the adler32 checksum of all output produced so far (that is,
414*4882a593Smuzhiyun   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
415*4882a593Smuzhiyun   below. At the end of the stream, inflate() checks that its computed adler32
416*4882a593Smuzhiyun   checksum is equal to that saved by the compressor and returns Z_STREAM_END
417*4882a593Smuzhiyun   only if the checksum is correct.
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun     inflate() will decompress and check either zlib-wrapped or gzip-wrapped
420*4882a593Smuzhiyun   deflate data.  The header type is detected automatically.  Any information
421*4882a593Smuzhiyun   contained in the gzip header is not retained, so applications that need that
422*4882a593Smuzhiyun   information should instead use raw inflate, see inflateInit2() below, or
423*4882a593Smuzhiyun   inflateBack() and perform their own processing of the gzip header and
424*4882a593Smuzhiyun   trailer.
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun     inflate() returns Z_OK if some progress has been made (more input processed
427*4882a593Smuzhiyun   or more output produced), Z_STREAM_END if the end of the compressed data has
428*4882a593Smuzhiyun   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
429*4882a593Smuzhiyun   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
430*4882a593Smuzhiyun   corrupted (input stream not conforming to the zlib format or incorrect check
431*4882a593Smuzhiyun   value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
432*4882a593Smuzhiyun   if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
433*4882a593Smuzhiyun   Z_BUF_ERROR if no progress is possible or if there was not enough room in the
434*4882a593Smuzhiyun   output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
435*4882a593Smuzhiyun   inflate() can be called again with more input and more output space to
436*4882a593Smuzhiyun   continue decompressing. If Z_DATA_ERROR is returned, the application may then
437*4882a593Smuzhiyun   call inflateSync() to look for a good compression block if a partial recovery
438*4882a593Smuzhiyun   of the data is desired.
439*4882a593Smuzhiyun */
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun extern int zlib_inflateEnd (z_streamp strm);
443*4882a593Smuzhiyun /*
444*4882a593Smuzhiyun      All dynamically allocated data structures for this stream are freed.
445*4882a593Smuzhiyun    This function discards any unprocessed input and does not flush any
446*4882a593Smuzhiyun    pending output.
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
449*4882a593Smuzhiyun    was inconsistent. In the error case, msg may be set but then points to a
450*4882a593Smuzhiyun    static string (which must not be deallocated).
451*4882a593Smuzhiyun */
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun                         /* Advanced functions */
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun     The following functions are needed only in some special applications.
457*4882a593Smuzhiyun */
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun /*
460*4882a593Smuzhiyun extern int deflateInit2 (z_streamp strm,
461*4882a593Smuzhiyun                                      int  level,
462*4882a593Smuzhiyun                                      int  method,
463*4882a593Smuzhiyun                                      int  windowBits,
464*4882a593Smuzhiyun                                      int  memLevel,
465*4882a593Smuzhiyun                                      int  strategy);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun      This is another version of deflateInit with more compression options. The
468*4882a593Smuzhiyun    fields next_in, zalloc, zfree and opaque must be initialized before by
469*4882a593Smuzhiyun    the caller.
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun      The method parameter is the compression method. It must be Z_DEFLATED in
472*4882a593Smuzhiyun    this version of the library.
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun      The windowBits parameter is the base two logarithm of the window size
475*4882a593Smuzhiyun    (the size of the history buffer).  It should be in the range 8..15 for this
476*4882a593Smuzhiyun    version of the library. Larger values of this parameter result in better
477*4882a593Smuzhiyun    compression at the expense of memory usage. The default value is 15 if
478*4882a593Smuzhiyun    deflateInit is used instead.
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun      The memLevel parameter specifies how much memory should be allocated
481*4882a593Smuzhiyun    for the internal compression state. memLevel=1 uses minimum memory but
482*4882a593Smuzhiyun    is slow and reduces compression ratio; memLevel=9 uses maximum memory
483*4882a593Smuzhiyun    for optimal speed. The default value is 8. See zconf.h for total memory
484*4882a593Smuzhiyun    usage as a function of windowBits and memLevel.
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun      The strategy parameter is used to tune the compression algorithm. Use the
487*4882a593Smuzhiyun    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
488*4882a593Smuzhiyun    filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
489*4882a593Smuzhiyun    string match).  Filtered data consists mostly of small values with a
490*4882a593Smuzhiyun    somewhat random distribution. In this case, the compression algorithm is
491*4882a593Smuzhiyun    tuned to compress them better. The effect of Z_FILTERED is to force more
492*4882a593Smuzhiyun    Huffman coding and less string matching; it is somewhat intermediate
493*4882a593Smuzhiyun    between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
494*4882a593Smuzhiyun    the compression ratio but not the correctness of the compressed output even
495*4882a593Smuzhiyun    if it is not set appropriately.
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun       deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
498*4882a593Smuzhiyun    memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
499*4882a593Smuzhiyun    method). msg is set to null if there is no error message.  deflateInit2 does
500*4882a593Smuzhiyun    not perform any compression: this will be done by deflate().
501*4882a593Smuzhiyun */
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun extern int zlib_deflateReset (z_streamp strm);
504*4882a593Smuzhiyun /*
505*4882a593Smuzhiyun      This function is equivalent to deflateEnd followed by deflateInit,
506*4882a593Smuzhiyun    but does not free and reallocate all the internal compression state.
507*4882a593Smuzhiyun    The stream will keep the same compression level and any other attributes
508*4882a593Smuzhiyun    that may have been set by deflateInit2.
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun       deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
511*4882a593Smuzhiyun    stream state was inconsistent (such as zalloc or state being NULL).
512*4882a593Smuzhiyun */
513*4882a593Smuzhiyun 
deflateBound(unsigned long s)514*4882a593Smuzhiyun static inline unsigned long deflateBound(unsigned long s)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	return s + ((s + 7) >> 3) + ((s + 63) >> 6) + 11;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun extern int inflateInit2 (z_streamp strm, int  windowBits);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun      This is another version of inflateInit with an extra parameter. The
523*4882a593Smuzhiyun    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
524*4882a593Smuzhiyun    before by the caller.
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun      The windowBits parameter is the base two logarithm of the maximum window
527*4882a593Smuzhiyun    size (the size of the history buffer).  It should be in the range 8..15 for
528*4882a593Smuzhiyun    this version of the library. The default value is 15 if inflateInit is used
529*4882a593Smuzhiyun    instead. windowBits must be greater than or equal to the windowBits value
530*4882a593Smuzhiyun    provided to deflateInit2() while compressing, or it must be equal to 15 if
531*4882a593Smuzhiyun    deflateInit2() was not used. If a compressed stream with a larger window
532*4882a593Smuzhiyun    size is given as input, inflate() will return with the error code
533*4882a593Smuzhiyun    Z_DATA_ERROR instead of trying to allocate a larger window.
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun      windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
536*4882a593Smuzhiyun    determines the window size. inflate() will then process raw deflate data,
537*4882a593Smuzhiyun    not looking for a zlib or gzip header, not generating a check value, and not
538*4882a593Smuzhiyun    looking for any check values for comparison at the end of the stream. This
539*4882a593Smuzhiyun    is for use with other formats that use the deflate compressed data format
540*4882a593Smuzhiyun    such as zip.  Those formats provide their own check values. If a custom
541*4882a593Smuzhiyun    format is developed using the raw deflate format for compressed data, it is
542*4882a593Smuzhiyun    recommended that a check value such as an adler32 or a crc32 be applied to
543*4882a593Smuzhiyun    the uncompressed data as is done in the zlib, gzip, and zip formats.  For
544*4882a593Smuzhiyun    most applications, the zlib format should be used as is. Note that comments
545*4882a593Smuzhiyun    above on the use in deflateInit2() applies to the magnitude of windowBits.
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun      windowBits can also be greater than 15 for optional gzip decoding. Add
548*4882a593Smuzhiyun    32 to windowBits to enable zlib and gzip decoding with automatic header
549*4882a593Smuzhiyun    detection, or add 16 to decode only the gzip format (the zlib format will
550*4882a593Smuzhiyun    return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is
551*4882a593Smuzhiyun    a crc32 instead of an adler32.
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
554*4882a593Smuzhiyun    memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
555*4882a593Smuzhiyun    is set to null if there is no error message.  inflateInit2 does not perform
556*4882a593Smuzhiyun    any decompression apart from reading the zlib header if present: this will
557*4882a593Smuzhiyun    be done by inflate(). (So next_in and avail_in may be modified, but next_out
558*4882a593Smuzhiyun    and avail_out are unchanged.)
559*4882a593Smuzhiyun */
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun extern int zlib_inflateReset (z_streamp strm);
562*4882a593Smuzhiyun /*
563*4882a593Smuzhiyun      This function is equivalent to inflateEnd followed by inflateInit,
564*4882a593Smuzhiyun    but does not free and reallocate all the internal decompression state.
565*4882a593Smuzhiyun    The stream will keep attributes that may have been set by inflateInit2.
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun       inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
568*4882a593Smuzhiyun    stream state was inconsistent (such as zalloc or state being NULL).
569*4882a593Smuzhiyun */
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun extern int zlib_inflateIncomp (z_stream *strm);
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun      This function adds the data at next_in (avail_in bytes) to the output
574*4882a593Smuzhiyun    history without performing any output.  There must be no pending output,
575*4882a593Smuzhiyun    and the decompressor must be expecting to see the start of a block.
576*4882a593Smuzhiyun    Calling this function is equivalent to decompressing a stored block
577*4882a593Smuzhiyun    containing the data at next_in (except that the data is not output).
578*4882a593Smuzhiyun */
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun #define zlib_deflateInit(strm, level) \
581*4882a593Smuzhiyun 	zlib_deflateInit2((strm), (level), Z_DEFLATED, MAX_WBITS, \
582*4882a593Smuzhiyun 			      DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY)
583*4882a593Smuzhiyun #define zlib_inflateInit(strm) \
584*4882a593Smuzhiyun 	zlib_inflateInit2((strm), DEF_WBITS)
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun extern int zlib_deflateInit2(z_streamp strm, int  level, int  method,
587*4882a593Smuzhiyun                                       int windowBits, int memLevel,
588*4882a593Smuzhiyun                                       int strategy);
589*4882a593Smuzhiyun extern int zlib_inflateInit2(z_streamp strm, int  windowBits);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun #if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
592*4882a593Smuzhiyun     struct internal_state {int dummy;}; /* hack for buggy compilers */
593*4882a593Smuzhiyun #endif
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun /* Utility function: initialize zlib, unpack binary blob, clean up zlib,
596*4882a593Smuzhiyun  * return len or negative error code. */
597*4882a593Smuzhiyun extern int zlib_inflate_blob(void *dst, unsigned dst_sz, const void *src, unsigned src_sz);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun #endif /* _ZLIB_H */
600