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