1 /* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995-1998 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
13 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
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
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;}
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. */
30 int inflate_fast(bl, bd, tl, td, s, z)
33 inflate_huft *td; /* need separate declaration for Borland C++ */
34 inflate_blocks_statef *s;
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 */
51 /* load input, output, bit values */
54 /* initialize masks */
55 ml = inflate_mask[bl];
56 md = inflate_mask[bd];
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)
65 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
66 "inflate: * literal '%c'\n" :
67 "inflate: * literal 0x%02x\n", t->base));
76 /* get extra bits for length */
78 c = t->base + ((uInt)b & inflate_mask[e]);
80 Tracevv((stderr, "inflate: * length %u\n", c));
82 /* decode distance base of block to copy */
83 GRABBITS(15); /* max bits for distance code */
84 e = (t = td + ((uInt)b & md))->exop;
89 /* get extra bits to add to distance base */
91 GRABBITS(e) /* get extra bits (up to 13) */
92 d = t->base + ((uInt)b & inflate_mask[e]);
94 Tracevv((stderr, "inflate: * distance %u\n", d));
98 if ((uInt)(q - s->window) >= d) /* offset before dest */
101 *q++ = *r++; c--; /* minimum count is three, */
102 *q++ = *r++; c--; /* so unroll loop a little */
104 else /* else offset after destination */
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, */
110 c -= e; /* copy to end of window */
114 r = s->window; /* copy rest from start of window */
117 do { /* copy all or what's left */
122 else if ((e & 64) == 0)
123 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
126 z->msg = (char*)"invalid distance code";
136 if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
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;
149 Tracevv((stderr, "inflate: * end of block\n"));
156 z->msg = (char*)"invalid literal/length code";
162 } while (m >= 258 && n >= 10);
164 /* not enough input or output--restore pointers and return */