1 /* inffast.c -- fast decoding
2 * Copyright (C) 1995-2004 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 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; /* while in < last, enough input available */
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 */
75 unsigned wsize; /* window size or zero if not using window */
76 unsigned whave; /* valid bytes in the window */
77 unsigned write; /* window write index */
78 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
79 unsigned long hold; /* local strm->hold */
80 unsigned bits; /* local strm->bits */
81 code const FAR *lcode; /* local strm->lencode */
82 code const FAR *dcode; /* local strm->distcode */
83 unsigned lmask; /* mask for first level of length codes */
84 unsigned dmask; /* mask for first level of distance codes */
85 code this; /* retrieved table entry */
86 unsigned op; /* code bits, operation, extra bits, or */
87 /* window position, window bytes to copy */
88 unsigned len; /* match length, unused bytes */
89 unsigned dist; /* match distance */
90 unsigned char FAR *from; /* where to copy match from */
92 /* copy state to local variables */
93 state = (struct inflate_state FAR *)strm->state;
94 in = strm->next_in - OFF;
95 last = in + (strm->avail_in - 5);
96 out = strm->next_out - OFF;
97 beg = out - (start - strm->avail_out);
98 end = out + (strm->avail_out - 257);
100 whave = state->whave;
101 write = state->write;
102 window = state->window;
105 lcode = state->lencode;
106 dcode = state->distcode;
107 lmask = (1U << state->lenbits) - 1;
108 dmask = (1U << state->distbits) - 1;
110 /* decode literals and length/distances until end-of-block or not enough
111 input data or output space */
114 hold += (unsigned long)(PUP(in)) << bits;
116 hold += (unsigned long)(PUP(in)) << bits;
119 this = lcode[hold & lmask];
121 op = (unsigned)(this.bits);
124 op = (unsigned)(this.op);
125 if (op == 0) { /* literal */
126 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
127 "inflate: literal '%c'\n" :
128 "inflate: literal 0x%02x\n", this.val));
129 PUP(out) = (unsigned char)(this.val);
131 else if (op & 16) { /* length base */
132 len = (unsigned)(this.val);
133 op &= 15; /* number of extra bits */
136 hold += (unsigned long)(PUP(in)) << bits;
139 len += (unsigned)hold & ((1U << op) - 1);
143 Tracevv((stderr, "inflate: length %u\n", len));
145 hold += (unsigned long)(PUP(in)) << bits;
147 hold += (unsigned long)(PUP(in)) << bits;
150 this = dcode[hold & dmask];
152 op = (unsigned)(this.bits);
155 op = (unsigned)(this.op);
156 if (op & 16) { /* distance base */
157 dist = (unsigned)(this.val);
158 op &= 15; /* number of extra bits */
160 hold += (unsigned long)(PUP(in)) << bits;
163 hold += (unsigned long)(PUP(in)) << bits;
167 dist += (unsigned)hold & ((1U << op) - 1);
170 Tracevv((stderr, "inflate: distance %u\n", dist));
171 op = (unsigned)(out - beg); /* max distance in output */
172 if (dist > op) { /* see if copy from window */
173 op = dist - op; /* distance back in window */
175 strm->msg = (char *)"invalid distance too far back";
180 if (write == 0) { /* very common case */
182 if (op < len) { /* some from window */
185 PUP(out) = PUP(from);
187 from = out - dist; /* rest from output */
190 else if (write < op) { /* wrap around window */
191 from += wsize + write - op;
193 if (op < len) { /* some from end of window */
196 PUP(out) = PUP(from);
199 if (write < len) { /* some from start of window */
203 PUP(out) = PUP(from);
205 from = out - dist; /* rest from output */
209 else { /* contiguous in window */
211 if (op < len) { /* some from window */
214 PUP(out) = PUP(from);
216 from = out - dist; /* rest from output */
220 PUP(out) = PUP(from);
221 PUP(out) = PUP(from);
222 PUP(out) = PUP(from);
226 PUP(out) = PUP(from);
228 PUP(out) = PUP(from);
232 from = out - dist; /* copy direct from output */
233 do { /* minimum length is three */
234 PUP(out) = PUP(from);
235 PUP(out) = PUP(from);
236 PUP(out) = PUP(from);
240 PUP(out) = PUP(from);
242 PUP(out) = PUP(from);
246 else if ((op & 64) == 0) { /* 2nd level distance code */
247 this = dcode[this.val + (hold & ((1U << op) - 1))];
251 strm->msg = (char *)"invalid distance code";
256 else if ((op & 64) == 0) { /* 2nd level length code */
257 this = lcode[this.val + (hold & ((1U << op) - 1))];
260 else if (op & 32) { /* end-of-block */
261 Tracevv((stderr, "inflate: end of block\n"));
266 strm->msg = (char *)"invalid literal/length code";
270 } while (in < last && out < end);
272 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
276 hold &= (1U << bits) - 1;
278 /* update state and return */
279 strm->next_in = in + OFF;
280 strm->next_out = out + OFF;
281 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
282 strm->avail_out = (unsigned)(out < end ?
283 257 + (end - out) : 257 - (out - end));
290 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
291 - Using bit fields for code structure
292 - Different op definition to avoid & for extra bits (do & for table bits)
293 - Three separate decoding do-loops for direct, window, and write == 0
294 - Special case for distance > 1 copies to do overlapped load and store copy
295 - Explicit branch predictions (based on measured branch probabilities)
296 - Deferring match copy and interspersed it with decoding subsequent codes
297 - Swapping literal/length else
298 - Swapping window/direct else
299 - Larger unrolled copy loops (three is about right)
300 - Moving len -= 3 statement into middle of loop