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