Skip to content
Snippets Groups Projects
zlib.c 62.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Wolfgang Denk's avatar
    Wolfgang Denk committed
    	  LEAVE
    	}
    	Tracev((stderr, "inflate:       trees ok\n"));
    	if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
    	{
    	  inflate_trees_free(td, z);
    	  inflate_trees_free(tl, z);
    	  r = Z_MEM_ERROR;
    	  LEAVE
    	}
    	ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
    	s->sub.decode.codes = c;
    	s->sub.decode.tl = tl;
    	s->sub.decode.td = td;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          s->mode = CODES;
        case CODES:
          UPDATE
          if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	return inflate_flush(s, z, r);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          r = Z_OK;
          inflate_codes_free(s->sub.decode.codes, z);
          inflate_trees_free(s->sub.decode.td, z);
          inflate_trees_free(s->sub.decode.tl, z);
          LOAD
          Tracev((stderr, "inflate:       codes end, %lu total out\n",
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	      z->total_out + (q >= s->read ? q - s->read :
    	      (s->end - s->read) + (q - s->window))));
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          if (!s->last)
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	s->mode = TYPE;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          if (k > 7)              /* return unused byte, if any */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	Assert(k < 16, "inflate_codes grabbed too many bytes")
    	k -= 8;
    	n++;
    	p--;                    /* can always return one */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          s->mode = DRY;
        case DRY:
          FLUSH
          if (s->read != s->write)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	LEAVE
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          s->mode = DONEB;
        case DONEB:
          r = Z_STREAM_END;
          LEAVE
        case BADB:
          r = Z_DATA_ERROR;
          LEAVE
        default:
          r = Z_STREAM_ERROR;
          LEAVE
      }
    }
    
    
    local int inflate_blocks_free(s, z, c)
    inflate_blocks_statef *s;
    z_stream *z;
    uLongf *c;
    {
      inflate_blocks_reset(s, z, c);
      ZFREE(z, s->window, s->end - s->window);
      ZFREE(z, s, sizeof(struct inflate_blocks_state));
      Trace((stderr, "inflate:   blocks freed\n"));
      return Z_OK;
    }
    
    /*
     * This subroutine adds the data at next_in/avail_in to the output history
     * without performing any output.  The output buffer must be "caught up";
     * i.e. no pending output (hence s->read equals s->write), and the state must
     * be BLOCKS (i.e. we should be willing to see the start of a series of
     * BLOCKS).  On exit, the output will also be caught up, and the checksum
     * will have been updated if need be.
     */
    local int inflate_addhistory(s, z)
    inflate_blocks_statef *s;
    z_stream *z;
    {
        uLong b;              /* bit buffer */  /* NOT USED HERE */
        uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
        uInt t;               /* temporary storage */
        Bytef *p;             /* input data pointer */
        uInt n;               /* bytes available there */
        Bytef *q;             /* output window write pointer */
        uInt m;               /* bytes to end of window or read pointer */
    
        if (s->read != s->write)
    	return Z_STREAM_ERROR;
        if (s->mode != TYPE)
    	return Z_DATA_ERROR;
    
        /* we're ready to rock */
        LOAD
        /* while there is input ready, copy to output buffer, moving
         * pointers as needed.
         */
        while (n) {
    	t = n;  /* how many to do */
    	/* is there room until end of buffer? */
    	if (t > m) t = m;
    	/* update check information */
    	if (s->checkfn != Z_NULL)
    	    s->check = (*s->checkfn)(s->check, q, t);
    	/* output callback */
    	if (z->outcb != Z_NULL)
    	    (*z->outcb)(q, t);
    	zmemcpy(q, p, t);
    	q += t;
    	p += t;
    	n -= t;
    	z->total_out += t;
    	s->read = q;    /* drag read pointer forward */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    /*      WRAP  */	/* expand WRAP macro by hand to handle s->read */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	if (q == s->end) {
    	    s->read = q = s->window;
    	    m = WAVAIL;
    	}
        }
        UPDATE
        return Z_OK;
    }
    
    
    /*
     * At the end of a Deflate-compressed PPP packet, we expect to have seen
     * a `stored' block type value but not the (zero) length bytes.
     */
    local int inflate_packet_flush(s)
        inflate_blocks_statef *s;
    {
        if (s->mode != LENS)
    	return Z_DATA_ERROR;
        s->mode = TYPE;
        return Z_OK;
    }
    
    
    /*+++++*/
    /* inftrees.c -- generate Huffman trees for efficient decoding
     * Copyright (C) 1995 Mark Adler
     * For conditions of distribution and use, see copyright notice in zlib.h
     */
    
    /* simplify the use of the inflate_huft type with some defines */
    #define base more.Base
    #define next more.Next
    #define exop word.what.Exop
    #define bits word.what.Bits
    
    
    local int huft_build OF((
        uIntf *,            /* code lengths in bits */
        uInt,               /* number of codes */
        uInt,               /* number of "simple" codes */
        uIntf *,            /* list of base values for non-simple codes */
        uIntf *,            /* list of extra bits for non-simple codes */
        inflate_huft * FAR*,/* result: starting table */
        uIntf *,            /* maximum lookup bits (returns actual) */
        z_stream *));       /* for zalloc function */
    
    local voidpf falloc OF((
        voidpf,             /* opaque pointer (not used) */
        uInt,               /* number of items */
        uInt));             /* size of item */
    
    local void ffree OF((
        voidpf q,           /* opaque pointer (not used) */
        voidpf p,           /* what to free (not used) */
        uInt n));		/* number of bytes (not used) */
    
    /* Tables for deflate from PKZIP's appnote.txt. */
    local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
    	35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
    	/* actually lengths - 2; also see note #13 above about 258 */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
    	3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
    	257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
    	8193, 12289, 16385, 24577};
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    local uInt cpdext[] = { /* Extra bits for distance codes */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
    	7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
    	12, 12, 13, 13};
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    /*
       Huffman code decoding is performed using a multi-level table lookup.
       The fastest way to decode is to simply build a lookup table whose
       size is determined by the longest code.  However, the time it takes
       to build this table can also be a factor if the data being decoded
       is not very long.  The most common codes are necessarily the
       shortest codes, so those codes dominate the decoding time, and hence
       the speed.  The idea is you can have a shorter table that decodes the
       shorter, more probable codes, and then point to subsidiary tables for
       the longer codes.  The time it costs to decode the longer codes is
       then traded against the time it takes to make longer tables.
    
       This results of this trade are in the variables lbits and dbits
       below.  lbits is the number of bits the first level table for literal/
       length codes can decode in one step, and dbits is the same thing for
       the distance codes.  Subsequent tables are also less than or equal to
       those sizes.  These values may be adjusted either when all of the
       codes are shorter than that, in which case the longest code length in
       bits is used, or when the shortest code is *longer* than the requested
       table size, in which case the length of the shortest code in bits is
       used.
    
       There are two different values for the two tables, since they code a
       different number of possibilities each.  The literal/length table
       codes 286 possible values, or in a flat code, a little over eight
       bits.  The distance table codes 30 possible values, or a little less
       than five bits, flat.  The optimum values for speed end up being
       about one bit more than those, so lbits is 8+1 and dbits is 5+1.
       The optimum values may differ though from machine to machine, and
       possibly even between compilers.  Your mileage may vary.
     */
    
    
    /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
    #define BMAX 15         /* maximum bit length of any code */
    #define N_MAX 288       /* maximum number of codes in any set */
    
    #ifdef DEBUG_ZLIB
      uInt inflate_hufts;
    #endif
    
    local int huft_build(b, n, s, d, e, t, m, zs)
    uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
    uInt n;                 /* number of codes (assumed <= N_MAX) */
    uInt s;                 /* number of simple-valued codes (0..s-1) */
    uIntf *d;               /* list of base values for non-simple codes */
    uIntf *e;               /* list of extra bits for non-simple codes */
    inflate_huft * FAR *t;  /* result: starting table */
    uIntf *m;               /* maximum lookup bits, returns actual */
    z_stream *zs;           /* for zalloc function */
    /* Given a list of code lengths and a maximum table size, make a set of
       tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
       if the given code set is incomplete (the tables are still built in this
       case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
       over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
    {
    
      uInt a;                       /* counter for codes of length k */
      uInt c[BMAX+1];               /* bit length count table */
      uInt f;                       /* i repeats in table every f entries */
      int g;                        /* maximum code length */
      int h;                        /* table level */
      register uInt i;              /* counter, current code */
      register uInt j;              /* counter */
      register int k;               /* number of bits in current code */
      int l;                        /* bits per table (returned in m) */
      register uIntf *p;            /* pointer into c[], b[], or v[] */
      inflate_huft *q;              /* points to current table */
      struct inflate_huft_s r;      /* table entry for structure assignment */
      inflate_huft *u[BMAX];        /* table stack */
      uInt v[N_MAX];                /* values in order of bit length */
      register int w;               /* bits before this table == (l * h) */
      uInt x[BMAX+1];               /* bit offsets, then code stack */
      uIntf *xp;                    /* pointer into x */
      int y;                        /* number of dummy codes added */
      uInt z;                       /* number of entries in current table */
    
    
      /* Generate counts for each bit length */
      p = c;
    #define C0 *p++ = 0;
    #define C2 C0 C0 C0 C0
    #define C4 C2 C2 C2 C2
      C4                            /* clear c[]--assume BMAX+1 is 16 */
      p = b;  i = n;
      do {
        c[*p++]++;                  /* assume all entries <= BMAX */
      } while (--i);
      if (c[0] == n)                /* null input--all zero length codes */
      {
        *t = (inflate_huft *)Z_NULL;
        *m = 0;
        return Z_OK;
      }
    
    
      /* Find minimum and maximum length, bound *m by those */
      l = *m;
      for (j = 1; j <= BMAX; j++)
        if (c[j])
          break;
      k = j;                        /* minimum code length */
      if ((uInt)l < j)
        l = j;
      for (i = BMAX; i; i--)
        if (c[i])
          break;
      g = i;                        /* maximum code length */
      if ((uInt)l > i)
        l = i;
      *m = l;
    
    
      /* Adjust last length count to fill out codes, if needed */
      for (y = 1 << j; j < i; j++, y <<= 1)
        if ((y -= c[j]) < 0)
          return Z_DATA_ERROR;
      if ((y -= c[i]) < 0)
        return Z_DATA_ERROR;
      c[i] += y;
    
    
      /* Generate starting offsets into the value table for each length */
      x[1] = j = 0;
      p = c + 1;  xp = x + 2;
      while (--i) {                 /* note that i == g from above */
        *xp++ = (j += *p++);
      }
    
    
      /* Make a table of values in order of bit lengths */
      p = b;  i = 0;
      do {
        if ((j = *p++) != 0)
          v[x[j]++] = i;
      } while (++i < n);
    
    
      /* Generate the Huffman codes and for each, make the table entries */
      x[0] = i = 0;                 /* first Huffman code is zero */
      p = v;                        /* grab values in bit order */
      h = -1;                       /* no tables yet--level -1 */
      w = -l;                       /* bits decoded == (l * h) */
      u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
      q = (inflate_huft *)Z_NULL;   /* ditto */
      z = 0;                        /* ditto */
    
      /* go through the bit lengths (k already is bits in shortest code) */
      for (; k <= g; k++)
      {
        a = c[k];
        while (a--)
        {
          /* here i is the Huffman code of length k bits for value *p */
          /* make tables up to required level */
          while (k > w + l)
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	h++;
    	w += l;                 /* previous table always l bits */
    
    	/* compute minimum size table less than or equal to l bits */
    	z = (z = g - w) > (uInt)l ? l : z;      /* table size upper limit */
    	if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
    	{                       /* too few codes for k-w bit table */
    	  f -= a + 1;           /* deduct codes from patterns left */
    	  xp = c + k;
    	  if (j < z)
    	    while (++j < z)     /* try smaller tables up to z bits */
    	    {
    	      if ((f <<= 1) <= *++xp)
    		break;          /* enough codes to use up j bits */
    	      f -= *xp;         /* else deduct codes from patterns */
    	    }
    	}
    	z = 1 << j;             /* table entries for j-bit table */
    
    	/* allocate and link in new table */
    	if ((q = (inflate_huft *)ZALLOC
    	     (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
    	{
    	  if (h)
    	    inflate_trees_free(u[0], zs);
    	  return Z_MEM_ERROR;   /* not enough memory */
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	q->word.Nalloc = z + 1;
    #ifdef DEBUG_ZLIB
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	inflate_hufts += z + 1;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #endif
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	*t = q + 1;             /* link to list for huft_free() */
    	*(t = &(q->next)) = Z_NULL;
    	u[h] = ++q;             /* table starts after link */
    
    	/* connect to last table, if there is one */
    	if (h)
    	{
    	  x[h] = i;             /* save pattern for backing up */
    	  r.bits = (Byte)l;     /* bits to dump before this table */
    	  r.exop = (Byte)j;     /* bits in this table */
    	  r.next = q;           /* pointer to this table */
    	  j = i >> (w - l);     /* (get around Turbo C bug) */
    	  u[h-1][j] = r;        /* connect to last table */
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
    
          /* set up table entry in r */
          r.bits = (Byte)(k - w);
          if (p >= v + n)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	r.exop = 128 + 64;      /* out of values--invalid code */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          else if (*p < s)
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
    	r.base = *p++;          /* simple code is just the value */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          else
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */
    	r.base = d[*p++ - s];
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
    
          /* fill code-like entries with r */
          f = 1 << (k - w);
          for (j = i >> w; j < z; j += f)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	q[j] = r;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
          /* backwards increment the k-bit code i */
          for (j = 1 << (k - 1); i & j; j >>= 1)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	i ^= j;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          i ^= j;
    
          /* backup over finished tables */
          while ((i & ((1 << w) - 1)) != x[h])
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	h--;                    /* don't need to update q */
    	w -= l;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
        }
      }
    
    
      /* Return Z_BUF_ERROR if we were given an incomplete table */
      return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
    }
    
    
    local int inflate_trees_bits(c, bb, tb, z)
    uIntf *c;               /* 19 code lengths */
    uIntf *bb;              /* bits tree desired/actual depth */
    inflate_huft * FAR *tb; /* bits tree result */
    z_stream *z;            /* for zfree function */
    {
      int r;
    
      r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
      if (r == Z_DATA_ERROR)
        z->msg = "oversubscribed dynamic bit lengths tree";
      else if (r == Z_BUF_ERROR)
      {
        inflate_trees_free(*tb, z);
        z->msg = "incomplete dynamic bit lengths tree";
        r = Z_DATA_ERROR;
      }
      return r;
    }
    
    
    local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
    uInt nl;                /* number of literal/length codes */
    uInt nd;                /* number of distance codes */
    uIntf *c;               /* that many (total) code lengths */
    uIntf *bl;              /* literal desired/actual bit depth */
    uIntf *bd;              /* distance desired/actual bit depth */
    inflate_huft * FAR *tl; /* literal/length tree result */
    inflate_huft * FAR *td; /* distance tree result */
    z_stream *z;            /* for zfree function */
    {
      int r;
    
      /* build literal/length tree */
      if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
      {
        if (r == Z_DATA_ERROR)
          z->msg = "oversubscribed literal/length tree";
        else if (r == Z_BUF_ERROR)
        {
          inflate_trees_free(*tl, z);
          z->msg = "incomplete literal/length tree";
          r = Z_DATA_ERROR;
        }
        return r;
      }
    
      /* build distance tree */
      if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
      {
        if (r == Z_DATA_ERROR)
          z->msg = "oversubscribed literal/length tree";
        else if (r == Z_BUF_ERROR) {
    #ifdef PKZIP_BUG_WORKAROUND
          r = Z_OK;
        }
    #else
          inflate_trees_free(*td, z);
          z->msg = "incomplete literal/length tree";
          r = Z_DATA_ERROR;
        }
        inflate_trees_free(*tl, z);
        return r;
    #endif
      }
    
      /* done */
      return Z_OK;
    }
    
    
    /* build fixed tables only once--keep them here */
    local int fixed_lock = 0;
    local int fixed_built = 0;
    #define FIXEDH 530      /* number of hufts used by fixed tables */
    local uInt fixed_left = FIXEDH;
    local inflate_huft fixed_mem[FIXEDH];
    local uInt fixed_bl;
    local uInt fixed_bd;
    local inflate_huft *fixed_tl;
    local inflate_huft *fixed_td;
    
    
    local voidpf falloc(q, n, s)
    voidpf q;        /* opaque pointer (not used) */
    uInt n;         /* number of items */
    uInt s;         /* size of item */
    {
      Assert(s == sizeof(inflate_huft) && n <= fixed_left,
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	 "inflate_trees falloc overflow");
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
      if (q) s++; /* to make some compilers happy */
      fixed_left -= n;
      return (voidpf)(fixed_mem + fixed_left);
    }
    
    
    local void ffree(q, p, n)
    voidpf q;
    voidpf p;
    uInt n;
    {
      Assert(0, "inflate_trees ffree called!");
      if (q) q = p; /* to make some compilers happy */
    }
    
    
    local int inflate_trees_fixed(bl, bd, tl, td)
    uIntf *bl;               /* literal desired/actual bit depth */
    uIntf *bd;               /* distance desired/actual bit depth */
    inflate_huft * FAR *tl;  /* literal/length tree result */
    inflate_huft * FAR *td;  /* distance tree result */
    {
      /* build fixed tables if not built already--lock out other instances */
      while (++fixed_lock > 1)
        fixed_lock--;
      if (!fixed_built)
      {
        int k;              /* temporary variable */
        unsigned c[288];    /* length list for huft_build */
        z_stream z;         /* for falloc function */
    
        /* set up fake z_stream for memory routines */
        z.zalloc = falloc;
        z.zfree = ffree;
        z.opaque = Z_NULL;
    
        /* literal table */
        for (k = 0; k < 144; k++)
          c[k] = 8;
        for (; k < 256; k++)
          c[k] = 9;
        for (; k < 280; k++)
          c[k] = 7;
        for (; k < 288; k++)
          c[k] = 8;
        fixed_bl = 7;
        huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
    
        /* distance table */
        for (k = 0; k < 30; k++)
          c[k] = 5;
        fixed_bd = 5;
        huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
    
        /* done */
        fixed_built = 1;
      }
      fixed_lock--;
      *bl = fixed_bl;
      *bd = fixed_bd;
      *tl = fixed_tl;
      *td = fixed_td;
      return Z_OK;
    }
    
    
    local int inflate_trees_free(t, z)
    inflate_huft *t;        /* table to free */
    z_stream *z;            /* for zfree function */
    /* Free the malloc'ed tables built by huft_build(), which makes a linked
       list of the tables it made, with the links in a dummy first entry of
       each table. */
    {
      register inflate_huft *p, *q;
    
      /* Go through linked list, freeing from the malloced (t[-1]) address. */
      p = t;
      while (p != Z_NULL)
      {
        q = (--p)->next;
        ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
        p = q;
      }
      return Z_OK;
    }
    
    /*+++++*/
    /* infcodes.c -- process literals and length/distance pairs
     * Copyright (C) 1995 Mark Adler
     * For conditions of distribution and use, see copyright notice in zlib.h
     */
    
    /* simplify the use of the inflate_huft type with some defines */
    #define base more.Base
    #define next more.Next
    #define exop word.what.Exop
    #define bits word.what.Bits
    
    /* inflate codes private state */
    struct inflate_codes_state {
    
      /* mode */
      enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
          START,    /* x: set up for LEN */
          LEN,      /* i: get length/literal/eob next */
          LENEXT,   /* i: getting length extra (have base) */
          DIST,     /* i: get distance next */
          DISTEXT,  /* i: getting distance extra */
          COPY,     /* o: copying bytes in window, waiting for space */
          LIT,      /* o: got literal, waiting for output space */
          WASH,     /* o: got eob, possibly still output waiting */
          END,      /* x: got eob and all data flushed */
          BADCODE}  /* x: got error */
        mode;               /* current inflate_codes mode */
    
      /* mode dependent information */
      uInt len;
      union {
        struct {
          inflate_huft *tree;       /* pointer into tree */
          uInt need;                /* bits needed */
        } code;             /* if LEN or DIST, where in tree */
        uInt lit;           /* if LIT, literal */
        struct {
          uInt get;                 /* bits to get for extra */
          uInt dist;                /* distance back to copy from */
        } copy;             /* if EXT or COPY, where and how much */
      } sub;                /* submode */
    
      /* mode independent information */
      Byte lbits;           /* ltree bits decoded per branch */
      Byte dbits;           /* dtree bits decoder per branch */
      inflate_huft *ltree;          /* literal/length/eob tree */
      inflate_huft *dtree;          /* distance tree */
    
    };
    
    
    local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
    uInt bl, bd;
    inflate_huft *tl, *td;
    z_stream *z;
    {
      inflate_codes_statef *c;
    
      if ((c = (inflate_codes_statef *)
           ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
      {
        c->mode = START;
        c->lbits = (Byte)bl;
        c->dbits = (Byte)bd;
        c->ltree = tl;
        c->dtree = td;
        Tracev((stderr, "inflate:       codes new\n"));
      }
      return c;
    }
    
    
    local int inflate_codes(s, z, r)
    inflate_blocks_statef *s;
    z_stream *z;
    int r;
    {
      uInt j;               /* temporary storage */
      inflate_huft *t;      /* temporary pointer */
      uInt e;               /* extra bits or operation */
      uLong b;              /* bit buffer */
      uInt k;               /* bits in bit buffer */
      Bytef *p;             /* input data pointer */
      uInt n;               /* bytes available there */
      Bytef *q;             /* output window write pointer */
      uInt m;               /* bytes to end of window or read pointer */
      Bytef *f;             /* pointer to copy strings from */
      inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
    
      /* copy input/output information to locals (UPDATE macro restores) */
      LOAD
    
      /* process input and output based on current state */
      while (1) switch (c->mode)
      {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
        case START:         /* x: set up for LEN */
    #ifndef SLOW
          if (m >= 258 && n >= 10)
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	UPDATE
    	r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
    	LOAD
    	if (r != Z_OK)
    	{
    	  c->mode = r == Z_STREAM_END ? WASH : BADCODE;
    	  break;
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
    #endif /* !SLOW */
          c->sub.code.need = c->lbits;
          c->sub.code.tree = c->ltree;
          c->mode = LEN;
        case LEN:           /* i: get length/literal/eob next */
          j = c->sub.code.need;
          NEEDBITS(j)
          t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
          DUMPBITS(t->bits)
          e = (uInt)(t->exop);
          if (e == 0)               /* literal */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	c->sub.lit = t->base;
    	Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
    		 "inflate:         literal '%c'\n" :
    		 "inflate:         literal 0x%02x\n", t->base));
    	c->mode = LIT;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          if (e & 16)               /* length */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	c->sub.copy.get = e & 15;
    	c->len = t->base;
    	c->mode = LENEXT;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          if ((e & 64) == 0)        /* next table */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	c->sub.code.need = e;
    	c->sub.code.tree = t->next;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          if (e & 32)               /* end of block */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	Tracevv((stderr, "inflate:         end of block\n"));
    	c->mode = WASH;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          c->mode = BADCODE;        /* invalid code */
          z->msg = "invalid literal/length code";
          r = Z_DATA_ERROR;
          LEAVE
        case LENEXT:        /* i: getting length extra (have base) */
          j = c->sub.copy.get;
          NEEDBITS(j)
          c->len += (uInt)b & inflate_mask[j];
          DUMPBITS(j)
          c->sub.code.need = c->dbits;
          c->sub.code.tree = c->dtree;
          Tracevv((stderr, "inflate:         length %u\n", c->len));
          c->mode = DIST;
        case DIST:          /* i: get distance next */
          j = c->sub.code.need;
          NEEDBITS(j)
          t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
          DUMPBITS(t->bits)
          e = (uInt)(t->exop);
          if (e & 16)               /* distance */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	c->sub.copy.get = e & 15;
    	c->sub.copy.dist = t->base;
    	c->mode = DISTEXT;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          if ((e & 64) == 0)        /* next table */
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	c->sub.code.need = e;
    	c->sub.code.tree = t->next;
    	break;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          c->mode = BADCODE;        /* invalid code */
          z->msg = "invalid distance code";
          r = Z_DATA_ERROR;
          LEAVE
        case DISTEXT:       /* i: getting distance extra */
          j = c->sub.copy.get;
          NEEDBITS(j)
          c->sub.copy.dist += (uInt)b & inflate_mask[j];
          DUMPBITS(j)
          Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
          c->mode = COPY;
        case COPY:          /* o: copying bytes in window, waiting for space */
    #ifndef __TURBOC__ /* Turbo C bug for following expression */
          f = (uInt)(q - s->window) < c->sub.copy.dist ?
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	  s->end - (c->sub.copy.dist - (q - s->window)) :
    	  q - c->sub.copy.dist;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #else
          f = q - c->sub.copy.dist;
          if ((uInt)(q - s->window) < c->sub.copy.dist)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	f = s->end - (c->sub.copy.dist - (q - s->window));
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #endif
          while (c->len)
          {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	NEEDOUT
    	OUTBYTE(*f++)
    	if (f == s->end)
    	  f = s->window;
    	c->len--;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          }
          c->mode = START;
          break;
        case LIT:           /* o: got literal, waiting for output space */
          NEEDOUT
          OUTBYTE(c->sub.lit)
          c->mode = START;
          break;
        case WASH:          /* o: got eob, possibly more output */
          FLUSH
          if (s->read != s->write)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	LEAVE
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          c->mode = END;
        case END:
          r = Z_STREAM_END;
          LEAVE
        case BADCODE:       /* x: got error */
          r = Z_DATA_ERROR;
          LEAVE
        default:
          r = Z_STREAM_ERROR;
          LEAVE
      }
    }
    
    
    local void inflate_codes_free(c, z)
    inflate_codes_statef *c;
    z_stream *z;
    {
      ZFREE(z, c, sizeof(struct inflate_codes_state));
      Tracev((stderr, "inflate:       codes free\n"));
    }
    
    /*+++++*/
    /* inflate_util.c -- data and routines common to blocks and codes
     * Copyright (C) 1995 Mark Adler
     * For conditions of distribution and use, see copyright notice in zlib.h
     */
    
    /* copy as much as possible from the sliding window to the output area */
    local int inflate_flush(s, z, r)
    inflate_blocks_statef *s;
    z_stream *z;
    int r;
    {
      uInt n;
      Bytef *p, *q;
    
      /* local copies of source and destination pointers */
      p = z->next_out;
      q = s->read;
    
      /* compute number of bytes to copy as far as end of window */
      n = (uInt)((q <= s->write ? s->write : s->end) - q);
      if (n > z->avail_out) n = z->avail_out;
      if (n && r == Z_BUF_ERROR) r = Z_OK;
    
      /* update counters */
      z->avail_out -= n;
      z->total_out += n;
    
      /* update check information */
      if (s->checkfn != Z_NULL)
        s->check = (*s->checkfn)(s->check, q, n);
    
      /* output callback */
      if (z->outcb != Z_NULL)
        (*z->outcb)(q, n);
    
      /* copy as far as end of window */
      zmemcpy(p, q, n);
      p += n;
      q += n;
    
      /* see if more to copy at beginning of window */
      if (q == s->end)
      {
        /* wrap pointers */
        q = s->window;
        if (s->write == s->end)
          s->write = s->window;
    
        /* compute bytes to copy */
        n = (uInt)(s->write - q);
        if (n > z->avail_out) n = z->avail_out;
        if (n && r == Z_BUF_ERROR) r = Z_OK;
    
        /* update counters */
        z->avail_out -= n;
        z->total_out += n;
    
        /* update check information */
        if (s->checkfn != Z_NULL)
          s->check = (*s->checkfn)(s->check, q, n);
    
        /* output callback */
        if (z->outcb != Z_NULL)
    	(*z->outcb)(q, n);
    
        /* copy */
        zmemcpy(p, q, n);
        p += n;
        q += n;
      }
    
      /* update pointers */
      z->next_out = p;
      s->read = q;
    
      /* done */
      return r;
    }
    
    
    /*+++++*/
    /* inffast.c -- process literals and length/distance pairs fast
     * Copyright (C) 1995 Mark Adler
     * For conditions of distribution and use, see copyright notice in zlib.h
     */
    
    /* simplify the use of the inflate_huft type with some defines */
    #define base more.Base
    #define next more.Next
    #define exop word.what.Exop
    #define bits word.what.Bits
    
    /* macros for bit input with no checking and for returning unused bytes */
    #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
    #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
    
    /* Called with number of bytes left to write in window at least 258
       (the maximum string length) and number of input bytes available
       at least ten.  The ten bytes are six bytes for the longest length/
       distance pair plus four bytes for overloading the bit buffer. */
    
    local int inflate_fast(bl, bd, tl, td, s, z)
    uInt bl, bd;
    inflate_huft *tl, *td;
    inflate_blocks_statef *s;
    z_stream *z;
    {
      inflate_huft *t;      /* temporary pointer */
      uInt e;               /* extra bits or operation */
      uLong b;              /* bit buffer */
      uInt k;               /* bits in bit buffer */
      Bytef *p;             /* input data pointer */
      uInt n;               /* bytes available there */
      Bytef *q;             /* output window write pointer */
      uInt m;               /* bytes to end of window or read pointer */
      uInt ml;              /* mask for literal/length tree */
      uInt md;              /* mask for distance tree */
      uInt c;               /* bytes to copy */
      uInt d;               /* distance back to copy from */
      Bytef *r;             /* copy source pointer */
    
      /* load input, output, bit values */
      LOAD
    
      /* initialize masks */
      ml = inflate_mask[bl];
      md = inflate_mask[bd];
    
      /* do until not enough input or output space for fast loop */
      do {                          /* assume called with m >= 258 && n >= 10 */
        /* get literal/length code */
        GRABBITS(20)                /* max bits for literal/length code */
        if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
        {
          DUMPBITS(t->bits)
          Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		"inflate:         * literal '%c'\n" :
    		"inflate:         * literal 0x%02x\n", t->base));
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
          *q++ = (Byte)t->base;
          m--;
          continue;
        }
        do {
          DUMPBITS(t->bits)
          if (e & 16)
          {