Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / Deflate.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["JU.Tree"], "JU.Deflate", ["JU.CRC32", "$.GZIPHeader", "$.StaticTree"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.strm = null;
5 this.status = 0;
6 this.pending_buf = null;
7 this.pending_buf_size = 0;
8 this.pending_out = 0;
9 this.pending = 0;
10 this.wrap = 1;
11 this.data_type = 0;
12 this.method = 0;
13 this.last_flush = 0;
14 this.w_size = 0;
15 this.w_bits = 0;
16 this.w_mask = 0;
17 this.window = null;
18 this.window_size = 0;
19 this.prev = null;
20 this.head = null;
21 this.ins_h = 0;
22 this.hash_size = 0;
23 this.hash_bits = 0;
24 this.hash_mask = 0;
25 this.hash_shift = 0;
26 this.block_start = 0;
27 this.match_length = 0;
28 this.prev_match = 0;
29 this.match_available = 0;
30 this.strstart = 0;
31 this.match_start = 0;
32 this.lookahead = 0;
33 this.prev_length = 0;
34 this.max_chain_length = 0;
35 this.max_lazy_match = 0;
36 this.level = 0;
37 this.strategy = 0;
38 this.good_match = 0;
39 this.nice_match = 0;
40 this.dyn_ltree = null;
41 this.dyn_dtree = null;
42 this.bl_tree = null;
43 this.l_desc = null;
44 this.d_desc = null;
45 this.bl_desc = null;
46 this.bl_count = null;
47 this.heap = null;
48 this.heap_len = 0;
49 this.heap_max = 0;
50 this.depth = null;
51 this.l_buf = 0;
52 this.lit_bufsize = 0;
53 this.last_lit = 0;
54 this.d_buf = 0;
55 this.opt_len = 0;
56 this.static_len = 0;
57 this.matches = 0;
58 this.last_eob_len = 0;
59 this.bi_buf = 0;
60 this.bi_valid = 0;
61 this.gheader = null;
62 Clazz.instantialize (this, arguments);
63 }, JU, "Deflate");
64 Clazz.prepareFields (c$, function () {
65 this.l_desc =  new JU.Tree ();
66 this.d_desc =  new JU.Tree ();
67 this.bl_desc =  new JU.Tree ();
68 this.bl_count =  Clazz.newShortArray (16, 0);
69 this.heap =  Clazz.newIntArray (573, 0);
70 this.depth =  Clazz.newByteArray (573, 0);
71 });
72 Clazz.makeConstructor (c$, 
73 function (strm) {
74 this.strm = strm;
75 this.dyn_ltree =  Clazz.newShortArray (1146, 0);
76 this.dyn_dtree =  Clazz.newShortArray (122, 0);
77 this.bl_tree =  Clazz.newShortArray (78, 0);
78 }, "JU.ZStream");
79 Clazz.defineMethod (c$, "deflateInit", 
80 function (level) {
81 return this.deflateInit2 (level, 15);
82 }, "~N");
83 Clazz.defineMethod (c$, "deflateInit2", 
84 function (level, bits) {
85 return this.deflateInit5 (level, 8, bits, 8, 0);
86 }, "~N,~N");
87 Clazz.defineMethod (c$, "deflateInit3", 
88 function (level, bits, memlevel) {
89 return this.deflateInit5 (level, 8, bits, memlevel, 0);
90 }, "~N,~N,~N");
91 Clazz.defineMethod (c$, "lm_init", 
92 function () {
93 this.window_size = 2 * this.w_size;
94 this.head[this.hash_size - 1] = 0;
95 for (var i = 0; i < this.hash_size - 1; i++) {
96 this.head[i] = 0;
97 }
98 this.max_lazy_match = JU.Deflate.config_table[this.level].max_lazy;
99 this.good_match = JU.Deflate.config_table[this.level].good_length;
100 this.nice_match = JU.Deflate.config_table[this.level].nice_length;
101 this.max_chain_length = JU.Deflate.config_table[this.level].max_chain;
102 this.strstart = 0;
103 this.block_start = 0;
104 this.lookahead = 0;
105 this.match_length = this.prev_length = 2;
106 this.match_available = 0;
107 this.ins_h = 0;
108 });
109 Clazz.defineMethod (c$, "tr_init", 
110 function () {
111 this.l_desc.dyn_tree = this.dyn_ltree;
112 this.l_desc.stat_desc = JU.StaticTree.static_l_desc;
113 this.d_desc.dyn_tree = this.dyn_dtree;
114 this.d_desc.stat_desc = JU.StaticTree.static_d_desc;
115 this.bl_desc.dyn_tree = this.bl_tree;
116 this.bl_desc.stat_desc = JU.StaticTree.static_bl_desc;
117 this.bi_buf = 0;
118 this.bi_valid = 0;
119 this.last_eob_len = 8;
120 this.init_block ();
121 });
122 Clazz.defineMethod (c$, "init_block", 
123 function () {
124 for (var i = 0; i < 286; i++) this.dyn_ltree[i * 2] = 0;
125
126 for (var i = 0; i < 30; i++) this.dyn_dtree[i * 2] = 0;
127
128 for (var i = 0; i < 19; i++) this.bl_tree[i * 2] = 0;
129
130 this.dyn_ltree[512] = 1;
131 this.opt_len = this.static_len = 0;
132 this.last_lit = this.matches = 0;
133 });
134 Clazz.defineMethod (c$, "pqdownheap", 
135 function (tree, k) {
136 var v = this.heap[k];
137 var j = k << 1;
138 while (j <= this.heap_len) {
139 if (j < this.heap_len && JU.Deflate.smaller (tree, this.heap[j + 1], this.heap[j], this.depth)) {
140 j++;
141 }if (JU.Deflate.smaller (tree, v, this.heap[j], this.depth)) break;
142 this.heap[k] = this.heap[j];
143 k = j;
144 j <<= 1;
145 }
146 this.heap[k] = v;
147 }, "~A,~N");
148 c$.smaller = Clazz.defineMethod (c$, "smaller", 
149 function (tree, n, m, depth) {
150 var tn2 = tree[n * 2];
151 var tm2 = tree[m * 2];
152 return (tn2 < tm2 || (tn2 == tm2 && depth[n] <= depth[m]));
153 }, "~A,~N,~N,~A");
154 Clazz.defineMethod (c$, "scan_tree", 
155 function (tree, max_code) {
156 var n;
157 var prevlen = -1;
158 var curlen;
159 var nextlen = tree[1];
160 var count = 0;
161 var max_count = 7;
162 var min_count = 4;
163 if (nextlen == 0) {
164 max_count = 138;
165 min_count = 3;
166 }tree[(max_code + 1) * 2 + 1] = 0xffff;
167 for (n = 0; n <= max_code; n++) {
168 curlen = nextlen;
169 nextlen = tree[(n + 1) * 2 + 1];
170 if (++count < max_count && curlen == nextlen) {
171 continue;
172 } else if (count < min_count) {
173 this.bl_tree[curlen * 2] += count;
174 } else if (curlen != 0) {
175 if (curlen != prevlen) this.bl_tree[curlen * 2]++;
176 this.bl_tree[32]++;
177 } else if (count <= 10) {
178 this.bl_tree[34]++;
179 } else {
180 this.bl_tree[36]++;
181 }count = 0;
182 prevlen = curlen;
183 if (nextlen == 0) {
184 max_count = 138;
185 min_count = 3;
186 } else if (curlen == nextlen) {
187 max_count = 6;
188 min_count = 3;
189 } else {
190 max_count = 7;
191 min_count = 4;
192 }}
193 }, "~A,~N");
194 Clazz.defineMethod (c$, "build_bl_tree", 
195 function () {
196 var max_blindex;
197 this.scan_tree (this.dyn_ltree, this.l_desc.max_code);
198 this.scan_tree (this.dyn_dtree, this.d_desc.max_code);
199 this.bl_desc.build_tree (this);
200 for (max_blindex = 18; max_blindex >= 3; max_blindex--) {
201 if (this.bl_tree[JU.Tree.bl_order[max_blindex] * 2 + 1] != 0) break;
202 }
203 this.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
204 return max_blindex;
205 });
206 Clazz.defineMethod (c$, "send_all_trees", 
207 function (lcodes, dcodes, blcodes) {
208 var rank;
209 this.send_bits (lcodes - 257, 5);
210 this.send_bits (dcodes - 1, 5);
211 this.send_bits (blcodes - 4, 4);
212 for (rank = 0; rank < blcodes; rank++) {
213 this.send_bits (this.bl_tree[JU.Tree.bl_order[rank] * 2 + 1], 3);
214 }
215 this.send_tree (this.dyn_ltree, lcodes - 1);
216 this.send_tree (this.dyn_dtree, dcodes - 1);
217 }, "~N,~N,~N");
218 Clazz.defineMethod (c$, "send_tree", 
219 function (tree, max_code) {
220 var n;
221 var prevlen = -1;
222 var curlen;
223 var nextlen = tree[1];
224 var count = 0;
225 var max_count = 7;
226 var min_count = 4;
227 if (nextlen == 0) {
228 max_count = 138;
229 min_count = 3;
230 }for (n = 0; n <= max_code; n++) {
231 curlen = nextlen;
232 nextlen = tree[(n + 1) * 2 + 1];
233 if (++count < max_count && curlen == nextlen) {
234 continue;
235 } else if (count < min_count) {
236 do {
237 this.send_code (curlen, this.bl_tree);
238 } while (--count != 0);
239 } else if (curlen != 0) {
240 if (curlen != prevlen) {
241 this.send_code (curlen, this.bl_tree);
242 count--;
243 }this.send_code (16, this.bl_tree);
244 this.send_bits (count - 3, 2);
245 } else if (count <= 10) {
246 this.send_code (17, this.bl_tree);
247 this.send_bits (count - 3, 3);
248 } else {
249 this.send_code (18, this.bl_tree);
250 this.send_bits (count - 11, 7);
251 }count = 0;
252 prevlen = curlen;
253 if (nextlen == 0) {
254 max_count = 138;
255 min_count = 3;
256 } else if (curlen == nextlen) {
257 max_count = 6;
258 min_count = 3;
259 } else {
260 max_count = 7;
261 min_count = 4;
262 }}
263 }, "~A,~N");
264 Clazz.defineMethod (c$, "put_byte", 
265 function (p, start, len) {
266 System.arraycopy (p, start, this.pending_buf, this.pending, len);
267 this.pending += len;
268 }, "~A,~N,~N");
269 Clazz.defineMethod (c$, "put_byteB", 
270 function (c) {
271 {
272 this.pending_buf[this.pending++] = c&0xff;
273 }}, "~N");
274 Clazz.defineMethod (c$, "put_short", 
275 function (w) {
276 this.put_byteB ((w));
277 this.put_byteB ((w >>> 8));
278 }, "~N");
279 Clazz.defineMethod (c$, "putShortMSB", 
280 function (b) {
281 this.put_byteB ((b >> 8));
282 this.put_byteB ((b));
283 }, "~N");
284 Clazz.defineMethod (c$, "send_code", 
285 function (c, tree) {
286 var c2 = c * 2;
287 this.send_bits ((tree[c2] & 0xffff), (tree[c2 + 1] & 0xffff));
288 }, "~N,~A");
289 Clazz.defineMethod (c$, "send_bits", 
290 function (value, length) {
291 var len = length;
292 if (this.bi_valid > 16 - len) {
293 var val = value;
294 this.bi_buf |= ((val << this.bi_valid) & 0xffff);
295 this.put_short (this.bi_buf);
296 this.bi_buf = ((val >>> (16 - this.bi_valid)) & 0xffff);
297 this.bi_valid += len - 16;
298 } else {
299 this.bi_buf |= (((value) << this.bi_valid) & 0xffff);
300 this.bi_valid += len;
301 }}, "~N,~N");
302 Clazz.defineMethod (c$, "_tr_align", 
303 function () {
304 this.send_bits (2, 3);
305 this.send_code (256, JU.StaticTree.static_ltree);
306 this.bi_flush ();
307 if (1 + this.last_eob_len + 10 - this.bi_valid < 9) {
308 this.send_bits (2, 3);
309 this.send_code (256, JU.StaticTree.static_ltree);
310 this.bi_flush ();
311 }this.last_eob_len = 7;
312 });
313 Clazz.defineMethod (c$, "_tr_tally", 
314 function (dist, lc) {
315 this.pending_buf[this.d_buf + this.last_lit * 2] = (dist >>> 8);
316 this.pending_buf[this.d_buf + this.last_lit * 2 + 1] = dist;
317 this.pending_buf[this.l_buf + this.last_lit] = lc;
318 this.last_lit++;
319 if (dist == 0) {
320 this.dyn_ltree[lc * 2]++;
321 } else {
322 this.matches++;
323 dist--;
324 this.dyn_ltree[(JU.Tree._length_code[lc] + 256 + 1) * 2]++;
325 this.dyn_dtree[JU.Tree.d_code (dist) * 2]++;
326 }if ((this.last_lit & 0x1fff) == 0 && this.level > 2) {
327 var out_length = this.last_lit * 8;
328 var in_length = this.strstart - this.block_start;
329 var dcode;
330 for (dcode = 0; dcode < 30; dcode++) {
331 out_length += this.dyn_dtree[dcode * 2] * (5 + JU.Tree.extra_dbits[dcode]);
332 }
333 out_length >>>= 3;
334 if ((this.matches < (Clazz.doubleToInt (this.last_lit / 2))) && out_length < Clazz.doubleToInt (in_length / 2)) return true;
335 }return (this.last_lit == this.lit_bufsize - 1);
336 }, "~N,~N");
337 Clazz.defineMethod (c$, "compress_block", 
338 function (ltree, dtree) {
339 var dist;
340 var lc;
341 var lx = 0;
342 var code;
343 var extra;
344 if (this.last_lit != 0) {
345 do {
346 dist = ((this.pending_buf[this.d_buf + lx * 2] << 8) & 0xff00) | (this.pending_buf[this.d_buf + lx * 2 + 1] & 0xff);
347 lc = (this.pending_buf[this.l_buf + lx]) & 0xff;
348 lx++;
349 if (dist == 0) {
350 this.send_code (lc, ltree);
351 } else {
352 code = JU.Tree._length_code[lc];
353 this.send_code (code + 256 + 1, ltree);
354 extra = JU.Tree.extra_lbits[code];
355 if (extra != 0) {
356 lc -= JU.Tree.base_length[code];
357 this.send_bits (lc, extra);
358 }dist--;
359 code = JU.Tree.d_code (dist);
360 this.send_code (code, dtree);
361 extra = JU.Tree.extra_dbits[code];
362 if (extra != 0) {
363 dist -= JU.Tree.base_dist[code];
364 this.send_bits (dist, extra);
365 }}} while (lx < this.last_lit);
366 }this.send_code (256, ltree);
367 this.last_eob_len = ltree[513];
368 }, "~A,~A");
369 Clazz.defineMethod (c$, "set_data_type", 
370 function () {
371 var n = 0;
372 var ascii_freq = 0;
373 var bin_freq = 0;
374 while (n < 7) {
375 bin_freq += this.dyn_ltree[n * 2];
376 n++;
377 }
378 while (n < 128) {
379 ascii_freq += this.dyn_ltree[n * 2];
380 n++;
381 }
382 while (n < 256) {
383 bin_freq += this.dyn_ltree[n * 2];
384 n++;
385 }
386 this.data_type = (bin_freq > (ascii_freq >>> 2) ? 0 : 1);
387 });
388 Clazz.defineMethod (c$, "bi_flush", 
389 function () {
390 if (this.bi_valid == 16) {
391 this.put_short (this.bi_buf);
392 this.bi_buf = 0;
393 this.bi_valid = 0;
394 } else if (this.bi_valid >= 8) {
395 this.put_byteB (this.bi_buf);
396 this.bi_buf >>>= 8;
397 this.bi_valid -= 8;
398 }});
399 Clazz.defineMethod (c$, "bi_windup", 
400 function () {
401 if (this.bi_valid > 8) {
402 this.put_short (this.bi_buf);
403 } else if (this.bi_valid > 0) {
404 this.put_byteB (this.bi_buf);
405 }this.bi_buf = 0;
406 this.bi_valid = 0;
407 });
408 Clazz.defineMethod (c$, "copy_block", 
409 function (buf, len, header) {
410 this.bi_windup ();
411 this.last_eob_len = 8;
412 if (header) {
413 this.put_short (len);
414 this.put_short (~len);
415 }this.put_byte (this.window, buf, len);
416 }, "~N,~N,~B");
417 Clazz.defineMethod (c$, "flush_block_only", 
418 function (eof) {
419 this._tr_flush_block (this.block_start >= 0 ? this.block_start : -1, this.strstart - this.block_start, eof);
420 this.block_start = this.strstart;
421 this.strm.flush_pending ();
422 }, "~B");
423 Clazz.defineMethod (c$, "deflate_stored", 
424 function (flush) {
425 var max_block_size = 0xffff;
426 var max_start;
427 if (max_block_size > this.pending_buf_size - 5) {
428 max_block_size = this.pending_buf_size - 5;
429 }while (true) {
430 if (this.lookahead <= 1) {
431 this.fill_window ();
432 if (this.lookahead == 0 && flush == 0) return 0;
433 if (this.lookahead == 0) break;
434 }this.strstart += this.lookahead;
435 this.lookahead = 0;
436 max_start = this.block_start + max_block_size;
437 if (this.strstart == 0 || this.strstart >= max_start) {
438 this.lookahead = (this.strstart - max_start);
439 this.strstart = max_start;
440 this.flush_block_only (false);
441 if (this.strm.avail_out == 0) return 0;
442 }if (this.strstart - this.block_start >= this.w_size - 262) {
443 this.flush_block_only (false);
444 if (this.strm.avail_out == 0) return 0;
445 }}
446 this.flush_block_only (flush == 4);
447 if (this.strm.avail_out == 0) return (flush == 4) ? 2 : 0;
448 return flush == 4 ? 3 : 1;
449 }, "~N");
450 Clazz.defineMethod (c$, "_tr_stored_block", 
451 function (buf, stored_len, eof) {
452 this.send_bits ((0) + (eof ? 1 : 0), 3);
453 this.copy_block (buf, stored_len, true);
454 }, "~N,~N,~B");
455 Clazz.defineMethod (c$, "_tr_flush_block", 
456 function (buf, stored_len, eof) {
457 var opt_lenb;
458 var static_lenb;
459 var max_blindex = 0;
460 if (this.level > 0) {
461 if (this.data_type == 2) this.set_data_type ();
462 this.l_desc.build_tree (this);
463 this.d_desc.build_tree (this);
464 max_blindex = this.build_bl_tree ();
465 opt_lenb = (this.opt_len + 3 + 7) >>> 3;
466 static_lenb = (this.static_len + 3 + 7) >>> 3;
467 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
468 } else {
469 opt_lenb = static_lenb = stored_len + 5;
470 }if (stored_len + 4 <= opt_lenb && buf != -1) {
471 this._tr_stored_block (buf, stored_len, eof);
472 } else if (static_lenb == opt_lenb) {
473 this.send_bits ((2) + (eof ? 1 : 0), 3);
474 this.compress_block (JU.StaticTree.static_ltree, JU.StaticTree.static_dtree);
475 } else {
476 this.send_bits ((4) + (eof ? 1 : 0), 3);
477 this.send_all_trees (this.l_desc.max_code + 1, this.d_desc.max_code + 1, max_blindex + 1);
478 this.compress_block (this.dyn_ltree, this.dyn_dtree);
479 }this.init_block ();
480 if (eof) {
481 this.bi_windup ();
482 }}, "~N,~N,~B");
483 Clazz.defineMethod (c$, "fill_window", 
484 function () {
485 var n;
486 var m;
487 var p;
488 var more;
489 do {
490 more = (this.window_size - this.lookahead - this.strstart);
491 if (more == 0 && this.strstart == 0 && this.lookahead == 0) {
492 more = this.w_size;
493 } else if (more == -1) {
494 more--;
495 } else if (this.strstart >= this.w_size + this.w_size - 262) {
496 System.arraycopy (this.window, this.w_size, this.window, 0, this.w_size);
497 this.match_start -= this.w_size;
498 this.strstart -= this.w_size;
499 this.block_start -= this.w_size;
500 n = this.hash_size;
501 p = n;
502 do {
503 m = (this.head[--p] & 0xffff);
504 this.head[p] = (m >= this.w_size ? (m - this.w_size) : 0);
505 } while (--n != 0);
506 n = this.w_size;
507 p = n;
508 do {
509 m = (this.prev[--p] & 0xffff);
510 this.prev[p] = (m >= this.w_size ? (m - this.w_size) : 0);
511 } while (--n != 0);
512 more += this.w_size;
513 }if (this.strm.avail_in == 0) return;
514 n = this.strm.read_buf (this.window, this.strstart + this.lookahead, more);
515 this.lookahead += n;
516 if (this.lookahead >= 3) {
517 this.ins_h = this.window[this.strstart] & 0xff;
518 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[this.strstart + 1] & 0xff)) & this.hash_mask;
519 }} while (this.lookahead < 262 && this.strm.avail_in != 0);
520 });
521 Clazz.defineMethod (c$, "deflate_fast", 
522 function (flush) {
523 var hash_head = 0;
524 var bflush;
525 while (true) {
526 if (this.lookahead < 262) {
527 this.fill_window ();
528 if (this.lookahead < 262 && flush == 0) {
529 return 0;
530 }if (this.lookahead == 0) break;
531 }if (this.lookahead >= 3) {
532 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[(this.strstart) + (2)] & 0xff)) & this.hash_mask;
533 hash_head = (this.head[this.ins_h] & 0xffff);
534 this.prev[this.strstart & this.w_mask] = this.head[this.ins_h];
535 this.head[this.ins_h] = this.strstart;
536 }if (hash_head != 0 && ((this.strstart - hash_head) & 0xffff) <= this.w_size - 262) {
537 if (this.strategy != 2) {
538 this.match_length = this.longest_match (hash_head);
539 }}if (this.match_length >= 3) {
540 bflush = this._tr_tally (this.strstart - this.match_start, this.match_length - 3);
541 this.lookahead -= this.match_length;
542 if (this.match_length <= this.max_lazy_match && this.lookahead >= 3) {
543 this.match_length--;
544 do {
545 this.strstart++;
546 this.ins_h = ((this.ins_h << this.hash_shift) ^ (this.window[(this.strstart) + (2)] & 0xff)) & this.hash_mask;
547 hash_head = (this.head[this.ins_h] & 0xffff);
548 this.prev[this.strstart & this.w_mask] = this.head[this.ins_h];
549 this.head[this.ins_h] = this.strstart;
550 } while (--this.match_length != 0);
551 this.strstart++;
552 } else {
553 this.strstart += this.match_length;
554 this.match_length = 0;
555 this.ins_h = this.window[this.strstart] & 0xff;
556 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[this.strstart + 1] & 0xff)) & this.hash_mask;
557 }} else {
558 bflush = this._tr_tally (0, this.window[this.strstart] & 0xff);
559 this.lookahead--;
560 this.strstart++;
561 }if (bflush) {
562 this.flush_block_only (false);
563 if (this.strm.avail_out == 0) return 0;
564 }}
565 this.flush_block_only (flush == 4);
566 if (this.strm.avail_out == 0) {
567 if (flush == 4) return 2;
568 return 0;
569 }return flush == 4 ? 3 : 1;
570 }, "~N");
571 Clazz.defineMethod (c$, "deflate_slow", 
572 function (flush) {
573 var hash_head = 0;
574 var bflush;
575 while (true) {
576 if (this.lookahead < 262) {
577 this.fill_window ();
578 if (this.lookahead < 262 && flush == 0) {
579 return 0;
580 }if (this.lookahead == 0) break;
581 }if (this.lookahead >= 3) {
582 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[(this.strstart) + (2)] & 0xff)) & this.hash_mask;
583 hash_head = (this.head[this.ins_h] & 0xffff);
584 this.prev[this.strstart & this.w_mask] = this.head[this.ins_h];
585 this.head[this.ins_h] = this.strstart;
586 }this.prev_length = this.match_length;
587 this.prev_match = this.match_start;
588 this.match_length = 2;
589 if (hash_head != 0 && this.prev_length < this.max_lazy_match && ((this.strstart - hash_head) & 0xffff) <= this.w_size - 262) {
590 if (this.strategy != 2) {
591 this.match_length = this.longest_match (hash_head);
592 }if (this.match_length <= 5 && (this.strategy == 1 || (this.match_length == 3 && this.strstart - this.match_start > 4096))) {
593 this.match_length = 2;
594 }}if (this.prev_length >= 3 && this.match_length <= this.prev_length) {
595 var max_insert = this.strstart + this.lookahead - 3;
596 bflush = this._tr_tally (this.strstart - 1 - this.prev_match, this.prev_length - 3);
597 this.lookahead -= this.prev_length - 1;
598 this.prev_length -= 2;
599 do {
600 if (++this.strstart <= max_insert) {
601 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[(this.strstart) + (2)] & 0xff)) & this.hash_mask;
602 hash_head = (this.head[this.ins_h] & 0xffff);
603 this.prev[this.strstart & this.w_mask] = this.head[this.ins_h];
604 this.head[this.ins_h] = this.strstart;
605 }} while (--this.prev_length != 0);
606 this.match_available = 0;
607 this.match_length = 2;
608 this.strstart++;
609 if (bflush) {
610 this.flush_block_only (false);
611 if (this.strm.avail_out == 0) return 0;
612 }} else if (this.match_available != 0) {
613 bflush = this._tr_tally (0, this.window[this.strstart - 1] & 0xff);
614 if (bflush) {
615 this.flush_block_only (false);
616 }this.strstart++;
617 this.lookahead--;
618 if (this.strm.avail_out == 0) return 0;
619 } else {
620 this.match_available = 1;
621 this.strstart++;
622 this.lookahead--;
623 }}
624 if (this.match_available != 0) {
625 bflush = this._tr_tally (0, this.window[this.strstart - 1] & 0xff);
626 this.match_available = 0;
627 }this.flush_block_only (flush == 4);
628 if (this.strm.avail_out == 0) {
629 if (flush == 4) return 2;
630 return 0;
631 }return flush == 4 ? 3 : 1;
632 }, "~N");
633 Clazz.defineMethod (c$, "longest_match", 
634 function (cur_match) {
635 var chain_length = this.max_chain_length;
636 var scan = this.strstart;
637 var match;
638 var len;
639 var best_len = this.prev_length;
640 var limit = this.strstart > (this.w_size - 262) ? this.strstart - (this.w_size - 262) : 0;
641 var nice_match = this.nice_match;
642 var wmask = this.w_mask;
643 var strend = this.strstart + 258;
644 var scan_end1 = this.window[scan + best_len - 1];
645 var scan_end = this.window[scan + best_len];
646 if (this.prev_length >= this.good_match) {
647 chain_length >>= 2;
648 }if (nice_match > this.lookahead) nice_match = this.lookahead;
649 do {
650 match = cur_match;
651 if (this.window[match + best_len] != scan_end || this.window[match + best_len - 1] != scan_end1 || this.window[match] != this.window[scan] || this.window[++match] != this.window[scan + 1]) continue;
652 scan += 2;
653 match++;
654 do {
655 } while (this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && this.window[++scan] == this.window[++match] && scan < strend);
656 len = 258 - (strend - scan);
657 scan = strend - 258;
658 if (len > best_len) {
659 this.match_start = cur_match;
660 best_len = len;
661 if (len >= nice_match) break;
662 scan_end1 = this.window[scan + best_len - 1];
663 scan_end = this.window[scan + best_len];
664 }} while ((cur_match = (this.prev[cur_match & wmask] & 0xffff)) > limit && --chain_length != 0);
665 if (best_len <= this.lookahead) return best_len;
666 return this.lookahead;
667 }, "~N");
668 Clazz.defineMethod (c$, "deflateInit5", 
669  function (level, method, windowBits, memLevel, strategy) {
670 var wrap = 1;
671 this.strm.msg = null;
672 if (level == -1) level = 6;
673 if (windowBits < 0) {
674 wrap = 0;
675 windowBits = -windowBits;
676 } else if (windowBits > 15) {
677 wrap = 2;
678 windowBits -= 16;
679 this.strm.checksum =  new JU.CRC32 ();
680 }if (memLevel < 1 || memLevel > 9 || method != 8 || windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > 2) {
681 return -2;
682 }this.strm.dstate = this;
683 this.wrap = wrap;
684 this.w_bits = windowBits;
685 this.w_size = 1 << this.w_bits;
686 this.w_mask = this.w_size - 1;
687 this.hash_bits = memLevel + 7;
688 this.hash_size = 1 << this.hash_bits;
689 this.hash_mask = this.hash_size - 1;
690 this.hash_shift = (Clazz.doubleToInt ((this.hash_bits + 3 - 1) / 3));
691 this.window =  Clazz.newByteArray (this.w_size * 2, 0);
692 this.prev =  Clazz.newShortArray (this.w_size, 0);
693 this.head =  Clazz.newShortArray (this.hash_size, 0);
694 this.lit_bufsize = 1 << (memLevel + 6);
695 this.pending_buf =  Clazz.newByteArray (this.lit_bufsize * 4, 0);
696 this.pending_buf_size = this.lit_bufsize * 4;
697 this.d_buf = Clazz.doubleToInt (this.lit_bufsize / 2);
698 this.l_buf = (3) * this.lit_bufsize;
699 this.level = level;
700 this.strategy = strategy;
701 this.method = method;
702 return this.deflateReset ();
703 }, "~N,~N,~N,~N,~N");
704 Clazz.defineMethod (c$, "deflateReset", 
705 function () {
706 this.strm.total_in = this.strm.total_out = 0;
707 this.strm.msg = null;
708 this.strm.data_type = 2;
709 this.pending = 0;
710 this.pending_out = 0;
711 if (this.wrap < 0) {
712 this.wrap = -this.wrap;
713 }this.status = (this.wrap == 0) ? 113 : 42;
714 this.strm.checksum.reset ();
715 this.last_flush = 0;
716 this.tr_init ();
717 this.lm_init ();
718 return 0;
719 });
720 Clazz.defineMethod (c$, "deflateEnd", 
721 function () {
722 if (this.status != 42 && this.status != 113 && this.status != 666) {
723 return -2;
724 }this.pending_buf = null;
725 this.head = null;
726 this.prev = null;
727 this.window = null;
728 return this.status == 113 ? -3 : 0;
729 });
730 Clazz.defineMethod (c$, "deflateParams", 
731 function (_level, _strategy) {
732 var err = 0;
733 if (_level == -1) {
734 _level = 6;
735 }if (_level < 0 || _level > 9 || _strategy < 0 || _strategy > 2) {
736 return -2;
737 }if (JU.Deflate.config_table[this.level].func != JU.Deflate.config_table[_level].func && this.strm.total_in != 0) {
738 err = this.strm.deflate (1);
739 }if (this.level != _level) {
740 this.level = _level;
741 this.max_lazy_match = JU.Deflate.config_table[this.level].max_lazy;
742 this.good_match = JU.Deflate.config_table[this.level].good_length;
743 this.nice_match = JU.Deflate.config_table[this.level].nice_length;
744 this.max_chain_length = JU.Deflate.config_table[this.level].max_chain;
745 }this.strategy = _strategy;
746 return err;
747 }, "~N,~N");
748 Clazz.defineMethod (c$, "deflateSetDictionary", 
749 function (dictionary, dictLength) {
750 var length = dictLength;
751 var index = 0;
752 if (dictionary == null || this.status != 42) return -2;
753 this.strm.checksum.update (dictionary, 0, dictLength);
754 if (length < 3) return 0;
755 if (length > this.w_size - 262) {
756 length = this.w_size - 262;
757 index = dictLength - length;
758 }System.arraycopy (dictionary, index, this.window, 0, length);
759 this.strstart = length;
760 this.block_start = length;
761 this.ins_h = this.window[0] & 0xff;
762 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[1] & 0xff)) & this.hash_mask;
763 for (var n = 0; n <= length - 3; n++) {
764 this.ins_h = (((this.ins_h) << this.hash_shift) ^ (this.window[(n) + (2)] & 0xff)) & this.hash_mask;
765 this.prev[n & this.w_mask] = this.head[this.ins_h];
766 this.head[this.ins_h] = n;
767 }
768 return 0;
769 }, "~A,~N");
770 Clazz.defineMethod (c$, "deflate", 
771 function (flush) {
772 var old_flush;
773 if (flush > 4 || flush < 0) {
774 return -2;
775 }if (this.strm.next_out == null || (this.strm.next_in == null && this.strm.avail_in != 0) || (this.status == 666 && flush != 4)) {
776 this.strm.msg = JU.Deflate.z_errmsg[4];
777 return -2;
778 }if (this.strm.avail_out == 0) {
779 this.strm.msg = JU.Deflate.z_errmsg[7];
780 return -5;
781 }old_flush = this.last_flush;
782 this.last_flush = flush;
783 if (this.status == 42) {
784 if (this.wrap == 2) {
785 this.getGZIPHeader ().put (this);
786 this.status = 113;
787 this.strm.checksum.reset ();
788 } else {
789 var header = (8 + ((this.w_bits - 8) << 4)) << 8;
790 var level_flags = ((this.level - 1) & 0xff) >> 1;
791 if (level_flags > 3) level_flags = 3;
792 header |= (level_flags << 6);
793 if (this.strstart != 0) header |= 32;
794 header += 31 - (header % 31);
795 this.status = 113;
796 this.putShortMSB (header);
797 if (this.strstart != 0) {
798 var adler = this.strm.checksum.getValue ();
799 this.putShortMSB ((adler >>> 16));
800 this.putShortMSB ((adler & 0xffff));
801 }this.strm.checksum.reset ();
802 }}if (this.pending != 0) {
803 this.strm.flush_pending ();
804 if (this.strm.avail_out == 0) {
805 this.last_flush = -1;
806 return 0;
807 }} else if (this.strm.avail_in == 0 && flush <= old_flush && flush != 4) {
808 this.strm.msg = JU.Deflate.z_errmsg[7];
809 return -5;
810 }if (this.status == 666 && this.strm.avail_in != 0) {
811 this.strm.msg = JU.Deflate.z_errmsg[7];
812 return -5;
813 }if (this.strm.avail_in != 0 || this.lookahead != 0 || (flush != 0 && this.status != 666)) {
814 var bstate = -1;
815 switch (JU.Deflate.config_table[this.level].func) {
816 case 0:
817 bstate = this.deflate_stored (flush);
818 break;
819 case 1:
820 bstate = this.deflate_fast (flush);
821 break;
822 case 2:
823 bstate = this.deflate_slow (flush);
824 break;
825 default:
826 }
827 if (bstate == 2 || bstate == 3) {
828 this.status = 666;
829 }if (bstate == 0 || bstate == 2) {
830 if (this.strm.avail_out == 0) {
831 this.last_flush = -1;
832 }return 0;
833 }if (bstate == 1) {
834 if (flush == 1) {
835 this._tr_align ();
836 } else {
837 this._tr_stored_block (0, 0, false);
838 if (flush == 3) {
839 for (var i = 0; i < this.hash_size; i++) this.head[i] = 0;
840
841 }}this.strm.flush_pending ();
842 if (this.strm.avail_out == 0) {
843 this.last_flush = -1;
844 return 0;
845 }}}if (flush != 4) return 0;
846 if (this.wrap <= 0) return 1;
847 if (this.wrap == 2) {
848 var adler = this.strm.checksum.getValue ();
849 this.put_byteB ((adler & 0xff));
850 this.put_byteB (((adler >> 8) & 0xff));
851 this.put_byteB (((adler >> 16) & 0xff));
852 this.put_byteB (((adler >> 24) & 0xff));
853 this.put_byteB ((this.strm.total_in & 0xff));
854 this.put_byteB (((this.strm.total_in >> 8) & 0xff));
855 this.put_byteB (((this.strm.total_in >> 16) & 0xff));
856 this.put_byteB (((this.strm.total_in >> 24) & 0xff));
857 this.getGZIPHeader ().setCRC (adler);
858 } else {
859 var adler = this.strm.checksum.getValue ();
860 this.putShortMSB ((adler >>> 16));
861 this.putShortMSB ((adler & 0xffff));
862 }this.strm.flush_pending ();
863 if (this.wrap > 0) this.wrap = -this.wrap;
864 return this.pending != 0 ? 0 : 1;
865 }, "~N");
866 Clazz.defineMethod (c$, "getGZIPHeader", 
867 function () {
868 if (this.gheader == null) {
869 this.gheader =  new JU.GZIPHeader ();
870 }return this.gheader;
871 });
872 Clazz.defineMethod (c$, "getBytesRead", 
873 function () {
874 return this.strm.total_in;
875 });
876 Clazz.defineMethod (c$, "getBytesWritten", 
877 function () {
878 return this.strm.total_out;
879 });
880 Clazz.pu$h(self.c$);
881 c$ = Clazz.decorateAsClass (function () {
882 this.good_length = 0;
883 this.max_lazy = 0;
884 this.nice_length = 0;
885 this.max_chain = 0;
886 this.func = 0;
887 Clazz.instantialize (this, arguments);
888 }, JU.Deflate, "Config");
889 Clazz.makeConstructor (c$, 
890 function (a, b, c, d, e) {
891 this.good_length = a;
892 this.max_lazy = b;
893 this.nice_length = c;
894 this.max_chain = d;
895 this.func = e;
896 }, "~N,~N,~N,~N,~N");
897 c$ = Clazz.p0p ();
898 Clazz.defineStatics (c$,
899 "MAX_MEM_LEVEL", 9,
900 "Z_DEFAULT_COMPRESSION", -1,
901 "MAX_WBITS", 15,
902 "DEF_MEM_LEVEL", 8,
903 "STORED", 0,
904 "FAST", 1,
905 "SLOW", 2,
906 "config_table", null);
907 {
908 JU.Deflate.config_table =  new Array (10);
909 JU.Deflate.config_table[0] =  new JU.Deflate.Config (0, 0, 0, 0, 0);
910 JU.Deflate.config_table[1] =  new JU.Deflate.Config (4, 4, 8, 4, 1);
911 JU.Deflate.config_table[2] =  new JU.Deflate.Config (4, 5, 16, 8, 1);
912 JU.Deflate.config_table[3] =  new JU.Deflate.Config (4, 6, 32, 32, 1);
913 JU.Deflate.config_table[4] =  new JU.Deflate.Config (4, 4, 16, 16, 2);
914 JU.Deflate.config_table[5] =  new JU.Deflate.Config (8, 16, 32, 32, 2);
915 JU.Deflate.config_table[6] =  new JU.Deflate.Config (8, 16, 128, 128, 2);
916 JU.Deflate.config_table[7] =  new JU.Deflate.Config (8, 32, 128, 256, 2);
917 JU.Deflate.config_table[8] =  new JU.Deflate.Config (32, 128, 258, 1024, 2);
918 JU.Deflate.config_table[9] =  new JU.Deflate.Config (32, 258, 258, 4096, 2);
919 }Clazz.defineStatics (c$,
920 "z_errmsg",  Clazz.newArray (-1, ["need dictionary", "stream end", "", "file error", "stream error", "data error", "insufficient memory", "buffer error", "incompatible version", ""]),
921 "NeedMore", 0,
922 "BlockDone", 1,
923 "FinishStarted", 2,
924 "FinishDone", 3,
925 "PRESET_DICT", 0x20,
926 "Z_FILTERED", 1,
927 "Z_HUFFMAN_ONLY", 2,
928 "Z_DEFAULT_STRATEGY", 0,
929 "Z_NO_FLUSH", 0,
930 "Z_PARTIAL_FLUSH", 1,
931 "Z_FULL_FLUSH", 3,
932 "Z_FINISH", 4,
933 "Z_OK", 0,
934 "Z_STREAM_END", 1,
935 "Z_NEED_DICT", 2,
936 "Z_STREAM_ERROR", -2,
937 "Z_DATA_ERROR", -3,
938 "Z_BUF_ERROR", -5,
939 "INIT_STATE", 42,
940 "BUSY_STATE", 113,
941 "FINISH_STATE", 666,
942 "Z_DEFLATED", 8,
943 "STORED_BLOCK", 0,
944 "STATIC_TREES", 1,
945 "DYN_TREES", 2,
946 "Z_BINARY", 0,
947 "Z_ASCII", 1,
948 "Z_UNKNOWN", 2,
949 "Buf_size", 16,
950 "REP_3_6", 16,
951 "REPZ_3_10", 17,
952 "REPZ_11_138", 18,
953 "MIN_MATCH", 3,
954 "MAX_MATCH", 258,
955 "MIN_LOOKAHEAD", (262),
956 "MAX_BITS", 15,
957 "D_CODES", 30,
958 "BL_CODES", 19,
959 "LENGTH_CODES", 29,
960 "LITERALS", 256,
961 "L_CODES", (286),
962 "HEAP_SIZE", (573),
963 "END_BLOCK", 256);
964 });