Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 1 | /* inffast.c -- fast decoding |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 2 | * Copyright (C) 1995-2008, 2010, 2013 Mark Adler |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 6 | /* U-Boot: we already included these |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 7 | #include "zutil.h" |
| 8 | #include "inftrees.h" |
| 9 | #include "inflate.h" |
| 10 | #include "inffast.h" |
| 11 | */ |
| 12 | |
| 13 | #ifndef ASMINF |
| 14 | |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 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 | */ |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 50 | void ZLIB_INTERNAL inflate_fast(strm, start) |
| 51 | z_streamp strm; |
| 52 | unsigned start; /* inflate()'s starting value for strm->avail_out */ |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 53 | { |
| 54 | struct inflate_state FAR *state; |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 55 | z_const unsigned char FAR *in; /* local strm->next_in */ |
| 56 | z_const unsigned char FAR *last; /* have enough input while in < last */ |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 57 | unsigned char FAR *out; /* local strm->next_out */ |
| 58 | unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ |
| 59 | unsigned char FAR *end; /* while out < end, enough space available */ |
| 60 | #ifdef INFLATE_STRICT |
| 61 | unsigned dmax; /* maximum distance from zlib header */ |
| 62 | #endif |
| 63 | unsigned wsize; /* window size or zero if not using window */ |
| 64 | unsigned whave; /* valid bytes in the window */ |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 65 | unsigned wnext; /* window write index */ |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 66 | unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ |
| 67 | unsigned long hold; /* local strm->hold */ |
| 68 | unsigned bits; /* local strm->bits */ |
| 69 | code const FAR *lcode; /* local strm->lencode */ |
| 70 | code const FAR *dcode; /* local strm->distcode */ |
| 71 | unsigned lmask; /* mask for first level of length codes */ |
| 72 | unsigned dmask; /* mask for first level of distance codes */ |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 73 | code here; /* retrieved table entry */ |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 74 | unsigned op; /* code bits, operation, extra bits, or */ |
| 75 | /* window position, window bytes to copy */ |
| 76 | unsigned len; /* match length, unused bytes */ |
| 77 | unsigned dist; /* match distance */ |
| 78 | unsigned char FAR *from; /* where to copy match from */ |
| 79 | |
| 80 | /* copy state to local variables */ |
| 81 | state = (struct inflate_state FAR *)strm->state; |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 82 | in = strm->next_in; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 83 | last = in + (strm->avail_in - 5); |
Anatolij Gustschin | 5640719 | 2011-10-15 06:32:52 +0000 | [diff] [blame] | 84 | if (in > last && strm->avail_in > 5) { |
| 85 | /* |
| 86 | * overflow detected, limit strm->avail_in to the |
| 87 | * max. possible size and recalculate last |
| 88 | */ |
Simon Glass | 27956ca | 2011-10-17 12:55:05 +0000 | [diff] [blame] | 89 | strm->avail_in = 0xffffffff - (uintptr_t)in; |
Anatolij Gustschin | 5640719 | 2011-10-15 06:32:52 +0000 | [diff] [blame] | 90 | last = in + (strm->avail_in - 5); |
| 91 | } |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 92 | out = strm->next_out; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 93 | beg = out - (start - strm->avail_out); |
| 94 | end = out + (strm->avail_out - 257); |
| 95 | #ifdef INFLATE_STRICT |
| 96 | dmax = state->dmax; |
| 97 | #endif |
| 98 | wsize = state->wsize; |
| 99 | whave = state->whave; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 100 | wnext = state->wnext; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 101 | window = state->window; |
| 102 | hold = state->hold; |
| 103 | bits = state->bits; |
| 104 | lcode = state->lencode; |
| 105 | dcode = state->distcode; |
| 106 | lmask = (1U << state->lenbits) - 1; |
| 107 | dmask = (1U << state->distbits) - 1; |
| 108 | |
| 109 | /* decode literals and length/distances until end-of-block or not enough |
| 110 | input data or output space */ |
| 111 | do { |
| 112 | if (bits < 15) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 113 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 114 | bits += 8; |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 115 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 116 | bits += 8; |
| 117 | } |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 118 | here = lcode[hold & lmask]; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 119 | dolen: |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 120 | op = (unsigned)(here.bits); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 121 | hold >>= op; |
| 122 | bits -= op; |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 123 | op = (unsigned)(here.op); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 124 | if (op == 0) { /* literal */ |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 125 | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 126 | "inflate: literal '%c'\n" : |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 127 | "inflate: literal 0x%02x\n", here.val)); |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 128 | *out++ = (unsigned char)(here.val); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 129 | } |
| 130 | else if (op & 16) { /* length base */ |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 131 | len = (unsigned)(here.val); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 132 | op &= 15; /* number of extra bits */ |
| 133 | if (op) { |
| 134 | if (bits < op) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 135 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 136 | bits += 8; |
| 137 | } |
| 138 | len += (unsigned)hold & ((1U << op) - 1); |
| 139 | hold >>= op; |
| 140 | bits -= op; |
| 141 | } |
| 142 | Tracevv((stderr, "inflate: length %u\n", len)); |
| 143 | if (bits < 15) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 144 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 145 | bits += 8; |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 146 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 147 | bits += 8; |
| 148 | } |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 149 | here = dcode[hold & dmask]; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 150 | dodist: |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 151 | op = (unsigned)(here.bits); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 152 | hold >>= op; |
| 153 | bits -= op; |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 154 | op = (unsigned)(here.op); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 155 | if (op & 16) { /* distance base */ |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 156 | dist = (unsigned)(here.val); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 157 | op &= 15; /* number of extra bits */ |
| 158 | if (bits < op) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 159 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 160 | bits += 8; |
| 161 | if (bits < op) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 162 | hold += (unsigned long)(*in++) << bits; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 163 | bits += 8; |
| 164 | } |
| 165 | } |
| 166 | dist += (unsigned)hold & ((1U << op) - 1); |
| 167 | #ifdef INFLATE_STRICT |
| 168 | if (dist > dmax) { |
| 169 | strm->msg = (char *)"invalid distance too far back"; |
| 170 | state->mode = BAD; |
| 171 | break; |
| 172 | } |
| 173 | #endif |
| 174 | hold >>= op; |
| 175 | bits -= op; |
| 176 | Tracevv((stderr, "inflate: distance %u\n", dist)); |
| 177 | op = (unsigned)(out - beg); /* max distance in output */ |
| 178 | if (dist > op) { /* see if copy from window */ |
| 179 | op = dist - op; /* distance back in window */ |
| 180 | if (op > whave) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 181 | strm->msg = |
| 182 | (char *)"invalid distance too far back"; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 183 | state->mode = BAD; |
| 184 | break; |
| 185 | } |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 186 | from = window; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 187 | if (wnext == 0) { /* very common case */ |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 188 | from += wsize - op; |
| 189 | if (op < len) { /* some from window */ |
| 190 | len -= op; |
| 191 | do { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 192 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 193 | } while (--op); |
| 194 | from = out - dist; /* rest from output */ |
| 195 | } |
| 196 | } |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 197 | else if (wnext < op) { /* wrap around window */ |
| 198 | from += wsize + wnext - op; |
| 199 | op -= wnext; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 200 | if (op < len) { /* some from end of window */ |
| 201 | len -= op; |
| 202 | do { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 203 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 204 | } while (--op); |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 205 | from = window; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 206 | if (wnext < len) { /* some from start of window */ |
| 207 | op = wnext; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 208 | len -= op; |
| 209 | do { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 210 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 211 | } while (--op); |
| 212 | from = out - dist; /* rest from output */ |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | else { /* contiguous in window */ |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 217 | from += wnext - op; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 218 | if (op < len) { /* some from window */ |
| 219 | len -= op; |
| 220 | do { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 221 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 222 | } while (--op); |
| 223 | from = out - dist; /* rest from output */ |
| 224 | } |
| 225 | } |
| 226 | while (len > 2) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 227 | *out++ = *from++; |
| 228 | *out++ = *from++; |
| 229 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 230 | len -= 3; |
| 231 | } |
| 232 | if (len) { |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 233 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 234 | if (len > 1) |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 235 | *out++ = *from++; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | else { |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 239 | from = out - dist; /* copy direct from output */ |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 240 | do { /* minimum length is three */ |
| 241 | *out++ = *from++; |
| 242 | *out++ = *from++; |
| 243 | *out++ = *from++; |
| 244 | len -= 3; |
| 245 | } while (len > 2); |
| 246 | if (len) { |
| 247 | *out++ = *from++; |
| 248 | if (len > 1) |
| 249 | *out++ = *from++; |
| 250 | } |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | else if ((op & 64) == 0) { /* 2nd level distance code */ |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 254 | here = dcode[here.val + (hold & ((1U << op) - 1))]; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 255 | goto dodist; |
| 256 | } |
| 257 | else { |
| 258 | strm->msg = (char *)"invalid distance code"; |
| 259 | state->mode = BAD; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | else if ((op & 64) == 0) { /* 2nd level length code */ |
Michal Simek | e2a7fb2 | 2024-03-27 15:14:50 +0100 | [diff] [blame] | 264 | here = lcode[here.val + (hold & ((1U << op) - 1))]; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 265 | goto dolen; |
| 266 | } |
| 267 | else if (op & 32) { /* end-of-block */ |
| 268 | Tracevv((stderr, "inflate: end of block\n")); |
| 269 | state->mode = TYPE; |
| 270 | break; |
| 271 | } |
| 272 | else { |
| 273 | strm->msg = (char *)"invalid literal/length code"; |
| 274 | state->mode = BAD; |
| 275 | break; |
| 276 | } |
| 277 | } while (in < last && out < end); |
| 278 | |
| 279 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ |
| 280 | len = bits >> 3; |
| 281 | in -= len; |
| 282 | bits -= len << 3; |
| 283 | hold &= (1U << bits) - 1; |
| 284 | |
| 285 | /* update state and return */ |
Michal Simek | c83bdfb | 2024-03-27 15:14:52 +0100 | [diff] [blame] | 286 | strm->next_in = in; |
| 287 | strm->next_out = out; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 288 | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); |
| 289 | strm->avail_out = (unsigned)(out < end ? |
| 290 | 257 + (end - out) : 257 - (out - end)); |
| 291 | state->hold = hold; |
| 292 | state->bits = bits; |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): |
| 298 | - Using bit fields for code structure |
| 299 | - Different op definition to avoid & for extra bits (do & for table bits) |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 300 | - Three separate decoding do-loops for direct, window, and wnext == 0 |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 301 | - Special case for distance > 1 copies to do overlapped load and store copy |
| 302 | - Explicit branch predictions (based on measured branch probabilities) |
| 303 | - Deferring match copy and interspersed it with decoding subsequent codes |
| 304 | - Swapping literal/length else |
| 305 | - Swapping window/direct else |
| 306 | - Larger unrolled copy loops (three is about right) |
| 307 | - Moving len -= 3 statement into middle of loop |
| 308 | */ |
| 309 | |
| 310 | #endif /* !ASMINF */ |