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