1a79de952Smillert /* inflate.h -- internal inflate state definition 2a6530658Stb * Copyright (C) 1995-2019 Mark Adler 3a79de952Smillert * For conditions of distribution and use, see copyright notice in zlib.h 4a79de952Smillert */ 5a79de952Smillert 6a79de952Smillert /* WARNING: this file should *not* be used by applications. It is 7a79de952Smillert part of the implementation of the compression library and is 8a79de952Smillert subject to change. Applications should only use zlib.h. 9a79de952Smillert */ 10a79de952Smillert 11a79de952Smillert /* define NO_GZIP when compiling if you want to disable gzip header and 12a79de952Smillert trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13a79de952Smillert the crc code when it is not needed. For shared libraries, gzip decoding 14a79de952Smillert should be left enabled. */ 15a79de952Smillert #ifndef NO_GZIP 16a79de952Smillert # define GUNZIP 17a79de952Smillert #endif 18a79de952Smillert 19a79de952Smillert /* Possible inflate modes between inflate() calls */ 20a79de952Smillert typedef enum { 2136f395ceStb HEAD = 16180, /* i: waiting for magic header */ 22a79de952Smillert FLAGS, /* i: waiting for method and flags (gzip) */ 23a79de952Smillert TIME, /* i: waiting for modification time (gzip) */ 24a79de952Smillert OS, /* i: waiting for extra flags and operating system (gzip) */ 25a79de952Smillert EXLEN, /* i: waiting for extra length (gzip) */ 26a79de952Smillert EXTRA, /* i: waiting for extra bytes (gzip) */ 27a79de952Smillert NAME, /* i: waiting for end of file name (gzip) */ 28a79de952Smillert COMMENT, /* i: waiting for end of comment (gzip) */ 29a79de952Smillert HCRC, /* i: waiting for header crc (gzip) */ 30a79de952Smillert DICTID, /* i: waiting for dictionary check value */ 31a79de952Smillert DICT, /* waiting for inflateSetDictionary() call */ 32a79de952Smillert TYPE, /* i: waiting for type bits, including last-flag bit */ 33a79de952Smillert TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34a79de952Smillert STORED, /* i: waiting for stored size (length and complement) */ 3536f395ceStb COPY_, /* i/o: same as COPY below, but only first time in */ 36a79de952Smillert COPY, /* i/o: waiting for input or output to copy stored block */ 37a79de952Smillert TABLE, /* i: waiting for dynamic block table lengths */ 38a79de952Smillert LENLENS, /* i: waiting for code length code lengths */ 39a79de952Smillert CODELENS, /* i: waiting for length/lit and distance code lengths */ 4036f395ceStb LEN_, /* i: same as LEN below, but only first time in */ 4136f395ceStb LEN, /* i: waiting for length/lit/eob code */ 42a79de952Smillert LENEXT, /* i: waiting for length extra bits */ 43a79de952Smillert DIST, /* i: waiting for distance code */ 44a79de952Smillert DISTEXT, /* i: waiting for distance extra bits */ 45a79de952Smillert MATCH, /* o: waiting for output space to copy string */ 46a79de952Smillert LIT, /* o: waiting for output space to write literal */ 47a79de952Smillert CHECK, /* i: waiting for 32-bit check value */ 48a79de952Smillert LENGTH, /* i: waiting for 32-bit length (gzip) */ 49a79de952Smillert DONE, /* finished check, done -- remain here until reset */ 50a79de952Smillert BAD, /* got a data error -- remain here until reset */ 51a79de952Smillert MEM, /* got an inflate() memory error -- remain here until reset */ 52a79de952Smillert SYNC /* looking for synchronization bytes to restart inflate() */ 53a79de952Smillert } inflate_mode; 54a79de952Smillert 55a79de952Smillert /* 56a79de952Smillert State transitions between above modes - 57a79de952Smillert 5836f395ceStb (most modes can go to BAD or MEM on error -- not shown for clarity) 59a79de952Smillert 60a79de952Smillert Process header: 6136f395ceStb HEAD -> (gzip) or (zlib) or (raw) 6236f395ceStb (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 6336f395ceStb HCRC -> TYPE 64a79de952Smillert (zlib) -> DICTID or TYPE 65a79de952Smillert DICTID -> DICT -> TYPE 6636f395ceStb (raw) -> TYPEDO 67a79de952Smillert Read deflate blocks: 6836f395ceStb TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 6936f395ceStb STORED -> COPY_ -> COPY -> TYPE 7036f395ceStb TABLE -> LENLENS -> CODELENS -> LEN_ 7136f395ceStb LEN_ -> LEN 7236f395ceStb Read deflate codes in fixed or dynamic block: 73a79de952Smillert LEN -> LENEXT or LIT or TYPE 74a79de952Smillert LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 75a79de952Smillert LIT -> LEN 76a79de952Smillert Process trailer: 77a79de952Smillert CHECK -> LENGTH -> DONE 78a79de952Smillert */ 79a79de952Smillert 8036f395ceStb /* State maintained between inflate() calls -- approximately 7K bytes, not 8136f395ceStb including the allocated sliding window, which is up to 32K bytes. */ 82a79de952Smillert struct inflate_state { 8336f395ceStb z_streamp strm; /* pointer back to this zlib stream */ 84a79de952Smillert inflate_mode mode; /* current inflate mode */ 85a79de952Smillert int last; /* true if processing last block */ 8636f395ceStb int wrap; /* bit 0 true for zlib, bit 1 true for gzip, 8736f395ceStb bit 2 true to validate check value */ 88a79de952Smillert int havedict; /* true if dictionary provided */ 89a6530658Stb int flags; /* gzip header method and flags, 0 if zlib, or 90a6530658Stb -1 if raw or no header yet */ 91d76b9bfaSmillert unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 92a79de952Smillert unsigned long check; /* protected copy of check value */ 93a79de952Smillert unsigned long total; /* protected copy of output count */ 94d76b9bfaSmillert gz_headerp head; /* where to save gzip header information */ 95a79de952Smillert /* sliding window */ 96a79de952Smillert unsigned wbits; /* log base 2 of requested window size */ 97a79de952Smillert unsigned wsize; /* window size or zero if not using window */ 98a79de952Smillert unsigned whave; /* valid bytes in the window */ 9936f395ceStb unsigned wnext; /* window write index */ 100a79de952Smillert unsigned char FAR *window; /* allocated sliding window, if needed */ 101a79de952Smillert /* bit accumulator */ 102a79de952Smillert unsigned long hold; /* input bit accumulator */ 103*e9d9a569Stb unsigned bits; /* number of bits in hold */ 104a79de952Smillert /* for string and stored block copying */ 105a79de952Smillert unsigned length; /* literal or length of data to copy */ 106a79de952Smillert unsigned offset; /* distance back to copy string from */ 107a79de952Smillert /* for table and code decoding */ 108a79de952Smillert unsigned extra; /* extra bits needed */ 109a79de952Smillert /* fixed and dynamic code tables */ 110a79de952Smillert code const FAR *lencode; /* starting table for length/literal codes */ 111a79de952Smillert code const FAR *distcode; /* starting table for distance codes */ 112a79de952Smillert unsigned lenbits; /* index bits for lencode */ 113a79de952Smillert unsigned distbits; /* index bits for distcode */ 114a79de952Smillert /* dynamic table building */ 115a79de952Smillert unsigned ncode; /* number of code length code lengths */ 116a79de952Smillert unsigned nlen; /* number of length code lengths */ 117a79de952Smillert unsigned ndist; /* number of distance code lengths */ 118a79de952Smillert unsigned have; /* number of code lengths in lens[] */ 119a79de952Smillert code FAR *next; /* next available space in codes[] */ 120a79de952Smillert unsigned short lens[320]; /* temporary storage for code lengths */ 121a79de952Smillert unsigned short work[288]; /* work area for code table building */ 122a79de952Smillert code codes[ENOUGH]; /* space for code tables */ 12336f395ceStb int sane; /* if false, allow invalid distance too far */ 12436f395ceStb int back; /* bits back of last unprocessed length/lit */ 12536f395ceStb unsigned was; /* initial length of match */ 126a79de952Smillert }; 127