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