1 2 29 34 35 package com.jcraft.jzlib; 36 37 final class Tree{ 38 static final private int MAX_BITS=15; 39 static final private int BL_CODES=19; 40 static final private int D_CODES=30; 41 static final private int LITERALS=256; 42 static final private int LENGTH_CODES=29; 43 static final private int L_CODES=(LITERALS+1+LENGTH_CODES); 44 static final private int HEAP_SIZE=(2*L_CODES+1); 45 46 static final int MAX_BL_BITS=7; 48 49 static final int END_BLOCK=256; 51 52 static final int REP_3_6=16; 54 55 static final int REPZ_3_10=17; 57 58 static final int REPZ_11_138=18; 60 61 static final int[] extra_lbits={ 63 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0 64 }; 65 66 static final int[] extra_dbits={ 68 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 69 }; 70 71 static final int[] extra_blbits={ 73 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7 74 }; 75 76 static final byte[] bl_order={ 77 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; 78 79 80 84 static final int Buf_size=8*2; 85 86 static final int DIST_CODE_LEN=512; 88 89 static final byte[] _dist_code = { 90 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 91 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 92 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 93 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 94 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 95 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 96 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 97 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 98 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 99 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 100 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 101 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 102 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, 103 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 104 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 105 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 106 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 107 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 108 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 109 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 110 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 111 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 112 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 113 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 114 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 115 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 116 }; 117 118 static final byte[] _length_code={ 119 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 120 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 121 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 122 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 123 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 124 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 125 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 126 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 127 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 128 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 129 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 130 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 131 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 132 }; 133 134 static final int[] base_length = { 135 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 136 64, 80, 96, 112, 128, 160, 192, 224, 0 137 }; 138 139 static final int[] base_dist = { 140 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 141 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 142 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 143 }; 144 145 static int d_code(int dist){ 149 return ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>>7)]); 150 } 151 152 short[] dyn_tree; int max_code; StaticTree stat_desc; 156 void gen_bitlen(Deflate s){ 165 short[] tree = dyn_tree; 166 short[] stree = stat_desc.static_tree; 167 int[] extra = stat_desc.extra_bits; 168 int base = stat_desc.extra_base; 169 int max_length = stat_desc.max_length; 170 int h; int n, m; int bits; int xbits; short f; int overflow = 0; 177 for (bits = 0; bits <= MAX_BITS; bits++) s.bl_count[bits] = 0; 178 179 tree[s.heap[s.heap_max]*2+1] = 0; 183 for(h=s.heap_max+1; h<HEAP_SIZE; h++){ 184 n = s.heap[h]; 185 bits = tree[tree[n*2+1]*2+1] + 1; 186 if (bits > max_length){ bits = max_length; overflow++; } 187 tree[n*2+1] = (short)bits; 188 190 if (n > max_code) continue; 192 s.bl_count[bits]++; 193 xbits = 0; 194 if (n >= base) xbits = extra[n-base]; 195 f = tree[n*2]; 196 s.opt_len += f * (bits + xbits); 197 if (stree!=null) s.static_len += f * (stree[n*2+1] + xbits); 198 } 199 if (overflow == 0) return; 200 201 do { 204 bits = max_length-1; 205 while(s.bl_count[bits]==0) bits--; 206 s.bl_count[bits]--; s.bl_count[bits+1]+=2; s.bl_count[max_length]--; 209 overflow -= 2; 212 } 213 while (overflow > 0); 214 215 for (bits = max_length; bits != 0; bits--) { 216 n = s.bl_count[bits]; 217 while (n != 0) { 218 m = s.heap[--h]; 219 if (m > max_code) continue; 220 if (tree[m*2+1] != bits) { 221 s.opt_len += ((long)bits - (long)tree[m*2+1])*(long)tree[m*2]; 222 tree[m*2+1] = (short)bits; 223 } 224 n--; 225 } 226 } 227 } 228 229 void build_tree(Deflate s){ 236 short[] tree=dyn_tree; 237 short[] stree=stat_desc.static_tree; 238 int elems=stat_desc.elems; 239 int n, m; int max_code=-1; int node; 243 s.heap_len = 0; 247 s.heap_max = HEAP_SIZE; 248 249 for(n=0; n<elems; n++) { 250 if(tree[n*2] != 0) { 251 s.heap[++s.heap_len] = max_code = n; 252 s.depth[n] = 0; 253 } 254 else{ 255 tree[n*2+1] = 0; 256 } 257 } 258 259 while (s.heap_len < 2) { 264 node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0); 265 tree[node*2] = 1; 266 s.depth[node] = 0; 267 s.opt_len--; if (stree!=null) s.static_len -= stree[node*2+1]; 268 } 270 this.max_code = max_code; 271 272 275 for(n=s.heap_len/2;n>=1; n--) 276 s.pqdownheap(tree, n); 277 278 281 node=elems; do{ 283 n=s.heap[1]; 285 s.heap[1]=s.heap[s.heap_len--]; 286 s.pqdownheap(tree, 1); 287 m=s.heap[1]; 289 s.heap[--s.heap_max] = n; s.heap[--s.heap_max] = m; 291 292 tree[node*2] = (short)(tree[n*2] + tree[m*2]); 294 s.depth[node] = (byte)(Math.max(s.depth[n],s.depth[m])+1); 295 tree[n*2+1] = tree[m*2+1] = (short)node; 296 297 s.heap[1] = node++; 299 s.pqdownheap(tree, 1); 300 } 301 while(s.heap_len>=2); 302 303 s.heap[--s.heap_max] = s.heap[1]; 304 305 308 gen_bitlen(s); 309 310 gen_codes(tree, max_code, s.bl_count); 312 } 313 314 static void gen_codes(short[] tree, int max_code, short[] bl_count ){ 324 short[] next_code=new short[MAX_BITS+1]; short code = 0; int bits; int n; 329 for (bits = 1; bits <= MAX_BITS; bits++) { 332 next_code[bits] = code = (short)((code + bl_count[bits-1]) << 1); 333 } 334 335 341 for (n = 0; n <= max_code; n++) { 342 int len = tree[n*2+1]; 343 if (len == 0) continue; 344 tree[n*2] = (short)(bi_reverse(next_code[len]++, len)); 346 } 347 } 348 349 static int bi_reverse(int code, int len ){ 355 int res = 0; 356 do{ 357 res|=code&1; 358 code>>>=1; 359 res<<=1; 360 } 361 while(--len>0); 362 return res>>>1; 363 } 364 } 365 366 | Popular Tags |