15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* inflate.h -- internal inflate state definition
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1995-2009 Mark Adler
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * For conditions of distribution and use, see copyright notice in zlib.h
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* WARNING: this file should *not* be used by applications. It is
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   part of the implementation of the compression library and is
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   subject to change. Applications should only use zlib.h.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* define NO_GZIP when compiling if you want to disable gzip header and
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   the crc code when it is not needed.  For shared libraries, gzip decoding
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   should be left enabled. */
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NO_GZIP
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#  define GUNZIP
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Possible inflate modes between inflate() calls */
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef enum {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HEAD,       /* i: waiting for magic header */
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FLAGS,      /* i: waiting for method and flags (gzip) */
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TIME,       /* i: waiting for modification time (gzip) */
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OS,         /* i: waiting for extra flags and operating system (gzip) */
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXLEN,      /* i: waiting for extra length (gzip) */
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXTRA,      /* i: waiting for extra bytes (gzip) */
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NAME,       /* i: waiting for end of file name (gzip) */
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    COMMENT,    /* i: waiting for end of comment (gzip) */
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HCRC,       /* i: waiting for header crc (gzip) */
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DICTID,     /* i: waiting for dictionary check value */
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DICT,       /* waiting for inflateSetDictionary() call */
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TYPE,       /* i: waiting for type bits, including last-flag bit */
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        STORED,     /* i: waiting for stored size (length and complement) */
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        COPY_,      /* i/o: same as COPY below, but only first time in */
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        COPY,       /* i/o: waiting for input or output to copy stored block */
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TABLE,      /* i: waiting for dynamic block table lengths */
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LENLENS,    /* i: waiting for code length code lengths */
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CODELENS,   /* i: waiting for length/lit and distance code lengths */
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            LEN_,       /* i: same as LEN below, but only first time in */
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            LEN,        /* i: waiting for length/lit/eob code */
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            LENEXT,     /* i: waiting for length extra bits */
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            DIST,       /* i: waiting for distance code */
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            DISTEXT,    /* i: waiting for distance extra bits */
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            MATCH,      /* o: waiting for output space to copy string */
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            LIT,        /* o: waiting for output space to write literal */
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK,      /* i: waiting for 32-bit check value */
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LENGTH,     /* i: waiting for 32-bit length (gzip) */
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DONE,       /* finished check, done -- remain here until reset */
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BAD,        /* got a data error -- remain here until reset */
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MEM,        /* got an inflate() memory error -- remain here until reset */
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SYNC        /* looking for synchronization bytes to restart inflate() */
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} inflate_mode;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    State transitions between above modes -
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    (most modes can go to BAD or MEM on error -- not shown for clarity)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Process header:
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        HEAD -> (gzip) or (zlib) or (raw)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  HCRC -> TYPE
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (zlib) -> DICTID or TYPE
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DICTID -> DICT -> TYPE
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (raw) -> TYPEDO
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Read deflate blocks:
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            STORED -> COPY_ -> COPY -> TYPE
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            TABLE -> LENLENS -> CODELENS -> LEN_
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            LEN_ -> LEN
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Read deflate codes in fixed or dynamic block:
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                LEN -> LENEXT or LIT or TYPE
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                LIT -> LEN
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Process trailer:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CHECK -> LENGTH -> DONE
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* state maintained between inflate() calls.  Approximately 10K bytes. */
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct inflate_state {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    inflate_mode mode;          /* current inflate mode */
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int last;                   /* true if processing last block */
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int havedict;               /* true if dictionary provided */
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int flags;                  /* gzip header method and flags (0 if zlib) */
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned long check;        /* protected copy of check value */
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned long total;        /* protected copy of output count */
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gz_headerp head;            /* where to save gzip header information */
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* sliding window */
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned wbits;             /* log base 2 of requested window size */
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned wsize;             /* window size or zero if not using window */
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned whave;             /* valid bytes in the window */
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned wnext;             /* window write index */
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned char FAR *window;  /* allocated sliding window, if needed */
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* bit accumulator */
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned long hold;         /* input bit accumulator */
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned bits;              /* number of bits in "in" */
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* for string and stored block copying */
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned length;            /* literal or length of data to copy */
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned offset;            /* distance back to copy string from */
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* for table and code decoding */
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned extra;             /* extra bits needed */
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* fixed and dynamic code tables */
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    code const FAR *lencode;    /* starting table for length/literal codes */
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    code const FAR *distcode;   /* starting table for distance codes */
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned lenbits;           /* index bits for lencode */
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned distbits;          /* index bits for distcode */
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        /* dynamic table building */
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned ncode;             /* number of code length code lengths */
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned nlen;              /* number of length code lengths */
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned ndist;             /* number of distance code lengths */
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned have;              /* number of code lengths in lens[] */
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    code FAR *next;             /* next available space in codes[] */
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned short lens[320];   /* temporary storage for code lengths */
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned short work[288];   /* work area for code table building */
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    code codes[ENOUGH];         /* space for code tables */
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int sane;                   /* if false, allow invalid distance too far */
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int back;                   /* bits back of last unprocessed length/lit */
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned was;               /* initial length of match */
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
123