1 /* $OpenBSD: inflate.c,v 1.12 2004/12/03 03:07:09 djm Exp $ */ 2 /* inflate.c -- zlib decompression 3 * Copyright (C) 1995-2003 Mark Adler 4 * For conditions of distribution and use, see copyright notice in zlib.h 5 */ 6 7 /* 8 * Change history: 9 * 10 * 1.2.beta0 24 Nov 2002 11 * - First version -- complete rewrite of inflate to simplify code, avoid 12 * creation of window when not needed, minimize use of window when it is 13 * needed, make inffast.c even faster, implement gzip decoding, and to 14 * improve code readability and style over the previous zlib inflate code 15 * 16 * 1.2.beta1 25 Nov 2002 17 * - Use pointers for available input and output checking in inffast.c 18 * - Remove input and output counters in inffast.c 19 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 20 * - Remove unnecessary second byte pull from length extra in inffast.c 21 * - Unroll direct copy to three copies per loop in inffast.c 22 * 23 * 1.2.beta2 4 Dec 2002 24 * - Change external routine names to reduce potential conflicts 25 * - Correct filename to inffixed.h for fixed tables in inflate.c 26 * - Make hbuf[] unsigned char to match parameter type in inflate.c 27 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) 28 * to avoid negation problem on Alphas (64 bit) in inflate.c 29 * 30 * 1.2.beta3 22 Dec 2002 31 * - Add comments on state->bits assertion in inffast.c 32 * - Add comments on op field in inftrees.h 33 * - Fix bug in reuse of allocated window after inflateReset() 34 * - Remove bit fields--back to byte structure for speed 35 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths 36 * - Change post-increments to pre-increments in inflate_fast(), PPC biased? 37 * - Add compile time option, POSTINC, to use post-increments instead (Intel?) 38 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used 39 * - Use local copies of stream next and avail values, as well as local bit 40 * buffer and bit count in inflate()--for speed when inflate_fast() not used 41 * 42 * 1.2.beta4 1 Jan 2003 43 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings 44 * - Move a comment on output buffer sizes from inffast.c to inflate.c 45 * - Add comments in inffast.c to introduce the inflate_fast() routine 46 * - Rearrange window copies in inflate_fast() for speed and simplification 47 * - Unroll last copy for window match in inflate_fast() 48 * - Use local copies of window variables in inflate_fast() for speed 49 * - Pull out common write == 0 case for speed in inflate_fast() 50 * - Make op and len in inflate_fast() unsigned for consistency 51 * - Add FAR to lcode and dcode declarations in inflate_fast() 52 * - Simplified bad distance check in inflate_fast() 53 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new 54 * source file infback.c to provide a call-back interface to inflate for 55 * programs like gzip and unzip -- uses window as output buffer to avoid 56 * window copying 57 * 58 * 1.2.beta5 1 Jan 2003 59 * - Improved inflateBack() interface to allow the caller to provide initial 60 * input in strm. 61 * - Fixed stored blocks bug in inflateBack() 62 * 63 * 1.2.beta6 4 Jan 2003 64 * - Added comments in inffast.c on effectiveness of POSTINC 65 * - Typecasting all around to reduce compiler warnings 66 * - Changed loops from while (1) or do {} while (1) to for (;;), again to 67 * make compilers happy 68 * - Changed type of window in inflateBackInit() to unsigned char * 69 * 70 * 1.2.beta7 27 Jan 2003 71 * - Changed many types to unsigned or unsigned short to avoid warnings 72 * - Added inflateCopy() function 73 * 74 * 1.2.0 9 Mar 2003 75 * - Changed inflateBack() interface to provide separate opaque descriptors 76 * for the in() and out() functions 77 * - Changed inflateBack() argument and in_func typedef to swap the length 78 * and buffer address return values for the input function 79 * - Check next_in and next_out for Z_NULL on entry to inflate() 80 * 81 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. 82 */ 83 84 #include "zutil.h" 85 #include "inftrees.h" 86 #include "inflate.h" 87 #include "inffast.h" 88 89 #ifdef MAKEFIXED 90 # ifndef BUILDFIXED 91 # define BUILDFIXED 92 # endif 93 #endif 94 95 /* function prototypes */ 96 local void fixedtables OF((struct inflate_state FAR *state)); 97 local int updatewindow OF((z_streamp strm, unsigned out)); 98 #ifdef BUILDFIXED 99 void makefixed OF((void)); 100 #endif 101 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, 102 unsigned len)); 103 104 int ZEXPORT inflateReset(strm) 105 z_streamp strm; 106 { 107 struct inflate_state FAR *state; 108 109 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 110 state = (struct inflate_state FAR *)strm->state; 111 strm->total_in = strm->total_out = state->total = 0; 112 strm->msg = Z_NULL; 113 strm->adler = 1; /* to support ill-conceived Java test suite */ 114 state->mode = HEAD; 115 state->last = 0; 116 state->havedict = 0; 117 state->wsize = 0; 118 state->whave = 0; 119 state->hold = 0; 120 state->bits = 0; 121 state->lencode = state->distcode = state->next = state->codes; 122 Tracev((stderr, "inflate: reset\n")); 123 return Z_OK; 124 } 125 126 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) 127 z_streamp strm; 128 int windowBits; 129 const char *version; 130 int stream_size; 131 { 132 struct inflate_state FAR *state; 133 134 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 135 stream_size != (int)(sizeof(z_stream))) 136 return Z_VERSION_ERROR; 137 if (strm == Z_NULL) return Z_STREAM_ERROR; 138 strm->msg = Z_NULL; /* in case we return an error */ 139 if (strm->zalloc == (alloc_func)0) { 140 strm->zalloc = zcalloc; 141 strm->opaque = (voidpf)0; 142 } 143 if (strm->zfree == (free_func)0) strm->zfree = zcfree; 144 state = (struct inflate_state FAR *) 145 ZALLOC(strm, 1, sizeof(struct inflate_state)); 146 if (state == Z_NULL) return Z_MEM_ERROR; 147 Tracev((stderr, "inflate: allocated\n")); 148 strm->state = (voidpf)state; 149 if (windowBits < 0) { 150 state->wrap = 0; 151 windowBits = -windowBits; 152 } 153 else { 154 state->wrap = (windowBits >> 4) + 1; 155 #ifdef GUNZIP 156 if (windowBits < 48) windowBits &= 15; 157 #endif 158 } 159 if (windowBits < 8 || windowBits > 15) { 160 ZFREE(strm, state); 161 strm->state = Z_NULL; 162 return Z_STREAM_ERROR; 163 } 164 state->wbits = (unsigned)windowBits; 165 state->window = Z_NULL; 166 return inflateReset(strm); 167 } 168 169 int ZEXPORT inflateInit_(strm, version, stream_size) 170 z_streamp strm; 171 const char *version; 172 int stream_size; 173 { 174 return inflateInit2_(strm, DEF_WBITS, version, stream_size); 175 } 176 177 /* 178 Return state with length and distance decoding tables and index sizes set to 179 fixed code decoding. Normally this returns fixed tables from inffixed.h. 180 If BUILDFIXED is defined, then instead this routine builds the tables the 181 first time it's called, and returns those tables the first time and 182 thereafter. This reduces the size of the code by about 2K bytes, in 183 exchange for a little execution time. However, BUILDFIXED should not be 184 used for threaded applications, since the rewriting of the tables and virgin 185 may not be thread-safe. 186 */ 187 local void fixedtables(state) 188 struct inflate_state FAR *state; 189 { 190 #ifdef BUILDFIXED 191 static int virgin = 1; 192 static code *lenfix, *distfix; 193 static code fixed[544]; 194 195 /* build fixed huffman tables if first call (may not be thread safe) */ 196 if (virgin) { 197 unsigned sym, bits; 198 static code *next; 199 200 /* literal/length table */ 201 sym = 0; 202 while (sym < 144) state->lens[sym++] = 8; 203 while (sym < 256) state->lens[sym++] = 9; 204 while (sym < 280) state->lens[sym++] = 7; 205 while (sym < 288) state->lens[sym++] = 8; 206 next = fixed; 207 lenfix = next; 208 bits = 9; 209 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); 210 211 /* distance table */ 212 sym = 0; 213 while (sym < 32) state->lens[sym++] = 5; 214 distfix = next; 215 bits = 5; 216 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); 217 218 /* do this just once */ 219 virgin = 0; 220 } 221 #else /* !BUILDFIXED */ 222 # include "inffixed.h" 223 #endif /* BUILDFIXED */ 224 state->lencode = lenfix; 225 state->lenbits = 9; 226 state->distcode = distfix; 227 state->distbits = 5; 228 } 229 230 #ifdef MAKEFIXED 231 #include <stdio.h> 232 233 /* 234 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also 235 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes 236 those tables to stdout, which would be piped to inffixed.h. A small program 237 can simply call makefixed to do this: 238 239 void makefixed(void); 240 241 int main(void) 242 { 243 makefixed(); 244 return 0; 245 } 246 247 Then that can be linked with zlib built with MAKEFIXED defined and run: 248 249 a.out > inffixed.h 250 */ 251 void makefixed() 252 { 253 unsigned low, size; 254 struct inflate_state state; 255 256 fixedtables(&state); 257 puts(" /* inffixed.h -- table for decoding fixed codes"); 258 puts(" * Generated automatically by makefixed()."); 259 puts(" */"); 260 puts(""); 261 puts(" /* WARNING: this file should *not* be used by applications."); 262 puts(" It is part of the implementation of this library and is"); 263 puts(" subject to change. Applications should only use zlib.h."); 264 puts(" */"); 265 puts(""); 266 size = 1U << 9; 267 printf(" static const code lenfix[%u] = {", size); 268 low = 0; 269 for (;;) { 270 if ((low % 7) == 0) printf("\n "); 271 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, 272 state.lencode[low].val); 273 if (++low == size) break; 274 putchar(','); 275 } 276 puts("\n };"); 277 size = 1U << 5; 278 printf("\n static const code distfix[%u] = {", size); 279 low = 0; 280 for (;;) { 281 if ((low % 6) == 0) printf("\n "); 282 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, 283 state.distcode[low].val); 284 if (++low == size) break; 285 putchar(','); 286 } 287 puts("\n };"); 288 } 289 #endif /* MAKEFIXED */ 290 291 /* 292 Update the window with the last wsize (normally 32K) bytes written before 293 returning. If window does not exist yet, create it. This is only called 294 when a window is already in use, or when output has been written during this 295 inflate call, but the end of the deflate stream has not been reached yet. 296 It is also called to create a window for dictionary data when a dictionary 297 is loaded. 298 299 Providing output buffers larger than 32K to inflate() should provide a speed 300 advantage, since only the last 32K of output is copied to the sliding window 301 upon return from inflate(), and since all distances after the first 32K of 302 output will fall in the output data, making match copies simpler and faster. 303 The advantage may be dependent on the size of the processor's data caches. 304 */ 305 local int updatewindow(strm, out) 306 z_streamp strm; 307 unsigned out; 308 { 309 struct inflate_state FAR *state; 310 unsigned copy, dist; 311 312 state = (struct inflate_state FAR *)strm->state; 313 314 /* if it hasn't been done already, allocate space for the window */ 315 if (state->window == Z_NULL) { 316 state->window = (unsigned char FAR *) 317 ZALLOC(strm, 1U << state->wbits, 318 sizeof(unsigned char)); 319 if (state->window == Z_NULL) return 1; 320 } 321 322 /* if window not in use yet, initialize */ 323 if (state->wsize == 0) { 324 state->wsize = 1U << state->wbits; 325 state->write = 0; 326 state->whave = 0; 327 } 328 329 /* copy state->wsize or less output bytes into the circular window */ 330 copy = out - strm->avail_out; 331 if (copy >= state->wsize) { 332 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); 333 state->write = 0; 334 state->whave = state->wsize; 335 } 336 else { 337 dist = state->wsize - state->write; 338 if (dist > copy) dist = copy; 339 zmemcpy(state->window + state->write, strm->next_out - copy, dist); 340 copy -= dist; 341 if (copy) { 342 zmemcpy(state->window, strm->next_out - copy, copy); 343 state->write = copy; 344 state->whave = state->wsize; 345 } 346 else { 347 state->write += dist; 348 if (state->write == state->wsize) state->write = 0; 349 if (state->whave < state->wsize) state->whave += dist; 350 } 351 } 352 return 0; 353 } 354 355 /* Macros for inflate(): */ 356 357 /* check function to use adler32() for zlib or crc32() for gzip */ 358 #ifdef GUNZIP 359 # define UPDATE(check, buf, len) \ 360 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) 361 #else 362 # define UPDATE(check, buf, len) adler32(check, buf, len) 363 #endif 364 365 /* check macros for header crc */ 366 #ifdef GUNZIP 367 # define CRC2(check, word) \ 368 do { \ 369 hbuf[0] = (unsigned char)(word); \ 370 hbuf[1] = (unsigned char)((word) >> 8); \ 371 check = crc32(check, hbuf, 2); \ 372 } while (0) 373 374 # define CRC4(check, word) \ 375 do { \ 376 hbuf[0] = (unsigned char)(word); \ 377 hbuf[1] = (unsigned char)((word) >> 8); \ 378 hbuf[2] = (unsigned char)((word) >> 16); \ 379 hbuf[3] = (unsigned char)((word) >> 24); \ 380 check = crc32(check, hbuf, 4); \ 381 } while (0) 382 #endif 383 384 /* Load registers with state in inflate() for speed */ 385 #define LOAD() \ 386 do { \ 387 put = strm->next_out; \ 388 left = strm->avail_out; \ 389 next = strm->next_in; \ 390 have = strm->avail_in; \ 391 hold = state->hold; \ 392 bits = state->bits; \ 393 } while (0) 394 395 /* Restore state from registers in inflate() */ 396 #define RESTORE() \ 397 do { \ 398 strm->next_out = put; \ 399 strm->avail_out = left; \ 400 strm->next_in = next; \ 401 strm->avail_in = have; \ 402 state->hold = hold; \ 403 state->bits = bits; \ 404 } while (0) 405 406 /* Clear the input bit accumulator */ 407 #define INITBITS() \ 408 do { \ 409 hold = 0; \ 410 bits = 0; \ 411 } while (0) 412 413 /* Get a byte of input into the bit accumulator, or return from inflate() 414 if there is no input available. */ 415 #define PULLBYTE() \ 416 do { \ 417 if (have == 0) goto inf_leave; \ 418 have--; \ 419 hold += (unsigned long)(*next++) << bits; \ 420 bits += 8; \ 421 } while (0) 422 423 /* Assure that there are at least n bits in the bit accumulator. If there is 424 not enough available input to do that, then return from inflate(). */ 425 #define NEEDBITS(n) \ 426 do { \ 427 while (bits < (unsigned)(n)) \ 428 PULLBYTE(); \ 429 } while (0) 430 431 /* Return the low n bits of the bit accumulator (n < 16) */ 432 #define BITS(n) \ 433 ((unsigned)hold & ((1U << (n)) - 1)) 434 435 /* Remove n bits from the bit accumulator */ 436 #define DROPBITS(n) \ 437 do { \ 438 hold >>= (n); \ 439 bits -= (unsigned)(n); \ 440 } while (0) 441 442 /* Remove zero to seven bits as needed to go to a byte boundary */ 443 #define BYTEBITS() \ 444 do { \ 445 hold >>= bits & 7; \ 446 bits -= bits & 7; \ 447 } while (0) 448 449 /* Reverse the bytes in a 32-bit value */ 450 #define REVERSE(q) \ 451 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 452 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 453 454 /* 455 inflate() uses a state machine to process as much input data and generate as 456 much output data as possible before returning. The state machine is 457 structured roughly as follows: 458 459 for (;;) switch (state) { 460 ... 461 case STATEn: 462 if (not enough input data or output space to make progress) 463 return; 464 ... make progress ... 465 state = STATEm; 466 break; 467 ... 468 } 469 470 so when inflate() is called again, the same case is attempted again, and 471 if the appropriate resources are provided, the machine proceeds to the 472 next state. The NEEDBITS() macro is usually the way the state evaluates 473 whether it can proceed or should return. NEEDBITS() does the return if 474 the requested bits are not available. The typical use of the BITS macros 475 is: 476 477 NEEDBITS(n); 478 ... do something with BITS(n) ... 479 DROPBITS(n); 480 481 where NEEDBITS(n) either returns from inflate() if there isn't enough 482 input left to load n bits into the accumulator, or it continues. BITS(n) 483 gives the low n bits in the accumulator. When done, DROPBITS(n) drops 484 the low n bits off the accumulator. INITBITS() clears the accumulator 485 and sets the number of available bits to zero. BYTEBITS() discards just 486 enough bits to put the accumulator on a byte boundary. After BYTEBITS() 487 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. 488 489 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return 490 if there is no input available. The decoding of variable length codes uses 491 PULLBYTE() directly in order to pull just enough bytes to decode the next 492 code, and no more. 493 494 Some states loop until they get enough input, making sure that enough 495 state information is maintained to continue the loop where it left off 496 if NEEDBITS() returns in the loop. For example, want, need, and keep 497 would all have to actually be part of the saved state in case NEEDBITS() 498 returns: 499 500 case STATEw: 501 while (want < need) { 502 NEEDBITS(n); 503 keep[want++] = BITS(n); 504 DROPBITS(n); 505 } 506 state = STATEx; 507 case STATEx: 508 509 As shown above, if the next state is also the next case, then the break 510 is omitted. 511 512 A state may also return if there is not enough output space available to 513 complete that state. Those states are copying stored data, writing a 514 literal byte, and copying a matching string. 515 516 When returning, a "goto inf_leave" is used to update the total counters, 517 update the check value, and determine whether any progress has been made 518 during that inflate() call in order to return the proper return code. 519 Progress is defined as a change in either strm->avail_in or strm->avail_out. 520 When there is a window, goto inf_leave will update the window with the last 521 output written. If a goto inf_leave occurs in the middle of decompression 522 and there is no window currently, goto inf_leave will create one and copy 523 output to the window for the next call of inflate(). 524 525 In this implementation, the flush parameter of inflate() only affects the 526 return code (per zlib.h). inflate() always writes as much as possible to 527 strm->next_out, given the space available and the provided input--the effect 528 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers 529 the allocation of and copying into a sliding window until necessary, which 530 provides the effect documented in zlib.h for Z_FINISH when the entire input 531 stream available. So the only thing the flush parameter actually does is: 532 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it 533 will return Z_BUF_ERROR if it has not reached the end of the stream. 534 */ 535 536 int ZEXPORT inflate(strm, flush) 537 z_streamp strm; 538 int flush; 539 { 540 struct inflate_state FAR *state; 541 unsigned char FAR *next; /* next input */ 542 unsigned char FAR *put; /* next output */ 543 unsigned have, left; /* available input and output */ 544 unsigned long hold; /* bit buffer */ 545 unsigned bits; /* bits in bit buffer */ 546 unsigned in, out; /* save starting available input and output */ 547 unsigned copy; /* number of stored or match bytes to copy */ 548 unsigned char FAR *from; /* where to copy match bytes from */ 549 code this; /* current decoding table entry */ 550 code last; /* parent table entry */ 551 unsigned len; /* length to copy for repeats, bits to drop */ 552 int ret; /* return code */ 553 #ifdef GUNZIP 554 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ 555 #endif 556 static const unsigned short order[19] = /* permutation of code lengths */ 557 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 558 559 if (strm == Z_NULL || strm->state == Z_NULL || 560 #ifndef __vax__ 561 strm->next_out == Z_NULL || 562 #endif 563 (strm->next_in == Z_NULL && strm->avail_in != 0)) 564 return Z_STREAM_ERROR; 565 566 state = (struct inflate_state FAR *)strm->state; 567 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ 568 LOAD(); 569 in = have; 570 out = left; 571 ret = Z_OK; 572 for (;;) 573 switch (state->mode) { 574 case HEAD: 575 if (state->wrap == 0) { 576 state->mode = TYPEDO; 577 break; 578 } 579 NEEDBITS(16); 580 #ifdef GUNZIP 581 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ 582 state->check = crc32(0L, Z_NULL, 0); 583 CRC2(state->check, hold); 584 INITBITS(); 585 state->mode = FLAGS; 586 break; 587 } 588 state->flags = 0; /* expect zlib header */ 589 if (!(state->wrap & 1) || /* check if zlib header allowed */ 590 #else 591 if ( 592 #endif 593 ((BITS(8) << 8) + (hold >> 8)) % 31) { 594 #ifdef SMALL 595 strm->msg = "error"; 596 #else 597 strm->msg = (char *)"incorrect header check"; 598 #endif 599 state->mode = BAD; 600 break; 601 } 602 if (BITS(4) != Z_DEFLATED) { 603 #ifdef SMALL 604 strm->msg = "error"; 605 #else 606 strm->msg = (char *)"unknown compression method"; 607 #endif 608 state->mode = BAD; 609 break; 610 } 611 DROPBITS(4); 612 if (BITS(4) + 8 > state->wbits) { 613 #ifdef SMALL 614 strm->msg = "error"; 615 #else 616 strm->msg = (char *)"invalid window size"; 617 #endif 618 state->mode = BAD; 619 break; 620 } 621 Tracev((stderr, "inflate: zlib header ok\n")); 622 strm->adler = state->check = adler32(0L, Z_NULL, 0); 623 state->mode = hold & 0x200 ? DICTID : TYPE; 624 INITBITS(); 625 break; 626 #ifdef GUNZIP 627 case FLAGS: 628 NEEDBITS(16); 629 state->flags = (int)(hold); 630 if ((state->flags & 0xff) != Z_DEFLATED) { 631 #ifdef SMALL 632 strm->msg = "error"; 633 #else 634 strm->msg = (char *)"unknown compression method"; 635 #endif 636 state->mode = BAD; 637 break; 638 } 639 if (state->flags & 0xe000) { 640 #ifdef SMALL 641 strm->msg = "error"; 642 #else 643 strm->msg = (char *)"unknown header flags set"; 644 #endif 645 state->mode = BAD; 646 break; 647 } 648 if (state->flags & 0x0200) CRC2(state->check, hold); 649 INITBITS(); 650 state->mode = TIME; 651 case TIME: 652 NEEDBITS(32); 653 if (state->flags & 0x0200) CRC4(state->check, hold); 654 INITBITS(); 655 state->mode = OS; 656 case OS: 657 NEEDBITS(16); 658 if (state->flags & 0x0200) CRC2(state->check, hold); 659 INITBITS(); 660 state->mode = EXLEN; 661 case EXLEN: 662 if (state->flags & 0x0400) { 663 NEEDBITS(16); 664 state->length = (unsigned)(hold); 665 if (state->flags & 0x0200) CRC2(state->check, hold); 666 INITBITS(); 667 } 668 state->mode = EXTRA; 669 case EXTRA: 670 if (state->flags & 0x0400) { 671 copy = state->length; 672 if (copy > have) copy = have; 673 if (copy) { 674 if (state->flags & 0x0200) 675 state->check = crc32(state->check, next, copy); 676 have -= copy; 677 next += copy; 678 state->length -= copy; 679 } 680 if (state->length) goto inf_leave; 681 } 682 state->mode = NAME; 683 case NAME: 684 if (state->flags & 0x0800) { 685 if (have == 0) goto inf_leave; 686 copy = 0; 687 do { 688 len = (unsigned)(next[copy++]); 689 } while (len && copy < have); 690 if (state->flags & 0x02000) 691 state->check = crc32(state->check, next, copy); 692 have -= copy; 693 next += copy; 694 if (len) goto inf_leave; 695 } 696 state->mode = COMMENT; 697 case COMMENT: 698 if (state->flags & 0x1000) { 699 if (have == 0) goto inf_leave; 700 copy = 0; 701 do { 702 len = (unsigned)(next[copy++]); 703 } while (len && copy < have); 704 if (state->flags & 0x02000) 705 state->check = crc32(state->check, next, copy); 706 have -= copy; 707 next += copy; 708 if (len) goto inf_leave; 709 } 710 state->mode = HCRC; 711 case HCRC: 712 if (state->flags & 0x0200) { 713 NEEDBITS(16); 714 if (hold != (state->check & 0xffff)) { 715 #ifdef SMALL 716 strm->msg = "error"; 717 #else 718 strm->msg = (char *)"header crc mismatch"; 719 #endif 720 state->mode = BAD; 721 break; 722 } 723 INITBITS(); 724 } 725 strm->adler = state->check = crc32(0L, Z_NULL, 0); 726 state->mode = TYPE; 727 break; 728 #endif 729 case DICTID: 730 NEEDBITS(32); 731 strm->adler = state->check = REVERSE(hold); 732 INITBITS(); 733 state->mode = DICT; 734 case DICT: 735 if (state->havedict == 0) { 736 RESTORE(); 737 return Z_NEED_DICT; 738 } 739 strm->adler = state->check = adler32(0L, Z_NULL, 0); 740 state->mode = TYPE; 741 case TYPE: 742 if (flush == Z_BLOCK) goto inf_leave; 743 case TYPEDO: 744 if (state->last) { 745 BYTEBITS(); 746 state->mode = CHECK; 747 break; 748 } 749 NEEDBITS(3); 750 state->last = BITS(1); 751 DROPBITS(1); 752 switch (BITS(2)) { 753 case 0: /* stored block */ 754 Tracev((stderr, "inflate: stored block%s\n", 755 state->last ? " (last)" : "")); 756 state->mode = STORED; 757 break; 758 case 1: /* fixed block */ 759 fixedtables(state); 760 Tracev((stderr, "inflate: fixed codes block%s\n", 761 state->last ? " (last)" : "")); 762 state->mode = LEN; /* decode codes */ 763 break; 764 case 2: /* dynamic block */ 765 Tracev((stderr, "inflate: dynamic codes block%s\n", 766 state->last ? " (last)" : "")); 767 state->mode = TABLE; 768 break; 769 case 3: 770 #ifdef SMALL 771 strm->msg = "error"; 772 #else 773 strm->msg = (char *)"invalid block type"; 774 #endif 775 state->mode = BAD; 776 } 777 DROPBITS(2); 778 break; 779 case STORED: 780 BYTEBITS(); /* go to byte boundary */ 781 NEEDBITS(32); 782 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { 783 #ifdef SMALL 784 strm->msg = "error"; 785 #else 786 strm->msg = (char *)"invalid stored block lengths"; 787 #endif 788 state->mode = BAD; 789 break; 790 } 791 state->length = (unsigned)hold & 0xffff; 792 Tracev((stderr, "inflate: stored length %u\n", 793 state->length)); 794 INITBITS(); 795 state->mode = COPY; 796 case COPY: 797 copy = state->length; 798 if (copy) { 799 if (copy > have) copy = have; 800 if (copy > left) copy = left; 801 if (copy == 0) goto inf_leave; 802 zmemcpy(put, next, copy); 803 have -= copy; 804 next += copy; 805 left -= copy; 806 put += copy; 807 state->length -= copy; 808 break; 809 } 810 Tracev((stderr, "inflate: stored end\n")); 811 state->mode = TYPE; 812 break; 813 case TABLE: 814 NEEDBITS(14); 815 state->nlen = BITS(5) + 257; 816 DROPBITS(5); 817 state->ndist = BITS(5) + 1; 818 DROPBITS(5); 819 state->ncode = BITS(4) + 4; 820 DROPBITS(4); 821 #ifndef PKZIP_BUG_WORKAROUND 822 if (state->nlen > 286 || state->ndist > 30) { 823 #ifdef SMALL 824 strm->msg = "error"; 825 #else 826 strm->msg = (char *)"too many length or distance symbols"; 827 #endif 828 state->mode = BAD; 829 break; 830 } 831 #endif 832 Tracev((stderr, "inflate: table sizes ok\n")); 833 state->have = 0; 834 state->mode = LENLENS; 835 case LENLENS: 836 while (state->have < state->ncode) { 837 NEEDBITS(3); 838 state->lens[order[state->have++]] = (unsigned short)BITS(3); 839 DROPBITS(3); 840 } 841 while (state->have < 19) 842 state->lens[order[state->have++]] = 0; 843 state->next = state->codes; 844 state->lencode = (code const FAR *)(state->next); 845 state->lenbits = 7; 846 ret = inflate_table(CODES, state->lens, 19, &(state->next), 847 &(state->lenbits), state->work); 848 if (ret) { 849 #ifdef SMALL 850 strm->msg = "error"; 851 #else 852 strm->msg = (char *)"invalid code lengths set"; 853 #endif 854 state->mode = BAD; 855 break; 856 } 857 Tracev((stderr, "inflate: code lengths ok\n")); 858 state->have = 0; 859 state->mode = CODELENS; 860 case CODELENS: 861 while (state->have < state->nlen + state->ndist) { 862 for (;;) { 863 this = state->lencode[BITS(state->lenbits)]; 864 if ((unsigned)(this.bits) <= bits) break; 865 PULLBYTE(); 866 } 867 if (this.val < 16) { 868 NEEDBITS(this.bits); 869 DROPBITS(this.bits); 870 state->lens[state->have++] = this.val; 871 } 872 else { 873 if (this.val == 16) { 874 NEEDBITS(this.bits + 2); 875 DROPBITS(this.bits); 876 if (state->have == 0) { 877 #ifdef SMALL 878 strm->msg = "error"; 879 #else 880 strm->msg = (char *)"invalid bit length repeat"; 881 #endif 882 state->mode = BAD; 883 break; 884 } 885 len = state->lens[state->have - 1]; 886 copy = 3 + BITS(2); 887 DROPBITS(2); 888 } 889 else if (this.val == 17) { 890 NEEDBITS(this.bits + 3); 891 DROPBITS(this.bits); 892 len = 0; 893 copy = 3 + BITS(3); 894 DROPBITS(3); 895 } 896 else { 897 NEEDBITS(this.bits + 7); 898 DROPBITS(this.bits); 899 len = 0; 900 copy = 11 + BITS(7); 901 DROPBITS(7); 902 } 903 if (state->have + copy > state->nlen + state->ndist) { 904 #ifdef SMALL 905 strm->msg = "error"; 906 #else 907 strm->msg = (char *)"invalid bit length repeat"; 908 #endif 909 state->mode = BAD; 910 break; 911 } 912 while (copy--) 913 state->lens[state->have++] = (unsigned short)len; 914 } 915 } 916 917 /* handle error breaks in while */ 918 if (state->mode == BAD) break; 919 920 /* build code tables */ 921 state->next = state->codes; 922 state->lencode = (code const FAR *)(state->next); 923 state->lenbits = 9; 924 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), 925 &(state->lenbits), state->work); 926 if (ret) { 927 #ifdef SMALL 928 strm->msg = "error"; 929 #else 930 strm->msg = (char *)"invalid literal/lengths set"; 931 #endif 932 state->mode = BAD; 933 break; 934 } 935 state->distcode = (code const FAR *)(state->next); 936 state->distbits = 6; 937 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, 938 &(state->next), &(state->distbits), state->work); 939 if (ret) { 940 #ifdef SMALL 941 strm->msg = "error"; 942 #else 943 strm->msg = (char *)"invalid distances set"; 944 #endif 945 state->mode = BAD; 946 break; 947 } 948 Tracev((stderr, "inflate: codes ok\n")); 949 state->mode = LEN; 950 case LEN: 951 #ifndef SLOW 952 if (have >= 6 && left >= 258) { 953 RESTORE(); 954 inflate_fast(strm, out); 955 LOAD(); 956 break; 957 } 958 #endif 959 for (;;) { 960 this = state->lencode[BITS(state->lenbits)]; 961 if ((unsigned)(this.bits) <= bits) break; 962 PULLBYTE(); 963 } 964 if (this.op && (this.op & 0xf0) == 0) { 965 last = this; 966 for (;;) { 967 this = state->lencode[last.val + 968 (BITS(last.bits + last.op) >> last.bits)]; 969 if ((unsigned)(last.bits + this.bits) <= bits) break; 970 PULLBYTE(); 971 } 972 DROPBITS(last.bits); 973 } 974 DROPBITS(this.bits); 975 state->length = (unsigned)this.val; 976 if ((int)(this.op) == 0) { 977 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? 978 "inflate: literal '%c'\n" : 979 "inflate: literal 0x%02x\n", this.val)); 980 state->mode = LIT; 981 break; 982 } 983 if (this.op & 32) { 984 Tracevv((stderr, "inflate: end of block\n")); 985 state->mode = TYPE; 986 break; 987 } 988 if (this.op & 64) { 989 #ifdef SMALL 990 strm->msg = "error"; 991 #else 992 strm->msg = (char *)"invalid literal/length code"; 993 #endif 994 state->mode = BAD; 995 break; 996 } 997 state->extra = (unsigned)(this.op) & 15; 998 state->mode = LENEXT; 999 case LENEXT: 1000 if (state->extra) { 1001 NEEDBITS(state->extra); 1002 state->length += BITS(state->extra); 1003 DROPBITS(state->extra); 1004 } 1005 Tracevv((stderr, "inflate: length %u\n", state->length)); 1006 state->mode = DIST; 1007 case DIST: 1008 for (;;) { 1009 this = state->distcode[BITS(state->distbits)]; 1010 if ((unsigned)(this.bits) <= bits) break; 1011 PULLBYTE(); 1012 } 1013 if ((this.op & 0xf0) == 0) { 1014 last = this; 1015 for (;;) { 1016 this = state->distcode[last.val + 1017 (BITS(last.bits + last.op) >> last.bits)]; 1018 if ((unsigned)(last.bits + this.bits) <= bits) break; 1019 PULLBYTE(); 1020 } 1021 DROPBITS(last.bits); 1022 } 1023 DROPBITS(this.bits); 1024 if (this.op & 64) { 1025 #ifdef SMALL 1026 strm->msg = "error"; 1027 #else 1028 strm->msg = (char *)"invalid distance code"; 1029 #endif 1030 state->mode = BAD; 1031 break; 1032 } 1033 state->offset = (unsigned)this.val; 1034 state->extra = (unsigned)(this.op) & 15; 1035 state->mode = DISTEXT; 1036 case DISTEXT: 1037 if (state->extra) { 1038 NEEDBITS(state->extra); 1039 state->offset += BITS(state->extra); 1040 DROPBITS(state->extra); 1041 } 1042 if (state->offset > state->whave + out - left) { 1043 #ifdef SMALL 1044 strm->msg = "error"; 1045 #else 1046 strm->msg = (char *)"invalid distance too far back"; 1047 #endif 1048 state->mode = BAD; 1049 break; 1050 } 1051 Tracevv((stderr, "inflate: distance %u\n", state->offset)); 1052 state->mode = MATCH; 1053 case MATCH: 1054 if (left == 0) goto inf_leave; 1055 copy = out - left; 1056 if (state->offset > copy) { /* copy from window */ 1057 copy = state->offset - copy; 1058 if (copy > state->write) { 1059 copy -= state->write; 1060 from = state->window + (state->wsize - copy); 1061 } 1062 else 1063 from = state->window + (state->write - copy); 1064 if (copy > state->length) copy = state->length; 1065 } 1066 else { /* copy from output */ 1067 from = put - state->offset; 1068 copy = state->length; 1069 } 1070 if (copy > left) copy = left; 1071 left -= copy; 1072 state->length -= copy; 1073 do { 1074 *put++ = *from++; 1075 } while (--copy); 1076 if (state->length == 0) state->mode = LEN; 1077 break; 1078 case LIT: 1079 if (left == 0) goto inf_leave; 1080 *put++ = (unsigned char)(state->length); 1081 left--; 1082 state->mode = LEN; 1083 break; 1084 case CHECK: 1085 if (state->wrap) { 1086 NEEDBITS(32); 1087 out -= left; 1088 strm->total_out += out; 1089 state->total += out; 1090 if (out) 1091 strm->adler = state->check = 1092 UPDATE(state->check, put - out, out); 1093 out = left; 1094 if (( 1095 #ifdef GUNZIP 1096 state->flags ? hold : 1097 #endif 1098 REVERSE(hold)) != state->check) { 1099 #ifdef SMALL 1100 strm->msg = "error"; 1101 #else 1102 strm->msg = (char *)"incorrect data check"; 1103 #endif 1104 state->mode = BAD; 1105 break; 1106 } 1107 INITBITS(); 1108 Tracev((stderr, "inflate: check matches trailer\n")); 1109 } 1110 #ifdef GUNZIP 1111 state->mode = LENGTH; 1112 case LENGTH: 1113 if (state->wrap && state->flags) { 1114 NEEDBITS(32); 1115 if (hold != (state->total & 0xffffffffUL)) { 1116 #ifdef SMALL 1117 strm->msg = "error"; 1118 #else 1119 strm->msg = (char *)"incorrect length check"; 1120 #endif 1121 state->mode = BAD; 1122 break; 1123 } 1124 INITBITS(); 1125 Tracev((stderr, "inflate: length matches trailer\n")); 1126 } 1127 #endif 1128 state->mode = DONE; 1129 case DONE: 1130 ret = Z_STREAM_END; 1131 goto inf_leave; 1132 case BAD: 1133 ret = Z_DATA_ERROR; 1134 goto inf_leave; 1135 case MEM: 1136 return Z_MEM_ERROR; 1137 case SYNC: 1138 default: 1139 return Z_STREAM_ERROR; 1140 } 1141 1142 /* 1143 Return from inflate(), updating the total counts and the check value. 1144 If there was no progress during the inflate() call, return a buffer 1145 error. Call updatewindow() to create and/or update the window state. 1146 Note: a memory error from inflate() is non-recoverable. 1147 */ 1148 inf_leave: 1149 RESTORE(); 1150 if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) 1151 if (updatewindow(strm, out)) { 1152 state->mode = MEM; 1153 return Z_MEM_ERROR; 1154 } 1155 in -= strm->avail_in; 1156 out -= strm->avail_out; 1157 strm->total_in += in; 1158 strm->total_out += out; 1159 state->total += out; 1160 if (state->wrap && out) 1161 strm->adler = state->check = 1162 UPDATE(state->check, strm->next_out - out, out); 1163 strm->data_type = state->bits + (state->last ? 64 : 0) + 1164 (state->mode == TYPE ? 128 : 0); 1165 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) 1166 ret = Z_BUF_ERROR; 1167 return ret; 1168 } 1169 1170 int ZEXPORT inflateEnd(strm) 1171 z_streamp strm; 1172 { 1173 struct inflate_state FAR *state; 1174 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) 1175 return Z_STREAM_ERROR; 1176 state = (struct inflate_state FAR *)strm->state; 1177 if (state->window != Z_NULL) ZFREE(strm, state->window); 1178 ZFREE(strm, strm->state); 1179 strm->state = Z_NULL; 1180 Tracev((stderr, "inflate: end\n")); 1181 return Z_OK; 1182 } 1183 1184 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) 1185 z_streamp strm; 1186 const Bytef *dictionary; 1187 uInt dictLength; 1188 { 1189 struct inflate_state FAR *state; 1190 unsigned long id; 1191 1192 /* check state */ 1193 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 1194 state = (struct inflate_state FAR *)strm->state; 1195 if (state->mode != DICT) return Z_STREAM_ERROR; 1196 1197 /* check for correct dictionary id */ 1198 id = adler32(0L, Z_NULL, 0); 1199 id = adler32(id, dictionary, dictLength); 1200 if (id != state->check) return Z_DATA_ERROR; 1201 1202 /* copy dictionary to window */ 1203 if (updatewindow(strm, strm->avail_out)) { 1204 state->mode = MEM; 1205 return Z_MEM_ERROR; 1206 } 1207 if (dictLength > state->wsize) { 1208 zmemcpy(state->window, dictionary + dictLength - state->wsize, 1209 state->wsize); 1210 state->whave = state->wsize; 1211 } 1212 else { 1213 zmemcpy(state->window + state->wsize - dictLength, dictionary, 1214 dictLength); 1215 state->whave = dictLength; 1216 } 1217 state->havedict = 1; 1218 Tracev((stderr, "inflate: dictionary set\n")); 1219 return Z_OK; 1220 } 1221 1222 /* 1223 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found 1224 or when out of input. When called, *have is the number of pattern bytes 1225 found in order so far, in 0..3. On return *have is updated to the new 1226 state. If on return *have equals four, then the pattern was found and the 1227 return value is how many bytes were read including the last byte of the 1228 pattern. If *have is less than four, then the pattern has not been found 1229 yet and the return value is len. In the latter case, syncsearch() can be 1230 called again with more data and the *have state. *have is initialized to 1231 zero for the first call. 1232 */ 1233 local unsigned syncsearch(have, buf, len) 1234 unsigned FAR *have; 1235 unsigned char FAR *buf; 1236 unsigned len; 1237 { 1238 unsigned got; 1239 unsigned next; 1240 1241 got = *have; 1242 next = 0; 1243 while (next < len && got < 4) { 1244 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) 1245 got++; 1246 else if (buf[next]) 1247 got = 0; 1248 else 1249 got = 4 - got; 1250 next++; 1251 } 1252 *have = got; 1253 return next; 1254 } 1255 1256 int ZEXPORT inflateSync(strm) 1257 z_streamp strm; 1258 { 1259 unsigned len; /* number of bytes to look at or looked at */ 1260 unsigned long in, out; /* temporary to save total_in and total_out */ 1261 unsigned char buf[4]; /* to restore bit buffer to byte string */ 1262 struct inflate_state FAR *state; 1263 1264 /* check parameters */ 1265 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 1266 state = (struct inflate_state FAR *)strm->state; 1267 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; 1268 1269 /* if first time, start search in bit buffer */ 1270 if (state->mode != SYNC) { 1271 state->mode = SYNC; 1272 state->hold <<= state->bits & 7; 1273 state->bits -= state->bits & 7; 1274 len = 0; 1275 while (state->bits >= 8) { 1276 buf[len++] = (unsigned char)(state->hold); 1277 state->hold >>= 8; 1278 state->bits -= 8; 1279 } 1280 state->have = 0; 1281 syncsearch(&(state->have), buf, len); 1282 } 1283 1284 /* search available input */ 1285 len = syncsearch(&(state->have), strm->next_in, strm->avail_in); 1286 strm->avail_in -= len; 1287 strm->next_in += len; 1288 strm->total_in += len; 1289 1290 /* return no joy or set up to restart inflate() on a new block */ 1291 if (state->have != 4) return Z_DATA_ERROR; 1292 in = strm->total_in; out = strm->total_out; 1293 inflateReset(strm); 1294 strm->total_in = in; strm->total_out = out; 1295 state->mode = TYPE; 1296 return Z_OK; 1297 } 1298 1299 /* 1300 Returns true if inflate is currently at the end of a block generated by 1301 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP 1302 implementation to provide an additional safety check. PPP uses 1303 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored 1304 block. When decompressing, PPP checks that at the end of input packet, 1305 inflate is waiting for these length bytes. 1306 */ 1307 int ZEXPORT inflateSyncPoint(strm) 1308 z_streamp strm; 1309 { 1310 struct inflate_state FAR *state; 1311 1312 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 1313 state = (struct inflate_state FAR *)strm->state; 1314 return state->mode == STORED && state->bits == 0; 1315 } 1316 1317 int ZEXPORT inflateCopy(dest, source) 1318 z_streamp dest; 1319 z_streamp source; 1320 { 1321 struct inflate_state FAR *state; 1322 struct inflate_state FAR *copy; 1323 unsigned char FAR *window; 1324 1325 /* check input */ 1326 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || 1327 source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) 1328 return Z_STREAM_ERROR; 1329 state = (struct inflate_state FAR *)source->state; 1330 1331 /* allocate space */ 1332 copy = (struct inflate_state FAR *) 1333 ZALLOC(source, 1, sizeof(struct inflate_state)); 1334 if (copy == Z_NULL) return Z_MEM_ERROR; 1335 window = Z_NULL; 1336 if (state->window != Z_NULL) { 1337 window = (unsigned char FAR *) 1338 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); 1339 if (window == Z_NULL) { 1340 ZFREE(source, copy); 1341 return Z_MEM_ERROR; 1342 } 1343 } 1344 1345 /* copy state */ 1346 *dest = *source; 1347 *copy = *state; 1348 copy->lencode = copy->codes + (state->lencode - state->codes); 1349 copy->distcode = copy->codes + (state->distcode - state->codes); 1350 copy->next = copy->codes + (state->next - state->codes); 1351 if (window != Z_NULL) 1352 zmemcpy(window, state->window, 1U << state->wbits); 1353 copy->window = window; 1354 dest->state = (voidpf)copy; 1355 return Z_OK; 1356 } 1357