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