| 1 | /* inflate.h -- internal inflate state definition |
| 2 | * Copyright (C) 1995-2003 Mark Adler |
| 3 | * Copyright (C) 2022 Hiroshi Miura |
| 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. |
| 9 | */ |
| 10 | |
| 11 | /* Possible inflate modes between inflate() calls */ |
| 12 | typedef enum { |
| 13 | TYPE, /* i: waiting for type bits, including last-flag bit */ |
| 14 | TYPED0, |
| 15 | STORED, /* i: waiting for stored size (length and complement) */ |
| 16 | COPY_, |
| 17 | COPY, |
| 18 | TABLE, /* i: waiting for dynamic block table lengths */ |
| 19 | LENLENS, |
| 20 | CODELENS, |
| 21 | LEN_, |
| 22 | LEN, /* i: waiting for length/lit code */ |
| 23 | LENEXT, |
| 24 | DIST, |
| 25 | DISTEXT, |
| 26 | MATCH, |
| 27 | LIT, |
| 28 | CHECK, |
| 29 | DONE, /* finished check, done -- remain here until reset */ |
| 30 | BAD, /* got a data error -- remain here until reset */ |
| 31 | MEM |
| 32 | } inflate_mode; |
| 33 | |
| 34 | /* |
| 35 | State transitions between above modes - |
| 36 | |
| 37 | (most modes can go to the BAD mode -- not shown for clarity) |
| 38 | |
| 39 | Read deflate blocks: |
| 40 | TYPE -> STORED or TABLE or LEN or DONE |
| 41 | STORED -> TYPE |
| 42 | TABLE -> LENLENS -> CODELENS -> LEN |
| 43 | Read deflate codes: |
| 44 | LEN -> LEN or TYPE |
| 45 | */ |
| 46 | |
| 47 | /* state maintained between inflate() calls. Approximately 7K bytes. */ |
| 48 | struct inflate_state { |
| 49 | z_streamp strm; |
| 50 | inflate_mode mode; /* current inflate mode */ |
| 51 | int last; /* true if processing last block */ |
| 52 | int wrap; /* true if the window has wrapped */ |
| 53 | |
| 54 | unsigned long check; |
| 55 | unsigned long total; |
| 56 | /* sliding window */ /* sliding window */ |
| 57 | unsigned wbits; /* log base 2 of requested window size */ |
| 58 | unsigned wsize; /* window size or zero if not using window */ |
| 59 | unsigned whave; /* valid bytes in the window */ |
| 60 | unsigned wnext; /* window write index */ |
| 61 | unsigned char FAR *window; /* allocated sliding window, if needed */ |
| 62 | /* bit accumulator */ |
| 63 | unsigned long hold; /* input bit accumulator */ |
| 64 | unsigned bits; /* number of bits in "in" */ |
| 65 | /* for string and stored block copying */ |
| 66 | unsigned length; /* literal or length of data to copy */ |
| 67 | unsigned offset; /* distance back to copy string from */ |
| 68 | /* for table and code decoding */ |
| 69 | unsigned extra; /* extra bits needed */ |
| 70 | /* fixed and dynamic code tables */ |
| 71 | code const FAR *lencode; /* starting table for length/literal codes */ |
| 72 | code const FAR *distcode; /* starting table for distance codes */ |
| 73 | unsigned lenbits; /* index bits for lencode */ |
| 74 | unsigned distbits; /* index bits for distcode */ |
| 75 | |
| 76 | /* dynamic table building */ |
| 77 | unsigned ncode; /* number of code length code lengths */ |
| 78 | unsigned nlen; /* number of length code lengths */ |
| 79 | unsigned ndist; /* number of distance code lengths */ |
| 80 | unsigned have; /* number of code lengths in lens[] */ |
| 81 | code FAR *next; /* next available space in codes[] */ |
| 82 | unsigned short lens[320]; /* temporary storage for code lengths */ |
| 83 | unsigned short work[288]; /* work area for code table building */ |
| 84 | code codes[ENOUGH]; /* space for code tables */ |
| 85 | int sane; /* if false, allow invalid distance too far */ |
| 86 | int back; /* bits back of last unprocessed length/lit */ |
| 87 | unsigned was; /* initial length of match */ |
| 88 | }; |