zlib.h revision 1.15 1 1.15 riastrad /* $NetBSD: zlib.h,v 1.15 2020/03/05 07:46:49 riastradh Exp $ */
2 1.1 paulus
3 1.1 paulus /* zlib.h -- interface of the 'zlib' general purpose compression library
4 1.6 fvdl version 1.1.4, March 11th, 2002
5 1.1 paulus
6 1.6 fvdl Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
7 1.1 paulus
8 1.1 paulus This software is provided 'as-is', without any express or implied
9 1.1 paulus warranty. In no event will the authors be held liable for any damages
10 1.1 paulus arising from the use of this software.
11 1.1 paulus
12 1.1 paulus Permission is granted to anyone to use this software for any purpose,
13 1.1 paulus including commercial applications, and to alter it and redistribute it
14 1.1 paulus freely, subject to the following restrictions:
15 1.1 paulus
16 1.1 paulus 1. The origin of this software must not be misrepresented; you must not
17 1.1 paulus claim that you wrote the original software. If you use this software
18 1.1 paulus in a product, an acknowledgment in the product documentation would be
19 1.1 paulus appreciated but is not required.
20 1.1 paulus 2. Altered source versions must be plainly marked as such, and must not be
21 1.1 paulus misrepresented as being the original software.
22 1.1 paulus 3. This notice may not be removed or altered from any source distribution.
23 1.1 paulus
24 1.1 paulus Jean-loup Gailly Mark Adler
25 1.6 fvdl jloup (at) gzip.org madler (at) alumni.caltech.edu
26 1.4 christos
27 1.4 christos
28 1.4 christos The data format used by the zlib library is described by RFCs (Request for
29 1.4 christos Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
30 1.4 christos (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
31 1.4 christos */
32 1.1 paulus
33 1.10 elad #ifndef _NET_ZLIB_H_
34 1.10 elad #define _NET_ZLIB_H_
35 1.1 paulus
36 1.6 fvdl #ifdef __NetBSD__
37 1.6 fvdl #include <sys/cdefs.h>
38 1.4 christos #endif
39 1.4 christos
40 1.4 christos /* +++ zconf.h */
41 1.1 paulus /* zconf.h -- configuration of the zlib compression library
42 1.6 fvdl * Copyright (C) 1995-2002 Jean-loup Gailly.
43 1.8 perry * For conditions of distribution and use, see copyright notice in zlib.h
44 1.1 paulus */
45 1.1 paulus
46 1.15 riastrad /* @(#) $Id: zlib.h,v 1.15 2020/03/05 07:46:49 riastradh Exp $ */
47 1.4 christos
48 1.12 christos #ifndef ZCONF_H
49 1.12 christos #define ZCONF_H
50 1.1 paulus
51 1.1 paulus /*
52 1.6 fvdl * Warning: This file pollutes the user's namespace with:
53 1.6 fvdl * Byte Bytef EXPORT FAR OF STDC
54 1.6 fvdl * charf intf uInt uIntf uLong uLonf
55 1.6 fvdl * Programs using this library appear to expect those...
56 1.6 fvdl */
57 1.6 fvdl
58 1.6 fvdl #include <sys/types.h>
59 1.6 fvdl
60 1.6 fvdl /*
61 1.4 christos * If you *really* need a unique prefix for all types and library functions,
62 1.4 christos * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
63 1.1 paulus */
64 1.4 christos #ifdef Z_PREFIX
65 1.4 christos # define deflateInit_ z_deflateInit_
66 1.4 christos # define deflate z_deflate
67 1.4 christos # define deflateEnd z_deflateEnd
68 1.4 christos # define inflateInit_ z_inflateInit_
69 1.4 christos # define inflate z_inflate
70 1.4 christos # define inflateEnd z_inflateEnd
71 1.4 christos # define deflateInit2_ z_deflateInit2_
72 1.4 christos # define deflateSetDictionary z_deflateSetDictionary
73 1.4 christos # define deflateCopy z_deflateCopy
74 1.4 christos # define deflateReset z_deflateReset
75 1.4 christos # define deflateParams z_deflateParams
76 1.4 christos # define inflateInit2_ z_inflateInit2_
77 1.4 christos # define inflateSetDictionary z_inflateSetDictionary
78 1.4 christos # define inflateSync z_inflateSync
79 1.6 fvdl # define inflateSyncPoint z_inflateSyncPoint
80 1.4 christos # define inflateReset z_inflateReset
81 1.4 christos # define compress z_compress
82 1.6 fvdl # define compress2 z_compress2
83 1.4 christos # define uncompress z_uncompress
84 1.4 christos # define adler32 z_adler32
85 1.4 christos # define crc32 z_crc32
86 1.4 christos # define get_crc_table z_get_crc_table
87 1.4 christos
88 1.4 christos # define Byte z_Byte
89 1.4 christos # define uInt z_uInt
90 1.4 christos # define uLong z_uLong
91 1.4 christos # define Bytef z_Bytef
92 1.4 christos # define charf z_charf
93 1.4 christos # define intf z_intf
94 1.4 christos # define uIntf z_uIntf
95 1.4 christos # define uLongf z_uLongf
96 1.4 christos # define voidpf z_voidpf
97 1.4 christos # define voidp z_voidp
98 1.4 christos #endif
99 1.4 christos
100 1.6 fvdl #ifndef __32BIT__
101 1.6 fvdl /* Don't be alarmed; this just means we have at least 32-bits */
102 1.6 fvdl # define __32BIT__
103 1.4 christos #endif
104 1.1 paulus
105 1.1 paulus /*
106 1.1 paulus * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
107 1.1 paulus * than 64k bytes at a time (needed on systems with 16-bit int).
108 1.1 paulus */
109 1.4 christos #if defined(MSDOS) && !defined(__32BIT__)
110 1.4 christos # define MAXSEG_64K
111 1.4 christos #endif
112 1.6 fvdl
113 1.6 fvdl #if 0
114 1.6 fvdl /* XXX: Are there machines where we should define this? m68k? */
115 1.4 christos # define UNALIGNED_OK
116 1.4 christos #endif
117 1.4 christos
118 1.6 fvdl #if (defined(__STDC__) || defined(__cplusplus)) && !defined(STDC)
119 1.6 fvdl /* XXX: Look out - this is used in zutil.h and elsewhere... */
120 1.4 christos # define STDC
121 1.4 christos #endif
122 1.6 fvdl #if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
123 1.6 fvdl # ifndef STDC
124 1.6 fvdl # define STDC
125 1.6 fvdl # endif
126 1.4 christos #endif
127 1.1 paulus
128 1.1 paulus #ifndef STDC
129 1.6 fvdl # ifndef const
130 1.4 christos # define const
131 1.1 paulus # endif
132 1.1 paulus #endif
133 1.1 paulus
134 1.4 christos /* Some Mac compilers merge all .h files incorrectly: */
135 1.4 christos #if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
136 1.4 christos # define NO_DUMMY_DECL
137 1.1 paulus #endif
138 1.15 riastrad #define NO_DUMMY_DECL
139 1.1 paulus
140 1.6 fvdl /* Old Borland C incorrectly complains about missing returns: */
141 1.6 fvdl #if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
142 1.6 fvdl # define NEED_DUMMY_RETURN
143 1.6 fvdl #endif
144 1.6 fvdl
145 1.6 fvdl
146 1.1 paulus /* Maximum value for memLevel in deflateInit2 */
147 1.1 paulus #ifndef MAX_MEM_LEVEL
148 1.1 paulus # ifdef MAXSEG_64K
149 1.1 paulus # define MAX_MEM_LEVEL 8
150 1.1 paulus # else
151 1.1 paulus # define MAX_MEM_LEVEL 9
152 1.1 paulus # endif
153 1.1 paulus #endif
154 1.1 paulus
155 1.6 fvdl /* Maximum value for windowBits in deflateInit2 and inflateInit2.
156 1.6 fvdl * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
157 1.6 fvdl * created by gzip. (Files created by minigzip can still be extracted by
158 1.6 fvdl * gzip.)
159 1.6 fvdl */
160 1.1 paulus #ifndef MAX_WBITS
161 1.1 paulus # define MAX_WBITS 15 /* 32K LZ77 window */
162 1.1 paulus #endif
163 1.1 paulus
164 1.1 paulus /* The memory requirements for deflate are (in bytes):
165 1.6 fvdl (1 << (windowBits+2)) + (1 << (memLevel+9))
166 1.1 paulus that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
167 1.1 paulus plus a few kilobytes for small objects. For example, if you want to reduce
168 1.1 paulus the default memory requirements from 256K to 128K, compile with
169 1.1 paulus make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
170 1.1 paulus Of course this will generally degrade compression (there's no free lunch).
171 1.1 paulus
172 1.1 paulus The memory requirements for inflate are (in bytes) 1 << windowBits
173 1.1 paulus that is, 32K for windowBits=15 (default value) plus a few kilobytes
174 1.1 paulus for small objects.
175 1.1 paulus */
176 1.1 paulus
177 1.1 paulus /* Type declarations */
178 1.1 paulus
179 1.6 fvdl #ifndef __P /* function prototypes */
180 1.1 paulus # ifdef STDC
181 1.6 fvdl # define __P(args) args
182 1.1 paulus # else
183 1.6 fvdl # define __P(args) ()
184 1.1 paulus # endif
185 1.1 paulus #endif
186 1.1 paulus
187 1.4 christos /* The following definitions for FAR are needed only for MSDOS mixed
188 1.4 christos * model programming (small or medium model with some far allocations).
189 1.4 christos * This was tested only with MSC; for other MSDOS compilers you may have
190 1.4 christos * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
191 1.4 christos * just define FAR to be empty.
192 1.4 christos */
193 1.4 christos #if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
194 1.4 christos /* MSC small or medium model */
195 1.4 christos # define SMALL_MEDIUM
196 1.4 christos # ifdef _MSC_VER
197 1.6 fvdl # define FAR _far
198 1.4 christos # else
199 1.4 christos # define FAR far
200 1.4 christos # endif
201 1.4 christos #endif
202 1.4 christos #if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
203 1.4 christos # ifndef __32BIT__
204 1.4 christos # define SMALL_MEDIUM
205 1.6 fvdl # define FAR _far
206 1.6 fvdl # endif
207 1.6 fvdl #endif
208 1.6 fvdl
209 1.6 fvdl /* Compile with -DZLIB_DLL for Windows DLL support */
210 1.6 fvdl #if defined(ZLIB_DLL)
211 1.6 fvdl # if defined(_WINDOWS) || defined(WINDOWS)
212 1.6 fvdl # ifdef FAR
213 1.6 fvdl # undef FAR
214 1.6 fvdl # endif
215 1.6 fvdl # include <windows.h>
216 1.6 fvdl # define ZEXPORT WINAPI
217 1.6 fvdl # ifdef WIN32
218 1.6 fvdl # define ZEXPORTVA WINAPIV
219 1.6 fvdl # else
220 1.6 fvdl # define ZEXPORTVA FAR _cdecl _export
221 1.6 fvdl # endif
222 1.6 fvdl # endif
223 1.6 fvdl # if defined (__BORLANDC__)
224 1.6 fvdl # if (__BORLANDC__ >= 0x0500) && defined (WIN32)
225 1.6 fvdl # include <windows.h>
226 1.6 fvdl # define ZEXPORT __declspec(dllexport) WINAPI
227 1.6 fvdl # define ZEXPORTRVA __declspec(dllexport) WINAPIV
228 1.6 fvdl # else
229 1.6 fvdl # if defined (_Windows) && defined (__DLL__)
230 1.6 fvdl # define ZEXPORT _export
231 1.6 fvdl # define ZEXPORTVA _export
232 1.6 fvdl # endif
233 1.6 fvdl # endif
234 1.4 christos # endif
235 1.4 christos #endif
236 1.6 fvdl
237 1.6 fvdl #if defined (__BEOS__)
238 1.6 fvdl # if defined (ZLIB_DLL)
239 1.6 fvdl # define ZEXTERN extern __declspec(dllexport)
240 1.6 fvdl # else
241 1.6 fvdl # define ZEXTERN extern __declspec(dllimport)
242 1.6 fvdl # endif
243 1.6 fvdl #endif
244 1.6 fvdl
245 1.6 fvdl #ifndef ZEXPORT
246 1.6 fvdl # define ZEXPORT
247 1.6 fvdl #endif
248 1.6 fvdl #ifndef ZEXPORTVA
249 1.6 fvdl # define ZEXPORTVA
250 1.6 fvdl #endif
251 1.6 fvdl #ifndef ZEXTERN
252 1.6 fvdl # define ZEXTERN extern
253 1.6 fvdl #endif
254 1.6 fvdl
255 1.4 christos #ifndef FAR
256 1.4 christos # define FAR
257 1.4 christos #endif
258 1.4 christos
259 1.6 fvdl #if !defined(MACOS) && !defined(TARGET_OS_MAC)
260 1.1 paulus typedef unsigned char Byte; /* 8 bits */
261 1.6 fvdl #endif
262 1.1 paulus typedef unsigned int uInt; /* 16 bits or more */
263 1.1 paulus typedef unsigned long uLong; /* 32 bits or more */
264 1.1 paulus
265 1.6 fvdl #ifdef SMALL_MEDIUM
266 1.6 fvdl /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
267 1.4 christos # define Bytef Byte FAR
268 1.4 christos #else
269 1.4 christos typedef Byte FAR Bytef;
270 1.4 christos #endif
271 1.4 christos typedef char FAR charf;
272 1.4 christos typedef int FAR intf;
273 1.4 christos typedef uInt FAR uIntf;
274 1.1 paulus typedef uLong FAR uLongf;
275 1.1 paulus
276 1.1 paulus #ifdef STDC
277 1.1 paulus typedef void FAR *voidpf;
278 1.1 paulus typedef void *voidp;
279 1.1 paulus #else
280 1.1 paulus typedef Byte FAR *voidpf;
281 1.1 paulus typedef Byte *voidp;
282 1.1 paulus #endif
283 1.1 paulus
284 1.6 fvdl #if (defined(HAVE_UNISTD_H) || defined(__NetBSD__)) && !defined(_KERNEL)
285 1.6 fvdl # include <sys/types.h> /* for off_t */
286 1.6 fvdl # include <unistd.h> /* for SEEK_* and off_t */
287 1.6 fvdl # define z_off_t off_t
288 1.6 fvdl #endif
289 1.6 fvdl #ifndef SEEK_SET
290 1.6 fvdl # define SEEK_SET 0 /* Seek from beginning of file. */
291 1.6 fvdl # define SEEK_CUR 1 /* Seek from current position. */
292 1.6 fvdl # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
293 1.6 fvdl #endif
294 1.6 fvdl #ifndef z_off_t
295 1.6 fvdl # define z_off_t long
296 1.6 fvdl #endif
297 1.6 fvdl
298 1.6 fvdl /* MVS linker does not support external names larger than 8 bytes */
299 1.6 fvdl #if defined(__MVS__)
300 1.6 fvdl # pragma map(deflateInit_,"DEIN")
301 1.6 fvdl # pragma map(deflateInit2_,"DEIN2")
302 1.6 fvdl # pragma map(deflateEnd,"DEEND")
303 1.6 fvdl # pragma map(inflateInit_,"ININ")
304 1.6 fvdl # pragma map(inflateInit2_,"ININ2")
305 1.6 fvdl # pragma map(inflateEnd,"INEND")
306 1.6 fvdl # pragma map(inflateSync,"INSY")
307 1.6 fvdl # pragma map(inflateSetDictionary,"INSEDI")
308 1.6 fvdl # pragma map(inflate_blocks,"INBL")
309 1.6 fvdl # pragma map(inflate_blocks_new,"INBLNE")
310 1.6 fvdl # pragma map(inflate_blocks_free,"INBLFR")
311 1.6 fvdl # pragma map(inflate_blocks_reset,"INBLRE")
312 1.6 fvdl # pragma map(inflate_codes_free,"INCOFR")
313 1.6 fvdl # pragma map(inflate_codes,"INCO")
314 1.6 fvdl # pragma map(inflate_fast,"INFA")
315 1.6 fvdl # pragma map(inflate_flush,"INFLU")
316 1.6 fvdl # pragma map(inflate_mask,"INMA")
317 1.6 fvdl # pragma map(inflate_set_dictionary,"INSEDI2")
318 1.6 fvdl # pragma map(inflate_copyright,"INCOPY")
319 1.6 fvdl # pragma map(inflate_trees_bits,"INTRBI")
320 1.6 fvdl # pragma map(inflate_trees_dynamic,"INTRDY")
321 1.6 fvdl # pragma map(inflate_trees_fixed,"INTRFI")
322 1.6 fvdl # pragma map(inflate_trees_free,"INTRFR")
323 1.4 christos #endif
324 1.4 christos
325 1.12 christos #endif /* !ZCONF_H */
326 1.4 christos /* --- zconf.h */
327 1.4 christos
328 1.12 christos #ifndef ZLIB_H
329 1.12 christos #define ZLIB_H
330 1.6 fvdl #ifdef __cplusplus
331 1.6 fvdl extern "C" {
332 1.6 fvdl #endif
333 1.6 fvdl
334 1.6 fvdl #define ZLIB_VERSION "1.1.4"
335 1.1 paulus
336 1.8 perry /*
337 1.1 paulus The 'zlib' compression library provides in-memory compression and
338 1.1 paulus decompression functions, including integrity checks of the uncompressed
339 1.1 paulus data. This version of the library supports only one compression method
340 1.6 fvdl (deflation) but other algorithms will be added later and will have the same
341 1.1 paulus stream interface.
342 1.1 paulus
343 1.1 paulus Compression can be done in a single step if the buffers are large
344 1.1 paulus enough (for example if an input file is mmap'ed), or can be done by
345 1.1 paulus repeated calls of the compression function. In the latter case, the
346 1.1 paulus application must provide more input and/or consume the output
347 1.1 paulus (providing more output space) before each call.
348 1.4 christos
349 1.6 fvdl The library also supports reading and writing files in gzip (.gz) format
350 1.6 fvdl with an interface similar to that of stdio.
351 1.6 fvdl
352 1.6 fvdl The library does not install any signal handler. The decoder checks
353 1.6 fvdl the consistency of the compressed data, so the library should never
354 1.6 fvdl crash even in case of corrupted input.
355 1.1 paulus */
356 1.1 paulus
357 1.13 dsl typedef voidpf (*alloc_func)(voidpf, uInt, uInt);
358 1.13 dsl typedef void (*free_func)(voidpf, voidpf);
359 1.1 paulus
360 1.1 paulus struct internal_state;
361 1.1 paulus
362 1.1 paulus typedef struct z_stream_s {
363 1.1 paulus Bytef *next_in; /* next input byte */
364 1.1 paulus uInt avail_in; /* number of bytes available at next_in */
365 1.1 paulus uLong total_in; /* total nb of input bytes read so far */
366 1.1 paulus
367 1.1 paulus Bytef *next_out; /* next output byte should be put there */
368 1.1 paulus uInt avail_out; /* remaining free space at next_out */
369 1.1 paulus uLong total_out; /* total nb of bytes output so far */
370 1.1 paulus
371 1.9 christos const char *msg; /* last error message, NULL if no error */
372 1.1 paulus struct internal_state FAR *state; /* not visible by applications */
373 1.1 paulus
374 1.1 paulus alloc_func zalloc; /* used to allocate the internal state */
375 1.1 paulus free_func zfree; /* used to free the internal state */
376 1.4 christos voidpf opaque; /* private data object passed to zalloc and zfree */
377 1.1 paulus
378 1.4 christos int data_type; /* best guess about the data type: ascii or binary */
379 1.4 christos uLong adler; /* adler32 value of the uncompressed data */
380 1.4 christos uLong reserved; /* reserved for future use */
381 1.4 christos } z_stream;
382 1.1 paulus
383 1.4 christos typedef z_stream FAR *z_streamp;
384 1.1 paulus
385 1.1 paulus /*
386 1.1 paulus The application must update next_in and avail_in when avail_in has
387 1.1 paulus dropped to zero. It must update next_out and avail_out when avail_out
388 1.1 paulus has dropped to zero. The application must initialize zalloc, zfree and
389 1.1 paulus opaque before calling the init function. All other fields are set by the
390 1.1 paulus compression library and must not be updated by the application.
391 1.1 paulus
392 1.1 paulus The opaque value provided by the application will be passed as the first
393 1.1 paulus parameter for calls of zalloc and zfree. This can be useful for custom
394 1.1 paulus memory management. The compression library attaches no meaning to the
395 1.1 paulus opaque value.
396 1.1 paulus
397 1.1 paulus zalloc must return Z_NULL if there is not enough memory for the object.
398 1.6 fvdl If zlib is used in a multi-threaded application, zalloc and zfree must be
399 1.6 fvdl thread safe.
400 1.6 fvdl
401 1.1 paulus On 16-bit systems, the functions zalloc and zfree must be able to allocate
402 1.1 paulus exactly 65536 bytes, but will not be required to allocate more than this
403 1.1 paulus if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
404 1.1 paulus pointers returned by zalloc for objects of exactly 65536 bytes *must*
405 1.1 paulus have their offset normalized to zero. The default allocation function
406 1.1 paulus provided by this library ensures this (see zutil.c). To reduce memory
407 1.1 paulus requirements and avoid any allocation of 64K objects, at the expense of
408 1.1 paulus compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
409 1.1 paulus
410 1.1 paulus The fields total_in and total_out can be used for statistics or
411 1.1 paulus progress reports. After compression, total_in holds the total size of
412 1.1 paulus the uncompressed data and may be saved for use in the decompressor
413 1.1 paulus (particularly if the decompressor wants to decompress everything in
414 1.1 paulus a single step).
415 1.1 paulus */
416 1.1 paulus
417 1.1 paulus /* constants */
418 1.1 paulus
419 1.1 paulus #define Z_NO_FLUSH 0
420 1.6 fvdl #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
421 1.6 fvdl #define Z_PACKET_FLUSH 2
422 1.4 christos #define Z_SYNC_FLUSH 3
423 1.4 christos #define Z_FULL_FLUSH 4
424 1.4 christos #define Z_FINISH 5
425 1.4 christos /* Allowed flush values; see deflate() below for details */
426 1.1 paulus
427 1.1 paulus #define Z_OK 0
428 1.1 paulus #define Z_STREAM_END 1
429 1.4 christos #define Z_NEED_DICT 2
430 1.1 paulus #define Z_ERRNO (-1)
431 1.1 paulus #define Z_STREAM_ERROR (-2)
432 1.1 paulus #define Z_DATA_ERROR (-3)
433 1.1 paulus #define Z_MEM_ERROR (-4)
434 1.1 paulus #define Z_BUF_ERROR (-5)
435 1.4 christos #define Z_VERSION_ERROR (-6)
436 1.4 christos /* Return codes for the compression/decompression functions. Negative
437 1.4 christos * values are errors, positive values are used for special but normal events.
438 1.4 christos */
439 1.1 paulus
440 1.4 christos #define Z_NO_COMPRESSION 0
441 1.1 paulus #define Z_BEST_SPEED 1
442 1.1 paulus #define Z_BEST_COMPRESSION 9
443 1.1 paulus #define Z_DEFAULT_COMPRESSION (-1)
444 1.1 paulus /* compression levels */
445 1.1 paulus
446 1.1 paulus #define Z_FILTERED 1
447 1.1 paulus #define Z_HUFFMAN_ONLY 2
448 1.1 paulus #define Z_DEFAULT_STRATEGY 0
449 1.4 christos /* compression strategy; see deflateInit2() below for details */
450 1.1 paulus
451 1.1 paulus #define Z_BINARY 0
452 1.1 paulus #define Z_ASCII 1
453 1.1 paulus #define Z_UNKNOWN 2
454 1.4 christos /* Possible values of the data_type field */
455 1.4 christos
456 1.4 christos #define Z_DEFLATED 8
457 1.4 christos /* The deflate compression method (the only one supported in this version) */
458 1.1 paulus
459 1.1 paulus #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
460 1.1 paulus
461 1.4 christos #define zlib_version zlibVersion()
462 1.4 christos /* for compatibility with versions < 1.0.2 */
463 1.4 christos
464 1.4 christos /* basic functions */
465 1.4 christos
466 1.13 dsl ZEXTERN const char * ZEXPORT zlibVersion(void);
467 1.4 christos /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
468 1.1 paulus If the first character differs, the library code actually used is
469 1.1 paulus not compatible with the zlib.h header file used by the application.
470 1.4 christos This check is automatically made by deflateInit and inflateInit.
471 1.1 paulus */
472 1.1 paulus
473 1.8 perry /*
474 1.13 dsl ZEXTERN int ZEXPORT deflateInit(z_streamp, int);
475 1.1 paulus
476 1.1 paulus Initializes the internal stream state for compression. The fields
477 1.1 paulus zalloc, zfree and opaque must be initialized before by the caller.
478 1.1 paulus If zalloc and zfree are set to Z_NULL, deflateInit updates them to
479 1.1 paulus use default allocation functions.
480 1.1 paulus
481 1.4 christos The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
482 1.4 christos 1 gives best speed, 9 gives best compression, 0 gives no compression at
483 1.4 christos all (the input data is simply copied a block at a time).
484 1.4 christos Z_DEFAULT_COMPRESSION requests a default compromise between speed and
485 1.4 christos compression (currently equivalent to level 6).
486 1.1 paulus
487 1.1 paulus deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
488 1.4 christos enough memory, Z_STREAM_ERROR if level is not a valid compression level,
489 1.4 christos Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
490 1.4 christos with the version assumed by the caller (ZLIB_VERSION).
491 1.1 paulus msg is set to null if there is no error message. deflateInit does not
492 1.1 paulus perform any compression: this will be done by deflate().
493 1.1 paulus */
494 1.1 paulus
495 1.1 paulus
496 1.13 dsl ZEXTERN int ZEXPORT deflate(z_streamp, int);
497 1.1 paulus /*
498 1.6 fvdl deflate compresses as much data as possible, and stops when the input
499 1.6 fvdl buffer becomes empty or the output buffer becomes full. It may introduce some
500 1.6 fvdl output latency (reading input without producing any output) except when
501 1.6 fvdl forced to flush.
502 1.6 fvdl
503 1.6 fvdl The detailed semantics are as follows. deflate performs one or both of the
504 1.6 fvdl following actions:
505 1.1 paulus
506 1.1 paulus - Compress more input starting at next_in and update next_in and avail_in
507 1.1 paulus accordingly. If not all input can be processed (because there is not
508 1.1 paulus enough room in the output buffer), next_in and avail_in are updated and
509 1.1 paulus processing will resume at this point for the next call of deflate().
510 1.1 paulus
511 1.1 paulus - Provide more output starting at next_out and update next_out and avail_out
512 1.1 paulus accordingly. This action is forced if the parameter flush is non zero.
513 1.1 paulus Forcing flush frequently degrades the compression ratio, so this parameter
514 1.1 paulus should be set only when necessary (in interactive applications).
515 1.1 paulus Some output may be provided even if flush is not set.
516 1.1 paulus
517 1.1 paulus Before the call of deflate(), the application should ensure that at least
518 1.1 paulus one of the actions is possible, by providing more input and/or consuming
519 1.1 paulus more output, and updating avail_in or avail_out accordingly; avail_out
520 1.1 paulus should never be zero before the call. The application can consume the
521 1.1 paulus compressed output when it wants, for example when the output buffer is full
522 1.4 christos (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
523 1.4 christos and with zero avail_out, it must be called again after making room in the
524 1.4 christos output buffer because there might be more output pending.
525 1.1 paulus
526 1.6 fvdl If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
527 1.6 fvdl flushed to the output buffer and the output is aligned on a byte boundary, so
528 1.6 fvdl that the decompressor can get all input data available so far. (In particular
529 1.6 fvdl avail_in is zero after the call if enough output space has been provided
530 1.6 fvdl before the call.) Flushing may degrade compression for some compression
531 1.6 fvdl algorithms and so it should be used only when necessary.
532 1.6 fvdl
533 1.6 fvdl If flush is set to Z_FULL_FLUSH, all output is flushed as with
534 1.6 fvdl Z_SYNC_FLUSH, and the compression state is reset so that decompression can
535 1.6 fvdl restart from this point if previous compressed data has been damaged or if
536 1.6 fvdl random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
537 1.6 fvdl the compression.
538 1.6 fvdl
539 1.6 fvdl If deflate returns with avail_out == 0, this function must be called again
540 1.6 fvdl with the same value of the flush parameter and more output space (updated
541 1.6 fvdl avail_out), until the flush is complete (deflate returns with non-zero
542 1.6 fvdl avail_out).
543 1.1 paulus
544 1.1 paulus If the parameter flush is set to Z_PACKET_FLUSH, the compression
545 1.1 paulus block is terminated, and a zero-length stored block is output,
546 1.1 paulus omitting the length bytes (the effect of this is that the 3-bit type
547 1.1 paulus code 000 for a stored block is output, and the output is then
548 1.1 paulus byte-aligned). This is designed for use at the end of a PPP packet.
549 1.1 paulus
550 1.6 fvdl
551 1.4 christos If the parameter flush is set to Z_FINISH, pending input is processed,
552 1.4 christos pending output is flushed and deflate returns with Z_STREAM_END if there
553 1.1 paulus was enough output space; if deflate returns with Z_OK, this function must be
554 1.1 paulus called again with Z_FINISH and more output space (updated avail_out) but no
555 1.1 paulus more input data, until it returns with Z_STREAM_END or an error. After
556 1.1 paulus deflate has returned Z_STREAM_END, the only possible operations on the
557 1.1 paulus stream are deflateReset or deflateEnd.
558 1.8 perry
559 1.1 paulus Z_FINISH can be used immediately after deflateInit if all the compression
560 1.1 paulus is to be done in a single step. In this case, avail_out must be at least
561 1.1 paulus 0.1% larger than avail_in plus 12 bytes. If deflate does not return
562 1.1 paulus Z_STREAM_END, then it must be called again as described above.
563 1.1 paulus
564 1.6 fvdl deflate() sets strm->adler to the adler32 checksum of all input read
565 1.6 fvdl so far (that is, total_in bytes).
566 1.6 fvdl
567 1.1 paulus deflate() may update data_type if it can make a good guess about
568 1.1 paulus the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
569 1.1 paulus binary. This field is only for information purposes and does not affect
570 1.1 paulus the compression algorithm in any manner.
571 1.1 paulus
572 1.1 paulus deflate() returns Z_OK if some progress has been made (more input
573 1.1 paulus processed or more output produced), Z_STREAM_END if all input has been
574 1.1 paulus consumed and all output has been produced (only when flush is set to
575 1.1 paulus Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
576 1.6 fvdl if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
577 1.6 fvdl (for example avail_in or avail_out was zero).
578 1.1 paulus */
579 1.1 paulus
580 1.1 paulus
581 1.13 dsl ZEXTERN int ZEXPORT deflateEnd(z_streamp);
582 1.1 paulus /*
583 1.1 paulus All dynamically allocated data structures for this stream are freed.
584 1.1 paulus This function discards any unprocessed input and does not flush any
585 1.1 paulus pending output.
586 1.1 paulus
587 1.1 paulus deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
588 1.4 christos stream state was inconsistent, Z_DATA_ERROR if the stream was freed
589 1.4 christos prematurely (some input or output was discarded). In the error case,
590 1.4 christos msg may be set but then points to a static string (which must not be
591 1.4 christos deallocated).
592 1.1 paulus */
593 1.1 paulus
594 1.1 paulus
595 1.8 perry /*
596 1.13 dsl ZEXTERN int ZEXPORT inflateInit(z_streamp);
597 1.4 christos
598 1.1 paulus Initializes the internal stream state for decompression. The fields
599 1.6 fvdl next_in, avail_in, zalloc, zfree and opaque must be initialized before by
600 1.6 fvdl the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
601 1.6 fvdl value depends on the compression method), inflateInit determines the
602 1.6 fvdl compression method from the zlib header and allocates all data structures
603 1.6 fvdl accordingly; otherwise the allocation will be deferred to the first call of
604 1.6 fvdl inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
605 1.6 fvdl use default allocation functions.
606 1.1 paulus
607 1.6 fvdl inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
608 1.6 fvdl memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
609 1.6 fvdl version assumed by the caller. msg is set to null if there is no error
610 1.6 fvdl message. inflateInit does not perform any decompression apart from reading
611 1.6 fvdl the zlib header if present: this will be done by inflate(). (So next_in and
612 1.6 fvdl avail_in may be modified, but next_out and avail_out are unchanged.)
613 1.1 paulus */
614 1.1 paulus
615 1.2 christos
616 1.13 dsl ZEXTERN int ZEXPORT inflate(z_streamp, int);
617 1.1 paulus /*
618 1.6 fvdl inflate decompresses as much data as possible, and stops when the input
619 1.6 fvdl buffer becomes empty or the output buffer becomes full. It may some
620 1.6 fvdl introduce some output latency (reading input without producing any output)
621 1.6 fvdl except when forced to flush.
622 1.6 fvdl
623 1.6 fvdl The detailed semantics are as follows. inflate performs one or both of the
624 1.6 fvdl following actions:
625 1.1 paulus
626 1.1 paulus - Decompress more input starting at next_in and update next_in and avail_in
627 1.1 paulus accordingly. If not all input can be processed (because there is not
628 1.1 paulus enough room in the output buffer), next_in is updated and processing
629 1.1 paulus will resume at this point for the next call of inflate().
630 1.1 paulus
631 1.1 paulus - Provide more output starting at next_out and update next_out and avail_out
632 1.4 christos accordingly. inflate() provides as much output as possible, until there
633 1.4 christos is no more input data or no more space in the output buffer (see below
634 1.4 christos about the flush parameter).
635 1.1 paulus
636 1.1 paulus Before the call of inflate(), the application should ensure that at least
637 1.1 paulus one of the actions is possible, by providing more input and/or consuming
638 1.1 paulus more output, and updating the next_* and avail_* values accordingly.
639 1.1 paulus The application can consume the uncompressed output when it wants, for
640 1.1 paulus example when the output buffer is full (avail_out == 0), or after each
641 1.4 christos call of inflate(). If inflate returns Z_OK and with zero avail_out, it
642 1.4 christos must be called again after making room in the output buffer because there
643 1.4 christos might be more output pending.
644 1.1 paulus
645 1.6 fvdl If the parameter flush is set to Z_SYNC_FLUSH or Z_PACKET_FLUSH,
646 1.1 paulus inflate flushes as much output as possible to the output buffer. The
647 1.1 paulus flushing behavior of inflate is not specified for values of the flush
648 1.6 fvdl parameter other than Z_SYNC_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the
649 1.1 paulus current implementation actually flushes as much output as possible
650 1.6 fvdl anyway. For Z_PACKET_FLUSH, inflate checks that once all the input data
651 1.1 paulus has been consumed, it is expecting to see the length field of a stored
652 1.1 paulus block; if not, it returns Z_DATA_ERROR.
653 1.1 paulus
654 1.1 paulus inflate() should normally be called until it returns Z_STREAM_END or an
655 1.1 paulus error. However if all decompression is to be performed in a single step
656 1.1 paulus (a single call of inflate), the parameter flush should be set to
657 1.1 paulus Z_FINISH. In this case all pending input is processed and all pending
658 1.1 paulus output is flushed; avail_out must be large enough to hold all the
659 1.1 paulus uncompressed data. (The size of the uncompressed data may have been saved
660 1.1 paulus by the compressor for this purpose.) The next operation on this stream must
661 1.1 paulus be inflateEnd to deallocate the decompression state. The use of Z_FINISH
662 1.1 paulus is never required, but can be used to inform inflate that a faster routine
663 1.1 paulus may be used for the single inflate() call.
664 1.1 paulus
665 1.6 fvdl If a preset dictionary is needed at this point (see inflateSetDictionary
666 1.6 fvdl below), inflate sets strm-adler to the adler32 checksum of the
667 1.8 perry dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
668 1.6 fvdl it sets strm->adler to the adler32 checksum of all output produced
669 1.6 fvdl so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
670 1.6 fvdl an error code as described below. At the end of the stream, inflate()
671 1.6 fvdl checks that its computed adler32 checksum is equal to that saved by the
672 1.6 fvdl compressor and returns Z_STREAM_END only if the checksum is correct.
673 1.6 fvdl
674 1.6 fvdl inflate() returns Z_OK if some progress has been made (more input processed
675 1.6 fvdl or more output produced), Z_STREAM_END if the end of the compressed data has
676 1.6 fvdl been reached and all uncompressed output has been produced, Z_NEED_DICT if a
677 1.6 fvdl preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
678 1.6 fvdl corrupted (input stream not conforming to the zlib format or incorrect
679 1.6 fvdl adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
680 1.6 fvdl (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
681 1.6 fvdl enough memory, Z_BUF_ERROR if no progress is possible or if there was not
682 1.6 fvdl enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
683 1.6 fvdl case, the application may then call inflateSync to look for a good
684 1.6 fvdl compression block.
685 1.4 christos */
686 1.1 paulus
687 1.1 paulus
688 1.13 dsl ZEXTERN int ZEXPORT inflateEnd(z_streamp);
689 1.1 paulus /*
690 1.1 paulus All dynamically allocated data structures for this stream are freed.
691 1.1 paulus This function discards any unprocessed input and does not flush any
692 1.1 paulus pending output.
693 1.1 paulus
694 1.1 paulus inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
695 1.1 paulus was inconsistent. In the error case, msg may be set but then points to a
696 1.1 paulus static string (which must not be deallocated).
697 1.1 paulus */
698 1.1 paulus
699 1.4 christos /* Advanced functions */
700 1.1 paulus
701 1.1 paulus /*
702 1.1 paulus The following functions are needed only in some special applications.
703 1.1 paulus */
704 1.1 paulus
705 1.8 perry /*
706 1.13 dsl ZEXTERN int ZEXPORT deflateInit2(z_streamp, int, int, int, int, int);
707 1.4 christos
708 1.1 paulus This is another version of deflateInit with more compression options. The
709 1.4 christos fields next_in, zalloc, zfree and opaque must be initialized before by
710 1.4 christos the caller.
711 1.1 paulus
712 1.4 christos The method parameter is the compression method. It must be Z_DEFLATED in
713 1.6 fvdl this version of the library.
714 1.1 paulus
715 1.1 paulus The windowBits parameter is the base two logarithm of the window size
716 1.1 paulus (the size of the history buffer). It should be in the range 8..15 for this
717 1.6 fvdl version of the library. Larger values of this parameter result in better
718 1.6 fvdl compression at the expense of memory usage. The default value is 15 if
719 1.6 fvdl deflateInit is used instead.
720 1.1 paulus
721 1.4 christos The memLevel parameter specifies how much memory should be allocated
722 1.1 paulus for the internal compression state. memLevel=1 uses minimum memory but
723 1.1 paulus is slow and reduces compression ratio; memLevel=9 uses maximum memory
724 1.1 paulus for optimal speed. The default value is 8. See zconf.h for total memory
725 1.1 paulus usage as a function of windowBits and memLevel.
726 1.1 paulus
727 1.4 christos The strategy parameter is used to tune the compression algorithm. Use the
728 1.4 christos value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
729 1.4 christos filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
730 1.4 christos string match). Filtered data consists mostly of small values with a
731 1.4 christos somewhat random distribution. In this case, the compression algorithm is
732 1.4 christos tuned to compress them better. The effect of Z_FILTERED is to force more
733 1.4 christos Huffman coding and less string matching; it is somewhat intermediate
734 1.4 christos between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
735 1.4 christos the compression ratio but not the correctness of the compressed output even
736 1.4 christos if it is not set appropriately.
737 1.1 paulus
738 1.6 fvdl deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
739 1.6 fvdl memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
740 1.6 fvdl method). msg is set to null if there is no error message. deflateInit2 does
741 1.6 fvdl not perform any compression: this will be done by deflate().
742 1.1 paulus */
743 1.8 perry
744 1.13 dsl ZEXTERN int ZEXPORT deflateSetDictionary(z_streamp, const Bytef *, uInt);
745 1.6 fvdl /*
746 1.6 fvdl Initializes the compression dictionary from the given byte sequence
747 1.6 fvdl without producing any compressed output. This function must be called
748 1.6 fvdl immediately after deflateInit, deflateInit2 or deflateReset, before any
749 1.6 fvdl call of deflate. The compressor and decompressor must use exactly the same
750 1.4 christos dictionary (see inflateSetDictionary).
751 1.6 fvdl
752 1.4 christos The dictionary should consist of strings (byte sequences) that are likely
753 1.4 christos to be encountered later in the data to be compressed, with the most commonly
754 1.4 christos used strings preferably put towards the end of the dictionary. Using a
755 1.6 fvdl dictionary is most useful when the data to be compressed is short and can be
756 1.6 fvdl predicted with good accuracy; the data can then be compressed better than
757 1.6 fvdl with the default empty dictionary.
758 1.6 fvdl
759 1.6 fvdl Depending on the size of the compression data structures selected by
760 1.6 fvdl deflateInit or deflateInit2, a part of the dictionary may in effect be
761 1.6 fvdl discarded, for example if the dictionary is larger than the window size in
762 1.6 fvdl deflate or deflate2. Thus the strings most likely to be useful should be
763 1.6 fvdl put at the end of the dictionary, not at the front.
764 1.6 fvdl
765 1.4 christos Upon return of this function, strm->adler is set to the Adler32 value
766 1.4 christos of the dictionary; the decompressor may later use this value to determine
767 1.4 christos which dictionary has been used by the compressor. (The Adler32 value
768 1.4 christos applies to the whole dictionary even if only a subset of the dictionary is
769 1.4 christos actually used by the compressor.)
770 1.4 christos
771 1.4 christos deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
772 1.6 fvdl parameter is invalid (such as NULL dictionary) or the stream state is
773 1.6 fvdl inconsistent (for example if deflate has already been called for this stream
774 1.6 fvdl or if the compression method is bsort). deflateSetDictionary does not
775 1.6 fvdl perform any compression: this will be done by deflate().
776 1.4 christos */
777 1.4 christos
778 1.13 dsl ZEXTERN int ZEXPORT deflateCopy(z_streamp, z_streamp);
779 1.6 fvdl /*
780 1.6 fvdl Sets the destination stream as a complete copy of the source stream.
781 1.1 paulus
782 1.4 christos This function can be useful when several compression strategies will be
783 1.1 paulus tried, for example when there are several ways of pre-processing the input
784 1.1 paulus data with a filter. The streams that will be discarded should then be freed
785 1.1 paulus by calling deflateEnd. Note that deflateCopy duplicates the internal
786 1.1 paulus compression state which can be quite large, so this strategy is slow and
787 1.1 paulus can consume lots of memory.
788 1.1 paulus
789 1.4 christos deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
790 1.1 paulus enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
791 1.1 paulus (such as zalloc being NULL). msg is left unchanged in both source and
792 1.1 paulus destination.
793 1.1 paulus */
794 1.1 paulus
795 1.13 dsl extern int inflateIncomp(z_stream *);
796 1.6 fvdl /*
797 1.6 fvdl This function adds the data at next_in (avail_in bytes) to the output
798 1.6 fvdl history without performing any output. There must be no pending output,
799 1.6 fvdl and the decompressor must be expecting to see the start of a block.
800 1.6 fvdl Calling this function is equivalent to decompressing a stored block
801 1.6 fvdl containing the data at next_in (except that the data is not output).
802 1.6 fvdl */
803 1.6 fvdl
804 1.13 dsl ZEXTERN int ZEXPORT deflateReset(z_streamp);
805 1.1 paulus /*
806 1.1 paulus This function is equivalent to deflateEnd followed by deflateInit,
807 1.1 paulus but does not free and reallocate all the internal compression state.
808 1.1 paulus The stream will keep the same compression level and any other attributes
809 1.1 paulus that may have been set by deflateInit2.
810 1.1 paulus
811 1.1 paulus deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
812 1.1 paulus stream state was inconsistent (such as zalloc or state being NULL).
813 1.1 paulus */
814 1.1 paulus
815 1.13 dsl ZEXTERN int ZEXPORT deflateParams(z_streamp, int, int);
816 1.6 fvdl /*
817 1.6 fvdl Dynamically update the compression level and compression strategy. The
818 1.6 fvdl interpretation of level and strategy is as in deflateInit2. This can be
819 1.6 fvdl used to switch between compression and straight copy of the input data, or
820 1.6 fvdl to switch to a different kind of input data requiring a different
821 1.6 fvdl strategy. If the compression level is changed, the input available so far
822 1.6 fvdl is compressed with the old level (and may be flushed); the new level will
823 1.6 fvdl take effect only at the next call of deflate().
824 1.4 christos
825 1.4 christos Before the call of deflateParams, the stream state must be set as for
826 1.4 christos a call of deflate(), since the currently available input may have to
827 1.4 christos be compressed and flushed. In particular, strm->avail_out must be non-zero.
828 1.4 christos
829 1.4 christos deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
830 1.4 christos stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
831 1.4 christos if strm->avail_out was zero.
832 1.4 christos */
833 1.4 christos
834 1.13 dsl ZEXTERN int ZEXPORT deflateOutputPending(z_streamp);
835 1.4 christos /*
836 1.4 christos Returns the number of bytes of output which are immediately
837 1.4 christos available from the compressor (i.e. without any further input
838 1.4 christos or flush).
839 1.4 christos */
840 1.4 christos
841 1.8 perry /*
842 1.13 dsl ZEXTERN int ZEXPORT inflateInit2(z_streamp, int);
843 1.4 christos
844 1.6 fvdl This is another version of inflateInit with an extra parameter. The
845 1.6 fvdl fields next_in, avail_in, zalloc, zfree and opaque must be initialized
846 1.6 fvdl before by the caller.
847 1.1 paulus
848 1.1 paulus The windowBits parameter is the base two logarithm of the maximum window
849 1.1 paulus size (the size of the history buffer). It should be in the range 8..15 for
850 1.6 fvdl this version of the library. The default value is 15 if inflateInit is used
851 1.6 fvdl instead. If a compressed stream with a larger window size is given as
852 1.6 fvdl input, inflate() will return with the error code Z_DATA_ERROR instead of
853 1.6 fvdl trying to allocate a larger window.
854 1.6 fvdl
855 1.6 fvdl inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
856 1.6 fvdl memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
857 1.6 fvdl memLevel). msg is set to null if there is no error message. inflateInit2
858 1.6 fvdl does not perform any decompression apart from reading the zlib header if
859 1.6 fvdl present: this will be done by inflate(). (So next_in and avail_in may be
860 1.6 fvdl modified, but next_out and avail_out are unchanged.)
861 1.6 fvdl */
862 1.6 fvdl
863 1.13 dsl ZEXTERN int ZEXPORT inflateSetDictionary(z_streamp, const Bytef *, uInt);
864 1.6 fvdl /*
865 1.6 fvdl Initializes the decompression dictionary from the given uncompressed byte
866 1.6 fvdl sequence. This function must be called immediately after a call of inflate
867 1.6 fvdl if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
868 1.6 fvdl can be determined from the Adler32 value returned by this call of
869 1.6 fvdl inflate. The compressor and decompressor must use exactly the same
870 1.4 christos dictionary (see deflateSetDictionary).
871 1.4 christos
872 1.4 christos inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
873 1.4 christos parameter is invalid (such as NULL dictionary) or the stream state is
874 1.4 christos inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
875 1.4 christos expected one (incorrect Adler32 value). inflateSetDictionary does not
876 1.4 christos perform any decompression: this will be done by subsequent calls of
877 1.4 christos inflate().
878 1.4 christos */
879 1.4 christos
880 1.13 dsl ZEXTERN int ZEXPORT inflateSync(z_streamp);
881 1.8 perry /*
882 1.6 fvdl Skips invalid compressed data until a full flush point (see above the
883 1.6 fvdl description of deflate with Z_FULL_FLUSH) can be found, or until all
884 1.6 fvdl available input is skipped. No output is provided.
885 1.1 paulus
886 1.6 fvdl inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
887 1.6 fvdl if no more input was provided, Z_DATA_ERROR if no flush point has been found,
888 1.1 paulus or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
889 1.1 paulus case, the application may save the current current value of total_in which
890 1.1 paulus indicates where valid compressed data was found. In the error case, the
891 1.1 paulus application may repeatedly call inflateSync, providing more input each time,
892 1.1 paulus until success or end of the input data.
893 1.1 paulus */
894 1.1 paulus
895 1.13 dsl ZEXTERN int ZEXPORT inflateReset(z_streamp);
896 1.1 paulus /*
897 1.1 paulus This function is equivalent to inflateEnd followed by inflateInit,
898 1.1 paulus but does not free and reallocate all the internal decompression state.
899 1.1 paulus The stream will keep attributes that may have been set by inflateInit2.
900 1.1 paulus
901 1.1 paulus inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
902 1.1 paulus stream state was inconsistent (such as zalloc or state being NULL).
903 1.1 paulus */
904 1.1 paulus
905 1.1 paulus
906 1.4 christos /* utility functions */
907 1.4 christos
908 1.4 christos /*
909 1.4 christos The following utility functions are implemented on top of the
910 1.4 christos basic stream-oriented functions. To simplify the interface, some
911 1.6 fvdl default options are assumed (compression level and memory usage,
912 1.4 christos standard memory allocation functions). The source code of these
913 1.4 christos utility functions can easily be modified if you need special options.
914 1.4 christos */
915 1.4 christos
916 1.13 dsl ZEXTERN int ZEXPORT compress(Bytef *, uLongf *, const Bytef *, uLong);
917 1.4 christos /*
918 1.4 christos Compresses the source buffer into the destination buffer. sourceLen is
919 1.4 christos the byte length of the source buffer. Upon entry, destLen is the total
920 1.4 christos size of the destination buffer, which must be at least 0.1% larger than
921 1.4 christos sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
922 1.4 christos compressed buffer.
923 1.4 christos This function can be used to compress a whole file at once if the
924 1.4 christos input file is mmap'ed.
925 1.4 christos compress returns Z_OK if success, Z_MEM_ERROR if there was not
926 1.4 christos enough memory, Z_BUF_ERROR if there was not enough room in the output
927 1.4 christos buffer.
928 1.4 christos */
929 1.4 christos
930 1.13 dsl ZEXTERN int ZEXPORT compress2(Bytef *, uLongf *, const Bytef *,
931 1.13 dsl uLong, int);
932 1.6 fvdl /*
933 1.6 fvdl Compresses the source buffer into the destination buffer. The level
934 1.6 fvdl parameter has the same meaning as in deflateInit. sourceLen is the byte
935 1.6 fvdl length of the source buffer. Upon entry, destLen is the total size of the
936 1.6 fvdl destination buffer, which must be at least 0.1% larger than sourceLen plus
937 1.6 fvdl 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
938 1.6 fvdl
939 1.6 fvdl compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
940 1.6 fvdl memory, Z_BUF_ERROR if there was not enough room in the output buffer,
941 1.6 fvdl Z_STREAM_ERROR if the level parameter is invalid.
942 1.6 fvdl */
943 1.6 fvdl
944 1.13 dsl ZEXTERN int ZEXPORT uncompress(Bytef *, uLongf *, const Bytef *, uLong);
945 1.4 christos /*
946 1.4 christos Decompresses the source buffer into the destination buffer. sourceLen is
947 1.4 christos the byte length of the source buffer. Upon entry, destLen is the total
948 1.4 christos size of the destination buffer, which must be large enough to hold the
949 1.4 christos entire uncompressed data. (The size of the uncompressed data must have
950 1.4 christos been saved previously by the compressor and transmitted to the decompressor
951 1.4 christos by some mechanism outside the scope of this compression library.)
952 1.4 christos Upon exit, destLen is the actual size of the compressed buffer.
953 1.4 christos This function can be used to decompress a whole file at once if the
954 1.4 christos input file is mmap'ed.
955 1.4 christos
956 1.4 christos uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
957 1.4 christos enough memory, Z_BUF_ERROR if there was not enough room in the output
958 1.4 christos buffer, or Z_DATA_ERROR if the input data was corrupted.
959 1.4 christos */
960 1.4 christos
961 1.4 christos
962 1.4 christos typedef voidp gzFile;
963 1.4 christos
964 1.13 dsl ZEXTERN gzFile ZEXPORT gzopen(const char *, const char *);
965 1.4 christos /*
966 1.4 christos Opens a gzip (.gz) file for reading or writing. The mode parameter
967 1.4 christos is as in fopen ("rb" or "wb") but can also include a compression level
968 1.6 fvdl ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
969 1.6 fvdl Huffman only compression as in "wb1h". (See the description
970 1.6 fvdl of deflateInit2 for more information about the strategy parameter.)
971 1.6 fvdl
972 1.6 fvdl gzopen can be used to read a file which is not in gzip format; in this
973 1.6 fvdl case gzread will directly read from the file without decompression.
974 1.6 fvdl
975 1.4 christos gzopen returns NULL if the file could not be opened or if there was
976 1.4 christos insufficient memory to allocate the (de)compression state; errno
977 1.4 christos can be checked to distinguish the two cases (if errno is zero, the
978 1.6 fvdl zlib error is Z_MEM_ERROR). */
979 1.4 christos
980 1.13 dsl ZEXTERN gzFile ZEXPORT gzdopen(int, const char *);
981 1.4 christos /*
982 1.4 christos gzdopen() associates a gzFile with the file descriptor fd. File
983 1.4 christos descriptors are obtained from calls like open, dup, creat, pipe or
984 1.4 christos fileno (in the file has been previously opened with fopen).
985 1.4 christos The mode parameter is as in gzopen.
986 1.4 christos The next call of gzclose on the returned gzFile will also close the
987 1.4 christos file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
988 1.4 christos descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
989 1.4 christos gzdopen returns NULL if there was insufficient memory to allocate
990 1.4 christos the (de)compression state.
991 1.4 christos */
992 1.4 christos
993 1.13 dsl ZEXTERN int ZEXPORT gzsetparams(gzFile, int, int);
994 1.6 fvdl /*
995 1.6 fvdl Dynamically update the compression level or strategy. See the description
996 1.6 fvdl of deflateInit2 for the meaning of these parameters.
997 1.6 fvdl gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
998 1.6 fvdl opened for writing.
999 1.6 fvdl */
1000 1.6 fvdl
1001 1.13 dsl ZEXTERN int ZEXPORT gzread(gzFile, voidp, unsigned);
1002 1.4 christos /*
1003 1.4 christos Reads the given number of uncompressed bytes from the compressed file.
1004 1.4 christos If the input file was not in gzip format, gzread copies the given number
1005 1.4 christos of bytes into the buffer.
1006 1.4 christos gzread returns the number of uncompressed bytes actually read (0 for
1007 1.4 christos end of file, -1 for error). */
1008 1.4 christos
1009 1.13 dsl ZEXTERN int ZEXPORT gzwrite(gzFile, const voidp, unsigned);
1010 1.4 christos /*
1011 1.4 christos Writes the given number of uncompressed bytes into the compressed file.
1012 1.4 christos gzwrite returns the number of uncompressed bytes actually written
1013 1.4 christos (0 in case of error).
1014 1.4 christos */
1015 1.4 christos
1016 1.13 dsl ZEXTERN int ZEXPORTVA gzprintf(gzFile, const char *, ...)
1017 1.6 fvdl __attribute__((__format__(__printf__, 2, 3)));
1018 1.6 fvdl /*
1019 1.6 fvdl Converts, formats, and writes the args to the compressed file under
1020 1.6 fvdl control of the format string, as in fprintf. gzprintf returns the number of
1021 1.6 fvdl uncompressed bytes actually written (0 in case of error).
1022 1.6 fvdl */
1023 1.6 fvdl
1024 1.13 dsl ZEXTERN int ZEXPORT gzputs(gzFile, const char *);
1025 1.6 fvdl /*
1026 1.6 fvdl Writes the given null-terminated string to the compressed file, excluding
1027 1.6 fvdl the terminating null character.
1028 1.6 fvdl gzputs returns the number of characters written, or -1 in case of error.
1029 1.6 fvdl */
1030 1.6 fvdl
1031 1.13 dsl ZEXTERN char * ZEXPORT gzgets(gzFile, char *, int);
1032 1.6 fvdl /*
1033 1.6 fvdl Reads bytes from the compressed file until len-1 characters are read, or
1034 1.6 fvdl a newline character is read and transferred to buf, or an end-of-file
1035 1.6 fvdl condition is encountered. The string is then terminated with a null
1036 1.6 fvdl character.
1037 1.6 fvdl gzgets returns buf, or Z_NULL in case of error.
1038 1.6 fvdl */
1039 1.6 fvdl
1040 1.13 dsl ZEXTERN int ZEXPORT gzputc(gzFile, int);
1041 1.6 fvdl /*
1042 1.6 fvdl Writes c, converted to an unsigned char, into the compressed file.
1043 1.6 fvdl gzputc returns the value that was written, or -1 in case of error.
1044 1.6 fvdl */
1045 1.6 fvdl
1046 1.13 dsl ZEXTERN int ZEXPORT gzgetc(gzFile);
1047 1.6 fvdl /*
1048 1.6 fvdl Reads one byte from the compressed file. gzgetc returns this byte
1049 1.6 fvdl or -1 in case of end of file or error.
1050 1.6 fvdl */
1051 1.6 fvdl
1052 1.13 dsl ZEXTERN int ZEXPORT gzflush(gzFile, int);
1053 1.4 christos /*
1054 1.4 christos Flushes all pending output into the compressed file. The parameter
1055 1.4 christos flush is as in the deflate() function. The return value is the zlib
1056 1.4 christos error number (see function gzerror below). gzflush returns Z_OK if
1057 1.4 christos the flush parameter is Z_FINISH and all output could be flushed.
1058 1.4 christos gzflush should be called only when strictly necessary because it can
1059 1.4 christos degrade compression.
1060 1.6 fvdl
1061 1.4 christos */
1062 1.4 christos
1063 1.8 perry /*
1064 1.6 fvdl * NetBSD note:
1065 1.6 fvdl * "long" gzseek has been there till Oct 1999 (1.4L), which was wrong.
1066 1.6 fvdl */
1067 1.13 dsl ZEXTERN z_off_t ZEXPORT gzseek(gzFile, z_off_t, int);
1068 1.8 perry /*
1069 1.6 fvdl Sets the starting position for the next gzread or gzwrite on the
1070 1.6 fvdl given compressed file. The offset represents a number of bytes in the
1071 1.6 fvdl uncompressed data stream. The whence parameter is defined as in lseek(2);
1072 1.6 fvdl the value SEEK_END is not supported.
1073 1.6 fvdl If the file is opened for reading, this function is emulated but can be
1074 1.6 fvdl extremely slow. If the file is opened for writing, only forward seeks are
1075 1.6 fvdl supported; gzseek then compresses a sequence of zeroes up to the new
1076 1.6 fvdl starting position.
1077 1.6 fvdl
1078 1.6 fvdl gzseek returns the resulting offset location as measured in bytes from
1079 1.6 fvdl the beginning of the uncompressed stream, or -1 in case of error, in
1080 1.6 fvdl particular if the file is opened for writing and the new starting position
1081 1.6 fvdl would be before the current position.
1082 1.6 fvdl */
1083 1.6 fvdl
1084 1.13 dsl ZEXTERN int ZEXPORT gzrewind(gzFile);
1085 1.6 fvdl /*
1086 1.6 fvdl Rewinds the given file. This function is supported only for reading.
1087 1.6 fvdl
1088 1.6 fvdl gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
1089 1.6 fvdl */
1090 1.6 fvdl
1091 1.8 perry /*
1092 1.6 fvdl * NetBSD note:
1093 1.6 fvdl * "long" gztell has been there till Oct 1999 (1.4L), which was wrong.
1094 1.6 fvdl */
1095 1.13 dsl ZEXTERN z_off_t ZEXPORT gztell(gzFile);
1096 1.6 fvdl /*
1097 1.6 fvdl Returns the starting position for the next gzread or gzwrite on the
1098 1.6 fvdl given compressed file. This position represents a number of bytes in the
1099 1.6 fvdl uncompressed data stream.
1100 1.6 fvdl
1101 1.6 fvdl gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
1102 1.6 fvdl */
1103 1.6 fvdl
1104 1.13 dsl ZEXTERN int ZEXPORT gzeof(gzFile);
1105 1.6 fvdl /*
1106 1.6 fvdl Returns 1 when EOF has previously been detected reading the given
1107 1.6 fvdl input stream, otherwise zero.
1108 1.6 fvdl */
1109 1.6 fvdl
1110 1.13 dsl ZEXTERN int ZEXPORT gzclose(gzFile);
1111 1.4 christos /*
1112 1.4 christos Flushes all pending output if necessary, closes the compressed file
1113 1.4 christos and deallocates all the (de)compression state. The return value is the zlib
1114 1.4 christos error number (see function gzerror below).
1115 1.4 christos */
1116 1.4 christos
1117 1.13 dsl ZEXTERN const char * ZEXPORT gzerror(gzFile, int *);
1118 1.4 christos /*
1119 1.4 christos Returns the error message for the last error which occurred on the
1120 1.4 christos given compressed file. errnum is set to zlib error number. If an
1121 1.4 christos error occurred in the file system and not in the compression library,
1122 1.4 christos errnum is set to Z_ERRNO and the application may consult errno
1123 1.4 christos to get the exact error code.
1124 1.4 christos */
1125 1.4 christos
1126 1.1 paulus /* checksum functions */
1127 1.1 paulus
1128 1.1 paulus /*
1129 1.4 christos These functions are not related to compression but are exported
1130 1.4 christos anyway because they might be useful in applications using the
1131 1.1 paulus compression library.
1132 1.1 paulus */
1133 1.1 paulus
1134 1.13 dsl ZEXTERN uLong ZEXPORT adler32(uLong, const Bytef *, uInt);
1135 1.1 paulus
1136 1.1 paulus /*
1137 1.1 paulus Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1138 1.1 paulus return the updated checksum. If buf is NULL, this function returns
1139 1.1 paulus the required initial value for the checksum.
1140 1.1 paulus An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
1141 1.1 paulus much faster. Usage example:
1142 1.1 paulus
1143 1.1 paulus uLong adler = adler32(0L, Z_NULL, 0);
1144 1.1 paulus
1145 1.1 paulus while (read_buffer(buffer, length) != EOF) {
1146 1.1 paulus adler = adler32(adler, buffer, length);
1147 1.1 paulus }
1148 1.1 paulus if (adler != original_adler) error();
1149 1.1 paulus */
1150 1.1 paulus
1151 1.14 darran #ifdef STANDALONE
1152 1.13 dsl ZEXTERN uLong ZEXPORT crc32(uLong, const Bytef *, uInt);
1153 1.14 darran #endif
1154 1.4 christos /*
1155 1.4 christos Update a running crc with the bytes buf[0..len-1] and return the updated
1156 1.4 christos crc. If buf is NULL, this function returns the required initial value
1157 1.4 christos for the crc. Pre- and post-conditioning (one's complement) is performed
1158 1.4 christos within this function so it shouldn't be done by the application.
1159 1.4 christos Usage example:
1160 1.4 christos
1161 1.4 christos uLong crc = crc32(0L, Z_NULL, 0);
1162 1.4 christos
1163 1.4 christos while (read_buffer(buffer, length) != EOF) {
1164 1.4 christos crc = crc32(crc, buffer, length);
1165 1.4 christos }
1166 1.4 christos if (crc != original_crc) error();
1167 1.4 christos */
1168 1.4 christos
1169 1.4 christos
1170 1.4 christos /* various hacks, don't look :) */
1171 1.4 christos
1172 1.4 christos /* deflateInit and inflateInit are macros to allow checking the zlib version
1173 1.4 christos * and the compiler's view of z_stream:
1174 1.4 christos */
1175 1.13 dsl ZEXTERN int ZEXPORT deflateInit_(z_streamp, int, const char *, int);
1176 1.13 dsl ZEXTERN int ZEXPORT inflateInit_(z_streamp, const char *, int);
1177 1.13 dsl ZEXTERN int ZEXPORT deflateInit2_(z_streamp, int, int, int, int,
1178 1.13 dsl int, const char *, int);
1179 1.13 dsl ZEXTERN int ZEXPORT inflateInit2_(z_streamp, int, const char *, int);
1180 1.4 christos #define deflateInit(strm, level) \
1181 1.4 christos deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
1182 1.4 christos #define inflateInit(strm) \
1183 1.4 christos inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
1184 1.4 christos #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1185 1.4 christos deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1186 1.6 fvdl (strategy), ZLIB_VERSION, sizeof(z_stream))
1187 1.4 christos #define inflateInit2(strm, windowBits) \
1188 1.4 christos inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
1189 1.4 christos
1190 1.6 fvdl
1191 1.12 christos #if !defined(Z_UTIL_H) && !defined(NO_DUMMY_DECL)
1192 1.1 paulus struct internal_state {int dummy;}; /* hack for buggy compilers */
1193 1.1 paulus #endif
1194 1.1 paulus
1195 1.13 dsl ZEXTERN const char * ZEXPORT zError(int);
1196 1.13 dsl ZEXTERN int ZEXPORT inflateSyncPoint(z_streamp);
1197 1.13 dsl ZEXTERN const uLongf * ZEXPORT get_crc_table(void);
1198 1.4 christos
1199 1.4 christos #ifdef __cplusplus
1200 1.4 christos }
1201 1.4 christos #endif
1202 1.12 christos #endif /* !ZLIB_H */
1203 1.4 christos
1204 1.10 elad #endif /* !_NET_ZLIB_H_ */
1205 1.6 fvdl /* -- zlib.h */
1206