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