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