1 /* zlib.h -- interface of the 'zlib' general purpose compression library 2 version 1.2.8, April 28th, 2013 3 4 Copyright (C) 1995-2013 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 http://tools.ietf.org/html/rfc1950 28 (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). 29 */ 30 31 #ifndef ZLIB_H 32 #define ZLIB_H 33 34 //#include "zconf.h" 35 36 #include "hammer2_zlib_zconf.h" 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 #define ZLIB_VERSION "1.2.8" 43 #define ZLIB_VERNUM 0x1280 44 #define ZLIB_VER_MAJOR 1 45 #define ZLIB_VER_MINOR 2 46 #define ZLIB_VER_REVISION 8 47 #define ZLIB_VER_SUBREVISION 0 48 49 /* 50 The 'zlib' compression library provides in-memory compression and 51 decompression functions, including integrity checks of the uncompressed data. 52 This version of the library supports only one compression method (deflation) 53 but other algorithms will be added later and will have the same stream 54 interface. 55 56 Compression can be done in a single step if the buffers are large enough, 57 or can be done by repeated calls of the compression function. In the latter 58 case, the 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 default by the in-memory functions is 62 the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped 63 around a 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 This library can optionally read and write gzip streams in memory as well. 71 72 The zlib format was designed to be compact and fast for use in memory 73 and on communications channels. The gzip format was designed for single- 74 file compression on file systems, has a larger header than zlib to maintain 75 directory information, and uses a different, slower check method than zlib. 76 77 The library does not install any signal handler. The decoder checks 78 the consistency of the compressed data, so the library should never crash 79 even in case of corrupted input. 80 */ 81 82 struct internal_state; 83 84 typedef struct z_stream_s { 85 z_const Bytef *next_in; /* next input byte */ 86 uInt avail_in; /* number of bytes available at next_in */ 87 uLong total_in; /* total number of input bytes read so far */ 88 89 Bytef *next_out; /* next output byte should be put there */ 90 uInt avail_out; /* remaining free space at next_out */ 91 uLong total_out; /* total number of bytes output so far */ 92 93 z_const char *msg; /* last error message, NULL if no error */ 94 struct internal_state FAR *state; /* not visible by applications */ 95 96 int data_type; /* best guess about the data type: binary or text */ 97 uLong adler; /* adler32 value of the uncompressed data */ 98 uLong reserved; /* reserved for future use */ 99 } z_stream; 100 101 typedef z_stream FAR *z_streamp; 102 103 /* 104 The application must update next_in and avail_in when avail_in has dropped 105 to zero. It must update next_out and avail_out when avail_out has dropped 106 to zero. The application must initialize zalloc, zfree and opaque before 107 calling the init function. All other fields are set by the compression 108 library and must not be updated by the application. 109 110 The opaque value provided by the application will be passed as the first 111 parameter for calls of zalloc and zfree. This can be useful for custom 112 memory management. The compression library attaches no meaning to the 113 opaque value. 114 115 zalloc must return Z_NULL if there is not enough memory for the object. 116 If zlib is used in a multi-threaded application, zalloc and zfree must be 117 thread safe. 118 119 On 16-bit systems, the functions zalloc and zfree must be able to allocate 120 exactly 65536 bytes, but will not be required to allocate more than this if 121 the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers 122 returned by zalloc for objects of exactly 65536 bytes *must* have their 123 offset normalized to zero. The default allocation function provided by this 124 library ensures this (see zutil.c). To reduce memory requirements and avoid 125 any allocation of 64K objects, at the expense of compression ratio, compile 126 the library with -DMAX_WBITS=14 (see zconf.h). 127 128 The fields total_in and total_out can be used for statistics or progress 129 reports. After compression, total_in holds the total size of the 130 uncompressed data and may be saved for use in the decompressor (particularly 131 if the decompressor wants to decompress everything in a single step). 132 */ 133 134 /* constants */ 135 136 #define Z_NO_FLUSH 0 137 #define Z_PARTIAL_FLUSH 1 138 #define Z_SYNC_FLUSH 2 139 #define Z_FULL_FLUSH 3 140 #define Z_FINISH 4 141 #define Z_BLOCK 5 142 #define Z_TREES 6 143 /* Allowed flush values; see deflate() and inflate() below for details */ 144 145 #define Z_OK 0 146 #define Z_STREAM_END 1 147 #define Z_NEED_DICT 2 148 #define Z_ERRNO (-1) 149 #define Z_STREAM_ERROR (-2) 150 #define Z_DATA_ERROR (-3) 151 #define Z_MEM_ERROR (-4) 152 #define Z_BUF_ERROR (-5) 153 #define Z_VERSION_ERROR (-6) 154 /* Return codes for the compression/decompression functions. Negative values 155 * are errors, positive values are used for special but normal events. 156 */ 157 158 #define Z_NO_COMPRESSION 0 159 #define Z_BEST_SPEED 1 160 #define Z_BEST_COMPRESSION 9 161 #define Z_DEFAULT_COMPRESSION (-1) 162 /* compression levels */ 163 164 #define Z_FILTERED 1 165 #define Z_HUFFMAN_ONLY 2 166 #define Z_RLE 3 167 #define Z_FIXED 4 168 #define Z_DEFAULT_STRATEGY 0 169 /* compression strategy; see deflateInit2() below for details */ 170 171 #define Z_BINARY 0 172 #define Z_TEXT 1 173 #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ 174 #define Z_UNKNOWN 2 175 /* Possible values of the data_type field (though see inflate()) */ 176 177 #define Z_DEFLATED 8 178 /* The deflate compression method (the only one supported in this version) */ 179 180 #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 181 182 #define zlib_version zlibVersion() 183 /* for compatibility with versions < 1.0.2 */ 184 185 186 /* basic functions */ 187 188 //ZEXTERN const char * ZEXPORT zlibVersion OF((void)); 189 /* The application can compare zlibVersion and ZLIB_VERSION for consistency. 190 If the first character differs, the library code actually used is not 191 compatible with the zlib.h header file used by the application. This check 192 is automatically made by deflateInit and inflateInit. 193 */ 194 195 int deflateInit(z_streamp strm, int level); 196 197 /* 198 ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); 199 200 Initializes the internal stream state for compression. The fields 201 zalloc, zfree and opaque must be initialized before by the caller. If 202 zalloc and zfree are set to Z_NULL, deflateInit updates them to use default 203 allocation functions. 204 205 The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 206 1 gives best speed, 9 gives best compression, 0 gives no compression at all 207 (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION 208 requests a default compromise between speed and compression (currently 209 equivalent to level 6). 210 211 deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 212 memory, Z_STREAM_ERROR if level is not a valid compression level, or 213 Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible 214 with the version assumed by the caller (ZLIB_VERSION). msg is set to null 215 if there is no error message. deflateInit does not perform any compression: 216 this will be done by deflate(). 217 */ 218 219 220 int deflate(z_streamp strm, int flush); 221 /* 222 deflate compresses as much data as possible, and stops when the input 223 buffer becomes empty or the output buffer becomes full. It may introduce 224 some output latency (reading input without producing any output) except when 225 forced to flush. 226 227 The detailed semantics are as follows. deflate performs one or both of the 228 following actions: 229 230 - Compress more input starting at next_in and update next_in and avail_in 231 accordingly. If not all input can be processed (because there is not 232 enough room in the output buffer), next_in and avail_in are updated and 233 processing will resume at this point for the next call of deflate(). 234 235 - Provide more output starting at next_out and update next_out and avail_out 236 accordingly. This action is forced if the parameter flush is non zero. 237 Forcing flush frequently degrades the compression ratio, so this parameter 238 should be set only when necessary (in interactive applications). Some 239 output may be provided even if flush is not set. 240 241 Before the call of deflate(), the application should ensure that at least 242 one of the actions is possible, by providing more input and/or consuming more 243 output, and updating avail_in or avail_out accordingly; avail_out should 244 never be zero before the call. The application can consume the compressed 245 output when it wants, for example when the output buffer is full (avail_out 246 == 0), or after each call of deflate(). If deflate returns Z_OK and with 247 zero avail_out, it must be called again after making room in the output 248 buffer because there might be more output pending. 249 250 Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to 251 decide how much data to accumulate before producing output, in order to 252 maximize compression. 253 254 If the parameter flush is set to Z_SYNC_FLUSH, all pending output is 255 flushed to the output buffer and the output is aligned on a byte boundary, so 256 that the decompressor can get all input data available so far. (In 257 particular avail_in is zero after the call if enough output space has been 258 provided before the call.) Flushing may degrade compression for some 259 compression algorithms and so it should be used only when necessary. This 260 completes the current deflate block and follows it with an empty stored block 261 that is three bits plus filler bits to the next byte, followed by four bytes 262 (00 00 ff ff). 263 264 If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the 265 output buffer, but the output is not aligned to a byte boundary. All of the 266 input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. 267 This completes the current deflate block and follows it with an empty fixed 268 codes block that is 10 bits long. This assures that enough bytes are output 269 in order for the decompressor to finish the block before the empty fixed code 270 block. 271 272 If flush is set to Z_BLOCK, a deflate block is completed and emitted, as 273 for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to 274 seven bits of the current block are held to be written as the next byte after 275 the next deflate block is completed. In this case, the decompressor may not 276 be provided enough bits at this point in order to complete decompression of 277 the data provided so far to the compressor. It may need to wait for the next 278 block to be emitted. This is for advanced applications that need to control 279 the emission of deflate blocks. 280 281 If flush is set to Z_FULL_FLUSH, all output is flushed as with 282 Z_SYNC_FLUSH, and the compression state is reset so that decompression can 283 restart from this point if previous compressed data has been damaged or if 284 random access is desired. Using Z_FULL_FLUSH too often can seriously degrade 285 compression. 286 287 If deflate returns with avail_out == 0, this function must be called again 288 with the same value of the flush parameter and more output space (updated 289 avail_out), until the flush is complete (deflate returns with non-zero 290 avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that 291 avail_out is greater than six to avoid repeated flush markers due to 292 avail_out == 0 on return. 293 294 If the parameter flush is set to Z_FINISH, pending input is processed, 295 pending output is flushed and deflate returns with Z_STREAM_END if there was 296 enough output space; if deflate returns with Z_OK, this function must be 297 called again with Z_FINISH and more output space (updated avail_out) but no 298 more input data, until it returns with Z_STREAM_END or an error. After 299 deflate has returned Z_STREAM_END, the only possible operations on the stream 300 are deflateReset or deflateEnd. 301 302 Z_FINISH can be used immediately after deflateInit if all the compression 303 is to be done in a single step. In this case, avail_out must be at least the 304 value returned by deflateBound (see below). Then deflate is guaranteed to 305 return Z_STREAM_END. If not enough output space is provided, deflate will 306 not return Z_STREAM_END, and it must be called again as described above. 307 308 deflate() sets strm->adler to the adler32 checksum of all input read 309 so far (that is, total_in bytes). 310 311 deflate() may update strm->data_type if it can make a good guess about 312 the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered 313 binary. This field is only for information purposes and does not affect the 314 compression algorithm in any manner. 315 316 deflate() returns Z_OK if some progress has been made (more input 317 processed or more output produced), Z_STREAM_END if all input has been 318 consumed and all output has been produced (only when flush is set to 319 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 320 if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible 321 (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not 322 fatal, and deflate() can be called again with more input and more output 323 space to continue compressing. 324 */ 325 326 327 int deflateEnd(z_streamp strm); 328 /* 329 All dynamically allocated data structures for this stream are freed. 330 This function discards any unprocessed input and does not flush any pending 331 output. 332 333 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 334 stream state was inconsistent, Z_DATA_ERROR if the stream was freed 335 prematurely (some input or output was discarded). In the error case, msg 336 may be set but then points to a static string (which must not be 337 deallocated). 338 */ 339 340 int inflateInit(z_streamp strm); 341 342 /* 343 ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); 344 345 Initializes the internal stream state for decompression. The fields 346 next_in, avail_in, zalloc, zfree and opaque must be initialized before by 347 the caller. If next_in is not Z_NULL and avail_in is large enough (the 348 exact value depends on the compression method), inflateInit determines the 349 compression method from the zlib header and allocates all data structures 350 accordingly; otherwise the allocation will be deferred to the first call of 351 inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to 352 use default allocation functions. 353 354 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough 355 memory, Z_VERSION_ERROR if the zlib library version is incompatible with the 356 version assumed by the caller, or Z_STREAM_ERROR if the parameters are 357 invalid, such as a null pointer to the structure. msg is set to null if 358 there is no error message. inflateInit does not perform any decompression 359 apart from possibly reading the zlib header if present: actual decompression 360 will be done by inflate(). (So next_in and avail_in may be modified, but 361 next_out and avail_out are unused and unchanged.) The current implementation 362 of inflateInit() does not process any header information -- that is deferred 363 until inflate() is called. 364 */ 365 366 367 int inflate(z_streamp strm, int flush); 368 /* 369 inflate decompresses as much data as possible, and stops when the input 370 buffer becomes empty or the output buffer becomes full. It may introduce 371 some output latency (reading input without producing any output) except when 372 forced to flush. 373 374 The detailed semantics are as follows. inflate performs one or both of the 375 following actions: 376 377 - Decompress more input starting at next_in and update next_in and avail_in 378 accordingly. If not all input can be processed (because there is not 379 enough room in the output buffer), next_in is updated and processing will 380 resume at this point for the next call of inflate(). 381 382 - Provide more output starting at next_out and update next_out and avail_out 383 accordingly. inflate() provides as much output as possible, until there is 384 no more input data or no more space in the output buffer (see below about 385 the flush parameter). 386 387 Before the call of inflate(), the application should ensure that at least 388 one of the actions is possible, by providing more input and/or consuming more 389 output, and updating the next_* and avail_* values accordingly. The 390 application can consume the uncompressed output when it wants, for example 391 when the output buffer is full (avail_out == 0), or after each call of 392 inflate(). If inflate returns Z_OK and with zero avail_out, it must be 393 called again after making room in the output buffer because there might be 394 more output pending. 395 396 The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, 397 Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much 398 output as possible to the output buffer. Z_BLOCK requests that inflate() 399 stop if and when it gets to the next deflate block boundary. When decoding 400 the zlib or gzip format, this will cause inflate() to return immediately 401 after the header and before the first block. When doing a raw inflate, 402 inflate() will go ahead and process the first block, and will return when it 403 gets to the end of that block, or when it runs out of data. 404 405 The Z_BLOCK option assists in appending to or combining deflate streams. 406 Also to assist in this, on return inflate() will set strm->data_type to the 407 number of unused bits in the last byte taken from strm->next_in, plus 64 if 408 inflate() is currently decoding the last block in the deflate stream, plus 409 128 if inflate() returned immediately after decoding an end-of-block code or 410 decoding the complete header up to just before the first byte of the deflate 411 stream. The end-of-block will not be indicated until all of the uncompressed 412 data from that block has been written to strm->next_out. The number of 413 unused bits may in general be greater than seven, except when bit 7 of 414 data_type is set, in which case the number of unused bits will be less than 415 eight. data_type is set as noted here every time inflate() returns for all 416 flush options, and so can be used to determine the amount of currently 417 consumed input in bits. 418 419 The Z_TREES option behaves as Z_BLOCK does, but it also returns when the 420 end of each deflate block header is reached, before any actual data in that 421 block is decoded. This allows the caller to determine the length of the 422 deflate block header for later use in random access within a deflate block. 423 256 is added to the value of strm->data_type when inflate() returns 424 immediately after reaching the end of the deflate block header. 425 426 inflate() should normally be called until it returns Z_STREAM_END or an 427 error. However if all decompression is to be performed in a single step (a 428 single call of inflate), the parameter flush should be set to Z_FINISH. In 429 this case all pending input is processed and all pending output is flushed; 430 avail_out must be large enough to hold all of the uncompressed data for the 431 operation to complete. (The size of the uncompressed data may have been 432 saved by the compressor for this purpose.) The use of Z_FINISH is not 433 required to perform an inflation in one step. However it may be used to 434 inform inflate that a faster approach can be used for the single inflate() 435 call. Z_FINISH also informs inflate to not maintain a sliding window if the 436 stream completes, which reduces inflate's memory footprint. If the stream 437 does not complete, either because not all of the stream is provided or not 438 enough output space is provided, then a sliding window will be allocated and 439 inflate() can be called again to continue the operation as if Z_NO_FLUSH had 440 been used. 441 442 In this implementation, inflate() always flushes as much output as 443 possible to the output buffer, and always uses the faster approach on the 444 first call. So the effects of the flush parameter in this implementation are 445 on the return value of inflate() as noted below, when inflate() returns early 446 when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of 447 memory for a sliding window when Z_FINISH is used. 448 449 If a preset dictionary is needed after this call (see inflateSetDictionary 450 below), inflate sets strm->adler to the Adler-32 checksum of the dictionary 451 chosen by the compressor and returns Z_NEED_DICT; otherwise it sets 452 strm->adler to the Adler-32 checksum of all output produced so far (that is, 453 total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described 454 below. At the end of the stream, inflate() checks that its computed adler32 455 checksum is equal to that saved by the compressor and returns Z_STREAM_END 456 only if the checksum is correct. 457 458 inflate() can decompress and check either zlib-wrapped or gzip-wrapped 459 deflate data. The header type is detected automatically, if requested when 460 initializing with inflateInit2(). Any information contained in the gzip 461 header is not retained, so applications that need that information should 462 instead use raw inflate, see inflateInit2() below, or inflateBack() and 463 perform their own processing of the gzip header and trailer. When processing 464 gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output 465 producted so far. The CRC-32 is checked against the gzip trailer. 466 467 inflate() returns Z_OK if some progress has been made (more input processed 468 or more output produced), Z_STREAM_END if the end of the compressed data has 469 been reached and all uncompressed output has been produced, Z_NEED_DICT if a 470 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was 471 corrupted (input stream not conforming to the zlib format or incorrect check 472 value), Z_STREAM_ERROR if the stream structure was inconsistent (for example 473 next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory, 474 Z_BUF_ERROR if no progress is possible or if there was not enough room in the 475 output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and 476 inflate() can be called again with more input and more output space to 477 continue decompressing. If Z_DATA_ERROR is returned, the application may 478 then call inflateSync() to look for a good compression block if a partial 479 recovery of the data is desired. 480 */ 481 482 483 int inflateEnd(z_streamp strm); 484 /* 485 All dynamically allocated data structures for this stream are freed. 486 This function discards any unprocessed input and does not flush any pending 487 output. 488 489 inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state 490 was inconsistent. In the error case, msg may be set but then points to a 491 static string (which must not be deallocated). 492 */ 493 494 /* checksum functions */ 495 496 /* 497 These functions are not related to compression but are exported 498 anyway because they might be useful in applications using the compression 499 library. 500 */ 501 502 uLong adler32(uLong adler, const Bytef *buf, uInt len); 503 /* 504 Update a running Adler-32 checksum with the bytes buf[0..len-1] and 505 return the updated checksum. If buf is Z_NULL, this function returns the 506 required initial value for the checksum. 507 508 An Adler-32 checksum is almost as reliable as a crc32_zlib but can be computed 509 much faster. 510 511 Usage example: 512 513 uLong adler = adler32(0L, Z_NULL, 0); 514 515 while (read_buffer(buffer, length) != EOF) { 516 adler = adler32(adler, buffer, length); 517 } 518 if (adler != original_adler) error(); 519 */ 520 521 /* various hacks, don't look :) */ 522 523 /* deflateInit and inflateInit are macros to allow checking the zlib version 524 * and the compiler's view of z_stream: 525 */ 526 int deflateInit_(z_streamp strm, int level, 527 const char *version, int stream_size); 528 int inflateInit_(z_streamp strm, 529 const char *version, int stream_size); 530 531 #define deflateInit(strm, level) \ 532 deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) 533 #define inflateInit(strm) \ 534 inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) 535 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ 536 deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ 537 (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) 538 #define inflateInit2(strm, windowBits) \ 539 inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ 540 (int)sizeof(z_stream)) 541 542 /* hack for buggy compilers */ 543 #if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) 544 struct internal_state {int dummy;}; 545 #endif 546 547 #ifdef __cplusplus 548 } 549 #endif 550 551 #endif /* ZLIB_H */ 552