Mercurial > projects > ldc
comparison lphobos/etc/c/zlib.d @ 473:373489eeaf90
Applied downs' lphobos update
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Mon, 04 Aug 2008 19:28:49 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
472:15c804b6ce77 | 473:373489eeaf90 |
---|---|
1 /* zlib.d: modified from zlib.h by Walter Bright */ | |
2 /* NOTE: This file has been patched from the original DMD distribution to | |
3 work with the GDC compiler. | |
4 | |
5 Modified by David Friedman, February 2007 | |
6 */ | |
7 | |
8 module etc.c.zlib; | |
9 | |
10 /* zlib.h -- interface of the 'zlib' general purpose compression library | |
11 version 1.2.1, November 17th, 2003 | |
12 | |
13 Copyright (C) 1995-2003 Jean-loup Gailly and Mark Adler | |
14 | |
15 This software is provided 'as-is', without any express or implied | |
16 warranty. In no event will the authors be held liable for any damages | |
17 arising from the use of this software. | |
18 | |
19 Permission is granted to anyone to use this software for any purpose, | |
20 including commercial applications, and to alter it and redistribute it | |
21 freely, subject to the following restrictions: | |
22 | |
23 1. The origin of this software must not be misrepresented; you must not | |
24 claim that you wrote the original software. If you use this software | |
25 in a product, an acknowledgment in the product documentation would be | |
26 appreciated but is not required. | |
27 2. Altered source versions must be plainly marked as such, and must not be | |
28 misrepresented as being the original software. | |
29 3. This notice may not be removed or altered from any source distribution. | |
30 | |
31 Jean-loup Gailly Mark Adler | |
32 jloup@gzip.org madler@alumni.caltech.edu | |
33 | |
34 | |
35 The data format used by the zlib library is described by RFCs (Request for | |
36 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt | |
37 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). | |
38 */ | |
39 | |
40 private import std.stdint; | |
41 | |
42 extern (C): | |
43 alias uint Culong_t; | |
44 alias int Clong_t; | |
45 | |
46 char[] ZLIB_VERSION = "1.2.1"; | |
47 | |
48 /* | |
49 The 'zlib' compression library provides in-memory compression and | |
50 decompression functions, including integrity checks of the uncompressed | |
51 data. This version of the library supports only one compression method | |
52 (deflation) but other algorithms will be added later and will have the same | |
53 stream interface. | |
54 | |
55 Compression can be done in a single step if the buffers are large | |
56 enough (for example if an input file is mmap'ed), or can be done by | |
57 repeated calls of the compression function. In the latter case, the | |
58 application must provide more input and/or consume the output | |
59 (providing more output space) before each call. | |
60 | |
61 The compressed data format used by the in-memory functions is the zlib | |
62 format, which is a zlib wrapper documented in RFC 1950, wrapped around a | |
63 deflate stream, which is itself documented in RFC 1951. | |
64 | |
65 The library also supports reading and writing files in gzip (.gz) format | |
66 with an interface similar to that of stdio using the functions that start | |
67 with "gz". The gzip format is different from the zlib format. gzip is a | |
68 gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. | |
69 | |
70 The zlib format was designed to be compact and fast for use in memory | |
71 and on communications channels. The gzip format was designed for single- | |
72 file compression on file systems, has a larger header than zlib to maintain | |
73 directory information, and uses a different, slower check method than zlib. | |
74 | |
75 This library does not provide any functions to write gzip files in memory. | |
76 However such functions could be easily written using zlib's deflate function, | |
77 the documentation in the gzip RFC, and the examples in gzio.c. | |
78 | |
79 The library does not install any signal handler. The decoder checks | |
80 the consistency of the compressed data, so the library should never | |
81 crash even in case of corrupted input. | |
82 */ | |
83 | |
84 alias void* (*alloc_func) (void* opaque, uint items, uint size); | |
85 alias void (*free_func) (void* opaque, void* address); | |
86 | |
87 struct z_stream | |
88 { | |
89 ubyte *next_in; /* next input byte */ | |
90 uint avail_in; /* number of bytes available at next_in */ | |
91 Culong_t total_in; /* total nb of input bytes read so far */ | |
92 | |
93 ubyte *next_out; /* next output byte should be put there */ | |
94 uint avail_out; /* remaining free space at next_out */ | |
95 Culong_t total_out; /* total nb of bytes output so far */ | |
96 | |
97 char *msg; /* last error message, NULL if no error */ | |
98 void* state; /* not visible by applications */ | |
99 | |
100 alloc_func zalloc; /* used to allocate the internal state */ | |
101 free_func zfree; /* used to free the internal state */ | |
102 void* opaque; /* private data object passed to zalloc and zfree */ | |
103 | |
104 int data_type; /* best guess about the data type: ascii or binary */ | |
105 Culong_t adler; /* adler32 value of the uncompressed data */ | |
106 Culong_t reserved; /* reserved for future use */ | |
107 } | |
108 | |
109 alias z_stream* z_streamp; | |
110 | |
111 /* | |
112 The application must update next_in and avail_in when avail_in has | |
113 dropped to zero. It must update next_out and avail_out when avail_out | |
114 has dropped to zero. The application must initialize zalloc, zfree and | |
115 opaque before calling the init function. All other fields are set by the | |
116 compression library and must not be updated by the application. | |
117 | |
118 The opaque value provided by the application will be passed as the first | |
119 parameter for calls of zalloc and zfree. This can be useful for custom | |
120 memory management. The compression library attaches no meaning to the | |
121 opaque value. | |
122 | |
123 zalloc must return Z_NULL if there is not enough memory for the object. | |
124 If zlib is used in a multi-threaded application, zalloc and zfree must be | |
125 thread safe. | |
126 | |
127 On 16-bit systems, the functions zalloc and zfree must be able to allocate | |
128 exactly 65536 bytes, but will not be required to allocate more than this | |
129 if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, | |
130 pointers returned by zalloc for objects of exactly 65536 bytes *must* | |
131 have their offset normalized to zero. The default allocation function | |
132 provided by this library ensures this (see zutil.c). To reduce memory | |
133 requirements and avoid any allocation of 64K objects, at the expense of | |
134 compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). | |
135 | |
136 The fields total_in and total_out can be used for statistics or | |
137 progress reports. After compression, total_in holds the total size of | |
138 the uncompressed data and may be saved for use in the decompressor | |
139 (particularly if the decompressor wants to decompress everything in | |
140 a single step). | |
141 */ | |
142 | |
143 /* constants */ | |
144 | |
145 enum | |
146 { | |
147 Z_NO_FLUSH = 0, | |
148 Z_PARTIAL_FLUSH = 1, /* will be removed, use Z_SYNC_FLUSH instead */ | |
149 Z_SYNC_FLUSH = 2, | |
150 Z_FULL_FLUSH = 3, | |
151 Z_FINISH = 4, | |
152 Z_BLOCK = 5 | |
153 } | |
154 /* Allowed flush values; see deflate() and inflate() below for details */ | |
155 | |
156 enum | |
157 { | |
158 Z_OK = 0, | |
159 Z_STREAM_END = 1, | |
160 Z_NEED_DICT = 2, | |
161 Z_ERRNO = -1, | |
162 Z_STREAM_ERROR = -2, | |
163 Z_DATA_ERROR = -3, | |
164 Z_MEM_ERROR = -4, | |
165 Z_BUF_ERROR = -5, | |
166 Z_VERSION_ERROR = -6, | |
167 } | |
168 /* Return codes for the compression/decompression functions. Negative | |
169 * values are errors, positive values are used for special but normal events. | |
170 */ | |
171 | |
172 enum | |
173 { | |
174 Z_NO_COMPRESSION = 0, | |
175 Z_BEST_SPEED = 1, | |
176 Z_BEST_COMPRESSION = 9, | |
177 Z_DEFAULT_COMPRESSION = -1, | |
178 } | |
179 /* compression levels */ | |
180 | |
181 enum | |
182 { | |
183 Z_FILTERED = 1, | |
184 Z_HUFFMAN_ONLY = 2, | |
185 Z_RLE = 3, | |
186 Z_DEFAULT_STRATEGY = 0, | |
187 } | |
188 /* compression strategy; see deflateInit2() below for details */ | |
189 | |
190 enum | |
191 { | |
192 Z_BINARY = 0, | |
193 Z_ASCII = 1, | |
194 Z_UNKNOWN = 2, | |
195 } | |
196 /* Possible values of the data_type field (though see inflate()) */ | |
197 | |
198 enum | |
199 { | |
200 Z_DEFLATED = 8, | |
201 } | |
202 /* The deflate compression method (the only one supported in this version) */ | |
203 | |
204 const int Z_NULL = 0; /* for initializing zalloc, zfree, opaque */ | |
205 | |
206 /* basic functions */ | |
207 | |
208 char* zlibVersion(); | |
209 /* The application can compare zlibVersion and ZLIB_VERSION for consistency. | |
210 If the first character differs, the library code actually used is | |
211 not compatible with the zlib.h header file used by the application. | |
212 This check is automatically made by deflateInit and inflateInit. | |
213 */ | |
214 | |
215 int deflateInit(z_streamp strm, int level) | |
216 { | |
217 return deflateInit_(strm, level, ZLIB_VERSION.ptr, z_stream.sizeof); | |
218 } | |
219 /* | |
220 Initializes the internal stream state for compression. The fields | |
221 zalloc, zfree and opaque must be initialized before by the caller. | |
222 If zalloc and zfree are set to Z_NULL, deflateInit updates them to | |
223 use default allocation functions. | |
224 | |
225 The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: | |
226 1 gives best speed, 9 gives best compression, 0 gives no compression at | |
227 all (the input data is simply copied a block at a time). | |
228 Z_DEFAULT_COMPRESSION requests a default compromise between speed and | |
229 compression (currently equivalent to level 6). | |
230 | |
231 deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not | |
232 enough memory, Z_STREAM_ERROR if level is not a valid compression level, | |
233 Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible | |
234 with the version assumed by the caller (ZLIB_VERSION). | |
235 msg is set to null if there is no error message. deflateInit does not | |
236 perform any compression: this will be done by deflate(). | |
237 */ | |
238 | |
239 | |
240 int deflate(z_streamp strm, int flush); | |
241 /* | |
242 deflate compresses as much data as possible, and stops when the input | |
243 buffer becomes empty or the output buffer becomes full. It may introduce some | |
244 output latency (reading input without producing any output) except when | |
245 forced to flush. | |
246 | |
247 The detailed semantics are as follows. deflate performs one or both of the | |
248 following actions: | |
249 | |
250 - Compress more input starting at next_in and update next_in and avail_in | |
251 accordingly. If not all input can be processed (because there is not | |
252 enough room in the output buffer), next_in and avail_in are updated and | |
253 processing will resume at this point for the next call of deflate(). | |
254 | |
255 - Provide more output starting at next_out and update next_out and avail_out | |
256 accordingly. This action is forced if the parameter flush is non zero. | |
257 Forcing flush frequently degrades the compression ratio, so this parameter | |
258 should be set only when necessary (in interactive applications). | |
259 Some output may be provided even if flush is not set. | |
260 | |
261 Before the call of deflate(), the application should ensure that at least | |
262 one of the actions is possible, by providing more input and/or consuming | |
263 more output, and updating avail_in or avail_out accordingly; avail_out | |
264 should never be zero before the call. The application can consume the | |
265 compressed output when it wants, for example when the output buffer is full | |
266 (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK | |
267 and with zero avail_out, it must be called again after making room in the | |
268 output buffer because there might be more output pending. | |
269 | |
270 If the parameter flush is set to Z_SYNC_FLUSH, all pending output is | |
271 flushed to the output buffer and the output is aligned on a byte boundary, so | |
272 that the decompressor can get all input data available so far. (In particular | |
273 avail_in is zero after the call if enough output space has been provided | |
274 before the call.) Flushing may degrade compression for some compression | |
275 algorithms and so it should be used only when necessary. | |
276 | |
277 If flush is set to Z_FULL_FLUSH, all output is flushed as with | |
278 Z_SYNC_FLUSH, and the compression state is reset so that decompression can | |
279 restart from this point if previous compressed data has been damaged or if | |
280 random access is desired. Using Z_FULL_FLUSH too often can seriously degrade | |
281 the compression. | |
282 | |
283 If deflate returns with avail_out == 0, this function must be called again | |
284 with the same value of the flush parameter and more output space (updated | |
285 avail_out), until the flush is complete (deflate returns with non-zero | |
286 avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that | |
287 avail_out is greater than six to avoid repeated flush markers due to | |
288 avail_out == 0 on return. | |
289 | |
290 If the parameter flush is set to Z_FINISH, pending input is processed, | |
291 pending output is flushed and deflate returns with Z_STREAM_END if there | |
292 was enough output space; if deflate returns with Z_OK, this function must be | |
293 called again with Z_FINISH and more output space (updated avail_out) but no | |
294 more input data, until it returns with Z_STREAM_END or an error. After | |
295 deflate has returned Z_STREAM_END, the only possible operations on the | |
296 stream are deflateReset or deflateEnd. | |
297 | |
298 Z_FINISH can be used immediately after deflateInit if all the compression | |
299 is to be done in a single step. In this case, avail_out must be at least | |
300 the value returned by deflateBound (see below). If deflate does not return | |
301 Z_STREAM_END, then it must be called again as described above. | |
302 | |
303 deflate() sets strm->adler to the adler32 checksum of all input read | |
304 so far (that is, total_in bytes). | |
305 | |
306 deflate() may update data_type if it can make a good guess about | |
307 the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered | |
308 binary. This field is only for information purposes and does not affect | |
309 the compression algorithm in any manner. | |
310 | |
311 deflate() returns Z_OK if some progress has been made (more input | |
312 processed or more output produced), Z_STREAM_END if all input has been | |
313 consumed and all output has been produced (only when flush is set to | |
314 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example | |
315 if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible | |
316 (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not | |
317 fatal, and deflate() can be called again with more input and more output | |
318 space to continue compressing. | |
319 */ | |
320 | |
321 | |
322 int deflateEnd(z_streamp strm); | |
323 /* | |
324 All dynamically allocated data structures for this stream are freed. | |
325 This function discards any unprocessed input and does not flush any | |
326 pending output. | |
327 | |
328 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the | |
329 stream state was inconsistent, Z_DATA_ERROR if the stream was freed | |
330 prematurely (some input or output was discarded). In the error case, | |
331 msg may be set but then points to a static string (which must not be | |
332 deallocated). | |
333 */ | |
334 | |
335 | |
336 int inflateInit(z_streamp strm) | |
337 { | |
338 return inflateInit_(strm, ZLIB_VERSION.ptr, z_stream.sizeof); | |
339 } | |
340 /* | |
341 Initializes the internal stream state for decompression. The fields | |
342 next_in, avail_in, zalloc, zfree and opaque must be initialized before by | |
343 the caller. If next_in is not Z_NULL and avail_in is large enough (the exact | |
344 value depends on the compression method), inflateInit determines the | |
345 compression method from the zlib header and allocates all data structures | |
346 accordingly; otherwise the allocation will be deferred to the first call of | |
347 inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to | |
348 use default allocation functions. | |
349 | |
350 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
351 memory, Z_VERSION_ERROR if the zlib library version is incompatible with the | |
352 version assumed by the caller. msg is set to null if there is no error | |
353 message. inflateInit does not perform any decompression apart from reading | |
354 the zlib header if present: this will be done by inflate(). (So next_in and | |
355 avail_in may be modified, but next_out and avail_out are unchanged.) | |
356 */ | |
357 | |
358 | |
359 int inflate(z_streamp strm, int flush); | |
360 /* | |
361 inflate decompresses as much data as possible, and stops when the input | |
362 buffer becomes empty or the output buffer becomes full. It may introduce | |
363 some output latency (reading input without producing any output) except when | |
364 forced to flush. | |
365 | |
366 The detailed semantics are as follows. inflate performs one or both of the | |
367 following actions: | |
368 | |
369 - Decompress more input starting at next_in and update next_in and avail_in | |
370 accordingly. If not all input can be processed (because there is not | |
371 enough room in the output buffer), next_in is updated and processing | |
372 will resume at this point for the next call of inflate(). | |
373 | |
374 - Provide more output starting at next_out and update next_out and avail_out | |
375 accordingly. inflate() provides as much output as possible, until there | |
376 is no more input data or no more space in the output buffer (see below | |
377 about the flush parameter). | |
378 | |
379 Before the call of inflate(), the application should ensure that at least | |
380 one of the actions is possible, by providing more input and/or consuming | |
381 more output, and updating the next_* and avail_* values accordingly. | |
382 The application can consume the uncompressed output when it wants, for | |
383 example when the output buffer is full (avail_out == 0), or after each | |
384 call of inflate(). If inflate returns Z_OK and with zero avail_out, it | |
385 must be called again after making room in the output buffer because there | |
386 might be more output pending. | |
387 | |
388 The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, | |
389 Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much | |
390 output as possible to the output buffer. Z_BLOCK requests that inflate() stop | |
391 if and when it get to the next deflate block boundary. When decoding the zlib | |
392 or gzip format, this will cause inflate() to return immediately after the | |
393 header and before the first block. When doing a raw inflate, inflate() will | |
394 go ahead and process the first block, and will return when it gets to the end | |
395 of that block, or when it runs out of data. | |
396 | |
397 The Z_BLOCK option assists in appending to or combining deflate streams. | |
398 Also to assist in this, on return inflate() will set strm->data_type to the | |
399 number of unused bits in the last byte taken from strm->next_in, plus 64 | |
400 if inflate() is currently decoding the last block in the deflate stream, | |
401 plus 128 if inflate() returned immediately after decoding an end-of-block | |
402 code or decoding the complete header up to just before the first byte of the | |
403 deflate stream. The end-of-block will not be indicated until all of the | |
404 uncompressed data from that block has been written to strm->next_out. The | |
405 number of unused bits may in general be greater than seven, except when | |
406 bit 7 of data_type is set, in which case the number of unused bits will be | |
407 less than eight. | |
408 | |
409 inflate() should normally be called until it returns Z_STREAM_END or an | |
410 error. However if all decompression is to be performed in a single step | |
411 (a single call of inflate), the parameter flush should be set to | |
412 Z_FINISH. In this case all pending input is processed and all pending | |
413 output is flushed; avail_out must be large enough to hold all the | |
414 uncompressed data. (The size of the uncompressed data may have been saved | |
415 by the compressor for this purpose.) The next operation on this stream must | |
416 be inflateEnd to deallocate the decompression state. The use of Z_FINISH | |
417 is never required, but can be used to inform inflate that a faster approach | |
418 may be used for the single inflate() call. | |
419 | |
420 In this implementation, inflate() always flushes as much output as | |
421 possible to the output buffer, and always uses the faster approach on the | |
422 first call. So the only effect of the flush parameter in this implementation | |
423 is on the return value of inflate(), as noted below, or when it returns early | |
424 because Z_BLOCK is used. | |
425 | |
426 If a preset dictionary is needed after this call (see inflateSetDictionary | |
427 below), inflate sets strm-adler to the adler32 checksum of the dictionary | |
428 chosen by the compressor and returns Z_NEED_DICT; otherwise it sets | |
429 strm->adler to the adler32 checksum of all output produced so far (that is, | |
430 total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described | |
431 below. At the end of the stream, inflate() checks that its computed adler32 | |
432 checksum is equal to that saved by the compressor and returns Z_STREAM_END | |
433 only if the checksum is correct. | |
434 | |
435 inflate() will decompress and check either zlib-wrapped or gzip-wrapped | |
436 deflate data. The header type is detected automatically. Any information | |
437 contained in the gzip header is not retained, so applications that need that | |
438 information should instead use raw inflate, see inflateInit2() below, or | |
439 inflateBack() and perform their own processing of the gzip header and | |
440 trailer. | |
441 | |
442 inflate() returns Z_OK if some progress has been made (more input processed | |
443 or more output produced), Z_STREAM_END if the end of the compressed data has | |
444 been reached and all uncompressed output has been produced, Z_NEED_DICT if a | |
445 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was | |
446 corrupted (input stream not conforming to the zlib format or incorrect check | |
447 value), Z_STREAM_ERROR if the stream structure was inconsistent (for example | |
448 if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, | |
449 Z_BUF_ERROR if no progress is possible or if there was not enough room in the | |
450 output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and | |
451 inflate() can be called again with more input and more output space to | |
452 continue decompressing. If Z_DATA_ERROR is returned, the application may then | |
453 call inflateSync() to look for a good compression block if a partial recovery | |
454 of the data is desired. | |
455 */ | |
456 | |
457 | |
458 int inflateEnd(z_streamp strm); | |
459 /* | |
460 All dynamically allocated data structures for this stream are freed. | |
461 This function discards any unprocessed input and does not flush any | |
462 pending output. | |
463 | |
464 inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state | |
465 was inconsistent. In the error case, msg may be set but then points to a | |
466 static string (which must not be deallocated). | |
467 */ | |
468 | |
469 /* Advanced functions */ | |
470 | |
471 /* | |
472 The following functions are needed only in some special applications. | |
473 */ | |
474 | |
475 int deflateInit2(z_streamp strm, | |
476 int level, | |
477 int method, | |
478 int windowBits, | |
479 int memLevel, | |
480 int strategy) | |
481 { | |
482 return deflateInit2_(strm, level, method, windowBits, memLevel, | |
483 strategy, ZLIB_VERSION.ptr, z_stream.sizeof); | |
484 } | |
485 /* | |
486 This is another version of deflateInit with more compression options. The | |
487 fields next_in, zalloc, zfree and opaque must be initialized before by | |
488 the caller. | |
489 | |
490 The method parameter is the compression method. It must be Z_DEFLATED in | |
491 this version of the library. | |
492 | |
493 The windowBits parameter is the base two logarithm of the window size | |
494 (the size of the history buffer). It should be in the range 8..15 for this | |
495 version of the library. Larger values of this parameter result in better | |
496 compression at the expense of memory usage. The default value is 15 if | |
497 deflateInit is used instead. | |
498 | |
499 windowBits can also be -8..-15 for raw deflate. In this case, -windowBits | |
500 determines the window size. deflate() will then generate raw deflate data | |
501 with no zlib header or trailer, and will not compute an adler32 check value. | |
502 | |
503 windowBits can also be greater than 15 for optional gzip encoding. Add | |
504 16 to windowBits to write a simple gzip header and trailer around the | |
505 compressed data instead of a zlib wrapper. The gzip header will have no | |
506 file name, no extra data, no comment, no modification time (set to zero), | |
507 no header crc, and the operating system will be set to 255 (unknown). | |
508 | |
509 The memLevel parameter specifies how much memory should be allocated | |
510 for the internal compression state. memLevel=1 uses minimum memory but | |
511 is slow and reduces compression ratio; memLevel=9 uses maximum memory | |
512 for optimal speed. The default value is 8. See zconf.h for total memory | |
513 usage as a function of windowBits and memLevel. | |
514 | |
515 The strategy parameter is used to tune the compression algorithm. Use the | |
516 value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a | |
517 filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no | |
518 string match), or Z_RLE to limit match distances to one (run-length | |
519 encoding). Filtered data consists mostly of small values with a somewhat | |
520 random distribution. In this case, the compression algorithm is tuned to | |
521 compress them better. The effect of Z_FILTERED is to force more Huffman | |
522 coding and less string matching; it is somewhat intermediate between | |
523 Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as | |
524 Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy | |
525 parameter only affects the compression ratio but not the correctness of the | |
526 compressed output even if it is not set appropriately. | |
527 | |
528 deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
529 memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid | |
530 method). msg is set to null if there is no error message. deflateInit2 does | |
531 not perform any compression: this will be done by deflate(). | |
532 */ | |
533 | |
534 int deflateSetDictionary(z_streamp strm, ubyte* dictionary, uint dictLength); | |
535 /* | |
536 Initializes the compression dictionary from the given byte sequence | |
537 without producing any compressed output. This function must be called | |
538 immediately after deflateInit, deflateInit2 or deflateReset, before any | |
539 call of deflate. The compressor and decompressor must use exactly the same | |
540 dictionary (see inflateSetDictionary). | |
541 | |
542 The dictionary should consist of strings (byte sequences) that are likely | |
543 to be encountered later in the data to be compressed, with the most commonly | |
544 used strings preferably put towards the end of the dictionary. Using a | |
545 dictionary is most useful when the data to be compressed is short and can be | |
546 predicted with good accuracy; the data can then be compressed better than | |
547 with the default empty dictionary. | |
548 | |
549 Depending on the size of the compression data structures selected by | |
550 deflateInit or deflateInit2, a part of the dictionary may in effect be | |
551 discarded, for example if the dictionary is larger than the window size in | |
552 deflate or deflate2. Thus the strings most likely to be useful should be | |
553 put at the end of the dictionary, not at the front. | |
554 | |
555 Upon return of this function, strm->adler is set to the adler32 value | |
556 of the dictionary; the decompressor may later use this value to determine | |
557 which dictionary has been used by the compressor. (The adler32 value | |
558 applies to the whole dictionary even if only a subset of the dictionary is | |
559 actually used by the compressor.) If a raw deflate was requested, then the | |
560 adler32 value is not computed and strm->adler is not set. | |
561 | |
562 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a | |
563 parameter is invalid (such as NULL dictionary) or the stream state is | |
564 inconsistent (for example if deflate has already been called for this stream | |
565 or if the compression method is bsort). deflateSetDictionary does not | |
566 perform any compression: this will be done by deflate(). | |
567 */ | |
568 | |
569 int deflateCopy(z_streamp dest, z_streamp source); | |
570 /* | |
571 Sets the destination stream as a complete copy of the source stream. | |
572 | |
573 This function can be useful when several compression strategies will be | |
574 tried, for example when there are several ways of pre-processing the input | |
575 data with a filter. The streams that will be discarded should then be freed | |
576 by calling deflateEnd. Note that deflateCopy duplicates the internal | |
577 compression state which can be quite large, so this strategy is slow and | |
578 can consume lots of memory. | |
579 | |
580 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not | |
581 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent | |
582 (such as zalloc being NULL). msg is left unchanged in both source and | |
583 destination. | |
584 */ | |
585 | |
586 int deflateReset(z_streamp strm); | |
587 /* | |
588 This function is equivalent to deflateEnd followed by deflateInit, | |
589 but does not free and reallocate all the internal compression state. | |
590 The stream will keep the same compression level and any other attributes | |
591 that may have been set by deflateInit2. | |
592 | |
593 deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | |
594 stream state was inconsistent (such as zalloc or state being NULL). | |
595 */ | |
596 | |
597 int deflateParams(z_streamp strm, int level, int strategy); | |
598 /* | |
599 Dynamically update the compression level and compression strategy. The | |
600 interpretation of level and strategy is as in deflateInit2. This can be | |
601 used to switch between compression and straight copy of the input data, or | |
602 to switch to a different kind of input data requiring a different | |
603 strategy. If the compression level is changed, the input available so far | |
604 is compressed with the old level (and may be flushed); the new level will | |
605 take effect only at the next call of deflate(). | |
606 | |
607 Before the call of deflateParams, the stream state must be set as for | |
608 a call of deflate(), since the currently available input may have to | |
609 be compressed and flushed. In particular, strm->avail_out must be non-zero. | |
610 | |
611 deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source | |
612 stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR | |
613 if strm->avail_out was zero. | |
614 */ | |
615 | |
616 uint deflateBound(z_streamp strm, uint Culong_t); | |
617 /* | |
618 deflateBound() returns an upper bound on the compressed size after | |
619 deflation of sourceLen bytes. It must be called after deflateInit() | |
620 or deflateInit2(). This would be used to allocate an output buffer | |
621 for deflation in a single pass, and so would be called before deflate(). | |
622 */ | |
623 | |
624 int deflatePrime(z_streamp strm, int bits, int value); | |
625 /* | |
626 deflatePrime() inserts bits in the deflate output stream. The intent | |
627 is that this function is used to start off the deflate output with the | |
628 bits leftover from a previous deflate stream when appending to it. As such, | |
629 this function can only be used for raw deflate, and must be used before the | |
630 first deflate() call after a deflateInit2() or deflateReset(). bits must be | |
631 less than or equal to 16, and that many of the least significant bits of | |
632 value will be inserted in the output. | |
633 | |
634 deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source | |
635 stream state was inconsistent. | |
636 */ | |
637 | |
638 int inflateInit2(z_streamp strm, int windowBits) | |
639 { | |
640 return inflateInit2_(strm, windowBits, ZLIB_VERSION.ptr, z_stream.sizeof); | |
641 } | |
642 /* | |
643 This is another version of inflateInit with an extra parameter. The | |
644 fields next_in, avail_in, zalloc, zfree and opaque must be initialized | |
645 before by the caller. | |
646 | |
647 The windowBits parameter is the base two logarithm of the maximum window | |
648 size (the size of the history buffer). It should be in the range 8..15 for | |
649 this version of the library. The default value is 15 if inflateInit is used | |
650 instead. windowBits must be greater than or equal to the windowBits value | |
651 provided to deflateInit2() while compressing, or it must be equal to 15 if | |
652 deflateInit2() was not used. If a compressed stream with a larger window | |
653 size is given as input, inflate() will return with the error code | |
654 Z_DATA_ERROR instead of trying to allocate a larger window. | |
655 | |
656 windowBits can also be -8..-15 for raw inflate. In this case, -windowBits | |
657 determines the window size. inflate() will then process raw deflate data, | |
658 not looking for a zlib or gzip header, not generating a check value, and not | |
659 looking for any check values for comparison at the end of the stream. This | |
660 is for use with other formats that use the deflate compressed data format | |
661 such as zip. Those formats provide their own check values. If a custom | |
662 format is developed using the raw deflate format for compressed data, it is | |
663 recommended that a check value such as an adler32 or a crc32 be applied to | |
664 the uncompressed data as is done in the zlib, gzip, and zip formats. For | |
665 most applications, the zlib format should be used as is. Note that comments | |
666 above on the use in deflateInit2() applies to the magnitude of windowBits. | |
667 | |
668 windowBits can also be greater than 15 for optional gzip decoding. Add | |
669 32 to windowBits to enable zlib and gzip decoding with automatic header | |
670 detection, or add 16 to decode only the gzip format (the zlib format will | |
671 return a Z_DATA_ERROR). | |
672 | |
673 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
674 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative | |
675 memLevel). msg is set to null if there is no error message. inflateInit2 | |
676 does not perform any decompression apart from reading the zlib header if | |
677 present: this will be done by inflate(). (So next_in and avail_in may be | |
678 modified, but next_out and avail_out are unchanged.) | |
679 */ | |
680 | |
681 int inflateSetDictionary(z_streamp strm, ubyte* dictionary, uint dictLength); | |
682 /* | |
683 Initializes the decompression dictionary from the given uncompressed byte | |
684 sequence. This function must be called immediately after a call of inflate | |
685 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor | |
686 can be determined from the adler32 value returned by this call of | |
687 inflate. The compressor and decompressor must use exactly the same | |
688 dictionary (see deflateSetDictionary). | |
689 | |
690 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a | |
691 parameter is invalid (such as NULL dictionary) or the stream state is | |
692 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the | |
693 expected one (incorrect adler32 value). inflateSetDictionary does not | |
694 perform any decompression: this will be done by subsequent calls of | |
695 inflate(). | |
696 */ | |
697 | |
698 int inflateSync(z_streamp strm); | |
699 /* | |
700 Skips invalid compressed data until a full flush point (see above the | |
701 description of deflate with Z_FULL_FLUSH) can be found, or until all | |
702 available input is skipped. No output is provided. | |
703 | |
704 inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR | |
705 if no more input was provided, Z_DATA_ERROR if no flush point has been found, | |
706 or Z_STREAM_ERROR if the stream structure was inconsistent. In the success | |
707 case, the application may save the current current value of total_in which | |
708 indicates where valid compressed data was found. In the error case, the | |
709 application may repeatedly call inflateSync, providing more input each time, | |
710 until success or end of the input data. | |
711 */ | |
712 | |
713 int inflateCopy (z_streamp dest, z_streamp source); | |
714 /* | |
715 Sets the destination stream as a complete copy of the source stream. | |
716 | |
717 This function can be useful when randomly accessing a large stream. The | |
718 first pass through the stream can periodically record the inflate state, | |
719 allowing restarting inflate at those points when randomly accessing the | |
720 stream. | |
721 | |
722 inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not | |
723 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent | |
724 (such as zalloc being NULL). msg is left unchanged in both source and | |
725 destination. | |
726 */ | |
727 | |
728 int inflateReset(z_streamp strm); | |
729 /* | |
730 This function is equivalent to inflateEnd followed by inflateInit, | |
731 but does not free and reallocate all the internal decompression state. | |
732 The stream will keep attributes that may have been set by inflateInit2. | |
733 | |
734 inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | |
735 stream state was inconsistent (such as zalloc or state being NULL). | |
736 */ | |
737 | |
738 | |
739 int inflateBackInit(z_stream* strm, int windowBits, ubyte* window) | |
740 { | |
741 return inflateBackInit_(strm, windowBits, window, ZLIB_VERSION.ptr, z_stream.sizeof); | |
742 } | |
743 /* | |
744 Initialize the internal stream state for decompression using inflateBack() | |
745 calls. The fields zalloc, zfree and opaque in strm must be initialized | |
746 before the call. If zalloc and zfree are Z_NULL, then the default library- | |
747 derived memory allocation routines are used. windowBits is the base two | |
748 logarithm of the window size, in the range 8..15. window is a caller | |
749 supplied buffer of that size. Except for special applications where it is | |
750 assured that deflate was used with small window sizes, windowBits must be 15 | |
751 and a 32K byte window must be supplied to be able to decompress general | |
752 deflate streams. | |
753 | |
754 See inflateBack() for the usage of these routines. | |
755 | |
756 inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of | |
757 the paramaters are invalid, Z_MEM_ERROR if the internal state could not | |
758 be allocated, or Z_VERSION_ERROR if the version of the library does not | |
759 match the version of the header file. | |
760 */ | |
761 | |
762 alias uint function(void*, ubyte**) in_func; | |
763 alias int function(void*, ubyte*, uint) out_func; | |
764 | |
765 int inflateBack(z_stream* strm, | |
766 in_func f_in, | |
767 void* in_desc, | |
768 out_func f_out, | |
769 void* out_desc); | |
770 /* | |
771 inflateBack() does a raw inflate with a single call using a call-back | |
772 interface for input and output. This is more efficient than inflate() for | |
773 file i/o applications in that it avoids copying between the output and the | |
774 sliding window by simply making the window itself the output buffer. This | |
775 function trusts the application to not change the output buffer passed by | |
776 the output function, at least until inflateBack() returns. | |
777 | |
778 inflateBackInit() must be called first to allocate the internal state | |
779 and to initialize the state with the user-provided window buffer. | |
780 inflateBack() may then be used multiple times to inflate a complete, raw | |
781 deflate stream with each call. inflateBackEnd() is then called to free | |
782 the allocated state. | |
783 | |
784 A raw deflate stream is one with no zlib or gzip header or trailer. | |
785 This routine would normally be used in a utility that reads zip or gzip | |
786 files and writes out uncompressed files. The utility would decode the | |
787 header and process the trailer on its own, hence this routine expects | |
788 only the raw deflate stream to decompress. This is different from the | |
789 normal behavior of inflate(), which expects either a zlib or gzip header and | |
790 trailer around the deflate stream. | |
791 | |
792 inflateBack() uses two subroutines supplied by the caller that are then | |
793 called by inflateBack() for input and output. inflateBack() calls those | |
794 routines until it reads a complete deflate stream and writes out all of the | |
795 uncompressed data, or until it encounters an error. The function's | |
796 parameters and return types are defined above in the in_func and out_func | |
797 typedefs. inflateBack() will call in(in_desc, &buf) which should return the | |
798 number of bytes of provided input, and a pointer to that input in buf. If | |
799 there is no input available, in() must return zero--buf is ignored in that | |
800 case--and inflateBack() will return a buffer error. inflateBack() will call | |
801 out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() | |
802 should return zero on success, or non-zero on failure. If out() returns | |
803 non-zero, inflateBack() will return with an error. Neither in() nor out() | |
804 are permitted to change the contents of the window provided to | |
805 inflateBackInit(), which is also the buffer that out() uses to write from. | |
806 The length written by out() will be at most the window size. Any non-zero | |
807 amount of input may be provided by in(). | |
808 | |
809 For convenience, inflateBack() can be provided input on the first call by | |
810 setting strm->next_in and strm->avail_in. If that input is exhausted, then | |
811 in() will be called. Therefore strm->next_in must be initialized before | |
812 calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called | |
813 immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in | |
814 must also be initialized, and then if strm->avail_in is not zero, input will | |
815 initially be taken from strm->next_in[0 .. strm->avail_in - 1]. | |
816 | |
817 The in_desc and out_desc parameters of inflateBack() is passed as the | |
818 first parameter of in() and out() respectively when they are called. These | |
819 descriptors can be optionally used to pass any information that the caller- | |
820 supplied in() and out() functions need to do their job. | |
821 | |
822 On return, inflateBack() will set strm->next_in and strm->avail_in to | |
823 pass back any unused input that was provided by the last in() call. The | |
824 return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR | |
825 if in() or out() returned an error, Z_DATA_ERROR if there was a format | |
826 error in the deflate stream (in which case strm->msg is set to indicate the | |
827 nature of the error), or Z_STREAM_ERROR if the stream was not properly | |
828 initialized. In the case of Z_BUF_ERROR, an input or output error can be | |
829 distinguished using strm->next_in which will be Z_NULL only if in() returned | |
830 an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to | |
831 out() returning non-zero. (in() will always be called before out(), so | |
832 strm->next_in is assured to be defined if out() returns non-zero.) Note | |
833 that inflateBack() cannot return Z_OK. | |
834 */ | |
835 | |
836 int inflateBackEnd(z_stream* strm); | |
837 /* | |
838 All memory allocated by inflateBackInit() is freed. | |
839 | |
840 inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream | |
841 state was inconsistent. | |
842 */ | |
843 | |
844 uint zlibCompileFlags(); | |
845 /* Return flags indicating compile-time options. | |
846 | |
847 Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: | |
848 1.0: size of uInt | |
849 3.2: size of uLong | |
850 5.4: size of voidpf (pointer) | |
851 7.6: size of z_off_t | |
852 | |
853 Compiler, assembler, and debug options: | |
854 8: DEBUG | |
855 9: ASMV or ASMINF -- use ASM code | |
856 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention | |
857 11: 0 (reserved) | |
858 | |
859 One-time table building (smaller code, but not thread-safe if true): | |
860 12: BUILDFIXED -- build static block decoding tables when needed | |
861 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed | |
862 14,15: 0 (reserved) | |
863 | |
864 Library content (indicates missing functionality): | |
865 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking | |
866 deflate code when not needed) | |
867 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect | |
868 and decode gzip streams (to avoid linking crc code) | |
869 18-19: 0 (reserved) | |
870 | |
871 Operation variations (changes in library functionality): | |
872 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate | |
873 21: FASTEST -- deflate algorithm with only one, lowest compression level | |
874 22,23: 0 (reserved) | |
875 | |
876 The sprintf variant used by gzprintf (zero is best): | |
877 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format | |
878 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! | |
879 26: 0 = returns value, 1 = void -- 1 means inferred string length returned | |
880 | |
881 Remainder: | |
882 27-31: 0 (reserved) | |
883 */ | |
884 | |
885 /* utility functions */ | |
886 | |
887 /* | |
888 The following utility functions are implemented on top of the | |
889 basic stream-oriented functions. To simplify the interface, some | |
890 default options are assumed (compression level and memory usage, | |
891 standard memory allocation functions). The source code of these | |
892 utility functions can easily be modified if you need special options. | |
893 */ | |
894 | |
895 int compress(ubyte* dest, | |
896 Culong_t* destLen, | |
897 ubyte* source, | |
898 Culong_t sourceLen); | |
899 /* | |
900 Compresses the source buffer into the destination buffer. sourceLen is | |
901 the byte length of the source buffer. Upon entry, destLen is the total | |
902 size of the destination buffer, which must be at least the value returned | |
903 by compressBound(sourceLen). Upon exit, destLen is the actual size of the | |
904 compressed buffer. | |
905 This function can be used to compress a whole file at once if the | |
906 input file is mmap'ed. | |
907 compress returns Z_OK if success, Z_MEM_ERROR if there was not | |
908 enough memory, Z_BUF_ERROR if there was not enough room in the output | |
909 buffer. | |
910 */ | |
911 | |
912 int compress2(ubyte* dest, | |
913 Culong_t* destLen, | |
914 ubyte* source, | |
915 Culong_t sourceLen, | |
916 int level); | |
917 /* | |
918 Compresses the source buffer into the destination buffer. The level | |
919 parameter has the same meaning as in deflateInit. sourceLen is the byte | |
920 length of the source buffer. Upon entry, destLen is the total size of the | |
921 destination buffer, which must be at least the value returned by | |
922 compressBound(sourceLen). Upon exit, destLen is the actual size of the | |
923 compressed buffer. | |
924 | |
925 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | |
926 memory, Z_BUF_ERROR if there was not enough room in the output buffer, | |
927 Z_STREAM_ERROR if the level parameter is invalid. | |
928 */ | |
929 | |
930 uint compressBound(Culong_t sourceLen); | |
931 /* | |
932 compressBound() returns an upper bound on the compressed size after | |
933 compress() or compress2() on sourceLen bytes. It would be used before | |
934 a compress() or compress2() call to allocate the destination buffer. | |
935 */ | |
936 | |
937 int uncompress(ubyte* dest, | |
938 uint* Culong_t, | |
939 ubyte* source, | |
940 uint Culong_t); | |
941 /* | |
942 Decompresses the source buffer into the destination buffer. sourceLen is | |
943 the byte length of the source buffer. Upon entry, destLen is the total | |
944 size of the destination buffer, which must be large enough to hold the | |
945 entire uncompressed data. (The size of the uncompressed data must have | |
946 been saved previously by the compressor and transmitted to the decompressor | |
947 by some mechanism outside the scope of this compression library.) | |
948 Upon exit, destLen is the actual size of the compressed buffer. | |
949 This function can be used to decompress a whole file at once if the | |
950 input file is mmap'ed. | |
951 | |
952 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not | |
953 enough memory, Z_BUF_ERROR if there was not enough room in the output | |
954 buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. | |
955 */ | |
956 | |
957 | |
958 typedef void* gzFile; | |
959 alias int z_off_t; // file offset | |
960 | |
961 gzFile gzopen(char* path, char* mode); | |
962 /* | |
963 Opens a gzip (.gz) file for reading or writing. The mode parameter | |
964 is as in fopen ("rb" or "wb") but can also include a compression level | |
965 ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for | |
966 Huffman only compression as in "wb1h", or 'R' for run-length encoding | |
967 as in "wb1R". (See the description of deflateInit2 for more information | |
968 about the strategy parameter.) | |
969 | |
970 gzopen can be used to read a file which is not in gzip format; in this | |
971 case gzread will directly read from the file without decompression. | |
972 | |
973 gzopen returns NULL if the file could not be opened or if there was | |
974 insufficient memory to allocate the (de)compression state; errno | |
975 can be checked to distinguish the two cases (if errno is zero, the | |
976 zlib error is Z_MEM_ERROR). */ | |
977 | |
978 gzFile gzdopen(int fd, char* mode); | |
979 /* | |
980 gzdopen() associates a gzFile with the file descriptor fd. File | |
981 descriptors are obtained from calls like open, dup, creat, pipe or | |
982 fileno (in the file has been previously opened with fopen). | |
983 The mode parameter is as in gzopen. | |
984 The next call of gzclose on the returned gzFile will also close the | |
985 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file | |
986 descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). | |
987 gzdopen returns NULL if there was insufficient memory to allocate | |
988 the (de)compression state. | |
989 */ | |
990 | |
991 int gzsetparams(gzFile file, int level, int strategy); | |
992 /* | |
993 Dynamically update the compression level or strategy. See the description | |
994 of deflateInit2 for the meaning of these parameters. | |
995 gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not | |
996 opened for writing. | |
997 */ | |
998 | |
999 int gzread(gzFile file, void* buf, uint len); | |
1000 /* | |
1001 Reads the given number of uncompressed bytes from the compressed file. | |
1002 If the input file was not in gzip format, gzread copies the given number | |
1003 of bytes into the buffer. | |
1004 gzread returns the number of uncompressed bytes actually read (0 for | |
1005 end of file, -1 for error). */ | |
1006 | |
1007 int gzwrite(gzFile file, void* buf, uint len); | |
1008 /* | |
1009 Writes the given number of uncompressed bytes into the compressed file. | |
1010 gzwrite returns the number of uncompressed bytes actually written | |
1011 (0 in case of error). | |
1012 */ | |
1013 | |
1014 int gzprintf(gzFile file, char* format, ...); | |
1015 /* | |
1016 Converts, formats, and writes the args to the compressed file under | |
1017 control of the format string, as in fprintf. gzprintf returns the number of | |
1018 uncompressed bytes actually written (0 in case of error). The number of | |
1019 uncompressed bytes written is limited to 4095. The caller should assure that | |
1020 this limit is not exceeded. If it is exceeded, then gzprintf() will return | |
1021 return an error (0) with nothing written. In this case, there may also be a | |
1022 buffer overflow with unpredictable consequences, which is possible only if | |
1023 zlib was compiled with the insecure functions sprintf() or vsprintf() | |
1024 because the secure snprintf() or vsnprintf() functions were not available. | |
1025 */ | |
1026 | |
1027 int gzputs(gzFile file, char* s); | |
1028 /* | |
1029 Writes the given null-terminated string to the compressed file, excluding | |
1030 the terminating null character. | |
1031 gzputs returns the number of characters written, or -1 in case of error. | |
1032 */ | |
1033 | |
1034 char* gzgets(gzFile file, char* buf, int len); | |
1035 /* | |
1036 Reads bytes from the compressed file until len-1 characters are read, or | |
1037 a newline character is read and transferred to buf, or an end-of-file | |
1038 condition is encountered. The string is then terminated with a null | |
1039 character. | |
1040 gzgets returns buf, or Z_NULL in case of error. | |
1041 */ | |
1042 | |
1043 int gzputc(gzFile file, int c); | |
1044 /* | |
1045 Writes c, converted to an unsigned char, into the compressed file. | |
1046 gzputc returns the value that was written, or -1 in case of error. | |
1047 */ | |
1048 | |
1049 int gzgetc(gzFile file); | |
1050 /* | |
1051 Reads one byte from the compressed file. gzgetc returns this byte | |
1052 or -1 in case of end of file or error. | |
1053 */ | |
1054 | |
1055 int gzungetc(int c, gzFile file); | |
1056 /* | |
1057 Push one character back onto the stream to be read again later. | |
1058 Only one character of push-back is allowed. gzungetc() returns the | |
1059 character pushed, or -1 on failure. gzungetc() will fail if a | |
1060 character has been pushed but not read yet, or if c is -1. The pushed | |
1061 character will be discarded if the stream is repositioned with gzseek() | |
1062 or gzrewind(). | |
1063 */ | |
1064 | |
1065 int gzflush(gzFile file, int flush); | |
1066 /* | |
1067 Flushes all pending output into the compressed file. The parameter | |
1068 flush is as in the deflate() function. The return value is the zlib | |
1069 error number (see function gzerror below). gzflush returns Z_OK if | |
1070 the flush parameter is Z_FINISH and all output could be flushed. | |
1071 gzflush should be called only when strictly necessary because it can | |
1072 degrade compression. | |
1073 */ | |
1074 | |
1075 z_off_t gzseek(gzFile file, z_off_t offset, int whence); | |
1076 /* | |
1077 Sets the starting position for the next gzread or gzwrite on the | |
1078 given compressed file. The offset represents a number of bytes in the | |
1079 uncompressed data stream. The whence parameter is defined as in lseek(2); | |
1080 the value SEEK_END is not supported. | |
1081 If the file is opened for reading, this function is emulated but can be | |
1082 extremely slow. If the file is opened for writing, only forward seeks are | |
1083 supported; gzseek then compresses a sequence of zeroes up to the new | |
1084 starting position. | |
1085 | |
1086 gzseek returns the resulting offset location as measured in bytes from | |
1087 the beginning of the uncompressed stream, or -1 in case of error, in | |
1088 particular if the file is opened for writing and the new starting position | |
1089 would be before the current position. | |
1090 */ | |
1091 | |
1092 int gzrewind(gzFile file); | |
1093 /* | |
1094 Rewinds the given file. This function is supported only for reading. | |
1095 | |
1096 gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) | |
1097 */ | |
1098 | |
1099 z_off_t gztell(gzFile file); | |
1100 /* | |
1101 Returns the starting position for the next gzread or gzwrite on the | |
1102 given compressed file. This position represents a number of bytes in the | |
1103 uncompressed data stream. | |
1104 | |
1105 gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) | |
1106 */ | |
1107 | |
1108 int gzeof(gzFile file); | |
1109 /* | |
1110 Returns 1 when EOF has previously been detected reading the given | |
1111 input stream, otherwise zero. | |
1112 */ | |
1113 | |
1114 int gzclose(gzFile file); | |
1115 /* | |
1116 Flushes all pending output if necessary, closes the compressed file | |
1117 and deallocates all the (de)compression state. The return value is the zlib | |
1118 error number (see function gzerror below). | |
1119 */ | |
1120 | |
1121 char* gzerror(gzFile file, int *errnum); | |
1122 /* | |
1123 Returns the error message for the last error which occurred on the | |
1124 given compressed file. errnum is set to zlib error number. If an | |
1125 error occurred in the file system and not in the compression library, | |
1126 errnum is set to Z_ERRNO and the application may consult errno | |
1127 to get the exact error code. | |
1128 */ | |
1129 | |
1130 void gzclearerr (gzFile file); | |
1131 /* | |
1132 Clears the error and end-of-file flags for file. This is analogous to the | |
1133 clearerr() function in stdio. This is useful for continuing to read a gzip | |
1134 file that is being written concurrently. | |
1135 */ | |
1136 | |
1137 /* checksum functions */ | |
1138 | |
1139 /* | |
1140 These functions are not related to compression but are exported | |
1141 anyway because they might be useful in applications using the | |
1142 compression library. | |
1143 */ | |
1144 | |
1145 Culong_t adler32 (Culong_t adler, ubyte *buf, uint len); | |
1146 | |
1147 /* | |
1148 Update a running Adler-32 checksum with the bytes buf[0..len-1] and | |
1149 return the updated checksum. If buf is NULL, this function returns | |
1150 the required initial value for the checksum. | |
1151 An Adler-32 checksum is almost as reliable as a CRC32 but can be computed | |
1152 much faster. Usage example: | |
1153 | |
1154 uint adler = adler32(0L, Z_NULL, 0); | |
1155 | |
1156 while (read_buffer(buffer, length) != EOF) { | |
1157 adler = adler32(adler, buffer, length); | |
1158 } | |
1159 if (adler != original_adler) error(); | |
1160 */ | |
1161 | |
1162 Culong_t crc32(Culong_t crc, ubyte *buf, uint len); | |
1163 /* | |
1164 Update a running crc with the bytes buf[0..len-1] and return the updated | |
1165 crc. If buf is NULL, this function returns the required initial value | |
1166 for the crc. Pre- and post-conditioning (one's complement) is performed | |
1167 within this function so it shouldn't be done by the application. | |
1168 Usage example: | |
1169 | |
1170 uint crc = crc32(0L, Z_NULL, 0); | |
1171 | |
1172 while (read_buffer(buffer, length) != EOF) { | |
1173 crc = crc32(crc, buffer, length); | |
1174 } | |
1175 if (crc != original_crc) error(); | |
1176 */ | |
1177 | |
1178 | |
1179 /* various hacks, don't look :) */ | |
1180 | |
1181 /* deflateInit and inflateInit are macros to allow checking the zlib version | |
1182 * and the compiler's view of z_stream: | |
1183 */ | |
1184 int deflateInit_(z_streamp strm, | |
1185 int level, | |
1186 char* versionx, | |
1187 int stream_size); | |
1188 | |
1189 int inflateInit_(z_streamp strm, | |
1190 char* versionx, | |
1191 int stream_size); | |
1192 | |
1193 int deflateInit2_(z_streamp strm, | |
1194 int level, | |
1195 int method, | |
1196 int windowBits, | |
1197 int memLevel, | |
1198 int strategy, | |
1199 char* versionx, | |
1200 int stream_size); | |
1201 | |
1202 int inflateBackInit_(z_stream* strm, | |
1203 int windowBits, | |
1204 ubyte* window, | |
1205 char* z_version, | |
1206 int stream_size); | |
1207 | |
1208 int inflateInit2_(z_streamp strm, | |
1209 int windowBits, | |
1210 char* versionx, | |
1211 int stream_size); | |
1212 | |
1213 char* zError(int err); | |
1214 int inflateSyncPoint(z_streamp z); | |
1215 Culong_t* get_crc_table(); |