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