kusano fc6ab3
/* inflate.h -- internal inflate state definition
kusano fc6ab3
 * Copyright (C) 1995-2009 Mark Adler
kusano fc6ab3
 * For conditions of distribution and use, see copyright notice in zlib.h
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
/* WARNING: this file should *not* be used by applications. It is
kusano fc6ab3
   part of the implementation of the compression library and is
kusano fc6ab3
   subject to change. Applications should only use zlib.h.
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
/* define NO_GZIP when compiling if you want to disable gzip header and
kusano fc6ab3
   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
kusano fc6ab3
   the crc code when it is not needed.  For shared libraries, gzip decoding
kusano fc6ab3
   should be left enabled. */
kusano fc6ab3
#ifndef NO_GZIP
kusano fc6ab3
#  define GUNZIP
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
/* Possible inflate modes between inflate() calls */
kusano fc6ab3
typedef enum {
kusano fc6ab3
    HEAD,       /* i: waiting for magic header */
kusano fc6ab3
    FLAGS,      /* i: waiting for method and flags (gzip) */
kusano fc6ab3
    TIME,       /* i: waiting for modification time (gzip) */
kusano fc6ab3
    OS,         /* i: waiting for extra flags and operating system (gzip) */
kusano fc6ab3
    EXLEN,      /* i: waiting for extra length (gzip) */
kusano fc6ab3
    EXTRA,      /* i: waiting for extra bytes (gzip) */
kusano fc6ab3
    NAME,       /* i: waiting for end of file name (gzip) */
kusano fc6ab3
    COMMENT,    /* i: waiting for end of comment (gzip) */
kusano fc6ab3
    HCRC,       /* i: waiting for header crc (gzip) */
kusano fc6ab3
    DICTID,     /* i: waiting for dictionary check value */
kusano fc6ab3
    DICT,       /* waiting for inflateSetDictionary() call */
kusano fc6ab3
        TYPE,       /* i: waiting for type bits, including last-flag bit */
kusano fc6ab3
        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
kusano fc6ab3
        STORED,     /* i: waiting for stored size (length and complement) */
kusano fc6ab3
        COPY_,      /* i/o: same as COPY below, but only first time in */
kusano fc6ab3
        COPY,       /* i/o: waiting for input or output to copy stored block */
kusano fc6ab3
        TABLE,      /* i: waiting for dynamic block table lengths */
kusano fc6ab3
        LENLENS,    /* i: waiting for code length code lengths */
kusano fc6ab3
        CODELENS,   /* i: waiting for length/lit and distance code lengths */
kusano fc6ab3
            LEN_,       /* i: same as LEN below, but only first time in */
kusano fc6ab3
            LEN,        /* i: waiting for length/lit/eob code */
kusano fc6ab3
            LENEXT,     /* i: waiting for length extra bits */
kusano fc6ab3
            DIST,       /* i: waiting for distance code */
kusano fc6ab3
            DISTEXT,    /* i: waiting for distance extra bits */
kusano fc6ab3
            MATCH,      /* o: waiting for output space to copy string */
kusano fc6ab3
            LIT,        /* o: waiting for output space to write literal */
kusano fc6ab3
    CHECK,      /* i: waiting for 32-bit check value */
kusano fc6ab3
    LENGTH,     /* i: waiting for 32-bit length (gzip) */
kusano fc6ab3
    DONE,       /* finished check, done -- remain here until reset */
kusano fc6ab3
    BAD,        /* got a data error -- remain here until reset */
kusano fc6ab3
    MEM,        /* got an inflate() memory error -- remain here until reset */
kusano fc6ab3
    SYNC        /* looking for synchronization bytes to restart inflate() */
kusano fc6ab3
} inflate_mode;
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
    State transitions between above modes -
kusano fc6ab3
kusano fc6ab3
    (most modes can go to BAD or MEM on error -- not shown for clarity)
kusano fc6ab3
kusano fc6ab3
    Process header:
kusano fc6ab3
        HEAD -> (gzip) or (zlib) or (raw)
kusano fc6ab3
        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
kusano fc6ab3
                  HCRC -> TYPE
kusano fc6ab3
        (zlib) -> DICTID or TYPE
kusano fc6ab3
        DICTID -> DICT -> TYPE
kusano fc6ab3
        (raw) -> TYPEDO
kusano fc6ab3
    Read deflate blocks:
kusano fc6ab3
            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
kusano fc6ab3
            STORED -> COPY_ -> COPY -> TYPE
kusano fc6ab3
            TABLE -> LENLENS -> CODELENS -> LEN_
kusano fc6ab3
            LEN_ -> LEN
kusano fc6ab3
    Read deflate codes in fixed or dynamic block:
kusano fc6ab3
                LEN -> LENEXT or LIT or TYPE
kusano fc6ab3
                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
kusano fc6ab3
                LIT -> LEN
kusano fc6ab3
    Process trailer:
kusano fc6ab3
        CHECK -> LENGTH -> DONE
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
/* state maintained between inflate() calls.  Approximately 10K bytes. */
kusano fc6ab3
struct inflate_state {
kusano fc6ab3
    inflate_mode mode;          /* current inflate mode */
kusano fc6ab3
    int last;                   /* true if processing last block */
kusano fc6ab3
    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
kusano fc6ab3
    int havedict;               /* true if dictionary provided */
kusano fc6ab3
    int flags;                  /* gzip header method and flags (0 if zlib) */
kusano fc6ab3
    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
kusano fc6ab3
    unsigned long check;        /* protected copy of check value */
kusano fc6ab3
    unsigned long total;        /* protected copy of output count */
kusano fc6ab3
    gz_headerp head;            /* where to save gzip header information */
kusano fc6ab3
        /* sliding window */
kusano fc6ab3
    unsigned wbits;             /* log base 2 of requested window size */
kusano fc6ab3
    unsigned wsize;             /* window size or zero if not using window */
kusano fc6ab3
    unsigned whave;             /* valid bytes in the window */
kusano fc6ab3
    unsigned wnext;             /* window write index */
kusano fc6ab3
    unsigned char FAR *window;  /* allocated sliding window, if needed */
kusano fc6ab3
        /* bit accumulator */
kusano fc6ab3
    unsigned long hold;         /* input bit accumulator */
kusano fc6ab3
    unsigned bits;              /* number of bits in "in" */
kusano fc6ab3
        /* for string and stored block copying */
kusano fc6ab3
    unsigned length;            /* literal or length of data to copy */
kusano fc6ab3
    unsigned offset;            /* distance back to copy string from */
kusano fc6ab3
        /* for table and code decoding */
kusano fc6ab3
    unsigned extra;             /* extra bits needed */
kusano fc6ab3
        /* fixed and dynamic code tables */
kusano fc6ab3
    code const FAR *lencode;    /* starting table for length/literal codes */
kusano fc6ab3
    code const FAR *distcode;   /* starting table for distance codes */
kusano fc6ab3
    unsigned lenbits;           /* index bits for lencode */
kusano fc6ab3
    unsigned distbits;          /* index bits for distcode */
kusano fc6ab3
        /* dynamic table building */
kusano fc6ab3
    unsigned ncode;             /* number of code length code lengths */
kusano fc6ab3
    unsigned nlen;              /* number of length code lengths */
kusano fc6ab3
    unsigned ndist;             /* number of distance code lengths */
kusano fc6ab3
    unsigned have;              /* number of code lengths in lens[] */
kusano fc6ab3
    code FAR *next;             /* next available space in codes[] */
kusano fc6ab3
    unsigned short lens[320];   /* temporary storage for code lengths */
kusano fc6ab3
    unsigned short work[288];   /* work area for code table building */
kusano fc6ab3
    code codes[ENOUGH];         /* space for code tables */
kusano fc6ab3
    int sane;                   /* if false, allow invalid distance too far */
kusano fc6ab3
    int back;                   /* bits back of last unprocessed length/lit */
kusano fc6ab3
    unsigned was;               /* initial length of match */
kusano fc6ab3
};