199767f8919635c4928607450d9e0abb932109ceToomas Soome/* inflate.h -- internal inflate state definition
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 1995-2009 Mark Adler
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* WARNING: this file should *not* be used by applications. It is
199767f8919635c4928607450d9e0abb932109ceToomas Soome part of the implementation of the compression library and is
199767f8919635c4928607450d9e0abb932109ceToomas Soome subject to change. Applications should only use zlib.h.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* define NO_GZIP when compiling if you want to disable gzip header and
199767f8919635c4928607450d9e0abb932109ceToomas Soome trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
199767f8919635c4928607450d9e0abb932109ceToomas Soome the crc code when it is not needed. For shared libraries, gzip decoding
199767f8919635c4928607450d9e0abb932109ceToomas Soome should be left enabled. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NO_GZIP
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define GUNZIP
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Possible inflate modes between inflate() calls */
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef enum {
199767f8919635c4928607450d9e0abb932109ceToomas Soome HEAD, /* i: waiting for magic header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome FLAGS, /* i: waiting for method and flags (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome TIME, /* i: waiting for modification time (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome OS, /* i: waiting for extra flags and operating system (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome EXLEN, /* i: waiting for extra length (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome EXTRA, /* i: waiting for extra bytes (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome NAME, /* i: waiting for end of file name (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome COMMENT, /* i: waiting for end of comment (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome HCRC, /* i: waiting for header crc (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DICTID, /* i: waiting for dictionary check value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DICT, /* waiting for inflateSetDictionary() call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome TYPE, /* i: waiting for type bits, including last-flag bit */
199767f8919635c4928607450d9e0abb932109ceToomas Soome TYPEDO, /* i: same, but skip check to exit inflate on new block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome STORED, /* i: waiting for stored size (length and complement) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY_, /* i/o: same as COPY below, but only first time in */
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY, /* i/o: waiting for input or output to copy stored block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome TABLE, /* i: waiting for dynamic block table lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome LENLENS, /* i: waiting for code length code lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CODELENS, /* i: waiting for length/lit and distance code lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome LEN_, /* i: same as LEN below, but only first time in */
199767f8919635c4928607450d9e0abb932109ceToomas Soome LEN, /* i: waiting for length/lit/eob code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome LENEXT, /* i: waiting for length extra bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DIST, /* i: waiting for distance code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DISTEXT, /* i: waiting for distance extra bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome MATCH, /* o: waiting for output space to copy string */
199767f8919635c4928607450d9e0abb932109ceToomas Soome LIT, /* o: waiting for output space to write literal */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CHECK, /* i: waiting for 32-bit check value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome LENGTH, /* i: waiting for 32-bit length (gzip) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DONE, /* finished check, done -- remain here until reset */
199767f8919635c4928607450d9e0abb932109ceToomas Soome BAD, /* got a data error -- remain here until reset */
199767f8919635c4928607450d9e0abb932109ceToomas Soome MEM, /* got an inflate() memory error -- remain here until reset */
199767f8919635c4928607450d9e0abb932109ceToomas Soome SYNC /* looking for synchronization bytes to restart inflate() */
199767f8919635c4928607450d9e0abb932109ceToomas Soome} inflate_mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome State transitions between above modes -
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome (most modes can go to BAD or MEM on error -- not shown for clarity)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Process header:
199767f8919635c4928607450d9e0abb932109ceToomas Soome HEAD -> (gzip) or (zlib) or (raw)
199767f8919635c4928607450d9e0abb932109ceToomas Soome (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
199767f8919635c4928607450d9e0abb932109ceToomas Soome HCRC -> TYPE
199767f8919635c4928607450d9e0abb932109ceToomas Soome (zlib) -> DICTID or TYPE
199767f8919635c4928607450d9e0abb932109ceToomas Soome DICTID -> DICT -> TYPE
199767f8919635c4928607450d9e0abb932109ceToomas Soome (raw) -> TYPEDO
199767f8919635c4928607450d9e0abb932109ceToomas Soome Read deflate blocks:
199767f8919635c4928607450d9e0abb932109ceToomas Soome TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
199767f8919635c4928607450d9e0abb932109ceToomas Soome STORED -> COPY_ -> COPY -> TYPE
199767f8919635c4928607450d9e0abb932109ceToomas Soome TABLE -> LENLENS -> CODELENS -> LEN_
199767f8919635c4928607450d9e0abb932109ceToomas Soome LEN_ -> LEN
199767f8919635c4928607450d9e0abb932109ceToomas Soome Read deflate codes in fixed or dynamic block:
199767f8919635c4928607450d9e0abb932109ceToomas Soome LEN -> LENEXT or LIT or TYPE
199767f8919635c4928607450d9e0abb932109ceToomas Soome LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
199767f8919635c4928607450d9e0abb932109ceToomas Soome LIT -> LEN
199767f8919635c4928607450d9e0abb932109ceToomas Soome Process trailer:
199767f8919635c4928607450d9e0abb932109ceToomas Soome CHECK -> LENGTH -> DONE
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* state maintained between inflate() calls. Approximately 10K bytes. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct inflate_state {
199767f8919635c4928607450d9e0abb932109ceToomas Soome inflate_mode mode; /* current inflate mode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int last; /* true if processing last block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int havedict; /* true if dictionary provided */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int flags; /* gzip header method and flags (0 if zlib) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long check; /* protected copy of check value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long total; /* protected copy of output count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome gz_headerp head; /* where to save gzip header information */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* sliding window */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned wbits; /* log base 2 of requested window size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned wsize; /* window size or zero if not using window */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned whave; /* valid bytes in the window */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned wnext; /* window write index */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned char FAR *window; /* allocated sliding window, if needed */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* bit accumulator */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long hold; /* input bit accumulator */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned bits; /* number of bits in "in" */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* for string and stored block copying */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned length; /* literal or length of data to copy */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned offset; /* distance back to copy string from */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* for table and code decoding */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned extra; /* extra bits needed */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* fixed and dynamic code tables */
199767f8919635c4928607450d9e0abb932109ceToomas Soome code const FAR *lencode; /* starting table for length/literal codes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome code const FAR *distcode; /* starting table for distance codes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned lenbits; /* index bits for lencode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned distbits; /* index bits for distcode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* dynamic table building */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned ncode; /* number of code length code lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned nlen; /* number of length code lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned ndist; /* number of distance code lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned have; /* number of code lengths in lens[] */
199767f8919635c4928607450d9e0abb932109ceToomas Soome code FAR *next; /* next available space in codes[] */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned short lens[320]; /* temporary storage for code lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned short work[288]; /* work area for code table building */
199767f8919635c4928607450d9e0abb932109ceToomas Soome code codes[ENOUGH]; /* space for code tables */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sane; /* if false, allow invalid distance too far */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int back; /* bits back of last unprocessed length/lit */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned was; /* initial length of match */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};