1221b1638SMasahiro Yamada /* inflate.h -- internal inflate state definition 2a194255dSDaniel Boulby * Copyright (C) 1995-2019 Mark Adler 3221b1638SMasahiro Yamada * For conditions of distribution and use, see copyright notice in zlib.h 4221b1638SMasahiro Yamada */ 5221b1638SMasahiro Yamada 6221b1638SMasahiro Yamada /* WARNING: this file should *not* be used by applications. It is 7221b1638SMasahiro Yamada part of the implementation of the compression library and is 8221b1638SMasahiro Yamada subject to change. Applications should only use zlib.h. 9221b1638SMasahiro Yamada */ 10221b1638SMasahiro Yamada 11221b1638SMasahiro Yamada /* define NO_GZIP when compiling if you want to disable gzip header and 12221b1638SMasahiro Yamada trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13221b1638SMasahiro Yamada the crc code when it is not needed. For shared libraries, gzip decoding 14221b1638SMasahiro Yamada should be left enabled. */ 15221b1638SMasahiro Yamada #ifndef NO_GZIP 16221b1638SMasahiro Yamada # define GUNZIP 17221b1638SMasahiro Yamada #endif 18221b1638SMasahiro Yamada 19221b1638SMasahiro Yamada /* Possible inflate modes between inflate() calls */ 20221b1638SMasahiro Yamada typedef enum { 21221b1638SMasahiro Yamada HEAD = 16180, /* i: waiting for magic header */ 22221b1638SMasahiro Yamada FLAGS, /* i: waiting for method and flags (gzip) */ 23221b1638SMasahiro Yamada TIME, /* i: waiting for modification time (gzip) */ 24221b1638SMasahiro Yamada OS, /* i: waiting for extra flags and operating system (gzip) */ 25221b1638SMasahiro Yamada EXLEN, /* i: waiting for extra length (gzip) */ 26221b1638SMasahiro Yamada EXTRA, /* i: waiting for extra bytes (gzip) */ 27221b1638SMasahiro Yamada NAME, /* i: waiting for end of file name (gzip) */ 28221b1638SMasahiro Yamada COMMENT, /* i: waiting for end of comment (gzip) */ 29221b1638SMasahiro Yamada HCRC, /* i: waiting for header crc (gzip) */ 30221b1638SMasahiro Yamada DICTID, /* i: waiting for dictionary check value */ 31221b1638SMasahiro Yamada DICT, /* waiting for inflateSetDictionary() call */ 32221b1638SMasahiro Yamada TYPE, /* i: waiting for type bits, including last-flag bit */ 33221b1638SMasahiro Yamada TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34221b1638SMasahiro Yamada STORED, /* i: waiting for stored size (length and complement) */ 35221b1638SMasahiro Yamada COPY_, /* i/o: same as COPY below, but only first time in */ 36221b1638SMasahiro Yamada COPY, /* i/o: waiting for input or output to copy stored block */ 37221b1638SMasahiro Yamada TABLE, /* i: waiting for dynamic block table lengths */ 38221b1638SMasahiro Yamada LENLENS, /* i: waiting for code length code lengths */ 39221b1638SMasahiro Yamada CODELENS, /* i: waiting for length/lit and distance code lengths */ 40221b1638SMasahiro Yamada LEN_, /* i: same as LEN below, but only first time in */ 41221b1638SMasahiro Yamada LEN, /* i: waiting for length/lit/eob code */ 42221b1638SMasahiro Yamada LENEXT, /* i: waiting for length extra bits */ 43221b1638SMasahiro Yamada DIST, /* i: waiting for distance code */ 44221b1638SMasahiro Yamada DISTEXT, /* i: waiting for distance extra bits */ 45221b1638SMasahiro Yamada MATCH, /* o: waiting for output space to copy string */ 46221b1638SMasahiro Yamada LIT, /* o: waiting for output space to write literal */ 47221b1638SMasahiro Yamada CHECK, /* i: waiting for 32-bit check value */ 48221b1638SMasahiro Yamada LENGTH, /* i: waiting for 32-bit length (gzip) */ 49221b1638SMasahiro Yamada DONE, /* finished check, done -- remain here until reset */ 50221b1638SMasahiro Yamada BAD, /* got a data error -- remain here until reset */ 51221b1638SMasahiro Yamada MEM, /* got an inflate() memory error -- remain here until reset */ 52221b1638SMasahiro Yamada SYNC /* looking for synchronization bytes to restart inflate() */ 53221b1638SMasahiro Yamada } inflate_mode; 54221b1638SMasahiro Yamada 55221b1638SMasahiro Yamada /* 56221b1638SMasahiro Yamada State transitions between above modes - 57221b1638SMasahiro Yamada 58221b1638SMasahiro Yamada (most modes can go to BAD or MEM on error -- not shown for clarity) 59221b1638SMasahiro Yamada 60221b1638SMasahiro Yamada Process header: 61221b1638SMasahiro Yamada HEAD -> (gzip) or (zlib) or (raw) 62221b1638SMasahiro Yamada (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 63221b1638SMasahiro Yamada HCRC -> TYPE 64221b1638SMasahiro Yamada (zlib) -> DICTID or TYPE 65221b1638SMasahiro Yamada DICTID -> DICT -> TYPE 66221b1638SMasahiro Yamada (raw) -> TYPEDO 67221b1638SMasahiro Yamada Read deflate blocks: 68221b1638SMasahiro Yamada TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 69221b1638SMasahiro Yamada STORED -> COPY_ -> COPY -> TYPE 70221b1638SMasahiro Yamada TABLE -> LENLENS -> CODELENS -> LEN_ 71221b1638SMasahiro Yamada LEN_ -> LEN 72221b1638SMasahiro Yamada Read deflate codes in fixed or dynamic block: 73221b1638SMasahiro Yamada LEN -> LENEXT or LIT or TYPE 74221b1638SMasahiro Yamada LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 75221b1638SMasahiro Yamada LIT -> LEN 76221b1638SMasahiro Yamada Process trailer: 77221b1638SMasahiro Yamada CHECK -> LENGTH -> DONE 78221b1638SMasahiro Yamada */ 79221b1638SMasahiro Yamada 80221b1638SMasahiro Yamada /* State maintained between inflate() calls -- approximately 7K bytes, not 81221b1638SMasahiro Yamada including the allocated sliding window, which is up to 32K bytes. */ 82221b1638SMasahiro Yamada struct inflate_state { 83221b1638SMasahiro Yamada z_streamp strm; /* pointer back to this zlib stream */ 84221b1638SMasahiro Yamada inflate_mode mode; /* current inflate mode */ 85221b1638SMasahiro Yamada int last; /* true if processing last block */ 86221b1638SMasahiro Yamada int wrap; /* bit 0 true for zlib, bit 1 true for gzip, 87221b1638SMasahiro Yamada bit 2 true to validate check value */ 88221b1638SMasahiro Yamada int havedict; /* true if dictionary provided */ 89a194255dSDaniel Boulby int flags; /* gzip header method and flags, 0 if zlib, or 90a194255dSDaniel Boulby -1 if raw or no header yet */ 91221b1638SMasahiro Yamada unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 92221b1638SMasahiro Yamada unsigned long check; /* protected copy of check value */ 93221b1638SMasahiro Yamada unsigned long total; /* protected copy of output count */ 94221b1638SMasahiro Yamada gz_headerp head; /* where to save gzip header information */ 95221b1638SMasahiro Yamada /* sliding window */ 96221b1638SMasahiro Yamada unsigned wbits; /* log base 2 of requested window size */ 97221b1638SMasahiro Yamada unsigned wsize; /* window size or zero if not using window */ 98221b1638SMasahiro Yamada unsigned whave; /* valid bytes in the window */ 99221b1638SMasahiro Yamada unsigned wnext; /* window write index */ 100221b1638SMasahiro Yamada unsigned char FAR *window; /* allocated sliding window, if needed */ 101221b1638SMasahiro Yamada /* bit accumulator */ 102221b1638SMasahiro Yamada unsigned long hold; /* input bit accumulator */ 103*a8c877cbSChris Kay unsigned bits; /* number of bits in hold */ 104221b1638SMasahiro Yamada /* for string and stored block copying */ 105221b1638SMasahiro Yamada unsigned length; /* literal or length of data to copy */ 106221b1638SMasahiro Yamada unsigned offset; /* distance back to copy string from */ 107221b1638SMasahiro Yamada /* for table and code decoding */ 108221b1638SMasahiro Yamada unsigned extra; /* extra bits needed */ 109221b1638SMasahiro Yamada /* fixed and dynamic code tables */ 110221b1638SMasahiro Yamada code const FAR *lencode; /* starting table for length/literal codes */ 111221b1638SMasahiro Yamada code const FAR *distcode; /* starting table for distance codes */ 112221b1638SMasahiro Yamada unsigned lenbits; /* index bits for lencode */ 113221b1638SMasahiro Yamada unsigned distbits; /* index bits for distcode */ 114221b1638SMasahiro Yamada /* dynamic table building */ 115221b1638SMasahiro Yamada unsigned ncode; /* number of code length code lengths */ 116221b1638SMasahiro Yamada unsigned nlen; /* number of length code lengths */ 117221b1638SMasahiro Yamada unsigned ndist; /* number of distance code lengths */ 118221b1638SMasahiro Yamada unsigned have; /* number of code lengths in lens[] */ 119221b1638SMasahiro Yamada code FAR *next; /* next available space in codes[] */ 120221b1638SMasahiro Yamada unsigned short lens[320]; /* temporary storage for code lengths */ 121221b1638SMasahiro Yamada unsigned short work[288]; /* work area for code table building */ 122221b1638SMasahiro Yamada code codes[ENOUGH]; /* space for code tables */ 123221b1638SMasahiro Yamada int sane; /* if false, allow invalid distance too far */ 124221b1638SMasahiro Yamada int back; /* bits back of last unprocessed length/lit */ 125221b1638SMasahiro Yamada unsigned was; /* initial length of match */ 126221b1638SMasahiro Yamada }; 127