/src/zlib-ng/inffast_tpl.h
Line | Count | Source |
1 | | /* inffast.c -- fast decoding |
2 | | * Copyright (C) 1995-2017 Mark Adler |
3 | | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | | */ |
5 | | |
6 | | #include "zbuild.h" |
7 | | #include "zutil.h" |
8 | | #include "inftrees.h" |
9 | | #include "inflate.h" |
10 | | #include "inflate_p.h" |
11 | | #include "functable.h" |
12 | | |
13 | | /* |
14 | | Decode literal, length, and distance codes and write out the resulting |
15 | | literal and match bytes until either not enough input or output is |
16 | | available, an end-of-block is encountered, or a data error is encountered. |
17 | | When large enough input and output buffers are supplied to inflate(), for |
18 | | example, a 16K input buffer and a 64K output buffer, more than 95% of the |
19 | | inflate execution time is spent in this routine. |
20 | | |
21 | | Entry assumptions: |
22 | | |
23 | | state->mode == LEN |
24 | | strm->avail_in >= INFLATE_FAST_MIN_HAVE |
25 | | strm->avail_out >= INFLATE_FAST_MIN_LEFT |
26 | | start >= strm->avail_out |
27 | | state->bits < 8 |
28 | | |
29 | | On return, state->mode is one of: |
30 | | |
31 | | LEN -- ran out of enough output space or enough available input |
32 | | TYPE -- reached end of block code, inflate() to interpret next block |
33 | | BAD -- error in block data |
34 | | |
35 | | Notes: |
36 | | |
37 | | - The maximum input bits used by a length/distance pair is 15 bits for the |
38 | | length code, 5 bits for the length extra, 15 bits for the distance code, |
39 | | and 13 bits for the distance extra. This totals 48 bits, or six bytes. |
40 | | Therefore if strm->avail_in >= 6, then there is enough input to avoid |
41 | | checking for available input while decoding. |
42 | | |
43 | | - On some architectures, it can be significantly faster (e.g. up to 1.2x |
44 | | faster on x86_64) to load from strm->next_in 64 bits, or 8 bytes, at a |
45 | | time, so INFLATE_FAST_MIN_HAVE == 8. |
46 | | |
47 | | - The maximum bytes that a single length/distance pair can output is 258 |
48 | | bytes, which is the maximum length that can be coded. inflate_fast() |
49 | | requires strm->avail_out >= 258 for each loop to avoid checking for |
50 | | output space. |
51 | | */ |
52 | 11.3M | void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start, int safe_mode) { |
53 | | /* start: inflate()'s starting value for strm->avail_out */ |
54 | 11.3M | struct inflate_state *state; |
55 | 11.3M | z_const unsigned char *in; /* local strm->next_in */ |
56 | 11.3M | const unsigned char *last; /* have enough input while in < last */ |
57 | 11.3M | unsigned char *out; /* local strm->next_out */ |
58 | 11.3M | unsigned char *beg; /* inflate()'s initial strm->next_out */ |
59 | 11.3M | unsigned char *end; /* while out < end, enough space available */ |
60 | 11.3M | unsigned char *safe; /* can use chunkcopy provided out < safe */ |
61 | 11.3M | unsigned char *window; /* allocated sliding window, if wsize != 0 */ |
62 | 11.3M | unsigned wsize; /* window size or zero if not using window */ |
63 | 11.3M | unsigned whave; /* valid bytes in the window */ |
64 | 11.3M | unsigned wnext; /* window write index */ |
65 | | |
66 | | /* hold is a local copy of strm->hold. By default, hold satisfies the same |
67 | | invariants that strm->hold does, namely that (hold >> bits) == 0. This |
68 | | invariant is kept by loading bits into hold one byte at a time, like: |
69 | | |
70 | | hold |= next_byte_of_input << bits; in++; bits += 8; |
71 | | |
72 | | If we need to ensure that bits >= 15 then this code snippet is simply |
73 | | repeated. Over one iteration of the outermost do/while loop, this |
74 | | happens up to six times (48 bits of input), as described in the NOTES |
75 | | above. |
76 | | |
77 | | However, on some little endian architectures, it can be significantly |
78 | | faster to load 64 bits once instead of 8 bits six times: |
79 | | |
80 | | if (bits <= 16) { |
81 | | hold |= next_8_bytes_of_input << bits; in += 6; bits += 48; |
82 | | } |
83 | | |
84 | | Unlike the simpler one byte load, shifting the next_8_bytes_of_input |
85 | | by bits will overflow and lose those high bits, up to 2 bytes' worth. |
86 | | The conservative estimate is therefore that we have read only 6 bytes |
87 | | (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the |
88 | | rest of the iteration, and we will not need to load another 8 bytes. |
89 | | |
90 | | Inside this function, we no longer satisfy (hold >> bits) == 0, but |
91 | | this is not problematic, even if that overflow does not land on an 8 bit |
92 | | byte boundary. Those excess bits will eventually shift down lower as the |
93 | | Huffman decoder consumes input, and when new input bits need to be loaded |
94 | | into the bits variable, the same input bits will be or'ed over those |
95 | | existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b). |
96 | | Note that we therefore write that load operation as "hold |= etc" and not |
97 | | "hold += etc". |
98 | | |
99 | | Outside that loop, at the end of the function, hold is bitwise and'ed |
100 | | with (1<<bits)-1 to drop those excess bits so that, on function exit, we |
101 | | keep the invariant that (state->hold >> state->bits) == 0. |
102 | | */ |
103 | 11.3M | bits_t bits; /* local strm->bits */ |
104 | 11.3M | uint64_t hold; /* local strm->hold */ |
105 | 11.3M | unsigned lmask; /* mask for first level of length codes */ |
106 | 11.3M | unsigned dmask; /* mask for first level of distance codes */ |
107 | 11.3M | code const *lcode; /* local strm->lencode */ |
108 | 11.3M | code const *dcode; /* local strm->distcode */ |
109 | 11.3M | code here; /* retrieved table entry */ |
110 | 11.3M | unsigned op; /* code bits, operation, extra bits, or */ |
111 | | /* window position, window bytes to copy */ |
112 | 11.3M | unsigned len; /* match length, unused bytes */ |
113 | 11.3M | unsigned char *from; /* where to copy match from */ |
114 | 11.3M | unsigned dist; /* match distance */ |
115 | 11.3M | uint64_t old; /* look-behind buffer for extra bits */ |
116 | | |
117 | | /* copy state to local variables */ |
118 | 11.3M | state = (struct inflate_state *)strm->state; |
119 | 11.3M | in = strm->next_in; |
120 | 11.3M | last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1)); |
121 | 11.3M | out = strm->next_out; |
122 | 11.3M | beg = out - (start - strm->avail_out); |
123 | 11.3M | safe = out + strm->avail_out; |
124 | 11.3M | end = safe - (safe_mode ? INFLATE_FAST_MIN_SAFE : INFLATE_FAST_MIN_LEFT) + 1; |
125 | 11.3M | wsize = state->wsize; |
126 | 11.3M | whave = state->whave; |
127 | 11.3M | wnext = state->wnext; |
128 | 11.3M | window = state->window; |
129 | 11.3M | hold = state->hold; |
130 | 11.3M | bits = (bits_t)state->bits; |
131 | 11.3M | lcode = state->lencode; |
132 | 11.3M | dcode = state->distcode; |
133 | 11.3M | lmask = (1U << state->lenbits) - 1; |
134 | 11.3M | dmask = (1U << state->distbits) - 1; |
135 | | |
136 | | /* Ensure enough bits for the first length table lookup. Every refill after |
137 | | this one is issued behind a table lookup that only needs bits already in |
138 | | hold, so the refill's load latency overlaps with the table load instead |
139 | | of feeding it. */ |
140 | 11.3M | REFILL(); |
141 | | |
142 | | /* decode literals and length/distances until end-of-block or not enough |
143 | | input data or output space */ |
144 | 148M | do { |
145 | | /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here |
146 | | guarantees: each refill leaves bits >= 56, the literal-only paths |
147 | | loop after consuming at most 35 bits, and the distance path refills |
148 | | to at least MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS |
149 | | before consuming at most MAX_BITS + MAX_DIST_EXTRA_BITS. */ |
150 | 148M | here = lcode[hold & lmask]; |
151 | 148M | Z_TOUCH(here); |
152 | | /* No-op on the first iteration: the pre-loop refill already filled hold |
153 | | and the lookup above consumes nothing, so bits is still >= 56. Tops |
154 | | hold back up on every later iteration. */ |
155 | 148M | REFILL(); |
156 | 148M | old = hold; |
157 | 148M | DROPBITS(here.bits); |
158 | 148M | if (LIKELY(here.op == 0)) { |
159 | 124M | TRACE_LITERAL(here.val); |
160 | 124M | *out++ = (unsigned char)(here.val); |
161 | 124M | here = lcode[hold & lmask]; |
162 | 124M | Z_TOUCH(here); |
163 | 124M | old = hold; |
164 | 124M | DROPBITS(here.bits); |
165 | 124M | if (LIKELY(here.op == 0)) { |
166 | 121M | TRACE_LITERAL(here.val); |
167 | 121M | *out++ = (unsigned char)(here.val); |
168 | 121M | here = lcode[hold & lmask]; |
169 | 121M | Z_TOUCH(here); |
170 | 122M | dolen: |
171 | 122M | old = hold; |
172 | 122M | DROPBITS(here.bits); |
173 | 122M | if (LIKELY(here.op == 0)) { |
174 | 120M | TRACE_LITERAL(here.val); |
175 | 120M | *out++ = (unsigned char)(here.val); |
176 | 120M | continue; |
177 | 120M | } |
178 | 122M | } |
179 | 124M | } |
180 | 30.2M | op = here.op; |
181 | 30.2M | if (LIKELY(op & 16)) { /* length base */ |
182 | 17.5M | len = here.val + EXTRA_BITS(old, here, op); |
183 | 17.5M | TRACE_LENGTH(len); |
184 | 17.5M | here = dcode[hold & dmask]; |
185 | 17.5M | Z_TOUCH(here); |
186 | | /* Reserve bits for the distance code and its extra bits, plus the |
187 | | next iteration's length table lookup, which happens before the |
188 | | next refill. */ |
189 | 17.5M | if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) { |
190 | 833k | REFILL(); |
191 | 833k | } |
192 | 17.5M | dodist: |
193 | 17.5M | old = hold; |
194 | 17.5M | DROPBITS(here.bits); |
195 | 17.5M | op = here.op; |
196 | 17.5M | if (LIKELY(op & 16)) { /* distance base */ |
197 | 17.5M | dist = here.val + EXTRA_BITS(old, here, op); |
198 | | #ifdef INFLATE_STRICT |
199 | | if (UNLIKELY(dist > state->dmax)) { |
200 | | SET_BAD("invalid distance too far back"); |
201 | | break; |
202 | | } |
203 | | #endif |
204 | 17.5M | TRACE_DISTANCE(dist); |
205 | | |
206 | | /* In safe mode, if there isn't enough output space for the full copy, |
207 | | bail to the slow path's MATCH state which handles partial copies. */ |
208 | 17.5M | if (UNLIKELY(safe_mode && len > (unsigned)(safe - out))) { |
209 | 188 | state->mode = MATCH; |
210 | 188 | state->length = len; |
211 | 188 | state->offset = dist; |
212 | 188 | break; |
213 | 188 | } |
214 | | |
215 | 17.5M | op = (unsigned)(out - beg); /* max distance in output */ |
216 | 17.5M | if (UNLIKELY(dist > op)) { /* see if copy from window */ |
217 | 568 | op = dist - op; /* distance back in window */ |
218 | 568 | if (UNLIKELY(op > whave)) { |
219 | | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
220 | | if (LIKELY(state->sane)) { |
221 | | SET_BAD("invalid distance too far back"); |
222 | | break; |
223 | | } |
224 | | unsigned gap = op - whave; |
225 | | unsigned zeros = MIN(len, gap); |
226 | | memset(out, 0, zeros); /* fill missing bytes with zeros */ |
227 | | out += zeros; |
228 | | len -= zeros; |
229 | | if (UNLIKELY(len == 0)) |
230 | | continue; |
231 | | op = whave; |
232 | | if (UNLIKELY(op == 0)) {/* copy from already-decoded output */ |
233 | | out = chunkcopy_safe(out, out - dist, len, safe); |
234 | | continue; |
235 | | } |
236 | | #else |
237 | 568 | SET_BAD("invalid distance too far back"); |
238 | 568 | break; |
239 | 568 | #endif |
240 | 568 | } |
241 | 0 | from = window; |
242 | 0 | if (LIKELY(wnext == 0)) { /* very common case */ |
243 | 0 | from += wsize - op; |
244 | 0 | } else if (LIKELY(wnext >= op)) { /* contiguous in window */ |
245 | 0 | from += wnext - op; |
246 | 0 | } else { /* wrap around window */ |
247 | 0 | op -= wnext; |
248 | 0 | from += wsize - op; |
249 | 0 | if (UNLIKELY(op < len)) { /* some from end of window */ |
250 | 0 | len -= op; |
251 | 0 | out = CHUNKCOPY_SAFE(out, from, op, safe); |
252 | 0 | from = window; /* more from start of window */ |
253 | 0 | op = wnext; |
254 | | /* This (rare) case can create a situation where |
255 | | the first chunkcopy below must be checked. |
256 | | */ |
257 | 0 | } |
258 | 0 | } |
259 | 0 | if (UNLIKELY(op < len)) { /* still need some from output */ |
260 | 0 | len -= op; |
261 | 0 | if (LIKELY(!safe_mode)) { |
262 | 0 | out = CHUNKCOPY_SAFE(out, from, op, safe); |
263 | 0 | out = CHUNKUNROLL(out, &dist, &len); |
264 | 0 | out = CHUNKCOPY_SAFE(out, out - dist, len, safe); |
265 | 0 | } else { |
266 | | #ifdef HAVE_MASKED_READWRITE |
267 | | out = CHUNKCOPY_SAFE(out, from, op, safe); |
268 | | out = CHUNKCOPY_SAFE(out, out - dist, len, safe); |
269 | | #else |
270 | | out = chunkcopy_safe(out, from, op, safe); |
271 | | out = chunkcopy_safe(out, out - dist, len, safe); |
272 | | #endif |
273 | 0 | } |
274 | 0 | } else { |
275 | | #ifdef HAVE_MASKED_READWRITE |
276 | | out = CHUNKCOPY_SAFE(out, from, len, safe); |
277 | | #else |
278 | 0 | if (LIKELY(!safe_mode)) |
279 | 0 | out = CHUNKCOPY_SAFE(out, from, len, safe); |
280 | 0 | else |
281 | 0 | out = chunkcopy_safe(out, from, len, safe); |
282 | | #endif |
283 | 0 | } |
284 | 17.5M | } else if (LIKELY(!safe_mode)) { |
285 | | /* Whole reference is in range of current output. No range checks are |
286 | | necessary because we start with room for at least 258 bytes of output, |
287 | | so unroll and roundoff operations can write beyond `out+len` so long |
288 | | as they stay within 258 bytes of `out`. |
289 | | */ |
290 | 17.4M | if (LIKELY(dist >= len || dist >= CHUNKSIZE())) |
291 | 16.1M | out = CHUNKCOPY(out, out - dist, len); |
292 | 1.25M | else |
293 | 1.25M | out = CHUNKMEMSET(out, out - dist, len); |
294 | 17.4M | } else { |
295 | | #ifdef HAVE_MASKED_READWRITE |
296 | | out = CHUNKCOPY_SAFE(out, out - dist, len, safe); |
297 | | #else |
298 | | out = chunkcopy_safe(out, out - dist, len, safe); |
299 | | #endif |
300 | 77.6k | } |
301 | 17.5M | } else if (UNLIKELY((op & 64) == 0)) { /* 2nd level distance code */ |
302 | 48.8k | here = dcode[here.val + BITS(op)]; |
303 | 48.8k | Z_TOUCH(here); |
304 | 48.8k | goto dodist; |
305 | 48.8k | } else { |
306 | 36 | SET_BAD("invalid distance code"); |
307 | 36 | break; |
308 | 36 | } |
309 | 17.5M | } else if (UNLIKELY((op & 64) == 0)) { /* 2nd level length code */ |
310 | 1.40M | here = lcode[here.val + BITS(op)]; |
311 | 1.40M | Z_TOUCH(here); |
312 | 1.40M | goto dolen; |
313 | 11.3M | } else if (UNLIKELY(op & 32)) { /* end-of-block */ |
314 | 11.3M | TRACE_END_OF_BLOCK(); |
315 | 11.3M | state->mode = TYPE; |
316 | 11.3M | break; |
317 | 11.3M | } else { |
318 | 29 | SET_BAD("invalid literal/length code"); |
319 | 29 | break; |
320 | 29 | } |
321 | 137M | } while (in < last && out < end); |
322 | | |
323 | | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ |
324 | 11.3M | len = bits >> 3; |
325 | 11.3M | in -= len; |
326 | 11.3M | bits -= (bits_t)(len << 3); |
327 | 11.3M | hold &= (UINT64_C(1) << bits) - 1; |
328 | | |
329 | | /* update state and return */ |
330 | 11.3M | strm->next_in = in; |
331 | 11.3M | strm->next_out = out; |
332 | 11.3M | strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in) |
333 | 11.3M | : (INFLATE_FAST_MIN_HAVE - 1) - (in - last)); |
334 | 11.3M | strm->avail_out = (unsigned)(safe - out); |
335 | | |
336 | 11.3M | Assert(bits <= 32, "Remaining bits greater than 32"); |
337 | 11.3M | state->hold = (uint32_t)hold; |
338 | 11.3M | state->bits = bits; |
339 | 11.3M | return; |
340 | 11.3M | } Unexecuted instantiation: inflate_fast_sse2 Unexecuted instantiation: inflate_fast_ssse3 Line | Count | Source | 52 | 11.3M | void Z_INTERNAL INFLATE_FAST(PREFIX3(stream) *strm, uint32_t start, int safe_mode) { | 53 | | /* start: inflate()'s starting value for strm->avail_out */ | 54 | 11.3M | struct inflate_state *state; | 55 | 11.3M | z_const unsigned char *in; /* local strm->next_in */ | 56 | 11.3M | const unsigned char *last; /* have enough input while in < last */ | 57 | 11.3M | unsigned char *out; /* local strm->next_out */ | 58 | 11.3M | unsigned char *beg; /* inflate()'s initial strm->next_out */ | 59 | 11.3M | unsigned char *end; /* while out < end, enough space available */ | 60 | 11.3M | unsigned char *safe; /* can use chunkcopy provided out < safe */ | 61 | 11.3M | unsigned char *window; /* allocated sliding window, if wsize != 0 */ | 62 | 11.3M | unsigned wsize; /* window size or zero if not using window */ | 63 | 11.3M | unsigned whave; /* valid bytes in the window */ | 64 | 11.3M | unsigned wnext; /* window write index */ | 65 | | | 66 | | /* hold is a local copy of strm->hold. By default, hold satisfies the same | 67 | | invariants that strm->hold does, namely that (hold >> bits) == 0. This | 68 | | invariant is kept by loading bits into hold one byte at a time, like: | 69 | | | 70 | | hold |= next_byte_of_input << bits; in++; bits += 8; | 71 | | | 72 | | If we need to ensure that bits >= 15 then this code snippet is simply | 73 | | repeated. Over one iteration of the outermost do/while loop, this | 74 | | happens up to six times (48 bits of input), as described in the NOTES | 75 | | above. | 76 | | | 77 | | However, on some little endian architectures, it can be significantly | 78 | | faster to load 64 bits once instead of 8 bits six times: | 79 | | | 80 | | if (bits <= 16) { | 81 | | hold |= next_8_bytes_of_input << bits; in += 6; bits += 48; | 82 | | } | 83 | | | 84 | | Unlike the simpler one byte load, shifting the next_8_bytes_of_input | 85 | | by bits will overflow and lose those high bits, up to 2 bytes' worth. | 86 | | The conservative estimate is therefore that we have read only 6 bytes | 87 | | (48 bits). Again, as per the NOTES above, 48 bits is sufficient for the | 88 | | rest of the iteration, and we will not need to load another 8 bytes. | 89 | | | 90 | | Inside this function, we no longer satisfy (hold >> bits) == 0, but | 91 | | this is not problematic, even if that overflow does not land on an 8 bit | 92 | | byte boundary. Those excess bits will eventually shift down lower as the | 93 | | Huffman decoder consumes input, and when new input bits need to be loaded | 94 | | into the bits variable, the same input bits will be or'ed over those | 95 | | existing bits. A bitwise or is idempotent: (a | b | b) equals (a | b). | 96 | | Note that we therefore write that load operation as "hold |= etc" and not | 97 | | "hold += etc". | 98 | | | 99 | | Outside that loop, at the end of the function, hold is bitwise and'ed | 100 | | with (1<<bits)-1 to drop those excess bits so that, on function exit, we | 101 | | keep the invariant that (state->hold >> state->bits) == 0. | 102 | | */ | 103 | 11.3M | bits_t bits; /* local strm->bits */ | 104 | 11.3M | uint64_t hold; /* local strm->hold */ | 105 | 11.3M | unsigned lmask; /* mask for first level of length codes */ | 106 | 11.3M | unsigned dmask; /* mask for first level of distance codes */ | 107 | 11.3M | code const *lcode; /* local strm->lencode */ | 108 | 11.3M | code const *dcode; /* local strm->distcode */ | 109 | 11.3M | code here; /* retrieved table entry */ | 110 | 11.3M | unsigned op; /* code bits, operation, extra bits, or */ | 111 | | /* window position, window bytes to copy */ | 112 | 11.3M | unsigned len; /* match length, unused bytes */ | 113 | 11.3M | unsigned char *from; /* where to copy match from */ | 114 | 11.3M | unsigned dist; /* match distance */ | 115 | 11.3M | uint64_t old; /* look-behind buffer for extra bits */ | 116 | | | 117 | | /* copy state to local variables */ | 118 | 11.3M | state = (struct inflate_state *)strm->state; | 119 | 11.3M | in = strm->next_in; | 120 | 11.3M | last = in + (strm->avail_in - (INFLATE_FAST_MIN_HAVE - 1)); | 121 | 11.3M | out = strm->next_out; | 122 | 11.3M | beg = out - (start - strm->avail_out); | 123 | 11.3M | safe = out + strm->avail_out; | 124 | 11.3M | end = safe - (safe_mode ? INFLATE_FAST_MIN_SAFE : INFLATE_FAST_MIN_LEFT) + 1; | 125 | 11.3M | wsize = state->wsize; | 126 | 11.3M | whave = state->whave; | 127 | 11.3M | wnext = state->wnext; | 128 | 11.3M | window = state->window; | 129 | 11.3M | hold = state->hold; | 130 | 11.3M | bits = (bits_t)state->bits; | 131 | 11.3M | lcode = state->lencode; | 132 | 11.3M | dcode = state->distcode; | 133 | 11.3M | lmask = (1U << state->lenbits) - 1; | 134 | 11.3M | dmask = (1U << state->distbits) - 1; | 135 | | | 136 | | /* Ensure enough bits for the first length table lookup. Every refill after | 137 | | this one is issued behind a table lookup that only needs bits already in | 138 | | hold, so the refill's load latency overlaps with the table load instead | 139 | | of feeding it. */ | 140 | 11.3M | REFILL(); | 141 | | | 142 | | /* decode literals and length/distances until end-of-block or not enough | 143 | | input data or output space */ | 144 | 148M | do { | 145 | | /* This lookup needs bits >= MAX_LEN_ROOT_BITS, which every path here | 146 | | guarantees: each refill leaves bits >= 56, the literal-only paths | 147 | | loop after consuming at most 35 bits, and the distance path refills | 148 | | to at least MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS | 149 | | before consuming at most MAX_BITS + MAX_DIST_EXTRA_BITS. */ | 150 | 148M | here = lcode[hold & lmask]; | 151 | 148M | Z_TOUCH(here); | 152 | | /* No-op on the first iteration: the pre-loop refill already filled hold | 153 | | and the lookup above consumes nothing, so bits is still >= 56. Tops | 154 | | hold back up on every later iteration. */ | 155 | 148M | REFILL(); | 156 | 148M | old = hold; | 157 | 148M | DROPBITS(here.bits); | 158 | 148M | if (LIKELY(here.op == 0)) { | 159 | 124M | TRACE_LITERAL(here.val); | 160 | 124M | *out++ = (unsigned char)(here.val); | 161 | 124M | here = lcode[hold & lmask]; | 162 | 124M | Z_TOUCH(here); | 163 | 124M | old = hold; | 164 | 124M | DROPBITS(here.bits); | 165 | 124M | if (LIKELY(here.op == 0)) { | 166 | 121M | TRACE_LITERAL(here.val); | 167 | 121M | *out++ = (unsigned char)(here.val); | 168 | 121M | here = lcode[hold & lmask]; | 169 | 121M | Z_TOUCH(here); | 170 | 122M | dolen: | 171 | 122M | old = hold; | 172 | 122M | DROPBITS(here.bits); | 173 | 122M | if (LIKELY(here.op == 0)) { | 174 | 120M | TRACE_LITERAL(here.val); | 175 | 120M | *out++ = (unsigned char)(here.val); | 176 | 120M | continue; | 177 | 120M | } | 178 | 122M | } | 179 | 124M | } | 180 | 30.2M | op = here.op; | 181 | 30.2M | if (LIKELY(op & 16)) { /* length base */ | 182 | 17.5M | len = here.val + EXTRA_BITS(old, here, op); | 183 | 17.5M | TRACE_LENGTH(len); | 184 | 17.5M | here = dcode[hold & dmask]; | 185 | 17.5M | Z_TOUCH(here); | 186 | | /* Reserve bits for the distance code and its extra bits, plus the | 187 | | next iteration's length table lookup, which happens before the | 188 | | next refill. */ | 189 | 17.5M | if (UNLIKELY(bits < MAX_BITS + MAX_DIST_EXTRA_BITS + MAX_LEN_ROOT_BITS)) { | 190 | 833k | REFILL(); | 191 | 833k | } | 192 | 17.5M | dodist: | 193 | 17.5M | old = hold; | 194 | 17.5M | DROPBITS(here.bits); | 195 | 17.5M | op = here.op; | 196 | 17.5M | if (LIKELY(op & 16)) { /* distance base */ | 197 | 17.5M | dist = here.val + EXTRA_BITS(old, here, op); | 198 | | #ifdef INFLATE_STRICT | 199 | | if (UNLIKELY(dist > state->dmax)) { | 200 | | SET_BAD("invalid distance too far back"); | 201 | | break; | 202 | | } | 203 | | #endif | 204 | 17.5M | TRACE_DISTANCE(dist); | 205 | | | 206 | | /* In safe mode, if there isn't enough output space for the full copy, | 207 | | bail to the slow path's MATCH state which handles partial copies. */ | 208 | 17.5M | if (UNLIKELY(safe_mode && len > (unsigned)(safe - out))) { | 209 | 188 | state->mode = MATCH; | 210 | 188 | state->length = len; | 211 | 188 | state->offset = dist; | 212 | 188 | break; | 213 | 188 | } | 214 | | | 215 | 17.5M | op = (unsigned)(out - beg); /* max distance in output */ | 216 | 17.5M | if (UNLIKELY(dist > op)) { /* see if copy from window */ | 217 | 568 | op = dist - op; /* distance back in window */ | 218 | 568 | if (UNLIKELY(op > whave)) { | 219 | | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR | 220 | | if (LIKELY(state->sane)) { | 221 | | SET_BAD("invalid distance too far back"); | 222 | | break; | 223 | | } | 224 | | unsigned gap = op - whave; | 225 | | unsigned zeros = MIN(len, gap); | 226 | | memset(out, 0, zeros); /* fill missing bytes with zeros */ | 227 | | out += zeros; | 228 | | len -= zeros; | 229 | | if (UNLIKELY(len == 0)) | 230 | | continue; | 231 | | op = whave; | 232 | | if (UNLIKELY(op == 0)) {/* copy from already-decoded output */ | 233 | | out = chunkcopy_safe(out, out - dist, len, safe); | 234 | | continue; | 235 | | } | 236 | | #else | 237 | 568 | SET_BAD("invalid distance too far back"); | 238 | 568 | break; | 239 | 568 | #endif | 240 | 568 | } | 241 | 0 | from = window; | 242 | 0 | if (LIKELY(wnext == 0)) { /* very common case */ | 243 | 0 | from += wsize - op; | 244 | 0 | } else if (LIKELY(wnext >= op)) { /* contiguous in window */ | 245 | 0 | from += wnext - op; | 246 | 0 | } else { /* wrap around window */ | 247 | 0 | op -= wnext; | 248 | 0 | from += wsize - op; | 249 | 0 | if (UNLIKELY(op < len)) { /* some from end of window */ | 250 | 0 | len -= op; | 251 | 0 | out = CHUNKCOPY_SAFE(out, from, op, safe); | 252 | 0 | from = window; /* more from start of window */ | 253 | 0 | op = wnext; | 254 | | /* This (rare) case can create a situation where | 255 | | the first chunkcopy below must be checked. | 256 | | */ | 257 | 0 | } | 258 | 0 | } | 259 | 0 | if (UNLIKELY(op < len)) { /* still need some from output */ | 260 | 0 | len -= op; | 261 | 0 | if (LIKELY(!safe_mode)) { | 262 | 0 | out = CHUNKCOPY_SAFE(out, from, op, safe); | 263 | 0 | out = CHUNKUNROLL(out, &dist, &len); | 264 | 0 | out = CHUNKCOPY_SAFE(out, out - dist, len, safe); | 265 | 0 | } else { | 266 | | #ifdef HAVE_MASKED_READWRITE | 267 | | out = CHUNKCOPY_SAFE(out, from, op, safe); | 268 | | out = CHUNKCOPY_SAFE(out, out - dist, len, safe); | 269 | | #else | 270 | 0 | out = chunkcopy_safe(out, from, op, safe); | 271 | 0 | out = chunkcopy_safe(out, out - dist, len, safe); | 272 | 0 | #endif | 273 | 0 | } | 274 | 0 | } else { | 275 | | #ifdef HAVE_MASKED_READWRITE | 276 | | out = CHUNKCOPY_SAFE(out, from, len, safe); | 277 | | #else | 278 | 0 | if (LIKELY(!safe_mode)) | 279 | 0 | out = CHUNKCOPY_SAFE(out, from, len, safe); | 280 | 0 | else | 281 | 0 | out = chunkcopy_safe(out, from, len, safe); | 282 | 0 | #endif | 283 | 0 | } | 284 | 17.5M | } else if (LIKELY(!safe_mode)) { | 285 | | /* Whole reference is in range of current output. No range checks are | 286 | | necessary because we start with room for at least 258 bytes of output, | 287 | | so unroll and roundoff operations can write beyond `out+len` so long | 288 | | as they stay within 258 bytes of `out`. | 289 | | */ | 290 | 17.4M | if (LIKELY(dist >= len || dist >= CHUNKSIZE())) | 291 | 16.1M | out = CHUNKCOPY(out, out - dist, len); | 292 | 1.25M | else | 293 | 1.25M | out = CHUNKMEMSET(out, out - dist, len); | 294 | 17.4M | } else { | 295 | | #ifdef HAVE_MASKED_READWRITE | 296 | | out = CHUNKCOPY_SAFE(out, out - dist, len, safe); | 297 | | #else | 298 | 77.6k | out = chunkcopy_safe(out, out - dist, len, safe); | 299 | 77.6k | #endif | 300 | 77.6k | } | 301 | 17.5M | } else if (UNLIKELY((op & 64) == 0)) { /* 2nd level distance code */ | 302 | 48.8k | here = dcode[here.val + BITS(op)]; | 303 | 48.8k | Z_TOUCH(here); | 304 | 48.8k | goto dodist; | 305 | 48.8k | } else { | 306 | 36 | SET_BAD("invalid distance code"); | 307 | 36 | break; | 308 | 36 | } | 309 | 17.5M | } else if (UNLIKELY((op & 64) == 0)) { /* 2nd level length code */ | 310 | 1.40M | here = lcode[here.val + BITS(op)]; | 311 | 1.40M | Z_TOUCH(here); | 312 | 1.40M | goto dolen; | 313 | 11.3M | } else if (UNLIKELY(op & 32)) { /* end-of-block */ | 314 | 11.3M | TRACE_END_OF_BLOCK(); | 315 | 11.3M | state->mode = TYPE; | 316 | 11.3M | break; | 317 | 11.3M | } else { | 318 | 29 | SET_BAD("invalid literal/length code"); | 319 | 29 | break; | 320 | 29 | } | 321 | 137M | } while (in < last && out < end); | 322 | | | 323 | | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ | 324 | 11.3M | len = bits >> 3; | 325 | 11.3M | in -= len; | 326 | 11.3M | bits -= (bits_t)(len << 3); | 327 | 11.3M | hold &= (UINT64_C(1) << bits) - 1; | 328 | | | 329 | | /* update state and return */ | 330 | 11.3M | strm->next_in = in; | 331 | 11.3M | strm->next_out = out; | 332 | 11.3M | strm->avail_in = (unsigned)(in < last ? (INFLATE_FAST_MIN_HAVE - 1) + (last - in) | 333 | 11.3M | : (INFLATE_FAST_MIN_HAVE - 1) - (in - last)); | 334 | 11.3M | strm->avail_out = (unsigned)(safe - out); | 335 | | | 336 | 11.3M | Assert(bits <= 32, "Remaining bits greater than 32"); | 337 | 11.3M | state->hold = (uint32_t)hold; | 338 | 11.3M | state->bits = bits; | 339 | 11.3M | return; | 340 | 11.3M | } |
Unexecuted instantiation: inflate_fast_avx512 |
341 | | |
342 | | /* |
343 | | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): |
344 | | - Using bit fields for code structure |
345 | | - Different op definition to avoid & for extra bits (do & for table bits) |
346 | | - Three separate decoding do-loops for direct, window, and wnext == 0 |
347 | | - Special case for distance > 1 copies to do overlapped load and store copy |
348 | | - Explicit branch predictions (based on measured branch probabilities) |
349 | | - Deferring match copy and interspersed it with decoding subsequent codes |
350 | | - Swapping literal/length else |
351 | | - Swapping window/direct else |
352 | | - Larger unrolled copy loops (three is about right) |
353 | | - Moving len -= 3 statement into middle of loop |
354 | | */ |
355 | | |
356 | | // Cleanup |
357 | | #undef CHUNKCOPY |
358 | | #undef CHUNKMEMSET |
359 | | #undef CHUNKMEMSET_SAFE |
360 | | #undef CHUNKSIZE |
361 | | #undef CHUNKUNROLL |
362 | | #undef HAVE_CHUNKCOPY |
363 | | #undef HAVE_CHUNKMEMSET_2 |
364 | | #undef HAVE_CHUNKMEMSET_4 |
365 | | #undef HAVE_CHUNKMEMSET_8 |
366 | | #undef HAVE_CHUNKMEMSET_16 |
367 | | #undef HAVE_CHUNK_MAG |
368 | | #undef HAVE_HALFCHUNKCOPY |
369 | | #undef HAVE_HALF_CHUNK |
370 | | #undef HAVE_MASKED_READWRITE |
371 | | #undef INFLATE_FAST |