Copyright (C) 1995-2005 Jean-loup Gailly Mark Adler
' ', - . - .
, , :
zlib.h, - piaip , ..
http://www.zlib.org - zlib.
, ( , ), . .
gzip (.gz) stdio.
- , . , , .
flush Z_SYNC_FLUSH, , , .. . ( , avail_in , .) , .
flush Z_FULL_FLUSH, , Z_SYNC_FLUSH, state .., , . Z_FULL_FLUSH .
deflate avail_out == 0, flush ( avail_out), (deflate avail_out).
flush Z_FINISH, , deflate Z_STREAM_END ; deflate Z_OK, Z_FINISH ( avail_out) , . Z_STREAM_END . deflate Z_STREAM_END, deflateReset deflateEnd.
Z_FINISH deflateInit . avail_out 0.1% avail_in 12 . deflate Z_STREAM_END, , .
deflate() strm-> adler adler32 (., total_in ).
deflate() data_type (Z_ASCII Z_BINARY). . .
deflate() :
msg , ( ).
msg NULL, - . inflateInit - , zlib , . inflate(). (.. next_in avail_in , next_out avail_out .)
. inflate :
inflate(), , , , , - next_* avail_*. , , (.. avail_out == 0), inflate(). inflate Z_OK avail_out, , , .
flush Z_SYNC_FLUSH, inflate , . inflate flush, Z_SYNC_FLUSH Z_FINISH, ,
inflate() , Z_STREAM_END . , , flush Z_FINISH. ; avail_out , . ( ). inflateEnd - state. Z_FINISH , inflate(), .
(. inflateSetDictionary ), inflate strm->adler adler32 Z_NEED_DICT; strm->adler adler32 (.. total_out ) Z_OK, Z_STREAM_END , . inflate() adler32 , Z_STREAM_END.
inflate() :
msg , ( ).
method . Z_DEFLATED.
windowBits ( ). 8..15 . . deflateInit 15.
memLevel state. memLevel=1 , ; memLevel=9 . - 8. windowBits memLevel zconf.h.
strategy . Z_DEFAULT_STRATEGY , Z_FILTERED , ( ), Z_HUFFMAN_ONLY ( ). - . - compress. Z_FILTERED ; - Z_DEFAULT Z_HUFFMAN_ONLY. strategy .
msg NULL . deflateInit2 - : deflate().
( ) . , . , ; , , .
deflateInit deflateInit2 , , , deflate deflate2. .. , , , .
strm->adler Adler32 ; , , . (Adler32 , .)
deflateSetDictionary - , deflate().
, , , . , , deflateEnd. , deflateCopy state, , .
msg .
deflateParams, state deflate(), . , strm->avail_out .
The windowBits parameter ( ). 8..15 . - 15, - inflateInit. , inflate() Z_DATA_ERROR .
msg NULL, - . inflateInit2 - , zlib , . inflate(). (.. next_in avail_in , next_out avail_out .)
inflateSetDictionary - , inflate().
total_in, . inflateSync, , , .
Adler-32 , CRC32, . :
uLong adler = adler32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
adler = adler32(adler, buffer, length);
}
if (adler != original_adler) error();
uLong crc = crc32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();
typedef struct z_stream_s {
Bytef *next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */
uLong total_in; /* total nb of input bytes read so far */
Bytef *next_out; /* next output byte should be put there */
uInt avail_out; /* remaining free space at next_out */
uLong total_out; /* total nb of bytes output so far */
char *msg; /* last error message, NULL if no error */
struct internal_state FAR *state; /* not visible by applications */
alloc_func zalloc; /* used to allocate the internal state */
free_func zfree; /* used to free the internal state */
voidpf opaque; /* private data object passed to zalloc and zfree */
int data_type; /* best guess about the data type: ascii or binary */
uLong adler; /* adler32 value of the uncompressed data */
uLong reserved; /* reserved for future use */
} z_stream ;
typedef z_stream FAR * z_streamp;
next_in avail_in
avail_in . next_out
avail_out avail_out .
zalloc, zfree
opaque init-.
.opaque, , zalloc zfree. . opaque .
zalloc Z_NULL . zlib , zalloc zfree .
16- , zalloc zfree 65536 , , MAXSEG_64K (.zconf.h). : MSDOS, , zalloc 65536 ** 0. (. zutil.c). 64K, , -DMAX_WBITS=14 (. zconf.h).
total_in total_out . , total_in ( , ).
#define Z_NO_FLUSH 0 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ #define Z_SYNC_FLUSH 2 #define Z_FULL_FLUSH 3 #define Z_FINISH 4 /* Allowed flush values ; see deflate() below for details */ #define Z_OK 0 #define Z_STREAM_END 1 #define Z_NEED_DICT 2 #define Z_ERRNO (-1) #define Z_STREAM_ERROR (-2) #define Z_DATA_ERROR (-3) #define Z_MEM_ERROR (-4) #define Z_BUF_ERROR (-5) #define Z_VERSION_ERROR (-6) /* Return codes for the compression/decompression functions. Negative * values are errors, positive values are used for special but normal events. */ #define Z_NO_COMPRESSION 0 #define Z_BEST_SPEED 1 #define Z_BEST_COMPRESSION 9 #define Z_DEFAULT_COMPRESSION (-1) /* compression levels */ #define Z_FILTERED 1 #define Z_HUFFMAN_ONLY 2 #define Z_DEFAULT_STRATEGY 0 /* compression strategy ; see deflateInit2() below for details */ #define Z_BINARY 0 #define Z_ASCII 1 #define Z_UNKNOWN 2 /* Possible values of the data_type field */ #define Z_DEFLATED 8 /* The deflate compression method (the only one supported in this version) */ #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ #define zlib_version zlibVersion() /* for compatibility with versions less than 1.0.2 */
, :