efront 3.25.16 → 3.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1691 +0,0 @@
1
- /*
2
- * $Id: rawdeflate.js,v 0.3 2009/03/01 19:05:05 dankogai Exp dankogai $
3
- *
4
- * Original:
5
- * http://www.onicos.com/staff/iz/amuse/javascript/expert/deflate.txt
6
- */
7
-
8
- /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
9
- * Version: 1.0.1
10
- * LastModified: Dec 25 1999
11
- */
12
-
13
- /* Interface:
14
- * data = deflate(src);
15
- */
16
-
17
- /* constant parameters */
18
- var WSIZE = 32768, // Sliding Window size
19
- STORED_BLOCK = 0,
20
- STATIC_TREES = 1,
21
- DYN_TREES = 2,
22
-
23
- /* for deflate */
24
- DEFAULT_LEVEL = 6,
25
- FULL_SEARCH = false,
26
- INBUFSIZ = 32768, // Input buffer size
27
- //INBUF_EXTRA = 64, // Extra buffer
28
- OUTBUFSIZ = 1024 * 8,
29
- window_size = 2 * WSIZE,
30
- MIN_MATCH = 3,
31
- MAX_MATCH = 258,
32
- BITS = 16,
33
- // for SMALL_MEM
34
- LIT_BUFSIZE = 0x2000,
35
- // HASH_BITS = 13,
36
- //for MEDIUM_MEM
37
- // LIT_BUFSIZE = 0x4000,
38
- // HASH_BITS = 14,
39
- // for BIG_MEM
40
- // LIT_BUFSIZE = 0x8000,
41
- HASH_BITS = 15,
42
- DIST_BUFSIZE = LIT_BUFSIZE,
43
- HASH_SIZE = 1 << HASH_BITS,
44
- HASH_MASK = HASH_SIZE - 1,
45
- WMASK = WSIZE - 1,
46
- NIL = 0, // Tail of hash chains
47
- TOO_FAR = 4096,
48
- MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1,
49
- MAX_DIST = WSIZE - MIN_LOOKAHEAD,
50
- SMALLEST = 1,
51
- MAX_BITS = 15,
52
- MAX_BL_BITS = 7,
53
- LENGTH_CODES = 29,
54
- LITERALS = 256,
55
- END_BLOCK = 256,
56
- L_CODES = LITERALS + 1 + LENGTH_CODES,
57
- D_CODES = 30,
58
- BL_CODES = 19,
59
- REP_3_6 = 16,
60
- REPZ_3_10 = 17,
61
- REPZ_11_138 = 18,
62
- HEAP_SIZE = 2 * L_CODES + 1,
63
- H_SHIFT = parseInt((HASH_BITS + MIN_MATCH - 1) / MIN_MATCH, 10),
64
-
65
- /* variables */
66
- free_queue,
67
- qhead,
68
- qtail,
69
- initflag,
70
- outbuf = null,
71
- outcnt,
72
- outoff,
73
- complete,
74
- window,
75
- d_buf,
76
- l_buf,
77
- prev,
78
- bi_buf,
79
- bi_valid,
80
- block_start,
81
- ins_h,
82
- hash_head,
83
- prev_match,
84
- match_available,
85
- match_length,
86
- prev_length,
87
- strstart,
88
- match_start,
89
- eofile,
90
- lookahead,
91
- max_chain_length,
92
- max_lazy_match,
93
- compr_level,
94
- good_match,
95
- nice_match,
96
- dyn_ltree,
97
- dyn_dtree,
98
- static_ltree,
99
- static_dtree,
100
- bl_tree,
101
- l_desc,
102
- d_desc,
103
- bl_desc,
104
- bl_count,
105
- heap,
106
- heap_len,
107
- heap_max,
108
- depth,
109
- length_code,
110
- dist_code,
111
- base_length,
112
- base_dist,
113
- flag_buf,
114
- last_lit,
115
- last_dist,
116
- last_flags,
117
- flags,
118
- flag_bit,
119
- opt_len,
120
- static_len,
121
- deflate_data,
122
- deflate_pos;
123
-
124
- if (LIT_BUFSIZE > INBUFSIZ) {
125
- console.error("error: INBUFSIZ is too small");
126
- }
127
- if ((WSIZE << 1) > (1 << BITS)) {
128
- console.error("error: WSIZE is too large");
129
- }
130
- if (HASH_BITS > BITS - 1) {
131
- console.error("error: HASH_BITS is too large");
132
- }
133
- if (HASH_BITS < 8 || MAX_MATCH !== 258) {
134
- console.error("error: Code too clever");
135
- }
136
-
137
- /* objects (deflate) */
138
-
139
- function DeflateCT() {
140
- this.fc = 0; // frequency count or bit string
141
- this.dl = 0; // father node in Huffman tree or length of bit string
142
- }
143
-
144
- function DeflateTreeDesc() {
145
- this.dyn_tree = null; // the dynamic tree
146
- this.static_tree = null; // corresponding static tree or NULL
147
- this.extra_bits = null; // extra bits for each code or NULL
148
- this.extra_base = 0; // base index for extra_bits
149
- this.elems = 0; // max number of elements in the tree
150
- this.max_length = 0; // max bit length for the codes
151
- this.max_code = 0; // largest code with non zero frequency
152
- }
153
-
154
- /* Values for max_lazy_match, good_match and max_chain_length, depending on
155
- * the desired pack level (0..9). The values given below have been tuned to
156
- * exclude worst case performance for pathological files. Better values may be
157
- * found for specific files.
158
- */
159
- function DeflateConfiguration(a, b, c, d) {
160
- this.good_length = a; // reduce lazy search above this match length
161
- this.max_lazy = b; // do not perform lazy search above this match length
162
- this.nice_length = c; // quit search above this match length
163
- this.max_chain = d;
164
- }
165
-
166
- function DeflateBuffer() {
167
- this.next = null;
168
- this.len = 0;
169
- this.ptr = []; // new Array(OUTBUFSIZ); // ptr.length is never read
170
- this.off = 0;
171
- }
172
-
173
- /* constant tables */
174
- var extra_lbits = [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];
175
- var extra_dbits = [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];
176
- var extra_blbits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7];
177
- var bl_order = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
178
- var configuration_table = [
179
- new DeflateConfiguration(0, 0, 0, 0),
180
- new DeflateConfiguration(4, 4, 8, 4),
181
- new DeflateConfiguration(4, 5, 16, 8),
182
- new DeflateConfiguration(4, 6, 32, 32),
183
- new DeflateConfiguration(4, 4, 16, 16),
184
- new DeflateConfiguration(8, 16, 32, 32),
185
- new DeflateConfiguration(8, 16, 128, 128),
186
- new DeflateConfiguration(8, 32, 128, 256),
187
- new DeflateConfiguration(32, 128, 258, 1024),
188
- new DeflateConfiguration(32, 258, 258, 4096)
189
- ];
190
-
191
-
192
- /* routines (deflate) */
193
-
194
- function deflate_start(level) {
195
- var i;
196
-
197
- if (!level) {
198
- level = DEFAULT_LEVEL;
199
- } else if (level < 1) {
200
- level = 1;
201
- } else if (level > 9) {
202
- level = 9;
203
- }
204
-
205
- compr_level = level;
206
- initflag = false;
207
- eofile = false;
208
- if (outbuf !== null) {
209
- return;
210
- }
211
-
212
- free_queue = qhead = qtail = null;
213
- outbuf = []; // new Array(OUTBUFSIZ); // outbuf.length never called
214
- window = []; // new Array(window_size); // window.length never called
215
- d_buf = []; // new Array(DIST_BUFSIZE); // d_buf.length never called
216
- l_buf = []; // new Array(INBUFSIZ + INBUF_EXTRA); // l_buf.length never called
217
- prev = []; // new Array(1 << BITS); // prev.length never called
218
-
219
- dyn_ltree = [];
220
- for (i = 0; i < HEAP_SIZE; i++) {
221
- dyn_ltree[i] = new DeflateCT();
222
- }
223
- dyn_dtree = [];
224
- for (i = 0; i < 2 * D_CODES + 1; i++) {
225
- dyn_dtree[i] = new DeflateCT();
226
- }
227
- static_ltree = [];
228
- for (i = 0; i < L_CODES + 2; i++) {
229
- static_ltree[i] = new DeflateCT();
230
- }
231
- static_dtree = [];
232
- for (i = 0; i < D_CODES; i++) {
233
- static_dtree[i] = new DeflateCT();
234
- }
235
- bl_tree = [];
236
- for (i = 0; i < 2 * BL_CODES + 1; i++) {
237
- bl_tree[i] = new DeflateCT();
238
- }
239
- l_desc = new DeflateTreeDesc();
240
- d_desc = new DeflateTreeDesc();
241
- bl_desc = new DeflateTreeDesc();
242
- bl_count = []; // new Array(MAX_BITS+1); // bl_count.length never called
243
- heap = []; // new Array(2*L_CODES+1); // heap.length never called
244
- depth = []; // new Array(2*L_CODES+1); // depth.length never called
245
- length_code = []; // new Array(MAX_MATCH-MIN_MATCH+1); // length_code.length never called
246
- dist_code = []; // new Array(512); // dist_code.length never called
247
- base_length = []; // new Array(LENGTH_CODES); // base_length.length never called
248
- base_dist = []; // new Array(D_CODES); // base_dist.length never called
249
- flag_buf = []; // new Array(parseInt(LIT_BUFSIZE / 8, 10)); // flag_buf.length never called
250
- }
251
-
252
- function deflate_end() {
253
- free_queue = qhead = qtail = null;
254
- outbuf = null;
255
- window = null;
256
- d_buf = null;
257
- l_buf = null;
258
- prev = null;
259
- dyn_ltree = null;
260
- dyn_dtree = null;
261
- static_ltree = null;
262
- static_dtree = null;
263
- bl_tree = null;
264
- l_desc = null;
265
- d_desc = null;
266
- bl_desc = null;
267
- bl_count = null;
268
- heap = null;
269
- depth = null;
270
- length_code = null;
271
- dist_code = null;
272
- base_length = null;
273
- base_dist = null;
274
- flag_buf = null;
275
- }
276
-
277
- function reuse_queue(p) {
278
- p.next = free_queue;
279
- free_queue = p;
280
- }
281
-
282
- function new_queue() {
283
- var p;
284
-
285
- if (free_queue !== null) {
286
- p = free_queue;
287
- free_queue = free_queue.next;
288
- } else {
289
- p = new DeflateBuffer();
290
- }
291
- p.next = null;
292
- p.len = p.off = 0;
293
-
294
- return p;
295
- }
296
-
297
- function head1(i) {
298
- return prev[WSIZE + i];
299
- }
300
-
301
- function head2(i, val) {
302
- return (prev[WSIZE + i] = val);
303
- }
304
-
305
- /* put_byte is used for the compressed output, put_ubyte for the
306
- * uncompressed output. However unlzw() uses window for its
307
- * suffix table instead of its output buffer, so it does not use put_ubyte
308
- * (to be cleaned up).
309
- */
310
- function put_byte(c) {
311
- outbuf[outoff + outcnt++] = c;
312
- if (outoff + outcnt === OUTBUFSIZ) {
313
- qoutbuf();
314
- }
315
- }
316
-
317
- /* Output a 16 bit value, lsb first */
318
- function put_short(w) {
319
- w &= 0xffff;
320
- if (outoff + outcnt < OUTBUFSIZ - 2) {
321
- outbuf[outoff + outcnt++] = (w & 0xff);
322
- outbuf[outoff + outcnt++] = (w >>> 8);
323
- } else {
324
- put_byte(w & 0xff);
325
- put_byte(w >>> 8);
326
- }
327
- }
328
-
329
- /* ==========================================================================
330
- * Insert string s in the dictionary and set match_head to the previous head
331
- * of the hash chain (the most recent string with same hash key). Return
332
- * the previous length of the hash chain.
333
- * IN assertion: all calls to to INSERT_STRING are made with consecutive
334
- * input characters and the first MIN_MATCH bytes of s are valid
335
- * (except for the last MIN_MATCH-1 bytes of the input file).
336
- */
337
- function INSERT_STRING() {
338
- ins_h = ((ins_h << H_SHIFT) ^ (window[strstart + MIN_MATCH - 1] & 0xff)) & HASH_MASK;
339
- hash_head = head1(ins_h);
340
- prev[strstart & WMASK] = hash_head;
341
- head2(ins_h, strstart);
342
- }
343
-
344
- /* Send a code of the given tree. c and tree must not have side effects */
345
- function SEND_CODE(c, tree) {
346
- send_bits(tree[c].fc, tree[c].dl);
347
- }
348
-
349
- /* Mapping from a distance to a distance code. dist is the distance - 1 and
350
- * must not have side effects. dist_code[256] and dist_code[257] are never
351
- * used.
352
- */
353
- function D_CODE(dist) {
354
- return (dist < 256 ? dist_code[dist] : dist_code[256 + (dist >> 7)]) & 0xff;
355
- }
356
-
357
- /* ==========================================================================
358
- * Compares to subtrees, using the tree depth as tie breaker when
359
- * the subtrees have equal frequency. This minimizes the worst case length.
360
- */
361
- function SMALLER(tree, n, m) {
362
- return tree[n].fc < tree[m].fc || (tree[n].fc === tree[m].fc && depth[n] <= depth[m]);
363
- }
364
-
365
- /* ==========================================================================
366
- * read string data
367
- */
368
- function read_buff(buff, offset, n) {
369
- var i;
370
- for (i = 0; i < n && deflate_pos < deflate_data.length; i++) {
371
- buff[offset + i] = deflate_data[deflate_pos++] & 0xff;
372
- }
373
- return i;
374
- }
375
-
376
- /* ==========================================================================
377
- * Initialize the "longest match" routines for a new file
378
- */
379
- function lm_init() {
380
- var j;
381
-
382
- // Initialize the hash table. */
383
- for (j = 0; j < HASH_SIZE; j++) {
384
- // head2(j, NIL);
385
- prev[WSIZE + j] = 0;
386
- }
387
- // prev will be initialized on the fly */
388
-
389
- // Set the default configuration parameters:
390
- max_lazy_match = configuration_table[compr_level].max_lazy;
391
- good_match = configuration_table[compr_level].good_length;
392
- if (!FULL_SEARCH) {
393
- nice_match = configuration_table[compr_level].nice_length;
394
- }
395
- max_chain_length = configuration_table[compr_level].max_chain;
396
-
397
- strstart = 0;
398
- block_start = 0;
399
-
400
- lookahead = read_buff(window, 0, 2 * WSIZE);
401
- if (lookahead <= 0) {
402
- eofile = true;
403
- lookahead = 0;
404
- return;
405
- }
406
- eofile = false;
407
- // Make sure that we always have enough lookahead. This is important
408
- // if input comes from a device such as a tty.
409
- while (lookahead < MIN_LOOKAHEAD && !eofile) {
410
- fill_window();
411
- }
412
-
413
- // If lookahead < MIN_MATCH, ins_h is garbage, but this is
414
- // not important since only literal bytes will be emitted.
415
- ins_h = 0;
416
- for (j = 0; j < MIN_MATCH - 1; j++) {
417
- // UPDATE_HASH(ins_h, window[j]);
418
- ins_h = ((ins_h << H_SHIFT) ^ (window[j] & 0xff)) & HASH_MASK;
419
- }
420
- }
421
-
422
- /* ==========================================================================
423
- * Set match_start to the longest match starting at the given string and
424
- * return its length. Matches shorter or equal to prev_length are discarded,
425
- * in which case the result is equal to prev_length and match_start is
426
- * garbage.
427
- * IN assertions: cur_match is the head of the hash chain for the current
428
- * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
429
- */
430
- function longest_match(cur_match) {
431
- var chain_length = max_chain_length; // max hash chain length
432
- var scanp = strstart; // current string
433
- var matchp; // matched string
434
- var len; // length of current match
435
- var best_len = prev_length; // best match length so far
436
-
437
- // Stop when cur_match becomes <= limit. To simplify the code,
438
- // we prevent matches with the string of window index 0.
439
- var limit = (strstart > MAX_DIST ? strstart - MAX_DIST : NIL);
440
-
441
- var strendp = strstart + MAX_MATCH;
442
- var scan_end1 = window[scanp + best_len - 1];
443
- var scan_end = window[scanp + best_len];
444
-
445
- var i, broke;
446
-
447
- // Do not waste too much time if we already have a good match: */
448
- if (prev_length >= good_match) {
449
- chain_length >>= 2;
450
- }
451
-
452
- // Assert(encoder->strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
453
-
454
- do {
455
- // Assert(cur_match < encoder->strstart, "no future");
456
- matchp = cur_match;
457
-
458
- // Skip to next match if the match length cannot increase
459
- // or if the match length is less than 2:
460
- if (window[matchp + best_len] !== scan_end ||
461
- window[matchp + best_len - 1] !== scan_end1 ||
462
- window[matchp] !== window[scanp] ||
463
- window[++matchp] !== window[scanp + 1]) {
464
- continue;
465
- }
466
-
467
- // The check at best_len-1 can be removed because it will be made
468
- // again later. (This heuristic is not always a win.)
469
- // It is not necessary to compare scan[2] and match[2] since they
470
- // are always equal when the other bytes match, given that
471
- // the hash keys are equal and that HASH_BITS >= 8.
472
- scanp += 2;
473
- matchp++;
474
-
475
- // We check for insufficient lookahead only every 8th comparison;
476
- // the 256th check will be made at strstart+258.
477
- while (scanp < strendp) {
478
- broke = false;
479
- for (i = 0; i < 8; i += 1) {
480
- scanp += 1;
481
- matchp += 1;
482
- if (window[scanp] !== window[matchp]) {
483
- broke = true;
484
- break;
485
- }
486
- }
487
-
488
- if (broke) {
489
- break;
490
- }
491
- }
492
-
493
- len = MAX_MATCH - (strendp - scanp);
494
- scanp = strendp - MAX_MATCH;
495
-
496
- if (len > best_len) {
497
- match_start = cur_match;
498
- best_len = len;
499
- if (FULL_SEARCH) {
500
- if (len >= MAX_MATCH) {
501
- break;
502
- }
503
- } else {
504
- if (len >= nice_match) {
505
- break;
506
- }
507
- }
508
-
509
- scan_end1 = window[scanp + best_len - 1];
510
- scan_end = window[scanp + best_len];
511
- }
512
- } while ((cur_match = prev[cur_match & WMASK]) > limit && --chain_length !== 0);
513
-
514
- return best_len;
515
- }
516
-
517
- /* ==========================================================================
518
- * Fill the window when the lookahead becomes insufficient.
519
- * Updates strstart and lookahead, and sets eofile if end of input file.
520
- * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
521
- * OUT assertions: at least one byte has been read, or eofile is set;
522
- * file reads are performed for at least two bytes (required for the
523
- * translate_eol option).
524
- */
525
- function fill_window() {
526
- var n, m;
527
-
528
- // Amount of free space at the end of the window.
529
- var more = window_size - lookahead - strstart;
530
-
531
- // If the window is almost full and there is insufficient lookahead,
532
- // move the upper half to the lower one to make room in the upper half.
533
- if (more === -1) {
534
- // Very unlikely, but possible on 16 bit machine if strstart == 0
535
- // and lookahead == 1 (input done one byte at time)
536
- more--;
537
- } else if (strstart >= WSIZE + MAX_DIST) {
538
- // By the IN assertion, the window is not empty so we can't confuse
539
- // more == 0 with more == 64K on a 16 bit machine.
540
- // Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
541
-
542
- // System.arraycopy(window, WSIZE, window, 0, WSIZE);
543
- for (n = 0; n < WSIZE; n++) {
544
- window[n] = window[n + WSIZE];
545
- }
546
-
547
- match_start -= WSIZE;
548
- strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
549
- block_start -= WSIZE;
550
-
551
- for (n = 0; n < HASH_SIZE; n++) {
552
- m = head1(n);
553
- head2(n, m >= WSIZE ? m - WSIZE : NIL);
554
- }
555
- for (n = 0; n < WSIZE; n++) {
556
- // If n is not on any hash chain, prev[n] is garbage but
557
- // its value will never be used.
558
- m = prev[n];
559
- prev[n] = (m >= WSIZE ? m - WSIZE : NIL);
560
- }
561
- more += WSIZE;
562
- }
563
- // At this point, more >= 2
564
- if (!eofile) {
565
- n = read_buff(window, strstart + lookahead, more);
566
- if (n <= 0) {
567
- eofile = true;
568
- } else {
569
- lookahead += n;
570
- }
571
- }
572
- }
573
-
574
- /* ==========================================================================
575
- * Processes a new input file and return its compressed length. This
576
- * function does not perform lazy evaluationof matches and inserts
577
- * new strings in the dictionary only for unmatched strings or for short
578
- * matches. It is used only for the fast compression options.
579
- */
580
- function deflate_fast() {
581
- while (lookahead !== 0 && qhead === null) {
582
- var flush; // set if current block must be flushed
583
-
584
- // Insert the string window[strstart .. strstart+2] in the
585
- // dictionary, and set hash_head to the head of the hash chain:
586
- INSERT_STRING();
587
-
588
- // Find the longest match, discarding those <= prev_length.
589
- // At this point we have always match_length < MIN_MATCH
590
- if (hash_head !== NIL && strstart - hash_head <= MAX_DIST) {
591
- // To simplify the code, we prevent matches with the string
592
- // of window index 0 (in particular we have to avoid a match
593
- // of the string with itself at the start of the input file).
594
- match_length = longest_match(hash_head);
595
- // longest_match() sets match_start */
596
- if (match_length > lookahead) {
597
- match_length = lookahead;
598
- }
599
- }
600
- if (match_length >= MIN_MATCH) {
601
- // check_match(strstart, match_start, match_length);
602
-
603
- flush = ct_tally(strstart - match_start, match_length - MIN_MATCH);
604
- lookahead -= match_length;
605
-
606
- // Insert new strings in the hash table only if the match length
607
- // is not too large. This saves time but degrades compression.
608
- if (match_length <= max_lazy_match) {
609
- match_length--; // string at strstart already in hash table
610
- do {
611
- strstart++;
612
- INSERT_STRING();
613
- // strstart never exceeds WSIZE-MAX_MATCH, so there are
614
- // always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
615
- // these bytes are garbage, but it does not matter since
616
- // the next lookahead bytes will be emitted as literals.
617
- } while (--match_length !== 0);
618
- strstart++;
619
- } else {
620
- strstart += match_length;
621
- match_length = 0;
622
- ins_h = window[strstart] & 0xff;
623
- // UPDATE_HASH(ins_h, window[strstart + 1]);
624
- ins_h = ((ins_h << H_SHIFT) ^ (window[strstart + 1] & 0xff)) & HASH_MASK;
625
-
626
- //#if MIN_MATCH !== 3
627
- // Call UPDATE_HASH() MIN_MATCH-3 more times
628
- //#endif
629
-
630
- }
631
- } else {
632
- // No match, output a literal byte */
633
- flush = ct_tally(0, window[strstart] & 0xff);
634
- lookahead--;
635
- strstart++;
636
- }
637
- if (flush) {
638
- flush_block(0);
639
- block_start = strstart;
640
- }
641
-
642
- // Make sure that we always have enough lookahead, except
643
- // at the end of the input file. We need MAX_MATCH bytes
644
- // for the next match, plus MIN_MATCH bytes to insert the
645
- // string following the next match.
646
- while (lookahead < MIN_LOOKAHEAD && !eofile) {
647
- fill_window();
648
- }
649
- }
650
- }
651
-
652
- function deflate_better() {
653
- // Process the input block. */
654
- while (lookahead !== 0 && qhead === null) {
655
- // Insert the string window[strstart .. strstart+2] in the
656
- // dictionary, and set hash_head to the head of the hash chain:
657
- INSERT_STRING();
658
-
659
- // Find the longest match, discarding those <= prev_length.
660
- prev_length = match_length;
661
- prev_match = match_start;
662
- match_length = MIN_MATCH - 1;
663
-
664
- if (hash_head !== NIL && prev_length < max_lazy_match && strstart - hash_head <= MAX_DIST) {
665
- // To simplify the code, we prevent matches with the string
666
- // of window index 0 (in particular we have to avoid a match
667
- // of the string with itself at the start of the input file).
668
- match_length = longest_match(hash_head);
669
- // longest_match() sets match_start */
670
- if (match_length > lookahead) {
671
- match_length = lookahead;
672
- }
673
-
674
- // Ignore a length 3 match if it is too distant: */
675
- if (match_length === MIN_MATCH && strstart - match_start > TOO_FAR) {
676
- // If prev_match is also MIN_MATCH, match_start is garbage
677
- // but we will ignore the current match anyway.
678
- match_length--;
679
- }
680
- }
681
- // If there was a match at the previous step and the current
682
- // match is not better, output the previous match:
683
- if (prev_length >= MIN_MATCH && match_length <= prev_length) {
684
- var flush; // set if current block must be flushed
685
-
686
- // check_match(strstart - 1, prev_match, prev_length);
687
- flush = ct_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH);
688
-
689
- // Insert in hash table all strings up to the end of the match.
690
- // strstart-1 and strstart are already inserted.
691
- lookahead -= prev_length - 1;
692
- prev_length -= 2;
693
- do {
694
- strstart++;
695
- INSERT_STRING();
696
- // strstart never exceeds WSIZE-MAX_MATCH, so there are
697
- // always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
698
- // these bytes are garbage, but it does not matter since the
699
- // next lookahead bytes will always be emitted as literals.
700
- } while (--prev_length !== 0);
701
- match_available = false;
702
- match_length = MIN_MATCH - 1;
703
- strstart++;
704
- if (flush) {
705
- flush_block(0);
706
- block_start = strstart;
707
- }
708
- } else if (match_available) {
709
- // If there was no match at the previous position, output a
710
- // single literal. If there was a match but the current match
711
- // is longer, truncate the previous match to a single literal.
712
- if (ct_tally(0, window[strstart - 1] & 0xff)) {
713
- flush_block(0);
714
- block_start = strstart;
715
- }
716
- strstart++;
717
- lookahead--;
718
- } else {
719
- // There is no previous match to compare with, wait for
720
- // the next step to decide.
721
- match_available = true;
722
- strstart++;
723
- lookahead--;
724
- }
725
-
726
- // Make sure that we always have enough lookahead, except
727
- // at the end of the input file. We need MAX_MATCH bytes
728
- // for the next match, plus MIN_MATCH bytes to insert the
729
- // string following the next match.
730
- while (lookahead < MIN_LOOKAHEAD && !eofile) {
731
- fill_window();
732
- }
733
- }
734
- }
735
-
736
- function init_deflate() {
737
- if (eofile) {
738
- return;
739
- }
740
- bi_buf = 0;
741
- bi_valid = 0;
742
- ct_init();
743
- lm_init();
744
-
745
- qhead = null;
746
- outcnt = 0;
747
- outoff = 0;
748
-
749
- if (compr_level <= 3) {
750
- prev_length = MIN_MATCH - 1;
751
- match_length = 0;
752
- } else {
753
- match_length = MIN_MATCH - 1;
754
- match_available = false;
755
- }
756
-
757
- complete = false;
758
- }
759
-
760
- /* ==========================================================================
761
- * Same as above, but achieves better compression. We use a lazy
762
- * evaluation for matches: a match is finally adopted only if there is
763
- * no better match at the next window position.
764
- */
765
- function deflate_internal(buff, off, buff_size) {
766
- var n;
767
-
768
- if (!initflag) {
769
- init_deflate();
770
- initflag = true;
771
- if (lookahead === 0) { // empty
772
- complete = true;
773
- return 0;
774
- }
775
- }
776
-
777
- n = qcopy(buff, off, buff_size);
778
- if (n === buff_size) {
779
- return buff_size;
780
- }
781
-
782
- if (complete) {
783
- return n;
784
- }
785
-
786
- if (compr_level <= 3) {
787
- // optimized for speed
788
- deflate_fast();
789
- } else {
790
- deflate_better();
791
- }
792
-
793
- if (lookahead === 0) {
794
- if (match_available) {
795
- ct_tally(0, window[strstart - 1] & 0xff);
796
- }
797
- flush_block(1);
798
- complete = true;
799
- }
800
-
801
- return n + qcopy(buff, n + off, buff_size - n);
802
- }
803
-
804
- function qcopy(buff, off, buff_size) {
805
- var n, i, j;
806
-
807
- n = 0;
808
- while (qhead !== null && n < buff_size) {
809
- i = buff_size - n;
810
- if (i > qhead.len) {
811
- i = qhead.len;
812
- }
813
- // System.arraycopy(qhead.ptr, qhead.off, buff, off + n, i);
814
- for (j = 0; j < i; j++) {
815
- buff[off + n + j] = qhead.ptr[qhead.off + j];
816
- }
817
-
818
- qhead.off += i;
819
- qhead.len -= i;
820
- n += i;
821
- if (qhead.len === 0) {
822
- var p;
823
- p = qhead;
824
- qhead = qhead.next;
825
- reuse_queue(p);
826
- }
827
- }
828
-
829
- if (n === buff_size) {
830
- return n;
831
- }
832
-
833
- if (outoff < outcnt) {
834
- i = buff_size - n;
835
- if (i > outcnt - outoff) {
836
- i = outcnt - outoff;
837
- }
838
- // System.arraycopy(outbuf, outoff, buff, off + n, i);
839
- for (j = 0; j < i; j++) {
840
- buff[off + n + j] = outbuf[outoff + j];
841
- }
842
- outoff += i;
843
- n += i;
844
- if (outcnt === outoff) {
845
- outcnt = outoff = 0;
846
- }
847
- }
848
- return n;
849
- }
850
-
851
- /* ==========================================================================
852
- * Allocate the match buffer, initialize the various tables and save the
853
- * location of the internal file attribute (ascii/binary) and method
854
- * (DEFLATE/STORE).
855
- */
856
- function ct_init() {
857
- var n; // iterates over tree elements
858
- var bits; // bit counter
859
- var length; // length value
860
- var code; // code value
861
- var dist; // distance index
862
-
863
- if (static_dtree[0].dl !== 0) {
864
- return; // ct_init already called
865
- }
866
-
867
- l_desc.dyn_tree = dyn_ltree;
868
- l_desc.static_tree = static_ltree;
869
- l_desc.extra_bits = extra_lbits;
870
- l_desc.extra_base = LITERALS + 1;
871
- l_desc.elems = L_CODES;
872
- l_desc.max_length = MAX_BITS;
873
- l_desc.max_code = 0;
874
-
875
- d_desc.dyn_tree = dyn_dtree;
876
- d_desc.static_tree = static_dtree;
877
- d_desc.extra_bits = extra_dbits;
878
- d_desc.extra_base = 0;
879
- d_desc.elems = D_CODES;
880
- d_desc.max_length = MAX_BITS;
881
- d_desc.max_code = 0;
882
-
883
- bl_desc.dyn_tree = bl_tree;
884
- bl_desc.static_tree = null;
885
- bl_desc.extra_bits = extra_blbits;
886
- bl_desc.extra_base = 0;
887
- bl_desc.elems = BL_CODES;
888
- bl_desc.max_length = MAX_BL_BITS;
889
- bl_desc.max_code = 0;
890
-
891
- // Initialize the mapping length (0..255) -> length code (0..28)
892
- length = 0;
893
- for (code = 0; code < LENGTH_CODES - 1; code++) {
894
- base_length[code] = length;
895
- for (n = 0; n < (1 << extra_lbits[code]); n++) {
896
- length_code[length++] = code;
897
- }
898
- }
899
- // Assert (length === 256, "ct_init: length !== 256");
900
-
901
- // Note that the length 255 (match length 258) can be represented
902
- // in two different ways: code 284 + 5 bits or code 285, so we
903
- // overwrite length_code[255] to use the best encoding:
904
- length_code[length - 1] = code;
905
-
906
- // Initialize the mapping dist (0..32K) -> dist code (0..29) */
907
- dist = 0;
908
- for (code = 0; code < 16; code++) {
909
- base_dist[code] = dist;
910
- for (n = 0; n < (1 << extra_dbits[code]); n++) {
911
- dist_code[dist++] = code;
912
- }
913
- }
914
- // Assert (dist === 256, "ct_init: dist !== 256");
915
- // from now on, all distances are divided by 128
916
- for (dist >>= 7; code < D_CODES; code++) {
917
- base_dist[code] = dist << 7;
918
- for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
919
- dist_code[256 + dist++] = code;
920
- }
921
- }
922
- // Assert (dist === 256, "ct_init: 256+dist !== 512");
923
-
924
- // Construct the codes of the static literal tree
925
- for (bits = 0; bits <= MAX_BITS; bits++) {
926
- bl_count[bits] = 0;
927
- }
928
- n = 0;
929
- while (n <= 143) {
930
- static_ltree[n++].dl = 8;
931
- bl_count[8]++;
932
- }
933
- while (n <= 255) {
934
- static_ltree[n++].dl = 9;
935
- bl_count[9]++;
936
- }
937
- while (n <= 279) {
938
- static_ltree[n++].dl = 7;
939
- bl_count[7]++;
940
- }
941
- while (n <= 287) {
942
- static_ltree[n++].dl = 8;
943
- bl_count[8]++;
944
- }
945
- // Codes 286 and 287 do not exist, but we must include them in the
946
- // tree construction to get a canonical Huffman tree (longest code
947
- // all ones)
948
- gen_codes(static_ltree, L_CODES + 1);
949
-
950
- // The static distance tree is trivial: */
951
- for (n = 0; n < D_CODES; n++) {
952
- static_dtree[n].dl = 5;
953
- static_dtree[n].fc = bi_reverse(n, 5);
954
- }
955
-
956
- // Initialize the first block of the first file:
957
- init_block();
958
- }
959
-
960
- /* ==========================================================================
961
- * Initialize a new block.
962
- */
963
- function init_block() {
964
- var n; // iterates over tree elements
965
-
966
- // Initialize the trees.
967
- for (n = 0; n < L_CODES; n++) {
968
- dyn_ltree[n].fc = 0;
969
- }
970
- for (n = 0; n < D_CODES; n++) {
971
- dyn_dtree[n].fc = 0;
972
- }
973
- for (n = 0; n < BL_CODES; n++) {
974
- bl_tree[n].fc = 0;
975
- }
976
-
977
- dyn_ltree[END_BLOCK].fc = 1;
978
- opt_len = static_len = 0;
979
- last_lit = last_dist = last_flags = 0;
980
- flags = 0;
981
- flag_bit = 1;
982
- }
983
-
984
- /* ==========================================================================
985
- * Restore the heap property by moving down the tree starting at node k,
986
- * exchanging a node with the smallest of its two sons if necessary, stopping
987
- * when the heap property is re-established (each father smaller than its
988
- * two sons).
989
- *
990
- * @param tree- tree to restore
991
- * @param k- node to move down
992
- */
993
- function pqdownheap(tree, k) {
994
- var v = heap[k],
995
- j = k << 1; // left son of k
996
-
997
- while (j <= heap_len) {
998
- // Set j to the smallest of the two sons:
999
- if (j < heap_len && SMALLER(tree, heap[j + 1], heap[j])) {
1000
- j++;
1001
- }
1002
-
1003
- // Exit if v is smaller than both sons
1004
- if (SMALLER(tree, v, heap[j])) {
1005
- break;
1006
- }
1007
-
1008
- // Exchange v with the smallest son
1009
- heap[k] = heap[j];
1010
- k = j;
1011
-
1012
- // And continue down the tree, setting j to the left son of k
1013
- j <<= 1;
1014
- }
1015
- heap[k] = v;
1016
- }
1017
-
1018
- /* ==========================================================================
1019
- * Compute the optimal bit lengths for a tree and update the total bit length
1020
- * for the current block.
1021
- * IN assertion: the fields freq and dad are set, heap[heap_max] and
1022
- * above are the tree nodes sorted by increasing frequency.
1023
- * OUT assertions: the field len is set to the optimal bit length, the
1024
- * array bl_count contains the frequencies for each bit length.
1025
- * The length opt_len is updated; static_len is also updated if stree is
1026
- * not null.
1027
- */
1028
- function gen_bitlen(desc) { // the tree descriptor
1029
- var tree = desc.dyn_tree;
1030
- var extra = desc.extra_bits;
1031
- var base = desc.extra_base;
1032
- var max_code = desc.max_code;
1033
- var max_length = desc.max_length;
1034
- var stree = desc.static_tree;
1035
- var h; // heap index
1036
- var n, m; // iterate over the tree elements
1037
- var bits; // bit length
1038
- var xbits; // extra bits
1039
- var f; // frequency
1040
- var overflow = 0; // number of elements with bit length too large
1041
-
1042
- for (bits = 0; bits <= MAX_BITS; bits++) {
1043
- bl_count[bits] = 0;
1044
- }
1045
-
1046
- // In a first pass, compute the optimal bit lengths (which may
1047
- // overflow in the case of the bit length tree).
1048
- tree[heap[heap_max]].dl = 0; // root of the heap
1049
-
1050
- for (h = heap_max + 1; h < HEAP_SIZE; h++) {
1051
- n = heap[h];
1052
- bits = tree[tree[n].dl].dl + 1;
1053
- if (bits > max_length) {
1054
- bits = max_length;
1055
- overflow++;
1056
- }
1057
- tree[n].dl = bits;
1058
- // We overwrite tree[n].dl which is no longer needed
1059
-
1060
- if (n > max_code) {
1061
- continue; // not a leaf node
1062
- }
1063
-
1064
- bl_count[bits]++;
1065
- xbits = 0;
1066
- if (n >= base) {
1067
- xbits = extra[n - base];
1068
- }
1069
- f = tree[n].fc;
1070
- opt_len += f * (bits + xbits);
1071
- if (stree !== null) {
1072
- static_len += f * (stree[n].dl + xbits);
1073
- }
1074
- }
1075
- if (overflow === 0) {
1076
- return;
1077
- }
1078
-
1079
- // This happens for example on obj2 and pic of the Calgary corpus
1080
-
1081
- // Find the first bit length which could increase:
1082
- do {
1083
- bits = max_length - 1;
1084
- while (bl_count[bits] === 0) {
1085
- bits--;
1086
- }
1087
- bl_count[bits]--; // move one leaf down the tree
1088
- bl_count[bits + 1] += 2; // move one overflow item as its brother
1089
- bl_count[max_length]--;
1090
- // The brother of the overflow item also moves one step up,
1091
- // but this does not affect bl_count[max_length]
1092
- overflow -= 2;
1093
- } while (overflow > 0);
1094
-
1095
- // Now recompute all bit lengths, scanning in increasing frequency.
1096
- // h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
1097
- // lengths instead of fixing only the wrong ones. This idea is taken
1098
- // from 'ar' written by Haruhiko Okumura.)
1099
- for (bits = max_length; bits !== 0; bits--) {
1100
- n = bl_count[bits];
1101
- while (n !== 0) {
1102
- m = heap[--h];
1103
- if (m > max_code) {
1104
- continue;
1105
- }
1106
- if (tree[m].dl !== bits) {
1107
- opt_len += (bits - tree[m].dl) * tree[m].fc;
1108
- tree[m].fc = bits;
1109
- }
1110
- n--;
1111
- }
1112
- }
1113
- }
1114
-
1115
- /* ==========================================================================
1116
- * Generate the codes for a given tree and bit counts (which need not be
1117
- * optimal).
1118
- * IN assertion: the array bl_count contains the bit length statistics for
1119
- * the given tree and the field len is set for all tree elements.
1120
- * OUT assertion: the field code is set for all tree elements of non
1121
- * zero code length.
1122
- * @param tree- the tree to decorate
1123
- * @param max_code- largest code with non-zero frequency
1124
- */
1125
- function gen_codes(tree, max_code) {
1126
- var next_code = []; // new Array(MAX_BITS + 1); // next code value for each bit length
1127
- var code = 0; // running code value
1128
- var bits; // bit index
1129
- var n; // code index
1130
-
1131
- // The distribution counts are first used to generate the code values
1132
- // without bit reversal.
1133
- for (bits = 1; bits <= MAX_BITS; bits++) {
1134
- code = ((code + bl_count[bits - 1]) << 1);
1135
- next_code[bits] = code;
1136
- }
1137
-
1138
- // Check that the bit counts in bl_count are consistent. The last code
1139
- // must be all ones.
1140
- // Assert (code + encoder->bl_count[MAX_BITS]-1 === (1<<MAX_BITS)-1, "inconsistent bit counts");
1141
- // Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
1142
-
1143
- for (n = 0; n <= max_code; n++) {
1144
- var len = tree[n].dl;
1145
- if (len === 0) {
1146
- continue;
1147
- }
1148
- // Now reverse the bits
1149
- tree[n].fc = bi_reverse(next_code[len]++, len);
1150
-
1151
- // Tracec(tree !== static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", n, (isgraph(n) ? n : ' '), len, tree[n].fc, next_code[len]-1));
1152
- }
1153
- }
1154
-
1155
- /* ==========================================================================
1156
- * Construct one Huffman tree and assigns the code bit strings and lengths.
1157
- * Update the total bit length for the current block.
1158
- * IN assertion: the field freq is set for all tree elements.
1159
- * OUT assertions: the fields len and code are set to the optimal bit length
1160
- * and corresponding code. The length opt_len is updated; static_len is
1161
- * also updated if stree is not null. The field max_code is set.
1162
- */
1163
- function build_tree(desc) { // the tree descriptor
1164
- var tree = desc.dyn_tree;
1165
- var stree = desc.static_tree;
1166
- var elems = desc.elems;
1167
- var n, m; // iterate over heap elements
1168
- var max_code = -1; // largest code with non zero frequency
1169
- var node = elems; // next internal node of the tree
1170
-
1171
- // Construct the initial heap, with least frequent element in
1172
- // heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
1173
- // heap[0] is not used.
1174
- heap_len = 0;
1175
- heap_max = HEAP_SIZE;
1176
-
1177
- for (n = 0; n < elems; n++) {
1178
- if (tree[n].fc !== 0) {
1179
- heap[++heap_len] = max_code = n;
1180
- depth[n] = 0;
1181
- } else {
1182
- tree[n].dl = 0;
1183
- }
1184
- }
1185
-
1186
- // The pkzip format requires that at least one distance code exists,
1187
- // and that at least one bit should be sent even if there is only one
1188
- // possible code. So to avoid special checks later on we force at least
1189
- // two codes of non zero frequency.
1190
- while (heap_len < 2) {
1191
- var xnew = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
1192
- tree[xnew].fc = 1;
1193
- depth[xnew] = 0;
1194
- opt_len--;
1195
- if (stree !== null) {
1196
- static_len -= stree[xnew].dl;
1197
- }
1198
- // new is 0 or 1 so it does not have extra bits
1199
- }
1200
- desc.max_code = max_code;
1201
-
1202
- // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
1203
- // establish sub-heaps of increasing lengths:
1204
- for (n = heap_len >> 1; n >= 1; n--) {
1205
- pqdownheap(tree, n);
1206
- }
1207
-
1208
- // Construct the Huffman tree by repeatedly combining the least two
1209
- // frequent nodes.
1210
- do {
1211
- n = heap[SMALLEST];
1212
- heap[SMALLEST] = heap[heap_len--];
1213
- pqdownheap(tree, SMALLEST);
1214
-
1215
- m = heap[SMALLEST]; // m = node of next least frequency
1216
-
1217
- // keep the nodes sorted by frequency
1218
- heap[--heap_max] = n;
1219
- heap[--heap_max] = m;
1220
-
1221
- // Create a new node father of n and m
1222
- tree[node].fc = tree[n].fc + tree[m].fc;
1223
- // depth[node] = (char)(MAX(depth[n], depth[m]) + 1);
1224
- if (depth[n] > depth[m] + 1) {
1225
- depth[node] = depth[n];
1226
- } else {
1227
- depth[node] = depth[m] + 1;
1228
- }
1229
- tree[n].dl = tree[m].dl = node;
1230
-
1231
- // and insert the new node in the heap
1232
- heap[SMALLEST] = node++;
1233
- pqdownheap(tree, SMALLEST);
1234
-
1235
- } while (heap_len >= 2);
1236
-
1237
- heap[--heap_max] = heap[SMALLEST];
1238
-
1239
- // At this point, the fields freq and dad are set. We can now
1240
- // generate the bit lengths.
1241
- gen_bitlen(desc);
1242
-
1243
- // The field len is now set, we can generate the bit codes
1244
- gen_codes(tree, max_code);
1245
- }
1246
-
1247
- /* ==========================================================================
1248
- * Scan a literal or distance tree to determine the frequencies of the codes
1249
- * in the bit length tree. Updates opt_len to take into account the repeat
1250
- * counts. (The contribution of the bit length codes will be added later
1251
- * during the construction of bl_tree.)
1252
- *
1253
- * @param tree- the tree to be scanned
1254
- * @param max_code- and its largest code of non zero frequency
1255
- */
1256
- function scan_tree(tree, max_code) {
1257
- var n, // iterates over all tree elements
1258
- prevlen = -1, // last emitted length
1259
- curlen, // length of current code
1260
- nextlen = tree[0].dl, // length of next code
1261
- count = 0, // repeat count of the current code
1262
- max_count = 7, // max repeat count
1263
- min_count = 4; // min repeat count
1264
-
1265
- if (nextlen === 0) {
1266
- max_count = 138;
1267
- min_count = 3;
1268
- }
1269
- tree[max_code + 1].dl = 0xffff; // guard
1270
-
1271
- for (n = 0; n <= max_code; n++) {
1272
- curlen = nextlen;
1273
- nextlen = tree[n + 1].dl;
1274
- if (++count < max_count && curlen === nextlen) {
1275
- continue;
1276
- } else if (count < min_count) {
1277
- bl_tree[curlen].fc += count;
1278
- } else if (curlen !== 0) {
1279
- if (curlen !== prevlen) {
1280
- bl_tree[curlen].fc++;
1281
- }
1282
- bl_tree[REP_3_6].fc++;
1283
- } else if (count <= 10) {
1284
- bl_tree[REPZ_3_10].fc++;
1285
- } else {
1286
- bl_tree[REPZ_11_138].fc++;
1287
- }
1288
- count = 0; prevlen = curlen;
1289
- if (nextlen === 0) {
1290
- max_count = 138;
1291
- min_count = 3;
1292
- } else if (curlen === nextlen) {
1293
- max_count = 6;
1294
- min_count = 3;
1295
- } else {
1296
- max_count = 7;
1297
- min_count = 4;
1298
- }
1299
- }
1300
- }
1301
-
1302
- /* ==========================================================================
1303
- * Send a literal or distance tree in compressed form, using the codes in
1304
- * bl_tree.
1305
- *
1306
- * @param tree- the tree to be scanned
1307
- * @param max_code- and its largest code of non zero frequency
1308
- */
1309
- function send_tree(tree, max_code) {
1310
- var n; // iterates over all tree elements
1311
- var prevlen = -1; // last emitted length
1312
- var curlen; // length of current code
1313
- var nextlen = tree[0].dl; // length of next code
1314
- var count = 0; // repeat count of the current code
1315
- var max_count = 7; // max repeat count
1316
- var min_count = 4; // min repeat count
1317
-
1318
- // tree[max_code+1].dl = -1; */ /* guard already set */
1319
- if (nextlen === 0) {
1320
- max_count = 138;
1321
- min_count = 3;
1322
- }
1323
-
1324
- for (n = 0; n <= max_code; n++) {
1325
- curlen = nextlen;
1326
- nextlen = tree[n + 1].dl;
1327
- if (++count < max_count && curlen === nextlen) {
1328
- continue;
1329
- } else if (count < min_count) {
1330
- do {
1331
- SEND_CODE(curlen, bl_tree);
1332
- } while (--count !== 0);
1333
- } else if (curlen !== 0) {
1334
- if (curlen !== prevlen) {
1335
- SEND_CODE(curlen, bl_tree);
1336
- count--;
1337
- }
1338
- // Assert(count >= 3 && count <= 6, " 3_6?");
1339
- SEND_CODE(REP_3_6, bl_tree);
1340
- send_bits(count - 3, 2);
1341
- } else if (count <= 10) {
1342
- SEND_CODE(REPZ_3_10, bl_tree);
1343
- send_bits(count - 3, 3);
1344
- } else {
1345
- SEND_CODE(REPZ_11_138, bl_tree);
1346
- send_bits(count - 11, 7);
1347
- }
1348
- count = 0;
1349
- prevlen = curlen;
1350
- if (nextlen === 0) {
1351
- max_count = 138;
1352
- min_count = 3;
1353
- } else if (curlen === nextlen) {
1354
- max_count = 6;
1355
- min_count = 3;
1356
- } else {
1357
- max_count = 7;
1358
- min_count = 4;
1359
- }
1360
- }
1361
- }
1362
-
1363
- /* ==========================================================================
1364
- * Construct the Huffman tree for the bit lengths and return the index in
1365
- * bl_order of the last bit length code to send.
1366
- */
1367
- function build_bl_tree() {
1368
- var max_blindex; // index of last bit length code of non zero freq
1369
-
1370
- // Determine the bit length frequencies for literal and distance trees
1371
- scan_tree(dyn_ltree, l_desc.max_code);
1372
- scan_tree(dyn_dtree, d_desc.max_code);
1373
-
1374
- // Build the bit length tree:
1375
- build_tree(bl_desc);
1376
- // opt_len now includes the length of the tree representations, except
1377
- // the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
1378
-
1379
- // Determine the number of bit length codes to send. The pkzip format
1380
- // requires that at least 4 bit length codes be sent. (appnote.txt says
1381
- // 3 but the actual value used is 4.)
1382
- for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
1383
- if (bl_tree[bl_order[max_blindex]].dl !== 0) {
1384
- break;
1385
- }
1386
- }
1387
- // Update opt_len to include the bit length tree and counts */
1388
- opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
1389
- // Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
1390
- // encoder->opt_len, encoder->static_len));
1391
-
1392
- return max_blindex;
1393
- }
1394
-
1395
- /* ==========================================================================
1396
- * Send the header for a block using dynamic Huffman trees: the counts, the
1397
- * lengths of the bit length codes, the literal tree and the distance tree.
1398
- * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
1399
- */
1400
- function send_all_trees(lcodes, dcodes, blcodes) { // number of codes for each tree
1401
- var rank; // index in bl_order
1402
-
1403
- // Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
1404
- // Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, "too many codes");
1405
- // Tracev((stderr, "\nbl counts: "));
1406
- send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt
1407
- send_bits(dcodes - 1, 5);
1408
- send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt
1409
- for (rank = 0; rank < blcodes; rank++) {
1410
- // Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
1411
- send_bits(bl_tree[bl_order[rank]].dl, 3);
1412
- }
1413
-
1414
- // send the literal tree
1415
- send_tree(dyn_ltree, lcodes - 1);
1416
-
1417
- // send the distance tree
1418
- send_tree(dyn_dtree, dcodes - 1);
1419
- }
1420
-
1421
- /* ==========================================================================
1422
- * Determine the best encoding for the current block: dynamic trees, static
1423
- * trees or store, and output the encoded block to the zip file.
1424
- */
1425
- function flush_block(eof) { // true if this is the last block for a file
1426
- var opt_lenb, static_lenb, // opt_len and static_len in bytes
1427
- max_blindex, // index of last bit length code of non zero freq
1428
- stored_len, // length of input block
1429
- i;
1430
-
1431
- stored_len = strstart - block_start;
1432
- flag_buf[last_flags] = flags; // Save the flags for the last 8 items
1433
-
1434
- // Construct the literal and distance trees
1435
- build_tree(l_desc);
1436
- // Tracev((stderr, "\nlit data: dyn %ld, stat %ld",
1437
- // encoder->opt_len, encoder->static_len));
1438
-
1439
- build_tree(d_desc);
1440
- // Tracev((stderr, "\ndist data: dyn %ld, stat %ld",
1441
- // encoder->opt_len, encoder->static_len));
1442
- // At this point, opt_len and static_len are the total bit lengths of
1443
- // the compressed block data, excluding the tree representations.
1444
-
1445
- // Build the bit length tree for the above two trees, and get the index
1446
- // in bl_order of the last bit length code to send.
1447
- max_blindex = build_bl_tree();
1448
-
1449
- // Determine the best encoding. Compute first the block length in bytes
1450
- opt_lenb = (opt_len + 3 + 7) >> 3;
1451
- static_lenb = (static_len + 3 + 7) >> 3;
1452
-
1453
- // Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ", opt_lenb, encoder->opt_len, static_lenb, encoder->static_len, stored_len, encoder->last_lit, encoder->last_dist));
1454
-
1455
- if (static_lenb <= opt_lenb) {
1456
- opt_lenb = static_lenb;
1457
- }
1458
- if (stored_len + 4 <= opt_lenb && block_start >= 0) { // 4: two words for the lengths
1459
- // The test buf !== NULL is only necessary if LIT_BUFSIZE > WSIZE.
1460
- // Otherwise we can't have processed more than WSIZE input bytes since
1461
- // the last block flush, because compression would have been
1462
- // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
1463
- // transform a block into a stored block.
1464
- send_bits((STORED_BLOCK << 1) + eof, 3); /* send block type */
1465
- bi_windup(); /* align on byte boundary */
1466
- put_short(stored_len);
1467
- put_short(~stored_len);
1468
-
1469
- // copy block
1470
- /*
1471
- p = &window[block_start];
1472
- for (i = 0; i < stored_len; i++) {
1473
- put_byte(p[i]);
1474
- }
1475
- */
1476
- for (i = 0; i < stored_len; i++) {
1477
- put_byte(window[block_start + i]);
1478
- }
1479
- } else if (static_lenb === opt_lenb) {
1480
- send_bits((STATIC_TREES << 1) + eof, 3);
1481
- compress_block(static_ltree, static_dtree);
1482
- } else {
1483
- send_bits((DYN_TREES << 1) + eof, 3);
1484
- send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1);
1485
- compress_block(dyn_ltree, dyn_dtree);
1486
- }
1487
-
1488
- init_block();
1489
-
1490
- if (eof !== 0) {
1491
- bi_windup();
1492
- }
1493
- }
1494
-
1495
- /* ==========================================================================
1496
- * Save the match info and tally the frequency counts. Return true if
1497
- * the current block must be flushed.
1498
- *
1499
- * @param dist- distance of matched string
1500
- * @param lc- (match length - MIN_MATCH) or unmatched char (if dist === 0)
1501
- */
1502
- function ct_tally(dist, lc) {
1503
- l_buf[last_lit++] = lc;
1504
- if (dist === 0) {
1505
- // lc is the unmatched char
1506
- dyn_ltree[lc].fc++;
1507
- } else {
1508
- // Here, lc is the match length - MIN_MATCH
1509
- dist--; // dist = match distance - 1
1510
- // Assert((ush)dist < (ush)MAX_DIST && (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && (ush)D_CODE(dist) < (ush)D_CODES, "ct_tally: bad match");
1511
-
1512
- dyn_ltree[length_code[lc] + LITERALS + 1].fc++;
1513
- dyn_dtree[D_CODE(dist)].fc++;
1514
-
1515
- d_buf[last_dist++] = dist;
1516
- flags |= flag_bit;
1517
- }
1518
- flag_bit <<= 1;
1519
-
1520
- // Output the flags if they fill a byte
1521
- if ((last_lit & 7) === 0) {
1522
- flag_buf[last_flags++] = flags;
1523
- flags = 0;
1524
- flag_bit = 1;
1525
- }
1526
- // Try to guess if it is profitable to stop the current block here
1527
- if (compr_level > 2 && (last_lit & 0xfff) === 0) {
1528
- // Compute an upper bound for the compressed length
1529
- var out_length = last_lit * 8;
1530
- var in_length = strstart - block_start;
1531
- var dcode;
1532
-
1533
- for (dcode = 0; dcode < D_CODES; dcode++) {
1534
- out_length += dyn_dtree[dcode].fc * (5 + extra_dbits[dcode]);
1535
- }
1536
- out_length >>= 3;
1537
- // Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ", encoder->last_lit, encoder->last_dist, in_length, out_length, 100L - out_length*100L/in_length));
1538
- if (last_dist < parseInt(last_lit / 2, 10) && out_length < parseInt(in_length / 2, 10)) {
1539
- return true;
1540
- }
1541
- }
1542
- return (last_lit === LIT_BUFSIZE - 1 || last_dist === DIST_BUFSIZE);
1543
- // We avoid equality with LIT_BUFSIZE because of wraparound at 64K
1544
- // on 16 bit machines and because stored blocks are restricted to
1545
- // 64K-1 bytes.
1546
- }
1547
-
1548
- /* ==========================================================================
1549
- * Send the block data compressed using the given Huffman trees
1550
- *
1551
- * @param ltree- literal tree
1552
- * @param dtree- distance tree
1553
- */
1554
- function compress_block(ltree, dtree) {
1555
- var dist; // distance of matched string
1556
- var lc; // match length or unmatched char (if dist === 0)
1557
- var lx = 0; // running index in l_buf
1558
- var dx = 0; // running index in d_buf
1559
- var fx = 0; // running index in flag_buf
1560
- var flag = 0; // current flags
1561
- var code; // the code to send
1562
- var extra; // number of extra bits to send
1563
-
1564
- if (last_lit !== 0) {
1565
- do {
1566
- if ((lx & 7) === 0) {
1567
- flag = flag_buf[fx++];
1568
- }
1569
- lc = l_buf[lx++] & 0xff;
1570
- if ((flag & 1) === 0) {
1571
- SEND_CODE(lc, ltree); /* send a literal byte */
1572
- // Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1573
- } else {
1574
- // Here, lc is the match length - MIN_MATCH
1575
- code = length_code[lc];
1576
- SEND_CODE(code + LITERALS + 1, ltree); // send the length code
1577
- extra = extra_lbits[code];
1578
- if (extra !== 0) {
1579
- lc -= base_length[code];
1580
- send_bits(lc, extra); // send the extra length bits
1581
- }
1582
- dist = d_buf[dx++];
1583
- // Here, dist is the match distance - 1
1584
- code = D_CODE(dist);
1585
- // Assert (code < D_CODES, "bad d_code");
1586
-
1587
- SEND_CODE(code, dtree); // send the distance code
1588
- extra = extra_dbits[code];
1589
- if (extra !== 0) {
1590
- dist -= base_dist[code];
1591
- send_bits(dist, extra); // send the extra distance bits
1592
- }
1593
- } // literal or match pair ?
1594
- flag >>= 1;
1595
- } while (lx < last_lit);
1596
- }
1597
-
1598
- SEND_CODE(END_BLOCK, ltree);
1599
- }
1600
-
1601
- /* ==========================================================================
1602
- * Send a value on a given number of bits.
1603
- * IN assertion: length <= 16 and value fits in length bits.
1604
- *
1605
- * @param value- value to send
1606
- * @param length- number of bits
1607
- */
1608
- var Buf_size = 16; // bit size of bi_buf
1609
- function send_bits(value, length) {
1610
- // If not enough room in bi_buf, use (valid) bits from bi_buf and
1611
- // (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
1612
- // unused bits in value.
1613
- if (bi_valid > Buf_size - length) {
1614
- bi_buf |= (value << bi_valid);
1615
- put_short(bi_buf);
1616
- bi_buf = (value >> (Buf_size - bi_valid));
1617
- bi_valid += length - Buf_size;
1618
- } else {
1619
- bi_buf |= value << bi_valid;
1620
- bi_valid += length;
1621
- }
1622
- }
1623
-
1624
- /* ==========================================================================
1625
- * Reverse the first len bits of a code, using straightforward code (a faster
1626
- * method would use a table)
1627
- * IN assertion: 1 <= len <= 15
1628
- *
1629
- * @param code- the value to invert
1630
- * @param len- its bit length
1631
- */
1632
- function bi_reverse(code, len) {
1633
- var res = 0;
1634
- do {
1635
- res |= code & 1;
1636
- code >>= 1;
1637
- res <<= 1;
1638
- } while (--len > 0);
1639
- return res >> 1;
1640
- }
1641
-
1642
- /* ==========================================================================
1643
- * Write out any remaining bits in an incomplete byte.
1644
- */
1645
- function bi_windup() {
1646
- if (bi_valid > 8) {
1647
- put_short(bi_buf);
1648
- } else if (bi_valid > 0) {
1649
- put_byte(bi_buf);
1650
- }
1651
- bi_buf = 0;
1652
- bi_valid = 0;
1653
- }
1654
-
1655
- function qoutbuf() {
1656
- var q, i;
1657
- if (outcnt !== 0) {
1658
- q = new_queue();
1659
- if (qhead === null) {
1660
- qhead = qtail = q;
1661
- } else {
1662
- qtail = qtail.next = q;
1663
- }
1664
- q.len = outcnt - outoff;
1665
- // System.arraycopy(outbuf, outoff, q.ptr, 0, q.len);
1666
- for (i = 0; i < q.len; i++) {
1667
- q.ptr[i] = outbuf[outoff + i];
1668
- }
1669
- outcnt = outoff = 0;
1670
- }
1671
- }
1672
-
1673
- function deflate(arr, level) {
1674
- var i, j, buff;
1675
-
1676
- deflate_data = arr;
1677
- deflate_pos = 0;
1678
- if (typeof level === "undefined") {
1679
- level = DEFAULT_LEVEL;
1680
- }
1681
- deflate_start(level);
1682
-
1683
- buff = [];
1684
-
1685
- do {
1686
- i = deflate_internal(buff, buff.length, 1024);
1687
- } while (i > 0);
1688
-
1689
- deflate_data = null; // G.C.
1690
- return buff;
1691
- }