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