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