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