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