1 /* inffast.c -- fast decoding
2 * Copyright (C) 1995-2008, 2010, 2013 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
13 /* Allow machine dependent optimization for post-increment or pre-increment.
14 Based on testing to date,
15 Pre-increment preferred for:
17 - MIPS R5000 (Randers-Pehrson)
18 Post-increment preferred for:
20 No measurable difference:
21 - Pentium III (Anderson)
26 # define PUP(a) *(a)++
29 # define PUP(a) *++(a)
33 Decode literal, length, and distance codes and write out the resulting
34 literal and match bytes until either not enough input or output is
35 available, an end-of-block is encountered, or a data error is encountered.
36 When large enough input and output buffers are supplied to inflate(), for
37 example, a 16K input buffer and a 64K output buffer, more than 95% of the
38 inflate execution time is spent in this routine.
44 strm->avail_out >= 258
45 start >= strm->avail_out
48 On return, state->mode is one of:
50 LEN -- ran out of enough output space or enough available input
51 TYPE -- reached end of block code, inflate() to interpret next block
52 BAD -- error in block data
56 - The maximum input bits used by a length/distance pair is 15 bits for the
57 length code, 5 bits for the length extra, 15 bits for the distance code,
58 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
59 Therefore if strm->avail_in >= 6, then there is enough input to avoid
60 checking for available input while decoding.
62 - The maximum bytes that a single length/distance pair can output is 258
63 bytes, which is the maximum length that can be coded. inflate_fast()
64 requires strm->avail_out >= 258 for each loop to avoid checking for
67 void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start)
69 struct inflate_state FAR *state;
70 unsigned char FAR *in; /* local strm->next_in */
71 unsigned char FAR *last; /* have enough input while in < last */
72 unsigned char FAR *out; /* local strm->next_out */
73 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
74 unsigned char FAR *end; /* while out < end, enough space available */
76 unsigned dmax; /* maximum distance from zlib header */
78 unsigned wsize; /* window size or zero if not using window */
79 unsigned whave; /* valid bytes in the window */
80 unsigned wnext; /* window write index */
81 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
82 unsigned long hold; /* local strm->hold */
83 unsigned bits; /* local strm->bits */
84 code const FAR *lcode; /* local strm->lencode */
85 code const FAR *dcode; /* local strm->distcode */
86 unsigned lmask; /* mask for first level of length codes */
87 unsigned dmask; /* mask for first level of distance codes */
88 code here; /* retrieved table entry */
89 unsigned op; /* code bits, operation, extra bits, or */
90 /* window position, window bytes to copy */
91 unsigned len; /* match length, unused bytes */
92 unsigned dist; /* match distance */
93 unsigned char FAR *from; /* where to copy match from */
95 /* copy state to local variables */
96 state = (struct inflate_state FAR *)strm->state;
97 in = strm->next_in - OFF;
98 last = in + (strm->avail_in - 5);
99 out = strm->next_out - OFF;
100 beg = out - (start - strm->avail_out);
101 end = out + (strm->avail_out - 257);
102 #ifdef INFLATE_STRICT
105 wsize = state->wsize;
106 whave = state->whave;
107 wnext = state->wnext;
108 window = state->window;
111 lcode = state->lencode;
112 dcode = state->distcode;
113 lmask = (1U << state->lenbits) - 1;
114 dmask = (1U << state->distbits) - 1;
116 /* decode literals and length/distances until end-of-block or not enough
117 input data or output space */
120 hold += (unsigned long)(PUP(in)) << bits;
122 hold += (unsigned long)(PUP(in)) << bits;
125 here = lcode[hold & lmask];
127 op = (unsigned)(here.bits);
130 op = (unsigned)(here.op);
131 if (op == 0) { /* literal */
132 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
133 "inflate: literal '%c'\n" :
134 "inflate: literal 0x%02x\n", here.val));
135 PUP(out) = (unsigned char)(here.val);
137 else if (op & 16) { /* length base */
138 len = (unsigned)(here.val);
139 op &= 15; /* number of extra bits */
142 hold += (unsigned long)(PUP(in)) << bits;
145 len += (unsigned)hold & ((1U << op) - 1);
149 Tracevv((stderr, "inflate: length %u\n", len));
151 hold += (unsigned long)(PUP(in)) << bits;
153 hold += (unsigned long)(PUP(in)) << bits;
156 here = dcode[hold & dmask];
158 op = (unsigned)(here.bits);
161 op = (unsigned)(here.op);
162 if (op & 16) { /* distance base */
163 dist = (unsigned)(here.val);
164 op &= 15; /* number of extra bits */
166 hold += (unsigned long)(PUP(in)) << bits;
169 hold += (unsigned long)(PUP(in)) << bits;
173 dist += (unsigned)hold & ((1U << op) - 1);
174 #ifdef INFLATE_STRICT
176 strm->msg = (char *)"invalid distance too far back";
183 Tracevv((stderr, "inflate: distance %u\n", dist));
184 op = (unsigned)(out - beg); /* max distance in output */
185 if (dist > op) { /* see if copy from window */
186 op = dist - op; /* distance back in window */
190 (char *)"invalid distance too far back";
194 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
195 if (len <= op - whave) {
204 } while (--op > whave);
208 PUP(out) = PUP(from);
215 if (wnext == 0) { /* very common case */
217 if (op < len) { /* some from window */
220 PUP(out) = PUP(from);
222 from = out - dist; /* rest from output */
225 else if (wnext < op) { /* wrap around window */
226 from += wsize + wnext - op;
228 if (op < len) { /* some from end of window */
231 PUP(out) = PUP(from);
234 if (wnext < len) { /* some from start of window */
238 PUP(out) = PUP(from);
240 from = out - dist; /* rest from output */
244 else { /* contiguous in window */
246 if (op < len) { /* some from window */
249 PUP(out) = PUP(from);
251 from = out - dist; /* rest from output */
255 PUP(out) = PUP(from);
256 PUP(out) = PUP(from);
257 PUP(out) = PUP(from);
261 PUP(out) = PUP(from);
263 PUP(out) = PUP(from);
267 from = out - dist; /* copy direct from output */
268 do { /* minimum length is three */
269 PUP(out) = PUP(from);
270 PUP(out) = PUP(from);
271 PUP(out) = PUP(from);
275 PUP(out) = PUP(from);
277 PUP(out) = PUP(from);
281 else if ((op & 64) == 0) { /* 2nd level distance code */
282 here = dcode[here.val + (hold & ((1U << op) - 1))];
286 strm->msg = (char *)"invalid distance code";
291 else if ((op & 64) == 0) { /* 2nd level length code */
292 here = lcode[here.val + (hold & ((1U << op) - 1))];
295 else if (op & 32) { /* end-of-block */
296 Tracevv((stderr, "inflate: end of block\n"));
301 strm->msg = (char *)"invalid literal/length code";
305 } while (in < last && out < end);
307 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
311 hold &= (1U << bits) - 1;
313 /* update state and return */
314 strm->next_in = in + OFF;
315 strm->next_out = out + OFF;
316 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
317 strm->avail_out = (unsigned)(out < end ?
318 257 + (end - out) : 257 - (out - end));
325 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
326 - Using bit fields for code structure
327 - Different op definition to avoid & for extra bits (do & for table bits)
328 - Three separate decoding do-loops for direct, window, and wnext == 0
329 - Special case for distance > 1 copies to do overlapped load and store copy
330 - Explicit branch predictions (based on measured branch probabilities)
331 - Deferring match copy and interspersed it with decoding subsequent codes
332 - Swapping literal/length else
333 - Swapping window/direct else
334 - Larger unrolled copy loops (three is about right)
335 - Moving len -= 3 statement into middle of loop