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