| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* zlib.h -- interface of the 'zlib' general purpose compression library | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 3 |   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  | 
 | 5 |   This software is provided 'as-is', without any express or implied | 
 | 6 |   warranty.  In no event will the authors be held liable for any damages | 
 | 7 |   arising from the use of this software. | 
 | 8 |  | 
 | 9 |   Permission is granted to anyone to use this software for any purpose, | 
 | 10 |   including commercial applications, and to alter it and redistribute it | 
 | 11 |   freely, subject to the following restrictions: | 
 | 12 |  | 
 | 13 |   1. The origin of this software must not be misrepresented; you must not | 
 | 14 |      claim that you wrote the original software. If you use this software | 
 | 15 |      in a product, an acknowledgment in the product documentation would be | 
 | 16 |      appreciated but is not required. | 
 | 17 |   2. Altered source versions must be plainly marked as such, and must not be | 
 | 18 |      misrepresented as being the original software. | 
 | 19 |   3. This notice may not be removed or altered from any source distribution. | 
 | 20 |  | 
 | 21 |   Jean-loup Gailly        Mark Adler | 
 | 22 |   jloup@gzip.org          madler@alumni.caltech.edu | 
 | 23 |  | 
 | 24 |  | 
 | 25 |   The data format used by the zlib library is described by RFCs (Request for | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 26 |   Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). | 
 | 28 | */ | 
 | 29 |  | 
 | 30 | #ifndef _ZLIB_H | 
 | 31 | #define _ZLIB_H | 
 | 32 |  | 
 | 33 | #include <linux/zconf.h> | 
 | 34 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 35 | /* zlib deflate based on ZLIB_VERSION "1.1.3" */ | 
 | 36 | /* zlib inflate based on ZLIB_VERSION "1.2.3" */ | 
 | 37 |  | 
 | 38 | /* | 
 | 39 |   This is a modified version of zlib for use inside the Linux kernel. | 
 | 40 |   The main changes are to perform all memory allocation in advance. | 
 | 41 |  | 
 | 42 |   Inflation Changes: | 
 | 43 |     * Z_PACKET_FLUSH is added and used by ppp_deflate. Before returning | 
 | 44 |       this checks there is no more input data available and the next data | 
 | 45 |       is a STORED block. It also resets the mode to be read for the next | 
 | 46 |       data, all as per PPP requirements. | 
 | 47 |     * Addition of zlib_inflateIncomp which copies incompressible data into | 
 | 48 |       the history window and adjusts the accoutning without calling | 
 | 49 |       zlib_inflate itself to inflate the data. | 
 | 50 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
 | 52 | /*  | 
 | 53 |      The 'zlib' compression library provides in-memory compression and | 
 | 54 |   decompression functions, including integrity checks of the uncompressed | 
 | 55 |   data.  This version of the library supports only one compression method | 
 | 56 |   (deflation) but other algorithms will be added later and will have the same | 
 | 57 |   stream interface. | 
 | 58 |  | 
 | 59 |      Compression can be done in a single step if the buffers are large | 
 | 60 |   enough (for example if an input file is mmap'ed), or can be done by | 
 | 61 |   repeated calls of the compression function.  In the latter case, the | 
 | 62 |   application must provide more input and/or consume the output | 
 | 63 |   (providing more output space) before each call. | 
 | 64 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 65 |      The compressed data format used by default by the in-memory functions is | 
 | 66 |   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped | 
 | 67 |   around a deflate stream, which is itself documented in RFC 1951. | 
 | 68 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 |      The library also supports reading and writing files in gzip (.gz) format | 
 | 70 |   with an interface similar to that of stdio. | 
 | 71 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 |      The library does not install any signal handler. The decoder checks | 
 | 78 |   the consistency of the compressed data, so the library should never | 
 | 79 |   crash even in case of corrupted input. | 
 | 80 | */ | 
 | 81 |  | 
 | 82 | struct internal_state; | 
 | 83 |  | 
 | 84 | typedef struct z_stream_s { | 
| Denys Vlasenko | 8336793 | 2007-09-30 17:56:49 -0700 | [diff] [blame] | 85 |     const Byte *next_in;   /* next input byte */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |     uInt     avail_in;  /* number of bytes available at next_in */ | 
 | 87 |     uLong    total_in;  /* total nb of input bytes read so far */ | 
 | 88 |  | 
 | 89 |     Byte    *next_out;  /* next output byte should be put there */ | 
 | 90 |     uInt     avail_out; /* remaining free space at next_out */ | 
 | 91 |     uLong    total_out; /* total nb of bytes output so far */ | 
 | 92 |  | 
 | 93 |     char     *msg;      /* last error message, NULL if no error */ | 
 | 94 |     struct internal_state *state; /* not visible by applications */ | 
 | 95 |  | 
 | 96 |     void     *workspace; /* memory allocated for this stream */ | 
 | 97 |  | 
 | 98 |     int     data_type;  /* best guess about the data type: ascii or binary */ | 
 | 99 |     uLong   adler;      /* adler32 value of the uncompressed data */ | 
 | 100 |     uLong   reserved;   /* reserved for future use */ | 
 | 101 | } z_stream; | 
 | 102 |  | 
 | 103 | typedef z_stream *z_streamp; | 
 | 104 |  | 
 | 105 | /* | 
 | 106 |    The application must update next_in and avail_in when avail_in has | 
 | 107 |    dropped to zero. It must update next_out and avail_out when avail_out | 
 | 108 |    has dropped to zero. The application must initialize zalloc, zfree and | 
 | 109 |    opaque before calling the init function. All other fields are set by the | 
 | 110 |    compression library and must not be updated by the application. | 
 | 111 |  | 
 | 112 |    The opaque value provided by the application will be passed as the first | 
 | 113 |    parameter for calls of zalloc and zfree. This can be useful for custom | 
 | 114 |    memory management. The compression library attaches no meaning to the | 
 | 115 |    opaque value. | 
 | 116 |  | 
 | 117 |    zalloc must return NULL if there is not enough memory for the object. | 
 | 118 |    If zlib is used in a multi-threaded application, zalloc and zfree must be | 
 | 119 |    thread safe. | 
 | 120 |  | 
 | 121 |    On 16-bit systems, the functions zalloc and zfree must be able to allocate | 
 | 122 |    exactly 65536 bytes, but will not be required to allocate more than this | 
 | 123 |    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, | 
 | 124 |    pointers returned by zalloc for objects of exactly 65536 bytes *must* | 
 | 125 |    have their offset normalized to zero. The default allocation function | 
 | 126 |    provided by this library ensures this (see zutil.c). To reduce memory | 
 | 127 |    requirements and avoid any allocation of 64K objects, at the expense of | 
 | 128 |    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). | 
 | 129 |  | 
 | 130 |    The fields total_in and total_out can be used for statistics or | 
 | 131 |    progress reports. After compression, total_in holds the total size of | 
 | 132 |    the uncompressed data and may be saved for use in the decompressor | 
 | 133 |    (particularly if the decompressor wants to decompress everything in | 
 | 134 |    a single step). | 
 | 135 | */ | 
 | 136 |  | 
 | 137 |                         /* constants */ | 
 | 138 |  | 
 | 139 | #define Z_NO_FLUSH      0 | 
 | 140 | #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ | 
 | 141 | #define Z_PACKET_FLUSH  2 | 
 | 142 | #define Z_SYNC_FLUSH    3 | 
 | 143 | #define Z_FULL_FLUSH    4 | 
 | 144 | #define Z_FINISH        5 | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 145 | #define Z_BLOCK         6 /* Only for inflate at present */ | 
 | 146 | /* Allowed flush values; see deflate() and inflate() below for details */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 |  | 
 | 148 | #define Z_OK            0 | 
 | 149 | #define Z_STREAM_END    1 | 
 | 150 | #define Z_NEED_DICT     2 | 
 | 151 | #define Z_ERRNO        (-1) | 
 | 152 | #define Z_STREAM_ERROR (-2) | 
 | 153 | #define Z_DATA_ERROR   (-3) | 
 | 154 | #define Z_MEM_ERROR    (-4) | 
 | 155 | #define Z_BUF_ERROR    (-5) | 
 | 156 | #define Z_VERSION_ERROR (-6) | 
 | 157 | /* Return codes for the compression/decompression functions. Negative | 
 | 158 |  * values are errors, positive values are used for special but normal events. | 
 | 159 |  */ | 
 | 160 |  | 
 | 161 | #define Z_NO_COMPRESSION         0 | 
 | 162 | #define Z_BEST_SPEED             1 | 
 | 163 | #define Z_BEST_COMPRESSION       9 | 
 | 164 | #define Z_DEFAULT_COMPRESSION  (-1) | 
 | 165 | /* compression levels */ | 
 | 166 |  | 
 | 167 | #define Z_FILTERED            1 | 
 | 168 | #define Z_HUFFMAN_ONLY        2 | 
 | 169 | #define Z_DEFAULT_STRATEGY    0 | 
 | 170 | /* compression strategy; see deflateInit2() below for details */ | 
 | 171 |  | 
 | 172 | #define Z_BINARY   0 | 
 | 173 | #define Z_ASCII    1 | 
 | 174 | #define Z_UNKNOWN  2 | 
 | 175 | /* Possible values of the data_type field */ | 
 | 176 |  | 
 | 177 | #define Z_DEFLATED   8 | 
 | 178 | /* The deflate compression method (the only one supported in this version) */ | 
 | 179 |  | 
 | 180 |                         /* basic functions */ | 
 | 181 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | extern int zlib_deflate_workspacesize (void); | 
 | 183 | /* | 
 | 184 |    Returns the number of bytes that needs to be allocated for a per- | 
 | 185 |    stream workspace.  A pointer to this number of bytes should be | 
 | 186 |    returned in stream->workspace before calling zlib_deflateInit(). | 
 | 187 | */ | 
 | 188 |  | 
 | 189 | /*  | 
 | 190 | extern int deflateInit (z_streamp strm, int level); | 
 | 191 |  | 
 | 192 |      Initializes the internal stream state for compression. The fields | 
 | 193 |    zalloc, zfree and opaque must be initialized before by the caller. | 
 | 194 |    If zalloc and zfree are set to NULL, deflateInit updates them to | 
 | 195 |    use default allocation functions. | 
 | 196 |  | 
 | 197 |      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: | 
 | 198 |    1 gives best speed, 9 gives best compression, 0 gives no compression at | 
 | 199 |    all (the input data is simply copied a block at a time). | 
 | 200 |    Z_DEFAULT_COMPRESSION requests a default compromise between speed and | 
 | 201 |    compression (currently equivalent to level 6). | 
 | 202 |  | 
 | 203 |      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not | 
 | 204 |    enough memory, Z_STREAM_ERROR if level is not a valid compression level, | 
 | 205 |    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible | 
 | 206 |    with the version assumed by the caller (ZLIB_VERSION). | 
 | 207 |    msg is set to null if there is no error message.  deflateInit does not | 
 | 208 |    perform any compression: this will be done by deflate(). | 
 | 209 | */ | 
 | 210 |  | 
 | 211 |  | 
 | 212 | extern int zlib_deflate (z_streamp strm, int flush); | 
 | 213 | /* | 
 | 214 |     deflate compresses as much data as possible, and stops when the input | 
 | 215 |   buffer becomes empty or the output buffer becomes full. It may introduce some | 
 | 216 |   output latency (reading input without producing any output) except when | 
 | 217 |   forced to flush. | 
 | 218 |  | 
 | 219 |     The detailed semantics are as follows. deflate performs one or both of the | 
 | 220 |   following actions: | 
 | 221 |  | 
 | 222 |   - Compress more input starting at next_in and update next_in and avail_in | 
 | 223 |     accordingly. If not all input can be processed (because there is not | 
 | 224 |     enough room in the output buffer), next_in and avail_in are updated and | 
 | 225 |     processing will resume at this point for the next call of deflate(). | 
 | 226 |  | 
 | 227 |   - Provide more output starting at next_out and update next_out and avail_out | 
 | 228 |     accordingly. This action is forced if the parameter flush is non zero. | 
 | 229 |     Forcing flush frequently degrades the compression ratio, so this parameter | 
 | 230 |     should be set only when necessary (in interactive applications). | 
 | 231 |     Some output may be provided even if flush is not set. | 
 | 232 |  | 
 | 233 |   Before the call of deflate(), the application should ensure that at least | 
 | 234 |   one of the actions is possible, by providing more input and/or consuming | 
 | 235 |   more output, and updating avail_in or avail_out accordingly; avail_out | 
 | 236 |   should never be zero before the call. The application can consume the | 
 | 237 |   compressed output when it wants, for example when the output buffer is full | 
 | 238 |   (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK | 
 | 239 |   and with zero avail_out, it must be called again after making room in the | 
 | 240 |   output buffer because there might be more output pending. | 
 | 241 |  | 
 | 242 |     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is | 
 | 243 |   flushed to the output buffer and the output is aligned on a byte boundary, so | 
 | 244 |   that the decompressor can get all input data available so far. (In particular | 
 | 245 |   avail_in is zero after the call if enough output space has been provided | 
 | 246 |   before the call.)  Flushing may degrade compression for some compression | 
 | 247 |   algorithms and so it should be used only when necessary. | 
 | 248 |  | 
 | 249 |     If flush is set to Z_FULL_FLUSH, all output is flushed as with | 
 | 250 |   Z_SYNC_FLUSH, and the compression state is reset so that decompression can | 
 | 251 |   restart from this point if previous compressed data has been damaged or if | 
 | 252 |   random access is desired. Using Z_FULL_FLUSH too often can seriously degrade | 
 | 253 |   the compression. | 
 | 254 |  | 
 | 255 |     If deflate returns with avail_out == 0, this function must be called again | 
 | 256 |   with the same value of the flush parameter and more output space (updated | 
 | 257 |   avail_out), until the flush is complete (deflate returns with non-zero | 
 | 258 |   avail_out). | 
 | 259 |  | 
 | 260 |     If the parameter flush is set to Z_FINISH, pending input is processed, | 
 | 261 |   pending output is flushed and deflate returns with Z_STREAM_END if there | 
 | 262 |   was enough output space; if deflate returns with Z_OK, this function must be | 
 | 263 |   called again with Z_FINISH and more output space (updated avail_out) but no | 
 | 264 |   more input data, until it returns with Z_STREAM_END or an error. After | 
 | 265 |   deflate has returned Z_STREAM_END, the only possible operations on the | 
 | 266 |   stream are deflateReset or deflateEnd. | 
 | 267 |    | 
 | 268 |     Z_FINISH can be used immediately after deflateInit if all the compression | 
 | 269 |   is to be done in a single step. In this case, avail_out must be at least | 
 | 270 |   0.1% larger than avail_in plus 12 bytes.  If deflate does not return | 
 | 271 |   Z_STREAM_END, then it must be called again as described above. | 
 | 272 |  | 
 | 273 |     deflate() sets strm->adler to the adler32 checksum of all input read | 
 | 274 |   so far (that is, total_in bytes). | 
 | 275 |  | 
 | 276 |     deflate() may update data_type if it can make a good guess about | 
 | 277 |   the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered | 
 | 278 |   binary. This field is only for information purposes and does not affect | 
 | 279 |   the compression algorithm in any manner. | 
 | 280 |  | 
 | 281 |     deflate() returns Z_OK if some progress has been made (more input | 
 | 282 |   processed or more output produced), Z_STREAM_END if all input has been | 
 | 283 |   consumed and all output has been produced (only when flush is set to | 
 | 284 |   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example | 
 | 285 |   if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible | 
 | 286 |   (for example avail_in or avail_out was zero). | 
 | 287 | */ | 
 | 288 |  | 
 | 289 |  | 
 | 290 | extern int zlib_deflateEnd (z_streamp strm); | 
 | 291 | /* | 
 | 292 |      All dynamically allocated data structures for this stream are freed. | 
 | 293 |    This function discards any unprocessed input and does not flush any | 
 | 294 |    pending output. | 
 | 295 |  | 
 | 296 |      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the | 
 | 297 |    stream state was inconsistent, Z_DATA_ERROR if the stream was freed | 
 | 298 |    prematurely (some input or output was discarded). In the error case, | 
 | 299 |    msg may be set but then points to a static string (which must not be | 
 | 300 |    deallocated). | 
 | 301 | */ | 
 | 302 |  | 
 | 303 |  | 
 | 304 | extern int zlib_inflate_workspacesize (void); | 
 | 305 | /* | 
 | 306 |    Returns the number of bytes that needs to be allocated for a per- | 
 | 307 |    stream workspace.  A pointer to this number of bytes should be | 
 | 308 |    returned in stream->workspace before calling zlib_inflateInit(). | 
 | 309 | */ | 
 | 310 |  | 
 | 311 | /*  | 
 | 312 | extern int zlib_inflateInit (z_streamp strm); | 
 | 313 |  | 
 | 314 |      Initializes the internal stream state for decompression. The fields | 
 | 315 |    next_in, avail_in, and workspace must be initialized before by | 
 | 316 |    the caller. If next_in is not NULL and avail_in is large enough (the exact | 
 | 317 |    value depends on the compression method), inflateInit determines the | 
 | 318 |    compression method from the zlib header and allocates all data structures | 
 | 319 |    accordingly; otherwise the allocation will be deferred to the first call of | 
 | 320 |    inflate.  If zalloc and zfree are set to NULL, inflateInit updates them to | 
 | 321 |    use default allocation functions. | 
 | 322 |  | 
 | 323 |      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough | 
 | 324 |    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the | 
 | 325 |    version assumed by the caller.  msg is set to null if there is no error | 
 | 326 |    message. inflateInit does not perform any decompression apart from reading | 
 | 327 |    the zlib header if present: this will be done by inflate().  (So next_in and | 
 | 328 |    avail_in may be modified, but next_out and avail_out are unchanged.) | 
 | 329 | */ | 
 | 330 |  | 
 | 331 |  | 
 | 332 | extern int zlib_inflate (z_streamp strm, int flush); | 
 | 333 | /* | 
 | 334 |     inflate decompresses as much data as possible, and stops when the input | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 335 |   buffer becomes empty or the output buffer becomes full. It may introduce | 
 | 336 |   some output latency (reading input without producing any output) except when | 
 | 337 |   forced to flush. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 |  | 
 | 339 |   The detailed semantics are as follows. inflate performs one or both of the | 
 | 340 |   following actions: | 
 | 341 |  | 
 | 342 |   - Decompress more input starting at next_in and update next_in and avail_in | 
 | 343 |     accordingly. If not all input can be processed (because there is not | 
 | 344 |     enough room in the output buffer), next_in is updated and processing | 
 | 345 |     will resume at this point for the next call of inflate(). | 
 | 346 |  | 
 | 347 |   - Provide more output starting at next_out and update next_out and avail_out | 
 | 348 |     accordingly.  inflate() provides as much output as possible, until there | 
 | 349 |     is no more input data or no more space in the output buffer (see below | 
 | 350 |     about the flush parameter). | 
 | 351 |  | 
 | 352 |   Before the call of inflate(), the application should ensure that at least | 
 | 353 |   one of the actions is possible, by providing more input and/or consuming | 
 | 354 |   more output, and updating the next_* and avail_* values accordingly. | 
 | 355 |   The application can consume the uncompressed output when it wants, for | 
 | 356 |   example when the output buffer is full (avail_out == 0), or after each | 
 | 357 |   call of inflate(). If inflate returns Z_OK and with zero avail_out, it | 
 | 358 |   must be called again after making room in the output buffer because there | 
 | 359 |   might be more output pending. | 
 | 360 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 361 |     The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, | 
 | 362 |   Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much | 
 | 363 |   output as possible to the output buffer. Z_BLOCK requests that inflate() stop | 
 | 364 |   if and when it gets to the next deflate block boundary. When decoding the | 
 | 365 |   zlib or gzip format, this will cause inflate() to return immediately after | 
 | 366 |   the header and before the first block. When doing a raw inflate, inflate() | 
 | 367 |   will go ahead and process the first block, and will return when it gets to | 
 | 368 |   the end of that block, or when it runs out of data. | 
 | 369 |  | 
 | 370 |     The Z_BLOCK option assists in appending to or combining deflate streams. | 
 | 371 |   Also to assist in this, on return inflate() will set strm->data_type to the | 
 | 372 |   number of unused bits in the last byte taken from strm->next_in, plus 64 | 
 | 373 |   if inflate() is currently decoding the last block in the deflate stream, | 
 | 374 |   plus 128 if inflate() returned immediately after decoding an end-of-block | 
 | 375 |   code or decoding the complete header up to just before the first byte of the | 
 | 376 |   deflate stream. The end-of-block will not be indicated until all of the | 
 | 377 |   uncompressed data from that block has been written to strm->next_out.  The | 
 | 378 |   number of unused bits may in general be greater than seven, except when | 
 | 379 |   bit 7 of data_type is set, in which case the number of unused bits will be | 
 | 380 |   less than eight. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 |  | 
 | 382 |     inflate() should normally be called until it returns Z_STREAM_END or an | 
 | 383 |   error. However if all decompression is to be performed in a single step | 
 | 384 |   (a single call of inflate), the parameter flush should be set to | 
 | 385 |   Z_FINISH. In this case all pending input is processed and all pending | 
 | 386 |   output is flushed; avail_out must be large enough to hold all the | 
 | 387 |   uncompressed data. (The size of the uncompressed data may have been saved | 
 | 388 |   by the compressor for this purpose.) The next operation on this stream must | 
 | 389 |   be inflateEnd to deallocate the decompression state. The use of Z_FINISH | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 390 |   is never required, but can be used to inform inflate that a faster approach | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 |   may be used for the single inflate() call. | 
 | 392 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 393 |      In this implementation, inflate() always flushes as much output as | 
 | 394 |   possible to the output buffer, and always uses the faster approach on the | 
 | 395 |   first call. So the only effect of the flush parameter in this implementation | 
 | 396 |   is on the return value of inflate(), as noted below, or when it returns early | 
 | 397 |   because Z_BLOCK is used. | 
 | 398 |  | 
 | 399 |      If a preset dictionary is needed after this call (see inflateSetDictionary | 
 | 400 |   below), inflate sets strm->adler to the adler32 checksum of the dictionary | 
 | 401 |   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets | 
 | 402 |   strm->adler to the adler32 checksum of all output produced so far (that is, | 
 | 403 |   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described | 
 | 404 |   below. At the end of the stream, inflate() checks that its computed adler32 | 
 | 405 |   checksum is equal to that saved by the compressor and returns Z_STREAM_END | 
 | 406 |   only if the checksum is correct. | 
 | 407 |  | 
 | 408 |     inflate() will decompress and check either zlib-wrapped or gzip-wrapped | 
 | 409 |   deflate data.  The header type is detected automatically.  Any information | 
 | 410 |   contained in the gzip header is not retained, so applications that need that | 
 | 411 |   information should instead use raw inflate, see inflateInit2() below, or | 
 | 412 |   inflateBack() and perform their own processing of the gzip header and | 
 | 413 |   trailer. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 |  | 
 | 415 |     inflate() returns Z_OK if some progress has been made (more input processed | 
 | 416 |   or more output produced), Z_STREAM_END if the end of the compressed data has | 
 | 417 |   been reached and all uncompressed output has been produced, Z_NEED_DICT if a | 
 | 418 |   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 419 |   corrupted (input stream not conforming to the zlib format or incorrect check | 
 | 420 |   value), Z_STREAM_ERROR if the stream structure was inconsistent (for example | 
 | 421 |   if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, | 
 | 422 |   Z_BUF_ERROR if no progress is possible or if there was not enough room in the | 
 | 423 |   output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and | 
 | 424 |   inflate() can be called again with more input and more output space to | 
 | 425 |   continue decompressing. If Z_DATA_ERROR is returned, the application may then | 
 | 426 |   call inflateSync() to look for a good compression block if a partial recovery | 
 | 427 |   of the data is desired. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | */ | 
 | 429 |  | 
 | 430 |  | 
 | 431 | extern int zlib_inflateEnd (z_streamp strm); | 
 | 432 | /* | 
 | 433 |      All dynamically allocated data structures for this stream are freed. | 
 | 434 |    This function discards any unprocessed input and does not flush any | 
 | 435 |    pending output. | 
 | 436 |  | 
 | 437 |      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state | 
 | 438 |    was inconsistent. In the error case, msg may be set but then points to a | 
 | 439 |    static string (which must not be deallocated). | 
 | 440 | */ | 
 | 441 |  | 
 | 442 |                         /* Advanced functions */ | 
 | 443 |  | 
 | 444 | /* | 
 | 445 |     The following functions are needed only in some special applications. | 
 | 446 | */ | 
 | 447 |  | 
 | 448 | /*    | 
 | 449 | extern int deflateInit2 (z_streamp strm, | 
 | 450 |                                      int  level, | 
 | 451 |                                      int  method, | 
 | 452 |                                      int  windowBits, | 
 | 453 |                                      int  memLevel, | 
 | 454 |                                      int  strategy); | 
 | 455 |  | 
 | 456 |      This is another version of deflateInit with more compression options. The | 
 | 457 |    fields next_in, zalloc, zfree and opaque must be initialized before by | 
 | 458 |    the caller. | 
 | 459 |  | 
 | 460 |      The method parameter is the compression method. It must be Z_DEFLATED in | 
 | 461 |    this version of the library. | 
 | 462 |  | 
 | 463 |      The windowBits parameter is the base two logarithm of the window size | 
 | 464 |    (the size of the history buffer).  It should be in the range 8..15 for this | 
 | 465 |    version of the library. Larger values of this parameter result in better | 
 | 466 |    compression at the expense of memory usage. The default value is 15 if | 
 | 467 |    deflateInit is used instead. | 
 | 468 |  | 
 | 469 |      The memLevel parameter specifies how much memory should be allocated | 
 | 470 |    for the internal compression state. memLevel=1 uses minimum memory but | 
 | 471 |    is slow and reduces compression ratio; memLevel=9 uses maximum memory | 
 | 472 |    for optimal speed. The default value is 8. See zconf.h for total memory | 
 | 473 |    usage as a function of windowBits and memLevel. | 
 | 474 |  | 
 | 475 |      The strategy parameter is used to tune the compression algorithm. Use the | 
 | 476 |    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a | 
 | 477 |    filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no | 
 | 478 |    string match).  Filtered data consists mostly of small values with a | 
 | 479 |    somewhat random distribution. In this case, the compression algorithm is | 
 | 480 |    tuned to compress them better. The effect of Z_FILTERED is to force more | 
 | 481 |    Huffman coding and less string matching; it is somewhat intermediate | 
 | 482 |    between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects | 
 | 483 |    the compression ratio but not the correctness of the compressed output even | 
 | 484 |    if it is not set appropriately. | 
 | 485 |  | 
 | 486 |       deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | 
 | 487 |    memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid | 
 | 488 |    method). msg is set to null if there is no error message.  deflateInit2 does | 
 | 489 |    not perform any compression: this will be done by deflate(). | 
 | 490 | */ | 
 | 491 |                              | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 492 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | extern int zlib_deflateSetDictionary (z_streamp strm, | 
 | 494 | 						     const Byte *dictionary, | 
 | 495 | 						     uInt  dictLength); | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 496 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | /* | 
 | 498 |      Initializes the compression dictionary from the given byte sequence | 
 | 499 |    without producing any compressed output. This function must be called | 
 | 500 |    immediately after deflateInit, deflateInit2 or deflateReset, before any | 
 | 501 |    call of deflate. The compressor and decompressor must use exactly the same | 
 | 502 |    dictionary (see inflateSetDictionary). | 
 | 503 |  | 
 | 504 |      The dictionary should consist of strings (byte sequences) that are likely | 
 | 505 |    to be encountered later in the data to be compressed, with the most commonly | 
 | 506 |    used strings preferably put towards the end of the dictionary. Using a | 
 | 507 |    dictionary is most useful when the data to be compressed is short and can be | 
 | 508 |    predicted with good accuracy; the data can then be compressed better than | 
 | 509 |    with the default empty dictionary. | 
 | 510 |  | 
 | 511 |      Depending on the size of the compression data structures selected by | 
 | 512 |    deflateInit or deflateInit2, a part of the dictionary may in effect be | 
 | 513 |    discarded, for example if the dictionary is larger than the window size in | 
 | 514 |    deflate or deflate2. Thus the strings most likely to be useful should be | 
 | 515 |    put at the end of the dictionary, not at the front. | 
 | 516 |  | 
 | 517 |      Upon return of this function, strm->adler is set to the Adler32 value | 
 | 518 |    of the dictionary; the decompressor may later use this value to determine | 
 | 519 |    which dictionary has been used by the compressor. (The Adler32 value | 
 | 520 |    applies to the whole dictionary even if only a subset of the dictionary is | 
 | 521 |    actually used by the compressor.) | 
 | 522 |  | 
 | 523 |      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a | 
 | 524 |    parameter is invalid (such as NULL dictionary) or the stream state is | 
 | 525 |    inconsistent (for example if deflate has already been called for this stream | 
 | 526 |    or if the compression method is bsort). deflateSetDictionary does not | 
 | 527 |    perform any compression: this will be done by deflate(). | 
 | 528 | */ | 
 | 529 |  | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 530 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | extern int zlib_deflateCopy (z_streamp dest, z_streamp source); | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 532 | #endif | 
 | 533 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /* | 
 | 535 |      Sets the destination stream as a complete copy of the source stream. | 
 | 536 |  | 
 | 537 |      This function can be useful when several compression strategies will be | 
 | 538 |    tried, for example when there are several ways of pre-processing the input | 
 | 539 |    data with a filter. The streams that will be discarded should then be freed | 
 | 540 |    by calling deflateEnd.  Note that deflateCopy duplicates the internal | 
 | 541 |    compression state which can be quite large, so this strategy is slow and | 
 | 542 |    can consume lots of memory. | 
 | 543 |  | 
 | 544 |      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not | 
 | 545 |    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent | 
 | 546 |    (such as zalloc being NULL). msg is left unchanged in both source and | 
 | 547 |    destination. | 
 | 548 | */ | 
 | 549 |  | 
 | 550 | extern int zlib_deflateReset (z_streamp strm); | 
 | 551 | /* | 
 | 552 |      This function is equivalent to deflateEnd followed by deflateInit, | 
 | 553 |    but does not free and reallocate all the internal compression state. | 
 | 554 |    The stream will keep the same compression level and any other attributes | 
 | 555 |    that may have been set by deflateInit2. | 
 | 556 |  | 
 | 557 |       deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | 
 | 558 |    stream state was inconsistent (such as zalloc or state being NULL). | 
 | 559 | */ | 
 | 560 |  | 
| Linus Torvalds | 243393c | 2005-08-06 09:39:57 -0700 | [diff] [blame] | 561 | static inline unsigned long deflateBound(unsigned long s) | 
 | 562 | { | 
 | 563 | 	return s + ((s + 7) >> 3) + ((s + 63) >> 6) + 11; | 
 | 564 | } | 
 | 565 |  | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 566 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | extern int zlib_deflateParams (z_streamp strm, int level, int strategy); | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 568 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | /* | 
 | 570 |      Dynamically update the compression level and compression strategy.  The | 
 | 571 |    interpretation of level and strategy is as in deflateInit2.  This can be | 
 | 572 |    used to switch between compression and straight copy of the input data, or | 
 | 573 |    to switch to a different kind of input data requiring a different | 
 | 574 |    strategy. If the compression level is changed, the input available so far | 
 | 575 |    is compressed with the old level (and may be flushed); the new level will | 
 | 576 |    take effect only at the next call of deflate(). | 
 | 577 |  | 
 | 578 |      Before the call of deflateParams, the stream state must be set as for | 
 | 579 |    a call of deflate(), since the currently available input may have to | 
 | 580 |    be compressed and flushed. In particular, strm->avail_out must be non-zero. | 
 | 581 |  | 
 | 582 |      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source | 
 | 583 |    stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR | 
 | 584 |    if strm->avail_out was zero. | 
 | 585 | */ | 
 | 586 |  | 
 | 587 | /*    | 
 | 588 | extern int inflateInit2 (z_streamp strm, int  windowBits); | 
 | 589 |  | 
 | 590 |      This is another version of inflateInit with an extra parameter. The | 
 | 591 |    fields next_in, avail_in, zalloc, zfree and opaque must be initialized | 
 | 592 |    before by the caller. | 
 | 593 |  | 
 | 594 |      The windowBits parameter is the base two logarithm of the maximum window | 
 | 595 |    size (the size of the history buffer).  It should be in the range 8..15 for | 
 | 596 |    this version of the library. The default value is 15 if inflateInit is used | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 597 |    instead. windowBits must be greater than or equal to the windowBits value | 
 | 598 |    provided to deflateInit2() while compressing, or it must be equal to 15 if | 
 | 599 |    deflateInit2() was not used. If a compressed stream with a larger window | 
 | 600 |    size is given as input, inflate() will return with the error code | 
 | 601 |    Z_DATA_ERROR instead of trying to allocate a larger window. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 603 |      windowBits can also be -8..-15 for raw inflate. In this case, -windowBits | 
 | 604 |    determines the window size. inflate() will then process raw deflate data, | 
 | 605 |    not looking for a zlib or gzip header, not generating a check value, and not | 
 | 606 |    looking for any check values for comparison at the end of the stream. This | 
 | 607 |    is for use with other formats that use the deflate compressed data format | 
 | 608 |    such as zip.  Those formats provide their own check values. If a custom | 
 | 609 |    format is developed using the raw deflate format for compressed data, it is | 
 | 610 |    recommended that a check value such as an adler32 or a crc32 be applied to | 
 | 611 |    the uncompressed data as is done in the zlib, gzip, and zip formats.  For | 
 | 612 |    most applications, the zlib format should be used as is. Note that comments | 
 | 613 |    above on the use in deflateInit2() applies to the magnitude of windowBits. | 
 | 614 |  | 
 | 615 |      windowBits can also be greater than 15 for optional gzip decoding. Add | 
 | 616 |    32 to windowBits to enable zlib and gzip decoding with automatic header | 
 | 617 |    detection, or add 16 to decode only the gzip format (the zlib format will | 
 | 618 |    return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is | 
 | 619 |    a crc32 instead of an adler32. | 
 | 620 |  | 
 | 621 |      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | 
 | 622 |    memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg | 
 | 623 |    is set to null if there is no error message.  inflateInit2 does not perform | 
 | 624 |    any decompression apart from reading the zlib header if present: this will | 
 | 625 |    be done by inflate(). (So next_in and avail_in may be modified, but next_out | 
 | 626 |    and avail_out are unchanged.) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | */ | 
 | 628 |  | 
 | 629 | extern int zlib_inflateSetDictionary (z_streamp strm, | 
 | 630 | 						     const Byte *dictionary, | 
 | 631 | 						     uInt  dictLength); | 
 | 632 | /* | 
 | 633 |      Initializes the decompression dictionary from the given uncompressed byte | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 634 |    sequence. This function must be called immediately after a call of inflate, | 
 | 635 |    if that call returned Z_NEED_DICT. The dictionary chosen by the compressor | 
 | 636 |    can be determined from the adler32 value returned by that call of inflate. | 
 | 637 |    The compressor and decompressor must use exactly the same dictionary (see | 
 | 638 |    deflateSetDictionary).  For raw inflate, this function can be called | 
 | 639 |    immediately after inflateInit2() or inflateReset() and before any call of | 
 | 640 |    inflate() to set the dictionary.  The application must insure that the | 
 | 641 |    dictionary that was used for compression is provided. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 |  | 
 | 643 |      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a | 
 | 644 |    parameter is invalid (such as NULL dictionary) or the stream state is | 
 | 645 |    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 646 |    expected one (incorrect adler32 value). inflateSetDictionary does not | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 |    perform any decompression: this will be done by subsequent calls of | 
 | 648 |    inflate(). | 
 | 649 | */ | 
 | 650 |  | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 651 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | extern int zlib_inflateSync (z_streamp strm); | 
| Adrian Bunk | 87c2ce3 | 2006-01-09 20:54:07 -0800 | [diff] [blame] | 653 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /*  | 
 | 655 |     Skips invalid compressed data until a full flush point (see above the | 
 | 656 |   description of deflate with Z_FULL_FLUSH) can be found, or until all | 
 | 657 |   available input is skipped. No output is provided. | 
 | 658 |  | 
 | 659 |     inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR | 
 | 660 |   if no more input was provided, Z_DATA_ERROR if no flush point has been found, | 
 | 661 |   or Z_STREAM_ERROR if the stream structure was inconsistent. In the success | 
 | 662 |   case, the application may save the current current value of total_in which | 
 | 663 |   indicates where valid compressed data was found. In the error case, the | 
 | 664 |   application may repeatedly call inflateSync, providing more input each time, | 
 | 665 |   until success or end of the input data. | 
 | 666 | */ | 
 | 667 |  | 
 | 668 | extern int zlib_inflateReset (z_streamp strm); | 
 | 669 | /* | 
 | 670 |      This function is equivalent to inflateEnd followed by inflateInit, | 
 | 671 |    but does not free and reallocate all the internal decompression state. | 
 | 672 |    The stream will keep attributes that may have been set by inflateInit2. | 
 | 673 |  | 
 | 674 |       inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | 
 | 675 |    stream state was inconsistent (such as zalloc or state being NULL). | 
 | 676 | */ | 
 | 677 |  | 
 | 678 | extern int zlib_inflateIncomp (z_stream *strm); | 
 | 679 | /* | 
 | 680 |      This function adds the data at next_in (avail_in bytes) to the output | 
 | 681 |    history without performing any output.  There must be no pending output, | 
 | 682 |    and the decompressor must be expecting to see the start of a block. | 
 | 683 |    Calling this function is equivalent to decompressing a stored block | 
 | 684 |    containing the data at next_in (except that the data is not output). | 
 | 685 | */ | 
 | 686 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | #define zlib_deflateInit(strm, level) \ | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 688 | 	zlib_deflateInit2((strm), (level), Z_DEFLATED, MAX_WBITS, \ | 
 | 689 | 			      DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | #define zlib_inflateInit(strm) \ | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 691 | 	zlib_inflateInit2((strm), DEF_WBITS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 |  | 
| Richard Purdie | 4f3865f | 2006-06-22 14:47:34 -0700 | [diff] [blame] | 693 | extern int zlib_deflateInit2(z_streamp strm, int  level, int  method, | 
 | 694 |                                       int windowBits, int memLevel, | 
 | 695 |                                       int strategy); | 
 | 696 | extern int zlib_inflateInit2(z_streamp strm, int  windowBits); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 |  | 
 | 698 | #if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL) | 
 | 699 |     struct internal_state {int dummy;}; /* hack for buggy compilers */ | 
 | 700 | #endif | 
 | 701 |  | 
| Denys Vlasenko | 8336793 | 2007-09-30 17:56:49 -0700 | [diff] [blame] | 702 | /* Utility function: initialize zlib, unpack binary blob, clean up zlib, | 
 | 703 |  * return len or negative error code. */ | 
 | 704 | extern int zlib_inflate_blob(void *dst, unsigned dst_sz, const void *src, unsigned src_sz); | 
 | 705 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | #endif /* _ZLIB_H */ |