You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4589 lines
132 KiB

1 year ago
  1. /*! pako 2.0.4 https://github.com/nodeca/pako @license (MIT AND Zlib) */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.pako = {}));
  6. }(this, (function (exports) { 'use strict';
  7. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  8. //
  9. // This software is provided 'as-is', without any express or implied
  10. // warranty. In no event will the authors be held liable for any damages
  11. // arising from the use of this software.
  12. //
  13. // Permission is granted to anyone to use this software for any purpose,
  14. // including commercial applications, and to alter it and redistribute it
  15. // freely, subject to the following restrictions:
  16. //
  17. // 1. The origin of this software must not be misrepresented; you must not
  18. // claim that you wrote the original software. If you use this software
  19. // in a product, an acknowledgment in the product documentation would be
  20. // appreciated but is not required.
  21. // 2. Altered source versions must be plainly marked as such, and must not be
  22. // misrepresented as being the original software.
  23. // 3. This notice may not be removed or altered from any source distribution.
  24. /* eslint-disable space-unary-ops */
  25. /* Public constants ==========================================================*/
  26. /* ===========================================================================*/
  27. //const Z_FILTERED = 1;
  28. //const Z_HUFFMAN_ONLY = 2;
  29. //const Z_RLE = 3;
  30. var Z_FIXED$1 = 4; //const Z_DEFAULT_STRATEGY = 0;
  31. /* Possible values of the data_type field (though see inflate()) */
  32. var Z_BINARY = 0;
  33. var Z_TEXT = 1; //const Z_ASCII = 1; // = Z_TEXT
  34. var Z_UNKNOWN$1 = 2;
  35. /*============================================================================*/
  36. function zero$1(buf) {
  37. var len = buf.length;
  38. while (--len >= 0) {
  39. buf[len] = 0;
  40. }
  41. } // From zutil.h
  42. var STORED_BLOCK = 0;
  43. var STATIC_TREES = 1;
  44. var DYN_TREES = 2;
  45. /* The three kinds of block type */
  46. var MIN_MATCH$1 = 3;
  47. var MAX_MATCH$1 = 258;
  48. /* The minimum and maximum match lengths */
  49. // From deflate.h
  50. /* ===========================================================================
  51. * Internal compression state.
  52. */
  53. var LENGTH_CODES$1 = 29;
  54. /* number of length codes, not counting the special END_BLOCK code */
  55. var LITERALS$1 = 256;
  56. /* number of literal bytes 0..255 */
  57. var L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
  58. /* number of Literal or Length codes, including the END_BLOCK code */
  59. var D_CODES$1 = 30;
  60. /* number of distance codes */
  61. var BL_CODES$1 = 19;
  62. /* number of codes used to transfer the bit lengths */
  63. var HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
  64. /* maximum heap size */
  65. var MAX_BITS$1 = 15;
  66. /* All codes must not exceed MAX_BITS bits */
  67. var Buf_size = 16;
  68. /* size of bit buffer in bi_buf */
  69. /* ===========================================================================
  70. * Constants
  71. */
  72. var MAX_BL_BITS = 7;
  73. /* Bit length codes must not exceed MAX_BL_BITS bits */
  74. var END_BLOCK = 256;
  75. /* end of block literal code */
  76. var REP_3_6 = 16;
  77. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  78. var REPZ_3_10 = 17;
  79. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  80. var REPZ_11_138 = 18;
  81. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  82. /* eslint-disable comma-spacing,array-bracket-spacing */
  83. var extra_lbits =
  84. /* extra bits for each length code */
  85. new Uint8Array([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]);
  86. var extra_dbits =
  87. /* extra bits for each distance code */
  88. new Uint8Array([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]);
  89. var extra_blbits =
  90. /* extra bits for each bit length code */
  91. new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7]);
  92. var bl_order = new Uint8Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  93. /* eslint-enable comma-spacing,array-bracket-spacing */
  94. /* The lengths of the bit length codes are sent in order of decreasing
  95. * probability, to avoid transmitting the lengths for unused bit length codes.
  96. */
  97. /* ===========================================================================
  98. * Local data. These are initialized only once.
  99. */
  100. // We pre-fill arrays with 0 to avoid uninitialized gaps
  101. var DIST_CODE_LEN = 512;
  102. /* see definition of array dist_code below */
  103. // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
  104. var static_ltree = new Array((L_CODES$1 + 2) * 2);
  105. zero$1(static_ltree);
  106. /* The static literal tree. Since the bit lengths are imposed, there is no
  107. * need for the L_CODES extra codes used during heap construction. However
  108. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  109. * below).
  110. */
  111. var static_dtree = new Array(D_CODES$1 * 2);
  112. zero$1(static_dtree);
  113. /* The static distance tree. (Actually a trivial tree since all codes use
  114. * 5 bits.)
  115. */
  116. var _dist_code = new Array(DIST_CODE_LEN);
  117. zero$1(_dist_code);
  118. /* Distance codes. The first 256 values correspond to the distances
  119. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  120. * the 15 bit distances.
  121. */
  122. var _length_code = new Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
  123. zero$1(_length_code);
  124. /* length code for each normalized match length (0 == MIN_MATCH) */
  125. var base_length = new Array(LENGTH_CODES$1);
  126. zero$1(base_length);
  127. /* First normalized length for each code (0 = MIN_MATCH) */
  128. var base_dist = new Array(D_CODES$1);
  129. zero$1(base_dist);
  130. /* First normalized distance for each code (0 = distance of 1) */
  131. function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
  132. this.static_tree = static_tree;
  133. /* static tree or NULL */
  134. this.extra_bits = extra_bits;
  135. /* extra bits for each code or NULL */
  136. this.extra_base = extra_base;
  137. /* base index for extra_bits */
  138. this.elems = elems;
  139. /* max number of elements in the tree */
  140. this.max_length = max_length;
  141. /* max bit length for the codes */
  142. // show if `static_tree` has data or dummy - needed for monomorphic objects
  143. this.has_stree = static_tree && static_tree.length;
  144. }
  145. var static_l_desc;
  146. var static_d_desc;
  147. var static_bl_desc;
  148. function TreeDesc(dyn_tree, stat_desc) {
  149. this.dyn_tree = dyn_tree;
  150. /* the dynamic tree */
  151. this.max_code = 0;
  152. /* largest code with non zero frequency */
  153. this.stat_desc = stat_desc;
  154. /* the corresponding static tree */
  155. }
  156. var d_code = function d_code(dist) {
  157. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  158. };
  159. /* ===========================================================================
  160. * Output a short LSB first on the stream.
  161. * IN assertion: there is enough room in pendingBuf.
  162. */
  163. var put_short = function put_short(s, w) {
  164. // put_byte(s, (uch)((w) & 0xff));
  165. // put_byte(s, (uch)((ush)(w) >> 8));
  166. s.pending_buf[s.pending++] = w & 0xff;
  167. s.pending_buf[s.pending++] = w >>> 8 & 0xff;
  168. };
  169. /* ===========================================================================
  170. * Send a value on a given number of bits.
  171. * IN assertion: length <= 16 and value fits in length bits.
  172. */
  173. var send_bits = function send_bits(s, value, length) {
  174. if (s.bi_valid > Buf_size - length) {
  175. s.bi_buf |= value << s.bi_valid & 0xffff;
  176. put_short(s, s.bi_buf);
  177. s.bi_buf = value >> Buf_size - s.bi_valid;
  178. s.bi_valid += length - Buf_size;
  179. } else {
  180. s.bi_buf |= value << s.bi_valid & 0xffff;
  181. s.bi_valid += length;
  182. }
  183. };
  184. var send_code = function send_code(s, c, tree) {
  185. send_bits(s, tree[c * 2]
  186. /*.Code*/
  187. , tree[c * 2 + 1]
  188. /*.Len*/
  189. );
  190. };
  191. /* ===========================================================================
  192. * Reverse the first len bits of a code, using straightforward code (a faster
  193. * method would use a table)
  194. * IN assertion: 1 <= len <= 15
  195. */
  196. var bi_reverse = function bi_reverse(code, len) {
  197. var res = 0;
  198. do {
  199. res |= code & 1;
  200. code >>>= 1;
  201. res <<= 1;
  202. } while (--len > 0);
  203. return res >>> 1;
  204. };
  205. /* ===========================================================================
  206. * Flush the bit buffer, keeping at most 7 bits in it.
  207. */
  208. var bi_flush = function bi_flush(s) {
  209. if (s.bi_valid === 16) {
  210. put_short(s, s.bi_buf);
  211. s.bi_buf = 0;
  212. s.bi_valid = 0;
  213. } else if (s.bi_valid >= 8) {
  214. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  215. s.bi_buf >>= 8;
  216. s.bi_valid -= 8;
  217. }
  218. };
  219. /* ===========================================================================
  220. * Compute the optimal bit lengths for a tree and update the total bit length
  221. * for the current block.
  222. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  223. * above are the tree nodes sorted by increasing frequency.
  224. * OUT assertions: the field len is set to the optimal bit length, the
  225. * array bl_count contains the frequencies for each bit length.
  226. * The length opt_len is updated; static_len is also updated if stree is
  227. * not null.
  228. */
  229. var gen_bitlen = function gen_bitlen(s, desc) // deflate_state *s;
  230. // tree_desc *desc; /* the tree descriptor */
  231. {
  232. var tree = desc.dyn_tree;
  233. var max_code = desc.max_code;
  234. var stree = desc.stat_desc.static_tree;
  235. var has_stree = desc.stat_desc.has_stree;
  236. var extra = desc.stat_desc.extra_bits;
  237. var base = desc.stat_desc.extra_base;
  238. var max_length = desc.stat_desc.max_length;
  239. var h;
  240. /* heap index */
  241. var n, m;
  242. /* iterate over the tree elements */
  243. var bits;
  244. /* bit length */
  245. var xbits;
  246. /* extra bits */
  247. var f;
  248. /* frequency */
  249. var overflow = 0;
  250. /* number of elements with bit length too large */
  251. for (bits = 0; bits <= MAX_BITS$1; bits++) {
  252. s.bl_count[bits] = 0;
  253. }
  254. /* In a first pass, compute the optimal bit lengths (which may
  255. * overflow in the case of the bit length tree).
  256. */
  257. tree[s.heap[s.heap_max] * 2 + 1]
  258. /*.Len*/
  259. = 0;
  260. /* root of the heap */
  261. for (h = s.heap_max + 1; h < HEAP_SIZE$1; h++) {
  262. n = s.heap[h];
  263. bits = tree[tree[n * 2 + 1]
  264. /*.Dad*/
  265. * 2 + 1]
  266. /*.Len*/
  267. + 1;
  268. if (bits > max_length) {
  269. bits = max_length;
  270. overflow++;
  271. }
  272. tree[n * 2 + 1]
  273. /*.Len*/
  274. = bits;
  275. /* We overwrite tree[n].Dad which is no longer needed */
  276. if (n > max_code) {
  277. continue;
  278. }
  279. /* not a leaf node */
  280. s.bl_count[bits]++;
  281. xbits = 0;
  282. if (n >= base) {
  283. xbits = extra[n - base];
  284. }
  285. f = tree[n * 2]
  286. /*.Freq*/
  287. ;
  288. s.opt_len += f * (bits + xbits);
  289. if (has_stree) {
  290. s.static_len += f * (stree[n * 2 + 1]
  291. /*.Len*/
  292. + xbits);
  293. }
  294. }
  295. if (overflow === 0) {
  296. return;
  297. } // Trace((stderr,"\nbit length overflow\n"));
  298. /* This happens for example on obj2 and pic of the Calgary corpus */
  299. /* Find the first bit length which could increase: */
  300. do {
  301. bits = max_length - 1;
  302. while (s.bl_count[bits] === 0) {
  303. bits--;
  304. }
  305. s.bl_count[bits]--;
  306. /* move one leaf down the tree */
  307. s.bl_count[bits + 1] += 2;
  308. /* move one overflow item as its brother */
  309. s.bl_count[max_length]--;
  310. /* The brother of the overflow item also moves one step up,
  311. * but this does not affect bl_count[max_length]
  312. */
  313. overflow -= 2;
  314. } while (overflow > 0);
  315. /* Now recompute all bit lengths, scanning in increasing frequency.
  316. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  317. * lengths instead of fixing only the wrong ones. This idea is taken
  318. * from 'ar' written by Haruhiko Okumura.)
  319. */
  320. for (bits = max_length; bits !== 0; bits--) {
  321. n = s.bl_count[bits];
  322. while (n !== 0) {
  323. m = s.heap[--h];
  324. if (m > max_code) {
  325. continue;
  326. }
  327. if (tree[m * 2 + 1]
  328. /*.Len*/
  329. !== bits) {
  330. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  331. s.opt_len += (bits - tree[m * 2 + 1]
  332. /*.Len*/
  333. ) * tree[m * 2]
  334. /*.Freq*/
  335. ;
  336. tree[m * 2 + 1]
  337. /*.Len*/
  338. = bits;
  339. }
  340. n--;
  341. }
  342. }
  343. };
  344. /* ===========================================================================
  345. * Generate the codes for a given tree and bit counts (which need not be
  346. * optimal).
  347. * IN assertion: the array bl_count contains the bit length statistics for
  348. * the given tree and the field len is set for all tree elements.
  349. * OUT assertion: the field code is set for all tree elements of non
  350. * zero code length.
  351. */
  352. var gen_codes = function gen_codes(tree, max_code, bl_count) // ct_data *tree; /* the tree to decorate */
  353. // int max_code; /* largest code with non zero frequency */
  354. // ushf *bl_count; /* number of codes at each bit length */
  355. {
  356. var next_code = new Array(MAX_BITS$1 + 1);
  357. /* next code value for each bit length */
  358. var code = 0;
  359. /* running code value */
  360. var bits;
  361. /* bit index */
  362. var n;
  363. /* code index */
  364. /* The distribution counts are first used to generate the code values
  365. * without bit reversal.
  366. */
  367. for (bits = 1; bits <= MAX_BITS$1; bits++) {
  368. next_code[bits] = code = code + bl_count[bits - 1] << 1;
  369. }
  370. /* Check that the bit counts in bl_count are consistent. The last code
  371. * must be all ones.
  372. */
  373. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  374. // "inconsistent bit counts");
  375. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  376. for (n = 0; n <= max_code; n++) {
  377. var len = tree[n * 2 + 1]
  378. /*.Len*/
  379. ;
  380. if (len === 0) {
  381. continue;
  382. }
  383. /* Now reverse the bits */
  384. tree[n * 2]
  385. /*.Code*/
  386. = bi_reverse(next_code[len]++, len); //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  387. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  388. }
  389. };
  390. /* ===========================================================================
  391. * Initialize the various 'constant' tables.
  392. */
  393. var tr_static_init = function tr_static_init() {
  394. var n;
  395. /* iterates over tree elements */
  396. var bits;
  397. /* bit counter */
  398. var length;
  399. /* length value */
  400. var code;
  401. /* code value */
  402. var dist;
  403. /* distance index */
  404. var bl_count = new Array(MAX_BITS$1 + 1);
  405. /* number of codes at each bit length for an optimal tree */
  406. // do check in _tr_init()
  407. //if (static_init_done) return;
  408. /* For some embedded targets, global variables are not initialized: */
  409. /*#ifdef NO_INIT_GLOBAL_POINTERS
  410. static_l_desc.static_tree = static_ltree;
  411. static_l_desc.extra_bits = extra_lbits;
  412. static_d_desc.static_tree = static_dtree;
  413. static_d_desc.extra_bits = extra_dbits;
  414. static_bl_desc.extra_bits = extra_blbits;
  415. #endif*/
  416. /* Initialize the mapping length (0..255) -> length code (0..28) */
  417. length = 0;
  418. for (code = 0; code < LENGTH_CODES$1 - 1; code++) {
  419. base_length[code] = length;
  420. for (n = 0; n < 1 << extra_lbits[code]; n++) {
  421. _length_code[length++] = code;
  422. }
  423. } //Assert (length == 256, "tr_static_init: length != 256");
  424. /* Note that the length 255 (match length 258) can be represented
  425. * in two different ways: code 284 + 5 bits or code 285, so we
  426. * overwrite length_code[255] to use the best encoding:
  427. */
  428. _length_code[length - 1] = code;
  429. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  430. dist = 0;
  431. for (code = 0; code < 16; code++) {
  432. base_dist[code] = dist;
  433. for (n = 0; n < 1 << extra_dbits[code]; n++) {
  434. _dist_code[dist++] = code;
  435. }
  436. } //Assert (dist == 256, "tr_static_init: dist != 256");
  437. dist >>= 7;
  438. /* from now on, all distances are divided by 128 */
  439. for (; code < D_CODES$1; code++) {
  440. base_dist[code] = dist << 7;
  441. for (n = 0; n < 1 << extra_dbits[code] - 7; n++) {
  442. _dist_code[256 + dist++] = code;
  443. }
  444. } //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  445. /* Construct the codes of the static literal tree */
  446. for (bits = 0; bits <= MAX_BITS$1; bits++) {
  447. bl_count[bits] = 0;
  448. }
  449. n = 0;
  450. while (n <= 143) {
  451. static_ltree[n * 2 + 1]
  452. /*.Len*/
  453. = 8;
  454. n++;
  455. bl_count[8]++;
  456. }
  457. while (n <= 255) {
  458. static_ltree[n * 2 + 1]
  459. /*.Len*/
  460. = 9;
  461. n++;
  462. bl_count[9]++;
  463. }
  464. while (n <= 279) {
  465. static_ltree[n * 2 + 1]
  466. /*.Len*/
  467. = 7;
  468. n++;
  469. bl_count[7]++;
  470. }
  471. while (n <= 287) {
  472. static_ltree[n * 2 + 1]
  473. /*.Len*/
  474. = 8;
  475. n++;
  476. bl_count[8]++;
  477. }
  478. /* Codes 286 and 287 do not exist, but we must include them in the
  479. * tree construction to get a canonical Huffman tree (longest code
  480. * all ones)
  481. */
  482. gen_codes(static_ltree, L_CODES$1 + 1, bl_count);
  483. /* The static distance tree is trivial: */
  484. for (n = 0; n < D_CODES$1; n++) {
  485. static_dtree[n * 2 + 1]
  486. /*.Len*/
  487. = 5;
  488. static_dtree[n * 2]
  489. /*.Code*/
  490. = bi_reverse(n, 5);
  491. } // Now data ready and we can init static trees
  492. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS$1 + 1, L_CODES$1, MAX_BITS$1);
  493. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES$1, MAX_BITS$1);
  494. static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES$1, MAX_BL_BITS); //static_init_done = true;
  495. };
  496. /* ===========================================================================
  497. * Initialize a new block.
  498. */
  499. var init_block = function init_block(s) {
  500. var n;
  501. /* iterates over tree elements */
  502. /* Initialize the trees. */
  503. for (n = 0; n < L_CODES$1; n++) {
  504. s.dyn_ltree[n * 2]
  505. /*.Freq*/
  506. = 0;
  507. }
  508. for (n = 0; n < D_CODES$1; n++) {
  509. s.dyn_dtree[n * 2]
  510. /*.Freq*/
  511. = 0;
  512. }
  513. for (n = 0; n < BL_CODES$1; n++) {
  514. s.bl_tree[n * 2]
  515. /*.Freq*/
  516. = 0;
  517. }
  518. s.dyn_ltree[END_BLOCK * 2]
  519. /*.Freq*/
  520. = 1;
  521. s.opt_len = s.static_len = 0;
  522. s.last_lit = s.matches = 0;
  523. };
  524. /* ===========================================================================
  525. * Flush the bit buffer and align the output on a byte boundary
  526. */
  527. var bi_windup = function bi_windup(s) {
  528. if (s.bi_valid > 8) {
  529. put_short(s, s.bi_buf);
  530. } else if (s.bi_valid > 0) {
  531. //put_byte(s, (Byte)s->bi_buf);
  532. s.pending_buf[s.pending++] = s.bi_buf;
  533. }
  534. s.bi_buf = 0;
  535. s.bi_valid = 0;
  536. };
  537. /* ===========================================================================
  538. * Copy a stored block, storing first the length and its
  539. * one's complement if requested.
  540. */
  541. var copy_block = function copy_block(s, buf, len, header) //DeflateState *s;
  542. //charf *buf; /* the input data */
  543. //unsigned len; /* its length */
  544. //int header; /* true if block header must be written */
  545. {
  546. bi_windup(s);
  547. /* align on byte boundary */
  548. if (header) {
  549. put_short(s, len);
  550. put_short(s, ~len);
  551. } // while (len--) {
  552. // put_byte(s, *buf++);
  553. // }
  554. s.pending_buf.set(s.window.subarray(buf, buf + len), s.pending);
  555. s.pending += len;
  556. };
  557. /* ===========================================================================
  558. * Compares to subtrees, using the tree depth as tie breaker when
  559. * the subtrees have equal frequency. This minimizes the worst case length.
  560. */
  561. var smaller = function smaller(tree, n, m, depth) {
  562. var _n2 = n * 2;
  563. var _m2 = m * 2;
  564. return tree[_n2]
  565. /*.Freq*/
  566. < tree[_m2]
  567. /*.Freq*/
  568. || tree[_n2]
  569. /*.Freq*/
  570. === tree[_m2]
  571. /*.Freq*/
  572. && depth[n] <= depth[m];
  573. };
  574. /* ===========================================================================
  575. * Restore the heap property by moving down the tree starting at node k,
  576. * exchanging a node with the smallest of its two sons if necessary, stopping
  577. * when the heap property is re-established (each father smaller than its
  578. * two sons).
  579. */
  580. var pqdownheap = function pqdownheap(s, tree, k) // deflate_state *s;
  581. // ct_data *tree; /* the tree to restore */
  582. // int k; /* node to move down */
  583. {
  584. var v = s.heap[k];
  585. var j = k << 1;
  586. /* left son of k */
  587. while (j <= s.heap_len) {
  588. /* Set j to the smallest of the two sons: */
  589. if (j < s.heap_len && smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
  590. j++;
  591. }
  592. /* Exit if v is smaller than both sons */
  593. if (smaller(tree, v, s.heap[j], s.depth)) {
  594. break;
  595. }
  596. /* Exchange v with the smallest son */
  597. s.heap[k] = s.heap[j];
  598. k = j;
  599. /* And continue down the tree, setting j to the left son of k */
  600. j <<= 1;
  601. }
  602. s.heap[k] = v;
  603. }; // inlined manually
  604. // const SMALLEST = 1;
  605. /* ===========================================================================
  606. * Send the block data compressed using the given Huffman trees
  607. */
  608. var compress_block = function compress_block(s, ltree, dtree) // deflate_state *s;
  609. // const ct_data *ltree; /* literal tree */
  610. // const ct_data *dtree; /* distance tree */
  611. {
  612. var dist;
  613. /* distance of matched string */
  614. var lc;
  615. /* match length or unmatched char (if dist == 0) */
  616. var lx = 0;
  617. /* running index in l_buf */
  618. var code;
  619. /* the code to send */
  620. var extra;
  621. /* number of extra bits to send */
  622. if (s.last_lit !== 0) {
  623. do {
  624. dist = s.pending_buf[s.d_buf + lx * 2] << 8 | s.pending_buf[s.d_buf + lx * 2 + 1];
  625. lc = s.pending_buf[s.l_buf + lx];
  626. lx++;
  627. if (dist === 0) {
  628. send_code(s, lc, ltree);
  629. /* send a literal byte */
  630. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  631. } else {
  632. /* Here, lc is the match length - MIN_MATCH */
  633. code = _length_code[lc];
  634. send_code(s, code + LITERALS$1 + 1, ltree);
  635. /* send the length code */
  636. extra = extra_lbits[code];
  637. if (extra !== 0) {
  638. lc -= base_length[code];
  639. send_bits(s, lc, extra);
  640. /* send the extra length bits */
  641. }
  642. dist--;
  643. /* dist is now the match distance - 1 */
  644. code = d_code(dist); //Assert (code < D_CODES, "bad d_code");
  645. send_code(s, code, dtree);
  646. /* send the distance code */
  647. extra = extra_dbits[code];
  648. if (extra !== 0) {
  649. dist -= base_dist[code];
  650. send_bits(s, dist, extra);
  651. /* send the extra distance bits */
  652. }
  653. }
  654. /* literal or match pair ? */
  655. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  656. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  657. // "pendingBuf overflow");
  658. } while (lx < s.last_lit);
  659. }
  660. send_code(s, END_BLOCK, ltree);
  661. };
  662. /* ===========================================================================
  663. * Construct one Huffman tree and assigns the code bit strings and lengths.
  664. * Update the total bit length for the current block.
  665. * IN assertion: the field freq is set for all tree elements.
  666. * OUT assertions: the fields len and code are set to the optimal bit length
  667. * and corresponding code. The length opt_len is updated; static_len is
  668. * also updated if stree is not null. The field max_code is set.
  669. */
  670. var build_tree = function build_tree(s, desc) // deflate_state *s;
  671. // tree_desc *desc; /* the tree descriptor */
  672. {
  673. var tree = desc.dyn_tree;
  674. var stree = desc.stat_desc.static_tree;
  675. var has_stree = desc.stat_desc.has_stree;
  676. var elems = desc.stat_desc.elems;
  677. var n, m;
  678. /* iterate over heap elements */
  679. var max_code = -1;
  680. /* largest code with non zero frequency */
  681. var node;
  682. /* new node being created */
  683. /* Construct the initial heap, with least frequent element in
  684. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  685. * heap[0] is not used.
  686. */
  687. s.heap_len = 0;
  688. s.heap_max = HEAP_SIZE$1;
  689. for (n = 0; n < elems; n++) {
  690. if (tree[n * 2]
  691. /*.Freq*/
  692. !== 0) {
  693. s.heap[++s.heap_len] = max_code = n;
  694. s.depth[n] = 0;
  695. } else {
  696. tree[n * 2 + 1]
  697. /*.Len*/
  698. = 0;
  699. }
  700. }
  701. /* The pkzip format requires that at least one distance code exists,
  702. * and that at least one bit should be sent even if there is only one
  703. * possible code. So to avoid special checks later on we force at least
  704. * two codes of non zero frequency.
  705. */
  706. while (s.heap_len < 2) {
  707. node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
  708. tree[node * 2]
  709. /*.Freq*/
  710. = 1;
  711. s.depth[node] = 0;
  712. s.opt_len--;
  713. if (has_stree) {
  714. s.static_len -= stree[node * 2 + 1]
  715. /*.Len*/
  716. ;
  717. }
  718. /* node is 0 or 1 so it does not have extra bits */
  719. }
  720. desc.max_code = max_code;
  721. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  722. * establish sub-heaps of increasing lengths:
  723. */
  724. for (n = s.heap_len >> 1
  725. /*int /2*/
  726. ; n >= 1; n--) {
  727. pqdownheap(s, tree, n);
  728. }
  729. /* Construct the Huffman tree by repeatedly combining the least two
  730. * frequent nodes.
  731. */
  732. node = elems;
  733. /* next internal node of the tree */
  734. do {
  735. //pqremove(s, tree, n); /* n = node of least frequency */
  736. /*** pqremove ***/
  737. n = s.heap[1
  738. /*SMALLEST*/
  739. ];
  740. s.heap[1
  741. /*SMALLEST*/
  742. ] = s.heap[s.heap_len--];
  743. pqdownheap(s, tree, 1
  744. /*SMALLEST*/
  745. );
  746. /***/
  747. m = s.heap[1
  748. /*SMALLEST*/
  749. ];
  750. /* m = node of next least frequency */
  751. s.heap[--s.heap_max] = n;
  752. /* keep the nodes sorted by frequency */
  753. s.heap[--s.heap_max] = m;
  754. /* Create a new node father of n and m */
  755. tree[node * 2]
  756. /*.Freq*/
  757. = tree[n * 2]
  758. /*.Freq*/
  759. + tree[m * 2]
  760. /*.Freq*/
  761. ;
  762. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  763. tree[n * 2 + 1]
  764. /*.Dad*/
  765. = tree[m * 2 + 1]
  766. /*.Dad*/
  767. = node;
  768. /* and insert the new node in the heap */
  769. s.heap[1
  770. /*SMALLEST*/
  771. ] = node++;
  772. pqdownheap(s, tree, 1
  773. /*SMALLEST*/
  774. );
  775. } while (s.heap_len >= 2);
  776. s.heap[--s.heap_max] = s.heap[1
  777. /*SMALLEST*/
  778. ];
  779. /* At this point, the fields freq and dad are set. We can now
  780. * generate the bit lengths.
  781. */
  782. gen_bitlen(s, desc);
  783. /* The field len is now set, we can generate the bit codes */
  784. gen_codes(tree, max_code, s.bl_count);
  785. };
  786. /* ===========================================================================
  787. * Scan a literal or distance tree to determine the frequencies of the codes
  788. * in the bit length tree.
  789. */
  790. var scan_tree = function scan_tree(s, tree, max_code) // deflate_state *s;
  791. // ct_data *tree; /* the tree to be scanned */
  792. // int max_code; /* and its largest code of non zero frequency */
  793. {
  794. var n;
  795. /* iterates over all tree elements */
  796. var prevlen = -1;
  797. /* last emitted length */
  798. var curlen;
  799. /* length of current code */
  800. var nextlen = tree[0 * 2 + 1]
  801. /*.Len*/
  802. ;
  803. /* length of next code */
  804. var count = 0;
  805. /* repeat count of the current code */
  806. var max_count = 7;
  807. /* max repeat count */
  808. var min_count = 4;
  809. /* min repeat count */
  810. if (nextlen === 0) {
  811. max_count = 138;
  812. min_count = 3;
  813. }
  814. tree[(max_code + 1) * 2 + 1]
  815. /*.Len*/
  816. = 0xffff;
  817. /* guard */
  818. for (n = 0; n <= max_code; n++) {
  819. curlen = nextlen;
  820. nextlen = tree[(n + 1) * 2 + 1]
  821. /*.Len*/
  822. ;
  823. if (++count < max_count && curlen === nextlen) {
  824. continue;
  825. } else if (count < min_count) {
  826. s.bl_tree[curlen * 2]
  827. /*.Freq*/
  828. += count;
  829. } else if (curlen !== 0) {
  830. if (curlen !== prevlen) {
  831. s.bl_tree[curlen * 2] /*.Freq*/++;
  832. }
  833. s.bl_tree[REP_3_6 * 2] /*.Freq*/++;
  834. } else if (count <= 10) {
  835. s.bl_tree[REPZ_3_10 * 2] /*.Freq*/++;
  836. } else {
  837. s.bl_tree[REPZ_11_138 * 2] /*.Freq*/++;
  838. }
  839. count = 0;
  840. prevlen = curlen;
  841. if (nextlen === 0) {
  842. max_count = 138;
  843. min_count = 3;
  844. } else if (curlen === nextlen) {
  845. max_count = 6;
  846. min_count = 3;
  847. } else {
  848. max_count = 7;
  849. min_count = 4;
  850. }
  851. }
  852. };
  853. /* ===========================================================================
  854. * Send a literal or distance tree in compressed form, using the codes in
  855. * bl_tree.
  856. */
  857. var send_tree = function send_tree(s, tree, max_code) // deflate_state *s;
  858. // ct_data *tree; /* the tree to be scanned */
  859. // int max_code; /* and its largest code of non zero frequency */
  860. {
  861. var n;
  862. /* iterates over all tree elements */
  863. var prevlen = -1;
  864. /* last emitted length */
  865. var curlen;
  866. /* length of current code */
  867. var nextlen = tree[0 * 2 + 1]
  868. /*.Len*/
  869. ;
  870. /* length of next code */
  871. var count = 0;
  872. /* repeat count of the current code */
  873. var max_count = 7;
  874. /* max repeat count */
  875. var min_count = 4;
  876. /* min repeat count */
  877. /* tree[max_code+1].Len = -1; */
  878. /* guard already set */
  879. if (nextlen === 0) {
  880. max_count = 138;
  881. min_count = 3;
  882. }
  883. for (n = 0; n <= max_code; n++) {
  884. curlen = nextlen;
  885. nextlen = tree[(n + 1) * 2 + 1]
  886. /*.Len*/
  887. ;
  888. if (++count < max_count && curlen === nextlen) {
  889. continue;
  890. } else if (count < min_count) {
  891. do {
  892. send_code(s, curlen, s.bl_tree);
  893. } while (--count !== 0);
  894. } else if (curlen !== 0) {
  895. if (curlen !== prevlen) {
  896. send_code(s, curlen, s.bl_tree);
  897. count--;
  898. } //Assert(count >= 3 && count <= 6, " 3_6?");
  899. send_code(s, REP_3_6, s.bl_tree);
  900. send_bits(s, count - 3, 2);
  901. } else if (count <= 10) {
  902. send_code(s, REPZ_3_10, s.bl_tree);
  903. send_bits(s, count - 3, 3);
  904. } else {
  905. send_code(s, REPZ_11_138, s.bl_tree);
  906. send_bits(s, count - 11, 7);
  907. }
  908. count = 0;
  909. prevlen = curlen;
  910. if (nextlen === 0) {
  911. max_count = 138;
  912. min_count = 3;
  913. } else if (curlen === nextlen) {
  914. max_count = 6;
  915. min_count = 3;
  916. } else {
  917. max_count = 7;
  918. min_count = 4;
  919. }
  920. }
  921. };
  922. /* ===========================================================================
  923. * Construct the Huffman tree for the bit lengths and return the index in
  924. * bl_order of the last bit length code to send.
  925. */
  926. var build_bl_tree = function build_bl_tree(s) {
  927. var max_blindex;
  928. /* index of last bit length code of non zero freq */
  929. /* Determine the bit length frequencies for literal and distance trees */
  930. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  931. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  932. /* Build the bit length tree: */
  933. build_tree(s, s.bl_desc);
  934. /* opt_len now includes the length of the tree representations, except
  935. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  936. */
  937. /* Determine the number of bit length codes to send. The pkzip format
  938. * requires that at least 4 bit length codes be sent. (appnote.txt says
  939. * 3 but the actual value used is 4.)
  940. */
  941. for (max_blindex = BL_CODES$1 - 1; max_blindex >= 3; max_blindex--) {
  942. if (s.bl_tree[bl_order[max_blindex] * 2 + 1]
  943. /*.Len*/
  944. !== 0) {
  945. break;
  946. }
  947. }
  948. /* Update opt_len to include the bit length tree and counts */
  949. s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  950. // s->opt_len, s->static_len));
  951. return max_blindex;
  952. };
  953. /* ===========================================================================
  954. * Send the header for a block using dynamic Huffman trees: the counts, the
  955. * lengths of the bit length codes, the literal tree and the distance tree.
  956. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  957. */
  958. var send_all_trees = function send_all_trees(s, lcodes, dcodes, blcodes) // deflate_state *s;
  959. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  960. {
  961. var rank;
  962. /* index in bl_order */
  963. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  964. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  965. // "too many codes");
  966. //Tracev((stderr, "\nbl counts: "));
  967. send_bits(s, lcodes - 257, 5);
  968. /* not +255 as stated in appnote.txt */
  969. send_bits(s, dcodes - 1, 5);
  970. send_bits(s, blcodes - 4, 4);
  971. /* not -3 as stated in appnote.txt */
  972. for (rank = 0; rank < blcodes; rank++) {
  973. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  974. send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]
  975. /*.Len*/
  976. , 3);
  977. } //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  978. send_tree(s, s.dyn_ltree, lcodes - 1);
  979. /* literal tree */
  980. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  981. send_tree(s, s.dyn_dtree, dcodes - 1);
  982. /* distance tree */
  983. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  984. };
  985. /* ===========================================================================
  986. * Check if the data type is TEXT or BINARY, using the following algorithm:
  987. * - TEXT if the two conditions below are satisfied:
  988. * a) There are no non-portable control characters belonging to the
  989. * "black list" (0..6, 14..25, 28..31).
  990. * b) There is at least one printable character belonging to the
  991. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  992. * - BINARY otherwise.
  993. * - The following partially-portable control characters form a
  994. * "gray list" that is ignored in this detection algorithm:
  995. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  996. * IN assertion: the fields Freq of dyn_ltree are set.
  997. */
  998. var detect_data_type = function detect_data_type(s) {
  999. /* black_mask is the bit mask of black-listed bytes
  1000. * set bits 0..6, 14..25, and 28..31
  1001. * 0xf3ffc07f = binary 11110011111111111100000001111111
  1002. */
  1003. var black_mask = 0xf3ffc07f;
  1004. var n;
  1005. /* Check for non-textual ("black-listed") bytes. */
  1006. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  1007. if (black_mask & 1 && s.dyn_ltree[n * 2]
  1008. /*.Freq*/
  1009. !== 0) {
  1010. return Z_BINARY;
  1011. }
  1012. }
  1013. /* Check for textual ("white-listed") bytes. */
  1014. if (s.dyn_ltree[9 * 2]
  1015. /*.Freq*/
  1016. !== 0 || s.dyn_ltree[10 * 2]
  1017. /*.Freq*/
  1018. !== 0 || s.dyn_ltree[13 * 2]
  1019. /*.Freq*/
  1020. !== 0) {
  1021. return Z_TEXT;
  1022. }
  1023. for (n = 32; n < LITERALS$1; n++) {
  1024. if (s.dyn_ltree[n * 2]
  1025. /*.Freq*/
  1026. !== 0) {
  1027. return Z_TEXT;
  1028. }
  1029. }
  1030. /* There are no "black-listed" or "white-listed" bytes:
  1031. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  1032. */
  1033. return Z_BINARY;
  1034. };
  1035. var static_init_done = false;
  1036. /* ===========================================================================
  1037. * Initialize the tree data structures for a new zlib stream.
  1038. */
  1039. var _tr_init$1 = function _tr_init(s) {
  1040. if (!static_init_done) {
  1041. tr_static_init();
  1042. static_init_done = true;
  1043. }
  1044. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  1045. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  1046. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  1047. s.bi_buf = 0;
  1048. s.bi_valid = 0;
  1049. /* Initialize the first block of the first file: */
  1050. init_block(s);
  1051. };
  1052. /* ===========================================================================
  1053. * Send a stored block
  1054. */
  1055. var _tr_stored_block$1 = function _tr_stored_block(s, buf, stored_len, last) //DeflateState *s;
  1056. //charf *buf; /* input block */
  1057. //ulg stored_len; /* length of input block */
  1058. //int last; /* one if this is the last block for a file */
  1059. {
  1060. send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3);
  1061. /* send block type */
  1062. copy_block(s, buf, stored_len, true);
  1063. /* with header */
  1064. };
  1065. /* ===========================================================================
  1066. * Send one empty static block to give enough lookahead for inflate.
  1067. * This takes 10 bits, of which 7 may remain in the bit buffer.
  1068. */
  1069. var _tr_align$1 = function _tr_align(s) {
  1070. send_bits(s, STATIC_TREES << 1, 3);
  1071. send_code(s, END_BLOCK, static_ltree);
  1072. bi_flush(s);
  1073. };
  1074. /* ===========================================================================
  1075. * Determine the best encoding for the current block: dynamic trees, static
  1076. * trees or store, and output the encoded block to the zip file.
  1077. */
  1078. var _tr_flush_block$1 = function _tr_flush_block(s, buf, stored_len, last) //DeflateState *s;
  1079. //charf *buf; /* input block, or NULL if too old */
  1080. //ulg stored_len; /* length of input block */
  1081. //int last; /* one if this is the last block for a file */
  1082. {
  1083. var opt_lenb, static_lenb;
  1084. /* opt_len and static_len in bytes */
  1085. var max_blindex = 0;
  1086. /* index of last bit length code of non zero freq */
  1087. /* Build the Huffman trees unless a stored block is forced */
  1088. if (s.level > 0) {
  1089. /* Check if the file is binary or text */
  1090. if (s.strm.data_type === Z_UNKNOWN$1) {
  1091. s.strm.data_type = detect_data_type(s);
  1092. }
  1093. /* Construct the literal and distance trees */
  1094. build_tree(s, s.l_desc); // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  1095. // s->static_len));
  1096. build_tree(s, s.d_desc); // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  1097. // s->static_len));
  1098. /* At this point, opt_len and static_len are the total bit lengths of
  1099. * the compressed block data, excluding the tree representations.
  1100. */
  1101. /* Build the bit length tree for the above two trees, and get the index
  1102. * in bl_order of the last bit length code to send.
  1103. */
  1104. max_blindex = build_bl_tree(s);
  1105. /* Determine the best encoding. Compute the block lengths in bytes. */
  1106. opt_lenb = s.opt_len + 3 + 7 >>> 3;
  1107. static_lenb = s.static_len + 3 + 7 >>> 3; // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  1108. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  1109. // s->last_lit));
  1110. if (static_lenb <= opt_lenb) {
  1111. opt_lenb = static_lenb;
  1112. }
  1113. } else {
  1114. // Assert(buf != (char*)0, "lost buf");
  1115. opt_lenb = static_lenb = stored_len + 5;
  1116. /* force a stored block */
  1117. }
  1118. if (stored_len + 4 <= opt_lenb && buf !== -1) {
  1119. /* 4: two words for the lengths */
  1120. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1121. * Otherwise we can't have processed more than WSIZE input bytes since
  1122. * the last block flush, because compression would have been
  1123. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1124. * transform a block into a stored block.
  1125. */
  1126. _tr_stored_block$1(s, buf, stored_len, last);
  1127. } else if (s.strategy === Z_FIXED$1 || static_lenb === opt_lenb) {
  1128. send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
  1129. compress_block(s, static_ltree, static_dtree);
  1130. } else {
  1131. send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
  1132. send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
  1133. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  1134. } // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  1135. /* The above check is made mod 2^32, for files larger than 512 MB
  1136. * and uLong implemented on 32 bits.
  1137. */
  1138. init_block(s);
  1139. if (last) {
  1140. bi_windup(s);
  1141. } // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  1142. // s->compressed_len-7*last));
  1143. };
  1144. /* ===========================================================================
  1145. * Save the match info and tally the frequency counts. Return true if
  1146. * the current block must be flushed.
  1147. */
  1148. var _tr_tally$1 = function _tr_tally(s, dist, lc) // deflate_state *s;
  1149. // unsigned dist; /* distance of matched string */
  1150. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  1151. {
  1152. //let out_length, in_length, dcode;
  1153. s.pending_buf[s.d_buf + s.last_lit * 2] = dist >>> 8 & 0xff;
  1154. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  1155. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  1156. s.last_lit++;
  1157. if (dist === 0) {
  1158. /* lc is the unmatched char */
  1159. s.dyn_ltree[lc * 2] /*.Freq*/++;
  1160. } else {
  1161. s.matches++;
  1162. /* Here, lc is the match length - MIN_MATCH */
  1163. dist--;
  1164. /* dist = match distance - 1 */
  1165. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  1166. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  1167. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  1168. s.dyn_ltree[(_length_code[lc] + LITERALS$1 + 1) * 2] /*.Freq*/++;
  1169. s.dyn_dtree[d_code(dist) * 2] /*.Freq*/++;
  1170. } // (!) This block is disabled in zlib defaults,
  1171. // don't enable it for binary compatibility
  1172. //#ifdef TRUNCATE_BLOCK
  1173. // /* Try to guess if it is profitable to stop the current block here */
  1174. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  1175. // /* Compute an upper bound for the compressed length */
  1176. // out_length = s.last_lit*8;
  1177. // in_length = s.strstart - s.block_start;
  1178. //
  1179. // for (dcode = 0; dcode < D_CODES; dcode++) {
  1180. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  1181. // }
  1182. // out_length >>>= 3;
  1183. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  1184. // // s->last_lit, in_length, out_length,
  1185. // // 100L - out_length*100L/in_length));
  1186. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  1187. // return true;
  1188. // }
  1189. // }
  1190. //#endif
  1191. return s.last_lit === s.lit_bufsize - 1;
  1192. /* We avoid equality with lit_bufsize because of wraparound at 64K
  1193. * on 16 bit machines and because stored blocks are restricted to
  1194. * 64K-1 bytes.
  1195. */
  1196. };
  1197. var _tr_init_1 = _tr_init$1;
  1198. var _tr_stored_block_1 = _tr_stored_block$1;
  1199. var _tr_flush_block_1 = _tr_flush_block$1;
  1200. var _tr_tally_1 = _tr_tally$1;
  1201. var _tr_align_1 = _tr_align$1;
  1202. var trees = {
  1203. _tr_init: _tr_init_1,
  1204. _tr_stored_block: _tr_stored_block_1,
  1205. _tr_flush_block: _tr_flush_block_1,
  1206. _tr_tally: _tr_tally_1,
  1207. _tr_align: _tr_align_1
  1208. };
  1209. // It isn't worth it to make additional optimizations as in original.
  1210. // Small size is preferable.
  1211. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1212. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1213. //
  1214. // This software is provided 'as-is', without any express or implied
  1215. // warranty. In no event will the authors be held liable for any damages
  1216. // arising from the use of this software.
  1217. //
  1218. // Permission is granted to anyone to use this software for any purpose,
  1219. // including commercial applications, and to alter it and redistribute it
  1220. // freely, subject to the following restrictions:
  1221. //
  1222. // 1. The origin of this software must not be misrepresented; you must not
  1223. // claim that you wrote the original software. If you use this software
  1224. // in a product, an acknowledgment in the product documentation would be
  1225. // appreciated but is not required.
  1226. // 2. Altered source versions must be plainly marked as such, and must not be
  1227. // misrepresented as being the original software.
  1228. // 3. This notice may not be removed or altered from any source distribution.
  1229. var adler32 = function adler32(adler, buf, len, pos) {
  1230. var s1 = adler & 0xffff | 0,
  1231. s2 = adler >>> 16 & 0xffff | 0,
  1232. n = 0;
  1233. while (len !== 0) {
  1234. // Set limit ~ twice less than 5552, to keep
  1235. // s2 in 31-bits, because we force signed ints.
  1236. // in other case %= will fail.
  1237. n = len > 2000 ? 2000 : len;
  1238. len -= n;
  1239. do {
  1240. s1 = s1 + buf[pos++] | 0;
  1241. s2 = s2 + s1 | 0;
  1242. } while (--n);
  1243. s1 %= 65521;
  1244. s2 %= 65521;
  1245. }
  1246. return s1 | s2 << 16 | 0;
  1247. };
  1248. var adler32_1 = adler32;
  1249. // So write code to minimize size - no pregenerated tables
  1250. // and array tools dependencies.
  1251. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1252. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1253. //
  1254. // This software is provided 'as-is', without any express or implied
  1255. // warranty. In no event will the authors be held liable for any damages
  1256. // arising from the use of this software.
  1257. //
  1258. // Permission is granted to anyone to use this software for any purpose,
  1259. // including commercial applications, and to alter it and redistribute it
  1260. // freely, subject to the following restrictions:
  1261. //
  1262. // 1. The origin of this software must not be misrepresented; you must not
  1263. // claim that you wrote the original software. If you use this software
  1264. // in a product, an acknowledgment in the product documentation would be
  1265. // appreciated but is not required.
  1266. // 2. Altered source versions must be plainly marked as such, and must not be
  1267. // misrepresented as being the original software.
  1268. // 3. This notice may not be removed or altered from any source distribution.
  1269. // Use ordinary array, since untyped makes no boost here
  1270. var makeTable = function makeTable() {
  1271. var c,
  1272. table = [];
  1273. for (var n = 0; n < 256; n++) {
  1274. c = n;
  1275. for (var k = 0; k < 8; k++) {
  1276. c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1;
  1277. }
  1278. table[n] = c;
  1279. }
  1280. return table;
  1281. }; // Create table on load. Just 255 signed longs. Not a problem.
  1282. var crcTable = new Uint32Array(makeTable());
  1283. var crc32 = function crc32(crc, buf, len, pos) {
  1284. var t = crcTable;
  1285. var end = pos + len;
  1286. crc ^= -1;
  1287. for (var i = pos; i < end; i++) {
  1288. crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 0xFF];
  1289. }
  1290. return crc ^ -1; // >>> 0;
  1291. };
  1292. var crc32_1 = crc32;
  1293. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1294. //
  1295. // This software is provided 'as-is', without any express or implied
  1296. // warranty. In no event will the authors be held liable for any damages
  1297. // arising from the use of this software.
  1298. //
  1299. // Permission is granted to anyone to use this software for any purpose,
  1300. // including commercial applications, and to alter it and redistribute it
  1301. // freely, subject to the following restrictions:
  1302. //
  1303. // 1. The origin of this software must not be misrepresented; you must not
  1304. // claim that you wrote the original software. If you use this software
  1305. // in a product, an acknowledgment in the product documentation would be
  1306. // appreciated but is not required.
  1307. // 2. Altered source versions must be plainly marked as such, and must not be
  1308. // misrepresented as being the original software.
  1309. // 3. This notice may not be removed or altered from any source distribution.
  1310. var messages = {
  1311. 2: 'need dictionary',
  1312. /* Z_NEED_DICT 2 */
  1313. 1: 'stream end',
  1314. /* Z_STREAM_END 1 */
  1315. 0: '',
  1316. /* Z_OK 0 */
  1317. '-1': 'file error',
  1318. /* Z_ERRNO (-1) */
  1319. '-2': 'stream error',
  1320. /* Z_STREAM_ERROR (-2) */
  1321. '-3': 'data error',
  1322. /* Z_DATA_ERROR (-3) */
  1323. '-4': 'insufficient memory',
  1324. /* Z_MEM_ERROR (-4) */
  1325. '-5': 'buffer error',
  1326. /* Z_BUF_ERROR (-5) */
  1327. '-6': 'incompatible version'
  1328. /* Z_VERSION_ERROR (-6) */
  1329. };
  1330. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1331. //
  1332. // This software is provided 'as-is', without any express or implied
  1333. // warranty. In no event will the authors be held liable for any damages
  1334. // arising from the use of this software.
  1335. //
  1336. // Permission is granted to anyone to use this software for any purpose,
  1337. // including commercial applications, and to alter it and redistribute it
  1338. // freely, subject to the following restrictions:
  1339. //
  1340. // 1. The origin of this software must not be misrepresented; you must not
  1341. // claim that you wrote the original software. If you use this software
  1342. // in a product, an acknowledgment in the product documentation would be
  1343. // appreciated but is not required.
  1344. // 2. Altered source versions must be plainly marked as such, and must not be
  1345. // misrepresented as being the original software.
  1346. // 3. This notice may not be removed or altered from any source distribution.
  1347. var constants$1 = {
  1348. /* Allowed flush values; see deflate() and inflate() below for details */
  1349. Z_NO_FLUSH: 0,
  1350. Z_PARTIAL_FLUSH: 1,
  1351. Z_SYNC_FLUSH: 2,
  1352. Z_FULL_FLUSH: 3,
  1353. Z_FINISH: 4,
  1354. Z_BLOCK: 5,
  1355. Z_TREES: 6,
  1356. /* Return codes for the compression/decompression functions. Negative values
  1357. * are errors, positive values are used for special but normal events.
  1358. */
  1359. Z_OK: 0,
  1360. Z_STREAM_END: 1,
  1361. Z_NEED_DICT: 2,
  1362. Z_ERRNO: -1,
  1363. Z_STREAM_ERROR: -2,
  1364. Z_DATA_ERROR: -3,
  1365. Z_MEM_ERROR: -4,
  1366. Z_BUF_ERROR: -5,
  1367. //Z_VERSION_ERROR: -6,
  1368. /* compression levels */
  1369. Z_NO_COMPRESSION: 0,
  1370. Z_BEST_SPEED: 1,
  1371. Z_BEST_COMPRESSION: 9,
  1372. Z_DEFAULT_COMPRESSION: -1,
  1373. Z_FILTERED: 1,
  1374. Z_HUFFMAN_ONLY: 2,
  1375. Z_RLE: 3,
  1376. Z_FIXED: 4,
  1377. Z_DEFAULT_STRATEGY: 0,
  1378. /* Possible values of the data_type field (though see inflate()) */
  1379. Z_BINARY: 0,
  1380. Z_TEXT: 1,
  1381. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  1382. Z_UNKNOWN: 2,
  1383. /* The deflate compression method */
  1384. Z_DEFLATED: 8 //Z_NULL: null // Use -1 or null inline, depending on var type
  1385. };
  1386. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1387. //
  1388. // This software is provided 'as-is', without any express or implied
  1389. // warranty. In no event will the authors be held liable for any damages
  1390. // arising from the use of this software.
  1391. //
  1392. // Permission is granted to anyone to use this software for any purpose,
  1393. // including commercial applications, and to alter it and redistribute it
  1394. // freely, subject to the following restrictions:
  1395. //
  1396. // 1. The origin of this software must not be misrepresented; you must not
  1397. // claim that you wrote the original software. If you use this software
  1398. // in a product, an acknowledgment in the product documentation would be
  1399. // appreciated but is not required.
  1400. // 2. Altered source versions must be plainly marked as such, and must not be
  1401. // misrepresented as being the original software.
  1402. // 3. This notice may not be removed or altered from any source distribution.
  1403. var _tr_init = trees._tr_init,
  1404. _tr_stored_block = trees._tr_stored_block,
  1405. _tr_flush_block = trees._tr_flush_block,
  1406. _tr_tally = trees._tr_tally,
  1407. _tr_align = trees._tr_align;
  1408. /* Public constants ==========================================================*/
  1409. /* ===========================================================================*/
  1410. var Z_NO_FLUSH$1 = constants$1.Z_NO_FLUSH,
  1411. Z_PARTIAL_FLUSH = constants$1.Z_PARTIAL_FLUSH,
  1412. Z_FULL_FLUSH$1 = constants$1.Z_FULL_FLUSH,
  1413. Z_FINISH$1 = constants$1.Z_FINISH,
  1414. Z_BLOCK = constants$1.Z_BLOCK,
  1415. Z_OK$1 = constants$1.Z_OK,
  1416. Z_STREAM_END$1 = constants$1.Z_STREAM_END,
  1417. Z_STREAM_ERROR = constants$1.Z_STREAM_ERROR,
  1418. Z_DATA_ERROR = constants$1.Z_DATA_ERROR,
  1419. Z_BUF_ERROR = constants$1.Z_BUF_ERROR,
  1420. Z_DEFAULT_COMPRESSION$1 = constants$1.Z_DEFAULT_COMPRESSION,
  1421. Z_FILTERED = constants$1.Z_FILTERED,
  1422. Z_HUFFMAN_ONLY = constants$1.Z_HUFFMAN_ONLY,
  1423. Z_RLE = constants$1.Z_RLE,
  1424. Z_FIXED = constants$1.Z_FIXED,
  1425. Z_DEFAULT_STRATEGY$1 = constants$1.Z_DEFAULT_STRATEGY,
  1426. Z_UNKNOWN = constants$1.Z_UNKNOWN,
  1427. Z_DEFLATED$1 = constants$1.Z_DEFLATED;
  1428. /*============================================================================*/
  1429. var MAX_MEM_LEVEL = 9;
  1430. /* Maximum value for memLevel in deflateInit2 */
  1431. var MAX_WBITS = 15;
  1432. /* 32K LZ77 window */
  1433. var DEF_MEM_LEVEL = 8;
  1434. var LENGTH_CODES = 29;
  1435. /* number of length codes, not counting the special END_BLOCK code */
  1436. var LITERALS = 256;
  1437. /* number of literal bytes 0..255 */
  1438. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  1439. /* number of Literal or Length codes, including the END_BLOCK code */
  1440. var D_CODES = 30;
  1441. /* number of distance codes */
  1442. var BL_CODES = 19;
  1443. /* number of codes used to transfer the bit lengths */
  1444. var HEAP_SIZE = 2 * L_CODES + 1;
  1445. /* maximum heap size */
  1446. var MAX_BITS = 15;
  1447. /* All codes must not exceed MAX_BITS bits */
  1448. var MIN_MATCH = 3;
  1449. var MAX_MATCH = 258;
  1450. var MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;
  1451. var PRESET_DICT = 0x20;
  1452. var INIT_STATE = 42;
  1453. var EXTRA_STATE = 69;
  1454. var NAME_STATE = 73;
  1455. var COMMENT_STATE = 91;
  1456. var HCRC_STATE = 103;
  1457. var BUSY_STATE = 113;
  1458. var FINISH_STATE = 666;
  1459. var BS_NEED_MORE = 1;
  1460. /* block not completed, need more input or more output */
  1461. var BS_BLOCK_DONE = 2;
  1462. /* block flush performed */
  1463. var BS_FINISH_STARTED = 3;
  1464. /* finish started, need only more output at next deflate */
  1465. var BS_FINISH_DONE = 4;
  1466. /* finish done, accept no more input or output */
  1467. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  1468. var err = function err(strm, errorCode) {
  1469. strm.msg = messages[errorCode];
  1470. return errorCode;
  1471. };
  1472. var rank = function rank(f) {
  1473. return (f << 1) - (f > 4 ? 9 : 0);
  1474. };
  1475. var zero = function zero(buf) {
  1476. var len = buf.length;
  1477. while (--len >= 0) {
  1478. buf[len] = 0;
  1479. }
  1480. };
  1481. /* eslint-disable new-cap */
  1482. var HASH_ZLIB = function HASH_ZLIB(s, prev, data) {
  1483. return (prev << s.hash_shift ^ data) & s.hash_mask;
  1484. }; // This hash causes less collisions, https://github.com/nodeca/pako/issues/135
  1485. // But breaks binary compatibility
  1486. //let HASH_FAST = (s, prev, data) => ((prev << 8) + (prev >> 8) + (data << 4)) & s.hash_mask;
  1487. var HASH = HASH_ZLIB;
  1488. /* =========================================================================
  1489. * Flush as much pending output as possible. All deflate() output goes
  1490. * through this function so some applications may wish to modify it
  1491. * to avoid allocating a large strm->output buffer and copying into it.
  1492. * (See also read_buf()).
  1493. */
  1494. var flush_pending = function flush_pending(strm) {
  1495. var s = strm.state; //_tr_flush_bits(s);
  1496. var len = s.pending;
  1497. if (len > strm.avail_out) {
  1498. len = strm.avail_out;
  1499. }
  1500. if (len === 0) {
  1501. return;
  1502. }
  1503. strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out);
  1504. strm.next_out += len;
  1505. s.pending_out += len;
  1506. strm.total_out += len;
  1507. strm.avail_out -= len;
  1508. s.pending -= len;
  1509. if (s.pending === 0) {
  1510. s.pending_out = 0;
  1511. }
  1512. };
  1513. var flush_block_only = function flush_block_only(s, last) {
  1514. _tr_flush_block(s, s.block_start >= 0 ? s.block_start : -1, s.strstart - s.block_start, last);
  1515. s.block_start = s.strstart;
  1516. flush_pending(s.strm);
  1517. };
  1518. var put_byte = function put_byte(s, b) {
  1519. s.pending_buf[s.pending++] = b;
  1520. };
  1521. /* =========================================================================
  1522. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  1523. * IN assertion: the stream state is correct and there is enough room in
  1524. * pending_buf.
  1525. */
  1526. var putShortMSB = function putShortMSB(s, b) {
  1527. // put_byte(s, (Byte)(b >> 8));
  1528. // put_byte(s, (Byte)(b & 0xff));
  1529. s.pending_buf[s.pending++] = b >>> 8 & 0xff;
  1530. s.pending_buf[s.pending++] = b & 0xff;
  1531. };
  1532. /* ===========================================================================
  1533. * Read a new buffer from the current input stream, update the adler32
  1534. * and total number of bytes read. All deflate() input goes through
  1535. * this function so some applications may wish to modify it to avoid
  1536. * allocating a large strm->input buffer and copying from it.
  1537. * (See also flush_pending()).
  1538. */
  1539. var read_buf = function read_buf(strm, buf, start, size) {
  1540. var len = strm.avail_in;
  1541. if (len > size) {
  1542. len = size;
  1543. }
  1544. if (len === 0) {
  1545. return 0;
  1546. }
  1547. strm.avail_in -= len; // zmemcpy(buf, strm->next_in, len);
  1548. buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start);
  1549. if (strm.state.wrap === 1) {
  1550. strm.adler = adler32_1(strm.adler, buf, len, start);
  1551. } else if (strm.state.wrap === 2) {
  1552. strm.adler = crc32_1(strm.adler, buf, len, start);
  1553. }
  1554. strm.next_in += len;
  1555. strm.total_in += len;
  1556. return len;
  1557. };
  1558. /* ===========================================================================
  1559. * Set match_start to the longest match starting at the given string and
  1560. * return its length. Matches shorter or equal to prev_length are discarded,
  1561. * in which case the result is equal to prev_length and match_start is
  1562. * garbage.
  1563. * IN assertions: cur_match is the head of the hash chain for the current
  1564. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1565. * OUT assertion: the match length is not greater than s->lookahead.
  1566. */
  1567. var longest_match = function longest_match(s, cur_match) {
  1568. var chain_length = s.max_chain_length;
  1569. /* max hash chain length */
  1570. var scan = s.strstart;
  1571. /* current string */
  1572. var match;
  1573. /* matched string */
  1574. var len;
  1575. /* length of current match */
  1576. var best_len = s.prev_length;
  1577. /* best match length so far */
  1578. var nice_match = s.nice_match;
  1579. /* stop if match long enough */
  1580. var limit = s.strstart > s.w_size - MIN_LOOKAHEAD ? s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0
  1581. /*NIL*/
  1582. ;
  1583. var _win = s.window; // shortcut
  1584. var wmask = s.w_mask;
  1585. var prev = s.prev;
  1586. /* Stop when cur_match becomes <= limit. To simplify the code,
  1587. * we prevent matches with the string of window index 0.
  1588. */
  1589. var strend = s.strstart + MAX_MATCH;
  1590. var scan_end1 = _win[scan + best_len - 1];
  1591. var scan_end = _win[scan + best_len];
  1592. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1593. * It is easy to get rid of this optimization if necessary.
  1594. */
  1595. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1596. /* Do not waste too much time if we already have a good match: */
  1597. if (s.prev_length >= s.good_match) {
  1598. chain_length >>= 2;
  1599. }
  1600. /* Do not look for matches beyond the end of the input. This is necessary
  1601. * to make deflate deterministic.
  1602. */
  1603. if (nice_match > s.lookahead) {
  1604. nice_match = s.lookahead;
  1605. } // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  1606. do {
  1607. // Assert(cur_match < s->strstart, "no future");
  1608. match = cur_match;
  1609. /* Skip to next match if the match length cannot increase
  1610. * or if the match length is less than 2. Note that the checks below
  1611. * for insufficient lookahead only occur occasionally for performance
  1612. * reasons. Therefore uninitialized memory will be accessed, and
  1613. * conditional jumps will be made that depend on those values.
  1614. * However the length of the match is limited to the lookahead, so
  1615. * the output of deflate is not affected by the uninitialized values.
  1616. */
  1617. if (_win[match + best_len] !== scan_end || _win[match + best_len - 1] !== scan_end1 || _win[match] !== _win[scan] || _win[++match] !== _win[scan + 1]) {
  1618. continue;
  1619. }
  1620. /* The check at best_len-1 can be removed because it will be made
  1621. * again later. (This heuristic is not always a win.)
  1622. * It is not necessary to compare scan[2] and match[2] since they
  1623. * are always equal when the other bytes match, given that
  1624. * the hash keys are equal and that HASH_BITS >= 8.
  1625. */
  1626. scan += 2;
  1627. match++; // Assert(*scan == *match, "match[2]?");
  1628. /* We check for insufficient lookahead only every 8th comparison;
  1629. * the 256th check will be made at strstart+258.
  1630. */
  1631. do {
  1632. /*jshint noempty:false*/
  1633. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend); // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1634. len = MAX_MATCH - (strend - scan);
  1635. scan = strend - MAX_MATCH;
  1636. if (len > best_len) {
  1637. s.match_start = cur_match;
  1638. best_len = len;
  1639. if (len >= nice_match) {
  1640. break;
  1641. }
  1642. scan_end1 = _win[scan + best_len - 1];
  1643. scan_end = _win[scan + best_len];
  1644. }
  1645. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  1646. if (best_len <= s.lookahead) {
  1647. return best_len;
  1648. }
  1649. return s.lookahead;
  1650. };
  1651. /* ===========================================================================
  1652. * Fill the window when the lookahead becomes insufficient.
  1653. * Updates strstart and lookahead.
  1654. *
  1655. * IN assertion: lookahead < MIN_LOOKAHEAD
  1656. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1657. * At least one byte has been read, or avail_in == 0; reads are
  1658. * performed for at least two bytes (required for the zip translate_eol
  1659. * option -- not supported here).
  1660. */
  1661. var fill_window = function fill_window(s) {
  1662. var _w_size = s.w_size;
  1663. var p, n, m, more, str; //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  1664. do {
  1665. more = s.window_size - s.lookahead - s.strstart; // JS ints have 32 bit, block below not needed
  1666. /* Deal with !@#$% 64K limit: */
  1667. //if (sizeof(int) <= 2) {
  1668. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1669. // more = wsize;
  1670. //
  1671. // } else if (more == (unsigned)(-1)) {
  1672. // /* Very unlikely, but possible on 16 bit machine if
  1673. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  1674. // */
  1675. // more--;
  1676. // }
  1677. //}
  1678. /* If the window is almost full and there is insufficient lookahead,
  1679. * move the upper half to the lower one to make room in the upper half.
  1680. */
  1681. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  1682. s.window.set(s.window.subarray(_w_size, _w_size + _w_size), 0);
  1683. s.match_start -= _w_size;
  1684. s.strstart -= _w_size;
  1685. /* we now have strstart >= MAX_DIST */
  1686. s.block_start -= _w_size;
  1687. /* Slide the hash table (could be avoided with 32 bit values
  1688. at the expense of memory usage). We slide even when level == 0
  1689. to keep the hash table consistent if we switch back to level > 0
  1690. later. (Using level 0 permanently is not an optimal usage of
  1691. zlib, so we don't care about this pathological case.)
  1692. */
  1693. n = s.hash_size;
  1694. p = n;
  1695. do {
  1696. m = s.head[--p];
  1697. s.head[p] = m >= _w_size ? m - _w_size : 0;
  1698. } while (--n);
  1699. n = _w_size;
  1700. p = n;
  1701. do {
  1702. m = s.prev[--p];
  1703. s.prev[p] = m >= _w_size ? m - _w_size : 0;
  1704. /* If n is not on any hash chain, prev[n] is garbage but
  1705. * its value will never be used.
  1706. */
  1707. } while (--n);
  1708. more += _w_size;
  1709. }
  1710. if (s.strm.avail_in === 0) {
  1711. break;
  1712. }
  1713. /* If there was no sliding:
  1714. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1715. * more == window_size - lookahead - strstart
  1716. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1717. * => more >= window_size - 2*WSIZE + 2
  1718. * In the BIG_MEM or MMAP case (not yet supported),
  1719. * window_size == input_size + MIN_LOOKAHEAD &&
  1720. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1721. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1722. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1723. */
  1724. //Assert(more >= 2, "more < 2");
  1725. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  1726. s.lookahead += n;
  1727. /* Initialize the hash value now that we have some input: */
  1728. if (s.lookahead + s.insert >= MIN_MATCH) {
  1729. str = s.strstart - s.insert;
  1730. s.ins_h = s.window[str];
  1731. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  1732. s.ins_h = HASH(s, s.ins_h, s.window[str + 1]); //#if MIN_MATCH != 3
  1733. // Call update_hash() MIN_MATCH-3 more times
  1734. //#endif
  1735. while (s.insert) {
  1736. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  1737. s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
  1738. s.prev[str & s.w_mask] = s.head[s.ins_h];
  1739. s.head[s.ins_h] = str;
  1740. str++;
  1741. s.insert--;
  1742. if (s.lookahead + s.insert < MIN_MATCH) {
  1743. break;
  1744. }
  1745. }
  1746. }
  1747. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1748. * but this is not important since only literal bytes will be emitted.
  1749. */
  1750. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  1751. /* If the WIN_INIT bytes after the end of the current data have never been
  1752. * written, then zero those bytes in order to avoid memory check reports of
  1753. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  1754. * the longest match routines. Update the high water mark for the next
  1755. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  1756. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  1757. */
  1758. // if (s.high_water < s.window_size) {
  1759. // const curr = s.strstart + s.lookahead;
  1760. // let init = 0;
  1761. //
  1762. // if (s.high_water < curr) {
  1763. // /* Previous high water mark below current data -- zero WIN_INIT
  1764. // * bytes or up to end of window, whichever is less.
  1765. // */
  1766. // init = s.window_size - curr;
  1767. // if (init > WIN_INIT)
  1768. // init = WIN_INIT;
  1769. // zmemzero(s->window + curr, (unsigned)init);
  1770. // s->high_water = curr + init;
  1771. // }
  1772. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  1773. // /* High water mark at or above current data, but below current data
  1774. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  1775. // * to end of window, whichever is less.
  1776. // */
  1777. // init = (ulg)curr + WIN_INIT - s->high_water;
  1778. // if (init > s->window_size - s->high_water)
  1779. // init = s->window_size - s->high_water;
  1780. // zmemzero(s->window + s->high_water, (unsigned)init);
  1781. // s->high_water += init;
  1782. // }
  1783. // }
  1784. //
  1785. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1786. // "not enough room for search");
  1787. };
  1788. /* ===========================================================================
  1789. * Copy without compression as much as possible from the input stream, return
  1790. * the current block state.
  1791. * This function does not insert new strings in the dictionary since
  1792. * uncompressible data is probably not useful. This function is used
  1793. * only for the level=0 compression option.
  1794. * NOTE: this function should be optimized to avoid extra copying from
  1795. * window to pending_buf.
  1796. */
  1797. var deflate_stored = function deflate_stored(s, flush) {
  1798. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1799. * to pending_buf_size, and each stored block has a 5 byte header:
  1800. */
  1801. var max_block_size = 0xffff;
  1802. if (max_block_size > s.pending_buf_size - 5) {
  1803. max_block_size = s.pending_buf_size - 5;
  1804. }
  1805. /* Copy as much as possible from input to output: */
  1806. for (;;) {
  1807. /* Fill the window as much as possible: */
  1808. if (s.lookahead <= 1) {
  1809. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1810. // s->block_start >= (long)s->w_size, "slide too late");
  1811. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  1812. // s.block_start >= s.w_size)) {
  1813. // throw new Error("slide too late");
  1814. // }
  1815. fill_window(s);
  1816. if (s.lookahead === 0 && flush === Z_NO_FLUSH$1) {
  1817. return BS_NEED_MORE;
  1818. }
  1819. if (s.lookahead === 0) {
  1820. break;
  1821. }
  1822. /* flush the current block */
  1823. } //Assert(s->block_start >= 0L, "block gone");
  1824. // if (s.block_start < 0) throw new Error("block gone");
  1825. s.strstart += s.lookahead;
  1826. s.lookahead = 0;
  1827. /* Emit a stored block if pending_buf will be full: */
  1828. var max_start = s.block_start + max_block_size;
  1829. if (s.strstart === 0 || s.strstart >= max_start) {
  1830. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1831. s.lookahead = s.strstart - max_start;
  1832. s.strstart = max_start;
  1833. /*** FLUSH_BLOCK(s, 0); ***/
  1834. flush_block_only(s, false);
  1835. if (s.strm.avail_out === 0) {
  1836. return BS_NEED_MORE;
  1837. }
  1838. /***/
  1839. }
  1840. /* Flush if we may have to slide, otherwise block_start may become
  1841. * negative and the data will be gone:
  1842. */
  1843. if (s.strstart - s.block_start >= s.w_size - MIN_LOOKAHEAD) {
  1844. /*** FLUSH_BLOCK(s, 0); ***/
  1845. flush_block_only(s, false);
  1846. if (s.strm.avail_out === 0) {
  1847. return BS_NEED_MORE;
  1848. }
  1849. /***/
  1850. }
  1851. }
  1852. s.insert = 0;
  1853. if (flush === Z_FINISH$1) {
  1854. /*** FLUSH_BLOCK(s, 1); ***/
  1855. flush_block_only(s, true);
  1856. if (s.strm.avail_out === 0) {
  1857. return BS_FINISH_STARTED;
  1858. }
  1859. /***/
  1860. return BS_FINISH_DONE;
  1861. }
  1862. if (s.strstart > s.block_start) {
  1863. /*** FLUSH_BLOCK(s, 0); ***/
  1864. flush_block_only(s, false);
  1865. if (s.strm.avail_out === 0) {
  1866. return BS_NEED_MORE;
  1867. }
  1868. /***/
  1869. }
  1870. return BS_NEED_MORE;
  1871. };
  1872. /* ===========================================================================
  1873. * Compress as much as possible from the input stream, return the current
  1874. * block state.
  1875. * This function does not perform lazy evaluation of matches and inserts
  1876. * new strings in the dictionary only for unmatched strings or for short
  1877. * matches. It is used only for the fast compression options.
  1878. */
  1879. var deflate_fast = function deflate_fast(s, flush) {
  1880. var hash_head;
  1881. /* head of the hash chain */
  1882. var bflush;
  1883. /* set if current block must be flushed */
  1884. for (;;) {
  1885. /* Make sure that we always have enough lookahead, except
  1886. * at the end of the input file. We need MAX_MATCH bytes
  1887. * for the next match, plus MIN_MATCH bytes to insert the
  1888. * string following the next match.
  1889. */
  1890. if (s.lookahead < MIN_LOOKAHEAD) {
  1891. fill_window(s);
  1892. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$1) {
  1893. return BS_NEED_MORE;
  1894. }
  1895. if (s.lookahead === 0) {
  1896. break;
  1897. /* flush the current block */
  1898. }
  1899. }
  1900. /* Insert the string window[strstart .. strstart+2] in the
  1901. * dictionary, and set hash_head to the head of the hash chain:
  1902. */
  1903. hash_head = 0
  1904. /*NIL*/
  1905. ;
  1906. if (s.lookahead >= MIN_MATCH) {
  1907. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1908. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  1909. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1910. s.head[s.ins_h] = s.strstart;
  1911. /***/
  1912. }
  1913. /* Find the longest match, discarding those <= prev_length.
  1914. * At this point we have always match_length < MIN_MATCH
  1915. */
  1916. if (hash_head !== 0
  1917. /*NIL*/
  1918. && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) {
  1919. /* To simplify the code, we prevent matches with the string
  1920. * of window index 0 (in particular we have to avoid a match
  1921. * of the string with itself at the start of the input file).
  1922. */
  1923. s.match_length = longest_match(s, hash_head);
  1924. /* longest_match() sets match_start */
  1925. }
  1926. if (s.match_length >= MIN_MATCH) {
  1927. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  1928. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  1929. s.match_length - MIN_MATCH, bflush); ***/
  1930. bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  1931. s.lookahead -= s.match_length;
  1932. /* Insert new strings in the hash table only if the match length
  1933. * is not too large. This saves time but degrades compression.
  1934. */
  1935. if (s.match_length <= s.max_lazy_match
  1936. /*max_insert_length*/
  1937. && s.lookahead >= MIN_MATCH) {
  1938. s.match_length--;
  1939. /* string at strstart already in table */
  1940. do {
  1941. s.strstart++;
  1942. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1943. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  1944. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1945. s.head[s.ins_h] = s.strstart;
  1946. /***/
  1947. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1948. * always MIN_MATCH bytes ahead.
  1949. */
  1950. } while (--s.match_length !== 0);
  1951. s.strstart++;
  1952. } else {
  1953. s.strstart += s.match_length;
  1954. s.match_length = 0;
  1955. s.ins_h = s.window[s.strstart];
  1956. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  1957. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]); //#if MIN_MATCH != 3
  1958. // Call UPDATE_HASH() MIN_MATCH-3 more times
  1959. //#endif
  1960. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1961. * matter since it will be recomputed at next deflate call.
  1962. */
  1963. }
  1964. } else {
  1965. /* No match, output a literal byte */
  1966. //Tracevv((stderr,"%c", s.window[s.strstart]));
  1967. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1968. bflush = _tr_tally(s, 0, s.window[s.strstart]);
  1969. s.lookahead--;
  1970. s.strstart++;
  1971. }
  1972. if (bflush) {
  1973. /*** FLUSH_BLOCK(s, 0); ***/
  1974. flush_block_only(s, false);
  1975. if (s.strm.avail_out === 0) {
  1976. return BS_NEED_MORE;
  1977. }
  1978. /***/
  1979. }
  1980. }
  1981. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  1982. if (flush === Z_FINISH$1) {
  1983. /*** FLUSH_BLOCK(s, 1); ***/
  1984. flush_block_only(s, true);
  1985. if (s.strm.avail_out === 0) {
  1986. return BS_FINISH_STARTED;
  1987. }
  1988. /***/
  1989. return BS_FINISH_DONE;
  1990. }
  1991. if (s.last_lit) {
  1992. /*** FLUSH_BLOCK(s, 0); ***/
  1993. flush_block_only(s, false);
  1994. if (s.strm.avail_out === 0) {
  1995. return BS_NEED_MORE;
  1996. }
  1997. /***/
  1998. }
  1999. return BS_BLOCK_DONE;
  2000. };
  2001. /* ===========================================================================
  2002. * Same as above, but achieves better compression. We use a lazy
  2003. * evaluation for matches: a match is finally adopted only if there is
  2004. * no better match at the next window position.
  2005. */
  2006. var deflate_slow = function deflate_slow(s, flush) {
  2007. var hash_head;
  2008. /* head of hash chain */
  2009. var bflush;
  2010. /* set if current block must be flushed */
  2011. var max_insert;
  2012. /* Process the input block. */
  2013. for (;;) {
  2014. /* Make sure that we always have enough lookahead, except
  2015. * at the end of the input file. We need MAX_MATCH bytes
  2016. * for the next match, plus MIN_MATCH bytes to insert the
  2017. * string following the next match.
  2018. */
  2019. if (s.lookahead < MIN_LOOKAHEAD) {
  2020. fill_window(s);
  2021. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$1) {
  2022. return BS_NEED_MORE;
  2023. }
  2024. if (s.lookahead === 0) {
  2025. break;
  2026. }
  2027. /* flush the current block */
  2028. }
  2029. /* Insert the string window[strstart .. strstart+2] in the
  2030. * dictionary, and set hash_head to the head of the hash chain:
  2031. */
  2032. hash_head = 0
  2033. /*NIL*/
  2034. ;
  2035. if (s.lookahead >= MIN_MATCH) {
  2036. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  2037. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  2038. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  2039. s.head[s.ins_h] = s.strstart;
  2040. /***/
  2041. }
  2042. /* Find the longest match, discarding those <= prev_length.
  2043. */
  2044. s.prev_length = s.match_length;
  2045. s.prev_match = s.match_start;
  2046. s.match_length = MIN_MATCH - 1;
  2047. if (hash_head !== 0
  2048. /*NIL*/
  2049. && s.prev_length < s.max_lazy_match && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD
  2050. /*MAX_DIST(s)*/
  2051. ) {
  2052. /* To simplify the code, we prevent matches with the string
  2053. * of window index 0 (in particular we have to avoid a match
  2054. * of the string with itself at the start of the input file).
  2055. */
  2056. s.match_length = longest_match(s, hash_head);
  2057. /* longest_match() sets match_start */
  2058. if (s.match_length <= 5 && (s.strategy === Z_FILTERED || s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096
  2059. /*TOO_FAR*/
  2060. )) {
  2061. /* If prev_match is also MIN_MATCH, match_start is garbage
  2062. * but we will ignore the current match anyway.
  2063. */
  2064. s.match_length = MIN_MATCH - 1;
  2065. }
  2066. }
  2067. /* If there was a match at the previous step and the current
  2068. * match is not better, output the previous match:
  2069. */
  2070. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  2071. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  2072. /* Do not insert strings in hash table beyond this. */
  2073. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  2074. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  2075. s.prev_length - MIN_MATCH, bflush);***/
  2076. bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
  2077. /* Insert in hash table all strings up to the end of the match.
  2078. * strstart-1 and strstart are already inserted. If there is not
  2079. * enough lookahead, the last two strings are not inserted in
  2080. * the hash table.
  2081. */
  2082. s.lookahead -= s.prev_length - 1;
  2083. s.prev_length -= 2;
  2084. do {
  2085. if (++s.strstart <= max_insert) {
  2086. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  2087. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  2088. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  2089. s.head[s.ins_h] = s.strstart;
  2090. /***/
  2091. }
  2092. } while (--s.prev_length !== 0);
  2093. s.match_available = 0;
  2094. s.match_length = MIN_MATCH - 1;
  2095. s.strstart++;
  2096. if (bflush) {
  2097. /*** FLUSH_BLOCK(s, 0); ***/
  2098. flush_block_only(s, false);
  2099. if (s.strm.avail_out === 0) {
  2100. return BS_NEED_MORE;
  2101. }
  2102. /***/
  2103. }
  2104. } else if (s.match_available) {
  2105. /* If there was no match at the previous position, output a
  2106. * single literal. If there was a match but the current match
  2107. * is longer, truncate the previous match to a single literal.
  2108. */
  2109. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  2110. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  2111. bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
  2112. if (bflush) {
  2113. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  2114. flush_block_only(s, false);
  2115. /***/
  2116. }
  2117. s.strstart++;
  2118. s.lookahead--;
  2119. if (s.strm.avail_out === 0) {
  2120. return BS_NEED_MORE;
  2121. }
  2122. } else {
  2123. /* There is no previous match to compare with, wait for
  2124. * the next step to decide.
  2125. */
  2126. s.match_available = 1;
  2127. s.strstart++;
  2128. s.lookahead--;
  2129. }
  2130. } //Assert (flush != Z_NO_FLUSH, "no flush?");
  2131. if (s.match_available) {
  2132. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  2133. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  2134. bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
  2135. s.match_available = 0;
  2136. }
  2137. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  2138. if (flush === Z_FINISH$1) {
  2139. /*** FLUSH_BLOCK(s, 1); ***/
  2140. flush_block_only(s, true);
  2141. if (s.strm.avail_out === 0) {
  2142. return BS_FINISH_STARTED;
  2143. }
  2144. /***/
  2145. return BS_FINISH_DONE;
  2146. }
  2147. if (s.last_lit) {
  2148. /*** FLUSH_BLOCK(s, 0); ***/
  2149. flush_block_only(s, false);
  2150. if (s.strm.avail_out === 0) {
  2151. return BS_NEED_MORE;
  2152. }
  2153. /***/
  2154. }
  2155. return BS_BLOCK_DONE;
  2156. };
  2157. /* ===========================================================================
  2158. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  2159. * one. Do not maintain a hash table. (It will be regenerated if this run of
  2160. * deflate switches away from Z_RLE.)
  2161. */
  2162. var deflate_rle = function deflate_rle(s, flush) {
  2163. var bflush;
  2164. /* set if current block must be flushed */
  2165. var prev;
  2166. /* byte at distance one to match */
  2167. var scan, strend;
  2168. /* scan goes up to strend for length of run */
  2169. var _win = s.window;
  2170. for (;;) {
  2171. /* Make sure that we always have enough lookahead, except
  2172. * at the end of the input file. We need MAX_MATCH bytes
  2173. * for the longest run, plus one for the unrolled loop.
  2174. */
  2175. if (s.lookahead <= MAX_MATCH) {
  2176. fill_window(s);
  2177. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH$1) {
  2178. return BS_NEED_MORE;
  2179. }
  2180. if (s.lookahead === 0) {
  2181. break;
  2182. }
  2183. /* flush the current block */
  2184. }
  2185. /* See how many times the previous byte repeats */
  2186. s.match_length = 0;
  2187. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  2188. scan = s.strstart - 1;
  2189. prev = _win[scan];
  2190. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  2191. strend = s.strstart + MAX_MATCH;
  2192. do {
  2193. /*jshint noempty:false*/
  2194. } while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend);
  2195. s.match_length = MAX_MATCH - (strend - scan);
  2196. if (s.match_length > s.lookahead) {
  2197. s.match_length = s.lookahead;
  2198. }
  2199. } //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  2200. }
  2201. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  2202. if (s.match_length >= MIN_MATCH) {
  2203. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  2204. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  2205. bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH);
  2206. s.lookahead -= s.match_length;
  2207. s.strstart += s.match_length;
  2208. s.match_length = 0;
  2209. } else {
  2210. /* No match, output a literal byte */
  2211. //Tracevv((stderr,"%c", s->window[s->strstart]));
  2212. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  2213. bflush = _tr_tally(s, 0, s.window[s.strstart]);
  2214. s.lookahead--;
  2215. s.strstart++;
  2216. }
  2217. if (bflush) {
  2218. /*** FLUSH_BLOCK(s, 0); ***/
  2219. flush_block_only(s, false);
  2220. if (s.strm.avail_out === 0) {
  2221. return BS_NEED_MORE;
  2222. }
  2223. /***/
  2224. }
  2225. }
  2226. s.insert = 0;
  2227. if (flush === Z_FINISH$1) {
  2228. /*** FLUSH_BLOCK(s, 1); ***/
  2229. flush_block_only(s, true);
  2230. if (s.strm.avail_out === 0) {
  2231. return BS_FINISH_STARTED;
  2232. }
  2233. /***/
  2234. return BS_FINISH_DONE;
  2235. }
  2236. if (s.last_lit) {
  2237. /*** FLUSH_BLOCK(s, 0); ***/
  2238. flush_block_only(s, false);
  2239. if (s.strm.avail_out === 0) {
  2240. return BS_NEED_MORE;
  2241. }
  2242. /***/
  2243. }
  2244. return BS_BLOCK_DONE;
  2245. };
  2246. /* ===========================================================================
  2247. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  2248. * (It will be regenerated if this run of deflate switches away from Huffman.)
  2249. */
  2250. var deflate_huff = function deflate_huff(s, flush) {
  2251. var bflush;
  2252. /* set if current block must be flushed */
  2253. for (;;) {
  2254. /* Make sure that we have a literal to write. */
  2255. if (s.lookahead === 0) {
  2256. fill_window(s);
  2257. if (s.lookahead === 0) {
  2258. if (flush === Z_NO_FLUSH$1) {
  2259. return BS_NEED_MORE;
  2260. }
  2261. break;
  2262. /* flush the current block */
  2263. }
  2264. }
  2265. /* Output a literal byte */
  2266. s.match_length = 0; //Tracevv((stderr,"%c", s->window[s->strstart]));
  2267. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  2268. bflush = _tr_tally(s, 0, s.window[s.strstart]);
  2269. s.lookahead--;
  2270. s.strstart++;
  2271. if (bflush) {
  2272. /*** FLUSH_BLOCK(s, 0); ***/
  2273. flush_block_only(s, false);
  2274. if (s.strm.avail_out === 0) {
  2275. return BS_NEED_MORE;
  2276. }
  2277. /***/
  2278. }
  2279. }
  2280. s.insert = 0;
  2281. if (flush === Z_FINISH$1) {
  2282. /*** FLUSH_BLOCK(s, 1); ***/
  2283. flush_block_only(s, true);
  2284. if (s.strm.avail_out === 0) {
  2285. return BS_FINISH_STARTED;
  2286. }
  2287. /***/
  2288. return BS_FINISH_DONE;
  2289. }
  2290. if (s.last_lit) {
  2291. /*** FLUSH_BLOCK(s, 0); ***/
  2292. flush_block_only(s, false);
  2293. if (s.strm.avail_out === 0) {
  2294. return BS_NEED_MORE;
  2295. }
  2296. /***/
  2297. }
  2298. return BS_BLOCK_DONE;
  2299. };
  2300. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  2301. * the desired pack level (0..9). The values given below have been tuned to
  2302. * exclude worst case performance for pathological files. Better values may be
  2303. * found for specific files.
  2304. */
  2305. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  2306. this.good_length = good_length;
  2307. this.max_lazy = max_lazy;
  2308. this.nice_length = nice_length;
  2309. this.max_chain = max_chain;
  2310. this.func = func;
  2311. }
  2312. var configuration_table = [
  2313. /* good lazy nice chain */
  2314. new Config(0, 0, 0, 0, deflate_stored),
  2315. /* 0 store only */
  2316. new Config(4, 4, 8, 4, deflate_fast),
  2317. /* 1 max speed, no lazy matches */
  2318. new Config(4, 5, 16, 8, deflate_fast),
  2319. /* 2 */
  2320. new Config(4, 6, 32, 32, deflate_fast),
  2321. /* 3 */
  2322. new Config(4, 4, 16, 16, deflate_slow),
  2323. /* 4 lazy matches */
  2324. new Config(8, 16, 32, 32, deflate_slow),
  2325. /* 5 */
  2326. new Config(8, 16, 128, 128, deflate_slow),
  2327. /* 6 */
  2328. new Config(8, 32, 128, 256, deflate_slow),
  2329. /* 7 */
  2330. new Config(32, 128, 258, 1024, deflate_slow),
  2331. /* 8 */
  2332. new Config(32, 258, 258, 4096, deflate_slow)
  2333. /* 9 max compression */
  2334. ];
  2335. /* ===========================================================================
  2336. * Initialize the "longest match" routines for a new zlib stream
  2337. */
  2338. var lm_init = function lm_init(s) {
  2339. s.window_size = 2 * s.w_size;
  2340. /*** CLEAR_HASH(s); ***/
  2341. zero(s.head); // Fill with NIL (= 0);
  2342. /* Set the default configuration parameters:
  2343. */
  2344. s.max_lazy_match = configuration_table[s.level].max_lazy;
  2345. s.good_match = configuration_table[s.level].good_length;
  2346. s.nice_match = configuration_table[s.level].nice_length;
  2347. s.max_chain_length = configuration_table[s.level].max_chain;
  2348. s.strstart = 0;
  2349. s.block_start = 0;
  2350. s.lookahead = 0;
  2351. s.insert = 0;
  2352. s.match_length = s.prev_length = MIN_MATCH - 1;
  2353. s.match_available = 0;
  2354. s.ins_h = 0;
  2355. };
  2356. function DeflateState() {
  2357. this.strm = null;
  2358. /* pointer back to this zlib stream */
  2359. this.status = 0;
  2360. /* as the name implies */
  2361. this.pending_buf = null;
  2362. /* output still pending */
  2363. this.pending_buf_size = 0;
  2364. /* size of pending_buf */
  2365. this.pending_out = 0;
  2366. /* next pending byte to output to the stream */
  2367. this.pending = 0;
  2368. /* nb of bytes in the pending buffer */
  2369. this.wrap = 0;
  2370. /* bit 0 true for zlib, bit 1 true for gzip */
  2371. this.gzhead = null;
  2372. /* gzip header information to write */
  2373. this.gzindex = 0;
  2374. /* where in extra, name, or comment */
  2375. this.method = Z_DEFLATED$1;
  2376. /* can only be DEFLATED */
  2377. this.last_flush = -1;
  2378. /* value of flush param for previous deflate call */
  2379. this.w_size = 0;
  2380. /* LZ77 window size (32K by default) */
  2381. this.w_bits = 0;
  2382. /* log2(w_size) (8..16) */
  2383. this.w_mask = 0;
  2384. /* w_size - 1 */
  2385. this.window = null;
  2386. /* Sliding window. Input bytes are read into the second half of the window,
  2387. * and move to the first half later to keep a dictionary of at least wSize
  2388. * bytes. With this organization, matches are limited to a distance of
  2389. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  2390. * performed with a length multiple of the block size.
  2391. */
  2392. this.window_size = 0;
  2393. /* Actual size of window: 2*wSize, except when the user input buffer
  2394. * is directly used as sliding window.
  2395. */
  2396. this.prev = null;
  2397. /* Link to older string with same hash index. To limit the size of this
  2398. * array to 64K, this link is maintained only for the last 32K strings.
  2399. * An index in this array is thus a window index modulo 32K.
  2400. */
  2401. this.head = null;
  2402. /* Heads of the hash chains or NIL. */
  2403. this.ins_h = 0;
  2404. /* hash index of string to be inserted */
  2405. this.hash_size = 0;
  2406. /* number of elements in hash table */
  2407. this.hash_bits = 0;
  2408. /* log2(hash_size) */
  2409. this.hash_mask = 0;
  2410. /* hash_size-1 */
  2411. this.hash_shift = 0;
  2412. /* Number of bits by which ins_h must be shifted at each input
  2413. * step. It must be such that after MIN_MATCH steps, the oldest
  2414. * byte no longer takes part in the hash key, that is:
  2415. * hash_shift * MIN_MATCH >= hash_bits
  2416. */
  2417. this.block_start = 0;
  2418. /* Window position at the beginning of the current output block. Gets
  2419. * negative when the window is moved backwards.
  2420. */
  2421. this.match_length = 0;
  2422. /* length of best match */
  2423. this.prev_match = 0;
  2424. /* previous match */
  2425. this.match_available = 0;
  2426. /* set if previous match exists */
  2427. this.strstart = 0;
  2428. /* start of string to insert */
  2429. this.match_start = 0;
  2430. /* start of matching string */
  2431. this.lookahead = 0;
  2432. /* number of valid bytes ahead in window */
  2433. this.prev_length = 0;
  2434. /* Length of the best match at previous step. Matches not greater than this
  2435. * are discarded. This is used in the lazy match evaluation.
  2436. */
  2437. this.max_chain_length = 0;
  2438. /* To speed up deflation, hash chains are never searched beyond this
  2439. * length. A higher limit improves compression ratio but degrades the
  2440. * speed.
  2441. */
  2442. this.max_lazy_match = 0;
  2443. /* Attempt to find a better match only when the current match is strictly
  2444. * smaller than this value. This mechanism is used only for compression
  2445. * levels >= 4.
  2446. */
  2447. // That's alias to max_lazy_match, don't use directly
  2448. //this.max_insert_length = 0;
  2449. /* Insert new strings in the hash table only if the match length is not
  2450. * greater than this length. This saves time but degrades compression.
  2451. * max_insert_length is used only for compression levels <= 3.
  2452. */
  2453. this.level = 0;
  2454. /* compression level (1..9) */
  2455. this.strategy = 0;
  2456. /* favor or force Huffman coding*/
  2457. this.good_match = 0;
  2458. /* Use a faster search when the previous match is longer than this */
  2459. this.nice_match = 0;
  2460. /* Stop searching when current match exceeds this */
  2461. /* used by trees.c: */
  2462. /* Didn't use ct_data typedef below to suppress compiler warning */
  2463. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  2464. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  2465. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  2466. // Use flat array of DOUBLE size, with interleaved fata,
  2467. // because JS does not support effective
  2468. this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2);
  2469. this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2);
  2470. this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2);
  2471. zero(this.dyn_ltree);
  2472. zero(this.dyn_dtree);
  2473. zero(this.bl_tree);
  2474. this.l_desc = null;
  2475. /* desc. for literal tree */
  2476. this.d_desc = null;
  2477. /* desc. for distance tree */
  2478. this.bl_desc = null;
  2479. /* desc. for bit length tree */
  2480. //ush bl_count[MAX_BITS+1];
  2481. this.bl_count = new Uint16Array(MAX_BITS + 1);
  2482. /* number of codes at each bit length for an optimal tree */
  2483. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  2484. this.heap = new Uint16Array(2 * L_CODES + 1);
  2485. /* heap used to build the Huffman trees */
  2486. zero(this.heap);
  2487. this.heap_len = 0;
  2488. /* number of elements in the heap */
  2489. this.heap_max = 0;
  2490. /* element of largest frequency */
  2491. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  2492. * The same heap array is used to build all trees.
  2493. */
  2494. this.depth = new Uint16Array(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
  2495. zero(this.depth);
  2496. /* Depth of each subtree used as tie breaker for trees of equal frequency
  2497. */
  2498. this.l_buf = 0;
  2499. /* buffer index for literals or lengths */
  2500. this.lit_bufsize = 0;
  2501. /* Size of match buffer for literals/lengths. There are 4 reasons for
  2502. * limiting lit_bufsize to 64K:
  2503. * - frequencies can be kept in 16 bit counters
  2504. * - if compression is not successful for the first block, all input
  2505. * data is still in the window so we can still emit a stored block even
  2506. * when input comes from standard input. (This can also be done for
  2507. * all blocks if lit_bufsize is not greater than 32K.)
  2508. * - if compression is not successful for a file smaller than 64K, we can
  2509. * even emit a stored file instead of a stored block (saving 5 bytes).
  2510. * This is applicable only for zip (not gzip or zlib).
  2511. * - creating new Huffman trees less frequently may not provide fast
  2512. * adaptation to changes in the input data statistics. (Take for
  2513. * example a binary file with poorly compressible code followed by
  2514. * a highly compressible string table.) Smaller buffer sizes give
  2515. * fast adaptation but have of course the overhead of transmitting
  2516. * trees more frequently.
  2517. * - I can't count above 4
  2518. */
  2519. this.last_lit = 0;
  2520. /* running index in l_buf */
  2521. this.d_buf = 0;
  2522. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  2523. * the same number of elements. To use different lengths, an extra flag
  2524. * array would be necessary.
  2525. */
  2526. this.opt_len = 0;
  2527. /* bit length of current block with optimal trees */
  2528. this.static_len = 0;
  2529. /* bit length of current block with static trees */
  2530. this.matches = 0;
  2531. /* number of string matches in current block */
  2532. this.insert = 0;
  2533. /* bytes at end of window left to insert */
  2534. this.bi_buf = 0;
  2535. /* Output buffer. bits are inserted starting at the bottom (least
  2536. * significant bits).
  2537. */
  2538. this.bi_valid = 0;
  2539. /* Number of valid bits in bi_buf. All bits above the last valid bit
  2540. * are always zero.
  2541. */
  2542. // Used for window memory init. We safely ignore it for JS. That makes
  2543. // sense only for pointers and memory check tools.
  2544. //this.high_water = 0;
  2545. /* High water mark offset in window for initialized bytes -- bytes above
  2546. * this are set to zero in order to avoid memory check warnings when
  2547. * longest match routines access bytes past the input. This is then
  2548. * updated to the new high water mark.
  2549. */
  2550. }
  2551. var deflateResetKeep = function deflateResetKeep(strm) {
  2552. if (!strm || !strm.state) {
  2553. return err(strm, Z_STREAM_ERROR);
  2554. }
  2555. strm.total_in = strm.total_out = 0;
  2556. strm.data_type = Z_UNKNOWN;
  2557. var s = strm.state;
  2558. s.pending = 0;
  2559. s.pending_out = 0;
  2560. if (s.wrap < 0) {
  2561. s.wrap = -s.wrap;
  2562. /* was made negative by deflate(..., Z_FINISH); */
  2563. }
  2564. s.status = s.wrap ? INIT_STATE : BUSY_STATE;
  2565. strm.adler = s.wrap === 2 ? 0 // crc32(0, Z_NULL, 0)
  2566. : 1; // adler32(0, Z_NULL, 0)
  2567. s.last_flush = Z_NO_FLUSH$1;
  2568. _tr_init(s);
  2569. return Z_OK$1;
  2570. };
  2571. var deflateReset = function deflateReset(strm) {
  2572. var ret = deflateResetKeep(strm);
  2573. if (ret === Z_OK$1) {
  2574. lm_init(strm.state);
  2575. }
  2576. return ret;
  2577. };
  2578. var deflateSetHeader = function deflateSetHeader(strm, head) {
  2579. if (!strm || !strm.state) {
  2580. return Z_STREAM_ERROR;
  2581. }
  2582. if (strm.state.wrap !== 2) {
  2583. return Z_STREAM_ERROR;
  2584. }
  2585. strm.state.gzhead = head;
  2586. return Z_OK$1;
  2587. };
  2588. var deflateInit2 = function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  2589. if (!strm) {
  2590. // === Z_NULL
  2591. return Z_STREAM_ERROR;
  2592. }
  2593. var wrap = 1;
  2594. if (level === Z_DEFAULT_COMPRESSION$1) {
  2595. level = 6;
  2596. }
  2597. if (windowBits < 0) {
  2598. /* suppress zlib wrapper */
  2599. wrap = 0;
  2600. windowBits = -windowBits;
  2601. } else if (windowBits > 15) {
  2602. wrap = 2;
  2603. /* write gzip wrapper instead */
  2604. windowBits -= 16;
  2605. }
  2606. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED$1 || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
  2607. return err(strm, Z_STREAM_ERROR);
  2608. }
  2609. if (windowBits === 8) {
  2610. windowBits = 9;
  2611. }
  2612. /* until 256-byte window bug fixed */
  2613. var s = new DeflateState();
  2614. strm.state = s;
  2615. s.strm = strm;
  2616. s.wrap = wrap;
  2617. s.gzhead = null;
  2618. s.w_bits = windowBits;
  2619. s.w_size = 1 << s.w_bits;
  2620. s.w_mask = s.w_size - 1;
  2621. s.hash_bits = memLevel + 7;
  2622. s.hash_size = 1 << s.hash_bits;
  2623. s.hash_mask = s.hash_size - 1;
  2624. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  2625. s.window = new Uint8Array(s.w_size * 2);
  2626. s.head = new Uint16Array(s.hash_size);
  2627. s.prev = new Uint16Array(s.w_size); // Don't need mem init magic for JS.
  2628. //s.high_water = 0; /* nothing written to s->window yet */
  2629. s.lit_bufsize = 1 << memLevel + 6;
  2630. /* 16K elements by default */
  2631. s.pending_buf_size = s.lit_bufsize * 4; //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  2632. //s->pending_buf = (uchf *) overlay;
  2633. s.pending_buf = new Uint8Array(s.pending_buf_size); // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
  2634. //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  2635. s.d_buf = 1 * s.lit_bufsize; //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  2636. s.l_buf = (1 + 2) * s.lit_bufsize;
  2637. s.level = level;
  2638. s.strategy = strategy;
  2639. s.method = method;
  2640. return deflateReset(strm);
  2641. };
  2642. var deflateInit = function deflateInit(strm, level) {
  2643. return deflateInit2(strm, level, Z_DEFLATED$1, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1);
  2644. };
  2645. var deflate$1 = function deflate(strm, flush) {
  2646. var beg, val; // for gzip header write only
  2647. if (!strm || !strm.state || flush > Z_BLOCK || flush < 0) {
  2648. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  2649. }
  2650. var s = strm.state;
  2651. if (!strm.output || !strm.input && strm.avail_in !== 0 || s.status === FINISH_STATE && flush !== Z_FINISH$1) {
  2652. return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR : Z_STREAM_ERROR);
  2653. }
  2654. s.strm = strm;
  2655. /* just in case */
  2656. var old_flush = s.last_flush;
  2657. s.last_flush = flush;
  2658. /* Write the header */
  2659. if (s.status === INIT_STATE) {
  2660. if (s.wrap === 2) {
  2661. // GZIP header
  2662. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2663. put_byte(s, 31);
  2664. put_byte(s, 139);
  2665. put_byte(s, 8);
  2666. if (!s.gzhead) {
  2667. // s->gzhead == Z_NULL
  2668. put_byte(s, 0);
  2669. put_byte(s, 0);
  2670. put_byte(s, 0);
  2671. put_byte(s, 0);
  2672. put_byte(s, 0);
  2673. put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0);
  2674. put_byte(s, OS_CODE);
  2675. s.status = BUSY_STATE;
  2676. } else {
  2677. put_byte(s, (s.gzhead.text ? 1 : 0) + (s.gzhead.hcrc ? 2 : 0) + (!s.gzhead.extra ? 0 : 4) + (!s.gzhead.name ? 0 : 8) + (!s.gzhead.comment ? 0 : 16));
  2678. put_byte(s, s.gzhead.time & 0xff);
  2679. put_byte(s, s.gzhead.time >> 8 & 0xff);
  2680. put_byte(s, s.gzhead.time >> 16 & 0xff);
  2681. put_byte(s, s.gzhead.time >> 24 & 0xff);
  2682. put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0);
  2683. put_byte(s, s.gzhead.os & 0xff);
  2684. if (s.gzhead.extra && s.gzhead.extra.length) {
  2685. put_byte(s, s.gzhead.extra.length & 0xff);
  2686. put_byte(s, s.gzhead.extra.length >> 8 & 0xff);
  2687. }
  2688. if (s.gzhead.hcrc) {
  2689. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0);
  2690. }
  2691. s.gzindex = 0;
  2692. s.status = EXTRA_STATE;
  2693. }
  2694. } else // DEFLATE header
  2695. {
  2696. var header = Z_DEFLATED$1 + (s.w_bits - 8 << 4) << 8;
  2697. var level_flags = -1;
  2698. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  2699. level_flags = 0;
  2700. } else if (s.level < 6) {
  2701. level_flags = 1;
  2702. } else if (s.level === 6) {
  2703. level_flags = 2;
  2704. } else {
  2705. level_flags = 3;
  2706. }
  2707. header |= level_flags << 6;
  2708. if (s.strstart !== 0) {
  2709. header |= PRESET_DICT;
  2710. }
  2711. header += 31 - header % 31;
  2712. s.status = BUSY_STATE;
  2713. putShortMSB(s, header);
  2714. /* Save the adler32 of the preset dictionary: */
  2715. if (s.strstart !== 0) {
  2716. putShortMSB(s, strm.adler >>> 16);
  2717. putShortMSB(s, strm.adler & 0xffff);
  2718. }
  2719. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  2720. }
  2721. } //#ifdef GZIP
  2722. if (s.status === EXTRA_STATE) {
  2723. if (s.gzhead.extra
  2724. /* != Z_NULL*/
  2725. ) {
  2726. beg = s.pending;
  2727. /* start of bytes to update crc */
  2728. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  2729. if (s.pending === s.pending_buf_size) {
  2730. if (s.gzhead.hcrc && s.pending > beg) {
  2731. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2732. }
  2733. flush_pending(strm);
  2734. beg = s.pending;
  2735. if (s.pending === s.pending_buf_size) {
  2736. break;
  2737. }
  2738. }
  2739. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  2740. s.gzindex++;
  2741. }
  2742. if (s.gzhead.hcrc && s.pending > beg) {
  2743. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2744. }
  2745. if (s.gzindex === s.gzhead.extra.length) {
  2746. s.gzindex = 0;
  2747. s.status = NAME_STATE;
  2748. }
  2749. } else {
  2750. s.status = NAME_STATE;
  2751. }
  2752. }
  2753. if (s.status === NAME_STATE) {
  2754. if (s.gzhead.name
  2755. /* != Z_NULL*/
  2756. ) {
  2757. beg = s.pending;
  2758. /* start of bytes to update crc */
  2759. //int val;
  2760. do {
  2761. if (s.pending === s.pending_buf_size) {
  2762. if (s.gzhead.hcrc && s.pending > beg) {
  2763. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2764. }
  2765. flush_pending(strm);
  2766. beg = s.pending;
  2767. if (s.pending === s.pending_buf_size) {
  2768. val = 1;
  2769. break;
  2770. }
  2771. } // JS specific: little magic to add zero terminator to end of string
  2772. if (s.gzindex < s.gzhead.name.length) {
  2773. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  2774. } else {
  2775. val = 0;
  2776. }
  2777. put_byte(s, val);
  2778. } while (val !== 0);
  2779. if (s.gzhead.hcrc && s.pending > beg) {
  2780. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2781. }
  2782. if (val === 0) {
  2783. s.gzindex = 0;
  2784. s.status = COMMENT_STATE;
  2785. }
  2786. } else {
  2787. s.status = COMMENT_STATE;
  2788. }
  2789. }
  2790. if (s.status === COMMENT_STATE) {
  2791. if (s.gzhead.comment
  2792. /* != Z_NULL*/
  2793. ) {
  2794. beg = s.pending;
  2795. /* start of bytes to update crc */
  2796. //int val;
  2797. do {
  2798. if (s.pending === s.pending_buf_size) {
  2799. if (s.gzhead.hcrc && s.pending > beg) {
  2800. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2801. }
  2802. flush_pending(strm);
  2803. beg = s.pending;
  2804. if (s.pending === s.pending_buf_size) {
  2805. val = 1;
  2806. break;
  2807. }
  2808. } // JS specific: little magic to add zero terminator to end of string
  2809. if (s.gzindex < s.gzhead.comment.length) {
  2810. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  2811. } else {
  2812. val = 0;
  2813. }
  2814. put_byte(s, val);
  2815. } while (val !== 0);
  2816. if (s.gzhead.hcrc && s.pending > beg) {
  2817. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2818. }
  2819. if (val === 0) {
  2820. s.status = HCRC_STATE;
  2821. }
  2822. } else {
  2823. s.status = HCRC_STATE;
  2824. }
  2825. }
  2826. if (s.status === HCRC_STATE) {
  2827. if (s.gzhead.hcrc) {
  2828. if (s.pending + 2 > s.pending_buf_size) {
  2829. flush_pending(strm);
  2830. }
  2831. if (s.pending + 2 <= s.pending_buf_size) {
  2832. put_byte(s, strm.adler & 0xff);
  2833. put_byte(s, strm.adler >> 8 & 0xff);
  2834. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2835. s.status = BUSY_STATE;
  2836. }
  2837. } else {
  2838. s.status = BUSY_STATE;
  2839. }
  2840. } //#endif
  2841. /* Flush as much pending output as possible */
  2842. if (s.pending !== 0) {
  2843. flush_pending(strm);
  2844. if (strm.avail_out === 0) {
  2845. /* Since avail_out is 0, deflate will be called again with
  2846. * more output space, but possibly with both pending and
  2847. * avail_in equal to zero. There won't be anything to do,
  2848. * but this is not an error situation so make sure we
  2849. * return OK instead of BUF_ERROR at next call of deflate:
  2850. */
  2851. s.last_flush = -1;
  2852. return Z_OK$1;
  2853. }
  2854. /* Make sure there is something to do and avoid duplicate consecutive
  2855. * flushes. For repeated and useless calls with Z_FINISH, we keep
  2856. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  2857. */
  2858. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && flush !== Z_FINISH$1) {
  2859. return err(strm, Z_BUF_ERROR);
  2860. }
  2861. /* User must not provide more input after the first FINISH: */
  2862. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  2863. return err(strm, Z_BUF_ERROR);
  2864. }
  2865. /* Start a new block or continue the current one.
  2866. */
  2867. if (strm.avail_in !== 0 || s.lookahead !== 0 || flush !== Z_NO_FLUSH$1 && s.status !== FINISH_STATE) {
  2868. var bstate = s.strategy === Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : s.strategy === Z_RLE ? deflate_rle(s, flush) : configuration_table[s.level].func(s, flush);
  2869. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  2870. s.status = FINISH_STATE;
  2871. }
  2872. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  2873. if (strm.avail_out === 0) {
  2874. s.last_flush = -1;
  2875. /* avoid BUF_ERROR next call, see above */
  2876. }
  2877. return Z_OK$1;
  2878. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  2879. * of deflate should use the same flush parameter to make sure
  2880. * that the flush is complete. So we don't have to output an
  2881. * empty block here, this will be done at next call. This also
  2882. * ensures that for a very small output buffer, we emit at most
  2883. * one empty block.
  2884. */
  2885. }
  2886. if (bstate === BS_BLOCK_DONE) {
  2887. if (flush === Z_PARTIAL_FLUSH) {
  2888. _tr_align(s);
  2889. } else if (flush !== Z_BLOCK) {
  2890. /* FULL_FLUSH or SYNC_FLUSH */
  2891. _tr_stored_block(s, 0, 0, false);
  2892. /* For a full flush, this empty block will be recognized
  2893. * as a special marker by inflate_sync().
  2894. */
  2895. if (flush === Z_FULL_FLUSH$1) {
  2896. /*** CLEAR_HASH(s); ***/
  2897. /* forget history */
  2898. zero(s.head); // Fill with NIL (= 0);
  2899. if (s.lookahead === 0) {
  2900. s.strstart = 0;
  2901. s.block_start = 0;
  2902. s.insert = 0;
  2903. }
  2904. }
  2905. }
  2906. flush_pending(strm);
  2907. if (strm.avail_out === 0) {
  2908. s.last_flush = -1;
  2909. /* avoid BUF_ERROR at next call, see above */
  2910. return Z_OK$1;
  2911. }
  2912. }
  2913. } //Assert(strm->avail_out > 0, "bug2");
  2914. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  2915. if (flush !== Z_FINISH$1) {
  2916. return Z_OK$1;
  2917. }
  2918. if (s.wrap <= 0) {
  2919. return Z_STREAM_END$1;
  2920. }
  2921. /* Write the trailer */
  2922. if (s.wrap === 2) {
  2923. put_byte(s, strm.adler & 0xff);
  2924. put_byte(s, strm.adler >> 8 & 0xff);
  2925. put_byte(s, strm.adler >> 16 & 0xff);
  2926. put_byte(s, strm.adler >> 24 & 0xff);
  2927. put_byte(s, strm.total_in & 0xff);
  2928. put_byte(s, strm.total_in >> 8 & 0xff);
  2929. put_byte(s, strm.total_in >> 16 & 0xff);
  2930. put_byte(s, strm.total_in >> 24 & 0xff);
  2931. } else {
  2932. putShortMSB(s, strm.adler >>> 16);
  2933. putShortMSB(s, strm.adler & 0xffff);
  2934. }
  2935. flush_pending(strm);
  2936. /* If avail_out is zero, the application will call deflate again
  2937. * to flush the rest.
  2938. */
  2939. if (s.wrap > 0) {
  2940. s.wrap = -s.wrap;
  2941. }
  2942. /* write the trailer only once! */
  2943. return s.pending !== 0 ? Z_OK$1 : Z_STREAM_END$1;
  2944. };
  2945. var deflateEnd = function deflateEnd(strm) {
  2946. if (!strm
  2947. /*== Z_NULL*/
  2948. || !strm.state
  2949. /*== Z_NULL*/
  2950. ) {
  2951. return Z_STREAM_ERROR;
  2952. }
  2953. var status = strm.state.status;
  2954. if (status !== INIT_STATE && status !== EXTRA_STATE && status !== NAME_STATE && status !== COMMENT_STATE && status !== HCRC_STATE && status !== BUSY_STATE && status !== FINISH_STATE) {
  2955. return err(strm, Z_STREAM_ERROR);
  2956. }
  2957. strm.state = null;
  2958. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK$1;
  2959. };
  2960. /* =========================================================================
  2961. * Initializes the compression dictionary from the given byte
  2962. * sequence without producing any compressed output.
  2963. */
  2964. var deflateSetDictionary = function deflateSetDictionary(strm, dictionary) {
  2965. var dictLength = dictionary.length;
  2966. if (!strm
  2967. /*== Z_NULL*/
  2968. || !strm.state
  2969. /*== Z_NULL*/
  2970. ) {
  2971. return Z_STREAM_ERROR;
  2972. }
  2973. var s = strm.state;
  2974. var wrap = s.wrap;
  2975. if (wrap === 2 || wrap === 1 && s.status !== INIT_STATE || s.lookahead) {
  2976. return Z_STREAM_ERROR;
  2977. }
  2978. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  2979. if (wrap === 1) {
  2980. /* adler32(strm->adler, dictionary, dictLength); */
  2981. strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0);
  2982. }
  2983. s.wrap = 0;
  2984. /* avoid computing Adler-32 in read_buf */
  2985. /* if dictionary would fill window, just replace the history */
  2986. if (dictLength >= s.w_size) {
  2987. if (wrap === 0) {
  2988. /* already empty otherwise */
  2989. /*** CLEAR_HASH(s); ***/
  2990. zero(s.head); // Fill with NIL (= 0);
  2991. s.strstart = 0;
  2992. s.block_start = 0;
  2993. s.insert = 0;
  2994. }
  2995. /* use the tail */
  2996. // dictionary = dictionary.slice(dictLength - s.w_size);
  2997. var tmpDict = new Uint8Array(s.w_size);
  2998. tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0);
  2999. dictionary = tmpDict;
  3000. dictLength = s.w_size;
  3001. }
  3002. /* insert dictionary into window and hash */
  3003. var avail = strm.avail_in;
  3004. var next = strm.next_in;
  3005. var input = strm.input;
  3006. strm.avail_in = dictLength;
  3007. strm.next_in = 0;
  3008. strm.input = dictionary;
  3009. fill_window(s);
  3010. while (s.lookahead >= MIN_MATCH) {
  3011. var str = s.strstart;
  3012. var n = s.lookahead - (MIN_MATCH - 1);
  3013. do {
  3014. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  3015. s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
  3016. s.prev[str & s.w_mask] = s.head[s.ins_h];
  3017. s.head[s.ins_h] = str;
  3018. str++;
  3019. } while (--n);
  3020. s.strstart = str;
  3021. s.lookahead = MIN_MATCH - 1;
  3022. fill_window(s);
  3023. }
  3024. s.strstart += s.lookahead;
  3025. s.block_start = s.strstart;
  3026. s.insert = s.lookahead;
  3027. s.lookahead = 0;
  3028. s.match_length = s.prev_length = MIN_MATCH - 1;
  3029. s.match_available = 0;
  3030. strm.next_in = next;
  3031. strm.input = input;
  3032. strm.avail_in = avail;
  3033. s.wrap = wrap;
  3034. return Z_OK$1;
  3035. };
  3036. var deflateInit_1 = deflateInit;
  3037. var deflateInit2_1 = deflateInit2;
  3038. var deflateReset_1 = deflateReset;
  3039. var deflateResetKeep_1 = deflateResetKeep;
  3040. var deflateSetHeader_1 = deflateSetHeader;
  3041. var deflate_2$1 = deflate$1;
  3042. var deflateEnd_1 = deflateEnd;
  3043. var deflateSetDictionary_1 = deflateSetDictionary;
  3044. var deflateInfo = 'pako deflate (from Nodeca project)';
  3045. /* Not implemented
  3046. module.exports.deflateBound = deflateBound;
  3047. module.exports.deflateCopy = deflateCopy;
  3048. module.exports.deflateParams = deflateParams;
  3049. module.exports.deflatePending = deflatePending;
  3050. module.exports.deflatePrime = deflatePrime;
  3051. module.exports.deflateTune = deflateTune;
  3052. */
  3053. var deflate_1$1 = {
  3054. deflateInit: deflateInit_1,
  3055. deflateInit2: deflateInit2_1,
  3056. deflateReset: deflateReset_1,
  3057. deflateResetKeep: deflateResetKeep_1,
  3058. deflateSetHeader: deflateSetHeader_1,
  3059. deflate: deflate_2$1,
  3060. deflateEnd: deflateEnd_1,
  3061. deflateSetDictionary: deflateSetDictionary_1,
  3062. deflateInfo: deflateInfo
  3063. };
  3064. function _typeof(obj) {
  3065. "@babel/helpers - typeof";
  3066. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  3067. _typeof = function (obj) {
  3068. return typeof obj;
  3069. };
  3070. } else {
  3071. _typeof = function (obj) {
  3072. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  3073. };
  3074. }
  3075. return _typeof(obj);
  3076. }
  3077. var _has = function _has(obj, key) {
  3078. return Object.prototype.hasOwnProperty.call(obj, key);
  3079. };
  3080. var assign = function assign(obj
  3081. /*from1, from2, from3, ...*/
  3082. ) {
  3083. var sources = Array.prototype.slice.call(arguments, 1);
  3084. while (sources.length) {
  3085. var source = sources.shift();
  3086. if (!source) {
  3087. continue;
  3088. }
  3089. if (_typeof(source) !== 'object') {
  3090. throw new TypeError(source + 'must be non-object');
  3091. }
  3092. for (var p in source) {
  3093. if (_has(source, p)) {
  3094. obj[p] = source[p];
  3095. }
  3096. }
  3097. }
  3098. return obj;
  3099. }; // Join array of chunks to single array.
  3100. var flattenChunks = function flattenChunks(chunks) {
  3101. // calculate data length
  3102. var len = 0;
  3103. for (var i = 0, l = chunks.length; i < l; i++) {
  3104. len += chunks[i].length;
  3105. } // join chunks
  3106. var result = new Uint8Array(len);
  3107. for (var _i = 0, pos = 0, _l = chunks.length; _i < _l; _i++) {
  3108. var chunk = chunks[_i];
  3109. result.set(chunk, pos);
  3110. pos += chunk.length;
  3111. }
  3112. return result;
  3113. };
  3114. var common = {
  3115. assign: assign,
  3116. flattenChunks: flattenChunks
  3117. };
  3118. // String encode/decode helpers
  3119. //
  3120. // - apply(Array) can fail on Android 2.2
  3121. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  3122. //
  3123. var STR_APPLY_UIA_OK = true;
  3124. try {
  3125. String.fromCharCode.apply(null, new Uint8Array(1));
  3126. } catch (__) {
  3127. STR_APPLY_UIA_OK = false;
  3128. } // Table with utf8 lengths (calculated by first byte of sequence)
  3129. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  3130. // because max possible codepoint is 0x10ffff
  3131. var _utf8len = new Uint8Array(256);
  3132. for (var q = 0; q < 256; q++) {
  3133. _utf8len[q] = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1;
  3134. }
  3135. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  3136. // convert string to array (typed, when possible)
  3137. var string2buf = function string2buf(str) {
  3138. if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
  3139. return new TextEncoder().encode(str);
  3140. }
  3141. var buf,
  3142. c,
  3143. c2,
  3144. m_pos,
  3145. i,
  3146. str_len = str.length,
  3147. buf_len = 0; // count binary size
  3148. for (m_pos = 0; m_pos < str_len; m_pos++) {
  3149. c = str.charCodeAt(m_pos);
  3150. if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
  3151. c2 = str.charCodeAt(m_pos + 1);
  3152. if ((c2 & 0xfc00) === 0xdc00) {
  3153. c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00);
  3154. m_pos++;
  3155. }
  3156. }
  3157. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  3158. } // allocate buffer
  3159. buf = new Uint8Array(buf_len); // convert
  3160. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  3161. c = str.charCodeAt(m_pos);
  3162. if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
  3163. c2 = str.charCodeAt(m_pos + 1);
  3164. if ((c2 & 0xfc00) === 0xdc00) {
  3165. c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00);
  3166. m_pos++;
  3167. }
  3168. }
  3169. if (c < 0x80) {
  3170. /* one byte */
  3171. buf[i++] = c;
  3172. } else if (c < 0x800) {
  3173. /* two bytes */
  3174. buf[i++] = 0xC0 | c >>> 6;
  3175. buf[i++] = 0x80 | c & 0x3f;
  3176. } else if (c < 0x10000) {
  3177. /* three bytes */
  3178. buf[i++] = 0xE0 | c >>> 12;
  3179. buf[i++] = 0x80 | c >>> 6 & 0x3f;
  3180. buf[i++] = 0x80 | c & 0x3f;
  3181. } else {
  3182. /* four bytes */
  3183. buf[i++] = 0xf0 | c >>> 18;
  3184. buf[i++] = 0x80 | c >>> 12 & 0x3f;
  3185. buf[i++] = 0x80 | c >>> 6 & 0x3f;
  3186. buf[i++] = 0x80 | c & 0x3f;
  3187. }
  3188. }
  3189. return buf;
  3190. }; // Helper
  3191. var buf2binstring = function buf2binstring(buf, len) {
  3192. // On Chrome, the arguments in a function call that are allowed is `65534`.
  3193. // If the length of the buffer is smaller than that, we can use this optimization,
  3194. // otherwise we will take a slower path.
  3195. if (len < 65534) {
  3196. if (buf.subarray && STR_APPLY_UIA_OK) {
  3197. return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
  3198. }
  3199. }
  3200. var result = '';
  3201. for (var i = 0; i < len; i++) {
  3202. result += String.fromCharCode(buf[i]);
  3203. }
  3204. return result;
  3205. }; // convert array to string
  3206. var buf2string = function buf2string(buf, max) {
  3207. var len = max || buf.length;
  3208. if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) {
  3209. return new TextDecoder().decode(buf.subarray(0, max));
  3210. }
  3211. var i, out; // Reserve max possible length (2 words per char)
  3212. // NB: by unknown reasons, Array is significantly faster for
  3213. // String.fromCharCode.apply than Uint16Array.
  3214. var utf16buf = new Array(len * 2);
  3215. for (out = 0, i = 0; i < len;) {
  3216. var c = buf[i++]; // quick process ascii
  3217. if (c < 0x80) {
  3218. utf16buf[out++] = c;
  3219. continue;
  3220. }
  3221. var c_len = _utf8len[c]; // skip 5 & 6 byte codes
  3222. if (c_len > 4) {
  3223. utf16buf[out++] = 0xfffd;
  3224. i += c_len - 1;
  3225. continue;
  3226. } // apply mask on first byte
  3227. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07; // join the rest
  3228. while (c_len > 1 && i < len) {
  3229. c = c << 6 | buf[i++] & 0x3f;
  3230. c_len--;
  3231. } // terminated by end of string?
  3232. if (c_len > 1) {
  3233. utf16buf[out++] = 0xfffd;
  3234. continue;
  3235. }
  3236. if (c < 0x10000) {
  3237. utf16buf[out++] = c;
  3238. } else {
  3239. c -= 0x10000;
  3240. utf16buf[out++] = 0xd800 | c >> 10 & 0x3ff;
  3241. utf16buf[out++] = 0xdc00 | c & 0x3ff;
  3242. }
  3243. }
  3244. return buf2binstring(utf16buf, out);
  3245. }; // Calculate max possible position in utf8 buffer,
  3246. // that will not break sequence. If that's not possible
  3247. // - (very small limits) return max size as is.
  3248. //
  3249. // buf[] - utf8 bytes array
  3250. // max - length limit (mandatory);
  3251. var utf8border = function utf8border(buf, max) {
  3252. max = max || buf.length;
  3253. if (max > buf.length) {
  3254. max = buf.length;
  3255. } // go back from last position, until start of sequence found
  3256. var pos = max - 1;
  3257. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) {
  3258. pos--;
  3259. } // Very small and broken sequence,
  3260. // return max, because we should return something anyway.
  3261. if (pos < 0) {
  3262. return max;
  3263. } // If we came to start of buffer - that means buffer is too small,
  3264. // return max too.
  3265. if (pos === 0) {
  3266. return max;
  3267. }
  3268. return pos + _utf8len[buf[pos]] > max ? pos : max;
  3269. };
  3270. var strings = {
  3271. string2buf: string2buf,
  3272. buf2string: buf2string,
  3273. utf8border: utf8border
  3274. };
  3275. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3276. //
  3277. // This software is provided 'as-is', without any express or implied
  3278. // warranty. In no event will the authors be held liable for any damages
  3279. // arising from the use of this software.
  3280. //
  3281. // Permission is granted to anyone to use this software for any purpose,
  3282. // including commercial applications, and to alter it and redistribute it
  3283. // freely, subject to the following restrictions:
  3284. //
  3285. // 1. The origin of this software must not be misrepresented; you must not
  3286. // claim that you wrote the original software. If you use this software
  3287. // in a product, an acknowledgment in the product documentation would be
  3288. // appreciated but is not required.
  3289. // 2. Altered source versions must be plainly marked as such, and must not be
  3290. // misrepresented as being the original software.
  3291. // 3. This notice may not be removed or altered from any source distribution.
  3292. function ZStream() {
  3293. /* next input byte */
  3294. this.input = null; // JS specific, because we have no pointers
  3295. this.next_in = 0;
  3296. /* number of bytes available at input */
  3297. this.avail_in = 0;
  3298. /* total number of input bytes read so far */
  3299. this.total_in = 0;
  3300. /* next output byte should be put there */
  3301. this.output = null; // JS specific, because we have no pointers
  3302. this.next_out = 0;
  3303. /* remaining free space at output */
  3304. this.avail_out = 0;
  3305. /* total number of bytes output so far */
  3306. this.total_out = 0;
  3307. /* last error message, NULL if no error */
  3308. this.msg = ''
  3309. /*Z_NULL*/
  3310. ;
  3311. /* not visible by applications */
  3312. this.state = null;
  3313. /* best guess about the data type: binary or text */
  3314. this.data_type = 2
  3315. /*Z_UNKNOWN*/
  3316. ;
  3317. /* adler32 value of the uncompressed data */
  3318. this.adler = 0;
  3319. }
  3320. var zstream = ZStream;
  3321. var toString = Object.prototype.toString;
  3322. /* Public constants ==========================================================*/
  3323. /* ===========================================================================*/
  3324. var Z_NO_FLUSH = constants$1.Z_NO_FLUSH,
  3325. Z_SYNC_FLUSH = constants$1.Z_SYNC_FLUSH,
  3326. Z_FULL_FLUSH = constants$1.Z_FULL_FLUSH,
  3327. Z_FINISH = constants$1.Z_FINISH,
  3328. Z_OK = constants$1.Z_OK,
  3329. Z_STREAM_END = constants$1.Z_STREAM_END,
  3330. Z_DEFAULT_COMPRESSION = constants$1.Z_DEFAULT_COMPRESSION,
  3331. Z_DEFAULT_STRATEGY = constants$1.Z_DEFAULT_STRATEGY,
  3332. Z_DEFLATED = constants$1.Z_DEFLATED;
  3333. /* ===========================================================================*/
  3334. /**
  3335. * class Deflate
  3336. *
  3337. * Generic JS-style wrapper for zlib calls. If you don't need
  3338. * streaming behaviour - use more simple functions: [[deflate]],
  3339. * [[deflateRaw]] and [[gzip]].
  3340. **/
  3341. /* internal
  3342. * Deflate.chunks -> Array
  3343. *
  3344. * Chunks of output data, if [[Deflate#onData]] not overridden.
  3345. **/
  3346. /**
  3347. * Deflate.result -> Uint8Array
  3348. *
  3349. * Compressed result, generated by default [[Deflate#onData]]
  3350. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  3351. * (call [[Deflate#push]] with `Z_FINISH` / `true` param).
  3352. **/
  3353. /**
  3354. * Deflate.err -> Number
  3355. *
  3356. * Error code after deflate finished. 0 (Z_OK) on success.
  3357. * You will not need it in real life, because deflate errors
  3358. * are possible only on wrong options or bad `onData` / `onEnd`
  3359. * custom handlers.
  3360. **/
  3361. /**
  3362. * Deflate.msg -> String
  3363. *
  3364. * Error message, if [[Deflate.err]] != 0
  3365. **/
  3366. /**
  3367. * new Deflate(options)
  3368. * - options (Object): zlib deflate options.
  3369. *
  3370. * Creates new deflator instance with specified params. Throws exception
  3371. * on bad params. Supported options:
  3372. *
  3373. * - `level`
  3374. * - `windowBits`
  3375. * - `memLevel`
  3376. * - `strategy`
  3377. * - `dictionary`
  3378. *
  3379. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3380. * for more information on these.
  3381. *
  3382. * Additional options, for internal needs:
  3383. *
  3384. * - `chunkSize` - size of generated data chunks (16K by default)
  3385. * - `raw` (Boolean) - do raw deflate
  3386. * - `gzip` (Boolean) - create gzip wrapper
  3387. * - `header` (Object) - custom header for gzip
  3388. * - `text` (Boolean) - true if compressed data believed to be text
  3389. * - `time` (Number) - modification time, unix timestamp
  3390. * - `os` (Number) - operation system code
  3391. * - `extra` (Array) - array of bytes with extra data (max 65536)
  3392. * - `name` (String) - file name (binary string)
  3393. * - `comment` (String) - comment (binary string)
  3394. * - `hcrc` (Boolean) - true if header crc should be added
  3395. *
  3396. * ##### Example:
  3397. *
  3398. * ```javascript
  3399. * const pako = require('pako')
  3400. * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  3401. * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  3402. *
  3403. * const deflate = new pako.Deflate({ level: 3});
  3404. *
  3405. * deflate.push(chunk1, false);
  3406. * deflate.push(chunk2, true); // true -> last chunk
  3407. *
  3408. * if (deflate.err) { throw new Error(deflate.err); }
  3409. *
  3410. * console.log(deflate.result);
  3411. * ```
  3412. **/
  3413. function Deflate(options) {
  3414. this.options = common.assign({
  3415. level: Z_DEFAULT_COMPRESSION,
  3416. method: Z_DEFLATED,
  3417. chunkSize: 16384,
  3418. windowBits: 15,
  3419. memLevel: 8,
  3420. strategy: Z_DEFAULT_STRATEGY
  3421. }, options || {});
  3422. var opt = this.options;
  3423. if (opt.raw && opt.windowBits > 0) {
  3424. opt.windowBits = -opt.windowBits;
  3425. } else if (opt.gzip && opt.windowBits > 0 && opt.windowBits < 16) {
  3426. opt.windowBits += 16;
  3427. }
  3428. this.err = 0; // error code, if happens (0 = Z_OK)
  3429. this.msg = ''; // error message
  3430. this.ended = false; // used to avoid multiple onEnd() calls
  3431. this.chunks = []; // chunks of compressed data
  3432. this.strm = new zstream();
  3433. this.strm.avail_out = 0;
  3434. var status = deflate_1$1.deflateInit2(this.strm, opt.level, opt.method, opt.windowBits, opt.memLevel, opt.strategy);
  3435. if (status !== Z_OK) {
  3436. throw new Error(messages[status]);
  3437. }
  3438. if (opt.header) {
  3439. deflate_1$1.deflateSetHeader(this.strm, opt.header);
  3440. }
  3441. if (opt.dictionary) {
  3442. var dict; // Convert data if needed
  3443. if (typeof opt.dictionary === 'string') {
  3444. // If we need to compress text, change encoding to utf8.
  3445. dict = strings.string2buf(opt.dictionary);
  3446. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  3447. dict = new Uint8Array(opt.dictionary);
  3448. } else {
  3449. dict = opt.dictionary;
  3450. }
  3451. status = deflate_1$1.deflateSetDictionary(this.strm, dict);
  3452. if (status !== Z_OK) {
  3453. throw new Error(messages[status]);
  3454. }
  3455. this._dict_set = true;
  3456. }
  3457. }
  3458. /**
  3459. * Deflate#push(data[, flush_mode]) -> Boolean
  3460. * - data (Uint8Array|ArrayBuffer|String): input data. Strings will be
  3461. * converted to utf8 byte sequence.
  3462. * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  3463. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
  3464. *
  3465. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  3466. * new compressed chunks. Returns `true` on success. The last data block must
  3467. * have `flush_mode` Z_FINISH (or `true`). That will flush internal pending
  3468. * buffers and call [[Deflate#onEnd]].
  3469. *
  3470. * On fail call [[Deflate#onEnd]] with error code and return false.
  3471. *
  3472. * ##### Example
  3473. *
  3474. * ```javascript
  3475. * push(chunk, false); // push one of data chunks
  3476. * ...
  3477. * push(chunk, true); // push last chunk
  3478. * ```
  3479. **/
  3480. Deflate.prototype.push = function (data, flush_mode) {
  3481. var strm = this.strm;
  3482. var chunkSize = this.options.chunkSize;
  3483. var status, _flush_mode;
  3484. if (this.ended) {
  3485. return false;
  3486. }
  3487. if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH; // Convert data if needed
  3488. if (typeof data === 'string') {
  3489. // If we need to compress text, change encoding to utf8.
  3490. strm.input = strings.string2buf(data);
  3491. } else if (toString.call(data) === '[object ArrayBuffer]') {
  3492. strm.input = new Uint8Array(data);
  3493. } else {
  3494. strm.input = data;
  3495. }
  3496. strm.next_in = 0;
  3497. strm.avail_in = strm.input.length;
  3498. for (;;) {
  3499. if (strm.avail_out === 0) {
  3500. strm.output = new Uint8Array(chunkSize);
  3501. strm.next_out = 0;
  3502. strm.avail_out = chunkSize;
  3503. } // Make sure avail_out > 6 to avoid repeating markers
  3504. if ((_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH) && strm.avail_out <= 6) {
  3505. this.onData(strm.output.subarray(0, strm.next_out));
  3506. strm.avail_out = 0;
  3507. continue;
  3508. }
  3509. status = deflate_1$1.deflate(strm, _flush_mode); // Ended => flush and finish
  3510. if (status === Z_STREAM_END) {
  3511. if (strm.next_out > 0) {
  3512. this.onData(strm.output.subarray(0, strm.next_out));
  3513. }
  3514. status = deflate_1$1.deflateEnd(this.strm);
  3515. this.onEnd(status);
  3516. this.ended = true;
  3517. return status === Z_OK;
  3518. } // Flush if out buffer full
  3519. if (strm.avail_out === 0) {
  3520. this.onData(strm.output);
  3521. continue;
  3522. } // Flush if requested and has data
  3523. if (_flush_mode > 0 && strm.next_out > 0) {
  3524. this.onData(strm.output.subarray(0, strm.next_out));
  3525. strm.avail_out = 0;
  3526. continue;
  3527. }
  3528. if (strm.avail_in === 0) break;
  3529. }
  3530. return true;
  3531. };
  3532. /**
  3533. * Deflate#onData(chunk) -> Void
  3534. * - chunk (Uint8Array): output data.
  3535. *
  3536. * By default, stores data blocks in `chunks[]` property and glue
  3537. * those in `onEnd`. Override this handler, if you need another behaviour.
  3538. **/
  3539. Deflate.prototype.onData = function (chunk) {
  3540. this.chunks.push(chunk);
  3541. };
  3542. /**
  3543. * Deflate#onEnd(status) -> Void
  3544. * - status (Number): deflate status. 0 (Z_OK) on success,
  3545. * other if not.
  3546. *
  3547. * Called once after you tell deflate that the input stream is
  3548. * complete (Z_FINISH). By default - join collected chunks,
  3549. * free memory and fill `results` / `err` properties.
  3550. **/
  3551. Deflate.prototype.onEnd = function (status) {
  3552. // On success - join
  3553. if (status === Z_OK) {
  3554. this.result = common.flattenChunks(this.chunks);
  3555. }
  3556. this.chunks = [];
  3557. this.err = status;
  3558. this.msg = this.strm.msg;
  3559. };
  3560. /**
  3561. * deflate(data[, options]) -> Uint8Array
  3562. * - data (Uint8Array|String): input data to compress.
  3563. * - options (Object): zlib deflate options.
  3564. *
  3565. * Compress `data` with deflate algorithm and `options`.
  3566. *
  3567. * Supported options are:
  3568. *
  3569. * - level
  3570. * - windowBits
  3571. * - memLevel
  3572. * - strategy
  3573. * - dictionary
  3574. *
  3575. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3576. * for more information on these.
  3577. *
  3578. * Sugar (options):
  3579. *
  3580. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  3581. * negative windowBits implicitly.
  3582. *
  3583. * ##### Example:
  3584. *
  3585. * ```javascript
  3586. * const pako = require('pako')
  3587. * const data = new Uint8Array([1,2,3,4,5,6,7,8,9]);
  3588. *
  3589. * console.log(pako.deflate(data));
  3590. * ```
  3591. **/
  3592. function deflate(input, options) {
  3593. var deflator = new Deflate(options);
  3594. deflator.push(input, true); // That will never happens, if you don't cheat with options :)
  3595. if (deflator.err) {
  3596. throw deflator.msg || messages[deflator.err];
  3597. }
  3598. return deflator.result;
  3599. }
  3600. /**
  3601. * deflateRaw(data[, options]) -> Uint8Array
  3602. * - data (Uint8Array|String): input data to compress.
  3603. * - options (Object): zlib deflate options.
  3604. *
  3605. * The same as [[deflate]], but creates raw data, without wrapper
  3606. * (header and adler32 crc).
  3607. **/
  3608. function deflateRaw(input, options) {
  3609. options = options || {};
  3610. options.raw = true;
  3611. return deflate(input, options);
  3612. }
  3613. /**
  3614. * gzip(data[, options]) -> Uint8Array
  3615. * - data (Uint8Array|String): input data to compress.
  3616. * - options (Object): zlib deflate options.
  3617. *
  3618. * The same as [[deflate]], but create gzip wrapper instead of
  3619. * deflate one.
  3620. **/
  3621. function gzip(input, options) {
  3622. options = options || {};
  3623. options.gzip = true;
  3624. return deflate(input, options);
  3625. }
  3626. var Deflate_1 = Deflate;
  3627. var deflate_2 = deflate;
  3628. var deflateRaw_1 = deflateRaw;
  3629. var gzip_1 = gzip;
  3630. var constants = constants$1;
  3631. var deflate_1 = {
  3632. Deflate: Deflate_1,
  3633. deflate: deflate_2,
  3634. deflateRaw: deflateRaw_1,
  3635. gzip: gzip_1,
  3636. constants: constants
  3637. };
  3638. exports.Deflate = Deflate_1;
  3639. exports.constants = constants;
  3640. exports['default'] = deflate_1;
  3641. exports.deflate = deflate_2;
  3642. exports.deflateRaw = deflateRaw_1;
  3643. exports.gzip = gzip_1;
  3644. Object.defineProperty(exports, '__esModule', { value: true });
  3645. })));