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