inflate.h revision 347244
117680Spst/* inflate.h -- internal inflate state definition
217680Spst * Copyright (C) 1995-2016 Mark Adler
317680Spst * For conditions of distribution and use, see copyright notice in zlib.h
417680Spst */
517680Spst
617680Spst/* WARNING: this file should *not* be used by applications. It is
717680Spst   part of the implementation of the compression library and is
817680Spst   subject to change. Applications should only use zlib.h.
917680Spst */
1017680Spst
1117680Spst/* define NO_GZIP when compiling if you want to disable gzip header and
1217680Spst   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
1317680Spst   the crc code when it is not needed.  For shared libraries, gzip decoding
1417680Spst   should be left enabled. */
1517680Spst#ifndef NO_GZIP
1617680Spst#  define GUNZIP
1717680Spst#endif
1817680Spst
1917680Spst/* Possible inflate modes between inflate() calls */
2017680Spsttypedef enum {
2117680Spst    HEAD = 16180,   /* i: waiting for magic header */
2217680Spst    FLAGS,      /* i: waiting for method and flags (gzip) */
2317680Spst    TIME,       /* i: waiting for modification time (gzip) */
2417680Spst    OS,         /* i: waiting for extra flags and operating system (gzip) */
2517680Spst    EXLEN,      /* i: waiting for extra length (gzip) */
2617680Spst    EXTRA,      /* i: waiting for extra bytes (gzip) */
2717680Spst    NAME,       /* i: waiting for end of file name (gzip) */
2817680Spst    COMMENT,    /* i: waiting for end of comment (gzip) */
2917680Spst    HCRC,       /* i: waiting for header crc (gzip) */
3017680Spst    DICTID,     /* i: waiting for dictionary check value */
3117680Spst    DICT,       /* waiting for inflateSetDictionary() call */
3217680Spst        TYPE,       /* i: waiting for type bits, including last-flag bit */
3317680Spst        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
3417680Spst        STORED,     /* i: waiting for stored size (length and complement) */
3517680Spst        COPY_,      /* i/o: same as COPY below, but only first time in */
3617680Spst        COPY,       /* i/o: waiting for input or output to copy stored block */
3717680Spst        TABLE,      /* i: waiting for dynamic block table lengths */
3817680Spst        LENLENS,    /* i: waiting for code length code lengths */
3917680Spst        CODELENS,   /* i: waiting for length/lit and distance code lengths */
4017680Spst            LEN_,       /* i: same as LEN below, but only first time in */
4117680Spst            LEN,        /* i: waiting for length/lit/eob code */
4217680Spst            LENEXT,     /* i: waiting for length extra bits */
4317680Spst            DIST,       /* i: waiting for distance code */
4417680Spst            DISTEXT,    /* i: waiting for distance extra bits */
4517680Spst            MATCH,      /* o: waiting for output space to copy string */
4617680Spst            LIT,        /* o: waiting for output space to write literal */
4717680Spst    CHECK,      /* i: waiting for 32-bit check value */
4817680Spst    LENGTH,     /* i: waiting for 32-bit length (gzip) */
4917680Spst    DONE,       /* finished check, done -- remain here until reset */
5017680Spst    BAD,        /* got a data error -- remain here until reset */
5117680Spst    MEM,        /* got an inflate() memory error -- remain here until reset */
5217680Spst    SYNC        /* looking for synchronization bytes to restart inflate() */
5317680Spst} inflate_mode;
5417680Spst
5517680Spst/*
5617680Spst    State transitions between above modes -
5717680Spst
5817680Spst    (most modes can go to BAD or MEM on error -- not shown for clarity)
5917680Spst
6017680Spst    Process header:
6117680Spst        HEAD -> (gzip) or (zlib) or (raw)
6217680Spst        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
6317680Spst                  HCRC -> TYPE
6417680Spst        (zlib) -> DICTID or TYPE
6517680Spst        DICTID -> DICT -> TYPE
6617680Spst        (raw) -> TYPEDO
6717680Spst    Read deflate blocks:
6817680Spst            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
69            STORED -> COPY_ -> COPY -> TYPE
70            TABLE -> LENLENS -> CODELENS -> LEN_
71            LEN_ -> LEN
72    Read deflate codes in fixed or dynamic block:
73                LEN -> LENEXT or LIT or TYPE
74                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
75                LIT -> LEN
76    Process trailer:
77        CHECK -> LENGTH -> DONE
78 */
79
80/* State maintained between inflate() calls -- approximately 7K bytes, not
81   including the allocated sliding window, which is up to 32K bytes. */
82struct inflate_state {
83    z_streamp strm;             /* pointer back to this zlib stream */
84    inflate_mode mode;          /* current inflate mode */
85    int last;                   /* true if processing last block */
86    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip,
87                                   bit 2 true to validate check value */
88    int havedict;               /* true if dictionary provided */
89    int flags;                  /* gzip header method and flags (0 if zlib) */
90    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
91    unsigned long check;        /* protected copy of check value */
92    unsigned long total;        /* protected copy of output count */
93    gz_headerp head;            /* where to save gzip header information */
94        /* sliding window */
95    unsigned wbits;             /* log base 2 of requested window size */
96    unsigned wsize;             /* window size or zero if not using window */
97    unsigned whave;             /* valid bytes in the window */
98    unsigned wnext;             /* window write index */
99    unsigned char FAR *window;  /* allocated sliding window, if needed */
100        /* bit accumulator */
101    unsigned long hold;         /* input bit accumulator */
102    unsigned bits;              /* number of bits in "in" */
103        /* for string and stored block copying */
104    unsigned length;            /* literal or length of data to copy */
105    unsigned offset;            /* distance back to copy string from */
106        /* for table and code decoding */
107    unsigned extra;             /* extra bits needed */
108        /* fixed and dynamic code tables */
109    code const FAR *lencode;    /* starting table for length/literal codes */
110    code const FAR *distcode;   /* starting table for distance codes */
111    unsigned lenbits;           /* index bits for lencode */
112    unsigned distbits;          /* index bits for distcode */
113        /* dynamic table building */
114    unsigned ncode;             /* number of code length code lengths */
115    unsigned nlen;              /* number of length code lengths */
116    unsigned ndist;             /* number of distance code lengths */
117    unsigned have;              /* number of code lengths in lens[] */
118    code FAR *next;             /* next available space in codes[] */
119    unsigned short lens[320];   /* temporary storage for code lengths */
120    unsigned short work[288];   /* work area for code table building */
121    code codes[ENOUGH];         /* space for code tables */
122    int sane;                   /* if false, allow invalid distance too far */
123    int back;                   /* bits back of last unprocessed length/lit */
124    unsigned was;               /* initial length of match */
125};
126