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