OLD | NEW |
1 /* infback.c -- inflate using a call-back interface | 1 /* infback.c -- inflate using a call-back interface |
2 * Copyright (C) 1995-2005 Mark Adler | 2 * Copyright (C) 1995-2009 Mark Adler |
3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
4 */ | 4 */ |
5 | 5 |
6 /* | 6 /* |
7 This code is largely copied from inflate.c. Normally either infback.o or | 7 This code is largely copied from inflate.c. Normally either infback.o or |
8 inflate.o would be linked into an application--not both. The interface | 8 inflate.o would be linked into an application--not both. The interface |
9 with inffast.c is retained so that optimized assembler-coded versions of | 9 with inffast.c is retained so that optimized assembler-coded versions of |
10 inflate_fast() can be used with either inflate.c or infback.c. | 10 inflate_fast() can be used with either inflate.c or infback.c. |
11 */ | 11 */ |
12 | 12 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 if (strm->zfree == (free_func)0) strm->zfree = zcfree; | 48 if (strm->zfree == (free_func)0) strm->zfree = zcfree; |
49 state = (struct inflate_state FAR *)ZALLOC(strm, 1, | 49 state = (struct inflate_state FAR *)ZALLOC(strm, 1, |
50 sizeof(struct inflate_state)); | 50 sizeof(struct inflate_state)); |
51 if (state == Z_NULL) return Z_MEM_ERROR; | 51 if (state == Z_NULL) return Z_MEM_ERROR; |
52 Tracev((stderr, "inflate: allocated\n")); | 52 Tracev((stderr, "inflate: allocated\n")); |
53 strm->state = (struct internal_state FAR *)state; | 53 strm->state = (struct internal_state FAR *)state; |
54 state->dmax = 32768U; | 54 state->dmax = 32768U; |
55 state->wbits = windowBits; | 55 state->wbits = windowBits; |
56 state->wsize = 1U << windowBits; | 56 state->wsize = 1U << windowBits; |
57 state->window = window; | 57 state->window = window; |
58 state->write = 0; | 58 state->wnext = 0; |
59 state->whave = 0; | 59 state->whave = 0; |
60 return Z_OK; | 60 return Z_OK; |
61 } | 61 } |
62 | 62 |
63 /* | 63 /* |
64 Return state with length and distance decoding tables and index sizes set to | 64 Return state with length and distance decoding tables and index sizes set to |
65 fixed code decoding. Normally this returns fixed tables from inffixed.h. | 65 fixed code decoding. Normally this returns fixed tables from inffixed.h. |
66 If BUILDFIXED is defined, then instead this routine builds the tables the | 66 If BUILDFIXED is defined, then instead this routine builds the tables the |
67 first time it's called, and returns those tables the first time and | 67 first time it's called, and returns those tables the first time and |
68 thereafter. This reduces the size of the code by about 2K bytes, in | 68 thereafter. This reduces the size of the code by about 2K bytes, in |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 void FAR *out_desc; | 246 void FAR *out_desc; |
247 { | 247 { |
248 struct inflate_state FAR *state; | 248 struct inflate_state FAR *state; |
249 unsigned char FAR *next; /* next input */ | 249 unsigned char FAR *next; /* next input */ |
250 unsigned char FAR *put; /* next output */ | 250 unsigned char FAR *put; /* next output */ |
251 unsigned have, left; /* available input and output */ | 251 unsigned have, left; /* available input and output */ |
252 unsigned long hold; /* bit buffer */ | 252 unsigned long hold; /* bit buffer */ |
253 unsigned bits; /* bits in bit buffer */ | 253 unsigned bits; /* bits in bit buffer */ |
254 unsigned copy; /* number of stored or match bytes to copy */ | 254 unsigned copy; /* number of stored or match bytes to copy */ |
255 unsigned char FAR *from; /* where to copy match bytes from */ | 255 unsigned char FAR *from; /* where to copy match bytes from */ |
256 code this; /* current decoding table entry */ | 256 code here; /* current decoding table entry */ |
257 code last; /* parent table entry */ | 257 code last; /* parent table entry */ |
258 unsigned len; /* length to copy for repeats, bits to drop */ | 258 unsigned len; /* length to copy for repeats, bits to drop */ |
259 int ret; /* return code */ | 259 int ret; /* return code */ |
260 static const unsigned short order[19] = /* permutation of code lengths */ | 260 static const unsigned short order[19] = /* permutation of code lengths */ |
261 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 261 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
262 | 262 |
263 /* Check that the strm exists and that the state was initialized */ | 263 /* Check that the strm exists and that the state was initialized */ |
264 if (strm == Z_NULL || strm->state == Z_NULL) | 264 if (strm == Z_NULL || strm->state == Z_NULL) |
265 return Z_STREAM_ERROR; | 265 return Z_STREAM_ERROR; |
266 state = (struct inflate_state FAR *)strm->state; | 266 state = (struct inflate_state FAR *)strm->state; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 strm->msg = (char *)"invalid code lengths set"; | 382 strm->msg = (char *)"invalid code lengths set"; |
383 state->mode = BAD; | 383 state->mode = BAD; |
384 break; | 384 break; |
385 } | 385 } |
386 Tracev((stderr, "inflate: code lengths ok\n")); | 386 Tracev((stderr, "inflate: code lengths ok\n")); |
387 | 387 |
388 /* get length and distance code code lengths */ | 388 /* get length and distance code code lengths */ |
389 state->have = 0; | 389 state->have = 0; |
390 while (state->have < state->nlen + state->ndist) { | 390 while (state->have < state->nlen + state->ndist) { |
391 for (;;) { | 391 for (;;) { |
392 this = state->lencode[BITS(state->lenbits)]; | 392 here = state->lencode[BITS(state->lenbits)]; |
393 if ((unsigned)(this.bits) <= bits) break; | 393 if ((unsigned)(here.bits) <= bits) break; |
394 PULLBYTE(); | 394 PULLBYTE(); |
395 } | 395 } |
396 if (this.val < 16) { | 396 if (here.val < 16) { |
397 NEEDBITS(this.bits); | 397 NEEDBITS(here.bits); |
398 DROPBITS(this.bits); | 398 DROPBITS(here.bits); |
399 state->lens[state->have++] = this.val; | 399 state->lens[state->have++] = here.val; |
400 } | 400 } |
401 else { | 401 else { |
402 if (this.val == 16) { | 402 if (here.val == 16) { |
403 NEEDBITS(this.bits + 2); | 403 NEEDBITS(here.bits + 2); |
404 DROPBITS(this.bits); | 404 DROPBITS(here.bits); |
405 if (state->have == 0) { | 405 if (state->have == 0) { |
406 strm->msg = (char *)"invalid bit length repeat"; | 406 strm->msg = (char *)"invalid bit length repeat"; |
407 state->mode = BAD; | 407 state->mode = BAD; |
408 break; | 408 break; |
409 } | 409 } |
410 len = (unsigned)(state->lens[state->have - 1]); | 410 len = (unsigned)(state->lens[state->have - 1]); |
411 copy = 3 + BITS(2); | 411 copy = 3 + BITS(2); |
412 DROPBITS(2); | 412 DROPBITS(2); |
413 } | 413 } |
414 else if (this.val == 17) { | 414 else if (here.val == 17) { |
415 NEEDBITS(this.bits + 3); | 415 NEEDBITS(here.bits + 3); |
416 DROPBITS(this.bits); | 416 DROPBITS(here.bits); |
417 len = 0; | 417 len = 0; |
418 copy = 3 + BITS(3); | 418 copy = 3 + BITS(3); |
419 DROPBITS(3); | 419 DROPBITS(3); |
420 } | 420 } |
421 else { | 421 else { |
422 NEEDBITS(this.bits + 7); | 422 NEEDBITS(here.bits + 7); |
423 DROPBITS(this.bits); | 423 DROPBITS(here.bits); |
424 len = 0; | 424 len = 0; |
425 copy = 11 + BITS(7); | 425 copy = 11 + BITS(7); |
426 DROPBITS(7); | 426 DROPBITS(7); |
427 } | 427 } |
428 if (state->have + copy > state->nlen + state->ndist) { | 428 if (state->have + copy > state->nlen + state->ndist) { |
429 strm->msg = (char *)"invalid bit length repeat"; | 429 strm->msg = (char *)"invalid bit length repeat"; |
430 state->mode = BAD; | 430 state->mode = BAD; |
431 break; | 431 break; |
432 } | 432 } |
433 while (copy--) | 433 while (copy--) |
434 state->lens[state->have++] = (unsigned short)len; | 434 state->lens[state->have++] = (unsigned short)len; |
435 } | 435 } |
436 } | 436 } |
437 | 437 |
438 /* handle error breaks in while */ | 438 /* handle error breaks in while */ |
439 if (state->mode == BAD) break; | 439 if (state->mode == BAD) break; |
440 | 440 |
441 /* build code tables */ | 441 /* check for end-of-block code (better have one) */ |
| 442 if (state->lens[256] == 0) { |
| 443 strm->msg = (char *)"invalid code -- missing end-of-block"; |
| 444 state->mode = BAD; |
| 445 break; |
| 446 } |
| 447 |
| 448 /* build code tables -- note: do not change the lenbits or distbits |
| 449 values here (9 and 6) without reading the comments in inftrees.h |
| 450 concerning the ENOUGH constants, which depend on those values */ |
442 state->next = state->codes; | 451 state->next = state->codes; |
443 state->lencode = (code const FAR *)(state->next); | 452 state->lencode = (code const FAR *)(state->next); |
444 state->lenbits = 9; | 453 state->lenbits = 9; |
445 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), | 454 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), |
446 &(state->lenbits), state->work); | 455 &(state->lenbits), state->work); |
447 if (ret) { | 456 if (ret) { |
448 strm->msg = (char *)"invalid literal/lengths set"; | 457 strm->msg = (char *)"invalid literal/lengths set"; |
449 state->mode = BAD; | 458 state->mode = BAD; |
450 break; | 459 break; |
451 } | 460 } |
(...skipping 15 matching lines...) Expand all Loading... |
467 RESTORE(); | 476 RESTORE(); |
468 if (state->whave < state->wsize) | 477 if (state->whave < state->wsize) |
469 state->whave = state->wsize - left; | 478 state->whave = state->wsize - left; |
470 inflate_fast(strm, state->wsize); | 479 inflate_fast(strm, state->wsize); |
471 LOAD(); | 480 LOAD(); |
472 break; | 481 break; |
473 } | 482 } |
474 | 483 |
475 /* get a literal, length, or end-of-block code */ | 484 /* get a literal, length, or end-of-block code */ |
476 for (;;) { | 485 for (;;) { |
477 this = state->lencode[BITS(state->lenbits)]; | 486 here = state->lencode[BITS(state->lenbits)]; |
478 if ((unsigned)(this.bits) <= bits) break; | 487 if ((unsigned)(here.bits) <= bits) break; |
479 PULLBYTE(); | 488 PULLBYTE(); |
480 } | 489 } |
481 if (this.op && (this.op & 0xf0) == 0) { | 490 if (here.op && (here.op & 0xf0) == 0) { |
482 last = this; | 491 last = here; |
483 for (;;) { | 492 for (;;) { |
484 this = state->lencode[last.val + | 493 here = state->lencode[last.val + |
485 (BITS(last.bits + last.op) >> last.bits)]; | 494 (BITS(last.bits + last.op) >> last.bits)]; |
486 if ((unsigned)(last.bits + this.bits) <= bits) break; | 495 if ((unsigned)(last.bits + here.bits) <= bits) break; |
487 PULLBYTE(); | 496 PULLBYTE(); |
488 } | 497 } |
489 DROPBITS(last.bits); | 498 DROPBITS(last.bits); |
490 } | 499 } |
491 DROPBITS(this.bits); | 500 DROPBITS(here.bits); |
492 state->length = (unsigned)this.val; | 501 state->length = (unsigned)here.val; |
493 | 502 |
494 /* process literal */ | 503 /* process literal */ |
495 if (this.op == 0) { | 504 if (here.op == 0) { |
496 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? | 505 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
497 "inflate: literal '%c'\n" : | 506 "inflate: literal '%c'\n" : |
498 "inflate: literal 0x%02x\n", this.val)); | 507 "inflate: literal 0x%02x\n", here.val)); |
499 ROOM(); | 508 ROOM(); |
500 *put++ = (unsigned char)(state->length); | 509 *put++ = (unsigned char)(state->length); |
501 left--; | 510 left--; |
502 state->mode = LEN; | 511 state->mode = LEN; |
503 break; | 512 break; |
504 } | 513 } |
505 | 514 |
506 /* process end of block */ | 515 /* process end of block */ |
507 if (this.op & 32) { | 516 if (here.op & 32) { |
508 Tracevv((stderr, "inflate: end of block\n")); | 517 Tracevv((stderr, "inflate: end of block\n")); |
509 state->mode = TYPE; | 518 state->mode = TYPE; |
510 break; | 519 break; |
511 } | 520 } |
512 | 521 |
513 /* invalid code */ | 522 /* invalid code */ |
514 if (this.op & 64) { | 523 if (here.op & 64) { |
515 strm->msg = (char *)"invalid literal/length code"; | 524 strm->msg = (char *)"invalid literal/length code"; |
516 state->mode = BAD; | 525 state->mode = BAD; |
517 break; | 526 break; |
518 } | 527 } |
519 | 528 |
520 /* length code -- get extra bits, if any */ | 529 /* length code -- get extra bits, if any */ |
521 state->extra = (unsigned)(this.op) & 15; | 530 state->extra = (unsigned)(here.op) & 15; |
522 if (state->extra != 0) { | 531 if (state->extra != 0) { |
523 NEEDBITS(state->extra); | 532 NEEDBITS(state->extra); |
524 state->length += BITS(state->extra); | 533 state->length += BITS(state->extra); |
525 DROPBITS(state->extra); | 534 DROPBITS(state->extra); |
526 } | 535 } |
527 Tracevv((stderr, "inflate: length %u\n", state->length)); | 536 Tracevv((stderr, "inflate: length %u\n", state->length)); |
528 | 537 |
529 /* get distance code */ | 538 /* get distance code */ |
530 for (;;) { | 539 for (;;) { |
531 this = state->distcode[BITS(state->distbits)]; | 540 here = state->distcode[BITS(state->distbits)]; |
532 if ((unsigned)(this.bits) <= bits) break; | 541 if ((unsigned)(here.bits) <= bits) break; |
533 PULLBYTE(); | 542 PULLBYTE(); |
534 } | 543 } |
535 if ((this.op & 0xf0) == 0) { | 544 if ((here.op & 0xf0) == 0) { |
536 last = this; | 545 last = here; |
537 for (;;) { | 546 for (;;) { |
538 this = state->distcode[last.val + | 547 here = state->distcode[last.val + |
539 (BITS(last.bits + last.op) >> last.bits)]; | 548 (BITS(last.bits + last.op) >> last.bits)]; |
540 if ((unsigned)(last.bits + this.bits) <= bits) break; | 549 if ((unsigned)(last.bits + here.bits) <= bits) break; |
541 PULLBYTE(); | 550 PULLBYTE(); |
542 } | 551 } |
543 DROPBITS(last.bits); | 552 DROPBITS(last.bits); |
544 } | 553 } |
545 DROPBITS(this.bits); | 554 DROPBITS(here.bits); |
546 if (this.op & 64) { | 555 if (here.op & 64) { |
547 strm->msg = (char *)"invalid distance code"; | 556 strm->msg = (char *)"invalid distance code"; |
548 state->mode = BAD; | 557 state->mode = BAD; |
549 break; | 558 break; |
550 } | 559 } |
551 state->offset = (unsigned)this.val; | 560 state->offset = (unsigned)here.val; |
552 | 561 |
553 /* get distance extra bits, if any */ | 562 /* get distance extra bits, if any */ |
554 state->extra = (unsigned)(this.op) & 15; | 563 state->extra = (unsigned)(here.op) & 15; |
555 if (state->extra != 0) { | 564 if (state->extra != 0) { |
556 NEEDBITS(state->extra); | 565 NEEDBITS(state->extra); |
557 state->offset += BITS(state->extra); | 566 state->offset += BITS(state->extra); |
558 DROPBITS(state->extra); | 567 DROPBITS(state->extra); |
559 } | 568 } |
560 if (state->offset > state->wsize - (state->whave < state->wsize ? | 569 if (state->offset > state->wsize - (state->whave < state->wsize ? |
561 left : 0)) { | 570 left : 0)) { |
562 strm->msg = (char *)"invalid distance too far back"; | 571 strm->msg = (char *)"invalid distance too far back"; |
563 state->mode = BAD; | 572 state->mode = BAD; |
564 break; | 573 break; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 int ZEXPORT inflateBackEnd(strm) | 623 int ZEXPORT inflateBackEnd(strm) |
615 z_streamp strm; | 624 z_streamp strm; |
616 { | 625 { |
617 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) | 626 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) |
618 return Z_STREAM_ERROR; | 627 return Z_STREAM_ERROR; |
619 ZFREE(strm, strm->state); | 628 ZFREE(strm, strm->state); |
620 strm->state = Z_NULL; | 629 strm->state = Z_NULL; |
621 Tracev((stderr, "inflate: end\n")); | 630 Tracev((stderr, "inflate: end\n")); |
622 return Z_OK; | 631 return Z_OK; |
623 } | 632 } |
OLD | NEW |