Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80550 views
1
'use strict';
2
3
var utils = require('../utils/common');
4
var trees = require('./trees');
5
var adler32 = require('./adler32');
6
var crc32 = require('./crc32');
7
var msg = require('./messages');
8
9
/* Public constants ==========================================================*/
10
/* ===========================================================================*/
11
12
13
/* Allowed flush values; see deflate() and inflate() below for details */
14
var Z_NO_FLUSH = 0;
15
var Z_PARTIAL_FLUSH = 1;
16
//var Z_SYNC_FLUSH = 2;
17
var Z_FULL_FLUSH = 3;
18
var Z_FINISH = 4;
19
var Z_BLOCK = 5;
20
//var Z_TREES = 6;
21
22
23
/* Return codes for the compression/decompression functions. Negative values
24
* are errors, positive values are used for special but normal events.
25
*/
26
var Z_OK = 0;
27
var Z_STREAM_END = 1;
28
//var Z_NEED_DICT = 2;
29
//var Z_ERRNO = -1;
30
var Z_STREAM_ERROR = -2;
31
var Z_DATA_ERROR = -3;
32
//var Z_MEM_ERROR = -4;
33
var Z_BUF_ERROR = -5;
34
//var Z_VERSION_ERROR = -6;
35
36
37
/* compression levels */
38
//var Z_NO_COMPRESSION = 0;
39
//var Z_BEST_SPEED = 1;
40
//var Z_BEST_COMPRESSION = 9;
41
var Z_DEFAULT_COMPRESSION = -1;
42
43
44
var Z_FILTERED = 1;
45
var Z_HUFFMAN_ONLY = 2;
46
var Z_RLE = 3;
47
var Z_FIXED = 4;
48
var Z_DEFAULT_STRATEGY = 0;
49
50
/* Possible values of the data_type field (though see inflate()) */
51
//var Z_BINARY = 0;
52
//var Z_TEXT = 1;
53
//var Z_ASCII = 1; // = Z_TEXT
54
var Z_UNKNOWN = 2;
55
56
57
/* The deflate compression method */
58
var Z_DEFLATED = 8;
59
60
/*============================================================================*/
61
62
63
var MAX_MEM_LEVEL = 9;
64
/* Maximum value for memLevel in deflateInit2 */
65
var MAX_WBITS = 15;
66
/* 32K LZ77 window */
67
var DEF_MEM_LEVEL = 8;
68
69
70
var LENGTH_CODES = 29;
71
/* number of length codes, not counting the special END_BLOCK code */
72
var LITERALS = 256;
73
/* number of literal bytes 0..255 */
74
var L_CODES = LITERALS + 1 + LENGTH_CODES;
75
/* number of Literal or Length codes, including the END_BLOCK code */
76
var D_CODES = 30;
77
/* number of distance codes */
78
var BL_CODES = 19;
79
/* number of codes used to transfer the bit lengths */
80
var HEAP_SIZE = 2*L_CODES + 1;
81
/* maximum heap size */
82
var MAX_BITS = 15;
83
/* All codes must not exceed MAX_BITS bits */
84
85
var MIN_MATCH = 3;
86
var MAX_MATCH = 258;
87
var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
88
89
var PRESET_DICT = 0x20;
90
91
var INIT_STATE = 42;
92
var EXTRA_STATE = 69;
93
var NAME_STATE = 73;
94
var COMMENT_STATE = 91;
95
var HCRC_STATE = 103;
96
var BUSY_STATE = 113;
97
var FINISH_STATE = 666;
98
99
var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
100
var BS_BLOCK_DONE = 2; /* block flush performed */
101
var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
102
var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
103
104
var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
105
106
function err(strm, errorCode) {
107
strm.msg = msg[errorCode];
108
return errorCode;
109
}
110
111
function rank(f) {
112
return ((f) << 1) - ((f) > 4 ? 9 : 0);
113
}
114
115
function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
116
117
118
/* =========================================================================
119
* Flush as much pending output as possible. All deflate() output goes
120
* through this function so some applications may wish to modify it
121
* to avoid allocating a large strm->output buffer and copying into it.
122
* (See also read_buf()).
123
*/
124
function flush_pending(strm) {
125
var s = strm.state;
126
127
//_tr_flush_bits(s);
128
var len = s.pending;
129
if (len > strm.avail_out) {
130
len = strm.avail_out;
131
}
132
if (len === 0) { return; }
133
134
utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
135
strm.next_out += len;
136
s.pending_out += len;
137
strm.total_out += len;
138
strm.avail_out -= len;
139
s.pending -= len;
140
if (s.pending === 0) {
141
s.pending_out = 0;
142
}
143
}
144
145
146
function flush_block_only (s, last) {
147
trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
148
s.block_start = s.strstart;
149
flush_pending(s.strm);
150
}
151
152
153
function put_byte(s, b) {
154
s.pending_buf[s.pending++] = b;
155
}
156
157
158
/* =========================================================================
159
* Put a short in the pending buffer. The 16-bit value is put in MSB order.
160
* IN assertion: the stream state is correct and there is enough room in
161
* pending_buf.
162
*/
163
function putShortMSB(s, b) {
164
// put_byte(s, (Byte)(b >> 8));
165
// put_byte(s, (Byte)(b & 0xff));
166
s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
167
s.pending_buf[s.pending++] = b & 0xff;
168
}
169
170
171
/* ===========================================================================
172
* Read a new buffer from the current input stream, update the adler32
173
* and total number of bytes read. All deflate() input goes through
174
* this function so some applications may wish to modify it to avoid
175
* allocating a large strm->input buffer and copying from it.
176
* (See also flush_pending()).
177
*/
178
function read_buf(strm, buf, start, size) {
179
var len = strm.avail_in;
180
181
if (len > size) { len = size; }
182
if (len === 0) { return 0; }
183
184
strm.avail_in -= len;
185
186
utils.arraySet(buf, strm.input, strm.next_in, len, start);
187
if (strm.state.wrap === 1) {
188
strm.adler = adler32(strm.adler, buf, len, start);
189
}
190
191
else if (strm.state.wrap === 2) {
192
strm.adler = crc32(strm.adler, buf, len, start);
193
}
194
195
strm.next_in += len;
196
strm.total_in += len;
197
198
return len;
199
}
200
201
202
/* ===========================================================================
203
* Set match_start to the longest match starting at the given string and
204
* return its length. Matches shorter or equal to prev_length are discarded,
205
* in which case the result is equal to prev_length and match_start is
206
* garbage.
207
* IN assertions: cur_match is the head of the hash chain for the current
208
* string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
209
* OUT assertion: the match length is not greater than s->lookahead.
210
*/
211
function longest_match(s, cur_match) {
212
var chain_length = s.max_chain_length; /* max hash chain length */
213
var scan = s.strstart; /* current string */
214
var match; /* matched string */
215
var len; /* length of current match */
216
var best_len = s.prev_length; /* best match length so far */
217
var nice_match = s.nice_match; /* stop if match long enough */
218
var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
219
s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
220
221
var _win = s.window; // shortcut
222
223
var wmask = s.w_mask;
224
var prev = s.prev;
225
226
/* Stop when cur_match becomes <= limit. To simplify the code,
227
* we prevent matches with the string of window index 0.
228
*/
229
230
var strend = s.strstart + MAX_MATCH;
231
var scan_end1 = _win[scan + best_len - 1];
232
var scan_end = _win[scan + best_len];
233
234
/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
235
* It is easy to get rid of this optimization if necessary.
236
*/
237
// Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
238
239
/* Do not waste too much time if we already have a good match: */
240
if (s.prev_length >= s.good_match) {
241
chain_length >>= 2;
242
}
243
/* Do not look for matches beyond the end of the input. This is necessary
244
* to make deflate deterministic.
245
*/
246
if (nice_match > s.lookahead) { nice_match = s.lookahead; }
247
248
// Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
249
250
do {
251
// Assert(cur_match < s->strstart, "no future");
252
match = cur_match;
253
254
/* Skip to next match if the match length cannot increase
255
* or if the match length is less than 2. Note that the checks below
256
* for insufficient lookahead only occur occasionally for performance
257
* reasons. Therefore uninitialized memory will be accessed, and
258
* conditional jumps will be made that depend on those values.
259
* However the length of the match is limited to the lookahead, so
260
* the output of deflate is not affected by the uninitialized values.
261
*/
262
263
if (_win[match + best_len] !== scan_end ||
264
_win[match + best_len - 1] !== scan_end1 ||
265
_win[match] !== _win[scan] ||
266
_win[++match] !== _win[scan + 1]) {
267
continue;
268
}
269
270
/* The check at best_len-1 can be removed because it will be made
271
* again later. (This heuristic is not always a win.)
272
* It is not necessary to compare scan[2] and match[2] since they
273
* are always equal when the other bytes match, given that
274
* the hash keys are equal and that HASH_BITS >= 8.
275
*/
276
scan += 2;
277
match++;
278
// Assert(*scan == *match, "match[2]?");
279
280
/* We check for insufficient lookahead only every 8th comparison;
281
* the 256th check will be made at strstart+258.
282
*/
283
do {
284
/*jshint noempty:false*/
285
} while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
286
_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
287
_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
288
_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
289
scan < strend);
290
291
// Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
292
293
len = MAX_MATCH - (strend - scan);
294
scan = strend - MAX_MATCH;
295
296
if (len > best_len) {
297
s.match_start = cur_match;
298
best_len = len;
299
if (len >= nice_match) {
300
break;
301
}
302
scan_end1 = _win[scan + best_len - 1];
303
scan_end = _win[scan + best_len];
304
}
305
} while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
306
307
if (best_len <= s.lookahead) {
308
return best_len;
309
}
310
return s.lookahead;
311
}
312
313
314
/* ===========================================================================
315
* Fill the window when the lookahead becomes insufficient.
316
* Updates strstart and lookahead.
317
*
318
* IN assertion: lookahead < MIN_LOOKAHEAD
319
* OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
320
* At least one byte has been read, or avail_in == 0; reads are
321
* performed for at least two bytes (required for the zip translate_eol
322
* option -- not supported here).
323
*/
324
function fill_window(s) {
325
var _w_size = s.w_size;
326
var p, n, m, more, str;
327
328
//Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
329
330
do {
331
more = s.window_size - s.lookahead - s.strstart;
332
333
// JS ints have 32 bit, block below not needed
334
/* Deal with !@#$% 64K limit: */
335
//if (sizeof(int) <= 2) {
336
// if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
337
// more = wsize;
338
//
339
// } else if (more == (unsigned)(-1)) {
340
// /* Very unlikely, but possible on 16 bit machine if
341
// * strstart == 0 && lookahead == 1 (input done a byte at time)
342
// */
343
// more--;
344
// }
345
//}
346
347
348
/* If the window is almost full and there is insufficient lookahead,
349
* move the upper half to the lower one to make room in the upper half.
350
*/
351
if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
352
353
utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
354
s.match_start -= _w_size;
355
s.strstart -= _w_size;
356
/* we now have strstart >= MAX_DIST */
357
s.block_start -= _w_size;
358
359
/* Slide the hash table (could be avoided with 32 bit values
360
at the expense of memory usage). We slide even when level == 0
361
to keep the hash table consistent if we switch back to level > 0
362
later. (Using level 0 permanently is not an optimal usage of
363
zlib, so we don't care about this pathological case.)
364
*/
365
366
n = s.hash_size;
367
p = n;
368
do {
369
m = s.head[--p];
370
s.head[p] = (m >= _w_size ? m - _w_size : 0);
371
} while (--n);
372
373
n = _w_size;
374
p = n;
375
do {
376
m = s.prev[--p];
377
s.prev[p] = (m >= _w_size ? m - _w_size : 0);
378
/* If n is not on any hash chain, prev[n] is garbage but
379
* its value will never be used.
380
*/
381
} while (--n);
382
383
more += _w_size;
384
}
385
if (s.strm.avail_in === 0) {
386
break;
387
}
388
389
/* If there was no sliding:
390
* strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
391
* more == window_size - lookahead - strstart
392
* => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
393
* => more >= window_size - 2*WSIZE + 2
394
* In the BIG_MEM or MMAP case (not yet supported),
395
* window_size == input_size + MIN_LOOKAHEAD &&
396
* strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
397
* Otherwise, window_size == 2*WSIZE so more >= 2.
398
* If there was sliding, more >= WSIZE. So in all cases, more >= 2.
399
*/
400
//Assert(more >= 2, "more < 2");
401
n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
402
s.lookahead += n;
403
404
/* Initialize the hash value now that we have some input: */
405
if (s.lookahead + s.insert >= MIN_MATCH) {
406
str = s.strstart - s.insert;
407
s.ins_h = s.window[str];
408
409
/* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
410
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
411
//#if MIN_MATCH != 3
412
// Call update_hash() MIN_MATCH-3 more times
413
//#endif
414
while (s.insert) {
415
/* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
416
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH-1]) & s.hash_mask;
417
418
s.prev[str & s.w_mask] = s.head[s.ins_h];
419
s.head[s.ins_h] = str;
420
str++;
421
s.insert--;
422
if (s.lookahead + s.insert < MIN_MATCH) {
423
break;
424
}
425
}
426
}
427
/* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
428
* but this is not important since only literal bytes will be emitted.
429
*/
430
431
} while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
432
433
/* If the WIN_INIT bytes after the end of the current data have never been
434
* written, then zero those bytes in order to avoid memory check reports of
435
* the use of uninitialized (or uninitialised as Julian writes) bytes by
436
* the longest match routines. Update the high water mark for the next
437
* time through here. WIN_INIT is set to MAX_MATCH since the longest match
438
* routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
439
*/
440
// if (s.high_water < s.window_size) {
441
// var curr = s.strstart + s.lookahead;
442
// var init = 0;
443
//
444
// if (s.high_water < curr) {
445
// /* Previous high water mark below current data -- zero WIN_INIT
446
// * bytes or up to end of window, whichever is less.
447
// */
448
// init = s.window_size - curr;
449
// if (init > WIN_INIT)
450
// init = WIN_INIT;
451
// zmemzero(s->window + curr, (unsigned)init);
452
// s->high_water = curr + init;
453
// }
454
// else if (s->high_water < (ulg)curr + WIN_INIT) {
455
// /* High water mark at or above current data, but below current data
456
// * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
457
// * to end of window, whichever is less.
458
// */
459
// init = (ulg)curr + WIN_INIT - s->high_water;
460
// if (init > s->window_size - s->high_water)
461
// init = s->window_size - s->high_water;
462
// zmemzero(s->window + s->high_water, (unsigned)init);
463
// s->high_water += init;
464
// }
465
// }
466
//
467
// Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
468
// "not enough room for search");
469
}
470
471
/* ===========================================================================
472
* Copy without compression as much as possible from the input stream, return
473
* the current block state.
474
* This function does not insert new strings in the dictionary since
475
* uncompressible data is probably not useful. This function is used
476
* only for the level=0 compression option.
477
* NOTE: this function should be optimized to avoid extra copying from
478
* window to pending_buf.
479
*/
480
function deflate_stored(s, flush) {
481
/* Stored blocks are limited to 0xffff bytes, pending_buf is limited
482
* to pending_buf_size, and each stored block has a 5 byte header:
483
*/
484
var max_block_size = 0xffff;
485
486
if (max_block_size > s.pending_buf_size - 5) {
487
max_block_size = s.pending_buf_size - 5;
488
}
489
490
/* Copy as much as possible from input to output: */
491
for (;;) {
492
/* Fill the window as much as possible: */
493
if (s.lookahead <= 1) {
494
495
//Assert(s->strstart < s->w_size+MAX_DIST(s) ||
496
// s->block_start >= (long)s->w_size, "slide too late");
497
// if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
498
// s.block_start >= s.w_size)) {
499
// throw new Error("slide too late");
500
// }
501
502
fill_window(s);
503
if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
504
return BS_NEED_MORE;
505
}
506
507
if (s.lookahead === 0) {
508
break;
509
}
510
/* flush the current block */
511
}
512
//Assert(s->block_start >= 0L, "block gone");
513
// if (s.block_start < 0) throw new Error("block gone");
514
515
s.strstart += s.lookahead;
516
s.lookahead = 0;
517
518
/* Emit a stored block if pending_buf will be full: */
519
var max_start = s.block_start + max_block_size;
520
521
if (s.strstart === 0 || s.strstart >= max_start) {
522
/* strstart == 0 is possible when wraparound on 16-bit machine */
523
s.lookahead = s.strstart - max_start;
524
s.strstart = max_start;
525
/*** FLUSH_BLOCK(s, 0); ***/
526
flush_block_only(s, false);
527
if (s.strm.avail_out === 0) {
528
return BS_NEED_MORE;
529
}
530
/***/
531
532
533
}
534
/* Flush if we may have to slide, otherwise block_start may become
535
* negative and the data will be gone:
536
*/
537
if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
538
/*** FLUSH_BLOCK(s, 0); ***/
539
flush_block_only(s, false);
540
if (s.strm.avail_out === 0) {
541
return BS_NEED_MORE;
542
}
543
/***/
544
}
545
}
546
547
s.insert = 0;
548
549
if (flush === Z_FINISH) {
550
/*** FLUSH_BLOCK(s, 1); ***/
551
flush_block_only(s, true);
552
if (s.strm.avail_out === 0) {
553
return BS_FINISH_STARTED;
554
}
555
/***/
556
return BS_FINISH_DONE;
557
}
558
559
if (s.strstart > s.block_start) {
560
/*** FLUSH_BLOCK(s, 0); ***/
561
flush_block_only(s, false);
562
if (s.strm.avail_out === 0) {
563
return BS_NEED_MORE;
564
}
565
/***/
566
}
567
568
return BS_NEED_MORE;
569
}
570
571
/* ===========================================================================
572
* Compress as much as possible from the input stream, return the current
573
* block state.
574
* This function does not perform lazy evaluation of matches and inserts
575
* new strings in the dictionary only for unmatched strings or for short
576
* matches. It is used only for the fast compression options.
577
*/
578
function deflate_fast(s, flush) {
579
var hash_head; /* head of the hash chain */
580
var bflush; /* set if current block must be flushed */
581
582
for (;;) {
583
/* Make sure that we always have enough lookahead, except
584
* at the end of the input file. We need MAX_MATCH bytes
585
* for the next match, plus MIN_MATCH bytes to insert the
586
* string following the next match.
587
*/
588
if (s.lookahead < MIN_LOOKAHEAD) {
589
fill_window(s);
590
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
591
return BS_NEED_MORE;
592
}
593
if (s.lookahead === 0) {
594
break; /* flush the current block */
595
}
596
}
597
598
/* Insert the string window[strstart .. strstart+2] in the
599
* dictionary, and set hash_head to the head of the hash chain:
600
*/
601
hash_head = 0/*NIL*/;
602
if (s.lookahead >= MIN_MATCH) {
603
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
604
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
605
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
606
s.head[s.ins_h] = s.strstart;
607
/***/
608
}
609
610
/* Find the longest match, discarding those <= prev_length.
611
* At this point we have always match_length < MIN_MATCH
612
*/
613
if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
614
/* To simplify the code, we prevent matches with the string
615
* of window index 0 (in particular we have to avoid a match
616
* of the string with itself at the start of the input file).
617
*/
618
s.match_length = longest_match(s, hash_head);
619
/* longest_match() sets match_start */
620
}
621
if (s.match_length >= MIN_MATCH) {
622
// check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
623
624
/*** _tr_tally_dist(s, s.strstart - s.match_start,
625
s.match_length - MIN_MATCH, bflush); ***/
626
bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
627
628
s.lookahead -= s.match_length;
629
630
/* Insert new strings in the hash table only if the match length
631
* is not too large. This saves time but degrades compression.
632
*/
633
if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
634
s.match_length--; /* string at strstart already in table */
635
do {
636
s.strstart++;
637
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
638
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
639
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
640
s.head[s.ins_h] = s.strstart;
641
/***/
642
/* strstart never exceeds WSIZE-MAX_MATCH, so there are
643
* always MIN_MATCH bytes ahead.
644
*/
645
} while (--s.match_length !== 0);
646
s.strstart++;
647
} else
648
{
649
s.strstart += s.match_length;
650
s.match_length = 0;
651
s.ins_h = s.window[s.strstart];
652
/* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
653
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
654
655
//#if MIN_MATCH != 3
656
// Call UPDATE_HASH() MIN_MATCH-3 more times
657
//#endif
658
/* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
659
* matter since it will be recomputed at next deflate call.
660
*/
661
}
662
} else {
663
/* No match, output a literal byte */
664
//Tracevv((stderr,"%c", s.window[s.strstart]));
665
/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
666
bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
667
668
s.lookahead--;
669
s.strstart++;
670
}
671
if (bflush) {
672
/*** FLUSH_BLOCK(s, 0); ***/
673
flush_block_only(s, false);
674
if (s.strm.avail_out === 0) {
675
return BS_NEED_MORE;
676
}
677
/***/
678
}
679
}
680
s.insert = ((s.strstart < (MIN_MATCH-1)) ? s.strstart : MIN_MATCH-1);
681
if (flush === Z_FINISH) {
682
/*** FLUSH_BLOCK(s, 1); ***/
683
flush_block_only(s, true);
684
if (s.strm.avail_out === 0) {
685
return BS_FINISH_STARTED;
686
}
687
/***/
688
return BS_FINISH_DONE;
689
}
690
if (s.last_lit) {
691
/*** FLUSH_BLOCK(s, 0); ***/
692
flush_block_only(s, false);
693
if (s.strm.avail_out === 0) {
694
return BS_NEED_MORE;
695
}
696
/***/
697
}
698
return BS_BLOCK_DONE;
699
}
700
701
/* ===========================================================================
702
* Same as above, but achieves better compression. We use a lazy
703
* evaluation for matches: a match is finally adopted only if there is
704
* no better match at the next window position.
705
*/
706
function deflate_slow(s, flush) {
707
var hash_head; /* head of hash chain */
708
var bflush; /* set if current block must be flushed */
709
710
var max_insert;
711
712
/* Process the input block. */
713
for (;;) {
714
/* Make sure that we always have enough lookahead, except
715
* at the end of the input file. We need MAX_MATCH bytes
716
* for the next match, plus MIN_MATCH bytes to insert the
717
* string following the next match.
718
*/
719
if (s.lookahead < MIN_LOOKAHEAD) {
720
fill_window(s);
721
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
722
return BS_NEED_MORE;
723
}
724
if (s.lookahead === 0) { break; } /* flush the current block */
725
}
726
727
/* Insert the string window[strstart .. strstart+2] in the
728
* dictionary, and set hash_head to the head of the hash chain:
729
*/
730
hash_head = 0/*NIL*/;
731
if (s.lookahead >= MIN_MATCH) {
732
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
733
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
734
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
735
s.head[s.ins_h] = s.strstart;
736
/***/
737
}
738
739
/* Find the longest match, discarding those <= prev_length.
740
*/
741
s.prev_length = s.match_length;
742
s.prev_match = s.match_start;
743
s.match_length = MIN_MATCH-1;
744
745
if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
746
s.strstart - hash_head <= (s.w_size-MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
747
/* To simplify the code, we prevent matches with the string
748
* of window index 0 (in particular we have to avoid a match
749
* of the string with itself at the start of the input file).
750
*/
751
s.match_length = longest_match(s, hash_head);
752
/* longest_match() sets match_start */
753
754
if (s.match_length <= 5 &&
755
(s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
756
757
/* If prev_match is also MIN_MATCH, match_start is garbage
758
* but we will ignore the current match anyway.
759
*/
760
s.match_length = MIN_MATCH-1;
761
}
762
}
763
/* If there was a match at the previous step and the current
764
* match is not better, output the previous match:
765
*/
766
if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
767
max_insert = s.strstart + s.lookahead - MIN_MATCH;
768
/* Do not insert strings in hash table beyond this. */
769
770
//check_match(s, s.strstart-1, s.prev_match, s.prev_length);
771
772
/***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
773
s.prev_length - MIN_MATCH, bflush);***/
774
bflush = trees._tr_tally(s, s.strstart - 1- s.prev_match, s.prev_length - MIN_MATCH);
775
/* Insert in hash table all strings up to the end of the match.
776
* strstart-1 and strstart are already inserted. If there is not
777
* enough lookahead, the last two strings are not inserted in
778
* the hash table.
779
*/
780
s.lookahead -= s.prev_length-1;
781
s.prev_length -= 2;
782
do {
783
if (++s.strstart <= max_insert) {
784
/*** INSERT_STRING(s, s.strstart, hash_head); ***/
785
s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
786
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
787
s.head[s.ins_h] = s.strstart;
788
/***/
789
}
790
} while (--s.prev_length !== 0);
791
s.match_available = 0;
792
s.match_length = MIN_MATCH-1;
793
s.strstart++;
794
795
if (bflush) {
796
/*** FLUSH_BLOCK(s, 0); ***/
797
flush_block_only(s, false);
798
if (s.strm.avail_out === 0) {
799
return BS_NEED_MORE;
800
}
801
/***/
802
}
803
804
} else if (s.match_available) {
805
/* If there was no match at the previous position, output a
806
* single literal. If there was a match but the current match
807
* is longer, truncate the previous match to a single literal.
808
*/
809
//Tracevv((stderr,"%c", s->window[s->strstart-1]));
810
/*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
811
bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);
812
813
if (bflush) {
814
/*** FLUSH_BLOCK_ONLY(s, 0) ***/
815
flush_block_only(s, false);
816
/***/
817
}
818
s.strstart++;
819
s.lookahead--;
820
if (s.strm.avail_out === 0) {
821
return BS_NEED_MORE;
822
}
823
} else {
824
/* There is no previous match to compare with, wait for
825
* the next step to decide.
826
*/
827
s.match_available = 1;
828
s.strstart++;
829
s.lookahead--;
830
}
831
}
832
//Assert (flush != Z_NO_FLUSH, "no flush?");
833
if (s.match_available) {
834
//Tracevv((stderr,"%c", s->window[s->strstart-1]));
835
/*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
836
bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);
837
838
s.match_available = 0;
839
}
840
s.insert = s.strstart < MIN_MATCH-1 ? s.strstart : MIN_MATCH-1;
841
if (flush === Z_FINISH) {
842
/*** FLUSH_BLOCK(s, 1); ***/
843
flush_block_only(s, true);
844
if (s.strm.avail_out === 0) {
845
return BS_FINISH_STARTED;
846
}
847
/***/
848
return BS_FINISH_DONE;
849
}
850
if (s.last_lit) {
851
/*** FLUSH_BLOCK(s, 0); ***/
852
flush_block_only(s, false);
853
if (s.strm.avail_out === 0) {
854
return BS_NEED_MORE;
855
}
856
/***/
857
}
858
859
return BS_BLOCK_DONE;
860
}
861
862
863
/* ===========================================================================
864
* For Z_RLE, simply look for runs of bytes, generate matches only of distance
865
* one. Do not maintain a hash table. (It will be regenerated if this run of
866
* deflate switches away from Z_RLE.)
867
*/
868
function deflate_rle(s, flush) {
869
var bflush; /* set if current block must be flushed */
870
var prev; /* byte at distance one to match */
871
var scan, strend; /* scan goes up to strend for length of run */
872
873
var _win = s.window;
874
875
for (;;) {
876
/* Make sure that we always have enough lookahead, except
877
* at the end of the input file. We need MAX_MATCH bytes
878
* for the longest run, plus one for the unrolled loop.
879
*/
880
if (s.lookahead <= MAX_MATCH) {
881
fill_window(s);
882
if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
883
return BS_NEED_MORE;
884
}
885
if (s.lookahead === 0) { break; } /* flush the current block */
886
}
887
888
/* See how many times the previous byte repeats */
889
s.match_length = 0;
890
if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
891
scan = s.strstart - 1;
892
prev = _win[scan];
893
if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
894
strend = s.strstart + MAX_MATCH;
895
do {
896
/*jshint noempty:false*/
897
} while (prev === _win[++scan] && prev === _win[++scan] &&
898
prev === _win[++scan] && prev === _win[++scan] &&
899
prev === _win[++scan] && prev === _win[++scan] &&
900
prev === _win[++scan] && prev === _win[++scan] &&
901
scan < strend);
902
s.match_length = MAX_MATCH - (strend - scan);
903
if (s.match_length > s.lookahead) {
904
s.match_length = s.lookahead;
905
}
906
}
907
//Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
908
}
909
910
/* Emit match if have run of MIN_MATCH or longer, else emit literal */
911
if (s.match_length >= MIN_MATCH) {
912
//check_match(s, s.strstart, s.strstart - 1, s.match_length);
913
914
/*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
915
bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
916
917
s.lookahead -= s.match_length;
918
s.strstart += s.match_length;
919
s.match_length = 0;
920
} else {
921
/* No match, output a literal byte */
922
//Tracevv((stderr,"%c", s->window[s->strstart]));
923
/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
924
bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
925
926
s.lookahead--;
927
s.strstart++;
928
}
929
if (bflush) {
930
/*** FLUSH_BLOCK(s, 0); ***/
931
flush_block_only(s, false);
932
if (s.strm.avail_out === 0) {
933
return BS_NEED_MORE;
934
}
935
/***/
936
}
937
}
938
s.insert = 0;
939
if (flush === Z_FINISH) {
940
/*** FLUSH_BLOCK(s, 1); ***/
941
flush_block_only(s, true);
942
if (s.strm.avail_out === 0) {
943
return BS_FINISH_STARTED;
944
}
945
/***/
946
return BS_FINISH_DONE;
947
}
948
if (s.last_lit) {
949
/*** FLUSH_BLOCK(s, 0); ***/
950
flush_block_only(s, false);
951
if (s.strm.avail_out === 0) {
952
return BS_NEED_MORE;
953
}
954
/***/
955
}
956
return BS_BLOCK_DONE;
957
}
958
959
/* ===========================================================================
960
* For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
961
* (It will be regenerated if this run of deflate switches away from Huffman.)
962
*/
963
function deflate_huff(s, flush) {
964
var bflush; /* set if current block must be flushed */
965
966
for (;;) {
967
/* Make sure that we have a literal to write. */
968
if (s.lookahead === 0) {
969
fill_window(s);
970
if (s.lookahead === 0) {
971
if (flush === Z_NO_FLUSH) {
972
return BS_NEED_MORE;
973
}
974
break; /* flush the current block */
975
}
976
}
977
978
/* Output a literal byte */
979
s.match_length = 0;
980
//Tracevv((stderr,"%c", s->window[s->strstart]));
981
/*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
982
bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
983
s.lookahead--;
984
s.strstart++;
985
if (bflush) {
986
/*** FLUSH_BLOCK(s, 0); ***/
987
flush_block_only(s, false);
988
if (s.strm.avail_out === 0) {
989
return BS_NEED_MORE;
990
}
991
/***/
992
}
993
}
994
s.insert = 0;
995
if (flush === Z_FINISH) {
996
/*** FLUSH_BLOCK(s, 1); ***/
997
flush_block_only(s, true);
998
if (s.strm.avail_out === 0) {
999
return BS_FINISH_STARTED;
1000
}
1001
/***/
1002
return BS_FINISH_DONE;
1003
}
1004
if (s.last_lit) {
1005
/*** FLUSH_BLOCK(s, 0); ***/
1006
flush_block_only(s, false);
1007
if (s.strm.avail_out === 0) {
1008
return BS_NEED_MORE;
1009
}
1010
/***/
1011
}
1012
return BS_BLOCK_DONE;
1013
}
1014
1015
/* Values for max_lazy_match, good_match and max_chain_length, depending on
1016
* the desired pack level (0..9). The values given below have been tuned to
1017
* exclude worst case performance for pathological files. Better values may be
1018
* found for specific files.
1019
*/
1020
var Config = function (good_length, max_lazy, nice_length, max_chain, func) {
1021
this.good_length = good_length;
1022
this.max_lazy = max_lazy;
1023
this.nice_length = nice_length;
1024
this.max_chain = max_chain;
1025
this.func = func;
1026
};
1027
1028
var configuration_table;
1029
1030
configuration_table = [
1031
/* good lazy nice chain */
1032
new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
1033
new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
1034
new Config(4, 5, 16, 8, deflate_fast), /* 2 */
1035
new Config(4, 6, 32, 32, deflate_fast), /* 3 */
1036
1037
new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
1038
new Config(8, 16, 32, 32, deflate_slow), /* 5 */
1039
new Config(8, 16, 128, 128, deflate_slow), /* 6 */
1040
new Config(8, 32, 128, 256, deflate_slow), /* 7 */
1041
new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
1042
new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
1043
];
1044
1045
1046
/* ===========================================================================
1047
* Initialize the "longest match" routines for a new zlib stream
1048
*/
1049
function lm_init(s) {
1050
s.window_size = 2 * s.w_size;
1051
1052
/*** CLEAR_HASH(s); ***/
1053
zero(s.head); // Fill with NIL (= 0);
1054
1055
/* Set the default configuration parameters:
1056
*/
1057
s.max_lazy_match = configuration_table[s.level].max_lazy;
1058
s.good_match = configuration_table[s.level].good_length;
1059
s.nice_match = configuration_table[s.level].nice_length;
1060
s.max_chain_length = configuration_table[s.level].max_chain;
1061
1062
s.strstart = 0;
1063
s.block_start = 0;
1064
s.lookahead = 0;
1065
s.insert = 0;
1066
s.match_length = s.prev_length = MIN_MATCH - 1;
1067
s.match_available = 0;
1068
s.ins_h = 0;
1069
}
1070
1071
1072
function DeflateState() {
1073
this.strm = null; /* pointer back to this zlib stream */
1074
this.status = 0; /* as the name implies */
1075
this.pending_buf = null; /* output still pending */
1076
this.pending_buf_size = 0; /* size of pending_buf */
1077
this.pending_out = 0; /* next pending byte to output to the stream */
1078
this.pending = 0; /* nb of bytes in the pending buffer */
1079
this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
1080
this.gzhead = null; /* gzip header information to write */
1081
this.gzindex = 0; /* where in extra, name, or comment */
1082
this.method = Z_DEFLATED; /* can only be DEFLATED */
1083
this.last_flush = -1; /* value of flush param for previous deflate call */
1084
1085
this.w_size = 0; /* LZ77 window size (32K by default) */
1086
this.w_bits = 0; /* log2(w_size) (8..16) */
1087
this.w_mask = 0; /* w_size - 1 */
1088
1089
this.window = null;
1090
/* Sliding window. Input bytes are read into the second half of the window,
1091
* and move to the first half later to keep a dictionary of at least wSize
1092
* bytes. With this organization, matches are limited to a distance of
1093
* wSize-MAX_MATCH bytes, but this ensures that IO is always
1094
* performed with a length multiple of the block size.
1095
*/
1096
1097
this.window_size = 0;
1098
/* Actual size of window: 2*wSize, except when the user input buffer
1099
* is directly used as sliding window.
1100
*/
1101
1102
this.prev = null;
1103
/* Link to older string with same hash index. To limit the size of this
1104
* array to 64K, this link is maintained only for the last 32K strings.
1105
* An index in this array is thus a window index modulo 32K.
1106
*/
1107
1108
this.head = null; /* Heads of the hash chains or NIL. */
1109
1110
this.ins_h = 0; /* hash index of string to be inserted */
1111
this.hash_size = 0; /* number of elements in hash table */
1112
this.hash_bits = 0; /* log2(hash_size) */
1113
this.hash_mask = 0; /* hash_size-1 */
1114
1115
this.hash_shift = 0;
1116
/* Number of bits by which ins_h must be shifted at each input
1117
* step. It must be such that after MIN_MATCH steps, the oldest
1118
* byte no longer takes part in the hash key, that is:
1119
* hash_shift * MIN_MATCH >= hash_bits
1120
*/
1121
1122
this.block_start = 0;
1123
/* Window position at the beginning of the current output block. Gets
1124
* negative when the window is moved backwards.
1125
*/
1126
1127
this.match_length = 0; /* length of best match */
1128
this.prev_match = 0; /* previous match */
1129
this.match_available = 0; /* set if previous match exists */
1130
this.strstart = 0; /* start of string to insert */
1131
this.match_start = 0; /* start of matching string */
1132
this.lookahead = 0; /* number of valid bytes ahead in window */
1133
1134
this.prev_length = 0;
1135
/* Length of the best match at previous step. Matches not greater than this
1136
* are discarded. This is used in the lazy match evaluation.
1137
*/
1138
1139
this.max_chain_length = 0;
1140
/* To speed up deflation, hash chains are never searched beyond this
1141
* length. A higher limit improves compression ratio but degrades the
1142
* speed.
1143
*/
1144
1145
this.max_lazy_match = 0;
1146
/* Attempt to find a better match only when the current match is strictly
1147
* smaller than this value. This mechanism is used only for compression
1148
* levels >= 4.
1149
*/
1150
// That's alias to max_lazy_match, don't use directly
1151
//this.max_insert_length = 0;
1152
/* Insert new strings in the hash table only if the match length is not
1153
* greater than this length. This saves time but degrades compression.
1154
* max_insert_length is used only for compression levels <= 3.
1155
*/
1156
1157
this.level = 0; /* compression level (1..9) */
1158
this.strategy = 0; /* favor or force Huffman coding*/
1159
1160
this.good_match = 0;
1161
/* Use a faster search when the previous match is longer than this */
1162
1163
this.nice_match = 0; /* Stop searching when current match exceeds this */
1164
1165
/* used by trees.c: */
1166
1167
/* Didn't use ct_data typedef below to suppress compiler warning */
1168
1169
// struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
1170
// struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
1171
// struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
1172
1173
// Use flat array of DOUBLE size, with interleaved fata,
1174
// because JS does not support effective
1175
this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
1176
this.dyn_dtree = new utils.Buf16((2*D_CODES+1) * 2);
1177
this.bl_tree = new utils.Buf16((2*BL_CODES+1) * 2);
1178
zero(this.dyn_ltree);
1179
zero(this.dyn_dtree);
1180
zero(this.bl_tree);
1181
1182
this.l_desc = null; /* desc. for literal tree */
1183
this.d_desc = null; /* desc. for distance tree */
1184
this.bl_desc = null; /* desc. for bit length tree */
1185
1186
//ush bl_count[MAX_BITS+1];
1187
this.bl_count = new utils.Buf16(MAX_BITS+1);
1188
/* number of codes at each bit length for an optimal tree */
1189
1190
//int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
1191
this.heap = new utils.Buf16(2*L_CODES+1); /* heap used to build the Huffman trees */
1192
zero(this.heap);
1193
1194
this.heap_len = 0; /* number of elements in the heap */
1195
this.heap_max = 0; /* element of largest frequency */
1196
/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1197
* The same heap array is used to build all trees.
1198
*/
1199
1200
this.depth = new utils.Buf16(2*L_CODES+1); //uch depth[2*L_CODES+1];
1201
zero(this.depth);
1202
/* Depth of each subtree used as tie breaker for trees of equal frequency
1203
*/
1204
1205
this.l_buf = 0; /* buffer index for literals or lengths */
1206
1207
this.lit_bufsize = 0;
1208
/* Size of match buffer for literals/lengths. There are 4 reasons for
1209
* limiting lit_bufsize to 64K:
1210
* - frequencies can be kept in 16 bit counters
1211
* - if compression is not successful for the first block, all input
1212
* data is still in the window so we can still emit a stored block even
1213
* when input comes from standard input. (This can also be done for
1214
* all blocks if lit_bufsize is not greater than 32K.)
1215
* - if compression is not successful for a file smaller than 64K, we can
1216
* even emit a stored file instead of a stored block (saving 5 bytes).
1217
* This is applicable only for zip (not gzip or zlib).
1218
* - creating new Huffman trees less frequently may not provide fast
1219
* adaptation to changes in the input data statistics. (Take for
1220
* example a binary file with poorly compressible code followed by
1221
* a highly compressible string table.) Smaller buffer sizes give
1222
* fast adaptation but have of course the overhead of transmitting
1223
* trees more frequently.
1224
* - I can't count above 4
1225
*/
1226
1227
this.last_lit = 0; /* running index in l_buf */
1228
1229
this.d_buf = 0;
1230
/* Buffer index for distances. To simplify the code, d_buf and l_buf have
1231
* the same number of elements. To use different lengths, an extra flag
1232
* array would be necessary.
1233
*/
1234
1235
this.opt_len = 0; /* bit length of current block with optimal trees */
1236
this.static_len = 0; /* bit length of current block with static trees */
1237
this.matches = 0; /* number of string matches in current block */
1238
this.insert = 0; /* bytes at end of window left to insert */
1239
1240
1241
this.bi_buf = 0;
1242
/* Output buffer. bits are inserted starting at the bottom (least
1243
* significant bits).
1244
*/
1245
this.bi_valid = 0;
1246
/* Number of valid bits in bi_buf. All bits above the last valid bit
1247
* are always zero.
1248
*/
1249
1250
// Used for window memory init. We safely ignore it for JS. That makes
1251
// sense only for pointers and memory check tools.
1252
//this.high_water = 0;
1253
/* High water mark offset in window for initialized bytes -- bytes above
1254
* this are set to zero in order to avoid memory check warnings when
1255
* longest match routines access bytes past the input. This is then
1256
* updated to the new high water mark.
1257
*/
1258
}
1259
1260
1261
function deflateResetKeep(strm) {
1262
var s;
1263
1264
if (!strm || !strm.state) {
1265
return err(strm, Z_STREAM_ERROR);
1266
}
1267
1268
strm.total_in = strm.total_out = 0;
1269
strm.data_type = Z_UNKNOWN;
1270
1271
s = strm.state;
1272
s.pending = 0;
1273
s.pending_out = 0;
1274
1275
if (s.wrap < 0) {
1276
s.wrap = -s.wrap;
1277
/* was made negative by deflate(..., Z_FINISH); */
1278
}
1279
s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
1280
strm.adler = (s.wrap === 2) ?
1281
0 // crc32(0, Z_NULL, 0)
1282
:
1283
1; // adler32(0, Z_NULL, 0)
1284
s.last_flush = Z_NO_FLUSH;
1285
trees._tr_init(s);
1286
return Z_OK;
1287
}
1288
1289
1290
function deflateReset(strm) {
1291
var ret = deflateResetKeep(strm);
1292
if (ret === Z_OK) {
1293
lm_init(strm.state);
1294
}
1295
return ret;
1296
}
1297
1298
1299
function deflateSetHeader(strm, head) {
1300
if (!strm || !strm.state) { return Z_STREAM_ERROR; }
1301
if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
1302
strm.state.gzhead = head;
1303
return Z_OK;
1304
}
1305
1306
1307
function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
1308
if (!strm) { // === Z_NULL
1309
return Z_STREAM_ERROR;
1310
}
1311
var wrap = 1;
1312
1313
if (level === Z_DEFAULT_COMPRESSION) {
1314
level = 6;
1315
}
1316
1317
if (windowBits < 0) { /* suppress zlib wrapper */
1318
wrap = 0;
1319
windowBits = -windowBits;
1320
}
1321
1322
else if (windowBits > 15) {
1323
wrap = 2; /* write gzip wrapper instead */
1324
windowBits -= 16;
1325
}
1326
1327
1328
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
1329
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
1330
strategy < 0 || strategy > Z_FIXED) {
1331
return err(strm, Z_STREAM_ERROR);
1332
}
1333
1334
1335
if (windowBits === 8) {
1336
windowBits = 9;
1337
}
1338
/* until 256-byte window bug fixed */
1339
1340
var s = new DeflateState();
1341
1342
strm.state = s;
1343
s.strm = strm;
1344
1345
s.wrap = wrap;
1346
s.gzhead = null;
1347
s.w_bits = windowBits;
1348
s.w_size = 1 << s.w_bits;
1349
s.w_mask = s.w_size - 1;
1350
1351
s.hash_bits = memLevel + 7;
1352
s.hash_size = 1 << s.hash_bits;
1353
s.hash_mask = s.hash_size - 1;
1354
s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
1355
1356
s.window = new utils.Buf8(s.w_size * 2);
1357
s.head = new utils.Buf16(s.hash_size);
1358
s.prev = new utils.Buf16(s.w_size);
1359
1360
// Don't need mem init magic for JS.
1361
//s.high_water = 0; /* nothing written to s->window yet */
1362
1363
s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
1364
1365
s.pending_buf_size = s.lit_bufsize * 4;
1366
s.pending_buf = new utils.Buf8(s.pending_buf_size);
1367
1368
s.d_buf = s.lit_bufsize >> 1;
1369
s.l_buf = (1 + 2) * s.lit_bufsize;
1370
1371
s.level = level;
1372
s.strategy = strategy;
1373
s.method = method;
1374
1375
return deflateReset(strm);
1376
}
1377
1378
function deflateInit(strm, level) {
1379
return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
1380
}
1381
1382
1383
function deflate(strm, flush) {
1384
var old_flush, s;
1385
var beg, val; // for gzip header write only
1386
1387
if (!strm || !strm.state ||
1388
flush > Z_BLOCK || flush < 0) {
1389
return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
1390
}
1391
1392
s = strm.state;
1393
1394
if (!strm.output ||
1395
(!strm.input && strm.avail_in !== 0) ||
1396
(s.status === FINISH_STATE && flush !== Z_FINISH)) {
1397
return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
1398
}
1399
1400
s.strm = strm; /* just in case */
1401
old_flush = s.last_flush;
1402
s.last_flush = flush;
1403
1404
/* Write the header */
1405
if (s.status === INIT_STATE) {
1406
1407
if (s.wrap === 2) { // GZIP header
1408
strm.adler = 0; //crc32(0L, Z_NULL, 0);
1409
put_byte(s, 31);
1410
put_byte(s, 139);
1411
put_byte(s, 8);
1412
if (!s.gzhead) { // s->gzhead == Z_NULL
1413
put_byte(s, 0);
1414
put_byte(s, 0);
1415
put_byte(s, 0);
1416
put_byte(s, 0);
1417
put_byte(s, 0);
1418
put_byte(s, s.level === 9 ? 2 :
1419
(s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
1420
4 : 0));
1421
put_byte(s, OS_CODE);
1422
s.status = BUSY_STATE;
1423
}
1424
else {
1425
put_byte(s, (s.gzhead.text ? 1 : 0) +
1426
(s.gzhead.hcrc ? 2 : 0) +
1427
(!s.gzhead.extra ? 0 : 4) +
1428
(!s.gzhead.name ? 0 : 8) +
1429
(!s.gzhead.comment ? 0 : 16)
1430
);
1431
put_byte(s, s.gzhead.time & 0xff);
1432
put_byte(s, (s.gzhead.time >> 8) & 0xff);
1433
put_byte(s, (s.gzhead.time >> 16) & 0xff);
1434
put_byte(s, (s.gzhead.time >> 24) & 0xff);
1435
put_byte(s, s.level === 9 ? 2 :
1436
(s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
1437
4 : 0));
1438
put_byte(s, s.gzhead.os & 0xff);
1439
if (s.gzhead.extra && s.gzhead.extra.length) {
1440
put_byte(s, s.gzhead.extra.length & 0xff);
1441
put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
1442
}
1443
if (s.gzhead.hcrc) {
1444
strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
1445
}
1446
s.gzindex = 0;
1447
s.status = EXTRA_STATE;
1448
}
1449
}
1450
else // DEFLATE header
1451
{
1452
var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
1453
var level_flags = -1;
1454
1455
if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
1456
level_flags = 0;
1457
} else if (s.level < 6) {
1458
level_flags = 1;
1459
} else if (s.level === 6) {
1460
level_flags = 2;
1461
} else {
1462
level_flags = 3;
1463
}
1464
header |= (level_flags << 6);
1465
if (s.strstart !== 0) { header |= PRESET_DICT; }
1466
header += 31 - (header % 31);
1467
1468
s.status = BUSY_STATE;
1469
putShortMSB(s, header);
1470
1471
/* Save the adler32 of the preset dictionary: */
1472
if (s.strstart !== 0) {
1473
putShortMSB(s, strm.adler >>> 16);
1474
putShortMSB(s, strm.adler & 0xffff);
1475
}
1476
strm.adler = 1; // adler32(0L, Z_NULL, 0);
1477
}
1478
}
1479
1480
//#ifdef GZIP
1481
if (s.status === EXTRA_STATE) {
1482
if (s.gzhead.extra/* != Z_NULL*/) {
1483
beg = s.pending; /* start of bytes to update crc */
1484
1485
while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
1486
if (s.pending === s.pending_buf_size) {
1487
if (s.gzhead.hcrc && s.pending > beg) {
1488
strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1489
}
1490
flush_pending(strm);
1491
beg = s.pending;
1492
if (s.pending === s.pending_buf_size) {
1493
break;
1494
}
1495
}
1496
put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
1497
s.gzindex++;
1498
}
1499
if (s.gzhead.hcrc && s.pending > beg) {
1500
strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1501
}
1502
if (s.gzindex === s.gzhead.extra.length) {
1503
s.gzindex = 0;
1504
s.status = NAME_STATE;
1505
}
1506
}
1507
else {
1508
s.status = NAME_STATE;
1509
}
1510
}
1511
if (s.status === NAME_STATE) {
1512
if (s.gzhead.name/* != Z_NULL*/) {
1513
beg = s.pending; /* start of bytes to update crc */
1514
//int val;
1515
1516
do {
1517
if (s.pending === s.pending_buf_size) {
1518
if (s.gzhead.hcrc && s.pending > beg) {
1519
strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1520
}
1521
flush_pending(strm);
1522
beg = s.pending;
1523
if (s.pending === s.pending_buf_size) {
1524
val = 1;
1525
break;
1526
}
1527
}
1528
// JS specific: little magic to add zero terminator to end of string
1529
if (s.gzindex < s.gzhead.name.length) {
1530
val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
1531
} else {
1532
val = 0;
1533
}
1534
put_byte(s, val);
1535
} while (val !== 0);
1536
1537
if (s.gzhead.hcrc && s.pending > beg){
1538
strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1539
}
1540
if (val === 0) {
1541
s.gzindex = 0;
1542
s.status = COMMENT_STATE;
1543
}
1544
}
1545
else {
1546
s.status = COMMENT_STATE;
1547
}
1548
}
1549
if (s.status === COMMENT_STATE) {
1550
if (s.gzhead.comment/* != Z_NULL*/) {
1551
beg = s.pending; /* start of bytes to update crc */
1552
//int val;
1553
1554
do {
1555
if (s.pending === s.pending_buf_size) {
1556
if (s.gzhead.hcrc && s.pending > beg) {
1557
strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1558
}
1559
flush_pending(strm);
1560
beg = s.pending;
1561
if (s.pending === s.pending_buf_size) {
1562
val = 1;
1563
break;
1564
}
1565
}
1566
// JS specific: little magic to add zero terminator to end of string
1567
if (s.gzindex < s.gzhead.comment.length) {
1568
val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
1569
} else {
1570
val = 0;
1571
}
1572
put_byte(s, val);
1573
} while (val !== 0);
1574
1575
if (s.gzhead.hcrc && s.pending > beg) {
1576
strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
1577
}
1578
if (val === 0) {
1579
s.status = HCRC_STATE;
1580
}
1581
}
1582
else {
1583
s.status = HCRC_STATE;
1584
}
1585
}
1586
if (s.status === HCRC_STATE) {
1587
if (s.gzhead.hcrc) {
1588
if (s.pending + 2 > s.pending_buf_size) {
1589
flush_pending(strm);
1590
}
1591
if (s.pending + 2 <= s.pending_buf_size) {
1592
put_byte(s, strm.adler & 0xff);
1593
put_byte(s, (strm.adler >> 8) & 0xff);
1594
strm.adler = 0; //crc32(0L, Z_NULL, 0);
1595
s.status = BUSY_STATE;
1596
}
1597
}
1598
else {
1599
s.status = BUSY_STATE;
1600
}
1601
}
1602
//#endif
1603
1604
/* Flush as much pending output as possible */
1605
if (s.pending !== 0) {
1606
flush_pending(strm);
1607
if (strm.avail_out === 0) {
1608
/* Since avail_out is 0, deflate will be called again with
1609
* more output space, but possibly with both pending and
1610
* avail_in equal to zero. There won't be anything to do,
1611
* but this is not an error situation so make sure we
1612
* return OK instead of BUF_ERROR at next call of deflate:
1613
*/
1614
s.last_flush = -1;
1615
return Z_OK;
1616
}
1617
1618
/* Make sure there is something to do and avoid duplicate consecutive
1619
* flushes. For repeated and useless calls with Z_FINISH, we keep
1620
* returning Z_STREAM_END instead of Z_BUF_ERROR.
1621
*/
1622
} else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
1623
flush !== Z_FINISH) {
1624
return err(strm, Z_BUF_ERROR);
1625
}
1626
1627
/* User must not provide more input after the first FINISH: */
1628
if (s.status === FINISH_STATE && strm.avail_in !== 0) {
1629
return err(strm, Z_BUF_ERROR);
1630
}
1631
1632
/* Start a new block or continue the current one.
1633
*/
1634
if (strm.avail_in !== 0 || s.lookahead !== 0 ||
1635
(flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
1636
var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
1637
(s.strategy === Z_RLE ? deflate_rle(s, flush) :
1638
configuration_table[s.level].func(s, flush));
1639
1640
if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
1641
s.status = FINISH_STATE;
1642
}
1643
if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
1644
if (strm.avail_out === 0) {
1645
s.last_flush = -1;
1646
/* avoid BUF_ERROR next call, see above */
1647
}
1648
return Z_OK;
1649
/* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1650
* of deflate should use the same flush parameter to make sure
1651
* that the flush is complete. So we don't have to output an
1652
* empty block here, this will be done at next call. This also
1653
* ensures that for a very small output buffer, we emit at most
1654
* one empty block.
1655
*/
1656
}
1657
if (bstate === BS_BLOCK_DONE) {
1658
if (flush === Z_PARTIAL_FLUSH) {
1659
trees._tr_align(s);
1660
}
1661
else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1662
1663
trees._tr_stored_block(s, 0, 0, false);
1664
/* For a full flush, this empty block will be recognized
1665
* as a special marker by inflate_sync().
1666
*/
1667
if (flush === Z_FULL_FLUSH) {
1668
/*** CLEAR_HASH(s); ***/ /* forget history */
1669
zero(s.head); // Fill with NIL (= 0);
1670
1671
if (s.lookahead === 0) {
1672
s.strstart = 0;
1673
s.block_start = 0;
1674
s.insert = 0;
1675
}
1676
}
1677
}
1678
flush_pending(strm);
1679
if (strm.avail_out === 0) {
1680
s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1681
return Z_OK;
1682
}
1683
}
1684
}
1685
//Assert(strm->avail_out > 0, "bug2");
1686
//if (strm.avail_out <= 0) { throw new Error("bug2");}
1687
1688
if (flush !== Z_FINISH) { return Z_OK; }
1689
if (s.wrap <= 0) { return Z_STREAM_END; }
1690
1691
/* Write the trailer */
1692
if (s.wrap === 2) {
1693
put_byte(s, strm.adler & 0xff);
1694
put_byte(s, (strm.adler >> 8) & 0xff);
1695
put_byte(s, (strm.adler >> 16) & 0xff);
1696
put_byte(s, (strm.adler >> 24) & 0xff);
1697
put_byte(s, strm.total_in & 0xff);
1698
put_byte(s, (strm.total_in >> 8) & 0xff);
1699
put_byte(s, (strm.total_in >> 16) & 0xff);
1700
put_byte(s, (strm.total_in >> 24) & 0xff);
1701
}
1702
else
1703
{
1704
putShortMSB(s, strm.adler >>> 16);
1705
putShortMSB(s, strm.adler & 0xffff);
1706
}
1707
1708
flush_pending(strm);
1709
/* If avail_out is zero, the application will call deflate again
1710
* to flush the rest.
1711
*/
1712
if (s.wrap > 0) { s.wrap = -s.wrap; }
1713
/* write the trailer only once! */
1714
return s.pending !== 0 ? Z_OK : Z_STREAM_END;
1715
}
1716
1717
function deflateEnd(strm) {
1718
var status;
1719
1720
if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
1721
return Z_STREAM_ERROR;
1722
}
1723
1724
status = strm.state.status;
1725
if (status !== INIT_STATE &&
1726
status !== EXTRA_STATE &&
1727
status !== NAME_STATE &&
1728
status !== COMMENT_STATE &&
1729
status !== HCRC_STATE &&
1730
status !== BUSY_STATE &&
1731
status !== FINISH_STATE
1732
) {
1733
return err(strm, Z_STREAM_ERROR);
1734
}
1735
1736
strm.state = null;
1737
1738
return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
1739
}
1740
1741
/* =========================================================================
1742
* Copy the source state to the destination state
1743
*/
1744
//function deflateCopy(dest, source) {
1745
//
1746
//}
1747
1748
exports.deflateInit = deflateInit;
1749
exports.deflateInit2 = deflateInit2;
1750
exports.deflateReset = deflateReset;
1751
exports.deflateResetKeep = deflateResetKeep;
1752
exports.deflateSetHeader = deflateSetHeader;
1753
exports.deflate = deflate;
1754
exports.deflateEnd = deflateEnd;
1755
exports.deflateInfo = 'pako deflate (from Nodeca project)';
1756
1757
/* Not implemented
1758
exports.deflateBound = deflateBound;
1759
exports.deflateCopy = deflateCopy;
1760
exports.deflateSetDictionary = deflateSetDictionary;
1761
exports.deflateParams = deflateParams;
1762
exports.deflatePending = deflatePending;
1763
exports.deflatePrime = deflatePrime;
1764
exports.deflateTune = deflateTune;
1765
*/
1766