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