1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* inflate.h -- internal inflate state definition
  26  * Copyright (C) 1995-2009 Mark Adler
  27  * For conditions of distribution and use, see copyright notice in zlib.h
  28  */
  29 
  30 /* WARNING: this file should *not* be used by applications. It is
  31    part of the implementation of the compression library and is
  32    subject to change. Applications should only use zlib.h.
  33  */
  34 
  35 /* define NO_GZIP when compiling if you want to disable gzip header and
  36    trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
  37    the crc code when it is not needed.  For shared libraries, gzip decoding
  38    should be left enabled. */
  39 #ifndef NO_GZIP
  40 #  define GUNZIP
  41 #endif
  42 
  43 /* Possible inflate modes between inflate() calls */
  44 typedef enum {
  45     HEAD,       /* i: waiting for magic header */
  46     FLAGS,      /* i: waiting for method and flags (gzip) */
  47     TIME,       /* i: waiting for modification time (gzip) */
  48     OS,         /* i: waiting for extra flags and operating system (gzip) */
  49     EXLEN,      /* i: waiting for extra length (gzip) */
  50     EXTRA,      /* i: waiting for extra bytes (gzip) */
  51     NAME,       /* i: waiting for end of file name (gzip) */
  52     COMMENT,    /* i: waiting for end of comment (gzip) */
  53     HCRC,       /* i: waiting for header crc (gzip) */
  54     DICTID,     /* i: waiting for dictionary check value */
  55     DICT,       /* waiting for inflateSetDictionary() call */
  56         TYPE,       /* i: waiting for type bits, including last-flag bit */
  57         TYPEDO,     /* i: same, but skip check to exit inflate on new block */
  58         STORED,     /* i: waiting for stored size (length and complement) */
  59         COPY_,      /* i/o: same as COPY below, but only first time in */
  60         COPY,       /* i/o: waiting for input or output to copy stored block */
  61         TABLE,      /* i: waiting for dynamic block table lengths */
  62         LENLENS,    /* i: waiting for code length code lengths */
  63         CODELENS,   /* i: waiting for length/lit and distance code lengths */
  64             LEN_,       /* i: same as LEN below, but only first time in */
  65             LEN,        /* i: waiting for length/lit/eob code */
  66             LENEXT,     /* i: waiting for length extra bits */
  67             DIST,       /* i: waiting for distance code */
  68             DISTEXT,    /* i: waiting for distance extra bits */
  69             MATCH,      /* o: waiting for output space to copy string */
  70             LIT,        /* o: waiting for output space to write literal */
  71     CHECK,      /* i: waiting for 32-bit check value */
  72     LENGTH,     /* i: waiting for 32-bit length (gzip) */
  73     DONE,       /* finished check, done -- remain here until reset */
  74     BAD,        /* got a data error -- remain here until reset */
  75     MEM,        /* got an inflate() memory error -- remain here until reset */
  76     SYNC        /* looking for synchronization bytes to restart inflate() */
  77 } inflate_mode;
  78 
  79 /*
  80     State transitions between above modes -
  81 
  82     (most modes can go to BAD or MEM on error -- not shown for clarity)
  83 
  84     Process header:
  85         HEAD -> (gzip) or (zlib) or (raw)
  86         (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
  87                   HCRC -> TYPE
  88         (zlib) -> DICTID or TYPE
  89         DICTID -> DICT -> TYPE
  90         (raw) -> TYPEDO
  91     Read deflate blocks:
  92             TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
  93             STORED -> COPY_ -> COPY -> TYPE
  94             TABLE -> LENLENS -> CODELENS -> LEN_
  95             LEN_ -> LEN
  96     Read deflate codes in fixed or dynamic block:
  97                 LEN -> LENEXT or LIT or TYPE
  98                 LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
  99                 LIT -> LEN
 100     Process trailer:
 101         CHECK -> LENGTH -> DONE
 102  */
 103 
 104 /* state maintained between inflate() calls.  Approximately 10K bytes. */
 105 struct inflate_state {
 106     inflate_mode mode;          /* current inflate mode */
 107     int last;                   /* true if processing last block */
 108     int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
 109     int havedict;               /* true if dictionary provided */
 110     int flags;                  /* gzip header method and flags (0 if zlib) */
 111     unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
 112     unsigned long check;        /* protected copy of check value */
 113     unsigned long total;        /* protected copy of output count */
 114     gz_headerp head;            /* where to save gzip header information */
 115         /* sliding window */
 116     unsigned wbits;             /* log base 2 of requested window size */
 117     unsigned wsize;             /* window size or zero if not using window */
 118     unsigned whave;             /* valid bytes in the window */
 119     unsigned wnext;             /* window write index */
 120     unsigned char FAR *window;  /* allocated sliding window, if needed */
 121         /* bit accumulator */
 122     unsigned long hold;         /* input bit accumulator */
 123     unsigned bits;              /* number of bits in "in" */
 124         /* for string and stored block copying */
 125     unsigned length;            /* literal or length of data to copy */
 126     unsigned offset;            /* distance back to copy string from */
 127         /* for table and code decoding */
 128     unsigned extra;             /* extra bits needed */
 129         /* fixed and dynamic code tables */
 130     code const FAR *lencode;    /* starting table for length/literal codes */
 131     code const FAR *distcode;   /* starting table for distance codes */
 132     unsigned lenbits;           /* index bits for lencode */
 133     unsigned distbits;          /* index bits for distcode */
 134         /* dynamic table building */
 135     unsigned ncode;             /* number of code length code lengths */
 136     unsigned nlen;              /* number of length code lengths */
 137     unsigned ndist;             /* number of distance code lengths */
 138     unsigned have;              /* number of code lengths in lens[] */
 139     code FAR *next;             /* next available space in codes[] */
 140     unsigned short lens[320];   /* temporary storage for code lengths */
 141     unsigned short work[288];   /* work area for code table building */
 142     code codes[ENOUGH];         /* space for code tables */
 143     int sane;                   /* if false, allow invalid distance too far */
 144     int back;                   /* bits back of last unprocessed length/lit */
 145     unsigned was;               /* initial length of match */
 146 };