inflate.h revision 157044
11297Salm/* inflate.h -- internal inflate state definition
21297Salm * Copyright (C) 1995-2004 Mark Adler
31297Salm * For conditions of distribution and use, see copyright notice in zlib.h
41297Salm */
51297Salm
61297Salm/* WARNING: this file should *not* be used by applications. It is
71297Salm   part of the implementation of the compression library and is
81297Salm   subject to change. Applications should only use zlib.h.
91297Salm */
101297Salm
111297Salm/* define NO_GZIP when compiling if you want to disable gzip header and
121297Salm   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
131297Salm   the crc code when it is not needed.  For shared libraries, gzip decoding
141297Salm   should be left enabled. */
151297Salm#ifndef NO_GZIP
161297Salm#  define GUNZIP
171297Salm#endif
181297Salm
191297Salm/* Possible inflate modes between inflate() calls */
201297Salmtypedef enum {
211297Salm    HEAD,       /* i: waiting for magic header */
221297Salm    FLAGS,      /* i: waiting for method and flags (gzip) */
231297Salm    TIME,       /* i: waiting for modification time (gzip) */
241297Salm    OS,         /* i: waiting for extra flags and operating system (gzip) */
251297Salm    EXLEN,      /* i: waiting for extra length (gzip) */
261297Salm    EXTRA,      /* i: waiting for extra bytes (gzip) */
271297Salm    NAME,       /* i: waiting for end of file name (gzip) */
281297Salm    COMMENT,    /* i: waiting for end of comment (gzip) */
291297Salm    HCRC,       /* i: waiting for header crc (gzip) */
301297Salm    DICTID,     /* i: waiting for dictionary check value */
311297Salm    DICT,       /* waiting for inflateSetDictionary() call */
321297Salm        TYPE,       /* i: waiting for type bits, including last-flag bit */
331297Salm        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
341297Salm        STORED,     /* i: waiting for stored size (length and complement) */
351297Salm        COPY,       /* i/o: waiting for input or output to copy stored block */
361297Salm        TABLE,      /* i: waiting for dynamic block table lengths */
371297Salm        LENLENS,    /* i: waiting for code length code lengths */
381297Salm        CODELENS,   /* i: waiting for length/lit and distance code lengths */
391297Salm            LEN,        /* i: waiting for length/lit code */
401297Salm            LENEXT,     /* i: waiting for length extra bits */
411297Salm            DIST,       /* i: waiting for distance code */
421297Salm            DISTEXT,    /* i: waiting for distance extra bits */
431297Salm            MATCH,      /* o: waiting for output space to copy string */
441297Salm            LIT,        /* o: waiting for output space to write literal */
451297Salm    CHECK,      /* i: waiting for 32-bit check value */
461297Salm    LENGTH,     /* i: waiting for 32-bit length (gzip) */
471297Salm    DONE,       /* finished check, done -- remain here until reset */
481297Salm    BAD,        /* got a data error -- remain here until reset */
491297Salm    MEM,        /* got an inflate() memory error -- remain here until reset */
501297Salm    SYNC        /* looking for synchronization bytes to restart inflate() */
511297Salm} inflate_mode;
521297Salm
531297Salm/*
541297Salm    State transitions between above modes -
551297Salm
561297Salm    (most modes can go to the BAD or MEM mode -- not shown for clarity)
571297Salm
581297Salm    Process header:
591297Salm        HEAD -> (gzip) or (zlib)
601297Salm        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
611297Salm        NAME -> COMMENT -> HCRC -> TYPE
621297Salm        (zlib) -> DICTID or TYPE
631297Salm        DICTID -> DICT -> TYPE
641297Salm    Read deflate blocks:
651297Salm            TYPE -> STORED or TABLE or LEN or CHECK
661297Salm            STORED -> COPY -> TYPE
671297Salm            TABLE -> LENLENS -> CODELENS -> LEN
681297Salm    Read deflate codes:
691297Salm                LEN -> LENEXT or LIT or TYPE
701297Salm                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
711297Salm                LIT -> LEN
721297Salm    Process trailer:
731297Salm        CHECK -> LENGTH -> DONE
741297Salm */
751297Salm
761297Salm/* state maintained between inflate() calls.  Approximately 7K bytes. */
771297Salmstruct inflate_state {
781297Salm    inflate_mode mode;          /* current inflate mode */
791297Salm    int last;                   /* true if processing last block */
801297Salm    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
811297Salm    int havedict;               /* true if dictionary provided */
821297Salm    int flags;                  /* gzip header method and flags (0 if zlib) */
831297Salm    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
841297Salm    unsigned long check;        /* protected copy of check value */
851297Salm    unsigned long total;        /* protected copy of output count */
861297Salm    gz_headerp head;            /* where to save gzip header information */
871297Salm        /* sliding window */
881297Salm    unsigned wbits;             /* log base 2 of requested window size */
891297Salm    unsigned wsize;             /* window size or zero if not using window */
901297Salm    unsigned whave;             /* valid bytes in the window */
911297Salm    unsigned write;             /* window write index */
921297Salm    unsigned char FAR *window;  /* allocated sliding window, if needed */
931297Salm        /* bit accumulator */
941297Salm    unsigned long hold;         /* input bit accumulator */
951297Salm    unsigned bits;              /* number of bits in "in" */
961297Salm        /* for string and stored block copying */
971297Salm    unsigned length;            /* literal or length of data to copy */
981297Salm    unsigned offset;            /* distance back to copy string from */
991297Salm        /* for table and code decoding */
1001297Salm    unsigned extra;             /* extra bits needed */
1011297Salm        /* fixed and dynamic code tables */
1021297Salm    code const FAR *lencode;    /* starting table for length/literal codes */
1031297Salm    code const FAR *distcode;   /* starting table for distance codes */
1041297Salm    unsigned lenbits;           /* index bits for lencode */
1051297Salm    unsigned distbits;          /* index bits for distcode */
1061297Salm        /* dynamic table building */
1071297Salm    unsigned ncode;             /* number of code length code lengths */
1081297Salm    unsigned nlen;              /* number of length code lengths */
1091297Salm    unsigned ndist;             /* number of distance code lengths */
1101297Salm    unsigned have;              /* number of code lengths in lens[] */
1111297Salm    code FAR *next;             /* next available space in codes[] */
1121297Salm    unsigned short lens[320];   /* temporary storage for code lengths */
1131297Salm    unsigned short work[288];   /* work area for code table building */
1141297Salm    code codes[ENOUGH];         /* space for code tables */
1151297Salm};
1161297Salm