1 2/*-------------------------------------------------------------*/ 3/*--- Library top-level functions. ---*/ 4/*--- bzlib.c ---*/ 5/*-------------------------------------------------------------*/ 6 7/*-- 8 This file is a part of bzip2 and/or libbzip2, a program and 9 library for lossless, block-sorting data compression. 10 |
11 Copyright (C) 1996-2005 Julian R Seward. All rights reserved. |
12 13 Redistribution and use in source and binary forms, with or without 14 modification, are permitted provided that the following conditions 15 are met: 16 17 1. Redistributions of source code must retain the above copyright 18 notice, this list of conditions and the following disclaimer. 19 --- 17 unchanged lines hidden (view full) --- 37 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 40 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 43 44 Julian Seward, Cambridge, UK. |
45 jseward@bzip.org |
46 bzip2/libbzip2 version 1.0 of 21 March 2000 47 48 This program is based on (at least) the work of: 49 Mike Burrows 50 David Wheeler 51 Peter Fenwick 52 Alistair Moffat 53 Radford Neal --- 29 unchanged lines hidden (view full) --- 83 84/*---------------------------------------------------*/ 85#ifndef BZ_NO_STDIO 86void BZ2_bz__AssertH__fail ( int errcode ) 87{ 88 fprintf(stderr, 89 "\n\nbzip2/libbzip2: internal error number %d.\n" 90 "This is a bug in bzip2/libbzip2, %s.\n" |
91 "Please report it to me at: jseward@bzip.org. If this happened\n" |
92 "when you were using some program which uses libbzip2 as a\n" 93 "component, you should also report this bug to the author(s)\n" 94 "of that program. Please make an effort to report this bug;\n" 95 "timely and accurate bug reports eventually lead to higher\n" |
96 "quality software. Thanks. Julian Seward, 15 February 2005.\n\n", |
97 errcode, 98 BZ2_bzlibVersion() 99 ); 100 101 if (errcode == 1007) { 102 fprintf(stderr, 103 "\n*** A special note about internal error number 1007 ***\n" 104 "\n" --- 464 unchanged lines hidden (view full) --- 569 s->currBlockNo = 0; 570 s->verbosity = verbosity; 571 572 return BZ_OK; 573} 574 575 576/*---------------------------------------------------*/ |
577/* Return True iff data corruption is discovered. 578 Returns False if there is no problem. 579*/ |
580static |
581Bool unRLE_obuf_to_output_FAST ( DState* s ) |
582{ 583 UChar k1; 584 585 if (s->blockRandomised) { 586 587 while (True) { 588 /* try to finish existing run */ 589 while (True) { |
590 if (s->strm->avail_out == 0) return False; |
591 if (s->state_out_len == 0) break; 592 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; 593 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); 594 s->state_out_len--; 595 s->strm->next_out++; 596 s->strm->avail_out--; 597 s->strm->total_out_lo32++; 598 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; 599 } |
600 |
601 /* can a new run be started? */ |
602 if (s->nblock_used == s->save_nblock+1) return False; |
603 |
604 /* Only caused by corrupt data stream? */ 605 if (s->nblock_used > s->save_nblock+1) 606 return True; |
607 608 s->state_out_len = 1; 609 s->state_out_ch = s->k0; 610 BZ_GET_FAST(k1); BZ_RAND_UPD_MASK; 611 k1 ^= BZ_RAND_MASK; s->nblock_used++; 612 if (s->nblock_used == s->save_nblock+1) continue; 613 if (k1 != s->k0) { s->k0 = k1; continue; }; 614 --- 53 unchanged lines hidden (view full) --- 668 c_state_out_len = 1; goto return_notr; 669 }; 670 *( (UChar*)(cs_next_out) ) = c_state_out_ch; 671 BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); 672 cs_next_out++; 673 cs_avail_out--; 674 } 675 } |
676 /* Only caused by corrupt data stream? */ 677 if (c_nblock_used > s_save_nblockPP) 678 return True; 679 |
680 /* can a new run be started? */ 681 if (c_nblock_used == s_save_nblockPP) { 682 c_state_out_len = 0; goto return_notr; 683 }; 684 c_state_out_ch = c_k0; 685 BZ_GET_FAST_C(k1); c_nblock_used++; 686 if (k1 != c_k0) { 687 c_k0 = k1; goto s_state_out_len_eq_one; --- 29 unchanged lines hidden (view full) --- 717 s->nblock_used = c_nblock_used; 718 s->k0 = c_k0; 719 s->tt = c_tt; 720 s->tPos = c_tPos; 721 s->strm->next_out = cs_next_out; 722 s->strm->avail_out = cs_avail_out; 723 /* end save */ 724 } |
725 return False; |
726} 727 728 729 730/*---------------------------------------------------*/ 731__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab ) 732{ 733 Int32 nb, na, mid; --- 4 unchanged lines hidden (view full) --- 738 if (indx >= cftab[mid]) nb = mid; else na = mid; 739 } 740 while (na - nb != 1); 741 return nb; 742} 743 744 745/*---------------------------------------------------*/ |
746/* Return True iff data corruption is discovered. 747 Returns False if there is no problem. 748*/ |
749static |
750Bool unRLE_obuf_to_output_SMALL ( DState* s ) |
751{ 752 UChar k1; 753 754 if (s->blockRandomised) { 755 756 while (True) { 757 /* try to finish existing run */ 758 while (True) { |
759 if (s->strm->avail_out == 0) return False; |
760 if (s->state_out_len == 0) break; 761 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; 762 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); 763 s->state_out_len--; 764 s->strm->next_out++; 765 s->strm->avail_out--; 766 s->strm->total_out_lo32++; 767 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; 768 } 769 770 /* can a new run be started? */ |
771 if (s->nblock_used == s->save_nblock+1) return False; 772 773 /* Only caused by corrupt data stream? */ 774 if (s->nblock_used > s->save_nblock+1) 775 return True; |
776 777 s->state_out_len = 1; 778 s->state_out_ch = s->k0; 779 BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK; 780 k1 ^= BZ_RAND_MASK; s->nblock_used++; 781 if (s->nblock_used == s->save_nblock+1) continue; 782 if (k1 != s->k0) { s->k0 = k1; continue; }; 783 --- 16 unchanged lines hidden (view full) --- 800 s->k0 ^= BZ_RAND_MASK; s->nblock_used++; 801 } 802 803 } else { 804 805 while (True) { 806 /* try to finish existing run */ 807 while (True) { |
808 if (s->strm->avail_out == 0) return False; |
809 if (s->state_out_len == 0) break; 810 *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; 811 BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); 812 s->state_out_len--; 813 s->strm->next_out++; 814 s->strm->avail_out--; 815 s->strm->total_out_lo32++; 816 if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; 817 } 818 819 /* can a new run be started? */ |
820 if (s->nblock_used == s->save_nblock+1) return False; 821 822 /* Only caused by corrupt data stream? */ 823 if (s->nblock_used > s->save_nblock+1) 824 return True; |
825 826 s->state_out_len = 1; 827 s->state_out_ch = s->k0; 828 BZ_GET_SMALL(k1); s->nblock_used++; 829 if (s->nblock_used == s->save_nblock+1) continue; 830 if (k1 != s->k0) { s->k0 = k1; continue; }; 831 832 s->state_out_len = 2; --- 13 unchanged lines hidden (view full) --- 846 847 } 848} 849 850 851/*---------------------------------------------------*/ 852int BZ_API(BZ2_bzDecompress) ( bz_stream *strm ) 853{ |
854 Bool corrupt; |
855 DState* s; 856 if (strm == NULL) return BZ_PARAM_ERROR; 857 s = strm->state; 858 if (s == NULL) return BZ_PARAM_ERROR; 859 if (s->strm != strm) return BZ_PARAM_ERROR; 860 861 while (True) { 862 if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR; 863 if (s->state == BZ_X_OUTPUT) { 864 if (s->smallDecompress) |
865 corrupt = unRLE_obuf_to_output_SMALL ( s ); else 866 corrupt = unRLE_obuf_to_output_FAST ( s ); 867 if (corrupt) return BZ_DATA_ERROR; |
868 if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) { 869 BZ_FINALISE_CRC ( s->calculatedBlockCRC ); 870 if (s->verbosity >= 3) |
871 VPrintf2 ( " {0x%08x, 0x%08x}", s->storedBlockCRC, |
872 s->calculatedBlockCRC ); 873 if (s->verbosity >= 2) VPrintf0 ( "]" ); 874 if (s->calculatedBlockCRC != s->storedBlockCRC) 875 return BZ_DATA_ERROR; 876 s->calculatedCombinedCRC 877 = (s->calculatedCombinedCRC << 1) | 878 (s->calculatedCombinedCRC >> 31); 879 s->calculatedCombinedCRC ^= s->calculatedBlockCRC; 880 s->state = BZ_X_BLKHDR_1; 881 } else { 882 return BZ_OK; 883 } 884 } 885 if (s->state >= BZ_X_MAGIC_1) { 886 Int32 r = BZ2_decompress ( s ); 887 if (r == BZ_STREAM_END) { 888 if (s->verbosity >= 3) |
889 VPrintf2 ( "\n combined CRCs: stored = 0x%08x, computed = 0x%08x", |
890 s->storedCombinedCRC, s->calculatedCombinedCRC ); 891 if (s->calculatedCombinedCRC != s->storedCombinedCRC) 892 return BZ_DATA_ERROR; 893 return r; 894 } 895 if (s->state != BZ_X_OUTPUT) return r; 896 } 897 } --- 719 unchanged lines hidden --- |