1 8 package installer; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 13 19 public class CBZip2InputStream 20 extends InputStream 21 implements BZip2Constants 22 { 23 private static final int START_BLOCK_STATE = 1; 24 private static final int RAND_PART_A_STATE = 2; 25 private static final int RAND_PART_B_STATE = 3; 26 private static final int RAND_PART_C_STATE = 4; 27 private static final int NO_RAND_PART_A_STATE = 5; 28 private static final int NO_RAND_PART_B_STATE = 6; 29 private static final int NO_RAND_PART_C_STATE = 7; 30 31 private CRC m_crc = new CRC(); 32 private boolean[] m_inUse = new boolean[ 256 ]; 33 private char[] m_seqToUnseq = new char[ 256 ]; 34 private char[] m_unseqToSeq = new char[ 256 ]; 35 private char[] m_selector = new char[ MAX_SELECTORS ]; 36 private char[] m_selectorMtf = new char[ MAX_SELECTORS ]; 37 38 42 private int[] m_unzftab = new int[ 256 ]; 43 44 private int[][] m_limit = new int[ N_GROUPS ][ MAX_ALPHA_SIZE ]; 45 private int[][] m_base = new int[ N_GROUPS ][ MAX_ALPHA_SIZE ]; 46 private int[][] m_perm = new int[ N_GROUPS ][ MAX_ALPHA_SIZE ]; 47 private int[] m_minLens = new int[ N_GROUPS ]; 48 49 private boolean m_streamEnd; 50 private int m_currentChar = -1; 51 52 private int m_currentState = START_BLOCK_STATE; 53 private int m_rNToGo; 54 private int m_rTPos; 55 private int m_tPos; 56 57 private int i2; 58 private int count; 59 private int chPrev; 60 private int ch2; 61 private int j2; 62 private char z; 63 64 private boolean m_blockRandomised; 65 66 70 private int m_blockSize100k; 71 private int m_bsBuff; 72 private int m_bsLive; 73 74 private InputStream m_input; 75 76 private int m_computedBlockCRC; 77 private int m_computedCombinedCRC; 78 79 83 private int m_last; 84 private char[] m_ll8; 85 private int m_nInUse; 86 87 90 private int m_origPtr; 91 92 private int m_storedBlockCRC; 93 private int m_storedCombinedCRC; 94 private int[] m_tt; 95 96 public CBZip2InputStream( final InputStream input ) 97 { 98 bsSetStream( input ); 99 initialize(); 100 initBlock(); 101 setupBlock(); 102 } 103 104 private static void badBlockHeader() 105 { 106 cadvise(); 107 } 108 109 private static void blockOverrun() 110 { 111 cadvise(); 112 } 113 114 private static void cadvise() 115 { 116 System.out.println( "CRC Error" ); 117 } 119 120 private static void compressedStreamEOF() 121 { 122 cadvise(); 123 } 124 125 private static void crcError() 126 { 127 cadvise(); 128 } 129 130 public int read() 131 { 132 if( m_streamEnd ) 133 { 134 return -1; 135 } 136 else 137 { 138 int retChar = m_currentChar; 139 switch( m_currentState ) 140 { 141 case START_BLOCK_STATE: 142 break; 143 case RAND_PART_A_STATE: 144 break; 145 case RAND_PART_B_STATE: 146 setupRandPartB(); 147 break; 148 case RAND_PART_C_STATE: 149 setupRandPartC(); 150 break; 151 case NO_RAND_PART_A_STATE: 152 break; 153 case NO_RAND_PART_B_STATE: 154 setupNoRandPartB(); 155 break; 156 case NO_RAND_PART_C_STATE: 157 setupNoRandPartC(); 158 break; 159 default: 160 break; 161 } 162 return retChar; 163 } 164 } 165 166 private void setDecompressStructureSizes( int newSize100k ) 167 { 168 if( !( 0 <= newSize100k && newSize100k <= 9 && 0 <= m_blockSize100k 169 && m_blockSize100k <= 9 ) ) 170 { 171 } 173 174 m_blockSize100k = newSize100k; 175 176 if( newSize100k == 0 ) 177 { 178 return; 179 } 180 181 int n = BASE_BLOCK_SIZE * newSize100k; 182 m_ll8 = new char[ n ]; 183 m_tt = new int[ n ]; 184 } 185 186 private void setupBlock() 187 { 188 int[] cftab = new int[ 257 ]; 189 char ch; 190 191 cftab[ 0 ] = 0; 192 for( int i = 1; i <= 256; i++ ) 193 { 194 cftab[ i ] = m_unzftab[ i - 1 ]; 195 } 196 for( int i = 1; i <= 256; i++ ) 197 { 198 cftab[ i ] += cftab[ i - 1 ]; 199 } 200 201 for( int i = 0; i <= m_last; i++ ) 202 { 203 ch = m_ll8[ i ]; 204 m_tt[ cftab[ ch ] ] = i; 205 cftab[ ch ]++; 206 } 207 cftab = null; 208 209 m_tPos = m_tt[ m_origPtr ]; 210 211 count = 0; 212 i2 = 0; 213 ch2 = 256; 214 217 if( m_blockRandomised ) 218 { 219 m_rNToGo = 0; 220 m_rTPos = 0; 221 setupRandPartA(); 222 } 223 else 224 { 225 setupNoRandPartA(); 226 } 227 } 228 229 private void setupNoRandPartA() 230 { 231 if( i2 <= m_last ) 232 { 233 chPrev = ch2; 234 ch2 = m_ll8[ m_tPos ]; 235 m_tPos = m_tt[ m_tPos ]; 236 i2++; 237 238 m_currentChar = ch2; 239 m_currentState = NO_RAND_PART_B_STATE; 240 m_crc.updateCRC( ch2 ); 241 } 242 else 243 { 244 endBlock(); 245 initBlock(); 246 setupBlock(); 247 } 248 } 249 250 private void setupNoRandPartB() 251 { 252 if( ch2 != chPrev ) 253 { 254 m_currentState = NO_RAND_PART_A_STATE; 255 count = 1; 256 setupNoRandPartA(); 257 } 258 else 259 { 260 count++; 261 if( count >= 4 ) 262 { 263 z = m_ll8[ m_tPos ]; 264 m_tPos = m_tt[ m_tPos ]; 265 m_currentState = NO_RAND_PART_C_STATE; 266 j2 = 0; 267 setupNoRandPartC(); 268 } 269 else 270 { 271 m_currentState = NO_RAND_PART_A_STATE; 272 setupNoRandPartA(); 273 } 274 } 275 } 276 277 private void setupNoRandPartC() 278 { 279 if( j2 < z ) 280 { 281 m_currentChar = ch2; 282 m_crc.updateCRC( ch2 ); 283 j2++; 284 } 285 else 286 { 287 m_currentState = NO_RAND_PART_A_STATE; 288 i2++; 289 count = 0; 290 setupNoRandPartA(); 291 } 292 } 293 294 private void setupRandPartA() 295 { 296 if( i2 <= m_last ) 297 { 298 chPrev = ch2; 299 ch2 = m_ll8[ m_tPos ]; 300 m_tPos = m_tt[ m_tPos ]; 301 if( m_rNToGo == 0 ) 302 { 303 m_rNToGo = RAND_NUMS[ m_rTPos ]; 304 m_rTPos++; 305 if( m_rTPos == 512 ) 306 { 307 m_rTPos = 0; 308 } 309 } 310 m_rNToGo--; 311 ch2 ^= ( ( m_rNToGo == 1 ) ? 1 : 0 ); 312 i2++; 313 314 m_currentChar = ch2; 315 m_currentState = RAND_PART_B_STATE; 316 m_crc.updateCRC( ch2 ); 317 } 318 else 319 { 320 endBlock(); 321 initBlock(); 322 setupBlock(); 323 } 324 } 325 326 private void setupRandPartB() 327 { 328 if( ch2 != chPrev ) 329 { 330 m_currentState = RAND_PART_A_STATE; 331 count = 1; 332 setupRandPartA(); 333 } 334 else 335 { 336 count++; 337 if( count >= 4 ) 338 { 339 z = m_ll8[ m_tPos ]; 340 m_tPos = m_tt[ m_tPos ]; 341 if( m_rNToGo == 0 ) 342 { 343 m_rNToGo = RAND_NUMS[ m_rTPos ]; 344 m_rTPos++; 345 if( m_rTPos == 512 ) 346 { 347 m_rTPos = 0; 348 } 349 } 350 m_rNToGo--; 351 z ^= ( ( m_rNToGo == 1 ) ? 1 : 0 ); 352 j2 = 0; 353 m_currentState = RAND_PART_C_STATE; 354 setupRandPartC(); 355 } 356 else 357 { 358 m_currentState = RAND_PART_A_STATE; 359 setupRandPartA(); 360 } 361 } 362 } 363 364 private void setupRandPartC() 365 { 366 if( j2 < z ) 367 { 368 m_currentChar = ch2; 369 m_crc.updateCRC( ch2 ); 370 j2++; 371 } 372 else 373 { 374 m_currentState = RAND_PART_A_STATE; 375 i2++; 376 count = 0; 377 setupRandPartA(); 378 } 379 } 380 381 private void getAndMoveToFrontDecode() 382 { 383 int nextSym; 384 385 int limitLast = BASE_BLOCK_SIZE * m_blockSize100k; 386 m_origPtr = readVariableSizedInt( 24 ); 387 388 recvDecodingTables(); 389 int EOB = m_nInUse + 1; 390 int groupNo = -1; 391 int groupPos = 0; 392 393 399 for( int i = 0; i <= 255; i++ ) 400 { 401 m_unzftab[ i ] = 0; 402 } 403 404 final char[] yy = new char[ 256 ]; 405 for( int i = 0; i <= 255; i++ ) 406 { 407 yy[ i ] = (char)i; 408 } 409 410 m_last = -1; 411 int zt; 412 int zn; 413 int zvec; 414 int zj; 415 groupNo++; 416 groupPos = G_SIZE - 1; 417 418 zt = m_selector[ groupNo ]; 419 zn = m_minLens[ zt ]; 420 zvec = bsR( zn ); 421 while( zvec > m_limit[ zt ][ zn ] ) 422 { 423 zn++; 424 425 while( m_bsLive < 1 ) 426 { 427 int zzi; 428 try 429 { 430 zzi = m_input.read(); 431 } 432 catch( IOException e ) 433 { 434 compressedStreamEOF(); 435 break; 436 } 437 if( zzi == -1 ) 438 { 439 compressedStreamEOF(); 440 break; 441 } 442 m_bsBuff = ( m_bsBuff << 8 ) | ( zzi & 0xff ); 443 m_bsLive += 8; 444 } 445 446 zj = ( m_bsBuff >> ( m_bsLive - 1 ) ) & 1; 447 m_bsLive--; 448 449 zvec = ( zvec << 1 ) | zj; 450 } 451 nextSym = m_perm[ zt ][ zvec - m_base[ zt ][ zn ] ]; 452 453 while( true ) 454 { 455 if( nextSym == EOB ) 456 { 457 break; 458 } 459 460 if( nextSym == RUNA || nextSym == RUNB ) 461 { 462 char ch; 463 int s = -1; 464 int N = 1; 465 do 466 { 467 if( nextSym == RUNA ) 468 { 469 s = s + ( 0 + 1 ) * N; 470 } 471 else { 473 s = s + ( 1 + 1 ) * N; 474 } 475 N = N * 2; 476 477 if( groupPos == 0 ) 478 { 479 groupNo++; 480 groupPos = G_SIZE; 481 } 482 groupPos--; 483 zt = m_selector[ groupNo ]; 484 zn = m_minLens[ zt ]; 485 zvec = bsR( zn ); 486 while( zvec > m_limit[ zt ][ zn ] ) 487 { 488 zn++; 489 490 while( m_bsLive < 1 ) 491 { 492 int zzi; 493 char thech = 0; 494 try 495 { 496 thech = (char)m_input.read(); 497 } 498 catch( IOException e ) 499 { 500 compressedStreamEOF(); 501 } 502 if( thech == -1 ) 503 { 504 compressedStreamEOF(); 505 } 506 zzi = thech; 507 m_bsBuff = ( m_bsBuff << 8 ) | ( zzi & 0xff ); 508 m_bsLive += 8; 509 } 510 511 zj = ( m_bsBuff >> ( m_bsLive - 1 ) ) & 1; 512 m_bsLive--; 513 zvec = ( zvec << 1 ) | zj; 514 } 515 516 nextSym = m_perm[ zt ][ zvec - m_base[ zt ][ zn ] ]; 517 518 } while( nextSym == RUNA || nextSym == RUNB ); 519 520 s++; 521 ch = m_seqToUnseq[ yy[ 0 ] ]; 522 m_unzftab[ ch ] += s; 523 524 while( s > 0 ) 525 { 526 m_last++; 527 m_ll8[ m_last ] = ch; 528 s--; 529 } 530 531 if( m_last >= limitLast ) 532 { 533 blockOverrun(); 534 } 535 continue; 536 } 537 else 538 { 539 char tmp; 540 m_last++; 541 if( m_last >= limitLast ) 542 { 543 blockOverrun(); 544 } 545 546 tmp = yy[ nextSym - 1 ]; 547 m_unzftab[ m_seqToUnseq[ tmp ] ]++; 548 m_ll8[ m_last ] = m_seqToUnseq[ tmp ]; 549 550 555 int j = nextSym - 1; 556 for( ; j > 3; j -= 4 ) 557 { 558 yy[ j ] = yy[ j - 1 ]; 559 yy[ j - 1 ] = yy[ j - 2 ]; 560 yy[ j - 2 ] = yy[ j - 3 ]; 561 yy[ j - 3 ] = yy[ j - 4 ]; 562 } 563 for( ; j > 0; j-- ) 564 { 565 yy[ j ] = yy[ j - 1 ]; 566 } 567 568 yy[ 0 ] = tmp; 569 570 if( groupPos == 0 ) 571 { 572 groupNo++; 573 groupPos = G_SIZE; 574 } 575 groupPos--; 576 zt = m_selector[ groupNo ]; 577 zn = m_minLens[ zt ]; 578 zvec = bsR( zn ); 579 while( zvec > m_limit[ zt ][ zn ] ) 580 { 581 zn++; 582 583 while( m_bsLive < 1 ) 584 { 585 char ch = 0; 586 try 587 { 588 ch = (char)m_input.read(); 589 } 590 catch( IOException e ) 591 { 592 compressedStreamEOF(); 593 } 594 595 m_bsBuff = ( m_bsBuff << 8 ) | ( ch & 0xff ); 596 m_bsLive += 8; 597 } 598 599 zj = ( m_bsBuff >> ( m_bsLive - 1 ) ) & 1; 600 m_bsLive--; 601 602 zvec = ( zvec << 1 ) | zj; 603 } 604 nextSym = m_perm[ zt ][ zvec - m_base[ zt ][ zn ] ]; 605 606 continue; 607 } 608 } 609 } 610 611 private void bsFinishedWithStream() 612 { 613 m_input = null; 614 } 615 616 private int readVariableSizedInt( final int numBits ) 617 { 618 return bsR( numBits ); 619 } 620 621 private char readUnsignedChar() 622 { 623 return (char)bsR( 8 ); 624 } 625 626 private int readInt() 627 { 628 int u = 0; 629 u = ( u << 8 ) | bsR( 8 ); 630 u = ( u << 8 ) | bsR( 8 ); 631 u = ( u << 8 ) | bsR( 8 ); 632 u = ( u << 8 ) | bsR( 8 ); 633 return u; 634 } 635 636 private int bsR( final int n ) 637 { 638 while( m_bsLive < n ) 639 { 640 char ch = 0; 641 try 642 { 643 ch = (char)m_input.read(); 644 } 645 catch( final IOException ioe ) 646 { 647 compressedStreamEOF(); 648 } 649 650 if( ch == -1 ) 651 { 652 compressedStreamEOF(); 653 } 654 655 m_bsBuff = ( m_bsBuff << 8 ) | ( ch & 0xff ); 656 m_bsLive += 8; 657 } 658 659 final int result = ( m_bsBuff >> ( m_bsLive - n ) ) & ( ( 1 << n ) - 1 ); 660 m_bsLive -= n; 661 return result; 662 } 663 664 private void bsSetStream( final InputStream input ) 665 { 666 m_input = input; 667 m_bsLive = 0; 668 m_bsBuff = 0; 669 } 670 671 private void complete() 672 { 673 m_storedCombinedCRC = readInt(); 674 if( m_storedCombinedCRC != m_computedCombinedCRC ) 675 { 676 crcError(); 677 } 678 679 bsFinishedWithStream(); 680 m_streamEnd = true; 681 } 682 683 private void endBlock() 684 { 685 m_computedBlockCRC = m_crc.getFinalCRC(); 686 689 if( m_storedBlockCRC != m_computedBlockCRC ) 690 { 691 crcError(); 692 } 693 694 m_computedCombinedCRC = ( m_computedCombinedCRC << 1 ) 695 | ( m_computedCombinedCRC >>> 31 ); 696 m_computedCombinedCRC ^= m_computedBlockCRC; 697 } 698 699 private void hbCreateDecodeTables( final int[] limit, 700 final int[] base, 701 final int[] perm, 702 final char[] length, 703 final int minLen, 704 final int maxLen, 705 final int alphaSize ) 706 { 707 int pp = 0; 708 for( int i = minLen; i <= maxLen; i++ ) 709 { 710 for( int j = 0; j < alphaSize; j++ ) 711 { 712 if( length[ j ] == i ) 713 { 714 perm[ pp ] = j; 715 pp++; 716 } 717 } 718 } 719 720 for( int i = 0; i < MAX_CODE_LEN; i++ ) 721 { 722 base[ i ] = 0; 723 } 724 725 for( int i = 0; i < alphaSize; i++ ) 726 { 727 base[ length[ i ] + 1 ]++; 728 } 729 730 for( int i = 1; i < MAX_CODE_LEN; i++ ) 731 { 732 base[ i ] += base[ i - 1 ]; 733 } 734 735 for( int i = 0; i < MAX_CODE_LEN; i++ ) 736 { 737 limit[ i ] = 0; 738 } 739 740 int vec = 0; 741 for( int i = minLen; i <= maxLen; i++ ) 742 { 743 vec += ( base[ i + 1 ] - base[ i ] ); 744 limit[ i ] = vec - 1; 745 vec <<= 1; 746 } 747 748 for( int i = minLen + 1; i <= maxLen; i++ ) 749 { 750 base[ i ] = ( ( limit[ i - 1 ] + 1 ) << 1 ) - base[ i ]; 751 } 752 } 753 754 private void initBlock() 755 { 756 final char magic1 = readUnsignedChar(); 757 final char magic2 = readUnsignedChar(); 758 final char magic3 = readUnsignedChar(); 759 final char magic4 = readUnsignedChar(); 760 final char magic5 = readUnsignedChar(); 761 final char magic6 = readUnsignedChar(); 762 if( magic1 == 0x17 && magic2 == 0x72 && magic3 == 0x45 && 763 magic4 == 0x38 && magic5 == 0x50 && magic6 == 0x90 ) 764 { 765 complete(); 766 return; 767 } 768 769 if( magic1 != 0x31 || magic2 != 0x41 || magic3 != 0x59 || 770 magic4 != 0x26 || magic5 != 0x53 || magic6 != 0x59 ) 771 { 772 badBlockHeader(); 773 m_streamEnd = true; 774 return; 775 } 776 777 m_storedBlockCRC = readInt(); 778 779 if( bsR( 1 ) == 1 ) 780 { 781 m_blockRandomised = true; 782 } 783 else 784 { 785 m_blockRandomised = false; 786 } 787 788 getAndMoveToFrontDecode(); 790 791 m_crc.initialiseCRC(); 792 m_currentState = START_BLOCK_STATE; 793 } 794 795 private void initialize() 796 { 797 final char magic3 = readUnsignedChar(); 798 final char magic4 = readUnsignedChar(); 799 if( magic3 != 'h' || magic4 < '1' || magic4 > '9' ) 800 { 801 bsFinishedWithStream(); 802 m_streamEnd = true; 803 return; 804 } 805 806 setDecompressStructureSizes( magic4 - '0' ); 807 m_computedCombinedCRC = 0; 808 } 809 810 private void makeMaps() 811 { 812 m_nInUse = 0; 813 for( int i = 0; i < 256; i++ ) 814 { 815 if( m_inUse[ i ] ) 816 { 817 m_seqToUnseq[ m_nInUse ] = (char)i; 818 m_unseqToSeq[ i ] = (char)m_nInUse; 819 m_nInUse++; 820 } 821 } 822 } 823 824 private void recvDecodingTables() 825 { 826 buildInUseTable(); 827 makeMaps(); 828 final int alphaSize = m_nInUse + 2; 829 830 833 final int groupCount = bsR( 3 ); 834 final int selectorCount = bsR( 15 ); 835 for( int i = 0; i < selectorCount; i++ ) 836 { 837 int run = 0; 838 while( bsR( 1 ) == 1 ) 839 { 840 run++; 841 } 842 m_selectorMtf[ i ] = (char)run; 843 } 844 845 848 final char[] pos = new char[ N_GROUPS ]; 849 for( char v = 0; v < groupCount; v++ ) 850 { 851 pos[ v ] = v; 852 } 853 854 for( int i = 0; i < selectorCount; i++ ) 855 { 856 int v = m_selectorMtf[ i ]; 857 final char tmp = pos[ v ]; 858 while( v > 0 ) 859 { 860 pos[ v ] = pos[ v - 1 ]; 861 v--; 862 } 863 pos[ 0 ] = tmp; 864 m_selector[ i ] = tmp; 865 } 866 867 final char[][] len = new char[ N_GROUPS ][ MAX_ALPHA_SIZE ]; 868 871 for( int i = 0; i < groupCount; i++ ) 872 { 873 int curr = bsR( 5 ); 874 for( int j = 0; j < alphaSize; j++ ) 875 { 876 while( bsR( 1 ) == 1 ) 877 { 878 if( bsR( 1 ) == 0 ) 879 { 880 curr++; 881 } 882 else 883 { 884 curr--; 885 } 886 } 887 len[ i ][ j ] = (char)curr; 888 } 889 } 890 891 894 for( int k = 0; k < groupCount; k++ ) 895 { 896 int minLen = 32; 897 int maxLen = 0; 898 for( int i = 0; i < alphaSize; i++ ) 899 { 900 if( len[ k ][ i ] > maxLen ) 901 { 902 maxLen = len[ k ][ i ]; 903 } 904 if( len[ k ][ i ] < minLen ) 905 { 906 minLen = len[ k ][ i ]; 907 } 908 } 909 hbCreateDecodeTables( m_limit[ k ], m_base[ k ], m_perm[ k ], len[ k ], minLen, 910 maxLen, alphaSize ); 911 m_minLens[ k ] = minLen; 912 } 913 } 914 915 private void buildInUseTable() 916 { 917 final boolean[] inUse16 = new boolean[ 16 ]; 918 919 922 for( int i = 0; i < 16; i++ ) 923 { 924 if( bsR( 1 ) == 1 ) 925 { 926 inUse16[ i ] = true; 927 } 928 else 929 { 930 inUse16[ i ] = false; 931 } 932 } 933 934 for( int i = 0; i < 256; i++ ) 935 { 936 m_inUse[ i ] = false; 937 } 938 939 for( int i = 0; i < 16; i++ ) 940 { 941 if( inUse16[ i ] ) 942 { 943 for( int j = 0; j < 16; j++ ) 944 { 945 if( bsR( 1 ) == 1 ) 946 { 947 m_inUse[ i * 16 + j ] = true; 948 } 949 } 950 } 951 } 952 } 953 } 954 | Popular Tags |