1 /* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13 /* simplify the use of the inflate_huft type with some defines */
14 #define base more.Base
15 #define next more.Next
16 #define exop word.what.Exop
17 #define bits word.what.Bits
19 /* macros for bit input with no checking and for returning unused bytes */
22 # define NEXTBYTE (n--?0:fprintf(stderr,"inffast underrun\n"),*p++)
24 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
25 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
27 /* Called with number of bytes left to write in window at least 258
28 (the maximum string length) and number of input bytes available
29 at least ten. The ten bytes are six bytes for the longest length/
30 distance pair plus four bytes for overloading the bit buffer. */
32 int inflate_fast(bl, bd, tl, td, s, z)
34 inflate_huft *tl, *td;
35 struct inflate_blocks_state *s;
38 inflate_huft *t; /* temporary pointer */
39 int e; /* extra bits or operation */
40 uLong b; /* bit buffer */
41 uInt k; /* bits in bit buffer */
42 Byte *p; /* input data pointer */
43 uInt n; /* bytes available there */
44 Byte *q; /* output window write pointer */
45 uInt m; /* bytes to end of window or read pointer */
46 uInt ml; /* mask for literal/length tree */
47 uInt md; /* mask for distance tree */
48 uInt c; /* bytes to copy */
49 uInt d; /* distance back to copy from */
50 Byte *r; /* copy source pointer */
52 /* load input, output, bit values */
55 /* initialize masks in registers */
56 ml = inflate_mask[bl];
57 md = inflate_mask[bd];
59 /* do until not enough input or output space for fast loop */
60 do { /* assume called with m >= 258 && n >= 10 */
61 /* get literal/length code */
62 GRABBITS(20) /* max bits for literal/length code */
63 if ((e = (t = tl + ((uInt)b & ml))->exop) < 0)
67 z->msg = "invalid literal/length code";
74 if (e & 64) /* end of block */
76 Tracevv((stderr, "inflate: * end of block\n"));
81 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
84 /* process literal or length (end of block already trapped) */
85 if (e & 16) /* then it's a literal */
87 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
88 "inflate: * literal '%c'\n" :
89 "inflate: * literal 0x%02x\n", t->base));
93 else /* it's a length */
95 /* get length of block to copy (already have extra bits) */
96 c = t->base + ((uInt)b & inflate_mask[e]);
98 Tracevv((stderr, "inflate: * length %u\n", c));
100 /* decode distance base of block to copy */
101 GRABBITS(15); /* max bits for distance code */
102 if ((e = (t = td + ((uInt)b & md))->exop) < 0)
106 z->msg = "invalid distance code";
113 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
116 /* get extra bits to add to distance base */
117 GRABBITS((uInt)e) /* get extra bits (up to 13) */
118 d = t->base + ((uInt)b & inflate_mask[e]);
120 Tracevv((stderr, "inflate: * distance %u\n", d));
124 if ((uInt)(q - s->window) >= d) /* if offset before destination, */
127 *q++ = *r++; c--; /* minimum count is three, */
128 *q++ = *r++; c--; /* so unroll loop a little */
133 else /* else offset after destination */
135 e = d - (q - s->window); /* bytes from offset to end */
136 r = s->end - e; /* pointer to offset */
137 if (c > (uInt)e) /* if source crosses, */
139 c -= e; /* copy to end of window */
143 r = s->window; /* copy rest from start of window */
145 do { /* copy all or what's left */
150 } while (m >= 258 && n >= 10);
152 /* not enough input or output--restore pointers and return */