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