1 /* inffast.c -- fast decoding
2 * Copyright (C) 1995-2003 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
11 /* Allow machine dependent optimization for post-increment or pre-increment.
12 Based on testing to date,
13 Pre-increment preferred for:
15 - MIPS R5000 (Randers-Pehrson)
16 Post-increment preferred for:
18 No measurable difference:
19 - Pentium III (Anderson)
23 # define PUP(a) *(a)++
26 # define PUP(a) *++(a)
30 Decode literal, length, and distance codes and write out the resulting
31 literal and match bytes until either not enough input or output is
32 available, an end-of-block is encountered, or a data error is encountered.
33 When large enough input and output buffers are supplied to inflate(), for
34 example, a 16K input buffer and a 64K output buffer, more than 95% of the
35 inflate execution time is spent in this routine.
41 strm->avail_out >= 258
42 start >= strm->avail_out
45 On return, state->mode is one of:
47 LEN -- ran out of enough output space or enough available input
48 TYPE -- reached end of block code, inflate() to interpret next block
49 BAD -- error in block data
53 - The maximum input bits used by a length/distance pair is 15 bits for the
54 length code, 5 bits for the length extra, 15 bits for the distance code,
55 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
56 Therefore if strm->avail_in >= 6, then there is enough input to avoid
57 checking for available input while decoding.
59 - The maximum bytes that a single length/distance pair can output is 258
60 bytes, which is the maximum length that can be coded. inflate_fast()
61 requires strm->avail_out >= 258 for each loop to avoid checking for
64 void inflate_fast(strm, start)
66 unsigned start; /* inflate()'s starting value for strm->avail_out */
68 struct inflate_state FAR *state;
69 unsigned char FAR *in; /* local strm->next_in */
70 unsigned char FAR *last; /* while in < last, enough input available */
71 unsigned char FAR *out; /* local strm->next_out */
72 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
73 unsigned char FAR *end; /* while out < end, enough space available */
74 unsigned wsize; /* window size or zero if not using window */
75 unsigned write; /* window write index */
76 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
77 unsigned long hold; /* local strm->hold */
78 unsigned bits; /* local strm->bits */
79 code const FAR *lcode; /* local strm->lencode */
80 code const FAR *dcode; /* local strm->distcode */
81 unsigned lmask; /* mask for first level of length codes */
82 unsigned dmask; /* mask for first level of distance codes */
83 code this; /* retrieved table entry */
84 unsigned op; /* code bits, operation, extra bits, or */
85 /* window position, window bytes to copy */
86 unsigned len; /* match length, unused bytes */
87 unsigned dist; /* match distance */
88 unsigned char FAR *from; /* where to copy match from */
90 /* copy state to local variables */
91 state = (struct inflate_state FAR *)strm->state;
92 in = strm->next_in - OFF;
93 last = in + (strm->avail_in - 5);
94 out = strm->next_out - OFF;
95 beg = out - (start - strm->avail_out);
96 end = out + (strm->avail_out - 257);
99 window = state->window;
102 lcode = state->lencode;
103 dcode = state->distcode;
104 lmask = (1U << state->lenbits) - 1;
105 dmask = (1U << state->distbits) - 1;
107 /* decode literals and length/distances until end-of-block or not enough
108 input data or output space */
111 hold += (unsigned long)(PUP(in)) << bits;
113 hold += (unsigned long)(PUP(in)) << bits;
116 this = lcode[hold & lmask];
118 op = (unsigned)(this.bits);
121 op = (unsigned)(this.op);
122 if (op == 0) { /* literal */
123 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
124 "inflate: literal '%c'\n" :
125 "inflate: literal 0x%02x\n", this.val));
126 PUP(out) = (unsigned char)(this.val);
128 else if (op & 16) { /* length base */
129 len = (unsigned)(this.val);
130 op &= 15; /* number of extra bits */
133 hold += (unsigned long)(PUP(in)) << bits;
136 len += hold & ((1U << op) - 1);
140 Tracevv((stderr, "inflate: length %u\n", len));
142 hold += (unsigned long)(PUP(in)) << bits;
144 hold += (unsigned long)(PUP(in)) << bits;
147 this = dcode[hold & dmask];
149 op = (unsigned)(this.bits);
152 op = (unsigned)(this.op);
153 if (op & 16) { /* distance base */
154 dist = (unsigned)(this.val);
155 op &= 15; /* number of extra bits */
157 hold += (unsigned long)(PUP(in)) << bits;
160 hold += (unsigned long)(PUP(in)) << bits;
164 dist += hold & ((1U << op) - 1);
167 Tracevv((stderr, "inflate: distance %u\n", dist));
168 op = (unsigned)(out - beg); /* max distance in output */
169 if (dist > op) { /* see if copy from window */
171 strm->msg = (char *)"invalid distance too far back";
176 op = dist - op; /* distance back in window */
177 if (write == 0) { /* very common case */
179 if (op < len) { /* some from window */
182 PUP(out) = PUP(from);
184 from = out - dist; /* rest from output */
187 else if (write < op) { /* wrap around window */
188 from += wsize + write - op;
190 if (op < len) { /* some from end of window */
193 PUP(out) = PUP(from);
196 if (write < len) { /* some from start of window */
200 PUP(out) = PUP(from);
202 from = out - dist; /* rest from output */
206 else { /* contiguous in window */
208 if (op < len) { /* some from window */
211 PUP(out) = PUP(from);
213 from = out - dist; /* rest from output */
217 PUP(out) = PUP(from);
218 PUP(out) = PUP(from);
219 PUP(out) = PUP(from);
223 PUP(out) = PUP(from);
225 PUP(out) = PUP(from);
229 from = out - dist; /* copy direct from output */
230 do { /* minimum length is three */
231 PUP(out) = PUP(from);
232 PUP(out) = PUP(from);
233 PUP(out) = PUP(from);
237 PUP(out) = PUP(from);
239 PUP(out) = PUP(from);
243 else if ((op & 64) == 0) { /* 2nd level distance code */
244 this = dcode[this.val + (hold & ((1U << op) - 1))];
248 strm->msg = (char *)"invalid distance code";
253 else if ((op & 64) == 0) { /* 2nd level length code */
254 this = lcode[this.val + (hold & ((1U << op) - 1))];
257 else if (op & 32) { /* end-of-block */
258 Tracevv((stderr, "inflate: end of block\n"));
263 strm->msg = (char *)"invalid literal/length code";
267 } while (in < last && out < end);
269 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
273 hold &= (1U << bits) - 1;
275 /* update state and return */
276 strm->next_in = in + OFF;
277 strm->next_out = out + OFF;
278 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
279 strm->avail_out = (unsigned)(out < end ?
280 257 + (end - out) : 257 - (out - end));
287 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
288 - Using bit fields for code structure
289 - Different op definition to avoid & for extra bits (do & for table bits)
290 - Three separate decoding do-loops for direct, window, and write == 0
291 - Special case for distance > 1 copies to do overlapped load and store copy
292 - Explicit branch predictions (based on measured branch probabilities)
293 - Deferring match copy and interspersed it with decoding subsequent codes
294 - Swapping literal/length else
295 - Swapping window/direct else
296 - Larger unrolled copy loops (three is about right)
297 - Moving len -= 3 statement into middle of loop