diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-09-16 07:25:41 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-09-16 07:25:41 +0000 |
commit | 8fede28c7408b2231a50c88da22c2e07f26f6d56 (patch) | |
tree | 755234d2b15e608a57dfcda690dd8e3fa8ea2a2a | |
parent | a9cc8961eda213ae40a7faa9427ca5be166a3154 (diff) | |
download | busybox-w32-8fede28c7408b2231a50c88da22c2e07f26f6d56.tar.gz busybox-w32-8fede28c7408b2231a50c88da22c2e07f26f6d56.tar.bz2 busybox-w32-8fede28c7408b2231a50c88da22c2e07f26f6d56.zip |
Patch from Matthias Lang <matthias@corelatus.se> to fix gunzip
error handling and prevent gunzip from hanging.
-rw-r--r-- | archival/libunarchive/decompress_unzip.c | 89 | ||||
-rw-r--r-- | archival/libunarchive/unzip.c | 89 | ||||
-rw-r--r-- | libbb/unzip.c | 89 |
3 files changed, 189 insertions, 78 deletions
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index 0da60c3be..d84067068 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c | |||
@@ -390,6 +390,7 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
390 | unsigned ml, md; /* masks for bl and bd bits */ | 390 | unsigned ml, md; /* masks for bl and bd bits */ |
391 | register unsigned long b; /* bit buffer */ | 391 | register unsigned long b; /* bit buffer */ |
392 | register unsigned k; /* number of bits in bit buffer */ | 392 | register unsigned k; /* number of bits in bit buffer */ |
393 | register int input_char; | ||
393 | 394 | ||
394 | /* make local copies of globals */ | 395 | /* make local copies of globals */ |
395 | b = bb; /* initialize bit buffer */ | 396 | b = bb; /* initialize bit buffer */ |
@@ -401,7 +402,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
401 | md = mask_bits[bd]; | 402 | md = mask_bits[bd]; |
402 | for (;;) { /* do until end of block */ | 403 | for (;;) { /* do until end of block */ |
403 | while (k < (unsigned) bl) { | 404 | while (k < (unsigned) bl) { |
404 | b |= ((unsigned long) fgetc(in_file)) << k; | 405 | input_char = fgetc(in_file); |
406 | if (input_char == EOF) return 1; | ||
407 | b |= ((unsigned long)input_char) << k; | ||
405 | k += 8; | 408 | k += 8; |
406 | } | 409 | } |
407 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | 410 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
@@ -413,7 +416,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
413 | k -= t->b; | 416 | k -= t->b; |
414 | e -= 16; | 417 | e -= 16; |
415 | while (k < e) { | 418 | while (k < e) { |
416 | b |= ((unsigned long) fgetc(in_file)) << k; | 419 | input_char = fgetc(in_file); |
420 | if (input_char == EOF) return 1; | ||
421 | b |= ((unsigned long)input_char) << k; | ||
417 | k += 8; | 422 | k += 8; |
418 | } | 423 | } |
419 | } while ((e = | 424 | } while ((e = |
@@ -435,7 +440,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
435 | 440 | ||
436 | /* get length of block to copy */ | 441 | /* get length of block to copy */ |
437 | while (k < e) { | 442 | while (k < e) { |
438 | b |= ((unsigned long) fgetc(in_file)) << k; | 443 | input_char = fgetc(in_file); |
444 | if (input_char == EOF) return 1; | ||
445 | b |= ((unsigned long)input_char) << k; | ||
439 | k += 8; | 446 | k += 8; |
440 | } | 447 | } |
441 | n = t->v.n + ((unsigned) b & mask_bits[e]); | 448 | n = t->v.n + ((unsigned) b & mask_bits[e]); |
@@ -444,7 +451,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
444 | 451 | ||
445 | /* decode distance of block to copy */ | 452 | /* decode distance of block to copy */ |
446 | while (k < (unsigned) bd) { | 453 | while (k < (unsigned) bd) { |
447 | b |= ((unsigned long) fgetc(in_file)) << k; | 454 | input_char = fgetc(in_file); |
455 | if (input_char == EOF) return 1; | ||
456 | b |= ((unsigned long)input_char) << k; | ||
448 | k += 8; | 457 | k += 8; |
449 | } | 458 | } |
450 | 459 | ||
@@ -456,7 +465,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
456 | k -= t->b; | 465 | k -= t->b; |
457 | e -= 16; | 466 | e -= 16; |
458 | while (k < e) { | 467 | while (k < e) { |
459 | b |= ((unsigned long) fgetc(in_file)) << k; | 468 | input_char = fgetc(in_file); |
469 | if (input_char == EOF) return 1; | ||
470 | b |= ((unsigned long)input_char) << k; | ||
460 | k += 8; | 471 | k += 8; |
461 | } | 472 | } |
462 | } while ((e = | 473 | } while ((e = |
@@ -465,7 +476,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
465 | b >>= t->b; | 476 | b >>= t->b; |
466 | k -= t->b; | 477 | k -= t->b; |
467 | while (k < e) { | 478 | while (k < e) { |
468 | b |= ((unsigned long) fgetc(in_file)) << k; | 479 | input_char = fgetc(in_file); |
480 | if (input_char == EOF) return 1; | ||
481 | b |= ((unsigned long)input_char) << k; | ||
469 | k += 8; | 482 | k += 8; |
470 | } | 483 | } |
471 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); | 484 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
@@ -541,6 +554,7 @@ static int inflate_block(int *e) | |||
541 | unsigned t; /* block type */ | 554 | unsigned t; /* block type */ |
542 | register unsigned long b; /* bit buffer */ | 555 | register unsigned long b; /* bit buffer */ |
543 | register unsigned k; /* number of bits in bit buffer */ | 556 | register unsigned k; /* number of bits in bit buffer */ |
557 | int input_char; | ||
544 | 558 | ||
545 | /* make local bit buffer */ | 559 | /* make local bit buffer */ |
546 | b = bb; | 560 | b = bb; |
@@ -548,7 +562,9 @@ static int inflate_block(int *e) | |||
548 | 562 | ||
549 | /* read in last block bit */ | 563 | /* read in last block bit */ |
550 | while (k < 1) { | 564 | while (k < 1) { |
551 | b |= ((unsigned long) fgetc(in_file)) << k; | 565 | input_char = fgetc(in_file); |
566 | if (input_char == EOF) return 1; | ||
567 | b |= ((unsigned long)input_char) << k; | ||
552 | k += 8; | 568 | k += 8; |
553 | } | 569 | } |
554 | *e = (int) b & 1; | 570 | *e = (int) b & 1; |
@@ -557,7 +573,9 @@ static int inflate_block(int *e) | |||
557 | 573 | ||
558 | /* read in block type */ | 574 | /* read in block type */ |
559 | while (k < 2) { | 575 | while (k < 2) { |
560 | b |= ((unsigned long) fgetc(in_file)) << k; | 576 | input_char = fgetc(in_file); |
577 | if (input_char == EOF) return 1; | ||
578 | b |= ((unsigned long)input_char) << k; | ||
561 | k += 8; | 579 | k += 8; |
562 | } | 580 | } |
563 | t = (unsigned) b & 3; | 581 | t = (unsigned) b & 3; |
@@ -589,14 +607,18 @@ static int inflate_block(int *e) | |||
589 | 607 | ||
590 | /* get the length and its complement */ | 608 | /* get the length and its complement */ |
591 | while (k_stored < 16) { | 609 | while (k_stored < 16) { |
592 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 610 | input_char = fgetc(in_file); |
611 | if (input_char == EOF) return 1; | ||
612 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
593 | k_stored += 8; | 613 | k_stored += 8; |
594 | } | 614 | } |
595 | n = ((unsigned) b_stored & 0xffff); | 615 | n = ((unsigned) b_stored & 0xffff); |
596 | b_stored >>= 16; | 616 | b_stored >>= 16; |
597 | k_stored -= 16; | 617 | k_stored -= 16; |
598 | while (k_stored < 16) { | 618 | while (k_stored < 16) { |
599 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 619 | input_char = fgetc(in_file); |
620 | if (input_char == EOF) return 1; | ||
621 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
600 | k_stored += 8; | 622 | k_stored += 8; |
601 | } | 623 | } |
602 | if (n != (unsigned) ((~b_stored) & 0xffff)) { | 624 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
@@ -608,7 +630,9 @@ static int inflate_block(int *e) | |||
608 | /* read and output the compressed data */ | 630 | /* read and output the compressed data */ |
609 | while (n--) { | 631 | while (n--) { |
610 | while (k_stored < 8) { | 632 | while (k_stored < 8) { |
611 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 633 | input_char = fgetc(in_file); |
634 | if (input_char == EOF) return 1; | ||
635 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
612 | k_stored += 8; | 636 | k_stored += 8; |
613 | } | 637 | } |
614 | window[w++] = (unsigned char) b_stored; | 638 | window[w++] = (unsigned char) b_stored; |
@@ -704,21 +728,27 @@ static int inflate_block(int *e) | |||
704 | 728 | ||
705 | /* read in table lengths */ | 729 | /* read in table lengths */ |
706 | while (k_dynamic < 5) { | 730 | while (k_dynamic < 5) { |
707 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 731 | input_char = fgetc(in_file); |
732 | if (input_char == EOF) return 1; | ||
733 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
708 | k_dynamic += 8; | 734 | k_dynamic += 8; |
709 | } | 735 | } |
710 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ | 736 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ |
711 | b_dynamic >>= 5; | 737 | b_dynamic >>= 5; |
712 | k_dynamic -= 5; | 738 | k_dynamic -= 5; |
713 | while (k_dynamic < 5) { | 739 | while (k_dynamic < 5) { |
714 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 740 | input_char = fgetc(in_file); |
741 | if (input_char == EOF) return 1; | ||
742 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
715 | k_dynamic += 8; | 743 | k_dynamic += 8; |
716 | } | 744 | } |
717 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ | 745 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ |
718 | b_dynamic >>= 5; | 746 | b_dynamic >>= 5; |
719 | k_dynamic -= 5; | 747 | k_dynamic -= 5; |
720 | while (k_dynamic < 4) { | 748 | while (k_dynamic < 4) { |
721 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 749 | input_char = fgetc(in_file); |
750 | if (input_char == EOF) return 1; | ||
751 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
722 | k_dynamic += 8; | 752 | k_dynamic += 8; |
723 | } | 753 | } |
724 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ | 754 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ |
@@ -731,7 +761,9 @@ static int inflate_block(int *e) | |||
731 | /* read in bit-length-code lengths */ | 761 | /* read in bit-length-code lengths */ |
732 | for (j = 0; j < nb; j++) { | 762 | for (j = 0; j < nb; j++) { |
733 | while (k_dynamic < 3) { | 763 | while (k_dynamic < 3) { |
734 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 764 | input_char = fgetc(in_file); |
765 | if (input_char == EOF) return 1; | ||
766 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
735 | k_dynamic += 8; | 767 | k_dynamic += 8; |
736 | } | 768 | } |
737 | ll[border[j]] = (unsigned) b_dynamic & 7; | 769 | ll[border[j]] = (unsigned) b_dynamic & 7; |
@@ -757,7 +789,9 @@ static int inflate_block(int *e) | |||
757 | i = l = 0; | 789 | i = l = 0; |
758 | while ((unsigned) i < n) { | 790 | while ((unsigned) i < n) { |
759 | while (k_dynamic < (unsigned) bl) { | 791 | while (k_dynamic < (unsigned) bl) { |
760 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 792 | input_char = fgetc(in_file); |
793 | if (input_char == EOF) return 1; | ||
794 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
761 | k_dynamic += 8; | 795 | k_dynamic += 8; |
762 | } | 796 | } |
763 | j = (td = tl + ((unsigned) b_dynamic & m))->b; | 797 | j = (td = tl + ((unsigned) b_dynamic & m))->b; |
@@ -768,8 +802,9 @@ static int inflate_block(int *e) | |||
768 | ll[i++] = l = j; /* save last length in l */ | 802 | ll[i++] = l = j; /* save last length in l */ |
769 | } else if (j == 16) { /* repeat last length 3 to 6 times */ | 803 | } else if (j == 16) { /* repeat last length 3 to 6 times */ |
770 | while (k_dynamic < 2) { | 804 | while (k_dynamic < 2) { |
771 | b_dynamic |= | 805 | input_char = fgetc(in_file); |
772 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 806 | if (input_char == EOF) return 1; |
807 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
773 | k_dynamic += 8; | 808 | k_dynamic += 8; |
774 | } | 809 | } |
775 | j = 3 + ((unsigned) b_dynamic & 3); | 810 | j = 3 + ((unsigned) b_dynamic & 3); |
@@ -783,8 +818,9 @@ static int inflate_block(int *e) | |||
783 | } | 818 | } |
784 | } else if (j == 17) { /* 3 to 10 zero length codes */ | 819 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
785 | while (k_dynamic < 3) { | 820 | while (k_dynamic < 3) { |
786 | b_dynamic |= | 821 | input_char = fgetc(in_file); |
787 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 822 | if (input_char == EOF) return 1; |
823 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
788 | k_dynamic += 8; | 824 | k_dynamic += 8; |
789 | } | 825 | } |
790 | j = 3 + ((unsigned) b_dynamic & 7); | 826 | j = 3 + ((unsigned) b_dynamic & 7); |
@@ -799,8 +835,9 @@ static int inflate_block(int *e) | |||
799 | l = 0; | 835 | l = 0; |
800 | } else { /* j == 18: 11 to 138 zero length codes */ | 836 | } else { /* j == 18: 11 to 138 zero length codes */ |
801 | while (k_dynamic < 7) { | 837 | while (k_dynamic < 7) { |
802 | b_dynamic |= | 838 | input_char = fgetc(in_file); |
803 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 839 | if (input_char == EOF) return 1; |
840 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
804 | k_dynamic += 8; | 841 | k_dynamic += 8; |
805 | } | 842 | } |
806 | j = 11 + ((unsigned) b_dynamic & 0x7f); | 843 | j = 11 + ((unsigned) b_dynamic & 0x7f); |
@@ -953,7 +990,7 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
953 | } | 990 | } |
954 | 991 | ||
955 | /* Check the compression method */ | 992 | /* Check the compression method */ |
956 | if (fgetc(l_in_file) != 8) { | 993 | if (fgetc(l_in_file) != 8) /* also catches EOF */ { |
957 | error_msg("Unknown compression method"); | 994 | error_msg("Unknown compression method"); |
958 | return (-1); | 995 | return (-1); |
959 | } | 996 | } |
@@ -969,7 +1006,7 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
969 | /* bit 2 set: extra field present */ | 1006 | /* bit 2 set: extra field present */ |
970 | const unsigned short extra = | 1007 | const unsigned short extra = |
971 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); | 1008 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); |
972 | 1009 | if (feof(in_file)) return 1; | |
973 | for (i = 0; i < extra; i++) { | 1010 | for (i = 0; i < extra; i++) { |
974 | fgetc(l_in_file); | 1011 | fgetc(l_in_file); |
975 | } | 1012 | } |
@@ -978,13 +1015,13 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
978 | /* Discard original name if any */ | 1015 | /* Discard original name if any */ |
979 | if (flags & 0x08) { | 1016 | if (flags & 0x08) { |
980 | /* bit 3 set: original file name present */ | 1017 | /* bit 3 set: original file name present */ |
981 | while (fgetc(l_in_file) != 0); /* null */ | 1018 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ |
982 | } | 1019 | } |
983 | 1020 | ||
984 | /* Discard file comment if any */ | 1021 | /* Discard file comment if any */ |
985 | if (flags & 0x10) { | 1022 | if (flags & 0x10) { |
986 | /* bit 4 set: file comment present */ | 1023 | /* bit 4 set: file comment present */ |
987 | while (fgetc(l_in_file) != 0); /* null */ | 1024 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ |
988 | } | 1025 | } |
989 | 1026 | ||
990 | /* Decompress */ | 1027 | /* Decompress */ |
diff --git a/archival/libunarchive/unzip.c b/archival/libunarchive/unzip.c index 0da60c3be..d84067068 100644 --- a/archival/libunarchive/unzip.c +++ b/archival/libunarchive/unzip.c | |||
@@ -390,6 +390,7 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
390 | unsigned ml, md; /* masks for bl and bd bits */ | 390 | unsigned ml, md; /* masks for bl and bd bits */ |
391 | register unsigned long b; /* bit buffer */ | 391 | register unsigned long b; /* bit buffer */ |
392 | register unsigned k; /* number of bits in bit buffer */ | 392 | register unsigned k; /* number of bits in bit buffer */ |
393 | register int input_char; | ||
393 | 394 | ||
394 | /* make local copies of globals */ | 395 | /* make local copies of globals */ |
395 | b = bb; /* initialize bit buffer */ | 396 | b = bb; /* initialize bit buffer */ |
@@ -401,7 +402,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
401 | md = mask_bits[bd]; | 402 | md = mask_bits[bd]; |
402 | for (;;) { /* do until end of block */ | 403 | for (;;) { /* do until end of block */ |
403 | while (k < (unsigned) bl) { | 404 | while (k < (unsigned) bl) { |
404 | b |= ((unsigned long) fgetc(in_file)) << k; | 405 | input_char = fgetc(in_file); |
406 | if (input_char == EOF) return 1; | ||
407 | b |= ((unsigned long)input_char) << k; | ||
405 | k += 8; | 408 | k += 8; |
406 | } | 409 | } |
407 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | 410 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
@@ -413,7 +416,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
413 | k -= t->b; | 416 | k -= t->b; |
414 | e -= 16; | 417 | e -= 16; |
415 | while (k < e) { | 418 | while (k < e) { |
416 | b |= ((unsigned long) fgetc(in_file)) << k; | 419 | input_char = fgetc(in_file); |
420 | if (input_char == EOF) return 1; | ||
421 | b |= ((unsigned long)input_char) << k; | ||
417 | k += 8; | 422 | k += 8; |
418 | } | 423 | } |
419 | } while ((e = | 424 | } while ((e = |
@@ -435,7 +440,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
435 | 440 | ||
436 | /* get length of block to copy */ | 441 | /* get length of block to copy */ |
437 | while (k < e) { | 442 | while (k < e) { |
438 | b |= ((unsigned long) fgetc(in_file)) << k; | 443 | input_char = fgetc(in_file); |
444 | if (input_char == EOF) return 1; | ||
445 | b |= ((unsigned long)input_char) << k; | ||
439 | k += 8; | 446 | k += 8; |
440 | } | 447 | } |
441 | n = t->v.n + ((unsigned) b & mask_bits[e]); | 448 | n = t->v.n + ((unsigned) b & mask_bits[e]); |
@@ -444,7 +451,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
444 | 451 | ||
445 | /* decode distance of block to copy */ | 452 | /* decode distance of block to copy */ |
446 | while (k < (unsigned) bd) { | 453 | while (k < (unsigned) bd) { |
447 | b |= ((unsigned long) fgetc(in_file)) << k; | 454 | input_char = fgetc(in_file); |
455 | if (input_char == EOF) return 1; | ||
456 | b |= ((unsigned long)input_char) << k; | ||
448 | k += 8; | 457 | k += 8; |
449 | } | 458 | } |
450 | 459 | ||
@@ -456,7 +465,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
456 | k -= t->b; | 465 | k -= t->b; |
457 | e -= 16; | 466 | e -= 16; |
458 | while (k < e) { | 467 | while (k < e) { |
459 | b |= ((unsigned long) fgetc(in_file)) << k; | 468 | input_char = fgetc(in_file); |
469 | if (input_char == EOF) return 1; | ||
470 | b |= ((unsigned long)input_char) << k; | ||
460 | k += 8; | 471 | k += 8; |
461 | } | 472 | } |
462 | } while ((e = | 473 | } while ((e = |
@@ -465,7 +476,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
465 | b >>= t->b; | 476 | b >>= t->b; |
466 | k -= t->b; | 477 | k -= t->b; |
467 | while (k < e) { | 478 | while (k < e) { |
468 | b |= ((unsigned long) fgetc(in_file)) << k; | 479 | input_char = fgetc(in_file); |
480 | if (input_char == EOF) return 1; | ||
481 | b |= ((unsigned long)input_char) << k; | ||
469 | k += 8; | 482 | k += 8; |
470 | } | 483 | } |
471 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); | 484 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
@@ -541,6 +554,7 @@ static int inflate_block(int *e) | |||
541 | unsigned t; /* block type */ | 554 | unsigned t; /* block type */ |
542 | register unsigned long b; /* bit buffer */ | 555 | register unsigned long b; /* bit buffer */ |
543 | register unsigned k; /* number of bits in bit buffer */ | 556 | register unsigned k; /* number of bits in bit buffer */ |
557 | int input_char; | ||
544 | 558 | ||
545 | /* make local bit buffer */ | 559 | /* make local bit buffer */ |
546 | b = bb; | 560 | b = bb; |
@@ -548,7 +562,9 @@ static int inflate_block(int *e) | |||
548 | 562 | ||
549 | /* read in last block bit */ | 563 | /* read in last block bit */ |
550 | while (k < 1) { | 564 | while (k < 1) { |
551 | b |= ((unsigned long) fgetc(in_file)) << k; | 565 | input_char = fgetc(in_file); |
566 | if (input_char == EOF) return 1; | ||
567 | b |= ((unsigned long)input_char) << k; | ||
552 | k += 8; | 568 | k += 8; |
553 | } | 569 | } |
554 | *e = (int) b & 1; | 570 | *e = (int) b & 1; |
@@ -557,7 +573,9 @@ static int inflate_block(int *e) | |||
557 | 573 | ||
558 | /* read in block type */ | 574 | /* read in block type */ |
559 | while (k < 2) { | 575 | while (k < 2) { |
560 | b |= ((unsigned long) fgetc(in_file)) << k; | 576 | input_char = fgetc(in_file); |
577 | if (input_char == EOF) return 1; | ||
578 | b |= ((unsigned long)input_char) << k; | ||
561 | k += 8; | 579 | k += 8; |
562 | } | 580 | } |
563 | t = (unsigned) b & 3; | 581 | t = (unsigned) b & 3; |
@@ -589,14 +607,18 @@ static int inflate_block(int *e) | |||
589 | 607 | ||
590 | /* get the length and its complement */ | 608 | /* get the length and its complement */ |
591 | while (k_stored < 16) { | 609 | while (k_stored < 16) { |
592 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 610 | input_char = fgetc(in_file); |
611 | if (input_char == EOF) return 1; | ||
612 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
593 | k_stored += 8; | 613 | k_stored += 8; |
594 | } | 614 | } |
595 | n = ((unsigned) b_stored & 0xffff); | 615 | n = ((unsigned) b_stored & 0xffff); |
596 | b_stored >>= 16; | 616 | b_stored >>= 16; |
597 | k_stored -= 16; | 617 | k_stored -= 16; |
598 | while (k_stored < 16) { | 618 | while (k_stored < 16) { |
599 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 619 | input_char = fgetc(in_file); |
620 | if (input_char == EOF) return 1; | ||
621 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
600 | k_stored += 8; | 622 | k_stored += 8; |
601 | } | 623 | } |
602 | if (n != (unsigned) ((~b_stored) & 0xffff)) { | 624 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
@@ -608,7 +630,9 @@ static int inflate_block(int *e) | |||
608 | /* read and output the compressed data */ | 630 | /* read and output the compressed data */ |
609 | while (n--) { | 631 | while (n--) { |
610 | while (k_stored < 8) { | 632 | while (k_stored < 8) { |
611 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 633 | input_char = fgetc(in_file); |
634 | if (input_char == EOF) return 1; | ||
635 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
612 | k_stored += 8; | 636 | k_stored += 8; |
613 | } | 637 | } |
614 | window[w++] = (unsigned char) b_stored; | 638 | window[w++] = (unsigned char) b_stored; |
@@ -704,21 +728,27 @@ static int inflate_block(int *e) | |||
704 | 728 | ||
705 | /* read in table lengths */ | 729 | /* read in table lengths */ |
706 | while (k_dynamic < 5) { | 730 | while (k_dynamic < 5) { |
707 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 731 | input_char = fgetc(in_file); |
732 | if (input_char == EOF) return 1; | ||
733 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
708 | k_dynamic += 8; | 734 | k_dynamic += 8; |
709 | } | 735 | } |
710 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ | 736 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ |
711 | b_dynamic >>= 5; | 737 | b_dynamic >>= 5; |
712 | k_dynamic -= 5; | 738 | k_dynamic -= 5; |
713 | while (k_dynamic < 5) { | 739 | while (k_dynamic < 5) { |
714 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 740 | input_char = fgetc(in_file); |
741 | if (input_char == EOF) return 1; | ||
742 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
715 | k_dynamic += 8; | 743 | k_dynamic += 8; |
716 | } | 744 | } |
717 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ | 745 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ |
718 | b_dynamic >>= 5; | 746 | b_dynamic >>= 5; |
719 | k_dynamic -= 5; | 747 | k_dynamic -= 5; |
720 | while (k_dynamic < 4) { | 748 | while (k_dynamic < 4) { |
721 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 749 | input_char = fgetc(in_file); |
750 | if (input_char == EOF) return 1; | ||
751 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
722 | k_dynamic += 8; | 752 | k_dynamic += 8; |
723 | } | 753 | } |
724 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ | 754 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ |
@@ -731,7 +761,9 @@ static int inflate_block(int *e) | |||
731 | /* read in bit-length-code lengths */ | 761 | /* read in bit-length-code lengths */ |
732 | for (j = 0; j < nb; j++) { | 762 | for (j = 0; j < nb; j++) { |
733 | while (k_dynamic < 3) { | 763 | while (k_dynamic < 3) { |
734 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 764 | input_char = fgetc(in_file); |
765 | if (input_char == EOF) return 1; | ||
766 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
735 | k_dynamic += 8; | 767 | k_dynamic += 8; |
736 | } | 768 | } |
737 | ll[border[j]] = (unsigned) b_dynamic & 7; | 769 | ll[border[j]] = (unsigned) b_dynamic & 7; |
@@ -757,7 +789,9 @@ static int inflate_block(int *e) | |||
757 | i = l = 0; | 789 | i = l = 0; |
758 | while ((unsigned) i < n) { | 790 | while ((unsigned) i < n) { |
759 | while (k_dynamic < (unsigned) bl) { | 791 | while (k_dynamic < (unsigned) bl) { |
760 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 792 | input_char = fgetc(in_file); |
793 | if (input_char == EOF) return 1; | ||
794 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
761 | k_dynamic += 8; | 795 | k_dynamic += 8; |
762 | } | 796 | } |
763 | j = (td = tl + ((unsigned) b_dynamic & m))->b; | 797 | j = (td = tl + ((unsigned) b_dynamic & m))->b; |
@@ -768,8 +802,9 @@ static int inflate_block(int *e) | |||
768 | ll[i++] = l = j; /* save last length in l */ | 802 | ll[i++] = l = j; /* save last length in l */ |
769 | } else if (j == 16) { /* repeat last length 3 to 6 times */ | 803 | } else if (j == 16) { /* repeat last length 3 to 6 times */ |
770 | while (k_dynamic < 2) { | 804 | while (k_dynamic < 2) { |
771 | b_dynamic |= | 805 | input_char = fgetc(in_file); |
772 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 806 | if (input_char == EOF) return 1; |
807 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
773 | k_dynamic += 8; | 808 | k_dynamic += 8; |
774 | } | 809 | } |
775 | j = 3 + ((unsigned) b_dynamic & 3); | 810 | j = 3 + ((unsigned) b_dynamic & 3); |
@@ -783,8 +818,9 @@ static int inflate_block(int *e) | |||
783 | } | 818 | } |
784 | } else if (j == 17) { /* 3 to 10 zero length codes */ | 819 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
785 | while (k_dynamic < 3) { | 820 | while (k_dynamic < 3) { |
786 | b_dynamic |= | 821 | input_char = fgetc(in_file); |
787 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 822 | if (input_char == EOF) return 1; |
823 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
788 | k_dynamic += 8; | 824 | k_dynamic += 8; |
789 | } | 825 | } |
790 | j = 3 + ((unsigned) b_dynamic & 7); | 826 | j = 3 + ((unsigned) b_dynamic & 7); |
@@ -799,8 +835,9 @@ static int inflate_block(int *e) | |||
799 | l = 0; | 835 | l = 0; |
800 | } else { /* j == 18: 11 to 138 zero length codes */ | 836 | } else { /* j == 18: 11 to 138 zero length codes */ |
801 | while (k_dynamic < 7) { | 837 | while (k_dynamic < 7) { |
802 | b_dynamic |= | 838 | input_char = fgetc(in_file); |
803 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 839 | if (input_char == EOF) return 1; |
840 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
804 | k_dynamic += 8; | 841 | k_dynamic += 8; |
805 | } | 842 | } |
806 | j = 11 + ((unsigned) b_dynamic & 0x7f); | 843 | j = 11 + ((unsigned) b_dynamic & 0x7f); |
@@ -953,7 +990,7 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
953 | } | 990 | } |
954 | 991 | ||
955 | /* Check the compression method */ | 992 | /* Check the compression method */ |
956 | if (fgetc(l_in_file) != 8) { | 993 | if (fgetc(l_in_file) != 8) /* also catches EOF */ { |
957 | error_msg("Unknown compression method"); | 994 | error_msg("Unknown compression method"); |
958 | return (-1); | 995 | return (-1); |
959 | } | 996 | } |
@@ -969,7 +1006,7 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
969 | /* bit 2 set: extra field present */ | 1006 | /* bit 2 set: extra field present */ |
970 | const unsigned short extra = | 1007 | const unsigned short extra = |
971 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); | 1008 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); |
972 | 1009 | if (feof(in_file)) return 1; | |
973 | for (i = 0; i < extra; i++) { | 1010 | for (i = 0; i < extra; i++) { |
974 | fgetc(l_in_file); | 1011 | fgetc(l_in_file); |
975 | } | 1012 | } |
@@ -978,13 +1015,13 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
978 | /* Discard original name if any */ | 1015 | /* Discard original name if any */ |
979 | if (flags & 0x08) { | 1016 | if (flags & 0x08) { |
980 | /* bit 3 set: original file name present */ | 1017 | /* bit 3 set: original file name present */ |
981 | while (fgetc(l_in_file) != 0); /* null */ | 1018 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ |
982 | } | 1019 | } |
983 | 1020 | ||
984 | /* Discard file comment if any */ | 1021 | /* Discard file comment if any */ |
985 | if (flags & 0x10) { | 1022 | if (flags & 0x10) { |
986 | /* bit 4 set: file comment present */ | 1023 | /* bit 4 set: file comment present */ |
987 | while (fgetc(l_in_file) != 0); /* null */ | 1024 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ |
988 | } | 1025 | } |
989 | 1026 | ||
990 | /* Decompress */ | 1027 | /* Decompress */ |
diff --git a/libbb/unzip.c b/libbb/unzip.c index 0da60c3be..d84067068 100644 --- a/libbb/unzip.c +++ b/libbb/unzip.c | |||
@@ -390,6 +390,7 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
390 | unsigned ml, md; /* masks for bl and bd bits */ | 390 | unsigned ml, md; /* masks for bl and bd bits */ |
391 | register unsigned long b; /* bit buffer */ | 391 | register unsigned long b; /* bit buffer */ |
392 | register unsigned k; /* number of bits in bit buffer */ | 392 | register unsigned k; /* number of bits in bit buffer */ |
393 | register int input_char; | ||
393 | 394 | ||
394 | /* make local copies of globals */ | 395 | /* make local copies of globals */ |
395 | b = bb; /* initialize bit buffer */ | 396 | b = bb; /* initialize bit buffer */ |
@@ -401,7 +402,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
401 | md = mask_bits[bd]; | 402 | md = mask_bits[bd]; |
402 | for (;;) { /* do until end of block */ | 403 | for (;;) { /* do until end of block */ |
403 | while (k < (unsigned) bl) { | 404 | while (k < (unsigned) bl) { |
404 | b |= ((unsigned long) fgetc(in_file)) << k; | 405 | input_char = fgetc(in_file); |
406 | if (input_char == EOF) return 1; | ||
407 | b |= ((unsigned long)input_char) << k; | ||
405 | k += 8; | 408 | k += 8; |
406 | } | 409 | } |
407 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | 410 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
@@ -413,7 +416,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
413 | k -= t->b; | 416 | k -= t->b; |
414 | e -= 16; | 417 | e -= 16; |
415 | while (k < e) { | 418 | while (k < e) { |
416 | b |= ((unsigned long) fgetc(in_file)) << k; | 419 | input_char = fgetc(in_file); |
420 | if (input_char == EOF) return 1; | ||
421 | b |= ((unsigned long)input_char) << k; | ||
417 | k += 8; | 422 | k += 8; |
418 | } | 423 | } |
419 | } while ((e = | 424 | } while ((e = |
@@ -435,7 +440,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
435 | 440 | ||
436 | /* get length of block to copy */ | 441 | /* get length of block to copy */ |
437 | while (k < e) { | 442 | while (k < e) { |
438 | b |= ((unsigned long) fgetc(in_file)) << k; | 443 | input_char = fgetc(in_file); |
444 | if (input_char == EOF) return 1; | ||
445 | b |= ((unsigned long)input_char) << k; | ||
439 | k += 8; | 446 | k += 8; |
440 | } | 447 | } |
441 | n = t->v.n + ((unsigned) b & mask_bits[e]); | 448 | n = t->v.n + ((unsigned) b & mask_bits[e]); |
@@ -444,7 +451,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
444 | 451 | ||
445 | /* decode distance of block to copy */ | 452 | /* decode distance of block to copy */ |
446 | while (k < (unsigned) bd) { | 453 | while (k < (unsigned) bd) { |
447 | b |= ((unsigned long) fgetc(in_file)) << k; | 454 | input_char = fgetc(in_file); |
455 | if (input_char == EOF) return 1; | ||
456 | b |= ((unsigned long)input_char) << k; | ||
448 | k += 8; | 457 | k += 8; |
449 | } | 458 | } |
450 | 459 | ||
@@ -456,7 +465,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
456 | k -= t->b; | 465 | k -= t->b; |
457 | e -= 16; | 466 | e -= 16; |
458 | while (k < e) { | 467 | while (k < e) { |
459 | b |= ((unsigned long) fgetc(in_file)) << k; | 468 | input_char = fgetc(in_file); |
469 | if (input_char == EOF) return 1; | ||
470 | b |= ((unsigned long)input_char) << k; | ||
460 | k += 8; | 471 | k += 8; |
461 | } | 472 | } |
462 | } while ((e = | 473 | } while ((e = |
@@ -465,7 +476,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
465 | b >>= t->b; | 476 | b >>= t->b; |
466 | k -= t->b; | 477 | k -= t->b; |
467 | while (k < e) { | 478 | while (k < e) { |
468 | b |= ((unsigned long) fgetc(in_file)) << k; | 479 | input_char = fgetc(in_file); |
480 | if (input_char == EOF) return 1; | ||
481 | b |= ((unsigned long)input_char) << k; | ||
469 | k += 8; | 482 | k += 8; |
470 | } | 483 | } |
471 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); | 484 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
@@ -541,6 +554,7 @@ static int inflate_block(int *e) | |||
541 | unsigned t; /* block type */ | 554 | unsigned t; /* block type */ |
542 | register unsigned long b; /* bit buffer */ | 555 | register unsigned long b; /* bit buffer */ |
543 | register unsigned k; /* number of bits in bit buffer */ | 556 | register unsigned k; /* number of bits in bit buffer */ |
557 | int input_char; | ||
544 | 558 | ||
545 | /* make local bit buffer */ | 559 | /* make local bit buffer */ |
546 | b = bb; | 560 | b = bb; |
@@ -548,7 +562,9 @@ static int inflate_block(int *e) | |||
548 | 562 | ||
549 | /* read in last block bit */ | 563 | /* read in last block bit */ |
550 | while (k < 1) { | 564 | while (k < 1) { |
551 | b |= ((unsigned long) fgetc(in_file)) << k; | 565 | input_char = fgetc(in_file); |
566 | if (input_char == EOF) return 1; | ||
567 | b |= ((unsigned long)input_char) << k; | ||
552 | k += 8; | 568 | k += 8; |
553 | } | 569 | } |
554 | *e = (int) b & 1; | 570 | *e = (int) b & 1; |
@@ -557,7 +573,9 @@ static int inflate_block(int *e) | |||
557 | 573 | ||
558 | /* read in block type */ | 574 | /* read in block type */ |
559 | while (k < 2) { | 575 | while (k < 2) { |
560 | b |= ((unsigned long) fgetc(in_file)) << k; | 576 | input_char = fgetc(in_file); |
577 | if (input_char == EOF) return 1; | ||
578 | b |= ((unsigned long)input_char) << k; | ||
561 | k += 8; | 579 | k += 8; |
562 | } | 580 | } |
563 | t = (unsigned) b & 3; | 581 | t = (unsigned) b & 3; |
@@ -589,14 +607,18 @@ static int inflate_block(int *e) | |||
589 | 607 | ||
590 | /* get the length and its complement */ | 608 | /* get the length and its complement */ |
591 | while (k_stored < 16) { | 609 | while (k_stored < 16) { |
592 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 610 | input_char = fgetc(in_file); |
611 | if (input_char == EOF) return 1; | ||
612 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
593 | k_stored += 8; | 613 | k_stored += 8; |
594 | } | 614 | } |
595 | n = ((unsigned) b_stored & 0xffff); | 615 | n = ((unsigned) b_stored & 0xffff); |
596 | b_stored >>= 16; | 616 | b_stored >>= 16; |
597 | k_stored -= 16; | 617 | k_stored -= 16; |
598 | while (k_stored < 16) { | 618 | while (k_stored < 16) { |
599 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 619 | input_char = fgetc(in_file); |
620 | if (input_char == EOF) return 1; | ||
621 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
600 | k_stored += 8; | 622 | k_stored += 8; |
601 | } | 623 | } |
602 | if (n != (unsigned) ((~b_stored) & 0xffff)) { | 624 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
@@ -608,7 +630,9 @@ static int inflate_block(int *e) | |||
608 | /* read and output the compressed data */ | 630 | /* read and output the compressed data */ |
609 | while (n--) { | 631 | while (n--) { |
610 | while (k_stored < 8) { | 632 | while (k_stored < 8) { |
611 | b_stored |= ((unsigned long) fgetc(in_file)) << k_stored; | 633 | input_char = fgetc(in_file); |
634 | if (input_char == EOF) return 1; | ||
635 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
612 | k_stored += 8; | 636 | k_stored += 8; |
613 | } | 637 | } |
614 | window[w++] = (unsigned char) b_stored; | 638 | window[w++] = (unsigned char) b_stored; |
@@ -704,21 +728,27 @@ static int inflate_block(int *e) | |||
704 | 728 | ||
705 | /* read in table lengths */ | 729 | /* read in table lengths */ |
706 | while (k_dynamic < 5) { | 730 | while (k_dynamic < 5) { |
707 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 731 | input_char = fgetc(in_file); |
732 | if (input_char == EOF) return 1; | ||
733 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
708 | k_dynamic += 8; | 734 | k_dynamic += 8; |
709 | } | 735 | } |
710 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ | 736 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ |
711 | b_dynamic >>= 5; | 737 | b_dynamic >>= 5; |
712 | k_dynamic -= 5; | 738 | k_dynamic -= 5; |
713 | while (k_dynamic < 5) { | 739 | while (k_dynamic < 5) { |
714 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 740 | input_char = fgetc(in_file); |
741 | if (input_char == EOF) return 1; | ||
742 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
715 | k_dynamic += 8; | 743 | k_dynamic += 8; |
716 | } | 744 | } |
717 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ | 745 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ |
718 | b_dynamic >>= 5; | 746 | b_dynamic >>= 5; |
719 | k_dynamic -= 5; | 747 | k_dynamic -= 5; |
720 | while (k_dynamic < 4) { | 748 | while (k_dynamic < 4) { |
721 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 749 | input_char = fgetc(in_file); |
750 | if (input_char == EOF) return 1; | ||
751 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
722 | k_dynamic += 8; | 752 | k_dynamic += 8; |
723 | } | 753 | } |
724 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ | 754 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ |
@@ -731,7 +761,9 @@ static int inflate_block(int *e) | |||
731 | /* read in bit-length-code lengths */ | 761 | /* read in bit-length-code lengths */ |
732 | for (j = 0; j < nb; j++) { | 762 | for (j = 0; j < nb; j++) { |
733 | while (k_dynamic < 3) { | 763 | while (k_dynamic < 3) { |
734 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 764 | input_char = fgetc(in_file); |
765 | if (input_char == EOF) return 1; | ||
766 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
735 | k_dynamic += 8; | 767 | k_dynamic += 8; |
736 | } | 768 | } |
737 | ll[border[j]] = (unsigned) b_dynamic & 7; | 769 | ll[border[j]] = (unsigned) b_dynamic & 7; |
@@ -757,7 +789,9 @@ static int inflate_block(int *e) | |||
757 | i = l = 0; | 789 | i = l = 0; |
758 | while ((unsigned) i < n) { | 790 | while ((unsigned) i < n) { |
759 | while (k_dynamic < (unsigned) bl) { | 791 | while (k_dynamic < (unsigned) bl) { |
760 | b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic; | 792 | input_char = fgetc(in_file); |
793 | if (input_char == EOF) return 1; | ||
794 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
761 | k_dynamic += 8; | 795 | k_dynamic += 8; |
762 | } | 796 | } |
763 | j = (td = tl + ((unsigned) b_dynamic & m))->b; | 797 | j = (td = tl + ((unsigned) b_dynamic & m))->b; |
@@ -768,8 +802,9 @@ static int inflate_block(int *e) | |||
768 | ll[i++] = l = j; /* save last length in l */ | 802 | ll[i++] = l = j; /* save last length in l */ |
769 | } else if (j == 16) { /* repeat last length 3 to 6 times */ | 803 | } else if (j == 16) { /* repeat last length 3 to 6 times */ |
770 | while (k_dynamic < 2) { | 804 | while (k_dynamic < 2) { |
771 | b_dynamic |= | 805 | input_char = fgetc(in_file); |
772 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 806 | if (input_char == EOF) return 1; |
807 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
773 | k_dynamic += 8; | 808 | k_dynamic += 8; |
774 | } | 809 | } |
775 | j = 3 + ((unsigned) b_dynamic & 3); | 810 | j = 3 + ((unsigned) b_dynamic & 3); |
@@ -783,8 +818,9 @@ static int inflate_block(int *e) | |||
783 | } | 818 | } |
784 | } else if (j == 17) { /* 3 to 10 zero length codes */ | 819 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
785 | while (k_dynamic < 3) { | 820 | while (k_dynamic < 3) { |
786 | b_dynamic |= | 821 | input_char = fgetc(in_file); |
787 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 822 | if (input_char == EOF) return 1; |
823 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
788 | k_dynamic += 8; | 824 | k_dynamic += 8; |
789 | } | 825 | } |
790 | j = 3 + ((unsigned) b_dynamic & 7); | 826 | j = 3 + ((unsigned) b_dynamic & 7); |
@@ -799,8 +835,9 @@ static int inflate_block(int *e) | |||
799 | l = 0; | 835 | l = 0; |
800 | } else { /* j == 18: 11 to 138 zero length codes */ | 836 | } else { /* j == 18: 11 to 138 zero length codes */ |
801 | while (k_dynamic < 7) { | 837 | while (k_dynamic < 7) { |
802 | b_dynamic |= | 838 | input_char = fgetc(in_file); |
803 | ((unsigned long) fgetc(in_file)) << k_dynamic; | 839 | if (input_char == EOF) return 1; |
840 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
804 | k_dynamic += 8; | 841 | k_dynamic += 8; |
805 | } | 842 | } |
806 | j = 11 + ((unsigned) b_dynamic & 0x7f); | 843 | j = 11 + ((unsigned) b_dynamic & 0x7f); |
@@ -953,7 +990,7 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
953 | } | 990 | } |
954 | 991 | ||
955 | /* Check the compression method */ | 992 | /* Check the compression method */ |
956 | if (fgetc(l_in_file) != 8) { | 993 | if (fgetc(l_in_file) != 8) /* also catches EOF */ { |
957 | error_msg("Unknown compression method"); | 994 | error_msg("Unknown compression method"); |
958 | return (-1); | 995 | return (-1); |
959 | } | 996 | } |
@@ -969,7 +1006,7 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
969 | /* bit 2 set: extra field present */ | 1006 | /* bit 2 set: extra field present */ |
970 | const unsigned short extra = | 1007 | const unsigned short extra = |
971 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); | 1008 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); |
972 | 1009 | if (feof(in_file)) return 1; | |
973 | for (i = 0; i < extra; i++) { | 1010 | for (i = 0; i < extra; i++) { |
974 | fgetc(l_in_file); | 1011 | fgetc(l_in_file); |
975 | } | 1012 | } |
@@ -978,13 +1015,13 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
978 | /* Discard original name if any */ | 1015 | /* Discard original name if any */ |
979 | if (flags & 0x08) { | 1016 | if (flags & 0x08) { |
980 | /* bit 3 set: original file name present */ | 1017 | /* bit 3 set: original file name present */ |
981 | while (fgetc(l_in_file) != 0); /* null */ | 1018 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ |
982 | } | 1019 | } |
983 | 1020 | ||
984 | /* Discard file comment if any */ | 1021 | /* Discard file comment if any */ |
985 | if (flags & 0x10) { | 1022 | if (flags & 0x10) { |
986 | /* bit 4 set: file comment present */ | 1023 | /* bit 4 set: file comment present */ |
987 | while (fgetc(l_in_file) != 0); /* null */ | 1024 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ |
988 | } | 1025 | } |
989 | 1026 | ||
990 | /* Decompress */ | 1027 | /* Decompress */ |