1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 package org.jboss.netty.util.internal.jzlib;
49
50 final class Tree {
51
52 static final int[] extra_lbits = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
53 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
54
55
56 static final int[] extra_dbits = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5,
57 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
58
59
60 static final int[] extra_blbits = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 2, 3, 7 };
62
63 static final byte[] bl_order = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4,
64 12, 3, 13, 2, 14, 1, 15 };
65
66 static final byte[] _dist_code = { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7,
67 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10,
68 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
69 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12,
70 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
71 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13,
72 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
73 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14,
74 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
75 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
76 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
77 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
78 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
79 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
80 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
81 15, 0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
82 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24,
83 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25,
84 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26,
85 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
86 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27,
87 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
88 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28,
89 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
90 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
91 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
92 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
93 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
94 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
95 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
96 29, 29 };
97
98 static final byte[] _length_code = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9,
99 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
100 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17,
101 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19,
102 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
103 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,
104 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23,
105 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24,
106 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
107 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
108 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
109 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26,
110 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
111 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27,
112 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
113 27, 27, 27, 27, 27, 28 };
114
115 static final int[] base_length = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14,
116 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192,
117 224, 0 };
118
119 static final int[] base_dist = { 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48,
120 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096,
121 6144, 8192, 12288, 16384, 24576 };
122
123
124
125
126 static int d_code(int dist) {
127 return dist < 256? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
128 }
129
130 short[] dyn_tree;
131 int max_code;
132 StaticTree stat_desc;
133
134
135
136
137
138
139
140
141
142 private void gen_bitlen(Deflate s) {
143 short[] tree = dyn_tree;
144 short[] stree = stat_desc.static_tree;
145 int[] extra = stat_desc.extra_bits;
146 int base = stat_desc.extra_base;
147 int max_length = stat_desc.max_length;
148 int h;
149 int n, m;
150 int bits;
151 int xbits;
152 short f;
153 int overflow = 0;
154
155 for (bits = 0; bits <= JZlib.MAX_BITS; bits ++) {
156 s.bl_count[bits] = 0;
157 }
158
159
160
161 tree[s.heap[s.heap_max] * 2 + 1] = 0;
162
163 for (h = s.heap_max + 1; h < JZlib.HEAP_SIZE; h ++) {
164 n = s.heap[h];
165 bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
166 if (bits > max_length) {
167 bits = max_length;
168 overflow ++;
169 }
170 tree[n * 2 + 1] = (short) bits;
171
172
173 if (n > max_code) {
174 continue;
175 }
176
177 s.bl_count[bits] ++;
178 xbits = 0;
179 if (n >= base) {
180 xbits = extra[n - base];
181 }
182 f = tree[n * 2];
183 s.opt_len += f * (bits + xbits);
184 if (stree != null) {
185 s.static_len += f * (stree[n * 2 + 1] + xbits);
186 }
187 }
188 if (overflow == 0) {
189 return;
190 }
191
192
193
194 do {
195 bits = max_length - 1;
196 while (s.bl_count[bits] == 0) {
197 bits --;
198 }
199 s.bl_count[bits] --;
200 s.bl_count[bits + 1] += 2;
201 s.bl_count[max_length] --;
202
203
204 overflow -= 2;
205 } while (overflow > 0);
206
207 for (bits = max_length; bits != 0; bits --) {
208 n = s.bl_count[bits];
209 while (n != 0) {
210 m = s.heap[-- h];
211 if (m > max_code) {
212 continue;
213 }
214 if (tree[m * 2 + 1] != bits) {
215 s.opt_len += ((long) bits - (long) tree[m * 2 + 1]) *
216 tree[m * 2];
217 tree[m * 2 + 1] = (short) bits;
218 }
219 n --;
220 }
221 }
222 }
223
224
225
226
227
228
229
230 void build_tree(Deflate s) {
231 short[] tree = dyn_tree;
232 short[] stree = stat_desc.static_tree;
233 int elems = stat_desc.elems;
234 int n, m;
235 int max_code = -1;
236 int node;
237
238
239
240
241 s.heap_len = 0;
242 s.heap_max = JZlib.HEAP_SIZE;
243
244 for (n = 0; n < elems; n ++) {
245 if (tree[n * 2] != 0) {
246 s.heap[++ s.heap_len] = max_code = n;
247 s.depth[n] = 0;
248 } else {
249 tree[n * 2 + 1] = 0;
250 }
251 }
252
253
254
255
256
257 while (s.heap_len < 2) {
258 node = s.heap[++ s.heap_len] = max_code < 2? ++ max_code : 0;
259 tree[node * 2] = 1;
260 s.depth[node] = 0;
261 s.opt_len --;
262 if (stree != null) {
263 s.static_len -= stree[node * 2 + 1];
264
265 }
266 }
267 this.max_code = max_code;
268
269
270
271
272 for (n = s.heap_len / 2; n >= 1; n --) {
273 s.pqdownheap(tree, n);
274 }
275
276
277
278
279 node = elems;
280 do {
281
282 n = s.heap[1];
283 s.heap[1] = s.heap[s.heap_len --];
284 s.pqdownheap(tree, 1);
285 m = s.heap[1];
286
287 s.heap[-- s.heap_max] = n;
288 s.heap[-- s.heap_max] = m;
289
290
291 tree[node * 2] = (short) (tree[n * 2] + tree[m * 2]);
292 s.depth[node] = (byte) (Math.max(s.depth[n], s.depth[m]) + 1);
293 tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
294
295
296 s.heap[1] = node ++;
297 s.pqdownheap(tree, 1);
298 } while (s.heap_len >= 2);
299
300 s.heap[-- s.heap_max] = s.heap[1];
301
302
303
304
305 gen_bitlen(s);
306
307
308 gen_codes(tree, max_code, s.bl_count);
309 }
310
311
312
313
314
315
316
317 private static void gen_codes(short[] tree,
318 int max_code,
319 short[] bl_count
320 ) {
321 short[] next_code = new short[JZlib.MAX_BITS + 1];
322 short code = 0;
323 int bits;
324 int n;
325
326
327
328 for (bits = 1; bits <= JZlib.MAX_BITS; bits ++) {
329 next_code[bits] = code = (short) (code + bl_count[bits - 1] << 1);
330 }
331
332
333
334
335
336
337
338 for (n = 0; n <= max_code; n ++) {
339 int len = tree[n * 2 + 1];
340 if (len == 0) {
341 continue;
342 }
343
344 tree[n * 2] = (short) bi_reverse(next_code[len] ++, len);
345 }
346 }
347
348
349
350
351 private static int bi_reverse(int code,
352 int len
353 ) {
354 int res = 0;
355 do {
356 res |= code & 1;
357 code >>>= 1;
358 res <<= 1;
359 } while (-- len > 0);
360 return res >>> 1;
361 }
362 }