3
/* zlib.h -- interface of the 'zlib' general purpose compression library
4
version 1.0.4, Jul 24th, 1996.
6
Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
8
This software is provided 'as-is', without any express or implied
9
warranty. In no event will the authors be held liable for any damages
10
arising from the use of this software.
12
Permission is granted to anyone to use this software for any purpose,
13
including commercial applications, and to alter it and redistribute it
14
freely, subject to the following restrictions:
16
1. The origin of this software must not be misrepresented; you must not
17
claim that you wrote the original software. If you use this software
18
in a product, an acknowledgment in the product documentation would be
19
appreciated but is not required.
20
2. Altered source versions must be plainly marked as such, and must not be
21
misrepresented as being the original software.
22
3. This notice may not be removed or altered from any source distribution.
24
Jean-loup Gailly Mark Adler
25
gzip@prep.ai.mit.edu madler@alumni.caltech.edu
28
The data format used by the zlib library is described by RFCs (Request for
29
Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
30
(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
42
#define ZLIB_VERSION "1.0.4"
45
The 'zlib' compression library provides in-memory compression and
46
decompression functions, including integrity checks of the uncompressed
47
data. This version of the library supports only one compression method
48
(deflation) but other algorithms may be added later and will have the same
51
For compression the application must provide the output buffer and
52
may optionally provide the input buffer for optimization. For decompression,
53
the application must provide the input buffer and may optionally provide
54
the output buffer for optimization.
56
Compression can be done in a single step if the buffers are large
57
enough (for example if an input file is mmap'ed), or can be done by
58
repeated calls of the compression function. In the latter case, the
59
application must provide more input and/or consume the output
60
(providing more output space) before each call.
62
The library does not install any signal handler. It is recommended to
63
add at least a handler for SIGSEGV when decompressing; the library checks
64
the consistency of the input data whenever possible but may go nuts
65
for some forms of corrupted input.
68
typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
69
typedef void (*free_func) OF((voidpf opaque, voidpf address));
71
struct internal_state;
73
typedef struct z_stream_s {
74
Bytef *next_in; /* next input byte */
75
uInt avail_in; /* number of bytes available at next_in */
76
uLong total_in; /* total nb of input bytes read so far */
78
Bytef *next_out; /* next output byte should be put there */
79
uInt avail_out; /* remaining free space at next_out */
80
uLong total_out; /* total nb of bytes output so far */
82
char *msg; /* last error message, NULL if no error */
83
struct internal_state FAR *state; /* not visible by applications */
85
alloc_func zalloc; /* used to allocate the internal state */
86
free_func zfree; /* used to free the internal state */
87
voidpf opaque; /* private data object passed to zalloc and zfree */
89
int data_type; /* best guess about the data type: ascii or binary */
90
uLong adler; /* adler32 value of the uncompressed data */
91
uLong reserved; /* reserved for future use */
94
typedef z_stream FAR *z_streamp;
97
The application must update next_in and avail_in when avail_in has
98
dropped to zero. It must update next_out and avail_out when avail_out
99
has dropped to zero. The application must initialize zalloc, zfree and
100
opaque before calling the init function. All other fields are set by the
101
compression library and must not be updated by the application.
103
The opaque value provided by the application will be passed as the first
104
parameter for calls of zalloc and zfree. This can be useful for custom
105
memory management. The compression library attaches no meaning to the
108
zalloc must return Z_NULL if there is not enough memory for the object.
109
On 16-bit systems, the functions zalloc and zfree must be able to allocate
110
exactly 65536 bytes, but will not be required to allocate more than this
111
if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
112
pointers returned by zalloc for objects of exactly 65536 bytes *must*
113
have their offset normalized to zero. The default allocation function
114
provided by this library ensures this (see zutil.c). To reduce memory
115
requirements and avoid any allocation of 64K objects, at the expense of
116
compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
118
The fields total_in and total_out can be used for statistics or
119
progress reports. After compression, total_in holds the total size of
120
the uncompressed data and may be saved for use in the decompressor
121
(particularly if the decompressor wants to decompress everything in
128
#define Z_PARTIAL_FLUSH 1
129
#define Z_SYNC_FLUSH 2
130
#define Z_FULL_FLUSH 3
132
/* Allowed flush values; see deflate() below for details */
135
#define Z_STREAM_END 1
136
#define Z_NEED_DICT 2
138
#define Z_STREAM_ERROR (-2)
139
#define Z_DATA_ERROR (-3)
140
#define Z_MEM_ERROR (-4)
141
#define Z_BUF_ERROR (-5)
142
#define Z_VERSION_ERROR (-6)
143
/* Return codes for the compression/decompression functions. Negative
144
* values are errors, positive values are used for special but normal events.
147
#define Z_NO_COMPRESSION 0
148
#define Z_BEST_SPEED 1
149
#define Z_BEST_COMPRESSION 9
150
#define Z_DEFAULT_COMPRESSION (-1)
151
/* compression levels */
154
#define Z_HUFFMAN_ONLY 2
155
#define Z_DEFAULT_STRATEGY 0
156
/* compression strategy; see deflateInit2() below for details */
161
/* Possible values of the data_type field */
164
/* The deflate compression method (the only one supported in this version) */
166
#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
168
#define zlib_version zlibVersion()
169
/* for compatibility with versions < 1.0.2 */
171
/* basic functions */
173
extern const char * EXPORT zlibVersion OF((void));
174
/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
175
If the first character differs, the library code actually used is
176
not compatible with the zlib.h header file used by the application.
177
This check is automatically made by deflateInit and inflateInit.
181
extern int EXPORT deflateInit OF((z_streamp strm, int level));
183
Initializes the internal stream state for compression. The fields
184
zalloc, zfree and opaque must be initialized before by the caller.
185
If zalloc and zfree are set to Z_NULL, deflateInit updates them to
186
use default allocation functions.
188
The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
189
1 gives best speed, 9 gives best compression, 0 gives no compression at
190
all (the input data is simply copied a block at a time).
191
Z_DEFAULT_COMPRESSION requests a default compromise between speed and
192
compression (currently equivalent to level 6).
194
deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
195
enough memory, Z_STREAM_ERROR if level is not a valid compression level,
196
Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
197
with the version assumed by the caller (ZLIB_VERSION).
198
msg is set to null if there is no error message. deflateInit does not
199
perform any compression: this will be done by deflate().
203
extern int EXPORT deflate OF((z_streamp strm, int flush));
205
Performs one or both of the following actions:
207
- Compress more input starting at next_in and update next_in and avail_in
208
accordingly. If not all input can be processed (because there is not
209
enough room in the output buffer), next_in and avail_in are updated and
210
processing will resume at this point for the next call of deflate().
212
- Provide more output starting at next_out and update next_out and avail_out
213
accordingly. This action is forced if the parameter flush is non zero.
214
Forcing flush frequently degrades the compression ratio, so this parameter
215
should be set only when necessary (in interactive applications).
216
Some output may be provided even if flush is not set.
218
Before the call of deflate(), the application should ensure that at least
219
one of the actions is possible, by providing more input and/or consuming
220
more output, and updating avail_in or avail_out accordingly; avail_out
221
should never be zero before the call. The application can consume the
222
compressed output when it wants, for example when the output buffer is full
223
(avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
224
and with zero avail_out, it must be called again after making room in the
225
output buffer because there might be more output pending.
227
If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression
228
block is terminated and flushed to the output buffer so that the
229
decompressor can get all input data available so far. For method 9, a future
230
variant on method 8, the current block will be flushed but not terminated.
231
Z_SYNC_FLUSH has the same effect as partial flush except that the compressed
232
output is byte aligned (the compressor can clear its internal bit buffer)
233
and the current block is always terminated; this can be useful if the
234
compressor has to be restarted from scratch after an interruption (in which
235
case the internal state of the compressor may be lost).
236
If flush is set to Z_FULL_FLUSH, the compression block is terminated, a
237
special marker is output and the compression dictionary is discarded; this
238
is useful to allow the decompressor to synchronize if one compressed block
239
has been damaged (see inflateSync below). Flushing degrades compression and
240
so should be used only when necessary. Using Z_FULL_FLUSH too often can
241
seriously degrade the compression. If deflate returns with avail_out == 0,
242
this function must be called again with the same value of the flush
243
parameter and more output space (updated avail_out), until the flush is
244
complete (deflate returns with non-zero avail_out).
246
If the parameter flush is set to Z_FINISH, pending input is processed,
247
pending output is flushed and deflate returns with Z_STREAM_END if there
248
was enough output space; if deflate returns with Z_OK, this function must be
249
called again with Z_FINISH and more output space (updated avail_out) but no
250
more input data, until it returns with Z_STREAM_END or an error. After
251
deflate has returned Z_STREAM_END, the only possible operations on the
252
stream are deflateReset or deflateEnd.
254
Z_FINISH can be used immediately after deflateInit if all the compression
255
is to be done in a single step. In this case, avail_out must be at least
256
0.1% larger than avail_in plus 12 bytes. If deflate does not return
257
Z_STREAM_END, then it must be called again as described above.
259
deflate() may update data_type if it can make a good guess about
260
the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
261
binary. This field is only for information purposes and does not affect
262
the compression algorithm in any manner.
264
deflate() returns Z_OK if some progress has been made (more input
265
processed or more output produced), Z_STREAM_END if all input has been
266
consumed and all output has been produced (only when flush is set to
267
Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
268
if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible.
272
extern int EXPORT deflateEnd OF((z_streamp strm));
274
All dynamically allocated data structures for this stream are freed.
275
This function discards any unprocessed input and does not flush any
278
deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
279
stream state was inconsistent, Z_DATA_ERROR if the stream was freed
280
prematurely (some input or output was discarded). In the error case,
281
msg may be set but then points to a static string (which must not be
287
extern int EXPORT inflateInit OF((z_streamp strm));
289
Initializes the internal stream state for decompression. The fields
290
zalloc, zfree and opaque must be initialized before by the caller. If
291
zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
292
allocation functions.
294
inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
295
enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
296
with the version assumed by the caller. msg is set to null if there is no
297
error message. inflateInit does not perform any decompression: this will be
302
extern int EXPORT inflate OF((z_streamp strm, int flush));
304
Performs one or both of the following actions:
306
- Decompress more input starting at next_in and update next_in and avail_in
307
accordingly. If not all input can be processed (because there is not
308
enough room in the output buffer), next_in is updated and processing
309
will resume at this point for the next call of inflate().
311
- Provide more output starting at next_out and update next_out and avail_out
312
accordingly. inflate() provides as much output as possible, until there
313
is no more input data or no more space in the output buffer (see below
314
about the flush parameter).
316
Before the call of inflate(), the application should ensure that at least
317
one of the actions is possible, by providing more input and/or consuming
318
more output, and updating the next_* and avail_* values accordingly.
319
The application can consume the uncompressed output when it wants, for
320
example when the output buffer is full (avail_out == 0), or after each
321
call of inflate(). If inflate returns Z_OK and with zero avail_out, it
322
must be called again after making room in the output buffer because there
323
might be more output pending.
325
If the parameter flush is set to Z_PARTIAL_FLUSH, inflate flushes as much
326
output as possible to the output buffer. The flushing behavior of inflate is
327
not specified for values of the flush parameter other than Z_PARTIAL_FLUSH
328
and Z_FINISH, but the current implementation actually flushes as much output
331
inflate() should normally be called until it returns Z_STREAM_END or an
332
error. However if all decompression is to be performed in a single step
333
(a single call of inflate), the parameter flush should be set to
334
Z_FINISH. In this case all pending input is processed and all pending
335
output is flushed; avail_out must be large enough to hold all the
336
uncompressed data. (The size of the uncompressed data may have been saved
337
by the compressor for this purpose.) The next operation on this stream must
338
be inflateEnd to deallocate the decompression state. The use of Z_FINISH
339
is never required, but can be used to inform inflate that a faster routine
340
may be used for the single inflate() call.
342
inflate() returns Z_OK if some progress has been made (more input
343
processed or more output produced), Z_STREAM_END if the end of the
344
compressed data has been reached and all uncompressed output has been
345
produced, Z_NEED_DICT if a preset dictionary is needed at this point (see
346
inflateSetDictionary below), Z_DATA_ERROR if the input data was corrupted,
347
Z_STREAM_ERROR if the stream structure was inconsistent (for example if
348
next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
349
Z_BUF_ERROR if no progress is possible or if there was not enough room in
350
the output buffer when Z_FINISH is used. In the Z_DATA_ERROR case, the
351
application may then call inflateSync to look for a good compression block.
352
In the Z_NEED_DICT case, strm->adler is set to the Adler32 value of the
353
dictionary chosen by the compressor.
357
extern int EXPORT inflateEnd OF((z_streamp strm));
359
All dynamically allocated data structures for this stream are freed.
360
This function discards any unprocessed input and does not flush any
363
inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
364
was inconsistent. In the error case, msg may be set but then points to a
365
static string (which must not be deallocated).
368
/* Advanced functions */
371
The following functions are needed only in some special applications.
375
extern int EXPORT deflateInit2 OF((z_streamp strm,
382
This is another version of deflateInit with more compression options. The
383
fields next_in, zalloc, zfree and opaque must be initialized before by
386
The method parameter is the compression method. It must be Z_DEFLATED in
387
this version of the library. (Method 9 will allow a 64K history buffer and
388
partial block flushes.)
390
The windowBits parameter is the base two logarithm of the window size
391
(the size of the history buffer). It should be in the range 8..15 for this
392
version of the library (the value 16 will be allowed for method 9). Larger
393
values of this parameter result in better compression at the expense of
394
memory usage. The default value is 15 if deflateInit is used instead.
396
The memLevel parameter specifies how much memory should be allocated
397
for the internal compression state. memLevel=1 uses minimum memory but
398
is slow and reduces compression ratio; memLevel=9 uses maximum memory
399
for optimal speed. The default value is 8. See zconf.h for total memory
400
usage as a function of windowBits and memLevel.
402
The strategy parameter is used to tune the compression algorithm. Use the
403
value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
404
filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
405
string match). Filtered data consists mostly of small values with a
406
somewhat random distribution. In this case, the compression algorithm is
407
tuned to compress them better. The effect of Z_FILTERED is to force more
408
Huffman coding and less string matching; it is somewhat intermediate
409
between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
410
the compression ratio but not the correctness of the compressed output even
411
if it is not set appropriately.
413
If next_in is not null, the library will use this buffer to hold also
414
some history information; the buffer must either hold the entire input
415
data, or have at least 1<<(windowBits+1) bytes and be writable. If next_in
416
is null, the library will allocate its own history buffer (and leave next_in
417
null). next_out need not be provided here but must be provided by the
418
application for the next call of deflate().
420
If the history buffer is provided by the application, next_in must
421
must never be changed by the application since the compressor maintains
422
information inside this buffer from call to call; the application
423
must provide more input only by increasing avail_in. next_in is always
424
reset by the library in this case.
426
deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
427
not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
428
an invalid method). msg is set to null if there is no error message.
429
deflateInit2 does not perform any compression: this will be done by
433
extern int EXPORT deflateSetDictionary OF((z_streamp strm,
434
const Bytef *dictionary,
437
Initializes the compression dictionary (history buffer) from the given
438
byte sequence without producing any compressed output. This function must
439
be called immediately after deflateInit or deflateInit2, before any call
440
of deflate. The compressor and decompressor must use exactly the same
441
dictionary (see inflateSetDictionary).
442
The dictionary should consist of strings (byte sequences) that are likely
443
to be encountered later in the data to be compressed, with the most commonly
444
used strings preferably put towards the end of the dictionary. Using a
445
dictionary is most useful when the data to be compressed is short and
446
can be predicted with good accuracy; the data can then be compressed better
447
than with the default empty dictionary. In this version of the library,
448
only the last 32K bytes of the dictionary are used.
449
Upon return of this function, strm->adler is set to the Adler32 value
450
of the dictionary; the decompressor may later use this value to determine
451
which dictionary has been used by the compressor. (The Adler32 value
452
applies to the whole dictionary even if only a subset of the dictionary is
453
actually used by the compressor.)
455
deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
456
parameter is invalid (such as NULL dictionary) or the stream state
457
is inconsistent (for example if deflate has already been called for this
458
stream). deflateSetDictionary does not perform any compression: this will
459
be done by deflate().
462
extern int EXPORT deflateCopy OF((z_streamp dest,
465
Sets the destination stream as a complete copy of the source stream. If
466
the source stream is using an application-supplied history buffer, a new
467
buffer is allocated for the destination stream. The compressed output
468
buffer is always application-supplied. It's the responsibility of the
469
application to provide the correct values of next_out and avail_out for the
470
next call of deflate.
472
This function can be useful when several compression strategies will be
473
tried, for example when there are several ways of pre-processing the input
474
data with a filter. The streams that will be discarded should then be freed
475
by calling deflateEnd. Note that deflateCopy duplicates the internal
476
compression state which can be quite large, so this strategy is slow and
477
can consume lots of memory.
479
deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
480
enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
481
(such as zalloc being NULL). msg is left unchanged in both source and
485
extern int EXPORT deflateReset OF((z_streamp strm));
487
This function is equivalent to deflateEnd followed by deflateInit,
488
but does not free and reallocate all the internal compression state.
489
The stream will keep the same compression level and any other attributes
490
that may have been set by deflateInit2.
492
deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
493
stream state was inconsistent (such as zalloc or state being NULL).
496
extern int EXPORT deflateParams OF((z_streamp strm, int level, int strategy));
498
Dynamically update the compression level and compression strategy.
499
This can be used to switch between compression and straight copy of
500
the input data, or to switch to a different kind of input data requiring
501
a different strategy. If the compression level is changed, the input
502
available so far is compressed with the old level (and may be flushed);
503
the new level will take effect only at the next call of deflate().
505
Before the call of deflateParams, the stream state must be set as for
506
a call of deflate(), since the currently available input may have to
507
be compressed and flushed. In particular, strm->avail_out must be non-zero.
509
deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
510
stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
511
if strm->avail_out was zero.
515
extern int EXPORT inflateInit2 OF((z_streamp strm,
518
This is another version of inflateInit with more compression options. The
519
fields next_out, zalloc, zfree and opaque must be initialized before by
522
The windowBits parameter is the base two logarithm of the maximum window
523
size (the size of the history buffer). It should be in the range 8..15 for
524
this version of the library (the value 16 will be allowed soon). The
525
default value is 15 if inflateInit is used instead. If a compressed stream
526
with a larger window size is given as input, inflate() will return with
527
the error code Z_DATA_ERROR instead of trying to allocate a larger window.
529
If next_out is not null, the library will use this buffer for the history
530
buffer; the buffer must either be large enough to hold the entire output
531
data, or have at least 1<<windowBits bytes. If next_out is null, the
532
library will allocate its own buffer (and leave next_out null). next_in
533
need not be provided here but must be provided by the application for the
534
next call of inflate().
536
If the history buffer is provided by the application, next_out must
537
never be changed by the application since the decompressor maintains
538
history information inside this buffer from call to call; the application
539
can only reset next_out to the beginning of the history buffer when
540
avail_out is zero and all output has been consumed.
542
inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
543
not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
544
windowBits < 8). msg is set to null if there is no error message.
545
inflateInit2 does not perform any decompression: this will be done by
549
extern int EXPORT inflateSetDictionary OF((z_streamp strm,
550
const Bytef *dictionary,
553
Initializes the decompression dictionary (history buffer) from the given
554
uncompressed byte sequence. This function must be called immediately after
555
a call of inflate if this call returned Z_NEED_DICT. The dictionary chosen
556
by the compressor can be determined from the Adler32 value returned by this
557
call of inflate. The compressor and decompressor must use exactly the same
558
dictionary (see deflateSetDictionary).
560
inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
561
parameter is invalid (such as NULL dictionary) or the stream state is
562
inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
563
expected one (incorrect Adler32 value). inflateSetDictionary does not
564
perform any decompression: this will be done by subsequent calls of
568
extern int EXPORT inflateSync OF((z_streamp strm));
570
Skips invalid compressed data until the special marker (see deflate()
571
above) can be found, or until all available input is skipped. No output
574
inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR
575
if no more input was provided, Z_DATA_ERROR if no marker has been found,
576
or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
577
case, the application may save the current current value of total_in which
578
indicates where valid compressed data was found. In the error case, the
579
application may repeatedly call inflateSync, providing more input each time,
580
until success or end of the input data.
583
extern int EXPORT inflateReset OF((z_streamp strm));
585
This function is equivalent to inflateEnd followed by inflateInit,
586
but does not free and reallocate all the internal decompression state.
587
The stream will keep attributes that may have been set by inflateInit2.
589
inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
590
stream state was inconsistent (such as zalloc or state being NULL).
594
/* utility functions */
597
The following utility functions are implemented on top of the
598
basic stream-oriented functions. To simplify the interface, some
599
default options are assumed (compression level, window size,
600
standard memory allocation functions). The source code of these
601
utility functions can easily be modified if you need special options.
604
extern int EXPORT compress OF((Bytef *dest, uLongf *destLen,
605
const Bytef *source, uLong sourceLen));
607
Compresses the source buffer into the destination buffer. sourceLen is
608
the byte length of the source buffer. Upon entry, destLen is the total
609
size of the destination buffer, which must be at least 0.1% larger than
610
sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
612
This function can be used to compress a whole file at once if the
613
input file is mmap'ed.
614
compress returns Z_OK if success, Z_MEM_ERROR if there was not
615
enough memory, Z_BUF_ERROR if there was not enough room in the output
619
extern int EXPORT uncompress OF((Bytef *dest, uLongf *destLen,
620
const Bytef *source, uLong sourceLen));
622
Decompresses the source buffer into the destination buffer. sourceLen is
623
the byte length of the source buffer. Upon entry, destLen is the total
624
size of the destination buffer, which must be large enough to hold the
625
entire uncompressed data. (The size of the uncompressed data must have
626
been saved previously by the compressor and transmitted to the decompressor
627
by some mechanism outside the scope of this compression library.)
628
Upon exit, destLen is the actual size of the compressed buffer.
629
This function can be used to decompress a whole file at once if the
630
input file is mmap'ed.
632
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
633
enough memory, Z_BUF_ERROR if there was not enough room in the output
634
buffer, or Z_DATA_ERROR if the input data was corrupted.
638
typedef voidp gzFile;
640
extern gzFile EXPORT gzopen OF((const char *path, const char *mode));
642
Opens a gzip (.gz) file for reading or writing. The mode parameter
643
is as in fopen ("rb" or "wb") but can also include a compression level
644
("wb9"). gzopen can be used to read a file which is not in gzip format;
645
in this case gzread will directly read from the file without decompression.
646
gzopen returns NULL if the file could not be opened or if there was
647
insufficient memory to allocate the (de)compression state; errno
648
can be checked to distinguish the two cases (if errno is zero, the
649
zlib error is Z_MEM_ERROR).
652
extern gzFile EXPORT gzdopen OF((int fd, const char *mode));
654
gzdopen() associates a gzFile with the file descriptor fd. File
655
descriptors are obtained from calls like open, dup, creat, pipe or
656
fileno (in the file has been previously opened with fopen).
657
The mode parameter is as in gzopen.
658
The next call of gzclose on the returned gzFile will also close the
659
file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
660
descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
661
gzdopen returns NULL if there was insufficient memory to allocate
662
the (de)compression state.
665
extern int EXPORT gzread OF((gzFile file, voidp buf, unsigned len));
667
Reads the given number of uncompressed bytes from the compressed file.
668
If the input file was not in gzip format, gzread copies the given number
669
of bytes into the buffer.
670
gzread returns the number of uncompressed bytes actually read (0 for
671
end of file, -1 for error). */
673
extern int EXPORT gzwrite OF((gzFile file, const voidp buf, unsigned len));
675
Writes the given number of uncompressed bytes into the compressed file.
676
gzwrite returns the number of uncompressed bytes actually written
677
(0 in case of error).
680
extern int EXPORT gzflush OF((gzFile file, int flush));
682
Flushes all pending output into the compressed file. The parameter
683
flush is as in the deflate() function. The return value is the zlib
684
error number (see function gzerror below). gzflush returns Z_OK if
685
the flush parameter is Z_FINISH and all output could be flushed.
686
gzflush should be called only when strictly necessary because it can
690
extern int EXPORT gzclose OF((gzFile file));
692
Flushes all pending output if necessary, closes the compressed file
693
and deallocates all the (de)compression state. The return value is the zlib
694
error number (see function gzerror below).
697
extern const char * EXPORT gzerror OF((gzFile file, int *errnum));
699
Returns the error message for the last error which occurred on the
700
given compressed file. errnum is set to zlib error number. If an
701
error occurred in the file system and not in the compression library,
702
errnum is set to Z_ERRNO and the application may consult errno
703
to get the exact error code.
706
/* checksum functions */
709
These functions are not related to compression but are exported
710
anyway because they might be useful in applications using the
714
extern uLong EXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
717
Update a running Adler-32 checksum with the bytes buf[0..len-1] and
718
return the updated checksum. If buf is NULL, this function returns
719
the required initial value for the checksum.
720
An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
721
much faster. Usage example:
723
uLong adler = adler32(0L, Z_NULL, 0);
725
while (read_buffer(buffer, length) != EOF) {
726
adler = adler32(adler, buffer, length);
728
if (adler != original_adler) error();
731
extern uLong EXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
733
Update a running crc with the bytes buf[0..len-1] and return the updated
734
crc. If buf is NULL, this function returns the required initial value
735
for the crc. Pre- and post-conditioning (one's complement) is performed
736
within this function so it shouldn't be done by the application.
739
uLong crc = crc32(0L, Z_NULL, 0);
741
while (read_buffer(buffer, length) != EOF) {
742
crc = crc32(crc, buffer, length);
744
if (crc != original_crc) error();
748
/* various hacks, don't look :) */
750
/* deflateInit and inflateInit are macros to allow checking the zlib version
751
* and the compiler's view of z_stream:
753
extern int EXPORT deflateInit_ OF((z_streamp strm, int level,
754
const char *version, int stream_size));
755
extern int EXPORT inflateInit_ OF((z_streamp strm,
756
const char *version, int stream_size));
757
extern int EXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
758
int windowBits, int memLevel, int strategy,
759
const char *version, int stream_size));
760
extern int EXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
761
const char *version, int stream_size));
762
#define deflateInit(strm, level) \
763
deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
764
#define inflateInit(strm) \
765
inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
766
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
767
deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
768
(strategy), ZLIB_VERSION, sizeof(z_stream))
769
#define inflateInit2(strm, windowBits) \
770
inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
772
#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
773
struct internal_state {int dummy;}; /* hack for buggy compilers */
776
uLongf *get_crc_table OF((void)); /* can be used by asm versions of crc32() */