1N/A/* $Id: zlib.h,v 1.1 1999/03/23 03:21:58 paulus Exp $ */ 1N/A * distribution by Jean-loup Gailly and Mark Adler, with some additions 1N/A * by Paul Mackerras to aid in implementing Deflate compression and 1N/A * decompression for PPP packets. 1N/A/* zlib.h -- interface of the 'zlib' general purpose compression library 1N/A version 0.95, Aug 16th, 1995. 1N/A Copyright (C) 1995 Jean-loup Gailly and Mark Adler 1N/A This software is provided 'as-is', without any express or implied 1N/A warranty. In no event will the authors be held liable for any damages 1N/A arising from the use of this software. 1N/A Permission is granted to anyone to use this software for any purpose, 1N/A including commercial applications, and to alter it and redistribute it 1N/A freely, subject to the following restrictions: 1N/A 1. The origin of this software must not be misrepresented; you must not 1N/A claim that you wrote the original software. If you use this software 1N/A in a product, an acknowledgment in the product documentation would be 1N/A appreciated but is not required. 1N/A 2. Altered source versions must be plainly marked as such, and must not be 1N/A misrepresented as being the original software. 1N/A 3. This notice may not be removed or altered from any source distribution. 1N/A Jean-loup Gailly Mark Adler 1N/A gzip@prep.ai.mit.edu madler@alumni.caltech.edu 1N/A/* #include "zconf.h" */ /* included directly here */ 1N/A/* zconf.h -- configuration of the zlib compression library 1N/A * Copyright (C) 1995 Jean-loup Gailly. 1N/A * For conditions of distribution and use, see copyright notice in zlib.h 1N/A/* From: zconf.h,v 1.12 1995/05/03 17:27:12 jloup Exp */ 1N/A The library does not install any signal handler. It is recommended to 1N/A add at least a handler for SIGSEGV when decompressing; the library checks 1N/A the consistency of the input data whenever possible but may go nuts 1N/A for some forms of corrupted input. 1N/A * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 1N/A * than 64k bytes at a time (needed on systems with 16-bit int). 1N/A * Compile with -DUNALIGNED_OK if it is OK to access shorts or ints 1N/A * at addresses which are not a multiple of their size. 1N/A * Under DOS, -DFAR=far or -DFAR=__far may be needed. 1N/A/* Maximum value for memLevel in deflateInit2 */ 1N/A/* Maximum value for windowBits in deflateInit2 and inflateInit2 */ 1N/A/* The memory requirements for deflate are (in bytes): 1N/A 1 << (windowBits+2) + 1 << (memLevel+9) 1N/A that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 1N/A plus a few kilobytes for small objects. For example, if you want to reduce 1N/A the default memory requirements from 256K to 128K, compile with 1N/A make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 1N/A Of course this will generally degrade compression (there's no free lunch). 1N/A The memory requirements for inflate are (in bytes) 1 << windowBits 1N/A that is, 32K for windowBits=15 (default value) plus a few kilobytes 1N/A /* Type declarations */ 1N/A#
ifndef OF /* function prototypes */ 1N/Atypedef unsigned char Byte;
/* 8 bits */ 1N/Atypedef unsigned int uInt;
/* 16 bits or more */ 1N/Atypedef unsigned long uLong;
/* 32 bits or more */ 1N/A The 'zlib' compression library provides in-memory compression and 1N/A decompression functions, including integrity checks of the uncompressed 1N/A data. This version of the library supports only one compression method 1N/A (deflation) but other algorithms may be added later and will have the same 1N/A For compression the application must provide the output buffer and 1N/A may optionally provide the input buffer for optimization. For decompression, 1N/A the application must provide the input buffer and may optionally provide 1N/A the output buffer for optimization. 1N/A Compression can be done in a single step if the buffers are large 1N/A enough (for example if an input file is mmap'ed), or can be done by 1N/A repeated calls of the compression function. In the latter case, the 1N/A application must provide more input and/or consume the output 1N/A (providing more output space) before each call. 1N/A char *
msg;
/* last error message, NULL if no error */ 1N/A The application must update next_in and avail_in when avail_in has 1N/A dropped to zero. It must update next_out and avail_out when avail_out 1N/A has dropped to zero. The application must initialize zalloc, zfree and 1N/A opaque before calling the init function. All other fields are set by the 1N/A compression library and must not be updated by the application. 1N/A The opaque value provided by the application will be passed as the first 1N/A parameter for calls of zalloc and zfree. This can be useful for custom 1N/A memory management. The compression library attaches no meaning to the 1N/A zalloc must return Z_NULL if there is not enough memory for the object. 1N/A On 16-bit systems, the functions zalloc and zfree must be able to allocate 1N/A exactly 65536 bytes, but will not be required to allocate more than this 1N/A if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, 1N/A pointers returned by zalloc for objects of exactly 65536 bytes *must* 1N/A have their offset normalized to zero. The default allocation function 1N/A provided by this library ensures this (see zutil.c). To reduce memory 1N/A requirements and avoid any allocation of 64K objects, at the expense of 1N/A compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). 1N/A The fields total_in and total_out can be used for statistics or 1N/A progress reports. After compression, total_in holds the total size of 1N/A the uncompressed data and may be saved for use in the decompressor 1N/A (particularly if the decompressor wants to decompress everything in 1N/A/* See deflate() below for the usage of these constants */ 1N/A/* compression levels */ 1N/A/* Used to set the data_type field */ 1N/A#
define Z_NULL 0
/* for initializing zalloc, zfree, opaque */ 1N/A/* The application can compare zlib_version and ZLIB_VERSION for consistency. 1N/A If the first character differs, the library code actually used is 1N/A not compatible with the zlib.h header file used by the application. 1N/A /* basic functions */ 1N/A Initializes the internal stream state for compression. The fields 1N/A zalloc, zfree and opaque must be initialized before by the caller. 1N/A If zalloc and zfree are set to Z_NULL, deflateInit updates them to 1N/A use default allocation functions. 1N/A The compression level must be Z_DEFAULT_COMPRESSION, or between 1 and 9: 1N/A 1 gives best speed, 9 gives best compression. Z_DEFAULT_COMPRESSION requests 1N/A a default compromise between speed and compression (currently equivalent 1N/A deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 1N/A enough memory, Z_STREAM_ERROR if level is not a valid compression level. 1N/A msg is set to null if there is no error message. deflateInit does not 1N/A perform any compression: this will be done by deflate(). 1N/A Performs one or both of the following actions: 1N/A - Compress more input starting at next_in and update next_in and avail_in 1N/A accordingly. If not all input can be processed (because there is not 1N/A enough room in the output buffer), next_in and avail_in are updated and 1N/A processing will resume at this point for the next call of deflate(). 1N/A - Provide more output starting at next_out and update next_out and avail_out 1N/A accordingly. This action is forced if the parameter flush is non zero. 1N/A Forcing flush frequently degrades the compression ratio, so this parameter 1N/A should be set only when necessary (in interactive applications). 1N/A Some output may be provided even if flush is not set. 1N/A Before the call of deflate(), the application should ensure that at least 1N/A one of the actions is possible, by providing more input and/or consuming 1N/A more output, and updating avail_in or avail_out accordingly; avail_out 1N/A should never be zero before the call. The application can consume the 1N/A compressed output when it wants, for example when the output buffer is full 1N/A (avail_out == 0), or after each call of deflate(). 1N/A If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression 1N/A block is terminated and flushed to the output buffer so that the 1N/A decompressor can get all input data available so far. For method 9, a future 1N/A variant on method 8, the current block will be flushed but not terminated. 1N/A If flush is set to Z_FULL_FLUSH, the compression block is terminated, a 1N/A special marker is output and the compression dictionary is discarded; this 1N/A is useful to allow the decompressor to synchronize if one compressed block 1N/A has been damaged (see inflateSync below). Flushing degrades compression and 1N/A so should be used only when necessary. Using Z_FULL_FLUSH too often can 1N/A seriously degrade the compression. If deflate returns with avail_out == 0, 1N/A this function must be called again with the same value of the flush 1N/A parameter and more output space (updated avail_out), until the flush is 1N/A complete (deflate returns with non-zero avail_out). 1N/A If the parameter flush is set to Z_PACKET_FLUSH, the compression 1N/A block is terminated, and a zero-length stored block is output, 1N/A omitting the length bytes (the effect of this is that the 3-bit type 1N/A code 000 for a stored block is output, and the output is then 1N/A byte-aligned). This is designed for use at the end of a PPP packet. 1N/A In addition, if the current compression block contains all the data 1N/A since the last Z_PACKET_FLUSH, it is never output as a stored block. 1N/A If the current compression block output as a static or dynamic block 1N/A would not be at least `minCompression' bytes smaller than the 1N/A original data, then nothing is output for that block. (The type 1N/A code for the zero-length stored block is still output, resulting in 1N/A a single zero byte being output for the whole packet.) 1N/A `MinCompression' is a parameter to deflateInit2, or 0 if deflateInit 1N/A If the parameter flush is set to Z_FINISH, all pending input is processed, 1N/A all pending output is flushed and deflate returns with Z_STREAM_END if there 1N/A was enough output space; if deflate returns with Z_OK, this function must be 1N/A called again with Z_FINISH and more output space (updated avail_out) but no 1N/A more input data, until it returns with Z_STREAM_END or an error. After 1N/A deflate has returned Z_STREAM_END, the only possible operations on the 1N/A stream are deflateReset or deflateEnd. 1N/A Z_FINISH can be used immediately after deflateInit if all the compression 1N/A is to be done in a single step. In this case, avail_out must be at least 1N/A 0.1% larger than avail_in plus 12 bytes. If deflate does not return 1N/A Z_STREAM_END, then it must be called again as described above. 1N/A deflate() may update data_type if it can make a good guess about 1N/A the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered 1N/A binary. This field is only for information purposes and does not affect 1N/A the compression algorithm in any manner. 1N/A deflate() returns Z_OK if some progress has been made (more input 1N/A processed or more output produced), Z_STREAM_END if all input has been 1N/A consumed and all output has been produced (only when flush is set to 1N/A Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example 1N/A if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible. 1N/A All dynamically allocated data structures for this stream are freed. 1N/A This function discards any unprocessed input and does not flush any 1N/A deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the 1N/A stream state was inconsistent. In the error case, msg may be set 1N/A but then points to a static string (which must not be deallocated). 1N/A Initializes the internal stream state for decompression. The fields 1N/A zalloc and zfree must be initialized before by the caller. If zalloc and 1N/A zfree are set to Z_NULL, inflateInit updates them to use default allocation 1N/A inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not 1N/A enough memory. msg is set to null if there is no error message. 1N/A inflateInit does not perform any decompression: this will be done by 1N/A Performs one or both of the following actions: 1N/A - Decompress more input starting at next_in and update next_in and avail_in 1N/A accordingly. If not all input can be processed (because there is not 1N/A enough room in the output buffer), next_in is updated and processing 1N/A will resume at this point for the next call of inflate(). 1N/A - Provide more output starting at next_out and update next_out and avail_out 1N/A accordingly. inflate() always provides as much output as possible 1N/A (until there is no more input data or no more space in the output buffer). 1N/A Before the call of inflate(), the application should ensure that at least 1N/A one of the actions is possible, by providing more input and/or consuming 1N/A more output, and updating the next_* and avail_* values accordingly. 1N/A The application can consume the uncompressed output when it wants, for 1N/A example when the output buffer is full (avail_out == 0), or after each 1N/A If the parameter flush is set to Z_PARTIAL_FLUSH or Z_PACKET_FLUSH, 1N/A inflate flushes as much output as possible to the output buffer. The 1N/A flushing behavior of inflate is not specified for values of the flush 1N/A parameter other than Z_PARTIAL_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the 1N/A current implementation actually flushes as much output as possible 1N/A anyway. For Z_PACKET_FLUSH, inflate checks that once all the input data 1N/A has been consumed, it is expecting to see the length field of a stored 1N/A block; if not, it returns Z_DATA_ERROR. 1N/A inflate() should normally be called until it returns Z_STREAM_END or an 1N/A error. However if all decompression is to be performed in a single step 1N/A (a single call of inflate), the parameter flush should be set to 1N/A Z_FINISH. In this case all pending input is processed and all pending 1N/A output is flushed; avail_out must be large enough to hold all the 1N/A uncompressed data. (The size of the uncompressed data may have been saved 1N/A by the compressor for this purpose.) The next operation on this stream must 1N/A be inflateEnd to deallocate the decompression state. The use of Z_FINISH 1N/A is never required, but can be used to inform inflate that a faster routine 1N/A may be used for the single inflate() call. 1N/A inflate() returns Z_OK if some progress has been made (more input 1N/A processed or more output produced), Z_STREAM_END if the end of the 1N/A compressed data has been reached and all uncompressed output has been 1N/A produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if 1N/A the stream structure was inconsistent (for example if next_in or next_out 1N/A was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no 1N/A progress is possible or if there was not enough room in the output buffer 1N/A when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then 1N/A call inflateSync to look for a good compression block. */ 1N/A All dynamically allocated data structures for this stream are freed. 1N/A This function discards any unprocessed input and does not flush any 1N/A inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state 1N/A was inconsistent. In the error case, msg may be set but then points to a 1N/A static string (which must not be deallocated). 1N/A /* advanced functions */ 1N/A The following functions are needed only in some special applications. 1N/A This is another version of deflateInit with more compression options. The 1N/A fields next_in, zalloc and zfree must be initialized before by the caller. 1N/A The method parameter is the compression method. It must be 8 in this 1N/A version of the library. (Method 9 will allow a 64K history buffer and 1N/A partial block flushes.) 1N/A The windowBits parameter is the base two logarithm of the window size 1N/A (the size of the history buffer). It should be in the range 8..15 for this 1N/A version of the library (the value 16 will be allowed for method 9). Larger 1N/A values of this parameter result in better compression at the expense of 1N/A memory usage. The default value is 15 if deflateInit is used instead. 1N/A The memLevel parameter specifies how much memory should be allocated 1N/A for the internal compression state. memLevel=1 uses minimum memory but 1N/A is slow and reduces compression ratio; memLevel=9 uses maximum memory 1N/A for optimal speed. The default value is 8. See zconf.h for total memory 1N/A usage as a function of windowBits and memLevel. 1N/A The strategy parameter is used to tune the compression algorithm. Use 1N/A the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data 1N/A produced by a filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman 1N/A encoding only (no string match). Filtered data consists mostly of small 1N/A values with a somewhat random distribution. In this case, the 1N/A compression algorithm is tuned to compress them better. The strategy 1N/A parameter only affects the compression ratio but not the correctness of 1N/A the compressed output even if it is not set appropriately. 1N/A The minCompression parameter specifies the minimum reduction in size 1N/A required for a compressed block to be output when Z_PACKET_FLUSH is 1N/A used (see the description of deflate above). 1N/A If next_in is not null, the library will use this buffer to hold also 1N/A some history information; the buffer must either hold the entire input 1N/A data, or have at least 1<<(windowBits+1) bytes and be writable. If next_in 1N/A is null, the library will allocate its own history buffer (and leave next_in 1N/A null). next_out need not be provided here but must be provided by the 1N/A application for the next call of deflate(). 1N/A If the history buffer is provided by the application, next_in must 1N/A must never be changed by the application since the compressor maintains 1N/A information inside this buffer from call to call; the application 1N/A must provide more input only by increasing avail_in. next_in is always 1N/A reset by the library in this case. 1N/A deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was 1N/A not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as 1N/A an invalid method). msg is set to null if there is no error message. 1N/A deflateInit2 does not perform any compression: this will be done by 1N/A Sets the destination stream as a complete copy of the source stream. If 1N/A the source stream is using an application-supplied history buffer, a new 1N/A buffer is allocated for the destination stream. The compressed output 1N/A buffer is always application-supplied. It's the responsibility of the 1N/A application to provide the correct values of next_out and avail_out for the 1N/A next call of deflate. 1N/A This function is useful when several compression strategies will be 1N/A tried, for example when there are several ways of pre-processing the input 1N/A data with a filter. The streams that will be discarded should then be freed 1N/A by calling deflateEnd. Note that deflateCopy duplicates the internal 1N/A compression state which can be quite large, so this strategy is slow and 1N/A can consume lots of memory. 1N/A deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not 1N/A enough memory, Z_STREAM_ERROR if the source stream state was inconsistent 1N/A (such as zalloc being NULL). msg is left unchanged in both source and 1N/A This function is equivalent to deflateEnd followed by deflateInit, 1N/A but does not free and reallocate all the internal compression state. 1N/A The stream will keep the same compression level and any other attributes 1N/A that may have been set by deflateInit2. 1N/A deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 1N/A stream state was inconsistent (such as zalloc or state being NULL). 1N/A This is another version of inflateInit with more compression options. The 1N/A fields next_out, zalloc and zfree must be initialized before by the caller. 1N/A The windowBits parameter is the base two logarithm of the maximum window 1N/A size (the size of the history buffer). It should be in the range 8..15 for 1N/A this version of the library (the value 16 will be allowed soon). The 1N/A default value is 15 if inflateInit is used instead. If a compressed stream 1N/A with a larger window size is given as input, inflate() will return with 1N/A the error code Z_DATA_ERROR instead of trying to allocate a larger window. 1N/A If next_out is not null, the library will use this buffer for the history 1N/A buffer; the buffer must either be large enough to hold the entire output 1N/A data, or have at least 1<<windowBits bytes. If next_out is null, the 1N/A library will allocate its own buffer (and leave next_out null). next_in 1N/A need not be provided here but must be provided by the application for the 1N/A next call of inflate(). 1N/A If the history buffer is provided by the application, next_out must 1N/A never be changed by the application since the decompressor maintains 1N/A history information inside this buffer from call to call; the application 1N/A can only reset next_out to the beginning of the history buffer when 1N/A avail_out is zero and all output has been consumed. 1N/A inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was 1N/A not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as 1N/A windowBits < 8). msg is set to null if there is no error message. 1N/A inflateInit2 does not perform any decompression: this will be done by 1N/A Skips invalid compressed data until the special marker (see deflate() 1N/A above) can be found, or until all available input is skipped. No output 1N/A inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR 1N/A if no more input was provided, Z_DATA_ERROR if no marker has been found, 1N/A or Z_STREAM_ERROR if the stream structure was inconsistent. In the success 1N/A case, the application may save the current current value of total_in which 1N/A indicates where valid compressed data was found. In the error case, the 1N/A application may repeatedly call inflateSync, providing more input each time, 1N/A until success or end of the input data. 1N/A This function is equivalent to inflateEnd followed by inflateInit, 1N/A but does not free and reallocate all the internal decompression state. 1N/A The stream will keep attributes that may have been set by inflateInit2. 1N/A inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source 1N/A stream state was inconsistent (such as zalloc or state being NULL). 1N/A This function adds the data at next_in (avail_in bytes) to the output 1N/A history without performing any output. There must be no pending output, 1N/A and the decompressor must be expecting to see the start of a block. 1N/A Calling this function is equivalent to decompressing a stored block 1N/A containing the data at next_in (except that the data is not output). 1N/A /* checksum functions */ 1N/A This function is not related to compression but is exported 1N/A anyway because it might be useful in applications using the 1N/A compression library. 1N/A Update a running Adler-32 checksum with the bytes buf[0..len-1] and 1N/A return the updated checksum. If buf is NULL, this function returns 1N/A the required initial value for the checksum. 1N/A An Adler-32 checksum is almost as reliable as a CRC32 but can be computed 1N/A much faster. Usage example: 1N/A uLong adler = adler32(0L, Z_NULL, 0); 1N/A while (read_buffer(buffer, length) != EOF) { 1N/A adler = adler32(adler, buffer, length); 1N/A if (adler != original_adler) error();