1*3886Sahl /* inffast.c -- fast decoding
2*3886Sahl * Copyright (C) 1995-2004 Mark Adler
3*3886Sahl * For conditions of distribution and use, see copyright notice in zlib.h
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
70Sstevel@tonic-gate
80Sstevel@tonic-gate #include "zutil.h"
90Sstevel@tonic-gate #include "inftrees.h"
10*3886Sahl #include "inflate.h"
110Sstevel@tonic-gate #include "inffast.h"
120Sstevel@tonic-gate
13*3886Sahl #ifndef ASMINF
140Sstevel@tonic-gate
15*3886Sahl /* Allow machine dependent optimization for post-increment or pre-increment.
16*3886Sahl Based on testing to date,
17*3886Sahl Pre-increment preferred for:
18*3886Sahl - PowerPC G3 (Adler)
19*3886Sahl - MIPS R5000 (Randers-Pehrson)
20*3886Sahl Post-increment preferred for:
21*3886Sahl - none
22*3886Sahl No measurable difference:
23*3886Sahl - Pentium III (Anderson)
24*3886Sahl - M68060 (Nikl)
25*3886Sahl */
26*3886Sahl #ifdef POSTINC
27*3886Sahl # define OFF 0
28*3886Sahl # define PUP(a) *(a)++
29*3886Sahl #else
30*3886Sahl # define OFF 1
31*3886Sahl # define PUP(a) *++(a)
32*3886Sahl #endif
330Sstevel@tonic-gate
34*3886Sahl /*
35*3886Sahl Decode literal, length, and distance codes and write out the resulting
36*3886Sahl literal and match bytes until either not enough input or output is
37*3886Sahl available, an end-of-block is encountered, or a data error is encountered.
38*3886Sahl When large enough input and output buffers are supplied to inflate(), for
39*3886Sahl example, a 16K input buffer and a 64K output buffer, more than 95% of the
40*3886Sahl inflate execution time is spent in this routine.
41*3886Sahl
42*3886Sahl Entry assumptions:
430Sstevel@tonic-gate
44*3886Sahl state->mode == LEN
45*3886Sahl strm->avail_in >= 6
46*3886Sahl strm->avail_out >= 258
47*3886Sahl start >= strm->avail_out
48*3886Sahl state->bits < 8
49*3886Sahl
50*3886Sahl On return, state->mode is one of:
51*3886Sahl
52*3886Sahl LEN -- ran out of enough output space or enough available input
53*3886Sahl TYPE -- reached end of block code, inflate() to interpret next block
54*3886Sahl BAD -- error in block data
55*3886Sahl
56*3886Sahl Notes:
57*3886Sahl
58*3886Sahl - The maximum input bits used by a length/distance pair is 15 bits for the
59*3886Sahl length code, 5 bits for the length extra, 15 bits for the distance code,
60*3886Sahl and 13 bits for the distance extra. This totals 48 bits, or six bytes.
61*3886Sahl Therefore if strm->avail_in >= 6, then there is enough input to avoid
62*3886Sahl checking for available input while decoding.
630Sstevel@tonic-gate
64*3886Sahl - The maximum bytes that a single length/distance pair can output is 258
65*3886Sahl bytes, which is the maximum length that can be coded. inflate_fast()
66*3886Sahl requires strm->avail_out >= 258 for each loop to avoid checking for
67*3886Sahl output space.
68*3886Sahl */
inflate_fast(strm,start)69*3886Sahl void inflate_fast(strm, start)
70*3886Sahl z_streamp strm;
71*3886Sahl unsigned start; /* inflate()'s starting value for strm->avail_out */
72*3886Sahl {
73*3886Sahl struct inflate_state FAR *state;
74*3886Sahl unsigned char FAR *in; /* local strm->next_in */
75*3886Sahl unsigned char FAR *last; /* while in < last, enough input available */
76*3886Sahl unsigned char FAR *out; /* local strm->next_out */
77*3886Sahl unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
78*3886Sahl unsigned char FAR *end; /* while out < end, enough space available */
79*3886Sahl #ifdef INFLATE_STRICT
80*3886Sahl unsigned dmax; /* maximum distance from zlib header */
81*3886Sahl #endif
82*3886Sahl unsigned wsize; /* window size or zero if not using window */
83*3886Sahl unsigned whave; /* valid bytes in the window */
84*3886Sahl unsigned write; /* window write index */
85*3886Sahl unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
86*3886Sahl unsigned long hold; /* local strm->hold */
87*3886Sahl unsigned bits; /* local strm->bits */
88*3886Sahl code const FAR *lcode; /* local strm->lencode */
89*3886Sahl code const FAR *dcode; /* local strm->distcode */
90*3886Sahl unsigned lmask; /* mask for first level of length codes */
91*3886Sahl unsigned dmask; /* mask for first level of distance codes */
92*3886Sahl code this; /* retrieved table entry */
93*3886Sahl unsigned op; /* code bits, operation, extra bits, or */
94*3886Sahl /* window position, window bytes to copy */
95*3886Sahl unsigned len; /* match length, unused bytes */
96*3886Sahl unsigned dist; /* match distance */
97*3886Sahl unsigned char FAR *from; /* where to copy match from */
980Sstevel@tonic-gate
99*3886Sahl /* copy state to local variables */
100*3886Sahl state = (struct inflate_state FAR *)strm->state;
101*3886Sahl in = strm->next_in - OFF;
102*3886Sahl last = in + (strm->avail_in - 5);
103*3886Sahl out = strm->next_out - OFF;
104*3886Sahl beg = out - (start - strm->avail_out);
105*3886Sahl end = out + (strm->avail_out - 257);
106*3886Sahl #ifdef INFLATE_STRICT
107*3886Sahl dmax = state->dmax;
108*3886Sahl #endif
109*3886Sahl wsize = state->wsize;
110*3886Sahl whave = state->whave;
111*3886Sahl write = state->write;
112*3886Sahl window = state->window;
113*3886Sahl hold = state->hold;
114*3886Sahl bits = state->bits;
115*3886Sahl lcode = state->lencode;
116*3886Sahl dcode = state->distcode;
117*3886Sahl lmask = (1U << state->lenbits) - 1;
118*3886Sahl dmask = (1U << state->distbits) - 1;
1190Sstevel@tonic-gate
120*3886Sahl /* decode literals and length/distances until end-of-block or not enough
121*3886Sahl input data or output space */
122*3886Sahl do {
123*3886Sahl if (bits < 15) {
124*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
125*3886Sahl bits += 8;
126*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
127*3886Sahl bits += 8;
128*3886Sahl }
129*3886Sahl this = lcode[hold & lmask];
130*3886Sahl dolen:
131*3886Sahl op = (unsigned)(this.bits);
132*3886Sahl hold >>= op;
133*3886Sahl bits -= op;
134*3886Sahl op = (unsigned)(this.op);
135*3886Sahl if (op == 0) { /* literal */
136*3886Sahl Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
137*3886Sahl "inflate: literal '%c'\n" :
138*3886Sahl "inflate: literal 0x%02x\n", this.val));
139*3886Sahl PUP(out) = (unsigned char)(this.val);
140*3886Sahl }
141*3886Sahl else if (op & 16) { /* length base */
142*3886Sahl len = (unsigned)(this.val);
143*3886Sahl op &= 15; /* number of extra bits */
144*3886Sahl if (op) {
145*3886Sahl if (bits < op) {
146*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
147*3886Sahl bits += 8;
148*3886Sahl }
149*3886Sahl len += (unsigned)hold & ((1U << op) - 1);
150*3886Sahl hold >>= op;
151*3886Sahl bits -= op;
152*3886Sahl }
153*3886Sahl Tracevv((stderr, "inflate: length %u\n", len));
154*3886Sahl if (bits < 15) {
155*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
156*3886Sahl bits += 8;
157*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
158*3886Sahl bits += 8;
1590Sstevel@tonic-gate }
160*3886Sahl this = dcode[hold & dmask];
161*3886Sahl dodist:
162*3886Sahl op = (unsigned)(this.bits);
163*3886Sahl hold >>= op;
164*3886Sahl bits -= op;
165*3886Sahl op = (unsigned)(this.op);
166*3886Sahl if (op & 16) { /* distance base */
167*3886Sahl dist = (unsigned)(this.val);
168*3886Sahl op &= 15; /* number of extra bits */
169*3886Sahl if (bits < op) {
170*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
171*3886Sahl bits += 8;
172*3886Sahl if (bits < op) {
173*3886Sahl hold += (unsigned long)(PUP(in)) << bits;
174*3886Sahl bits += 8;
175*3886Sahl }
176*3886Sahl }
177*3886Sahl dist += (unsigned)hold & ((1U << op) - 1);
178*3886Sahl #ifdef INFLATE_STRICT
179*3886Sahl if (dist > dmax) {
180*3886Sahl strm->msg = (char *)"invalid distance too far back";
181*3886Sahl state->mode = BAD;
182*3886Sahl break;
183*3886Sahl }
184*3886Sahl #endif
185*3886Sahl hold >>= op;
186*3886Sahl bits -= op;
187*3886Sahl Tracevv((stderr, "inflate: distance %u\n", dist));
188*3886Sahl op = (unsigned)(out - beg); /* max distance in output */
189*3886Sahl if (dist > op) { /* see if copy from window */
190*3886Sahl op = dist - op; /* distance back in window */
191*3886Sahl if (op > whave) {
192*3886Sahl strm->msg = (char *)"invalid distance too far back";
193*3886Sahl state->mode = BAD;
194*3886Sahl break;
195*3886Sahl }
196*3886Sahl from = window - OFF;
197*3886Sahl if (write == 0) { /* very common case */
198*3886Sahl from += wsize - op;
199*3886Sahl if (op < len) { /* some from window */
200*3886Sahl len -= op;
201*3886Sahl do {
202*3886Sahl PUP(out) = PUP(from);
203*3886Sahl } while (--op);
204*3886Sahl from = out - dist; /* rest from output */
205*3886Sahl }
206*3886Sahl }
207*3886Sahl else if (write < op) { /* wrap around window */
208*3886Sahl from += wsize + write - op;
209*3886Sahl op -= write;
210*3886Sahl if (op < len) { /* some from end of window */
211*3886Sahl len -= op;
212*3886Sahl do {
213*3886Sahl PUP(out) = PUP(from);
214*3886Sahl } while (--op);
215*3886Sahl from = window - OFF;
216*3886Sahl if (write < len) { /* some from start of window */
217*3886Sahl op = write;
218*3886Sahl len -= op;
219*3886Sahl do {
220*3886Sahl PUP(out) = PUP(from);
221*3886Sahl } while (--op);
222*3886Sahl from = out - dist; /* rest from output */
223*3886Sahl }
224*3886Sahl }
225*3886Sahl }
226*3886Sahl else { /* contiguous in window */
227*3886Sahl from += write - op;
228*3886Sahl if (op < len) { /* some from window */
229*3886Sahl len -= op;
230*3886Sahl do {
231*3886Sahl PUP(out) = PUP(from);
232*3886Sahl } while (--op);
233*3886Sahl from = out - dist; /* rest from output */
234*3886Sahl }
235*3886Sahl }
236*3886Sahl while (len > 2) {
237*3886Sahl PUP(out) = PUP(from);
238*3886Sahl PUP(out) = PUP(from);
239*3886Sahl PUP(out) = PUP(from);
240*3886Sahl len -= 3;
241*3886Sahl }
242*3886Sahl if (len) {
243*3886Sahl PUP(out) = PUP(from);
244*3886Sahl if (len > 1)
245*3886Sahl PUP(out) = PUP(from);
246*3886Sahl }
247*3886Sahl }
248*3886Sahl else {
249*3886Sahl from = out - dist; /* copy direct from output */
250*3886Sahl do { /* minimum length is three */
251*3886Sahl PUP(out) = PUP(from);
252*3886Sahl PUP(out) = PUP(from);
253*3886Sahl PUP(out) = PUP(from);
254*3886Sahl len -= 3;
255*3886Sahl } while (len > 2);
256*3886Sahl if (len) {
257*3886Sahl PUP(out) = PUP(from);
258*3886Sahl if (len > 1)
259*3886Sahl PUP(out) = PUP(from);
260*3886Sahl }
261*3886Sahl }
2620Sstevel@tonic-gate }
263*3886Sahl else if ((op & 64) == 0) { /* 2nd level distance code */
264*3886Sahl this = dcode[this.val + (hold & ((1U << op) - 1))];
265*3886Sahl goto dodist;
266*3886Sahl }
267*3886Sahl else {
268*3886Sahl strm->msg = (char *)"invalid distance code";
269*3886Sahl state->mode = BAD;
270*3886Sahl break;
271*3886Sahl }
272*3886Sahl }
273*3886Sahl else if ((op & 64) == 0) { /* 2nd level length code */
274*3886Sahl this = lcode[this.val + (hold & ((1U << op) - 1))];
275*3886Sahl goto dolen;
276*3886Sahl }
277*3886Sahl else if (op & 32) { /* end-of-block */
278*3886Sahl Tracevv((stderr, "inflate: end of block\n"));
279*3886Sahl state->mode = TYPE;
2800Sstevel@tonic-gate break;
281*3886Sahl }
282*3886Sahl else {
283*3886Sahl strm->msg = (char *)"invalid literal/length code";
284*3886Sahl state->mode = BAD;
285*3886Sahl break;
2860Sstevel@tonic-gate }
287*3886Sahl } while (in < last && out < end);
288*3886Sahl
289*3886Sahl /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
290*3886Sahl len = bits >> 3;
291*3886Sahl in -= len;
292*3886Sahl bits -= len << 3;
293*3886Sahl hold &= (1U << bits) - 1;
2940Sstevel@tonic-gate
295*3886Sahl /* update state and return */
296*3886Sahl strm->next_in = in + OFF;
297*3886Sahl strm->next_out = out + OFF;
298*3886Sahl strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
299*3886Sahl strm->avail_out = (unsigned)(out < end ?
300*3886Sahl 257 + (end - out) : 257 - (out - end));
301*3886Sahl state->hold = hold;
302*3886Sahl state->bits = bits;
303*3886Sahl return;
3040Sstevel@tonic-gate }
305*3886Sahl
306*3886Sahl /*
307*3886Sahl inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
308*3886Sahl - Using bit fields for code structure
309*3886Sahl - Different op definition to avoid & for extra bits (do & for table bits)
310*3886Sahl - Three separate decoding do-loops for direct, window, and write == 0
311*3886Sahl - Special case for distance > 1 copies to do overlapped load and store copy
312*3886Sahl - Explicit branch predictions (based on measured branch probabilities)
313*3886Sahl - Deferring match copy and interspersed it with decoding subsequent codes
314*3886Sahl - Swapping literal/length else
315*3886Sahl - Swapping window/direct else
316*3886Sahl - Larger unrolled copy loops (three is about right)
317*3886Sahl - Moving len -= 3 statement into middle of loop
318*3886Sahl */
319*3886Sahl
320*3886Sahl #endif /* !ASMINF */
321