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