zlib 1.1.4


  1. z_stream_s
  .

'zlib' 1.1.4, 11 2002

Copyright (C) 1995-2005 Jean-loup Gailly Mark Adler

' ', - . - .

, , :

  1. ; , . , , .
  2. .
  3. - .
Jean-loup Gailly
jloup@gzip.org
Mark Adler
madler@alumni.caltech.edu , zlib, RFC (Request for Comments) 1950 1952 rfc1950.txt (zlib ), rfc1951.txt ( ) rfc1952.txt (gzip ).

zlib.h, - piaip , ..

http://www.zlib.org - zlib.


'zlib' , . (), .

, ( , ), . .

gzip (.gz) stdio.

- , . , , .


- . , ( , , ). , - .

int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
. sourceLen . , destLen , 0.1% sourceLen 12 . , destLen .
, .
compress :

int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);

. level , deflateInit. sourceLen . , destLen , 0.1% sourceLen 12 . , destLen .
compress2 :

int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

. sourceLen . , destLen , , . ( , .) , destLen .
, .
uncompress :

typedef voidp gzFile;

gzFile gzopen (const char *path, const char *mode);

gzip (.gz) . , fopen ("rb" "wb") ("wb9") : 'f' ("wb6f"), 'h' ("wb1h"). deflateInit2.
gzopen gzip, gzread , .
gzopen NULL, \. errno , .. 2 ( errno 0, zlib Z_MEM_ERROR).

gzFile gzdopen (int fd, const char *mode);

gzdopen() gzFile fd. open, dup, creat, pipe fileno ( , fopen). mode , gzopen.
gzclose gzFile, fd, fclose(fdopen(fd), mode). fd , gzdopen(dup(fd), mode).
gzdopen NULL, \.

int gzsetparams (gzFile file, int level, int strategy);

. deflateInit2.
gzsetparams :

int gzread (gzFile file, voidp buf, unsigned len);

. gzip, gzread .
gzread (0 -1 ).

int gzwrite (gzFile file, const voidp buf, unsigned len);

.
gzwrite , . (0 ).

int VA gzprintf (gzFile file, const char *format, ...);

, gzFile , , fprintf.
gzprintf , . (0 ).

int gzputs (gzFile file, const char *s);

( ) . .
gzputs , -1 .

char * gzgets (gzFile file, char *buf, int len);

, len-1 , , , , .
gzgets buf, Z_NULL in case of error.

int gzputc (gzFile file, int c);

c, unsigned char.
gzputc , -1 .

int gzgetc (gzFile file);

.
gzgetc -1 .

int gzflush (gzFile file, int flush);

. flush , deflate(). - zlib (. gzerror ).
gzflush Z_OK flush Z_FINISH .
gzflush , .. .

z_off_t gzseek (gzFile file, z_off_t offset, int whence);

gzread gzwrite . offset . whence , lseek(2); SEEK_END .
, , . , . gzseek , .
gzseek , , -1 , , .

int gzrewind (gzFile file);

"" . .
gzrewind(file) (int)gzseek(file, 0L, SEEK_SET)

z_off_t gztell (gzFile file);

gzread gzwrite . .
gztell(file) gzseek(file, 0L, SEEK_CUR)

int gzeof (gzFile file);

1 , 0.

int gzclose (gzFile file);

, (state) \.
zlib (. gzerror ).

const char * gzerror (gzFile file, int *errnum);

, . errnum zlib. , , errnum Z_ERRNO errno , .


const char * zlibVersion (void);
zlibVersion ZLIB_VERSION . , zlib.h, . deflateInit inflateInit.

int deflateInit (z_streamp strm, int level);

state. zalloc, zfree opaque . zalloc zfree Z_NULL, deflateInit .., .
level Z_DEFAULT_COMPRESSION, 0 9: 1 , 9 , 0 ( ).
Z_DEFAULT_COMPRESSION ( 6 level).
deflateInit : msg null, .
deflateInit , deflate().

int deflate (z_streamp strm, int flush);

deflate , , . (.. - ) , .
. deflate : deflate(), , (\) , .. , avail_in avail_out ; avail_out . , , , , (.. avail_out == 0), deflate(). deflate Z_OK avail_out, , , .. .

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-&gt adler adler32 (., total_in ).

deflate() data_type (Z_ASCII Z_BINARY). . .

deflate() :

int deflateEnd (z_streamp strm);

, . .

deflateEnd :

msg , ( ).

int inflateInit (z_streamp strm);

state . next_in, avail_in, zalloc, zfree opaque . next_in Z_NULL avail_in ( ), inflateInit zlib ; , inflate. zalloc zfree Z_NULL, inflateInit .., .

inflateInit :

msg NULL, - . inflateInit - , zlib , . inflate(). (.. next_in avail_in , next_out avail_out .)

int inflate (z_streamp strm, int flush);

inflate , , . ( - ) , flush .

. 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() :

Z_DATA_ERROR, inflateSync .

int inflateEnd (z_streamp strm);

. .

inflateEnd :

msg , ( ).


.

int deflateInit2 (z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy);
deflateInit . next_in, zalloc, zfree opaque .

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 .

deflateInit2 :

msg NULL . deflateInit2 - : deflate().

int deflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength);

- \. deflateInit, deflateInit2 deflateReset, deflate. (. inflateSetDictionary).

( ) . , . , ; , , .

deflateInit deflateInit2 , , , deflate deflate2. .. , , , .

strm->adler Adler32 ; , , . (Adler32 , .)

deflateSetDictionary :

deflateSetDictionary - , deflate().

int deflateCopy (z_streamp dest, z_streamp source);

.

, , , . , , deflateEnd. , deflateCopy state, , .

deflateCopy :

msg .

int deflateReset (z_streamp strm);

deflateEnd deflateInit, state. level deflateInit2.

deflateReset :

int deflateParams (z_streamp strm, int level, int strategy);

level strategy. deflateInit2. , . level , deflate().

deflateParams, state deflate(), . , strm->avail_out .

deflateParams :

int inflateInit2 (z_streamp strm, int windowBits);

inflateInit . next_in, avail_in, zalloc, zfree and opaque .

The windowBits parameter ( ). 8..15 . - 15, - inflateInit. , inflate() Z_DATA_ERROR .

inflateInit2 :

msg NULL, - . inflateInit2 - , zlib , . inflate(). (.. next_in avail_in , next_out avail_out .)

int inflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength);

. inflate, Z_NEED_DICT. , , Adler32 . (. deflateSetDictionary).

inflateSetDictionary :

inflateSetDictionary - , inflate().

int inflateSync (z_streamp strm);

(. deflate Z_FULL_FLUSH). . .

inflateSync :

total_in, . inflateSync, , , .

int inflateReset (z_streamp strm);

inflateEnd inflateInit, state. , inflateInit2.

inflateReset :


, , , .

uLong adler32 (uLong adler, const Bytef *buf, uInt len);
Adler-32 buf[0..len-1] . buf NULL, .

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 crc32 (uLong crc, const Bytef *buf, uInt len);
buf[0..len-1] . buf NULL, . :

     uLong crc = crc32(0L, Z_NULL, 0);

     while (read_buffer(buffer, length) != EOF) {
       crc = crc32(crc, buffer, length);
     }
     if (crc != original_crc) error();
   

struct z_stream_s

:
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 */

deflateInit inflateInit , , z_stream.

, :

const char * zError (int err);
int inflateSyncPoint (z_streamp z);
const uLongf * get_crc_table (void);

- 13 2002
- 22 2005