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