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