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.

8027 lines
225 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$2 = {
  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$2 = constants$2.Z_NO_FLUSH,
  1411. Z_PARTIAL_FLUSH = constants$2.Z_PARTIAL_FLUSH,
  1412. Z_FULL_FLUSH$1 = constants$2.Z_FULL_FLUSH,
  1413. Z_FINISH$3 = constants$2.Z_FINISH,
  1414. Z_BLOCK$1 = constants$2.Z_BLOCK,
  1415. Z_OK$3 = constants$2.Z_OK,
  1416. Z_STREAM_END$3 = constants$2.Z_STREAM_END,
  1417. Z_STREAM_ERROR$2 = constants$2.Z_STREAM_ERROR,
  1418. Z_DATA_ERROR$2 = constants$2.Z_DATA_ERROR,
  1419. Z_BUF_ERROR$1 = constants$2.Z_BUF_ERROR,
  1420. Z_DEFAULT_COMPRESSION$1 = constants$2.Z_DEFAULT_COMPRESSION,
  1421. Z_FILTERED = constants$2.Z_FILTERED,
  1422. Z_HUFFMAN_ONLY = constants$2.Z_HUFFMAN_ONLY,
  1423. Z_RLE = constants$2.Z_RLE,
  1424. Z_FIXED = constants$2.Z_FIXED,
  1425. Z_DEFAULT_STRATEGY$1 = constants$2.Z_DEFAULT_STRATEGY,
  1426. Z_UNKNOWN = constants$2.Z_UNKNOWN,
  1427. Z_DEFLATED$2 = constants$2.Z_DEFLATED;
  1428. /*============================================================================*/
  1429. var MAX_MEM_LEVEL = 9;
  1430. /* Maximum value for memLevel in deflateInit2 */
  1431. var MAX_WBITS$1 = 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$2) {
  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$3) {
  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$2) {
  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$3) {
  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$2) {
  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$3) {
  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$2) {
  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$3) {
  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$2) {
  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$3) {
  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$2;
  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$2);
  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$2;
  2568. _tr_init(s);
  2569. return Z_OK$3;
  2570. };
  2571. var deflateReset = function deflateReset(strm) {
  2572. var ret = deflateResetKeep(strm);
  2573. if (ret === Z_OK$3) {
  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$2;
  2581. }
  2582. if (strm.state.wrap !== 2) {
  2583. return Z_STREAM_ERROR$2;
  2584. }
  2585. strm.state.gzhead = head;
  2586. return Z_OK$3;
  2587. };
  2588. var deflateInit2 = function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  2589. if (!strm) {
  2590. // === Z_NULL
  2591. return Z_STREAM_ERROR$2;
  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$2 || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
  2607. return err(strm, Z_STREAM_ERROR$2);
  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$2, MAX_WBITS$1, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1);
  2644. };
  2645. var deflate$2 = function deflate(strm, flush) {
  2646. var beg, val; // for gzip header write only
  2647. if (!strm || !strm.state || flush > Z_BLOCK$1 || flush < 0) {
  2648. return strm ? err(strm, Z_STREAM_ERROR$2) : Z_STREAM_ERROR$2;
  2649. }
  2650. var s = strm.state;
  2651. if (!strm.output || !strm.input && strm.avail_in !== 0 || s.status === FINISH_STATE && flush !== Z_FINISH$3) {
  2652. return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR$1 : Z_STREAM_ERROR$2);
  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$2 + (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$3;
  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$3) {
  2859. return err(strm, Z_BUF_ERROR$1);
  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$1);
  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$2 && 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$3;
  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$1) {
  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$3;
  2911. }
  2912. }
  2913. } //Assert(strm->avail_out > 0, "bug2");
  2914. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  2915. if (flush !== Z_FINISH$3) {
  2916. return Z_OK$3;
  2917. }
  2918. if (s.wrap <= 0) {
  2919. return Z_STREAM_END$3;
  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$3 : Z_STREAM_END$3;
  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$2;
  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$2);
  2956. }
  2957. strm.state = null;
  2958. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR$2) : Z_OK$3;
  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$2;
  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$2;
  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$3;
  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$2;
  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$2 = {
  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$1 = Object.prototype.toString;
  3322. /* Public constants ==========================================================*/
  3323. /* ===========================================================================*/
  3324. var Z_NO_FLUSH$1 = constants$2.Z_NO_FLUSH,
  3325. Z_SYNC_FLUSH = constants$2.Z_SYNC_FLUSH,
  3326. Z_FULL_FLUSH = constants$2.Z_FULL_FLUSH,
  3327. Z_FINISH$2 = constants$2.Z_FINISH,
  3328. Z_OK$2 = constants$2.Z_OK,
  3329. Z_STREAM_END$2 = constants$2.Z_STREAM_END,
  3330. Z_DEFAULT_COMPRESSION = constants$2.Z_DEFAULT_COMPRESSION,
  3331. Z_DEFAULT_STRATEGY = constants$2.Z_DEFAULT_STRATEGY,
  3332. Z_DEFLATED$1 = constants$2.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$1(options) {
  3414. this.options = common.assign({
  3415. level: Z_DEFAULT_COMPRESSION,
  3416. method: Z_DEFLATED$1,
  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$2.deflateInit2(this.strm, opt.level, opt.method, opt.windowBits, opt.memLevel, opt.strategy);
  3435. if (status !== Z_OK$2) {
  3436. throw new Error(messages[status]);
  3437. }
  3438. if (opt.header) {
  3439. deflate_1$2.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$1.call(opt.dictionary) === '[object ArrayBuffer]') {
  3447. dict = new Uint8Array(opt.dictionary);
  3448. } else {
  3449. dict = opt.dictionary;
  3450. }
  3451. status = deflate_1$2.deflateSetDictionary(this.strm, dict);
  3452. if (status !== Z_OK$2) {
  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$1.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$2 : Z_NO_FLUSH$1; // 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$1.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$2.deflate(strm, _flush_mode); // Ended => flush and finish
  3510. if (status === Z_STREAM_END$2) {
  3511. if (strm.next_out > 0) {
  3512. this.onData(strm.output.subarray(0, strm.next_out));
  3513. }
  3514. status = deflate_1$2.deflateEnd(this.strm);
  3515. this.onEnd(status);
  3516. this.ended = true;
  3517. return status === Z_OK$2;
  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$1.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$1.prototype.onEnd = function (status) {
  3552. // On success - join
  3553. if (status === Z_OK$2) {
  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$1(input, options) {
  3593. var deflator = new Deflate$1(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$1(input, options) {
  3609. options = options || {};
  3610. options.raw = true;
  3611. return deflate$1(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$1(input, options) {
  3622. options = options || {};
  3623. options.gzip = true;
  3624. return deflate$1(input, options);
  3625. }
  3626. var Deflate_1$1 = Deflate$1;
  3627. var deflate_2 = deflate$1;
  3628. var deflateRaw_1$1 = deflateRaw$1;
  3629. var gzip_1$1 = gzip$1;
  3630. var constants$1 = constants$2;
  3631. var deflate_1$1 = {
  3632. Deflate: Deflate_1$1,
  3633. deflate: deflate_2,
  3634. deflateRaw: deflateRaw_1$1,
  3635. gzip: gzip_1$1,
  3636. constants: constants$1
  3637. };
  3638. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3639. //
  3640. // This software is provided 'as-is', without any express or implied
  3641. // warranty. In no event will the authors be held liable for any damages
  3642. // arising from the use of this software.
  3643. //
  3644. // Permission is granted to anyone to use this software for any purpose,
  3645. // including commercial applications, and to alter it and redistribute it
  3646. // freely, subject to the following restrictions:
  3647. //
  3648. // 1. The origin of this software must not be misrepresented; you must not
  3649. // claim that you wrote the original software. If you use this software
  3650. // in a product, an acknowledgment in the product documentation would be
  3651. // appreciated but is not required.
  3652. // 2. Altered source versions must be plainly marked as such, and must not be
  3653. // misrepresented as being the original software.
  3654. // 3. This notice may not be removed or altered from any source distribution.
  3655. // See state defs from inflate.js
  3656. var BAD$1 = 30;
  3657. /* got a data error -- remain here until reset */
  3658. var TYPE$1 = 12;
  3659. /* i: waiting for type bits, including last-flag bit */
  3660. /*
  3661. Decode literal, length, and distance codes and write out the resulting
  3662. literal and match bytes until either not enough input or output is
  3663. available, an end-of-block is encountered, or a data error is encountered.
  3664. When large enough input and output buffers are supplied to inflate(), for
  3665. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  3666. inflate execution time is spent in this routine.
  3667. Entry assumptions:
  3668. state.mode === LEN
  3669. strm.avail_in >= 6
  3670. strm.avail_out >= 258
  3671. start >= strm.avail_out
  3672. state.bits < 8
  3673. On return, state.mode is one of:
  3674. LEN -- ran out of enough output space or enough available input
  3675. TYPE -- reached end of block code, inflate() to interpret next block
  3676. BAD -- error in block data
  3677. Notes:
  3678. - The maximum input bits used by a length/distance pair is 15 bits for the
  3679. length code, 5 bits for the length extra, 15 bits for the distance code,
  3680. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  3681. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  3682. checking for available input while decoding.
  3683. - The maximum bytes that a single length/distance pair can output is 258
  3684. bytes, which is the maximum length that can be coded. inflate_fast()
  3685. requires strm.avail_out >= 258 for each loop to avoid checking for
  3686. output space.
  3687. */
  3688. var inffast = function inflate_fast(strm, start) {
  3689. var _in;
  3690. /* local strm.input */
  3691. var last;
  3692. /* have enough input while in < last */
  3693. var _out;
  3694. /* local strm.output */
  3695. var beg;
  3696. /* inflate()'s initial strm.output */
  3697. var end;
  3698. /* while out < end, enough space available */
  3699. //#ifdef INFLATE_STRICT
  3700. var dmax;
  3701. /* maximum distance from zlib header */
  3702. //#endif
  3703. var wsize;
  3704. /* window size or zero if not using window */
  3705. var whave;
  3706. /* valid bytes in the window */
  3707. var wnext;
  3708. /* window write index */
  3709. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  3710. var s_window;
  3711. /* allocated sliding window, if wsize != 0 */
  3712. var hold;
  3713. /* local strm.hold */
  3714. var bits;
  3715. /* local strm.bits */
  3716. var lcode;
  3717. /* local strm.lencode */
  3718. var dcode;
  3719. /* local strm.distcode */
  3720. var lmask;
  3721. /* mask for first level of length codes */
  3722. var dmask;
  3723. /* mask for first level of distance codes */
  3724. var here;
  3725. /* retrieved table entry */
  3726. var op;
  3727. /* code bits, operation, extra bits, or */
  3728. /* window position, window bytes to copy */
  3729. var len;
  3730. /* match length, unused bytes */
  3731. var dist;
  3732. /* match distance */
  3733. var from;
  3734. /* where to copy match from */
  3735. var from_source;
  3736. var input, output; // JS specific, because we have no pointers
  3737. /* copy state to local variables */
  3738. var state = strm.state; //here = state.here;
  3739. _in = strm.next_in;
  3740. input = strm.input;
  3741. last = _in + (strm.avail_in - 5);
  3742. _out = strm.next_out;
  3743. output = strm.output;
  3744. beg = _out - (start - strm.avail_out);
  3745. end = _out + (strm.avail_out - 257); //#ifdef INFLATE_STRICT
  3746. dmax = state.dmax; //#endif
  3747. wsize = state.wsize;
  3748. whave = state.whave;
  3749. wnext = state.wnext;
  3750. s_window = state.window;
  3751. hold = state.hold;
  3752. bits = state.bits;
  3753. lcode = state.lencode;
  3754. dcode = state.distcode;
  3755. lmask = (1 << state.lenbits) - 1;
  3756. dmask = (1 << state.distbits) - 1;
  3757. /* decode literals and length/distances until end-of-block or not enough
  3758. input data or output space */
  3759. top: do {
  3760. if (bits < 15) {
  3761. hold += input[_in++] << bits;
  3762. bits += 8;
  3763. hold += input[_in++] << bits;
  3764. bits += 8;
  3765. }
  3766. here = lcode[hold & lmask];
  3767. dolen: for (;;) {
  3768. // Goto emulation
  3769. op = here >>> 24
  3770. /*here.bits*/
  3771. ;
  3772. hold >>>= op;
  3773. bits -= op;
  3774. op = here >>> 16 & 0xff
  3775. /*here.op*/
  3776. ;
  3777. if (op === 0) {
  3778. /* literal */
  3779. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  3780. // "inflate: literal '%c'\n" :
  3781. // "inflate: literal 0x%02x\n", here.val));
  3782. output[_out++] = here & 0xffff
  3783. /*here.val*/
  3784. ;
  3785. } else if (op & 16) {
  3786. /* length base */
  3787. len = here & 0xffff
  3788. /*here.val*/
  3789. ;
  3790. op &= 15;
  3791. /* number of extra bits */
  3792. if (op) {
  3793. if (bits < op) {
  3794. hold += input[_in++] << bits;
  3795. bits += 8;
  3796. }
  3797. len += hold & (1 << op) - 1;
  3798. hold >>>= op;
  3799. bits -= op;
  3800. } //Tracevv((stderr, "inflate: length %u\n", len));
  3801. if (bits < 15) {
  3802. hold += input[_in++] << bits;
  3803. bits += 8;
  3804. hold += input[_in++] << bits;
  3805. bits += 8;
  3806. }
  3807. here = dcode[hold & dmask];
  3808. dodist: for (;;) {
  3809. // goto emulation
  3810. op = here >>> 24
  3811. /*here.bits*/
  3812. ;
  3813. hold >>>= op;
  3814. bits -= op;
  3815. op = here >>> 16 & 0xff
  3816. /*here.op*/
  3817. ;
  3818. if (op & 16) {
  3819. /* distance base */
  3820. dist = here & 0xffff
  3821. /*here.val*/
  3822. ;
  3823. op &= 15;
  3824. /* number of extra bits */
  3825. if (bits < op) {
  3826. hold += input[_in++] << bits;
  3827. bits += 8;
  3828. if (bits < op) {
  3829. hold += input[_in++] << bits;
  3830. bits += 8;
  3831. }
  3832. }
  3833. dist += hold & (1 << op) - 1; //#ifdef INFLATE_STRICT
  3834. if (dist > dmax) {
  3835. strm.msg = 'invalid distance too far back';
  3836. state.mode = BAD$1;
  3837. break top;
  3838. } //#endif
  3839. hold >>>= op;
  3840. bits -= op; //Tracevv((stderr, "inflate: distance %u\n", dist));
  3841. op = _out - beg;
  3842. /* max distance in output */
  3843. if (dist > op) {
  3844. /* see if copy from window */
  3845. op = dist - op;
  3846. /* distance back in window */
  3847. if (op > whave) {
  3848. if (state.sane) {
  3849. strm.msg = 'invalid distance too far back';
  3850. state.mode = BAD$1;
  3851. break top;
  3852. } // (!) This block is disabled in zlib defaults,
  3853. // don't enable it for binary compatibility
  3854. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  3855. // if (len <= op - whave) {
  3856. // do {
  3857. // output[_out++] = 0;
  3858. // } while (--len);
  3859. // continue top;
  3860. // }
  3861. // len -= op - whave;
  3862. // do {
  3863. // output[_out++] = 0;
  3864. // } while (--op > whave);
  3865. // if (op === 0) {
  3866. // from = _out - dist;
  3867. // do {
  3868. // output[_out++] = output[from++];
  3869. // } while (--len);
  3870. // continue top;
  3871. // }
  3872. //#endif
  3873. }
  3874. from = 0; // window index
  3875. from_source = s_window;
  3876. if (wnext === 0) {
  3877. /* very common case */
  3878. from += wsize - op;
  3879. if (op < len) {
  3880. /* some from window */
  3881. len -= op;
  3882. do {
  3883. output[_out++] = s_window[from++];
  3884. } while (--op);
  3885. from = _out - dist;
  3886. /* rest from output */
  3887. from_source = output;
  3888. }
  3889. } else if (wnext < op) {
  3890. /* wrap around window */
  3891. from += wsize + wnext - op;
  3892. op -= wnext;
  3893. if (op < len) {
  3894. /* some from end of window */
  3895. len -= op;
  3896. do {
  3897. output[_out++] = s_window[from++];
  3898. } while (--op);
  3899. from = 0;
  3900. if (wnext < len) {
  3901. /* some from start of window */
  3902. op = wnext;
  3903. len -= op;
  3904. do {
  3905. output[_out++] = s_window[from++];
  3906. } while (--op);
  3907. from = _out - dist;
  3908. /* rest from output */
  3909. from_source = output;
  3910. }
  3911. }
  3912. } else {
  3913. /* contiguous in window */
  3914. from += wnext - op;
  3915. if (op < len) {
  3916. /* some from window */
  3917. len -= op;
  3918. do {
  3919. output[_out++] = s_window[from++];
  3920. } while (--op);
  3921. from = _out - dist;
  3922. /* rest from output */
  3923. from_source = output;
  3924. }
  3925. }
  3926. while (len > 2) {
  3927. output[_out++] = from_source[from++];
  3928. output[_out++] = from_source[from++];
  3929. output[_out++] = from_source[from++];
  3930. len -= 3;
  3931. }
  3932. if (len) {
  3933. output[_out++] = from_source[from++];
  3934. if (len > 1) {
  3935. output[_out++] = from_source[from++];
  3936. }
  3937. }
  3938. } else {
  3939. from = _out - dist;
  3940. /* copy direct from output */
  3941. do {
  3942. /* minimum length is three */
  3943. output[_out++] = output[from++];
  3944. output[_out++] = output[from++];
  3945. output[_out++] = output[from++];
  3946. len -= 3;
  3947. } while (len > 2);
  3948. if (len) {
  3949. output[_out++] = output[from++];
  3950. if (len > 1) {
  3951. output[_out++] = output[from++];
  3952. }
  3953. }
  3954. }
  3955. } else if ((op & 64) === 0) {
  3956. /* 2nd level distance code */
  3957. here = dcode[(here & 0xffff) + (hold & (1 << op) - 1)];
  3958. continue dodist;
  3959. } else {
  3960. strm.msg = 'invalid distance code';
  3961. state.mode = BAD$1;
  3962. break top;
  3963. }
  3964. break; // need to emulate goto via "continue"
  3965. }
  3966. } else if ((op & 64) === 0) {
  3967. /* 2nd level length code */
  3968. here = lcode[(here & 0xffff) + (hold & (1 << op) - 1)];
  3969. continue dolen;
  3970. } else if (op & 32) {
  3971. /* end-of-block */
  3972. //Tracevv((stderr, "inflate: end of block\n"));
  3973. state.mode = TYPE$1;
  3974. break top;
  3975. } else {
  3976. strm.msg = 'invalid literal/length code';
  3977. state.mode = BAD$1;
  3978. break top;
  3979. }
  3980. break; // need to emulate goto via "continue"
  3981. }
  3982. } while (_in < last && _out < end);
  3983. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  3984. len = bits >> 3;
  3985. _in -= len;
  3986. bits -= len << 3;
  3987. hold &= (1 << bits) - 1;
  3988. /* update state and return */
  3989. strm.next_in = _in;
  3990. strm.next_out = _out;
  3991. strm.avail_in = _in < last ? 5 + (last - _in) : 5 - (_in - last);
  3992. strm.avail_out = _out < end ? 257 + (end - _out) : 257 - (_out - end);
  3993. state.hold = hold;
  3994. state.bits = bits;
  3995. return;
  3996. };
  3997. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3998. //
  3999. // This software is provided 'as-is', without any express or implied
  4000. // warranty. In no event will the authors be held liable for any damages
  4001. // arising from the use of this software.
  4002. //
  4003. // Permission is granted to anyone to use this software for any purpose,
  4004. // including commercial applications, and to alter it and redistribute it
  4005. // freely, subject to the following restrictions:
  4006. //
  4007. // 1. The origin of this software must not be misrepresented; you must not
  4008. // claim that you wrote the original software. If you use this software
  4009. // in a product, an acknowledgment in the product documentation would be
  4010. // appreciated but is not required.
  4011. // 2. Altered source versions must be plainly marked as such, and must not be
  4012. // misrepresented as being the original software.
  4013. // 3. This notice may not be removed or altered from any source distribution.
  4014. var MAXBITS = 15;
  4015. var ENOUGH_LENS$1 = 852;
  4016. var ENOUGH_DISTS$1 = 592; //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  4017. var CODES$1 = 0;
  4018. var LENS$1 = 1;
  4019. var DISTS$1 = 2;
  4020. var lbase = new Uint16Array([
  4021. /* Length codes 257..285 base */
  4022. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0]);
  4023. var lext = new Uint8Array([
  4024. /* Length codes 257..285 extra */
  4025. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78]);
  4026. var dbase = new Uint16Array([
  4027. /* Distance codes 0..29 base */
  4028. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0]);
  4029. var dext = new Uint8Array([
  4030. /* Distance codes 0..29 extra */
  4031. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 64, 64]);
  4032. var inflate_table = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) {
  4033. var bits = opts.bits; //here = opts.here; /* table entry for duplication */
  4034. var len = 0;
  4035. /* a code's length in bits */
  4036. var sym = 0;
  4037. /* index of code symbols */
  4038. var min = 0,
  4039. max = 0;
  4040. /* minimum and maximum code lengths */
  4041. var root = 0;
  4042. /* number of index bits for root table */
  4043. var curr = 0;
  4044. /* number of index bits for current table */
  4045. var drop = 0;
  4046. /* code bits to drop for sub-table */
  4047. var left = 0;
  4048. /* number of prefix codes available */
  4049. var used = 0;
  4050. /* code entries in table used */
  4051. var huff = 0;
  4052. /* Huffman code */
  4053. var incr;
  4054. /* for incrementing code, index */
  4055. var fill;
  4056. /* index for replicating entries */
  4057. var low;
  4058. /* low bits for current root entry */
  4059. var mask;
  4060. /* mask for low root bits */
  4061. var next;
  4062. /* next available space in table */
  4063. var base = null;
  4064. /* base value table to use */
  4065. var base_index = 0; // let shoextra; /* extra bits table to use */
  4066. var end;
  4067. /* use base and extra for symbol > end */
  4068. var count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  4069. var offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  4070. var extra = null;
  4071. var extra_index = 0;
  4072. var here_bits, here_op, here_val;
  4073. /*
  4074. Process a set of code lengths to create a canonical Huffman code. The
  4075. code lengths are lens[0..codes-1]. Each length corresponds to the
  4076. symbols 0..codes-1. The Huffman code is generated by first sorting the
  4077. symbols by length from short to long, and retaining the symbol order
  4078. for codes with equal lengths. Then the code starts with all zero bits
  4079. for the first code of the shortest length, and the codes are integer
  4080. increments for the same length, and zeros are appended as the length
  4081. increases. For the deflate format, these bits are stored backwards
  4082. from their more natural integer increment ordering, and so when the
  4083. decoding tables are built in the large loop below, the integer codes
  4084. are incremented backwards.
  4085. This routine assumes, but does not check, that all of the entries in
  4086. lens[] are in the range 0..MAXBITS. The caller must assure this.
  4087. 1..MAXBITS is interpreted as that code length. zero means that that
  4088. symbol does not occur in this code.
  4089. The codes are sorted by computing a count of codes for each length,
  4090. creating from that a table of starting indices for each length in the
  4091. sorted table, and then entering the symbols in order in the sorted
  4092. table. The sorted table is work[], with that space being provided by
  4093. the caller.
  4094. The length counts are used for other purposes as well, i.e. finding
  4095. the minimum and maximum length codes, determining if there are any
  4096. codes at all, checking for a valid set of lengths, and looking ahead
  4097. at length counts to determine sub-table sizes when building the
  4098. decoding tables.
  4099. */
  4100. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  4101. for (len = 0; len <= MAXBITS; len++) {
  4102. count[len] = 0;
  4103. }
  4104. for (sym = 0; sym < codes; sym++) {
  4105. count[lens[lens_index + sym]]++;
  4106. }
  4107. /* bound code lengths, force root to be within code lengths */
  4108. root = bits;
  4109. for (max = MAXBITS; max >= 1; max--) {
  4110. if (count[max] !== 0) {
  4111. break;
  4112. }
  4113. }
  4114. if (root > max) {
  4115. root = max;
  4116. }
  4117. if (max === 0) {
  4118. /* no symbols to code at all */
  4119. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  4120. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  4121. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  4122. table[table_index++] = 1 << 24 | 64 << 16 | 0; //table.op[opts.table_index] = 64;
  4123. //table.bits[opts.table_index] = 1;
  4124. //table.val[opts.table_index++] = 0;
  4125. table[table_index++] = 1 << 24 | 64 << 16 | 0;
  4126. opts.bits = 1;
  4127. return 0;
  4128. /* no symbols, but wait for decoding to report error */
  4129. }
  4130. for (min = 1; min < max; min++) {
  4131. if (count[min] !== 0) {
  4132. break;
  4133. }
  4134. }
  4135. if (root < min) {
  4136. root = min;
  4137. }
  4138. /* check for an over-subscribed or incomplete set of lengths */
  4139. left = 1;
  4140. for (len = 1; len <= MAXBITS; len++) {
  4141. left <<= 1;
  4142. left -= count[len];
  4143. if (left < 0) {
  4144. return -1;
  4145. }
  4146. /* over-subscribed */
  4147. }
  4148. if (left > 0 && (type === CODES$1 || max !== 1)) {
  4149. return -1;
  4150. /* incomplete set */
  4151. }
  4152. /* generate offsets into symbol table for each length for sorting */
  4153. offs[1] = 0;
  4154. for (len = 1; len < MAXBITS; len++) {
  4155. offs[len + 1] = offs[len] + count[len];
  4156. }
  4157. /* sort symbols by length, by symbol order within each length */
  4158. for (sym = 0; sym < codes; sym++) {
  4159. if (lens[lens_index + sym] !== 0) {
  4160. work[offs[lens[lens_index + sym]]++] = sym;
  4161. }
  4162. }
  4163. /*
  4164. Create and fill in decoding tables. In this loop, the table being
  4165. filled is at next and has curr index bits. The code being used is huff
  4166. with length len. That code is converted to an index by dropping drop
  4167. bits off of the bottom. For codes where len is less than drop + curr,
  4168. those top drop + curr - len bits are incremented through all values to
  4169. fill the table with replicated entries.
  4170. root is the number of index bits for the root table. When len exceeds
  4171. root, sub-tables are created pointed to by the root entry with an index
  4172. of the low root bits of huff. This is saved in low to check for when a
  4173. new sub-table should be started. drop is zero when the root table is
  4174. being filled, and drop is root when sub-tables are being filled.
  4175. When a new sub-table is needed, it is necessary to look ahead in the
  4176. code lengths to determine what size sub-table is needed. The length
  4177. counts are used for this, and so count[] is decremented as codes are
  4178. entered in the tables.
  4179. used keeps track of how many table entries have been allocated from the
  4180. provided *table space. It is checked for LENS and DIST tables against
  4181. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  4182. the initial root table size constants. See the comments in inftrees.h
  4183. for more information.
  4184. sym increments through all symbols, and the loop terminates when
  4185. all codes of length max, i.e. all codes, have been processed. This
  4186. routine permits incomplete codes, so another loop after this one fills
  4187. in the rest of the decoding tables with invalid code markers.
  4188. */
  4189. /* set up for code type */
  4190. // poor man optimization - use if-else instead of switch,
  4191. // to avoid deopts in old v8
  4192. if (type === CODES$1) {
  4193. base = extra = work;
  4194. /* dummy value--not used */
  4195. end = 19;
  4196. } else if (type === LENS$1) {
  4197. base = lbase;
  4198. base_index -= 257;
  4199. extra = lext;
  4200. extra_index -= 257;
  4201. end = 256;
  4202. } else {
  4203. /* DISTS */
  4204. base = dbase;
  4205. extra = dext;
  4206. end = -1;
  4207. }
  4208. /* initialize opts for loop */
  4209. huff = 0;
  4210. /* starting code */
  4211. sym = 0;
  4212. /* starting code symbol */
  4213. len = min;
  4214. /* starting code length */
  4215. next = table_index;
  4216. /* current table to fill in */
  4217. curr = root;
  4218. /* current table index bits */
  4219. drop = 0;
  4220. /* current bits to drop from code for index */
  4221. low = -1;
  4222. /* trigger new sub-table when len > root */
  4223. used = 1 << root;
  4224. /* use root table entries */
  4225. mask = used - 1;
  4226. /* mask for comparing low */
  4227. /* check available table space */
  4228. if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) {
  4229. return 1;
  4230. }
  4231. /* process all codes and make table entries */
  4232. for (;;) {
  4233. /* create table entry */
  4234. here_bits = len - drop;
  4235. if (work[sym] < end) {
  4236. here_op = 0;
  4237. here_val = work[sym];
  4238. } else if (work[sym] > end) {
  4239. here_op = extra[extra_index + work[sym]];
  4240. here_val = base[base_index + work[sym]];
  4241. } else {
  4242. here_op = 32 + 64;
  4243. /* end of block */
  4244. here_val = 0;
  4245. }
  4246. /* replicate for those indices with low len bits equal to huff */
  4247. incr = 1 << len - drop;
  4248. fill = 1 << curr;
  4249. min = fill;
  4250. /* save offset to next table */
  4251. do {
  4252. fill -= incr;
  4253. table[next + (huff >> drop) + fill] = here_bits << 24 | here_op << 16 | here_val | 0;
  4254. } while (fill !== 0);
  4255. /* backwards increment the len-bit code huff */
  4256. incr = 1 << len - 1;
  4257. while (huff & incr) {
  4258. incr >>= 1;
  4259. }
  4260. if (incr !== 0) {
  4261. huff &= incr - 1;
  4262. huff += incr;
  4263. } else {
  4264. huff = 0;
  4265. }
  4266. /* go to next symbol, update count, len */
  4267. sym++;
  4268. if (--count[len] === 0) {
  4269. if (len === max) {
  4270. break;
  4271. }
  4272. len = lens[lens_index + work[sym]];
  4273. }
  4274. /* create new sub-table if needed */
  4275. if (len > root && (huff & mask) !== low) {
  4276. /* if first time, transition to sub-tables */
  4277. if (drop === 0) {
  4278. drop = root;
  4279. }
  4280. /* increment past last table */
  4281. next += min;
  4282. /* here min is 1 << curr */
  4283. /* determine length of next table */
  4284. curr = len - drop;
  4285. left = 1 << curr;
  4286. while (curr + drop < max) {
  4287. left -= count[curr + drop];
  4288. if (left <= 0) {
  4289. break;
  4290. }
  4291. curr++;
  4292. left <<= 1;
  4293. }
  4294. /* check for enough space */
  4295. used += 1 << curr;
  4296. if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) {
  4297. return 1;
  4298. }
  4299. /* point entry in root table to sub-table */
  4300. low = huff & mask;
  4301. /*table.op[low] = curr;
  4302. table.bits[low] = root;
  4303. table.val[low] = next - opts.table_index;*/
  4304. table[low] = root << 24 | curr << 16 | next - table_index | 0;
  4305. }
  4306. }
  4307. /* fill in remaining table entry if code is incomplete (guaranteed to have
  4308. at most one remaining entry, since if the code is incomplete, the
  4309. maximum code length that was allowed to get this far is one bit) */
  4310. if (huff !== 0) {
  4311. //table.op[next + huff] = 64; /* invalid code marker */
  4312. //table.bits[next + huff] = len - drop;
  4313. //table.val[next + huff] = 0;
  4314. table[next + huff] = len - drop << 24 | 64 << 16 | 0;
  4315. }
  4316. /* set return parameters */
  4317. //opts.table_index += used;
  4318. opts.bits = root;
  4319. return 0;
  4320. };
  4321. var inftrees = inflate_table;
  4322. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  4323. //
  4324. // This software is provided 'as-is', without any express or implied
  4325. // warranty. In no event will the authors be held liable for any damages
  4326. // arising from the use of this software.
  4327. //
  4328. // Permission is granted to anyone to use this software for any purpose,
  4329. // including commercial applications, and to alter it and redistribute it
  4330. // freely, subject to the following restrictions:
  4331. //
  4332. // 1. The origin of this software must not be misrepresented; you must not
  4333. // claim that you wrote the original software. If you use this software
  4334. // in a product, an acknowledgment in the product documentation would be
  4335. // appreciated but is not required.
  4336. // 2. Altered source versions must be plainly marked as such, and must not be
  4337. // misrepresented as being the original software.
  4338. // 3. This notice may not be removed or altered from any source distribution.
  4339. var CODES = 0;
  4340. var LENS = 1;
  4341. var DISTS = 2;
  4342. /* Public constants ==========================================================*/
  4343. /* ===========================================================================*/
  4344. var Z_FINISH$1 = constants$2.Z_FINISH,
  4345. Z_BLOCK = constants$2.Z_BLOCK,
  4346. Z_TREES = constants$2.Z_TREES,
  4347. Z_OK$1 = constants$2.Z_OK,
  4348. Z_STREAM_END$1 = constants$2.Z_STREAM_END,
  4349. Z_NEED_DICT$1 = constants$2.Z_NEED_DICT,
  4350. Z_STREAM_ERROR$1 = constants$2.Z_STREAM_ERROR,
  4351. Z_DATA_ERROR$1 = constants$2.Z_DATA_ERROR,
  4352. Z_MEM_ERROR$1 = constants$2.Z_MEM_ERROR,
  4353. Z_BUF_ERROR = constants$2.Z_BUF_ERROR,
  4354. Z_DEFLATED = constants$2.Z_DEFLATED;
  4355. /* STATES ====================================================================*/
  4356. /* ===========================================================================*/
  4357. var HEAD = 1;
  4358. /* i: waiting for magic header */
  4359. var FLAGS = 2;
  4360. /* i: waiting for method and flags (gzip) */
  4361. var TIME = 3;
  4362. /* i: waiting for modification time (gzip) */
  4363. var OS = 4;
  4364. /* i: waiting for extra flags and operating system (gzip) */
  4365. var EXLEN = 5;
  4366. /* i: waiting for extra length (gzip) */
  4367. var EXTRA = 6;
  4368. /* i: waiting for extra bytes (gzip) */
  4369. var NAME = 7;
  4370. /* i: waiting for end of file name (gzip) */
  4371. var COMMENT = 8;
  4372. /* i: waiting for end of comment (gzip) */
  4373. var HCRC = 9;
  4374. /* i: waiting for header crc (gzip) */
  4375. var DICTID = 10;
  4376. /* i: waiting for dictionary check value */
  4377. var DICT = 11;
  4378. /* waiting for inflateSetDictionary() call */
  4379. var TYPE = 12;
  4380. /* i: waiting for type bits, including last-flag bit */
  4381. var TYPEDO = 13;
  4382. /* i: same, but skip check to exit inflate on new block */
  4383. var STORED = 14;
  4384. /* i: waiting for stored size (length and complement) */
  4385. var COPY_ = 15;
  4386. /* i/o: same as COPY below, but only first time in */
  4387. var COPY = 16;
  4388. /* i/o: waiting for input or output to copy stored block */
  4389. var TABLE = 17;
  4390. /* i: waiting for dynamic block table lengths */
  4391. var LENLENS = 18;
  4392. /* i: waiting for code length code lengths */
  4393. var CODELENS = 19;
  4394. /* i: waiting for length/lit and distance code lengths */
  4395. var LEN_ = 20;
  4396. /* i: same as LEN below, but only first time in */
  4397. var LEN = 21;
  4398. /* i: waiting for length/lit/eob code */
  4399. var LENEXT = 22;
  4400. /* i: waiting for length extra bits */
  4401. var DIST = 23;
  4402. /* i: waiting for distance code */
  4403. var DISTEXT = 24;
  4404. /* i: waiting for distance extra bits */
  4405. var MATCH = 25;
  4406. /* o: waiting for output space to copy string */
  4407. var LIT = 26;
  4408. /* o: waiting for output space to write literal */
  4409. var CHECK = 27;
  4410. /* i: waiting for 32-bit check value */
  4411. var LENGTH = 28;
  4412. /* i: waiting for 32-bit length (gzip) */
  4413. var DONE = 29;
  4414. /* finished check, done -- remain here until reset */
  4415. var BAD = 30;
  4416. /* got a data error -- remain here until reset */
  4417. var MEM = 31;
  4418. /* got an inflate() memory error -- remain here until reset */
  4419. var SYNC = 32;
  4420. /* looking for synchronization bytes to restart inflate() */
  4421. /* ===========================================================================*/
  4422. var ENOUGH_LENS = 852;
  4423. var ENOUGH_DISTS = 592; //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  4424. var MAX_WBITS = 15;
  4425. /* 32K LZ77 window */
  4426. var DEF_WBITS = MAX_WBITS;
  4427. var zswap32 = function zswap32(q) {
  4428. return (q >>> 24 & 0xff) + (q >>> 8 & 0xff00) + ((q & 0xff00) << 8) + ((q & 0xff) << 24);
  4429. };
  4430. function InflateState() {
  4431. this.mode = 0;
  4432. /* current inflate mode */
  4433. this.last = false;
  4434. /* true if processing last block */
  4435. this.wrap = 0;
  4436. /* bit 0 true for zlib, bit 1 true for gzip */
  4437. this.havedict = false;
  4438. /* true if dictionary provided */
  4439. this.flags = 0;
  4440. /* gzip header method and flags (0 if zlib) */
  4441. this.dmax = 0;
  4442. /* zlib header max distance (INFLATE_STRICT) */
  4443. this.check = 0;
  4444. /* protected copy of check value */
  4445. this.total = 0;
  4446. /* protected copy of output count */
  4447. // TODO: may be {}
  4448. this.head = null;
  4449. /* where to save gzip header information */
  4450. /* sliding window */
  4451. this.wbits = 0;
  4452. /* log base 2 of requested window size */
  4453. this.wsize = 0;
  4454. /* window size or zero if not using window */
  4455. this.whave = 0;
  4456. /* valid bytes in the window */
  4457. this.wnext = 0;
  4458. /* window write index */
  4459. this.window = null;
  4460. /* allocated sliding window, if needed */
  4461. /* bit accumulator */
  4462. this.hold = 0;
  4463. /* input bit accumulator */
  4464. this.bits = 0;
  4465. /* number of bits in "in" */
  4466. /* for string and stored block copying */
  4467. this.length = 0;
  4468. /* literal or length of data to copy */
  4469. this.offset = 0;
  4470. /* distance back to copy string from */
  4471. /* for table and code decoding */
  4472. this.extra = 0;
  4473. /* extra bits needed */
  4474. /* fixed and dynamic code tables */
  4475. this.lencode = null;
  4476. /* starting table for length/literal codes */
  4477. this.distcode = null;
  4478. /* starting table for distance codes */
  4479. this.lenbits = 0;
  4480. /* index bits for lencode */
  4481. this.distbits = 0;
  4482. /* index bits for distcode */
  4483. /* dynamic table building */
  4484. this.ncode = 0;
  4485. /* number of code length code lengths */
  4486. this.nlen = 0;
  4487. /* number of length code lengths */
  4488. this.ndist = 0;
  4489. /* number of distance code lengths */
  4490. this.have = 0;
  4491. /* number of code lengths in lens[] */
  4492. this.next = null;
  4493. /* next available space in codes[] */
  4494. this.lens = new Uint16Array(320);
  4495. /* temporary storage for code lengths */
  4496. this.work = new Uint16Array(288);
  4497. /* work area for code table building */
  4498. /*
  4499. because we don't have pointers in js, we use lencode and distcode directly
  4500. as buffers so we don't need codes
  4501. */
  4502. //this.codes = new Int32Array(ENOUGH); /* space for code tables */
  4503. this.lendyn = null;
  4504. /* dynamic table for length/literal codes (JS specific) */
  4505. this.distdyn = null;
  4506. /* dynamic table for distance codes (JS specific) */
  4507. this.sane = 0;
  4508. /* if false, allow invalid distance too far */
  4509. this.back = 0;
  4510. /* bits back of last unprocessed length/lit */
  4511. this.was = 0;
  4512. /* initial length of match */
  4513. }
  4514. var inflateResetKeep = function inflateResetKeep(strm) {
  4515. if (!strm || !strm.state) {
  4516. return Z_STREAM_ERROR$1;
  4517. }
  4518. var state = strm.state;
  4519. strm.total_in = strm.total_out = state.total = 0;
  4520. strm.msg = '';
  4521. /*Z_NULL*/
  4522. if (state.wrap) {
  4523. /* to support ill-conceived Java test suite */
  4524. strm.adler = state.wrap & 1;
  4525. }
  4526. state.mode = HEAD;
  4527. state.last = 0;
  4528. state.havedict = 0;
  4529. state.dmax = 32768;
  4530. state.head = null
  4531. /*Z_NULL*/
  4532. ;
  4533. state.hold = 0;
  4534. state.bits = 0; //state.lencode = state.distcode = state.next = state.codes;
  4535. state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
  4536. state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
  4537. state.sane = 1;
  4538. state.back = -1; //Tracev((stderr, "inflate: reset\n"));
  4539. return Z_OK$1;
  4540. };
  4541. var inflateReset = function inflateReset(strm) {
  4542. if (!strm || !strm.state) {
  4543. return Z_STREAM_ERROR$1;
  4544. }
  4545. var state = strm.state;
  4546. state.wsize = 0;
  4547. state.whave = 0;
  4548. state.wnext = 0;
  4549. return inflateResetKeep(strm);
  4550. };
  4551. var inflateReset2 = function inflateReset2(strm, windowBits) {
  4552. var wrap;
  4553. /* get the state */
  4554. if (!strm || !strm.state) {
  4555. return Z_STREAM_ERROR$1;
  4556. }
  4557. var state = strm.state;
  4558. /* extract wrap request from windowBits parameter */
  4559. if (windowBits < 0) {
  4560. wrap = 0;
  4561. windowBits = -windowBits;
  4562. } else {
  4563. wrap = (windowBits >> 4) + 1;
  4564. if (windowBits < 48) {
  4565. windowBits &= 15;
  4566. }
  4567. }
  4568. /* set number of window bits, free window if different */
  4569. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  4570. return Z_STREAM_ERROR$1;
  4571. }
  4572. if (state.window !== null && state.wbits !== windowBits) {
  4573. state.window = null;
  4574. }
  4575. /* update state and reset the rest of it */
  4576. state.wrap = wrap;
  4577. state.wbits = windowBits;
  4578. return inflateReset(strm);
  4579. };
  4580. var inflateInit2 = function inflateInit2(strm, windowBits) {
  4581. if (!strm) {
  4582. return Z_STREAM_ERROR$1;
  4583. } //strm.msg = Z_NULL; /* in case we return an error */
  4584. var state = new InflateState(); //if (state === Z_NULL) return Z_MEM_ERROR;
  4585. //Tracev((stderr, "inflate: allocated\n"));
  4586. strm.state = state;
  4587. state.window = null
  4588. /*Z_NULL*/
  4589. ;
  4590. var ret = inflateReset2(strm, windowBits);
  4591. if (ret !== Z_OK$1) {
  4592. strm.state = null
  4593. /*Z_NULL*/
  4594. ;
  4595. }
  4596. return ret;
  4597. };
  4598. var inflateInit = function inflateInit(strm) {
  4599. return inflateInit2(strm, DEF_WBITS);
  4600. };
  4601. /*
  4602. Return state with length and distance decoding tables and index sizes set to
  4603. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  4604. If BUILDFIXED is defined, then instead this routine builds the tables the
  4605. first time it's called, and returns those tables the first time and
  4606. thereafter. This reduces the size of the code by about 2K bytes, in
  4607. exchange for a little execution time. However, BUILDFIXED should not be
  4608. used for threaded applications, since the rewriting of the tables and virgin
  4609. may not be thread-safe.
  4610. */
  4611. var virgin = true;
  4612. var lenfix, distfix; // We have no pointers in JS, so keep tables separate
  4613. var fixedtables = function fixedtables(state) {
  4614. /* build fixed huffman tables if first call (may not be thread safe) */
  4615. if (virgin) {
  4616. lenfix = new Int32Array(512);
  4617. distfix = new Int32Array(32);
  4618. /* literal/length table */
  4619. var sym = 0;
  4620. while (sym < 144) {
  4621. state.lens[sym++] = 8;
  4622. }
  4623. while (sym < 256) {
  4624. state.lens[sym++] = 9;
  4625. }
  4626. while (sym < 280) {
  4627. state.lens[sym++] = 7;
  4628. }
  4629. while (sym < 288) {
  4630. state.lens[sym++] = 8;
  4631. }
  4632. inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, {
  4633. bits: 9
  4634. });
  4635. /* distance table */
  4636. sym = 0;
  4637. while (sym < 32) {
  4638. state.lens[sym++] = 5;
  4639. }
  4640. inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, {
  4641. bits: 5
  4642. });
  4643. /* do this just once */
  4644. virgin = false;
  4645. }
  4646. state.lencode = lenfix;
  4647. state.lenbits = 9;
  4648. state.distcode = distfix;
  4649. state.distbits = 5;
  4650. };
  4651. /*
  4652. Update the window with the last wsize (normally 32K) bytes written before
  4653. returning. If window does not exist yet, create it. This is only called
  4654. when a window is already in use, or when output has been written during this
  4655. inflate call, but the end of the deflate stream has not been reached yet.
  4656. It is also called to create a window for dictionary data when a dictionary
  4657. is loaded.
  4658. Providing output buffers larger than 32K to inflate() should provide a speed
  4659. advantage, since only the last 32K of output is copied to the sliding window
  4660. upon return from inflate(), and since all distances after the first 32K of
  4661. output will fall in the output data, making match copies simpler and faster.
  4662. The advantage may be dependent on the size of the processor's data caches.
  4663. */
  4664. var updatewindow = function updatewindow(strm, src, end, copy) {
  4665. var dist;
  4666. var state = strm.state;
  4667. /* if it hasn't been done already, allocate space for the window */
  4668. if (state.window === null) {
  4669. state.wsize = 1 << state.wbits;
  4670. state.wnext = 0;
  4671. state.whave = 0;
  4672. state.window = new Uint8Array(state.wsize);
  4673. }
  4674. /* copy state->wsize or less output bytes into the circular window */
  4675. if (copy >= state.wsize) {
  4676. state.window.set(src.subarray(end - state.wsize, end), 0);
  4677. state.wnext = 0;
  4678. state.whave = state.wsize;
  4679. } else {
  4680. dist = state.wsize - state.wnext;
  4681. if (dist > copy) {
  4682. dist = copy;
  4683. } //zmemcpy(state->window + state->wnext, end - copy, dist);
  4684. state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext);
  4685. copy -= dist;
  4686. if (copy) {
  4687. //zmemcpy(state->window, end - copy, copy);
  4688. state.window.set(src.subarray(end - copy, end), 0);
  4689. state.wnext = copy;
  4690. state.whave = state.wsize;
  4691. } else {
  4692. state.wnext += dist;
  4693. if (state.wnext === state.wsize) {
  4694. state.wnext = 0;
  4695. }
  4696. if (state.whave < state.wsize) {
  4697. state.whave += dist;
  4698. }
  4699. }
  4700. }
  4701. return 0;
  4702. };
  4703. var inflate$2 = function inflate(strm, flush) {
  4704. var state;
  4705. var input, output; // input/output buffers
  4706. var next;
  4707. /* next input INDEX */
  4708. var put;
  4709. /* next output INDEX */
  4710. var have, left;
  4711. /* available input and output */
  4712. var hold;
  4713. /* bit buffer */
  4714. var bits;
  4715. /* bits in bit buffer */
  4716. var _in, _out;
  4717. /* save starting available input and output */
  4718. var copy;
  4719. /* number of stored or match bytes to copy */
  4720. var from;
  4721. /* where to copy match bytes from */
  4722. var from_source;
  4723. var here = 0;
  4724. /* current decoding table entry */
  4725. var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  4726. //let last; /* parent table entry */
  4727. var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  4728. var len;
  4729. /* length to copy for repeats, bits to drop */
  4730. var ret;
  4731. /* return code */
  4732. var hbuf = new Uint8Array(4);
  4733. /* buffer for gzip header crc calculation */
  4734. var opts;
  4735. var n; // temporary variable for NEED_BITS
  4736. var order =
  4737. /* permutation of code lengths */
  4738. new Uint8Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
  4739. if (!strm || !strm.state || !strm.output || !strm.input && strm.avail_in !== 0) {
  4740. return Z_STREAM_ERROR$1;
  4741. }
  4742. state = strm.state;
  4743. if (state.mode === TYPE) {
  4744. state.mode = TYPEDO;
  4745. }
  4746. /* skip check */
  4747. //--- LOAD() ---
  4748. put = strm.next_out;
  4749. output = strm.output;
  4750. left = strm.avail_out;
  4751. next = strm.next_in;
  4752. input = strm.input;
  4753. have = strm.avail_in;
  4754. hold = state.hold;
  4755. bits = state.bits; //---
  4756. _in = have;
  4757. _out = left;
  4758. ret = Z_OK$1;
  4759. inf_leave: // goto emulation
  4760. for (;;) {
  4761. switch (state.mode) {
  4762. case HEAD:
  4763. if (state.wrap === 0) {
  4764. state.mode = TYPEDO;
  4765. break;
  4766. } //=== NEEDBITS(16);
  4767. while (bits < 16) {
  4768. if (have === 0) {
  4769. break inf_leave;
  4770. }
  4771. have--;
  4772. hold += input[next++] << bits;
  4773. bits += 8;
  4774. } //===//
  4775. if (state.wrap & 2 && hold === 0x8b1f) {
  4776. /* gzip header */
  4777. state.check = 0
  4778. /*crc32(0L, Z_NULL, 0)*/
  4779. ; //=== CRC2(state.check, hold);
  4780. hbuf[0] = hold & 0xff;
  4781. hbuf[1] = hold >>> 8 & 0xff;
  4782. state.check = crc32_1(state.check, hbuf, 2, 0); //===//
  4783. //=== INITBITS();
  4784. hold = 0;
  4785. bits = 0; //===//
  4786. state.mode = FLAGS;
  4787. break;
  4788. }
  4789. state.flags = 0;
  4790. /* expect zlib header */
  4791. if (state.head) {
  4792. state.head.done = false;
  4793. }
  4794. if (!(state.wrap & 1) ||
  4795. /* check if zlib header allowed */
  4796. (((hold & 0xff) << 8) + (hold >> 8)) % 31) {
  4797. strm.msg = 'incorrect header check';
  4798. state.mode = BAD;
  4799. break;
  4800. }
  4801. if ((hold & 0x0f) !== Z_DEFLATED) {
  4802. strm.msg = 'unknown compression method';
  4803. state.mode = BAD;
  4804. break;
  4805. } //--- DROPBITS(4) ---//
  4806. hold >>>= 4;
  4807. bits -= 4; //---//
  4808. len = (hold & 0x0f) + 8;
  4809. if (state.wbits === 0) {
  4810. state.wbits = len;
  4811. } else if (len > state.wbits) {
  4812. strm.msg = 'invalid window size';
  4813. state.mode = BAD;
  4814. break;
  4815. } // !!! pako patch. Force use `options.windowBits` if passed.
  4816. // Required to always use max window size by default.
  4817. state.dmax = 1 << state.wbits; //state.dmax = 1 << len;
  4818. //Tracev((stderr, "inflate: zlib header ok\n"));
  4819. strm.adler = state.check = 1
  4820. /*adler32(0L, Z_NULL, 0)*/
  4821. ;
  4822. state.mode = hold & 0x200 ? DICTID : TYPE; //=== INITBITS();
  4823. hold = 0;
  4824. bits = 0; //===//
  4825. break;
  4826. case FLAGS:
  4827. //=== NEEDBITS(16); */
  4828. while (bits < 16) {
  4829. if (have === 0) {
  4830. break inf_leave;
  4831. }
  4832. have--;
  4833. hold += input[next++] << bits;
  4834. bits += 8;
  4835. } //===//
  4836. state.flags = hold;
  4837. if ((state.flags & 0xff) !== Z_DEFLATED) {
  4838. strm.msg = 'unknown compression method';
  4839. state.mode = BAD;
  4840. break;
  4841. }
  4842. if (state.flags & 0xe000) {
  4843. strm.msg = 'unknown header flags set';
  4844. state.mode = BAD;
  4845. break;
  4846. }
  4847. if (state.head) {
  4848. state.head.text = hold >> 8 & 1;
  4849. }
  4850. if (state.flags & 0x0200) {
  4851. //=== CRC2(state.check, hold);
  4852. hbuf[0] = hold & 0xff;
  4853. hbuf[1] = hold >>> 8 & 0xff;
  4854. state.check = crc32_1(state.check, hbuf, 2, 0); //===//
  4855. } //=== INITBITS();
  4856. hold = 0;
  4857. bits = 0; //===//
  4858. state.mode = TIME;
  4859. /* falls through */
  4860. case TIME:
  4861. //=== NEEDBITS(32); */
  4862. while (bits < 32) {
  4863. if (have === 0) {
  4864. break inf_leave;
  4865. }
  4866. have--;
  4867. hold += input[next++] << bits;
  4868. bits += 8;
  4869. } //===//
  4870. if (state.head) {
  4871. state.head.time = hold;
  4872. }
  4873. if (state.flags & 0x0200) {
  4874. //=== CRC4(state.check, hold)
  4875. hbuf[0] = hold & 0xff;
  4876. hbuf[1] = hold >>> 8 & 0xff;
  4877. hbuf[2] = hold >>> 16 & 0xff;
  4878. hbuf[3] = hold >>> 24 & 0xff;
  4879. state.check = crc32_1(state.check, hbuf, 4, 0); //===
  4880. } //=== INITBITS();
  4881. hold = 0;
  4882. bits = 0; //===//
  4883. state.mode = OS;
  4884. /* falls through */
  4885. case OS:
  4886. //=== NEEDBITS(16); */
  4887. while (bits < 16) {
  4888. if (have === 0) {
  4889. break inf_leave;
  4890. }
  4891. have--;
  4892. hold += input[next++] << bits;
  4893. bits += 8;
  4894. } //===//
  4895. if (state.head) {
  4896. state.head.xflags = hold & 0xff;
  4897. state.head.os = hold >> 8;
  4898. }
  4899. if (state.flags & 0x0200) {
  4900. //=== CRC2(state.check, hold);
  4901. hbuf[0] = hold & 0xff;
  4902. hbuf[1] = hold >>> 8 & 0xff;
  4903. state.check = crc32_1(state.check, hbuf, 2, 0); //===//
  4904. } //=== INITBITS();
  4905. hold = 0;
  4906. bits = 0; //===//
  4907. state.mode = EXLEN;
  4908. /* falls through */
  4909. case EXLEN:
  4910. if (state.flags & 0x0400) {
  4911. //=== NEEDBITS(16); */
  4912. while (bits < 16) {
  4913. if (have === 0) {
  4914. break inf_leave;
  4915. }
  4916. have--;
  4917. hold += input[next++] << bits;
  4918. bits += 8;
  4919. } //===//
  4920. state.length = hold;
  4921. if (state.head) {
  4922. state.head.extra_len = hold;
  4923. }
  4924. if (state.flags & 0x0200) {
  4925. //=== CRC2(state.check, hold);
  4926. hbuf[0] = hold & 0xff;
  4927. hbuf[1] = hold >>> 8 & 0xff;
  4928. state.check = crc32_1(state.check, hbuf, 2, 0); //===//
  4929. } //=== INITBITS();
  4930. hold = 0;
  4931. bits = 0; //===//
  4932. } else if (state.head) {
  4933. state.head.extra = null
  4934. /*Z_NULL*/
  4935. ;
  4936. }
  4937. state.mode = EXTRA;
  4938. /* falls through */
  4939. case EXTRA:
  4940. if (state.flags & 0x0400) {
  4941. copy = state.length;
  4942. if (copy > have) {
  4943. copy = have;
  4944. }
  4945. if (copy) {
  4946. if (state.head) {
  4947. len = state.head.extra_len - state.length;
  4948. if (!state.head.extra) {
  4949. // Use untyped array for more convenient processing later
  4950. state.head.extra = new Uint8Array(state.head.extra_len);
  4951. }
  4952. state.head.extra.set(input.subarray(next, // extra field is limited to 65536 bytes
  4953. // - no need for additional size check
  4954. next + copy),
  4955. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  4956. len); //zmemcpy(state.head.extra + len, next,
  4957. // len + copy > state.head.extra_max ?
  4958. // state.head.extra_max - len : copy);
  4959. }
  4960. if (state.flags & 0x0200) {
  4961. state.check = crc32_1(state.check, input, copy, next);
  4962. }
  4963. have -= copy;
  4964. next += copy;
  4965. state.length -= copy;
  4966. }
  4967. if (state.length) {
  4968. break inf_leave;
  4969. }
  4970. }
  4971. state.length = 0;
  4972. state.mode = NAME;
  4973. /* falls through */
  4974. case NAME:
  4975. if (state.flags & 0x0800) {
  4976. if (have === 0) {
  4977. break inf_leave;
  4978. }
  4979. copy = 0;
  4980. do {
  4981. // TODO: 2 or 1 bytes?
  4982. len = input[next + copy++];
  4983. /* use constant limit because in js we should not preallocate memory */
  4984. if (state.head && len && state.length < 65536
  4985. /*state.head.name_max*/
  4986. ) {
  4987. state.head.name += String.fromCharCode(len);
  4988. }
  4989. } while (len && copy < have);
  4990. if (state.flags & 0x0200) {
  4991. state.check = crc32_1(state.check, input, copy, next);
  4992. }
  4993. have -= copy;
  4994. next += copy;
  4995. if (len) {
  4996. break inf_leave;
  4997. }
  4998. } else if (state.head) {
  4999. state.head.name = null;
  5000. }
  5001. state.length = 0;
  5002. state.mode = COMMENT;
  5003. /* falls through */
  5004. case COMMENT:
  5005. if (state.flags & 0x1000) {
  5006. if (have === 0) {
  5007. break inf_leave;
  5008. }
  5009. copy = 0;
  5010. do {
  5011. len = input[next + copy++];
  5012. /* use constant limit because in js we should not preallocate memory */
  5013. if (state.head && len && state.length < 65536
  5014. /*state.head.comm_max*/
  5015. ) {
  5016. state.head.comment += String.fromCharCode(len);
  5017. }
  5018. } while (len && copy < have);
  5019. if (state.flags & 0x0200) {
  5020. state.check = crc32_1(state.check, input, copy, next);
  5021. }
  5022. have -= copy;
  5023. next += copy;
  5024. if (len) {
  5025. break inf_leave;
  5026. }
  5027. } else if (state.head) {
  5028. state.head.comment = null;
  5029. }
  5030. state.mode = HCRC;
  5031. /* falls through */
  5032. case HCRC:
  5033. if (state.flags & 0x0200) {
  5034. //=== NEEDBITS(16); */
  5035. while (bits < 16) {
  5036. if (have === 0) {
  5037. break inf_leave;
  5038. }
  5039. have--;
  5040. hold += input[next++] << bits;
  5041. bits += 8;
  5042. } //===//
  5043. if (hold !== (state.check & 0xffff)) {
  5044. strm.msg = 'header crc mismatch';
  5045. state.mode = BAD;
  5046. break;
  5047. } //=== INITBITS();
  5048. hold = 0;
  5049. bits = 0; //===//
  5050. }
  5051. if (state.head) {
  5052. state.head.hcrc = state.flags >> 9 & 1;
  5053. state.head.done = true;
  5054. }
  5055. strm.adler = state.check = 0;
  5056. state.mode = TYPE;
  5057. break;
  5058. case DICTID:
  5059. //=== NEEDBITS(32); */
  5060. while (bits < 32) {
  5061. if (have === 0) {
  5062. break inf_leave;
  5063. }
  5064. have--;
  5065. hold += input[next++] << bits;
  5066. bits += 8;
  5067. } //===//
  5068. strm.adler = state.check = zswap32(hold); //=== INITBITS();
  5069. hold = 0;
  5070. bits = 0; //===//
  5071. state.mode = DICT;
  5072. /* falls through */
  5073. case DICT:
  5074. if (state.havedict === 0) {
  5075. //--- RESTORE() ---
  5076. strm.next_out = put;
  5077. strm.avail_out = left;
  5078. strm.next_in = next;
  5079. strm.avail_in = have;
  5080. state.hold = hold;
  5081. state.bits = bits; //---
  5082. return Z_NEED_DICT$1;
  5083. }
  5084. strm.adler = state.check = 1
  5085. /*adler32(0L, Z_NULL, 0)*/
  5086. ;
  5087. state.mode = TYPE;
  5088. /* falls through */
  5089. case TYPE:
  5090. if (flush === Z_BLOCK || flush === Z_TREES) {
  5091. break inf_leave;
  5092. }
  5093. /* falls through */
  5094. case TYPEDO:
  5095. if (state.last) {
  5096. //--- BYTEBITS() ---//
  5097. hold >>>= bits & 7;
  5098. bits -= bits & 7; //---//
  5099. state.mode = CHECK;
  5100. break;
  5101. } //=== NEEDBITS(3); */
  5102. while (bits < 3) {
  5103. if (have === 0) {
  5104. break inf_leave;
  5105. }
  5106. have--;
  5107. hold += input[next++] << bits;
  5108. bits += 8;
  5109. } //===//
  5110. state.last = hold & 0x01
  5111. /*BITS(1)*/
  5112. ; //--- DROPBITS(1) ---//
  5113. hold >>>= 1;
  5114. bits -= 1; //---//
  5115. switch (hold & 0x03) {
  5116. case 0:
  5117. /* stored block */
  5118. //Tracev((stderr, "inflate: stored block%s\n",
  5119. // state.last ? " (last)" : ""));
  5120. state.mode = STORED;
  5121. break;
  5122. case 1:
  5123. /* fixed block */
  5124. fixedtables(state); //Tracev((stderr, "inflate: fixed codes block%s\n",
  5125. // state.last ? " (last)" : ""));
  5126. state.mode = LEN_;
  5127. /* decode codes */
  5128. if (flush === Z_TREES) {
  5129. //--- DROPBITS(2) ---//
  5130. hold >>>= 2;
  5131. bits -= 2; //---//
  5132. break inf_leave;
  5133. }
  5134. break;
  5135. case 2:
  5136. /* dynamic block */
  5137. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  5138. // state.last ? " (last)" : ""));
  5139. state.mode = TABLE;
  5140. break;
  5141. case 3:
  5142. strm.msg = 'invalid block type';
  5143. state.mode = BAD;
  5144. } //--- DROPBITS(2) ---//
  5145. hold >>>= 2;
  5146. bits -= 2; //---//
  5147. break;
  5148. case STORED:
  5149. //--- BYTEBITS() ---// /* go to byte boundary */
  5150. hold >>>= bits & 7;
  5151. bits -= bits & 7; //---//
  5152. //=== NEEDBITS(32); */
  5153. while (bits < 32) {
  5154. if (have === 0) {
  5155. break inf_leave;
  5156. }
  5157. have--;
  5158. hold += input[next++] << bits;
  5159. bits += 8;
  5160. } //===//
  5161. if ((hold & 0xffff) !== (hold >>> 16 ^ 0xffff)) {
  5162. strm.msg = 'invalid stored block lengths';
  5163. state.mode = BAD;
  5164. break;
  5165. }
  5166. state.length = hold & 0xffff; //Tracev((stderr, "inflate: stored length %u\n",
  5167. // state.length));
  5168. //=== INITBITS();
  5169. hold = 0;
  5170. bits = 0; //===//
  5171. state.mode = COPY_;
  5172. if (flush === Z_TREES) {
  5173. break inf_leave;
  5174. }
  5175. /* falls through */
  5176. case COPY_:
  5177. state.mode = COPY;
  5178. /* falls through */
  5179. case COPY:
  5180. copy = state.length;
  5181. if (copy) {
  5182. if (copy > have) {
  5183. copy = have;
  5184. }
  5185. if (copy > left) {
  5186. copy = left;
  5187. }
  5188. if (copy === 0) {
  5189. break inf_leave;
  5190. } //--- zmemcpy(put, next, copy); ---
  5191. output.set(input.subarray(next, next + copy), put); //---//
  5192. have -= copy;
  5193. next += copy;
  5194. left -= copy;
  5195. put += copy;
  5196. state.length -= copy;
  5197. break;
  5198. } //Tracev((stderr, "inflate: stored end\n"));
  5199. state.mode = TYPE;
  5200. break;
  5201. case TABLE:
  5202. //=== NEEDBITS(14); */
  5203. while (bits < 14) {
  5204. if (have === 0) {
  5205. break inf_leave;
  5206. }
  5207. have--;
  5208. hold += input[next++] << bits;
  5209. bits += 8;
  5210. } //===//
  5211. state.nlen = (hold & 0x1f) + 257; //--- DROPBITS(5) ---//
  5212. hold >>>= 5;
  5213. bits -= 5; //---//
  5214. state.ndist = (hold & 0x1f) + 1; //--- DROPBITS(5) ---//
  5215. hold >>>= 5;
  5216. bits -= 5; //---//
  5217. state.ncode = (hold & 0x0f) + 4; //--- DROPBITS(4) ---//
  5218. hold >>>= 4;
  5219. bits -= 4; //---//
  5220. //#ifndef PKZIP_BUG_WORKAROUND
  5221. if (state.nlen > 286 || state.ndist > 30) {
  5222. strm.msg = 'too many length or distance symbols';
  5223. state.mode = BAD;
  5224. break;
  5225. } //#endif
  5226. //Tracev((stderr, "inflate: table sizes ok\n"));
  5227. state.have = 0;
  5228. state.mode = LENLENS;
  5229. /* falls through */
  5230. case LENLENS:
  5231. while (state.have < state.ncode) {
  5232. //=== NEEDBITS(3);
  5233. while (bits < 3) {
  5234. if (have === 0) {
  5235. break inf_leave;
  5236. }
  5237. have--;
  5238. hold += input[next++] << bits;
  5239. bits += 8;
  5240. } //===//
  5241. state.lens[order[state.have++]] = hold & 0x07; //BITS(3);
  5242. //--- DROPBITS(3) ---//
  5243. hold >>>= 3;
  5244. bits -= 3; //---//
  5245. }
  5246. while (state.have < 19) {
  5247. state.lens[order[state.have++]] = 0;
  5248. } // We have separate tables & no pointers. 2 commented lines below not needed.
  5249. //state.next = state.codes;
  5250. //state.lencode = state.next;
  5251. // Switch to use dynamic table
  5252. state.lencode = state.lendyn;
  5253. state.lenbits = 7;
  5254. opts = {
  5255. bits: state.lenbits
  5256. };
  5257. ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  5258. state.lenbits = opts.bits;
  5259. if (ret) {
  5260. strm.msg = 'invalid code lengths set';
  5261. state.mode = BAD;
  5262. break;
  5263. } //Tracev((stderr, "inflate: code lengths ok\n"));
  5264. state.have = 0;
  5265. state.mode = CODELENS;
  5266. /* falls through */
  5267. case CODELENS:
  5268. while (state.have < state.nlen + state.ndist) {
  5269. for (;;) {
  5270. here = state.lencode[hold & (1 << state.lenbits) - 1];
  5271. /*BITS(state.lenbits)*/
  5272. here_bits = here >>> 24;
  5273. here_op = here >>> 16 & 0xff;
  5274. here_val = here & 0xffff;
  5275. if (here_bits <= bits) {
  5276. break;
  5277. } //--- PULLBYTE() ---//
  5278. if (have === 0) {
  5279. break inf_leave;
  5280. }
  5281. have--;
  5282. hold += input[next++] << bits;
  5283. bits += 8; //---//
  5284. }
  5285. if (here_val < 16) {
  5286. //--- DROPBITS(here.bits) ---//
  5287. hold >>>= here_bits;
  5288. bits -= here_bits; //---//
  5289. state.lens[state.have++] = here_val;
  5290. } else {
  5291. if (here_val === 16) {
  5292. //=== NEEDBITS(here.bits + 2);
  5293. n = here_bits + 2;
  5294. while (bits < n) {
  5295. if (have === 0) {
  5296. break inf_leave;
  5297. }
  5298. have--;
  5299. hold += input[next++] << bits;
  5300. bits += 8;
  5301. } //===//
  5302. //--- DROPBITS(here.bits) ---//
  5303. hold >>>= here_bits;
  5304. bits -= here_bits; //---//
  5305. if (state.have === 0) {
  5306. strm.msg = 'invalid bit length repeat';
  5307. state.mode = BAD;
  5308. break;
  5309. }
  5310. len = state.lens[state.have - 1];
  5311. copy = 3 + (hold & 0x03); //BITS(2);
  5312. //--- DROPBITS(2) ---//
  5313. hold >>>= 2;
  5314. bits -= 2; //---//
  5315. } else if (here_val === 17) {
  5316. //=== NEEDBITS(here.bits + 3);
  5317. n = here_bits + 3;
  5318. while (bits < n) {
  5319. if (have === 0) {
  5320. break inf_leave;
  5321. }
  5322. have--;
  5323. hold += input[next++] << bits;
  5324. bits += 8;
  5325. } //===//
  5326. //--- DROPBITS(here.bits) ---//
  5327. hold >>>= here_bits;
  5328. bits -= here_bits; //---//
  5329. len = 0;
  5330. copy = 3 + (hold & 0x07); //BITS(3);
  5331. //--- DROPBITS(3) ---//
  5332. hold >>>= 3;
  5333. bits -= 3; //---//
  5334. } else {
  5335. //=== NEEDBITS(here.bits + 7);
  5336. n = here_bits + 7;
  5337. while (bits < n) {
  5338. if (have === 0) {
  5339. break inf_leave;
  5340. }
  5341. have--;
  5342. hold += input[next++] << bits;
  5343. bits += 8;
  5344. } //===//
  5345. //--- DROPBITS(here.bits) ---//
  5346. hold >>>= here_bits;
  5347. bits -= here_bits; //---//
  5348. len = 0;
  5349. copy = 11 + (hold & 0x7f); //BITS(7);
  5350. //--- DROPBITS(7) ---//
  5351. hold >>>= 7;
  5352. bits -= 7; //---//
  5353. }
  5354. if (state.have + copy > state.nlen + state.ndist) {
  5355. strm.msg = 'invalid bit length repeat';
  5356. state.mode = BAD;
  5357. break;
  5358. }
  5359. while (copy--) {
  5360. state.lens[state.have++] = len;
  5361. }
  5362. }
  5363. }
  5364. /* handle error breaks in while */
  5365. if (state.mode === BAD) {
  5366. break;
  5367. }
  5368. /* check for end-of-block code (better have one) */
  5369. if (state.lens[256] === 0) {
  5370. strm.msg = 'invalid code -- missing end-of-block';
  5371. state.mode = BAD;
  5372. break;
  5373. }
  5374. /* build code tables -- note: do not change the lenbits or distbits
  5375. values here (9 and 6) without reading the comments in inftrees.h
  5376. concerning the ENOUGH constants, which depend on those values */
  5377. state.lenbits = 9;
  5378. opts = {
  5379. bits: state.lenbits
  5380. };
  5381. ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed.
  5382. // state.next_index = opts.table_index;
  5383. state.lenbits = opts.bits; // state.lencode = state.next;
  5384. if (ret) {
  5385. strm.msg = 'invalid literal/lengths set';
  5386. state.mode = BAD;
  5387. break;
  5388. }
  5389. state.distbits = 6; //state.distcode.copy(state.codes);
  5390. // Switch to use dynamic table
  5391. state.distcode = state.distdyn;
  5392. opts = {
  5393. bits: state.distbits
  5394. };
  5395. ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed.
  5396. // state.next_index = opts.table_index;
  5397. state.distbits = opts.bits; // state.distcode = state.next;
  5398. if (ret) {
  5399. strm.msg = 'invalid distances set';
  5400. state.mode = BAD;
  5401. break;
  5402. } //Tracev((stderr, 'inflate: codes ok\n'));
  5403. state.mode = LEN_;
  5404. if (flush === Z_TREES) {
  5405. break inf_leave;
  5406. }
  5407. /* falls through */
  5408. case LEN_:
  5409. state.mode = LEN;
  5410. /* falls through */
  5411. case LEN:
  5412. if (have >= 6 && left >= 258) {
  5413. //--- RESTORE() ---
  5414. strm.next_out = put;
  5415. strm.avail_out = left;
  5416. strm.next_in = next;
  5417. strm.avail_in = have;
  5418. state.hold = hold;
  5419. state.bits = bits; //---
  5420. inffast(strm, _out); //--- LOAD() ---
  5421. put = strm.next_out;
  5422. output = strm.output;
  5423. left = strm.avail_out;
  5424. next = strm.next_in;
  5425. input = strm.input;
  5426. have = strm.avail_in;
  5427. hold = state.hold;
  5428. bits = state.bits; //---
  5429. if (state.mode === TYPE) {
  5430. state.back = -1;
  5431. }
  5432. break;
  5433. }
  5434. state.back = 0;
  5435. for (;;) {
  5436. here = state.lencode[hold & (1 << state.lenbits) - 1];
  5437. /*BITS(state.lenbits)*/
  5438. here_bits = here >>> 24;
  5439. here_op = here >>> 16 & 0xff;
  5440. here_val = here & 0xffff;
  5441. if (here_bits <= bits) {
  5442. break;
  5443. } //--- PULLBYTE() ---//
  5444. if (have === 0) {
  5445. break inf_leave;
  5446. }
  5447. have--;
  5448. hold += input[next++] << bits;
  5449. bits += 8; //---//
  5450. }
  5451. if (here_op && (here_op & 0xf0) === 0) {
  5452. last_bits = here_bits;
  5453. last_op = here_op;
  5454. last_val = here_val;
  5455. for (;;) {
  5456. here = state.lencode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)];
  5457. here_bits = here >>> 24;
  5458. here_op = here >>> 16 & 0xff;
  5459. here_val = here & 0xffff;
  5460. if (last_bits + here_bits <= bits) {
  5461. break;
  5462. } //--- PULLBYTE() ---//
  5463. if (have === 0) {
  5464. break inf_leave;
  5465. }
  5466. have--;
  5467. hold += input[next++] << bits;
  5468. bits += 8; //---//
  5469. } //--- DROPBITS(last.bits) ---//
  5470. hold >>>= last_bits;
  5471. bits -= last_bits; //---//
  5472. state.back += last_bits;
  5473. } //--- DROPBITS(here.bits) ---//
  5474. hold >>>= here_bits;
  5475. bits -= here_bits; //---//
  5476. state.back += here_bits;
  5477. state.length = here_val;
  5478. if (here_op === 0) {
  5479. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  5480. // "inflate: literal '%c'\n" :
  5481. // "inflate: literal 0x%02x\n", here.val));
  5482. state.mode = LIT;
  5483. break;
  5484. }
  5485. if (here_op & 32) {
  5486. //Tracevv((stderr, "inflate: end of block\n"));
  5487. state.back = -1;
  5488. state.mode = TYPE;
  5489. break;
  5490. }
  5491. if (here_op & 64) {
  5492. strm.msg = 'invalid literal/length code';
  5493. state.mode = BAD;
  5494. break;
  5495. }
  5496. state.extra = here_op & 15;
  5497. state.mode = LENEXT;
  5498. /* falls through */
  5499. case LENEXT:
  5500. if (state.extra) {
  5501. //=== NEEDBITS(state.extra);
  5502. n = state.extra;
  5503. while (bits < n) {
  5504. if (have === 0) {
  5505. break inf_leave;
  5506. }
  5507. have--;
  5508. hold += input[next++] << bits;
  5509. bits += 8;
  5510. } //===//
  5511. state.length += hold & (1 << state.extra) - 1
  5512. /*BITS(state.extra)*/
  5513. ; //--- DROPBITS(state.extra) ---//
  5514. hold >>>= state.extra;
  5515. bits -= state.extra; //---//
  5516. state.back += state.extra;
  5517. } //Tracevv((stderr, "inflate: length %u\n", state.length));
  5518. state.was = state.length;
  5519. state.mode = DIST;
  5520. /* falls through */
  5521. case DIST:
  5522. for (;;) {
  5523. here = state.distcode[hold & (1 << state.distbits) - 1];
  5524. /*BITS(state.distbits)*/
  5525. here_bits = here >>> 24;
  5526. here_op = here >>> 16 & 0xff;
  5527. here_val = here & 0xffff;
  5528. if (here_bits <= bits) {
  5529. break;
  5530. } //--- PULLBYTE() ---//
  5531. if (have === 0) {
  5532. break inf_leave;
  5533. }
  5534. have--;
  5535. hold += input[next++] << bits;
  5536. bits += 8; //---//
  5537. }
  5538. if ((here_op & 0xf0) === 0) {
  5539. last_bits = here_bits;
  5540. last_op = here_op;
  5541. last_val = here_val;
  5542. for (;;) {
  5543. here = state.distcode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)];
  5544. here_bits = here >>> 24;
  5545. here_op = here >>> 16 & 0xff;
  5546. here_val = here & 0xffff;
  5547. if (last_bits + here_bits <= bits) {
  5548. break;
  5549. } //--- PULLBYTE() ---//
  5550. if (have === 0) {
  5551. break inf_leave;
  5552. }
  5553. have--;
  5554. hold += input[next++] << bits;
  5555. bits += 8; //---//
  5556. } //--- DROPBITS(last.bits) ---//
  5557. hold >>>= last_bits;
  5558. bits -= last_bits; //---//
  5559. state.back += last_bits;
  5560. } //--- DROPBITS(here.bits) ---//
  5561. hold >>>= here_bits;
  5562. bits -= here_bits; //---//
  5563. state.back += here_bits;
  5564. if (here_op & 64) {
  5565. strm.msg = 'invalid distance code';
  5566. state.mode = BAD;
  5567. break;
  5568. }
  5569. state.offset = here_val;
  5570. state.extra = here_op & 15;
  5571. state.mode = DISTEXT;
  5572. /* falls through */
  5573. case DISTEXT:
  5574. if (state.extra) {
  5575. //=== NEEDBITS(state.extra);
  5576. n = state.extra;
  5577. while (bits < n) {
  5578. if (have === 0) {
  5579. break inf_leave;
  5580. }
  5581. have--;
  5582. hold += input[next++] << bits;
  5583. bits += 8;
  5584. } //===//
  5585. state.offset += hold & (1 << state.extra) - 1
  5586. /*BITS(state.extra)*/
  5587. ; //--- DROPBITS(state.extra) ---//
  5588. hold >>>= state.extra;
  5589. bits -= state.extra; //---//
  5590. state.back += state.extra;
  5591. } //#ifdef INFLATE_STRICT
  5592. if (state.offset > state.dmax) {
  5593. strm.msg = 'invalid distance too far back';
  5594. state.mode = BAD;
  5595. break;
  5596. } //#endif
  5597. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  5598. state.mode = MATCH;
  5599. /* falls through */
  5600. case MATCH:
  5601. if (left === 0) {
  5602. break inf_leave;
  5603. }
  5604. copy = _out - left;
  5605. if (state.offset > copy) {
  5606. /* copy from window */
  5607. copy = state.offset - copy;
  5608. if (copy > state.whave) {
  5609. if (state.sane) {
  5610. strm.msg = 'invalid distance too far back';
  5611. state.mode = BAD;
  5612. break;
  5613. } // (!) This block is disabled in zlib defaults,
  5614. // don't enable it for binary compatibility
  5615. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  5616. // Trace((stderr, "inflate.c too far\n"));
  5617. // copy -= state.whave;
  5618. // if (copy > state.length) { copy = state.length; }
  5619. // if (copy > left) { copy = left; }
  5620. // left -= copy;
  5621. // state.length -= copy;
  5622. // do {
  5623. // output[put++] = 0;
  5624. // } while (--copy);
  5625. // if (state.length === 0) { state.mode = LEN; }
  5626. // break;
  5627. //#endif
  5628. }
  5629. if (copy > state.wnext) {
  5630. copy -= state.wnext;
  5631. from = state.wsize - copy;
  5632. } else {
  5633. from = state.wnext - copy;
  5634. }
  5635. if (copy > state.length) {
  5636. copy = state.length;
  5637. }
  5638. from_source = state.window;
  5639. } else {
  5640. /* copy from output */
  5641. from_source = output;
  5642. from = put - state.offset;
  5643. copy = state.length;
  5644. }
  5645. if (copy > left) {
  5646. copy = left;
  5647. }
  5648. left -= copy;
  5649. state.length -= copy;
  5650. do {
  5651. output[put++] = from_source[from++];
  5652. } while (--copy);
  5653. if (state.length === 0) {
  5654. state.mode = LEN;
  5655. }
  5656. break;
  5657. case LIT:
  5658. if (left === 0) {
  5659. break inf_leave;
  5660. }
  5661. output[put++] = state.length;
  5662. left--;
  5663. state.mode = LEN;
  5664. break;
  5665. case CHECK:
  5666. if (state.wrap) {
  5667. //=== NEEDBITS(32);
  5668. while (bits < 32) {
  5669. if (have === 0) {
  5670. break inf_leave;
  5671. }
  5672. have--; // Use '|' instead of '+' to make sure that result is signed
  5673. hold |= input[next++] << bits;
  5674. bits += 8;
  5675. } //===//
  5676. _out -= left;
  5677. strm.total_out += _out;
  5678. state.total += _out;
  5679. if (_out) {
  5680. strm.adler = state.check = state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out);
  5681. }
  5682. _out = left; // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  5683. if ((state.flags ? hold : zswap32(hold)) !== state.check) {
  5684. strm.msg = 'incorrect data check';
  5685. state.mode = BAD;
  5686. break;
  5687. } //=== INITBITS();
  5688. hold = 0;
  5689. bits = 0; //===//
  5690. //Tracev((stderr, "inflate: check matches trailer\n"));
  5691. }
  5692. state.mode = LENGTH;
  5693. /* falls through */
  5694. case LENGTH:
  5695. if (state.wrap && state.flags) {
  5696. //=== NEEDBITS(32);
  5697. while (bits < 32) {
  5698. if (have === 0) {
  5699. break inf_leave;
  5700. }
  5701. have--;
  5702. hold += input[next++] << bits;
  5703. bits += 8;
  5704. } //===//
  5705. if (hold !== (state.total & 0xffffffff)) {
  5706. strm.msg = 'incorrect length check';
  5707. state.mode = BAD;
  5708. break;
  5709. } //=== INITBITS();
  5710. hold = 0;
  5711. bits = 0; //===//
  5712. //Tracev((stderr, "inflate: length matches trailer\n"));
  5713. }
  5714. state.mode = DONE;
  5715. /* falls through */
  5716. case DONE:
  5717. ret = Z_STREAM_END$1;
  5718. break inf_leave;
  5719. case BAD:
  5720. ret = Z_DATA_ERROR$1;
  5721. break inf_leave;
  5722. case MEM:
  5723. return Z_MEM_ERROR$1;
  5724. case SYNC:
  5725. /* falls through */
  5726. default:
  5727. return Z_STREAM_ERROR$1;
  5728. }
  5729. } // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  5730. /*
  5731. Return from inflate(), updating the total counts and the check value.
  5732. If there was no progress during the inflate() call, return a buffer
  5733. error. Call updatewindow() to create and/or update the window state.
  5734. Note: a memory error from inflate() is non-recoverable.
  5735. */
  5736. //--- RESTORE() ---
  5737. strm.next_out = put;
  5738. strm.avail_out = left;
  5739. strm.next_in = next;
  5740. strm.avail_in = have;
  5741. state.hold = hold;
  5742. state.bits = bits; //---
  5743. if (state.wsize || _out !== strm.avail_out && state.mode < BAD && (state.mode < CHECK || flush !== Z_FINISH$1)) {
  5744. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ;
  5745. }
  5746. _in -= strm.avail_in;
  5747. _out -= strm.avail_out;
  5748. strm.total_in += _in;
  5749. strm.total_out += _out;
  5750. state.total += _out;
  5751. if (state.wrap && _out) {
  5752. strm.adler = state.check = state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out);
  5753. }
  5754. strm.data_type = state.bits + (state.last ? 64 : 0) + (state.mode === TYPE ? 128 : 0) + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  5755. if ((_in === 0 && _out === 0 || flush === Z_FINISH$1) && ret === Z_OK$1) {
  5756. ret = Z_BUF_ERROR;
  5757. }
  5758. return ret;
  5759. };
  5760. var inflateEnd = function inflateEnd(strm) {
  5761. if (!strm || !strm.state
  5762. /*|| strm->zfree == (free_func)0*/
  5763. ) {
  5764. return Z_STREAM_ERROR$1;
  5765. }
  5766. var state = strm.state;
  5767. if (state.window) {
  5768. state.window = null;
  5769. }
  5770. strm.state = null;
  5771. return Z_OK$1;
  5772. };
  5773. var inflateGetHeader = function inflateGetHeader(strm, head) {
  5774. /* check state */
  5775. if (!strm || !strm.state) {
  5776. return Z_STREAM_ERROR$1;
  5777. }
  5778. var state = strm.state;
  5779. if ((state.wrap & 2) === 0) {
  5780. return Z_STREAM_ERROR$1;
  5781. }
  5782. /* save header structure */
  5783. state.head = head;
  5784. head.done = false;
  5785. return Z_OK$1;
  5786. };
  5787. var inflateSetDictionary = function inflateSetDictionary(strm, dictionary) {
  5788. var dictLength = dictionary.length;
  5789. var state;
  5790. var dictid;
  5791. var ret;
  5792. /* check state */
  5793. if (!strm
  5794. /* == Z_NULL */
  5795. || !strm.state
  5796. /* == Z_NULL */
  5797. ) {
  5798. return Z_STREAM_ERROR$1;
  5799. }
  5800. state = strm.state;
  5801. if (state.wrap !== 0 && state.mode !== DICT) {
  5802. return Z_STREAM_ERROR$1;
  5803. }
  5804. /* check for correct dictionary identifier */
  5805. if (state.mode === DICT) {
  5806. dictid = 1;
  5807. /* adler32(0, null, 0)*/
  5808. /* dictid = adler32(dictid, dictionary, dictLength); */
  5809. dictid = adler32_1(dictid, dictionary, dictLength, 0);
  5810. if (dictid !== state.check) {
  5811. return Z_DATA_ERROR$1;
  5812. }
  5813. }
  5814. /* copy dictionary to window using updatewindow(), which will amend the
  5815. existing dictionary if appropriate */
  5816. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  5817. if (ret) {
  5818. state.mode = MEM;
  5819. return Z_MEM_ERROR$1;
  5820. }
  5821. state.havedict = 1; // Tracev((stderr, "inflate: dictionary set\n"));
  5822. return Z_OK$1;
  5823. };
  5824. var inflateReset_1 = inflateReset;
  5825. var inflateReset2_1 = inflateReset2;
  5826. var inflateResetKeep_1 = inflateResetKeep;
  5827. var inflateInit_1 = inflateInit;
  5828. var inflateInit2_1 = inflateInit2;
  5829. var inflate_2$1 = inflate$2;
  5830. var inflateEnd_1 = inflateEnd;
  5831. var inflateGetHeader_1 = inflateGetHeader;
  5832. var inflateSetDictionary_1 = inflateSetDictionary;
  5833. var inflateInfo = 'pako inflate (from Nodeca project)';
  5834. /* Not implemented
  5835. module.exports.inflateCopy = inflateCopy;
  5836. module.exports.inflateGetDictionary = inflateGetDictionary;
  5837. module.exports.inflateMark = inflateMark;
  5838. module.exports.inflatePrime = inflatePrime;
  5839. module.exports.inflateSync = inflateSync;
  5840. module.exports.inflateSyncPoint = inflateSyncPoint;
  5841. module.exports.inflateUndermine = inflateUndermine;
  5842. */
  5843. var inflate_1$2 = {
  5844. inflateReset: inflateReset_1,
  5845. inflateReset2: inflateReset2_1,
  5846. inflateResetKeep: inflateResetKeep_1,
  5847. inflateInit: inflateInit_1,
  5848. inflateInit2: inflateInit2_1,
  5849. inflate: inflate_2$1,
  5850. inflateEnd: inflateEnd_1,
  5851. inflateGetHeader: inflateGetHeader_1,
  5852. inflateSetDictionary: inflateSetDictionary_1,
  5853. inflateInfo: inflateInfo
  5854. };
  5855. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  5856. //
  5857. // This software is provided 'as-is', without any express or implied
  5858. // warranty. In no event will the authors be held liable for any damages
  5859. // arising from the use of this software.
  5860. //
  5861. // Permission is granted to anyone to use this software for any purpose,
  5862. // including commercial applications, and to alter it and redistribute it
  5863. // freely, subject to the following restrictions:
  5864. //
  5865. // 1. The origin of this software must not be misrepresented; you must not
  5866. // claim that you wrote the original software. If you use this software
  5867. // in a product, an acknowledgment in the product documentation would be
  5868. // appreciated but is not required.
  5869. // 2. Altered source versions must be plainly marked as such, and must not be
  5870. // misrepresented as being the original software.
  5871. // 3. This notice may not be removed or altered from any source distribution.
  5872. function GZheader() {
  5873. /* true if compressed data believed to be text */
  5874. this.text = 0;
  5875. /* modification time */
  5876. this.time = 0;
  5877. /* extra flags (not used when writing a gzip file) */
  5878. this.xflags = 0;
  5879. /* operating system */
  5880. this.os = 0;
  5881. /* pointer to extra field or Z_NULL if none */
  5882. this.extra = null;
  5883. /* extra field length (valid if extra != Z_NULL) */
  5884. this.extra_len = 0; // Actually, we don't need it in JS,
  5885. // but leave for few code modifications
  5886. //
  5887. // Setup limits is not necessary because in js we should not preallocate memory
  5888. // for inflate use constant limit in 65536 bytes
  5889. //
  5890. /* space at extra (only when reading header) */
  5891. // this.extra_max = 0;
  5892. /* pointer to zero-terminated file name or Z_NULL */
  5893. this.name = '';
  5894. /* space at name (only when reading header) */
  5895. // this.name_max = 0;
  5896. /* pointer to zero-terminated comment or Z_NULL */
  5897. this.comment = '';
  5898. /* space at comment (only when reading header) */
  5899. // this.comm_max = 0;
  5900. /* true if there was or will be a header crc */
  5901. this.hcrc = 0;
  5902. /* true when done reading gzip header (not used when writing a gzip file) */
  5903. this.done = false;
  5904. }
  5905. var gzheader = GZheader;
  5906. var toString = Object.prototype.toString;
  5907. /* Public constants ==========================================================*/
  5908. /* ===========================================================================*/
  5909. var Z_NO_FLUSH = constants$2.Z_NO_FLUSH,
  5910. Z_FINISH = constants$2.Z_FINISH,
  5911. Z_OK = constants$2.Z_OK,
  5912. Z_STREAM_END = constants$2.Z_STREAM_END,
  5913. Z_NEED_DICT = constants$2.Z_NEED_DICT,
  5914. Z_STREAM_ERROR = constants$2.Z_STREAM_ERROR,
  5915. Z_DATA_ERROR = constants$2.Z_DATA_ERROR,
  5916. Z_MEM_ERROR = constants$2.Z_MEM_ERROR;
  5917. /* ===========================================================================*/
  5918. /**
  5919. * class Inflate
  5920. *
  5921. * Generic JS-style wrapper for zlib calls. If you don't need
  5922. * streaming behaviour - use more simple functions: [[inflate]]
  5923. * and [[inflateRaw]].
  5924. **/
  5925. /* internal
  5926. * inflate.chunks -> Array
  5927. *
  5928. * Chunks of output data, if [[Inflate#onData]] not overridden.
  5929. **/
  5930. /**
  5931. * Inflate.result -> Uint8Array|String
  5932. *
  5933. * Uncompressed result, generated by default [[Inflate#onData]]
  5934. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  5935. * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
  5936. **/
  5937. /**
  5938. * Inflate.err -> Number
  5939. *
  5940. * Error code after inflate finished. 0 (Z_OK) on success.
  5941. * Should be checked if broken data possible.
  5942. **/
  5943. /**
  5944. * Inflate.msg -> String
  5945. *
  5946. * Error message, if [[Inflate.err]] != 0
  5947. **/
  5948. /**
  5949. * new Inflate(options)
  5950. * - options (Object): zlib inflate options.
  5951. *
  5952. * Creates new inflator instance with specified params. Throws exception
  5953. * on bad params. Supported options:
  5954. *
  5955. * - `windowBits`
  5956. * - `dictionary`
  5957. *
  5958. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  5959. * for more information on these.
  5960. *
  5961. * Additional options, for internal needs:
  5962. *
  5963. * - `chunkSize` - size of generated data chunks (16K by default)
  5964. * - `raw` (Boolean) - do raw inflate
  5965. * - `to` (String) - if equal to 'string', then result will be converted
  5966. * from utf8 to utf16 (javascript) string. When string output requested,
  5967. * chunk length can differ from `chunkSize`, depending on content.
  5968. *
  5969. * By default, when no options set, autodetect deflate/gzip data format via
  5970. * wrapper header.
  5971. *
  5972. * ##### Example:
  5973. *
  5974. * ```javascript
  5975. * const pako = require('pako')
  5976. * const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  5977. * const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  5978. *
  5979. * const inflate = new pako.Inflate({ level: 3});
  5980. *
  5981. * inflate.push(chunk1, false);
  5982. * inflate.push(chunk2, true); // true -> last chunk
  5983. *
  5984. * if (inflate.err) { throw new Error(inflate.err); }
  5985. *
  5986. * console.log(inflate.result);
  5987. * ```
  5988. **/
  5989. function Inflate$1(options) {
  5990. this.options = common.assign({
  5991. chunkSize: 1024 * 64,
  5992. windowBits: 15,
  5993. to: ''
  5994. }, options || {});
  5995. var opt = this.options; // Force window size for `raw` data, if not set directly,
  5996. // because we have no header for autodetect.
  5997. if (opt.raw && opt.windowBits >= 0 && opt.windowBits < 16) {
  5998. opt.windowBits = -opt.windowBits;
  5999. if (opt.windowBits === 0) {
  6000. opt.windowBits = -15;
  6001. }
  6002. } // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  6003. if (opt.windowBits >= 0 && opt.windowBits < 16 && !(options && options.windowBits)) {
  6004. opt.windowBits += 32;
  6005. } // Gzip header has no info about windows size, we can do autodetect only
  6006. // for deflate. So, if window size not set, force it to max when gzip possible
  6007. if (opt.windowBits > 15 && opt.windowBits < 48) {
  6008. // bit 3 (16) -> gzipped data
  6009. // bit 4 (32) -> autodetect gzip/deflate
  6010. if ((opt.windowBits & 15) === 0) {
  6011. opt.windowBits |= 15;
  6012. }
  6013. }
  6014. this.err = 0; // error code, if happens (0 = Z_OK)
  6015. this.msg = ''; // error message
  6016. this.ended = false; // used to avoid multiple onEnd() calls
  6017. this.chunks = []; // chunks of compressed data
  6018. this.strm = new zstream();
  6019. this.strm.avail_out = 0;
  6020. var status = inflate_1$2.inflateInit2(this.strm, opt.windowBits);
  6021. if (status !== Z_OK) {
  6022. throw new Error(messages[status]);
  6023. }
  6024. this.header = new gzheader();
  6025. inflate_1$2.inflateGetHeader(this.strm, this.header); // Setup dictionary
  6026. if (opt.dictionary) {
  6027. // Convert data if needed
  6028. if (typeof opt.dictionary === 'string') {
  6029. opt.dictionary = strings.string2buf(opt.dictionary);
  6030. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  6031. opt.dictionary = new Uint8Array(opt.dictionary);
  6032. }
  6033. if (opt.raw) {
  6034. //In raw mode we need to set the dictionary early
  6035. status = inflate_1$2.inflateSetDictionary(this.strm, opt.dictionary);
  6036. if (status !== Z_OK) {
  6037. throw new Error(messages[status]);
  6038. }
  6039. }
  6040. }
  6041. }
  6042. /**
  6043. * Inflate#push(data[, flush_mode]) -> Boolean
  6044. * - data (Uint8Array|ArrayBuffer): input data
  6045. * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE
  6046. * flush modes. See constants. Skipped or `false` means Z_NO_FLUSH,
  6047. * `true` means Z_FINISH.
  6048. *
  6049. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  6050. * new output chunks. Returns `true` on success. If end of stream detected,
  6051. * [[Inflate#onEnd]] will be called.
  6052. *
  6053. * `flush_mode` is not needed for normal operation, because end of stream
  6054. * detected automatically. You may try to use it for advanced things, but
  6055. * this functionality was not tested.
  6056. *
  6057. * On fail call [[Inflate#onEnd]] with error code and return false.
  6058. *
  6059. * ##### Example
  6060. *
  6061. * ```javascript
  6062. * push(chunk, false); // push one of data chunks
  6063. * ...
  6064. * push(chunk, true); // push last chunk
  6065. * ```
  6066. **/
  6067. Inflate$1.prototype.push = function (data, flush_mode) {
  6068. var strm = this.strm;
  6069. var chunkSize = this.options.chunkSize;
  6070. var dictionary = this.options.dictionary;
  6071. var status, _flush_mode, last_avail_out;
  6072. if (this.ended) return false;
  6073. if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH; // Convert data if needed
  6074. if (toString.call(data) === '[object ArrayBuffer]') {
  6075. strm.input = new Uint8Array(data);
  6076. } else {
  6077. strm.input = data;
  6078. }
  6079. strm.next_in = 0;
  6080. strm.avail_in = strm.input.length;
  6081. for (;;) {
  6082. if (strm.avail_out === 0) {
  6083. strm.output = new Uint8Array(chunkSize);
  6084. strm.next_out = 0;
  6085. strm.avail_out = chunkSize;
  6086. }
  6087. status = inflate_1$2.inflate(strm, _flush_mode);
  6088. if (status === Z_NEED_DICT && dictionary) {
  6089. status = inflate_1$2.inflateSetDictionary(strm, dictionary);
  6090. if (status === Z_OK) {
  6091. status = inflate_1$2.inflate(strm, _flush_mode);
  6092. } else if (status === Z_DATA_ERROR) {
  6093. // Replace code with more verbose
  6094. status = Z_NEED_DICT;
  6095. }
  6096. } // Skip snyc markers if more data follows and not raw mode
  6097. while (strm.avail_in > 0 && status === Z_STREAM_END && strm.state.wrap > 0 && data[strm.next_in] !== 0) {
  6098. inflate_1$2.inflateReset(strm);
  6099. status = inflate_1$2.inflate(strm, _flush_mode);
  6100. }
  6101. switch (status) {
  6102. case Z_STREAM_ERROR:
  6103. case Z_DATA_ERROR:
  6104. case Z_NEED_DICT:
  6105. case Z_MEM_ERROR:
  6106. this.onEnd(status);
  6107. this.ended = true;
  6108. return false;
  6109. } // Remember real `avail_out` value, because we may patch out buffer content
  6110. // to align utf8 strings boundaries.
  6111. last_avail_out = strm.avail_out;
  6112. if (strm.next_out) {
  6113. if (strm.avail_out === 0 || status === Z_STREAM_END) {
  6114. if (this.options.to === 'string') {
  6115. var next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  6116. var tail = strm.next_out - next_out_utf8;
  6117. var utf8str = strings.buf2string(strm.output, next_out_utf8); // move tail & realign counters
  6118. strm.next_out = tail;
  6119. strm.avail_out = chunkSize - tail;
  6120. if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0);
  6121. this.onData(utf8str);
  6122. } else {
  6123. this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out));
  6124. }
  6125. }
  6126. } // Must repeat iteration if out buffer is full
  6127. if (status === Z_OK && last_avail_out === 0) continue; // Finalize if end of stream reached.
  6128. if (status === Z_STREAM_END) {
  6129. status = inflate_1$2.inflateEnd(this.strm);
  6130. this.onEnd(status);
  6131. this.ended = true;
  6132. return true;
  6133. }
  6134. if (strm.avail_in === 0) break;
  6135. }
  6136. return true;
  6137. };
  6138. /**
  6139. * Inflate#onData(chunk) -> Void
  6140. * - chunk (Uint8Array|String): output data. When string output requested,
  6141. * each chunk will be string.
  6142. *
  6143. * By default, stores data blocks in `chunks[]` property and glue
  6144. * those in `onEnd`. Override this handler, if you need another behaviour.
  6145. **/
  6146. Inflate$1.prototype.onData = function (chunk) {
  6147. this.chunks.push(chunk);
  6148. };
  6149. /**
  6150. * Inflate#onEnd(status) -> Void
  6151. * - status (Number): inflate status. 0 (Z_OK) on success,
  6152. * other if not.
  6153. *
  6154. * Called either after you tell inflate that the input stream is
  6155. * complete (Z_FINISH). By default - join collected chunks,
  6156. * free memory and fill `results` / `err` properties.
  6157. **/
  6158. Inflate$1.prototype.onEnd = function (status) {
  6159. // On success - join
  6160. if (status === Z_OK) {
  6161. if (this.options.to === 'string') {
  6162. this.result = this.chunks.join('');
  6163. } else {
  6164. this.result = common.flattenChunks(this.chunks);
  6165. }
  6166. }
  6167. this.chunks = [];
  6168. this.err = status;
  6169. this.msg = this.strm.msg;
  6170. };
  6171. /**
  6172. * inflate(data[, options]) -> Uint8Array|String
  6173. * - data (Uint8Array): input data to decompress.
  6174. * - options (Object): zlib inflate options.
  6175. *
  6176. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  6177. * format via wrapper header by default. That's why we don't provide
  6178. * separate `ungzip` method.
  6179. *
  6180. * Supported options are:
  6181. *
  6182. * - windowBits
  6183. *
  6184. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  6185. * for more information.
  6186. *
  6187. * Sugar (options):
  6188. *
  6189. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  6190. * negative windowBits implicitly.
  6191. * - `to` (String) - if equal to 'string', then result will be converted
  6192. * from utf8 to utf16 (javascript) string. When string output requested,
  6193. * chunk length can differ from `chunkSize`, depending on content.
  6194. *
  6195. *
  6196. * ##### Example:
  6197. *
  6198. * ```javascript
  6199. * const pako = require('pako');
  6200. * const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9]));
  6201. * let output;
  6202. *
  6203. * try {
  6204. * output = pako.inflate(input);
  6205. * } catch (err) {
  6206. * console.log(err);
  6207. * }
  6208. * ```
  6209. **/
  6210. function inflate$1(input, options) {
  6211. var inflator = new Inflate$1(options);
  6212. inflator.push(input); // That will never happens, if you don't cheat with options :)
  6213. if (inflator.err) throw inflator.msg || messages[inflator.err];
  6214. return inflator.result;
  6215. }
  6216. /**
  6217. * inflateRaw(data[, options]) -> Uint8Array|String
  6218. * - data (Uint8Array): input data to decompress.
  6219. * - options (Object): zlib inflate options.
  6220. *
  6221. * The same as [[inflate]], but creates raw data, without wrapper
  6222. * (header and adler32 crc).
  6223. **/
  6224. function inflateRaw$1(input, options) {
  6225. options = options || {};
  6226. options.raw = true;
  6227. return inflate$1(input, options);
  6228. }
  6229. /**
  6230. * ungzip(data[, options]) -> Uint8Array|String
  6231. * - data (Uint8Array): input data to decompress.
  6232. * - options (Object): zlib inflate options.
  6233. *
  6234. * Just shortcut to [[inflate]], because it autodetects format
  6235. * by header.content. Done for convenience.
  6236. **/
  6237. var Inflate_1$1 = Inflate$1;
  6238. var inflate_2 = inflate$1;
  6239. var inflateRaw_1$1 = inflateRaw$1;
  6240. var ungzip$1 = inflate$1;
  6241. var constants = constants$2;
  6242. var inflate_1$1 = {
  6243. Inflate: Inflate_1$1,
  6244. inflate: inflate_2,
  6245. inflateRaw: inflateRaw_1$1,
  6246. ungzip: ungzip$1,
  6247. constants: constants
  6248. };
  6249. var Deflate = deflate_1$1.Deflate,
  6250. deflate = deflate_1$1.deflate,
  6251. deflateRaw = deflate_1$1.deflateRaw,
  6252. gzip = deflate_1$1.gzip;
  6253. var Inflate = inflate_1$1.Inflate,
  6254. inflate = inflate_1$1.inflate,
  6255. inflateRaw = inflate_1$1.inflateRaw,
  6256. ungzip = inflate_1$1.ungzip;
  6257. var Deflate_1 = Deflate;
  6258. var deflate_1 = deflate;
  6259. var deflateRaw_1 = deflateRaw;
  6260. var gzip_1 = gzip;
  6261. var Inflate_1 = Inflate;
  6262. var inflate_1 = inflate;
  6263. var inflateRaw_1 = inflateRaw;
  6264. var ungzip_1 = ungzip;
  6265. var constants_1 = constants$2;
  6266. var pako = {
  6267. Deflate: Deflate_1,
  6268. deflate: deflate_1,
  6269. deflateRaw: deflateRaw_1,
  6270. gzip: gzip_1,
  6271. Inflate: Inflate_1,
  6272. inflate: inflate_1,
  6273. inflateRaw: inflateRaw_1,
  6274. ungzip: ungzip_1,
  6275. constants: constants_1
  6276. };
  6277. exports.Deflate = Deflate_1;
  6278. exports.Inflate = Inflate_1;
  6279. exports.constants = constants_1;
  6280. exports['default'] = pako;
  6281. exports.deflate = deflate_1;
  6282. exports.deflateRaw = deflateRaw_1;
  6283. exports.gzip = gzip_1;
  6284. exports.inflate = inflate_1;
  6285. exports.inflateRaw = inflateRaw_1;
  6286. exports.ungzip = ungzip_1;
  6287. Object.defineProperty(exports, '__esModule', { value: true });
  6288. })));