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