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