inffast.c revision 17651
1/* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995-1996 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "inftrees.h"
8#include "infblock.h"
9#include "infcodes.h"
10#include "infutil.h"
11#include "inffast.h"
12
13struct inflate_codes_state {int dummy;}; /* for buggy compilers */
14
15/* simplify the use of the inflate_huft type with some defines */
16#define base more.Base
17#define next more.Next
18#define exop word.what.Exop
19#define bits word.what.Bits
20
21/* macros for bit input with no checking and for returning unused bytes */
22#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
23#define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
24
25/* Called with number of bytes left to write in window at least 258
26   (the maximum string length) and number of input bytes available
27   at least ten.  The ten bytes are six bytes for the longest length/
28   distance pair plus four bytes for overloading the bit buffer. */
29
30int inflate_fast(bl, bd, tl, td, s, z)
31uInt bl, bd;
32inflate_huft *tl;
33inflate_huft *td; /* need separate declaration for Borland C++ */
34inflate_blocks_statef *s;
35z_streamp z;
36{
37  inflate_huft *t;      /* temporary pointer */
38  uInt e;               /* extra bits or operation */
39  uLong b;              /* bit buffer */
40  uInt k;               /* bits in bit buffer */
41  Bytef *p;             /* input data pointer */
42  uInt n;               /* bytes available there */
43  Bytef *q;             /* output window write pointer */
44  uInt m;               /* bytes to end of window or read pointer */
45  uInt ml;              /* mask for literal/length tree */
46  uInt md;              /* mask for distance tree */
47  uInt c;               /* bytes to copy */
48  uInt d;               /* distance back to copy from */
49  Bytef *r;             /* copy source pointer */
50
51  /* load input, output, bit values */
52  LOAD
53
54  /* initialize masks */
55  ml = inflate_mask[bl];
56  md = inflate_mask[bd];
57
58  /* do until not enough input or output space for fast loop */
59  do {                          /* assume called with m >= 258 && n >= 10 */
60    /* get literal/length code */
61    GRABBITS(20)                /* max bits for literal/length code */
62    if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
63    {
64      DUMPBITS(t->bits)
65      Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
66                "inflate:         * literal '%c'\n" :
67                "inflate:         * literal 0x%02x\n", t->base));
68      *q++ = (Byte)t->base;
69      m--;
70      continue;
71    }
72    do {
73      DUMPBITS(t->bits)
74      if (e & 16)
75      {
76        /* get extra bits for length */
77        e &= 15;
78        c = t->base + ((uInt)b & inflate_mask[e]);
79        DUMPBITS(e)
80        Tracevv((stderr, "inflate:         * length %u\n", c));
81
82        /* decode distance base of block to copy */
83        GRABBITS(15);           /* max bits for distance code */
84        e = (t = td + ((uInt)b & md))->exop;
85        do {
86          DUMPBITS(t->bits)
87          if (e & 16)
88          {
89            /* get extra bits to add to distance base */
90            e &= 15;
91            GRABBITS(e)         /* get extra bits (up to 13) */
92            d = t->base + ((uInt)b & inflate_mask[e]);
93            DUMPBITS(e)
94            Tracevv((stderr, "inflate:         * distance %u\n", d));
95
96            /* do the copy */
97            m -= c;
98            if ((uInt)(q - s->window) >= d)     /* offset before dest */
99            {                                   /*  just copy */
100              r = q - d;
101              *q++ = *r++;  c--;        /* minimum count is three, */
102              *q++ = *r++;  c--;        /*  so unroll loop a little */
103            }
104            else                        /* else offset after destination */
105            {
106              e = d - (uInt)(q - s->window); /* bytes from offset to end */
107              r = s->end - e;           /* pointer to offset */
108              if (c > e)                /* if source crosses, */
109              {
110                c -= e;                 /* copy to end of window */
111                do {
112                  *q++ = *r++;
113                } while (--e);
114                r = s->window;          /* copy rest from start of window */
115              }
116            }
117            do {                        /* copy all or what's left */
118              *q++ = *r++;
119            } while (--c);
120            break;
121          }
122          else if ((e & 64) == 0)
123            e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
124          else
125          {
126            z->msg = (char*)"invalid distance code";
127            UNGRAB
128            UPDATE
129            return Z_DATA_ERROR;
130          }
131        } while (1);
132        break;
133      }
134      if ((e & 64) == 0)
135      {
136        if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
137        {
138          DUMPBITS(t->bits)
139          Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
140                    "inflate:         * literal '%c'\n" :
141                    "inflate:         * literal 0x%02x\n", t->base));
142          *q++ = (Byte)t->base;
143          m--;
144          break;
145        }
146      }
147      else if (e & 32)
148      {
149        Tracevv((stderr, "inflate:         * end of block\n"));
150        UNGRAB
151        UPDATE
152        return Z_STREAM_END;
153      }
154      else
155      {
156        z->msg = (char*)"invalid literal/length code";
157        UNGRAB
158        UPDATE
159        return Z_DATA_ERROR;
160      }
161    } while (1);
162  } while (m >= 258 && n >= 10);
163
164  /* not enough input or output--restore pointers and return */
165  UNGRAB
166  UPDATE
167  return Z_OK;
168}
169