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