1 /* $NetBSD: zlib.h,v 1.13 2009/03/14 14:46:10 dsl Exp $ */
3 /* zlib.h -- interface of the 'zlib' general purpose compression library
4 version 1.1.4, March 11th, 2002
6 Copyright (C) 1995-2002 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 jloup@gzip.org 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).
37 #include <sys/cdefs.h>
41 /* zconf.h -- configuration of the zlib compression library
42 * Copyright (C) 1995-2002 Jean-loup Gailly.
43 * For conditions of distribution and use, see copyright notice in zlib.h
46 /* @(#) $Id: zlib.h,v 1.14 2009/03/25 01:26:12 darran Exp $ */
52 * Warning: This file pollutes the user's namespace with:
53 * Byte Bytef EXPORT FAR OF STDC
54 * charf intf uInt uIntf uLong uLonf
55 * Programs using this library appear to expect those...
58 #include <sys/types.h>
61 * If you *really* need a unique prefix for all types and library functions,
62 * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
65 # define deflateInit_ z_deflateInit_
66 # define deflate z_deflate
67 # define deflateEnd z_deflateEnd
68 # define inflateInit_ z_inflateInit_
69 # define inflate z_inflate
70 # define inflateEnd z_inflateEnd
71 # define deflateInit2_ z_deflateInit2_
72 # define deflateSetDictionary z_deflateSetDictionary
73 # define deflateCopy z_deflateCopy
74 # define deflateReset z_deflateReset
75 # define deflateParams z_deflateParams
76 # define inflateInit2_ z_inflateInit2_
77 # define inflateSetDictionary z_inflateSetDictionary
78 # define inflateSync z_inflateSync
79 # define inflateSyncPoint z_inflateSyncPoint
80 # define inflateReset z_inflateReset
81 # define compress z_compress
82 # define compress2 z_compress2
83 # define uncompress z_uncompress
84 # define adler32 z_adler32
85 # define crc32 z_crc32
86 # define get_crc_table z_get_crc_table
90 # define uLong z_uLong
91 # define Bytef z_Bytef
92 # define charf z_charf
94 # define uIntf z_uIntf
95 # define uLongf z_uLongf
96 # define voidpf z_voidpf
97 # define voidp z_voidp
101 /* Don't be alarmed; this just means we have at least 32-bits */
106 * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
107 * than 64k bytes at a time (needed on systems with 16-bit int).
109 #if defined(MSDOS) && !defined(__32BIT__)
114 /* XXX: Are there machines where we should define this? m68k? */
115 # define UNALIGNED_OK
118 #if (defined(__STDC__) || defined(__cplusplus)) && !defined(STDC)
119 /* XXX: Look out - this is used in zutil.h and elsewhere... */
122 #if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
134 /* Some Mac compilers merge all .h files incorrectly: */
135 #if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
136 # define NO_DUMMY_DECL
139 /* Old Borland C incorrectly complains about missing returns: */
140 #if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
141 # define NEED_DUMMY_RETURN
145 /* Maximum value for memLevel in deflateInit2 */
146 #ifndef MAX_MEM_LEVEL
148 # define MAX_MEM_LEVEL 8
150 # define MAX_MEM_LEVEL 9
154 /* Maximum value for windowBits in deflateInit2 and inflateInit2.
155 * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
156 * created by gzip. (Files created by minigzip can still be extracted by
160 # define MAX_WBITS 15 /* 32K LZ77 window */
163 /* The memory requirements for deflate are (in bytes):
164 (1 << (windowBits+2)) + (1 << (memLevel+9))
165 that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
166 plus a few kilobytes for small objects. For example, if you want to reduce
167 the default memory requirements from 256K to 128K, compile with
168 make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
169 Of course this will generally degrade compression (there's no free lunch).
171 The memory requirements for inflate are (in bytes) 1 << windowBits
172 that is, 32K for windowBits=15 (default value) plus a few kilobytes
176 /* Type declarations */
178 #ifndef __P /* function prototypes */
180 # define __P(args) args
182 # define __P(args) ()
186 /* The following definitions for FAR are needed only for MSDOS mixed
187 * model programming (small or medium model with some far allocations).
188 * This was tested only with MSC; for other MSDOS compilers you may have
189 * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
190 * just define FAR to be empty.
192 #if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
193 /* MSC small or medium model */
194 # define SMALL_MEDIUM
201 #if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
203 # define SMALL_MEDIUM
208 /* Compile with -DZLIB_DLL for Windows DLL support */
209 #if defined(ZLIB_DLL)
210 # if defined(_WINDOWS) || defined(WINDOWS)
214 # include <windows.h>
215 # define ZEXPORT WINAPI
217 # define ZEXPORTVA WINAPIV
219 # define ZEXPORTVA FAR _cdecl _export
222 # if defined (__BORLANDC__)
223 # if (__BORLANDC__ >= 0x0500) && defined (WIN32)
224 # include <windows.h>
225 # define ZEXPORT __declspec(dllexport) WINAPI
226 # define ZEXPORTRVA __declspec(dllexport) WINAPIV
228 # if defined (_Windows) && defined (__DLL__)
229 # define ZEXPORT _export
230 # define ZEXPORTVA _export
236 #if defined (__BEOS__)
237 # if defined (ZLIB_DLL)
238 # define ZEXTERN extern __declspec(dllexport)
240 # define ZEXTERN extern __declspec(dllimport)
251 # define ZEXTERN extern
258 #if !defined(MACOS) && !defined(TARGET_OS_MAC)
259 typedef unsigned char Byte
; /* 8 bits */
261 typedef unsigned int uInt
; /* 16 bits or more */
262 typedef unsigned long uLong
; /* 32 bits or more */
265 /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
266 # define Bytef Byte FAR
268 typedef Byte FAR Bytef
;
270 typedef char FAR charf
;
271 typedef int FAR intf
;
272 typedef uInt FAR uIntf
;
273 typedef uLong FAR uLongf
;
276 typedef void FAR
*voidpf
;
279 typedef Byte FAR
*voidpf
;
283 #if (defined(HAVE_UNISTD_H) || defined(__NetBSD__)) && !defined(_KERNEL)
284 # include <sys/types.h> /* for off_t */
285 # include <unistd.h> /* for SEEK_* and off_t */
286 # define z_off_t off_t
289 # define SEEK_SET 0 /* Seek from beginning of file. */
290 # define SEEK_CUR 1 /* Seek from current position. */
291 # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
294 # define z_off_t long
297 /* MVS linker does not support external names larger than 8 bytes */
299 # pragma map(deflateInit_,"DEIN")
300 # pragma map(deflateInit2_,"DEIN2")
301 # pragma map(deflateEnd,"DEEND")
302 # pragma map(inflateInit_,"ININ")
303 # pragma map(inflateInit2_,"ININ2")
304 # pragma map(inflateEnd,"INEND")
305 # pragma map(inflateSync,"INSY")
306 # pragma map(inflateSetDictionary,"INSEDI")
307 # pragma map(inflate_blocks,"INBL")
308 # pragma map(inflate_blocks_new,"INBLNE")
309 # pragma map(inflate_blocks_free,"INBLFR")
310 # pragma map(inflate_blocks_reset,"INBLRE")
311 # pragma map(inflate_codes_free,"INCOFR")
312 # pragma map(inflate_codes,"INCO")
313 # pragma map(inflate_fast,"INFA")
314 # pragma map(inflate_flush,"INFLU")
315 # pragma map(inflate_mask,"INMA")
316 # pragma map(inflate_set_dictionary,"INSEDI2")
317 # pragma map(inflate_copyright,"INCOPY")
318 # pragma map(inflate_trees_bits,"INTRBI")
319 # pragma map(inflate_trees_dynamic,"INTRDY")
320 # pragma map(inflate_trees_fixed,"INTRFI")
321 # pragma map(inflate_trees_free,"INTRFR")
324 #endif /* !ZCONF_H */
333 #define ZLIB_VERSION "1.1.4"
336 The 'zlib' compression library provides in-memory compression and
337 decompression functions, including integrity checks of the uncompressed
338 data. This version of the library supports only one compression method
339 (deflation) but other algorithms will be added later and will have the same
342 Compression can be done in a single step if the buffers are large
343 enough (for example if an input file is mmap'ed), or can be done by
344 repeated calls of the compression function. In the latter case, the
345 application must provide more input and/or consume the output
346 (providing more output space) before each call.
348 The library also supports reading and writing files in gzip (.gz) format
349 with an interface similar to that of stdio.
351 The library does not install any signal handler. The decoder checks
352 the consistency of the compressed data, so the library should never
353 crash even in case of corrupted input.
356 typedef voidpf (*alloc_func
)(voidpf
, uInt
, uInt
);
357 typedef void (*free_func
)(voidpf
, voidpf
);
359 struct internal_state
;
361 typedef struct z_stream_s
{
362 Bytef
*next_in
; /* next input byte */
363 uInt avail_in
; /* number of bytes available at next_in */
364 uLong total_in
; /* total nb of input bytes read so far */
366 Bytef
*next_out
; /* next output byte should be put there */
367 uInt avail_out
; /* remaining free space at next_out */
368 uLong total_out
; /* total nb of bytes output so far */
370 const char *msg
; /* last error message, NULL if no error */
371 struct internal_state FAR
*state
; /* not visible by applications */
373 alloc_func zalloc
; /* used to allocate the internal state */
374 free_func zfree
; /* used to free the internal state */
375 voidpf opaque
; /* private data object passed to zalloc and zfree */
377 int data_type
; /* best guess about the data type: ascii or binary */
378 uLong adler
; /* adler32 value of the uncompressed data */
379 uLong reserved
; /* reserved for future use */
382 typedef z_stream FAR
*z_streamp
;
385 The application must update next_in and avail_in when avail_in has
386 dropped to zero. It must update next_out and avail_out when avail_out
387 has dropped to zero. The application must initialize zalloc, zfree and
388 opaque before calling the init function. All other fields are set by the
389 compression library and must not be updated by the application.
391 The opaque value provided by the application will be passed as the first
392 parameter for calls of zalloc and zfree. This can be useful for custom
393 memory management. The compression library attaches no meaning to the
396 zalloc must return Z_NULL if there is not enough memory for the object.
397 If zlib is used in a multi-threaded application, zalloc and zfree must be
400 On 16-bit systems, the functions zalloc and zfree must be able to allocate
401 exactly 65536 bytes, but will not be required to allocate more than this
402 if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
403 pointers returned by zalloc for objects of exactly 65536 bytes *must*
404 have their offset normalized to zero. The default allocation function
405 provided by this library ensures this (see zutil.c). To reduce memory
406 requirements and avoid any allocation of 64K objects, at the expense of
407 compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
409 The fields total_in and total_out can be used for statistics or
410 progress reports. After compression, total_in holds the total size of
411 the uncompressed data and may be saved for use in the decompressor
412 (particularly if the decompressor wants to decompress everything in
419 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
420 #define Z_PACKET_FLUSH 2
421 #define Z_SYNC_FLUSH 3
422 #define Z_FULL_FLUSH 4
424 /* Allowed flush values; see deflate() below for details */
427 #define Z_STREAM_END 1
428 #define Z_NEED_DICT 2
430 #define Z_STREAM_ERROR (-2)
431 #define Z_DATA_ERROR (-3)
432 #define Z_MEM_ERROR (-4)
433 #define Z_BUF_ERROR (-5)
434 #define Z_VERSION_ERROR (-6)
435 /* Return codes for the compression/decompression functions. Negative
436 * values are errors, positive values are used for special but normal events.
439 #define Z_NO_COMPRESSION 0
440 #define Z_BEST_SPEED 1
441 #define Z_BEST_COMPRESSION 9
442 #define Z_DEFAULT_COMPRESSION (-1)
443 /* compression levels */
446 #define Z_HUFFMAN_ONLY 2
447 #define Z_DEFAULT_STRATEGY 0
448 /* compression strategy; see deflateInit2() below for details */
453 /* Possible values of the data_type field */
456 /* The deflate compression method (the only one supported in this version) */
458 #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
460 #define zlib_version zlibVersion()
461 /* for compatibility with versions < 1.0.2 */
463 /* basic functions */
465 ZEXTERN
const char * ZEXPORT
zlibVersion(void);
466 /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
467 If the first character differs, the library code actually used is
468 not compatible with the zlib.h header file used by the application.
469 This check is automatically made by deflateInit and inflateInit.
473 ZEXTERN int ZEXPORT deflateInit(z_streamp, int);
475 Initializes the internal stream state for compression. The fields
476 zalloc, zfree and opaque must be initialized before by the caller.
477 If zalloc and zfree are set to Z_NULL, deflateInit updates them to
478 use default allocation functions.
480 The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
481 1 gives best speed, 9 gives best compression, 0 gives no compression at
482 all (the input data is simply copied a block at a time).
483 Z_DEFAULT_COMPRESSION requests a default compromise between speed and
484 compression (currently equivalent to level 6).
486 deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
487 enough memory, Z_STREAM_ERROR if level is not a valid compression level,
488 Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
489 with the version assumed by the caller (ZLIB_VERSION).
490 msg is set to null if there is no error message. deflateInit does not
491 perform any compression: this will be done by deflate().
495 ZEXTERN
int ZEXPORT
deflate(z_streamp
, int);
497 deflate compresses as much data as possible, and stops when the input
498 buffer becomes empty or the output buffer becomes full. It may introduce some
499 output latency (reading input without producing any output) except when
502 The detailed semantics are as follows. deflate performs one or both of the
505 - Compress more input starting at next_in and update next_in and avail_in
506 accordingly. If not all input can be processed (because there is not
507 enough room in the output buffer), next_in and avail_in are updated and
508 processing will resume at this point for the next call of deflate().
510 - Provide more output starting at next_out and update next_out and avail_out
511 accordingly. This action is forced if the parameter flush is non zero.
512 Forcing flush frequently degrades the compression ratio, so this parameter
513 should be set only when necessary (in interactive applications).
514 Some output may be provided even if flush is not set.
516 Before the call of deflate(), the application should ensure that at least
517 one of the actions is possible, by providing more input and/or consuming
518 more output, and updating avail_in or avail_out accordingly; avail_out
519 should never be zero before the call. The application can consume the
520 compressed output when it wants, for example when the output buffer is full
521 (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
522 and with zero avail_out, it must be called again after making room in the
523 output buffer because there might be more output pending.
525 If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
526 flushed to the output buffer and the output is aligned on a byte boundary, so
527 that the decompressor can get all input data available so far. (In particular
528 avail_in is zero after the call if enough output space has been provided
529 before the call.) Flushing may degrade compression for some compression
530 algorithms and so it should be used only when necessary.
532 If flush is set to Z_FULL_FLUSH, all output is flushed as with
533 Z_SYNC_FLUSH, and the compression state is reset so that decompression can
534 restart from this point if previous compressed data has been damaged or if
535 random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
538 If deflate returns with avail_out == 0, this function must be called again
539 with the same value of the flush parameter and more output space (updated
540 avail_out), until the flush is complete (deflate returns with non-zero
543 If the parameter flush is set to Z_PACKET_FLUSH, the compression
544 block is terminated, and a zero-length stored block is output,
545 omitting the length bytes (the effect of this is that the 3-bit type
546 code 000 for a stored block is output, and the output is then
547 byte-aligned). This is designed for use at the end of a PPP packet.
550 If the parameter flush is set to Z_FINISH, pending input is processed,
551 pending output is flushed and deflate returns with Z_STREAM_END if there
552 was enough output space; if deflate returns with Z_OK, this function must be
553 called again with Z_FINISH and more output space (updated avail_out) but no
554 more input data, until it returns with Z_STREAM_END or an error. After
555 deflate has returned Z_STREAM_END, the only possible operations on the
556 stream are deflateReset or deflateEnd.
558 Z_FINISH can be used immediately after deflateInit if all the compression
559 is to be done in a single step. In this case, avail_out must be at least
560 0.1% larger than avail_in plus 12 bytes. If deflate does not return
561 Z_STREAM_END, then it must be called again as described above.
563 deflate() sets strm->adler to the adler32 checksum of all input read
564 so far (that is, total_in bytes).
566 deflate() may update data_type if it can make a good guess about
567 the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
568 binary. This field is only for information purposes and does not affect
569 the compression algorithm in any manner.
571 deflate() returns Z_OK if some progress has been made (more input
572 processed or more output produced), Z_STREAM_END if all input has been
573 consumed and all output has been produced (only when flush is set to
574 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
575 if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
576 (for example avail_in or avail_out was zero).
580 ZEXTERN
int ZEXPORT
deflateEnd(z_streamp
);
582 All dynamically allocated data structures for this stream are freed.
583 This function discards any unprocessed input and does not flush any
586 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
587 stream state was inconsistent, Z_DATA_ERROR if the stream was freed
588 prematurely (some input or output was discarded). In the error case,
589 msg may be set but then points to a static string (which must not be
595 ZEXTERN int ZEXPORT inflateInit(z_streamp);
597 Initializes the internal stream state for decompression. The fields
598 next_in, avail_in, zalloc, zfree and opaque must be initialized before by
599 the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
600 value depends on the compression method), inflateInit determines the
601 compression method from the zlib header and allocates all data structures
602 accordingly; otherwise the allocation will be deferred to the first call of
603 inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
604 use default allocation functions.
606 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
607 memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
608 version assumed by the caller. msg is set to null if there is no error
609 message. inflateInit does not perform any decompression apart from reading
610 the zlib header if present: this will be done by inflate(). (So next_in and
611 avail_in may be modified, but next_out and avail_out are unchanged.)
615 ZEXTERN
int ZEXPORT
inflate(z_streamp
, int);
617 inflate decompresses as much data as possible, and stops when the input
618 buffer becomes empty or the output buffer becomes full. It may some
619 introduce some output latency (reading input without producing any output)
620 except when forced to flush.
622 The detailed semantics are as follows. inflate performs one or both of the
625 - Decompress more input starting at next_in and update next_in and avail_in
626 accordingly. If not all input can be processed (because there is not
627 enough room in the output buffer), next_in is updated and processing
628 will resume at this point for the next call of inflate().
630 - Provide more output starting at next_out and update next_out and avail_out
631 accordingly. inflate() provides as much output as possible, until there
632 is no more input data or no more space in the output buffer (see below
633 about the flush parameter).
635 Before the call of inflate(), the application should ensure that at least
636 one of the actions is possible, by providing more input and/or consuming
637 more output, and updating the next_* and avail_* values accordingly.
638 The application can consume the uncompressed output when it wants, for
639 example when the output buffer is full (avail_out == 0), or after each
640 call of inflate(). If inflate returns Z_OK and with zero avail_out, it
641 must be called again after making room in the output buffer because there
642 might be more output pending.
644 If the parameter flush is set to Z_SYNC_FLUSH or Z_PACKET_FLUSH,
645 inflate flushes as much output as possible to the output buffer. The
646 flushing behavior of inflate is not specified for values of the flush
647 parameter other than Z_SYNC_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the
648 current implementation actually flushes as much output as possible
649 anyway. For Z_PACKET_FLUSH, inflate checks that once all the input data
650 has been consumed, it is expecting to see the length field of a stored
651 block; if not, it returns Z_DATA_ERROR.
653 inflate() should normally be called until it returns Z_STREAM_END or an
654 error. However if all decompression is to be performed in a single step
655 (a single call of inflate), the parameter flush should be set to
656 Z_FINISH. In this case all pending input is processed and all pending
657 output is flushed; avail_out must be large enough to hold all the
658 uncompressed data. (The size of the uncompressed data may have been saved
659 by the compressor for this purpose.) The next operation on this stream must
660 be inflateEnd to deallocate the decompression state. The use of Z_FINISH
661 is never required, but can be used to inform inflate that a faster routine
662 may be used for the single inflate() call.
664 If a preset dictionary is needed at this point (see inflateSetDictionary
665 below), inflate sets strm-adler to the adler32 checksum of the
666 dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
667 it sets strm->adler to the adler32 checksum of all output produced
668 so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
669 an error code as described below. At the end of the stream, inflate()
670 checks that its computed adler32 checksum is equal to that saved by the
671 compressor and returns Z_STREAM_END only if the checksum is correct.
673 inflate() returns Z_OK if some progress has been made (more input processed
674 or more output produced), Z_STREAM_END if the end of the compressed data has
675 been reached and all uncompressed output has been produced, Z_NEED_DICT if a
676 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
677 corrupted (input stream not conforming to the zlib format or incorrect
678 adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
679 (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
680 enough memory, Z_BUF_ERROR if no progress is possible or if there was not
681 enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
682 case, the application may then call inflateSync to look for a good
687 ZEXTERN
int ZEXPORT
inflateEnd(z_streamp
);
689 All dynamically allocated data structures for this stream are freed.
690 This function discards any unprocessed input and does not flush any
693 inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
694 was inconsistent. In the error case, msg may be set but then points to a
695 static string (which must not be deallocated).
698 /* Advanced functions */
701 The following functions are needed only in some special applications.
705 ZEXTERN int ZEXPORT deflateInit2(z_streamp, int, int, int, int, int);
707 This is another version of deflateInit with more compression options. The
708 fields next_in, zalloc, zfree and opaque must be initialized before by
711 The method parameter is the compression method. It must be Z_DEFLATED in
712 this version of the library.
714 The windowBits parameter is the base two logarithm of the window size
715 (the size of the history buffer). It should be in the range 8..15 for this
716 version of the library. Larger values of this parameter result in better
717 compression at the expense of memory usage. The default value is 15 if
718 deflateInit is used instead.
720 The memLevel parameter specifies how much memory should be allocated
721 for the internal compression state. memLevel=1 uses minimum memory but
722 is slow and reduces compression ratio; memLevel=9 uses maximum memory
723 for optimal speed. The default value is 8. See zconf.h for total memory
724 usage as a function of windowBits and memLevel.
726 The strategy parameter is used to tune the compression algorithm. Use the
727 value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
728 filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
729 string match). Filtered data consists mostly of small values with a
730 somewhat random distribution. In this case, the compression algorithm is
731 tuned to compress them better. The effect of Z_FILTERED is to force more
732 Huffman coding and less string matching; it is somewhat intermediate
733 between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
734 the compression ratio but not the correctness of the compressed output even
735 if it is not set appropriately.
737 deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
738 memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
739 method). msg is set to null if there is no error message. deflateInit2 does
740 not perform any compression: this will be done by deflate().
743 ZEXTERN
int ZEXPORT
deflateSetDictionary(z_streamp
, const Bytef
*, uInt
);
745 Initializes the compression dictionary from the given byte sequence
746 without producing any compressed output. This function must be called
747 immediately after deflateInit, deflateInit2 or deflateReset, before any
748 call of deflate. The compressor and decompressor must use exactly the same
749 dictionary (see inflateSetDictionary).
751 The dictionary should consist of strings (byte sequences) that are likely
752 to be encountered later in the data to be compressed, with the most commonly
753 used strings preferably put towards the end of the dictionary. Using a
754 dictionary is most useful when the data to be compressed is short and can be
755 predicted with good accuracy; the data can then be compressed better than
756 with the default empty dictionary.
758 Depending on the size of the compression data structures selected by
759 deflateInit or deflateInit2, a part of the dictionary may in effect be
760 discarded, for example if the dictionary is larger than the window size in
761 deflate or deflate2. Thus the strings most likely to be useful should be
762 put at the end of the dictionary, not at the front.
764 Upon return of this function, strm->adler is set to the Adler32 value
765 of the dictionary; the decompressor may later use this value to determine
766 which dictionary has been used by the compressor. (The Adler32 value
767 applies to the whole dictionary even if only a subset of the dictionary is
768 actually used by the compressor.)
770 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
771 parameter is invalid (such as NULL dictionary) or the stream state is
772 inconsistent (for example if deflate has already been called for this stream
773 or if the compression method is bsort). deflateSetDictionary does not
774 perform any compression: this will be done by deflate().
777 ZEXTERN
int ZEXPORT
deflateCopy(z_streamp
, z_streamp
);
779 Sets the destination stream as a complete copy of the source stream.
781 This function can be useful when several compression strategies will be
782 tried, for example when there are several ways of pre-processing the input
783 data with a filter. The streams that will be discarded should then be freed
784 by calling deflateEnd. Note that deflateCopy duplicates the internal
785 compression state which can be quite large, so this strategy is slow and
786 can consume lots of memory.
788 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
789 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
790 (such as zalloc being NULL). msg is left unchanged in both source and
794 extern int inflateIncomp(z_stream
*);
796 This function adds the data at next_in (avail_in bytes) to the output
797 history without performing any output. There must be no pending output,
798 and the decompressor must be expecting to see the start of a block.
799 Calling this function is equivalent to decompressing a stored block
800 containing the data at next_in (except that the data is not output).
803 ZEXTERN
int ZEXPORT
deflateReset(z_streamp
);
805 This function is equivalent to deflateEnd followed by deflateInit,
806 but does not free and reallocate all the internal compression state.
807 The stream will keep the same compression level and any other attributes
808 that may have been set by deflateInit2.
810 deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
811 stream state was inconsistent (such as zalloc or state being NULL).
814 ZEXTERN
int ZEXPORT
deflateParams(z_streamp
, int, int);
816 Dynamically update the compression level and compression strategy. The
817 interpretation of level and strategy is as in deflateInit2. This can be
818 used to switch between compression and straight copy of the input data, or
819 to switch to a different kind of input data requiring a different
820 strategy. If the compression level is changed, the input available so far
821 is compressed with the old level (and may be flushed); the new level will
822 take effect only at the next call of deflate().
824 Before the call of deflateParams, the stream state must be set as for
825 a call of deflate(), since the currently available input may have to
826 be compressed and flushed. In particular, strm->avail_out must be non-zero.
828 deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
829 stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
830 if strm->avail_out was zero.
833 ZEXTERN
int ZEXPORT
deflateOutputPending(z_streamp
);
835 Returns the number of bytes of output which are immediately
836 available from the compressor (i.e. without any further input
841 ZEXTERN int ZEXPORT inflateInit2(z_streamp, int);
843 This is another version of inflateInit with an extra parameter. The
844 fields next_in, avail_in, zalloc, zfree and opaque must be initialized
845 before by the caller.
847 The windowBits parameter is the base two logarithm of the maximum window
848 size (the size of the history buffer). It should be in the range 8..15 for
849 this version of the library. The default value is 15 if inflateInit is used
850 instead. If a compressed stream with a larger window size is given as
851 input, inflate() will return with the error code Z_DATA_ERROR instead of
852 trying to allocate a larger window.
854 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
855 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
856 memLevel). msg is set to null if there is no error message. inflateInit2
857 does not perform any decompression apart from reading the zlib header if
858 present: this will be done by inflate(). (So next_in and avail_in may be
859 modified, but next_out and avail_out are unchanged.)
862 ZEXTERN
int ZEXPORT
inflateSetDictionary(z_streamp
, const Bytef
*, uInt
);
864 Initializes the decompression dictionary from the given uncompressed byte
865 sequence. This function must be called immediately after a call of inflate
866 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
867 can be determined from the Adler32 value returned by this call of
868 inflate. The compressor and decompressor must use exactly the same
869 dictionary (see deflateSetDictionary).
871 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
872 parameter is invalid (such as NULL dictionary) or the stream state is
873 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
874 expected one (incorrect Adler32 value). inflateSetDictionary does not
875 perform any decompression: this will be done by subsequent calls of
879 ZEXTERN
int ZEXPORT
inflateSync(z_streamp
);
881 Skips invalid compressed data until a full flush point (see above the
882 description of deflate with Z_FULL_FLUSH) can be found, or until all
883 available input is skipped. No output is provided.
885 inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
886 if no more input was provided, Z_DATA_ERROR if no flush point has been found,
887 or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
888 case, the application may save the current current value of total_in which
889 indicates where valid compressed data was found. In the error case, the
890 application may repeatedly call inflateSync, providing more input each time,
891 until success or end of the input data.
894 ZEXTERN
int ZEXPORT
inflateReset(z_streamp
);
896 This function is equivalent to inflateEnd followed by inflateInit,
897 but does not free and reallocate all the internal decompression state.
898 The stream will keep attributes that may have been set by inflateInit2.
900 inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
901 stream state was inconsistent (such as zalloc or state being NULL).
905 /* utility functions */
908 The following utility functions are implemented on top of the
909 basic stream-oriented functions. To simplify the interface, some
910 default options are assumed (compression level and memory usage,
911 standard memory allocation functions). The source code of these
912 utility functions can easily be modified if you need special options.
915 ZEXTERN
int ZEXPORT
compress(Bytef
*, uLongf
*, const Bytef
*, uLong
);
917 Compresses the source buffer into the destination buffer. sourceLen is
918 the byte length of the source buffer. Upon entry, destLen is the total
919 size of the destination buffer, which must be at least 0.1% larger than
920 sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
922 This function can be used to compress a whole file at once if the
923 input file is mmap'ed.
924 compress returns Z_OK if success, Z_MEM_ERROR if there was not
925 enough memory, Z_BUF_ERROR if there was not enough room in the output
929 ZEXTERN
int ZEXPORT
compress2(Bytef
*, uLongf
*, const Bytef
*,
932 Compresses the source buffer into the destination buffer. The level
933 parameter has the same meaning as in deflateInit. sourceLen is the byte
934 length of the source buffer. Upon entry, destLen is the total size of the
935 destination buffer, which must be at least 0.1% larger than sourceLen plus
936 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
938 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
939 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
940 Z_STREAM_ERROR if the level parameter is invalid.
943 ZEXTERN
int ZEXPORT
uncompress(Bytef
*, uLongf
*, const Bytef
*, uLong
);
945 Decompresses the source buffer into the destination buffer. sourceLen is
946 the byte length of the source buffer. Upon entry, destLen is the total
947 size of the destination buffer, which must be large enough to hold the
948 entire uncompressed data. (The size of the uncompressed data must have
949 been saved previously by the compressor and transmitted to the decompressor
950 by some mechanism outside the scope of this compression library.)
951 Upon exit, destLen is the actual size of the compressed buffer.
952 This function can be used to decompress a whole file at once if the
953 input file is mmap'ed.
955 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
956 enough memory, Z_BUF_ERROR if there was not enough room in the output
957 buffer, or Z_DATA_ERROR if the input data was corrupted.
961 typedef voidp gzFile
;
963 ZEXTERN gzFile ZEXPORT
gzopen(const char *, const char *);
965 Opens a gzip (.gz) file for reading or writing. The mode parameter
966 is as in fopen ("rb" or "wb") but can also include a compression level
967 ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
968 Huffman only compression as in "wb1h". (See the description
969 of deflateInit2 for more information about the strategy parameter.)
971 gzopen can be used to read a file which is not in gzip format; in this
972 case gzread will directly read from the file without decompression.
974 gzopen returns NULL if the file could not be opened or if there was
975 insufficient memory to allocate the (de)compression state; errno
976 can be checked to distinguish the two cases (if errno is zero, the
977 zlib error is Z_MEM_ERROR). */
979 ZEXTERN gzFile ZEXPORT
gzdopen(int, const char *);
981 gzdopen() associates a gzFile with the file descriptor fd. File
982 descriptors are obtained from calls like open, dup, creat, pipe or
983 fileno (in the file has been previously opened with fopen).
984 The mode parameter is as in gzopen.
985 The next call of gzclose on the returned gzFile will also close the
986 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
987 descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
988 gzdopen returns NULL if there was insufficient memory to allocate
989 the (de)compression state.
992 ZEXTERN
int ZEXPORT
gzsetparams(gzFile
, int, int);
994 Dynamically update the compression level or strategy. See the description
995 of deflateInit2 for the meaning of these parameters.
996 gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
1000 ZEXTERN
int ZEXPORT
gzread(gzFile
, voidp
, unsigned);
1002 Reads the given number of uncompressed bytes from the compressed file.
1003 If the input file was not in gzip format, gzread copies the given number
1004 of bytes into the buffer.
1005 gzread returns the number of uncompressed bytes actually read (0 for
1006 end of file, -1 for error). */
1008 ZEXTERN
int ZEXPORT
gzwrite(gzFile
, const voidp
, unsigned);
1010 Writes the given number of uncompressed bytes into the compressed file.
1011 gzwrite returns the number of uncompressed bytes actually written
1012 (0 in case of error).
1015 ZEXTERN
int ZEXPORTVA
gzprintf(gzFile
, const char *, ...)
1016 __attribute__((__format__(__printf__
, 2, 3)));
1018 Converts, formats, and writes the args to the compressed file under
1019 control of the format string, as in fprintf. gzprintf returns the number of
1020 uncompressed bytes actually written (0 in case of error).
1023 ZEXTERN
int ZEXPORT
gzputs(gzFile
, const char *);
1025 Writes the given null-terminated string to the compressed file, excluding
1026 the terminating null character.
1027 gzputs returns the number of characters written, or -1 in case of error.
1030 ZEXTERN
char * ZEXPORT
gzgets(gzFile
, char *, int);
1032 Reads bytes from the compressed file until len-1 characters are read, or
1033 a newline character is read and transferred to buf, or an end-of-file
1034 condition is encountered. The string is then terminated with a null
1036 gzgets returns buf, or Z_NULL in case of error.
1039 ZEXTERN
int ZEXPORT
gzputc(gzFile
, int);
1041 Writes c, converted to an unsigned char, into the compressed file.
1042 gzputc returns the value that was written, or -1 in case of error.
1045 ZEXTERN
int ZEXPORT
gzgetc(gzFile
);
1047 Reads one byte from the compressed file. gzgetc returns this byte
1048 or -1 in case of end of file or error.
1051 ZEXTERN
int ZEXPORT
gzflush(gzFile
, int);
1053 Flushes all pending output into the compressed file. The parameter
1054 flush is as in the deflate() function. The return value is the zlib
1055 error number (see function gzerror below). gzflush returns Z_OK if
1056 the flush parameter is Z_FINISH and all output could be flushed.
1057 gzflush should be called only when strictly necessary because it can
1058 degrade compression.
1064 * "long" gzseek has been there till Oct 1999 (1.4L), which was wrong.
1066 ZEXTERN z_off_t ZEXPORT
gzseek(gzFile
, z_off_t
, int);
1068 Sets the starting position for the next gzread or gzwrite on the
1069 given compressed file. The offset represents a number of bytes in the
1070 uncompressed data stream. The whence parameter is defined as in lseek(2);
1071 the value SEEK_END is not supported.
1072 If the file is opened for reading, this function is emulated but can be
1073 extremely slow. If the file is opened for writing, only forward seeks are
1074 supported; gzseek then compresses a sequence of zeroes up to the new
1077 gzseek returns the resulting offset location as measured in bytes from
1078 the beginning of the uncompressed stream, or -1 in case of error, in
1079 particular if the file is opened for writing and the new starting position
1080 would be before the current position.
1083 ZEXTERN
int ZEXPORT
gzrewind(gzFile
);
1085 Rewinds the given file. This function is supported only for reading.
1087 gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
1092 * "long" gztell has been there till Oct 1999 (1.4L), which was wrong.
1094 ZEXTERN z_off_t ZEXPORT
gztell(gzFile
);
1096 Returns the starting position for the next gzread or gzwrite on the
1097 given compressed file. This position represents a number of bytes in the
1098 uncompressed data stream.
1100 gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
1103 ZEXTERN
int ZEXPORT
gzeof(gzFile
);
1105 Returns 1 when EOF has previously been detected reading the given
1106 input stream, otherwise zero.
1109 ZEXTERN
int ZEXPORT
gzclose(gzFile
);
1111 Flushes all pending output if necessary, closes the compressed file
1112 and deallocates all the (de)compression state. The return value is the zlib
1113 error number (see function gzerror below).
1116 ZEXTERN
const char * ZEXPORT
gzerror(gzFile
, int *);
1118 Returns the error message for the last error which occurred on the
1119 given compressed file. errnum is set to zlib error number. If an
1120 error occurred in the file system and not in the compression library,
1121 errnum is set to Z_ERRNO and the application may consult errno
1122 to get the exact error code.
1125 /* checksum functions */
1128 These functions are not related to compression but are exported
1129 anyway because they might be useful in applications using the
1130 compression library.
1133 ZEXTERN uLong ZEXPORT
adler32(uLong
, const Bytef
*, uInt
);
1136 Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1137 return the updated checksum. If buf is NULL, this function returns
1138 the required initial value for the checksum.
1139 An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
1140 much faster. Usage example:
1142 uLong adler = adler32(0L, Z_NULL, 0);
1144 while (read_buffer(buffer, length) != EOF) {
1145 adler = adler32(adler, buffer, length);
1147 if (adler != original_adler) error();
1151 ZEXTERN uLong ZEXPORT
crc32(uLong
, const Bytef
*, uInt
);
1154 Update a running crc with the bytes buf[0..len-1] and return the updated
1155 crc. If buf is NULL, this function returns the required initial value
1156 for the crc. Pre- and post-conditioning (one's complement) is performed
1157 within this function so it shouldn't be done by the application.
1160 uLong crc = crc32(0L, Z_NULL, 0);
1162 while (read_buffer(buffer, length) != EOF) {
1163 crc = crc32(crc, buffer, length);
1165 if (crc != original_crc) error();
1169 /* various hacks, don't look :) */
1171 /* deflateInit and inflateInit are macros to allow checking the zlib version
1172 * and the compiler's view of z_stream:
1174 ZEXTERN
int ZEXPORT
deflateInit_(z_streamp
, int, const char *, int);
1175 ZEXTERN
int ZEXPORT
inflateInit_(z_streamp
, const char *, int);
1176 ZEXTERN
int ZEXPORT
deflateInit2_(z_streamp
, int, int, int, int,
1177 int, const char *, int);
1178 ZEXTERN
int ZEXPORT
inflateInit2_(z_streamp
, int, const char *, int);
1179 #define deflateInit(strm, level) \
1180 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
1181 #define inflateInit(strm) \
1182 inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
1183 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1184 deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1185 (strategy), ZLIB_VERSION, sizeof(z_stream))
1186 #define inflateInit2(strm, windowBits) \
1187 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
1190 #if !defined(Z_UTIL_H) && !defined(NO_DUMMY_DECL)
1191 struct internal_state
{int dummy
;}; /* hack for buggy compilers */
1194 ZEXTERN
const char * ZEXPORT
zError(int);
1195 ZEXTERN
int ZEXPORT
inflateSyncPoint(z_streamp
);
1196 ZEXTERN
const uLongf
* ZEXPORT
get_crc_table(void);
1201 #endif /* !ZLIB_H */
1203 #endif /* !_NET_ZLIB_H_ */