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