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