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 /* simplify the use of the inflate_huft type with some defines */
12 #define base more.Base
13 #define next more.Next
14 #define exop word.what.Exop
15 #define bits word.what.Bits
17 /* macros for bit input with no checking and for returning unused bytes */
20 # define NEXTBYTE (n--?0:fprintf(stderr,"inffast underrun\n"),*p++)
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)
32 inflate_huft *tl, *td;
33 struct inflate_blocks_state *s;
36 inflate_huft *t; /* temporary pointer */
37 int e; /* extra bits or operation */
38 uLong b; /* bit buffer */
39 uInt k; /* bits in bit buffer */
40 Byte *p; /* input data pointer */
41 uInt n; /* bytes available there */
42 Byte *q; /* output window write pointer */
43 uInt m; /* bytes to end of window or read pointer */
44 uInt ml; /* mask for literal/length tree */
45 uInt md; /* mask for distance tree */
46 uInt c; /* bytes to copy */
47 uInt d; /* distance back to copy from */
48 Byte *r; /* copy source pointer */
50 /* load input, output, bit values */
53 /* initialize masks in registers */
54 ml = inflate_mask[bl];
55 md = inflate_mask[bd];
57 /* do until not enough input or output space for fast loop */
58 do { /* assume called with m >= 258 && n >= 10 */
59 /* get literal/length code */
60 GRABBITS(20) /* max bits for literal/length code */
61 if ((e = (t = tl + ((uInt)b & ml))->exop) < 0)
65 z->msg = "invalid literal/length code";
72 if (e & 64) /* end of block */
78 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
81 /* process literal or length (end of block already trapped) */
82 if (e & 16) /* then it's a literal */
87 else /* it's a length */
89 /* get length of block to copy (already have extra bits) */
90 c = t->base + ((uInt)b & inflate_mask[e]);
93 /* decode distance base of block to copy */
94 GRABBITS(15); /* max bits for distance code */
95 if ((e = (t = td + ((uInt)b & md))->exop) < 0)
99 z->msg = "invalid distance code";
106 } while ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) < 0);
109 /* get extra bits to add to distance base */
110 GRABBITS(e) /* get extra bits (up to 13) */
111 d = t->base + ((uInt)b & inflate_mask[e]);
116 if (q - s->window >= d) /* if offset before destination, */
119 *q++ = *r++; c--; /* minimum count is three, */
120 *q++ = *r++; c--; /* so unroll loop a little */
125 else /* else offset after destination */
127 e = d - (q - s->window); /* bytes from offset to end */
128 r = s->end - e; /* pointer to offset */
129 if (c > e) /* if source crosses, */
131 c -= e; /* copy to end of window */
135 r = s->window; /* copy rest from start of window */
137 do { /* copy all or what's left */
142 } while (m >= 258 && n >= 10);
144 /* not enough input or output--restore pointers and return */