diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-20 06:46:46 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-20 06:46:46 +0000 |
commit | 0126fda5bbb01fef3e6de07c8f4619863d606f50 (patch) | |
tree | cf6f63fea2e2610023f0fa2b4c2deac8503b0d98 | |
parent | efe78890863d44334e72c4bc23b5250f281d24ce (diff) | |
download | busybox-w32-0126fda5bbb01fef3e6de07c8f4619863d606f50.tar.gz busybox-w32-0126fda5bbb01fef3e6de07c8f4619863d606f50.tar.bz2 busybox-w32-0126fda5bbb01fef3e6de07c8f4619863d606f50.zip |
read_gz patch 3 from Laurence Anderson
-rw-r--r-- | archival/libunarchive/decompress_unzip.c | 166 | ||||
-rw-r--r-- | archival/libunarchive/unzip.c | 166 |
2 files changed, 188 insertions, 144 deletions
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index 046f4dd72..7ebbd0ee7 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c | |||
@@ -155,12 +155,6 @@ static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current | |||
155 | return(bitbuffer); | 155 | return(bitbuffer); |
156 | } | 156 | } |
157 | 157 | ||
158 | static void abort_gzip(void) | ||
159 | { | ||
160 | error_msg("gzip aborted\n"); | ||
161 | exit(-1); | ||
162 | } | ||
163 | |||
164 | static void make_gunzip_crc_table(void) | 158 | static void make_gunzip_crc_table(void) |
165 | { | 159 | { |
166 | const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */ | 160 | const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */ |
@@ -438,24 +432,37 @@ static void flush_gunzip_window(void) | |||
438 | * tl, td: literal/length and distance decoder tables | 432 | * tl, td: literal/length and distance decoder tables |
439 | * bl, bd: number of bits decoded by tl[] and td[] | 433 | * bl, bd: number of bits decoded by tl[] and td[] |
440 | */ | 434 | */ |
441 | static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const unsigned int bd) | 435 | static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup) |
442 | { | 436 | { |
443 | unsigned int e; /* table entry flag/number of extra bits */ | 437 | static unsigned int e; /* table entry flag/number of extra bits */ |
444 | unsigned int n, d; /* length and index for copy */ | 438 | static unsigned int n, d; /* length and index for copy */ |
445 | unsigned int w; /* current gunzip_window position */ | 439 | static unsigned int w; /* current gunzip_window position */ |
446 | huft_t *t; /* pointer to table entry */ | 440 | static huft_t *t; /* pointer to table entry */ |
447 | unsigned int ml, md; /* masks for bl and bd bits */ | 441 | static unsigned int ml, md; /* masks for bl and bd bits */ |
448 | unsigned int b; /* bit buffer */ | 442 | static unsigned int b; /* bit buffer */ |
449 | unsigned int k; /* number of bits in bit buffer */ | 443 | static unsigned int k; /* number of bits in bit buffer */ |
450 | 444 | static huft_t *tl, *td; | |
451 | /* make local copies of globals */ | 445 | static unsigned int bl, bd; |
452 | b = gunzip_bb; /* initialize bit buffer */ | 446 | static int resumeCopy = 0; |
453 | k = gunzip_bk; | 447 | |
454 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | 448 | if (setup) { // 1st time we are called, copy in variables |
455 | 449 | tl = my_tl; | |
456 | /* inflate the coded data */ | 450 | td = my_td; |
457 | ml = mask_bits[bl]; /* precompute masks for speed */ | 451 | bl = my_bl; |
458 | md = mask_bits[bd]; | 452 | bd = my_bd; |
453 | /* make local copies of globals */ | ||
454 | b = gunzip_bb; /* initialize bit buffer */ | ||
455 | k = gunzip_bk; | ||
456 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
457 | |||
458 | /* inflate the coded data */ | ||
459 | ml = mask_bits[bl]; /* precompute masks for speed */ | ||
460 | md = mask_bits[bd]; | ||
461 | return 0; // Don't actually do anything the first time | ||
462 | } | ||
463 | |||
464 | if (resumeCopy) goto do_copy; | ||
465 | |||
459 | while (1) { /* do until end of block */ | 466 | while (1) { /* do until end of block */ |
460 | b = fill_bitbuffer(b, &k, bl); | 467 | b = fill_bitbuffer(b, &k, bl); |
461 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | 468 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
@@ -475,8 +482,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
475 | gunzip_window[w++] = (unsigned char) t->v.n; | 482 | gunzip_window[w++] = (unsigned char) t->v.n; |
476 | if (w == gunzip_wsize) { | 483 | if (w == gunzip_wsize) { |
477 | gunzip_outbuf_count = (w); | 484 | gunzip_outbuf_count = (w); |
478 | flush_gunzip_window(); | 485 | //flush_gunzip_window(); |
479 | w = 0; | 486 | w = 0; |
487 | return -1; | ||
480 | } | 488 | } |
481 | } else { /* it's an EOB or a length */ | 489 | } else { /* it's an EOB or a length */ |
482 | 490 | ||
@@ -512,7 +520,7 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
512 | k -= e; | 520 | k -= e; |
513 | 521 | ||
514 | /* do the copy */ | 522 | /* do the copy */ |
515 | do { | 523 | do_copy: do { |
516 | n -= (e = | 524 | n -= (e = |
517 | (e = | 525 | (e = |
518 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); | 526 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); |
@@ -530,11 +538,14 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
530 | } | 538 | } |
531 | if (w == gunzip_wsize) { | 539 | if (w == gunzip_wsize) { |
532 | gunzip_outbuf_count = (w); | 540 | gunzip_outbuf_count = (w); |
533 | flush_gunzip_window(); | 541 | if (n) resumeCopy = 1; |
542 | else resumeCopy = 0; | ||
543 | //flush_gunzip_window(); | ||
534 | w = 0; | 544 | w = 0; |
545 | return -1; | ||
535 | } | 546 | } |
536 | |||
537 | } while (n); | 547 | } while (n); |
548 | resumeCopy = 0; | ||
538 | } | 549 | } |
539 | } | 550 | } |
540 | 551 | ||
@@ -543,16 +554,56 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
543 | gunzip_bb = b; /* restore global bit buffer */ | 554 | gunzip_bb = b; /* restore global bit buffer */ |
544 | gunzip_bk = k; | 555 | gunzip_bk = k; |
545 | 556 | ||
557 | /* normally just after call to inflate_codes, but save code by putting it here */ | ||
558 | /* free the decoding tables, return */ | ||
559 | huft_free(tl); | ||
560 | huft_free(td); | ||
561 | |||
546 | /* done */ | 562 | /* done */ |
547 | return 0; | 563 | return 0; |
548 | } | 564 | } |
549 | 565 | ||
566 | int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) | ||
567 | { | ||
568 | static int n, b_stored, k_stored, w; | ||
569 | if (setup) { | ||
570 | n = my_n; | ||
571 | b_stored = my_b_stored; | ||
572 | k_stored = my_k_stored; | ||
573 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
574 | return 0; // Don't do anything first time | ||
575 | } | ||
576 | |||
577 | /* read and output the compressed data */ | ||
578 | while (n--) { | ||
579 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); | ||
580 | gunzip_window[w++] = (unsigned char) b_stored; | ||
581 | if (w == (unsigned int) gunzip_wsize) { | ||
582 | gunzip_outbuf_count = (w); | ||
583 | //flush_gunzip_window(); | ||
584 | w = 0; | ||
585 | b_stored >>= 8; | ||
586 | k_stored -= 8; | ||
587 | return -1; // Means more stuff 2do | ||
588 | } | ||
589 | b_stored >>= 8; | ||
590 | k_stored -= 8; | ||
591 | } | ||
592 | |||
593 | /* restore the globals from the locals */ | ||
594 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ | ||
595 | gunzip_bb = b_stored; /* restore global bit buffer */ | ||
596 | gunzip_bk = k_stored; | ||
597 | return 0; // Finished | ||
598 | } | ||
599 | |||
550 | /* | 600 | /* |
551 | * decompress an inflated block | 601 | * decompress an inflated block |
552 | * e: last block flag | 602 | * e: last block flag |
553 | * | 603 | * |
554 | * GLOBAL VARIABLES: bb, kk, | 604 | * GLOBAL VARIABLES: bb, kk, |
555 | */ | 605 | */ |
606 | // Return values: -1 = inflate_stored, -2 = inflate_codes | ||
556 | static int inflate_block(int *e) | 607 | static int inflate_block(int *e) |
557 | { | 608 | { |
558 | unsigned t; /* block type */ | 609 | unsigned t; /* block type */ |
@@ -585,14 +636,12 @@ static int inflate_block(int *e) | |||
585 | case 0: /* Inflate stored */ | 636 | case 0: /* Inflate stored */ |
586 | { | 637 | { |
587 | unsigned int n; /* number of bytes in block */ | 638 | unsigned int n; /* number of bytes in block */ |
588 | unsigned int w; /* current gunzip_window position */ | ||
589 | unsigned int b_stored; /* bit buffer */ | 639 | unsigned int b_stored; /* bit buffer */ |
590 | unsigned int k_stored; /* number of bits in bit buffer */ | 640 | unsigned int k_stored; /* number of bits in bit buffer */ |
591 | 641 | ||
592 | /* make local copies of globals */ | 642 | /* make local copies of globals */ |
593 | b_stored = gunzip_bb; /* initialize bit buffer */ | 643 | b_stored = gunzip_bb; /* initialize bit buffer */ |
594 | k_stored = gunzip_bk; | 644 | k_stored = gunzip_bk; |
595 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
596 | 645 | ||
597 | /* go to byte boundary */ | 646 | /* go to byte boundary */ |
598 | n = k_stored & 7; | 647 | n = k_stored & 7; |
@@ -612,24 +661,8 @@ static int inflate_block(int *e) | |||
612 | b_stored >>= 16; | 661 | b_stored >>= 16; |
613 | k_stored -= 16; | 662 | k_stored -= 16; |
614 | 663 | ||
615 | /* read and output the compressed data */ | 664 | inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored |
616 | while (n--) { | 665 | return -1; |
617 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); | ||
618 | gunzip_window[w++] = (unsigned char) b_stored; | ||
619 | if (w == (unsigned int) gunzip_wsize) { | ||
620 | gunzip_outbuf_count = (w); | ||
621 | flush_gunzip_window(); | ||
622 | w = 0; | ||
623 | } | ||
624 | b_stored >>= 8; | ||
625 | k_stored -= 8; | ||
626 | } | ||
627 | |||
628 | /* restore the globals from the locals */ | ||
629 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ | ||
630 | gunzip_bb = b_stored; /* restore global bit buffer */ | ||
631 | gunzip_bk = k_stored; | ||
632 | return 0; | ||
633 | } | 666 | } |
634 | case 1: /* Inflate fixed | 667 | case 1: /* Inflate fixed |
635 | * decompress an inflated type 1 (fixed Huffman codes) block. We should | 668 | * decompress an inflated type 1 (fixed Huffman codes) block. We should |
@@ -673,14 +706,11 @@ static int inflate_block(int *e) | |||
673 | } | 706 | } |
674 | 707 | ||
675 | /* decompress until an end-of-block code */ | 708 | /* decompress until an end-of-block code */ |
676 | if (inflate_codes(tl, td, bl, bd)) { | 709 | inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes |
677 | return 1; | 710 | |
678 | } | 711 | /* huft_free code moved into inflate_codes */ |
679 | 712 | ||
680 | /* free the decoding tables, return */ | 713 | return -2; |
681 | huft_free(tl); | ||
682 | huft_free(td); | ||
683 | return 0; | ||
684 | } | 714 | } |
685 | case 2: /* Inflate dynamic */ | 715 | case 2: /* Inflate dynamic */ |
686 | { | 716 | { |
@@ -828,14 +858,11 @@ static int inflate_block(int *e) | |||
828 | } | 858 | } |
829 | 859 | ||
830 | /* decompress until an end-of-block code */ | 860 | /* decompress until an end-of-block code */ |
831 | if (inflate_codes(tl, td, bl, bd)) { | 861 | inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes |
832 | return 1; | ||
833 | } | ||
834 | 862 | ||
835 | /* free the decoding tables, return */ | 863 | /* huft_free code moved into inflate_codes */ |
836 | huft_free(tl); | 864 | |
837 | huft_free(td); | 865 | return -2; |
838 | return 0; | ||
839 | } | 866 | } |
840 | default: | 867 | default: |
841 | /* bad block type */ | 868 | /* bad block type */ |
@@ -862,7 +889,11 @@ extern ssize_t read_gz(int fd, void *buf, size_t count) | |||
862 | if (e) return 0; // No more data here! | 889 | if (e) return 0; // No more data here! |
863 | gunzip_hufts = 0; | 890 | gunzip_hufts = 0; |
864 | r = inflate_block(&e); | 891 | r = inflate_block(&e); |
865 | if (r != 0) { | 892 | if (r == -1) { // Call inflate_stored while returning -1 |
893 | while(inflate_stored(0,0,0,0) == -1) flush_gunzip_window(); | ||
894 | } else if (r == -2) { // Call inflate_codes while returning -1 | ||
895 | while(inflate_codes(0,0,0,0,0) == -1) flush_gunzip_window(); | ||
896 | } else { | ||
866 | error_msg_and_die("inflate error %d", r); | 897 | error_msg_and_die("inflate error %d", r); |
867 | return -1; | 898 | return -1; |
868 | } | 899 | } |
@@ -895,15 +926,6 @@ extern void GZ_gzReadOpen(int fd, void *unused, int nUnused) | |||
895 | 926 | ||
896 | gunzip_in_buffer = malloc(8); | 927 | gunzip_in_buffer = malloc(8); |
897 | 928 | ||
898 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) { | ||
899 | (void) signal(SIGINT, (sig_type) abort_gzip); | ||
900 | } | ||
901 | #ifdef SIGHUP | ||
902 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { | ||
903 | (void) signal(SIGHUP, (sig_type) abort_gzip); | ||
904 | } | ||
905 | #endif | ||
906 | |||
907 | /* initialize gunzip_window, bit buffer */ | 929 | /* initialize gunzip_window, bit buffer */ |
908 | gunzip_bk = 0; | 930 | gunzip_bk = 0; |
909 | gunzip_bb = 0; | 931 | gunzip_bb = 0; |
diff --git a/archival/libunarchive/unzip.c b/archival/libunarchive/unzip.c index 046f4dd72..7ebbd0ee7 100644 --- a/archival/libunarchive/unzip.c +++ b/archival/libunarchive/unzip.c | |||
@@ -155,12 +155,6 @@ static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current | |||
155 | return(bitbuffer); | 155 | return(bitbuffer); |
156 | } | 156 | } |
157 | 157 | ||
158 | static void abort_gzip(void) | ||
159 | { | ||
160 | error_msg("gzip aborted\n"); | ||
161 | exit(-1); | ||
162 | } | ||
163 | |||
164 | static void make_gunzip_crc_table(void) | 158 | static void make_gunzip_crc_table(void) |
165 | { | 159 | { |
166 | const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */ | 160 | const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */ |
@@ -438,24 +432,37 @@ static void flush_gunzip_window(void) | |||
438 | * tl, td: literal/length and distance decoder tables | 432 | * tl, td: literal/length and distance decoder tables |
439 | * bl, bd: number of bits decoded by tl[] and td[] | 433 | * bl, bd: number of bits decoded by tl[] and td[] |
440 | */ | 434 | */ |
441 | static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const unsigned int bd) | 435 | static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup) |
442 | { | 436 | { |
443 | unsigned int e; /* table entry flag/number of extra bits */ | 437 | static unsigned int e; /* table entry flag/number of extra bits */ |
444 | unsigned int n, d; /* length and index for copy */ | 438 | static unsigned int n, d; /* length and index for copy */ |
445 | unsigned int w; /* current gunzip_window position */ | 439 | static unsigned int w; /* current gunzip_window position */ |
446 | huft_t *t; /* pointer to table entry */ | 440 | static huft_t *t; /* pointer to table entry */ |
447 | unsigned int ml, md; /* masks for bl and bd bits */ | 441 | static unsigned int ml, md; /* masks for bl and bd bits */ |
448 | unsigned int b; /* bit buffer */ | 442 | static unsigned int b; /* bit buffer */ |
449 | unsigned int k; /* number of bits in bit buffer */ | 443 | static unsigned int k; /* number of bits in bit buffer */ |
450 | 444 | static huft_t *tl, *td; | |
451 | /* make local copies of globals */ | 445 | static unsigned int bl, bd; |
452 | b = gunzip_bb; /* initialize bit buffer */ | 446 | static int resumeCopy = 0; |
453 | k = gunzip_bk; | 447 | |
454 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | 448 | if (setup) { // 1st time we are called, copy in variables |
455 | 449 | tl = my_tl; | |
456 | /* inflate the coded data */ | 450 | td = my_td; |
457 | ml = mask_bits[bl]; /* precompute masks for speed */ | 451 | bl = my_bl; |
458 | md = mask_bits[bd]; | 452 | bd = my_bd; |
453 | /* make local copies of globals */ | ||
454 | b = gunzip_bb; /* initialize bit buffer */ | ||
455 | k = gunzip_bk; | ||
456 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
457 | |||
458 | /* inflate the coded data */ | ||
459 | ml = mask_bits[bl]; /* precompute masks for speed */ | ||
460 | md = mask_bits[bd]; | ||
461 | return 0; // Don't actually do anything the first time | ||
462 | } | ||
463 | |||
464 | if (resumeCopy) goto do_copy; | ||
465 | |||
459 | while (1) { /* do until end of block */ | 466 | while (1) { /* do until end of block */ |
460 | b = fill_bitbuffer(b, &k, bl); | 467 | b = fill_bitbuffer(b, &k, bl); |
461 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | 468 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
@@ -475,8 +482,9 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
475 | gunzip_window[w++] = (unsigned char) t->v.n; | 482 | gunzip_window[w++] = (unsigned char) t->v.n; |
476 | if (w == gunzip_wsize) { | 483 | if (w == gunzip_wsize) { |
477 | gunzip_outbuf_count = (w); | 484 | gunzip_outbuf_count = (w); |
478 | flush_gunzip_window(); | 485 | //flush_gunzip_window(); |
479 | w = 0; | 486 | w = 0; |
487 | return -1; | ||
480 | } | 488 | } |
481 | } else { /* it's an EOB or a length */ | 489 | } else { /* it's an EOB or a length */ |
482 | 490 | ||
@@ -512,7 +520,7 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
512 | k -= e; | 520 | k -= e; |
513 | 521 | ||
514 | /* do the copy */ | 522 | /* do the copy */ |
515 | do { | 523 | do_copy: do { |
516 | n -= (e = | 524 | n -= (e = |
517 | (e = | 525 | (e = |
518 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); | 526 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); |
@@ -530,11 +538,14 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
530 | } | 538 | } |
531 | if (w == gunzip_wsize) { | 539 | if (w == gunzip_wsize) { |
532 | gunzip_outbuf_count = (w); | 540 | gunzip_outbuf_count = (w); |
533 | flush_gunzip_window(); | 541 | if (n) resumeCopy = 1; |
542 | else resumeCopy = 0; | ||
543 | //flush_gunzip_window(); | ||
534 | w = 0; | 544 | w = 0; |
545 | return -1; | ||
535 | } | 546 | } |
536 | |||
537 | } while (n); | 547 | } while (n); |
548 | resumeCopy = 0; | ||
538 | } | 549 | } |
539 | } | 550 | } |
540 | 551 | ||
@@ -543,16 +554,56 @@ static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const | |||
543 | gunzip_bb = b; /* restore global bit buffer */ | 554 | gunzip_bb = b; /* restore global bit buffer */ |
544 | gunzip_bk = k; | 555 | gunzip_bk = k; |
545 | 556 | ||
557 | /* normally just after call to inflate_codes, but save code by putting it here */ | ||
558 | /* free the decoding tables, return */ | ||
559 | huft_free(tl); | ||
560 | huft_free(td); | ||
561 | |||
546 | /* done */ | 562 | /* done */ |
547 | return 0; | 563 | return 0; |
548 | } | 564 | } |
549 | 565 | ||
566 | int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) | ||
567 | { | ||
568 | static int n, b_stored, k_stored, w; | ||
569 | if (setup) { | ||
570 | n = my_n; | ||
571 | b_stored = my_b_stored; | ||
572 | k_stored = my_k_stored; | ||
573 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
574 | return 0; // Don't do anything first time | ||
575 | } | ||
576 | |||
577 | /* read and output the compressed data */ | ||
578 | while (n--) { | ||
579 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); | ||
580 | gunzip_window[w++] = (unsigned char) b_stored; | ||
581 | if (w == (unsigned int) gunzip_wsize) { | ||
582 | gunzip_outbuf_count = (w); | ||
583 | //flush_gunzip_window(); | ||
584 | w = 0; | ||
585 | b_stored >>= 8; | ||
586 | k_stored -= 8; | ||
587 | return -1; // Means more stuff 2do | ||
588 | } | ||
589 | b_stored >>= 8; | ||
590 | k_stored -= 8; | ||
591 | } | ||
592 | |||
593 | /* restore the globals from the locals */ | ||
594 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ | ||
595 | gunzip_bb = b_stored; /* restore global bit buffer */ | ||
596 | gunzip_bk = k_stored; | ||
597 | return 0; // Finished | ||
598 | } | ||
599 | |||
550 | /* | 600 | /* |
551 | * decompress an inflated block | 601 | * decompress an inflated block |
552 | * e: last block flag | 602 | * e: last block flag |
553 | * | 603 | * |
554 | * GLOBAL VARIABLES: bb, kk, | 604 | * GLOBAL VARIABLES: bb, kk, |
555 | */ | 605 | */ |
606 | // Return values: -1 = inflate_stored, -2 = inflate_codes | ||
556 | static int inflate_block(int *e) | 607 | static int inflate_block(int *e) |
557 | { | 608 | { |
558 | unsigned t; /* block type */ | 609 | unsigned t; /* block type */ |
@@ -585,14 +636,12 @@ static int inflate_block(int *e) | |||
585 | case 0: /* Inflate stored */ | 636 | case 0: /* Inflate stored */ |
586 | { | 637 | { |
587 | unsigned int n; /* number of bytes in block */ | 638 | unsigned int n; /* number of bytes in block */ |
588 | unsigned int w; /* current gunzip_window position */ | ||
589 | unsigned int b_stored; /* bit buffer */ | 639 | unsigned int b_stored; /* bit buffer */ |
590 | unsigned int k_stored; /* number of bits in bit buffer */ | 640 | unsigned int k_stored; /* number of bits in bit buffer */ |
591 | 641 | ||
592 | /* make local copies of globals */ | 642 | /* make local copies of globals */ |
593 | b_stored = gunzip_bb; /* initialize bit buffer */ | 643 | b_stored = gunzip_bb; /* initialize bit buffer */ |
594 | k_stored = gunzip_bk; | 644 | k_stored = gunzip_bk; |
595 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ | ||
596 | 645 | ||
597 | /* go to byte boundary */ | 646 | /* go to byte boundary */ |
598 | n = k_stored & 7; | 647 | n = k_stored & 7; |
@@ -612,24 +661,8 @@ static int inflate_block(int *e) | |||
612 | b_stored >>= 16; | 661 | b_stored >>= 16; |
613 | k_stored -= 16; | 662 | k_stored -= 16; |
614 | 663 | ||
615 | /* read and output the compressed data */ | 664 | inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored |
616 | while (n--) { | 665 | return -1; |
617 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); | ||
618 | gunzip_window[w++] = (unsigned char) b_stored; | ||
619 | if (w == (unsigned int) gunzip_wsize) { | ||
620 | gunzip_outbuf_count = (w); | ||
621 | flush_gunzip_window(); | ||
622 | w = 0; | ||
623 | } | ||
624 | b_stored >>= 8; | ||
625 | k_stored -= 8; | ||
626 | } | ||
627 | |||
628 | /* restore the globals from the locals */ | ||
629 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ | ||
630 | gunzip_bb = b_stored; /* restore global bit buffer */ | ||
631 | gunzip_bk = k_stored; | ||
632 | return 0; | ||
633 | } | 666 | } |
634 | case 1: /* Inflate fixed | 667 | case 1: /* Inflate fixed |
635 | * decompress an inflated type 1 (fixed Huffman codes) block. We should | 668 | * decompress an inflated type 1 (fixed Huffman codes) block. We should |
@@ -673,14 +706,11 @@ static int inflate_block(int *e) | |||
673 | } | 706 | } |
674 | 707 | ||
675 | /* decompress until an end-of-block code */ | 708 | /* decompress until an end-of-block code */ |
676 | if (inflate_codes(tl, td, bl, bd)) { | 709 | inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes |
677 | return 1; | 710 | |
678 | } | 711 | /* huft_free code moved into inflate_codes */ |
679 | 712 | ||
680 | /* free the decoding tables, return */ | 713 | return -2; |
681 | huft_free(tl); | ||
682 | huft_free(td); | ||
683 | return 0; | ||
684 | } | 714 | } |
685 | case 2: /* Inflate dynamic */ | 715 | case 2: /* Inflate dynamic */ |
686 | { | 716 | { |
@@ -828,14 +858,11 @@ static int inflate_block(int *e) | |||
828 | } | 858 | } |
829 | 859 | ||
830 | /* decompress until an end-of-block code */ | 860 | /* decompress until an end-of-block code */ |
831 | if (inflate_codes(tl, td, bl, bd)) { | 861 | inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes |
832 | return 1; | ||
833 | } | ||
834 | 862 | ||
835 | /* free the decoding tables, return */ | 863 | /* huft_free code moved into inflate_codes */ |
836 | huft_free(tl); | 864 | |
837 | huft_free(td); | 865 | return -2; |
838 | return 0; | ||
839 | } | 866 | } |
840 | default: | 867 | default: |
841 | /* bad block type */ | 868 | /* bad block type */ |
@@ -862,7 +889,11 @@ extern ssize_t read_gz(int fd, void *buf, size_t count) | |||
862 | if (e) return 0; // No more data here! | 889 | if (e) return 0; // No more data here! |
863 | gunzip_hufts = 0; | 890 | gunzip_hufts = 0; |
864 | r = inflate_block(&e); | 891 | r = inflate_block(&e); |
865 | if (r != 0) { | 892 | if (r == -1) { // Call inflate_stored while returning -1 |
893 | while(inflate_stored(0,0,0,0) == -1) flush_gunzip_window(); | ||
894 | } else if (r == -2) { // Call inflate_codes while returning -1 | ||
895 | while(inflate_codes(0,0,0,0,0) == -1) flush_gunzip_window(); | ||
896 | } else { | ||
866 | error_msg_and_die("inflate error %d", r); | 897 | error_msg_and_die("inflate error %d", r); |
867 | return -1; | 898 | return -1; |
868 | } | 899 | } |
@@ -895,15 +926,6 @@ extern void GZ_gzReadOpen(int fd, void *unused, int nUnused) | |||
895 | 926 | ||
896 | gunzip_in_buffer = malloc(8); | 927 | gunzip_in_buffer = malloc(8); |
897 | 928 | ||
898 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) { | ||
899 | (void) signal(SIGINT, (sig_type) abort_gzip); | ||
900 | } | ||
901 | #ifdef SIGHUP | ||
902 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { | ||
903 | (void) signal(SIGHUP, (sig_type) abort_gzip); | ||
904 | } | ||
905 | #endif | ||
906 | |||
907 | /* initialize gunzip_window, bit buffer */ | 929 | /* initialize gunzip_window, bit buffer */ |
908 | gunzip_bk = 0; | 930 | gunzip_bk = 0; |
909 | gunzip_bb = 0; | 931 | gunzip_bb = 0; |