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