1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25/* inflate.h -- internal inflate state definition
26 * Copyright (C) 1995-2016 Mark Adler
27 * For conditions of distribution and use, see copyright notice in zlib.h
28 */
29
30/* WARNING: this file should *not* be used by applications. It is
31   part of the implementation of the compression library and is
32   subject to change. Applications should only use zlib.h.
33 */
34
35/* define NO_GZIP when compiling if you want to disable gzip header and
36   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
37   the crc code when it is not needed.  For shared libraries, gzip decoding
38   should be left enabled. */
39#ifndef NO_GZIP
40#  define GUNZIP
41#endif
42
43/* Possible inflate modes between inflate() calls */
44typedef enum {
45    HEAD = 16180,   /* i: waiting for magic header */
46    FLAGS,      /* i: waiting for method and flags (gzip) */
47    TIME,       /* i: waiting for modification time (gzip) */
48    OS,         /* i: waiting for extra flags and operating system (gzip) */
49    EXLEN,      /* i: waiting for extra length (gzip) */
50    EXTRA,      /* i: waiting for extra bytes (gzip) */
51    NAME,       /* i: waiting for end of file name (gzip) */
52    COMMENT,    /* i: waiting for end of comment (gzip) */
53    HCRC,       /* i: waiting for header crc (gzip) */
54    DICTID,     /* i: waiting for dictionary check value */
55    DICT,       /* waiting for inflateSetDictionary() call */
56        TYPE,       /* i: waiting for type bits, including last-flag bit */
57        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
58        STORED,     /* i: waiting for stored size (length and complement) */
59        COPY_,      /* i/o: same as COPY below, but only first time in */
60        COPY,       /* i/o: waiting for input or output to copy stored block */
61        TABLE,      /* i: waiting for dynamic block table lengths */
62        LENLENS,    /* i: waiting for code length code lengths */
63        CODELENS,   /* i: waiting for length/lit and distance code lengths */
64            LEN_,       /* i: same as LEN below, but only first time in */
65            LEN,        /* i: waiting for length/lit/eob code */
66            LENEXT,     /* i: waiting for length extra bits */
67            DIST,       /* i: waiting for distance code */
68            DISTEXT,    /* i: waiting for distance extra bits */
69            MATCH,      /* o: waiting for output space to copy string */
70            LIT,        /* o: waiting for output space to write literal */
71    CHECK,      /* i: waiting for 32-bit check value */
72    LENGTH,     /* i: waiting for 32-bit length (gzip) */
73    DONE,       /* finished check, done -- remain here until reset */
74    BAD,        /* got a data error -- remain here until reset */
75    MEM,        /* got an inflate() memory error -- remain here until reset */
76    SYNC        /* looking for synchronization bytes to restart inflate() */
77} inflate_mode;
78
79/*
80    State transitions between above modes -
81
82    (most modes can go to BAD or MEM on error -- not shown for clarity)
83
84    Process header:
85        HEAD -> (gzip) or (zlib) or (raw)
86        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
87                  HCRC -> TYPE
88        (zlib) -> DICTID or TYPE
89        DICTID -> DICT -> TYPE
90        (raw) -> TYPEDO
91    Read deflate blocks:
92            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
93            STORED -> COPY_ -> COPY -> TYPE
94            TABLE -> LENLENS -> CODELENS -> LEN_
95            LEN_ -> LEN
96    Read deflate codes in fixed or dynamic block:
97                LEN -> LENEXT or LIT or TYPE
98                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
99                LIT -> LEN
100    Process trailer:
101        CHECK -> LENGTH -> DONE
102 */
103
104/* State maintained between inflate() calls -- approximately 7K bytes, not
105   including the allocated sliding window, which is up to 32K bytes. */
106struct inflate_state {
107    z_streamp strm;             /* pointer back to this zlib stream */
108    inflate_mode mode;          /* current inflate mode */
109    int last;                   /* true if processing last block */
110    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip,
111                                   bit 2 true to validate check value */
112    int havedict;               /* true if dictionary provided */
113    int flags;                  /* gzip header method and flags (0 if zlib) */
114    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
115    unsigned long check;        /* protected copy of check value */
116    unsigned long total;        /* protected copy of output count */
117    gz_headerp head;            /* where to save gzip header information */
118        /* sliding window */
119    unsigned wbits;             /* log base 2 of requested window size */
120    unsigned wsize;             /* window size or zero if not using window */
121    unsigned whave;             /* valid bytes in the window */
122    unsigned wnext;             /* window write index */
123    unsigned char FAR *window;  /* allocated sliding window, if needed */
124        /* bit accumulator */
125    unsigned long hold;         /* input bit accumulator */
126    unsigned bits;              /* number of bits in "in" */
127        /* for string and stored block copying */
128    unsigned length;            /* literal or length of data to copy */
129    unsigned offset;            /* distance back to copy string from */
130        /* for table and code decoding */
131    unsigned extra;             /* extra bits needed */
132        /* fixed and dynamic code tables */
133    code const FAR *lencode;    /* starting table for length/literal codes */
134    code const FAR *distcode;   /* starting table for distance codes */
135    unsigned lenbits;           /* index bits for lencode */
136    unsigned distbits;          /* index bits for distcode */
137        /* dynamic table building */
138    unsigned ncode;             /* number of code length code lengths */
139    unsigned nlen;              /* number of length code lengths */
140    unsigned ndist;             /* number of distance code lengths */
141    unsigned have;              /* number of code lengths in lens[] */
142    code FAR *next;             /* next available space in codes[] */
143    unsigned short lens[320];   /* temporary storage for code lengths */
144    unsigned short work[288];   /* work area for code table building */
145    code codes[ENOUGH];         /* space for code tables */
146    int sane;                   /* if false, allow invalid distance too far */
147    int back;                   /* bits back of last unprocessed length/lit */
148    unsigned was;               /* initial length of match */
149};
150