xref: /dflybsd-src/sys/vfs/hammer2/zlib/hammer2_zlib.h (revision 355d67fcd81e0a7b17007d691bb00bdd151f3d28)
1*355d67fcSMatthew Dillon /* zlib.h -- interface of the 'zlib' general purpose compression library
2*355d67fcSMatthew Dillon   version 1.2.8, April 28th, 2013
3*355d67fcSMatthew Dillon 
4*355d67fcSMatthew Dillon   Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
5*355d67fcSMatthew Dillon 
6*355d67fcSMatthew Dillon   This software is provided 'as-is', without any express or implied
7*355d67fcSMatthew Dillon   warranty.  In no event will the authors be held liable for any damages
8*355d67fcSMatthew Dillon   arising from the use of this software.
9*355d67fcSMatthew Dillon 
10*355d67fcSMatthew Dillon   Permission is granted to anyone to use this software for any purpose,
11*355d67fcSMatthew Dillon   including commercial applications, and to alter it and redistribute it
12*355d67fcSMatthew Dillon   freely, subject to the following restrictions:
13*355d67fcSMatthew Dillon 
14*355d67fcSMatthew Dillon   1. The origin of this software must not be misrepresented; you must not
15*355d67fcSMatthew Dillon      claim that you wrote the original software. If you use this software
16*355d67fcSMatthew Dillon      in a product, an acknowledgment in the product documentation would be
17*355d67fcSMatthew Dillon      appreciated but is not required.
18*355d67fcSMatthew Dillon   2. Altered source versions must be plainly marked as such, and must not be
19*355d67fcSMatthew Dillon      misrepresented as being the original software.
20*355d67fcSMatthew Dillon   3. This notice may not be removed or altered from any source distribution.
21*355d67fcSMatthew Dillon 
22*355d67fcSMatthew Dillon   Jean-loup Gailly        Mark Adler
23*355d67fcSMatthew Dillon   jloup@gzip.org          madler@alumni.caltech.edu
24*355d67fcSMatthew Dillon 
25*355d67fcSMatthew Dillon 
26*355d67fcSMatthew Dillon   The data format used by the zlib library is described by RFCs (Request for
27*355d67fcSMatthew Dillon   Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
28*355d67fcSMatthew Dillon   (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
29*355d67fcSMatthew Dillon */
30*355d67fcSMatthew Dillon 
31*355d67fcSMatthew Dillon #ifndef ZLIB_H
32*355d67fcSMatthew Dillon #define ZLIB_H
33*355d67fcSMatthew Dillon 
34*355d67fcSMatthew Dillon //#include "zconf.h"
35*355d67fcSMatthew Dillon 
36*355d67fcSMatthew Dillon #include "hammer2_zlib_zconf.h"
37*355d67fcSMatthew Dillon 
38*355d67fcSMatthew Dillon #ifdef __cplusplus
39*355d67fcSMatthew Dillon extern "C" {
40*355d67fcSMatthew Dillon #endif
41*355d67fcSMatthew Dillon 
42*355d67fcSMatthew Dillon #define ZLIB_VERSION "1.2.8"
43*355d67fcSMatthew Dillon #define ZLIB_VERNUM 0x1280
44*355d67fcSMatthew Dillon #define ZLIB_VER_MAJOR 1
45*355d67fcSMatthew Dillon #define ZLIB_VER_MINOR 2
46*355d67fcSMatthew Dillon #define ZLIB_VER_REVISION 8
47*355d67fcSMatthew Dillon #define ZLIB_VER_SUBREVISION 0
48*355d67fcSMatthew Dillon 
49*355d67fcSMatthew Dillon /*
50*355d67fcSMatthew Dillon     The 'zlib' compression library provides in-memory compression and
51*355d67fcSMatthew Dillon   decompression functions, including integrity checks of the uncompressed data.
52*355d67fcSMatthew Dillon   This version of the library supports only one compression method (deflation)
53*355d67fcSMatthew Dillon   but other algorithms will be added later and will have the same stream
54*355d67fcSMatthew Dillon   interface.
55*355d67fcSMatthew Dillon 
56*355d67fcSMatthew Dillon     Compression can be done in a single step if the buffers are large enough,
57*355d67fcSMatthew Dillon   or can be done by repeated calls of the compression function.  In the latter
58*355d67fcSMatthew Dillon   case, the application must provide more input and/or consume the output
59*355d67fcSMatthew Dillon   (providing more output space) before each call.
60*355d67fcSMatthew Dillon 
61*355d67fcSMatthew Dillon     The compressed data format used by default by the in-memory functions is
62*355d67fcSMatthew Dillon   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
63*355d67fcSMatthew Dillon   around a deflate stream, which is itself documented in RFC 1951.
64*355d67fcSMatthew Dillon 
65*355d67fcSMatthew Dillon     The library also supports reading and writing files in gzip (.gz) format
66*355d67fcSMatthew Dillon   with an interface similar to that of stdio using the functions that start
67*355d67fcSMatthew Dillon   with "gz".  The gzip format is different from the zlib format.  gzip is a
68*355d67fcSMatthew Dillon   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
69*355d67fcSMatthew Dillon 
70*355d67fcSMatthew Dillon     This library can optionally read and write gzip streams in memory as well.
71*355d67fcSMatthew Dillon 
72*355d67fcSMatthew Dillon     The zlib format was designed to be compact and fast for use in memory
73*355d67fcSMatthew Dillon   and on communications channels.  The gzip format was designed for single-
74*355d67fcSMatthew Dillon   file compression on file systems, has a larger header than zlib to maintain
75*355d67fcSMatthew Dillon   directory information, and uses a different, slower check method than zlib.
76*355d67fcSMatthew Dillon 
77*355d67fcSMatthew Dillon     The library does not install any signal handler.  The decoder checks
78*355d67fcSMatthew Dillon   the consistency of the compressed data, so the library should never crash
79*355d67fcSMatthew Dillon   even in case of corrupted input.
80*355d67fcSMatthew Dillon */
81*355d67fcSMatthew Dillon 
82*355d67fcSMatthew Dillon struct internal_state;
83*355d67fcSMatthew Dillon 
84*355d67fcSMatthew Dillon typedef struct z_stream_s {
85*355d67fcSMatthew Dillon     z_const Bytef *next_in;     /* next input byte */
86*355d67fcSMatthew Dillon     uInt     avail_in;  /* number of bytes available at next_in */
87*355d67fcSMatthew Dillon     uLong    total_in;  /* total number of input bytes read so far */
88*355d67fcSMatthew Dillon 
89*355d67fcSMatthew Dillon     Bytef    *next_out; /* next output byte should be put there */
90*355d67fcSMatthew Dillon     uInt     avail_out; /* remaining free space at next_out */
91*355d67fcSMatthew Dillon     uLong    total_out; /* total number of bytes output so far */
92*355d67fcSMatthew Dillon 
93*355d67fcSMatthew Dillon     z_const char *msg;  /* last error message, NULL if no error */
94*355d67fcSMatthew Dillon     struct internal_state FAR *state; /* not visible by applications */
95*355d67fcSMatthew Dillon 
96*355d67fcSMatthew Dillon     int     data_type;  /* best guess about the data type: binary or text */
97*355d67fcSMatthew Dillon     uLong   adler;      /* adler32 value of the uncompressed data */
98*355d67fcSMatthew Dillon     uLong   reserved;   /* reserved for future use */
99*355d67fcSMatthew Dillon } z_stream;
100*355d67fcSMatthew Dillon 
101*355d67fcSMatthew Dillon typedef z_stream FAR *z_streamp;
102*355d67fcSMatthew Dillon 
103*355d67fcSMatthew Dillon /*
104*355d67fcSMatthew Dillon      The application must update next_in and avail_in when avail_in has dropped
105*355d67fcSMatthew Dillon    to zero.  It must update next_out and avail_out when avail_out has dropped
106*355d67fcSMatthew Dillon    to zero.  The application must initialize zalloc, zfree and opaque before
107*355d67fcSMatthew Dillon    calling the init function.  All other fields are set by the compression
108*355d67fcSMatthew Dillon    library and must not be updated by the application.
109*355d67fcSMatthew Dillon 
110*355d67fcSMatthew Dillon      The opaque value provided by the application will be passed as the first
111*355d67fcSMatthew Dillon    parameter for calls of zalloc and zfree.  This can be useful for custom
112*355d67fcSMatthew Dillon    memory management.  The compression library attaches no meaning to the
113*355d67fcSMatthew Dillon    opaque value.
114*355d67fcSMatthew Dillon 
115*355d67fcSMatthew Dillon      zalloc must return Z_NULL if there is not enough memory for the object.
116*355d67fcSMatthew Dillon    If zlib is used in a multi-threaded application, zalloc and zfree must be
117*355d67fcSMatthew Dillon    thread safe.
118*355d67fcSMatthew Dillon 
119*355d67fcSMatthew Dillon      On 16-bit systems, the functions zalloc and zfree must be able to allocate
120*355d67fcSMatthew Dillon    exactly 65536 bytes, but will not be required to allocate more than this if
121*355d67fcSMatthew Dillon    the symbol MAXSEG_64K is defined (see zconf.h).  WARNING: On MSDOS, pointers
122*355d67fcSMatthew Dillon    returned by zalloc for objects of exactly 65536 bytes *must* have their
123*355d67fcSMatthew Dillon    offset normalized to zero.  The default allocation function provided by this
124*355d67fcSMatthew Dillon    library ensures this (see zutil.c).  To reduce memory requirements and avoid
125*355d67fcSMatthew Dillon    any allocation of 64K objects, at the expense of compression ratio, compile
126*355d67fcSMatthew Dillon    the library with -DMAX_WBITS=14 (see zconf.h).
127*355d67fcSMatthew Dillon 
128*355d67fcSMatthew Dillon      The fields total_in and total_out can be used for statistics or progress
129*355d67fcSMatthew Dillon    reports.  After compression, total_in holds the total size of the
130*355d67fcSMatthew Dillon    uncompressed data and may be saved for use in the decompressor (particularly
131*355d67fcSMatthew Dillon    if the decompressor wants to decompress everything in a single step).
132*355d67fcSMatthew Dillon */
133*355d67fcSMatthew Dillon 
134*355d67fcSMatthew Dillon                         /* constants */
135*355d67fcSMatthew Dillon 
136*355d67fcSMatthew Dillon #define Z_NO_FLUSH      0
137*355d67fcSMatthew Dillon #define Z_PARTIAL_FLUSH 1
138*355d67fcSMatthew Dillon #define Z_SYNC_FLUSH    2
139*355d67fcSMatthew Dillon #define Z_FULL_FLUSH    3
140*355d67fcSMatthew Dillon #define Z_FINISH        4
141*355d67fcSMatthew Dillon #define Z_BLOCK         5
142*355d67fcSMatthew Dillon #define Z_TREES         6
143*355d67fcSMatthew Dillon /* Allowed flush values; see deflate() and inflate() below for details */
144*355d67fcSMatthew Dillon 
145*355d67fcSMatthew Dillon #define Z_OK            0
146*355d67fcSMatthew Dillon #define Z_STREAM_END    1
147*355d67fcSMatthew Dillon #define Z_NEED_DICT     2
148*355d67fcSMatthew Dillon #define Z_ERRNO        (-1)
149*355d67fcSMatthew Dillon #define Z_STREAM_ERROR (-2)
150*355d67fcSMatthew Dillon #define Z_DATA_ERROR   (-3)
151*355d67fcSMatthew Dillon #define Z_MEM_ERROR    (-4)
152*355d67fcSMatthew Dillon #define Z_BUF_ERROR    (-5)
153*355d67fcSMatthew Dillon #define Z_VERSION_ERROR (-6)
154*355d67fcSMatthew Dillon /* Return codes for the compression/decompression functions. Negative values
155*355d67fcSMatthew Dillon  * are errors, positive values are used for special but normal events.
156*355d67fcSMatthew Dillon  */
157*355d67fcSMatthew Dillon 
158*355d67fcSMatthew Dillon #define Z_NO_COMPRESSION         0
159*355d67fcSMatthew Dillon #define Z_BEST_SPEED             1
160*355d67fcSMatthew Dillon #define Z_BEST_COMPRESSION       9
161*355d67fcSMatthew Dillon #define Z_DEFAULT_COMPRESSION  (-1)
162*355d67fcSMatthew Dillon /* compression levels */
163*355d67fcSMatthew Dillon 
164*355d67fcSMatthew Dillon #define Z_FILTERED            1
165*355d67fcSMatthew Dillon #define Z_HUFFMAN_ONLY        2
166*355d67fcSMatthew Dillon #define Z_RLE                 3
167*355d67fcSMatthew Dillon #define Z_FIXED               4
168*355d67fcSMatthew Dillon #define Z_DEFAULT_STRATEGY    0
169*355d67fcSMatthew Dillon /* compression strategy; see deflateInit2() below for details */
170*355d67fcSMatthew Dillon 
171*355d67fcSMatthew Dillon #define Z_BINARY   0
172*355d67fcSMatthew Dillon #define Z_TEXT     1
173*355d67fcSMatthew Dillon #define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
174*355d67fcSMatthew Dillon #define Z_UNKNOWN  2
175*355d67fcSMatthew Dillon /* Possible values of the data_type field (though see inflate()) */
176*355d67fcSMatthew Dillon 
177*355d67fcSMatthew Dillon #define Z_DEFLATED   8
178*355d67fcSMatthew Dillon /* The deflate compression method (the only one supported in this version) */
179*355d67fcSMatthew Dillon 
180*355d67fcSMatthew Dillon #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
181*355d67fcSMatthew Dillon 
182*355d67fcSMatthew Dillon #define zlib_version zlibVersion()
183*355d67fcSMatthew Dillon /* for compatibility with versions < 1.0.2 */
184*355d67fcSMatthew Dillon 
185*355d67fcSMatthew Dillon 
186*355d67fcSMatthew Dillon                         /* basic functions */
187*355d67fcSMatthew Dillon 
188*355d67fcSMatthew Dillon //ZEXTERN const char * ZEXPORT zlibVersion OF((void));
189*355d67fcSMatthew Dillon /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
190*355d67fcSMatthew Dillon    If the first character differs, the library code actually used is not
191*355d67fcSMatthew Dillon    compatible with the zlib.h header file used by the application.  This check
192*355d67fcSMatthew Dillon    is automatically made by deflateInit and inflateInit.
193*355d67fcSMatthew Dillon  */
194*355d67fcSMatthew Dillon 
195*355d67fcSMatthew Dillon int deflateInit(z_streamp strm, int level);
196*355d67fcSMatthew Dillon 
197*355d67fcSMatthew Dillon /*
198*355d67fcSMatthew Dillon ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
199*355d67fcSMatthew Dillon 
200*355d67fcSMatthew Dillon      Initializes the internal stream state for compression.  The fields
201*355d67fcSMatthew Dillon    zalloc, zfree and opaque must be initialized before by the caller.  If
202*355d67fcSMatthew Dillon    zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
203*355d67fcSMatthew Dillon    allocation functions.
204*355d67fcSMatthew Dillon 
205*355d67fcSMatthew Dillon      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
206*355d67fcSMatthew Dillon    1 gives best speed, 9 gives best compression, 0 gives no compression at all
207*355d67fcSMatthew Dillon    (the input data is simply copied a block at a time).  Z_DEFAULT_COMPRESSION
208*355d67fcSMatthew Dillon    requests a default compromise between speed and compression (currently
209*355d67fcSMatthew Dillon    equivalent to level 6).
210*355d67fcSMatthew Dillon 
211*355d67fcSMatthew Dillon      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
212*355d67fcSMatthew Dillon    memory, Z_STREAM_ERROR if level is not a valid compression level, or
213*355d67fcSMatthew Dillon    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
214*355d67fcSMatthew Dillon    with the version assumed by the caller (ZLIB_VERSION).  msg is set to null
215*355d67fcSMatthew Dillon    if there is no error message.  deflateInit does not perform any compression:
216*355d67fcSMatthew Dillon    this will be done by deflate().
217*355d67fcSMatthew Dillon */
218*355d67fcSMatthew Dillon 
219*355d67fcSMatthew Dillon 
220*355d67fcSMatthew Dillon int deflate(z_streamp strm, int flush);
221*355d67fcSMatthew Dillon /*
222*355d67fcSMatthew Dillon     deflate compresses as much data as possible, and stops when the input
223*355d67fcSMatthew Dillon   buffer becomes empty or the output buffer becomes full.  It may introduce
224*355d67fcSMatthew Dillon   some output latency (reading input without producing any output) except when
225*355d67fcSMatthew Dillon   forced to flush.
226*355d67fcSMatthew Dillon 
227*355d67fcSMatthew Dillon     The detailed semantics are as follows.  deflate performs one or both of the
228*355d67fcSMatthew Dillon   following actions:
229*355d67fcSMatthew Dillon 
230*355d67fcSMatthew Dillon   - Compress more input starting at next_in and update next_in and avail_in
231*355d67fcSMatthew Dillon     accordingly.  If not all input can be processed (because there is not
232*355d67fcSMatthew Dillon     enough room in the output buffer), next_in and avail_in are updated and
233*355d67fcSMatthew Dillon     processing will resume at this point for the next call of deflate().
234*355d67fcSMatthew Dillon 
235*355d67fcSMatthew Dillon   - Provide more output starting at next_out and update next_out and avail_out
236*355d67fcSMatthew Dillon     accordingly.  This action is forced if the parameter flush is non zero.
237*355d67fcSMatthew Dillon     Forcing flush frequently degrades the compression ratio, so this parameter
238*355d67fcSMatthew Dillon     should be set only when necessary (in interactive applications).  Some
239*355d67fcSMatthew Dillon     output may be provided even if flush is not set.
240*355d67fcSMatthew Dillon 
241*355d67fcSMatthew Dillon     Before the call of deflate(), the application should ensure that at least
242*355d67fcSMatthew Dillon   one of the actions is possible, by providing more input and/or consuming more
243*355d67fcSMatthew Dillon   output, and updating avail_in or avail_out accordingly; avail_out should
244*355d67fcSMatthew Dillon   never be zero before the call.  The application can consume the compressed
245*355d67fcSMatthew Dillon   output when it wants, for example when the output buffer is full (avail_out
246*355d67fcSMatthew Dillon   == 0), or after each call of deflate().  If deflate returns Z_OK and with
247*355d67fcSMatthew Dillon   zero avail_out, it must be called again after making room in the output
248*355d67fcSMatthew Dillon   buffer because there might be more output pending.
249*355d67fcSMatthew Dillon 
250*355d67fcSMatthew Dillon     Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
251*355d67fcSMatthew Dillon   decide how much data to accumulate before producing output, in order to
252*355d67fcSMatthew Dillon   maximize compression.
253*355d67fcSMatthew Dillon 
254*355d67fcSMatthew Dillon     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
255*355d67fcSMatthew Dillon   flushed to the output buffer and the output is aligned on a byte boundary, so
256*355d67fcSMatthew Dillon   that the decompressor can get all input data available so far.  (In
257*355d67fcSMatthew Dillon   particular avail_in is zero after the call if enough output space has been
258*355d67fcSMatthew Dillon   provided before the call.) Flushing may degrade compression for some
259*355d67fcSMatthew Dillon   compression algorithms and so it should be used only when necessary.  This
260*355d67fcSMatthew Dillon   completes the current deflate block and follows it with an empty stored block
261*355d67fcSMatthew Dillon   that is three bits plus filler bits to the next byte, followed by four bytes
262*355d67fcSMatthew Dillon   (00 00 ff ff).
263*355d67fcSMatthew Dillon 
264*355d67fcSMatthew Dillon     If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
265*355d67fcSMatthew Dillon   output buffer, but the output is not aligned to a byte boundary.  All of the
266*355d67fcSMatthew Dillon   input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
267*355d67fcSMatthew Dillon   This completes the current deflate block and follows it with an empty fixed
268*355d67fcSMatthew Dillon   codes block that is 10 bits long.  This assures that enough bytes are output
269*355d67fcSMatthew Dillon   in order for the decompressor to finish the block before the empty fixed code
270*355d67fcSMatthew Dillon   block.
271*355d67fcSMatthew Dillon 
272*355d67fcSMatthew Dillon     If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
273*355d67fcSMatthew Dillon   for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
274*355d67fcSMatthew Dillon   seven bits of the current block are held to be written as the next byte after
275*355d67fcSMatthew Dillon   the next deflate block is completed.  In this case, the decompressor may not
276*355d67fcSMatthew Dillon   be provided enough bits at this point in order to complete decompression of
277*355d67fcSMatthew Dillon   the data provided so far to the compressor.  It may need to wait for the next
278*355d67fcSMatthew Dillon   block to be emitted.  This is for advanced applications that need to control
279*355d67fcSMatthew Dillon   the emission of deflate blocks.
280*355d67fcSMatthew Dillon 
281*355d67fcSMatthew Dillon     If flush is set to Z_FULL_FLUSH, all output is flushed as with
282*355d67fcSMatthew Dillon   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
283*355d67fcSMatthew Dillon   restart from this point if previous compressed data has been damaged or if
284*355d67fcSMatthew Dillon   random access is desired.  Using Z_FULL_FLUSH too often can seriously degrade
285*355d67fcSMatthew Dillon   compression.
286*355d67fcSMatthew Dillon 
287*355d67fcSMatthew Dillon     If deflate returns with avail_out == 0, this function must be called again
288*355d67fcSMatthew Dillon   with the same value of the flush parameter and more output space (updated
289*355d67fcSMatthew Dillon   avail_out), until the flush is complete (deflate returns with non-zero
290*355d67fcSMatthew Dillon   avail_out).  In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
291*355d67fcSMatthew Dillon   avail_out is greater than six to avoid repeated flush markers due to
292*355d67fcSMatthew Dillon   avail_out == 0 on return.
293*355d67fcSMatthew Dillon 
294*355d67fcSMatthew Dillon     If the parameter flush is set to Z_FINISH, pending input is processed,
295*355d67fcSMatthew Dillon   pending output is flushed and deflate returns with Z_STREAM_END if there was
296*355d67fcSMatthew Dillon   enough output space; if deflate returns with Z_OK, this function must be
297*355d67fcSMatthew Dillon   called again with Z_FINISH and more output space (updated avail_out) but no
298*355d67fcSMatthew Dillon   more input data, until it returns with Z_STREAM_END or an error.  After
299*355d67fcSMatthew Dillon   deflate has returned Z_STREAM_END, the only possible operations on the stream
300*355d67fcSMatthew Dillon   are deflateReset or deflateEnd.
301*355d67fcSMatthew Dillon 
302*355d67fcSMatthew Dillon     Z_FINISH can be used immediately after deflateInit if all the compression
303*355d67fcSMatthew Dillon   is to be done in a single step.  In this case, avail_out must be at least the
304*355d67fcSMatthew Dillon   value returned by deflateBound (see below).  Then deflate is guaranteed to
305*355d67fcSMatthew Dillon   return Z_STREAM_END.  If not enough output space is provided, deflate will
306*355d67fcSMatthew Dillon   not return Z_STREAM_END, and it must be called again as described above.
307*355d67fcSMatthew Dillon 
308*355d67fcSMatthew Dillon     deflate() sets strm->adler to the adler32 checksum of all input read
309*355d67fcSMatthew Dillon   so far (that is, total_in bytes).
310*355d67fcSMatthew Dillon 
311*355d67fcSMatthew Dillon     deflate() may update strm->data_type if it can make a good guess about
312*355d67fcSMatthew Dillon   the input data type (Z_BINARY or Z_TEXT).  In doubt, the data is considered
313*355d67fcSMatthew Dillon   binary.  This field is only for information purposes and does not affect the
314*355d67fcSMatthew Dillon   compression algorithm in any manner.
315*355d67fcSMatthew Dillon 
316*355d67fcSMatthew Dillon     deflate() returns Z_OK if some progress has been made (more input
317*355d67fcSMatthew Dillon   processed or more output produced), Z_STREAM_END if all input has been
318*355d67fcSMatthew Dillon   consumed and all output has been produced (only when flush is set to
319*355d67fcSMatthew Dillon   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
320*355d67fcSMatthew Dillon   if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
321*355d67fcSMatthew Dillon   (for example avail_in or avail_out was zero).  Note that Z_BUF_ERROR is not
322*355d67fcSMatthew Dillon   fatal, and deflate() can be called again with more input and more output
323*355d67fcSMatthew Dillon   space to continue compressing.
324*355d67fcSMatthew Dillon */
325*355d67fcSMatthew Dillon 
326*355d67fcSMatthew Dillon 
327*355d67fcSMatthew Dillon int deflateEnd(z_streamp strm);
328*355d67fcSMatthew Dillon /*
329*355d67fcSMatthew Dillon      All dynamically allocated data structures for this stream are freed.
330*355d67fcSMatthew Dillon    This function discards any unprocessed input and does not flush any pending
331*355d67fcSMatthew Dillon    output.
332*355d67fcSMatthew Dillon 
333*355d67fcSMatthew Dillon      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
334*355d67fcSMatthew Dillon    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
335*355d67fcSMatthew Dillon    prematurely (some input or output was discarded).  In the error case, msg
336*355d67fcSMatthew Dillon    may be set but then points to a static string (which must not be
337*355d67fcSMatthew Dillon    deallocated).
338*355d67fcSMatthew Dillon */
339*355d67fcSMatthew Dillon 
340*355d67fcSMatthew Dillon int inflateInit(z_streamp strm);
341*355d67fcSMatthew Dillon 
342*355d67fcSMatthew Dillon /*
343*355d67fcSMatthew Dillon ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
344*355d67fcSMatthew Dillon 
345*355d67fcSMatthew Dillon      Initializes the internal stream state for decompression.  The fields
346*355d67fcSMatthew Dillon    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
347*355d67fcSMatthew Dillon    the caller.  If next_in is not Z_NULL and avail_in is large enough (the
348*355d67fcSMatthew Dillon    exact value depends on the compression method), inflateInit determines the
349*355d67fcSMatthew Dillon    compression method from the zlib header and allocates all data structures
350*355d67fcSMatthew Dillon    accordingly; otherwise the allocation will be deferred to the first call of
351*355d67fcSMatthew Dillon    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
352*355d67fcSMatthew Dillon    use default allocation functions.
353*355d67fcSMatthew Dillon 
354*355d67fcSMatthew Dillon      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
355*355d67fcSMatthew Dillon    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
356*355d67fcSMatthew Dillon    version assumed by the caller, or Z_STREAM_ERROR if the parameters are
357*355d67fcSMatthew Dillon    invalid, such as a null pointer to the structure.  msg is set to null if
358*355d67fcSMatthew Dillon    there is no error message.  inflateInit does not perform any decompression
359*355d67fcSMatthew Dillon    apart from possibly reading the zlib header if present: actual decompression
360*355d67fcSMatthew Dillon    will be done by inflate().  (So next_in and avail_in may be modified, but
361*355d67fcSMatthew Dillon    next_out and avail_out are unused and unchanged.) The current implementation
362*355d67fcSMatthew Dillon    of inflateInit() does not process any header information -- that is deferred
363*355d67fcSMatthew Dillon    until inflate() is called.
364*355d67fcSMatthew Dillon */
365*355d67fcSMatthew Dillon 
366*355d67fcSMatthew Dillon 
367*355d67fcSMatthew Dillon int inflate(z_streamp strm, int flush);
368*355d67fcSMatthew Dillon /*
369*355d67fcSMatthew Dillon     inflate decompresses as much data as possible, and stops when the input
370*355d67fcSMatthew Dillon   buffer becomes empty or the output buffer becomes full.  It may introduce
371*355d67fcSMatthew Dillon   some output latency (reading input without producing any output) except when
372*355d67fcSMatthew Dillon   forced to flush.
373*355d67fcSMatthew Dillon 
374*355d67fcSMatthew Dillon   The detailed semantics are as follows.  inflate performs one or both of the
375*355d67fcSMatthew Dillon   following actions:
376*355d67fcSMatthew Dillon 
377*355d67fcSMatthew Dillon   - Decompress more input starting at next_in and update next_in and avail_in
378*355d67fcSMatthew Dillon     accordingly.  If not all input can be processed (because there is not
379*355d67fcSMatthew Dillon     enough room in the output buffer), next_in is updated and processing will
380*355d67fcSMatthew Dillon     resume at this point for the next call of inflate().
381*355d67fcSMatthew Dillon 
382*355d67fcSMatthew Dillon   - Provide more output starting at next_out and update next_out and avail_out
383*355d67fcSMatthew Dillon     accordingly.  inflate() provides as much output as possible, until there is
384*355d67fcSMatthew Dillon     no more input data or no more space in the output buffer (see below about
385*355d67fcSMatthew Dillon     the flush parameter).
386*355d67fcSMatthew Dillon 
387*355d67fcSMatthew Dillon     Before the call of inflate(), the application should ensure that at least
388*355d67fcSMatthew Dillon   one of the actions is possible, by providing more input and/or consuming more
389*355d67fcSMatthew Dillon   output, and updating the next_* and avail_* values accordingly.  The
390*355d67fcSMatthew Dillon   application can consume the uncompressed output when it wants, for example
391*355d67fcSMatthew Dillon   when the output buffer is full (avail_out == 0), or after each call of
392*355d67fcSMatthew Dillon   inflate().  If inflate returns Z_OK and with zero avail_out, it must be
393*355d67fcSMatthew Dillon   called again after making room in the output buffer because there might be
394*355d67fcSMatthew Dillon   more output pending.
395*355d67fcSMatthew Dillon 
396*355d67fcSMatthew Dillon     The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
397*355d67fcSMatthew Dillon   Z_BLOCK, or Z_TREES.  Z_SYNC_FLUSH requests that inflate() flush as much
398*355d67fcSMatthew Dillon   output as possible to the output buffer.  Z_BLOCK requests that inflate()
399*355d67fcSMatthew Dillon   stop if and when it gets to the next deflate block boundary.  When decoding
400*355d67fcSMatthew Dillon   the zlib or gzip format, this will cause inflate() to return immediately
401*355d67fcSMatthew Dillon   after the header and before the first block.  When doing a raw inflate,
402*355d67fcSMatthew Dillon   inflate() will go ahead and process the first block, and will return when it
403*355d67fcSMatthew Dillon   gets to the end of that block, or when it runs out of data.
404*355d67fcSMatthew Dillon 
405*355d67fcSMatthew Dillon     The Z_BLOCK option assists in appending to or combining deflate streams.
406*355d67fcSMatthew Dillon   Also to assist in this, on return inflate() will set strm->data_type to the
407*355d67fcSMatthew Dillon   number of unused bits in the last byte taken from strm->next_in, plus 64 if
408*355d67fcSMatthew Dillon   inflate() is currently decoding the last block in the deflate stream, plus
409*355d67fcSMatthew Dillon   128 if inflate() returned immediately after decoding an end-of-block code or
410*355d67fcSMatthew Dillon   decoding the complete header up to just before the first byte of the deflate
411*355d67fcSMatthew Dillon   stream.  The end-of-block will not be indicated until all of the uncompressed
412*355d67fcSMatthew Dillon   data from that block has been written to strm->next_out.  The number of
413*355d67fcSMatthew Dillon   unused bits may in general be greater than seven, except when bit 7 of
414*355d67fcSMatthew Dillon   data_type is set, in which case the number of unused bits will be less than
415*355d67fcSMatthew Dillon   eight.  data_type is set as noted here every time inflate() returns for all
416*355d67fcSMatthew Dillon   flush options, and so can be used to determine the amount of currently
417*355d67fcSMatthew Dillon   consumed input in bits.
418*355d67fcSMatthew Dillon 
419*355d67fcSMatthew Dillon     The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
420*355d67fcSMatthew Dillon   end of each deflate block header is reached, before any actual data in that
421*355d67fcSMatthew Dillon   block is decoded.  This allows the caller to determine the length of the
422*355d67fcSMatthew Dillon   deflate block header for later use in random access within a deflate block.
423*355d67fcSMatthew Dillon   256 is added to the value of strm->data_type when inflate() returns
424*355d67fcSMatthew Dillon   immediately after reaching the end of the deflate block header.
425*355d67fcSMatthew Dillon 
426*355d67fcSMatthew Dillon     inflate() should normally be called until it returns Z_STREAM_END or an
427*355d67fcSMatthew Dillon   error.  However if all decompression is to be performed in a single step (a
428*355d67fcSMatthew Dillon   single call of inflate), the parameter flush should be set to Z_FINISH.  In
429*355d67fcSMatthew Dillon   this case all pending input is processed and all pending output is flushed;
430*355d67fcSMatthew Dillon   avail_out must be large enough to hold all of the uncompressed data for the
431*355d67fcSMatthew Dillon   operation to complete.  (The size of the uncompressed data may have been
432*355d67fcSMatthew Dillon   saved by the compressor for this purpose.) The use of Z_FINISH is not
433*355d67fcSMatthew Dillon   required to perform an inflation in one step.  However it may be used to
434*355d67fcSMatthew Dillon   inform inflate that a faster approach can be used for the single inflate()
435*355d67fcSMatthew Dillon   call.  Z_FINISH also informs inflate to not maintain a sliding window if the
436*355d67fcSMatthew Dillon   stream completes, which reduces inflate's memory footprint.  If the stream
437*355d67fcSMatthew Dillon   does not complete, either because not all of the stream is provided or not
438*355d67fcSMatthew Dillon   enough output space is provided, then a sliding window will be allocated and
439*355d67fcSMatthew Dillon   inflate() can be called again to continue the operation as if Z_NO_FLUSH had
440*355d67fcSMatthew Dillon   been used.
441*355d67fcSMatthew Dillon 
442*355d67fcSMatthew Dillon      In this implementation, inflate() always flushes as much output as
443*355d67fcSMatthew Dillon   possible to the output buffer, and always uses the faster approach on the
444*355d67fcSMatthew Dillon   first call.  So the effects of the flush parameter in this implementation are
445*355d67fcSMatthew Dillon   on the return value of inflate() as noted below, when inflate() returns early
446*355d67fcSMatthew Dillon   when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of
447*355d67fcSMatthew Dillon   memory for a sliding window when Z_FINISH is used.
448*355d67fcSMatthew Dillon 
449*355d67fcSMatthew Dillon      If a preset dictionary is needed after this call (see inflateSetDictionary
450*355d67fcSMatthew Dillon   below), inflate sets strm->adler to the Adler-32 checksum of the dictionary
451*355d67fcSMatthew Dillon   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
452*355d67fcSMatthew Dillon   strm->adler to the Adler-32 checksum of all output produced so far (that is,
453*355d67fcSMatthew Dillon   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
454*355d67fcSMatthew Dillon   below.  At the end of the stream, inflate() checks that its computed adler32
455*355d67fcSMatthew Dillon   checksum is equal to that saved by the compressor and returns Z_STREAM_END
456*355d67fcSMatthew Dillon   only if the checksum is correct.
457*355d67fcSMatthew Dillon 
458*355d67fcSMatthew Dillon     inflate() can decompress and check either zlib-wrapped or gzip-wrapped
459*355d67fcSMatthew Dillon   deflate data.  The header type is detected automatically, if requested when
460*355d67fcSMatthew Dillon   initializing with inflateInit2().  Any information contained in the gzip
461*355d67fcSMatthew Dillon   header is not retained, so applications that need that information should
462*355d67fcSMatthew Dillon   instead use raw inflate, see inflateInit2() below, or inflateBack() and
463*355d67fcSMatthew Dillon   perform their own processing of the gzip header and trailer.  When processing
464*355d67fcSMatthew Dillon   gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
465*355d67fcSMatthew Dillon   producted so far.  The CRC-32 is checked against the gzip trailer.
466*355d67fcSMatthew Dillon 
467*355d67fcSMatthew Dillon     inflate() returns Z_OK if some progress has been made (more input processed
468*355d67fcSMatthew Dillon   or more output produced), Z_STREAM_END if the end of the compressed data has
469*355d67fcSMatthew Dillon   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
470*355d67fcSMatthew Dillon   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
471*355d67fcSMatthew Dillon   corrupted (input stream not conforming to the zlib format or incorrect check
472*355d67fcSMatthew Dillon   value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
473*355d67fcSMatthew Dillon   next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
474*355d67fcSMatthew Dillon   Z_BUF_ERROR if no progress is possible or if there was not enough room in the
475*355d67fcSMatthew Dillon   output buffer when Z_FINISH is used.  Note that Z_BUF_ERROR is not fatal, and
476*355d67fcSMatthew Dillon   inflate() can be called again with more input and more output space to
477*355d67fcSMatthew Dillon   continue decompressing.  If Z_DATA_ERROR is returned, the application may
478*355d67fcSMatthew Dillon   then call inflateSync() to look for a good compression block if a partial
479*355d67fcSMatthew Dillon   recovery of the data is desired.
480*355d67fcSMatthew Dillon */
481*355d67fcSMatthew Dillon 
482*355d67fcSMatthew Dillon 
483*355d67fcSMatthew Dillon int inflateEnd(z_streamp strm);
484*355d67fcSMatthew Dillon /*
485*355d67fcSMatthew Dillon      All dynamically allocated data structures for this stream are freed.
486*355d67fcSMatthew Dillon    This function discards any unprocessed input and does not flush any pending
487*355d67fcSMatthew Dillon    output.
488*355d67fcSMatthew Dillon 
489*355d67fcSMatthew Dillon      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
490*355d67fcSMatthew Dillon    was inconsistent.  In the error case, msg may be set but then points to a
491*355d67fcSMatthew Dillon    static string (which must not be deallocated).
492*355d67fcSMatthew Dillon */
493*355d67fcSMatthew Dillon 
494*355d67fcSMatthew Dillon                         /* checksum functions */
495*355d67fcSMatthew Dillon 
496*355d67fcSMatthew Dillon /*
497*355d67fcSMatthew Dillon      These functions are not related to compression but are exported
498*355d67fcSMatthew Dillon    anyway because they might be useful in applications using the compression
499*355d67fcSMatthew Dillon    library.
500*355d67fcSMatthew Dillon */
501*355d67fcSMatthew Dillon 
502*355d67fcSMatthew Dillon uLong adler32(uLong adler, const Bytef *buf, uInt len);
503*355d67fcSMatthew Dillon /*
504*355d67fcSMatthew Dillon      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
505*355d67fcSMatthew Dillon    return the updated checksum.  If buf is Z_NULL, this function returns the
506*355d67fcSMatthew Dillon    required initial value for the checksum.
507*355d67fcSMatthew Dillon 
508*355d67fcSMatthew Dillon      An Adler-32 checksum is almost as reliable as a crc32_zlib but can be computed
509*355d67fcSMatthew Dillon    much faster.
510*355d67fcSMatthew Dillon 
511*355d67fcSMatthew Dillon    Usage example:
512*355d67fcSMatthew Dillon 
513*355d67fcSMatthew Dillon      uLong adler = adler32(0L, Z_NULL, 0);
514*355d67fcSMatthew Dillon 
515*355d67fcSMatthew Dillon      while (read_buffer(buffer, length) != EOF) {
516*355d67fcSMatthew Dillon        adler = adler32(adler, buffer, length);
517*355d67fcSMatthew Dillon      }
518*355d67fcSMatthew Dillon      if (adler != original_adler) error();
519*355d67fcSMatthew Dillon */
520*355d67fcSMatthew Dillon 
521*355d67fcSMatthew Dillon                         /* various hacks, don't look :) */
522*355d67fcSMatthew Dillon 
523*355d67fcSMatthew Dillon /* deflateInit and inflateInit are macros to allow checking the zlib version
524*355d67fcSMatthew Dillon  * and the compiler's view of z_stream:
525*355d67fcSMatthew Dillon  */
526*355d67fcSMatthew Dillon int deflateInit_(z_streamp strm, int level,
527*355d67fcSMatthew Dillon                                      const char *version, int stream_size);
528*355d67fcSMatthew Dillon int inflateInit_(z_streamp strm,
529*355d67fcSMatthew Dillon                                      const char *version, int stream_size);
530*355d67fcSMatthew Dillon 
531*355d67fcSMatthew Dillon #define deflateInit(strm, level) \
532*355d67fcSMatthew Dillon         deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
533*355d67fcSMatthew Dillon #define inflateInit(strm) \
534*355d67fcSMatthew Dillon         inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
535*355d67fcSMatthew Dillon #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
536*355d67fcSMatthew Dillon         deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
537*355d67fcSMatthew Dillon                       (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
538*355d67fcSMatthew Dillon #define inflateInit2(strm, windowBits) \
539*355d67fcSMatthew Dillon         inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
540*355d67fcSMatthew Dillon                       (int)sizeof(z_stream))
541*355d67fcSMatthew Dillon 
542*355d67fcSMatthew Dillon /* hack for buggy compilers */
543*355d67fcSMatthew Dillon #if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
544*355d67fcSMatthew Dillon     struct internal_state {int dummy;};
545*355d67fcSMatthew Dillon #endif
546*355d67fcSMatthew Dillon 
547*355d67fcSMatthew Dillon #ifdef __cplusplus
548*355d67fcSMatthew Dillon }
549*355d67fcSMatthew Dillon #endif
550*355d67fcSMatthew Dillon 
551*355d67fcSMatthew Dillon #endif /* ZLIB_H */
552