1131377Stjr/* inflate.h -- internal inflate state definition
2311285Sdelphij * Copyright (C) 1995-2016 Mark Adler
3131377Stjr * For conditions of distribution and use, see copyright notice in zlib.h
4131377Stjr */
5131377Stjr
6131377Stjr/* WARNING: this file should *not* be used by applications. It is
7131377Stjr   part of the implementation of the compression library and is
8131377Stjr   subject to change. Applications should only use zlib.h.
9131377Stjr */
10131377Stjr
11131377Stjr/* define NO_GZIP when compiling if you want to disable gzip header and
12131377Stjr   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
13131377Stjr   the crc code when it is not needed.  For shared libraries, gzip decoding
14131377Stjr   should be left enabled. */
15131377Stjr#ifndef NO_GZIP
16131377Stjr#  define GUNZIP
17131377Stjr#endif
18131377Stjr
19131377Stjr/* Possible inflate modes between inflate() calls */
20131377Stjrtypedef enum {
21311285Sdelphij    HEAD = 16180,   /* i: waiting for magic header */
22131377Stjr    FLAGS,      /* i: waiting for method and flags (gzip) */
23131377Stjr    TIME,       /* i: waiting for modification time (gzip) */
24131377Stjr    OS,         /* i: waiting for extra flags and operating system (gzip) */
25131377Stjr    EXLEN,      /* i: waiting for extra length (gzip) */
26131377Stjr    EXTRA,      /* i: waiting for extra bytes (gzip) */
27131377Stjr    NAME,       /* i: waiting for end of file name (gzip) */
28131377Stjr    COMMENT,    /* i: waiting for end of comment (gzip) */
29131377Stjr    HCRC,       /* i: waiting for header crc (gzip) */
30131377Stjr    DICTID,     /* i: waiting for dictionary check value */
31131377Stjr    DICT,       /* waiting for inflateSetDictionary() call */
32131377Stjr        TYPE,       /* i: waiting for type bits, including last-flag bit */
33131377Stjr        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
34131377Stjr        STORED,     /* i: waiting for stored size (length and complement) */
35205194Sdelphij        COPY_,      /* i/o: same as COPY below, but only first time in */
36131377Stjr        COPY,       /* i/o: waiting for input or output to copy stored block */
37131377Stjr        TABLE,      /* i: waiting for dynamic block table lengths */
38131377Stjr        LENLENS,    /* i: waiting for code length code lengths */
39131377Stjr        CODELENS,   /* i: waiting for length/lit and distance code lengths */
40205194Sdelphij            LEN_,       /* i: same as LEN below, but only first time in */
41205194Sdelphij            LEN,        /* i: waiting for length/lit/eob code */
42131377Stjr            LENEXT,     /* i: waiting for length extra bits */
43131377Stjr            DIST,       /* i: waiting for distance code */
44131377Stjr            DISTEXT,    /* i: waiting for distance extra bits */
45131377Stjr            MATCH,      /* o: waiting for output space to copy string */
46131377Stjr            LIT,        /* o: waiting for output space to write literal */
47131377Stjr    CHECK,      /* i: waiting for 32-bit check value */
48131377Stjr    LENGTH,     /* i: waiting for 32-bit length (gzip) */
49131377Stjr    DONE,       /* finished check, done -- remain here until reset */
50131377Stjr    BAD,        /* got a data error -- remain here until reset */
51131377Stjr    MEM,        /* got an inflate() memory error -- remain here until reset */
52131377Stjr    SYNC        /* looking for synchronization bytes to restart inflate() */
53131377Stjr} inflate_mode;
54131377Stjr
55131377Stjr/*
56131377Stjr    State transitions between above modes -
57131377Stjr
58205194Sdelphij    (most modes can go to BAD or MEM on error -- not shown for clarity)
59131377Stjr
60131377Stjr    Process header:
61205194Sdelphij        HEAD -> (gzip) or (zlib) or (raw)
62205194Sdelphij        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
63205194Sdelphij                  HCRC -> TYPE
64131377Stjr        (zlib) -> DICTID or TYPE
65131377Stjr        DICTID -> DICT -> TYPE
66205194Sdelphij        (raw) -> TYPEDO
67131377Stjr    Read deflate blocks:
68205194Sdelphij            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
69205194Sdelphij            STORED -> COPY_ -> COPY -> TYPE
70205194Sdelphij            TABLE -> LENLENS -> CODELENS -> LEN_
71205194Sdelphij            LEN_ -> LEN
72205194Sdelphij    Read deflate codes in fixed or dynamic block:
73131377Stjr                LEN -> LENEXT or LIT or TYPE
74131377Stjr                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
75131377Stjr                LIT -> LEN
76131377Stjr    Process trailer:
77131377Stjr        CHECK -> LENGTH -> DONE
78131377Stjr */
79131377Stjr
80311285Sdelphij/* State maintained between inflate() calls -- approximately 7K bytes, not
81311285Sdelphij   including the allocated sliding window, which is up to 32K bytes. */
82131377Stjrstruct inflate_state {
83311285Sdelphij    z_streamp strm;             /* pointer back to this zlib stream */
84131377Stjr    inflate_mode mode;          /* current inflate mode */
85131377Stjr    int last;                   /* true if processing last block */
86311285Sdelphij    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip,
87311285Sdelphij                                   bit 2 true to validate check value */
88131377Stjr    int havedict;               /* true if dictionary provided */
89131377Stjr    int flags;                  /* gzip header method and flags (0 if zlib) */
90157043Sdes    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
91131377Stjr    unsigned long check;        /* protected copy of check value */
92131377Stjr    unsigned long total;        /* protected copy of output count */
93157043Sdes    gz_headerp head;            /* where to save gzip header information */
94131377Stjr        /* sliding window */
95131377Stjr    unsigned wbits;             /* log base 2 of requested window size */
96131377Stjr    unsigned wsize;             /* window size or zero if not using window */
97131377Stjr    unsigned whave;             /* valid bytes in the window */
98205194Sdelphij    unsigned wnext;             /* window write index */
99131377Stjr    unsigned char FAR *window;  /* allocated sliding window, if needed */
100131377Stjr        /* bit accumulator */
101131377Stjr    unsigned long hold;         /* input bit accumulator */
102131377Stjr    unsigned bits;              /* number of bits in "in" */
103131377Stjr        /* for string and stored block copying */
104131377Stjr    unsigned length;            /* literal or length of data to copy */
105131377Stjr    unsigned offset;            /* distance back to copy string from */
106131377Stjr        /* for table and code decoding */
107131377Stjr    unsigned extra;             /* extra bits needed */
108131377Stjr        /* fixed and dynamic code tables */
109131377Stjr    code const FAR *lencode;    /* starting table for length/literal codes */
110131377Stjr    code const FAR *distcode;   /* starting table for distance codes */
111131377Stjr    unsigned lenbits;           /* index bits for lencode */
112131377Stjr    unsigned distbits;          /* index bits for distcode */
113131377Stjr        /* dynamic table building */
114131377Stjr    unsigned ncode;             /* number of code length code lengths */
115131377Stjr    unsigned nlen;              /* number of length code lengths */
116131377Stjr    unsigned ndist;             /* number of distance code lengths */
117131377Stjr    unsigned have;              /* number of code lengths in lens[] */
118131377Stjr    code FAR *next;             /* next available space in codes[] */
119131377Stjr    unsigned short lens[320];   /* temporary storage for code lengths */
120131377Stjr    unsigned short work[288];   /* work area for code table building */
121131377Stjr    code codes[ENOUGH];         /* space for code tables */
122205194Sdelphij    int sane;                   /* if false, allow invalid distance too far */
123205194Sdelphij    int back;                   /* bits back of last unprocessed length/lit */
124205194Sdelphij    unsigned was;               /* initial length of match */
125131377Stjr};
126