1 .\" $NetBSD: zlib.3,v 1.10 2014/03/18 18:20:38 riastradh Exp $
2 .\" $OpenBSD: zlib.3,v 1.1 2003/09/25 09:12:09 jmc Exp $
4 .\" Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
6 .\" This software is provided 'as-is', without any express or implied
7 .\" warranty. In no event will the authors be held liable for any damages
8 .\" arising from the use of this software.
10 .\" Permission is granted to anyone to use this software for any purpose,
11 .\" including commercial applications, and to alter it and redistribute it
12 .\" freely, subject to the following restrictions:
14 .\" The origin of this software must not be misrepresented; you must not
15 .\" claim that you wrote the original software. If you use this software
16 .\" in a product, an acknowledgment in the product documentation would be
17 .\" appreciated but is not required.
18 .\" Altered source versions must be plainly marked as such, and must not be
19 .\" misrepresented as being the original software.
20 .\" This notice may not be removed or altered from any source distribution.
22 .\" Converted to mdoc format for the OpenBSD project
23 .\" by Jason McIntyre <jmc@openbsd.org>
30 .Nd general purpose compression library
35 .Fn zlibVersion "void"
37 .Fn deflateInit "z_streamp strm" "int level"
39 .Fn deflate "z_streamp strm" "int flush"
41 .Fn deflateEnd "z_streamp strm"
43 .Fn inflateInit "z_streamp strm"
45 .Fn inflate "z_streamp strm" "int flush"
47 .Fn inflateEnd "z_streamp strm"
48 .Ss Advanced functions
50 .Fn deflateInit2 "z_streamp strm" "int level" "int method" \
51 "int windowBits" "int memLevel" "int strategy"
53 .Fn deflateSetDictionary "z_streamp strm" "const Bytef *dictionary" \
56 .Fn deflateCopy "z_streamp dest" "z_streamp source"
58 .Fn deflateReset "z_streamp strm"
60 .Fn deflateParams "z_streamp strm" "int level" "int strategy"
62 .Fn inflateInit2 "z_streamp strm" "int windowBits"
64 .Fn inflateSetDictionary "z_streamp strm" "const Bytef *dictionary" "uInt dictLength"
66 .Fn inflateSync "z_streamp strm"
68 .Fn inflateReset "z_streamp strm"
70 .Fd typedef voidp gzFile ;
73 .Fn compress "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen"
75 .Fn compress2 "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen" "int level"
77 .Fn uncompress "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen"
79 .Fn gzopen "const char *path" "const char *mode"
81 .Fn gzdopen "int fd" "const char *mode"
83 .Fn gzsetparams "gzFile file" "int level" "int strategy"
85 .Fn gzread "gzFile file" "voidp buf" "unsigned len"
87 .Fn gzwrite "gzFile file" "const voidp buf" "unsigned len"
89 .Fn gzprintf "gzFile file" "const char *format" "..."
91 .Fn gzputs "gzFile file" "const char *s"
93 .Fn gzgets "gzFile file" "char *buf" "int len"
95 .Fn gzputc "gzFile file" "int c"
97 .Fn gzgetc "gzFile file"
99 .Fn gzflush "gzFile file" "int flush"
101 .Fn gzseek "gzFile file" "z_off_t offset" "int whence"
103 .Fn gzrewind "gzFile file"
105 .Fn gztell "gzFile file"
107 .Fn gzeof "gzFile file"
109 .Fn gzclose "gzFile file"
111 .Fn gzerror "gzFile file" "int *errnum"
112 .Ss Checksum functions
114 .Fn adler32 "uLong adler" "const Bytef *buf" "uInt len"
116 .Fn crc32 "uLong crc" "const Bytef *buf" "uInt len"
118 This manual page describes the
120 general purpose compression library, version 1.1.4.
124 compression library provides in-memory compression and decompression functions,
125 including integrity checks of the uncompressed data.
126 This version of the library supports only one compression method
128 but other algorithms will be added later and will have the same
131 Compression can be done in a single step if the buffers are large enough
132 .Pq for example if an input file is mmap'ed ,
133 or can be done by repeated calls of the compression function.
134 In the latter case, the application must provide more input
135 and/or consume the output
136 .Pq providing more output space
139 The library also supports reading and writing files in
142 format with an interface similar to that of
145 The library does not install any signal handler.
146 The decoder checks the consistency of the compressed data,
147 so the library should never crash even in case of corrupted input.
149 The functions within the library are divided into the following sections:
151 .Bl -dash -offset indent -compact
163 .It Fa const char * Fn zlibVersion "void" ;
165 The application can compare
170 If the first character differs, the library code actually used is
171 not compatible with the
173 header file used by the application.
174 This check is automatically made by
178 .It Fa int Fn deflateInit "z_streamp strm" "int level" ;
182 function initializes the internal stream state for compression.
188 must be initialized before by the caller.
196 updates them to use default allocation functions.
198 The compression level must be
199 .Dv Z_DEFAULT_COMPRESSION ,
201 1 gives best speed, 9 gives best compression, 0 gives no compression at all
202 (the input data is simply copied a block at a time).
204 .Dv Z_DEFAULT_COMPRESSION
205 requests a default compromise between speed and compression
206 .Pq currently equivalent to level 6 .
213 if there was not enough memory,
215 if level is not a valid compression level,
221 is incompatible with the version assumed by the caller
224 is set to null if there is no error message.
226 does not perform any compression: this will be done by
228 .It Fa int Fn deflate "z_streamp strm" "int flush" ;
231 compresses as much data as possible, and stops when the input
232 buffer becomes empty or the output buffer becomes full.
233 It may introduce some output latency
234 .Pq reading input without producing any output
235 except when forced to flush.
237 The detailed semantics are as follows.
239 performs one or both of the following actions:
241 Compress more input starting at
248 If not all input can be processed
249 (because there is not enough room in the output buffer),
253 are updated and processing will resume at this point for the next call to
256 Provide more output starting at
263 This action is forced if the parameter
268 frequently degrades the compression ratio,
269 so this parameter should be set only when necessary
270 .Pq in interactive applications .
271 Some output may be provided even if
277 the application should ensure that at least
278 one of the actions is possible, by providing more input and/or consuming
279 more output, and updating
285 should never be zero before the call.
286 The application can consume the compressed output when it wants,
287 for example when the output buffer is full
289 or after each call to
297 it must be called again after making room in the
298 output buffer because there might be more output pending.
304 all pending output is flushed to the output buffer and the output
305 is aligned on a byte boundary, so that the decompressor can get all
306 input data available so far.
309 is zero after the call if enough output space
310 has been provided before the call.)
311 Flushing may degrade compression for some compression algorithms
312 and so it should be used only when necessary.
318 all output is flushed as with
320 and the compression state is reset so that decompression can restart from this
321 point if previous compressed data has been damaged or if random access
325 too often can seriously degrade the compression.
329 returns with avail_out == 0, this function must be called again
330 with the same value of the flush parameter and more output space
333 until the flush is complete
335 returns with non-zero
342 pending input is processed, pending output is flushed and
346 if there was enough output space; if
350 this function must be called again with
352 and more output space
355 but no more input data, until it returns with
362 the only possible operations on the stream are
368 can be used immediately after
370 if all the compression is to be done in a single step.
373 must be at least 0.1% larger than
380 then it must be called again as described above.
383 sets strm-\*[Gt]adler to the Adler-32 checksum of all input read so far
391 if it can make a good guess about the input data type
392 .Pq Z_ASCII or Z_BINARY .
393 If in doubt, the data is considered binary.
394 This field is only for information purposes and does not affect
395 the compression algorithm in any manner.
400 if some progress has been made
401 .Pq more input processed or more output produced ,
403 if all input has been consumed and all output has been produced
409 if the stream state was inconsistent
417 if no progress is possible
423 .It Fa int Fn deflateEnd "z_streamp strm" ;
425 All dynamically allocated data structures for this stream are freed.
426 This function discards any unprocessed input and does not flush any
434 if the stream state was inconsistent,
436 if the stream was freed prematurely
437 .Pq some input or output was discarded .
440 may be set but then points to a static string
441 .Pq which must not be deallocated .
442 .It Fa int Fn inflateInit "z_streamp strm" ;
445 function initializes the internal stream state for decompression.
453 must be initialized before by the caller.
461 .Pq the exact value depends on the compression method ,
463 determines the compression method from the
465 header and allocates all data structures accordingly;
466 otherwise the allocation will be deferred to the first call to
475 updates them to use default allocation functions.
482 if there was not enough memory,
486 library version is incompatible with the version assumed by the caller.
488 is set to null if there is no error message.
490 does not perform any decompression apart from reading the
492 header if present: this will be done by
504 .It Fa int Fn inflate "z_streamp strm" "int flush" ;
506 decompresses as much data as possible, and stops when the input
507 buffer becomes empty or the output buffer becomes full.
508 It may introduce some output latency
509 .Pq reading input without producing any output
510 except when forced to flush.
512 The detailed semantics are as follows.
514 performs one or both of the following actions:
516 Decompress more input starting at
523 If not all input can be processed
524 (because there is not enough room in the output buffer),
526 is updated and processing will resume at this point for the next call to
529 Provide more output starting at
537 provides as much output as possible,
538 until there is no more input data or no more space in the output buffer
539 .Pq see below about the flush parameter .
543 the application should ensure that at least one of the actions is possible,
544 by providing more input and/or consuming more output,
545 and updating the next_* and avail_* values accordingly.
546 The application can consume the uncompressed output when it wants,
547 for example when the output buffer is full (avail_out == 0),
548 or after each call to
556 it must be called again after making room
557 in the output buffer because there might be more output pending.
564 flushes as much output as possible to the output buffer.
565 The flushing behavior of
567 is not specified for values of the flush parameter other than
571 but the current implementation actually flushes as much output
575 should normally be called until it returns
578 However if all decompression is to be performed in a single step
579 .Pq a single call to inflate ,
584 In this case all pending input is processed and all pending output is flushed;
586 must be large enough to hold all the uncompressed data.
587 (The size of the uncompressed data may have been saved
588 by the compressor for this purpose.)
589 The next operation on this stream must be
591 to deallocate the decompression state.
594 is never required, but can be used to inform
596 that a faster routine may be used for the single
600 If a preset dictionary is needed at this point (see
601 .Fn inflateSetDictionary
604 sets strm-\*[Gt]adler to the Adler-32 checksum of the dictionary
605 chosen by the compressor and returns
607 otherwise it sets strm-\*[Gt]adler to the Adler-32 checksum of all output produced
615 or an error code as described below.
616 At the end of the stream,
618 checks that its computed Adler-32 checksum is equal to that saved by the
619 compressor and returns
621 only if the checksum is correct.
626 if some progress has been made
627 .Pq more input processed or more output produced ,
629 if the end of the compressed data has been reached and all uncompressed output
632 if a preset dictionary is needed at this point,
634 if the input data was corrupted (input stream not conforming to the
636 format or incorrect Adler-32 checksum),
638 if the stream structure was inconsistent
646 if there was not enough memory,
648 if no progress is possible or if there was not enough room in the output buffer
654 case, the application may then call
656 to look for a good compression block.
657 .It Fa int Fn inflateEnd "z_streamp strm" ;
658 All dynamically allocated data structures for this stream are freed.
659 This function discards any unprocessed input and does not flush any
667 if the stream state was inconsistent.
670 may be set but then points to a static string
671 .Pq which must not be deallocated .
673 .Sh ADVANCED FUNCTIONS
674 The following functions are needed only in some special applications.
676 .It Fa int Fn deflateInit2 "z_streamp strm" "int level" "int method" \
677 "int windowBits" "int memLevel" "int strategy" ;
679 This is another version of
681 with more compression options.
688 must be initialized before by the caller.
692 parameter is the compression method.
695 in this version of the library.
699 parameter is the base two logarithm of the window size
700 .Pq the size of the history buffer .
701 It should be in the range 8..15 for this version of the library.
702 Larger values of this parameter result in better compression
703 at the expense of memory usage.
704 The default value is 15 if
710 parameter specifies how much memory should be allocated
711 for the internal compression state.
712 memLevel=1 uses minimum memory but is slow and reduces compression ratio;
713 memLevel=9 uses maximum memory for optimal speed.
714 The default value is 8.
717 for total memory usage as a function of
724 parameter is used to tune the compression algorithm.
726 .Dv Z_DEFAULT_STRATEGY
729 for data produced by a filter
733 to force Huffman encoding only
734 .Pq no string match .
735 Filtered data consists mostly of small values with a
736 somewhat random distribution.
737 In this case, the compression algorithm is tuned to compress them better.
740 is to force more Huffman coding and less string matching;
741 it is somewhat intermediate between
747 parameter only affects the compression ratio but not the correctness of the
748 compressed output, even if it is not set appropriately.
755 if there was not enough memory,
757 if a parameter is invalid
758 .Pq such as an invalid method .
760 is set to null if there is no error message.
762 does not perform any compression: this will be done by
764 .It Fa int Fn deflateSetDictionary "z_streamp strm" \
765 "const Bytef *dictionary" "uInt dictLength" ;
767 Initializes the compression dictionary from the given byte sequence
768 without producing any compressed output.
769 This function must be called immediately after
776 The compressor and decompressor must use exactly the same dictionary
778 .Fn inflateSetDictionary ) .
780 The dictionary should consist of strings
782 that are likely to be encountered later in the data to be compressed,
783 with the most commonly used strings preferably put towards
784 the end of the dictionary.
785 Using a dictionary is most useful when the data to be compressed is short
786 and can be predicted with good accuracy;
787 the data can then be compressed better than with the default empty dictionary.
789 Depending on the size of the compression data structures selected by
793 a part of the dictionary may in effect be discarded,
794 for example if the dictionary is larger than the window size in
798 Thus the strings most likely to be useful should be
799 put at the end of the dictionary, not at the front.
801 Upon return of this function, strm-\*[Gt]adler is set to the Adler-32 value
802 of the dictionary; the decompressor may later use this value to determine
803 which dictionary has been used by the compressor.
804 (The Adler-32 value applies to the whole dictionary even if only a subset
805 of the dictionary is actually used by the compressor.)
807 .Fn deflateSetDictionary
813 if a parameter is invalid
814 .Pq such as NULL dictionary
815 or the stream state is inconsistent
818 has already been called for this stream or if the compression method is bsort).
819 .Fn deflateSetDictionary
820 does not perform any compression: this will be done by
822 .It Fa int Fn deflateCopy "z_streamp dest" "z_streamp source" ;
826 function sets the destination stream as a complete copy of the source stream.
828 This function can be useful when several compression strategies will be
829 tried, for example when there are several ways of pre-processing the input
831 The streams that will be discarded should then be freed by calling
835 duplicates the internal compression state which can be quite large,
836 so this strategy is slow and can consume lots of memory.
843 if there was not enough memory,
845 if the source stream state was inconsistent
850 is left unchanged in both source and destination.
851 .It Fa int Fn deflateReset "z_streamp strm" ;
853 This function is equivalent to
857 but does not free and reallocate all the internal compression state.
858 The stream will keep the same compression level and any other attributes
859 that may have been set by
867 if the source stream state was inconsistent
873 .It Fa int Fn deflateParams "z_streamp strm" "int level" "int strategy" ;
877 function dynamically updates the compression level and compression strategy.
878 The interpretation of level and strategy is as in
880 This can be used to switch between compression and straight copy
881 of the input data, or to switch to a different kind of input data
882 requiring a different strategy.
883 If the compression level is changed, the input available so far
884 is compressed with the old level
885 .Pq and may be flushed ;
886 the new level will take effect only at the next call to
891 the stream state must be set as for a call to
893 since the currently available input may have to be compressed and flushed.
894 In particular, strm-\*[Gt]avail_out must be non-zero.
901 if the source stream state was inconsistent or if a parameter was invalid, or
903 if strm-\*[Gt]avail_out was zero.
904 .It Fa int Fn inflateInit2 "z_streamp strm" "int windowBits" ;
906 This is another version of
908 with an extra parameter.
916 must be initialized before by the caller.
920 parameter is the base two logarithm of the maximum window size
921 .Pq the size of the history buffer .
922 It should be in the range 8..15 for this version of the library.
923 The default value is 15 if
926 If a compressed stream with a larger window size is given as input,
928 will return with the error code
930 instead of trying to allocate a larger window.
937 if there was not enough memory,
939 if a parameter is invalid
943 is set to null if there is no error message.
945 does not perform any decompression apart from reading the
947 header if present: this will be done by
958 .It Fa int Fn inflateSetDictionary "z_streamp strm" \
959 "const Bytef *dictionary" "uInt dictLength" ;
961 Initializes the decompression dictionary from the given uncompressed byte
963 This function must be called immediately after a call to
965 if this call returned
967 The dictionary chosen by the compressor can be determined from the
968 Adler-32 value returned by this call to
970 The compressor and decompressor must use exactly the same dictionary
972 .Fn deflateSetDictionary ) .
974 .Fn inflateSetDictionary
979 if a parameter is invalid
980 .Pq such as NULL dictionary
981 or the stream state is inconsistent,
983 if the given dictionary doesn't match the expected one
984 .Pq incorrect Adler-32 value .
985 .Fn inflateSetDictionary
986 does not perform any decompression: this will be done by subsequent calls of
988 .It Fa int Fn inflateSync "z_streamp strm" ;
990 Skips invalid compressed data until a full flush point
991 (see above the description of
995 can be found, or until all available input is skipped.
996 No output is provided.
1001 if a full flush point has been found,
1003 if no more input was provided,
1005 if no flush point has been found, or
1007 if the stream structure was inconsistent.
1008 In the success case, the application may save the current value of
1010 which indicates where valid compressed data was found.
1011 In the error case, the application may repeatedly call
1013 providing more input each time, until success or end of the input data.
1014 .It Fa int Fn inflateReset "z_streamp strm" ;
1016 This function is equivalent to
1020 but does not free and reallocate all the internal decompression state.
1021 The stream will keep attributes that may have been set by
1029 if the source stream state was inconsistent
1036 .Sh UTILITY FUNCTIONS
1037 The following utility functions are implemented on top of the
1038 basic stream-oriented functions.
1039 To simplify the interface,
1040 some default options are assumed (compression level and memory usage,
1041 standard memory allocation functions).
1042 The source code of these utility functions can easily be modified
1043 if you need special options.
1045 .It Fa int Fn compress "Bytef *dest" "uLongf *destLen" \
1046 "const Bytef *source" "uLong sourceLen" ;
1050 function compresses the source buffer into the destination buffer.
1052 is the byte length of the source buffer.
1055 is the total size of the destination buffer,
1056 which must be at least 0.1% larger than
1061 is the actual size of the compressed buffer.
1062 This function can be used to compress a whole file at once if the
1063 input file is mmap'ed.
1070 if there was not enough memory, or
1072 if there was not enough room in the output buffer.
1073 .It Fa int Fn compress2 "Bytef *dest" "uLongf *destLen" \
1074 "const Bytef *source" "uLong sourceLen" "int level" ;
1078 function compresses the source buffer into the destination buffer.
1081 parameter has the same meaning as in
1084 is the byte length of the source buffer.
1087 is the total size of the destination buffer,
1088 which must be at least 0.1% larger than
1093 is the actual size of the compressed buffer.
1100 if there was not enough memory,
1102 if there was not enough room in the output buffer, or
1104 if the level parameter is invalid.
1105 .It Fa int Fn uncompress "Bytef *dest" "uLongf *destLen" \
1106 "const Bytef *source" "uLong sourceLen" ;
1110 function decompresses the source buffer into the destination buffer.
1112 is the byte length of the source buffer.
1115 is the total size of the destination buffer,
1116 which must be large enough to hold the entire uncompressed data.
1117 (The size of the uncompressed data must have been saved previously
1118 by the compressor and transmitted to the decompressor
1119 by some mechanism outside the scope of this compression library.)
1122 is the actual size of the compressed buffer.
1123 This function can be used to decompress a whole file at once if the
1124 input file is mmap'ed.
1131 if there was not enough memory,
1133 if there was not enough room in the output buffer, or
1135 if the input data was corrupted.
1136 .It Fa gzFile Fn gzopen "const char *path" "const char *mode" ;
1140 function opens a gzip
1142 file for reading or writing.
1143 The mode parameter is as in
1150 but can also include a compression level
1154 for filtered data, as in
1157 for Huffman only compression, as in
1159 (See the description of
1161 for more information about the strategy parameter.)
1164 can be used to read a file which is not in gzip format;
1167 will directly read from the file without decompression.
1172 if the file could not be opened or if there was
1173 insufficient memory to allocate the (de)compression state;
1174 errno can be checked to distinguish the two cases (if errno is zero, the
1178 .It Fa gzFile Fn gzdopen "int fd" "const char *mode" ;
1182 function associates a gzFile with the file descriptor
1184 File descriptors are obtained from calls like
1191 (if the file has been previously opened with
1200 on the returned gzFile will also close the file descriptor fd,
1201 just like fclose(fdopen(fd), mode) closes the file descriptor fd.
1202 If you want to keep fd open, use gzdopen(dup(fd), mode).
1207 if there was insufficient memory to allocate the (de)compression state.
1208 .It Fa int Fn gzsetparams "gzFile file" "int level" "int strategy" ;
1212 function dynamically updates the compression level or strategy.
1213 See the description of
1215 for the meaning of these parameters.
1222 if the file was not opened for writing.
1223 .It Fa int Fn gzread "gzFile file" "voidp buf" "unsigned len" ;
1227 function reads the given number of uncompressed bytes from the compressed file.
1228 If the input file was not in gzip format,
1230 copies the given number of bytes into the buffer.
1233 returns the number of uncompressed bytes actually read
1234 (0 for end of file, \-1 for error).
1235 .It Fa int Fn gzwrite "gzFile file" "const voidp buf" "unsigned len" ;
1239 function writes the given number of uncompressed bytes into the compressed file.
1241 returns the number of uncompressed bytes actually written
1242 .Pq 0 in case of error .
1243 .It Fa int Fn gzprintf "gzFile file" "const char *format" "..." ;
1247 function converts, formats, and writes the args to the compressed file
1248 under control of the format string, as in
1251 returns the number of uncompressed bytes actually written
1252 .Pq 0 in case of error .
1253 .It Fa int Fn gzputs "gzFile file" "const char *s" ;
1257 function writes the given null-terminated string to the compressed file,
1258 excluding the terminating null character.
1261 returns the number of characters written, or \-1 in case of error.
1262 .It Fa char * Fn gzgets "gzFile file" "char *buf" "int len" ;
1266 function reads bytes from the compressed file until len\-1 characters are read,
1267 or a newline character is read and transferred to
1269 or an end-of-file condition is encountered.
1270 The string is then terminated with a null character.
1278 .It Fa int Fn gzputc "gzFile file" "int c" ;
1284 converted to an unsigned char, into the compressed file.
1286 returns the value that was written, or \-1 in case of error.
1287 .It Fa int Fn gzgetc "gzFile file" ;
1291 function reads one byte from the compressed file.
1293 returns this byte or \-1 in case of end of file or error.
1294 .It Fa int Fn gzflush "gzFile file" "int flush" ;
1298 function flushes all pending output into the compressed file.
1304 The return value is the
1306 error number (see function
1312 if the flush parameter is
1314 and all output could be flushed.
1317 should be called only when strictly necessary because it can
1318 degrade compression.
1319 .It Fa z_off_t Fn gzseek "gzFile file" "z_off_t offset" "int whence" ;
1321 Sets the starting position for the next
1325 on the given compressed file.
1326 The offset represents a number of bytes in the uncompressed data stream.
1327 The whence parameter is defined as in
1333 If the file is opened for reading, this function is emulated but can be
1335 If the file is opened for writing, only forward seeks are supported;
1337 then compresses a sequence of zeroes up to the new starting position.
1340 returns the resulting offset location as measured in bytes from
1341 the beginning of the uncompressed stream, or \-1 in case of error,
1342 in particular if the file is opened for writing and the new starting position
1343 would be before the current position.
1344 .It Fa int Fn gzrewind "gzFile file" ;
1348 function rewinds the given
1350 This function is supported only for reading.
1352 gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET).
1353 .It Fa z_off_t Fn gztell "gzFile file" ;
1357 function returns the starting position for the next
1361 on the given compressed file.
1362 This position represents a number of bytes in the uncompressed data stream.
1364 gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR).
1365 .It Fa int Fn gzeof "gzFile file" ;
1369 function returns 1 when
1371 has previously been detected reading the given input stream, otherwise zero.
1372 .It Fa int Fn gzclose "gzFile file" ;
1376 function flushes all pending output if necessary, closes the compressed file
1377 and deallocates all the (de)compression state.
1378 The return value is the
1380 error number (see function
1383 .It Fa const char * Fn gzerror "gzFile file" "int *errnum" ;
1387 function returns the error message for the last error which occurred on the
1394 If an error occurred in the file system and not in the compression library,
1398 and the application may consult errno to get the exact error code.
1400 .Sh CHECKSUM FUNCTIONS
1401 These functions are not related to compression but are exported
1402 anyway because they might be useful in applications using the
1403 compression library.
1405 .It Fa uLong Fn adler32 "uLong adler" "const Bytef *buf" "uInt len" ;
1408 function updates a running Adler-32 checksum with the bytes buf[0..len-1]
1409 and returns the updated checksum.
1414 this function returns the required initial value for the checksum.
1416 An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
1419 .Bd -literal -offset indent
1420 uLong adler = adler32(0L, Z_NULL, 0);
1422 while (read_buffer(buffer, length) != EOF) {
1423 adler = adler32(adler, buffer, length);
1425 if (adler != original_adler) error();
1427 .It Fa uLong Fn crc32 "uLong crc" "const Bytef *buf" "uInt len" ;
1430 function updates a running CRC with the bytes buf[0..len-1]
1431 and returns the updated CRC.
1436 this function returns the required initial value for the CRC.
1437 Pre- and post-conditioning
1438 .Pq one's complement
1439 is performed within this function so it shouldn't be done by the application.
1441 .Bd -literal -offset indent
1442 uLong crc = crc32(0L, Z_NULL, 0);
1444 while (read_buffer(buffer, length) != EOF) {
1445 crc = crc32(crc, buffer, length);
1447 if (crc != original_crc) error();
1452 struct internal_state;
1454 typedef struct z_stream_s {
1455 Bytef *next_in; /* next input byte */
1456 uInt avail_in; /* number of bytes available at next_in */
1457 uLong total_in; /* total nb of input bytes read so far */
1459 Bytef *next_out; /* next output byte should be put there */
1460 uInt avail_out; /* remaining free space at next_out */
1461 uLong total_out; /* total nb of bytes output so far */
1463 char *msg; /* last error message, NULL if no error */
1464 struct internal_state FAR *state; /* not visible by applications */
1466 alloc_func zalloc; /* used to allocate the internal state */
1467 free_func zfree; /* used to free the internal state */
1468 voidpf opaque; /* private data object passed to zalloc and zfree*/
1470 int data_type; /*best guess about the data type: ascii or binary*/
1471 uLong adler; /* Adler-32 value of the uncompressed data */
1472 uLong reserved; /* reserved for future use */
1475 typedef z_stream FAR * z_streamp;
1478 The application must update
1484 has dropped to zero.
1491 has dropped to zero.
1492 The application must initialize
1497 before calling the init function.
1498 All other fields are set by the compression library
1499 and must not be updated by the application.
1503 value provided by the application will be passed as the first
1504 parameter for calls to
1508 This can be useful for custom memory management.
1509 The compression library attaches no meaning to the
1516 if there is not enough memory for the object.
1519 is used in a multi-threaded application,
1523 must be thread safe.
1525 On 16-bit systems, the functions
1529 must be able to allocate exactly 65536 bytes,
1530 but will not be required to allocate more than this if the symbol MAXSEG_64K
1534 WARNING: On MSDOS, pointers returned by
1536 for objects of exactly 65536 bytes *must* have their offset normalized to zero.
1537 The default allocation function provided by this library ensures this (see
1539 To reduce memory requirements and avoid any allocation of 64K objects,
1540 at the expense of compression ratio,
1541 compile the library with -DMAX_WBITS=14 (see
1548 can be used for statistics or progress reports.
1551 holds the total size of the uncompressed data and may be saved for use
1553 (particularly if the decompressor wants to decompress everything
1557 #define Z_NO_FLUSH 0
1558 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
1559 #define Z_SYNC_FLUSH 2
1560 #define Z_FULL_FLUSH 3
1562 /* Allowed flush values; see deflate() below for details */
1565 #define Z_STREAM_END 1
1566 #define Z_NEED_DICT 2
1567 #define Z_ERRNO (-1)
1568 #define Z_STREAM_ERROR (-2)
1569 #define Z_DATA_ERROR (-3)
1570 #define Z_MEM_ERROR (-4)
1571 #define Z_BUF_ERROR (-5)
1572 #define Z_VERSION_ERROR (-6)
1573 /* Return codes for the compression/decompression functions.
1574 * Negative values are errors,
1575 * positive values are used for special but normal events.
1578 #define Z_NO_COMPRESSION 0
1579 #define Z_BEST_SPEED 1
1580 #define Z_BEST_COMPRESSION 9
1581 #define Z_DEFAULT_COMPRESSION (-1)
1582 /* compression levels */
1584 #define Z_FILTERED 1
1585 #define Z_HUFFMAN_ONLY 2
1586 #define Z_DEFAULT_STRATEGY 0
1587 /* compression strategy; see deflateInit2() below for details */
1592 /* Possible values of the data_type field */
1594 #define Z_DEFLATED 8
1595 /* The deflate compression method
1596 * (the only one supported in this version)
1599 #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
1601 #define zlib_version zlibVersion()
1602 /* for compatibility with versions \*[Lt] 1.0.2 */
1605 deflateInit and inflateInit are macros to allow checking the
1607 version and the compiler's view of
1610 .It Fa int Fn deflateInit_ "z_stream strm" "int level" \
1611 "const char *version" "int stream_size" ;
1612 .It Fa int Fn inflateInit_ "z_stream strm" "const char *version" \
1614 .It Fa int Fn deflateInit2_ "z_stream strm" "int level" "int method" \
1615 "int windowBits" "int memLevel" "int strategy" "const char *version" \
1617 .It Fa int Fn inflateInit2_ "z_stream strm" "int windowBits" \
1618 "const char *version" "int stream_size" ;
1619 .It Fa const char * Fn zError "int err" ;
1620 .It Fa int Fn inflateSyncPoint "z_streamp z" ;
1621 .It Fa const uLongf * Fn "get_crc_table" "void" ;
1624 .Bl -tag -width 12n -compact
1626 ZLIB Compressed Data Format Specification.
1628 DEFLATE Compressed Data Format Specification.
1630 GZIP File Format Specification.
1633 .Lk http://www.gzip.org/zlib/ "zlib"
1635 This manual page is based on an HTML version of
1638 .An piaip Aq Mt piaip@csie.ntu.edu.tw
1639 and was converted to mdoc format by the
1643 .An Jean-loup Gailly Aq Mt jloup@gzip.org
1644 .An Mark Adler Aq Mt madler@alumni.caltech.edu