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