File indexing completed on 2024-04-28 15:14:26

0001 /* inflate.h -- internal inflate state definition
0002  * Copyright (C) 1995-2009 Mark Adler
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 /* WARNING: this file should *not* be used by applications. It is
0007    part of the implementation of the compression library and is
0008    subject to change. Applications should only use zlib.h.
0009  */
0010 
0011 /* define NO_GZIP when compiling if you want to disable gzip header and
0012    trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
0013    the crc code when it is not needed.  For shared libraries, gzip decoding
0014    should be left enabled. */
0015 #ifndef NO_GZIP
0016 #  define GUNZIP
0017 #endif
0018 
0019 /* Possible inflate modes between inflate() calls */
0020 typedef enum {
0021     HEAD,       /* i: waiting for magic header */
0022     FLAGS,      /* i: waiting for method and flags (gzip) */
0023     TIME,       /* i: waiting for modification time (gzip) */
0024     OS,         /* i: waiting for extra flags and operating system (gzip) */
0025     EXLEN,      /* i: waiting for extra length (gzip) */
0026     EXTRA,      /* i: waiting for extra bytes (gzip) */
0027     NAME,       /* i: waiting for end of file name (gzip) */
0028     COMMENT,    /* i: waiting for end of comment (gzip) */
0029     HCRC,       /* i: waiting for header crc (gzip) */
0030     DICTID,     /* i: waiting for dictionary check value */
0031     DICT,       /* waiting for inflateSetDictionary() call */
0032         TYPE,       /* i: waiting for type bits, including last-flag bit */
0033         TYPEDO,     /* i: same, but skip check to exit inflate on new block */
0034         STORED,     /* i: waiting for stored size (length and complement) */
0035         COPY_,      /* i/o: same as COPY below, but only first time in */
0036         COPY,       /* i/o: waiting for input or output to copy stored block */
0037         TABLE,      /* i: waiting for dynamic block table lengths */
0038         LENLENS,    /* i: waiting for code length code lengths */
0039         CODELENS,   /* i: waiting for length/lit and distance code lengths */
0040             LEN_,       /* i: same as LEN below, but only first time in */
0041             LEN,        /* i: waiting for length/lit/eob code */
0042             LENEXT,     /* i: waiting for length extra bits */
0043             DIST,       /* i: waiting for distance code */
0044             DISTEXT,    /* i: waiting for distance extra bits */
0045             MATCH,      /* o: waiting for output space to copy string */
0046             LIT,        /* o: waiting for output space to write literal */
0047     CHECK,      /* i: waiting for 32-bit check value */
0048     LENGTH,     /* i: waiting for 32-bit length (gzip) */
0049     DONE,       /* finished check, done -- remain here until reset */
0050     BAD,        /* got a data error -- remain here until reset */
0051     MEM,        /* got an inflate() memory error -- remain here until reset */
0052     SYNC        /* looking for synchronization bytes to restart inflate() */
0053 } inflate_mode;
0054 
0055 /*
0056     State transitions between above modes -
0057 
0058     (most modes can go to BAD or MEM on error -- not shown for clarity)
0059 
0060     Process header:
0061         HEAD -> (gzip) or (zlib) or (raw)
0062         (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
0063                   HCRC -> TYPE
0064         (zlib) -> DICTID or TYPE
0065         DICTID -> DICT -> TYPE
0066         (raw) -> TYPEDO
0067     Read deflate blocks:
0068             TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
0069             STORED -> COPY_ -> COPY -> TYPE
0070             TABLE -> LENLENS -> CODELENS -> LEN_
0071             LEN_ -> LEN
0072     Read deflate codes in fixed or dynamic block:
0073                 LEN -> LENEXT or LIT or TYPE
0074                 LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
0075                 LIT -> LEN
0076     Process trailer:
0077         CHECK -> LENGTH -> DONE
0078  */
0079 
0080 /* state maintained between inflate() calls.  Approximately 10K bytes. */
0081 struct inflate_state {
0082     inflate_mode mode;          /* current inflate mode */
0083     int last;                   /* true if processing last block */
0084     int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
0085     int havedict;               /* true if dictionary provided */
0086     int flags;                  /* gzip header method and flags (0 if zlib) */
0087     unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
0088     unsigned long check;        /* protected copy of check value */
0089     unsigned long total;        /* protected copy of output count */
0090     gz_headerp head;            /* where to save gzip header information */
0091         /* sliding window */
0092     unsigned wbits;             /* log base 2 of requested window size */
0093     unsigned wsize;             /* window size or zero if not using window */
0094     unsigned whave;             /* valid bytes in the window */
0095     unsigned wnext;             /* window write index */
0096     unsigned char FAR *window;  /* allocated sliding window, if needed */
0097         /* bit accumulator */
0098     unsigned long hold;         /* input bit accumulator */
0099     unsigned bits;              /* number of bits in "in" */
0100         /* for string and stored block copying */
0101     unsigned length;            /* literal or length of data to copy */
0102     unsigned offset;            /* distance back to copy string from */
0103         /* for table and code decoding */
0104     unsigned extra;             /* extra bits needed */
0105         /* fixed and dynamic code tables */
0106     code const FAR *lencode;    /* starting table for length/literal codes */
0107     code const FAR *distcode;   /* starting table for distance codes */
0108     unsigned lenbits;           /* index bits for lencode */
0109     unsigned distbits;          /* index bits for distcode */
0110         /* dynamic table building */
0111     unsigned ncode;             /* number of code length code lengths */
0112     unsigned nlen;              /* number of length code lengths */
0113     unsigned ndist;             /* number of distance code lengths */
0114     unsigned have;              /* number of code lengths in lens[] */
0115     code FAR *next;             /* next available space in codes[] */
0116     unsigned short lens[320];   /* temporary storage for code lengths */
0117     unsigned short work[288];   /* work area for code table building */
0118     code codes[ENOUGH];         /* space for code tables */
0119     int sane;                   /* if false, allow invalid distance too far */
0120     int back;                   /* bits back of last unprocessed length/lit */
0121     unsigned was;               /* initial length of match */
0122 };