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