Line | Count | Source |
1 | | /* inffast.c -- fast decoding |
2 | | * Copyright (C) 1995-2026 Mark Adler |
3 | | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | | */ |
5 | | |
6 | | #include "zutil.h" |
7 | | #include "inftrees.h" |
8 | | #include "inflate.h" |
9 | | #include "inffast.h" |
10 | | |
11 | | /* Z_INFLATE_FETCH defines how compressed data is read from the buffer into the |
12 | | processor, and is the number of bits loaded by a single instruction, with |
13 | | the value 64, 32, or 8. 64 or 32-bit loads are unaligned, and so are used |
14 | | only where that's fast, when Z_UNALIGNED_FAST is defined. In that case, 64 |
15 | | bits is used if Z_REG64 is defined, otherwise 32 bits is used. Z_REG32 can |
16 | | be defined to override what would be a 64-bit ruling below. Where unaligned |
17 | | loads are slow or not known to be fast, or the register size was not |
18 | | determined, the byte-loading loop, 8, is the default. That loop is always |
19 | | present to pick up remaining work after the fast word-loading loop is done. |
20 | | Any of the three choices can be forced by defining Z_INFLATE_FETCH before |
21 | | this. To help the compiler do the right thing, zmemcpy() is used in certain |
22 | | cases to load the word. Z_INFLATE_BYTES can be defined to avoid the use of |
23 | | zmemcpy(), instead loading byte-by-byte in the source code. This is to |
24 | | facilitate testing the code for coverage and to expose compiler behaviors. |
25 | | It should not be defined for production code. Both the 64-bit and 32-bit |
26 | | fetch loops copy matches in blocks of eight or sixteen bytes, which are |
27 | | performed only if unaligned access is fast. */ |
28 | | #ifndef Z_INFLATE_FETCH |
29 | | # if defined(Z_UNALIGNED_FAST) && defined(Z_REG64) |
30 | | # define Z_INFLATE_FETCH 64 |
31 | | # elif defined(Z_UNALIGNED_FAST) && defined(Z_U4) |
32 | | # define Z_INFLATE_FETCH 32 |
33 | | # else |
34 | | # define Z_INFLATE_FETCH 8 |
35 | | # endif |
36 | | #endif |
37 | | #if Z_INFLATE_FETCH == 64 && !defined(Z_U8) |
38 | | # error Z_INFLATE_FETCH 64 needs a 64-bit type (Z_U8) |
39 | | #endif |
40 | | #if Z_INFLATE_FETCH == 32 && !defined(Z_U4) |
41 | | # error Z_INFLATE_FETCH 32 needs a 32-bit type (Z_U4) |
42 | | #endif |
43 | | #if Z_INFLATE_FETCH != 8 && Z_INFLATE_FETCH != 32 && Z_INFLATE_FETCH != 64 |
44 | | # error Z_INFLATE_FETCH must be 8, 32, or 64 |
45 | | #endif |
46 | | |
47 | | #ifdef ASMINF |
48 | | # pragma message("Assembler code may have bugs -- use at your own risk") |
49 | | #else |
50 | | |
51 | | #if Z_INFLATE_FETCH != 8 |
52 | | /* Fast loops. The bit buffer is refilled eight bytes at a time in the 64-bit |
53 | | loop and four in the 32-bit loop, and in both, matches are copied eight or |
54 | | sixteen bytes at a time. Only the 64-bit loop needs a 64-bit type. */ |
55 | | |
56 | | #define TRACE_LITERAL(here) \ |
57 | | Tracevv((stderr, (here)->val >= 0x20 && (here)->val < 0x7f ? \ |
58 | | "inflate: literal '%c'\n" : \ |
59 | | "inflate: literal 0x%02x\n", (here)->val)) |
60 | | |
61 | | #if Z_INFLATE_FETCH == 64 |
62 | | /* A byte swap of a 64-bit word, where the compiler has one. */ |
63 | | #if defined(__has_builtin) |
64 | | # if __has_builtin(__builtin_bswap64) |
65 | 0 | # define Z_BSWAP64 __builtin_bswap64 |
66 | | # endif |
67 | | #elif defined(__GNUC__) && \ |
68 | | (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) |
69 | | # define Z_BSWAP64 __builtin_bswap64 |
70 | | #endif |
71 | | |
72 | | /* Return the eight bytes at p in little-endian order. This should compile to |
73 | | one load on little-endian, or to one load and a swap (sometimes one |
74 | | instruction) on big-endian. Either way, inlined with no function call and no |
75 | | if condition surviving. Z_INFLATE_BYTES is for testing only. */ |
76 | 2.60M | local Z_U8 load64(z_const unsigned char FAR *p) { |
77 | 2.60M | #ifndef Z_INFLATE_BYTES |
78 | 2.60M | Z_U8 v = 1; |
79 | 2.60M | if (*(unsigned char *)&v) { |
80 | 2.60M | zmemcpy(&v, p, 8); |
81 | 2.60M | return v; |
82 | 2.60M | } |
83 | 0 | # ifdef Z_BSWAP64 |
84 | 0 | zmemcpy(&v, p, 8); |
85 | 0 | return Z_BSWAP64(v); |
86 | 0 | # endif |
87 | 0 | #endif |
88 | 0 | return (Z_U8)p[0] | ((Z_U8)p[1] << 8) | ((Z_U8)p[2] << 16) | |
89 | 0 | ((Z_U8)p[3] << 24) | ((Z_U8)p[4] << 32) | ((Z_U8)p[5] << 40) | |
90 | 0 | ((Z_U8)p[6] << 48) | ((Z_U8)p[7] << 56); |
91 | 2.60M | } |
92 | | #endif |
93 | | |
94 | | /* Copy len >= 1 bytes from dist bytes back in the output to out, and return |
95 | | out + len. It may write up to fifteen bytes after out + len, so the caller |
96 | | must have that room. */ |
97 | | local unsigned char FAR *copy_back(unsigned char FAR *out, unsigned dist, |
98 | 1.43M | unsigned len) { |
99 | 1.43M | unsigned char FAR *from = out - dist; |
100 | 1.43M | unsigned char FAR *stop = out + len; |
101 | | |
102 | 1.43M | if (dist >= 16) { /* sixteen at a time, no overlap */ |
103 | 4.28M | do { |
104 | 4.28M | zmemcpy(out, from, 16); |
105 | 4.28M | out += 16; |
106 | 4.28M | from += 16; |
107 | 4.28M | } while (out < stop); |
108 | 396k | return stop; |
109 | 396k | } |
110 | 1.04M | if (dist >= 8) { /* eight at a time, no overlap */ |
111 | 8.06k | do { |
112 | 8.06k | zmemcpy(out, from, 8); |
113 | 8.06k | out += 8; |
114 | 8.06k | from += 8; |
115 | 8.06k | } while (out < stop); |
116 | 1.40k | return stop; |
117 | 1.40k | } |
118 | 1.04M | if (dist == 1) { /* a run of one byte, eight at once */ |
119 | 461k | #ifdef Z_U8 |
120 | 461k | Z_U8 run = *from; |
121 | 461k | run |= run << 8; |
122 | 461k | run |= run << 16; |
123 | 461k | run |= run << 32; |
124 | 7.36M | do { |
125 | 7.36M | zmemcpy(out, &run, 8); |
126 | 7.36M | out += 8; |
127 | 7.36M | } while (out < stop); |
128 | | #else |
129 | | Z_U4 run = *from; |
130 | | run |= run << 8; |
131 | | run |= run << 16; |
132 | | do { |
133 | | zmemcpy(out, &run, 4); |
134 | | zmemcpy(out + 4, &run, 4); |
135 | | out += 8; |
136 | | } while (out < stop); |
137 | | #endif |
138 | 461k | return stop; |
139 | 461k | } |
140 | | /* dist 2..7: eight bytes per step, of which the first dist are right; |
141 | | the next step rewrites the rest. All eight are read before any are |
142 | | written, since they overlap. */ |
143 | 68.2M | do { |
144 | 68.2M | #ifdef Z_U8 |
145 | 68.2M | Z_U8 v; |
146 | 68.2M | zmemcpy(&v, from, 8); |
147 | 68.2M | zmemcpy(out, &v, 8); |
148 | | #else |
149 | | Z_U4 v0, v1; |
150 | | zmemcpy(&v0, from, 4); |
151 | | zmemcpy(&v1, from + 4, 4); |
152 | | zmemcpy(out, &v0, 4); |
153 | | zmemcpy(out + 4, &v1, 4); |
154 | | #endif |
155 | 68.2M | out += dist; |
156 | 68.2M | from += dist; |
157 | 68.2M | } while (out < stop); |
158 | 580k | return stop; |
159 | 1.04M | } |
160 | | #endif |
161 | | |
162 | | #if Z_INFLATE_FETCH == 32 |
163 | | /* Return the four bytes at p in little-endian order. This should compile to |
164 | | one load on little-endian, or to one load and a swap (sometimes one |
165 | | instruction) on big-endian. Either way, inlined with no function call and no |
166 | | if condition surviving. Z_INFLATE_BYTES is for testing only. */ |
167 | | local Z_U4 load32(z_const unsigned char FAR *p) { |
168 | | #ifndef Z_INFLATE_BYTES |
169 | | Z_U4 v = 1; |
170 | | if (*(unsigned char *)&v) { |
171 | | zmemcpy(&v, p, 4); |
172 | | return v; |
173 | | } |
174 | | #endif |
175 | | return (Z_U4)p[0] | ((Z_U4)p[1] << 8) | ((Z_U4)p[2] << 16) | |
176 | | ((Z_U4)p[3] << 24); |
177 | | } |
178 | | #endif |
179 | | |
180 | | /* |
181 | | Decode literal, length, and distance codes and write out the resulting |
182 | | literal and match bytes until either not enough input or output is |
183 | | available, an end-of-block is encountered, or a data error is encountered. |
184 | | When large enough input and output buffers are supplied to inflate(), for |
185 | | example, a 16K input buffer and a 64K output buffer, more than 95% of the |
186 | | inflate execution time is spent in this routine. |
187 | | |
188 | | Entry assumptions: |
189 | | |
190 | | state->mode == LEN |
191 | | strm->avail_in >= 6 |
192 | | strm->avail_out >= 258 |
193 | | start >= strm->avail_out |
194 | | state->bits < 8 |
195 | | |
196 | | On return, state->mode is one of: |
197 | | |
198 | | LEN -- ran out of enough output space or enough available input |
199 | | TYPE -- reached end of block code, inflate() to interpret next block |
200 | | BAD -- error in block data |
201 | | |
202 | | Notes: |
203 | | |
204 | | - The maximum input bits used by a length/distance pair is 15 bits for the |
205 | | length code, 5 bits for the length extra, 15 bits for the distance code, |
206 | | and 13 bits for the distance extra. This totals 48 bits, or six bytes. |
207 | | Therefore if strm->avail_in >= 6, then there is enough input to avoid |
208 | | checking for available input while decoding. |
209 | | |
210 | | - The maximum bytes that a single length/distance pair can output is 258 |
211 | | bytes, which is the maximum length that can be coded. inflate_fast() |
212 | | requires strm->avail_out >= 258 for each loop to avoid checking for |
213 | | output space. |
214 | | */ |
215 | 67.0k | void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) { |
216 | 67.0k | struct inflate_state FAR *state; |
217 | 67.0k | z_const unsigned char FAR *in; /* local strm->next_in */ |
218 | 67.0k | z_const unsigned char FAR *last; /* have enough input while in < last */ |
219 | 67.0k | unsigned char FAR *out; /* local strm->next_out */ |
220 | 67.0k | unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ |
221 | 67.0k | unsigned char FAR *end; /* while out < end, enough space available */ |
222 | | #ifdef INFLATE_STRICT |
223 | | unsigned dmax; /* maximum distance from zlib header */ |
224 | | #endif |
225 | 67.0k | unsigned wsize; /* window size or zero if not using window */ |
226 | 67.0k | unsigned whave; /* valid bytes in the window */ |
227 | 67.0k | unsigned wnext; /* window write index */ |
228 | 67.0k | unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ |
229 | 67.0k | #if Z_INFLATE_FETCH != 8 |
230 | 67.0k | int back; /* decoding into the window: inflateBack() */ |
231 | 67.0k | #endif |
232 | 67.0k | unsigned long hold; /* local strm->hold */ |
233 | 67.0k | unsigned bits; /* local strm->bits */ |
234 | 67.0k | code const FAR *lcode; /* local strm->lencode */ |
235 | 67.0k | code const FAR *dcode; /* local strm->distcode */ |
236 | 67.0k | unsigned lmask; /* mask for first level of length codes */ |
237 | 67.0k | unsigned dmask; /* mask for first level of distance codes */ |
238 | 67.0k | code const *here; /* retrieved table entry */ |
239 | 67.0k | unsigned op; /* code bits, operation, extra bits, or */ |
240 | | /* window position, window bytes to copy */ |
241 | 67.0k | unsigned len; /* match length, unused bytes */ |
242 | 67.0k | unsigned dist; /* match distance */ |
243 | 67.0k | unsigned char FAR *from; /* where to copy match from */ |
244 | 67.0k | #if Z_INFLATE_FETCH == 64 |
245 | 67.0k | Z_U8 hold8; /* 64-bit bit buffer of the fast loop */ |
246 | 67.0k | z_const unsigned char FAR *last8; /* two refills fit after in */ |
247 | 67.0k | unsigned char FAR *end8; /* room for two literals, a match, 15 more */ |
248 | 67.0k | #endif |
249 | | #if Z_INFLATE_FETCH == 32 |
250 | | Z_U4 hold4; /* 32-bit bit buffer of the fast loop */ |
251 | | z_const unsigned char FAR *last4; /* three refills fit after in */ |
252 | | unsigned char FAR *end4; /* room for a match and fifteen more */ |
253 | | #endif |
254 | | |
255 | | /* copy state to local variables */ |
256 | 67.0k | state = (struct inflate_state FAR *)strm->state; |
257 | 67.0k | in = strm->next_in; |
258 | 67.0k | last = in + (strm->avail_in - 5); |
259 | 67.0k | out = strm->next_out; |
260 | 67.0k | beg = out - (start - strm->avail_out); |
261 | 67.0k | end = out + (strm->avail_out - 257); |
262 | | #ifdef INFLATE_STRICT |
263 | | dmax = state->dmax; |
264 | | #endif |
265 | 67.0k | wsize = state->wsize; |
266 | 67.0k | whave = state->whave; |
267 | 67.0k | wnext = state->wnext; |
268 | 67.0k | window = state->window; |
269 | 67.0k | #if Z_INFLATE_FETCH != 8 |
270 | | /* For inflateBack(), only use the loop at the end, which does not write |
271 | | past the end of the copy. */ |
272 | 67.0k | back = window != Z_NULL && out + strm->avail_out == window + wsize; |
273 | 67.0k | #endif |
274 | 67.0k | hold = state->hold; |
275 | 67.0k | bits = state->bits; |
276 | 67.0k | lcode = state->lencode; |
277 | 67.0k | dcode = state->distcode; |
278 | 67.0k | lmask = (1U << state->lenbits) - 1; |
279 | 67.0k | dmask = (1U << state->distbits) - 1; |
280 | | |
281 | 67.0k | #if Z_INFLATE_FETCH == 64 |
282 | | /* Fast loop, while sixteen bytes are available at in (two refills of eight |
283 | | bytes, the first advancing in by up to seven) and the output has room |
284 | | for the longest match plus 17 bytes (one iteration can write two |
285 | | literals and then a match, and copy_back() may write 15 bytes past the |
286 | | match). Each length/literal code starts with at least 56 bits in hold8, |
287 | | enough for a length/distance pair (at most 48 bits). */ |
288 | 67.0k | if (!back && strm->avail_in >= 16 && strm->avail_out >= 2 + 258 + 15) { |
289 | 59.9k | last8 = in + (strm->avail_in - 16); |
290 | 59.9k | end8 = out + (strm->avail_out - (2 + 258 + 15)); |
291 | 59.9k | hold8 = hold; |
292 | 2.05M | do { |
293 | 2.05M | hold8 |= load64(in) << bits; |
294 | 2.05M | in += (63 - bits) >> 3; |
295 | 2.05M | bits |= 56; |
296 | 2.05M | here = lcode + (hold8 & lmask); |
297 | 2.05M | if (here->op == 0) { /* up to three literals a refill */ |
298 | 1.13M | hold8 >>= here->bits; |
299 | 1.13M | bits -= here->bits; |
300 | 1.13M | TRACE_LITERAL(here); |
301 | 1.13M | *out++ = (unsigned char)(here->val); |
302 | 1.13M | here = lcode + (hold8 & lmask); |
303 | 1.13M | if (here->op == 0) { |
304 | 1.02M | hold8 >>= here->bits; |
305 | 1.02M | bits -= here->bits; |
306 | 1.02M | TRACE_LITERAL(here); |
307 | 1.02M | *out++ = (unsigned char)(here->val); |
308 | 1.02M | here = lcode + (hold8 & lmask); |
309 | 1.02M | if (here->op == 0) { |
310 | 591k | hold8 >>= here->bits; |
311 | 591k | bits -= here->bits; |
312 | 591k | TRACE_LITERAL(here); |
313 | 591k | *out++ = (unsigned char)(here->val); |
314 | 591k | continue; |
315 | 591k | } |
316 | 1.02M | } |
317 | 546k | hold8 |= load64(in) << bits; /* 56 bits for a pair */ |
318 | 546k | in += (63 - bits) >> 3; |
319 | 546k | bits |= 56; |
320 | 546k | } |
321 | 2.22M | dolen8: |
322 | 2.22M | op = (unsigned)(here->bits); |
323 | 2.22M | hold8 >>= op; |
324 | 2.22M | bits -= op; |
325 | 2.22M | op = (unsigned)(here->op); |
326 | 2.22M | if (op == 0) { /* literal */ |
327 | 13.1k | TRACE_LITERAL(here); |
328 | 13.1k | *out++ = (unsigned char)(here->val); |
329 | 13.1k | } |
330 | 2.21M | else if (op & 16) { /* length base */ |
331 | 1.44M | len = (unsigned)(here->val); |
332 | 1.44M | op &= 15; /* number of extra bits */ |
333 | 1.44M | len += (unsigned)hold8 & ((1U << op) - 1); |
334 | 1.44M | hold8 >>= op; |
335 | 1.44M | bits -= op; |
336 | 1.44M | Tracevv((stderr, "inflate: length %u\n", len)); |
337 | 1.44M | here = dcode + (hold8 & dmask); |
338 | 1.47M | dodist8: |
339 | 1.47M | op = (unsigned)(here->bits); |
340 | 1.47M | hold8 >>= op; |
341 | 1.47M | bits -= op; |
342 | 1.47M | op = (unsigned)(here->op); |
343 | 1.47M | if (op & 16) { /* distance base */ |
344 | 1.44M | dist = (unsigned)(here->val); |
345 | 1.44M | op &= 15; /* number of extra bits */ |
346 | 1.44M | dist += (unsigned)hold8 & ((1U << op) - 1); |
347 | | #ifdef INFLATE_STRICT |
348 | | if (dist > dmax) { |
349 | | strm->msg = (z_const char *) |
350 | | "invalid distance too far back"; |
351 | | state->mode = BAD; |
352 | | break; |
353 | | } |
354 | | #endif |
355 | 1.44M | hold8 >>= op; |
356 | 1.44M | bits -= op; |
357 | 1.44M | Tracevv((stderr, "inflate: distance %u\n", dist)); |
358 | 1.44M | op = (unsigned)(out - beg); /* max distance in output */ |
359 | 1.44M | if (dist > op) { /* see if copy from window */ |
360 | 2.82k | op = dist - op; /* distance back in window */ |
361 | 2.82k | if (op > whave) { |
362 | 440 | if (state->sane) { |
363 | 440 | strm->msg = (z_const char *) |
364 | 440 | "invalid distance too far back"; |
365 | 440 | state->mode = BAD; |
366 | 440 | break; |
367 | 440 | } |
368 | | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
369 | | if (len <= op - whave) { |
370 | | do { |
371 | | *out++ = 0; |
372 | | } while (--len); |
373 | | continue; |
374 | | } |
375 | | len -= op - whave; |
376 | | do { |
377 | | *out++ = 0; |
378 | | } while (--op > whave); |
379 | | if (op == 0) { |
380 | | out = copy_back(out, dist, len); |
381 | | continue; |
382 | | } |
383 | | #endif |
384 | 440 | } |
385 | 2.38k | from = window; |
386 | 2.38k | if (wnext == 0) /* very common case */ |
387 | 90 | from += wsize - op; |
388 | 2.29k | else if (wnext < op) { /* wrap around window */ |
389 | 987 | from += wsize + wnext - op; |
390 | 987 | op -= wnext; |
391 | 987 | if (op < len) { /* end of window, then start */ |
392 | 159 | zmemcpy(out, from, op); |
393 | 159 | out += op; |
394 | 159 | len -= op; |
395 | 159 | from = window; |
396 | 159 | op = wnext; |
397 | 159 | } |
398 | 987 | } |
399 | 1.30k | else /* contiguous in window */ |
400 | 1.30k | from += wnext - op; |
401 | 2.38k | if (op > len) |
402 | 1.35k | op = len; |
403 | 2.38k | zmemcpy(out, from, op); /* from the window */ |
404 | 2.38k | out += op; |
405 | 2.38k | len -= op; |
406 | 2.38k | if (len) /* rest from output */ |
407 | 1.02k | out = copy_back(out, dist, len); |
408 | 2.38k | } |
409 | 1.43M | else |
410 | 1.43M | out = copy_back(out, dist, len); |
411 | 1.44M | } |
412 | 29.7k | else if ((op & 64) == 0) { /* 2nd level distance code */ |
413 | 29.1k | here = dcode + here->val + (hold8 & ((1U << op) - 1)); |
414 | 29.1k | goto dodist8; |
415 | 29.1k | } |
416 | 563 | else { |
417 | 563 | strm->msg = (z_const char *)"invalid distance code"; |
418 | 563 | state->mode = BAD; |
419 | 563 | break; |
420 | 563 | } |
421 | 1.47M | } |
422 | 773k | else if ((op & 64) == 0) { /* 2nd level length code */ |
423 | 764k | here = lcode + here->val + (hold8 & ((1U << op) - 1)); |
424 | 764k | goto dolen8; |
425 | 764k | } |
426 | 9.56k | else if (op & 32) { /* end-of-block */ |
427 | 9.32k | Tracevv((stderr, "inflate: end of block\n")); |
428 | 9.32k | state->mode = TYPE; |
429 | 9.32k | break; |
430 | 9.32k | } |
431 | 246 | else { |
432 | 246 | strm->msg = (z_const char *)"invalid literal/length code"; |
433 | 246 | state->mode = BAD; |
434 | 246 | break; |
435 | 246 | } |
436 | 2.22M | } while (in <= last8 && out <= end8); |
437 | | |
438 | | /* return the whole bytes not used, leaving fewer than eight bits */ |
439 | 59.9k | len = bits >> 3; |
440 | 59.9k | in -= len; |
441 | 59.9k | bits -= len << 3; |
442 | 59.9k | hold = (unsigned long)(hold8 & ((1U << bits) - 1)); |
443 | 59.9k | if (state->mode != LEN || out >= end) |
444 | 51.9k | goto leave; |
445 | 59.9k | } |
446 | 15.0k | #endif |
447 | | #if Z_INFLATE_FETCH == 32 |
448 | | /* Fast loop with a 32-bit bit buffer, refilled four bytes at a time |
449 | | without branches. This needs eleven bytes available at in (three refills |
450 | | of four bytes, the first two advancing in by up to three each) and |
451 | | enough room in the output for the longest match plus 15 bytes (an |
452 | | iteration writes up to two literals or one match, which copy_back() may |
453 | | overrun by fifteen bytes). A refill leaves at least 24 bits, enough for |
454 | | a length/literal code and its extra bits (at most 20), or a distance |
455 | | code (at most 15). The distance code gets its own refill, and its extra |
456 | | bits another refill, when too few bits are left for them. */ |
457 | | if (!back && strm->avail_in >= 11 && strm->avail_out >= 258 + 15) { |
458 | | last4 = in + (strm->avail_in - 11); |
459 | | end4 = out + (strm->avail_out - (258 + 15)); |
460 | | hold4 = (Z_U4)hold; |
461 | | do { |
462 | | hold4 |= load32(in) << bits; |
463 | | in += (31 - bits) >> 3; |
464 | | bits |= 24; |
465 | | here = lcode + (hold4 & lmask); |
466 | | if (here->op == 0) { /* a literal, and another */ |
467 | | hold4 >>= here->bits; |
468 | | bits -= here->bits; |
469 | | TRACE_LITERAL(here); |
470 | | *out++ = (unsigned char)(here->val); |
471 | | here = lcode + (hold4 & lmask); |
472 | | if (here->op == 0 && (LENBITS <= 12 || here->bits <= bits)) { |
473 | | hold4 >>= here->bits; |
474 | | bits -= here->bits; |
475 | | TRACE_LITERAL(here); |
476 | | *out++ = (unsigned char)(here->val); |
477 | | } |
478 | | continue; |
479 | | } |
480 | | dolen4: |
481 | | op = (unsigned)(here->bits); |
482 | | hold4 >>= op; |
483 | | bits -= op; |
484 | | op = (unsigned)(here->op); |
485 | | if (op == 0) { /* literal */ |
486 | | TRACE_LITERAL(here); |
487 | | *out++ = (unsigned char)(here->val); |
488 | | } |
489 | | else if (op & 16) { /* length base */ |
490 | | len = (unsigned)(here->val); |
491 | | op &= 15; /* number of extra bits */ |
492 | | len += (unsigned)hold4 & ((1U << op) - 1); |
493 | | hold4 >>= op; |
494 | | bits -= op; |
495 | | Tracevv((stderr, "inflate: length %u\n", len)); |
496 | | hold4 |= load32(in) << bits; /* for the distance code */ |
497 | | in += (31 - bits) >> 3; |
498 | | bits |= 24; |
499 | | here = dcode + (hold4 & dmask); |
500 | | dodist4: |
501 | | op = (unsigned)(here->bits); |
502 | | hold4 >>= op; |
503 | | bits -= op; |
504 | | op = (unsigned)(here->op); |
505 | | if (op & 16) { /* distance base */ |
506 | | dist = (unsigned)(here->val); |
507 | | op &= 15; /* number of extra bits */ |
508 | | if (bits < op) { /* for long distances */ |
509 | | hold4 |= load32(in) << bits; |
510 | | in += (31 - bits) >> 3; |
511 | | bits |= 24; |
512 | | } |
513 | | dist += (unsigned)hold4 & ((1U << op) - 1); |
514 | | #ifdef INFLATE_STRICT |
515 | | if (dist > dmax) { |
516 | | strm->msg = (z_const char *) |
517 | | "invalid distance too far back"; |
518 | | state->mode = BAD; |
519 | | break; |
520 | | } |
521 | | #endif |
522 | | hold4 >>= op; |
523 | | bits -= op; |
524 | | Tracevv((stderr, "inflate: distance %u\n", dist)); |
525 | | op = (unsigned)(out - beg); /* max distance in output */ |
526 | | if (dist > op) { /* see if copy from window */ |
527 | | op = dist - op; /* distance back in window */ |
528 | | if (op > whave) { |
529 | | if (state->sane) { |
530 | | strm->msg = (z_const char *) |
531 | | "invalid distance too far back"; |
532 | | state->mode = BAD; |
533 | | break; |
534 | | } |
535 | | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
536 | | if (len <= op - whave) { |
537 | | do { |
538 | | *out++ = 0; |
539 | | } while (--len); |
540 | | continue; |
541 | | } |
542 | | len -= op - whave; |
543 | | do { |
544 | | *out++ = 0; |
545 | | } while (--op > whave); |
546 | | if (op == 0) { |
547 | | out = copy_back(out, dist, len); |
548 | | continue; |
549 | | } |
550 | | #endif |
551 | | } |
552 | | from = window; |
553 | | if (wnext == 0) /* very common case */ |
554 | | from += wsize - op; |
555 | | else if (wnext < op) { /* wrap around window */ |
556 | | from += wsize + wnext - op; |
557 | | op -= wnext; |
558 | | if (op < len) { /* end of window, then start */ |
559 | | zmemcpy(out, from, op); |
560 | | out += op; |
561 | | len -= op; |
562 | | from = window; |
563 | | op = wnext; |
564 | | } |
565 | | } |
566 | | else /* contiguous in window */ |
567 | | from += wnext - op; |
568 | | if (op > len) |
569 | | op = len; |
570 | | zmemcpy(out, from, op); /* from the window */ |
571 | | out += op; |
572 | | len -= op; |
573 | | if (len) /* rest from output */ |
574 | | out = copy_back(out, dist, len); |
575 | | } |
576 | | else |
577 | | out = copy_back(out, dist, len); |
578 | | } |
579 | | else if ((op & 64) == 0) { /* 2nd level distance code */ |
580 | | here = dcode + here->val + (hold4 & ((1U << op) - 1)); |
581 | | goto dodist4; |
582 | | } |
583 | | else { |
584 | | strm->msg = (z_const char *)"invalid distance code"; |
585 | | state->mode = BAD; |
586 | | break; |
587 | | } |
588 | | } |
589 | | else if ((op & 64) == 0) { /* 2nd level length code */ |
590 | | here = lcode + here->val + (hold4 & ((1U << op) - 1)); |
591 | | goto dolen4; |
592 | | } |
593 | | else if (op & 32) { /* end-of-block */ |
594 | | Tracevv((stderr, "inflate: end of block\n")); |
595 | | state->mode = TYPE; |
596 | | break; |
597 | | } |
598 | | else { |
599 | | strm->msg = (z_const char *)"invalid literal/length code"; |
600 | | state->mode = BAD; |
601 | | break; |
602 | | } |
603 | | } while (in <= last4 && out <= end4); |
604 | | |
605 | | /* return the whole bytes not used, leaving fewer than eight bits */ |
606 | | len = bits >> 3; |
607 | | in -= len; |
608 | | bits -= len << 3; |
609 | | hold = (unsigned long)(hold4 & ((1U << bits) - 1)); |
610 | | if (state->mode != LEN || in >= last || out >= end) |
611 | | goto leave; |
612 | | } |
613 | | #endif |
614 | | |
615 | | /* decode literals and length/distances until end-of-block or not enough |
616 | | input data or output space */ |
617 | 70.5k | do { |
618 | 70.5k | if (bits < 15) { |
619 | 36.4k | hold += (unsigned long)(*in++) << bits; |
620 | 36.4k | bits += 8; |
621 | 36.4k | hold += (unsigned long)(*in++) << bits; |
622 | 36.4k | bits += 8; |
623 | 36.4k | } |
624 | 70.5k | here = lcode + (hold & lmask); |
625 | 74.0k | dolen: |
626 | 74.0k | op = (unsigned)(here->bits); |
627 | 74.0k | hold >>= op; |
628 | 74.0k | bits -= op; |
629 | 74.0k | op = (unsigned)(here->op); |
630 | 74.0k | if (op == 0) { /* literal */ |
631 | 49.1k | Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ? |
632 | 49.1k | "inflate: literal '%c'\n" : |
633 | 49.1k | "inflate: literal 0x%02x\n", here->val)); |
634 | 49.1k | *out++ = (unsigned char)(here->val); |
635 | 49.1k | } |
636 | 24.8k | else if (op & 16) { /* length base */ |
637 | 19.9k | len = (unsigned)(here->val); |
638 | 19.9k | op &= 15; /* number of extra bits */ |
639 | 19.9k | if (op) { |
640 | 5.40k | if (bits < op) { |
641 | 467 | hold += (unsigned long)(*in++) << bits; |
642 | 467 | bits += 8; |
643 | 467 | } |
644 | 5.40k | len += (unsigned)hold & ((1U << op) - 1); |
645 | 5.40k | hold >>= op; |
646 | 5.40k | bits -= op; |
647 | 5.40k | } |
648 | 19.9k | Tracevv((stderr, "inflate: length %u\n", len)); |
649 | 19.9k | if (bits < 15) { |
650 | 5.55k | hold += (unsigned long)(*in++) << bits; |
651 | 5.55k | bits += 8; |
652 | 5.55k | hold += (unsigned long)(*in++) << bits; |
653 | 5.55k | bits += 8; |
654 | 5.55k | } |
655 | 19.9k | here = dcode + (hold & dmask); |
656 | 21.2k | dodist: |
657 | 21.2k | op = (unsigned)(here->bits); |
658 | 21.2k | hold >>= op; |
659 | 21.2k | bits -= op; |
660 | 21.2k | op = (unsigned)(here->op); |
661 | 21.2k | if (op & 16) { /* distance base */ |
662 | 19.6k | dist = (unsigned)(here->val); |
663 | 19.6k | op &= 15; /* number of extra bits */ |
664 | 19.6k | if (bits < op) { |
665 | 368 | hold += (unsigned long)(*in++) << bits; |
666 | 368 | bits += 8; |
667 | 368 | if (bits < op) { |
668 | 0 | hold += (unsigned long)(*in++) << bits; |
669 | 0 | bits += 8; |
670 | 0 | } |
671 | 368 | } |
672 | 19.6k | dist += (unsigned)hold & ((1U << op) - 1); |
673 | | #ifdef INFLATE_STRICT |
674 | | if (dist > dmax) { |
675 | | strm->msg = (z_const char *) |
676 | | "invalid distance too far back"; |
677 | | state->mode = BAD; |
678 | | break; |
679 | | } |
680 | | #endif |
681 | 19.6k | hold >>= op; |
682 | 19.6k | bits -= op; |
683 | 19.6k | Tracevv((stderr, "inflate: distance %u\n", dist)); |
684 | 19.6k | op = (unsigned)(out - beg); /* max distance in output */ |
685 | 19.6k | if (dist > op) { /* see if copy from window */ |
686 | 882 | op = dist - op; /* distance back in window */ |
687 | 882 | if (op > whave) { |
688 | 379 | if (state->sane) { |
689 | 379 | strm->msg = (z_const char *) |
690 | 379 | "invalid distance too far back"; |
691 | 379 | state->mode = BAD; |
692 | 379 | break; |
693 | 379 | } |
694 | | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
695 | | if (len <= op - whave) { |
696 | | do { |
697 | | *out++ = 0; |
698 | | } while (--len); |
699 | | continue; |
700 | | } |
701 | | len -= op - whave; |
702 | | do { |
703 | | *out++ = 0; |
704 | | } while (--op > whave); |
705 | | if (op == 0) { |
706 | | from = out - dist; |
707 | | do { |
708 | | *out++ = *from++; |
709 | | } while (--len); |
710 | | continue; |
711 | | } |
712 | | #endif |
713 | 379 | } |
714 | 503 | from = window; |
715 | 503 | if (wnext == 0) { /* very common case */ |
716 | 37 | from += wsize - op; |
717 | 37 | if (op < len) { /* some from window */ |
718 | 13 | len -= op; |
719 | 500 | do { |
720 | 500 | *out++ = *from++; |
721 | 500 | } while (--op); |
722 | 13 | from = out - dist; /* rest from output */ |
723 | 13 | } |
724 | 37 | } |
725 | 466 | else if (wnext < op) { /* wrap around window */ |
726 | 255 | from += wsize + wnext - op; |
727 | 255 | op -= wnext; |
728 | 255 | if (op < len) { /* some from end of window */ |
729 | 65 | len -= op; |
730 | 1.37k | do { |
731 | 1.37k | *out++ = *from++; |
732 | 1.37k | } while (--op); |
733 | 65 | from = window; |
734 | 65 | if (wnext < len) { /* some from start of window */ |
735 | 13 | op = wnext; |
736 | 13 | len -= op; |
737 | 504 | do { |
738 | 504 | *out++ = *from++; |
739 | 504 | } while (--op); |
740 | 13 | from = out - dist; /* rest from output */ |
741 | 13 | } |
742 | 65 | } |
743 | 255 | } |
744 | 211 | else { /* contiguous in window */ |
745 | 211 | from += wnext - op; |
746 | 211 | if (op < len) { /* some from window */ |
747 | 103 | len -= op; |
748 | 16.0k | do { |
749 | 16.0k | *out++ = *from++; |
750 | 16.0k | } while (--op); |
751 | 103 | from = out - dist; /* rest from output */ |
752 | 103 | } |
753 | 211 | } |
754 | 9.46k | while (len > 2) { |
755 | 8.96k | *out++ = *from++; |
756 | 8.96k | *out++ = *from++; |
757 | 8.96k | *out++ = *from++; |
758 | 8.96k | len -= 3; |
759 | 8.96k | } |
760 | 503 | if (len) { |
761 | 263 | *out++ = *from++; |
762 | 263 | if (len > 1) |
763 | 152 | *out++ = *from++; |
764 | 263 | } |
765 | 503 | } |
766 | 18.7k | else { |
767 | 18.7k | from = out - dist; /* copy direct from output */ |
768 | 1.30M | do { /* minimum length is three */ |
769 | 1.30M | *out++ = *from++; |
770 | 1.30M | *out++ = *from++; |
771 | 1.30M | *out++ = *from++; |
772 | 1.30M | len -= 3; |
773 | 1.30M | } while (len > 2); |
774 | 18.7k | if (len) { |
775 | 3.07k | *out++ = *from++; |
776 | 3.07k | if (len > 1) |
777 | 1.65k | *out++ = *from++; |
778 | 3.07k | } |
779 | 18.7k | } |
780 | 19.6k | } |
781 | 1.60k | else if ((op & 64) == 0) { /* 2nd level distance code */ |
782 | 1.27k | here = dcode + here->val + (hold & ((1U << op) - 1)); |
783 | 1.27k | goto dodist; |
784 | 1.27k | } |
785 | 322 | else { |
786 | 322 | strm->msg = (z_const char *)"invalid distance code"; |
787 | 322 | state->mode = BAD; |
788 | 322 | break; |
789 | 322 | } |
790 | 21.2k | } |
791 | 4.90k | else if ((op & 64) == 0) { /* 2nd level length code */ |
792 | 3.45k | here = lcode + here->val + (hold & ((1U << op) - 1)); |
793 | 3.45k | goto dolen; |
794 | 3.45k | } |
795 | 1.45k | else if (op & 32) { /* end-of-block */ |
796 | 1.23k | Tracevv((stderr, "inflate: end of block\n")); |
797 | 1.23k | state->mode = TYPE; |
798 | 1.23k | break; |
799 | 1.23k | } |
800 | 211 | else { |
801 | 211 | strm->msg = (z_const char *)"invalid literal/length code"; |
802 | 211 | state->mode = BAD; |
803 | 211 | break; |
804 | 211 | } |
805 | 74.0k | } while (in < last && out < end); |
806 | | |
807 | 15.0k | #if Z_INFLATE_FETCH != 8 |
808 | 67.0k | leave: |
809 | 67.0k | #endif |
810 | | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ |
811 | 67.0k | len = bits >> 3; |
812 | 67.0k | in -= len; |
813 | 67.0k | bits -= len << 3; |
814 | 67.0k | hold &= (1U << bits) - 1; |
815 | | |
816 | | /* update state and return */ |
817 | 67.0k | strm->next_in = in; |
818 | 67.0k | strm->next_out = out; |
819 | 67.0k | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); |
820 | 67.0k | strm->avail_out = (unsigned)(out < end ? |
821 | 45.9k | 257 + (end - out) : 257 - (out - end)); |
822 | 67.0k | state->hold = hold; |
823 | 67.0k | state->bits = bits; |
824 | 67.0k | return; |
825 | 15.0k | } |
826 | | |
827 | | /* |
828 | | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): |
829 | | - Using bit fields for code structure |
830 | | - Different op definition to avoid & for extra bits (do & for table bits) |
831 | | - Three separate decoding do-loops for direct, window, and wnext == 0 |
832 | | - Special case for distance > 1 copies to do overlapped load and store copy |
833 | | - Explicit branch predictions (based on measured branch probabilities) |
834 | | - Deferring match copy and interspersed it with decoding subsequent codes |
835 | | - Swapping literal/length else |
836 | | - Swapping window/direct else |
837 | | - Larger unrolled copy loops (three is about right) |
838 | | - Moving len -= 3 statement into middle of loop |
839 | | */ |
840 | | |
841 | | #endif /* !ASMINF */ |