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