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.

6728 lines
224KB

  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) 1995-2013 Jean-loup Gailly and Mark Adler
  8. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  9. //
  10. // This software is provided 'as-is', without any express or implied
  11. // warranty. In no event will the authors be held liable for any damages
  12. // arising from the use of this software.
  13. //
  14. // Permission is granted to anyone to use this software for any purpose,
  15. // including commercial applications, and to alter it and redistribute it
  16. // freely, subject to the following restrictions:
  17. //
  18. // 1. The origin of this software must not be misrepresented; you must not
  19. // claim that you wrote the original software. If you use this software
  20. // in a product, an acknowledgment in the product documentation would be
  21. // appreciated but is not required.
  22. // 2. Altered source versions must be plainly marked as such, and must not be
  23. // misrepresented as being the original software.
  24. // 3. This notice may not be removed or altered from any source distribution.
  25. /* eslint-disable space-unary-ops */
  26. /* Public constants ==========================================================*/
  27. /* ===========================================================================*/
  28. //const Z_FILTERED = 1;
  29. //const Z_HUFFMAN_ONLY = 2;
  30. //const Z_RLE = 3;
  31. const Z_FIXED$1 = 4;
  32. //const Z_DEFAULT_STRATEGY = 0;
  33. /* Possible values of the data_type field (though see inflate()) */
  34. const Z_BINARY = 0;
  35. const Z_TEXT = 1;
  36. //const Z_ASCII = 1; // = Z_TEXT
  37. const Z_UNKNOWN$1 = 2;
  38. /*============================================================================*/
  39. function zero$1(buf) { let len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  40. // From zutil.h
  41. const STORED_BLOCK = 0;
  42. const STATIC_TREES = 1;
  43. const DYN_TREES = 2;
  44. /* The three kinds of block type */
  45. const MIN_MATCH$1 = 3;
  46. const MAX_MATCH$1 = 258;
  47. /* The minimum and maximum match lengths */
  48. // From deflate.h
  49. /* ===========================================================================
  50. * Internal compression state.
  51. */
  52. const LENGTH_CODES$1 = 29;
  53. /* number of length codes, not counting the special END_BLOCK code */
  54. const LITERALS$1 = 256;
  55. /* number of literal bytes 0..255 */
  56. const L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
  57. /* number of Literal or Length codes, including the END_BLOCK code */
  58. const D_CODES$1 = 30;
  59. /* number of distance codes */
  60. const BL_CODES$1 = 19;
  61. /* number of codes used to transfer the bit lengths */
  62. const HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
  63. /* maximum heap size */
  64. const MAX_BITS$1 = 15;
  65. /* All codes must not exceed MAX_BITS bits */
  66. const Buf_size = 16;
  67. /* size of bit buffer in bi_buf */
  68. /* ===========================================================================
  69. * Constants
  70. */
  71. const MAX_BL_BITS = 7;
  72. /* Bit length codes must not exceed MAX_BL_BITS bits */
  73. const END_BLOCK = 256;
  74. /* end of block literal code */
  75. const REP_3_6 = 16;
  76. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  77. const REPZ_3_10 = 17;
  78. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  79. const REPZ_11_138 = 18;
  80. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  81. /* eslint-disable comma-spacing,array-bracket-spacing */
  82. const extra_lbits = /* extra bits for each length code */
  83. 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]);
  84. const extra_dbits = /* extra bits for each distance code */
  85. 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]);
  86. const extra_blbits = /* extra bits for each bit length code */
  87. new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]);
  88. const bl_order =
  89. new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);
  90. /* eslint-enable comma-spacing,array-bracket-spacing */
  91. /* The lengths of the bit length codes are sent in order of decreasing
  92. * probability, to avoid transmitting the lengths for unused bit length codes.
  93. */
  94. /* ===========================================================================
  95. * Local data. These are initialized only once.
  96. */
  97. // We pre-fill arrays with 0 to avoid uninitialized gaps
  98. const DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  99. // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
  100. const static_ltree = new Array((L_CODES$1 + 2) * 2);
  101. zero$1(static_ltree);
  102. /* The static literal tree. Since the bit lengths are imposed, there is no
  103. * need for the L_CODES extra codes used during heap construction. However
  104. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  105. * below).
  106. */
  107. const static_dtree = new Array(D_CODES$1 * 2);
  108. zero$1(static_dtree);
  109. /* The static distance tree. (Actually a trivial tree since all codes use
  110. * 5 bits.)
  111. */
  112. const _dist_code = new Array(DIST_CODE_LEN);
  113. zero$1(_dist_code);
  114. /* Distance codes. The first 256 values correspond to the distances
  115. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  116. * the 15 bit distances.
  117. */
  118. const _length_code = new Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
  119. zero$1(_length_code);
  120. /* length code for each normalized match length (0 == MIN_MATCH) */
  121. const base_length = new Array(LENGTH_CODES$1);
  122. zero$1(base_length);
  123. /* First normalized length for each code (0 = MIN_MATCH) */
  124. const base_dist = new Array(D_CODES$1);
  125. zero$1(base_dist);
  126. /* First normalized distance for each code (0 = distance of 1) */
  127. function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
  128. this.static_tree = static_tree; /* static tree or NULL */
  129. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  130. this.extra_base = extra_base; /* base index for extra_bits */
  131. this.elems = elems; /* max number of elements in the tree */
  132. this.max_length = max_length; /* max bit length for the codes */
  133. // show if `static_tree` has data or dummy - needed for monomorphic objects
  134. this.has_stree = static_tree && static_tree.length;
  135. }
  136. let static_l_desc;
  137. let static_d_desc;
  138. let static_bl_desc;
  139. function TreeDesc(dyn_tree, stat_desc) {
  140. this.dyn_tree = dyn_tree; /* the dynamic tree */
  141. this.max_code = 0; /* largest code with non zero frequency */
  142. this.stat_desc = stat_desc; /* the corresponding static tree */
  143. }
  144. const d_code = (dist) => {
  145. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  146. };
  147. /* ===========================================================================
  148. * Output a short LSB first on the stream.
  149. * IN assertion: there is enough room in pendingBuf.
  150. */
  151. const put_short = (s, w) => {
  152. // put_byte(s, (uch)((w) & 0xff));
  153. // put_byte(s, (uch)((ush)(w) >> 8));
  154. s.pending_buf[s.pending++] = (w) & 0xff;
  155. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  156. };
  157. /* ===========================================================================
  158. * Send a value on a given number of bits.
  159. * IN assertion: length <= 16 and value fits in length bits.
  160. */
  161. const send_bits = (s, value, length) => {
  162. if (s.bi_valid > (Buf_size - length)) {
  163. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  164. put_short(s, s.bi_buf);
  165. s.bi_buf = value >> (Buf_size - s.bi_valid);
  166. s.bi_valid += length - Buf_size;
  167. } else {
  168. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  169. s.bi_valid += length;
  170. }
  171. };
  172. const send_code = (s, c, tree) => {
  173. send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
  174. };
  175. /* ===========================================================================
  176. * Reverse the first len bits of a code, using straightforward code (a faster
  177. * method would use a table)
  178. * IN assertion: 1 <= len <= 15
  179. */
  180. const bi_reverse = (code, len) => {
  181. let res = 0;
  182. do {
  183. res |= code & 1;
  184. code >>>= 1;
  185. res <<= 1;
  186. } while (--len > 0);
  187. return res >>> 1;
  188. };
  189. /* ===========================================================================
  190. * Flush the bit buffer, keeping at most 7 bits in it.
  191. */
  192. const bi_flush = (s) => {
  193. if (s.bi_valid === 16) {
  194. put_short(s, s.bi_buf);
  195. s.bi_buf = 0;
  196. s.bi_valid = 0;
  197. } else if (s.bi_valid >= 8) {
  198. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  199. s.bi_buf >>= 8;
  200. s.bi_valid -= 8;
  201. }
  202. };
  203. /* ===========================================================================
  204. * Compute the optimal bit lengths for a tree and update the total bit length
  205. * for the current block.
  206. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  207. * above are the tree nodes sorted by increasing frequency.
  208. * OUT assertions: the field len is set to the optimal bit length, the
  209. * array bl_count contains the frequencies for each bit length.
  210. * The length opt_len is updated; static_len is also updated if stree is
  211. * not null.
  212. */
  213. const gen_bitlen = (s, desc) =>
  214. // deflate_state *s;
  215. // tree_desc *desc; /* the tree descriptor */
  216. {
  217. const tree = desc.dyn_tree;
  218. const max_code = desc.max_code;
  219. const stree = desc.stat_desc.static_tree;
  220. const has_stree = desc.stat_desc.has_stree;
  221. const extra = desc.stat_desc.extra_bits;
  222. const base = desc.stat_desc.extra_base;
  223. const max_length = desc.stat_desc.max_length;
  224. let h; /* heap index */
  225. let n, m; /* iterate over the tree elements */
  226. let bits; /* bit length */
  227. let xbits; /* extra bits */
  228. let f; /* frequency */
  229. let overflow = 0; /* number of elements with bit length too large */
  230. for (bits = 0; bits <= MAX_BITS$1; bits++) {
  231. s.bl_count[bits] = 0;
  232. }
  233. /* In a first pass, compute the optimal bit lengths (which may
  234. * overflow in the case of the bit length tree).
  235. */
  236. tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
  237. for (h = s.heap_max + 1; h < HEAP_SIZE$1; h++) {
  238. n = s.heap[h];
  239. bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  240. if (bits > max_length) {
  241. bits = max_length;
  242. overflow++;
  243. }
  244. tree[n * 2 + 1]/*.Len*/ = bits;
  245. /* We overwrite tree[n].Dad which is no longer needed */
  246. if (n > max_code) { continue; } /* not a leaf node */
  247. s.bl_count[bits]++;
  248. xbits = 0;
  249. if (n >= base) {
  250. xbits = extra[n - base];
  251. }
  252. f = tree[n * 2]/*.Freq*/;
  253. s.opt_len += f * (bits + xbits);
  254. if (has_stree) {
  255. s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
  256. }
  257. }
  258. if (overflow === 0) { return; }
  259. // Trace((stderr,"\nbit length overflow\n"));
  260. /* This happens for example on obj2 and pic of the Calgary corpus */
  261. /* Find the first bit length which could increase: */
  262. do {
  263. bits = max_length - 1;
  264. while (s.bl_count[bits] === 0) { bits--; }
  265. s.bl_count[bits]--; /* move one leaf down the tree */
  266. s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
  267. s.bl_count[max_length]--;
  268. /* The brother of the overflow item also moves one step up,
  269. * but this does not affect bl_count[max_length]
  270. */
  271. overflow -= 2;
  272. } while (overflow > 0);
  273. /* Now recompute all bit lengths, scanning in increasing frequency.
  274. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  275. * lengths instead of fixing only the wrong ones. This idea is taken
  276. * from 'ar' written by Haruhiko Okumura.)
  277. */
  278. for (bits = max_length; bits !== 0; bits--) {
  279. n = s.bl_count[bits];
  280. while (n !== 0) {
  281. m = s.heap[--h];
  282. if (m > max_code) { continue; }
  283. if (tree[m * 2 + 1]/*.Len*/ !== bits) {
  284. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  285. s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
  286. tree[m * 2 + 1]/*.Len*/ = bits;
  287. }
  288. n--;
  289. }
  290. }
  291. };
  292. /* ===========================================================================
  293. * Generate the codes for a given tree and bit counts (which need not be
  294. * optimal).
  295. * IN assertion: the array bl_count contains the bit length statistics for
  296. * the given tree and the field len is set for all tree elements.
  297. * OUT assertion: the field code is set for all tree elements of non
  298. * zero code length.
  299. */
  300. const gen_codes = (tree, max_code, bl_count) =>
  301. // ct_data *tree; /* the tree to decorate */
  302. // int max_code; /* largest code with non zero frequency */
  303. // ushf *bl_count; /* number of codes at each bit length */
  304. {
  305. const next_code = new Array(MAX_BITS$1 + 1); /* next code value for each bit length */
  306. let code = 0; /* running code value */
  307. let bits; /* bit index */
  308. let n; /* code index */
  309. /* The distribution counts are first used to generate the code values
  310. * without bit reversal.
  311. */
  312. for (bits = 1; bits <= MAX_BITS$1; bits++) {
  313. next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
  314. }
  315. /* Check that the bit counts in bl_count are consistent. The last code
  316. * must be all ones.
  317. */
  318. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  319. // "inconsistent bit counts");
  320. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  321. for (n = 0; n <= max_code; n++) {
  322. let len = tree[n * 2 + 1]/*.Len*/;
  323. if (len === 0) { continue; }
  324. /* Now reverse the bits */
  325. tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  326. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  327. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  328. }
  329. };
  330. /* ===========================================================================
  331. * Initialize the various 'constant' tables.
  332. */
  333. const tr_static_init = () => {
  334. let n; /* iterates over tree elements */
  335. let bits; /* bit counter */
  336. let length; /* length value */
  337. let code; /* code value */
  338. let dist; /* distance index */
  339. const bl_count = new Array(MAX_BITS$1 + 1);
  340. /* number of codes at each bit length for an optimal tree */
  341. // do check in _tr_init()
  342. //if (static_init_done) return;
  343. /* For some embedded targets, global variables are not initialized: */
  344. /*#ifdef NO_INIT_GLOBAL_POINTERS
  345. static_l_desc.static_tree = static_ltree;
  346. static_l_desc.extra_bits = extra_lbits;
  347. static_d_desc.static_tree = static_dtree;
  348. static_d_desc.extra_bits = extra_dbits;
  349. static_bl_desc.extra_bits = extra_blbits;
  350. #endif*/
  351. /* Initialize the mapping length (0..255) -> length code (0..28) */
  352. length = 0;
  353. for (code = 0; code < LENGTH_CODES$1 - 1; code++) {
  354. base_length[code] = length;
  355. for (n = 0; n < (1 << extra_lbits[code]); n++) {
  356. _length_code[length++] = code;
  357. }
  358. }
  359. //Assert (length == 256, "tr_static_init: length != 256");
  360. /* Note that the length 255 (match length 258) can be represented
  361. * in two different ways: code 284 + 5 bits or code 285, so we
  362. * overwrite length_code[255] to use the best encoding:
  363. */
  364. _length_code[length - 1] = code;
  365. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  366. dist = 0;
  367. for (code = 0; code < 16; code++) {
  368. base_dist[code] = dist;
  369. for (n = 0; n < (1 << extra_dbits[code]); n++) {
  370. _dist_code[dist++] = code;
  371. }
  372. }
  373. //Assert (dist == 256, "tr_static_init: dist != 256");
  374. dist >>= 7; /* from now on, all distances are divided by 128 */
  375. for (; code < D_CODES$1; code++) {
  376. base_dist[code] = dist << 7;
  377. for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
  378. _dist_code[256 + dist++] = code;
  379. }
  380. }
  381. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  382. /* Construct the codes of the static literal tree */
  383. for (bits = 0; bits <= MAX_BITS$1; bits++) {
  384. bl_count[bits] = 0;
  385. }
  386. n = 0;
  387. while (n <= 143) {
  388. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  389. n++;
  390. bl_count[8]++;
  391. }
  392. while (n <= 255) {
  393. static_ltree[n * 2 + 1]/*.Len*/ = 9;
  394. n++;
  395. bl_count[9]++;
  396. }
  397. while (n <= 279) {
  398. static_ltree[n * 2 + 1]/*.Len*/ = 7;
  399. n++;
  400. bl_count[7]++;
  401. }
  402. while (n <= 287) {
  403. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  404. n++;
  405. bl_count[8]++;
  406. }
  407. /* Codes 286 and 287 do not exist, but we must include them in the
  408. * tree construction to get a canonical Huffman tree (longest code
  409. * all ones)
  410. */
  411. gen_codes(static_ltree, L_CODES$1 + 1, bl_count);
  412. /* The static distance tree is trivial: */
  413. for (n = 0; n < D_CODES$1; n++) {
  414. static_dtree[n * 2 + 1]/*.Len*/ = 5;
  415. static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
  416. }
  417. // Now data ready and we can init static trees
  418. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS$1 + 1, L_CODES$1, MAX_BITS$1);
  419. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES$1, MAX_BITS$1);
  420. static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES$1, MAX_BL_BITS);
  421. //static_init_done = true;
  422. };
  423. /* ===========================================================================
  424. * Initialize a new block.
  425. */
  426. const init_block = (s) => {
  427. let n; /* iterates over tree elements */
  428. /* Initialize the trees. */
  429. for (n = 0; n < L_CODES$1; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
  430. for (n = 0; n < D_CODES$1; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
  431. for (n = 0; n < BL_CODES$1; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
  432. s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
  433. s.opt_len = s.static_len = 0;
  434. s.last_lit = s.matches = 0;
  435. };
  436. /* ===========================================================================
  437. * Flush the bit buffer and align the output on a byte boundary
  438. */
  439. const bi_windup = (s) =>
  440. {
  441. if (s.bi_valid > 8) {
  442. put_short(s, s.bi_buf);
  443. } else if (s.bi_valid > 0) {
  444. //put_byte(s, (Byte)s->bi_buf);
  445. s.pending_buf[s.pending++] = s.bi_buf;
  446. }
  447. s.bi_buf = 0;
  448. s.bi_valid = 0;
  449. };
  450. /* ===========================================================================
  451. * Copy a stored block, storing first the length and its
  452. * one's complement if requested.
  453. */
  454. const copy_block = (s, buf, len, header) =>
  455. //DeflateState *s;
  456. //charf *buf; /* the input data */
  457. //unsigned len; /* its length */
  458. //int header; /* true if block header must be written */
  459. {
  460. bi_windup(s); /* align on byte boundary */
  461. if (header) {
  462. put_short(s, len);
  463. put_short(s, ~len);
  464. }
  465. // while (len--) {
  466. // put_byte(s, *buf++);
  467. // }
  468. s.pending_buf.set(s.window.subarray(buf, buf + len), s.pending);
  469. s.pending += len;
  470. };
  471. /* ===========================================================================
  472. * Compares to subtrees, using the tree depth as tie breaker when
  473. * the subtrees have equal frequency. This minimizes the worst case length.
  474. */
  475. const smaller = (tree, n, m, depth) => {
  476. const _n2 = n * 2;
  477. const _m2 = m * 2;
  478. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  479. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  480. };
  481. /* ===========================================================================
  482. * Restore the heap property by moving down the tree starting at node k,
  483. * exchanging a node with the smallest of its two sons if necessary, stopping
  484. * when the heap property is re-established (each father smaller than its
  485. * two sons).
  486. */
  487. const pqdownheap = (s, tree, k) =>
  488. // deflate_state *s;
  489. // ct_data *tree; /* the tree to restore */
  490. // int k; /* node to move down */
  491. {
  492. const v = s.heap[k];
  493. let j = k << 1; /* left son of k */
  494. while (j <= s.heap_len) {
  495. /* Set j to the smallest of the two sons: */
  496. if (j < s.heap_len &&
  497. smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
  498. j++;
  499. }
  500. /* Exit if v is smaller than both sons */
  501. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  502. /* Exchange v with the smallest son */
  503. s.heap[k] = s.heap[j];
  504. k = j;
  505. /* And continue down the tree, setting j to the left son of k */
  506. j <<= 1;
  507. }
  508. s.heap[k] = v;
  509. };
  510. // inlined manually
  511. // const SMALLEST = 1;
  512. /* ===========================================================================
  513. * Send the block data compressed using the given Huffman trees
  514. */
  515. const compress_block = (s, ltree, dtree) =>
  516. // deflate_state *s;
  517. // const ct_data *ltree; /* literal tree */
  518. // const ct_data *dtree; /* distance tree */
  519. {
  520. let dist; /* distance of matched string */
  521. let lc; /* match length or unmatched char (if dist == 0) */
  522. let lx = 0; /* running index in l_buf */
  523. let code; /* the code to send */
  524. let extra; /* number of extra bits to send */
  525. if (s.last_lit !== 0) {
  526. do {
  527. dist = (s.pending_buf[s.d_buf + lx * 2] << 8) | (s.pending_buf[s.d_buf + lx * 2 + 1]);
  528. lc = s.pending_buf[s.l_buf + lx];
  529. lx++;
  530. if (dist === 0) {
  531. send_code(s, lc, ltree); /* send a literal byte */
  532. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  533. } else {
  534. /* Here, lc is the match length - MIN_MATCH */
  535. code = _length_code[lc];
  536. send_code(s, code + LITERALS$1 + 1, ltree); /* send the length code */
  537. extra = extra_lbits[code];
  538. if (extra !== 0) {
  539. lc -= base_length[code];
  540. send_bits(s, lc, extra); /* send the extra length bits */
  541. }
  542. dist--; /* dist is now the match distance - 1 */
  543. code = d_code(dist);
  544. //Assert (code < D_CODES, "bad d_code");
  545. send_code(s, code, dtree); /* send the distance code */
  546. extra = extra_dbits[code];
  547. if (extra !== 0) {
  548. dist -= base_dist[code];
  549. send_bits(s, dist, extra); /* send the extra distance bits */
  550. }
  551. } /* literal or match pair ? */
  552. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  553. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  554. // "pendingBuf overflow");
  555. } while (lx < s.last_lit);
  556. }
  557. send_code(s, END_BLOCK, ltree);
  558. };
  559. /* ===========================================================================
  560. * Construct one Huffman tree and assigns the code bit strings and lengths.
  561. * Update the total bit length for the current block.
  562. * IN assertion: the field freq is set for all tree elements.
  563. * OUT assertions: the fields len and code are set to the optimal bit length
  564. * and corresponding code. The length opt_len is updated; static_len is
  565. * also updated if stree is not null. The field max_code is set.
  566. */
  567. const build_tree = (s, desc) =>
  568. // deflate_state *s;
  569. // tree_desc *desc; /* the tree descriptor */
  570. {
  571. const tree = desc.dyn_tree;
  572. const stree = desc.stat_desc.static_tree;
  573. const has_stree = desc.stat_desc.has_stree;
  574. const elems = desc.stat_desc.elems;
  575. let n, m; /* iterate over heap elements */
  576. let max_code = -1; /* largest code with non zero frequency */
  577. let node; /* new node being created */
  578. /* Construct the initial heap, with least frequent element in
  579. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  580. * heap[0] is not used.
  581. */
  582. s.heap_len = 0;
  583. s.heap_max = HEAP_SIZE$1;
  584. for (n = 0; n < elems; n++) {
  585. if (tree[n * 2]/*.Freq*/ !== 0) {
  586. s.heap[++s.heap_len] = max_code = n;
  587. s.depth[n] = 0;
  588. } else {
  589. tree[n * 2 + 1]/*.Len*/ = 0;
  590. }
  591. }
  592. /* The pkzip format requires that at least one distance code exists,
  593. * and that at least one bit should be sent even if there is only one
  594. * possible code. So to avoid special checks later on we force at least
  595. * two codes of non zero frequency.
  596. */
  597. while (s.heap_len < 2) {
  598. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  599. tree[node * 2]/*.Freq*/ = 1;
  600. s.depth[node] = 0;
  601. s.opt_len--;
  602. if (has_stree) {
  603. s.static_len -= stree[node * 2 + 1]/*.Len*/;
  604. }
  605. /* node is 0 or 1 so it does not have extra bits */
  606. }
  607. desc.max_code = max_code;
  608. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  609. * establish sub-heaps of increasing lengths:
  610. */
  611. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  612. /* Construct the Huffman tree by repeatedly combining the least two
  613. * frequent nodes.
  614. */
  615. node = elems; /* next internal node of the tree */
  616. do {
  617. //pqremove(s, tree, n); /* n = node of least frequency */
  618. /*** pqremove ***/
  619. n = s.heap[1/*SMALLEST*/];
  620. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  621. pqdownheap(s, tree, 1/*SMALLEST*/);
  622. /***/
  623. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  624. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  625. s.heap[--s.heap_max] = m;
  626. /* Create a new node father of n and m */
  627. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  628. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  629. tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
  630. /* and insert the new node in the heap */
  631. s.heap[1/*SMALLEST*/] = node++;
  632. pqdownheap(s, tree, 1/*SMALLEST*/);
  633. } while (s.heap_len >= 2);
  634. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  635. /* At this point, the fields freq and dad are set. We can now
  636. * generate the bit lengths.
  637. */
  638. gen_bitlen(s, desc);
  639. /* The field len is now set, we can generate the bit codes */
  640. gen_codes(tree, max_code, s.bl_count);
  641. };
  642. /* ===========================================================================
  643. * Scan a literal or distance tree to determine the frequencies of the codes
  644. * in the bit length tree.
  645. */
  646. const scan_tree = (s, tree, max_code) =>
  647. // deflate_state *s;
  648. // ct_data *tree; /* the tree to be scanned */
  649. // int max_code; /* and its largest code of non zero frequency */
  650. {
  651. let n; /* iterates over all tree elements */
  652. let prevlen = -1; /* last emitted length */
  653. let curlen; /* length of current code */
  654. let nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  655. let count = 0; /* repeat count of the current code */
  656. let max_count = 7; /* max repeat count */
  657. let min_count = 4; /* min repeat count */
  658. if (nextlen === 0) {
  659. max_count = 138;
  660. min_count = 3;
  661. }
  662. tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
  663. for (n = 0; n <= max_code; n++) {
  664. curlen = nextlen;
  665. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  666. if (++count < max_count && curlen === nextlen) {
  667. continue;
  668. } else if (count < min_count) {
  669. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  670. } else if (curlen !== 0) {
  671. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  672. s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
  673. } else if (count <= 10) {
  674. s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
  675. } else {
  676. s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
  677. }
  678. count = 0;
  679. prevlen = curlen;
  680. if (nextlen === 0) {
  681. max_count = 138;
  682. min_count = 3;
  683. } else if (curlen === nextlen) {
  684. max_count = 6;
  685. min_count = 3;
  686. } else {
  687. max_count = 7;
  688. min_count = 4;
  689. }
  690. }
  691. };
  692. /* ===========================================================================
  693. * Send a literal or distance tree in compressed form, using the codes in
  694. * bl_tree.
  695. */
  696. const send_tree = (s, tree, max_code) =>
  697. // deflate_state *s;
  698. // ct_data *tree; /* the tree to be scanned */
  699. // int max_code; /* and its largest code of non zero frequency */
  700. {
  701. let n; /* iterates over all tree elements */
  702. let prevlen = -1; /* last emitted length */
  703. let curlen; /* length of current code */
  704. let nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  705. let count = 0; /* repeat count of the current code */
  706. let max_count = 7; /* max repeat count */
  707. let min_count = 4; /* min repeat count */
  708. /* tree[max_code+1].Len = -1; */ /* guard already set */
  709. if (nextlen === 0) {
  710. max_count = 138;
  711. min_count = 3;
  712. }
  713. for (n = 0; n <= max_code; n++) {
  714. curlen = nextlen;
  715. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  716. if (++count < max_count && curlen === nextlen) {
  717. continue;
  718. } else if (count < min_count) {
  719. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  720. } else if (curlen !== 0) {
  721. if (curlen !== prevlen) {
  722. send_code(s, curlen, s.bl_tree);
  723. count--;
  724. }
  725. //Assert(count >= 3 && count <= 6, " 3_6?");
  726. send_code(s, REP_3_6, s.bl_tree);
  727. send_bits(s, count - 3, 2);
  728. } else if (count <= 10) {
  729. send_code(s, REPZ_3_10, s.bl_tree);
  730. send_bits(s, count - 3, 3);
  731. } else {
  732. send_code(s, REPZ_11_138, s.bl_tree);
  733. send_bits(s, count - 11, 7);
  734. }
  735. count = 0;
  736. prevlen = curlen;
  737. if (nextlen === 0) {
  738. max_count = 138;
  739. min_count = 3;
  740. } else if (curlen === nextlen) {
  741. max_count = 6;
  742. min_count = 3;
  743. } else {
  744. max_count = 7;
  745. min_count = 4;
  746. }
  747. }
  748. };
  749. /* ===========================================================================
  750. * Construct the Huffman tree for the bit lengths and return the index in
  751. * bl_order of the last bit length code to send.
  752. */
  753. const build_bl_tree = (s) => {
  754. let max_blindex; /* index of last bit length code of non zero freq */
  755. /* Determine the bit length frequencies for literal and distance trees */
  756. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  757. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  758. /* Build the bit length tree: */
  759. build_tree(s, s.bl_desc);
  760. /* opt_len now includes the length of the tree representations, except
  761. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  762. */
  763. /* Determine the number of bit length codes to send. The pkzip format
  764. * requires that at least 4 bit length codes be sent. (appnote.txt says
  765. * 3 but the actual value used is 4.)
  766. */
  767. for (max_blindex = BL_CODES$1 - 1; max_blindex >= 3; max_blindex--) {
  768. if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
  769. break;
  770. }
  771. }
  772. /* Update opt_len to include the bit length tree and counts */
  773. s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  774. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  775. // s->opt_len, s->static_len));
  776. return max_blindex;
  777. };
  778. /* ===========================================================================
  779. * Send the header for a block using dynamic Huffman trees: the counts, the
  780. * lengths of the bit length codes, the literal tree and the distance tree.
  781. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  782. */
  783. const send_all_trees = (s, lcodes, dcodes, blcodes) =>
  784. // deflate_state *s;
  785. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  786. {
  787. let rank; /* index in bl_order */
  788. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  789. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  790. // "too many codes");
  791. //Tracev((stderr, "\nbl counts: "));
  792. send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
  793. send_bits(s, dcodes - 1, 5);
  794. send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
  795. for (rank = 0; rank < blcodes; rank++) {
  796. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  797. send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
  798. }
  799. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  800. send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
  801. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  802. send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
  803. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  804. };
  805. /* ===========================================================================
  806. * Check if the data type is TEXT or BINARY, using the following algorithm:
  807. * - TEXT if the two conditions below are satisfied:
  808. * a) There are no non-portable control characters belonging to the
  809. * "black list" (0..6, 14..25, 28..31).
  810. * b) There is at least one printable character belonging to the
  811. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  812. * - BINARY otherwise.
  813. * - The following partially-portable control characters form a
  814. * "gray list" that is ignored in this detection algorithm:
  815. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  816. * IN assertion: the fields Freq of dyn_ltree are set.
  817. */
  818. const detect_data_type = (s) => {
  819. /* black_mask is the bit mask of black-listed bytes
  820. * set bits 0..6, 14..25, and 28..31
  821. * 0xf3ffc07f = binary 11110011111111111100000001111111
  822. */
  823. let black_mask = 0xf3ffc07f;
  824. let n;
  825. /* Check for non-textual ("black-listed") bytes. */
  826. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  827. if ((black_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
  828. return Z_BINARY;
  829. }
  830. }
  831. /* Check for textual ("white-listed") bytes. */
  832. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  833. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  834. return Z_TEXT;
  835. }
  836. for (n = 32; n < LITERALS$1; n++) {
  837. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  838. return Z_TEXT;
  839. }
  840. }
  841. /* There are no "black-listed" or "white-listed" bytes:
  842. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  843. */
  844. return Z_BINARY;
  845. };
  846. let static_init_done = false;
  847. /* ===========================================================================
  848. * Initialize the tree data structures for a new zlib stream.
  849. */
  850. const _tr_init$1 = (s) =>
  851. {
  852. if (!static_init_done) {
  853. tr_static_init();
  854. static_init_done = true;
  855. }
  856. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  857. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  858. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  859. s.bi_buf = 0;
  860. s.bi_valid = 0;
  861. /* Initialize the first block of the first file: */
  862. init_block(s);
  863. };
  864. /* ===========================================================================
  865. * Send a stored block
  866. */
  867. const _tr_stored_block$1 = (s, buf, stored_len, last) =>
  868. //DeflateState *s;
  869. //charf *buf; /* input block */
  870. //ulg stored_len; /* length of input block */
  871. //int last; /* one if this is the last block for a file */
  872. {
  873. send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
  874. copy_block(s, buf, stored_len, true); /* with header */
  875. };
  876. /* ===========================================================================
  877. * Send one empty static block to give enough lookahead for inflate.
  878. * This takes 10 bits, of which 7 may remain in the bit buffer.
  879. */
  880. const _tr_align$1 = (s) => {
  881. send_bits(s, STATIC_TREES << 1, 3);
  882. send_code(s, END_BLOCK, static_ltree);
  883. bi_flush(s);
  884. };
  885. /* ===========================================================================
  886. * Determine the best encoding for the current block: dynamic trees, static
  887. * trees or store, and output the encoded block to the zip file.
  888. */
  889. const _tr_flush_block$1 = (s, buf, stored_len, last) =>
  890. //DeflateState *s;
  891. //charf *buf; /* input block, or NULL if too old */
  892. //ulg stored_len; /* length of input block */
  893. //int last; /* one if this is the last block for a file */
  894. {
  895. let opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  896. let max_blindex = 0; /* index of last bit length code of non zero freq */
  897. /* Build the Huffman trees unless a stored block is forced */
  898. if (s.level > 0) {
  899. /* Check if the file is binary or text */
  900. if (s.strm.data_type === Z_UNKNOWN$1) {
  901. s.strm.data_type = detect_data_type(s);
  902. }
  903. /* Construct the literal and distance trees */
  904. build_tree(s, s.l_desc);
  905. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  906. // s->static_len));
  907. build_tree(s, s.d_desc);
  908. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  909. // s->static_len));
  910. /* At this point, opt_len and static_len are the total bit lengths of
  911. * the compressed block data, excluding the tree representations.
  912. */
  913. /* Build the bit length tree for the above two trees, and get the index
  914. * in bl_order of the last bit length code to send.
  915. */
  916. max_blindex = build_bl_tree(s);
  917. /* Determine the best encoding. Compute the block lengths in bytes. */
  918. opt_lenb = (s.opt_len + 3 + 7) >>> 3;
  919. static_lenb = (s.static_len + 3 + 7) >>> 3;
  920. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  921. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  922. // s->last_lit));
  923. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  924. } else {
  925. // Assert(buf != (char*)0, "lost buf");
  926. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  927. }
  928. if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
  929. /* 4: two words for the lengths */
  930. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  931. * Otherwise we can't have processed more than WSIZE input bytes since
  932. * the last block flush, because compression would have been
  933. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  934. * transform a block into a stored block.
  935. */
  936. _tr_stored_block$1(s, buf, stored_len, last);
  937. } else if (s.strategy === Z_FIXED$1 || static_lenb === opt_lenb) {
  938. send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
  939. compress_block(s, static_ltree, static_dtree);
  940. } else {
  941. send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
  942. send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
  943. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  944. }
  945. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  946. /* The above check is made mod 2^32, for files larger than 512 MB
  947. * and uLong implemented on 32 bits.
  948. */
  949. init_block(s);
  950. if (last) {
  951. bi_windup(s);
  952. }
  953. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  954. // s->compressed_len-7*last));
  955. };
  956. /* ===========================================================================
  957. * Save the match info and tally the frequency counts. Return true if
  958. * the current block must be flushed.
  959. */
  960. const _tr_tally$1 = (s, dist, lc) =>
  961. // deflate_state *s;
  962. // unsigned dist; /* distance of matched string */
  963. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  964. {
  965. //let out_length, in_length, dcode;
  966. s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
  967. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  968. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  969. s.last_lit++;
  970. if (dist === 0) {
  971. /* lc is the unmatched char */
  972. s.dyn_ltree[lc * 2]/*.Freq*/++;
  973. } else {
  974. s.matches++;
  975. /* Here, lc is the match length - MIN_MATCH */
  976. dist--; /* dist = match distance - 1 */
  977. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  978. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  979. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  980. s.dyn_ltree[(_length_code[lc] + LITERALS$1 + 1) * 2]/*.Freq*/++;
  981. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  982. }
  983. // (!) This block is disabled in zlib defaults,
  984. // don't enable it for binary compatibility
  985. //#ifdef TRUNCATE_BLOCK
  986. // /* Try to guess if it is profitable to stop the current block here */
  987. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  988. // /* Compute an upper bound for the compressed length */
  989. // out_length = s.last_lit*8;
  990. // in_length = s.strstart - s.block_start;
  991. //
  992. // for (dcode = 0; dcode < D_CODES; dcode++) {
  993. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  994. // }
  995. // out_length >>>= 3;
  996. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  997. // // s->last_lit, in_length, out_length,
  998. // // 100L - out_length*100L/in_length));
  999. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  1000. // return true;
  1001. // }
  1002. // }
  1003. //#endif
  1004. return (s.last_lit === s.lit_bufsize - 1);
  1005. /* We avoid equality with lit_bufsize because of wraparound at 64K
  1006. * on 16 bit machines and because stored blocks are restricted to
  1007. * 64K-1 bytes.
  1008. */
  1009. };
  1010. var _tr_init_1 = _tr_init$1;
  1011. var _tr_stored_block_1 = _tr_stored_block$1;
  1012. var _tr_flush_block_1 = _tr_flush_block$1;
  1013. var _tr_tally_1 = _tr_tally$1;
  1014. var _tr_align_1 = _tr_align$1;
  1015. var trees = {
  1016. _tr_init: _tr_init_1,
  1017. _tr_stored_block: _tr_stored_block_1,
  1018. _tr_flush_block: _tr_flush_block_1,
  1019. _tr_tally: _tr_tally_1,
  1020. _tr_align: _tr_align_1
  1021. };
  1022. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  1023. // It isn't worth it to make additional optimizations as in original.
  1024. // Small size is preferable.
  1025. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1026. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1027. //
  1028. // This software is provided 'as-is', without any express or implied
  1029. // warranty. In no event will the authors be held liable for any damages
  1030. // arising from the use of this software.
  1031. //
  1032. // Permission is granted to anyone to use this software for any purpose,
  1033. // including commercial applications, and to alter it and redistribute it
  1034. // freely, subject to the following restrictions:
  1035. //
  1036. // 1. The origin of this software must not be misrepresented; you must not
  1037. // claim that you wrote the original software. If you use this software
  1038. // in a product, an acknowledgment in the product documentation would be
  1039. // appreciated but is not required.
  1040. // 2. Altered source versions must be plainly marked as such, and must not be
  1041. // misrepresented as being the original software.
  1042. // 3. This notice may not be removed or altered from any source distribution.
  1043. const adler32 = (adler, buf, len, pos) => {
  1044. let s1 = (adler & 0xffff) |0,
  1045. s2 = ((adler >>> 16) & 0xffff) |0,
  1046. n = 0;
  1047. while (len !== 0) {
  1048. // Set limit ~ twice less than 5552, to keep
  1049. // s2 in 31-bits, because we force signed ints.
  1050. // in other case %= will fail.
  1051. n = len > 2000 ? 2000 : len;
  1052. len -= n;
  1053. do {
  1054. s1 = (s1 + buf[pos++]) |0;
  1055. s2 = (s2 + s1) |0;
  1056. } while (--n);
  1057. s1 %= 65521;
  1058. s2 %= 65521;
  1059. }
  1060. return (s1 | (s2 << 16)) |0;
  1061. };
  1062. var adler32_1 = adler32;
  1063. // Note: we can't get significant speed boost here.
  1064. // So write code to minimize size - no pregenerated tables
  1065. // and array tools dependencies.
  1066. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1067. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1068. //
  1069. // This software is provided 'as-is', without any express or implied
  1070. // warranty. In no event will the authors be held liable for any damages
  1071. // arising from the use of this software.
  1072. //
  1073. // Permission is granted to anyone to use this software for any purpose,
  1074. // including commercial applications, and to alter it and redistribute it
  1075. // freely, subject to the following restrictions:
  1076. //
  1077. // 1. The origin of this software must not be misrepresented; you must not
  1078. // claim that you wrote the original software. If you use this software
  1079. // in a product, an acknowledgment in the product documentation would be
  1080. // appreciated but is not required.
  1081. // 2. Altered source versions must be plainly marked as such, and must not be
  1082. // misrepresented as being the original software.
  1083. // 3. This notice may not be removed or altered from any source distribution.
  1084. // Use ordinary array, since untyped makes no boost here
  1085. const makeTable = () => {
  1086. let c, table = [];
  1087. for (var n = 0; n < 256; n++) {
  1088. c = n;
  1089. for (var k = 0; k < 8; k++) {
  1090. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  1091. }
  1092. table[n] = c;
  1093. }
  1094. return table;
  1095. };
  1096. // Create table on load. Just 255 signed longs. Not a problem.
  1097. const crcTable = new Uint32Array(makeTable());
  1098. const crc32 = (crc, buf, len, pos) => {
  1099. const t = crcTable;
  1100. const end = pos + len;
  1101. crc ^= -1;
  1102. for (let i = pos; i < end; i++) {
  1103. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  1104. }
  1105. return (crc ^ (-1)); // >>> 0;
  1106. };
  1107. var crc32_1 = crc32;
  1108. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1109. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1110. //
  1111. // This software is provided 'as-is', without any express or implied
  1112. // warranty. In no event will the authors be held liable for any damages
  1113. // arising from the use of this software.
  1114. //
  1115. // Permission is granted to anyone to use this software for any purpose,
  1116. // including commercial applications, and to alter it and redistribute it
  1117. // freely, subject to the following restrictions:
  1118. //
  1119. // 1. The origin of this software must not be misrepresented; you must not
  1120. // claim that you wrote the original software. If you use this software
  1121. // in a product, an acknowledgment in the product documentation would be
  1122. // appreciated but is not required.
  1123. // 2. Altered source versions must be plainly marked as such, and must not be
  1124. // misrepresented as being the original software.
  1125. // 3. This notice may not be removed or altered from any source distribution.
  1126. var messages = {
  1127. 2: 'need dictionary', /* Z_NEED_DICT 2 */
  1128. 1: 'stream end', /* Z_STREAM_END 1 */
  1129. 0: '', /* Z_OK 0 */
  1130. '-1': 'file error', /* Z_ERRNO (-1) */
  1131. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  1132. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  1133. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  1134. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  1135. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  1136. };
  1137. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1138. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1139. //
  1140. // This software is provided 'as-is', without any express or implied
  1141. // warranty. In no event will the authors be held liable for any damages
  1142. // arising from the use of this software.
  1143. //
  1144. // Permission is granted to anyone to use this software for any purpose,
  1145. // including commercial applications, and to alter it and redistribute it
  1146. // freely, subject to the following restrictions:
  1147. //
  1148. // 1. The origin of this software must not be misrepresented; you must not
  1149. // claim that you wrote the original software. If you use this software
  1150. // in a product, an acknowledgment in the product documentation would be
  1151. // appreciated but is not required.
  1152. // 2. Altered source versions must be plainly marked as such, and must not be
  1153. // misrepresented as being the original software.
  1154. // 3. This notice may not be removed or altered from any source distribution.
  1155. var constants$2 = {
  1156. /* Allowed flush values; see deflate() and inflate() below for details */
  1157. Z_NO_FLUSH: 0,
  1158. Z_PARTIAL_FLUSH: 1,
  1159. Z_SYNC_FLUSH: 2,
  1160. Z_FULL_FLUSH: 3,
  1161. Z_FINISH: 4,
  1162. Z_BLOCK: 5,
  1163. Z_TREES: 6,
  1164. /* Return codes for the compression/decompression functions. Negative values
  1165. * are errors, positive values are used for special but normal events.
  1166. */
  1167. Z_OK: 0,
  1168. Z_STREAM_END: 1,
  1169. Z_NEED_DICT: 2,
  1170. Z_ERRNO: -1,
  1171. Z_STREAM_ERROR: -2,
  1172. Z_DATA_ERROR: -3,
  1173. Z_MEM_ERROR: -4,
  1174. Z_BUF_ERROR: -5,
  1175. //Z_VERSION_ERROR: -6,
  1176. /* compression levels */
  1177. Z_NO_COMPRESSION: 0,
  1178. Z_BEST_SPEED: 1,
  1179. Z_BEST_COMPRESSION: 9,
  1180. Z_DEFAULT_COMPRESSION: -1,
  1181. Z_FILTERED: 1,
  1182. Z_HUFFMAN_ONLY: 2,
  1183. Z_RLE: 3,
  1184. Z_FIXED: 4,
  1185. Z_DEFAULT_STRATEGY: 0,
  1186. /* Possible values of the data_type field (though see inflate()) */
  1187. Z_BINARY: 0,
  1188. Z_TEXT: 1,
  1189. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  1190. Z_UNKNOWN: 2,
  1191. /* The deflate compression method */
  1192. Z_DEFLATED: 8
  1193. //Z_NULL: null // Use -1 or null inline, depending on var type
  1194. };
  1195. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  1196. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  1197. //
  1198. // This software is provided 'as-is', without any express or implied
  1199. // warranty. In no event will the authors be held liable for any damages
  1200. // arising from the use of this software.
  1201. //
  1202. // Permission is granted to anyone to use this software for any purpose,
  1203. // including commercial applications, and to alter it and redistribute it
  1204. // freely, subject to the following restrictions:
  1205. //
  1206. // 1. The origin of this software must not be misrepresented; you must not
  1207. // claim that you wrote the original software. If you use this software
  1208. // in a product, an acknowledgment in the product documentation would be
  1209. // appreciated but is not required.
  1210. // 2. Altered source versions must be plainly marked as such, and must not be
  1211. // misrepresented as being the original software.
  1212. // 3. This notice may not be removed or altered from any source distribution.
  1213. const { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } = trees;
  1214. /* Public constants ==========================================================*/
  1215. /* ===========================================================================*/
  1216. const {
  1217. Z_NO_FLUSH: Z_NO_FLUSH$2, Z_PARTIAL_FLUSH, Z_FULL_FLUSH: Z_FULL_FLUSH$1, Z_FINISH: Z_FINISH$3, Z_BLOCK: Z_BLOCK$1,
  1218. Z_OK: Z_OK$3, Z_STREAM_END: Z_STREAM_END$3, Z_STREAM_ERROR: Z_STREAM_ERROR$2, Z_DATA_ERROR: Z_DATA_ERROR$2, Z_BUF_ERROR: Z_BUF_ERROR$1,
  1219. Z_DEFAULT_COMPRESSION: Z_DEFAULT_COMPRESSION$1,
  1220. Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED, Z_DEFAULT_STRATEGY: Z_DEFAULT_STRATEGY$1,
  1221. Z_UNKNOWN,
  1222. Z_DEFLATED: Z_DEFLATED$2
  1223. } = constants$2;
  1224. /*============================================================================*/
  1225. const MAX_MEM_LEVEL = 9;
  1226. /* Maximum value for memLevel in deflateInit2 */
  1227. const MAX_WBITS$1 = 15;
  1228. /* 32K LZ77 window */
  1229. const DEF_MEM_LEVEL = 8;
  1230. const LENGTH_CODES = 29;
  1231. /* number of length codes, not counting the special END_BLOCK code */
  1232. const LITERALS = 256;
  1233. /* number of literal bytes 0..255 */
  1234. const L_CODES = LITERALS + 1 + LENGTH_CODES;
  1235. /* number of Literal or Length codes, including the END_BLOCK code */
  1236. const D_CODES = 30;
  1237. /* number of distance codes */
  1238. const BL_CODES = 19;
  1239. /* number of codes used to transfer the bit lengths */
  1240. const HEAP_SIZE = 2 * L_CODES + 1;
  1241. /* maximum heap size */
  1242. const MAX_BITS = 15;
  1243. /* All codes must not exceed MAX_BITS bits */
  1244. const MIN_MATCH = 3;
  1245. const MAX_MATCH = 258;
  1246. const MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  1247. const PRESET_DICT = 0x20;
  1248. const INIT_STATE = 42;
  1249. const EXTRA_STATE = 69;
  1250. const NAME_STATE = 73;
  1251. const COMMENT_STATE = 91;
  1252. const HCRC_STATE = 103;
  1253. const BUSY_STATE = 113;
  1254. const FINISH_STATE = 666;
  1255. const BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  1256. const BS_BLOCK_DONE = 2; /* block flush performed */
  1257. const BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  1258. const BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  1259. const OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  1260. const err = (strm, errorCode) => {
  1261. strm.msg = messages[errorCode];
  1262. return errorCode;
  1263. };
  1264. const rank = (f) => {
  1265. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  1266. };
  1267. const zero = (buf) => {
  1268. let len = buf.length; while (--len >= 0) { buf[len] = 0; }
  1269. };
  1270. /* eslint-disable new-cap */
  1271. let HASH_ZLIB = (s, prev, data) => ((prev << s.hash_shift) ^ data) & s.hash_mask;
  1272. // This hash causes less collisions, https://github.com/nodeca/pako/issues/135
  1273. // But breaks binary compatibility
  1274. //let HASH_FAST = (s, prev, data) => ((prev << 8) + (prev >> 8) + (data << 4)) & s.hash_mask;
  1275. let HASH = HASH_ZLIB;
  1276. /* =========================================================================
  1277. * Flush as much pending output as possible. All deflate() output goes
  1278. * through this function so some applications may wish to modify it
  1279. * to avoid allocating a large strm->output buffer and copying into it.
  1280. * (See also read_buf()).
  1281. */
  1282. const flush_pending = (strm) => {
  1283. const s = strm.state;
  1284. //_tr_flush_bits(s);
  1285. let len = s.pending;
  1286. if (len > strm.avail_out) {
  1287. len = strm.avail_out;
  1288. }
  1289. if (len === 0) { return; }
  1290. strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out);
  1291. strm.next_out += len;
  1292. s.pending_out += len;
  1293. strm.total_out += len;
  1294. strm.avail_out -= len;
  1295. s.pending -= len;
  1296. if (s.pending === 0) {
  1297. s.pending_out = 0;
  1298. }
  1299. };
  1300. const flush_block_only = (s, last) => {
  1301. _tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  1302. s.block_start = s.strstart;
  1303. flush_pending(s.strm);
  1304. };
  1305. const put_byte = (s, b) => {
  1306. s.pending_buf[s.pending++] = b;
  1307. };
  1308. /* =========================================================================
  1309. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  1310. * IN assertion: the stream state is correct and there is enough room in
  1311. * pending_buf.
  1312. */
  1313. const putShortMSB = (s, b) => {
  1314. // put_byte(s, (Byte)(b >> 8));
  1315. // put_byte(s, (Byte)(b & 0xff));
  1316. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  1317. s.pending_buf[s.pending++] = b & 0xff;
  1318. };
  1319. /* ===========================================================================
  1320. * Read a new buffer from the current input stream, update the adler32
  1321. * and total number of bytes read. All deflate() input goes through
  1322. * this function so some applications may wish to modify it to avoid
  1323. * allocating a large strm->input buffer and copying from it.
  1324. * (See also flush_pending()).
  1325. */
  1326. const read_buf = (strm, buf, start, size) => {
  1327. let len = strm.avail_in;
  1328. if (len > size) { len = size; }
  1329. if (len === 0) { return 0; }
  1330. strm.avail_in -= len;
  1331. // zmemcpy(buf, strm->next_in, len);
  1332. buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start);
  1333. if (strm.state.wrap === 1) {
  1334. strm.adler = adler32_1(strm.adler, buf, len, start);
  1335. }
  1336. else if (strm.state.wrap === 2) {
  1337. strm.adler = crc32_1(strm.adler, buf, len, start);
  1338. }
  1339. strm.next_in += len;
  1340. strm.total_in += len;
  1341. return len;
  1342. };
  1343. /* ===========================================================================
  1344. * Set match_start to the longest match starting at the given string and
  1345. * return its length. Matches shorter or equal to prev_length are discarded,
  1346. * in which case the result is equal to prev_length and match_start is
  1347. * garbage.
  1348. * IN assertions: cur_match is the head of the hash chain for the current
  1349. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1350. * OUT assertion: the match length is not greater than s->lookahead.
  1351. */
  1352. const longest_match = (s, cur_match) => {
  1353. let chain_length = s.max_chain_length; /* max hash chain length */
  1354. let scan = s.strstart; /* current string */
  1355. let match; /* matched string */
  1356. let len; /* length of current match */
  1357. let best_len = s.prev_length; /* best match length so far */
  1358. let nice_match = s.nice_match; /* stop if match long enough */
  1359. const limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  1360. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  1361. const _win = s.window; // shortcut
  1362. const wmask = s.w_mask;
  1363. const prev = s.prev;
  1364. /* Stop when cur_match becomes <= limit. To simplify the code,
  1365. * we prevent matches with the string of window index 0.
  1366. */
  1367. const strend = s.strstart + MAX_MATCH;
  1368. let scan_end1 = _win[scan + best_len - 1];
  1369. let scan_end = _win[scan + best_len];
  1370. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1371. * It is easy to get rid of this optimization if necessary.
  1372. */
  1373. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1374. /* Do not waste too much time if we already have a good match: */
  1375. if (s.prev_length >= s.good_match) {
  1376. chain_length >>= 2;
  1377. }
  1378. /* Do not look for matches beyond the end of the input. This is necessary
  1379. * to make deflate deterministic.
  1380. */
  1381. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  1382. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  1383. do {
  1384. // Assert(cur_match < s->strstart, "no future");
  1385. match = cur_match;
  1386. /* Skip to next match if the match length cannot increase
  1387. * or if the match length is less than 2. Note that the checks below
  1388. * for insufficient lookahead only occur occasionally for performance
  1389. * reasons. Therefore uninitialized memory will be accessed, and
  1390. * conditional jumps will be made that depend on those values.
  1391. * However the length of the match is limited to the lookahead, so
  1392. * the output of deflate is not affected by the uninitialized values.
  1393. */
  1394. if (_win[match + best_len] !== scan_end ||
  1395. _win[match + best_len - 1] !== scan_end1 ||
  1396. _win[match] !== _win[scan] ||
  1397. _win[++match] !== _win[scan + 1]) {
  1398. continue;
  1399. }
  1400. /* The check at best_len-1 can be removed because it will be made
  1401. * again later. (This heuristic is not always a win.)
  1402. * It is not necessary to compare scan[2] and match[2] since they
  1403. * are always equal when the other bytes match, given that
  1404. * the hash keys are equal and that HASH_BITS >= 8.
  1405. */
  1406. scan += 2;
  1407. match++;
  1408. // Assert(*scan == *match, "match[2]?");
  1409. /* We check for insufficient lookahead only every 8th comparison;
  1410. * the 256th check will be made at strstart+258.
  1411. */
  1412. do {
  1413. /*jshint noempty:false*/
  1414. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1415. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1416. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1417. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  1418. scan < strend);
  1419. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1420. len = MAX_MATCH - (strend - scan);
  1421. scan = strend - MAX_MATCH;
  1422. if (len > best_len) {
  1423. s.match_start = cur_match;
  1424. best_len = len;
  1425. if (len >= nice_match) {
  1426. break;
  1427. }
  1428. scan_end1 = _win[scan + best_len - 1];
  1429. scan_end = _win[scan + best_len];
  1430. }
  1431. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  1432. if (best_len <= s.lookahead) {
  1433. return best_len;
  1434. }
  1435. return s.lookahead;
  1436. };
  1437. /* ===========================================================================
  1438. * Fill the window when the lookahead becomes insufficient.
  1439. * Updates strstart and lookahead.
  1440. *
  1441. * IN assertion: lookahead < MIN_LOOKAHEAD
  1442. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1443. * At least one byte has been read, or avail_in == 0; reads are
  1444. * performed for at least two bytes (required for the zip translate_eol
  1445. * option -- not supported here).
  1446. */
  1447. const fill_window = (s) => {
  1448. const _w_size = s.w_size;
  1449. let p, n, m, more, str;
  1450. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  1451. do {
  1452. more = s.window_size - s.lookahead - s.strstart;
  1453. // JS ints have 32 bit, block below not needed
  1454. /* Deal with !@#$% 64K limit: */
  1455. //if (sizeof(int) <= 2) {
  1456. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1457. // more = wsize;
  1458. //
  1459. // } else if (more == (unsigned)(-1)) {
  1460. // /* Very unlikely, but possible on 16 bit machine if
  1461. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  1462. // */
  1463. // more--;
  1464. // }
  1465. //}
  1466. /* If the window is almost full and there is insufficient lookahead,
  1467. * move the upper half to the lower one to make room in the upper half.
  1468. */
  1469. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  1470. s.window.set(s.window.subarray(_w_size, _w_size + _w_size), 0);
  1471. s.match_start -= _w_size;
  1472. s.strstart -= _w_size;
  1473. /* we now have strstart >= MAX_DIST */
  1474. s.block_start -= _w_size;
  1475. /* Slide the hash table (could be avoided with 32 bit values
  1476. at the expense of memory usage). We slide even when level == 0
  1477. to keep the hash table consistent if we switch back to level > 0
  1478. later. (Using level 0 permanently is not an optimal usage of
  1479. zlib, so we don't care about this pathological case.)
  1480. */
  1481. n = s.hash_size;
  1482. p = n;
  1483. do {
  1484. m = s.head[--p];
  1485. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  1486. } while (--n);
  1487. n = _w_size;
  1488. p = n;
  1489. do {
  1490. m = s.prev[--p];
  1491. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  1492. /* If n is not on any hash chain, prev[n] is garbage but
  1493. * its value will never be used.
  1494. */
  1495. } while (--n);
  1496. more += _w_size;
  1497. }
  1498. if (s.strm.avail_in === 0) {
  1499. break;
  1500. }
  1501. /* If there was no sliding:
  1502. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1503. * more == window_size - lookahead - strstart
  1504. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1505. * => more >= window_size - 2*WSIZE + 2
  1506. * In the BIG_MEM or MMAP case (not yet supported),
  1507. * window_size == input_size + MIN_LOOKAHEAD &&
  1508. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1509. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1510. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1511. */
  1512. //Assert(more >= 2, "more < 2");
  1513. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  1514. s.lookahead += n;
  1515. /* Initialize the hash value now that we have some input: */
  1516. if (s.lookahead + s.insert >= MIN_MATCH) {
  1517. str = s.strstart - s.insert;
  1518. s.ins_h = s.window[str];
  1519. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  1520. s.ins_h = HASH(s, s.ins_h, s.window[str + 1]);
  1521. //#if MIN_MATCH != 3
  1522. // Call update_hash() MIN_MATCH-3 more times
  1523. //#endif
  1524. while (s.insert) {
  1525. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  1526. s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
  1527. s.prev[str & s.w_mask] = s.head[s.ins_h];
  1528. s.head[s.ins_h] = str;
  1529. str++;
  1530. s.insert--;
  1531. if (s.lookahead + s.insert < MIN_MATCH) {
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1537. * but this is not important since only literal bytes will be emitted.
  1538. */
  1539. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  1540. /* If the WIN_INIT bytes after the end of the current data have never been
  1541. * written, then zero those bytes in order to avoid memory check reports of
  1542. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  1543. * the longest match routines. Update the high water mark for the next
  1544. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  1545. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  1546. */
  1547. // if (s.high_water < s.window_size) {
  1548. // const curr = s.strstart + s.lookahead;
  1549. // let init = 0;
  1550. //
  1551. // if (s.high_water < curr) {
  1552. // /* Previous high water mark below current data -- zero WIN_INIT
  1553. // * bytes or up to end of window, whichever is less.
  1554. // */
  1555. // init = s.window_size - curr;
  1556. // if (init > WIN_INIT)
  1557. // init = WIN_INIT;
  1558. // zmemzero(s->window + curr, (unsigned)init);
  1559. // s->high_water = curr + init;
  1560. // }
  1561. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  1562. // /* High water mark at or above current data, but below current data
  1563. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  1564. // * to end of window, whichever is less.
  1565. // */
  1566. // init = (ulg)curr + WIN_INIT - s->high_water;
  1567. // if (init > s->window_size - s->high_water)
  1568. // init = s->window_size - s->high_water;
  1569. // zmemzero(s->window + s->high_water, (unsigned)init);
  1570. // s->high_water += init;
  1571. // }
  1572. // }
  1573. //
  1574. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1575. // "not enough room for search");
  1576. };
  1577. /* ===========================================================================
  1578. * Copy without compression as much as possible from the input stream, return
  1579. * the current block state.
  1580. * This function does not insert new strings in the dictionary since
  1581. * uncompressible data is probably not useful. This function is used
  1582. * only for the level=0 compression option.
  1583. * NOTE: this function should be optimized to avoid extra copying from
  1584. * window to pending_buf.
  1585. */
  1586. const deflate_stored = (s, flush) => {
  1587. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1588. * to pending_buf_size, and each stored block has a 5 byte header:
  1589. */
  1590. let max_block_size = 0xffff;
  1591. if (max_block_size > s.pending_buf_size - 5) {
  1592. max_block_size = s.pending_buf_size - 5;
  1593. }
  1594. /* Copy as much as possible from input to output: */
  1595. for (;;) {
  1596. /* Fill the window as much as possible: */
  1597. if (s.lookahead <= 1) {
  1598. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1599. // s->block_start >= (long)s->w_size, "slide too late");
  1600. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  1601. // s.block_start >= s.w_size)) {
  1602. // throw new Error("slide too late");
  1603. // }
  1604. fill_window(s);
  1605. if (s.lookahead === 0 && flush === Z_NO_FLUSH$2) {
  1606. return BS_NEED_MORE;
  1607. }
  1608. if (s.lookahead === 0) {
  1609. break;
  1610. }
  1611. /* flush the current block */
  1612. }
  1613. //Assert(s->block_start >= 0L, "block gone");
  1614. // if (s.block_start < 0) throw new Error("block gone");
  1615. s.strstart += s.lookahead;
  1616. s.lookahead = 0;
  1617. /* Emit a stored block if pending_buf will be full: */
  1618. const max_start = s.block_start + max_block_size;
  1619. if (s.strstart === 0 || s.strstart >= max_start) {
  1620. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1621. s.lookahead = s.strstart - max_start;
  1622. s.strstart = max_start;
  1623. /*** FLUSH_BLOCK(s, 0); ***/
  1624. flush_block_only(s, false);
  1625. if (s.strm.avail_out === 0) {
  1626. return BS_NEED_MORE;
  1627. }
  1628. /***/
  1629. }
  1630. /* Flush if we may have to slide, otherwise block_start may become
  1631. * negative and the data will be gone:
  1632. */
  1633. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  1634. /*** FLUSH_BLOCK(s, 0); ***/
  1635. flush_block_only(s, false);
  1636. if (s.strm.avail_out === 0) {
  1637. return BS_NEED_MORE;
  1638. }
  1639. /***/
  1640. }
  1641. }
  1642. s.insert = 0;
  1643. if (flush === Z_FINISH$3) {
  1644. /*** FLUSH_BLOCK(s, 1); ***/
  1645. flush_block_only(s, true);
  1646. if (s.strm.avail_out === 0) {
  1647. return BS_FINISH_STARTED;
  1648. }
  1649. /***/
  1650. return BS_FINISH_DONE;
  1651. }
  1652. if (s.strstart > s.block_start) {
  1653. /*** FLUSH_BLOCK(s, 0); ***/
  1654. flush_block_only(s, false);
  1655. if (s.strm.avail_out === 0) {
  1656. return BS_NEED_MORE;
  1657. }
  1658. /***/
  1659. }
  1660. return BS_NEED_MORE;
  1661. };
  1662. /* ===========================================================================
  1663. * Compress as much as possible from the input stream, return the current
  1664. * block state.
  1665. * This function does not perform lazy evaluation of matches and inserts
  1666. * new strings in the dictionary only for unmatched strings or for short
  1667. * matches. It is used only for the fast compression options.
  1668. */
  1669. const deflate_fast = (s, flush) => {
  1670. let hash_head; /* head of the hash chain */
  1671. let bflush; /* set if current block must be flushed */
  1672. for (;;) {
  1673. /* Make sure that we always have enough lookahead, except
  1674. * at the end of the input file. We need MAX_MATCH bytes
  1675. * for the next match, plus MIN_MATCH bytes to insert the
  1676. * string following the next match.
  1677. */
  1678. if (s.lookahead < MIN_LOOKAHEAD) {
  1679. fill_window(s);
  1680. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) {
  1681. return BS_NEED_MORE;
  1682. }
  1683. if (s.lookahead === 0) {
  1684. break; /* flush the current block */
  1685. }
  1686. }
  1687. /* Insert the string window[strstart .. strstart+2] in the
  1688. * dictionary, and set hash_head to the head of the hash chain:
  1689. */
  1690. hash_head = 0/*NIL*/;
  1691. if (s.lookahead >= MIN_MATCH) {
  1692. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1693. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  1694. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1695. s.head[s.ins_h] = s.strstart;
  1696. /***/
  1697. }
  1698. /* Find the longest match, discarding those <= prev_length.
  1699. * At this point we have always match_length < MIN_MATCH
  1700. */
  1701. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  1702. /* To simplify the code, we prevent matches with the string
  1703. * of window index 0 (in particular we have to avoid a match
  1704. * of the string with itself at the start of the input file).
  1705. */
  1706. s.match_length = longest_match(s, hash_head);
  1707. /* longest_match() sets match_start */
  1708. }
  1709. if (s.match_length >= MIN_MATCH) {
  1710. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  1711. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  1712. s.match_length - MIN_MATCH, bflush); ***/
  1713. bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  1714. s.lookahead -= s.match_length;
  1715. /* Insert new strings in the hash table only if the match length
  1716. * is not too large. This saves time but degrades compression.
  1717. */
  1718. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  1719. s.match_length--; /* string at strstart already in table */
  1720. do {
  1721. s.strstart++;
  1722. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1723. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  1724. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1725. s.head[s.ins_h] = s.strstart;
  1726. /***/
  1727. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1728. * always MIN_MATCH bytes ahead.
  1729. */
  1730. } while (--s.match_length !== 0);
  1731. s.strstart++;
  1732. } else
  1733. {
  1734. s.strstart += s.match_length;
  1735. s.match_length = 0;
  1736. s.ins_h = s.window[s.strstart];
  1737. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  1738. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]);
  1739. //#if MIN_MATCH != 3
  1740. // Call UPDATE_HASH() MIN_MATCH-3 more times
  1741. //#endif
  1742. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1743. * matter since it will be recomputed at next deflate call.
  1744. */
  1745. }
  1746. } else {
  1747. /* No match, output a literal byte */
  1748. //Tracevv((stderr,"%c", s.window[s.strstart]));
  1749. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1750. bflush = _tr_tally(s, 0, s.window[s.strstart]);
  1751. s.lookahead--;
  1752. s.strstart++;
  1753. }
  1754. if (bflush) {
  1755. /*** FLUSH_BLOCK(s, 0); ***/
  1756. flush_block_only(s, false);
  1757. if (s.strm.avail_out === 0) {
  1758. return BS_NEED_MORE;
  1759. }
  1760. /***/
  1761. }
  1762. }
  1763. s.insert = ((s.strstart < (MIN_MATCH - 1)) ? s.strstart : MIN_MATCH - 1);
  1764. if (flush === Z_FINISH$3) {
  1765. /*** FLUSH_BLOCK(s, 1); ***/
  1766. flush_block_only(s, true);
  1767. if (s.strm.avail_out === 0) {
  1768. return BS_FINISH_STARTED;
  1769. }
  1770. /***/
  1771. return BS_FINISH_DONE;
  1772. }
  1773. if (s.last_lit) {
  1774. /*** FLUSH_BLOCK(s, 0); ***/
  1775. flush_block_only(s, false);
  1776. if (s.strm.avail_out === 0) {
  1777. return BS_NEED_MORE;
  1778. }
  1779. /***/
  1780. }
  1781. return BS_BLOCK_DONE;
  1782. };
  1783. /* ===========================================================================
  1784. * Same as above, but achieves better compression. We use a lazy
  1785. * evaluation for matches: a match is finally adopted only if there is
  1786. * no better match at the next window position.
  1787. */
  1788. const deflate_slow = (s, flush) => {
  1789. let hash_head; /* head of hash chain */
  1790. let bflush; /* set if current block must be flushed */
  1791. let max_insert;
  1792. /* Process the input block. */
  1793. for (;;) {
  1794. /* Make sure that we always have enough lookahead, except
  1795. * at the end of the input file. We need MAX_MATCH bytes
  1796. * for the next match, plus MIN_MATCH bytes to insert the
  1797. * string following the next match.
  1798. */
  1799. if (s.lookahead < MIN_LOOKAHEAD) {
  1800. fill_window(s);
  1801. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) {
  1802. return BS_NEED_MORE;
  1803. }
  1804. if (s.lookahead === 0) { break; } /* flush the current block */
  1805. }
  1806. /* Insert the string window[strstart .. strstart+2] in the
  1807. * dictionary, and set hash_head to the head of the hash chain:
  1808. */
  1809. hash_head = 0/*NIL*/;
  1810. if (s.lookahead >= MIN_MATCH) {
  1811. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1812. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  1813. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1814. s.head[s.ins_h] = s.strstart;
  1815. /***/
  1816. }
  1817. /* Find the longest match, discarding those <= prev_length.
  1818. */
  1819. s.prev_length = s.match_length;
  1820. s.prev_match = s.match_start;
  1821. s.match_length = MIN_MATCH - 1;
  1822. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  1823. s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  1824. /* To simplify the code, we prevent matches with the string
  1825. * of window index 0 (in particular we have to avoid a match
  1826. * of the string with itself at the start of the input file).
  1827. */
  1828. s.match_length = longest_match(s, hash_head);
  1829. /* longest_match() sets match_start */
  1830. if (s.match_length <= 5 &&
  1831. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  1832. /* If prev_match is also MIN_MATCH, match_start is garbage
  1833. * but we will ignore the current match anyway.
  1834. */
  1835. s.match_length = MIN_MATCH - 1;
  1836. }
  1837. }
  1838. /* If there was a match at the previous step and the current
  1839. * match is not better, output the previous match:
  1840. */
  1841. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  1842. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  1843. /* Do not insert strings in hash table beyond this. */
  1844. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  1845. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  1846. s.prev_length - MIN_MATCH, bflush);***/
  1847. bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
  1848. /* Insert in hash table all strings up to the end of the match.
  1849. * strstart-1 and strstart are already inserted. If there is not
  1850. * enough lookahead, the last two strings are not inserted in
  1851. * the hash table.
  1852. */
  1853. s.lookahead -= s.prev_length - 1;
  1854. s.prev_length -= 2;
  1855. do {
  1856. if (++s.strstart <= max_insert) {
  1857. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  1858. s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
  1859. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  1860. s.head[s.ins_h] = s.strstart;
  1861. /***/
  1862. }
  1863. } while (--s.prev_length !== 0);
  1864. s.match_available = 0;
  1865. s.match_length = MIN_MATCH - 1;
  1866. s.strstart++;
  1867. if (bflush) {
  1868. /*** FLUSH_BLOCK(s, 0); ***/
  1869. flush_block_only(s, false);
  1870. if (s.strm.avail_out === 0) {
  1871. return BS_NEED_MORE;
  1872. }
  1873. /***/
  1874. }
  1875. } else if (s.match_available) {
  1876. /* If there was no match at the previous position, output a
  1877. * single literal. If there was a match but the current match
  1878. * is longer, truncate the previous match to a single literal.
  1879. */
  1880. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1881. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1882. bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
  1883. if (bflush) {
  1884. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  1885. flush_block_only(s, false);
  1886. /***/
  1887. }
  1888. s.strstart++;
  1889. s.lookahead--;
  1890. if (s.strm.avail_out === 0) {
  1891. return BS_NEED_MORE;
  1892. }
  1893. } else {
  1894. /* There is no previous match to compare with, wait for
  1895. * the next step to decide.
  1896. */
  1897. s.match_available = 1;
  1898. s.strstart++;
  1899. s.lookahead--;
  1900. }
  1901. }
  1902. //Assert (flush != Z_NO_FLUSH, "no flush?");
  1903. if (s.match_available) {
  1904. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1905. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  1906. bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
  1907. s.match_available = 0;
  1908. }
  1909. s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
  1910. if (flush === Z_FINISH$3) {
  1911. /*** FLUSH_BLOCK(s, 1); ***/
  1912. flush_block_only(s, true);
  1913. if (s.strm.avail_out === 0) {
  1914. return BS_FINISH_STARTED;
  1915. }
  1916. /***/
  1917. return BS_FINISH_DONE;
  1918. }
  1919. if (s.last_lit) {
  1920. /*** FLUSH_BLOCK(s, 0); ***/
  1921. flush_block_only(s, false);
  1922. if (s.strm.avail_out === 0) {
  1923. return BS_NEED_MORE;
  1924. }
  1925. /***/
  1926. }
  1927. return BS_BLOCK_DONE;
  1928. };
  1929. /* ===========================================================================
  1930. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1931. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1932. * deflate switches away from Z_RLE.)
  1933. */
  1934. const deflate_rle = (s, flush) => {
  1935. let bflush; /* set if current block must be flushed */
  1936. let prev; /* byte at distance one to match */
  1937. let scan, strend; /* scan goes up to strend for length of run */
  1938. const _win = s.window;
  1939. for (;;) {
  1940. /* Make sure that we always have enough lookahead, except
  1941. * at the end of the input file. We need MAX_MATCH bytes
  1942. * for the longest run, plus one for the unrolled loop.
  1943. */
  1944. if (s.lookahead <= MAX_MATCH) {
  1945. fill_window(s);
  1946. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH$2) {
  1947. return BS_NEED_MORE;
  1948. }
  1949. if (s.lookahead === 0) { break; } /* flush the current block */
  1950. }
  1951. /* See how many times the previous byte repeats */
  1952. s.match_length = 0;
  1953. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  1954. scan = s.strstart - 1;
  1955. prev = _win[scan];
  1956. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  1957. strend = s.strstart + MAX_MATCH;
  1958. do {
  1959. /*jshint noempty:false*/
  1960. } while (prev === _win[++scan] && prev === _win[++scan] &&
  1961. prev === _win[++scan] && prev === _win[++scan] &&
  1962. prev === _win[++scan] && prev === _win[++scan] &&
  1963. prev === _win[++scan] && prev === _win[++scan] &&
  1964. scan < strend);
  1965. s.match_length = MAX_MATCH - (strend - scan);
  1966. if (s.match_length > s.lookahead) {
  1967. s.match_length = s.lookahead;
  1968. }
  1969. }
  1970. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  1971. }
  1972. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1973. if (s.match_length >= MIN_MATCH) {
  1974. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  1975. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  1976. bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH);
  1977. s.lookahead -= s.match_length;
  1978. s.strstart += s.match_length;
  1979. s.match_length = 0;
  1980. } else {
  1981. /* No match, output a literal byte */
  1982. //Tracevv((stderr,"%c", s->window[s->strstart]));
  1983. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  1984. bflush = _tr_tally(s, 0, s.window[s.strstart]);
  1985. s.lookahead--;
  1986. s.strstart++;
  1987. }
  1988. if (bflush) {
  1989. /*** FLUSH_BLOCK(s, 0); ***/
  1990. flush_block_only(s, false);
  1991. if (s.strm.avail_out === 0) {
  1992. return BS_NEED_MORE;
  1993. }
  1994. /***/
  1995. }
  1996. }
  1997. s.insert = 0;
  1998. if (flush === Z_FINISH$3) {
  1999. /*** FLUSH_BLOCK(s, 1); ***/
  2000. flush_block_only(s, true);
  2001. if (s.strm.avail_out === 0) {
  2002. return BS_FINISH_STARTED;
  2003. }
  2004. /***/
  2005. return BS_FINISH_DONE;
  2006. }
  2007. if (s.last_lit) {
  2008. /*** FLUSH_BLOCK(s, 0); ***/
  2009. flush_block_only(s, false);
  2010. if (s.strm.avail_out === 0) {
  2011. return BS_NEED_MORE;
  2012. }
  2013. /***/
  2014. }
  2015. return BS_BLOCK_DONE;
  2016. };
  2017. /* ===========================================================================
  2018. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  2019. * (It will be regenerated if this run of deflate switches away from Huffman.)
  2020. */
  2021. const deflate_huff = (s, flush) => {
  2022. let bflush; /* set if current block must be flushed */
  2023. for (;;) {
  2024. /* Make sure that we have a literal to write. */
  2025. if (s.lookahead === 0) {
  2026. fill_window(s);
  2027. if (s.lookahead === 0) {
  2028. if (flush === Z_NO_FLUSH$2) {
  2029. return BS_NEED_MORE;
  2030. }
  2031. break; /* flush the current block */
  2032. }
  2033. }
  2034. /* Output a literal byte */
  2035. s.match_length = 0;
  2036. //Tracevv((stderr,"%c", s->window[s->strstart]));
  2037. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  2038. bflush = _tr_tally(s, 0, s.window[s.strstart]);
  2039. s.lookahead--;
  2040. s.strstart++;
  2041. if (bflush) {
  2042. /*** FLUSH_BLOCK(s, 0); ***/
  2043. flush_block_only(s, false);
  2044. if (s.strm.avail_out === 0) {
  2045. return BS_NEED_MORE;
  2046. }
  2047. /***/
  2048. }
  2049. }
  2050. s.insert = 0;
  2051. if (flush === Z_FINISH$3) {
  2052. /*** FLUSH_BLOCK(s, 1); ***/
  2053. flush_block_only(s, true);
  2054. if (s.strm.avail_out === 0) {
  2055. return BS_FINISH_STARTED;
  2056. }
  2057. /***/
  2058. return BS_FINISH_DONE;
  2059. }
  2060. if (s.last_lit) {
  2061. /*** FLUSH_BLOCK(s, 0); ***/
  2062. flush_block_only(s, false);
  2063. if (s.strm.avail_out === 0) {
  2064. return BS_NEED_MORE;
  2065. }
  2066. /***/
  2067. }
  2068. return BS_BLOCK_DONE;
  2069. };
  2070. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  2071. * the desired pack level (0..9). The values given below have been tuned to
  2072. * exclude worst case performance for pathological files. Better values may be
  2073. * found for specific files.
  2074. */
  2075. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  2076. this.good_length = good_length;
  2077. this.max_lazy = max_lazy;
  2078. this.nice_length = nice_length;
  2079. this.max_chain = max_chain;
  2080. this.func = func;
  2081. }
  2082. const configuration_table = [
  2083. /* good lazy nice chain */
  2084. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  2085. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  2086. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  2087. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  2088. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  2089. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  2090. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  2091. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  2092. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  2093. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  2094. ];
  2095. /* ===========================================================================
  2096. * Initialize the "longest match" routines for a new zlib stream
  2097. */
  2098. const lm_init = (s) => {
  2099. s.window_size = 2 * s.w_size;
  2100. /*** CLEAR_HASH(s); ***/
  2101. zero(s.head); // Fill with NIL (= 0);
  2102. /* Set the default configuration parameters:
  2103. */
  2104. s.max_lazy_match = configuration_table[s.level].max_lazy;
  2105. s.good_match = configuration_table[s.level].good_length;
  2106. s.nice_match = configuration_table[s.level].nice_length;
  2107. s.max_chain_length = configuration_table[s.level].max_chain;
  2108. s.strstart = 0;
  2109. s.block_start = 0;
  2110. s.lookahead = 0;
  2111. s.insert = 0;
  2112. s.match_length = s.prev_length = MIN_MATCH - 1;
  2113. s.match_available = 0;
  2114. s.ins_h = 0;
  2115. };
  2116. function DeflateState() {
  2117. this.strm = null; /* pointer back to this zlib stream */
  2118. this.status = 0; /* as the name implies */
  2119. this.pending_buf = null; /* output still pending */
  2120. this.pending_buf_size = 0; /* size of pending_buf */
  2121. this.pending_out = 0; /* next pending byte to output to the stream */
  2122. this.pending = 0; /* nb of bytes in the pending buffer */
  2123. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  2124. this.gzhead = null; /* gzip header information to write */
  2125. this.gzindex = 0; /* where in extra, name, or comment */
  2126. this.method = Z_DEFLATED$2; /* can only be DEFLATED */
  2127. this.last_flush = -1; /* value of flush param for previous deflate call */
  2128. this.w_size = 0; /* LZ77 window size (32K by default) */
  2129. this.w_bits = 0; /* log2(w_size) (8..16) */
  2130. this.w_mask = 0; /* w_size - 1 */
  2131. this.window = null;
  2132. /* Sliding window. Input bytes are read into the second half of the window,
  2133. * and move to the first half later to keep a dictionary of at least wSize
  2134. * bytes. With this organization, matches are limited to a distance of
  2135. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  2136. * performed with a length multiple of the block size.
  2137. */
  2138. this.window_size = 0;
  2139. /* Actual size of window: 2*wSize, except when the user input buffer
  2140. * is directly used as sliding window.
  2141. */
  2142. this.prev = null;
  2143. /* Link to older string with same hash index. To limit the size of this
  2144. * array to 64K, this link is maintained only for the last 32K strings.
  2145. * An index in this array is thus a window index modulo 32K.
  2146. */
  2147. this.head = null; /* Heads of the hash chains or NIL. */
  2148. this.ins_h = 0; /* hash index of string to be inserted */
  2149. this.hash_size = 0; /* number of elements in hash table */
  2150. this.hash_bits = 0; /* log2(hash_size) */
  2151. this.hash_mask = 0; /* hash_size-1 */
  2152. this.hash_shift = 0;
  2153. /* Number of bits by which ins_h must be shifted at each input
  2154. * step. It must be such that after MIN_MATCH steps, the oldest
  2155. * byte no longer takes part in the hash key, that is:
  2156. * hash_shift * MIN_MATCH >= hash_bits
  2157. */
  2158. this.block_start = 0;
  2159. /* Window position at the beginning of the current output block. Gets
  2160. * negative when the window is moved backwards.
  2161. */
  2162. this.match_length = 0; /* length of best match */
  2163. this.prev_match = 0; /* previous match */
  2164. this.match_available = 0; /* set if previous match exists */
  2165. this.strstart = 0; /* start of string to insert */
  2166. this.match_start = 0; /* start of matching string */
  2167. this.lookahead = 0; /* number of valid bytes ahead in window */
  2168. this.prev_length = 0;
  2169. /* Length of the best match at previous step. Matches not greater than this
  2170. * are discarded. This is used in the lazy match evaluation.
  2171. */
  2172. this.max_chain_length = 0;
  2173. /* To speed up deflation, hash chains are never searched beyond this
  2174. * length. A higher limit improves compression ratio but degrades the
  2175. * speed.
  2176. */
  2177. this.max_lazy_match = 0;
  2178. /* Attempt to find a better match only when the current match is strictly
  2179. * smaller than this value. This mechanism is used only for compression
  2180. * levels >= 4.
  2181. */
  2182. // That's alias to max_lazy_match, don't use directly
  2183. //this.max_insert_length = 0;
  2184. /* Insert new strings in the hash table only if the match length is not
  2185. * greater than this length. This saves time but degrades compression.
  2186. * max_insert_length is used only for compression levels <= 3.
  2187. */
  2188. this.level = 0; /* compression level (1..9) */
  2189. this.strategy = 0; /* favor or force Huffman coding*/
  2190. this.good_match = 0;
  2191. /* Use a faster search when the previous match is longer than this */
  2192. this.nice_match = 0; /* Stop searching when current match exceeds this */
  2193. /* used by trees.c: */
  2194. /* Didn't use ct_data typedef below to suppress compiler warning */
  2195. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  2196. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  2197. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  2198. // Use flat array of DOUBLE size, with interleaved fata,
  2199. // because JS does not support effective
  2200. this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2);
  2201. this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2);
  2202. this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2);
  2203. zero(this.dyn_ltree);
  2204. zero(this.dyn_dtree);
  2205. zero(this.bl_tree);
  2206. this.l_desc = null; /* desc. for literal tree */
  2207. this.d_desc = null; /* desc. for distance tree */
  2208. this.bl_desc = null; /* desc. for bit length tree */
  2209. //ush bl_count[MAX_BITS+1];
  2210. this.bl_count = new Uint16Array(MAX_BITS + 1);
  2211. /* number of codes at each bit length for an optimal tree */
  2212. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  2213. this.heap = new Uint16Array(2 * L_CODES + 1); /* heap used to build the Huffman trees */
  2214. zero(this.heap);
  2215. this.heap_len = 0; /* number of elements in the heap */
  2216. this.heap_max = 0; /* element of largest frequency */
  2217. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  2218. * The same heap array is used to build all trees.
  2219. */
  2220. this.depth = new Uint16Array(2 * L_CODES + 1); //uch depth[2*L_CODES+1];
  2221. zero(this.depth);
  2222. /* Depth of each subtree used as tie breaker for trees of equal frequency
  2223. */
  2224. this.l_buf = 0; /* buffer index for literals or lengths */
  2225. this.lit_bufsize = 0;
  2226. /* Size of match buffer for literals/lengths. There are 4 reasons for
  2227. * limiting lit_bufsize to 64K:
  2228. * - frequencies can be kept in 16 bit counters
  2229. * - if compression is not successful for the first block, all input
  2230. * data is still in the window so we can still emit a stored block even
  2231. * when input comes from standard input. (This can also be done for
  2232. * all blocks if lit_bufsize is not greater than 32K.)
  2233. * - if compression is not successful for a file smaller than 64K, we can
  2234. * even emit a stored file instead of a stored block (saving 5 bytes).
  2235. * This is applicable only for zip (not gzip or zlib).
  2236. * - creating new Huffman trees less frequently may not provide fast
  2237. * adaptation to changes in the input data statistics. (Take for
  2238. * example a binary file with poorly compressible code followed by
  2239. * a highly compressible string table.) Smaller buffer sizes give
  2240. * fast adaptation but have of course the overhead of transmitting
  2241. * trees more frequently.
  2242. * - I can't count above 4
  2243. */
  2244. this.last_lit = 0; /* running index in l_buf */
  2245. this.d_buf = 0;
  2246. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  2247. * the same number of elements. To use different lengths, an extra flag
  2248. * array would be necessary.
  2249. */
  2250. this.opt_len = 0; /* bit length of current block with optimal trees */
  2251. this.static_len = 0; /* bit length of current block with static trees */
  2252. this.matches = 0; /* number of string matches in current block */
  2253. this.insert = 0; /* bytes at end of window left to insert */
  2254. this.bi_buf = 0;
  2255. /* Output buffer. bits are inserted starting at the bottom (least
  2256. * significant bits).
  2257. */
  2258. this.bi_valid = 0;
  2259. /* Number of valid bits in bi_buf. All bits above the last valid bit
  2260. * are always zero.
  2261. */
  2262. // Used for window memory init. We safely ignore it for JS. That makes
  2263. // sense only for pointers and memory check tools.
  2264. //this.high_water = 0;
  2265. /* High water mark offset in window for initialized bytes -- bytes above
  2266. * this are set to zero in order to avoid memory check warnings when
  2267. * longest match routines access bytes past the input. This is then
  2268. * updated to the new high water mark.
  2269. */
  2270. }
  2271. const deflateResetKeep = (strm) => {
  2272. if (!strm || !strm.state) {
  2273. return err(strm, Z_STREAM_ERROR$2);
  2274. }
  2275. strm.total_in = strm.total_out = 0;
  2276. strm.data_type = Z_UNKNOWN;
  2277. const s = strm.state;
  2278. s.pending = 0;
  2279. s.pending_out = 0;
  2280. if (s.wrap < 0) {
  2281. s.wrap = -s.wrap;
  2282. /* was made negative by deflate(..., Z_FINISH); */
  2283. }
  2284. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  2285. strm.adler = (s.wrap === 2) ?
  2286. 0 // crc32(0, Z_NULL, 0)
  2287. :
  2288. 1; // adler32(0, Z_NULL, 0)
  2289. s.last_flush = Z_NO_FLUSH$2;
  2290. _tr_init(s);
  2291. return Z_OK$3;
  2292. };
  2293. const deflateReset = (strm) => {
  2294. const ret = deflateResetKeep(strm);
  2295. if (ret === Z_OK$3) {
  2296. lm_init(strm.state);
  2297. }
  2298. return ret;
  2299. };
  2300. const deflateSetHeader = (strm, head) => {
  2301. if (!strm || !strm.state) { return Z_STREAM_ERROR$2; }
  2302. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR$2; }
  2303. strm.state.gzhead = head;
  2304. return Z_OK$3;
  2305. };
  2306. const deflateInit2 = (strm, level, method, windowBits, memLevel, strategy) => {
  2307. if (!strm) { // === Z_NULL
  2308. return Z_STREAM_ERROR$2;
  2309. }
  2310. let wrap = 1;
  2311. if (level === Z_DEFAULT_COMPRESSION$1) {
  2312. level = 6;
  2313. }
  2314. if (windowBits < 0) { /* suppress zlib wrapper */
  2315. wrap = 0;
  2316. windowBits = -windowBits;
  2317. }
  2318. else if (windowBits > 15) {
  2319. wrap = 2; /* write gzip wrapper instead */
  2320. windowBits -= 16;
  2321. }
  2322. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED$2 ||
  2323. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  2324. strategy < 0 || strategy > Z_FIXED) {
  2325. return err(strm, Z_STREAM_ERROR$2);
  2326. }
  2327. if (windowBits === 8) {
  2328. windowBits = 9;
  2329. }
  2330. /* until 256-byte window bug fixed */
  2331. const s = new DeflateState();
  2332. strm.state = s;
  2333. s.strm = strm;
  2334. s.wrap = wrap;
  2335. s.gzhead = null;
  2336. s.w_bits = windowBits;
  2337. s.w_size = 1 << s.w_bits;
  2338. s.w_mask = s.w_size - 1;
  2339. s.hash_bits = memLevel + 7;
  2340. s.hash_size = 1 << s.hash_bits;
  2341. s.hash_mask = s.hash_size - 1;
  2342. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  2343. s.window = new Uint8Array(s.w_size * 2);
  2344. s.head = new Uint16Array(s.hash_size);
  2345. s.prev = new Uint16Array(s.w_size);
  2346. // Don't need mem init magic for JS.
  2347. //s.high_water = 0; /* nothing written to s->window yet */
  2348. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  2349. s.pending_buf_size = s.lit_bufsize * 4;
  2350. //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  2351. //s->pending_buf = (uchf *) overlay;
  2352. s.pending_buf = new Uint8Array(s.pending_buf_size);
  2353. // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
  2354. //s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  2355. s.d_buf = 1 * s.lit_bufsize;
  2356. //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  2357. s.l_buf = (1 + 2) * s.lit_bufsize;
  2358. s.level = level;
  2359. s.strategy = strategy;
  2360. s.method = method;
  2361. return deflateReset(strm);
  2362. };
  2363. const deflateInit = (strm, level) => {
  2364. return deflateInit2(strm, level, Z_DEFLATED$2, MAX_WBITS$1, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1);
  2365. };
  2366. const deflate$2 = (strm, flush) => {
  2367. let beg, val; // for gzip header write only
  2368. if (!strm || !strm.state ||
  2369. flush > Z_BLOCK$1 || flush < 0) {
  2370. return strm ? err(strm, Z_STREAM_ERROR$2) : Z_STREAM_ERROR$2;
  2371. }
  2372. const s = strm.state;
  2373. if (!strm.output ||
  2374. (!strm.input && strm.avail_in !== 0) ||
  2375. (s.status === FINISH_STATE && flush !== Z_FINISH$3)) {
  2376. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR$1 : Z_STREAM_ERROR$2);
  2377. }
  2378. s.strm = strm; /* just in case */
  2379. const old_flush = s.last_flush;
  2380. s.last_flush = flush;
  2381. /* Write the header */
  2382. if (s.status === INIT_STATE) {
  2383. if (s.wrap === 2) { // GZIP header
  2384. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2385. put_byte(s, 31);
  2386. put_byte(s, 139);
  2387. put_byte(s, 8);
  2388. if (!s.gzhead) { // s->gzhead == Z_NULL
  2389. put_byte(s, 0);
  2390. put_byte(s, 0);
  2391. put_byte(s, 0);
  2392. put_byte(s, 0);
  2393. put_byte(s, 0);
  2394. put_byte(s, s.level === 9 ? 2 :
  2395. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  2396. 4 : 0));
  2397. put_byte(s, OS_CODE);
  2398. s.status = BUSY_STATE;
  2399. }
  2400. else {
  2401. put_byte(s, (s.gzhead.text ? 1 : 0) +
  2402. (s.gzhead.hcrc ? 2 : 0) +
  2403. (!s.gzhead.extra ? 0 : 4) +
  2404. (!s.gzhead.name ? 0 : 8) +
  2405. (!s.gzhead.comment ? 0 : 16)
  2406. );
  2407. put_byte(s, s.gzhead.time & 0xff);
  2408. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  2409. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  2410. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  2411. put_byte(s, s.level === 9 ? 2 :
  2412. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  2413. 4 : 0));
  2414. put_byte(s, s.gzhead.os & 0xff);
  2415. if (s.gzhead.extra && s.gzhead.extra.length) {
  2416. put_byte(s, s.gzhead.extra.length & 0xff);
  2417. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  2418. }
  2419. if (s.gzhead.hcrc) {
  2420. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0);
  2421. }
  2422. s.gzindex = 0;
  2423. s.status = EXTRA_STATE;
  2424. }
  2425. }
  2426. else // DEFLATE header
  2427. {
  2428. let header = (Z_DEFLATED$2 + ((s.w_bits - 8) << 4)) << 8;
  2429. let level_flags = -1;
  2430. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  2431. level_flags = 0;
  2432. } else if (s.level < 6) {
  2433. level_flags = 1;
  2434. } else if (s.level === 6) {
  2435. level_flags = 2;
  2436. } else {
  2437. level_flags = 3;
  2438. }
  2439. header |= (level_flags << 6);
  2440. if (s.strstart !== 0) { header |= PRESET_DICT; }
  2441. header += 31 - (header % 31);
  2442. s.status = BUSY_STATE;
  2443. putShortMSB(s, header);
  2444. /* Save the adler32 of the preset dictionary: */
  2445. if (s.strstart !== 0) {
  2446. putShortMSB(s, strm.adler >>> 16);
  2447. putShortMSB(s, strm.adler & 0xffff);
  2448. }
  2449. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  2450. }
  2451. }
  2452. //#ifdef GZIP
  2453. if (s.status === EXTRA_STATE) {
  2454. if (s.gzhead.extra/* != Z_NULL*/) {
  2455. beg = s.pending; /* start of bytes to update crc */
  2456. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  2457. if (s.pending === s.pending_buf_size) {
  2458. if (s.gzhead.hcrc && s.pending > beg) {
  2459. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2460. }
  2461. flush_pending(strm);
  2462. beg = s.pending;
  2463. if (s.pending === s.pending_buf_size) {
  2464. break;
  2465. }
  2466. }
  2467. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  2468. s.gzindex++;
  2469. }
  2470. if (s.gzhead.hcrc && s.pending > beg) {
  2471. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2472. }
  2473. if (s.gzindex === s.gzhead.extra.length) {
  2474. s.gzindex = 0;
  2475. s.status = NAME_STATE;
  2476. }
  2477. }
  2478. else {
  2479. s.status = NAME_STATE;
  2480. }
  2481. }
  2482. if (s.status === NAME_STATE) {
  2483. if (s.gzhead.name/* != Z_NULL*/) {
  2484. beg = s.pending; /* start of bytes to update crc */
  2485. //int val;
  2486. do {
  2487. if (s.pending === s.pending_buf_size) {
  2488. if (s.gzhead.hcrc && s.pending > beg) {
  2489. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2490. }
  2491. flush_pending(strm);
  2492. beg = s.pending;
  2493. if (s.pending === s.pending_buf_size) {
  2494. val = 1;
  2495. break;
  2496. }
  2497. }
  2498. // JS specific: little magic to add zero terminator to end of string
  2499. if (s.gzindex < s.gzhead.name.length) {
  2500. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  2501. } else {
  2502. val = 0;
  2503. }
  2504. put_byte(s, val);
  2505. } while (val !== 0);
  2506. if (s.gzhead.hcrc && s.pending > beg) {
  2507. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2508. }
  2509. if (val === 0) {
  2510. s.gzindex = 0;
  2511. s.status = COMMENT_STATE;
  2512. }
  2513. }
  2514. else {
  2515. s.status = COMMENT_STATE;
  2516. }
  2517. }
  2518. if (s.status === COMMENT_STATE) {
  2519. if (s.gzhead.comment/* != Z_NULL*/) {
  2520. beg = s.pending; /* start of bytes to update crc */
  2521. //int val;
  2522. do {
  2523. if (s.pending === s.pending_buf_size) {
  2524. if (s.gzhead.hcrc && s.pending > beg) {
  2525. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2526. }
  2527. flush_pending(strm);
  2528. beg = s.pending;
  2529. if (s.pending === s.pending_buf_size) {
  2530. val = 1;
  2531. break;
  2532. }
  2533. }
  2534. // JS specific: little magic to add zero terminator to end of string
  2535. if (s.gzindex < s.gzhead.comment.length) {
  2536. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  2537. } else {
  2538. val = 0;
  2539. }
  2540. put_byte(s, val);
  2541. } while (val !== 0);
  2542. if (s.gzhead.hcrc && s.pending > beg) {
  2543. strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
  2544. }
  2545. if (val === 0) {
  2546. s.status = HCRC_STATE;
  2547. }
  2548. }
  2549. else {
  2550. s.status = HCRC_STATE;
  2551. }
  2552. }
  2553. if (s.status === HCRC_STATE) {
  2554. if (s.gzhead.hcrc) {
  2555. if (s.pending + 2 > s.pending_buf_size) {
  2556. flush_pending(strm);
  2557. }
  2558. if (s.pending + 2 <= s.pending_buf_size) {
  2559. put_byte(s, strm.adler & 0xff);
  2560. put_byte(s, (strm.adler >> 8) & 0xff);
  2561. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  2562. s.status = BUSY_STATE;
  2563. }
  2564. }
  2565. else {
  2566. s.status = BUSY_STATE;
  2567. }
  2568. }
  2569. //#endif
  2570. /* Flush as much pending output as possible */
  2571. if (s.pending !== 0) {
  2572. flush_pending(strm);
  2573. if (strm.avail_out === 0) {
  2574. /* Since avail_out is 0, deflate will be called again with
  2575. * more output space, but possibly with both pending and
  2576. * avail_in equal to zero. There won't be anything to do,
  2577. * but this is not an error situation so make sure we
  2578. * return OK instead of BUF_ERROR at next call of deflate:
  2579. */
  2580. s.last_flush = -1;
  2581. return Z_OK$3;
  2582. }
  2583. /* Make sure there is something to do and avoid duplicate consecutive
  2584. * flushes. For repeated and useless calls with Z_FINISH, we keep
  2585. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  2586. */
  2587. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  2588. flush !== Z_FINISH$3) {
  2589. return err(strm, Z_BUF_ERROR$1);
  2590. }
  2591. /* User must not provide more input after the first FINISH: */
  2592. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  2593. return err(strm, Z_BUF_ERROR$1);
  2594. }
  2595. /* Start a new block or continue the current one.
  2596. */
  2597. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  2598. (flush !== Z_NO_FLUSH$2 && s.status !== FINISH_STATE)) {
  2599. let bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  2600. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  2601. configuration_table[s.level].func(s, flush));
  2602. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  2603. s.status = FINISH_STATE;
  2604. }
  2605. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  2606. if (strm.avail_out === 0) {
  2607. s.last_flush = -1;
  2608. /* avoid BUF_ERROR next call, see above */
  2609. }
  2610. return Z_OK$3;
  2611. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  2612. * of deflate should use the same flush parameter to make sure
  2613. * that the flush is complete. So we don't have to output an
  2614. * empty block here, this will be done at next call. This also
  2615. * ensures that for a very small output buffer, we emit at most
  2616. * one empty block.
  2617. */
  2618. }
  2619. if (bstate === BS_BLOCK_DONE) {
  2620. if (flush === Z_PARTIAL_FLUSH) {
  2621. _tr_align(s);
  2622. }
  2623. else if (flush !== Z_BLOCK$1) { /* FULL_FLUSH or SYNC_FLUSH */
  2624. _tr_stored_block(s, 0, 0, false);
  2625. /* For a full flush, this empty block will be recognized
  2626. * as a special marker by inflate_sync().
  2627. */
  2628. if (flush === Z_FULL_FLUSH$1) {
  2629. /*** CLEAR_HASH(s); ***/ /* forget history */
  2630. zero(s.head); // Fill with NIL (= 0);
  2631. if (s.lookahead === 0) {
  2632. s.strstart = 0;
  2633. s.block_start = 0;
  2634. s.insert = 0;
  2635. }
  2636. }
  2637. }
  2638. flush_pending(strm);
  2639. if (strm.avail_out === 0) {
  2640. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  2641. return Z_OK$3;
  2642. }
  2643. }
  2644. }
  2645. //Assert(strm->avail_out > 0, "bug2");
  2646. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  2647. if (flush !== Z_FINISH$3) { return Z_OK$3; }
  2648. if (s.wrap <= 0) { return Z_STREAM_END$3; }
  2649. /* Write the trailer */
  2650. if (s.wrap === 2) {
  2651. put_byte(s, strm.adler & 0xff);
  2652. put_byte(s, (strm.adler >> 8) & 0xff);
  2653. put_byte(s, (strm.adler >> 16) & 0xff);
  2654. put_byte(s, (strm.adler >> 24) & 0xff);
  2655. put_byte(s, strm.total_in & 0xff);
  2656. put_byte(s, (strm.total_in >> 8) & 0xff);
  2657. put_byte(s, (strm.total_in >> 16) & 0xff);
  2658. put_byte(s, (strm.total_in >> 24) & 0xff);
  2659. }
  2660. else
  2661. {
  2662. putShortMSB(s, strm.adler >>> 16);
  2663. putShortMSB(s, strm.adler & 0xffff);
  2664. }
  2665. flush_pending(strm);
  2666. /* If avail_out is zero, the application will call deflate again
  2667. * to flush the rest.
  2668. */
  2669. if (s.wrap > 0) { s.wrap = -s.wrap; }
  2670. /* write the trailer only once! */
  2671. return s.pending !== 0 ? Z_OK$3 : Z_STREAM_END$3;
  2672. };
  2673. const deflateEnd = (strm) => {
  2674. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2675. return Z_STREAM_ERROR$2;
  2676. }
  2677. const status = strm.state.status;
  2678. if (status !== INIT_STATE &&
  2679. status !== EXTRA_STATE &&
  2680. status !== NAME_STATE &&
  2681. status !== COMMENT_STATE &&
  2682. status !== HCRC_STATE &&
  2683. status !== BUSY_STATE &&
  2684. status !== FINISH_STATE
  2685. ) {
  2686. return err(strm, Z_STREAM_ERROR$2);
  2687. }
  2688. strm.state = null;
  2689. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR$2) : Z_OK$3;
  2690. };
  2691. /* =========================================================================
  2692. * Initializes the compression dictionary from the given byte
  2693. * sequence without producing any compressed output.
  2694. */
  2695. const deflateSetDictionary = (strm, dictionary) => {
  2696. let dictLength = dictionary.length;
  2697. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  2698. return Z_STREAM_ERROR$2;
  2699. }
  2700. const s = strm.state;
  2701. const wrap = s.wrap;
  2702. if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
  2703. return Z_STREAM_ERROR$2;
  2704. }
  2705. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  2706. if (wrap === 1) {
  2707. /* adler32(strm->adler, dictionary, dictLength); */
  2708. strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0);
  2709. }
  2710. s.wrap = 0; /* avoid computing Adler-32 in read_buf */
  2711. /* if dictionary would fill window, just replace the history */
  2712. if (dictLength >= s.w_size) {
  2713. if (wrap === 0) { /* already empty otherwise */
  2714. /*** CLEAR_HASH(s); ***/
  2715. zero(s.head); // Fill with NIL (= 0);
  2716. s.strstart = 0;
  2717. s.block_start = 0;
  2718. s.insert = 0;
  2719. }
  2720. /* use the tail */
  2721. // dictionary = dictionary.slice(dictLength - s.w_size);
  2722. let tmpDict = new Uint8Array(s.w_size);
  2723. tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0);
  2724. dictionary = tmpDict;
  2725. dictLength = s.w_size;
  2726. }
  2727. /* insert dictionary into window and hash */
  2728. const avail = strm.avail_in;
  2729. const next = strm.next_in;
  2730. const input = strm.input;
  2731. strm.avail_in = dictLength;
  2732. strm.next_in = 0;
  2733. strm.input = dictionary;
  2734. fill_window(s);
  2735. while (s.lookahead >= MIN_MATCH) {
  2736. let str = s.strstart;
  2737. let n = s.lookahead - (MIN_MATCH - 1);
  2738. do {
  2739. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  2740. s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
  2741. s.prev[str & s.w_mask] = s.head[s.ins_h];
  2742. s.head[s.ins_h] = str;
  2743. str++;
  2744. } while (--n);
  2745. s.strstart = str;
  2746. s.lookahead = MIN_MATCH - 1;
  2747. fill_window(s);
  2748. }
  2749. s.strstart += s.lookahead;
  2750. s.block_start = s.strstart;
  2751. s.insert = s.lookahead;
  2752. s.lookahead = 0;
  2753. s.match_length = s.prev_length = MIN_MATCH - 1;
  2754. s.match_available = 0;
  2755. strm.next_in = next;
  2756. strm.input = input;
  2757. strm.avail_in = avail;
  2758. s.wrap = wrap;
  2759. return Z_OK$3;
  2760. };
  2761. var deflateInit_1 = deflateInit;
  2762. var deflateInit2_1 = deflateInit2;
  2763. var deflateReset_1 = deflateReset;
  2764. var deflateResetKeep_1 = deflateResetKeep;
  2765. var deflateSetHeader_1 = deflateSetHeader;
  2766. var deflate_2$1 = deflate$2;
  2767. var deflateEnd_1 = deflateEnd;
  2768. var deflateSetDictionary_1 = deflateSetDictionary;
  2769. var deflateInfo = 'pako deflate (from Nodeca project)';
  2770. /* Not implemented
  2771. module.exports.deflateBound = deflateBound;
  2772. module.exports.deflateCopy = deflateCopy;
  2773. module.exports.deflateParams = deflateParams;
  2774. module.exports.deflatePending = deflatePending;
  2775. module.exports.deflatePrime = deflatePrime;
  2776. module.exports.deflateTune = deflateTune;
  2777. */
  2778. var deflate_1$2 = {
  2779. deflateInit: deflateInit_1,
  2780. deflateInit2: deflateInit2_1,
  2781. deflateReset: deflateReset_1,
  2782. deflateResetKeep: deflateResetKeep_1,
  2783. deflateSetHeader: deflateSetHeader_1,
  2784. deflate: deflate_2$1,
  2785. deflateEnd: deflateEnd_1,
  2786. deflateSetDictionary: deflateSetDictionary_1,
  2787. deflateInfo: deflateInfo
  2788. };
  2789. const _has = (obj, key) => {
  2790. return Object.prototype.hasOwnProperty.call(obj, key);
  2791. };
  2792. var assign = function (obj /*from1, from2, from3, ...*/) {
  2793. const sources = Array.prototype.slice.call(arguments, 1);
  2794. while (sources.length) {
  2795. const source = sources.shift();
  2796. if (!source) { continue; }
  2797. if (typeof source !== 'object') {
  2798. throw new TypeError(source + 'must be non-object');
  2799. }
  2800. for (const p in source) {
  2801. if (_has(source, p)) {
  2802. obj[p] = source[p];
  2803. }
  2804. }
  2805. }
  2806. return obj;
  2807. };
  2808. // Join array of chunks to single array.
  2809. var flattenChunks = (chunks) => {
  2810. // calculate data length
  2811. let len = 0;
  2812. for (let i = 0, l = chunks.length; i < l; i++) {
  2813. len += chunks[i].length;
  2814. }
  2815. // join chunks
  2816. const result = new Uint8Array(len);
  2817. for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
  2818. let chunk = chunks[i];
  2819. result.set(chunk, pos);
  2820. pos += chunk.length;
  2821. }
  2822. return result;
  2823. };
  2824. var common = {
  2825. assign: assign,
  2826. flattenChunks: flattenChunks
  2827. };
  2828. // String encode/decode helpers
  2829. // Quick check if we can use fast array to bin string conversion
  2830. //
  2831. // - apply(Array) can fail on Android 2.2
  2832. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  2833. //
  2834. let STR_APPLY_UIA_OK = true;
  2835. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  2836. // Table with utf8 lengths (calculated by first byte of sequence)
  2837. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  2838. // because max possible codepoint is 0x10ffff
  2839. const _utf8len = new Uint8Array(256);
  2840. for (let q = 0; q < 256; q++) {
  2841. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  2842. }
  2843. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  2844. // convert string to array (typed, when possible)
  2845. var string2buf = (str) => {
  2846. if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
  2847. return new TextEncoder().encode(str);
  2848. }
  2849. let buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  2850. // count binary size
  2851. for (m_pos = 0; m_pos < str_len; m_pos++) {
  2852. c = str.charCodeAt(m_pos);
  2853. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  2854. c2 = str.charCodeAt(m_pos + 1);
  2855. if ((c2 & 0xfc00) === 0xdc00) {
  2856. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2857. m_pos++;
  2858. }
  2859. }
  2860. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  2861. }
  2862. // allocate buffer
  2863. buf = new Uint8Array(buf_len);
  2864. // convert
  2865. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  2866. c = str.charCodeAt(m_pos);
  2867. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  2868. c2 = str.charCodeAt(m_pos + 1);
  2869. if ((c2 & 0xfc00) === 0xdc00) {
  2870. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2871. m_pos++;
  2872. }
  2873. }
  2874. if (c < 0x80) {
  2875. /* one byte */
  2876. buf[i++] = c;
  2877. } else if (c < 0x800) {
  2878. /* two bytes */
  2879. buf[i++] = 0xC0 | (c >>> 6);
  2880. buf[i++] = 0x80 | (c & 0x3f);
  2881. } else if (c < 0x10000) {
  2882. /* three bytes */
  2883. buf[i++] = 0xE0 | (c >>> 12);
  2884. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  2885. buf[i++] = 0x80 | (c & 0x3f);
  2886. } else {
  2887. /* four bytes */
  2888. buf[i++] = 0xf0 | (c >>> 18);
  2889. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  2890. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  2891. buf[i++] = 0x80 | (c & 0x3f);
  2892. }
  2893. }
  2894. return buf;
  2895. };
  2896. // Helper
  2897. const buf2binstring = (buf, len) => {
  2898. // On Chrome, the arguments in a function call that are allowed is `65534`.
  2899. // If the length of the buffer is smaller than that, we can use this optimization,
  2900. // otherwise we will take a slower path.
  2901. if (len < 65534) {
  2902. if (buf.subarray && STR_APPLY_UIA_OK) {
  2903. return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
  2904. }
  2905. }
  2906. let result = '';
  2907. for (let i = 0; i < len; i++) {
  2908. result += String.fromCharCode(buf[i]);
  2909. }
  2910. return result;
  2911. };
  2912. // convert array to string
  2913. var buf2string = (buf, max) => {
  2914. const len = max || buf.length;
  2915. if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) {
  2916. return new TextDecoder().decode(buf.subarray(0, max));
  2917. }
  2918. let i, out;
  2919. // Reserve max possible length (2 words per char)
  2920. // NB: by unknown reasons, Array is significantly faster for
  2921. // String.fromCharCode.apply than Uint16Array.
  2922. const utf16buf = new Array(len * 2);
  2923. for (out = 0, i = 0; i < len;) {
  2924. let c = buf[i++];
  2925. // quick process ascii
  2926. if (c < 0x80) { utf16buf[out++] = c; continue; }
  2927. let c_len = _utf8len[c];
  2928. // skip 5 & 6 byte codes
  2929. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  2930. // apply mask on first byte
  2931. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  2932. // join the rest
  2933. while (c_len > 1 && i < len) {
  2934. c = (c << 6) | (buf[i++] & 0x3f);
  2935. c_len--;
  2936. }
  2937. // terminated by end of string?
  2938. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  2939. if (c < 0x10000) {
  2940. utf16buf[out++] = c;
  2941. } else {
  2942. c -= 0x10000;
  2943. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  2944. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  2945. }
  2946. }
  2947. return buf2binstring(utf16buf, out);
  2948. };
  2949. // Calculate max possible position in utf8 buffer,
  2950. // that will not break sequence. If that's not possible
  2951. // - (very small limits) return max size as is.
  2952. //
  2953. // buf[] - utf8 bytes array
  2954. // max - length limit (mandatory);
  2955. var utf8border = (buf, max) => {
  2956. max = max || buf.length;
  2957. if (max > buf.length) { max = buf.length; }
  2958. // go back from last position, until start of sequence found
  2959. let pos = max - 1;
  2960. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  2961. // Very small and broken sequence,
  2962. // return max, because we should return something anyway.
  2963. if (pos < 0) { return max; }
  2964. // If we came to start of buffer - that means buffer is too small,
  2965. // return max too.
  2966. if (pos === 0) { return max; }
  2967. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  2968. };
  2969. var strings = {
  2970. string2buf: string2buf,
  2971. buf2string: buf2string,
  2972. utf8border: utf8border
  2973. };
  2974. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2975. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2976. //
  2977. // This software is provided 'as-is', without any express or implied
  2978. // warranty. In no event will the authors be held liable for any damages
  2979. // arising from the use of this software.
  2980. //
  2981. // Permission is granted to anyone to use this software for any purpose,
  2982. // including commercial applications, and to alter it and redistribute it
  2983. // freely, subject to the following restrictions:
  2984. //
  2985. // 1. The origin of this software must not be misrepresented; you must not
  2986. // claim that you wrote the original software. If you use this software
  2987. // in a product, an acknowledgment in the product documentation would be
  2988. // appreciated but is not required.
  2989. // 2. Altered source versions must be plainly marked as such, and must not be
  2990. // misrepresented as being the original software.
  2991. // 3. This notice may not be removed or altered from any source distribution.
  2992. function ZStream() {
  2993. /* next input byte */
  2994. this.input = null; // JS specific, because we have no pointers
  2995. this.next_in = 0;
  2996. /* number of bytes available at input */
  2997. this.avail_in = 0;
  2998. /* total number of input bytes read so far */
  2999. this.total_in = 0;
  3000. /* next output byte should be put there */
  3001. this.output = null; // JS specific, because we have no pointers
  3002. this.next_out = 0;
  3003. /* remaining free space at output */
  3004. this.avail_out = 0;
  3005. /* total number of bytes output so far */
  3006. this.total_out = 0;
  3007. /* last error message, NULL if no error */
  3008. this.msg = ''/*Z_NULL*/;
  3009. /* not visible by applications */
  3010. this.state = null;
  3011. /* best guess about the data type: binary or text */
  3012. this.data_type = 2/*Z_UNKNOWN*/;
  3013. /* adler32 value of the uncompressed data */
  3014. this.adler = 0;
  3015. }
  3016. var zstream = ZStream;
  3017. const toString$1 = Object.prototype.toString;
  3018. /* Public constants ==========================================================*/
  3019. /* ===========================================================================*/
  3020. const {
  3021. Z_NO_FLUSH: Z_NO_FLUSH$1, Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH: Z_FINISH$2,
  3022. Z_OK: Z_OK$2, Z_STREAM_END: Z_STREAM_END$2,
  3023. Z_DEFAULT_COMPRESSION,
  3024. Z_DEFAULT_STRATEGY,
  3025. Z_DEFLATED: Z_DEFLATED$1
  3026. } = constants$2;
  3027. /* ===========================================================================*/
  3028. /**
  3029. * class Deflate
  3030. *
  3031. * Generic JS-style wrapper for zlib calls. If you don't need
  3032. * streaming behaviour - use more simple functions: [[deflate]],
  3033. * [[deflateRaw]] and [[gzip]].
  3034. **/
  3035. /* internal
  3036. * Deflate.chunks -> Array
  3037. *
  3038. * Chunks of output data, if [[Deflate#onData]] not overridden.
  3039. **/
  3040. /**
  3041. * Deflate.result -> Uint8Array
  3042. *
  3043. * Compressed result, generated by default [[Deflate#onData]]
  3044. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  3045. * (call [[Deflate#push]] with `Z_FINISH` / `true` param).
  3046. **/
  3047. /**
  3048. * Deflate.err -> Number
  3049. *
  3050. * Error code after deflate finished. 0 (Z_OK) on success.
  3051. * You will not need it in real life, because deflate errors
  3052. * are possible only on wrong options or bad `onData` / `onEnd`
  3053. * custom handlers.
  3054. **/
  3055. /**
  3056. * Deflate.msg -> String
  3057. *
  3058. * Error message, if [[Deflate.err]] != 0
  3059. **/
  3060. /**
  3061. * new Deflate(options)
  3062. * - options (Object): zlib deflate options.
  3063. *
  3064. * Creates new deflator instance with specified params. Throws exception
  3065. * on bad params. Supported options:
  3066. *
  3067. * - `level`
  3068. * - `windowBits`
  3069. * - `memLevel`
  3070. * - `strategy`
  3071. * - `dictionary`
  3072. *
  3073. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3074. * for more information on these.
  3075. *
  3076. * Additional options, for internal needs:
  3077. *
  3078. * - `chunkSize` - size of generated data chunks (16K by default)
  3079. * - `raw` (Boolean) - do raw deflate
  3080. * - `gzip` (Boolean) - create gzip wrapper
  3081. * - `header` (Object) - custom header for gzip
  3082. * - `text` (Boolean) - true if compressed data believed to be text
  3083. * - `time` (Number) - modification time, unix timestamp
  3084. * - `os` (Number) - operation system code
  3085. * - `extra` (Array) - array of bytes with extra data (max 65536)
  3086. * - `name` (String) - file name (binary string)
  3087. * - `comment` (String) - comment (binary string)
  3088. * - `hcrc` (Boolean) - true if header crc should be added
  3089. *
  3090. * ##### Example:
  3091. *
  3092. * ```javascript
  3093. * const pako = require('pako')
  3094. * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  3095. * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  3096. *
  3097. * const deflate = new pako.Deflate({ level: 3});
  3098. *
  3099. * deflate.push(chunk1, false);
  3100. * deflate.push(chunk2, true); // true -> last chunk
  3101. *
  3102. * if (deflate.err) { throw new Error(deflate.err); }
  3103. *
  3104. * console.log(deflate.result);
  3105. * ```
  3106. **/
  3107. function Deflate$1(options) {
  3108. this.options = common.assign({
  3109. level: Z_DEFAULT_COMPRESSION,
  3110. method: Z_DEFLATED$1,
  3111. chunkSize: 16384,
  3112. windowBits: 15,
  3113. memLevel: 8,
  3114. strategy: Z_DEFAULT_STRATEGY
  3115. }, options || {});
  3116. let opt = this.options;
  3117. if (opt.raw && (opt.windowBits > 0)) {
  3118. opt.windowBits = -opt.windowBits;
  3119. }
  3120. else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
  3121. opt.windowBits += 16;
  3122. }
  3123. this.err = 0; // error code, if happens (0 = Z_OK)
  3124. this.msg = ''; // error message
  3125. this.ended = false; // used to avoid multiple onEnd() calls
  3126. this.chunks = []; // chunks of compressed data
  3127. this.strm = new zstream();
  3128. this.strm.avail_out = 0;
  3129. let status = deflate_1$2.deflateInit2(
  3130. this.strm,
  3131. opt.level,
  3132. opt.method,
  3133. opt.windowBits,
  3134. opt.memLevel,
  3135. opt.strategy
  3136. );
  3137. if (status !== Z_OK$2) {
  3138. throw new Error(messages[status]);
  3139. }
  3140. if (opt.header) {
  3141. deflate_1$2.deflateSetHeader(this.strm, opt.header);
  3142. }
  3143. if (opt.dictionary) {
  3144. let dict;
  3145. // Convert data if needed
  3146. if (typeof opt.dictionary === 'string') {
  3147. // If we need to compress text, change encoding to utf8.
  3148. dict = strings.string2buf(opt.dictionary);
  3149. } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') {
  3150. dict = new Uint8Array(opt.dictionary);
  3151. } else {
  3152. dict = opt.dictionary;
  3153. }
  3154. status = deflate_1$2.deflateSetDictionary(this.strm, dict);
  3155. if (status !== Z_OK$2) {
  3156. throw new Error(messages[status]);
  3157. }
  3158. this._dict_set = true;
  3159. }
  3160. }
  3161. /**
  3162. * Deflate#push(data[, flush_mode]) -> Boolean
  3163. * - data (Uint8Array|ArrayBuffer|String): input data. Strings will be
  3164. * converted to utf8 byte sequence.
  3165. * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  3166. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
  3167. *
  3168. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  3169. * new compressed chunks. Returns `true` on success. The last data block must
  3170. * have `flush_mode` Z_FINISH (or `true`). That will flush internal pending
  3171. * buffers and call [[Deflate#onEnd]].
  3172. *
  3173. * On fail call [[Deflate#onEnd]] with error code and return false.
  3174. *
  3175. * ##### Example
  3176. *
  3177. * ```javascript
  3178. * push(chunk, false); // push one of data chunks
  3179. * ...
  3180. * push(chunk, true); // push last chunk
  3181. * ```
  3182. **/
  3183. Deflate$1.prototype.push = function (data, flush_mode) {
  3184. const strm = this.strm;
  3185. const chunkSize = this.options.chunkSize;
  3186. let status, _flush_mode;
  3187. if (this.ended) { return false; }
  3188. if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
  3189. else _flush_mode = flush_mode === true ? Z_FINISH$2 : Z_NO_FLUSH$1;
  3190. // Convert data if needed
  3191. if (typeof data === 'string') {
  3192. // If we need to compress text, change encoding to utf8.
  3193. strm.input = strings.string2buf(data);
  3194. } else if (toString$1.call(data) === '[object ArrayBuffer]') {
  3195. strm.input = new Uint8Array(data);
  3196. } else {
  3197. strm.input = data;
  3198. }
  3199. strm.next_in = 0;
  3200. strm.avail_in = strm.input.length;
  3201. for (;;) {
  3202. if (strm.avail_out === 0) {
  3203. strm.output = new Uint8Array(chunkSize);
  3204. strm.next_out = 0;
  3205. strm.avail_out = chunkSize;
  3206. }
  3207. // Make sure avail_out > 6 to avoid repeating markers
  3208. if ((_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH) && strm.avail_out <= 6) {
  3209. this.onData(strm.output.subarray(0, strm.next_out));
  3210. strm.avail_out = 0;
  3211. continue;
  3212. }
  3213. status = deflate_1$2.deflate(strm, _flush_mode);
  3214. // Ended => flush and finish
  3215. if (status === Z_STREAM_END$2) {
  3216. if (strm.next_out > 0) {
  3217. this.onData(strm.output.subarray(0, strm.next_out));
  3218. }
  3219. status = deflate_1$2.deflateEnd(this.strm);
  3220. this.onEnd(status);
  3221. this.ended = true;
  3222. return status === Z_OK$2;
  3223. }
  3224. // Flush if out buffer full
  3225. if (strm.avail_out === 0) {
  3226. this.onData(strm.output);
  3227. continue;
  3228. }
  3229. // Flush if requested and has data
  3230. if (_flush_mode > 0 && strm.next_out > 0) {
  3231. this.onData(strm.output.subarray(0, strm.next_out));
  3232. strm.avail_out = 0;
  3233. continue;
  3234. }
  3235. if (strm.avail_in === 0) break;
  3236. }
  3237. return true;
  3238. };
  3239. /**
  3240. * Deflate#onData(chunk) -> Void
  3241. * - chunk (Uint8Array): output data.
  3242. *
  3243. * By default, stores data blocks in `chunks[]` property and glue
  3244. * those in `onEnd`. Override this handler, if you need another behaviour.
  3245. **/
  3246. Deflate$1.prototype.onData = function (chunk) {
  3247. this.chunks.push(chunk);
  3248. };
  3249. /**
  3250. * Deflate#onEnd(status) -> Void
  3251. * - status (Number): deflate status. 0 (Z_OK) on success,
  3252. * other if not.
  3253. *
  3254. * Called once after you tell deflate that the input stream is
  3255. * complete (Z_FINISH). By default - join collected chunks,
  3256. * free memory and fill `results` / `err` properties.
  3257. **/
  3258. Deflate$1.prototype.onEnd = function (status) {
  3259. // On success - join
  3260. if (status === Z_OK$2) {
  3261. this.result = common.flattenChunks(this.chunks);
  3262. }
  3263. this.chunks = [];
  3264. this.err = status;
  3265. this.msg = this.strm.msg;
  3266. };
  3267. /**
  3268. * deflate(data[, options]) -> Uint8Array
  3269. * - data (Uint8Array|String): input data to compress.
  3270. * - options (Object): zlib deflate options.
  3271. *
  3272. * Compress `data` with deflate algorithm and `options`.
  3273. *
  3274. * Supported options are:
  3275. *
  3276. * - level
  3277. * - windowBits
  3278. * - memLevel
  3279. * - strategy
  3280. * - dictionary
  3281. *
  3282. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3283. * for more information on these.
  3284. *
  3285. * Sugar (options):
  3286. *
  3287. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  3288. * negative windowBits implicitly.
  3289. *
  3290. * ##### Example:
  3291. *
  3292. * ```javascript
  3293. * const pako = require('pako')
  3294. * const data = new Uint8Array([1,2,3,4,5,6,7,8,9]);
  3295. *
  3296. * console.log(pako.deflate(data));
  3297. * ```
  3298. **/
  3299. function deflate$1(input, options) {
  3300. const deflator = new Deflate$1(options);
  3301. deflator.push(input, true);
  3302. // That will never happens, if you don't cheat with options :)
  3303. if (deflator.err) { throw deflator.msg || messages[deflator.err]; }
  3304. return deflator.result;
  3305. }
  3306. /**
  3307. * deflateRaw(data[, options]) -> Uint8Array
  3308. * - data (Uint8Array|String): input data to compress.
  3309. * - options (Object): zlib deflate options.
  3310. *
  3311. * The same as [[deflate]], but creates raw data, without wrapper
  3312. * (header and adler32 crc).
  3313. **/
  3314. function deflateRaw$1(input, options) {
  3315. options = options || {};
  3316. options.raw = true;
  3317. return deflate$1(input, options);
  3318. }
  3319. /**
  3320. * gzip(data[, options]) -> Uint8Array
  3321. * - data (Uint8Array|String): input data to compress.
  3322. * - options (Object): zlib deflate options.
  3323. *
  3324. * The same as [[deflate]], but create gzip wrapper instead of
  3325. * deflate one.
  3326. **/
  3327. function gzip$1(input, options) {
  3328. options = options || {};
  3329. options.gzip = true;
  3330. return deflate$1(input, options);
  3331. }
  3332. var Deflate_1$1 = Deflate$1;
  3333. var deflate_2 = deflate$1;
  3334. var deflateRaw_1$1 = deflateRaw$1;
  3335. var gzip_1$1 = gzip$1;
  3336. var constants$1 = constants$2;
  3337. var deflate_1$1 = {
  3338. Deflate: Deflate_1$1,
  3339. deflate: deflate_2,
  3340. deflateRaw: deflateRaw_1$1,
  3341. gzip: gzip_1$1,
  3342. constants: constants$1
  3343. };
  3344. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3345. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3346. //
  3347. // This software is provided 'as-is', without any express or implied
  3348. // warranty. In no event will the authors be held liable for any damages
  3349. // arising from the use of this software.
  3350. //
  3351. // Permission is granted to anyone to use this software for any purpose,
  3352. // including commercial applications, and to alter it and redistribute it
  3353. // freely, subject to the following restrictions:
  3354. //
  3355. // 1. The origin of this software must not be misrepresented; you must not
  3356. // claim that you wrote the original software. If you use this software
  3357. // in a product, an acknowledgment in the product documentation would be
  3358. // appreciated but is not required.
  3359. // 2. Altered source versions must be plainly marked as such, and must not be
  3360. // misrepresented as being the original software.
  3361. // 3. This notice may not be removed or altered from any source distribution.
  3362. // See state defs from inflate.js
  3363. const BAD$1 = 30; /* got a data error -- remain here until reset */
  3364. const TYPE$1 = 12; /* i: waiting for type bits, including last-flag bit */
  3365. /*
  3366. Decode literal, length, and distance codes and write out the resulting
  3367. literal and match bytes until either not enough input or output is
  3368. available, an end-of-block is encountered, or a data error is encountered.
  3369. When large enough input and output buffers are supplied to inflate(), for
  3370. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  3371. inflate execution time is spent in this routine.
  3372. Entry assumptions:
  3373. state.mode === LEN
  3374. strm.avail_in >= 6
  3375. strm.avail_out >= 258
  3376. start >= strm.avail_out
  3377. state.bits < 8
  3378. On return, state.mode is one of:
  3379. LEN -- ran out of enough output space or enough available input
  3380. TYPE -- reached end of block code, inflate() to interpret next block
  3381. BAD -- error in block data
  3382. Notes:
  3383. - The maximum input bits used by a length/distance pair is 15 bits for the
  3384. length code, 5 bits for the length extra, 15 bits for the distance code,
  3385. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  3386. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  3387. checking for available input while decoding.
  3388. - The maximum bytes that a single length/distance pair can output is 258
  3389. bytes, which is the maximum length that can be coded. inflate_fast()
  3390. requires strm.avail_out >= 258 for each loop to avoid checking for
  3391. output space.
  3392. */
  3393. var inffast = function inflate_fast(strm, start) {
  3394. let _in; /* local strm.input */
  3395. let last; /* have enough input while in < last */
  3396. let _out; /* local strm.output */
  3397. let beg; /* inflate()'s initial strm.output */
  3398. let end; /* while out < end, enough space available */
  3399. //#ifdef INFLATE_STRICT
  3400. let dmax; /* maximum distance from zlib header */
  3401. //#endif
  3402. let wsize; /* window size or zero if not using window */
  3403. let whave; /* valid bytes in the window */
  3404. let wnext; /* window write index */
  3405. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  3406. let s_window; /* allocated sliding window, if wsize != 0 */
  3407. let hold; /* local strm.hold */
  3408. let bits; /* local strm.bits */
  3409. let lcode; /* local strm.lencode */
  3410. let dcode; /* local strm.distcode */
  3411. let lmask; /* mask for first level of length codes */
  3412. let dmask; /* mask for first level of distance codes */
  3413. let here; /* retrieved table entry */
  3414. let op; /* code bits, operation, extra bits, or */
  3415. /* window position, window bytes to copy */
  3416. let len; /* match length, unused bytes */
  3417. let dist; /* match distance */
  3418. let from; /* where to copy match from */
  3419. let from_source;
  3420. let input, output; // JS specific, because we have no pointers
  3421. /* copy state to local variables */
  3422. const state = strm.state;
  3423. //here = state.here;
  3424. _in = strm.next_in;
  3425. input = strm.input;
  3426. last = _in + (strm.avail_in - 5);
  3427. _out = strm.next_out;
  3428. output = strm.output;
  3429. beg = _out - (start - strm.avail_out);
  3430. end = _out + (strm.avail_out - 257);
  3431. //#ifdef INFLATE_STRICT
  3432. dmax = state.dmax;
  3433. //#endif
  3434. wsize = state.wsize;
  3435. whave = state.whave;
  3436. wnext = state.wnext;
  3437. s_window = state.window;
  3438. hold = state.hold;
  3439. bits = state.bits;
  3440. lcode = state.lencode;
  3441. dcode = state.distcode;
  3442. lmask = (1 << state.lenbits) - 1;
  3443. dmask = (1 << state.distbits) - 1;
  3444. /* decode literals and length/distances until end-of-block or not enough
  3445. input data or output space */
  3446. top:
  3447. do {
  3448. if (bits < 15) {
  3449. hold += input[_in++] << bits;
  3450. bits += 8;
  3451. hold += input[_in++] << bits;
  3452. bits += 8;
  3453. }
  3454. here = lcode[hold & lmask];
  3455. dolen:
  3456. for (;;) { // Goto emulation
  3457. op = here >>> 24/*here.bits*/;
  3458. hold >>>= op;
  3459. bits -= op;
  3460. op = (here >>> 16) & 0xff/*here.op*/;
  3461. if (op === 0) { /* literal */
  3462. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  3463. // "inflate: literal '%c'\n" :
  3464. // "inflate: literal 0x%02x\n", here.val));
  3465. output[_out++] = here & 0xffff/*here.val*/;
  3466. }
  3467. else if (op & 16) { /* length base */
  3468. len = here & 0xffff/*here.val*/;
  3469. op &= 15; /* number of extra bits */
  3470. if (op) {
  3471. if (bits < op) {
  3472. hold += input[_in++] << bits;
  3473. bits += 8;
  3474. }
  3475. len += hold & ((1 << op) - 1);
  3476. hold >>>= op;
  3477. bits -= op;
  3478. }
  3479. //Tracevv((stderr, "inflate: length %u\n", len));
  3480. if (bits < 15) {
  3481. hold += input[_in++] << bits;
  3482. bits += 8;
  3483. hold += input[_in++] << bits;
  3484. bits += 8;
  3485. }
  3486. here = dcode[hold & dmask];
  3487. dodist:
  3488. for (;;) { // goto emulation
  3489. op = here >>> 24/*here.bits*/;
  3490. hold >>>= op;
  3491. bits -= op;
  3492. op = (here >>> 16) & 0xff/*here.op*/;
  3493. if (op & 16) { /* distance base */
  3494. dist = here & 0xffff/*here.val*/;
  3495. op &= 15; /* number of extra bits */
  3496. if (bits < op) {
  3497. hold += input[_in++] << bits;
  3498. bits += 8;
  3499. if (bits < op) {
  3500. hold += input[_in++] << bits;
  3501. bits += 8;
  3502. }
  3503. }
  3504. dist += hold & ((1 << op) - 1);
  3505. //#ifdef INFLATE_STRICT
  3506. if (dist > dmax) {
  3507. strm.msg = 'invalid distance too far back';
  3508. state.mode = BAD$1;
  3509. break top;
  3510. }
  3511. //#endif
  3512. hold >>>= op;
  3513. bits -= op;
  3514. //Tracevv((stderr, "inflate: distance %u\n", dist));
  3515. op = _out - beg; /* max distance in output */
  3516. if (dist > op) { /* see if copy from window */
  3517. op = dist - op; /* distance back in window */
  3518. if (op > whave) {
  3519. if (state.sane) {
  3520. strm.msg = 'invalid distance too far back';
  3521. state.mode = BAD$1;
  3522. break top;
  3523. }
  3524. // (!) This block is disabled in zlib defaults,
  3525. // don't enable it for binary compatibility
  3526. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  3527. // if (len <= op - whave) {
  3528. // do {
  3529. // output[_out++] = 0;
  3530. // } while (--len);
  3531. // continue top;
  3532. // }
  3533. // len -= op - whave;
  3534. // do {
  3535. // output[_out++] = 0;
  3536. // } while (--op > whave);
  3537. // if (op === 0) {
  3538. // from = _out - dist;
  3539. // do {
  3540. // output[_out++] = output[from++];
  3541. // } while (--len);
  3542. // continue top;
  3543. // }
  3544. //#endif
  3545. }
  3546. from = 0; // window index
  3547. from_source = s_window;
  3548. if (wnext === 0) { /* very common case */
  3549. from += wsize - op;
  3550. if (op < len) { /* some from window */
  3551. len -= op;
  3552. do {
  3553. output[_out++] = s_window[from++];
  3554. } while (--op);
  3555. from = _out - dist; /* rest from output */
  3556. from_source = output;
  3557. }
  3558. }
  3559. else if (wnext < op) { /* wrap around window */
  3560. from += wsize + wnext - op;
  3561. op -= wnext;
  3562. if (op < len) { /* some from end of window */
  3563. len -= op;
  3564. do {
  3565. output[_out++] = s_window[from++];
  3566. } while (--op);
  3567. from = 0;
  3568. if (wnext < len) { /* some from start of window */
  3569. op = wnext;
  3570. len -= op;
  3571. do {
  3572. output[_out++] = s_window[from++];
  3573. } while (--op);
  3574. from = _out - dist; /* rest from output */
  3575. from_source = output;
  3576. }
  3577. }
  3578. }
  3579. else { /* contiguous in window */
  3580. from += wnext - op;
  3581. if (op < len) { /* some from window */
  3582. len -= op;
  3583. do {
  3584. output[_out++] = s_window[from++];
  3585. } while (--op);
  3586. from = _out - dist; /* rest from output */
  3587. from_source = output;
  3588. }
  3589. }
  3590. while (len > 2) {
  3591. output[_out++] = from_source[from++];
  3592. output[_out++] = from_source[from++];
  3593. output[_out++] = from_source[from++];
  3594. len -= 3;
  3595. }
  3596. if (len) {
  3597. output[_out++] = from_source[from++];
  3598. if (len > 1) {
  3599. output[_out++] = from_source[from++];
  3600. }
  3601. }
  3602. }
  3603. else {
  3604. from = _out - dist; /* copy direct from output */
  3605. do { /* minimum length is three */
  3606. output[_out++] = output[from++];
  3607. output[_out++] = output[from++];
  3608. output[_out++] = output[from++];
  3609. len -= 3;
  3610. } while (len > 2);
  3611. if (len) {
  3612. output[_out++] = output[from++];
  3613. if (len > 1) {
  3614. output[_out++] = output[from++];
  3615. }
  3616. }
  3617. }
  3618. }
  3619. else if ((op & 64) === 0) { /* 2nd level distance code */
  3620. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  3621. continue dodist;
  3622. }
  3623. else {
  3624. strm.msg = 'invalid distance code';
  3625. state.mode = BAD$1;
  3626. break top;
  3627. }
  3628. break; // need to emulate goto via "continue"
  3629. }
  3630. }
  3631. else if ((op & 64) === 0) { /* 2nd level length code */
  3632. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  3633. continue dolen;
  3634. }
  3635. else if (op & 32) { /* end-of-block */
  3636. //Tracevv((stderr, "inflate: end of block\n"));
  3637. state.mode = TYPE$1;
  3638. break top;
  3639. }
  3640. else {
  3641. strm.msg = 'invalid literal/length code';
  3642. state.mode = BAD$1;
  3643. break top;
  3644. }
  3645. break; // need to emulate goto via "continue"
  3646. }
  3647. } while (_in < last && _out < end);
  3648. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  3649. len = bits >> 3;
  3650. _in -= len;
  3651. bits -= len << 3;
  3652. hold &= (1 << bits) - 1;
  3653. /* update state and return */
  3654. strm.next_in = _in;
  3655. strm.next_out = _out;
  3656. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  3657. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  3658. state.hold = hold;
  3659. state.bits = bits;
  3660. return;
  3661. };
  3662. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3663. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3664. //
  3665. // This software is provided 'as-is', without any express or implied
  3666. // warranty. In no event will the authors be held liable for any damages
  3667. // arising from the use of this software.
  3668. //
  3669. // Permission is granted to anyone to use this software for any purpose,
  3670. // including commercial applications, and to alter it and redistribute it
  3671. // freely, subject to the following restrictions:
  3672. //
  3673. // 1. The origin of this software must not be misrepresented; you must not
  3674. // claim that you wrote the original software. If you use this software
  3675. // in a product, an acknowledgment in the product documentation would be
  3676. // appreciated but is not required.
  3677. // 2. Altered source versions must be plainly marked as such, and must not be
  3678. // misrepresented as being the original software.
  3679. // 3. This notice may not be removed or altered from any source distribution.
  3680. const MAXBITS = 15;
  3681. const ENOUGH_LENS$1 = 852;
  3682. const ENOUGH_DISTS$1 = 592;
  3683. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  3684. const CODES$1 = 0;
  3685. const LENS$1 = 1;
  3686. const DISTS$1 = 2;
  3687. const lbase = new Uint16Array([ /* Length codes 257..285 base */
  3688. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  3689. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  3690. ]);
  3691. const lext = new Uint8Array([ /* Length codes 257..285 extra */
  3692. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  3693. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  3694. ]);
  3695. const dbase = new Uint16Array([ /* Distance codes 0..29 base */
  3696. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  3697. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  3698. 8193, 12289, 16385, 24577, 0, 0
  3699. ]);
  3700. const dext = new Uint8Array([ /* Distance codes 0..29 extra */
  3701. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  3702. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  3703. 28, 28, 29, 29, 64, 64
  3704. ]);
  3705. const inflate_table = (type, lens, lens_index, codes, table, table_index, work, opts) =>
  3706. {
  3707. const bits = opts.bits;
  3708. //here = opts.here; /* table entry for duplication */
  3709. let len = 0; /* a code's length in bits */
  3710. let sym = 0; /* index of code symbols */
  3711. let min = 0, max = 0; /* minimum and maximum code lengths */
  3712. let root = 0; /* number of index bits for root table */
  3713. let curr = 0; /* number of index bits for current table */
  3714. let drop = 0; /* code bits to drop for sub-table */
  3715. let left = 0; /* number of prefix codes available */
  3716. let used = 0; /* code entries in table used */
  3717. let huff = 0; /* Huffman code */
  3718. let incr; /* for incrementing code, index */
  3719. let fill; /* index for replicating entries */
  3720. let low; /* low bits for current root entry */
  3721. let mask; /* mask for low root bits */
  3722. let next; /* next available space in table */
  3723. let base = null; /* base value table to use */
  3724. let base_index = 0;
  3725. // let shoextra; /* extra bits table to use */
  3726. let end; /* use base and extra for symbol > end */
  3727. const count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  3728. const offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  3729. let extra = null;
  3730. let extra_index = 0;
  3731. let here_bits, here_op, here_val;
  3732. /*
  3733. Process a set of code lengths to create a canonical Huffman code. The
  3734. code lengths are lens[0..codes-1]. Each length corresponds to the
  3735. symbols 0..codes-1. The Huffman code is generated by first sorting the
  3736. symbols by length from short to long, and retaining the symbol order
  3737. for codes with equal lengths. Then the code starts with all zero bits
  3738. for the first code of the shortest length, and the codes are integer
  3739. increments for the same length, and zeros are appended as the length
  3740. increases. For the deflate format, these bits are stored backwards
  3741. from their more natural integer increment ordering, and so when the
  3742. decoding tables are built in the large loop below, the integer codes
  3743. are incremented backwards.
  3744. This routine assumes, but does not check, that all of the entries in
  3745. lens[] are in the range 0..MAXBITS. The caller must assure this.
  3746. 1..MAXBITS is interpreted as that code length. zero means that that
  3747. symbol does not occur in this code.
  3748. The codes are sorted by computing a count of codes for each length,
  3749. creating from that a table of starting indices for each length in the
  3750. sorted table, and then entering the symbols in order in the sorted
  3751. table. The sorted table is work[], with that space being provided by
  3752. the caller.
  3753. The length counts are used for other purposes as well, i.e. finding
  3754. the minimum and maximum length codes, determining if there are any
  3755. codes at all, checking for a valid set of lengths, and looking ahead
  3756. at length counts to determine sub-table sizes when building the
  3757. decoding tables.
  3758. */
  3759. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  3760. for (len = 0; len <= MAXBITS; len++) {
  3761. count[len] = 0;
  3762. }
  3763. for (sym = 0; sym < codes; sym++) {
  3764. count[lens[lens_index + sym]]++;
  3765. }
  3766. /* bound code lengths, force root to be within code lengths */
  3767. root = bits;
  3768. for (max = MAXBITS; max >= 1; max--) {
  3769. if (count[max] !== 0) { break; }
  3770. }
  3771. if (root > max) {
  3772. root = max;
  3773. }
  3774. if (max === 0) { /* no symbols to code at all */
  3775. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  3776. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  3777. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  3778. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  3779. //table.op[opts.table_index] = 64;
  3780. //table.bits[opts.table_index] = 1;
  3781. //table.val[opts.table_index++] = 0;
  3782. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  3783. opts.bits = 1;
  3784. return 0; /* no symbols, but wait for decoding to report error */
  3785. }
  3786. for (min = 1; min < max; min++) {
  3787. if (count[min] !== 0) { break; }
  3788. }
  3789. if (root < min) {
  3790. root = min;
  3791. }
  3792. /* check for an over-subscribed or incomplete set of lengths */
  3793. left = 1;
  3794. for (len = 1; len <= MAXBITS; len++) {
  3795. left <<= 1;
  3796. left -= count[len];
  3797. if (left < 0) {
  3798. return -1;
  3799. } /* over-subscribed */
  3800. }
  3801. if (left > 0 && (type === CODES$1 || max !== 1)) {
  3802. return -1; /* incomplete set */
  3803. }
  3804. /* generate offsets into symbol table for each length for sorting */
  3805. offs[1] = 0;
  3806. for (len = 1; len < MAXBITS; len++) {
  3807. offs[len + 1] = offs[len] + count[len];
  3808. }
  3809. /* sort symbols by length, by symbol order within each length */
  3810. for (sym = 0; sym < codes; sym++) {
  3811. if (lens[lens_index + sym] !== 0) {
  3812. work[offs[lens[lens_index + sym]]++] = sym;
  3813. }
  3814. }
  3815. /*
  3816. Create and fill in decoding tables. In this loop, the table being
  3817. filled is at next and has curr index bits. The code being used is huff
  3818. with length len. That code is converted to an index by dropping drop
  3819. bits off of the bottom. For codes where len is less than drop + curr,
  3820. those top drop + curr - len bits are incremented through all values to
  3821. fill the table with replicated entries.
  3822. root is the number of index bits for the root table. When len exceeds
  3823. root, sub-tables are created pointed to by the root entry with an index
  3824. of the low root bits of huff. This is saved in low to check for when a
  3825. new sub-table should be started. drop is zero when the root table is
  3826. being filled, and drop is root when sub-tables are being filled.
  3827. When a new sub-table is needed, it is necessary to look ahead in the
  3828. code lengths to determine what size sub-table is needed. The length
  3829. counts are used for this, and so count[] is decremented as codes are
  3830. entered in the tables.
  3831. used keeps track of how many table entries have been allocated from the
  3832. provided *table space. It is checked for LENS and DIST tables against
  3833. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  3834. the initial root table size constants. See the comments in inftrees.h
  3835. for more information.
  3836. sym increments through all symbols, and the loop terminates when
  3837. all codes of length max, i.e. all codes, have been processed. This
  3838. routine permits incomplete codes, so another loop after this one fills
  3839. in the rest of the decoding tables with invalid code markers.
  3840. */
  3841. /* set up for code type */
  3842. // poor man optimization - use if-else instead of switch,
  3843. // to avoid deopts in old v8
  3844. if (type === CODES$1) {
  3845. base = extra = work; /* dummy value--not used */
  3846. end = 19;
  3847. } else if (type === LENS$1) {
  3848. base = lbase;
  3849. base_index -= 257;
  3850. extra = lext;
  3851. extra_index -= 257;
  3852. end = 256;
  3853. } else { /* DISTS */
  3854. base = dbase;
  3855. extra = dext;
  3856. end = -1;
  3857. }
  3858. /* initialize opts for loop */
  3859. huff = 0; /* starting code */
  3860. sym = 0; /* starting code symbol */
  3861. len = min; /* starting code length */
  3862. next = table_index; /* current table to fill in */
  3863. curr = root; /* current table index bits */
  3864. drop = 0; /* current bits to drop from code for index */
  3865. low = -1; /* trigger new sub-table when len > root */
  3866. used = 1 << root; /* use root table entries */
  3867. mask = used - 1; /* mask for comparing low */
  3868. /* check available table space */
  3869. if ((type === LENS$1 && used > ENOUGH_LENS$1) ||
  3870. (type === DISTS$1 && used > ENOUGH_DISTS$1)) {
  3871. return 1;
  3872. }
  3873. /* process all codes and make table entries */
  3874. for (;;) {
  3875. /* create table entry */
  3876. here_bits = len - drop;
  3877. if (work[sym] < end) {
  3878. here_op = 0;
  3879. here_val = work[sym];
  3880. }
  3881. else if (work[sym] > end) {
  3882. here_op = extra[extra_index + work[sym]];
  3883. here_val = base[base_index + work[sym]];
  3884. }
  3885. else {
  3886. here_op = 32 + 64; /* end of block */
  3887. here_val = 0;
  3888. }
  3889. /* replicate for those indices with low len bits equal to huff */
  3890. incr = 1 << (len - drop);
  3891. fill = 1 << curr;
  3892. min = fill; /* save offset to next table */
  3893. do {
  3894. fill -= incr;
  3895. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  3896. } while (fill !== 0);
  3897. /* backwards increment the len-bit code huff */
  3898. incr = 1 << (len - 1);
  3899. while (huff & incr) {
  3900. incr >>= 1;
  3901. }
  3902. if (incr !== 0) {
  3903. huff &= incr - 1;
  3904. huff += incr;
  3905. } else {
  3906. huff = 0;
  3907. }
  3908. /* go to next symbol, update count, len */
  3909. sym++;
  3910. if (--count[len] === 0) {
  3911. if (len === max) { break; }
  3912. len = lens[lens_index + work[sym]];
  3913. }
  3914. /* create new sub-table if needed */
  3915. if (len > root && (huff & mask) !== low) {
  3916. /* if first time, transition to sub-tables */
  3917. if (drop === 0) {
  3918. drop = root;
  3919. }
  3920. /* increment past last table */
  3921. next += min; /* here min is 1 << curr */
  3922. /* determine length of next table */
  3923. curr = len - drop;
  3924. left = 1 << curr;
  3925. while (curr + drop < max) {
  3926. left -= count[curr + drop];
  3927. if (left <= 0) { break; }
  3928. curr++;
  3929. left <<= 1;
  3930. }
  3931. /* check for enough space */
  3932. used += 1 << curr;
  3933. if ((type === LENS$1 && used > ENOUGH_LENS$1) ||
  3934. (type === DISTS$1 && used > ENOUGH_DISTS$1)) {
  3935. return 1;
  3936. }
  3937. /* point entry in root table to sub-table */
  3938. low = huff & mask;
  3939. /*table.op[low] = curr;
  3940. table.bits[low] = root;
  3941. table.val[low] = next - opts.table_index;*/
  3942. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  3943. }
  3944. }
  3945. /* fill in remaining table entry if code is incomplete (guaranteed to have
  3946. at most one remaining entry, since if the code is incomplete, the
  3947. maximum code length that was allowed to get this far is one bit) */
  3948. if (huff !== 0) {
  3949. //table.op[next + huff] = 64; /* invalid code marker */
  3950. //table.bits[next + huff] = len - drop;
  3951. //table.val[next + huff] = 0;
  3952. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  3953. }
  3954. /* set return parameters */
  3955. //opts.table_index += used;
  3956. opts.bits = root;
  3957. return 0;
  3958. };
  3959. var inftrees = inflate_table;
  3960. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3961. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  3962. //
  3963. // This software is provided 'as-is', without any express or implied
  3964. // warranty. In no event will the authors be held liable for any damages
  3965. // arising from the use of this software.
  3966. //
  3967. // Permission is granted to anyone to use this software for any purpose,
  3968. // including commercial applications, and to alter it and redistribute it
  3969. // freely, subject to the following restrictions:
  3970. //
  3971. // 1. The origin of this software must not be misrepresented; you must not
  3972. // claim that you wrote the original software. If you use this software
  3973. // in a product, an acknowledgment in the product documentation would be
  3974. // appreciated but is not required.
  3975. // 2. Altered source versions must be plainly marked as such, and must not be
  3976. // misrepresented as being the original software.
  3977. // 3. This notice may not be removed or altered from any source distribution.
  3978. const CODES = 0;
  3979. const LENS = 1;
  3980. const DISTS = 2;
  3981. /* Public constants ==========================================================*/
  3982. /* ===========================================================================*/
  3983. const {
  3984. Z_FINISH: Z_FINISH$1, Z_BLOCK, Z_TREES,
  3985. Z_OK: Z_OK$1, Z_STREAM_END: Z_STREAM_END$1, Z_NEED_DICT: Z_NEED_DICT$1, Z_STREAM_ERROR: Z_STREAM_ERROR$1, Z_DATA_ERROR: Z_DATA_ERROR$1, Z_MEM_ERROR: Z_MEM_ERROR$1, Z_BUF_ERROR,
  3986. Z_DEFLATED
  3987. } = constants$2;
  3988. /* STATES ====================================================================*/
  3989. /* ===========================================================================*/
  3990. const HEAD = 1; /* i: waiting for magic header */
  3991. const FLAGS = 2; /* i: waiting for method and flags (gzip) */
  3992. const TIME = 3; /* i: waiting for modification time (gzip) */
  3993. const OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  3994. const EXLEN = 5; /* i: waiting for extra length (gzip) */
  3995. const EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  3996. const NAME = 7; /* i: waiting for end of file name (gzip) */
  3997. const COMMENT = 8; /* i: waiting for end of comment (gzip) */
  3998. const HCRC = 9; /* i: waiting for header crc (gzip) */
  3999. const DICTID = 10; /* i: waiting for dictionary check value */
  4000. const DICT = 11; /* waiting for inflateSetDictionary() call */
  4001. const TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  4002. const TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  4003. const STORED = 14; /* i: waiting for stored size (length and complement) */
  4004. const COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  4005. const COPY = 16; /* i/o: waiting for input or output to copy stored block */
  4006. const TABLE = 17; /* i: waiting for dynamic block table lengths */
  4007. const LENLENS = 18; /* i: waiting for code length code lengths */
  4008. const CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  4009. const LEN_ = 20; /* i: same as LEN below, but only first time in */
  4010. const LEN = 21; /* i: waiting for length/lit/eob code */
  4011. const LENEXT = 22; /* i: waiting for length extra bits */
  4012. const DIST = 23; /* i: waiting for distance code */
  4013. const DISTEXT = 24; /* i: waiting for distance extra bits */
  4014. const MATCH = 25; /* o: waiting for output space to copy string */
  4015. const LIT = 26; /* o: waiting for output space to write literal */
  4016. const CHECK = 27; /* i: waiting for 32-bit check value */
  4017. const LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  4018. const DONE = 29; /* finished check, done -- remain here until reset */
  4019. const BAD = 30; /* got a data error -- remain here until reset */
  4020. const MEM = 31; /* got an inflate() memory error -- remain here until reset */
  4021. const SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  4022. /* ===========================================================================*/
  4023. const ENOUGH_LENS = 852;
  4024. const ENOUGH_DISTS = 592;
  4025. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  4026. const MAX_WBITS = 15;
  4027. /* 32K LZ77 window */
  4028. const DEF_WBITS = MAX_WBITS;
  4029. const zswap32 = (q) => {
  4030. return (((q >>> 24) & 0xff) +
  4031. ((q >>> 8) & 0xff00) +
  4032. ((q & 0xff00) << 8) +
  4033. ((q & 0xff) << 24));
  4034. };
  4035. function InflateState() {
  4036. this.mode = 0; /* current inflate mode */
  4037. this.last = false; /* true if processing last block */
  4038. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  4039. this.havedict = false; /* true if dictionary provided */
  4040. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  4041. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  4042. this.check = 0; /* protected copy of check value */
  4043. this.total = 0; /* protected copy of output count */
  4044. // TODO: may be {}
  4045. this.head = null; /* where to save gzip header information */
  4046. /* sliding window */
  4047. this.wbits = 0; /* log base 2 of requested window size */
  4048. this.wsize = 0; /* window size or zero if not using window */
  4049. this.whave = 0; /* valid bytes in the window */
  4050. this.wnext = 0; /* window write index */
  4051. this.window = null; /* allocated sliding window, if needed */
  4052. /* bit accumulator */
  4053. this.hold = 0; /* input bit accumulator */
  4054. this.bits = 0; /* number of bits in "in" */
  4055. /* for string and stored block copying */
  4056. this.length = 0; /* literal or length of data to copy */
  4057. this.offset = 0; /* distance back to copy string from */
  4058. /* for table and code decoding */
  4059. this.extra = 0; /* extra bits needed */
  4060. /* fixed and dynamic code tables */
  4061. this.lencode = null; /* starting table for length/literal codes */
  4062. this.distcode = null; /* starting table for distance codes */
  4063. this.lenbits = 0; /* index bits for lencode */
  4064. this.distbits = 0; /* index bits for distcode */
  4065. /* dynamic table building */
  4066. this.ncode = 0; /* number of code length code lengths */
  4067. this.nlen = 0; /* number of length code lengths */
  4068. this.ndist = 0; /* number of distance code lengths */
  4069. this.have = 0; /* number of code lengths in lens[] */
  4070. this.next = null; /* next available space in codes[] */
  4071. this.lens = new Uint16Array(320); /* temporary storage for code lengths */
  4072. this.work = new Uint16Array(288); /* work area for code table building */
  4073. /*
  4074. because we don't have pointers in js, we use lencode and distcode directly
  4075. as buffers so we don't need codes
  4076. */
  4077. //this.codes = new Int32Array(ENOUGH); /* space for code tables */
  4078. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  4079. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  4080. this.sane = 0; /* if false, allow invalid distance too far */
  4081. this.back = 0; /* bits back of last unprocessed length/lit */
  4082. this.was = 0; /* initial length of match */
  4083. }
  4084. const inflateResetKeep = (strm) => {
  4085. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  4086. const state = strm.state;
  4087. strm.total_in = strm.total_out = state.total = 0;
  4088. strm.msg = ''; /*Z_NULL*/
  4089. if (state.wrap) { /* to support ill-conceived Java test suite */
  4090. strm.adler = state.wrap & 1;
  4091. }
  4092. state.mode = HEAD;
  4093. state.last = 0;
  4094. state.havedict = 0;
  4095. state.dmax = 32768;
  4096. state.head = null/*Z_NULL*/;
  4097. state.hold = 0;
  4098. state.bits = 0;
  4099. //state.lencode = state.distcode = state.next = state.codes;
  4100. state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
  4101. state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
  4102. state.sane = 1;
  4103. state.back = -1;
  4104. //Tracev((stderr, "inflate: reset\n"));
  4105. return Z_OK$1;
  4106. };
  4107. const inflateReset = (strm) => {
  4108. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  4109. const state = strm.state;
  4110. state.wsize = 0;
  4111. state.whave = 0;
  4112. state.wnext = 0;
  4113. return inflateResetKeep(strm);
  4114. };
  4115. const inflateReset2 = (strm, windowBits) => {
  4116. let wrap;
  4117. /* get the state */
  4118. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  4119. const state = strm.state;
  4120. /* extract wrap request from windowBits parameter */
  4121. if (windowBits < 0) {
  4122. wrap = 0;
  4123. windowBits = -windowBits;
  4124. }
  4125. else {
  4126. wrap = (windowBits >> 4) + 1;
  4127. if (windowBits < 48) {
  4128. windowBits &= 15;
  4129. }
  4130. }
  4131. /* set number of window bits, free window if different */
  4132. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  4133. return Z_STREAM_ERROR$1;
  4134. }
  4135. if (state.window !== null && state.wbits !== windowBits) {
  4136. state.window = null;
  4137. }
  4138. /* update state and reset the rest of it */
  4139. state.wrap = wrap;
  4140. state.wbits = windowBits;
  4141. return inflateReset(strm);
  4142. };
  4143. const inflateInit2 = (strm, windowBits) => {
  4144. if (!strm) { return Z_STREAM_ERROR$1; }
  4145. //strm.msg = Z_NULL; /* in case we return an error */
  4146. const state = new InflateState();
  4147. //if (state === Z_NULL) return Z_MEM_ERROR;
  4148. //Tracev((stderr, "inflate: allocated\n"));
  4149. strm.state = state;
  4150. state.window = null/*Z_NULL*/;
  4151. const ret = inflateReset2(strm, windowBits);
  4152. if (ret !== Z_OK$1) {
  4153. strm.state = null/*Z_NULL*/;
  4154. }
  4155. return ret;
  4156. };
  4157. const inflateInit = (strm) => {
  4158. return inflateInit2(strm, DEF_WBITS);
  4159. };
  4160. /*
  4161. Return state with length and distance decoding tables and index sizes set to
  4162. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  4163. If BUILDFIXED is defined, then instead this routine builds the tables the
  4164. first time it's called, and returns those tables the first time and
  4165. thereafter. This reduces the size of the code by about 2K bytes, in
  4166. exchange for a little execution time. However, BUILDFIXED should not be
  4167. used for threaded applications, since the rewriting of the tables and virgin
  4168. may not be thread-safe.
  4169. */
  4170. let virgin = true;
  4171. let lenfix, distfix; // We have no pointers in JS, so keep tables separate
  4172. const fixedtables = (state) => {
  4173. /* build fixed huffman tables if first call (may not be thread safe) */
  4174. if (virgin) {
  4175. lenfix = new Int32Array(512);
  4176. distfix = new Int32Array(32);
  4177. /* literal/length table */
  4178. let sym = 0;
  4179. while (sym < 144) { state.lens[sym++] = 8; }
  4180. while (sym < 256) { state.lens[sym++] = 9; }
  4181. while (sym < 280) { state.lens[sym++] = 7; }
  4182. while (sym < 288) { state.lens[sym++] = 8; }
  4183. inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
  4184. /* distance table */
  4185. sym = 0;
  4186. while (sym < 32) { state.lens[sym++] = 5; }
  4187. inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
  4188. /* do this just once */
  4189. virgin = false;
  4190. }
  4191. state.lencode = lenfix;
  4192. state.lenbits = 9;
  4193. state.distcode = distfix;
  4194. state.distbits = 5;
  4195. };
  4196. /*
  4197. Update the window with the last wsize (normally 32K) bytes written before
  4198. returning. If window does not exist yet, create it. This is only called
  4199. when a window is already in use, or when output has been written during this
  4200. inflate call, but the end of the deflate stream has not been reached yet.
  4201. It is also called to create a window for dictionary data when a dictionary
  4202. is loaded.
  4203. Providing output buffers larger than 32K to inflate() should provide a speed
  4204. advantage, since only the last 32K of output is copied to the sliding window
  4205. upon return from inflate(), and since all distances after the first 32K of
  4206. output will fall in the output data, making match copies simpler and faster.
  4207. The advantage may be dependent on the size of the processor's data caches.
  4208. */
  4209. const updatewindow = (strm, src, end, copy) => {
  4210. let dist;
  4211. const state = strm.state;
  4212. /* if it hasn't been done already, allocate space for the window */
  4213. if (state.window === null) {
  4214. state.wsize = 1 << state.wbits;
  4215. state.wnext = 0;
  4216. state.whave = 0;
  4217. state.window = new Uint8Array(state.wsize);
  4218. }
  4219. /* copy state->wsize or less output bytes into the circular window */
  4220. if (copy >= state.wsize) {
  4221. state.window.set(src.subarray(end - state.wsize, end), 0);
  4222. state.wnext = 0;
  4223. state.whave = state.wsize;
  4224. }
  4225. else {
  4226. dist = state.wsize - state.wnext;
  4227. if (dist > copy) {
  4228. dist = copy;
  4229. }
  4230. //zmemcpy(state->window + state->wnext, end - copy, dist);
  4231. state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext);
  4232. copy -= dist;
  4233. if (copy) {
  4234. //zmemcpy(state->window, end - copy, copy);
  4235. state.window.set(src.subarray(end - copy, end), 0);
  4236. state.wnext = copy;
  4237. state.whave = state.wsize;
  4238. }
  4239. else {
  4240. state.wnext += dist;
  4241. if (state.wnext === state.wsize) { state.wnext = 0; }
  4242. if (state.whave < state.wsize) { state.whave += dist; }
  4243. }
  4244. }
  4245. return 0;
  4246. };
  4247. const inflate$2 = (strm, flush) => {
  4248. let state;
  4249. let input, output; // input/output buffers
  4250. let next; /* next input INDEX */
  4251. let put; /* next output INDEX */
  4252. let have, left; /* available input and output */
  4253. let hold; /* bit buffer */
  4254. let bits; /* bits in bit buffer */
  4255. let _in, _out; /* save starting available input and output */
  4256. let copy; /* number of stored or match bytes to copy */
  4257. let from; /* where to copy match bytes from */
  4258. let from_source;
  4259. let here = 0; /* current decoding table entry */
  4260. let here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  4261. //let last; /* parent table entry */
  4262. let last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  4263. let len; /* length to copy for repeats, bits to drop */
  4264. let ret; /* return code */
  4265. const hbuf = new Uint8Array(4); /* buffer for gzip header crc calculation */
  4266. let opts;
  4267. let n; // temporary variable for NEED_BITS
  4268. const order = /* permutation of code lengths */
  4269. new Uint8Array([ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]);
  4270. if (!strm || !strm.state || !strm.output ||
  4271. (!strm.input && strm.avail_in !== 0)) {
  4272. return Z_STREAM_ERROR$1;
  4273. }
  4274. state = strm.state;
  4275. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  4276. //--- LOAD() ---
  4277. put = strm.next_out;
  4278. output = strm.output;
  4279. left = strm.avail_out;
  4280. next = strm.next_in;
  4281. input = strm.input;
  4282. have = strm.avail_in;
  4283. hold = state.hold;
  4284. bits = state.bits;
  4285. //---
  4286. _in = have;
  4287. _out = left;
  4288. ret = Z_OK$1;
  4289. inf_leave: // goto emulation
  4290. for (;;) {
  4291. switch (state.mode) {
  4292. case HEAD:
  4293. if (state.wrap === 0) {
  4294. state.mode = TYPEDO;
  4295. break;
  4296. }
  4297. //=== NEEDBITS(16);
  4298. while (bits < 16) {
  4299. if (have === 0) { break inf_leave; }
  4300. have--;
  4301. hold += input[next++] << bits;
  4302. bits += 8;
  4303. }
  4304. //===//
  4305. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  4306. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  4307. //=== CRC2(state.check, hold);
  4308. hbuf[0] = hold & 0xff;
  4309. hbuf[1] = (hold >>> 8) & 0xff;
  4310. state.check = crc32_1(state.check, hbuf, 2, 0);
  4311. //===//
  4312. //=== INITBITS();
  4313. hold = 0;
  4314. bits = 0;
  4315. //===//
  4316. state.mode = FLAGS;
  4317. break;
  4318. }
  4319. state.flags = 0; /* expect zlib header */
  4320. if (state.head) {
  4321. state.head.done = false;
  4322. }
  4323. if (!(state.wrap & 1) || /* check if zlib header allowed */
  4324. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  4325. strm.msg = 'incorrect header check';
  4326. state.mode = BAD;
  4327. break;
  4328. }
  4329. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  4330. strm.msg = 'unknown compression method';
  4331. state.mode = BAD;
  4332. break;
  4333. }
  4334. //--- DROPBITS(4) ---//
  4335. hold >>>= 4;
  4336. bits -= 4;
  4337. //---//
  4338. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  4339. if (state.wbits === 0) {
  4340. state.wbits = len;
  4341. }
  4342. else if (len > state.wbits) {
  4343. strm.msg = 'invalid window size';
  4344. state.mode = BAD;
  4345. break;
  4346. }
  4347. // !!! pako patch. Force use `options.windowBits` if passed.
  4348. // Required to always use max window size by default.
  4349. state.dmax = 1 << state.wbits;
  4350. //state.dmax = 1 << len;
  4351. //Tracev((stderr, "inflate: zlib header ok\n"));
  4352. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  4353. state.mode = hold & 0x200 ? DICTID : TYPE;
  4354. //=== INITBITS();
  4355. hold = 0;
  4356. bits = 0;
  4357. //===//
  4358. break;
  4359. case FLAGS:
  4360. //=== NEEDBITS(16); */
  4361. while (bits < 16) {
  4362. if (have === 0) { break inf_leave; }
  4363. have--;
  4364. hold += input[next++] << bits;
  4365. bits += 8;
  4366. }
  4367. //===//
  4368. state.flags = hold;
  4369. if ((state.flags & 0xff) !== Z_DEFLATED) {
  4370. strm.msg = 'unknown compression method';
  4371. state.mode = BAD;
  4372. break;
  4373. }
  4374. if (state.flags & 0xe000) {
  4375. strm.msg = 'unknown header flags set';
  4376. state.mode = BAD;
  4377. break;
  4378. }
  4379. if (state.head) {
  4380. state.head.text = ((hold >> 8) & 1);
  4381. }
  4382. if (state.flags & 0x0200) {
  4383. //=== CRC2(state.check, hold);
  4384. hbuf[0] = hold & 0xff;
  4385. hbuf[1] = (hold >>> 8) & 0xff;
  4386. state.check = crc32_1(state.check, hbuf, 2, 0);
  4387. //===//
  4388. }
  4389. //=== INITBITS();
  4390. hold = 0;
  4391. bits = 0;
  4392. //===//
  4393. state.mode = TIME;
  4394. /* falls through */
  4395. case TIME:
  4396. //=== NEEDBITS(32); */
  4397. while (bits < 32) {
  4398. if (have === 0) { break inf_leave; }
  4399. have--;
  4400. hold += input[next++] << bits;
  4401. bits += 8;
  4402. }
  4403. //===//
  4404. if (state.head) {
  4405. state.head.time = hold;
  4406. }
  4407. if (state.flags & 0x0200) {
  4408. //=== CRC4(state.check, hold)
  4409. hbuf[0] = hold & 0xff;
  4410. hbuf[1] = (hold >>> 8) & 0xff;
  4411. hbuf[2] = (hold >>> 16) & 0xff;
  4412. hbuf[3] = (hold >>> 24) & 0xff;
  4413. state.check = crc32_1(state.check, hbuf, 4, 0);
  4414. //===
  4415. }
  4416. //=== INITBITS();
  4417. hold = 0;
  4418. bits = 0;
  4419. //===//
  4420. state.mode = OS;
  4421. /* falls through */
  4422. case OS:
  4423. //=== NEEDBITS(16); */
  4424. while (bits < 16) {
  4425. if (have === 0) { break inf_leave; }
  4426. have--;
  4427. hold += input[next++] << bits;
  4428. bits += 8;
  4429. }
  4430. //===//
  4431. if (state.head) {
  4432. state.head.xflags = (hold & 0xff);
  4433. state.head.os = (hold >> 8);
  4434. }
  4435. if (state.flags & 0x0200) {
  4436. //=== CRC2(state.check, hold);
  4437. hbuf[0] = hold & 0xff;
  4438. hbuf[1] = (hold >>> 8) & 0xff;
  4439. state.check = crc32_1(state.check, hbuf, 2, 0);
  4440. //===//
  4441. }
  4442. //=== INITBITS();
  4443. hold = 0;
  4444. bits = 0;
  4445. //===//
  4446. state.mode = EXLEN;
  4447. /* falls through */
  4448. case EXLEN:
  4449. if (state.flags & 0x0400) {
  4450. //=== NEEDBITS(16); */
  4451. while (bits < 16) {
  4452. if (have === 0) { break inf_leave; }
  4453. have--;
  4454. hold += input[next++] << bits;
  4455. bits += 8;
  4456. }
  4457. //===//
  4458. state.length = hold;
  4459. if (state.head) {
  4460. state.head.extra_len = hold;
  4461. }
  4462. if (state.flags & 0x0200) {
  4463. //=== CRC2(state.check, hold);
  4464. hbuf[0] = hold & 0xff;
  4465. hbuf[1] = (hold >>> 8) & 0xff;
  4466. state.check = crc32_1(state.check, hbuf, 2, 0);
  4467. //===//
  4468. }
  4469. //=== INITBITS();
  4470. hold = 0;
  4471. bits = 0;
  4472. //===//
  4473. }
  4474. else if (state.head) {
  4475. state.head.extra = null/*Z_NULL*/;
  4476. }
  4477. state.mode = EXTRA;
  4478. /* falls through */
  4479. case EXTRA:
  4480. if (state.flags & 0x0400) {
  4481. copy = state.length;
  4482. if (copy > have) { copy = have; }
  4483. if (copy) {
  4484. if (state.head) {
  4485. len = state.head.extra_len - state.length;
  4486. if (!state.head.extra) {
  4487. // Use untyped array for more convenient processing later
  4488. state.head.extra = new Uint8Array(state.head.extra_len);
  4489. }
  4490. state.head.extra.set(
  4491. input.subarray(
  4492. next,
  4493. // extra field is limited to 65536 bytes
  4494. // - no need for additional size check
  4495. next + copy
  4496. ),
  4497. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  4498. len
  4499. );
  4500. //zmemcpy(state.head.extra + len, next,
  4501. // len + copy > state.head.extra_max ?
  4502. // state.head.extra_max - len : copy);
  4503. }
  4504. if (state.flags & 0x0200) {
  4505. state.check = crc32_1(state.check, input, copy, next);
  4506. }
  4507. have -= copy;
  4508. next += copy;
  4509. state.length -= copy;
  4510. }
  4511. if (state.length) { break inf_leave; }
  4512. }
  4513. state.length = 0;
  4514. state.mode = NAME;
  4515. /* falls through */
  4516. case NAME:
  4517. if (state.flags & 0x0800) {
  4518. if (have === 0) { break inf_leave; }
  4519. copy = 0;
  4520. do {
  4521. // TODO: 2 or 1 bytes?
  4522. len = input[next + copy++];
  4523. /* use constant limit because in js we should not preallocate memory */
  4524. if (state.head && len &&
  4525. (state.length < 65536 /*state.head.name_max*/)) {
  4526. state.head.name += String.fromCharCode(len);
  4527. }
  4528. } while (len && copy < have);
  4529. if (state.flags & 0x0200) {
  4530. state.check = crc32_1(state.check, input, copy, next);
  4531. }
  4532. have -= copy;
  4533. next += copy;
  4534. if (len) { break inf_leave; }
  4535. }
  4536. else if (state.head) {
  4537. state.head.name = null;
  4538. }
  4539. state.length = 0;
  4540. state.mode = COMMENT;
  4541. /* falls through */
  4542. case COMMENT:
  4543. if (state.flags & 0x1000) {
  4544. if (have === 0) { break inf_leave; }
  4545. copy = 0;
  4546. do {
  4547. len = input[next + copy++];
  4548. /* use constant limit because in js we should not preallocate memory */
  4549. if (state.head && len &&
  4550. (state.length < 65536 /*state.head.comm_max*/)) {
  4551. state.head.comment += String.fromCharCode(len);
  4552. }
  4553. } while (len && copy < have);
  4554. if (state.flags & 0x0200) {
  4555. state.check = crc32_1(state.check, input, copy, next);
  4556. }
  4557. have -= copy;
  4558. next += copy;
  4559. if (len) { break inf_leave; }
  4560. }
  4561. else if (state.head) {
  4562. state.head.comment = null;
  4563. }
  4564. state.mode = HCRC;
  4565. /* falls through */
  4566. case HCRC:
  4567. if (state.flags & 0x0200) {
  4568. //=== NEEDBITS(16); */
  4569. while (bits < 16) {
  4570. if (have === 0) { break inf_leave; }
  4571. have--;
  4572. hold += input[next++] << bits;
  4573. bits += 8;
  4574. }
  4575. //===//
  4576. if (hold !== (state.check & 0xffff)) {
  4577. strm.msg = 'header crc mismatch';
  4578. state.mode = BAD;
  4579. break;
  4580. }
  4581. //=== INITBITS();
  4582. hold = 0;
  4583. bits = 0;
  4584. //===//
  4585. }
  4586. if (state.head) {
  4587. state.head.hcrc = ((state.flags >> 9) & 1);
  4588. state.head.done = true;
  4589. }
  4590. strm.adler = state.check = 0;
  4591. state.mode = TYPE;
  4592. break;
  4593. case DICTID:
  4594. //=== NEEDBITS(32); */
  4595. while (bits < 32) {
  4596. if (have === 0) { break inf_leave; }
  4597. have--;
  4598. hold += input[next++] << bits;
  4599. bits += 8;
  4600. }
  4601. //===//
  4602. strm.adler = state.check = zswap32(hold);
  4603. //=== INITBITS();
  4604. hold = 0;
  4605. bits = 0;
  4606. //===//
  4607. state.mode = DICT;
  4608. /* falls through */
  4609. case DICT:
  4610. if (state.havedict === 0) {
  4611. //--- RESTORE() ---
  4612. strm.next_out = put;
  4613. strm.avail_out = left;
  4614. strm.next_in = next;
  4615. strm.avail_in = have;
  4616. state.hold = hold;
  4617. state.bits = bits;
  4618. //---
  4619. return Z_NEED_DICT$1;
  4620. }
  4621. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  4622. state.mode = TYPE;
  4623. /* falls through */
  4624. case TYPE:
  4625. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  4626. /* falls through */
  4627. case TYPEDO:
  4628. if (state.last) {
  4629. //--- BYTEBITS() ---//
  4630. hold >>>= bits & 7;
  4631. bits -= bits & 7;
  4632. //---//
  4633. state.mode = CHECK;
  4634. break;
  4635. }
  4636. //=== NEEDBITS(3); */
  4637. while (bits < 3) {
  4638. if (have === 0) { break inf_leave; }
  4639. have--;
  4640. hold += input[next++] << bits;
  4641. bits += 8;
  4642. }
  4643. //===//
  4644. state.last = (hold & 0x01)/*BITS(1)*/;
  4645. //--- DROPBITS(1) ---//
  4646. hold >>>= 1;
  4647. bits -= 1;
  4648. //---//
  4649. switch ((hold & 0x03)/*BITS(2)*/) {
  4650. case 0: /* stored block */
  4651. //Tracev((stderr, "inflate: stored block%s\n",
  4652. // state.last ? " (last)" : ""));
  4653. state.mode = STORED;
  4654. break;
  4655. case 1: /* fixed block */
  4656. fixedtables(state);
  4657. //Tracev((stderr, "inflate: fixed codes block%s\n",
  4658. // state.last ? " (last)" : ""));
  4659. state.mode = LEN_; /* decode codes */
  4660. if (flush === Z_TREES) {
  4661. //--- DROPBITS(2) ---//
  4662. hold >>>= 2;
  4663. bits -= 2;
  4664. //---//
  4665. break inf_leave;
  4666. }
  4667. break;
  4668. case 2: /* dynamic block */
  4669. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  4670. // state.last ? " (last)" : ""));
  4671. state.mode = TABLE;
  4672. break;
  4673. case 3:
  4674. strm.msg = 'invalid block type';
  4675. state.mode = BAD;
  4676. }
  4677. //--- DROPBITS(2) ---//
  4678. hold >>>= 2;
  4679. bits -= 2;
  4680. //---//
  4681. break;
  4682. case STORED:
  4683. //--- BYTEBITS() ---// /* go to byte boundary */
  4684. hold >>>= bits & 7;
  4685. bits -= bits & 7;
  4686. //---//
  4687. //=== NEEDBITS(32); */
  4688. while (bits < 32) {
  4689. if (have === 0) { break inf_leave; }
  4690. have--;
  4691. hold += input[next++] << bits;
  4692. bits += 8;
  4693. }
  4694. //===//
  4695. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  4696. strm.msg = 'invalid stored block lengths';
  4697. state.mode = BAD;
  4698. break;
  4699. }
  4700. state.length = hold & 0xffff;
  4701. //Tracev((stderr, "inflate: stored length %u\n",
  4702. // state.length));
  4703. //=== INITBITS();
  4704. hold = 0;
  4705. bits = 0;
  4706. //===//
  4707. state.mode = COPY_;
  4708. if (flush === Z_TREES) { break inf_leave; }
  4709. /* falls through */
  4710. case COPY_:
  4711. state.mode = COPY;
  4712. /* falls through */
  4713. case COPY:
  4714. copy = state.length;
  4715. if (copy) {
  4716. if (copy > have) { copy = have; }
  4717. if (copy > left) { copy = left; }
  4718. if (copy === 0) { break inf_leave; }
  4719. //--- zmemcpy(put, next, copy); ---
  4720. output.set(input.subarray(next, next + copy), put);
  4721. //---//
  4722. have -= copy;
  4723. next += copy;
  4724. left -= copy;
  4725. put += copy;
  4726. state.length -= copy;
  4727. break;
  4728. }
  4729. //Tracev((stderr, "inflate: stored end\n"));
  4730. state.mode = TYPE;
  4731. break;
  4732. case TABLE:
  4733. //=== NEEDBITS(14); */
  4734. while (bits < 14) {
  4735. if (have === 0) { break inf_leave; }
  4736. have--;
  4737. hold += input[next++] << bits;
  4738. bits += 8;
  4739. }
  4740. //===//
  4741. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  4742. //--- DROPBITS(5) ---//
  4743. hold >>>= 5;
  4744. bits -= 5;
  4745. //---//
  4746. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  4747. //--- DROPBITS(5) ---//
  4748. hold >>>= 5;
  4749. bits -= 5;
  4750. //---//
  4751. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  4752. //--- DROPBITS(4) ---//
  4753. hold >>>= 4;
  4754. bits -= 4;
  4755. //---//
  4756. //#ifndef PKZIP_BUG_WORKAROUND
  4757. if (state.nlen > 286 || state.ndist > 30) {
  4758. strm.msg = 'too many length or distance symbols';
  4759. state.mode = BAD;
  4760. break;
  4761. }
  4762. //#endif
  4763. //Tracev((stderr, "inflate: table sizes ok\n"));
  4764. state.have = 0;
  4765. state.mode = LENLENS;
  4766. /* falls through */
  4767. case LENLENS:
  4768. while (state.have < state.ncode) {
  4769. //=== NEEDBITS(3);
  4770. while (bits < 3) {
  4771. if (have === 0) { break inf_leave; }
  4772. have--;
  4773. hold += input[next++] << bits;
  4774. bits += 8;
  4775. }
  4776. //===//
  4777. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  4778. //--- DROPBITS(3) ---//
  4779. hold >>>= 3;
  4780. bits -= 3;
  4781. //---//
  4782. }
  4783. while (state.have < 19) {
  4784. state.lens[order[state.have++]] = 0;
  4785. }
  4786. // We have separate tables & no pointers. 2 commented lines below not needed.
  4787. //state.next = state.codes;
  4788. //state.lencode = state.next;
  4789. // Switch to use dynamic table
  4790. state.lencode = state.lendyn;
  4791. state.lenbits = 7;
  4792. opts = { bits: state.lenbits };
  4793. ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  4794. state.lenbits = opts.bits;
  4795. if (ret) {
  4796. strm.msg = 'invalid code lengths set';
  4797. state.mode = BAD;
  4798. break;
  4799. }
  4800. //Tracev((stderr, "inflate: code lengths ok\n"));
  4801. state.have = 0;
  4802. state.mode = CODELENS;
  4803. /* falls through */
  4804. case CODELENS:
  4805. while (state.have < state.nlen + state.ndist) {
  4806. for (;;) {
  4807. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  4808. here_bits = here >>> 24;
  4809. here_op = (here >>> 16) & 0xff;
  4810. here_val = here & 0xffff;
  4811. if ((here_bits) <= bits) { break; }
  4812. //--- PULLBYTE() ---//
  4813. if (have === 0) { break inf_leave; }
  4814. have--;
  4815. hold += input[next++] << bits;
  4816. bits += 8;
  4817. //---//
  4818. }
  4819. if (here_val < 16) {
  4820. //--- DROPBITS(here.bits) ---//
  4821. hold >>>= here_bits;
  4822. bits -= here_bits;
  4823. //---//
  4824. state.lens[state.have++] = here_val;
  4825. }
  4826. else {
  4827. if (here_val === 16) {
  4828. //=== NEEDBITS(here.bits + 2);
  4829. n = here_bits + 2;
  4830. while (bits < n) {
  4831. if (have === 0) { break inf_leave; }
  4832. have--;
  4833. hold += input[next++] << bits;
  4834. bits += 8;
  4835. }
  4836. //===//
  4837. //--- DROPBITS(here.bits) ---//
  4838. hold >>>= here_bits;
  4839. bits -= here_bits;
  4840. //---//
  4841. if (state.have === 0) {
  4842. strm.msg = 'invalid bit length repeat';
  4843. state.mode = BAD;
  4844. break;
  4845. }
  4846. len = state.lens[state.have - 1];
  4847. copy = 3 + (hold & 0x03);//BITS(2);
  4848. //--- DROPBITS(2) ---//
  4849. hold >>>= 2;
  4850. bits -= 2;
  4851. //---//
  4852. }
  4853. else if (here_val === 17) {
  4854. //=== NEEDBITS(here.bits + 3);
  4855. n = here_bits + 3;
  4856. while (bits < n) {
  4857. if (have === 0) { break inf_leave; }
  4858. have--;
  4859. hold += input[next++] << bits;
  4860. bits += 8;
  4861. }
  4862. //===//
  4863. //--- DROPBITS(here.bits) ---//
  4864. hold >>>= here_bits;
  4865. bits -= here_bits;
  4866. //---//
  4867. len = 0;
  4868. copy = 3 + (hold & 0x07);//BITS(3);
  4869. //--- DROPBITS(3) ---//
  4870. hold >>>= 3;
  4871. bits -= 3;
  4872. //---//
  4873. }
  4874. else {
  4875. //=== NEEDBITS(here.bits + 7);
  4876. n = here_bits + 7;
  4877. while (bits < n) {
  4878. if (have === 0) { break inf_leave; }
  4879. have--;
  4880. hold += input[next++] << bits;
  4881. bits += 8;
  4882. }
  4883. //===//
  4884. //--- DROPBITS(here.bits) ---//
  4885. hold >>>= here_bits;
  4886. bits -= here_bits;
  4887. //---//
  4888. len = 0;
  4889. copy = 11 + (hold & 0x7f);//BITS(7);
  4890. //--- DROPBITS(7) ---//
  4891. hold >>>= 7;
  4892. bits -= 7;
  4893. //---//
  4894. }
  4895. if (state.have + copy > state.nlen + state.ndist) {
  4896. strm.msg = 'invalid bit length repeat';
  4897. state.mode = BAD;
  4898. break;
  4899. }
  4900. while (copy--) {
  4901. state.lens[state.have++] = len;
  4902. }
  4903. }
  4904. }
  4905. /* handle error breaks in while */
  4906. if (state.mode === BAD) { break; }
  4907. /* check for end-of-block code (better have one) */
  4908. if (state.lens[256] === 0) {
  4909. strm.msg = 'invalid code -- missing end-of-block';
  4910. state.mode = BAD;
  4911. break;
  4912. }
  4913. /* build code tables -- note: do not change the lenbits or distbits
  4914. values here (9 and 6) without reading the comments in inftrees.h
  4915. concerning the ENOUGH constants, which depend on those values */
  4916. state.lenbits = 9;
  4917. opts = { bits: state.lenbits };
  4918. ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  4919. // We have separate tables & no pointers. 2 commented lines below not needed.
  4920. // state.next_index = opts.table_index;
  4921. state.lenbits = opts.bits;
  4922. // state.lencode = state.next;
  4923. if (ret) {
  4924. strm.msg = 'invalid literal/lengths set';
  4925. state.mode = BAD;
  4926. break;
  4927. }
  4928. state.distbits = 6;
  4929. //state.distcode.copy(state.codes);
  4930. // Switch to use dynamic table
  4931. state.distcode = state.distdyn;
  4932. opts = { bits: state.distbits };
  4933. ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  4934. // We have separate tables & no pointers. 2 commented lines below not needed.
  4935. // state.next_index = opts.table_index;
  4936. state.distbits = opts.bits;
  4937. // state.distcode = state.next;
  4938. if (ret) {
  4939. strm.msg = 'invalid distances set';
  4940. state.mode = BAD;
  4941. break;
  4942. }
  4943. //Tracev((stderr, 'inflate: codes ok\n'));
  4944. state.mode = LEN_;
  4945. if (flush === Z_TREES) { break inf_leave; }
  4946. /* falls through */
  4947. case LEN_:
  4948. state.mode = LEN;
  4949. /* falls through */
  4950. case LEN:
  4951. if (have >= 6 && left >= 258) {
  4952. //--- RESTORE() ---
  4953. strm.next_out = put;
  4954. strm.avail_out = left;
  4955. strm.next_in = next;
  4956. strm.avail_in = have;
  4957. state.hold = hold;
  4958. state.bits = bits;
  4959. //---
  4960. inffast(strm, _out);
  4961. //--- LOAD() ---
  4962. put = strm.next_out;
  4963. output = strm.output;
  4964. left = strm.avail_out;
  4965. next = strm.next_in;
  4966. input = strm.input;
  4967. have = strm.avail_in;
  4968. hold = state.hold;
  4969. bits = state.bits;
  4970. //---
  4971. if (state.mode === TYPE) {
  4972. state.back = -1;
  4973. }
  4974. break;
  4975. }
  4976. state.back = 0;
  4977. for (;;) {
  4978. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  4979. here_bits = here >>> 24;
  4980. here_op = (here >>> 16) & 0xff;
  4981. here_val = here & 0xffff;
  4982. if (here_bits <= bits) { break; }
  4983. //--- PULLBYTE() ---//
  4984. if (have === 0) { break inf_leave; }
  4985. have--;
  4986. hold += input[next++] << bits;
  4987. bits += 8;
  4988. //---//
  4989. }
  4990. if (here_op && (here_op & 0xf0) === 0) {
  4991. last_bits = here_bits;
  4992. last_op = here_op;
  4993. last_val = here_val;
  4994. for (;;) {
  4995. here = state.lencode[last_val +
  4996. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  4997. here_bits = here >>> 24;
  4998. here_op = (here >>> 16) & 0xff;
  4999. here_val = here & 0xffff;
  5000. if ((last_bits + here_bits) <= bits) { break; }
  5001. //--- PULLBYTE() ---//
  5002. if (have === 0) { break inf_leave; }
  5003. have--;
  5004. hold += input[next++] << bits;
  5005. bits += 8;
  5006. //---//
  5007. }
  5008. //--- DROPBITS(last.bits) ---//
  5009. hold >>>= last_bits;
  5010. bits -= last_bits;
  5011. //---//
  5012. state.back += last_bits;
  5013. }
  5014. //--- DROPBITS(here.bits) ---//
  5015. hold >>>= here_bits;
  5016. bits -= here_bits;
  5017. //---//
  5018. state.back += here_bits;
  5019. state.length = here_val;
  5020. if (here_op === 0) {
  5021. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  5022. // "inflate: literal '%c'\n" :
  5023. // "inflate: literal 0x%02x\n", here.val));
  5024. state.mode = LIT;
  5025. break;
  5026. }
  5027. if (here_op & 32) {
  5028. //Tracevv((stderr, "inflate: end of block\n"));
  5029. state.back = -1;
  5030. state.mode = TYPE;
  5031. break;
  5032. }
  5033. if (here_op & 64) {
  5034. strm.msg = 'invalid literal/length code';
  5035. state.mode = BAD;
  5036. break;
  5037. }
  5038. state.extra = here_op & 15;
  5039. state.mode = LENEXT;
  5040. /* falls through */
  5041. case LENEXT:
  5042. if (state.extra) {
  5043. //=== NEEDBITS(state.extra);
  5044. n = state.extra;
  5045. while (bits < n) {
  5046. if (have === 0) { break inf_leave; }
  5047. have--;
  5048. hold += input[next++] << bits;
  5049. bits += 8;
  5050. }
  5051. //===//
  5052. state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  5053. //--- DROPBITS(state.extra) ---//
  5054. hold >>>= state.extra;
  5055. bits -= state.extra;
  5056. //---//
  5057. state.back += state.extra;
  5058. }
  5059. //Tracevv((stderr, "inflate: length %u\n", state.length));
  5060. state.was = state.length;
  5061. state.mode = DIST;
  5062. /* falls through */
  5063. case DIST:
  5064. for (;;) {
  5065. here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/
  5066. here_bits = here >>> 24;
  5067. here_op = (here >>> 16) & 0xff;
  5068. here_val = here & 0xffff;
  5069. if ((here_bits) <= bits) { break; }
  5070. //--- PULLBYTE() ---//
  5071. if (have === 0) { break inf_leave; }
  5072. have--;
  5073. hold += input[next++] << bits;
  5074. bits += 8;
  5075. //---//
  5076. }
  5077. if ((here_op & 0xf0) === 0) {
  5078. last_bits = here_bits;
  5079. last_op = here_op;
  5080. last_val = here_val;
  5081. for (;;) {
  5082. here = state.distcode[last_val +
  5083. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  5084. here_bits = here >>> 24;
  5085. here_op = (here >>> 16) & 0xff;
  5086. here_val = here & 0xffff;
  5087. if ((last_bits + here_bits) <= bits) { break; }
  5088. //--- PULLBYTE() ---//
  5089. if (have === 0) { break inf_leave; }
  5090. have--;
  5091. hold += input[next++] << bits;
  5092. bits += 8;
  5093. //---//
  5094. }
  5095. //--- DROPBITS(last.bits) ---//
  5096. hold >>>= last_bits;
  5097. bits -= last_bits;
  5098. //---//
  5099. state.back += last_bits;
  5100. }
  5101. //--- DROPBITS(here.bits) ---//
  5102. hold >>>= here_bits;
  5103. bits -= here_bits;
  5104. //---//
  5105. state.back += here_bits;
  5106. if (here_op & 64) {
  5107. strm.msg = 'invalid distance code';
  5108. state.mode = BAD;
  5109. break;
  5110. }
  5111. state.offset = here_val;
  5112. state.extra = (here_op) & 15;
  5113. state.mode = DISTEXT;
  5114. /* falls through */
  5115. case DISTEXT:
  5116. if (state.extra) {
  5117. //=== NEEDBITS(state.extra);
  5118. n = state.extra;
  5119. while (bits < n) {
  5120. if (have === 0) { break inf_leave; }
  5121. have--;
  5122. hold += input[next++] << bits;
  5123. bits += 8;
  5124. }
  5125. //===//
  5126. state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  5127. //--- DROPBITS(state.extra) ---//
  5128. hold >>>= state.extra;
  5129. bits -= state.extra;
  5130. //---//
  5131. state.back += state.extra;
  5132. }
  5133. //#ifdef INFLATE_STRICT
  5134. if (state.offset > state.dmax) {
  5135. strm.msg = 'invalid distance too far back';
  5136. state.mode = BAD;
  5137. break;
  5138. }
  5139. //#endif
  5140. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  5141. state.mode = MATCH;
  5142. /* falls through */
  5143. case MATCH:
  5144. if (left === 0) { break inf_leave; }
  5145. copy = _out - left;
  5146. if (state.offset > copy) { /* copy from window */
  5147. copy = state.offset - copy;
  5148. if (copy > state.whave) {
  5149. if (state.sane) {
  5150. strm.msg = 'invalid distance too far back';
  5151. state.mode = BAD;
  5152. break;
  5153. }
  5154. // (!) This block is disabled in zlib defaults,
  5155. // don't enable it for binary compatibility
  5156. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  5157. // Trace((stderr, "inflate.c too far\n"));
  5158. // copy -= state.whave;
  5159. // if (copy > state.length) { copy = state.length; }
  5160. // if (copy > left) { copy = left; }
  5161. // left -= copy;
  5162. // state.length -= copy;
  5163. // do {
  5164. // output[put++] = 0;
  5165. // } while (--copy);
  5166. // if (state.length === 0) { state.mode = LEN; }
  5167. // break;
  5168. //#endif
  5169. }
  5170. if (copy > state.wnext) {
  5171. copy -= state.wnext;
  5172. from = state.wsize - copy;
  5173. }
  5174. else {
  5175. from = state.wnext - copy;
  5176. }
  5177. if (copy > state.length) { copy = state.length; }
  5178. from_source = state.window;
  5179. }
  5180. else { /* copy from output */
  5181. from_source = output;
  5182. from = put - state.offset;
  5183. copy = state.length;
  5184. }
  5185. if (copy > left) { copy = left; }
  5186. left -= copy;
  5187. state.length -= copy;
  5188. do {
  5189. output[put++] = from_source[from++];
  5190. } while (--copy);
  5191. if (state.length === 0) { state.mode = LEN; }
  5192. break;
  5193. case LIT:
  5194. if (left === 0) { break inf_leave; }
  5195. output[put++] = state.length;
  5196. left--;
  5197. state.mode = LEN;
  5198. break;
  5199. case CHECK:
  5200. if (state.wrap) {
  5201. //=== NEEDBITS(32);
  5202. while (bits < 32) {
  5203. if (have === 0) { break inf_leave; }
  5204. have--;
  5205. // Use '|' instead of '+' to make sure that result is signed
  5206. hold |= input[next++] << bits;
  5207. bits += 8;
  5208. }
  5209. //===//
  5210. _out -= left;
  5211. strm.total_out += _out;
  5212. state.total += _out;
  5213. if (_out) {
  5214. strm.adler = state.check =
  5215. /*UPDATE(state.check, put - _out, _out);*/
  5216. (state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out));
  5217. }
  5218. _out = left;
  5219. // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  5220. if ((state.flags ? hold : zswap32(hold)) !== state.check) {
  5221. strm.msg = 'incorrect data check';
  5222. state.mode = BAD;
  5223. break;
  5224. }
  5225. //=== INITBITS();
  5226. hold = 0;
  5227. bits = 0;
  5228. //===//
  5229. //Tracev((stderr, "inflate: check matches trailer\n"));
  5230. }
  5231. state.mode = LENGTH;
  5232. /* falls through */
  5233. case LENGTH:
  5234. if (state.wrap && state.flags) {
  5235. //=== NEEDBITS(32);
  5236. while (bits < 32) {
  5237. if (have === 0) { break inf_leave; }
  5238. have--;
  5239. hold += input[next++] << bits;
  5240. bits += 8;
  5241. }
  5242. //===//
  5243. if (hold !== (state.total & 0xffffffff)) {
  5244. strm.msg = 'incorrect length check';
  5245. state.mode = BAD;
  5246. break;
  5247. }
  5248. //=== INITBITS();
  5249. hold = 0;
  5250. bits = 0;
  5251. //===//
  5252. //Tracev((stderr, "inflate: length matches trailer\n"));
  5253. }
  5254. state.mode = DONE;
  5255. /* falls through */
  5256. case DONE:
  5257. ret = Z_STREAM_END$1;
  5258. break inf_leave;
  5259. case BAD:
  5260. ret = Z_DATA_ERROR$1;
  5261. break inf_leave;
  5262. case MEM:
  5263. return Z_MEM_ERROR$1;
  5264. case SYNC:
  5265. /* falls through */
  5266. default:
  5267. return Z_STREAM_ERROR$1;
  5268. }
  5269. }
  5270. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  5271. /*
  5272. Return from inflate(), updating the total counts and the check value.
  5273. If there was no progress during the inflate() call, return a buffer
  5274. error. Call updatewindow() to create and/or update the window state.
  5275. Note: a memory error from inflate() is non-recoverable.
  5276. */
  5277. //--- RESTORE() ---
  5278. strm.next_out = put;
  5279. strm.avail_out = left;
  5280. strm.next_in = next;
  5281. strm.avail_in = have;
  5282. state.hold = hold;
  5283. state.bits = bits;
  5284. //---
  5285. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  5286. (state.mode < CHECK || flush !== Z_FINISH$1))) {
  5287. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ;
  5288. }
  5289. _in -= strm.avail_in;
  5290. _out -= strm.avail_out;
  5291. strm.total_in += _in;
  5292. strm.total_out += _out;
  5293. state.total += _out;
  5294. if (state.wrap && _out) {
  5295. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  5296. (state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out));
  5297. }
  5298. strm.data_type = state.bits + (state.last ? 64 : 0) +
  5299. (state.mode === TYPE ? 128 : 0) +
  5300. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  5301. if (((_in === 0 && _out === 0) || flush === Z_FINISH$1) && ret === Z_OK$1) {
  5302. ret = Z_BUF_ERROR;
  5303. }
  5304. return ret;
  5305. };
  5306. const inflateEnd = (strm) => {
  5307. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
  5308. return Z_STREAM_ERROR$1;
  5309. }
  5310. let state = strm.state;
  5311. if (state.window) {
  5312. state.window = null;
  5313. }
  5314. strm.state = null;
  5315. return Z_OK$1;
  5316. };
  5317. const inflateGetHeader = (strm, head) => {
  5318. /* check state */
  5319. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  5320. const state = strm.state;
  5321. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR$1; }
  5322. /* save header structure */
  5323. state.head = head;
  5324. head.done = false;
  5325. return Z_OK$1;
  5326. };
  5327. const inflateSetDictionary = (strm, dictionary) => {
  5328. const dictLength = dictionary.length;
  5329. let state;
  5330. let dictid;
  5331. let ret;
  5332. /* check state */
  5333. if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR$1; }
  5334. state = strm.state;
  5335. if (state.wrap !== 0 && state.mode !== DICT) {
  5336. return Z_STREAM_ERROR$1;
  5337. }
  5338. /* check for correct dictionary identifier */
  5339. if (state.mode === DICT) {
  5340. dictid = 1; /* adler32(0, null, 0)*/
  5341. /* dictid = adler32(dictid, dictionary, dictLength); */
  5342. dictid = adler32_1(dictid, dictionary, dictLength, 0);
  5343. if (dictid !== state.check) {
  5344. return Z_DATA_ERROR$1;
  5345. }
  5346. }
  5347. /* copy dictionary to window using updatewindow(), which will amend the
  5348. existing dictionary if appropriate */
  5349. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  5350. if (ret) {
  5351. state.mode = MEM;
  5352. return Z_MEM_ERROR$1;
  5353. }
  5354. state.havedict = 1;
  5355. // Tracev((stderr, "inflate: dictionary set\n"));
  5356. return Z_OK$1;
  5357. };
  5358. var inflateReset_1 = inflateReset;
  5359. var inflateReset2_1 = inflateReset2;
  5360. var inflateResetKeep_1 = inflateResetKeep;
  5361. var inflateInit_1 = inflateInit;
  5362. var inflateInit2_1 = inflateInit2;
  5363. var inflate_2$1 = inflate$2;
  5364. var inflateEnd_1 = inflateEnd;
  5365. var inflateGetHeader_1 = inflateGetHeader;
  5366. var inflateSetDictionary_1 = inflateSetDictionary;
  5367. var inflateInfo = 'pako inflate (from Nodeca project)';
  5368. /* Not implemented
  5369. module.exports.inflateCopy = inflateCopy;
  5370. module.exports.inflateGetDictionary = inflateGetDictionary;
  5371. module.exports.inflateMark = inflateMark;
  5372. module.exports.inflatePrime = inflatePrime;
  5373. module.exports.inflateSync = inflateSync;
  5374. module.exports.inflateSyncPoint = inflateSyncPoint;
  5375. module.exports.inflateUndermine = inflateUndermine;
  5376. */
  5377. var inflate_1$2 = {
  5378. inflateReset: inflateReset_1,
  5379. inflateReset2: inflateReset2_1,
  5380. inflateResetKeep: inflateResetKeep_1,
  5381. inflateInit: inflateInit_1,
  5382. inflateInit2: inflateInit2_1,
  5383. inflate: inflate_2$1,
  5384. inflateEnd: inflateEnd_1,
  5385. inflateGetHeader: inflateGetHeader_1,
  5386. inflateSetDictionary: inflateSetDictionary_1,
  5387. inflateInfo: inflateInfo
  5388. };
  5389. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  5390. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  5391. //
  5392. // This software is provided 'as-is', without any express or implied
  5393. // warranty. In no event will the authors be held liable for any damages
  5394. // arising from the use of this software.
  5395. //
  5396. // Permission is granted to anyone to use this software for any purpose,
  5397. // including commercial applications, and to alter it and redistribute it
  5398. // freely, subject to the following restrictions:
  5399. //
  5400. // 1. The origin of this software must not be misrepresented; you must not
  5401. // claim that you wrote the original software. If you use this software
  5402. // in a product, an acknowledgment in the product documentation would be
  5403. // appreciated but is not required.
  5404. // 2. Altered source versions must be plainly marked as such, and must not be
  5405. // misrepresented as being the original software.
  5406. // 3. This notice may not be removed or altered from any source distribution.
  5407. function GZheader() {
  5408. /* true if compressed data believed to be text */
  5409. this.text = 0;
  5410. /* modification time */
  5411. this.time = 0;
  5412. /* extra flags (not used when writing a gzip file) */
  5413. this.xflags = 0;
  5414. /* operating system */
  5415. this.os = 0;
  5416. /* pointer to extra field or Z_NULL if none */
  5417. this.extra = null;
  5418. /* extra field length (valid if extra != Z_NULL) */
  5419. this.extra_len = 0; // Actually, we don't need it in JS,
  5420. // but leave for few code modifications
  5421. //
  5422. // Setup limits is not necessary because in js we should not preallocate memory
  5423. // for inflate use constant limit in 65536 bytes
  5424. //
  5425. /* space at extra (only when reading header) */
  5426. // this.extra_max = 0;
  5427. /* pointer to zero-terminated file name or Z_NULL */
  5428. this.name = '';
  5429. /* space at name (only when reading header) */
  5430. // this.name_max = 0;
  5431. /* pointer to zero-terminated comment or Z_NULL */
  5432. this.comment = '';
  5433. /* space at comment (only when reading header) */
  5434. // this.comm_max = 0;
  5435. /* true if there was or will be a header crc */
  5436. this.hcrc = 0;
  5437. /* true when done reading gzip header (not used when writing a gzip file) */
  5438. this.done = false;
  5439. }
  5440. var gzheader = GZheader;
  5441. const toString = Object.prototype.toString;
  5442. /* Public constants ==========================================================*/
  5443. /* ===========================================================================*/
  5444. const {
  5445. Z_NO_FLUSH, Z_FINISH,
  5446. Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR
  5447. } = constants$2;
  5448. /* ===========================================================================*/
  5449. /**
  5450. * class Inflate
  5451. *
  5452. * Generic JS-style wrapper for zlib calls. If you don't need
  5453. * streaming behaviour - use more simple functions: [[inflate]]
  5454. * and [[inflateRaw]].
  5455. **/
  5456. /* internal
  5457. * inflate.chunks -> Array
  5458. *
  5459. * Chunks of output data, if [[Inflate#onData]] not overridden.
  5460. **/
  5461. /**
  5462. * Inflate.result -> Uint8Array|String
  5463. *
  5464. * Uncompressed result, generated by default [[Inflate#onData]]
  5465. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  5466. * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
  5467. **/
  5468. /**
  5469. * Inflate.err -> Number
  5470. *
  5471. * Error code after inflate finished. 0 (Z_OK) on success.
  5472. * Should be checked if broken data possible.
  5473. **/
  5474. /**
  5475. * Inflate.msg -> String
  5476. *
  5477. * Error message, if [[Inflate.err]] != 0
  5478. **/
  5479. /**
  5480. * new Inflate(options)
  5481. * - options (Object): zlib inflate options.
  5482. *
  5483. * Creates new inflator instance with specified params. Throws exception
  5484. * on bad params. Supported options:
  5485. *
  5486. * - `windowBits`
  5487. * - `dictionary`
  5488. *
  5489. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  5490. * for more information on these.
  5491. *
  5492. * Additional options, for internal needs:
  5493. *
  5494. * - `chunkSize` - size of generated data chunks (16K by default)
  5495. * - `raw` (Boolean) - do raw inflate
  5496. * - `to` (String) - if equal to 'string', then result will be converted
  5497. * from utf8 to utf16 (javascript) string. When string output requested,
  5498. * chunk length can differ from `chunkSize`, depending on content.
  5499. *
  5500. * By default, when no options set, autodetect deflate/gzip data format via
  5501. * wrapper header.
  5502. *
  5503. * ##### Example:
  5504. *
  5505. * ```javascript
  5506. * const pako = require('pako')
  5507. * const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  5508. * const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  5509. *
  5510. * const inflate = new pako.Inflate({ level: 3});
  5511. *
  5512. * inflate.push(chunk1, false);
  5513. * inflate.push(chunk2, true); // true -> last chunk
  5514. *
  5515. * if (inflate.err) { throw new Error(inflate.err); }
  5516. *
  5517. * console.log(inflate.result);
  5518. * ```
  5519. **/
  5520. function Inflate$1(options) {
  5521. this.options = common.assign({
  5522. chunkSize: 1024 * 64,
  5523. windowBits: 15,
  5524. to: ''
  5525. }, options || {});
  5526. const opt = this.options;
  5527. // Force window size for `raw` data, if not set directly,
  5528. // because we have no header for autodetect.
  5529. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  5530. opt.windowBits = -opt.windowBits;
  5531. if (opt.windowBits === 0) { opt.windowBits = -15; }
  5532. }
  5533. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  5534. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  5535. !(options && options.windowBits)) {
  5536. opt.windowBits += 32;
  5537. }
  5538. // Gzip header has no info about windows size, we can do autodetect only
  5539. // for deflate. So, if window size not set, force it to max when gzip possible
  5540. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  5541. // bit 3 (16) -> gzipped data
  5542. // bit 4 (32) -> autodetect gzip/deflate
  5543. if ((opt.windowBits & 15) === 0) {
  5544. opt.windowBits |= 15;
  5545. }
  5546. }
  5547. this.err = 0; // error code, if happens (0 = Z_OK)
  5548. this.msg = ''; // error message
  5549. this.ended = false; // used to avoid multiple onEnd() calls
  5550. this.chunks = []; // chunks of compressed data
  5551. this.strm = new zstream();
  5552. this.strm.avail_out = 0;
  5553. let status = inflate_1$2.inflateInit2(
  5554. this.strm,
  5555. opt.windowBits
  5556. );
  5557. if (status !== Z_OK) {
  5558. throw new Error(messages[status]);
  5559. }
  5560. this.header = new gzheader();
  5561. inflate_1$2.inflateGetHeader(this.strm, this.header);
  5562. // Setup dictionary
  5563. if (opt.dictionary) {
  5564. // Convert data if needed
  5565. if (typeof opt.dictionary === 'string') {
  5566. opt.dictionary = strings.string2buf(opt.dictionary);
  5567. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  5568. opt.dictionary = new Uint8Array(opt.dictionary);
  5569. }
  5570. if (opt.raw) { //In raw mode we need to set the dictionary early
  5571. status = inflate_1$2.inflateSetDictionary(this.strm, opt.dictionary);
  5572. if (status !== Z_OK) {
  5573. throw new Error(messages[status]);
  5574. }
  5575. }
  5576. }
  5577. }
  5578. /**
  5579. * Inflate#push(data[, flush_mode]) -> Boolean
  5580. * - data (Uint8Array|ArrayBuffer): input data
  5581. * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE
  5582. * flush modes. See constants. Skipped or `false` means Z_NO_FLUSH,
  5583. * `true` means Z_FINISH.
  5584. *
  5585. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  5586. * new output chunks. Returns `true` on success. If end of stream detected,
  5587. * [[Inflate#onEnd]] will be called.
  5588. *
  5589. * `flush_mode` is not needed for normal operation, because end of stream
  5590. * detected automatically. You may try to use it for advanced things, but
  5591. * this functionality was not tested.
  5592. *
  5593. * On fail call [[Inflate#onEnd]] with error code and return false.
  5594. *
  5595. * ##### Example
  5596. *
  5597. * ```javascript
  5598. * push(chunk, false); // push one of data chunks
  5599. * ...
  5600. * push(chunk, true); // push last chunk
  5601. * ```
  5602. **/
  5603. Inflate$1.prototype.push = function (data, flush_mode) {
  5604. const strm = this.strm;
  5605. const chunkSize = this.options.chunkSize;
  5606. const dictionary = this.options.dictionary;
  5607. let status, _flush_mode, last_avail_out;
  5608. if (this.ended) return false;
  5609. if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
  5610. else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH;
  5611. // Convert data if needed
  5612. if (toString.call(data) === '[object ArrayBuffer]') {
  5613. strm.input = new Uint8Array(data);
  5614. } else {
  5615. strm.input = data;
  5616. }
  5617. strm.next_in = 0;
  5618. strm.avail_in = strm.input.length;
  5619. for (;;) {
  5620. if (strm.avail_out === 0) {
  5621. strm.output = new Uint8Array(chunkSize);
  5622. strm.next_out = 0;
  5623. strm.avail_out = chunkSize;
  5624. }
  5625. status = inflate_1$2.inflate(strm, _flush_mode);
  5626. if (status === Z_NEED_DICT && dictionary) {
  5627. status = inflate_1$2.inflateSetDictionary(strm, dictionary);
  5628. if (status === Z_OK) {
  5629. status = inflate_1$2.inflate(strm, _flush_mode);
  5630. } else if (status === Z_DATA_ERROR) {
  5631. // Replace code with more verbose
  5632. status = Z_NEED_DICT;
  5633. }
  5634. }
  5635. // Skip snyc markers if more data follows and not raw mode
  5636. while (strm.avail_in > 0 &&
  5637. status === Z_STREAM_END &&
  5638. strm.state.wrap > 0 &&
  5639. data[strm.next_in] !== 0)
  5640. {
  5641. inflate_1$2.inflateReset(strm);
  5642. status = inflate_1$2.inflate(strm, _flush_mode);
  5643. }
  5644. switch (status) {
  5645. case Z_STREAM_ERROR:
  5646. case Z_DATA_ERROR:
  5647. case Z_NEED_DICT:
  5648. case Z_MEM_ERROR:
  5649. this.onEnd(status);
  5650. this.ended = true;
  5651. return false;
  5652. }
  5653. // Remember real `avail_out` value, because we may patch out buffer content
  5654. // to align utf8 strings boundaries.
  5655. last_avail_out = strm.avail_out;
  5656. if (strm.next_out) {
  5657. if (strm.avail_out === 0 || status === Z_STREAM_END) {
  5658. if (this.options.to === 'string') {
  5659. let next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  5660. let tail = strm.next_out - next_out_utf8;
  5661. let utf8str = strings.buf2string(strm.output, next_out_utf8);
  5662. // move tail & realign counters
  5663. strm.next_out = tail;
  5664. strm.avail_out = chunkSize - tail;
  5665. if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0);
  5666. this.onData(utf8str);
  5667. } else {
  5668. this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out));
  5669. }
  5670. }
  5671. }
  5672. // Must repeat iteration if out buffer is full
  5673. if (status === Z_OK && last_avail_out === 0) continue;
  5674. // Finalize if end of stream reached.
  5675. if (status === Z_STREAM_END) {
  5676. status = inflate_1$2.inflateEnd(this.strm);
  5677. this.onEnd(status);
  5678. this.ended = true;
  5679. return true;
  5680. }
  5681. if (strm.avail_in === 0) break;
  5682. }
  5683. return true;
  5684. };
  5685. /**
  5686. * Inflate#onData(chunk) -> Void
  5687. * - chunk (Uint8Array|String): output data. When string output requested,
  5688. * each chunk will be string.
  5689. *
  5690. * By default, stores data blocks in `chunks[]` property and glue
  5691. * those in `onEnd`. Override this handler, if you need another behaviour.
  5692. **/
  5693. Inflate$1.prototype.onData = function (chunk) {
  5694. this.chunks.push(chunk);
  5695. };
  5696. /**
  5697. * Inflate#onEnd(status) -> Void
  5698. * - status (Number): inflate status. 0 (Z_OK) on success,
  5699. * other if not.
  5700. *
  5701. * Called either after you tell inflate that the input stream is
  5702. * complete (Z_FINISH). By default - join collected chunks,
  5703. * free memory and fill `results` / `err` properties.
  5704. **/
  5705. Inflate$1.prototype.onEnd = function (status) {
  5706. // On success - join
  5707. if (status === Z_OK) {
  5708. if (this.options.to === 'string') {
  5709. this.result = this.chunks.join('');
  5710. } else {
  5711. this.result = common.flattenChunks(this.chunks);
  5712. }
  5713. }
  5714. this.chunks = [];
  5715. this.err = status;
  5716. this.msg = this.strm.msg;
  5717. };
  5718. /**
  5719. * inflate(data[, options]) -> Uint8Array|String
  5720. * - data (Uint8Array): input data to decompress.
  5721. * - options (Object): zlib inflate options.
  5722. *
  5723. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  5724. * format via wrapper header by default. That's why we don't provide
  5725. * separate `ungzip` method.
  5726. *
  5727. * Supported options are:
  5728. *
  5729. * - windowBits
  5730. *
  5731. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  5732. * for more information.
  5733. *
  5734. * Sugar (options):
  5735. *
  5736. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  5737. * negative windowBits implicitly.
  5738. * - `to` (String) - if equal to 'string', then result will be converted
  5739. * from utf8 to utf16 (javascript) string. When string output requested,
  5740. * chunk length can differ from `chunkSize`, depending on content.
  5741. *
  5742. *
  5743. * ##### Example:
  5744. *
  5745. * ```javascript
  5746. * const pako = require('pako');
  5747. * const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9]));
  5748. * let output;
  5749. *
  5750. * try {
  5751. * output = pako.inflate(input);
  5752. * } catch (err) {
  5753. * console.log(err);
  5754. * }
  5755. * ```
  5756. **/
  5757. function inflate$1(input, options) {
  5758. const inflator = new Inflate$1(options);
  5759. inflator.push(input);
  5760. // That will never happens, if you don't cheat with options :)
  5761. if (inflator.err) throw inflator.msg || messages[inflator.err];
  5762. return inflator.result;
  5763. }
  5764. /**
  5765. * inflateRaw(data[, options]) -> Uint8Array|String
  5766. * - data (Uint8Array): input data to decompress.
  5767. * - options (Object): zlib inflate options.
  5768. *
  5769. * The same as [[inflate]], but creates raw data, without wrapper
  5770. * (header and adler32 crc).
  5771. **/
  5772. function inflateRaw$1(input, options) {
  5773. options = options || {};
  5774. options.raw = true;
  5775. return inflate$1(input, options);
  5776. }
  5777. /**
  5778. * ungzip(data[, options]) -> Uint8Array|String
  5779. * - data (Uint8Array): input data to decompress.
  5780. * - options (Object): zlib inflate options.
  5781. *
  5782. * Just shortcut to [[inflate]], because it autodetects format
  5783. * by header.content. Done for convenience.
  5784. **/
  5785. var Inflate_1$1 = Inflate$1;
  5786. var inflate_2 = inflate$1;
  5787. var inflateRaw_1$1 = inflateRaw$1;
  5788. var ungzip$1 = inflate$1;
  5789. var constants = constants$2;
  5790. var inflate_1$1 = {
  5791. Inflate: Inflate_1$1,
  5792. inflate: inflate_2,
  5793. inflateRaw: inflateRaw_1$1,
  5794. ungzip: ungzip$1,
  5795. constants: constants
  5796. };
  5797. const { Deflate, deflate, deflateRaw, gzip } = deflate_1$1;
  5798. const { Inflate, inflate, inflateRaw, ungzip } = inflate_1$1;
  5799. var Deflate_1 = Deflate;
  5800. var deflate_1 = deflate;
  5801. var deflateRaw_1 = deflateRaw;
  5802. var gzip_1 = gzip;
  5803. var Inflate_1 = Inflate;
  5804. var inflate_1 = inflate;
  5805. var inflateRaw_1 = inflateRaw;
  5806. var ungzip_1 = ungzip;
  5807. var constants_1 = constants$2;
  5808. var pako = {
  5809. Deflate: Deflate_1,
  5810. deflate: deflate_1,
  5811. deflateRaw: deflateRaw_1,
  5812. gzip: gzip_1,
  5813. Inflate: Inflate_1,
  5814. inflate: inflate_1,
  5815. inflateRaw: inflateRaw_1,
  5816. ungzip: ungzip_1,
  5817. constants: constants_1
  5818. };
  5819. exports.Deflate = Deflate_1;
  5820. exports.Inflate = Inflate_1;
  5821. exports.constants = constants_1;
  5822. exports['default'] = pako;
  5823. exports.deflate = deflate_1;
  5824. exports.deflateRaw = deflateRaw_1;
  5825. exports.gzip = gzip_1;
  5826. exports.inflate = inflate_1;
  5827. exports.inflateRaw = inflateRaw_1;
  5828. exports.ungzip = ungzip_1;
  5829. Object.defineProperty(exports, '__esModule', { value: true });
  5830. })));