Commit | Line | Data |
---|---|---|
d4286ec4 | 1 | /* zlib.h -- interface of the 'zlib' general purpose compression library |
06b91d8e | 2 | version 1.1.3, July 9th, 1998 |
d4286ec4 PM |
3 | |
4 | Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler | |
5 | ||
6 | This software is provided 'as-is', without any express or implied | |
7 | warranty. In no event will the authors be held liable for any damages | |
8 | arising from the use of this software. | |
9 | ||
10 | Permission is granted to anyone to use this software for any purpose, | |
11 | including commercial applications, and to alter it and redistribute it | |
12 | freely, subject to the following restrictions: | |
13 | ||
14 | 1. The origin of this software must not be misrepresented; you must not | |
15 | claim that you wrote the original software. If you use this software | |
16 | in a product, an acknowledgment in the product documentation would be | |
17 | appreciated but is not required. | |
18 | 2. Altered source versions must be plainly marked as such, and must not be | |
19 | misrepresented as being the original software. | |
20 | 3. This notice may not be removed or altered from any source distribution. | |
21 | ||
22 | Jean-loup Gailly Mark Adler | |
23 | jloup@gzip.org madler@alumni.caltech.edu | |
24 | ||
25 | ||
26 | The data format used by the zlib library is described by RFCs (Request for | |
27 | Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt | |
28 | (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). | |
29 | */ | |
30 | ||
31 | #ifndef _ZLIB_H | |
32 | #define _ZLIB_H | |
33 | ||
34 | #include "zconf.h" | |
35 | ||
36 | #ifdef __cplusplus | |
37 | extern "C" { | |
38 | #endif | |
39 | ||
06b91d8e | 40 | #define ZLIB_VERSION "1.1.3" |
d4286ec4 PM |
41 | |
42 | /* | |
43 | The 'zlib' compression library provides in-memory compression and | |
44 | decompression functions, including integrity checks of the uncompressed | |
45 | data. This version of the library supports only one compression method | |
46 | (deflation) but other algorithms will be added later and will have the same | |
47 | stream interface. | |
48 | ||
49 | Compression can be done in a single step if the buffers are large | |
50 | enough (for example if an input file is mmap'ed), or can be done by | |
51 | repeated calls of the compression function. In the latter case, the | |
52 | application must provide more input and/or consume the output | |
53 | (providing more output space) before each call. | |
54 | ||
55 | The library also supports reading and writing files in gzip (.gz) format | |
56 | with an interface similar to that of stdio. | |
57 | ||
58 | The library does not install any signal handler. The decoder checks | |
59 | the consistency of the compressed data, so the library should never | |
60 | crash even in case of corrupted input. | |
61 | */ | |
62 | ||
63 | typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); | |
64 | typedef void (*free_func) OF((voidpf opaque, voidpf address)); | |
65 | ||
66 | struct internal_state; | |
67 | ||
68 | typedef struct z_stream_s { | |
69 | Bytef *next_in; /* next input byte */ | |
70 | uInt avail_in; /* number of bytes available at next_in */ | |
71 | uLong total_in; /* total nb of input bytes read so far */ | |
72 | ||
73 | Bytef *next_out; /* next output byte should be put there */ | |
74 | uInt avail_out; /* remaining free space at next_out */ | |
75 | uLong total_out; /* total nb of bytes output so far */ | |
76 | ||
77 | char *msg; /* last error message, NULL if no error */ | |
78 | struct internal_state FAR *state; /* not visible by applications */ | |
79 | ||
80 | alloc_func zalloc; /* used to allocate the internal state */ | |
81 | free_func zfree; /* used to free the internal state */ | |
82 | voidpf opaque; /* private data object passed to zalloc and zfree */ | |
83 | ||
84 | int data_type; /* best guess about the data type: ascii or binary */ | |
85 | uLong adler; /* adler32 value of the uncompressed data */ | |
86 | uLong reserved; /* reserved for future use */ | |
87 | } z_stream; | |
88 | ||
89 | typedef z_stream FAR *z_streamp; | |
90 | ||
91 | /* | |
92 | The application must update next_in and avail_in when avail_in has | |
93 | dropped to zero. It must update next_out and avail_out when avail_out | |
94 | has dropped to zero. The application must initialize zalloc, zfree and | |
95 | opaque before calling the init function. All other fields are set by the | |
96 | compression library and must not be updated by the application. | |
97 | ||
98 | The opaque value provided by the application will be passed as the first | |
99 | parameter for calls of zalloc and zfree. This can be useful for custom | |
100 | memory management. The compression library attaches no meaning to the | |
101 | opaque value. | |
102 | ||
103 | zalloc must return Z_NULL if there is not enough memory for the object. | |
104 | If zlib is used in a multi-threaded application, zalloc and zfree must be | |
105 | thread safe. | |
106 | ||
107 | On 16-bit systems, the functions zalloc and zfree must be able to allocate | |
108 | exactly 65536 bytes, but will not be required to allocate more than this | |
109 | if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, | |
110 | pointers returned by zalloc for objects of exactly 65536 bytes *must* | |
111 | have their offset normalized to zero. The default allocation function | |
112 | provided by this library ensures this (see zutil.c). To reduce memory | |
113 | requirements and avoid any allocation of 64K objects, at the expense of | |
114 | compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). | |
115 | ||
116 | The fields total_in and total_out can be used for statistics or | |
117 | progress reports. After compression, total_in holds the total size of | |
118 | the uncompressed data and may be saved for use in the decompressor | |
119 | (particularly if the decompressor wants to decompress everything in | |
120 | a single step). | |
121 | */ | |
122 | ||
123 | /* constants */ | |
124 | ||
125 | #define Z_NO_FLUSH 0 | |
126 | #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ | |
127 | #define Z_SYNC_FLUSH 2 | |
128 | #define Z_FULL_FLUSH 3 | |
129 | #define Z_FINISH 4 | |
5914bf15 | 130 | #define Z_INSERT_ONLY 5 |
d4286ec4 PM |
131 | /* Allowed flush values; see deflate() below for details */ |
132 | ||
133 | #define Z_OK 0 | |
134 | #define Z_STREAM_END 1 | |
135 | #define Z_NEED_DICT 2 | |
136 | #define Z_ERRNO (-1) | |
137 | #define Z_STREAM_ERROR (-2) | |
138 | #define Z_DATA_ERROR (-3) | |
139 | #define Z_MEM_ERROR (-4) | |
140 | #define Z_BUF_ERROR (-5) | |
141 | #define Z_VERSION_ERROR (-6) | |
142 | /* Return codes for the compression/decompression functions. Negative | |
143 | * values are errors, positive values are used for special but normal events. | |
144 | */ | |
145 | ||
146 | #define Z_NO_COMPRESSION 0 | |
147 | #define Z_BEST_SPEED 1 | |
148 | #define Z_BEST_COMPRESSION 9 | |
149 | #define Z_DEFAULT_COMPRESSION (-1) | |
150 | /* compression levels */ | |
151 | ||
152 | #define Z_FILTERED 1 | |
153 | #define Z_HUFFMAN_ONLY 2 | |
154 | #define Z_DEFAULT_STRATEGY 0 | |
155 | /* compression strategy; see deflateInit2() below for details */ | |
156 | ||
157 | #define Z_BINARY 0 | |
158 | #define Z_ASCII 1 | |
159 | #define Z_UNKNOWN 2 | |
160 | /* Possible values of the data_type field */ | |
161 | ||
162 | #define Z_DEFLATED 8 | |
163 | /* The deflate compression method (the only one supported in this version) */ | |
164 | ||
165 | #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ | |
166 | ||
167 | #define zlib_version zlibVersion() | |
168 | /* for compatibility with versions < 1.0.2 */ | |
169 | ||
170 | /* basic functions */ | |
171 | ||
06b91d8e | 172 | ZEXTERN const char * ZEXPORT zlibVersion OF((void)); |
d4286ec4 PM |
173 | /* The application can compare zlibVersion and ZLIB_VERSION for consistency. |
174 | If the first character differs, the library code actually used is | |
175 | not compatible with the zlib.h header file used by the application. | |
176 | This check is automatically made by deflateInit and inflateInit. | |
177 | */ | |
178 | ||
179 | /* | |
06b91d8e | 180 | ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); |
d4286ec4 PM |
181 | |
182 | Initializes the internal stream state for compression. The fields | |
183 | zalloc, zfree and opaque must be initialized before by the caller. | |
184 | If zalloc and zfree are set to Z_NULL, deflateInit updates them to | |
185 | use default allocation functions. | |
186 | ||
187 | The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: | |
188 | 1 gives best speed, 9 gives best compression, 0 gives no compression at | |
189 | all (the input data is simply copied a block at a time). | |
190 | Z_DEFAULT_COMPRESSION requests a default compromise between speed and | |
191 | compression (currently equivalent to level 6). | |
192 | ||
193 | deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not | |
194 | enough memory, Z_STREAM_ERROR if level is not a valid compression level, | |
195 | Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible | |
196 | with the version assumed by the caller (ZLIB_VERSION). | |
197 | msg is set to null if there is no error message. deflateInit does not | |
198 | perform any compression: this will be done by deflate(). | |
199 | */ | |
200 | ||
201 | ||
06b91d8e | 202 | ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); |
d4286ec4 PM |
203 | /* |
204 | deflate compresses as much data as possible, and stops when the input | |
205 | buffer becomes empty or the output buffer becomes full. It may introduce some | |
206 | output latency (reading input without producing any output) except when | |
207 | forced to flush. | |
208 | ||
209 | The detailed semantics are as follows. deflate performs one or both of the | |
210 | following actions: | |
211 | ||
212 | - Compress more input starting at next_in and update next_in and avail_in | |
213 | accordingly. If not all input can be processed (because there is not | |
214 | enough room in the output buffer), next_in and avail_in are updated and | |
215 | processing will resume at this point for the next call of deflate(). | |
216 | ||
217 | - Provide more output starting at next_out and update next_out and avail_out | |
218 | accordingly. This action is forced if the parameter flush is non zero. | |
219 | Forcing flush frequently degrades the compression ratio, so this parameter | |
220 | should be set only when necessary (in interactive applications). | |
221 | Some output may be provided even if flush is not set. | |
222 | ||
223 | Before the call of deflate(), the application should ensure that at least | |
224 | one of the actions is possible, by providing more input and/or consuming | |
225 | more output, and updating avail_in or avail_out accordingly; avail_out | |
226 | should never be zero before the call. The application can consume the | |
227 | compressed output when it wants, for example when the output buffer is full | |
228 | (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK | |
229 | and with zero avail_out, it must be called again after making room in the | |
230 | output buffer because there might be more output pending. | |
231 | ||
232 | If the parameter flush is set to Z_SYNC_FLUSH, all pending output is | |
233 | flushed to the output buffer and the output is aligned on a byte boundary, so | |
234 | that the decompressor can get all input data available so far. (In particular | |
235 | avail_in is zero after the call if enough output space has been provided | |
236 | before the call.) Flushing may degrade compression for some compression | |
237 | algorithms and so it should be used only when necessary. | |
238 | ||
239 | If flush is set to Z_FULL_FLUSH, all output is flushed as with | |
240 | Z_SYNC_FLUSH, and the compression state is reset so that decompression can | |
241 | restart from this point if previous compressed data has been damaged or if | |
242 | random access is desired. Using Z_FULL_FLUSH too often can seriously degrade | |
243 | the compression. | |
244 | ||
245 | If deflate returns with avail_out == 0, this function must be called again | |
246 | with the same value of the flush parameter and more output space (updated | |
247 | avail_out), until the flush is complete (deflate returns with non-zero | |
248 | avail_out). | |
249 | ||
250 | If the parameter flush is set to Z_FINISH, pending input is processed, | |
251 | pending output is flushed and deflate returns with Z_STREAM_END if there | |
252 | was enough output space; if deflate returns with Z_OK, this function must be | |
253 | called again with Z_FINISH and more output space (updated avail_out) but no | |
254 | more input data, until it returns with Z_STREAM_END or an error. After | |
255 | deflate has returned Z_STREAM_END, the only possible operations on the | |
256 | stream are deflateReset or deflateEnd. | |
257 | ||
258 | Z_FINISH can be used immediately after deflateInit if all the compression | |
259 | is to be done in a single step. In this case, avail_out must be at least | |
260 | 0.1% larger than avail_in plus 12 bytes. If deflate does not return | |
261 | Z_STREAM_END, then it must be called again as described above. | |
262 | ||
263 | deflate() sets strm->adler to the adler32 checksum of all input read | |
264 | so far (that is, total_in bytes). | |
265 | ||
266 | deflate() may update data_type if it can make a good guess about | |
267 | the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered | |
268 | binary. This field is only for information purposes and does not affect | |
269 | the compression algorithm in any manner. | |
270 | ||
271 | deflate() returns Z_OK if some progress has been made (more input | |
272 | processed or more output produced), Z_STREAM_END if all input has been | |
273 | consumed and all output has been produced (only when flush is set to | |
274 | Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example | |
06b91d8e MP |
275 | if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible |
276 | (for example avail_in or avail_out was zero). | |
d4286ec4 PM |
277 | */ |
278 | ||
279 | ||
06b91d8e | 280 | ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); |
d4286ec4 PM |
281 | /* |
282 | All dynamically allocated data structures for this stream are freed. | |
283 | This function discards any unprocessed input and does not flush any | |
284 | pending output. | |
285 | ||
286 | deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the | |
287 | stream state was inconsistent, Z_DATA_ERROR if the stream was freed | |
288 | prematurely (some input or output was discarded). In the error case, | |
289 | msg may be set but then points to a static string (which must not be | |
290 | deallocated). | |
291 | */ | |
292 | ||
293 | ||
294 | /* | |
06b91d8e | 295 | ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); |
d4286ec4 PM |
296 | |
297 | Initializes the internal stream state for decompression. The fields | |
298 | next_in, avail_in, zalloc, zfree and opaque must be initialized before by | |
299 | the caller. If next_in is not Z_NULL and avail_in is large enough (the exact | |
300 | value depends on the compression method), inflateInit determines the | |
301 | compression method from the zlib header and allocates all data structures | |
302 | accordingly; otherwise the allocation will be deferred to the first call of | |
303 | inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to | |
304 | use default allocation functions. | |
305 | ||
306 | inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
307 | memory, Z_VERSION_ERROR if the zlib library version is incompatible with the | |
308 | version assumed by the caller. msg is set to null if there is no error | |
309 | message. inflateInit does not perform any decompression apart from reading | |
310 | the zlib header if present: this will be done by inflate(). (So next_in and | |
311 | avail_in may be modified, but next_out and avail_out are unchanged.) | |
312 | */ | |
313 | ||
314 | ||
06b91d8e | 315 | ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); |
d4286ec4 PM |
316 | /* |
317 | inflate decompresses as much data as possible, and stops when the input | |
318 | buffer becomes empty or the output buffer becomes full. It may some | |
319 | introduce some output latency (reading input without producing any output) | |
320 | except when forced to flush. | |
321 | ||
322 | The detailed semantics are as follows. inflate performs one or both of the | |
323 | following actions: | |
324 | ||
325 | - Decompress more input starting at next_in and update next_in and avail_in | |
326 | accordingly. If not all input can be processed (because there is not | |
327 | enough room in the output buffer), next_in is updated and processing | |
328 | will resume at this point for the next call of inflate(). | |
329 | ||
330 | - Provide more output starting at next_out and update next_out and avail_out | |
331 | accordingly. inflate() provides as much output as possible, until there | |
332 | is no more input data or no more space in the output buffer (see below | |
333 | about the flush parameter). | |
334 | ||
335 | Before the call of inflate(), the application should ensure that at least | |
336 | one of the actions is possible, by providing more input and/or consuming | |
337 | more output, and updating the next_* and avail_* values accordingly. | |
338 | The application can consume the uncompressed output when it wants, for | |
339 | example when the output buffer is full (avail_out == 0), or after each | |
340 | call of inflate(). If inflate returns Z_OK and with zero avail_out, it | |
341 | must be called again after making room in the output buffer because there | |
342 | might be more output pending. | |
343 | ||
344 | If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much | |
345 | output as possible to the output buffer. The flushing behavior of inflate is | |
346 | not specified for values of the flush parameter other than Z_SYNC_FLUSH | |
347 | and Z_FINISH, but the current implementation actually flushes as much output | |
348 | as possible anyway. | |
349 | ||
350 | inflate() should normally be called until it returns Z_STREAM_END or an | |
351 | error. However if all decompression is to be performed in a single step | |
352 | (a single call of inflate), the parameter flush should be set to | |
353 | Z_FINISH. In this case all pending input is processed and all pending | |
354 | output is flushed; avail_out must be large enough to hold all the | |
355 | uncompressed data. (The size of the uncompressed data may have been saved | |
356 | by the compressor for this purpose.) The next operation on this stream must | |
357 | be inflateEnd to deallocate the decompression state. The use of Z_FINISH | |
358 | is never required, but can be used to inform inflate that a faster routine | |
359 | may be used for the single inflate() call. | |
360 | ||
361 | If a preset dictionary is needed at this point (see inflateSetDictionary | |
362 | below), inflate sets strm-adler to the adler32 checksum of the | |
363 | dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise | |
364 | it sets strm->adler to the adler32 checksum of all output produced | |
365 | so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or | |
366 | an error code as described below. At the end of the stream, inflate() | |
367 | checks that its computed adler32 checksum is equal to that saved by the | |
368 | compressor and returns Z_STREAM_END only if the checksum is correct. | |
369 | ||
370 | inflate() returns Z_OK if some progress has been made (more input processed | |
371 | or more output produced), Z_STREAM_END if the end of the compressed data has | |
372 | been reached and all uncompressed output has been produced, Z_NEED_DICT if a | |
373 | preset dictionary is needed at this point, Z_DATA_ERROR if the input data was | |
374 | corrupted (input stream not conforming to the zlib format or incorrect | |
375 | adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent | |
376 | (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not | |
377 | enough memory, Z_BUF_ERROR if no progress is possible or if there was not | |
378 | enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR | |
379 | case, the application may then call inflateSync to look for a good | |
380 | compression block. | |
381 | */ | |
382 | ||
383 | ||
06b91d8e | 384 | ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); |
d4286ec4 PM |
385 | /* |
386 | All dynamically allocated data structures for this stream are freed. | |
387 | This function discards any unprocessed input and does not flush any | |
388 | pending output. | |
389 | ||
390 | inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state | |
391 | was inconsistent. In the error case, msg may be set but then points to a | |
392 | static string (which must not be deallocated). | |
393 | */ | |
394 | ||
395 | /* Advanced functions */ | |
396 | ||
397 | /* | |
398 | The following functions are needed only in some special applications. | |
399 | */ | |
400 | ||
401 | /* | |
06b91d8e MP |
402 | ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, |
403 | int level, | |
404 | int method, | |
405 | int windowBits, | |
406 | int memLevel, | |
407 | int strategy)); | |
d4286ec4 PM |
408 | |
409 | This is another version of deflateInit with more compression options. The | |
410 | fields next_in, zalloc, zfree and opaque must be initialized before by | |
411 | the caller. | |
412 | ||
413 | The method parameter is the compression method. It must be Z_DEFLATED in | |
414 | this version of the library. | |
415 | ||
416 | The windowBits parameter is the base two logarithm of the window size | |
417 | (the size of the history buffer). It should be in the range 8..15 for this | |
418 | version of the library. Larger values of this parameter result in better | |
419 | compression at the expense of memory usage. The default value is 15 if | |
420 | deflateInit is used instead. | |
421 | ||
422 | The memLevel parameter specifies how much memory should be allocated | |
423 | for the internal compression state. memLevel=1 uses minimum memory but | |
424 | is slow and reduces compression ratio; memLevel=9 uses maximum memory | |
425 | for optimal speed. The default value is 8. See zconf.h for total memory | |
426 | usage as a function of windowBits and memLevel. | |
427 | ||
428 | The strategy parameter is used to tune the compression algorithm. Use the | |
429 | value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a | |
430 | filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no | |
431 | string match). Filtered data consists mostly of small values with a | |
432 | somewhat random distribution. In this case, the compression algorithm is | |
433 | tuned to compress them better. The effect of Z_FILTERED is to force more | |
434 | Huffman coding and less string matching; it is somewhat intermediate | |
435 | between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects | |
436 | the compression ratio but not the correctness of the compressed output even | |
437 | if it is not set appropriately. | |
438 | ||
439 | deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
440 | memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid | |
441 | method). msg is set to null if there is no error message. deflateInit2 does | |
442 | not perform any compression: this will be done by deflate(). | |
443 | */ | |
444 | ||
06b91d8e MP |
445 | ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, |
446 | const Bytef *dictionary, | |
447 | uInt dictLength)); | |
d4286ec4 PM |
448 | /* |
449 | Initializes the compression dictionary from the given byte sequence | |
450 | without producing any compressed output. This function must be called | |
06b91d8e MP |
451 | immediately after deflateInit, deflateInit2 or deflateReset, before any |
452 | call of deflate. The compressor and decompressor must use exactly the same | |
d4286ec4 PM |
453 | dictionary (see inflateSetDictionary). |
454 | ||
455 | The dictionary should consist of strings (byte sequences) that are likely | |
456 | to be encountered later in the data to be compressed, with the most commonly | |
457 | used strings preferably put towards the end of the dictionary. Using a | |
458 | dictionary is most useful when the data to be compressed is short and can be | |
459 | predicted with good accuracy; the data can then be compressed better than | |
460 | with the default empty dictionary. | |
461 | ||
462 | Depending on the size of the compression data structures selected by | |
463 | deflateInit or deflateInit2, a part of the dictionary may in effect be | |
464 | discarded, for example if the dictionary is larger than the window size in | |
465 | deflate or deflate2. Thus the strings most likely to be useful should be | |
466 | put at the end of the dictionary, not at the front. | |
467 | ||
468 | Upon return of this function, strm->adler is set to the Adler32 value | |
469 | of the dictionary; the decompressor may later use this value to determine | |
470 | which dictionary has been used by the compressor. (The Adler32 value | |
471 | applies to the whole dictionary even if only a subset of the dictionary is | |
472 | actually used by the compressor.) | |
473 | ||
474 | deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a | |
475 | parameter is invalid (such as NULL dictionary) or the stream state is | |
476 | inconsistent (for example if deflate has already been called for this stream | |
477 | or if the compression method is bsort). deflateSetDictionary does not | |
478 | perform any compression: this will be done by deflate(). | |
479 | */ | |
480 | ||
06b91d8e MP |
481 | ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, |
482 | z_streamp source)); | |
d4286ec4 PM |
483 | /* |
484 | Sets the destination stream as a complete copy of the source stream. | |
485 | ||
486 | This function can be useful when several compression strategies will be | |
487 | tried, for example when there are several ways of pre-processing the input | |
488 | data with a filter. The streams that will be discarded should then be freed | |
489 | by calling deflateEnd. Note that deflateCopy duplicates the internal | |
490 | compression state which can be quite large, so this strategy is slow and | |
491 | can consume lots of memory. | |
492 | ||
493 | deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not | |
494 | enough memory, Z_STREAM_ERROR if the source stream state was inconsistent | |
495 | (such as zalloc being NULL). msg is left unchanged in both source and | |
496 | destination. | |
497 | */ | |
498 | ||
06b91d8e | 499 | ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); |
d4286ec4 PM |
500 | /* |
501 | This function is equivalent to deflateEnd followed by deflateInit, | |
502 | but does not free and reallocate all the internal compression state. | |
503 | The stream will keep the same compression level and any other attributes | |
504 | that may have been set by deflateInit2. | |
505 | ||
506 | deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | |
507 | stream state was inconsistent (such as zalloc or state being NULL). | |
508 | */ | |
509 | ||
06b91d8e MP |
510 | ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, |
511 | int level, | |
512 | int strategy)); | |
d4286ec4 PM |
513 | /* |
514 | Dynamically update the compression level and compression strategy. The | |
515 | interpretation of level and strategy is as in deflateInit2. This can be | |
516 | used to switch between compression and straight copy of the input data, or | |
517 | to switch to a different kind of input data requiring a different | |
518 | strategy. If the compression level is changed, the input available so far | |
519 | is compressed with the old level (and may be flushed); the new level will | |
520 | take effect only at the next call of deflate(). | |
521 | ||
522 | Before the call of deflateParams, the stream state must be set as for | |
523 | a call of deflate(), since the currently available input may have to | |
524 | be compressed and flushed. In particular, strm->avail_out must be non-zero. | |
525 | ||
526 | deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source | |
527 | stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR | |
528 | if strm->avail_out was zero. | |
529 | */ | |
530 | ||
531 | /* | |
06b91d8e MP |
532 | ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, |
533 | int windowBits)); | |
d4286ec4 PM |
534 | |
535 | This is another version of inflateInit with an extra parameter. The | |
536 | fields next_in, avail_in, zalloc, zfree and opaque must be initialized | |
537 | before by the caller. | |
538 | ||
539 | The windowBits parameter is the base two logarithm of the maximum window | |
540 | size (the size of the history buffer). It should be in the range 8..15 for | |
541 | this version of the library. The default value is 15 if inflateInit is used | |
542 | instead. If a compressed stream with a larger window size is given as | |
543 | input, inflate() will return with the error code Z_DATA_ERROR instead of | |
544 | trying to allocate a larger window. | |
545 | ||
546 | inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
547 | memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative | |
548 | memLevel). msg is set to null if there is no error message. inflateInit2 | |
549 | does not perform any decompression apart from reading the zlib header if | |
550 | present: this will be done by inflate(). (So next_in and avail_in may be | |
551 | modified, but next_out and avail_out are unchanged.) | |
552 | */ | |
553 | ||
06b91d8e MP |
554 | ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, |
555 | const Bytef *dictionary, | |
556 | uInt dictLength)); | |
d4286ec4 PM |
557 | /* |
558 | Initializes the decompression dictionary from the given uncompressed byte | |
559 | sequence. This function must be called immediately after a call of inflate | |
560 | if this call returned Z_NEED_DICT. The dictionary chosen by the compressor | |
561 | can be determined from the Adler32 value returned by this call of | |
562 | inflate. The compressor and decompressor must use exactly the same | |
563 | dictionary (see deflateSetDictionary). | |
564 | ||
565 | inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a | |
566 | parameter is invalid (such as NULL dictionary) or the stream state is | |
567 | inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the | |
568 | expected one (incorrect Adler32 value). inflateSetDictionary does not | |
569 | perform any decompression: this will be done by subsequent calls of | |
570 | inflate(). | |
571 | */ | |
572 | ||
06b91d8e | 573 | ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); |
d4286ec4 PM |
574 | /* |
575 | Skips invalid compressed data until a full flush point (see above the | |
576 | description of deflate with Z_FULL_FLUSH) can be found, or until all | |
577 | available input is skipped. No output is provided. | |
578 | ||
579 | inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR | |
580 | if no more input was provided, Z_DATA_ERROR if no flush point has been found, | |
581 | or Z_STREAM_ERROR if the stream structure was inconsistent. In the success | |
582 | case, the application may save the current current value of total_in which | |
583 | indicates where valid compressed data was found. In the error case, the | |
584 | application may repeatedly call inflateSync, providing more input each time, | |
585 | until success or end of the input data. | |
586 | */ | |
587 | ||
06b91d8e | 588 | ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); |
d4286ec4 PM |
589 | /* |
590 | This function is equivalent to inflateEnd followed by inflateInit, | |
591 | but does not free and reallocate all the internal decompression state. | |
592 | The stream will keep attributes that may have been set by inflateInit2. | |
593 | ||
594 | inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | |
595 | stream state was inconsistent (such as zalloc or state being NULL). | |
596 | */ | |
597 | ||
598 | ||
599 | /* utility functions */ | |
600 | ||
601 | /* | |
602 | The following utility functions are implemented on top of the | |
603 | basic stream-oriented functions. To simplify the interface, some | |
604 | default options are assumed (compression level and memory usage, | |
605 | standard memory allocation functions). The source code of these | |
606 | utility functions can easily be modified if you need special options. | |
607 | */ | |
608 | ||
06b91d8e MP |
609 | ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, |
610 | const Bytef *source, uLong sourceLen)); | |
d4286ec4 PM |
611 | /* |
612 | Compresses the source buffer into the destination buffer. sourceLen is | |
613 | the byte length of the source buffer. Upon entry, destLen is the total | |
614 | size of the destination buffer, which must be at least 0.1% larger than | |
615 | sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the | |
616 | compressed buffer. | |
617 | This function can be used to compress a whole file at once if the | |
618 | input file is mmap'ed. | |
619 | compress returns Z_OK if success, Z_MEM_ERROR if there was not | |
620 | enough memory, Z_BUF_ERROR if there was not enough room in the output | |
621 | buffer. | |
622 | */ | |
623 | ||
06b91d8e MP |
624 | ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, |
625 | const Bytef *source, uLong sourceLen, | |
626 | int level)); | |
d4286ec4 PM |
627 | /* |
628 | Compresses the source buffer into the destination buffer. The level | |
629 | parameter has the same meaning as in deflateInit. sourceLen is the byte | |
630 | length of the source buffer. Upon entry, destLen is the total size of the | |
631 | destination buffer, which must be at least 0.1% larger than sourceLen plus | |
632 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. | |
633 | ||
634 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
635 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, | |
636 | Z_STREAM_ERROR if the level parameter is invalid. | |
637 | */ | |
638 | ||
06b91d8e MP |
639 | ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, |
640 | const Bytef *source, uLong sourceLen)); | |
d4286ec4 PM |
641 | /* |
642 | Decompresses the source buffer into the destination buffer. sourceLen is | |
643 | the byte length of the source buffer. Upon entry, destLen is the total | |
644 | size of the destination buffer, which must be large enough to hold the | |
645 | entire uncompressed data. (The size of the uncompressed data must have | |
646 | been saved previously by the compressor and transmitted to the decompressor | |
647 | by some mechanism outside the scope of this compression library.) | |
648 | Upon exit, destLen is the actual size of the compressed buffer. | |
649 | This function can be used to decompress a whole file at once if the | |
650 | input file is mmap'ed. | |
651 | ||
652 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not | |
653 | enough memory, Z_BUF_ERROR if there was not enough room in the output | |
654 | buffer, or Z_DATA_ERROR if the input data was corrupted. | |
655 | */ | |
656 | ||
657 | ||
658 | typedef voidp gzFile; | |
659 | ||
06b91d8e | 660 | ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); |
d4286ec4 PM |
661 | /* |
662 | Opens a gzip (.gz) file for reading or writing. The mode parameter | |
663 | is as in fopen ("rb" or "wb") but can also include a compression level | |
664 | ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for | |
665 | Huffman only compression as in "wb1h". (See the description | |
666 | of deflateInit2 for more information about the strategy parameter.) | |
667 | ||
668 | gzopen can be used to read a file which is not in gzip format; in this | |
669 | case gzread will directly read from the file without decompression. | |
670 | ||
671 | gzopen returns NULL if the file could not be opened or if there was | |
672 | insufficient memory to allocate the (de)compression state; errno | |
673 | can be checked to distinguish the two cases (if errno is zero, the | |
674 | zlib error is Z_MEM_ERROR). */ | |
675 | ||
06b91d8e | 676 | ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); |
d4286ec4 PM |
677 | /* |
678 | gzdopen() associates a gzFile with the file descriptor fd. File | |
679 | descriptors are obtained from calls like open, dup, creat, pipe or | |
680 | fileno (in the file has been previously opened with fopen). | |
681 | The mode parameter is as in gzopen. | |
682 | The next call of gzclose on the returned gzFile will also close the | |
683 | file descriptor fd, just like fclose(fdopen(fd), mode) closes the file | |
684 | descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). | |
685 | gzdopen returns NULL if there was insufficient memory to allocate | |
686 | the (de)compression state. | |
687 | */ | |
688 | ||
06b91d8e | 689 | ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); |
d4286ec4 PM |
690 | /* |
691 | Dynamically update the compression level or strategy. See the description | |
692 | of deflateInit2 for the meaning of these parameters. | |
693 | gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not | |
694 | opened for writing. | |
695 | */ | |
696 | ||
06b91d8e | 697 | ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); |
d4286ec4 PM |
698 | /* |
699 | Reads the given number of uncompressed bytes from the compressed file. | |
700 | If the input file was not in gzip format, gzread copies the given number | |
701 | of bytes into the buffer. | |
702 | gzread returns the number of uncompressed bytes actually read (0 for | |
703 | end of file, -1 for error). */ | |
704 | ||
06b91d8e MP |
705 | ZEXTERN int ZEXPORT gzwrite OF((gzFile file, |
706 | const voidp buf, unsigned len)); | |
d4286ec4 PM |
707 | /* |
708 | Writes the given number of uncompressed bytes into the compressed file. | |
709 | gzwrite returns the number of uncompressed bytes actually written | |
710 | (0 in case of error). | |
711 | */ | |
712 | ||
06b91d8e | 713 | ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); |
d4286ec4 PM |
714 | /* |
715 | Converts, formats, and writes the args to the compressed file under | |
716 | control of the format string, as in fprintf. gzprintf returns the number of | |
717 | uncompressed bytes actually written (0 in case of error). | |
718 | */ | |
719 | ||
06b91d8e | 720 | ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); |
d4286ec4 PM |
721 | /* |
722 | Writes the given null-terminated string to the compressed file, excluding | |
723 | the terminating null character. | |
724 | gzputs returns the number of characters written, or -1 in case of error. | |
725 | */ | |
726 | ||
06b91d8e | 727 | ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); |
d4286ec4 PM |
728 | /* |
729 | Reads bytes from the compressed file until len-1 characters are read, or | |
730 | a newline character is read and transferred to buf, or an end-of-file | |
731 | condition is encountered. The string is then terminated with a null | |
732 | character. | |
733 | gzgets returns buf, or Z_NULL in case of error. | |
734 | */ | |
735 | ||
06b91d8e | 736 | ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); |
d4286ec4 PM |
737 | /* |
738 | Writes c, converted to an unsigned char, into the compressed file. | |
739 | gzputc returns the value that was written, or -1 in case of error. | |
740 | */ | |
741 | ||
06b91d8e | 742 | ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); |
d4286ec4 PM |
743 | /* |
744 | Reads one byte from the compressed file. gzgetc returns this byte | |
745 | or -1 in case of end of file or error. | |
746 | */ | |
747 | ||
06b91d8e | 748 | ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); |
d4286ec4 PM |
749 | /* |
750 | Flushes all pending output into the compressed file. The parameter | |
751 | flush is as in the deflate() function. The return value is the zlib | |
752 | error number (see function gzerror below). gzflush returns Z_OK if | |
753 | the flush parameter is Z_FINISH and all output could be flushed. | |
754 | gzflush should be called only when strictly necessary because it can | |
755 | degrade compression. | |
756 | */ | |
757 | ||
06b91d8e MP |
758 | ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, |
759 | z_off_t offset, int whence)); | |
d4286ec4 | 760 | /* |
06b91d8e MP |
761 | Sets the starting position for the next gzread or gzwrite on the |
762 | given compressed file. The offset represents a number of bytes in the | |
d4286ec4 PM |
763 | uncompressed data stream. The whence parameter is defined as in lseek(2); |
764 | the value SEEK_END is not supported. | |
765 | If the file is opened for reading, this function is emulated but can be | |
766 | extremely slow. If the file is opened for writing, only forward seeks are | |
767 | supported; gzseek then compresses a sequence of zeroes up to the new | |
768 | starting position. | |
769 | ||
770 | gzseek returns the resulting offset location as measured in bytes from | |
771 | the beginning of the uncompressed stream, or -1 in case of error, in | |
772 | particular if the file is opened for writing and the new starting position | |
773 | would be before the current position. | |
774 | */ | |
775 | ||
06b91d8e | 776 | ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); |
d4286ec4 PM |
777 | /* |
778 | Rewinds the given file. This function is supported only for reading. | |
779 | ||
780 | gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) | |
781 | */ | |
782 | ||
06b91d8e | 783 | ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); |
d4286ec4 PM |
784 | /* |
785 | Returns the starting position for the next gzread or gzwrite on the | |
786 | given compressed file. This position represents a number of bytes in the | |
787 | uncompressed data stream. | |
788 | ||
789 | gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) | |
790 | */ | |
791 | ||
06b91d8e | 792 | ZEXTERN int ZEXPORT gzeof OF((gzFile file)); |
d4286ec4 PM |
793 | /* |
794 | Returns 1 when EOF has previously been detected reading the given | |
795 | input stream, otherwise zero. | |
796 | */ | |
797 | ||
06b91d8e | 798 | ZEXTERN int ZEXPORT gzclose OF((gzFile file)); |
d4286ec4 PM |
799 | /* |
800 | Flushes all pending output if necessary, closes the compressed file | |
801 | and deallocates all the (de)compression state. The return value is the zlib | |
802 | error number (see function gzerror below). | |
803 | */ | |
804 | ||
06b91d8e | 805 | ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); |
d4286ec4 PM |
806 | /* |
807 | Returns the error message for the last error which occurred on the | |
808 | given compressed file. errnum is set to zlib error number. If an | |
809 | error occurred in the file system and not in the compression library, | |
810 | errnum is set to Z_ERRNO and the application may consult errno | |
811 | to get the exact error code. | |
812 | */ | |
813 | ||
814 | /* checksum functions */ | |
815 | ||
816 | /* | |
817 | These functions are not related to compression but are exported | |
818 | anyway because they might be useful in applications using the | |
819 | compression library. | |
820 | */ | |
821 | ||
06b91d8e | 822 | ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); |
d4286ec4 PM |
823 | |
824 | /* | |
825 | Update a running Adler-32 checksum with the bytes buf[0..len-1] and | |
826 | return the updated checksum. If buf is NULL, this function returns | |
827 | the required initial value for the checksum. | |
828 | An Adler-32 checksum is almost as reliable as a CRC32 but can be computed | |
829 | much faster. Usage example: | |
830 | ||
831 | uLong adler = adler32(0L, Z_NULL, 0); | |
832 | ||
833 | while (read_buffer(buffer, length) != EOF) { | |
834 | adler = adler32(adler, buffer, length); | |
835 | } | |
836 | if (adler != original_adler) error(); | |
837 | */ | |
838 | ||
06b91d8e | 839 | ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); |
d4286ec4 PM |
840 | /* |
841 | Update a running crc with the bytes buf[0..len-1] and return the updated | |
842 | crc. If buf is NULL, this function returns the required initial value | |
843 | for the crc. Pre- and post-conditioning (one's complement) is performed | |
844 | within this function so it shouldn't be done by the application. | |
845 | Usage example: | |
846 | ||
847 | uLong crc = crc32(0L, Z_NULL, 0); | |
848 | ||
849 | while (read_buffer(buffer, length) != EOF) { | |
850 | crc = crc32(crc, buffer, length); | |
851 | } | |
852 | if (crc != original_crc) error(); | |
853 | */ | |
854 | ||
855 | ||
856 | /* various hacks, don't look :) */ | |
857 | ||
858 | /* deflateInit and inflateInit are macros to allow checking the zlib version | |
859 | * and the compiler's view of z_stream: | |
860 | */ | |
06b91d8e | 861 | ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, |
d4286ec4 | 862 | const char *version, int stream_size)); |
06b91d8e MP |
863 | ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, |
864 | const char *version, int stream_size)); | |
865 | ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, | |
866 | int windowBits, int memLevel, | |
867 | int strategy, const char *version, | |
868 | int stream_size)); | |
869 | ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, | |
870 | const char *version, int stream_size)); | |
d4286ec4 PM |
871 | #define deflateInit(strm, level) \ |
872 | deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) | |
873 | #define inflateInit(strm) \ | |
874 | inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) | |
875 | #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ | |
876 | deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ | |
877 | (strategy), ZLIB_VERSION, sizeof(z_stream)) | |
878 | #define inflateInit2(strm, windowBits) \ | |
879 | inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) | |
880 | ||
881 | ||
882 | #if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL) | |
883 | struct internal_state {int dummy;}; /* hack for buggy compilers */ | |
884 | #endif | |
885 | ||
06b91d8e MP |
886 | ZEXTERN const char * ZEXPORT zError OF((int err)); |
887 | ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z)); | |
888 | ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); | |
d4286ec4 PM |
889 | |
890 | #ifdef __cplusplus | |
891 | } | |
892 | #endif | |
893 | ||
894 | #endif /* _ZLIB_H */ |