1 2 29 34 35 package com.jcraft.jzlib; 36 37 public 38 final class Deflate{ 39 40 static final private int MAX_MEM_LEVEL=9; 41 42 static final private int Z_DEFAULT_COMPRESSION=-1; 43 44 static final private int MAX_WBITS=15; static final private int DEF_MEM_LEVEL=8; 46 47 static class Config{ 48 int good_length; int max_lazy; int nice_length; int max_chain; 52 int func; 53 Config(int good_length, int max_lazy, 54 int nice_length, int max_chain, int func){ 55 this.good_length=good_length; 56 this.max_lazy=max_lazy; 57 this.nice_length=nice_length; 58 this.max_chain=max_chain; 59 this.func=func; 60 } 61 } 62 63 static final private int STORED=0; 64 static final private int FAST=1; 65 static final private int SLOW=2; 66 static final private Config[] config_table; 67 static{ 68 config_table=new Config[10]; 69 config_table[0]=new Config(0, 0, 0, 0, STORED); 71 config_table[1]=new Config(4, 4, 8, 4, FAST); 72 config_table[2]=new Config(4, 5, 16, 8, FAST); 73 config_table[3]=new Config(4, 6, 32, 32, FAST); 74 75 config_table[4]=new Config(4, 4, 16, 16, SLOW); 76 config_table[5]=new Config(8, 16, 32, 32, SLOW); 77 config_table[6]=new Config(8, 16, 128, 128, SLOW); 78 config_table[7]=new Config(8, 32, 128, 256, SLOW); 79 config_table[8]=new Config(32, 128, 258, 1024, SLOW); 80 config_table[9]=new Config(32, 258, 258, 4096, SLOW); 81 } 82 83 static final private String [] z_errmsg = { 84 "need dictionary", "stream end", "", "file error", "stream error", "data error", "insufficient memory", "buffer error", "incompatible version", "" 94 }; 95 96 static final private int NeedMore=0; 98 99 static final private int BlockDone=1; 101 102 static final private int FinishStarted=2; 104 105 static final private int FinishDone=3; 107 108 static final private int PRESET_DICT=0x20; 110 111 static final private int Z_FILTERED=1; 112 static final private int Z_HUFFMAN_ONLY=2; 113 static final private int Z_DEFAULT_STRATEGY=0; 114 115 static final private int Z_NO_FLUSH=0; 116 static final private int Z_PARTIAL_FLUSH=1; 117 static final private int Z_SYNC_FLUSH=2; 118 static final private int Z_FULL_FLUSH=3; 119 static final private int Z_FINISH=4; 120 121 static final private int Z_OK=0; 122 static final private int Z_STREAM_END=1; 123 static final private int Z_NEED_DICT=2; 124 static final private int Z_ERRNO=-1; 125 static final private int Z_STREAM_ERROR=-2; 126 static final private int Z_DATA_ERROR=-3; 127 static final private int Z_MEM_ERROR=-4; 128 static final private int Z_BUF_ERROR=-5; 129 static final private int Z_VERSION_ERROR=-6; 130 131 static final private int INIT_STATE=42; 132 static final private int BUSY_STATE=113; 133 static final private int FINISH_STATE=666; 134 135 static final private int Z_DEFLATED=8; 137 138 static final private int STORED_BLOCK=0; 139 static final private int STATIC_TREES=1; 140 static final private int DYN_TREES=2; 141 142 static final private int Z_BINARY=0; 144 static final private int Z_ASCII=1; 145 static final private int Z_UNKNOWN=2; 146 147 static final private int Buf_size=8*2; 148 149 static final private int REP_3_6=16; 151 152 static final private int REPZ_3_10=17; 154 155 static final private int REPZ_11_138=18; 157 158 static final private int MIN_MATCH=3; 159 static final private int MAX_MATCH=258; 160 static final private int MIN_LOOKAHEAD=(MAX_MATCH+MIN_MATCH+1); 161 162 static final private int MAX_BITS=15; 163 static final private int D_CODES=30; 164 static final private int BL_CODES=19; 165 static final private int LENGTH_CODES=29; 166 static final private int LITERALS=256; 167 static final private int L_CODES=(LITERALS+1+LENGTH_CODES); 168 static final private int HEAP_SIZE=(2*L_CODES+1); 169 170 static final private int END_BLOCK=256; 171 172 ZStream strm; int status; byte[] pending_buf; int pending_buf_size; int pending_out; int pending; int noheader; byte data_type; byte method; int last_flush; 183 int w_size; int w_bits; int w_mask; 187 byte[] window; 188 196 int window_size; 197 200 short[] prev; 201 205 short[] head; 207 int ins_h; int hash_size; int hash_bits; int hash_mask; 212 int hash_shift; 217 218 221 int block_start; 222 223 int match_length; int prev_match; int match_available; int strstart; int match_start; int lookahead; 230 int prev_length; 233 234 int max_chain_length; 237 238 int max_lazy_match; 242 243 247 int level; int strategy; 250 int good_match; 252 253 int nice_match; 255 256 short[] dyn_ltree; short[] dyn_dtree; short[] bl_tree; 260 Tree l_desc=new Tree(); Tree d_desc=new Tree(); Tree bl_desc=new Tree(); 264 short[] bl_count=new short[MAX_BITS+1]; 266 267 int[] heap=new int[2*L_CODES+1]; 269 270 int heap_len; int heap_max; 275 byte[] depth=new byte[2*L_CODES+1]; 277 278 int l_buf; 280 int lit_bufsize; 298 299 int last_lit; 301 305 int d_buf; 307 int opt_len; int static_len; int matches; int last_eob_len; 312 short bi_buf; 315 316 int bi_valid; 319 320 Deflate(){ 321 dyn_ltree=new short[HEAP_SIZE*2]; 322 dyn_dtree=new short[(2*D_CODES+1)*2]; bl_tree=new short[(2*BL_CODES+1)*2]; } 325 326 void lm_init() { 327 window_size=2*w_size; 328 329 head[hash_size-1]=0; 330 for(int i=0; i<hash_size-1; i++){ 331 head[i]=0; 332 } 333 334 max_lazy_match = Deflate.config_table[level].max_lazy; 336 good_match = Deflate.config_table[level].good_length; 337 nice_match = Deflate.config_table[level].nice_length; 338 max_chain_length = Deflate.config_table[level].max_chain; 339 340 strstart = 0; 341 block_start = 0; 342 lookahead = 0; 343 match_length = prev_length = MIN_MATCH-1; 344 match_available = 0; 345 ins_h = 0; 346 } 347 348 void tr_init(){ 350 351 l_desc.dyn_tree = dyn_ltree; 352 l_desc.stat_desc = StaticTree.static_l_desc; 353 354 d_desc.dyn_tree = dyn_dtree; 355 d_desc.stat_desc = StaticTree.static_d_desc; 356 357 bl_desc.dyn_tree = bl_tree; 358 bl_desc.stat_desc = StaticTree.static_bl_desc; 359 360 bi_buf = 0; 361 bi_valid = 0; 362 last_eob_len = 8; 364 init_block(); 366 } 367 368 void init_block(){ 369 for(int i = 0; i < L_CODES; i++) dyn_ltree[i*2] = 0; 371 for(int i= 0; i < D_CODES; i++) dyn_dtree[i*2] = 0; 372 for(int i= 0; i < BL_CODES; i++) bl_tree[i*2] = 0; 373 374 dyn_ltree[END_BLOCK*2] = 1; 375 opt_len = static_len = 0; 376 last_lit = matches = 0; 377 } 378 379 void pqdownheap(short[] tree, int k ){ 386 int v = heap[k]; 387 int j = k << 1; while (j <= heap_len) { 389 if (j < heap_len && 391 smaller(tree, heap[j+1], heap[j], depth)){ 392 j++; 393 } 394 if(smaller(tree, v, heap[j], depth)) break; 396 397 heap[k]=heap[j]; k = j; 399 j <<= 1; 401 } 402 heap[k] = v; 403 } 404 405 static boolean smaller(short[] tree, int n, int m, byte[] depth){ 406 short tn2=tree[n*2]; 407 short tm2=tree[m*2]; 408 return (tn2<tm2 || 409 (tn2==tm2 && depth[n] <= depth[m])); 410 } 411 412 void scan_tree (short[] tree, int max_code ){ 417 int n; int prevlen = -1; int curlen; int nextlen = tree[0*2+1]; int count = 0; int max_count = 7; int min_count = 4; 425 if (nextlen == 0){ max_count = 138; min_count = 3; } 426 tree[(max_code+1)*2+1] = (short)0xffff; 428 for(n = 0; n <= max_code; n++) { 429 curlen = nextlen; nextlen = tree[(n+1)*2+1]; 430 if(++count < max_count && curlen == nextlen) { 431 continue; 432 } 433 else if(count < min_count) { 434 bl_tree[curlen*2] += count; 435 } 436 else if(curlen != 0) { 437 if(curlen != prevlen) bl_tree[curlen*2]++; 438 bl_tree[REP_3_6*2]++; 439 } 440 else if(count <= 10) { 441 bl_tree[REPZ_3_10*2]++; 442 } 443 else{ 444 bl_tree[REPZ_11_138*2]++; 445 } 446 count = 0; prevlen = curlen; 447 if(nextlen == 0) { 448 max_count = 138; min_count = 3; 449 } 450 else if(curlen == nextlen) { 451 max_count = 6; min_count = 3; 452 } 453 else{ 454 max_count = 7; min_count = 4; 455 } 456 } 457 } 458 459 int build_bl_tree(){ 462 int max_blindex; 464 scan_tree(dyn_ltree, l_desc.max_code); 466 scan_tree(dyn_dtree, d_desc.max_code); 467 468 bl_desc.build_tree(this); 470 473 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { 477 if (bl_tree[Tree.bl_order[max_blindex]*2+1] != 0) break; 478 } 479 opt_len += 3*(max_blindex+1) + 5+5+4; 481 482 return max_blindex; 483 } 484 485 486 void send_all_trees(int lcodes, int dcodes, int blcodes){ 490 int rank; 492 send_bits(lcodes-257, 5); send_bits(dcodes-1, 5); 494 send_bits(blcodes-4, 4); for (rank = 0; rank < blcodes; rank++) { 496 send_bits(bl_tree[Tree.bl_order[rank]*2+1], 3); 497 } 498 send_tree(dyn_ltree, lcodes-1); send_tree(dyn_dtree, dcodes-1); } 501 502 void send_tree (short[] tree, int max_code ){ 507 int n; int prevlen = -1; int curlen; int nextlen = tree[0*2+1]; int count = 0; int max_count = 7; int min_count = 4; 515 if (nextlen == 0){ max_count = 138; min_count = 3; } 516 517 for (n = 0; n <= max_code; n++) { 518 curlen = nextlen; nextlen = tree[(n+1)*2+1]; 519 if(++count < max_count && curlen == nextlen) { 520 continue; 521 } 522 else if(count < min_count) { 523 do { send_code(curlen, bl_tree); } while (--count != 0); 524 } 525 else if(curlen != 0){ 526 if(curlen != prevlen){ 527 send_code(curlen, bl_tree); count--; 528 } 529 send_code(REP_3_6, bl_tree); 530 send_bits(count-3, 2); 531 } 532 else if(count <= 10){ 533 send_code(REPZ_3_10, bl_tree); 534 send_bits(count-3, 3); 535 } 536 else{ 537 send_code(REPZ_11_138, bl_tree); 538 send_bits(count-11, 7); 539 } 540 count = 0; prevlen = curlen; 541 if(nextlen == 0){ 542 max_count = 138; min_count = 3; 543 } 544 else if(curlen == nextlen){ 545 max_count = 6; min_count = 3; 546 } 547 else{ 548 max_count = 7; min_count = 4; 549 } 550 } 551 } 552 553 final void put_byte(byte[] p, int start, int len){ 556 System.arraycopy(p, start, pending_buf, pending, len); 557 pending+=len; 558 } 559 560 final void put_byte(byte c){ 561 pending_buf[pending++]=c; 562 } 563 final void put_short(int w) { 564 put_byte((byte)(w)); 565 put_byte((byte)(w>>>8)); 566 } 567 final void putShortMSB(int b){ 568 put_byte((byte)(b>>8)); 569 put_byte((byte)(b)); 570 } 571 572 final void send_code(int c, short[] tree){ 573 int c2=c*2; 574 send_bits((tree[c2]&0xffff), (tree[c2+1]&0xffff)); 575 } 576 577 void send_bits(int value, int length){ 578 int len = length; 579 if (bi_valid > (int)Buf_size - len) { 580 int val = value; 581 bi_buf |= ((val << bi_valid)&0xffff); 583 put_short(bi_buf); 584 bi_buf = (short)(val >>> (Buf_size - bi_valid)); 585 bi_valid += len - Buf_size; 586 } else { 587 bi_buf |= (((value) << bi_valid)&0xffff); 589 bi_valid += len; 590 } 591 } 592 593 void _tr_align(){ 603 send_bits(STATIC_TREES<<1, 3); 604 send_code(END_BLOCK, StaticTree.static_ltree); 605 606 bi_flush(); 607 608 if (1 + last_eob_len + 10 - bi_valid < 9) { 613 send_bits(STATIC_TREES<<1, 3); 614 send_code(END_BLOCK, StaticTree.static_ltree); 615 bi_flush(); 616 } 617 last_eob_len = 7; 618 } 619 620 621 boolean _tr_tally (int dist, int lc ){ 626 627 pending_buf[d_buf+last_lit*2] = (byte)(dist>>>8); 628 pending_buf[d_buf+last_lit*2+1] = (byte)dist; 629 630 pending_buf[l_buf+last_lit] = (byte)lc; last_lit++; 631 632 if (dist == 0) { 633 dyn_ltree[lc*2]++; 635 } 636 else { 637 matches++; 638 dist--; dyn_ltree[(Tree._length_code[lc]+LITERALS+1)*2]++; 641 dyn_dtree[Tree.d_code(dist)*2]++; 642 } 643 644 if ((last_lit & 0x1fff) == 0 && level > 2) { 645 int out_length = last_lit*8; 647 int in_length = strstart - block_start; 648 int dcode; 649 for (dcode = 0; dcode < D_CODES; dcode++) { 650 out_length += (int)dyn_dtree[dcode*2] * 651 (5L+Tree.extra_dbits[dcode]); 652 } 653 out_length >>>= 3; 654 if ((matches < (last_lit/2)) && out_length < in_length/2) return true; 655 } 656 657 return (last_lit == lit_bufsize-1); 658 } 662 663 void compress_block(short[] ltree, short[] dtree){ 665 int dist; int lc; int lx = 0; int code; int extra; 671 if (last_lit != 0){ 672 do{ 673 dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)| 674 (pending_buf[d_buf+lx*2+1]&0xff); 675 lc=(pending_buf[l_buf+lx])&0xff; lx++; 676 677 if(dist == 0){ 678 send_code(lc, ltree); } 680 else{ 681 code = Tree._length_code[lc]; 683 684 send_code(code+LITERALS+1, ltree); extra = Tree.extra_lbits[code]; 686 if(extra != 0){ 687 lc -= Tree.base_length[code]; 688 send_bits(lc, extra); } 690 dist--; code = Tree.d_code(dist); 692 693 send_code(code, dtree); extra = Tree.extra_dbits[code]; 695 if (extra != 0) { 696 dist -= Tree.base_dist[code]; 697 send_bits(dist, extra); } 699 } 701 } 703 while (lx < last_lit); 704 } 705 706 send_code(END_BLOCK, ltree); 707 last_eob_len = ltree[END_BLOCK*2+1]; 708 } 709 710 void set_data_type(){ 715 int n = 0; 716 int ascii_freq = 0; 717 int bin_freq = 0; 718 while(n<7){ bin_freq += dyn_ltree[n*2]; n++;} 719 while(n<128){ ascii_freq += dyn_ltree[n*2]; n++;} 720 while(n<LITERALS){ bin_freq += dyn_ltree[n*2]; n++;} 721 data_type=(byte)(bin_freq > (ascii_freq >>> 2) ? Z_BINARY : Z_ASCII); 722 } 723 724 void bi_flush(){ 726 if (bi_valid == 16) { 727 put_short(bi_buf); 728 bi_buf=0; 729 bi_valid=0; 730 } 731 else if (bi_valid >= 8) { 732 put_byte((byte)bi_buf); 733 bi_buf>>>=8; 734 bi_valid-=8; 735 } 736 } 737 738 void bi_windup(){ 740 if (bi_valid > 8) { 741 put_short(bi_buf); 742 } else if (bi_valid > 0) { 743 put_byte((byte)bi_buf); 744 } 745 bi_buf = 0; 746 bi_valid = 0; 747 } 748 749 void copy_block(int buf, int len, boolean header ){ 755 int index=0; 756 bi_windup(); last_eob_len = 8; 759 if (header) { 760 put_short((short)len); 761 put_short((short)~len); 762 } 763 764 put_byte(window, buf, len); 769 } 770 771 void flush_block_only(boolean eof){ 772 _tr_flush_block(block_start>=0 ? block_start : -1, 773 strstart-block_start, 774 eof); 775 block_start=strstart; 776 strm.flush_pending(); 777 } 778 779 int deflate_stored(int flush){ 787 790 int max_block_size = 0xffff; 791 int max_start; 792 793 if(max_block_size > pending_buf_size - 5) { 794 max_block_size = pending_buf_size - 5; 795 } 796 797 while(true){ 799 if(lookahead<=1){ 801 fill_window(); 802 if(lookahead==0 && flush==Z_NO_FLUSH) return NeedMore; 803 if(lookahead==0) break; } 805 806 strstart+=lookahead; 807 lookahead=0; 808 809 max_start=block_start+max_block_size; 811 if(strstart==0|| strstart>=max_start) { 812 lookahead = (int)(strstart-max_start); 814 strstart = (int)max_start; 815 816 flush_block_only(false); 817 if(strm.avail_out==0) return NeedMore; 818 819 } 820 821 if(strstart-block_start >= w_size-MIN_LOOKAHEAD) { 824 flush_block_only(false); 825 if(strm.avail_out==0) return NeedMore; 826 } 827 } 828 829 flush_block_only(flush == Z_FINISH); 830 if(strm.avail_out==0) 831 return (flush == Z_FINISH) ? FinishStarted : NeedMore; 832 833 return flush == Z_FINISH ? FinishDone : BlockDone; 834 } 835 836 void _tr_stored_block(int buf, int stored_len, boolean eof ){ 841 send_bits((STORED_BLOCK<<1)+(eof?1:0), 3); copy_block(buf, stored_len, true); } 844 845 void _tr_flush_block(int buf, int stored_len, boolean eof ) { 851 int opt_lenb, static_lenb; int max_blindex = 0; 854 if(level > 0) { 856 if(data_type == Z_UNKNOWN) set_data_type(); 858 859 l_desc.build_tree(this); 861 862 d_desc.build_tree(this); 863 864 867 max_blindex=build_bl_tree(); 870 871 opt_lenb=(opt_len+3+7)>>>3; 873 static_lenb=(static_len+3+7)>>>3; 874 875 if(static_lenb<=opt_lenb) opt_lenb=static_lenb; 876 } 877 else { 878 opt_lenb=static_lenb=stored_len+5; } 880 881 if(stored_len+4<=opt_lenb && buf != -1){ 882 _tr_stored_block(buf, stored_len, eof); 889 } 890 else if(static_lenb == opt_lenb){ 891 send_bits((STATIC_TREES<<1)+(eof?1:0), 3); 892 compress_block(StaticTree.static_ltree, StaticTree.static_dtree); 893 } 894 else{ 895 send_bits((DYN_TREES<<1)+(eof?1:0), 3); 896 send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1); 897 compress_block(dyn_ltree, dyn_dtree); 898 } 899 900 903 init_block(); 904 905 if(eof){ 906 bi_windup(); 907 } 908 } 909 910 void fill_window(){ 919 int n, m; 920 int p; 921 int more; 923 do{ 924 more = (window_size-lookahead-strstart); 925 926 if(more==0 && strstart==0 && lookahead==0){ 928 more = w_size; 929 } 930 else if(more==-1) { 931 more--; 934 935 } 938 else if(strstart >= w_size+ w_size-MIN_LOOKAHEAD) { 939 System.arraycopy(window, w_size, window, 0, w_size); 940 match_start-=w_size; 941 strstart-=w_size; block_start-=w_size; 943 944 950 n = hash_size; 951 p=n; 952 do { 953 m = (head[--p]&0xffff); 954 head[p]=(m>=w_size ? (short)(m-w_size) : 0); 955 } 956 while (--n != 0); 957 958 n = w_size; 959 p = n; 960 do { 961 m = (prev[--p]&0xffff); 962 prev[p] = (m >= w_size ? (short)(m-w_size) : 0); 963 } 966 while (--n!=0); 967 more += w_size; 968 } 969 970 if (strm.avail_in == 0) return; 971 972 983 n = strm.read_buf(window, strstart + lookahead, more); 984 lookahead += n; 985 986 if(lookahead >= MIN_MATCH) { 988 ins_h = window[strstart]&0xff; 989 ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask; 990 } 991 } 994 while (lookahead < MIN_LOOKAHEAD && strm.avail_in != 0); 995 } 996 997 int deflate_fast(int flush){ 1003 int hash_head = 0; boolean bflush; 1007 while(true){ 1008 if(lookahead < MIN_LOOKAHEAD){ 1013 fill_window(); 1014 if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH){ 1015 return NeedMore; 1016 } 1017 if(lookahead == 0) break; } 1019 1020 if(lookahead >= MIN_MATCH){ 1023 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask; 1024 1025 hash_head=(head[ins_h]&0xffff); 1027 prev[strstart&w_mask]=head[ins_h]; 1028 head[ins_h]=(short)strstart; 1029 } 1030 1031 1034 if(hash_head!=0L && 1035 ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD 1036 ){ 1037 if(strategy != Z_HUFFMAN_ONLY){ 1041 match_length=longest_match (hash_head); 1042 } 1043 } 1045 if(match_length>=MIN_MATCH){ 1046 1048 bflush=_tr_tally(strstart-match_start, match_length-MIN_MATCH); 1049 1050 lookahead -= match_length; 1051 1052 if(match_length <= max_lazy_match && 1055 lookahead >= MIN_MATCH) { 1056 match_length--; do{ 1058 strstart++; 1059 1060 ins_h=((ins_h<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask; 1061 hash_head=(head[ins_h]&0xffff); 1063 prev[strstart&w_mask]=head[ins_h]; 1064 head[ins_h]=(short)strstart; 1065 1066 } 1069 while (--match_length != 0); 1070 strstart++; 1071 } 1072 else{ 1073 strstart += match_length; 1074 match_length = 0; 1075 ins_h = window[strstart]&0xff; 1076 1077 ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask; 1078 } 1081 } 1082 else { 1083 1085 bflush=_tr_tally(0, window[strstart]&0xff); 1086 lookahead--; 1087 strstart++; 1088 } 1089 if (bflush){ 1090 1091 flush_block_only(false); 1092 if(strm.avail_out==0) return NeedMore; 1093 } 1094 } 1095 1096 flush_block_only(flush == Z_FINISH); 1097 if(strm.avail_out==0){ 1098 if(flush == Z_FINISH) return FinishStarted; 1099 else return NeedMore; 1100 } 1101 return flush==Z_FINISH ? FinishDone : BlockDone; 1102 } 1103 1104 int deflate_slow(int flush){ 1108 int hash_head = 0; boolean bflush; 1112 while(true){ 1114 1119 if (lookahead < MIN_LOOKAHEAD) { 1120 fill_window(); 1121 if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1122 return NeedMore; 1123 } 1124 if(lookahead == 0) break; } 1126 1127 1130 if(lookahead >= MIN_MATCH) { 1131 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff)) & hash_mask; 1132 hash_head=(head[ins_h]&0xffff); 1134 prev[strstart&w_mask]=head[ins_h]; 1135 head[ins_h]=(short)strstart; 1136 } 1137 1138 prev_length = match_length; prev_match = match_start; 1140 match_length = MIN_MATCH-1; 1141 1142 if (hash_head != 0 && prev_length < max_lazy_match && 1143 ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD 1144 ){ 1145 1149 if(strategy != Z_HUFFMAN_ONLY) { 1150 match_length = longest_match(hash_head); 1151 } 1152 1154 if (match_length <= 5 && (strategy == Z_FILTERED || 1155 (match_length == MIN_MATCH && 1156 strstart - match_start > 4096))) { 1157 1158 match_length = MIN_MATCH-1; 1161 } 1162 } 1163 1164 if(prev_length >= MIN_MATCH && match_length <= prev_length) { 1167 int max_insert = strstart + lookahead - MIN_MATCH; 1168 1170 1172 bflush=_tr_tally(strstart-1-prev_match, prev_length - MIN_MATCH); 1173 1174 lookahead -= prev_length-1; 1179 prev_length -= 2; 1180 do{ 1181 if(++strstart <= max_insert) { 1182 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask; 1183 hash_head=(head[ins_h]&0xffff); 1185 prev[strstart&w_mask]=head[ins_h]; 1186 head[ins_h]=(short)strstart; 1187 } 1188 } 1189 while(--prev_length != 0); 1190 match_available = 0; 1191 match_length = MIN_MATCH-1; 1192 strstart++; 1193 1194 if (bflush){ 1195 flush_block_only(false); 1196 if(strm.avail_out==0) return NeedMore; 1197 } 1198 } else if (match_available!=0) { 1199 1200 1204 bflush=_tr_tally(0, window[strstart-1]&0xff); 1205 1206 if (bflush) { 1207 flush_block_only(false); 1208 } 1209 strstart++; 1210 lookahead--; 1211 if(strm.avail_out == 0) return NeedMore; 1212 } else { 1213 1216 match_available = 1; 1217 strstart++; 1218 lookahead--; 1219 } 1220 } 1221 1222 if(match_available!=0) { 1223 bflush=_tr_tally(0, window[strstart-1]&0xff); 1224 match_available = 0; 1225 } 1226 flush_block_only(flush == Z_FINISH); 1227 1228 if(strm.avail_out==0){ 1229 if(flush == Z_FINISH) return FinishStarted; 1230 else return NeedMore; 1231 } 1232 1233 return flush == Z_FINISH ? FinishDone : BlockDone; 1234 } 1235 1236 int longest_match(int cur_match){ 1237 int chain_length = max_chain_length; int scan = strstart; int match; int len; int best_len = prev_length; int limit = strstart>(w_size-MIN_LOOKAHEAD) ? 1243 strstart-(w_size-MIN_LOOKAHEAD) : 0; 1244 int nice_match=this.nice_match; 1245 1246 1249 int wmask = w_mask; 1250 1251 int strend = strstart + MAX_MATCH; 1252 byte scan_end1 = window[scan+best_len-1]; 1253 byte scan_end = window[scan+best_len]; 1254 1255 1258 if (prev_length >= good_match) { 1260 chain_length >>= 2; 1261 } 1262 1263 if (nice_match > lookahead) nice_match = lookahead; 1266 1267 do { 1268 match = cur_match; 1269 1270 if (window[match+best_len] != scan_end || 1273 window[match+best_len-1] != scan_end1 || 1274 window[match] != window[scan] || 1275 window[++match] != window[scan+1]) continue; 1276 1277 scan += 2; match++; 1283 1284 do { 1287 } while (window[++scan] == window[++match] && 1288 window[++scan] == window[++match] && 1289 window[++scan] == window[++match] && 1290 window[++scan] == window[++match] && 1291 window[++scan] == window[++match] && 1292 window[++scan] == window[++match] && 1293 window[++scan] == window[++match] && 1294 window[++scan] == window[++match] && 1295 scan < strend); 1296 1297 len = MAX_MATCH - (int)(strend - scan); 1298 scan = strend - MAX_MATCH; 1299 1300 if(len>best_len) { 1301 match_start = cur_match; 1302 best_len = len; 1303 if (len >= nice_match) break; 1304 scan_end1 = window[scan+best_len-1]; 1305 scan_end = window[scan+best_len]; 1306 } 1307 1308 } while ((cur_match = (prev[cur_match & wmask]&0xffff)) > limit 1309 && --chain_length != 0); 1310 1311 if (best_len <= lookahead) return best_len; 1312 return lookahead; 1313 } 1314 1315 int deflateInit(ZStream strm, int level, int bits){ 1316 return deflateInit2(strm, level, Z_DEFLATED, bits, DEF_MEM_LEVEL, 1317 Z_DEFAULT_STRATEGY); 1318 } 1319 int deflateInit(ZStream strm, int level){ 1320 return deflateInit(strm, level, MAX_WBITS); 1321 } 1322 int deflateInit2(ZStream strm, int level, int method, int windowBits, 1323 int memLevel, int strategy){ 1324 int noheader = 0; 1325 1327 1333 strm.msg = null; 1334 1335 if (level == Z_DEFAULT_COMPRESSION) level = 6; 1336 1337 if (windowBits < 0) { noheader = 1; 1339 windowBits = -windowBits; 1340 } 1341 1342 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || 1343 method != Z_DEFLATED || 1344 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || 1345 strategy < 0 || strategy > Z_HUFFMAN_ONLY) { 1346 return Z_STREAM_ERROR; 1347 } 1348 1349 strm.dstate = (Deflate)this; 1350 1351 this.noheader = noheader; 1352 w_bits = windowBits; 1353 w_size = 1 << w_bits; 1354 w_mask = w_size - 1; 1355 1356 hash_bits = memLevel + 7; 1357 hash_size = 1 << hash_bits; 1358 hash_mask = hash_size - 1; 1359 hash_shift = ((hash_bits+MIN_MATCH-1)/MIN_MATCH); 1360 1361 window = new byte[w_size*2]; 1362 prev = new short[w_size]; 1363 head = new short[hash_size]; 1364 1365 lit_bufsize = 1 << (memLevel + 6); 1367 pending_buf = new byte[lit_bufsize*4]; 1370 pending_buf_size = lit_bufsize*4; 1371 1372 d_buf = lit_bufsize/2; 1373 l_buf = (1+2)*lit_bufsize; 1374 1375 this.level = level; 1376 1377 1379 this.strategy = strategy; 1380 this.method = (byte)method; 1381 1382 return deflateReset(strm); 1383 } 1384 1385 int deflateReset(ZStream strm){ 1386 strm.total_in = strm.total_out = 0; 1387 strm.msg = null; strm.data_type = Z_UNKNOWN; 1389 1390 pending = 0; 1391 pending_out = 0; 1392 1393 if(noheader < 0) { 1394 noheader = 0; } 1396 status = (noheader!=0) ? BUSY_STATE : INIT_STATE; 1397 strm.adler=strm._adler.adler32(0, null, 0, 0); 1398 1399 last_flush = Z_NO_FLUSH; 1400 1401 tr_init(); 1402 lm_init(); 1403 return Z_OK; 1404 } 1405 1406 int deflateEnd(){ 1407 if(status!=INIT_STATE && status!=BUSY_STATE && status!=FINISH_STATE){ 1408 return Z_STREAM_ERROR; 1409 } 1410 pending_buf=null; 1412 head=null; 1413 prev=null; 1414 window=null; 1415 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 1418 } 1419 1420 int deflateParams(ZStream strm, int _level, int _strategy){ 1421 int err=Z_OK; 1422 1423 if(_level == Z_DEFAULT_COMPRESSION){ 1424 _level = 6; 1425 } 1426 if(_level < 0 || _level > 9 || 1427 _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) { 1428 return Z_STREAM_ERROR; 1429 } 1430 1431 if(config_table[level].func!=config_table[_level].func && 1432 strm.total_in != 0) { 1433 err = strm.deflate(Z_PARTIAL_FLUSH); 1435 } 1436 1437 if(level != _level) { 1438 level = _level; 1439 max_lazy_match = config_table[level].max_lazy; 1440 good_match = config_table[level].good_length; 1441 nice_match = config_table[level].nice_length; 1442 max_chain_length = config_table[level].max_chain; 1443 } 1444 strategy = _strategy; 1445 return err; 1446 } 1447 1448 int deflateSetDictionary (ZStream strm, byte[] dictionary, int dictLength){ 1449 int length = dictLength; 1450 int index=0; 1451 1452 if(dictionary == null || status != INIT_STATE) 1453 return Z_STREAM_ERROR; 1454 1455 strm.adler=strm._adler.adler32(strm.adler, dictionary, 0, dictLength); 1456 1457 if(length < MIN_MATCH) return Z_OK; 1458 if(length > w_size-MIN_LOOKAHEAD){ 1459 length = w_size-MIN_LOOKAHEAD; 1460 index=dictLength-length; } 1462 System.arraycopy(dictionary, index, window, 0, length); 1463 strstart = length; 1464 block_start = length; 1465 1466 1470 ins_h = window[0]&0xff; 1471 ins_h=(((ins_h)<<hash_shift)^(window[1]&0xff))&hash_mask; 1472 1473 for(int n=0; n<=length-MIN_MATCH; n++){ 1474 ins_h=(((ins_h)<<hash_shift)^(window[(n)+(MIN_MATCH-1)]&0xff))&hash_mask; 1475 prev[n&w_mask]=head[ins_h]; 1476 head[ins_h]=(short)n; 1477 } 1478 return Z_OK; 1479 } 1480 1481 int deflate(ZStream strm, int flush){ 1482 int old_flush; 1483 1484 if(flush>Z_FINISH || flush<0){ 1485 return Z_STREAM_ERROR; 1486 } 1487 1488 if(strm.next_out == null || 1489 (strm.next_in == null && strm.avail_in != 0) || 1490 (status == FINISH_STATE && flush != Z_FINISH)) { 1491 strm.msg=z_errmsg[Z_NEED_DICT-(Z_STREAM_ERROR)]; 1492 return Z_STREAM_ERROR; 1493 } 1494 if(strm.avail_out == 0){ 1495 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)]; 1496 return Z_BUF_ERROR; 1497 } 1498 1499 this.strm = strm; old_flush = last_flush; 1501 last_flush = flush; 1502 1503 if(status == INIT_STATE) { 1505 int header = (Z_DEFLATED+((w_bits-8)<<4))<<8; 1506 int level_flags=((level-1)&0xff)>>1; 1507 1508 if(level_flags>3) level_flags=3; 1509 header |= (level_flags<<6); 1510 if(strstart!=0) header |= PRESET_DICT; 1511 header+=31-(header % 31); 1512 1513 status=BUSY_STATE; 1514 putShortMSB(header); 1515 1516 1517 if(strstart!=0){ 1519 putShortMSB((int)(strm.adler>>>16)); 1520 putShortMSB((int)(strm.adler&0xffff)); 1521 } 1522 strm.adler=strm._adler.adler32(0, null, 0, 0); 1523 } 1524 1525 if(pending != 0) { 1527 strm.flush_pending(); 1528 if(strm.avail_out == 0) { 1529 last_flush = -1; 1536 return Z_OK; 1537 } 1538 1539 } 1543 else if(strm.avail_in==0 && flush <= old_flush && 1544 flush != Z_FINISH) { 1545 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)]; 1546 return Z_BUF_ERROR; 1547 } 1548 1549 if(status == FINISH_STATE && strm.avail_in != 0) { 1551 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)]; 1552 return Z_BUF_ERROR; 1553 } 1554 1555 if(strm.avail_in!=0 || lookahead!=0 || 1557 (flush != Z_NO_FLUSH && status != FINISH_STATE)) { 1558 int bstate=-1; 1559 switch(config_table[level].func){ 1560 case STORED: 1561 bstate = deflate_stored(flush); 1562 break; 1563 case FAST: 1564 bstate = deflate_fast(flush); 1565 break; 1566 case SLOW: 1567 bstate = deflate_slow(flush); 1568 break; 1569 default: 1570 } 1571 1572 if (bstate==FinishStarted || bstate==FinishDone) { 1573 status = FINISH_STATE; 1574 } 1575 if (bstate==NeedMore || bstate==FinishStarted) { 1576 if(strm.avail_out == 0) { 1577 last_flush = -1; } 1579 return Z_OK; 1580 } 1587 1588 if (bstate==BlockDone) { 1589 if(flush == Z_PARTIAL_FLUSH) { 1590 _tr_align(); 1591 } 1592 else { _tr_stored_block(0, 0, false); 1594 if(flush == Z_FULL_FLUSH) { 1597 for(int i=0; i<hash_size; i++) head[i]=0; 1600 } 1601 } 1602 strm.flush_pending(); 1603 if(strm.avail_out == 0) { 1604 last_flush = -1; return Z_OK; 1606 } 1607 } 1608 } 1609 1610 if(flush!=Z_FINISH) return Z_OK; 1611 if(noheader!=0) return Z_STREAM_END; 1612 1613 putShortMSB((int)(strm.adler>>>16)); 1615 putShortMSB((int)(strm.adler&0xffff)); 1616 strm.flush_pending(); 1617 1618 noheader = -1; return pending != 0 ? Z_OK : Z_STREAM_END; 1622 } 1623} 1624 | Popular Tags |