xref: /openbsd-src/lib/libz/compress.3 (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1.\"	$OpenBSD: compress.3,v 1.21 2021/07/06 06:26:05 jmc Exp $
2.\"
3.\"  Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
4.\"
5.\"  This software is provided 'as-is', without any express or implied
6.\"  warranty.  In no event will the authors be held liable for any damages
7.\"  arising from the use of this software.
8.\"
9.\"  Permission is granted to anyone to use this software for any purpose,
10.\"  including commercial applications, and to alter it and redistribute it
11.\"  freely, subject to the following restrictions:
12.\"
13.\"  The origin of this software must not be misrepresented; you must not
14.\"  claim that you wrote the original software. If you use this software
15.\"  in a product, an acknowledgment in the product documentation would be
16.\"  appreciated but is not required.
17.\"  Altered source versions must be plainly marked as such, and must not be
18.\"  misrepresented as being the original software.
19.\"  This notice may not be removed or altered from any source distribution.
20.\"
21.\" Converted to mdoc format for the OpenBSD project
22.\" by Jason McIntyre <jmc@openbsd.org>
23.\"
24.\" This page corresponds to zlib version 1.2.3
25.\"
26.Dd $Mdocdate: July 6 2021 $
27.Dt COMPRESS 3
28.Os
29.Sh NAME
30.Nm compress ,
31.Nm zlibVersion ,
32.Nm deflateInit ,
33.Nm deflate ,
34.Nm deflateEnd ,
35.Nm inflateInit ,
36.Nm inflate ,
37.Nm inflateEnd ,
38.Nm deflateInit2 ,
39.Nm deflateSetDictionary ,
40.Nm deflateGetDictionary ,
41.Nm deflateCopy ,
42.Nm deflateReset ,
43.Nm deflateParams ,
44.Nm deflateTune ,
45.Nm deflateBound ,
46.Nm deflatePending ,
47.Nm deflatePrime ,
48.Nm deflateSetHeader ,
49.Nm inflateInit2 ,
50.Nm inflateSetDictionary ,
51.Nm inflateGetDictionary ,
52.Nm inflateSync ,
53.Nm inflateCopy ,
54.Nm inflateReset ,
55.Nm inflateReset2 ,
56.Nm inflatePrime ,
57.Nm inflateMark ,
58.Nm inflateGetHeader ,
59.Nm inflateBackInit ,
60.Nm inflateBack ,
61.Nm inflateBackEnd ,
62.Nm zlibCompileFlags ,
63.Nm compress2 ,
64.Nm compressBound ,
65.Nm uncompress ,
66.Nm uncompress2 ,
67.Nm gzopen ,
68.Nm gzdopen ,
69.Nm gzbuffer ,
70.Nm gzsetparams ,
71.Nm gzread ,
72.Nm gzfread ,
73.Nm gzwrite ,
74.Nm gzfwrite ,
75.Nm gzprintf ,
76.Nm gzputs ,
77.Nm gzgets ,
78.Nm gzputc ,
79.Nm gzgetc ,
80.Nm gzungetc ,
81.Nm gzflush ,
82.Nm gzseek ,
83.Nm gzrewind ,
84.Nm gztell ,
85.Nm gzoffset ,
86.Nm gzeof ,
87.Nm gzdirect ,
88.Nm gzclose ,
89.Nm gzclose_r ,
90.Nm gzclose_w ,
91.Nm gzerror ,
92.Nm gzclearerr ,
93.Nm adler32 ,
94.Nm adler32_z ,
95.Nm adler32_combine ,
96.Nm crc32 ,
97.Nm crc32_z ,
98.Nm crc32_combine
99.Nd zlib general purpose compression library
100.Sh SYNOPSIS
101.In zlib.h
102.Pp
103Basic functions
104.Pp
105.Ft const char *
106.Fn zlibVersion "void"
107.Ft int
108.Fn deflateInit "z_streamp strm" "int level"
109.Ft int
110.Fn deflate "z_streamp strm" "int flush"
111.Ft int
112.Fn deflateEnd "z_streamp strm"
113.Ft int
114.Fn inflateInit "z_streamp strm"
115.Ft int
116.Fn inflate "z_streamp strm" "int flush"
117.Ft int
118.Fn inflateEnd "z_streamp strm"
119.Pp
120Advanced functions
121.Pp
122.Ft int
123.Fn deflateInit2 "z_streamp strm" "int level" "int method" "int windowBits" "int memLevel" "int strategy"
124.Ft int
125.Fn deflateSetDictionary "z_streamp strm" "const Bytef *dictionary" "uInt dictLength"
126.Ft int
127.Fn deflateGetDictionary "z_streamp strm" "Bytef *dictionary" "uInt *dictLength"
128.Ft int
129.Fn deflateCopy "z_streamp dest" "z_streamp source"
130.Ft int
131.Fn deflateReset "z_streamp strm"
132.Ft int
133.Fn deflateParams "z_streamp strm" "int level" "int strategy"
134.Ft int
135.Fn deflateTune "z_streamp strm" "int good_length" "int max_lazy" "int nice_length" "int max_chain"
136.Ft uLong
137.Fn deflateBound "z_streamp strm" "uLong sourceLen"
138.Ft int
139.Fn deflatePending "z_streamp strm" "unsigned *pending" "int *bits"
140.Ft int
141.Fn deflatePrime "z_streamp strm" "int bits" "int value"
142.Ft int
143.Fn deflateSetHeader "z_streamp strm" "gz_headerp head"
144.Ft int
145.Fn inflateInit2 "z_streamp strm" "int windowBits"
146.Ft int
147.Fn inflateSetDictionary "z_streamp strm" "const Bytef *dictionary" "uInt dictLength"
148.Ft int
149.Fn inflateGetDictionary "z_streamp strm" "Bytef *dictionary" "uInt *dictLength"
150.Ft int
151.Fn inflateSync "z_streamp strm"
152.Ft int
153.Fn inflateCopy "z_streamp dst" "z_streamp source"
154.Ft int
155.Fn inflateReset "z_streamp strm"
156.Ft int
157.Fn inflateReset2 "z_streamp strm" "int windowBits"
158.Ft int
159.Fn inflatePrime "z_streamp strm" "int bits" "int value"
160.Ft int
161.Fn inflateMark "z_streamp strm"
162.Ft int
163.Fn inflateGetHeader "z_streamp strm" "gz_headerp head"
164.Ft int
165.Fn inflateBackInit "z_stream *strm" "int windowBits" "unsigned char FAR *window"
166.Ft int
167.Fn inflateBack "z_stream *strm" "in_func in" "void FAR *in_desc" "out_func out" "void FAR *out_desc"
168.Ft int
169.Fn inflateBackEnd "z_stream *strm"
170.Ft uLong
171.Fn zlibCompileFlags "void"
172.Pp
173Utility functions
174.Pp
175.Fd typedef voidp gzFile;
176.Pp
177.Ft int
178.Fn compress "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen"
179.Ft int
180.Fn compress2 "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen" "int level"
181.Ft uLong
182.Fn compressBound "uLong sourceLen"
183.Ft int
184.Fn uncompress "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen"
185.Ft int
186.Fn uncompress2 "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong *sourceLen"
187.Ft gzFile
188.Fn gzopen "const char *path" "const char *mode"
189.Ft gzFile
190.Fn gzdopen "int fd" "const char *mode"
191.Ft int
192.Fn gzbuffer "gzFile file" "unsigned size"
193.Ft int
194.Fn gzsetparams "gzFile file" "int level" "int strategy"
195.Ft int
196.Fn gzread "gzFile file" "voidp buf" "unsigned len"
197.Ft int
198.Fn gzfread "voidp buf" "z_size_t size" "z_size_t nitems" "gzFile file"
199.Ft int
200.Fn gzwrite "gzFile file" "voidpc buf" "unsigned len"
201.Ft int
202.Fn gzfwrite "voidpc buf" "z_size_t size" "z_size_t nitems" "gzFile file"
203.Ft int
204.Fn gzprintf "gzFile file" "const char *format" "..."
205.Ft int
206.Fn gzputs "gzFile file" "const char *s"
207.Ft char *
208.Fn gzgets "gzFile file" "char *buf" "int len"
209.Ft int
210.Fn gzputc "gzFile file" "int c"
211.Ft int
212.Fn gzgetc "gzFile file"
213.Ft int
214.Fn gzungetc "int c" "gzFile file"
215.Ft int
216.Fn gzflush "gzFile file" "int flush"
217.Ft z_off_t
218.Fn gzseek "gzFile file" "z_off_t offset" "int whence"
219.Ft int
220.Fn gzrewind "gzFile file"
221.Ft z_off_t
222.Fn gztell "gzFile file"
223.Ft int
224.Fn gzoffset "gzFile file"
225.Ft int
226.Fn gzeof "gzFile file"
227.Ft int
228.Fn gzdirect "gzFile file"
229.Ft int
230.Fn gzclose "gzFile file"
231.Ft int
232.Fn gzclose_r "gzFile file"
233.Ft int
234.Fn gzclose_w "gzFile file"
235.Ft const char *
236.Fn gzerror "gzFile file" "int *errnum"
237.Ft void
238.Fn gzclearerr "gzFile file"
239.Pp
240Checksum functions
241.Pp
242.Ft uLong
243.Fn adler32 "uLong adler" "const Bytef *buf" "uInt len"
244.Ft uLong
245.Fn adler32_z "uLong adler" "const Bytef *buf" "z_size_t len"
246.Ft uLong
247.Fn adler32_combine "uLong adler1" "uLong adler2" "z_off_t len2"
248.Ft uLong
249.Fn crc32 "uLong crc" "const Bytef *buf" "uInt len"
250.Ft uLong
251.Fn crc32_z "uLong adler" "const Bytef *buf" "z_size_t len"
252.Ft uLong
253.Fn crc32_combine "uLong crc1" "uLong crc2" "z_off_t len2"
254.Sh DESCRIPTION
255This manual page describes the
256.Nm zlib
257general purpose compression library, version 1.2.11.
258.Pp
259The
260.Nm zlib
261compression library provides in-memory compression and decompression functions,
262including integrity checks of the uncompressed data.
263This version of the library supports only one compression method
264.Pq deflation
265but other algorithms will be added later and will have the same
266stream interface.
267.Pp
268Compression can be done in a single step if the buffers are large enough
269or can be done by repeated calls of the compression function.
270In the latter case, the application must provide more input
271and/or consume the output
272.Pq providing more output space
273before each call.
274.Pp
275The compressed data format used by default by the in-memory functions is the
276.Nm zlib
277format, which is a zlib wrapper documented in RFC 1950,
278wrapped around a deflate stream, which is itself documented in RFC 1951.
279.Pp
280The library also supports reading and writing files in
281.Xr gzip 1
282.Pq .gz
283format with an interface similar to that of
284.Xr stdio 3
285using the functions that start with
286.Qq gz .
287The gzip format is different from the zlib format.
288gzip is a gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
289This library can optionally read and write gzip and raw deflate streams
290in memory as well.
291.Pp
292The zlib format was designed to be compact and fast for use in memory
293and on communications channels.
294The gzip format was designed for single-file compression on file systems,
295has a larger header than zlib to maintain directory information,
296and uses a different, slower, check method than zlib.
297.Pp
298The library does not install any signal handler.
299The decoder checks the consistency of the compressed data,
300so the library should never crash even in the case of corrupted input.
301.Pp
302The functions within the library are divided into the following sections:
303.Pp
304.Bl -dash -offset indent -compact
305.It
306Basic functions
307.It
308Advanced functions
309.It
310Utility functions
311.It
312Checksum functions
313.El
314.Sh BASIC FUNCTIONS
315.Bl -tag -width Ds
316.It Xo
317.Fa const char *
318.Fn zlibVersion "void" ;
319.Xc
320.Pp
321The application can compare
322.Fn zlibVersion
323and
324.Dv ZLIB_VERSION
325for consistency.
326If the first character differs, the library code actually used is
327not compatible with the
328.In zlib.h
329header file used by the application.
330This check is automatically made by
331.Fn deflateInit
332and
333.Fn inflateInit .
334.It Xo
335.Fa int
336.Fn deflateInit "z_streamp strm" "int level" ;
337.Xc
338.Pp
339The
340.Fn deflateInit
341function initializes the internal stream state for compression.
342The fields
343.Fa zalloc ,
344.Fa zfree ,
345and
346.Fa opaque
347must be initialized before by the caller.
348If
349.Fa zalloc
350and
351.Fa zfree
352are set to
353.Dv NULL ,
354.Fn deflateInit
355updates them to use default allocation functions.
356.Pp
357The compression level must be
358.Dv Z_DEFAULT_COMPRESSION ,
359or between 0 and 9:
3601 gives best speed, 9 gives best compression, 0 gives no compression at all
361(the input data is simply copied a block at a time).
362.Pp
363.Dv Z_DEFAULT_COMPRESSION
364requests a default compromise between speed and compression
365.Pq currently equivalent to level 6 .
366.Pp
367.Fn deflateInit
368returns
369.Dv Z_OK
370if successful,
371.Dv Z_MEM_ERROR
372if there was not enough memory,
373.Dv Z_STREAM_ERROR
374if level is not a valid compression level,
375.Dv Z_VERSION_ERROR
376if the
377.Nm zlib
378library version
379.Pq zlib_version
380is incompatible with the version assumed by the caller
381.Pq ZLIB_VERSION .
382.Fa msg
383is set to null if there is no error message.
384.Fn deflateInit
385does not perform any compression: this will be done by
386.Fn deflate .
387.It Xo
388.Fa int
389.Fn deflate "z_streamp strm" "int flush" ;
390.Xc
391.Pp
392.Fn deflate
393compresses as much data as possible, and stops when the input
394buffer becomes empty or the output buffer becomes full.
395It may introduce some output latency
396.Pq reading input without producing any output
397except when forced to flush.
398.Pp
399The detailed semantics are as follows.
400.Fn deflate
401performs one or both of the following actions:
402.Pp
403Compress more input starting at
404.Fa next_in
405and update
406.Fa next_in
407and
408.Fa avail_in
409accordingly.
410If not all input can be processed
411(because there is not enough room in the output buffer),
412.Fa next_in
413and
414.Fa avail_in
415are updated and processing will resume at this point for the next call to
416.Fn deflate .
417.Pp
418Generate more output starting at
419.Fa next_out
420and update
421.Fa next_out
422and
423.Fa avail_out
424accordingly.
425This action is forced if the parameter
426.Fa flush
427is non-zero.
428Forcing
429.Fa flush
430frequently degrades the compression ratio,
431so this parameter should be set only when necessary.
432Some output may be provided even if
433.Fa flush
434is not set.
435.Pp
436Before the call to
437.Fn deflate ,
438the application should ensure that at least
439one of the actions is possible, by providing more input and/or consuming
440more output, and updating
441.Fa avail_in
442or
443.Fa avail_out
444accordingly;
445.Fa avail_out
446should never be zero before the call.
447The application can consume the compressed output when it wants,
448for example when the output buffer is full
449.Pq avail_out == 0 ,
450or after each call to
451.Fn deflate .
452If
453.Fn deflate
454returns
455.Dv Z_OK
456and with zero
457.Fa avail_out ,
458it must be called again after making room in the
459output buffer because there might be more output pending.
460See
461.Fn deflatePending ,
462which can be used if desired to determine whether or not there is more output
463in that case.
464.Pp
465Normally the parameter
466.Fa flush
467is set to
468.Dv Z_NO_FLUSH ,
469which allows
470.Fn deflate
471to decide how much data to accumulate before producing output,
472in order to maximise compression.
473.Pp
474If the parameter
475.Fa flush
476is set to
477.Dv Z_SYNC_FLUSH ,
478all pending output is flushed to the output buffer and the output
479is aligned on a byte boundary,
480so that the decompressor can get all input data available so far.
481(In particular
482.Fa avail_in
483is zero after the call
484if enough output space has been provided before the call.)
485Flushing may degrade compression for some compression algorithms
486and so it should be used only when necessary.
487This completes the current deflate block and follows it with
488an empty stored block that is three bits plus filler bits to the next byte,
489followed by four bytes (00 00 ff ff).
490.Pp
491If
492.Fa flush
493is set to
494.Dv Z_PARTIAL_FLUSH ,
495all pending output is flushed to the output buffer,
496but the output is not aligned to a byte boundary.
497All of the input data so far will be available to the decompressor, as for
498.Dv Z_SYNC_FLUSH .
499This completes the current deflate block and follows it with an empty fixed
500code block that is 10 bits long.
501This assures that enough bytes are output in order for the decompressor to
502finish the block before the empty fixed codes block.
503.Pp
504If
505.Fa flush
506is set to
507.Dv Z_BLOCK ,
508a deflate block is completed and emitted, as for
509.Dv Z_SYNC_FLUSH ,
510but the output is not aligned on a byte boundary,
511and up to seven bits of the current block are held to be written as
512the next byte after the next deflate block is completed.
513In this case, the decompressor may not be provided enough bits at this point in
514order to complete decompression of the data provided so far to the compressor.
515It may need to wait for the next block to be emitted.
516This is for advanced applications that need to control
517the emission of deflate blocks.
518.Pp
519If
520.Fa flush
521is set to
522.Dv Z_FULL_FLUSH ,
523all output is flushed as with
524.Dv Z_SYNC_FLUSH ,
525and the compression state is reset so that decompression can restart from this
526point if previous compressed data has been damaged or if random access
527is desired.
528Using
529.Dv Z_FULL_FLUSH
530too often can seriously degrade compression.
531.Pp
532If
533.Fn deflate
534returns with avail_out == 0, this function must be called again
535with the same value of the flush parameter and more output space
536(updated
537.Fa avail_out ) ,
538until the flush is complete
539.Pf ( Fn deflate
540returns with non-zero
541.Fa avail_out ) .
542In the case of a
543.Dv Z_FULL_FLUSH
544or a
545.Dv Z_SYNC_FLUSH ,
546make sure that
547.Fa avail_out
548is greater than six to avoid repeated flush markers due to avail_out == 0
549on return.
550.Pp
551If the parameter
552.Fa flush
553is set to
554.Dv Z_FINISH ,
555pending input is processed, pending output is flushed and
556.Fn deflate
557returns with
558.Dv Z_STREAM_END
559if there was enough output space.
560If
561.Fn deflate
562returns with
563.Dv Z_OK
564or
565.Dv Z_BUF_ERROR ,
566this function must be called again with
567.Dv Z_FINISH
568and more output space
569(updated
570.Fa avail_out
571but no more input data, until it returns with
572.Dv Z_STREAM_END
573or an error.
574After
575.Fn deflate
576has returned
577.Dv Z_STREAM_END ,
578the only possible operations on the stream are
579.Fn deflateReset
580or
581.Fn deflateEnd .
582.Pp
583.Dv Z_FINISH
584can be used in the first deflate call after
585.Fn deflateInit
586if all the compression is to be done in a single step.
587In order to complete in one call,
588.Fa avail_out
589must be at least the value returned by
590.Fn deflateBound
591(see below).
592Then
593.Fn deflate
594is guaranteed to return
595.Dv Z_STREAM_END .
596If not enough output space is provided,
597.Fn deflate
598will not return
599.Dv Z_STREAM_END ,
600and it must be called again as described above.
601.Pp
602.Fn deflate
603sets strm->adler to the Adler-32 checksum of all input read so far
604(that is,
605.Fa total_in
606bytes).
607If a gzip stream is being generated,
608then strm->adler will be the CRC-32 checksum of the input read so far.
609(See
610.Fn deflateInit2
611below.)
612.Pp
613.Fn deflate
614may update strm->data_type
615if it can make a good guess about the input data type
616.Pq Z_BINARY or Z_TEXT .
617If in doubt, the data is considered binary.
618This field is only for information purposes and does not affect
619the compression algorithm in any manner.
620.Pp
621.Fn deflate
622returns
623.Dv Z_OK
624if some progress has been made
625.Pq more input processed or more output produced ,
626.Dv Z_STREAM_END
627if all input has been consumed and all output has been produced
628(only when
629.Fa flush
630is set to
631.Dv Z_FINISH ) ,
632.Dv Z_STREAM_ERROR
633if the stream state was inconsistent
634(for example, if
635.Fa next_in
636or
637.Fa next_out
638was
639.Dv NULL
640or the state was inadvertently written over by the application), or
641.Dv Z_BUF_ERROR
642if no progress is possible
643(for example,
644.Fa avail_in
645or
646.Fa avail_out
647was zero).
648Note that
649.Dv Z_BUF_ERROR
650is not fatal, and
651.Fn deflate
652can be called again with more input and more output space
653to continue compressing.
654.It Xo
655.Fa int
656.Fn deflateEnd "z_streamp strm" ;
657.Xc
658.Pp
659All dynamically allocated data structures for this stream are freed.
660This function discards any unprocessed input and does not flush any
661pending output.
662.Pp
663.Fn deflateEnd
664returns
665.Dv Z_OK
666if successful,
667.Dv Z_STREAM_ERROR
668if the stream state was inconsistent,
669.Dv Z_DATA_ERROR
670if the stream was freed prematurely
671.Pq some input or output was discarded .
672In the error case,
673.Fa msg
674may be set but then points to a static string
675.Pq which must not be deallocated .
676.It Xo
677.Fa int
678.Fn inflateInit "z_streamp strm" ;
679.Xc
680The
681.Fn inflateInit
682function initializes the internal stream state for decompression.
683The fields
684.Fa next_in ,
685.Fa avail_in ,
686.Fa zalloc ,
687.Fa zfree ,
688and
689.Fa opaque
690must be initialized before by the caller.
691In the current version of
692.Fn inflate ,
693the provided input is not read or consumed.
694The allocation of a sliding window will be deferred to the first call of
695.Fn inflate
696(if the decompression does not complete on the first call).
697If
698.Fa zalloc
699and
700.Fa zfree
701are set to
702.Dv NULL ,
703.Fn inflateInit
704updates them to use default allocation functions.
705.Pp
706.Fn inflateInit
707returns
708.Dv Z_OK
709if successful,
710.Dv Z_MEM_ERROR
711if there was not enough memory,
712.Dv Z_VERSION_ERROR
713if the
714.Nm zlib
715library version is incompatible with the version assumed by the caller or
716.Dv Z_STREAM_ERROR
717if the parameters are invalid, such as a null pointer to the structure.
718.Fa msg
719is set to null if there is no error message.
720.Fn inflateInit
721does not perform any decompression.
722Actual decompression will be done by
723.Fn inflate .
724So
725.Fa next_in , avail_in , next_out ,
726and
727.Fa avail_out
728are unused and unchanged.
729The current implementation of
730.Fn inflateInit
731does not process any header information \(em
732that is deferred until
733.Fn inflate
734is called.
735.It Xo
736.Fa int
737.Fn inflate "z_streamp strm" "int flush" ;
738.Xc
739.Fn inflate
740decompresses as much data as possible, and stops when the input
741buffer becomes empty or the output buffer becomes full.
742It may introduce some output latency
743.Pq reading input without producing any output
744except when forced to flush.
745.Pp
746The detailed semantics are as follows.
747.Fn inflate
748performs one or both of the following actions:
749.Pp
750Decompress more input starting at
751.Fa next_in
752and update
753.Fa next_in
754and
755.Fa avail_in
756accordingly.
757If not all input can be processed
758(because there is not enough room in the output buffer), then
759.Fa next_in
760and
761.Fa avail_in
762are updated accordingly,
763and processing will resume at this point for the next call to
764.Fn inflate .
765.Pp
766Generate more output starting at
767.Fa next_out
768and update
769.Fa next_out
770and
771.Fa avail_out
772accordingly.
773.Fn inflate
774provides as much output as possible,
775until there is no more input data or no more space in the output buffer
776.Pq see below about the flush parameter .
777.Pp
778Before the call to
779.Fn inflate ,
780the application should ensure that at least one of the actions is possible,
781by providing more input and/or consuming more output,
782and updating the next_* and avail_* values accordingly.
783If the caller of
784.Fn inflate
785does not provide both available input and available output space,
786it is possible that there will be no progress made.
787The application can consume the uncompressed output when it wants,
788for example when the output buffer is full (avail_out == 0),
789or after each call to
790.Fn inflate .
791If
792.Fn inflate
793returns
794.Dv Z_OK
795and with zero
796.Fa avail_out ,
797it must be called again after making room
798in the output buffer because there might be more output pending.
799.Pp
800The
801.Fa flush
802parameter of
803.Fn inflate
804can be
805.Dv Z_NO_FLUSH , Z_SYNC_FLUSH , Z_FINISH , Z_BLOCK
806or
807.Dv Z_TREES .
808.Dv Z_SYNC_FLUSH
809requests that
810.Fn inflate
811flush as much output as possible to the output buffer.
812.Dv Z_BLOCK
813requests that
814.Fn inflate
815stop if and when it gets to the next deflate block boundary.
816When decoding the zlib or gzip format, this will cause
817.Fn inflate
818to return immediately after the header and before the first block.
819When doing a raw inflate,
820.Fn inflate
821will go ahead and process the first block,
822and will return when it gets to the end of that block,
823or when it runs out of data.
824.Pp
825The
826.Dv Z_BLOCK
827option assists in appending to or combining deflate streams.
828To assist in this, on return
829.Fn inflate
830always sets strm->data_type to the number of unused bits
831in the last byte taken from strm->next_in, plus 64 if
832.Fn inflate
833is currently decoding the last block in the deflate stream, plus 128 if
834.Fn inflate
835returned immediately after decoding an end-of-block code or decoding the
836complete header up to just before the first byte of the deflate stream.
837The end-of-block will not be indicated until all of the uncompressed
838data from that block has been written to strm->next_out.
839The number of unused bits may in general be greater than seven,
840except when bit 7 of data_type is set,
841in which case the number of unused bits will be less than eight.
842.Fa data_type
843is set as noted here every time
844.Fn inflate
845returns for all flush options,
846and so can be used to determine the amount of currently consumed input in bits.
847.Pp
848The
849.Dv Z_TREES
850option behaves as
851.Dv Z_BLOCK
852does, but it also returns when the end of each deflate block header is reached,
853before any actual data in that block is decoded.
854This allows the caller to determine the length of the deflate block header for
855later use in random access within a deflate block.
856256 is added to the value of strm->data_type when
857.Fn inflate
858returns immediately after reaching the end of the deflate block header.
859.Pp
860.Fn inflate
861should normally be called until it returns
862.Dv Z_STREAM_END
863or an error.
864However if all decompression is to be performed in a single step
865.Pq a single call to inflate ,
866the parameter
867.Fa flush
868should be set to
869.Dv Z_FINISH .
870In this case all pending input is processed and all pending output is flushed;
871.Fa avail_out
872must be large enough to hold all the uncompressed data
873for the operation to complete.
874(The size of the uncompressed data may have been saved
875by the compressor for this purpose.)
876The use of
877.Dv Z_FINISH
878is not required to perform an inflation in one step.
879However it may be used to inform
880.Fn inflate
881that a faster approach can be used for the single
882.Fn inflate
883call.
884.Dv Z_FINISH
885also informs
886.Fn inflate
887to not maintain a sliding window if the stream completes,
888which reduces its memory footprint.
889If the stream does not complete,
890either because not all of the stream is provided or not enough output space
891is provided, then a sliding window will be allocated and
892.Fn inflate
893can be called again to continue the operation as if
894.Dv Z_NO_FLUSH
895had been used.
896.Pp
897In this implementation,
898.Fn inflate
899always flushes as much output as possible to the output buffer,
900and always uses the faster approach on the first call.
901So the effects of the flush parameter in this implementation are
902on the return value of
903.Fn inflate
904as noted below,
905when
906.Fn inflate
907returns early when
908.Dv Z_BLOCK
909or
910.Dv Z_TREES
911is used, and when
912.Fn inflate
913avoids the allocation of memory for a sliding window when
914.Dv Z_FINISH
915is used.
916.Pp
917If a preset dictionary is needed after this call (see
918.Fn inflateSetDictionary
919below),
920.Fn inflate
921sets strm->adler to the Adler-32 checksum of the dictionary
922chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
923strm->adler to the Adler-32 checksum of all output produced so far
924(that is,
925.Fa total_out
926bytes) and returns
927.Dv Z_OK , Z_STREAM_END
928or an error code as described below.
929At the end of the stream,
930.Fn inflate
931checks that its computed Adler-32 checksum is equal to that saved by
932the compressor and returns
933.Dv Z_STREAM_END
934only if the checksum is correct.
935.Pp
936.Fn inflate
937can decompress and check either zlib-wrapped or gzip-wrapped deflate data.
938The header type is detected automatically, if requested when initializing with
939.Fn inflateInit2 .
940Any information contained in the gzip header is not retained unless
941.Fn inflateGetHeader
942is used.
943When processing gzip-wrapped deflate data, strm->adler32 is set to the CRC-32
944of the output produced so far.
945The CRC-32 is checked against the gzip trailer,
946as is the uncompressed length, modulo 2^32.
947.Pp
948.Fn inflate
949returns
950.Dv Z_OK
951if some progress has been made
952.Pq more input processed or more output produced ,
953.Dv Z_STREAM_END
954if the end of the compressed data has been reached and all uncompressed output
955has been produced,
956.Dv Z_NEED_DICT
957if a preset dictionary is needed at this point,
958.Dv Z_DATA_ERROR
959if the input data was corrupted (input stream not conforming to the
960.Nm zlib
961format or incorrect check value,
962in which case strm->msg points to a string with a more specific error),
963.Dv Z_STREAM_ERROR
964if the stream structure was inconsistent
965(for example,
966.Fa next_in
967or
968.Fa next_out
969was
970.Dv NULL ,
971or the state was inadvertently over by the application),
972.Dv Z_MEM_ERROR
973if there was not enough memory,
974.Dv Z_BUF_ERROR
975if no progress was possible or if there was not enough room in the output buffer
976when
977.Dv Z_FINISH
978is used.
979Note that
980.Dv Z_BUF_ERROR
981is not fatal, and
982.Fn inflate
983can be called again with more input and more output space
984to continue compressing.
985If
986.Dv Z_DATA_ERROR
987is returned, the application may then call
988.Fn inflateSync
989to look for a good compression block if a partial recovery
990of the data is desired.
991.It Xo
992.Fa int
993.Fn inflateEnd "z_streamp strm" ;
994.Xc
995All dynamically allocated data structures for this stream are freed.
996This function discards any unprocessed input and does not flush any
997pending output.
998.Pp
999.Fn inflateEnd
1000returns
1001.Dv Z_OK
1002if successful, or
1003.Dv Z_STREAM_ERROR
1004if the stream state was inconsistent.
1005In the error case,
1006.Fa msg
1007may be set but then points to a static string
1008.Pq which must not be deallocated .
1009.El
1010.Sh ADVANCED FUNCTIONS
1011The following functions are needed only in some special applications.
1012.Bl -tag -width Ds
1013.It Xo
1014.Fa int
1015.Fn deflateInit2 "z_streamp strm" "int level" "int method" "int windowBits" "int memLevel" "int strategy" ;
1016.Xc
1017.Pp
1018This is another version of
1019.Fn deflateInit
1020with more compression options.
1021The fields
1022.Fa next_in ,
1023.Fa zalloc ,
1024.Fa zfree ,
1025and
1026.Fa opaque
1027must be initialized before by the caller.
1028.Pp
1029The
1030.Fa method
1031parameter is the compression method.
1032It must be
1033.Dv Z_DEFLATED
1034in this version of the library.
1035.Pp
1036The
1037.Fa windowBits
1038parameter is the base two logarithm of the window size
1039.Pq the size of the history buffer .
1040It should be in the range 8..15 for this version of the library.
1041Larger values of this parameter result in better compression
1042at the expense of memory usage.
1043The default value is 15 if
1044.Fn deflateInit
1045is used instead.
1046.Pp
1047For the current implementation of
1048.Fn deflate ,
1049a
1050.Fa windowBits
1051value of 8 (a window size of 256 bytes) is not supported.
1052As a result, a request for 8 will result in 9 (a 512-byte window).
1053In that case, providing 8 to
1054.Fn inflateInit2
1055will result in an error when the zlib header with 9 is
1056checked against the initialization of
1057.Fn inflate .
1058The remedy is to not use 8 with
1059.Fn deflateInit2
1060with this initialization, or at least in that case use 9 with
1061.Fn inflateInit2 .
1062.Pp
1063.Fa windowBits
1064can also be -8..-15 for raw deflate.
1065In this case, -windowBits determines the window size.
1066.Fn deflate
1067will then generate raw deflate data with no zlib header or trailer,
1068and will not compute a check value.
1069.Pp
1070.Fa windowBits
1071can also be greater than 15 for optional gzip encoding.
1072Add 16 to
1073.Fa windowBits
1074to write a simple gzip header and trailer around the
1075compressed data instead of a zlib wrapper.
1076The gzip header will have no file name, no extra data, no comment,
1077no modification time
1078.Pq set to zero ,
1079no header crc, and the operating system will be set to
1080the appropriate value,
1081if the operating system was determined at compile time.
1082If a gzip stream is being written,
1083strm->adler is a CRC-32 instead of an Adler-32.
1084.Pp
1085For raw deflate or gzip encoding, a request for a 256-byte window is
1086rejected as invalid, since only the zlib header provides a means of
1087transmitting the window size to the decompressor.
1088.Pp
1089The
1090.Fa memLevel
1091parameter specifies how much memory should be allocated
1092for the internal compression state.
1093memLevel=1 uses minimum memory but is slow and reduces compression ratio;
1094memLevel=9 uses maximum memory for optimal speed.
1095The default value is 8.
1096See
1097.In zconf.h
1098for total memory usage as a function of
1099.Fa windowBits
1100and
1101.Fa memLevel .
1102.Pp
1103The
1104.Fa strategy
1105parameter is used to tune the compression algorithm.
1106Use the value
1107.Dv Z_DEFAULT_STRATEGY
1108for normal data;
1109.Dv Z_FILTERED
1110for data produced by a filter
1111.Pq or predictor ;
1112.Dv Z_HUFFMAN_ONLY
1113to force Huffman encoding only
1114.Pq no string match ,
1115or
1116.Dv Z_RLE
1117to limit match distances to one
1118.Pq run-length encoding .
1119Filtered data consists mostly of small values with a
1120somewhat random distribution.
1121In this case, the compression algorithm is tuned to compress them better.
1122The effect of
1123.Dv Z_FILTERED
1124is to force more Huffman coding and less string matching;
1125it is somewhat intermediate between
1126.Dv Z_DEFAULT_STRATEGY
1127and
1128.Dv Z_HUFFMAN_ONLY .
1129.Dv Z_RLE
1130is designed to be almost as fast as
1131.Dv Z_HUFFMAN_ONLY ,
1132but gives better compression for PNG image data.
1133The
1134.Fa strategy
1135parameter only affects the compression ratio but not the correctness of the
1136compressed output, even if it is not set appropriately.
1137.Dv Z_FIXED
1138prevents the use of dynamic Huffman codes,
1139allowing for a simpler decoder for special applications.
1140.Pp
1141.Fn deflateInit2
1142returns
1143.Dv Z_OK
1144if successful,
1145.Dv Z_MEM_ERROR
1146if there was not enough memory,
1147.Dv Z_STREAM_ERROR
1148if any parameter is invalid
1149.Pq such as an invalid method ,
1150or
1151.Dv Z_VERSION_ERROR
1152if the zlib library version (zlib_version) is
1153incompatible with the version assumed by the caller (ZLIB_VERSION).
1154.Fa msg
1155is set to null if there is no error message.
1156.Fn deflateInit2
1157does not perform any compression: this will be done by
1158.Fn deflate .
1159.It Xo
1160.Fa int
1161.Fn deflateSetDictionary "z_streamp strm" "const Bytef *dictionary" "uInt dictLength" ;
1162.Xc
1163.Pp
1164Initializes the compression dictionary from the given byte sequence
1165without producing any compressed output.
1166When using the zlib format, this function must be called immediately after
1167.Fn deflateInit , deflateInit2
1168or
1169.Fn deflateReset ,
1170and before any call of
1171.Fn deflate .
1172When doing raw deflate, this function must be called either before any call of
1173.Fn deflate ,
1174or immediately after the completion of a deflate block,
1175i.e. after all input has been consumed and all output has been delivered
1176when using any of the flush options
1177.Dv Z_BLOCK , Z_PARTIAL_FLUSH , Z_SYNC_FLUSH ,
1178or
1179.Dv Z_FULL_FLUSH .
1180The compressor and decompressor must use exactly the same dictionary
1181(see
1182.Fn inflateSetDictionary ) .
1183.Pp
1184The dictionary should consist of strings
1185.Pq byte sequences
1186that are likely to be encountered later in the data to be compressed,
1187with the most commonly used strings preferably put towards
1188the end of the dictionary.
1189Using a dictionary is most useful when the data to be compressed is short
1190and can be predicted with good accuracy;
1191the data can then be compressed better than with the default empty dictionary.
1192.Pp
1193Depending on the size of the compression data structures selected by
1194.Fn deflateInit
1195or
1196.Fn deflateInit2 ,
1197a part of the dictionary may in effect be discarded,
1198for example if the dictionary is larger than the window size provided in
1199.Fn deflateInit
1200or
1201.Fn deflateInit2 .
1202Thus the strings most likely to be useful should be
1203put at the end of the dictionary, not at the front.
1204In addition, the current implementation of
1205.Fn deflate
1206will use at most the window size minus 262 bytes of the provided dictionary.
1207.Pp
1208Upon return of this function, strm->adler is set to the Adler-32 value
1209of the dictionary; the decompressor may later use this value to determine
1210which dictionary has been used by the compressor.
1211(The Adler-32 value applies to the whole dictionary even if only a subset
1212of the dictionary is actually used by the compressor.)
1213If a raw deflate was requested, then the Adler-32 value is not computed
1214and strm->adler is not set.
1215.Pp
1216.Fn deflateSetDictionary
1217returns
1218.Dv Z_OK
1219if successful,
1220or
1221.Dv Z_STREAM_ERROR
1222if a parameter is invalid
1223.Pq e.g. dictionary being NULL
1224or the stream state is inconsistent
1225(for example if
1226.Fn deflate
1227has already been called for this stream or if not at a block boundary for raw
1228deflate).
1229.Fn deflateSetDictionary
1230does not perform any compression: this will be done by
1231.Fn deflate .
1232.It Xo
1233.Fa int
1234.Fn deflateGetDictionary "z_streamp strm" "Bytef *dictionary uInt *dictLength" ;
1235.Xc
1236.Pp
1237Returns the sliding dictionary being maintained by
1238.Fn deflate .
1239.Fa dictLength
1240is set to the number of bytes in the dictionary, and that many bytes are copied
1241to
1242.Fa dictionary .
1243.Fa dictionary
1244must have enough space, where 32768 bytes is always enough.
1245If
1246.Fn deflateGetDictionary
1247is called with dictionary equal to
1248.Dv NULL ,
1249then only the dictionary length is returned, and nothing is copied.
1250Similary, if
1251.Fa dictLength
1252is
1253.Dv NULL ,
1254then it is not set.
1255.Pp
1256.Fn deflateGetDictionary
1257may return a length less than the window size,
1258even when more than the window size in input has been provided.
1259It may return up to 258 bytes less in that case,
1260due to how zlib's implementation of
1261.Fn deflate
1262manages the sliding window and lookahead for matches,
1263where matches can be up to 258 bytes long.
1264If the application needs the last window-size bytes of input,
1265then that would need to be saved by the application outside of
1266.Nm zlib .
1267.Pp
1268.Fn deflateGetDictionary
1269returns
1270.Dv Z_OK
1271on success, or
1272.Dv Z_STREAM_ERROR
1273if the stream state is inconsistent.
1274.It Xo
1275.Fa int
1276.Fn deflateCopy "z_streamp dest" "z_streamp source" ;
1277.Xc
1278.Pp
1279The
1280.Fn deflateCopy
1281function sets the destination stream as a complete copy of the source stream.
1282.Pp
1283This function can be useful when several compression strategies will be
1284tried, for example when there are several ways of pre-processing the input
1285data with a filter.
1286The streams that will be discarded should then be freed by calling
1287.Fn deflateEnd .
1288Note that
1289.Fn deflateCopy
1290duplicates the internal compression state which can be quite large,
1291so this strategy is slow and can consume lots of memory.
1292.Pp
1293.Fn deflateCopy
1294returns
1295.Dv Z_OK
1296if successful,
1297.Dv Z_MEM_ERROR
1298if there was not enough memory,
1299.Dv Z_STREAM_ERROR
1300if the source stream state was inconsistent
1301(such as
1302.Fa zalloc
1303being NULL).
1304.Fa msg
1305is left unchanged in both source and destination.
1306.It Xo
1307.Fa int
1308.Fn deflateReset "z_streamp strm" ;
1309.Xc
1310.Pp
1311This function is equivalent to
1312.Fn deflateEnd
1313followed by
1314.Fn deflateInit ,
1315but does not free and reallocate the internal compression state.
1316The stream will leave the compression level and any other attributes
1317that may have been set unchanged.
1318.Pp
1319.Fn deflateReset
1320returns
1321.Dv Z_OK
1322if successful, or
1323.Dv Z_STREAM_ERROR
1324if the source stream state was inconsistent
1325(such as
1326.Fa zalloc
1327or
1328.Fa state
1329being NULL).
1330.It Xo
1331.Fa int
1332.Fn deflateParams "z_streamp strm" "int level" "int strategy" ;
1333.Xc
1334.Pp
1335The
1336.Fn deflateParams
1337function dynamically updates the compression level and compression strategy.
1338The interpretation of level and strategy is as in
1339.Fn deflateInit2 .
1340This can be used to switch between compression and straight copy
1341of the input data, or to switch to a different kind of input data
1342requiring a different strategy.
1343If the compression approach (which is a function of the level) or the
1344strategy is changed, and if any input has been consumed in a previous
1345.Fn deflate
1346call, then the input available so far is compressed with the old
1347level and strategy using deflate(strm, Z_BLOCK).
1348There are three approaches for the compression levels 0, 1..3, and 4..9,
1349respectively.
1350The new level and strategy will take effect at the next call of
1351.Fn deflate .
1352.Pp
1353If a deflate(strm, Z_BLOCK) is performed by
1354.Fn deflateParams ,
1355and it does not have enough output space to complete,
1356then the parameter change will not take effect.
1357In this case,
1358.Fn deflateParams
1359can be called again with the same parameters and more output space to try again.
1360.Pp
1361In order to assure a change in the parameters on the first try, the
1362deflate stream should be flushed using
1363.Fn deflate
1364with
1365.Dv Z_BLOCK
1366or other flush request until
1367.Fa strm.avail_out
1368is not zero, before calling
1369.Fn deflateParams .
1370Then no more input data should be provided before the
1371.Fn deflateParams
1372call.
1373If this is done, the old level and strategy will be applied to the data
1374compressed before
1375.Fn deflateParams ,
1376and the new level and strategy will be applied to the the data compressed after
1377.Fn deflateParams .
1378.Pp
1379.Fn deflateParams
1380returns
1381.Dv Z_OK
1382on success,
1383.Dv Z_STREAM_ERROR
1384if the source stream state was inconsistent or if a parameter was invalid, or
1385.Dv Z_BUF_ERROR
1386if there was not enough output space to complete the compression of the
1387available input data before a change in the strategy or approach.
1388Note that in the case of a
1389.Dv Z_BUF_ERROR ,
1390the parameters are not changed.
1391A return value of
1392.Dv Z_BUF_ERROR
1393is not fatal, in which case
1394.Fn deflateParams
1395can be retried with more output space.
1396.It Xo
1397.Fa int
1398.Fn deflateTune "z_streamp strm" "int good_length" "int max_lazy" "int nice_length" "int max_chain" ;
1399.Xc
1400.Pp
1401Fine tune
1402.Fn deflate Ns 's
1403internal compression parameters.
1404This should only be used by someone who understands the algorithm
1405used by zlib's deflate for searching for the best matching string,
1406and even then only by the most fanatic optimizer
1407trying to squeeze out the last compressed bit for their specific input data.
1408Read the
1409.Pa deflate.c
1410source code for the meaning of the
1411.Fa max_lazy , good_length , nice_length ,
1412and
1413.Fa max_chain
1414parameters.
1415.Pp
1416.Fn deflateTune
1417can be called after
1418.Fn deflateInit
1419or
1420.Fn deflateInit2 ,
1421and returns
1422.Dv Z_OK
1423on success, or
1424.Dv Z_STREAM_ERROR
1425for an invalid deflate stream.
1426.It Xo
1427.Fa uLong
1428.Fn deflateBound "z_streamp strm" "uLong sourceLen" ;
1429.Xc
1430.Pp
1431.Fn deflateBound
1432returns an upper bound on the compressed size after deflation of
1433.Fa sourceLen
1434bytes.
1435It must be called after
1436.Fn deflateInit
1437or
1438.Fn deflateInit2 .
1439and after
1440.Fn deflateSetHeader ,
1441if used.
1442This would be used to allocate an output buffer for deflation in a single pass,
1443and so would be called before
1444.Fn deflate .
1445If that first
1446.Fn deflate
1447call is provided the
1448.Fa sourceLen
1449input bytes, an output buffer allocated to the size returned by
1450.Fn deflateBound ,
1451and the flush value
1452.Dv Z_FINISH ,
1453then
1454.Fn deflate
1455is guaranteed to return
1456.Dv Z_STREAM_END .
1457Note that it is possible for the compressed size to be larger than
1458the value returned by
1459.Fn deflateBound
1460if flush options other than
1461.Dv Z_FINISH
1462or
1463.Dv Z_NO_FLUSH
1464are used.
1465.It Xo
1466.Fa int
1467.Fn deflatePending "z_streamp strm" "unsigned *pending" "int *bits" ;
1468.Xc
1469.Pp
1470.Fn deflatePending
1471returns the number of bytes and bits of output that have been generated,
1472but not yet provided in the available output.
1473The bytes not provided would be due to the available output space
1474having been consumed.
1475The number of bits of output not provided are between 0 and 7,
1476where they await more bits to join them in order to fill out a full byte.
1477If
1478.Fa pending
1479or
1480.Fa bits
1481are
1482.Dv NULL ,
1483then those values are not set.
1484.Pp
1485.Fn deflatePending returns
1486.Dv Z_OK
1487if success, or
1488.Dv Z_STREAM_ERROR
1489if the source stream state was inconsistent.
1490.It Xo
1491.Fa int
1492.Fn deflatePrime "z_streamp strm" "int bits" "int value" ;
1493.Xc
1494.Pp
1495.Fn deflatePrime
1496inserts
1497.Fa bits
1498in the deflate output stream.
1499The intent is that this function is used to start off the deflate output
1500with the bits left over from a previous deflate stream when appending to it.
1501As such, this function can only be used for raw deflate,
1502and must be used before the first
1503.Fn deflate
1504call after a
1505.Fn deflateInit2
1506or
1507.Fn deflateReset .
1508.Fa bits
1509must be less than or equal to 16,
1510and that many of the least significant bits of
1511.Fa value
1512will be inserted in the output.
1513.Pp
1514.Fn deflatePrime
1515returns
1516.Dv Z_OK
1517if successful,
1518.Dv Z_BUF_ERROR
1519if there was not enough room in the internal buffer to insert the bits, or
1520.Dv Z_STREAM_ERROR
1521if the source stream state was inconsistent.
1522.It Xo
1523.Fa int
1524.Fn deflateSetHeader "z_streamp strm" "gz_headerp head" ;
1525.Xc
1526.Pp
1527.Fn deflateSetHeader
1528provides gzip header information for when a gzip
1529stream is requested by
1530.Fn deflateInit2 .
1531.Fn deflateSetHeader
1532may be called after
1533.Fn deflateInit2
1534or
1535.Fn deflateReset
1536and before the first call of
1537.Fn deflate .
1538The text, time, os, extra field, name, and comment information
1539in the provided gz_header structure are written to the gzip header
1540(xflag is ignored \- the extra flags are set
1541according to the compression level).
1542The caller must assure that, if not
1543.Dv NULL ,
1544.Fa name
1545and
1546.Fa comment
1547are terminated with a zero byte,
1548and that if
1549.Fa extra
1550is not
1551.Dv NULL ,
1552that
1553.Fa extra_len
1554bytes are available there.
1555If hcrc is true, a gzip header CRC is included.
1556Note that the current versions of the command-line version of
1557.Xr gzip 1
1558do not support header CRCs, and will report that it is a
1559.Dq multi-part gzip file
1560and give up.
1561.Pp
1562If
1563.Fn deflateSetHeader
1564is not used, the default gzip header has text false,
1565the time set to zero, and os set to 255, with no extra, name, or comment
1566fields.
1567The gzip header is returned to the default state by
1568.Fn deflateReset .
1569.Pp
1570.Fn deflateSetHeader
1571returns
1572.Dv Z_OK
1573if successful, or
1574.Dv Z_STREAM_ERROR
1575if the source stream state was inconsistent.
1576.It Xo
1577.Fa int
1578.Fn inflateInit2 "z_streamp strm" "int windowBits" ;
1579.Xc
1580.Pp
1581This is another version of
1582.Fn inflateInit
1583with an extra parameter.
1584The fields
1585.Fa next_in ,
1586.Fa avail_in ,
1587.Fa zalloc ,
1588.Fa zfree ,
1589and
1590.Fa opaque
1591must be initialized before by the caller.
1592.Pp
1593The
1594.Fa windowBits
1595parameter is the base two logarithm of the maximum window size
1596.Pq the size of the history buffer .
1597It should be in the range 8..15 for this version of the library.
1598The default value is 15 if
1599.Fn inflateInit
1600is used instead.
1601.Fa windowBits
1602must be greater than or equal to the
1603.Fa windowBits
1604value provided to
1605.Fn deflateInit2
1606while compressing, or it must be equal to 15 if
1607.Fn deflateInit2
1608was not used.
1609If a compressed stream with a larger window size is given as input,
1610.Fn inflate
1611will return with the error code
1612.Dv Z_DATA_ERROR
1613instead of trying to allocate a larger window.
1614.Pp
1615.Fa windowBits
1616can also be zero to request that inflate use the window size in the zlib header
1617of the compressed stream.
1618.Pp
1619.Fa windowBits
1620can also be -8..-15 for raw inflate.
1621In this case, -windowBits determines the window size.
1622.Fn inflate
1623will then process raw deflate data, not looking for a zlib or gzip header,
1624not generating a check value, and not looking for any check values
1625for comparison at the end of the stream.
1626This is for use with other formats that use the deflate compressed data format
1627such as zip.
1628Those formats provide their own check values.
1629If a custom format is developed using the raw deflate format
1630for compressed data, it is recommended that a check value such as an Adler-32
1631or a CRC-32 be applied to the uncompressed data as is done in the zlib, gzip,
1632and zip formats.
1633For most applications, the zlib format should be used as is.
1634Note that comments above on the use in
1635.Fn deflateInit2
1636applies to the magnitude of
1637.Fa windowBits .
1638.Pp
1639.Fa windowBits
1640can also be greater than 15 for optional gzip decoding.
1641Add 32 to windowBits to enable zlib and gzip decoding with automatic header
1642detection, or add 16 to decode only the gzip format
1643(the zlib format will return a
1644.Dv Z_DATA_ERROR ) .
1645If a gzip stream is being decoded,
1646strm->adler is a CRC-32 instead of an Adler-32.
1647Unlike the
1648.Xr gunzip 1
1649utility and
1650.Fn gzread
1651(see below),
1652.Fn inflate
1653will not automatically decode concatenated gzip streams.
1654.Fn inflate
1655will return
1656.Dv Z_STREAM_END
1657at the end of the gzip stream.
1658The state would need to be reset to continue decoding a subsequent gzip stream.
1659.Pp
1660.Fn inflateInit2
1661returns
1662.Dv Z_OK
1663if successful,
1664.Dv Z_MEM_ERROR
1665if there was not enough memory,
1666.Dv Z_VERSION_ERROR
1667if the
1668.Nm zlib
1669library version is incompatible with the version assumed by the caller, or
1670.Dv Z_STREAM_ERROR
1671if the parameters are invalid, such as a null pointer to the structure.
1672.Fa msg
1673is set to null if there is no error message.
1674.Fn inflateInit2
1675does not perform any decompression apart from possibly reading the zlib header
1676if present: actual decompression will be done by
1677.Fn inflate .
1678(So
1679.Fa next_in
1680and
1681.Fa avail_in
1682may be modified, but
1683.Fa next_out
1684and
1685.Fa avail_out
1686are unused and unchanged.)
1687The current implementation of
1688.Fn inflateInit2
1689does not process any header information \(em that is deferred until
1690.Fn inflate
1691is called.
1692.It Xo
1693.Fa int
1694.Fn inflateSetDictionary "z_streamp strm" "const Bytef *dictionary" "uInt dictLength" ;
1695.Xc
1696.Pp
1697Initializes the decompression dictionary from the given uncompressed byte
1698sequence.
1699This function must be called immediately after a call to
1700.Fn inflate
1701if that call returned
1702.Dv Z_NEED_DICT .
1703The dictionary chosen by the compressor can be determined from the
1704Adler-32 value returned by that call to
1705.Fn inflate .
1706The compressor and decompressor must use exactly the same dictionary
1707(see
1708.Fn deflateSetDictionary ) .
1709For raw inflate, this function can be called at any time to set the dictionary.
1710If the provided dictionary is smaller than the window and there is already
1711data in the window, then the provided dictionary will amend what's there.
1712The application must ensure that the dictionary
1713that was used for compression is provided.
1714.Pp
1715.Fn inflateSetDictionary
1716returns
1717.Dv Z_OK
1718if successful,
1719.Dv Z_STREAM_ERROR
1720if a parameter is invalid
1721.Pq e.g. dictionary being NULL
1722or the stream state is inconsistent,
1723.Dv Z_DATA_ERROR
1724if the given dictionary doesn't match the expected one
1725.Pq incorrect Adler-32 value .
1726.Fn inflateSetDictionary
1727does not perform any decompression: this will be done by subsequent calls of
1728.Fn inflate .
1729.It Xo
1730.Fa int
1731.Fn inflateGetDictionary "z_streamp strm" "Bytef *dictionary" "uInt *dictLength" ;
1732.Xc
1733.Pp
1734Returns the sliding dictionary being maintained by
1735.Fn inflate .
1736.Fa dictLength
1737is set to the number of bytes in the dictionary, and that many bytes are copied
1738to
1739.Fa dictionary .
1740.Fa dictionary
1741must have enough space, where 32768 bytes is always enough.
1742If
1743.Fn inflateGetDictionary
1744is called with dictionary equal to
1745.Dv NULL ,
1746then only the dictionary length is returned, and nothing is copied.
1747Similary, if
1748.Fa dictLength is
1749.Dv NULL ,
1750then it is not set.
1751.Pp
1752.Fn inflateGetDictionary
1753returns
1754.Dv Z_OK
1755on success, or
1756.Dv Z_STREAM_ERROR
1757if the stream state is inconsistent.
1758.It Xo
1759.Fa int
1760.Fn inflateSync "z_streamp strm" ;
1761.Xc
1762.Pp
1763Skips invalid compressed data until a possible full flush point
1764(see above the description of
1765.Fn deflate
1766with
1767.Dv Z_FULL_FLUSH )
1768can be found, or until all available input is skipped.
1769No output is provided.
1770.Pp
1771.Fn inflateSync
1772searches for a 00 00 FF FF pattern in the compressed data.
1773All full flush points have this pattern, but not all occurrences of this
1774pattern are full flush points.
1775.Pp
1776.Fn inflateSync
1777returns
1778.Dv Z_OK
1779if a possible full flush point has been found,
1780.Dv Z_BUF_ERROR
1781if no more input was provided,
1782.Dv Z_DATA_ERROR
1783if no flush point has been found, or
1784.Dv Z_STREAM_ERROR
1785if the stream structure was inconsistent.
1786In the success case, the application may save the current value of
1787.Fa total_in
1788which indicates where valid compressed data was found.
1789In the error case, the application may repeatedly call
1790.Fn inflateSync ,
1791providing more input each time, until success or end of the input data.
1792.It Xo
1793.Fa int
1794.Fn inflateCopy "z_streamp dest" "z_streamp source" ;
1795.Xc
1796.Pp
1797Sets the destination stream as a complete copy of the source stream.
1798.Pp
1799This function can be useful when randomly accessing a large stream.
1800The first pass through the stream can periodically record the inflate state,
1801allowing restarting inflate at those points when randomly accessing the stream.
1802.Pp
1803.Fn inflateCopy
1804returns
1805.Dv Z_OK
1806if success,
1807.Dv Z_MEM_ERROR
1808if there was not enough memory,
1809.Dv Z_STREAM_ERROR
1810if the source stream state was inconsistent
1811(such as
1812.Fa zalloc
1813being NULL).
1814.Fa msg
1815is left unchanged in both
1816.Fa source
1817and
1818.Fa dest .
1819.It Xo
1820.Fa int
1821.Fn inflateReset "z_streamp strm" ;
1822.Xc
1823.Pp
1824This function is equivalent to
1825.Fn inflateEnd
1826followed by
1827.Fn inflateInit ,
1828but does not free and reallocate the internal decompression state.
1829The stream will keep attributes that may have been set by
1830.Fn inflateInit2 .
1831.Pp
1832.Fn inflateReset
1833returns
1834.Dv Z_OK
1835if successful, or
1836.Dv Z_STREAM_ERROR
1837if the source stream state was inconsistent
1838(such as
1839.Fa zalloc
1840or
1841.Fa state
1842being NULL).
1843.It Xo
1844.Fa int
1845.Fn inflateReset2 "z_streamp strm" "int windowBits" ;
1846.Xc
1847.Pp
1848This function is the same as
1849.Fn inflateReset ,
1850but it also permits changing the wrap and window size requests.
1851The
1852.Fa windowBits
1853parameter is interpreted the same as it is for
1854.Fa inflateInit2 .
1855If the window size is changed, then the memory allocated for the window
1856is freed, and the window will be reallocated by
1857.Fn inflate
1858if needed.
1859.Pp
1860.Fn inflateReset2
1861returns
1862.Dv Z_OK
1863if success, or
1864.Dv Z_STREAM_ERROR
1865if the source stream state was inconsistent
1866(such as
1867.Fa zalloc
1868or
1869.Fa state
1870being
1871.Dv NULL ) ,
1872or if the
1873.Fa windowBits
1874parameter is invalid.
1875.It Xo
1876.Fa int
1877.Fn inflatePrime "z_stream strm" "int bits" "int value" ;
1878.Xc
1879.Pp
1880This function inserts bits in the inflate input stream.
1881The intent is that this function is used
1882to start inflating at a bit position in the middle of a byte.
1883The provided bits will be used before any bytes are used from
1884.Fa next_in .
1885This function should only be used with raw inflate,
1886and should be used before the first
1887.Fn inflate
1888call after
1889.Fn inflateInit2
1890or
1891.Fn inflateReset .
1892.Fa bits
1893must be less than or equal to 16,
1894and that many of the least significant bits of value
1895will be inserted in the input.
1896.Pp
1897If
1898.Fa bits
1899is negative, then the input stream bit buffer is emptied.
1900Then
1901.Fn inflatePrime
1902can be called again to put bits in the buffer.
1903This is used to clear out bits left over after feeding
1904.Fn inflate
1905a block description prior to feeding it codes.
1906.Pp
1907.Fn inflatePrime
1908returns
1909.Dv Z_OK
1910if successful, or
1911.Dv Z_STREAM_ERROR
1912if the source stream state was inconsistent.
1913.It Xo
1914.Fa long
1915.Fn inflateMark "z_streamp strm" ;
1916.Xc
1917.Pp
1918This function returns two values: one in the lower 16 bits of the return
1919value, and the other in the remaining upper bits, obtained by shifting the
1920return value down 16 bits.
1921If the upper value is -1 and the lower value is zero, then
1922.Fn inflate
1923is currently decoding information outside of a block.
1924If the upper value is -1 and the lower value is non-zero, then
1925.Fn inflate
1926is in the middle of a stored block, with the lower value equaling the number of
1927bytes from the input remaining to copy.
1928If the upper value is not -1, then it is the number of bits back from
1929the current bit position in the input of the code
1930(literal or length/distance pair)
1931currently being processed.
1932In that case the lower value is the number of bytes
1933already emitted for that code.
1934.Pp
1935A code is being processed if
1936.Fn inflate
1937is waiting for more input to complete decoding of the code,
1938or if it has completed decoding but is waiting for
1939more output space to write the literal or match data.
1940.Pp
1941.Fn inflateMark
1942is used to mark locations in the input data for random access,
1943which may be at bit positions,
1944and to note those cases where the output of a code may span
1945boundaries of random access blocks.
1946The current location in the input stream can be determined from
1947.Fa avail_in
1948and
1949.Fa data_type
1950as noted in the description for the
1951.Dv Z_BLOCK
1952flush parameter for
1953.Fn inflate .
1954.Pp
1955.Fn inflateMark
1956returns the value noted above,
1957or -65536 if the provided source stream state was inconsistent.
1958.It Xo
1959.Fa int
1960.Fn inflateGetHeader "z_streamp strm" "gz_headerp head" ;
1961.Xc
1962.Pp
1963.Fn inflateGetHeader
1964requests that gzip header information be stored in the
1965provided gz_header structure.
1966.Fn inflateGetHeader
1967may be called after
1968.Fn inflateInit2
1969or
1970.Fn inflateReset ,
1971and before the first call of
1972.Fn inflate .
1973As
1974.Fn inflate
1975processes the gzip stream, head->done is zero until the header
1976is completed, at which time head->done is set to one.
1977If a zlib stream is being decoded,
1978then head->done is set to -1 to indicate that there will be
1979no gzip header information forthcoming.
1980Note that
1981.Dv Z_BLOCK
1982or
1983.Dv Z_TREES
1984can be used to force
1985.Fn inflate
1986to return immediately after header processing is complete
1987and before any actual data is decompressed.
1988.Pp
1989The text, time, xflags, and os fields are filled in with the gzip header
1990contents.
1991hcrc is set to true if there is a header CRC.
1992(The header CRC was valid if done is set to one.)
1993If extra is not
1994.Dv NULL ,
1995then
1996.Fa extra_max
1997contains the maximum number of bytes to write to
1998.Fa extra .
1999Once done is true,
2000.Fa extra_len
2001contains the actual extra field length, and
2002.Fa extra
2003contains the extra field, or that field truncated if
2004.Fa extra_max
2005is less than
2006.Fa extra_len .
2007If name is not
2008.Dv NULL ,
2009then up to
2010.Fa name_max
2011characters are written there,
2012terminated with a zero unless the length is greater than
2013.Fa name_max .
2014If comment is not
2015.Dv NULL ,
2016then up to
2017.Fa comm_max
2018characters are written there,
2019terminated with a zero unless the length is greater than
2020.Fa comm_max .
2021When any of extra, name, or comment are not
2022.Dv NULL
2023and the respective field is not present in the header,
2024then that field is set to
2025.Dv NULL
2026to signal its absence.
2027This allows the use of
2028.Fn deflateSetHeader
2029with the returned structure to duplicate the header.
2030However if those fields are set to allocated memory,
2031then the application will need to save those pointers
2032elsewhere so that they can be eventually freed.
2033.Pp
2034If
2035.Fn inflateGetHeader
2036is not used, then the header information is simply discarded.
2037The header is always checked for validity,
2038including the header CRC if present.
2039.Fn inflateReset
2040will reset the process to discard the header information.
2041The application would need to call
2042.Fn inflateGetHeader
2043again to retrieve the header from the next gzip stream.
2044.Pp
2045.Fn inflateGetHeader
2046returns
2047.Dv Z_OK
2048if successful,
2049or
2050.Dv Z_STREAM_ERROR
2051if the source stream state was inconsistent.
2052.It Xo
2053.Fa int
2054.Fn inflateBackInit "z_stream *strm" "int windowBits" "unsigned char FAR *window" ;
2055.Xc
2056.Pp
2057Initialize the internal stream state for decompression using
2058.Fn inflateBack
2059calls.
2060The fields
2061.Fa zalloc , zfree
2062and
2063.Fa opaque
2064in
2065.Fa strm
2066must be initialized before the call.
2067If
2068.Fa zalloc
2069and
2070.Fa zfree
2071are
2072.Dv NULL ,
2073then the default library-derived memory allocation routines are used.
2074.Fa windowBits
2075is the base two logarithm of the window size, in the range 8..15.
2076.Fa window
2077is a caller supplied buffer of that size.
2078Except for special applications where it is assured that
2079.Fn deflate
2080was used with small window sizes,
2081.Fa windowBits
2082must be 15 and a 32K byte window must be supplied to be able to decompress
2083general deflate streams.
2084.Pp
2085See
2086.Fn inflateBack
2087for the usage of these routines.
2088.Pp
2089.Fn inflateBackInit
2090will return
2091.Dv Z_OK
2092on success,
2093.Dv Z_STREAM_ERROR
2094if any of the parameters are invalid,
2095.Dv Z_MEM_ERROR
2096if the internal state could not be allocated, or
2097.Dv Z_VERSION_ERROR
2098if the version of the library does not match the version of the header file.
2099.It Xo
2100.Fa int
2101.Fn inflateBack "z_stream *strm" "in_func in" "void FAR *in_desc" "out_func out" "void FAR *out_desc" ;
2102.Xc
2103.Pp
2104.Fn inflateBack
2105does a raw inflate with a single call using a call-back
2106interface for input and output.
2107This is potentially more efficient than
2108.Fn inflate
2109for file I/O applications, in that it avoids copying between the output and the
2110sliding window by simply making the window itself the output buffer.
2111.Fn inflate
2112can be faster on modern CPUs when used with large buffers.
2113.Fn inflateBack
2114trusts the application to not change the output buffer passed by
2115the output function, at least until
2116.Fn inflateBack
2117returns.
2118.Pp
2119.Fn inflateBackInit
2120must be called first to allocate the internal state
2121and to initialize the state with the user-provided window buffer.
2122.Fn inflateBack
2123may then be used multiple times to inflate a complete, raw
2124deflate stream with each call.
2125.Fn inflateBackEnd
2126is then called to free the allocated state.
2127.Pp
2128A raw deflate stream is one with no zlib or gzip header or trailer.
2129This routine would normally be used in a utility that reads zip or gzip
2130files and writes out uncompressed files.
2131The utility would decode the header and process the trailer on its own,
2132hence this routine expects only the raw deflate stream to decompress.
2133This is different from the default behavior of
2134.Fn inflate ,
2135which expects either a zlib header and trailer around the deflate stream.
2136.Pp
2137.Fn inflateBack
2138uses two subroutines supplied by the caller that are then called by
2139.Fn inflateBack
2140for input and output.
2141.Fn inflateBack
2142calls those routines until it reads a complete deflate stream and writes out
2143all of the uncompressed data, or until it encounters an error.
2144The function's parameters and return types are defined above in the
2145in_func and out_func typedefs.
2146.Fn inflateBack
2147will call in(in_desc, &buf) which should return the
2148number of bytes of provided input, and a pointer to that input in
2149.Fa buf .
2150If there is no input available,
2151.Fn in
2152must return zero
2153\(em buf is ignored in that case \(em
2154and
2155.Fn inflateBack
2156will return a buffer error.
2157.Fn inflateBack
2158will call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].
2159.Fn out
2160should return zero on success, or non-zero on failure.
2161If
2162.Fn out
2163returns non-zero,
2164.Fn inflateBack
2165will return with an error.
2166Neither
2167.Fn in
2168nor
2169.Fn out
2170are permitted to change the contents of the window provided to
2171.Fn inflateBackInit ,
2172which is also the buffer that
2173.Fn out
2174uses to write from.
2175The length written by
2176.Fn out
2177will be at most the window size.
2178Any non-zero amount of input may be provided by
2179.Fn in .
2180.Pp
2181For convenience,
2182.Fn inflateBack
2183can be provided input on the first call by setting strm->next_in
2184and strm->avail_in.
2185If that input is exhausted, then
2186.Fn in
2187will be called.
2188Therefore strm->next_in must be initialized before calling
2189.Fn inflateBack .
2190If strm->next_in is
2191.Dv NULL ,
2192then
2193.Fn in
2194will be called immediately for input.
2195If strm->next_in is not
2196.Dv NULL ,
2197then strm->avail_in must also be initialized,
2198and then if strm->avail_in is not zero,
2199input will initially be taken from
2200strm->next_in[0 .. strm->avail_in \- 1].
2201.Pp
2202The
2203.Fa in_desc
2204and
2205.Fa out_desc
2206parameters of
2207.Fn inflateBack
2208are passed as the first parameter of
2209.Fn in
2210and
2211.Fn out ,
2212respectively, when they are called.
2213These descriptors can be optionally used to pass any information that the
2214caller-supplied
2215.Fn in
2216and
2217.Fn out
2218functions need to do their job.
2219.Pp
2220On return,
2221.Fn inflateBack
2222will set strm->next_in and strm->avail_in to pass back any unused input
2223that was provided by the last
2224.Fn in
2225call.
2226The return values of
2227.Fn inflateBack
2228can be
2229.Dv Z_STREAM_END
2230on success,
2231.Dv Z_BUF_ERROR
2232if
2233.Fn in
2234or
2235.Fn out
2236returned an error,
2237.Dv Z_DATA_ERROR
2238if there was a format error in the deflate stream
2239(in which case strm->msg is set to indicate the nature of the error),
2240or
2241.Dv Z_STREAM_ERROR
2242if the stream was not properly initialized.
2243In the case of
2244.Dv Z_BUF_ERROR ,
2245an input or output error can be distinguished using strm->next_in which
2246will be
2247.Dv NULL
2248only if
2249.Fn in
2250returned an error.
2251If strm->next is not
2252.Dv NULL ,
2253then the
2254.Dv Z_BUF_ERROR
2255was due to
2256.Fn out
2257returning non-zero.
2258.Po
2259.Fn in
2260will always be called before
2261.Fn out ,
2262so strm->next_in is assured to be defined if
2263.Fn out
2264returns non-zero.
2265.Pc
2266Note that
2267.Fn inflateBack
2268cannot return
2269.Dv Z_OK .
2270.It Xo
2271.Fa int
2272.Fn inflateBackEnd "z_stream *strm" ;
2273.Xc
2274.Pp
2275All memory allocated by
2276.Fn inflateBackInit
2277is freed.
2278.Pp
2279.Fn inflateBackEnd
2280returns
2281.Dv Z_OK
2282on success, or
2283.Dv Z_STREAM_ERROR
2284if the stream state was inconsistent.
2285.It Xo
2286.Fa uLong
2287.Fn zlibCompileFlags "void" ;
2288.Xc
2289.Pp
2290This function returns flags indicating compile-time options.
2291.Pp
2292Type sizes, two bits each:
2293.Pp
2294.Bl -tag -width Ds -offset indent -compact
2295.It 00
229616 bits
2297.It 01
229832 bits
2299.It 10
230064 bits
2301.It 11
2302other:
2303.Pp
2304.Bl -tag -width Ds -offset indent -compact
2305.It 1.0
2306size of uInt
2307.It 3.2
2308size of uLong
2309.It 5.4
2310size of voidpf
2311.Pq pointer
2312.It 7.6
2313size of z_off_t
2314.El
2315.El
2316.Pp
2317Compiler, assembler, and debug options:
2318.Pp
2319.Bl -tag -width Ds -offset indent -compact
2320.It 8
2321ZLIB_DEBUG
2322.It 9
2323ASMV or ASMINF \(em use ASM code
2324.It 10
2325ZLIB_WINAPI \(em exported functions use the WINAPI calling convention
2326.It 11
23270
2328.Pq reserved
2329.El
2330.Pp
2331One-time table building
2332.Pq smaller code, but not thread-safe if true :
2333.Pp
2334.Bl -tag -width Ds -offset indent -compact
2335.It 12
2336BUILDFIXED \(em build static block decoding tables when needed
2337.It 13
2338DYNAMIC_CRC_TABLE \(em build CRC calculation tables when needed
2339.It 14,15
23400
2341.Pq reserved
2342.El
2343.Pp
2344Library content (indicates missing functionality):
2345.Pp
2346.Bl -tag -width Ds -offset indent -compact
2347.It 16
2348NO_GZCOMPRESS \(em gz* functions cannot compress
2349.Pq to avoid linking deflate code when not needed
2350.It 17
2351NO_GZIP \(em deflate can't write gzip streams, and inflate can't detect
2352and decode gzip streams
2353.Pq to avoid linking CRC code
2354.It 18-19
23550
2356.Pq reserved
2357.El
2358.Pp
2359Operation variations (changes in library functionality):
2360.Pp
2361.Bl -tag -width Ds -offset indent -compact
2362.It 20
2363PKZIP_BUG_WORKAROUND \(em slightly more permissive inflate
2364.It 21
2365FASTEST \(em deflate algorithm with only one, lowest compression level
2366.It 22,23
23670
2368.Pq reserved
2369.El
2370.Pp
2371The sprintf variant used by gzprintf
2372.Pq zero is best :
2373.Pp
2374.Bl -tag -width Ds -offset indent -compact
2375.It 24
23760 = vs*, 1 = s* \(em 1 means limited to 20 arguments after the format
2377.It 25
23780 = *nprintf, 1 = *printf \(em 1 means
2379.Fn gzprintf
2380not secure!
2381.It 26
23820 = returns value, 1 = void \(em 1 means inferred string length returned
2383.El
2384.Pp
2385Remainder:
2386.Pp
2387.Bl -tag -width Ds -offset indent -compact
2388.It 27-31
23890
2390.Pq reserved
2391.El
2392.El
2393.Sh UTILITY FUNCTIONS
2394The following utility functions are implemented on top of the
2395basic stream-oriented functions.
2396To simplify the interface,
2397some default options are assumed (compression level and memory usage,
2398standard memory allocation functions).
2399The source code of these utility functions can be modified
2400if you need special options.
2401.Bl -tag -width Ds
2402.It Xo
2403.Fa int
2404.Fn compress "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen" ;
2405.Xc
2406.Pp
2407The
2408.Fn compress
2409function compresses the source buffer into the destination buffer.
2410.Fa sourceLen
2411is the byte length of the source buffer.
2412Upon entry,
2413.Fa destLen
2414is the total size of the destination buffer,
2415which must be at least the value returned by
2416.Fn compressBound sourcelen .
2417Upon exit,
2418.Fa destLen
2419is the actual size of the compressed data.
2420.Fn compress
2421is equivalent to
2422.Fn compress2
2423with a level parameter of
2424.Dv Z_DEFAULT_COMPRESSION .
2425.Pp
2426.Fn compress
2427returns
2428.Dv Z_OK
2429if successful,
2430.Dv Z_MEM_ERROR
2431if there was not enough memory, or
2432.Dv Z_BUF_ERROR
2433if there was not enough room in the output buffer.
2434.It Xo
2435.Fa int
2436.Fn compress2 "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen" "int level" ;
2437.Xc
2438.Pp
2439The
2440.Fn compress2
2441function compresses the source buffer into the destination buffer.
2442The
2443.Fa level
2444parameter has the same meaning as in
2445.Fn deflateInit .
2446.Fa sourceLen
2447is the byte length of the source buffer.
2448Upon entry,
2449.Fa destLen
2450is the total size of the destination buffer,
2451which must be at least the value returned by
2452.Fn compressBound sourceLen .
2453Upon exit,
2454.Fa destLen
2455is the actual size of the compressed buffer.
2456.Pp
2457.Fn compress2
2458returns
2459.Dv Z_OK
2460if successful,
2461.Dv Z_MEM_ERROR
2462if there was not enough memory,
2463.Dv Z_BUF_ERROR
2464if there was not enough room in the output buffer, or
2465.Dv Z_STREAM_ERROR
2466if the level parameter is invalid.
2467.It Xo
2468.Fa uLong
2469.Fn compressBound "uLong sourceLen" ;
2470.Xc
2471.Pp
2472.Fn compressBound
2473returns an upper bound on the compressed size after
2474.Fn compress
2475or
2476.Fn compress2
2477on
2478.Fa sourceLen
2479bytes.
2480It would be used before a
2481.Fn compress
2482or
2483.Fn compress2
2484call to allocate the destination buffer.
2485.It Xo
2486.Fa int
2487.Fn uncompress "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong sourceLen" ;
2488.Xc
2489.Pp
2490The
2491.Fn uncompress
2492function decompresses the source buffer into the destination buffer.
2493.Fa sourceLen
2494is the byte length of the source buffer.
2495Upon entry,
2496.Fa destLen
2497is the total size of the destination buffer,
2498which must be large enough to hold the entire uncompressed data.
2499(The size of the uncompressed data must have been saved previously
2500by the compressor and transmitted to the decompressor
2501by some mechanism outside the scope of this compression library.)
2502Upon exit,
2503.Fa destLen
2504is the actual size of the uncompressed data.
2505This function can be used to decompress a whole file at once if the
2506input file is mmap'ed.
2507.Pp
2508.Fn uncompress
2509returns
2510.Dv Z_OK
2511if successful,
2512.Dv Z_MEM_ERROR
2513if there was not enough memory,
2514.Dv Z_BUF_ERROR
2515if there was not enough room in the output buffer, or
2516.Dv Z_DATA_ERROR
2517if the input data was corrupted or incomplete.
2518In the case where there is not enough room,
2519.Fn uncompress
2520will fill the output buffer with the uncompressed data up to that point.
2521.It Xo
2522.Fa int
2523.Fn uncompress2 "Bytef *dest" "uLongf *destLen" "const Bytef *source" "uLong *sourceLen" ;
2524.Xc
2525.Pp
2526Same as
2527.Fn uncompress ,
2528except that
2529.Fa sourceLen
2530is a pointer, where the length of the source is
2531.Fa *sourceLen .
2532On return,
2533.Fa *sourceLen
2534is the number of source bytes consumed.
2535.It Xo
2536.Fa gzFile
2537.Fn gzopen "const char *path" "const char *mode" ;
2538.Xc
2539.Pp
2540This library supports reading and writing files in gzip (.gz) format with
2541an interface similar to that of stdio, using the functions that start with "gz".
2542The gzip format is different from the zlib format.
2543gzip is a gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
2544.Pp
2545The
2546.Fn gzopen
2547function opens a gzip
2548.Pq .gz
2549file for reading or writing.
2550The mode parameter is as in
2551.Xr fopen 3
2552.Po
2553.Qq rb
2554or
2555.Qq wb
2556.Pc
2557but can also include a compression level
2558.Pq "wb9"
2559or a strategy:
2560.Sq f
2561for filtered data, as in
2562.Qq wb6f ;
2563.Sq h
2564for Huffman only compression, as in
2565.Qq wb1h ,
2566or
2567.Sq R
2568for run-length encoding as in
2569.Qq wb1R ,
2570or
2571.Sq F
2572for fixed code compression as in
2573.Qq wb9F .
2574(See the description of
2575.Fn deflateInit2
2576for more information about the strategy parameter.)
2577.Sq T
2578will request transparent writing or appending with no compression and not using
2579the gzip format.
2580.Pp
2581.Sq a
2582can be used instead of
2583.Sq w
2584to request that the gzip stream that will be written be appended to the file.
2585.Sq +
2586will result in an error,
2587since reading and writing to the same gzip file is not supported.
2588The addition of
2589.Sq x
2590when writing will create the file exclusively,
2591which fails if the file already exists.
2592On systems that support it, the addition of
2593.Sq e
2594when reading or writing will set the flag to close the file on an
2595.Xr execve 2
2596call.
2597.Pp
2598These functions, as well as gzip,
2599will read and decode a sequence of gzip streams in a file.
2600The append function of
2601.Fn gzopen
2602can be used to create such a file.
2603(Also see
2604.Fn gzflush
2605for another way to do this.)
2606When appending,
2607.Fn gzopen
2608does not test whether the file begins with a gzip stream,
2609nor does it look for the end of the gzip streams to begin appending.
2610.Fn gzopen
2611will simply append a gzip stream to the existing file.
2612.Pp
2613.Fn gzopen
2614can be used to read a file which is not in gzip format;
2615in this case
2616.Fn gzread
2617will directly read from the file without decompression.
2618When reading, this will be detected automatically
2619by looking for the magic two-byte gzip header.
2620.Pp
2621.Fn gzopen
2622returns
2623.Dv NULL
2624if the file could not be opened,
2625if there was insufficient memory to allocate the gzFile state,
2626or if an invalid mode was specified
2627(an
2628.Sq r ,
2629.Sq w ,
2630or
2631.Sq a
2632was not provided, or
2633.Sq +
2634was provided).
2635.Fa errno
2636can be checked to determine if the reason
2637.Fn gzopen
2638failed was that the file could not be opened.
2639.It Xo
2640.Fa gzFile
2641.Fn gzdopen "int fd" "const char *mode" ;
2642.Xc
2643.Pp
2644The
2645.Fn gzdopen
2646function associates a gzFile with the file descriptor
2647.Fa fd .
2648File descriptors are obtained from calls like
2649.Xr open 2 ,
2650.Xr dup 2 ,
2651.Xr creat 3 ,
2652.Xr pipe 2 ,
2653or
2654.Xr fileno 3
2655(if the file has been previously opened with
2656.Xr fopen 3 ) .
2657The
2658.Fa mode
2659parameter is as in
2660.Fn gzopen .
2661.Pp
2662The next call to
2663.Fn gzclose
2664on the returned gzFile will also close the file descriptor fd,
2665just like fclose(fdopen(fd), mode) closes the file descriptor fd.
2666If you want to keep fd open, use
2667.Dq fd = dup(fd_keep); gz = gzdopen(fd, mode); .
2668The duplicated descriptor should be saved to avoid a leak, since
2669.Fn gzdopen
2670does not close fd if it fails.
2671If you are using
2672.Fn fileno
2673to get the file descriptor from a FILE *,
2674then you will have to use
2675.Xr dup 2
2676to avoid double-closing the file descriptor.
2677Both
2678.Fn gzclose
2679and
2680.Fn fclose
2681will close the associated file descriptor,
2682so they need to have different file descriptors.
2683.Pp
2684.Fn gzdopen
2685returns NULL if there was insufficient memory to allocate the gzFile state,
2686if an invalid mode was specified
2687(an 'r', 'w', or 'a' was not provided, or '+' was provided), or if fd is -1.
2688The file descriptor is not used until the next gz* read, write, seek,
2689or close operation, so
2690.Fn gzdopen
2691will not detect if fd is invalid (unless fd is -1).
2692.It Xo
2693.Fa int
2694.Fn gzbuffer "gzFile file" "unsigned size" ;
2695.Xc
2696.Pp
2697Set the internal buffer size used by this library's functions.
2698The default buffer size is 8192 bytes.
2699This function must be called after
2700.Fn gzopen
2701or
2702.Fn gzdopen ,
2703and before any other calls that read or write the file.
2704The buffer memory allocation is always deferred to the first read or write.
2705Three times that size in buffer space is allocated.
2706A larger buffer size of, for example, 64K or 128K bytes,
2707will noticeably increase the speed of decompression (reading).
2708.Pp
2709The new buffer size also affects the maximum length for
2710.Fn gzprintf .
2711.Pp
2712.Fn gzbuffer
2713returns 0 on success, or -1 on failure, such as being called too late.
2714.It Xo
2715.Fa int
2716.Fn gzsetparams "gzFile file" "int level" "int strategy" ;
2717.Xc
2718.Pp
2719The
2720.Fn gzsetparams
2721function dynamically updates the compression level or strategy.
2722See the description of
2723.Fn deflateInit2
2724for the meaning of these parameters.
2725Previously provided data is flushed before the parameter change.
2726.Pp
2727.Fn gzsetparams
2728returns
2729.Dv Z_OK
2730if successful,
2731.Dv Z_STREAM_ERROR
2732if the file was not opened for writing,
2733.Dv Z_ERRNO
2734if there is an error writing the flushed data, or
2735.Dv Z_MEM_ERROR
2736if there is a memory allocation error.
2737.It Xo
2738.Fa int
2739.Fn gzread "gzFile file" "voidp buf" "unsigned len" ;
2740.Xc
2741.Pp
2742Reads the given number of uncompressed bytes from the compressed file.
2743If the input file is not in gzip format,
2744.Fn gzread
2745copies the given number ofbytes into the buffer directly from the file.
2746.Pp
2747After reaching the end of a gzip stream in the input,
2748.Fn gzread
2749will continue to read, looking for another gzip stream.
2750Any number of gzip streams may be concatenated in the input file,
2751and will all be decompressed by
2752.Fn gzread .
2753If something other than a gzip stream is encountered after a gzip stream,
2754that remaining trailing garbage is ignored (and no error is returned).
2755.Pp
2756.Fn gzread
2757can be used to read a gzip file that is being concurrently written.
2758Upon reaching the end of the input,
2759.Fn gzread
2760will return with the available data.
2761If the error code returned by
2762.Fn gzerror
2763is
2764.Dv Z_OK
2765or
2766.Dv Z_BUF_ERROR ,
2767then
2768.Fn gzclearerr
2769can be used to clear the end of file indicator in order to permit
2770.Fn gzread
2771to be tried again.
2772.Dv Z_OK
2773indicates that a gzip stream was completed on the last
2774.Fn gzread .
2775.Dv Z_BUF_ERROR
2776indicates that the input file ended in the middle of a gzip stream.
2777Note that
2778.Fn gzread
2779does not return -1 in the event of an incomplete gzip stream.
2780This error is deferred until
2781.Fn gzclose ,
2782which will return
2783.Dv Z_BUF_ERROR
2784if the last
2785.Fn gzread
2786ended in the middle of a gzip stream.
2787Alternatively,
2788.Fn gzerror
2789can be used before
2790.Fn gzclose
2791to detect this case.
2792.Pp
2793.Fn gzread
2794returns the number of uncompressed bytes actually read,
2795less than
2796.Fa len
2797for end of file, or -1 for error.
2798If
2799.Fa len
2800is too large to fit in an int,
2801then nothing is read, -1 is returned, and the error state is set to
2802.Dv Z_STREAM_ERROR .
2803.It Xo
2804.Fa z_size_t
2805.Fn gzfread "voidp buf" "z_size_t size" "z_size_t nitems" "gzFile file" ;
2806.Xc
2807.Pp
2808Read up to
2809.Fa nitems
2810items of size
2811.Fa size
2812from
2813.Fa file
2814to
2815.Fa buf ,
2816otherwise operating as
2817.Fn gzread
2818does.
2819This duplicates the interface of stdio's
2820.Xr fread 3 ,
2821with size_t request and return types.
2822If the library defines size_t, then z_size_t is identical to size_t.
2823If not, then z_size_t is an unsigned integer type that can contain a pointer.
2824.Pp
2825.Fn gzfread
2826returns the number of full items read of size
2827.Fa size ,
2828or zero if the end of the file was reached and a full item could not be read,
2829or if there was an error.
2830.Fn gzerror
2831must be consulted if zero is returned in order to determine
2832if there was an error.
2833If the multiplication of
2834.Fa size
2835and
2836.Fa nitems
2837overflows, i.e. the product does not fit in a z_size_t, then nothing is read,
2838zero is returned, and the error state is set to
2839.Dv Z_STREAM_ERROR .
2840.Pp
2841In the event that the end of file is reached and only a partial item is
2842available at the end, i.e. the remaining uncompressed data length is not a
2843multiple of size, then the final partial item is nevetheless read into
2844.Fa buf
2845and the end-of-file flag is set.
2846The length of the partial item read is not provided,
2847but could be inferred from the result of
2848.Fn gztell .
2849This behavior is the same as the behavior of
2850.Xr fread 3
2851implementations in common libraries,
2852but it prevents the direct use of
2853.Fn gzfread
2854to read a concurrently written file, resetting and retrying on end-of-file,
2855when size is not 1.
2856.It Xo
2857.Fa int
2858.Fn gzwrite "gzFile file" "voidpc buf" "unsigned len" ;
2859.Xc
2860.Pp
2861The
2862.Fn gzwrite
2863function writes the given number of uncompressed bytes into the compressed file.
2864.Fn gzwrite
2865returns the number of uncompressed bytes written or 0 in case of error.
2866.It Xo
2867.Fa z_size_t
2868.Fn gzfwrite "voidpc buf" "z_size_t size" "z_size_t nitems" "gzFile file" ;
2869.Xc
2870.Pp
2871.Fn gzfwrite
2872writes
2873.Fa nitems
2874items of size
2875.Fa size
2876from
2877.Fa buf
2878to
2879.Fa file ,
2880duplicating the interface of stdio's
2881.Xr fwrite 3 ,
2882with size_t request and return types.
2883If the library defines size_t, then z_size_t is identical to size_t.
2884If not, then z_size_t is an unsigned integer type that can contain a pointer.
2885.Pp
2886.Fn gzfwrite
2887returns the number of full items written of size
2888.Fa size ,
2889or zero if there was an error.
2890If the multiplication of
2891.Fa size
2892and
2893.Fa nitems
2894overflows,
2895i.e. the product does not fit in a z_size_t, then nothing is written,
2896zero is returned, and the error state is set to
2897.Dv Z_STREAM_ERROR .
2898.It Xo
2899.Fa int
2900.Fn gzprintf "gzFile file" "const char *format" "..." ;
2901.Xc
2902.Pp
2903The
2904.Fn gzprintf
2905function converts, formats, and writes the args to the compressed file
2906under control of the format string, as in
2907.Xr fprintf 3 .
2908.Fn gzprintf
2909returns the number of uncompressed bytes actually written,
2910or a negative zlib error code in case of error.
2911The number of uncompressed bytes written is limited to 8191,
2912or one less than the buffer size given to
2913.Fn gzbuffer .
2914The caller should ensure that this limit is not exceeded.
2915If it is exceeded, then
2916.Fn gzprintf
2917will return an error
2918.Pq 0
2919with nothing written.
2920In this case, there may also be a buffer overflow
2921with unpredictable consequences, which is possible only if
2922.Nm zlib
2923was compiled with the insecure functions
2924.Fn sprintf
2925or
2926.Fn vsprintf
2927because the secure
2928.Fn snprintf
2929or
2930.Fn vsnprintf
2931functions were not available.
2932This can be determined using
2933.Fn zlibCompileFlags .
2934.It Xo
2935.Fa int
2936.Fn gzputs "gzFile file" "const char *s" ;
2937.Xc
2938.Pp
2939The
2940.Fn gzputs
2941function writes the given NUL-terminated string to the compressed file,
2942excluding the terminating NUL character.
2943.Pp
2944.Fn gzputs
2945returns the number of characters written, or -1 in case of error.
2946.It Xo
2947.Fa char *
2948.Fn gzgets "gzFile file" "char *buf" "int len" ;
2949.Xc
2950.Pp
2951The
2952.Fn gzgets
2953function reads bytes from the compressed file until len-1 characters are read,
2954or a newline character is read and transferred to
2955.Fa buf ,
2956or an end-of-file condition is encountered.
2957If any characters are read or if len == 1,
2958the string is terminated with a NUL character.
2959If no characters are read due to an end-of-file or len < 1,
2960then the buffer is left untouched.
2961.Pp
2962.Fn gzgets
2963returns
2964.Fa buf ,
2965which is a NUL-terminated string, or it returns
2966.Dv NULL
2967for end-of-file or in case of error.
2968If there was an error, the contents at
2969.Fa buf
2970are indeterminate.
2971.Pp
2972.Fn gzgets
2973returns
2974.Fa buf ,
2975or
2976.Dv NULL
2977in case of error.
2978.It Xo
2979.Fa int
2980.Fn gzputc "gzFile file" "int c" ;
2981.Xc
2982.Pp
2983The
2984.Fn gzputc
2985function writes
2986.Fa c ,
2987converted to an unsigned char, into the compressed file.
2988.Fn gzputc
2989returns the value that was written, or -1 in case of error.
2990.It Xo
2991.Fa int
2992.Fn gzgetc "gzFile file" ;
2993.Xc
2994.Pp
2995The
2996.Fn gzgetc
2997function reads one byte from the compressed file.
2998.Fn gzgetc
2999returns this byte or -1 in case of end of file or error.
3000This is implemented as a macro for speed.
3001As such, it does not do all of the checking the other functions do.
3002That is, it does not check to see if file is
3003.Dv NULL ,
3004nor whether the structure
3005.Fa file
3006points to has been clobbered or not.
3007.It Xo
3008.Fa int
3009.Fn gzungetc "int c" "gzFile file" ;
3010.Xc
3011.Pp
3012Push one character back onto the stream to be read as the first character
3013on the next read.
3014At least one character of push-back is allowed.
3015.Fn gzungetc
3016returns the character pushed, or -1 on failure.
3017.Fn gzungetc
3018will fail if c is -1,
3019and may fail if a character has been pushed but not read yet.
3020If
3021.Fn gzungetc
3022is used immediately after
3023.Fn gzopen
3024or
3025.Fn gzdopen ,
3026at least the output buffer size of pushed characters is allowed.
3027(See
3028.Fn gzbuffer
3029above.)
3030The pushed character will be discarded if the stream is repositioned with
3031.Fn gzseek
3032or
3033.Fn gzrewind .
3034.It Xo
3035.Fa int
3036.Fn gzflush "gzFile file" "int flush" ;
3037.Xc
3038.Pp
3039The
3040.Fn gzflush
3041function flushes all pending output into the compressed file.
3042The parameter
3043.Fa flush
3044is as in the
3045.Fn deflate
3046function.
3047The return value is the
3048.Nm zlib
3049error number (see function
3050.Fn gzerror
3051below).
3052.Fn gzflush
3053is only permitted when writing.
3054.Pp
3055If the flush parameter is
3056.Dv Z_FINISH ,
3057the remaining data is written and the gzip stream is completed in the output.
3058If
3059.Fn gzwrite
3060is called again, a new gzip stream will be started in the output.
3061.Fn gzread
3062is able to read such concatenated gzip streams.
3063.Pp
3064.Fn gzflush
3065should be called only when strictly necessary because it will
3066degrade compression if called too often.
3067.It Xo
3068.Fa z_off_t
3069.Fn gzseek "gzFile file" "z_off_t offset" "int whence" ;
3070.Xc
3071.Pp
3072Sets the starting position for the next
3073.Fn gzread
3074or
3075.Fn gzwrite
3076on the given compressed file.
3077The offset represents a number of bytes in the uncompressed data stream.
3078The whence parameter is defined as in
3079.Xr lseek 2 ;
3080the value
3081.Dv SEEK_END
3082is not supported.
3083.Pp
3084If the file is opened for reading, this function is emulated but can be
3085extremely slow.
3086If the file is opened for writing, only forward seeks are supported;
3087.Fn gzseek
3088then compresses a sequence of zeroes up to the new starting position.
3089.Pp
3090.Fn gzseek
3091returns the resulting offset location as measured in bytes from
3092the beginning of the uncompressed stream, or -1 in case of error,
3093in particular if the file is opened for writing and the new starting position
3094would be before the current position.
3095.It Xo
3096.Fa int
3097.Fn gzrewind "gzFile file" ;
3098.Xc
3099.Pp
3100The
3101.Fn gzrewind
3102function rewinds the given
3103.Fa file .
3104This function is supported only for reading.
3105.Pp
3106gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET).
3107.It Xo
3108.Fa z_off_t
3109.Fn gztell "gzFile file" ;
3110.Xc
3111.Pp
3112The
3113.Fn gztell
3114function returns the starting position for the next
3115.Fn gzread
3116or
3117.Fn gzwrite
3118on the given compressed file.
3119This position represents a number of bytes in the uncompressed data stream,
3120and is zero when starting,
3121even if appending or reading a gzip stream from the middle of a file using
3122.Fn gzdopen .
3123.Pp
3124gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR).
3125.It Xo
3126.Fa int
3127.Fn gzeoffset "gzFile file" ;
3128.Xc
3129.Pp
3130Returns the current offset in the file being read or written.
3131This offset includes the count of bytes that precede the gzip stream,
3132for example when appending or when using
3133.Fn gzdopen
3134for reading.
3135When reading, the offset does not include as yet unused buffered input.
3136This information can be used for a progress indicator.
3137On error,
3138.Fn gzoffset
3139returns -1.
3140.It Xo
3141.Fa int
3142.Fn gzeof "gzFile file" ;
3143.Xc
3144.Pp
3145Returns true (1) if the end-of-file indicator has been set while reading,
3146false (0) otherwise.
3147Note that the end-of-file indicator is set only if the
3148read tried to go past the end of the input, but came up short.
3149Therefore just like
3150.Xr feof 3 ,
3151.Fn gzeof
3152may return false even if there is no more data to read,
3153in the event that the last read request was for the exact number of
3154bytes remaining in the input file.
3155This will happen if the input file size is an exact multiple of the buffer size.
3156.Pp
3157If
3158.Fn gzeof
3159returns true, then the read functions will return no more data,
3160unless the end-of-file indicator is reset by
3161.Fn gzclearerr
3162and the input file has grown since the previous end of file was detected.
3163.It Xo
3164.Fa int
3165.Fn gzdirect "gzFile file" ;
3166.Xc
3167.Pp
3168Returns true (1) if
3169.Fa file
3170is being copied directly while reading,
3171or false (0) if
3172.Fa file
3173is a gzip stream being decompressed.
3174.Pp
3175If the input file is empty,
3176.Fn gzdirect
3177will return true, since the input does not contain a gzip stream.
3178.Pp
3179If
3180.Fn gzdirect
3181is used immediately after
3182.Fn gzopen
3183or
3184.Fn gzdopen ,
3185it will cause buffers to be allocated to allow reading the file
3186to determine if it is a gzip file.
3187Therefore if
3188.Fn gzbuffer
3189is used, it should be called before
3190.Fn gzdirect .
3191.Pp
3192When writing,
3193.Fn gzdirect
3194returns true (1) if transparent writing was requested
3195("wT" for the
3196.Fn gzopen
3197mode),
3198or false (0) otherwise.
3199(Note:
3200.Fn gzdirect
3201is not needed when writing.
3202Transparent writing must be explicitly requested,
3203so the application already knows the answer.
3204When linking statically, using
3205.Fn gzdirect
3206will include all of the zlib code for gzip file reading and decompression,
3207which may not be desired.)
3208.It Xo
3209.Fa int
3210.Fn gzclose "gzFile file" ;
3211.Xc
3212.Pp
3213Flushes all pending output if necessary, closes the compressed file and
3214deallocates the (de)compression state.
3215Note that once file is closed, you cannot call
3216.Fn gzerror
3217with
3218.Fa file ,
3219since its structures have been deallocated.
3220.Fn gzclose
3221must not be called more than once on the same file,
3222just as
3223.Xr free 3
3224must not be called more than once on the same allocation.
3225.Pp
3226.Fn gzclose
3227will return
3228.Dv Z_STREAM_ERROR
3229if
3230.Fa file
3231is not valid,
3232.Dv Z_ERRNO
3233on a file operation error,
3234.Dv Z_MEM_ERROR
3235if out of memory,
3236.Dv Z_BUF_ERROR
3237if the last read ended in the middle of a gzip stream, or
3238.Dv Z_OK
3239on success.
3240.It Xo
3241.Fa int
3242.Fn gzclose_r "gzFile file" ;
3243.Xc
3244.It Xo
3245.Fa int
3246.Fn gzclose_w "gzFile file" ;
3247.Xc
3248.Pp
3249Same as
3250.Fn gzclose ,
3251but
3252.Fn gzclose_r
3253is only for use when reading, and
3254.Fn gzclose_w
3255is only for use when writing or appending.
3256The advantage to using these instead of
3257.Fn gzclose
3258is that they avoid linking in zlib compression or decompression code
3259that is not used when only reading or only writing, respectively.
3260If
3261.Fn gzclose
3262is used, then both compression and decompression code will be included
3263in the application when linking to a static zlib library.
3264.It Xo
3265.Fa const char *
3266.Fn gzerror "gzFile file" "int *errnum" ;
3267.Xc
3268.Pp
3269The
3270.Fn gzerror
3271function returns the error message for the last error which occurred on the
3272given compressed
3273.Fa file .
3274.Fa errnum
3275is set to the
3276.Nm zlib
3277error number.
3278If an error occurred in the file system and not in the compression library,
3279.Fa errnum
3280is set to
3281.Dv Z_ERRNO
3282and the application may consult errno to get the exact error code.
3283.Pp
3284The application must not modify the returned string.
3285Future calls to this function may invalidate the previously returned string.
3286If
3287.Ar file
3288is closed, then the string previously returned by
3289.Fn gzerror
3290will no longer be available.
3291.Pp
3292.Fn gzerror
3293should be used to distinguish errors from end-of-file for those
3294functions above that do not distinguish those cases in their return values.
3295.It Xo
3296.Fa void
3297.Fn gzclearerr "gzFile file" ;
3298.Xc
3299Clears the error and end-of-file flags for
3300.Fa file .
3301This is analogous to the
3302.Fn clearerr
3303function in stdio.
3304This is useful for continuing to read a gzip file
3305that is being written concurrently.
3306.El
3307.Sh CHECKSUM FUNCTIONS
3308These functions are not related to compression but are exported
3309anyway because they might be useful in applications using the
3310compression library.
3311.Bl -tag -width Ds
3312.It Xo
3313.Fa uLong
3314.Fn adler32 "uLong adler" "const Bytef *buf" "uInt len" ;
3315.Xc
3316The
3317.Fn adler32
3318function updates a running Adler-32 checksum with the bytes buf[0..len-1]
3319and returns the updated checksum.
3320If
3321.Fa buf
3322is
3323.Dv NULL ,
3324this function returns the required initial value for the checksum.
3325.Pp
3326An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
3327much faster.
3328Usage example:
3329.Bd -unfilled -offset indent
3330uLong adler = adler32(0L, NULL, 0);
3331
3332while (read_buffer(buffer, length) != EOF) {
3333adler = adler32(adler, buffer, length);
3334}
3335if (adler != original_adler) error();
3336.Ed
3337.It Xo
3338.Fa uLong
3339.Fn adler32_z "uLong adler" "const Bytef *buf" "z_size_t len" ;
3340.Xc
3341.Pp
3342The same as
3343.Fn adler32 ,
3344but with a size_t length.
3345.It Xo
3346.Fa uLong
3347.Fn adler32_combine "uLong adler1" "uLong adler2" "z_off_t len2" ;
3348.Xc
3349.Pp
3350The
3351.Fn adler32_combine
3352function combines two Adler-32 checksums into one.
3353For two sequences of bytes, seq1 and seq2 with lengths len1 and len2,
3354Adler-32 checksums are calculated for each, adler1 and adler2.
3355.Fn adler32_combine
3356returns the Adler-32 checksum of seq1 and seq2 concatenated,
3357requiring only adler1, adler2, and len2.
3358Note that the z_off_t type (like off_t) is a signed integer.
3359If
3360.Ar len2
3361is negative, the result has no meaning or utility.
3362.It Xo
3363.Fa uLong
3364.Fn crc32 "uLong crc" "const Bytef *buf" "uInt len" ;
3365.Xc
3366.Pp
3367The
3368.Fn crc32
3369function updates a running CRC-32 with the bytes buf[0..len-1]
3370and returns the updated CRC-32.
3371If
3372.Fa buf
3373is
3374.Dv NULL ,
3375this function returns the required initial value for the CRC.
3376Pre- and post-conditioning
3377.Pq one's complement
3378is performed within this function so it shouldn't be done by the application.
3379Usage example:
3380.Bd -unfilled -offset indent
3381uLong crc = crc32(0L, NULL, 0);
3382
3383while (read_buffer(buffer, length) != EOF) {
3384crc = crc32(crc, buffer, length);
3385}
3386if (crc != original_crc) error();
3387.Ed
3388.It Xo
3389.Fa uLong
3390.Fn crc32_z "uLong adler "const Bytef *buf" "z_size_t len" ;
3391.Xc
3392.Pp
3393The same as
3394.Fn crc32 ,
3395but with a size_t length.
3396.It Xo
3397.Fa uLong
3398.Fn crc32_combine "uLong crc1" "uLong crc2" "z_off_t len2" ;
3399.Xc
3400.Pp
3401The
3402.Fn crc32_combine
3403function combines two CRC-32 check values into one.
3404For two sequences of bytes,
3405seq1 and seq2 with lengths len1 and len2,
3406CRC-32 check values are calculated for each, crc1 and crc2.
3407.Fn crc32_combine
3408returns the CRC-32 check value of seq1 and seq2 concatenated,
3409requiring only crc1, crc2, and len2.
3410.El
3411.Sh STRUCTURES
3412.Bd -unfilled
3413struct internal_state;
3414
3415typedef struct z_stream_s {
3416    Bytef    *next_in;  /* next input byte */
3417    uInt     avail_in;  /* number of bytes available at next_in */
3418    off_t    total_in;  /* total number of input bytes read so far */
3419
3420    Bytef    *next_out; /* next output byte will go here */
3421    uInt     avail_out; /* remaining free space at next_out */
3422    off_t    total_out; /* total number of bytes output so far */
3423
3424    char     *msg;      /* last error message, NULL if no error */
3425    struct internal_state FAR *state; /* not visible by applications */
3426
3427    alloc_func zalloc;  /* used to allocate the internal state */
3428    free_func  zfree;   /* used to free the internal state */
3429    voidpf     opaque;  /* private data object passed to zalloc and zfree*/
3430
3431    int     data_type;  /* best guess about the data type: binary or text
3432                           for deflate, or the decoding state for inflate */
3433    uLong   adler;      /* Adler-32 or CRC-32 value of the uncompressed data */
3434    uLong   reserved;   /* reserved for future use */
3435} z_stream;
3436
3437typedef z_stream FAR * z_streamp;
3438.Ed
3439.Bd -unfilled
3440/*
3441     gzip header information passed to and from zlib routines.
3442  See RFC 1952 for more details on the meanings of these fields.
3443*/
3444typedef struct gz_header_s {
3445    int     text;       /* true if compressed data believed to be text */
3446    uLong   time;       /* modification time */
3447    int     xflags;     /*extra flags (not used when writing a gzip file)*/
3448    int     os;         /* operating system */
3449    Bytef   *extra;     /* pointer to extra field or NULL if none */
3450    uInt    extra_len;  /* extra field length (valid if extra != NULL) */
3451    uInt    extra_max;  /* space at extra (only when reading header) */
3452    Bytef   *name;      /* pointer to zero-terminated file name or NULL*/
3453    uInt    name_max;   /* space at name (only when reading header) */
3454    Bytef   *comment;   /* pointer to zero-terminated comment or NULL */
3455    uInt    comm_max;   /* space at comment (only when reading header) */
3456    int     hcrc;       /* true if there was or will be a header crc */
3457    int     done;       /* true when done reading gzip header (not used
3458                           when writing a gzip file) */
3459} gz_header;
3460
3461typedef gz_header FAR *gz_headerp;
3462.Ed
3463.Pp
3464The application must update
3465.Fa next_in
3466and
3467.Fa avail_in
3468when
3469.Fa avail_in
3470has dropped to zero.
3471It must update
3472.Fa next_out
3473and
3474.Fa avail_out
3475when
3476.Fa avail_out
3477has dropped to zero.
3478The application must initialize
3479.Fa zalloc ,
3480.Fa zfree ,
3481and
3482.Fa opaque
3483before calling the init function.
3484All other fields are set by the compression library
3485and must not be updated by the application.
3486.Pp
3487The
3488.Fa opaque
3489value provided by the application will be passed as the first
3490parameter for calls to
3491.Fn zalloc
3492and
3493.Fn zfree .
3494This can be useful for custom memory management.
3495The compression library attaches no meaning to the
3496.Fa opaque
3497value.
3498.Pp
3499.Fa zalloc
3500must return
3501.Dv NULL
3502if there is not enough memory for the object.
3503If
3504.Nm zlib
3505is used in a multi-threaded application,
3506.Fa zalloc
3507and
3508.Fa zfree
3509must be thread safe.
3510In that case,
3511.Nm zlib
3512is thread-safe.
3513When
3514.Fa zalloc
3515and
3516.Fa zfree
3517are
3518.Dv NULL
3519on entry to the initialization function,
3520they are set to internal routines that use the standard library functions
3521.Xr malloc 3
3522and
3523.Xr free 3 .
3524.Pp
3525On 16-bit systems, the functions
3526.Fa zalloc
3527and
3528.Fa zfree
3529must be able to allocate exactly 65536 bytes,
3530but will not be required to allocate more than this if the symbol MAXSEG_64K
3531is defined (see
3532.In zconf.h ) .
3533.Pp
3534WARNING: On MSDOS, pointers returned by
3535.Fa zalloc
3536for objects of exactly 65536 bytes *must* have their offset normalized to zero.
3537The default allocation function provided by this library ensures this (see
3538.Pa zutil.c ) .
3539To reduce memory requirements and avoid any allocation of 64K objects,
3540at the expense of compression ratio,
3541compile the library with -DMAX_WBITS=14 (see
3542.In zconf.h ) .
3543.Pp
3544The fields
3545.Fa total_in
3546and
3547.Fa total_out
3548can be used for statistics or progress reports.
3549After compression,
3550.Fa total_in
3551holds the total size of the uncompressed data and may be saved for use
3552in the decompressor
3553(particularly if the decompressor wants to decompress everything
3554in a single step).
3555.Sh CONSTANTS
3556.Bd -unfilled
3557#define Z_NO_FLUSH      0
3558#define Z_PARTIAL_FLUSH 1
3559#define Z_SYNC_FLUSH    2
3560#define Z_FULL_FLUSH    3
3561#define Z_FINISH        4
3562#define Z_BLOCK         5
3563#define Z_TREES         6
3564/* Allowed flush values; see deflate() and inflate() below for details */
3565
3566#define Z_OK            0
3567#define Z_STREAM_END    1
3568#define Z_NEED_DICT     2
3569#define Z_ERRNO        (-1)
3570#define Z_STREAM_ERROR (-2)
3571#define Z_DATA_ERROR   (-3)
3572#define Z_MEM_ERROR    (-4)
3573#define Z_BUF_ERROR    (-5)
3574#define Z_VERSION_ERROR (-6)
3575/* Return codes for the compression/decompression functions.
3576 * Negative values are errors,
3577 * positive values are used for special but normal events.
3578 */
3579
3580#define Z_NO_COMPRESSION         0
3581#define Z_BEST_SPEED             1
3582#define Z_BEST_COMPRESSION       9
3583#define Z_DEFAULT_COMPRESSION  (-1)
3584/* compression levels */
3585
3586#define Z_FILTERED            1
3587#define Z_HUFFMAN_ONLY        2
3588#define Z_RLE                 3
3589#define Z_FIXED               4
3590#define Z_DEFAULT_STRATEGY    0
3591/* compression strategy; see deflateInit2() below for details */
3592
3593#define Z_BINARY   0
3594#define Z_TEXT     1
3595#define Z_ASCII    Z_TEXT /* for compatibility with 1.2.2 and earlier */
3596#define Z_UNKNOWN  2
3597/* Possible values of the data_type field for deflate() */
3598
3599#define Z_DEFLATED   8
3600/* The deflate compression method
3601 * (the only one supported in this version)
3602*/
3603
3604#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
3605
3606#define zlib_version zlibVersion()
3607/* for compatibility with versions < 1.0.2 */
3608.Ed
3609.Sh VARIOUS HACKS
3610deflateInit and inflateInit are macros to allow checking the
3611.Nm zlib
3612version and the compiler's view of
3613.Fa z_stream .
3614.Bl -tag -width Ds
3615.It Xo
3616.Fa int
3617.Fn deflateInit_ "z_stream strm" "int level" "const char *version" "int stream_size" ;
3618.Xc
3619.It Xo
3620.Fa int
3621.Fn inflateInit_ "z_stream strm" "const char *version" "int stream_size" ;
3622.Xc
3623.It Xo
3624.Fa int
3625.Fo deflateInit2_
3626.Fa "z_stream strm"
3627.Fa "int level"
3628.Fa "int method"
3629.Fa "int windowBits"
3630.Fa "int memLevel"
3631.Fa "int strategy"
3632.Fa "const char *version"
3633.Fa "int stream_size"
3634.Fc ;
3635.Xc
3636.It Xo
3637.Fa int
3638.Fn inflateInit2_ "z_stream strm" "int windowBits" "const char *version" "int stream_size" ;
3639.Xc
3640.It Xo
3641.Fa int
3642.Fn inflateBackInit_ "z_stream *strm" "int windowBits" "unsigned char FAR *window" "const char *version" "int stream_size" ;
3643.Xc
3644.It Xo
3645.Fa const char *
3646.Fn zError "int err" ;
3647.Xc
3648.It Xo
3649.Fa int
3650.Fn inflateSyncPoint "z_streamp z" ;
3651.Xc
3652.It Xo
3653.Fa const uLongf *
3654.Fn "get_crc_table" "void" ;
3655.Xc
3656.El
3657.Sh SEE ALSO
3658.Xr compress 1 ,
3659.Xr gzip 1
3660.Sh STANDARDS
3661.Rs
3662.%A P. Deutsch
3663.%A J-L. Gailly
3664.%D May 1996
3665.%R RFC 1950
3666.%T ZLIB Compressed Data Format Specification version 3.3
3667.Re
3668.Pp
3669.Rs
3670.%A P. Deutsch
3671.%D May 1996
3672.%R RFC 1951
3673.%T DEFLATE Compressed Data Format Specification version 1.3
3674.Re
3675.Pp
3676.Rs
3677.%A P. Deutsch
3678.%D May 1996
3679.%R RFC 1952
3680.%T GZIP file format specification version 4.3
3681.Re
3682.Sh HISTORY
3683This manual page is based on an HTML version of
3684.In zlib.h
3685converted by
3686.An piaip Aq Mt piaip@csie.ntu.edu.tw
3687and was converted to mdoc format by the
3688.Ox
3689project.
3690.Sh AUTHORS
3691.An Jean-loup Gailly Aq Mt jloup@gzip.org
3692.An Mark Adler Aq Mt madler@alumni.caltech.edu
3693