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