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.

3218 lines
108 KiB

1 year ago
  1. /*! pako 2.0.4 https://github.com/nodeca/pako @license (MIT AND Zlib) */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.pako = {}));
  6. }(this, (function (exports) { 'use strict';
  7. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  8. // It isn't worth it to make additional optimizations as in original.
  9. // Small size is preferable.
  10. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  11. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  12. //
  13. // This software is provided 'as-is', without any express or implied
  14. // warranty. In no event will the authors be held liable for any damages
  15. // arising from the use of this software.
  16. //
  17. // Permission is granted to anyone to use this software for any purpose,
  18. // including commercial applications, and to alter it and redistribute it
  19. // freely, subject to the following restrictions:
  20. //
  21. // 1. The origin of this software must not be misrepresented; you must not
  22. // claim that you wrote the original software. If you use this software
  23. // in a product, an acknowledgment in the product documentation would be
  24. // appreciated but is not required.
  25. // 2. Altered source versions must be plainly marked as such, and must not be
  26. // misrepresented as being the original software.
  27. // 3. This notice may not be removed or altered from any source distribution.
  28. const adler32 = (adler, buf, len, pos) => {
  29. let s1 = (adler & 0xffff) |0,
  30. s2 = ((adler >>> 16) & 0xffff) |0,
  31. n = 0;
  32. while (len !== 0) {
  33. // Set limit ~ twice less than 5552, to keep
  34. // s2 in 31-bits, because we force signed ints.
  35. // in other case %= will fail.
  36. n = len > 2000 ? 2000 : len;
  37. len -= n;
  38. do {
  39. s1 = (s1 + buf[pos++]) |0;
  40. s2 = (s2 + s1) |0;
  41. } while (--n);
  42. s1 %= 65521;
  43. s2 %= 65521;
  44. }
  45. return (s1 | (s2 << 16)) |0;
  46. };
  47. var adler32_1 = adler32;
  48. // Note: we can't get significant speed boost here.
  49. // So write code to minimize size - no pregenerated tables
  50. // and array tools dependencies.
  51. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  52. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  53. //
  54. // This software is provided 'as-is', without any express or implied
  55. // warranty. In no event will the authors be held liable for any damages
  56. // arising from the use of this software.
  57. //
  58. // Permission is granted to anyone to use this software for any purpose,
  59. // including commercial applications, and to alter it and redistribute it
  60. // freely, subject to the following restrictions:
  61. //
  62. // 1. The origin of this software must not be misrepresented; you must not
  63. // claim that you wrote the original software. If you use this software
  64. // in a product, an acknowledgment in the product documentation would be
  65. // appreciated but is not required.
  66. // 2. Altered source versions must be plainly marked as such, and must not be
  67. // misrepresented as being the original software.
  68. // 3. This notice may not be removed or altered from any source distribution.
  69. // Use ordinary array, since untyped makes no boost here
  70. const makeTable = () => {
  71. let c, table = [];
  72. for (var n = 0; n < 256; n++) {
  73. c = n;
  74. for (var k = 0; k < 8; k++) {
  75. c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  76. }
  77. table[n] = c;
  78. }
  79. return table;
  80. };
  81. // Create table on load. Just 255 signed longs. Not a problem.
  82. const crcTable = new Uint32Array(makeTable());
  83. const crc32 = (crc, buf, len, pos) => {
  84. const t = crcTable;
  85. const end = pos + len;
  86. crc ^= -1;
  87. for (let i = pos; i < end; i++) {
  88. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  89. }
  90. return (crc ^ (-1)); // >>> 0;
  91. };
  92. var crc32_1 = crc32;
  93. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  94. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  95. //
  96. // This software is provided 'as-is', without any express or implied
  97. // warranty. In no event will the authors be held liable for any damages
  98. // arising from the use of this software.
  99. //
  100. // Permission is granted to anyone to use this software for any purpose,
  101. // including commercial applications, and to alter it and redistribute it
  102. // freely, subject to the following restrictions:
  103. //
  104. // 1. The origin of this software must not be misrepresented; you must not
  105. // claim that you wrote the original software. If you use this software
  106. // in a product, an acknowledgment in the product documentation would be
  107. // appreciated but is not required.
  108. // 2. Altered source versions must be plainly marked as such, and must not be
  109. // misrepresented as being the original software.
  110. // 3. This notice may not be removed or altered from any source distribution.
  111. // See state defs from inflate.js
  112. const BAD$1 = 30; /* got a data error -- remain here until reset */
  113. const TYPE$1 = 12; /* i: waiting for type bits, including last-flag bit */
  114. /*
  115. Decode literal, length, and distance codes and write out the resulting
  116. literal and match bytes until either not enough input or output is
  117. available, an end-of-block is encountered, or a data error is encountered.
  118. When large enough input and output buffers are supplied to inflate(), for
  119. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  120. inflate execution time is spent in this routine.
  121. Entry assumptions:
  122. state.mode === LEN
  123. strm.avail_in >= 6
  124. strm.avail_out >= 258
  125. start >= strm.avail_out
  126. state.bits < 8
  127. On return, state.mode is one of:
  128. LEN -- ran out of enough output space or enough available input
  129. TYPE -- reached end of block code, inflate() to interpret next block
  130. BAD -- error in block data
  131. Notes:
  132. - The maximum input bits used by a length/distance pair is 15 bits for the
  133. length code, 5 bits for the length extra, 15 bits for the distance code,
  134. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  135. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  136. checking for available input while decoding.
  137. - The maximum bytes that a single length/distance pair can output is 258
  138. bytes, which is the maximum length that can be coded. inflate_fast()
  139. requires strm.avail_out >= 258 for each loop to avoid checking for
  140. output space.
  141. */
  142. var inffast = function inflate_fast(strm, start) {
  143. let _in; /* local strm.input */
  144. let last; /* have enough input while in < last */
  145. let _out; /* local strm.output */
  146. let beg; /* inflate()'s initial strm.output */
  147. let end; /* while out < end, enough space available */
  148. //#ifdef INFLATE_STRICT
  149. let dmax; /* maximum distance from zlib header */
  150. //#endif
  151. let wsize; /* window size or zero if not using window */
  152. let whave; /* valid bytes in the window */
  153. let wnext; /* window write index */
  154. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  155. let s_window; /* allocated sliding window, if wsize != 0 */
  156. let hold; /* local strm.hold */
  157. let bits; /* local strm.bits */
  158. let lcode; /* local strm.lencode */
  159. let dcode; /* local strm.distcode */
  160. let lmask; /* mask for first level of length codes */
  161. let dmask; /* mask for first level of distance codes */
  162. let here; /* retrieved table entry */
  163. let op; /* code bits, operation, extra bits, or */
  164. /* window position, window bytes to copy */
  165. let len; /* match length, unused bytes */
  166. let dist; /* match distance */
  167. let from; /* where to copy match from */
  168. let from_source;
  169. let input, output; // JS specific, because we have no pointers
  170. /* copy state to local variables */
  171. const state = strm.state;
  172. //here = state.here;
  173. _in = strm.next_in;
  174. input = strm.input;
  175. last = _in + (strm.avail_in - 5);
  176. _out = strm.next_out;
  177. output = strm.output;
  178. beg = _out - (start - strm.avail_out);
  179. end = _out + (strm.avail_out - 257);
  180. //#ifdef INFLATE_STRICT
  181. dmax = state.dmax;
  182. //#endif
  183. wsize = state.wsize;
  184. whave = state.whave;
  185. wnext = state.wnext;
  186. s_window = state.window;
  187. hold = state.hold;
  188. bits = state.bits;
  189. lcode = state.lencode;
  190. dcode = state.distcode;
  191. lmask = (1 << state.lenbits) - 1;
  192. dmask = (1 << state.distbits) - 1;
  193. /* decode literals and length/distances until end-of-block or not enough
  194. input data or output space */
  195. top:
  196. do {
  197. if (bits < 15) {
  198. hold += input[_in++] << bits;
  199. bits += 8;
  200. hold += input[_in++] << bits;
  201. bits += 8;
  202. }
  203. here = lcode[hold & lmask];
  204. dolen:
  205. for (;;) { // Goto emulation
  206. op = here >>> 24/*here.bits*/;
  207. hold >>>= op;
  208. bits -= op;
  209. op = (here >>> 16) & 0xff/*here.op*/;
  210. if (op === 0) { /* literal */
  211. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  212. // "inflate: literal '%c'\n" :
  213. // "inflate: literal 0x%02x\n", here.val));
  214. output[_out++] = here & 0xffff/*here.val*/;
  215. }
  216. else if (op & 16) { /* length base */
  217. len = here & 0xffff/*here.val*/;
  218. op &= 15; /* number of extra bits */
  219. if (op) {
  220. if (bits < op) {
  221. hold += input[_in++] << bits;
  222. bits += 8;
  223. }
  224. len += hold & ((1 << op) - 1);
  225. hold >>>= op;
  226. bits -= op;
  227. }
  228. //Tracevv((stderr, "inflate: length %u\n", len));
  229. if (bits < 15) {
  230. hold += input[_in++] << bits;
  231. bits += 8;
  232. hold += input[_in++] << bits;
  233. bits += 8;
  234. }
  235. here = dcode[hold & dmask];
  236. dodist:
  237. for (;;) { // goto emulation
  238. op = here >>> 24/*here.bits*/;
  239. hold >>>= op;
  240. bits -= op;
  241. op = (here >>> 16) & 0xff/*here.op*/;
  242. if (op & 16) { /* distance base */
  243. dist = here & 0xffff/*here.val*/;
  244. op &= 15; /* number of extra bits */
  245. if (bits < op) {
  246. hold += input[_in++] << bits;
  247. bits += 8;
  248. if (bits < op) {
  249. hold += input[_in++] << bits;
  250. bits += 8;
  251. }
  252. }
  253. dist += hold & ((1 << op) - 1);
  254. //#ifdef INFLATE_STRICT
  255. if (dist > dmax) {
  256. strm.msg = 'invalid distance too far back';
  257. state.mode = BAD$1;
  258. break top;
  259. }
  260. //#endif
  261. hold >>>= op;
  262. bits -= op;
  263. //Tracevv((stderr, "inflate: distance %u\n", dist));
  264. op = _out - beg; /* max distance in output */
  265. if (dist > op) { /* see if copy from window */
  266. op = dist - op; /* distance back in window */
  267. if (op > whave) {
  268. if (state.sane) {
  269. strm.msg = 'invalid distance too far back';
  270. state.mode = BAD$1;
  271. break top;
  272. }
  273. // (!) This block is disabled in zlib defaults,
  274. // don't enable it for binary compatibility
  275. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  276. // if (len <= op - whave) {
  277. // do {
  278. // output[_out++] = 0;
  279. // } while (--len);
  280. // continue top;
  281. // }
  282. // len -= op - whave;
  283. // do {
  284. // output[_out++] = 0;
  285. // } while (--op > whave);
  286. // if (op === 0) {
  287. // from = _out - dist;
  288. // do {
  289. // output[_out++] = output[from++];
  290. // } while (--len);
  291. // continue top;
  292. // }
  293. //#endif
  294. }
  295. from = 0; // window index
  296. from_source = s_window;
  297. if (wnext === 0) { /* very common case */
  298. from += wsize - op;
  299. if (op < len) { /* some from window */
  300. len -= op;
  301. do {
  302. output[_out++] = s_window[from++];
  303. } while (--op);
  304. from = _out - dist; /* rest from output */
  305. from_source = output;
  306. }
  307. }
  308. else if (wnext < op) { /* wrap around window */
  309. from += wsize + wnext - op;
  310. op -= wnext;
  311. if (op < len) { /* some from end of window */
  312. len -= op;
  313. do {
  314. output[_out++] = s_window[from++];
  315. } while (--op);
  316. from = 0;
  317. if (wnext < len) { /* some from start of window */
  318. op = wnext;
  319. len -= op;
  320. do {
  321. output[_out++] = s_window[from++];
  322. } while (--op);
  323. from = _out - dist; /* rest from output */
  324. from_source = output;
  325. }
  326. }
  327. }
  328. else { /* contiguous in window */
  329. from += wnext - op;
  330. if (op < len) { /* some from window */
  331. len -= op;
  332. do {
  333. output[_out++] = s_window[from++];
  334. } while (--op);
  335. from = _out - dist; /* rest from output */
  336. from_source = output;
  337. }
  338. }
  339. while (len > 2) {
  340. output[_out++] = from_source[from++];
  341. output[_out++] = from_source[from++];
  342. output[_out++] = from_source[from++];
  343. len -= 3;
  344. }
  345. if (len) {
  346. output[_out++] = from_source[from++];
  347. if (len > 1) {
  348. output[_out++] = from_source[from++];
  349. }
  350. }
  351. }
  352. else {
  353. from = _out - dist; /* copy direct from output */
  354. do { /* minimum length is three */
  355. output[_out++] = output[from++];
  356. output[_out++] = output[from++];
  357. output[_out++] = output[from++];
  358. len -= 3;
  359. } while (len > 2);
  360. if (len) {
  361. output[_out++] = output[from++];
  362. if (len > 1) {
  363. output[_out++] = output[from++];
  364. }
  365. }
  366. }
  367. }
  368. else if ((op & 64) === 0) { /* 2nd level distance code */
  369. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  370. continue dodist;
  371. }
  372. else {
  373. strm.msg = 'invalid distance code';
  374. state.mode = BAD$1;
  375. break top;
  376. }
  377. break; // need to emulate goto via "continue"
  378. }
  379. }
  380. else if ((op & 64) === 0) { /* 2nd level length code */
  381. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  382. continue dolen;
  383. }
  384. else if (op & 32) { /* end-of-block */
  385. //Tracevv((stderr, "inflate: end of block\n"));
  386. state.mode = TYPE$1;
  387. break top;
  388. }
  389. else {
  390. strm.msg = 'invalid literal/length code';
  391. state.mode = BAD$1;
  392. break top;
  393. }
  394. break; // need to emulate goto via "continue"
  395. }
  396. } while (_in < last && _out < end);
  397. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  398. len = bits >> 3;
  399. _in -= len;
  400. bits -= len << 3;
  401. hold &= (1 << bits) - 1;
  402. /* update state and return */
  403. strm.next_in = _in;
  404. strm.next_out = _out;
  405. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  406. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  407. state.hold = hold;
  408. state.bits = bits;
  409. return;
  410. };
  411. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  412. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  413. //
  414. // This software is provided 'as-is', without any express or implied
  415. // warranty. In no event will the authors be held liable for any damages
  416. // arising from the use of this software.
  417. //
  418. // Permission is granted to anyone to use this software for any purpose,
  419. // including commercial applications, and to alter it and redistribute it
  420. // freely, subject to the following restrictions:
  421. //
  422. // 1. The origin of this software must not be misrepresented; you must not
  423. // claim that you wrote the original software. If you use this software
  424. // in a product, an acknowledgment in the product documentation would be
  425. // appreciated but is not required.
  426. // 2. Altered source versions must be plainly marked as such, and must not be
  427. // misrepresented as being the original software.
  428. // 3. This notice may not be removed or altered from any source distribution.
  429. const MAXBITS = 15;
  430. const ENOUGH_LENS$1 = 852;
  431. const ENOUGH_DISTS$1 = 592;
  432. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  433. const CODES$1 = 0;
  434. const LENS$1 = 1;
  435. const DISTS$1 = 2;
  436. const lbase = new Uint16Array([ /* Length codes 257..285 base */
  437. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  438. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  439. ]);
  440. const lext = new Uint8Array([ /* Length codes 257..285 extra */
  441. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  442. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  443. ]);
  444. const dbase = new Uint16Array([ /* Distance codes 0..29 base */
  445. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  446. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  447. 8193, 12289, 16385, 24577, 0, 0
  448. ]);
  449. const dext = new Uint8Array([ /* Distance codes 0..29 extra */
  450. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  451. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  452. 28, 28, 29, 29, 64, 64
  453. ]);
  454. const inflate_table = (type, lens, lens_index, codes, table, table_index, work, opts) =>
  455. {
  456. const bits = opts.bits;
  457. //here = opts.here; /* table entry for duplication */
  458. let len = 0; /* a code's length in bits */
  459. let sym = 0; /* index of code symbols */
  460. let min = 0, max = 0; /* minimum and maximum code lengths */
  461. let root = 0; /* number of index bits for root table */
  462. let curr = 0; /* number of index bits for current table */
  463. let drop = 0; /* code bits to drop for sub-table */
  464. let left = 0; /* number of prefix codes available */
  465. let used = 0; /* code entries in table used */
  466. let huff = 0; /* Huffman code */
  467. let incr; /* for incrementing code, index */
  468. let fill; /* index for replicating entries */
  469. let low; /* low bits for current root entry */
  470. let mask; /* mask for low root bits */
  471. let next; /* next available space in table */
  472. let base = null; /* base value table to use */
  473. let base_index = 0;
  474. // let shoextra; /* extra bits table to use */
  475. let end; /* use base and extra for symbol > end */
  476. const count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  477. const offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  478. let extra = null;
  479. let extra_index = 0;
  480. let here_bits, here_op, here_val;
  481. /*
  482. Process a set of code lengths to create a canonical Huffman code. The
  483. code lengths are lens[0..codes-1]. Each length corresponds to the
  484. symbols 0..codes-1. The Huffman code is generated by first sorting the
  485. symbols by length from short to long, and retaining the symbol order
  486. for codes with equal lengths. Then the code starts with all zero bits
  487. for the first code of the shortest length, and the codes are integer
  488. increments for the same length, and zeros are appended as the length
  489. increases. For the deflate format, these bits are stored backwards
  490. from their more natural integer increment ordering, and so when the
  491. decoding tables are built in the large loop below, the integer codes
  492. are incremented backwards.
  493. This routine assumes, but does not check, that all of the entries in
  494. lens[] are in the range 0..MAXBITS. The caller must assure this.
  495. 1..MAXBITS is interpreted as that code length. zero means that that
  496. symbol does not occur in this code.
  497. The codes are sorted by computing a count of codes for each length,
  498. creating from that a table of starting indices for each length in the
  499. sorted table, and then entering the symbols in order in the sorted
  500. table. The sorted table is work[], with that space being provided by
  501. the caller.
  502. The length counts are used for other purposes as well, i.e. finding
  503. the minimum and maximum length codes, determining if there are any
  504. codes at all, checking for a valid set of lengths, and looking ahead
  505. at length counts to determine sub-table sizes when building the
  506. decoding tables.
  507. */
  508. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  509. for (len = 0; len <= MAXBITS; len++) {
  510. count[len] = 0;
  511. }
  512. for (sym = 0; sym < codes; sym++) {
  513. count[lens[lens_index + sym]]++;
  514. }
  515. /* bound code lengths, force root to be within code lengths */
  516. root = bits;
  517. for (max = MAXBITS; max >= 1; max--) {
  518. if (count[max] !== 0) { break; }
  519. }
  520. if (root > max) {
  521. root = max;
  522. }
  523. if (max === 0) { /* no symbols to code at all */
  524. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  525. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  526. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  527. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  528. //table.op[opts.table_index] = 64;
  529. //table.bits[opts.table_index] = 1;
  530. //table.val[opts.table_index++] = 0;
  531. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  532. opts.bits = 1;
  533. return 0; /* no symbols, but wait for decoding to report error */
  534. }
  535. for (min = 1; min < max; min++) {
  536. if (count[min] !== 0) { break; }
  537. }
  538. if (root < min) {
  539. root = min;
  540. }
  541. /* check for an over-subscribed or incomplete set of lengths */
  542. left = 1;
  543. for (len = 1; len <= MAXBITS; len++) {
  544. left <<= 1;
  545. left -= count[len];
  546. if (left < 0) {
  547. return -1;
  548. } /* over-subscribed */
  549. }
  550. if (left > 0 && (type === CODES$1 || max !== 1)) {
  551. return -1; /* incomplete set */
  552. }
  553. /* generate offsets into symbol table for each length for sorting */
  554. offs[1] = 0;
  555. for (len = 1; len < MAXBITS; len++) {
  556. offs[len + 1] = offs[len] + count[len];
  557. }
  558. /* sort symbols by length, by symbol order within each length */
  559. for (sym = 0; sym < codes; sym++) {
  560. if (lens[lens_index + sym] !== 0) {
  561. work[offs[lens[lens_index + sym]]++] = sym;
  562. }
  563. }
  564. /*
  565. Create and fill in decoding tables. In this loop, the table being
  566. filled is at next and has curr index bits. The code being used is huff
  567. with length len. That code is converted to an index by dropping drop
  568. bits off of the bottom. For codes where len is less than drop + curr,
  569. those top drop + curr - len bits are incremented through all values to
  570. fill the table with replicated entries.
  571. root is the number of index bits for the root table. When len exceeds
  572. root, sub-tables are created pointed to by the root entry with an index
  573. of the low root bits of huff. This is saved in low to check for when a
  574. new sub-table should be started. drop is zero when the root table is
  575. being filled, and drop is root when sub-tables are being filled.
  576. When a new sub-table is needed, it is necessary to look ahead in the
  577. code lengths to determine what size sub-table is needed. The length
  578. counts are used for this, and so count[] is decremented as codes are
  579. entered in the tables.
  580. used keeps track of how many table entries have been allocated from the
  581. provided *table space. It is checked for LENS and DIST tables against
  582. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  583. the initial root table size constants. See the comments in inftrees.h
  584. for more information.
  585. sym increments through all symbols, and the loop terminates when
  586. all codes of length max, i.e. all codes, have been processed. This
  587. routine permits incomplete codes, so another loop after this one fills
  588. in the rest of the decoding tables with invalid code markers.
  589. */
  590. /* set up for code type */
  591. // poor man optimization - use if-else instead of switch,
  592. // to avoid deopts in old v8
  593. if (type === CODES$1) {
  594. base = extra = work; /* dummy value--not used */
  595. end = 19;
  596. } else if (type === LENS$1) {
  597. base = lbase;
  598. base_index -= 257;
  599. extra = lext;
  600. extra_index -= 257;
  601. end = 256;
  602. } else { /* DISTS */
  603. base = dbase;
  604. extra = dext;
  605. end = -1;
  606. }
  607. /* initialize opts for loop */
  608. huff = 0; /* starting code */
  609. sym = 0; /* starting code symbol */
  610. len = min; /* starting code length */
  611. next = table_index; /* current table to fill in */
  612. curr = root; /* current table index bits */
  613. drop = 0; /* current bits to drop from code for index */
  614. low = -1; /* trigger new sub-table when len > root */
  615. used = 1 << root; /* use root table entries */
  616. mask = used - 1; /* mask for comparing low */
  617. /* check available table space */
  618. if ((type === LENS$1 && used > ENOUGH_LENS$1) ||
  619. (type === DISTS$1 && used > ENOUGH_DISTS$1)) {
  620. return 1;
  621. }
  622. /* process all codes and make table entries */
  623. for (;;) {
  624. /* create table entry */
  625. here_bits = len - drop;
  626. if (work[sym] < end) {
  627. here_op = 0;
  628. here_val = work[sym];
  629. }
  630. else if (work[sym] > end) {
  631. here_op = extra[extra_index + work[sym]];
  632. here_val = base[base_index + work[sym]];
  633. }
  634. else {
  635. here_op = 32 + 64; /* end of block */
  636. here_val = 0;
  637. }
  638. /* replicate for those indices with low len bits equal to huff */
  639. incr = 1 << (len - drop);
  640. fill = 1 << curr;
  641. min = fill; /* save offset to next table */
  642. do {
  643. fill -= incr;
  644. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  645. } while (fill !== 0);
  646. /* backwards increment the len-bit code huff */
  647. incr = 1 << (len - 1);
  648. while (huff & incr) {
  649. incr >>= 1;
  650. }
  651. if (incr !== 0) {
  652. huff &= incr - 1;
  653. huff += incr;
  654. } else {
  655. huff = 0;
  656. }
  657. /* go to next symbol, update count, len */
  658. sym++;
  659. if (--count[len] === 0) {
  660. if (len === max) { break; }
  661. len = lens[lens_index + work[sym]];
  662. }
  663. /* create new sub-table if needed */
  664. if (len > root && (huff & mask) !== low) {
  665. /* if first time, transition to sub-tables */
  666. if (drop === 0) {
  667. drop = root;
  668. }
  669. /* increment past last table */
  670. next += min; /* here min is 1 << curr */
  671. /* determine length of next table */
  672. curr = len - drop;
  673. left = 1 << curr;
  674. while (curr + drop < max) {
  675. left -= count[curr + drop];
  676. if (left <= 0) { break; }
  677. curr++;
  678. left <<= 1;
  679. }
  680. /* check for enough space */
  681. used += 1 << curr;
  682. if ((type === LENS$1 && used > ENOUGH_LENS$1) ||
  683. (type === DISTS$1 && used > ENOUGH_DISTS$1)) {
  684. return 1;
  685. }
  686. /* point entry in root table to sub-table */
  687. low = huff & mask;
  688. /*table.op[low] = curr;
  689. table.bits[low] = root;
  690. table.val[low] = next - opts.table_index;*/
  691. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  692. }
  693. }
  694. /* fill in remaining table entry if code is incomplete (guaranteed to have
  695. at most one remaining entry, since if the code is incomplete, the
  696. maximum code length that was allowed to get this far is one bit) */
  697. if (huff !== 0) {
  698. //table.op[next + huff] = 64; /* invalid code marker */
  699. //table.bits[next + huff] = len - drop;
  700. //table.val[next + huff] = 0;
  701. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  702. }
  703. /* set return parameters */
  704. //opts.table_index += used;
  705. opts.bits = root;
  706. return 0;
  707. };
  708. var inftrees = inflate_table;
  709. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  710. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  711. //
  712. // This software is provided 'as-is', without any express or implied
  713. // warranty. In no event will the authors be held liable for any damages
  714. // arising from the use of this software.
  715. //
  716. // Permission is granted to anyone to use this software for any purpose,
  717. // including commercial applications, and to alter it and redistribute it
  718. // freely, subject to the following restrictions:
  719. //
  720. // 1. The origin of this software must not be misrepresented; you must not
  721. // claim that you wrote the original software. If you use this software
  722. // in a product, an acknowledgment in the product documentation would be
  723. // appreciated but is not required.
  724. // 2. Altered source versions must be plainly marked as such, and must not be
  725. // misrepresented as being the original software.
  726. // 3. This notice may not be removed or altered from any source distribution.
  727. var constants$1 = {
  728. /* Allowed flush values; see deflate() and inflate() below for details */
  729. Z_NO_FLUSH: 0,
  730. Z_PARTIAL_FLUSH: 1,
  731. Z_SYNC_FLUSH: 2,
  732. Z_FULL_FLUSH: 3,
  733. Z_FINISH: 4,
  734. Z_BLOCK: 5,
  735. Z_TREES: 6,
  736. /* Return codes for the compression/decompression functions. Negative values
  737. * are errors, positive values are used for special but normal events.
  738. */
  739. Z_OK: 0,
  740. Z_STREAM_END: 1,
  741. Z_NEED_DICT: 2,
  742. Z_ERRNO: -1,
  743. Z_STREAM_ERROR: -2,
  744. Z_DATA_ERROR: -3,
  745. Z_MEM_ERROR: -4,
  746. Z_BUF_ERROR: -5,
  747. //Z_VERSION_ERROR: -6,
  748. /* compression levels */
  749. Z_NO_COMPRESSION: 0,
  750. Z_BEST_SPEED: 1,
  751. Z_BEST_COMPRESSION: 9,
  752. Z_DEFAULT_COMPRESSION: -1,
  753. Z_FILTERED: 1,
  754. Z_HUFFMAN_ONLY: 2,
  755. Z_RLE: 3,
  756. Z_FIXED: 4,
  757. Z_DEFAULT_STRATEGY: 0,
  758. /* Possible values of the data_type field (though see inflate()) */
  759. Z_BINARY: 0,
  760. Z_TEXT: 1,
  761. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  762. Z_UNKNOWN: 2,
  763. /* The deflate compression method */
  764. Z_DEFLATED: 8
  765. //Z_NULL: null // Use -1 or null inline, depending on var type
  766. };
  767. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  768. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  769. //
  770. // This software is provided 'as-is', without any express or implied
  771. // warranty. In no event will the authors be held liable for any damages
  772. // arising from the use of this software.
  773. //
  774. // Permission is granted to anyone to use this software for any purpose,
  775. // including commercial applications, and to alter it and redistribute it
  776. // freely, subject to the following restrictions:
  777. //
  778. // 1. The origin of this software must not be misrepresented; you must not
  779. // claim that you wrote the original software. If you use this software
  780. // in a product, an acknowledgment in the product documentation would be
  781. // appreciated but is not required.
  782. // 2. Altered source versions must be plainly marked as such, and must not be
  783. // misrepresented as being the original software.
  784. // 3. This notice may not be removed or altered from any source distribution.
  785. const CODES = 0;
  786. const LENS = 1;
  787. const DISTS = 2;
  788. /* Public constants ==========================================================*/
  789. /* ===========================================================================*/
  790. const {
  791. Z_FINISH: Z_FINISH$1, Z_BLOCK, Z_TREES,
  792. 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,
  793. Z_DEFLATED
  794. } = constants$1;
  795. /* STATES ====================================================================*/
  796. /* ===========================================================================*/
  797. const HEAD = 1; /* i: waiting for magic header */
  798. const FLAGS = 2; /* i: waiting for method and flags (gzip) */
  799. const TIME = 3; /* i: waiting for modification time (gzip) */
  800. const OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  801. const EXLEN = 5; /* i: waiting for extra length (gzip) */
  802. const EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  803. const NAME = 7; /* i: waiting for end of file name (gzip) */
  804. const COMMENT = 8; /* i: waiting for end of comment (gzip) */
  805. const HCRC = 9; /* i: waiting for header crc (gzip) */
  806. const DICTID = 10; /* i: waiting for dictionary check value */
  807. const DICT = 11; /* waiting for inflateSetDictionary() call */
  808. const TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  809. const TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  810. const STORED = 14; /* i: waiting for stored size (length and complement) */
  811. const COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  812. const COPY = 16; /* i/o: waiting for input or output to copy stored block */
  813. const TABLE = 17; /* i: waiting for dynamic block table lengths */
  814. const LENLENS = 18; /* i: waiting for code length code lengths */
  815. const CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  816. const LEN_ = 20; /* i: same as LEN below, but only first time in */
  817. const LEN = 21; /* i: waiting for length/lit/eob code */
  818. const LENEXT = 22; /* i: waiting for length extra bits */
  819. const DIST = 23; /* i: waiting for distance code */
  820. const DISTEXT = 24; /* i: waiting for distance extra bits */
  821. const MATCH = 25; /* o: waiting for output space to copy string */
  822. const LIT = 26; /* o: waiting for output space to write literal */
  823. const CHECK = 27; /* i: waiting for 32-bit check value */
  824. const LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  825. const DONE = 29; /* finished check, done -- remain here until reset */
  826. const BAD = 30; /* got a data error -- remain here until reset */
  827. const MEM = 31; /* got an inflate() memory error -- remain here until reset */
  828. const SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  829. /* ===========================================================================*/
  830. const ENOUGH_LENS = 852;
  831. const ENOUGH_DISTS = 592;
  832. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  833. const MAX_WBITS = 15;
  834. /* 32K LZ77 window */
  835. const DEF_WBITS = MAX_WBITS;
  836. const zswap32 = (q) => {
  837. return (((q >>> 24) & 0xff) +
  838. ((q >>> 8) & 0xff00) +
  839. ((q & 0xff00) << 8) +
  840. ((q & 0xff) << 24));
  841. };
  842. function InflateState() {
  843. this.mode = 0; /* current inflate mode */
  844. this.last = false; /* true if processing last block */
  845. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  846. this.havedict = false; /* true if dictionary provided */
  847. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  848. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  849. this.check = 0; /* protected copy of check value */
  850. this.total = 0; /* protected copy of output count */
  851. // TODO: may be {}
  852. this.head = null; /* where to save gzip header information */
  853. /* sliding window */
  854. this.wbits = 0; /* log base 2 of requested window size */
  855. this.wsize = 0; /* window size or zero if not using window */
  856. this.whave = 0; /* valid bytes in the window */
  857. this.wnext = 0; /* window write index */
  858. this.window = null; /* allocated sliding window, if needed */
  859. /* bit accumulator */
  860. this.hold = 0; /* input bit accumulator */
  861. this.bits = 0; /* number of bits in "in" */
  862. /* for string and stored block copying */
  863. this.length = 0; /* literal or length of data to copy */
  864. this.offset = 0; /* distance back to copy string from */
  865. /* for table and code decoding */
  866. this.extra = 0; /* extra bits needed */
  867. /* fixed and dynamic code tables */
  868. this.lencode = null; /* starting table for length/literal codes */
  869. this.distcode = null; /* starting table for distance codes */
  870. this.lenbits = 0; /* index bits for lencode */
  871. this.distbits = 0; /* index bits for distcode */
  872. /* dynamic table building */
  873. this.ncode = 0; /* number of code length code lengths */
  874. this.nlen = 0; /* number of length code lengths */
  875. this.ndist = 0; /* number of distance code lengths */
  876. this.have = 0; /* number of code lengths in lens[] */
  877. this.next = null; /* next available space in codes[] */
  878. this.lens = new Uint16Array(320); /* temporary storage for code lengths */
  879. this.work = new Uint16Array(288); /* work area for code table building */
  880. /*
  881. because we don't have pointers in js, we use lencode and distcode directly
  882. as buffers so we don't need codes
  883. */
  884. //this.codes = new Int32Array(ENOUGH); /* space for code tables */
  885. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  886. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  887. this.sane = 0; /* if false, allow invalid distance too far */
  888. this.back = 0; /* bits back of last unprocessed length/lit */
  889. this.was = 0; /* initial length of match */
  890. }
  891. const inflateResetKeep = (strm) => {
  892. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  893. const state = strm.state;
  894. strm.total_in = strm.total_out = state.total = 0;
  895. strm.msg = ''; /*Z_NULL*/
  896. if (state.wrap) { /* to support ill-conceived Java test suite */
  897. strm.adler = state.wrap & 1;
  898. }
  899. state.mode = HEAD;
  900. state.last = 0;
  901. state.havedict = 0;
  902. state.dmax = 32768;
  903. state.head = null/*Z_NULL*/;
  904. state.hold = 0;
  905. state.bits = 0;
  906. //state.lencode = state.distcode = state.next = state.codes;
  907. state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
  908. state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
  909. state.sane = 1;
  910. state.back = -1;
  911. //Tracev((stderr, "inflate: reset\n"));
  912. return Z_OK$1;
  913. };
  914. const inflateReset = (strm) => {
  915. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  916. const state = strm.state;
  917. state.wsize = 0;
  918. state.whave = 0;
  919. state.wnext = 0;
  920. return inflateResetKeep(strm);
  921. };
  922. const inflateReset2 = (strm, windowBits) => {
  923. let wrap;
  924. /* get the state */
  925. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  926. const state = strm.state;
  927. /* extract wrap request from windowBits parameter */
  928. if (windowBits < 0) {
  929. wrap = 0;
  930. windowBits = -windowBits;
  931. }
  932. else {
  933. wrap = (windowBits >> 4) + 1;
  934. if (windowBits < 48) {
  935. windowBits &= 15;
  936. }
  937. }
  938. /* set number of window bits, free window if different */
  939. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  940. return Z_STREAM_ERROR$1;
  941. }
  942. if (state.window !== null && state.wbits !== windowBits) {
  943. state.window = null;
  944. }
  945. /* update state and reset the rest of it */
  946. state.wrap = wrap;
  947. state.wbits = windowBits;
  948. return inflateReset(strm);
  949. };
  950. const inflateInit2 = (strm, windowBits) => {
  951. if (!strm) { return Z_STREAM_ERROR$1; }
  952. //strm.msg = Z_NULL; /* in case we return an error */
  953. const state = new InflateState();
  954. //if (state === Z_NULL) return Z_MEM_ERROR;
  955. //Tracev((stderr, "inflate: allocated\n"));
  956. strm.state = state;
  957. state.window = null/*Z_NULL*/;
  958. const ret = inflateReset2(strm, windowBits);
  959. if (ret !== Z_OK$1) {
  960. strm.state = null/*Z_NULL*/;
  961. }
  962. return ret;
  963. };
  964. const inflateInit = (strm) => {
  965. return inflateInit2(strm, DEF_WBITS);
  966. };
  967. /*
  968. Return state with length and distance decoding tables and index sizes set to
  969. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  970. If BUILDFIXED is defined, then instead this routine builds the tables the
  971. first time it's called, and returns those tables the first time and
  972. thereafter. This reduces the size of the code by about 2K bytes, in
  973. exchange for a little execution time. However, BUILDFIXED should not be
  974. used for threaded applications, since the rewriting of the tables and virgin
  975. may not be thread-safe.
  976. */
  977. let virgin = true;
  978. let lenfix, distfix; // We have no pointers in JS, so keep tables separate
  979. const fixedtables = (state) => {
  980. /* build fixed huffman tables if first call (may not be thread safe) */
  981. if (virgin) {
  982. lenfix = new Int32Array(512);
  983. distfix = new Int32Array(32);
  984. /* literal/length table */
  985. let sym = 0;
  986. while (sym < 144) { state.lens[sym++] = 8; }
  987. while (sym < 256) { state.lens[sym++] = 9; }
  988. while (sym < 280) { state.lens[sym++] = 7; }
  989. while (sym < 288) { state.lens[sym++] = 8; }
  990. inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
  991. /* distance table */
  992. sym = 0;
  993. while (sym < 32) { state.lens[sym++] = 5; }
  994. inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
  995. /* do this just once */
  996. virgin = false;
  997. }
  998. state.lencode = lenfix;
  999. state.lenbits = 9;
  1000. state.distcode = distfix;
  1001. state.distbits = 5;
  1002. };
  1003. /*
  1004. Update the window with the last wsize (normally 32K) bytes written before
  1005. returning. If window does not exist yet, create it. This is only called
  1006. when a window is already in use, or when output has been written during this
  1007. inflate call, but the end of the deflate stream has not been reached yet.
  1008. It is also called to create a window for dictionary data when a dictionary
  1009. is loaded.
  1010. Providing output buffers larger than 32K to inflate() should provide a speed
  1011. advantage, since only the last 32K of output is copied to the sliding window
  1012. upon return from inflate(), and since all distances after the first 32K of
  1013. output will fall in the output data, making match copies simpler and faster.
  1014. The advantage may be dependent on the size of the processor's data caches.
  1015. */
  1016. const updatewindow = (strm, src, end, copy) => {
  1017. let dist;
  1018. const state = strm.state;
  1019. /* if it hasn't been done already, allocate space for the window */
  1020. if (state.window === null) {
  1021. state.wsize = 1 << state.wbits;
  1022. state.wnext = 0;
  1023. state.whave = 0;
  1024. state.window = new Uint8Array(state.wsize);
  1025. }
  1026. /* copy state->wsize or less output bytes into the circular window */
  1027. if (copy >= state.wsize) {
  1028. state.window.set(src.subarray(end - state.wsize, end), 0);
  1029. state.wnext = 0;
  1030. state.whave = state.wsize;
  1031. }
  1032. else {
  1033. dist = state.wsize - state.wnext;
  1034. if (dist > copy) {
  1035. dist = copy;
  1036. }
  1037. //zmemcpy(state->window + state->wnext, end - copy, dist);
  1038. state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext);
  1039. copy -= dist;
  1040. if (copy) {
  1041. //zmemcpy(state->window, end - copy, copy);
  1042. state.window.set(src.subarray(end - copy, end), 0);
  1043. state.wnext = copy;
  1044. state.whave = state.wsize;
  1045. }
  1046. else {
  1047. state.wnext += dist;
  1048. if (state.wnext === state.wsize) { state.wnext = 0; }
  1049. if (state.whave < state.wsize) { state.whave += dist; }
  1050. }
  1051. }
  1052. return 0;
  1053. };
  1054. const inflate$1 = (strm, flush) => {
  1055. let state;
  1056. let input, output; // input/output buffers
  1057. let next; /* next input INDEX */
  1058. let put; /* next output INDEX */
  1059. let have, left; /* available input and output */
  1060. let hold; /* bit buffer */
  1061. let bits; /* bits in bit buffer */
  1062. let _in, _out; /* save starting available input and output */
  1063. let copy; /* number of stored or match bytes to copy */
  1064. let from; /* where to copy match bytes from */
  1065. let from_source;
  1066. let here = 0; /* current decoding table entry */
  1067. let here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  1068. //let last; /* parent table entry */
  1069. let last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  1070. let len; /* length to copy for repeats, bits to drop */
  1071. let ret; /* return code */
  1072. const hbuf = new Uint8Array(4); /* buffer for gzip header crc calculation */
  1073. let opts;
  1074. let n; // temporary variable for NEED_BITS
  1075. const order = /* permutation of code lengths */
  1076. new Uint8Array([ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]);
  1077. if (!strm || !strm.state || !strm.output ||
  1078. (!strm.input && strm.avail_in !== 0)) {
  1079. return Z_STREAM_ERROR$1;
  1080. }
  1081. state = strm.state;
  1082. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  1083. //--- LOAD() ---
  1084. put = strm.next_out;
  1085. output = strm.output;
  1086. left = strm.avail_out;
  1087. next = strm.next_in;
  1088. input = strm.input;
  1089. have = strm.avail_in;
  1090. hold = state.hold;
  1091. bits = state.bits;
  1092. //---
  1093. _in = have;
  1094. _out = left;
  1095. ret = Z_OK$1;
  1096. inf_leave: // goto emulation
  1097. for (;;) {
  1098. switch (state.mode) {
  1099. case HEAD:
  1100. if (state.wrap === 0) {
  1101. state.mode = TYPEDO;
  1102. break;
  1103. }
  1104. //=== NEEDBITS(16);
  1105. while (bits < 16) {
  1106. if (have === 0) { break inf_leave; }
  1107. have--;
  1108. hold += input[next++] << bits;
  1109. bits += 8;
  1110. }
  1111. //===//
  1112. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  1113. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  1114. //=== CRC2(state.check, hold);
  1115. hbuf[0] = hold & 0xff;
  1116. hbuf[1] = (hold >>> 8) & 0xff;
  1117. state.check = crc32_1(state.check, hbuf, 2, 0);
  1118. //===//
  1119. //=== INITBITS();
  1120. hold = 0;
  1121. bits = 0;
  1122. //===//
  1123. state.mode = FLAGS;
  1124. break;
  1125. }
  1126. state.flags = 0; /* expect zlib header */
  1127. if (state.head) {
  1128. state.head.done = false;
  1129. }
  1130. if (!(state.wrap & 1) || /* check if zlib header allowed */
  1131. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  1132. strm.msg = 'incorrect header check';
  1133. state.mode = BAD;
  1134. break;
  1135. }
  1136. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  1137. strm.msg = 'unknown compression method';
  1138. state.mode = BAD;
  1139. break;
  1140. }
  1141. //--- DROPBITS(4) ---//
  1142. hold >>>= 4;
  1143. bits -= 4;
  1144. //---//
  1145. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  1146. if (state.wbits === 0) {
  1147. state.wbits = len;
  1148. }
  1149. else if (len > state.wbits) {
  1150. strm.msg = 'invalid window size';
  1151. state.mode = BAD;
  1152. break;
  1153. }
  1154. // !!! pako patch. Force use `options.windowBits` if passed.
  1155. // Required to always use max window size by default.
  1156. state.dmax = 1 << state.wbits;
  1157. //state.dmax = 1 << len;
  1158. //Tracev((stderr, "inflate: zlib header ok\n"));
  1159. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  1160. state.mode = hold & 0x200 ? DICTID : TYPE;
  1161. //=== INITBITS();
  1162. hold = 0;
  1163. bits = 0;
  1164. //===//
  1165. break;
  1166. case FLAGS:
  1167. //=== NEEDBITS(16); */
  1168. while (bits < 16) {
  1169. if (have === 0) { break inf_leave; }
  1170. have--;
  1171. hold += input[next++] << bits;
  1172. bits += 8;
  1173. }
  1174. //===//
  1175. state.flags = hold;
  1176. if ((state.flags & 0xff) !== Z_DEFLATED) {
  1177. strm.msg = 'unknown compression method';
  1178. state.mode = BAD;
  1179. break;
  1180. }
  1181. if (state.flags & 0xe000) {
  1182. strm.msg = 'unknown header flags set';
  1183. state.mode = BAD;
  1184. break;
  1185. }
  1186. if (state.head) {
  1187. state.head.text = ((hold >> 8) & 1);
  1188. }
  1189. if (state.flags & 0x0200) {
  1190. //=== CRC2(state.check, hold);
  1191. hbuf[0] = hold & 0xff;
  1192. hbuf[1] = (hold >>> 8) & 0xff;
  1193. state.check = crc32_1(state.check, hbuf, 2, 0);
  1194. //===//
  1195. }
  1196. //=== INITBITS();
  1197. hold = 0;
  1198. bits = 0;
  1199. //===//
  1200. state.mode = TIME;
  1201. /* falls through */
  1202. case TIME:
  1203. //=== NEEDBITS(32); */
  1204. while (bits < 32) {
  1205. if (have === 0) { break inf_leave; }
  1206. have--;
  1207. hold += input[next++] << bits;
  1208. bits += 8;
  1209. }
  1210. //===//
  1211. if (state.head) {
  1212. state.head.time = hold;
  1213. }
  1214. if (state.flags & 0x0200) {
  1215. //=== CRC4(state.check, hold)
  1216. hbuf[0] = hold & 0xff;
  1217. hbuf[1] = (hold >>> 8) & 0xff;
  1218. hbuf[2] = (hold >>> 16) & 0xff;
  1219. hbuf[3] = (hold >>> 24) & 0xff;
  1220. state.check = crc32_1(state.check, hbuf, 4, 0);
  1221. //===
  1222. }
  1223. //=== INITBITS();
  1224. hold = 0;
  1225. bits = 0;
  1226. //===//
  1227. state.mode = OS;
  1228. /* falls through */
  1229. case OS:
  1230. //=== NEEDBITS(16); */
  1231. while (bits < 16) {
  1232. if (have === 0) { break inf_leave; }
  1233. have--;
  1234. hold += input[next++] << bits;
  1235. bits += 8;
  1236. }
  1237. //===//
  1238. if (state.head) {
  1239. state.head.xflags = (hold & 0xff);
  1240. state.head.os = (hold >> 8);
  1241. }
  1242. if (state.flags & 0x0200) {
  1243. //=== CRC2(state.check, hold);
  1244. hbuf[0] = hold & 0xff;
  1245. hbuf[1] = (hold >>> 8) & 0xff;
  1246. state.check = crc32_1(state.check, hbuf, 2, 0);
  1247. //===//
  1248. }
  1249. //=== INITBITS();
  1250. hold = 0;
  1251. bits = 0;
  1252. //===//
  1253. state.mode = EXLEN;
  1254. /* falls through */
  1255. case EXLEN:
  1256. if (state.flags & 0x0400) {
  1257. //=== NEEDBITS(16); */
  1258. while (bits < 16) {
  1259. if (have === 0) { break inf_leave; }
  1260. have--;
  1261. hold += input[next++] << bits;
  1262. bits += 8;
  1263. }
  1264. //===//
  1265. state.length = hold;
  1266. if (state.head) {
  1267. state.head.extra_len = hold;
  1268. }
  1269. if (state.flags & 0x0200) {
  1270. //=== CRC2(state.check, hold);
  1271. hbuf[0] = hold & 0xff;
  1272. hbuf[1] = (hold >>> 8) & 0xff;
  1273. state.check = crc32_1(state.check, hbuf, 2, 0);
  1274. //===//
  1275. }
  1276. //=== INITBITS();
  1277. hold = 0;
  1278. bits = 0;
  1279. //===//
  1280. }
  1281. else if (state.head) {
  1282. state.head.extra = null/*Z_NULL*/;
  1283. }
  1284. state.mode = EXTRA;
  1285. /* falls through */
  1286. case EXTRA:
  1287. if (state.flags & 0x0400) {
  1288. copy = state.length;
  1289. if (copy > have) { copy = have; }
  1290. if (copy) {
  1291. if (state.head) {
  1292. len = state.head.extra_len - state.length;
  1293. if (!state.head.extra) {
  1294. // Use untyped array for more convenient processing later
  1295. state.head.extra = new Uint8Array(state.head.extra_len);
  1296. }
  1297. state.head.extra.set(
  1298. input.subarray(
  1299. next,
  1300. // extra field is limited to 65536 bytes
  1301. // - no need for additional size check
  1302. next + copy
  1303. ),
  1304. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  1305. len
  1306. );
  1307. //zmemcpy(state.head.extra + len, next,
  1308. // len + copy > state.head.extra_max ?
  1309. // state.head.extra_max - len : copy);
  1310. }
  1311. if (state.flags & 0x0200) {
  1312. state.check = crc32_1(state.check, input, copy, next);
  1313. }
  1314. have -= copy;
  1315. next += copy;
  1316. state.length -= copy;
  1317. }
  1318. if (state.length) { break inf_leave; }
  1319. }
  1320. state.length = 0;
  1321. state.mode = NAME;
  1322. /* falls through */
  1323. case NAME:
  1324. if (state.flags & 0x0800) {
  1325. if (have === 0) { break inf_leave; }
  1326. copy = 0;
  1327. do {
  1328. // TODO: 2 or 1 bytes?
  1329. len = input[next + copy++];
  1330. /* use constant limit because in js we should not preallocate memory */
  1331. if (state.head && len &&
  1332. (state.length < 65536 /*state.head.name_max*/)) {
  1333. state.head.name += String.fromCharCode(len);
  1334. }
  1335. } while (len && copy < have);
  1336. if (state.flags & 0x0200) {
  1337. state.check = crc32_1(state.check, input, copy, next);
  1338. }
  1339. have -= copy;
  1340. next += copy;
  1341. if (len) { break inf_leave; }
  1342. }
  1343. else if (state.head) {
  1344. state.head.name = null;
  1345. }
  1346. state.length = 0;
  1347. state.mode = COMMENT;
  1348. /* falls through */
  1349. case COMMENT:
  1350. if (state.flags & 0x1000) {
  1351. if (have === 0) { break inf_leave; }
  1352. copy = 0;
  1353. do {
  1354. len = input[next + copy++];
  1355. /* use constant limit because in js we should not preallocate memory */
  1356. if (state.head && len &&
  1357. (state.length < 65536 /*state.head.comm_max*/)) {
  1358. state.head.comment += String.fromCharCode(len);
  1359. }
  1360. } while (len && copy < have);
  1361. if (state.flags & 0x0200) {
  1362. state.check = crc32_1(state.check, input, copy, next);
  1363. }
  1364. have -= copy;
  1365. next += copy;
  1366. if (len) { break inf_leave; }
  1367. }
  1368. else if (state.head) {
  1369. state.head.comment = null;
  1370. }
  1371. state.mode = HCRC;
  1372. /* falls through */
  1373. case HCRC:
  1374. if (state.flags & 0x0200) {
  1375. //=== NEEDBITS(16); */
  1376. while (bits < 16) {
  1377. if (have === 0) { break inf_leave; }
  1378. have--;
  1379. hold += input[next++] << bits;
  1380. bits += 8;
  1381. }
  1382. //===//
  1383. if (hold !== (state.check & 0xffff)) {
  1384. strm.msg = 'header crc mismatch';
  1385. state.mode = BAD;
  1386. break;
  1387. }
  1388. //=== INITBITS();
  1389. hold = 0;
  1390. bits = 0;
  1391. //===//
  1392. }
  1393. if (state.head) {
  1394. state.head.hcrc = ((state.flags >> 9) & 1);
  1395. state.head.done = true;
  1396. }
  1397. strm.adler = state.check = 0;
  1398. state.mode = TYPE;
  1399. break;
  1400. case DICTID:
  1401. //=== NEEDBITS(32); */
  1402. while (bits < 32) {
  1403. if (have === 0) { break inf_leave; }
  1404. have--;
  1405. hold += input[next++] << bits;
  1406. bits += 8;
  1407. }
  1408. //===//
  1409. strm.adler = state.check = zswap32(hold);
  1410. //=== INITBITS();
  1411. hold = 0;
  1412. bits = 0;
  1413. //===//
  1414. state.mode = DICT;
  1415. /* falls through */
  1416. case DICT:
  1417. if (state.havedict === 0) {
  1418. //--- RESTORE() ---
  1419. strm.next_out = put;
  1420. strm.avail_out = left;
  1421. strm.next_in = next;
  1422. strm.avail_in = have;
  1423. state.hold = hold;
  1424. state.bits = bits;
  1425. //---
  1426. return Z_NEED_DICT$1;
  1427. }
  1428. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  1429. state.mode = TYPE;
  1430. /* falls through */
  1431. case TYPE:
  1432. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  1433. /* falls through */
  1434. case TYPEDO:
  1435. if (state.last) {
  1436. //--- BYTEBITS() ---//
  1437. hold >>>= bits & 7;
  1438. bits -= bits & 7;
  1439. //---//
  1440. state.mode = CHECK;
  1441. break;
  1442. }
  1443. //=== NEEDBITS(3); */
  1444. while (bits < 3) {
  1445. if (have === 0) { break inf_leave; }
  1446. have--;
  1447. hold += input[next++] << bits;
  1448. bits += 8;
  1449. }
  1450. //===//
  1451. state.last = (hold & 0x01)/*BITS(1)*/;
  1452. //--- DROPBITS(1) ---//
  1453. hold >>>= 1;
  1454. bits -= 1;
  1455. //---//
  1456. switch ((hold & 0x03)/*BITS(2)*/) {
  1457. case 0: /* stored block */
  1458. //Tracev((stderr, "inflate: stored block%s\n",
  1459. // state.last ? " (last)" : ""));
  1460. state.mode = STORED;
  1461. break;
  1462. case 1: /* fixed block */
  1463. fixedtables(state);
  1464. //Tracev((stderr, "inflate: fixed codes block%s\n",
  1465. // state.last ? " (last)" : ""));
  1466. state.mode = LEN_; /* decode codes */
  1467. if (flush === Z_TREES) {
  1468. //--- DROPBITS(2) ---//
  1469. hold >>>= 2;
  1470. bits -= 2;
  1471. //---//
  1472. break inf_leave;
  1473. }
  1474. break;
  1475. case 2: /* dynamic block */
  1476. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  1477. // state.last ? " (last)" : ""));
  1478. state.mode = TABLE;
  1479. break;
  1480. case 3:
  1481. strm.msg = 'invalid block type';
  1482. state.mode = BAD;
  1483. }
  1484. //--- DROPBITS(2) ---//
  1485. hold >>>= 2;
  1486. bits -= 2;
  1487. //---//
  1488. break;
  1489. case STORED:
  1490. //--- BYTEBITS() ---// /* go to byte boundary */
  1491. hold >>>= bits & 7;
  1492. bits -= bits & 7;
  1493. //---//
  1494. //=== NEEDBITS(32); */
  1495. while (bits < 32) {
  1496. if (have === 0) { break inf_leave; }
  1497. have--;
  1498. hold += input[next++] << bits;
  1499. bits += 8;
  1500. }
  1501. //===//
  1502. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  1503. strm.msg = 'invalid stored block lengths';
  1504. state.mode = BAD;
  1505. break;
  1506. }
  1507. state.length = hold & 0xffff;
  1508. //Tracev((stderr, "inflate: stored length %u\n",
  1509. // state.length));
  1510. //=== INITBITS();
  1511. hold = 0;
  1512. bits = 0;
  1513. //===//
  1514. state.mode = COPY_;
  1515. if (flush === Z_TREES) { break inf_leave; }
  1516. /* falls through */
  1517. case COPY_:
  1518. state.mode = COPY;
  1519. /* falls through */
  1520. case COPY:
  1521. copy = state.length;
  1522. if (copy) {
  1523. if (copy > have) { copy = have; }
  1524. if (copy > left) { copy = left; }
  1525. if (copy === 0) { break inf_leave; }
  1526. //--- zmemcpy(put, next, copy); ---
  1527. output.set(input.subarray(next, next + copy), put);
  1528. //---//
  1529. have -= copy;
  1530. next += copy;
  1531. left -= copy;
  1532. put += copy;
  1533. state.length -= copy;
  1534. break;
  1535. }
  1536. //Tracev((stderr, "inflate: stored end\n"));
  1537. state.mode = TYPE;
  1538. break;
  1539. case TABLE:
  1540. //=== NEEDBITS(14); */
  1541. while (bits < 14) {
  1542. if (have === 0) { break inf_leave; }
  1543. have--;
  1544. hold += input[next++] << bits;
  1545. bits += 8;
  1546. }
  1547. //===//
  1548. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  1549. //--- DROPBITS(5) ---//
  1550. hold >>>= 5;
  1551. bits -= 5;
  1552. //---//
  1553. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  1554. //--- DROPBITS(5) ---//
  1555. hold >>>= 5;
  1556. bits -= 5;
  1557. //---//
  1558. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  1559. //--- DROPBITS(4) ---//
  1560. hold >>>= 4;
  1561. bits -= 4;
  1562. //---//
  1563. //#ifndef PKZIP_BUG_WORKAROUND
  1564. if (state.nlen > 286 || state.ndist > 30) {
  1565. strm.msg = 'too many length or distance symbols';
  1566. state.mode = BAD;
  1567. break;
  1568. }
  1569. //#endif
  1570. //Tracev((stderr, "inflate: table sizes ok\n"));
  1571. state.have = 0;
  1572. state.mode = LENLENS;
  1573. /* falls through */
  1574. case LENLENS:
  1575. while (state.have < state.ncode) {
  1576. //=== NEEDBITS(3);
  1577. while (bits < 3) {
  1578. if (have === 0) { break inf_leave; }
  1579. have--;
  1580. hold += input[next++] << bits;
  1581. bits += 8;
  1582. }
  1583. //===//
  1584. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  1585. //--- DROPBITS(3) ---//
  1586. hold >>>= 3;
  1587. bits -= 3;
  1588. //---//
  1589. }
  1590. while (state.have < 19) {
  1591. state.lens[order[state.have++]] = 0;
  1592. }
  1593. // We have separate tables & no pointers. 2 commented lines below not needed.
  1594. //state.next = state.codes;
  1595. //state.lencode = state.next;
  1596. // Switch to use dynamic table
  1597. state.lencode = state.lendyn;
  1598. state.lenbits = 7;
  1599. opts = { bits: state.lenbits };
  1600. ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  1601. state.lenbits = opts.bits;
  1602. if (ret) {
  1603. strm.msg = 'invalid code lengths set';
  1604. state.mode = BAD;
  1605. break;
  1606. }
  1607. //Tracev((stderr, "inflate: code lengths ok\n"));
  1608. state.have = 0;
  1609. state.mode = CODELENS;
  1610. /* falls through */
  1611. case CODELENS:
  1612. while (state.have < state.nlen + state.ndist) {
  1613. for (;;) {
  1614. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  1615. here_bits = here >>> 24;
  1616. here_op = (here >>> 16) & 0xff;
  1617. here_val = here & 0xffff;
  1618. if ((here_bits) <= bits) { break; }
  1619. //--- PULLBYTE() ---//
  1620. if (have === 0) { break inf_leave; }
  1621. have--;
  1622. hold += input[next++] << bits;
  1623. bits += 8;
  1624. //---//
  1625. }
  1626. if (here_val < 16) {
  1627. //--- DROPBITS(here.bits) ---//
  1628. hold >>>= here_bits;
  1629. bits -= here_bits;
  1630. //---//
  1631. state.lens[state.have++] = here_val;
  1632. }
  1633. else {
  1634. if (here_val === 16) {
  1635. //=== NEEDBITS(here.bits + 2);
  1636. n = here_bits + 2;
  1637. while (bits < n) {
  1638. if (have === 0) { break inf_leave; }
  1639. have--;
  1640. hold += input[next++] << bits;
  1641. bits += 8;
  1642. }
  1643. //===//
  1644. //--- DROPBITS(here.bits) ---//
  1645. hold >>>= here_bits;
  1646. bits -= here_bits;
  1647. //---//
  1648. if (state.have === 0) {
  1649. strm.msg = 'invalid bit length repeat';
  1650. state.mode = BAD;
  1651. break;
  1652. }
  1653. len = state.lens[state.have - 1];
  1654. copy = 3 + (hold & 0x03);//BITS(2);
  1655. //--- DROPBITS(2) ---//
  1656. hold >>>= 2;
  1657. bits -= 2;
  1658. //---//
  1659. }
  1660. else if (here_val === 17) {
  1661. //=== NEEDBITS(here.bits + 3);
  1662. n = here_bits + 3;
  1663. while (bits < n) {
  1664. if (have === 0) { break inf_leave; }
  1665. have--;
  1666. hold += input[next++] << bits;
  1667. bits += 8;
  1668. }
  1669. //===//
  1670. //--- DROPBITS(here.bits) ---//
  1671. hold >>>= here_bits;
  1672. bits -= here_bits;
  1673. //---//
  1674. len = 0;
  1675. copy = 3 + (hold & 0x07);//BITS(3);
  1676. //--- DROPBITS(3) ---//
  1677. hold >>>= 3;
  1678. bits -= 3;
  1679. //---//
  1680. }
  1681. else {
  1682. //=== NEEDBITS(here.bits + 7);
  1683. n = here_bits + 7;
  1684. while (bits < n) {
  1685. if (have === 0) { break inf_leave; }
  1686. have--;
  1687. hold += input[next++] << bits;
  1688. bits += 8;
  1689. }
  1690. //===//
  1691. //--- DROPBITS(here.bits) ---//
  1692. hold >>>= here_bits;
  1693. bits -= here_bits;
  1694. //---//
  1695. len = 0;
  1696. copy = 11 + (hold & 0x7f);//BITS(7);
  1697. //--- DROPBITS(7) ---//
  1698. hold >>>= 7;
  1699. bits -= 7;
  1700. //---//
  1701. }
  1702. if (state.have + copy > state.nlen + state.ndist) {
  1703. strm.msg = 'invalid bit length repeat';
  1704. state.mode = BAD;
  1705. break;
  1706. }
  1707. while (copy--) {
  1708. state.lens[state.have++] = len;
  1709. }
  1710. }
  1711. }
  1712. /* handle error breaks in while */
  1713. if (state.mode === BAD) { break; }
  1714. /* check for end-of-block code (better have one) */
  1715. if (state.lens[256] === 0) {
  1716. strm.msg = 'invalid code -- missing end-of-block';
  1717. state.mode = BAD;
  1718. break;
  1719. }
  1720. /* build code tables -- note: do not change the lenbits or distbits
  1721. values here (9 and 6) without reading the comments in inftrees.h
  1722. concerning the ENOUGH constants, which depend on those values */
  1723. state.lenbits = 9;
  1724. opts = { bits: state.lenbits };
  1725. ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  1726. // We have separate tables & no pointers. 2 commented lines below not needed.
  1727. // state.next_index = opts.table_index;
  1728. state.lenbits = opts.bits;
  1729. // state.lencode = state.next;
  1730. if (ret) {
  1731. strm.msg = 'invalid literal/lengths set';
  1732. state.mode = BAD;
  1733. break;
  1734. }
  1735. state.distbits = 6;
  1736. //state.distcode.copy(state.codes);
  1737. // Switch to use dynamic table
  1738. state.distcode = state.distdyn;
  1739. opts = { bits: state.distbits };
  1740. ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  1741. // We have separate tables & no pointers. 2 commented lines below not needed.
  1742. // state.next_index = opts.table_index;
  1743. state.distbits = opts.bits;
  1744. // state.distcode = state.next;
  1745. if (ret) {
  1746. strm.msg = 'invalid distances set';
  1747. state.mode = BAD;
  1748. break;
  1749. }
  1750. //Tracev((stderr, 'inflate: codes ok\n'));
  1751. state.mode = LEN_;
  1752. if (flush === Z_TREES) { break inf_leave; }
  1753. /* falls through */
  1754. case LEN_:
  1755. state.mode = LEN;
  1756. /* falls through */
  1757. case LEN:
  1758. if (have >= 6 && left >= 258) {
  1759. //--- RESTORE() ---
  1760. strm.next_out = put;
  1761. strm.avail_out = left;
  1762. strm.next_in = next;
  1763. strm.avail_in = have;
  1764. state.hold = hold;
  1765. state.bits = bits;
  1766. //---
  1767. inffast(strm, _out);
  1768. //--- LOAD() ---
  1769. put = strm.next_out;
  1770. output = strm.output;
  1771. left = strm.avail_out;
  1772. next = strm.next_in;
  1773. input = strm.input;
  1774. have = strm.avail_in;
  1775. hold = state.hold;
  1776. bits = state.bits;
  1777. //---
  1778. if (state.mode === TYPE) {
  1779. state.back = -1;
  1780. }
  1781. break;
  1782. }
  1783. state.back = 0;
  1784. for (;;) {
  1785. here = state.lencode[hold & ((1 << state.lenbits) - 1)]; /*BITS(state.lenbits)*/
  1786. here_bits = here >>> 24;
  1787. here_op = (here >>> 16) & 0xff;
  1788. here_val = here & 0xffff;
  1789. if (here_bits <= bits) { break; }
  1790. //--- PULLBYTE() ---//
  1791. if (have === 0) { break inf_leave; }
  1792. have--;
  1793. hold += input[next++] << bits;
  1794. bits += 8;
  1795. //---//
  1796. }
  1797. if (here_op && (here_op & 0xf0) === 0) {
  1798. last_bits = here_bits;
  1799. last_op = here_op;
  1800. last_val = here_val;
  1801. for (;;) {
  1802. here = state.lencode[last_val +
  1803. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  1804. here_bits = here >>> 24;
  1805. here_op = (here >>> 16) & 0xff;
  1806. here_val = here & 0xffff;
  1807. if ((last_bits + here_bits) <= bits) { break; }
  1808. //--- PULLBYTE() ---//
  1809. if (have === 0) { break inf_leave; }
  1810. have--;
  1811. hold += input[next++] << bits;
  1812. bits += 8;
  1813. //---//
  1814. }
  1815. //--- DROPBITS(last.bits) ---//
  1816. hold >>>= last_bits;
  1817. bits -= last_bits;
  1818. //---//
  1819. state.back += last_bits;
  1820. }
  1821. //--- DROPBITS(here.bits) ---//
  1822. hold >>>= here_bits;
  1823. bits -= here_bits;
  1824. //---//
  1825. state.back += here_bits;
  1826. state.length = here_val;
  1827. if (here_op === 0) {
  1828. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  1829. // "inflate: literal '%c'\n" :
  1830. // "inflate: literal 0x%02x\n", here.val));
  1831. state.mode = LIT;
  1832. break;
  1833. }
  1834. if (here_op & 32) {
  1835. //Tracevv((stderr, "inflate: end of block\n"));
  1836. state.back = -1;
  1837. state.mode = TYPE;
  1838. break;
  1839. }
  1840. if (here_op & 64) {
  1841. strm.msg = 'invalid literal/length code';
  1842. state.mode = BAD;
  1843. break;
  1844. }
  1845. state.extra = here_op & 15;
  1846. state.mode = LENEXT;
  1847. /* falls through */
  1848. case LENEXT:
  1849. if (state.extra) {
  1850. //=== NEEDBITS(state.extra);
  1851. n = state.extra;
  1852. while (bits < n) {
  1853. if (have === 0) { break inf_leave; }
  1854. have--;
  1855. hold += input[next++] << bits;
  1856. bits += 8;
  1857. }
  1858. //===//
  1859. state.length += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  1860. //--- DROPBITS(state.extra) ---//
  1861. hold >>>= state.extra;
  1862. bits -= state.extra;
  1863. //---//
  1864. state.back += state.extra;
  1865. }
  1866. //Tracevv((stderr, "inflate: length %u\n", state.length));
  1867. state.was = state.length;
  1868. state.mode = DIST;
  1869. /* falls through */
  1870. case DIST:
  1871. for (;;) {
  1872. here = state.distcode[hold & ((1 << state.distbits) - 1)];/*BITS(state.distbits)*/
  1873. here_bits = here >>> 24;
  1874. here_op = (here >>> 16) & 0xff;
  1875. here_val = here & 0xffff;
  1876. if ((here_bits) <= bits) { break; }
  1877. //--- PULLBYTE() ---//
  1878. if (have === 0) { break inf_leave; }
  1879. have--;
  1880. hold += input[next++] << bits;
  1881. bits += 8;
  1882. //---//
  1883. }
  1884. if ((here_op & 0xf0) === 0) {
  1885. last_bits = here_bits;
  1886. last_op = here_op;
  1887. last_val = here_val;
  1888. for (;;) {
  1889. here = state.distcode[last_val +
  1890. ((hold & ((1 << (last_bits + last_op)) - 1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  1891. here_bits = here >>> 24;
  1892. here_op = (here >>> 16) & 0xff;
  1893. here_val = here & 0xffff;
  1894. if ((last_bits + here_bits) <= bits) { break; }
  1895. //--- PULLBYTE() ---//
  1896. if (have === 0) { break inf_leave; }
  1897. have--;
  1898. hold += input[next++] << bits;
  1899. bits += 8;
  1900. //---//
  1901. }
  1902. //--- DROPBITS(last.bits) ---//
  1903. hold >>>= last_bits;
  1904. bits -= last_bits;
  1905. //---//
  1906. state.back += last_bits;
  1907. }
  1908. //--- DROPBITS(here.bits) ---//
  1909. hold >>>= here_bits;
  1910. bits -= here_bits;
  1911. //---//
  1912. state.back += here_bits;
  1913. if (here_op & 64) {
  1914. strm.msg = 'invalid distance code';
  1915. state.mode = BAD;
  1916. break;
  1917. }
  1918. state.offset = here_val;
  1919. state.extra = (here_op) & 15;
  1920. state.mode = DISTEXT;
  1921. /* falls through */
  1922. case DISTEXT:
  1923. if (state.extra) {
  1924. //=== NEEDBITS(state.extra);
  1925. n = state.extra;
  1926. while (bits < n) {
  1927. if (have === 0) { break inf_leave; }
  1928. have--;
  1929. hold += input[next++] << bits;
  1930. bits += 8;
  1931. }
  1932. //===//
  1933. state.offset += hold & ((1 << state.extra) - 1)/*BITS(state.extra)*/;
  1934. //--- DROPBITS(state.extra) ---//
  1935. hold >>>= state.extra;
  1936. bits -= state.extra;
  1937. //---//
  1938. state.back += state.extra;
  1939. }
  1940. //#ifdef INFLATE_STRICT
  1941. if (state.offset > state.dmax) {
  1942. strm.msg = 'invalid distance too far back';
  1943. state.mode = BAD;
  1944. break;
  1945. }
  1946. //#endif
  1947. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  1948. state.mode = MATCH;
  1949. /* falls through */
  1950. case MATCH:
  1951. if (left === 0) { break inf_leave; }
  1952. copy = _out - left;
  1953. if (state.offset > copy) { /* copy from window */
  1954. copy = state.offset - copy;
  1955. if (copy > state.whave) {
  1956. if (state.sane) {
  1957. strm.msg = 'invalid distance too far back';
  1958. state.mode = BAD;
  1959. break;
  1960. }
  1961. // (!) This block is disabled in zlib defaults,
  1962. // don't enable it for binary compatibility
  1963. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1964. // Trace((stderr, "inflate.c too far\n"));
  1965. // copy -= state.whave;
  1966. // if (copy > state.length) { copy = state.length; }
  1967. // if (copy > left) { copy = left; }
  1968. // left -= copy;
  1969. // state.length -= copy;
  1970. // do {
  1971. // output[put++] = 0;
  1972. // } while (--copy);
  1973. // if (state.length === 0) { state.mode = LEN; }
  1974. // break;
  1975. //#endif
  1976. }
  1977. if (copy > state.wnext) {
  1978. copy -= state.wnext;
  1979. from = state.wsize - copy;
  1980. }
  1981. else {
  1982. from = state.wnext - copy;
  1983. }
  1984. if (copy > state.length) { copy = state.length; }
  1985. from_source = state.window;
  1986. }
  1987. else { /* copy from output */
  1988. from_source = output;
  1989. from = put - state.offset;
  1990. copy = state.length;
  1991. }
  1992. if (copy > left) { copy = left; }
  1993. left -= copy;
  1994. state.length -= copy;
  1995. do {
  1996. output[put++] = from_source[from++];
  1997. } while (--copy);
  1998. if (state.length === 0) { state.mode = LEN; }
  1999. break;
  2000. case LIT:
  2001. if (left === 0) { break inf_leave; }
  2002. output[put++] = state.length;
  2003. left--;
  2004. state.mode = LEN;
  2005. break;
  2006. case CHECK:
  2007. if (state.wrap) {
  2008. //=== NEEDBITS(32);
  2009. while (bits < 32) {
  2010. if (have === 0) { break inf_leave; }
  2011. have--;
  2012. // Use '|' instead of '+' to make sure that result is signed
  2013. hold |= input[next++] << bits;
  2014. bits += 8;
  2015. }
  2016. //===//
  2017. _out -= left;
  2018. strm.total_out += _out;
  2019. state.total += _out;
  2020. if (_out) {
  2021. strm.adler = state.check =
  2022. /*UPDATE(state.check, put - _out, _out);*/
  2023. (state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out));
  2024. }
  2025. _out = left;
  2026. // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too
  2027. if ((state.flags ? hold : zswap32(hold)) !== state.check) {
  2028. strm.msg = 'incorrect data check';
  2029. state.mode = BAD;
  2030. break;
  2031. }
  2032. //=== INITBITS();
  2033. hold = 0;
  2034. bits = 0;
  2035. //===//
  2036. //Tracev((stderr, "inflate: check matches trailer\n"));
  2037. }
  2038. state.mode = LENGTH;
  2039. /* falls through */
  2040. case LENGTH:
  2041. if (state.wrap && state.flags) {
  2042. //=== NEEDBITS(32);
  2043. while (bits < 32) {
  2044. if (have === 0) { break inf_leave; }
  2045. have--;
  2046. hold += input[next++] << bits;
  2047. bits += 8;
  2048. }
  2049. //===//
  2050. if (hold !== (state.total & 0xffffffff)) {
  2051. strm.msg = 'incorrect length check';
  2052. state.mode = BAD;
  2053. break;
  2054. }
  2055. //=== INITBITS();
  2056. hold = 0;
  2057. bits = 0;
  2058. //===//
  2059. //Tracev((stderr, "inflate: length matches trailer\n"));
  2060. }
  2061. state.mode = DONE;
  2062. /* falls through */
  2063. case DONE:
  2064. ret = Z_STREAM_END$1;
  2065. break inf_leave;
  2066. case BAD:
  2067. ret = Z_DATA_ERROR$1;
  2068. break inf_leave;
  2069. case MEM:
  2070. return Z_MEM_ERROR$1;
  2071. case SYNC:
  2072. /* falls through */
  2073. default:
  2074. return Z_STREAM_ERROR$1;
  2075. }
  2076. }
  2077. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  2078. /*
  2079. Return from inflate(), updating the total counts and the check value.
  2080. If there was no progress during the inflate() call, return a buffer
  2081. error. Call updatewindow() to create and/or update the window state.
  2082. Note: a memory error from inflate() is non-recoverable.
  2083. */
  2084. //--- RESTORE() ---
  2085. strm.next_out = put;
  2086. strm.avail_out = left;
  2087. strm.next_in = next;
  2088. strm.avail_in = have;
  2089. state.hold = hold;
  2090. state.bits = bits;
  2091. //---
  2092. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  2093. (state.mode < CHECK || flush !== Z_FINISH$1))) {
  2094. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ;
  2095. }
  2096. _in -= strm.avail_in;
  2097. _out -= strm.avail_out;
  2098. strm.total_in += _in;
  2099. strm.total_out += _out;
  2100. state.total += _out;
  2101. if (state.wrap && _out) {
  2102. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  2103. (state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out));
  2104. }
  2105. strm.data_type = state.bits + (state.last ? 64 : 0) +
  2106. (state.mode === TYPE ? 128 : 0) +
  2107. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  2108. if (((_in === 0 && _out === 0) || flush === Z_FINISH$1) && ret === Z_OK$1) {
  2109. ret = Z_BUF_ERROR;
  2110. }
  2111. return ret;
  2112. };
  2113. const inflateEnd = (strm) => {
  2114. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
  2115. return Z_STREAM_ERROR$1;
  2116. }
  2117. let state = strm.state;
  2118. if (state.window) {
  2119. state.window = null;
  2120. }
  2121. strm.state = null;
  2122. return Z_OK$1;
  2123. };
  2124. const inflateGetHeader = (strm, head) => {
  2125. /* check state */
  2126. if (!strm || !strm.state) { return Z_STREAM_ERROR$1; }
  2127. const state = strm.state;
  2128. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR$1; }
  2129. /* save header structure */
  2130. state.head = head;
  2131. head.done = false;
  2132. return Z_OK$1;
  2133. };
  2134. const inflateSetDictionary = (strm, dictionary) => {
  2135. const dictLength = dictionary.length;
  2136. let state;
  2137. let dictid;
  2138. let ret;
  2139. /* check state */
  2140. if (!strm /* == Z_NULL */ || !strm.state /* == Z_NULL */) { return Z_STREAM_ERROR$1; }
  2141. state = strm.state;
  2142. if (state.wrap !== 0 && state.mode !== DICT) {
  2143. return Z_STREAM_ERROR$1;
  2144. }
  2145. /* check for correct dictionary identifier */
  2146. if (state.mode === DICT) {
  2147. dictid = 1; /* adler32(0, null, 0)*/
  2148. /* dictid = adler32(dictid, dictionary, dictLength); */
  2149. dictid = adler32_1(dictid, dictionary, dictLength, 0);
  2150. if (dictid !== state.check) {
  2151. return Z_DATA_ERROR$1;
  2152. }
  2153. }
  2154. /* copy dictionary to window using updatewindow(), which will amend the
  2155. existing dictionary if appropriate */
  2156. ret = updatewindow(strm, dictionary, dictLength, dictLength);
  2157. if (ret) {
  2158. state.mode = MEM;
  2159. return Z_MEM_ERROR$1;
  2160. }
  2161. state.havedict = 1;
  2162. // Tracev((stderr, "inflate: dictionary set\n"));
  2163. return Z_OK$1;
  2164. };
  2165. var inflateReset_1 = inflateReset;
  2166. var inflateReset2_1 = inflateReset2;
  2167. var inflateResetKeep_1 = inflateResetKeep;
  2168. var inflateInit_1 = inflateInit;
  2169. var inflateInit2_1 = inflateInit2;
  2170. var inflate_2$1 = inflate$1;
  2171. var inflateEnd_1 = inflateEnd;
  2172. var inflateGetHeader_1 = inflateGetHeader;
  2173. var inflateSetDictionary_1 = inflateSetDictionary;
  2174. var inflateInfo = 'pako inflate (from Nodeca project)';
  2175. /* Not implemented
  2176. module.exports.inflateCopy = inflateCopy;
  2177. module.exports.inflateGetDictionary = inflateGetDictionary;
  2178. module.exports.inflateMark = inflateMark;
  2179. module.exports.inflatePrime = inflatePrime;
  2180. module.exports.inflateSync = inflateSync;
  2181. module.exports.inflateSyncPoint = inflateSyncPoint;
  2182. module.exports.inflateUndermine = inflateUndermine;
  2183. */
  2184. var inflate_1$1 = {
  2185. inflateReset: inflateReset_1,
  2186. inflateReset2: inflateReset2_1,
  2187. inflateResetKeep: inflateResetKeep_1,
  2188. inflateInit: inflateInit_1,
  2189. inflateInit2: inflateInit2_1,
  2190. inflate: inflate_2$1,
  2191. inflateEnd: inflateEnd_1,
  2192. inflateGetHeader: inflateGetHeader_1,
  2193. inflateSetDictionary: inflateSetDictionary_1,
  2194. inflateInfo: inflateInfo
  2195. };
  2196. const _has = (obj, key) => {
  2197. return Object.prototype.hasOwnProperty.call(obj, key);
  2198. };
  2199. var assign = function (obj /*from1, from2, from3, ...*/) {
  2200. const sources = Array.prototype.slice.call(arguments, 1);
  2201. while (sources.length) {
  2202. const source = sources.shift();
  2203. if (!source) { continue; }
  2204. if (typeof source !== 'object') {
  2205. throw new TypeError(source + 'must be non-object');
  2206. }
  2207. for (const p in source) {
  2208. if (_has(source, p)) {
  2209. obj[p] = source[p];
  2210. }
  2211. }
  2212. }
  2213. return obj;
  2214. };
  2215. // Join array of chunks to single array.
  2216. var flattenChunks = (chunks) => {
  2217. // calculate data length
  2218. let len = 0;
  2219. for (let i = 0, l = chunks.length; i < l; i++) {
  2220. len += chunks[i].length;
  2221. }
  2222. // join chunks
  2223. const result = new Uint8Array(len);
  2224. for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
  2225. let chunk = chunks[i];
  2226. result.set(chunk, pos);
  2227. pos += chunk.length;
  2228. }
  2229. return result;
  2230. };
  2231. var common = {
  2232. assign: assign,
  2233. flattenChunks: flattenChunks
  2234. };
  2235. // String encode/decode helpers
  2236. // Quick check if we can use fast array to bin string conversion
  2237. //
  2238. // - apply(Array) can fail on Android 2.2
  2239. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  2240. //
  2241. let STR_APPLY_UIA_OK = true;
  2242. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  2243. // Table with utf8 lengths (calculated by first byte of sequence)
  2244. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  2245. // because max possible codepoint is 0x10ffff
  2246. const _utf8len = new Uint8Array(256);
  2247. for (let q = 0; q < 256; q++) {
  2248. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  2249. }
  2250. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  2251. // convert string to array (typed, when possible)
  2252. var string2buf = (str) => {
  2253. if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
  2254. return new TextEncoder().encode(str);
  2255. }
  2256. let buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  2257. // count binary size
  2258. for (m_pos = 0; m_pos < str_len; m_pos++) {
  2259. c = str.charCodeAt(m_pos);
  2260. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  2261. c2 = str.charCodeAt(m_pos + 1);
  2262. if ((c2 & 0xfc00) === 0xdc00) {
  2263. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2264. m_pos++;
  2265. }
  2266. }
  2267. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  2268. }
  2269. // allocate buffer
  2270. buf = new Uint8Array(buf_len);
  2271. // convert
  2272. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  2273. c = str.charCodeAt(m_pos);
  2274. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  2275. c2 = str.charCodeAt(m_pos + 1);
  2276. if ((c2 & 0xfc00) === 0xdc00) {
  2277. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2278. m_pos++;
  2279. }
  2280. }
  2281. if (c < 0x80) {
  2282. /* one byte */
  2283. buf[i++] = c;
  2284. } else if (c < 0x800) {
  2285. /* two bytes */
  2286. buf[i++] = 0xC0 | (c >>> 6);
  2287. buf[i++] = 0x80 | (c & 0x3f);
  2288. } else if (c < 0x10000) {
  2289. /* three bytes */
  2290. buf[i++] = 0xE0 | (c >>> 12);
  2291. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  2292. buf[i++] = 0x80 | (c & 0x3f);
  2293. } else {
  2294. /* four bytes */
  2295. buf[i++] = 0xf0 | (c >>> 18);
  2296. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  2297. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  2298. buf[i++] = 0x80 | (c & 0x3f);
  2299. }
  2300. }
  2301. return buf;
  2302. };
  2303. // Helper
  2304. const buf2binstring = (buf, len) => {
  2305. // On Chrome, the arguments in a function call that are allowed is `65534`.
  2306. // If the length of the buffer is smaller than that, we can use this optimization,
  2307. // otherwise we will take a slower path.
  2308. if (len < 65534) {
  2309. if (buf.subarray && STR_APPLY_UIA_OK) {
  2310. return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
  2311. }
  2312. }
  2313. let result = '';
  2314. for (let i = 0; i < len; i++) {
  2315. result += String.fromCharCode(buf[i]);
  2316. }
  2317. return result;
  2318. };
  2319. // convert array to string
  2320. var buf2string = (buf, max) => {
  2321. const len = max || buf.length;
  2322. if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) {
  2323. return new TextDecoder().decode(buf.subarray(0, max));
  2324. }
  2325. let i, out;
  2326. // Reserve max possible length (2 words per char)
  2327. // NB: by unknown reasons, Array is significantly faster for
  2328. // String.fromCharCode.apply than Uint16Array.
  2329. const utf16buf = new Array(len * 2);
  2330. for (out = 0, i = 0; i < len;) {
  2331. let c = buf[i++];
  2332. // quick process ascii
  2333. if (c < 0x80) { utf16buf[out++] = c; continue; }
  2334. let c_len = _utf8len[c];
  2335. // skip 5 & 6 byte codes
  2336. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  2337. // apply mask on first byte
  2338. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  2339. // join the rest
  2340. while (c_len > 1 && i < len) {
  2341. c = (c << 6) | (buf[i++] & 0x3f);
  2342. c_len--;
  2343. }
  2344. // terminated by end of string?
  2345. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  2346. if (c < 0x10000) {
  2347. utf16buf[out++] = c;
  2348. } else {
  2349. c -= 0x10000;
  2350. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  2351. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  2352. }
  2353. }
  2354. return buf2binstring(utf16buf, out);
  2355. };
  2356. // Calculate max possible position in utf8 buffer,
  2357. // that will not break sequence. If that's not possible
  2358. // - (very small limits) return max size as is.
  2359. //
  2360. // buf[] - utf8 bytes array
  2361. // max - length limit (mandatory);
  2362. var utf8border = (buf, max) => {
  2363. max = max || buf.length;
  2364. if (max > buf.length) { max = buf.length; }
  2365. // go back from last position, until start of sequence found
  2366. let pos = max - 1;
  2367. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  2368. // Very small and broken sequence,
  2369. // return max, because we should return something anyway.
  2370. if (pos < 0) { return max; }
  2371. // If we came to start of buffer - that means buffer is too small,
  2372. // return max too.
  2373. if (pos === 0) { return max; }
  2374. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  2375. };
  2376. var strings = {
  2377. string2buf: string2buf,
  2378. buf2string: buf2string,
  2379. utf8border: utf8border
  2380. };
  2381. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2382. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2383. //
  2384. // This software is provided 'as-is', without any express or implied
  2385. // warranty. In no event will the authors be held liable for any damages
  2386. // arising from the use of this software.
  2387. //
  2388. // Permission is granted to anyone to use this software for any purpose,
  2389. // including commercial applications, and to alter it and redistribute it
  2390. // freely, subject to the following restrictions:
  2391. //
  2392. // 1. The origin of this software must not be misrepresented; you must not
  2393. // claim that you wrote the original software. If you use this software
  2394. // in a product, an acknowledgment in the product documentation would be
  2395. // appreciated but is not required.
  2396. // 2. Altered source versions must be plainly marked as such, and must not be
  2397. // misrepresented as being the original software.
  2398. // 3. This notice may not be removed or altered from any source distribution.
  2399. var messages = {
  2400. 2: 'need dictionary', /* Z_NEED_DICT 2 */
  2401. 1: 'stream end', /* Z_STREAM_END 1 */
  2402. 0: '', /* Z_OK 0 */
  2403. '-1': 'file error', /* Z_ERRNO (-1) */
  2404. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  2405. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  2406. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  2407. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  2408. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  2409. };
  2410. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2411. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2412. //
  2413. // This software is provided 'as-is', without any express or implied
  2414. // warranty. In no event will the authors be held liable for any damages
  2415. // arising from the use of this software.
  2416. //
  2417. // Permission is granted to anyone to use this software for any purpose,
  2418. // including commercial applications, and to alter it and redistribute it
  2419. // freely, subject to the following restrictions:
  2420. //
  2421. // 1. The origin of this software must not be misrepresented; you must not
  2422. // claim that you wrote the original software. If you use this software
  2423. // in a product, an acknowledgment in the product documentation would be
  2424. // appreciated but is not required.
  2425. // 2. Altered source versions must be plainly marked as such, and must not be
  2426. // misrepresented as being the original software.
  2427. // 3. This notice may not be removed or altered from any source distribution.
  2428. function ZStream() {
  2429. /* next input byte */
  2430. this.input = null; // JS specific, because we have no pointers
  2431. this.next_in = 0;
  2432. /* number of bytes available at input */
  2433. this.avail_in = 0;
  2434. /* total number of input bytes read so far */
  2435. this.total_in = 0;
  2436. /* next output byte should be put there */
  2437. this.output = null; // JS specific, because we have no pointers
  2438. this.next_out = 0;
  2439. /* remaining free space at output */
  2440. this.avail_out = 0;
  2441. /* total number of bytes output so far */
  2442. this.total_out = 0;
  2443. /* last error message, NULL if no error */
  2444. this.msg = ''/*Z_NULL*/;
  2445. /* not visible by applications */
  2446. this.state = null;
  2447. /* best guess about the data type: binary or text */
  2448. this.data_type = 2/*Z_UNKNOWN*/;
  2449. /* adler32 value of the uncompressed data */
  2450. this.adler = 0;
  2451. }
  2452. var zstream = ZStream;
  2453. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  2454. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  2455. //
  2456. // This software is provided 'as-is', without any express or implied
  2457. // warranty. In no event will the authors be held liable for any damages
  2458. // arising from the use of this software.
  2459. //
  2460. // Permission is granted to anyone to use this software for any purpose,
  2461. // including commercial applications, and to alter it and redistribute it
  2462. // freely, subject to the following restrictions:
  2463. //
  2464. // 1. The origin of this software must not be misrepresented; you must not
  2465. // claim that you wrote the original software. If you use this software
  2466. // in a product, an acknowledgment in the product documentation would be
  2467. // appreciated but is not required.
  2468. // 2. Altered source versions must be plainly marked as such, and must not be
  2469. // misrepresented as being the original software.
  2470. // 3. This notice may not be removed or altered from any source distribution.
  2471. function GZheader() {
  2472. /* true if compressed data believed to be text */
  2473. this.text = 0;
  2474. /* modification time */
  2475. this.time = 0;
  2476. /* extra flags (not used when writing a gzip file) */
  2477. this.xflags = 0;
  2478. /* operating system */
  2479. this.os = 0;
  2480. /* pointer to extra field or Z_NULL if none */
  2481. this.extra = null;
  2482. /* extra field length (valid if extra != Z_NULL) */
  2483. this.extra_len = 0; // Actually, we don't need it in JS,
  2484. // but leave for few code modifications
  2485. //
  2486. // Setup limits is not necessary because in js we should not preallocate memory
  2487. // for inflate use constant limit in 65536 bytes
  2488. //
  2489. /* space at extra (only when reading header) */
  2490. // this.extra_max = 0;
  2491. /* pointer to zero-terminated file name or Z_NULL */
  2492. this.name = '';
  2493. /* space at name (only when reading header) */
  2494. // this.name_max = 0;
  2495. /* pointer to zero-terminated comment or Z_NULL */
  2496. this.comment = '';
  2497. /* space at comment (only when reading header) */
  2498. // this.comm_max = 0;
  2499. /* true if there was or will be a header crc */
  2500. this.hcrc = 0;
  2501. /* true when done reading gzip header (not used when writing a gzip file) */
  2502. this.done = false;
  2503. }
  2504. var gzheader = GZheader;
  2505. const toString = Object.prototype.toString;
  2506. /* Public constants ==========================================================*/
  2507. /* ===========================================================================*/
  2508. const {
  2509. Z_NO_FLUSH, Z_FINISH,
  2510. Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR
  2511. } = constants$1;
  2512. /* ===========================================================================*/
  2513. /**
  2514. * class Inflate
  2515. *
  2516. * Generic JS-style wrapper for zlib calls. If you don't need
  2517. * streaming behaviour - use more simple functions: [[inflate]]
  2518. * and [[inflateRaw]].
  2519. **/
  2520. /* internal
  2521. * inflate.chunks -> Array
  2522. *
  2523. * Chunks of output data, if [[Inflate#onData]] not overridden.
  2524. **/
  2525. /**
  2526. * Inflate.result -> Uint8Array|String
  2527. *
  2528. * Uncompressed result, generated by default [[Inflate#onData]]
  2529. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  2530. * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
  2531. **/
  2532. /**
  2533. * Inflate.err -> Number
  2534. *
  2535. * Error code after inflate finished. 0 (Z_OK) on success.
  2536. * Should be checked if broken data possible.
  2537. **/
  2538. /**
  2539. * Inflate.msg -> String
  2540. *
  2541. * Error message, if [[Inflate.err]] != 0
  2542. **/
  2543. /**
  2544. * new Inflate(options)
  2545. * - options (Object): zlib inflate options.
  2546. *
  2547. * Creates new inflator instance with specified params. Throws exception
  2548. * on bad params. Supported options:
  2549. *
  2550. * - `windowBits`
  2551. * - `dictionary`
  2552. *
  2553. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2554. * for more information on these.
  2555. *
  2556. * Additional options, for internal needs:
  2557. *
  2558. * - `chunkSize` - size of generated data chunks (16K by default)
  2559. * - `raw` (Boolean) - do raw inflate
  2560. * - `to` (String) - if equal to 'string', then result will be converted
  2561. * from utf8 to utf16 (javascript) string. When string output requested,
  2562. * chunk length can differ from `chunkSize`, depending on content.
  2563. *
  2564. * By default, when no options set, autodetect deflate/gzip data format via
  2565. * wrapper header.
  2566. *
  2567. * ##### Example:
  2568. *
  2569. * ```javascript
  2570. * const pako = require('pako')
  2571. * const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
  2572. * const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  2573. *
  2574. * const inflate = new pako.Inflate({ level: 3});
  2575. *
  2576. * inflate.push(chunk1, false);
  2577. * inflate.push(chunk2, true); // true -> last chunk
  2578. *
  2579. * if (inflate.err) { throw new Error(inflate.err); }
  2580. *
  2581. * console.log(inflate.result);
  2582. * ```
  2583. **/
  2584. function Inflate(options) {
  2585. this.options = common.assign({
  2586. chunkSize: 1024 * 64,
  2587. windowBits: 15,
  2588. to: ''
  2589. }, options || {});
  2590. const opt = this.options;
  2591. // Force window size for `raw` data, if not set directly,
  2592. // because we have no header for autodetect.
  2593. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  2594. opt.windowBits = -opt.windowBits;
  2595. if (opt.windowBits === 0) { opt.windowBits = -15; }
  2596. }
  2597. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  2598. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  2599. !(options && options.windowBits)) {
  2600. opt.windowBits += 32;
  2601. }
  2602. // Gzip header has no info about windows size, we can do autodetect only
  2603. // for deflate. So, if window size not set, force it to max when gzip possible
  2604. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  2605. // bit 3 (16) -> gzipped data
  2606. // bit 4 (32) -> autodetect gzip/deflate
  2607. if ((opt.windowBits & 15) === 0) {
  2608. opt.windowBits |= 15;
  2609. }
  2610. }
  2611. this.err = 0; // error code, if happens (0 = Z_OK)
  2612. this.msg = ''; // error message
  2613. this.ended = false; // used to avoid multiple onEnd() calls
  2614. this.chunks = []; // chunks of compressed data
  2615. this.strm = new zstream();
  2616. this.strm.avail_out = 0;
  2617. let status = inflate_1$1.inflateInit2(
  2618. this.strm,
  2619. opt.windowBits
  2620. );
  2621. if (status !== Z_OK) {
  2622. throw new Error(messages[status]);
  2623. }
  2624. this.header = new gzheader();
  2625. inflate_1$1.inflateGetHeader(this.strm, this.header);
  2626. // Setup dictionary
  2627. if (opt.dictionary) {
  2628. // Convert data if needed
  2629. if (typeof opt.dictionary === 'string') {
  2630. opt.dictionary = strings.string2buf(opt.dictionary);
  2631. } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
  2632. opt.dictionary = new Uint8Array(opt.dictionary);
  2633. }
  2634. if (opt.raw) { //In raw mode we need to set the dictionary early
  2635. status = inflate_1$1.inflateSetDictionary(this.strm, opt.dictionary);
  2636. if (status !== Z_OK) {
  2637. throw new Error(messages[status]);
  2638. }
  2639. }
  2640. }
  2641. }
  2642. /**
  2643. * Inflate#push(data[, flush_mode]) -> Boolean
  2644. * - data (Uint8Array|ArrayBuffer): input data
  2645. * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE
  2646. * flush modes. See constants. Skipped or `false` means Z_NO_FLUSH,
  2647. * `true` means Z_FINISH.
  2648. *
  2649. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  2650. * new output chunks. Returns `true` on success. If end of stream detected,
  2651. * [[Inflate#onEnd]] will be called.
  2652. *
  2653. * `flush_mode` is not needed for normal operation, because end of stream
  2654. * detected automatically. You may try to use it for advanced things, but
  2655. * this functionality was not tested.
  2656. *
  2657. * On fail call [[Inflate#onEnd]] with error code and return false.
  2658. *
  2659. * ##### Example
  2660. *
  2661. * ```javascript
  2662. * push(chunk, false); // push one of data chunks
  2663. * ...
  2664. * push(chunk, true); // push last chunk
  2665. * ```
  2666. **/
  2667. Inflate.prototype.push = function (data, flush_mode) {
  2668. const strm = this.strm;
  2669. const chunkSize = this.options.chunkSize;
  2670. const dictionary = this.options.dictionary;
  2671. let status, _flush_mode, last_avail_out;
  2672. if (this.ended) return false;
  2673. if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
  2674. else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH;
  2675. // Convert data if needed
  2676. if (toString.call(data) === '[object ArrayBuffer]') {
  2677. strm.input = new Uint8Array(data);
  2678. } else {
  2679. strm.input = data;
  2680. }
  2681. strm.next_in = 0;
  2682. strm.avail_in = strm.input.length;
  2683. for (;;) {
  2684. if (strm.avail_out === 0) {
  2685. strm.output = new Uint8Array(chunkSize);
  2686. strm.next_out = 0;
  2687. strm.avail_out = chunkSize;
  2688. }
  2689. status = inflate_1$1.inflate(strm, _flush_mode);
  2690. if (status === Z_NEED_DICT && dictionary) {
  2691. status = inflate_1$1.inflateSetDictionary(strm, dictionary);
  2692. if (status === Z_OK) {
  2693. status = inflate_1$1.inflate(strm, _flush_mode);
  2694. } else if (status === Z_DATA_ERROR) {
  2695. // Replace code with more verbose
  2696. status = Z_NEED_DICT;
  2697. }
  2698. }
  2699. // Skip snyc markers if more data follows and not raw mode
  2700. while (strm.avail_in > 0 &&
  2701. status === Z_STREAM_END &&
  2702. strm.state.wrap > 0 &&
  2703. data[strm.next_in] !== 0)
  2704. {
  2705. inflate_1$1.inflateReset(strm);
  2706. status = inflate_1$1.inflate(strm, _flush_mode);
  2707. }
  2708. switch (status) {
  2709. case Z_STREAM_ERROR:
  2710. case Z_DATA_ERROR:
  2711. case Z_NEED_DICT:
  2712. case Z_MEM_ERROR:
  2713. this.onEnd(status);
  2714. this.ended = true;
  2715. return false;
  2716. }
  2717. // Remember real `avail_out` value, because we may patch out buffer content
  2718. // to align utf8 strings boundaries.
  2719. last_avail_out = strm.avail_out;
  2720. if (strm.next_out) {
  2721. if (strm.avail_out === 0 || status === Z_STREAM_END) {
  2722. if (this.options.to === 'string') {
  2723. let next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  2724. let tail = strm.next_out - next_out_utf8;
  2725. let utf8str = strings.buf2string(strm.output, next_out_utf8);
  2726. // move tail & realign counters
  2727. strm.next_out = tail;
  2728. strm.avail_out = chunkSize - tail;
  2729. if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0);
  2730. this.onData(utf8str);
  2731. } else {
  2732. this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out));
  2733. }
  2734. }
  2735. }
  2736. // Must repeat iteration if out buffer is full
  2737. if (status === Z_OK && last_avail_out === 0) continue;
  2738. // Finalize if end of stream reached.
  2739. if (status === Z_STREAM_END) {
  2740. status = inflate_1$1.inflateEnd(this.strm);
  2741. this.onEnd(status);
  2742. this.ended = true;
  2743. return true;
  2744. }
  2745. if (strm.avail_in === 0) break;
  2746. }
  2747. return true;
  2748. };
  2749. /**
  2750. * Inflate#onData(chunk) -> Void
  2751. * - chunk (Uint8Array|String): output data. When string output requested,
  2752. * each chunk will be string.
  2753. *
  2754. * By default, stores data blocks in `chunks[]` property and glue
  2755. * those in `onEnd`. Override this handler, if you need another behaviour.
  2756. **/
  2757. Inflate.prototype.onData = function (chunk) {
  2758. this.chunks.push(chunk);
  2759. };
  2760. /**
  2761. * Inflate#onEnd(status) -> Void
  2762. * - status (Number): inflate status. 0 (Z_OK) on success,
  2763. * other if not.
  2764. *
  2765. * Called either after you tell inflate that the input stream is
  2766. * complete (Z_FINISH). By default - join collected chunks,
  2767. * free memory and fill `results` / `err` properties.
  2768. **/
  2769. Inflate.prototype.onEnd = function (status) {
  2770. // On success - join
  2771. if (status === Z_OK) {
  2772. if (this.options.to === 'string') {
  2773. this.result = this.chunks.join('');
  2774. } else {
  2775. this.result = common.flattenChunks(this.chunks);
  2776. }
  2777. }
  2778. this.chunks = [];
  2779. this.err = status;
  2780. this.msg = this.strm.msg;
  2781. };
  2782. /**
  2783. * inflate(data[, options]) -> Uint8Array|String
  2784. * - data (Uint8Array): input data to decompress.
  2785. * - options (Object): zlib inflate options.
  2786. *
  2787. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  2788. * format via wrapper header by default. That's why we don't provide
  2789. * separate `ungzip` method.
  2790. *
  2791. * Supported options are:
  2792. *
  2793. * - windowBits
  2794. *
  2795. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  2796. * for more information.
  2797. *
  2798. * Sugar (options):
  2799. *
  2800. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  2801. * negative windowBits implicitly.
  2802. * - `to` (String) - if equal to 'string', then result will be converted
  2803. * from utf8 to utf16 (javascript) string. When string output requested,
  2804. * chunk length can differ from `chunkSize`, depending on content.
  2805. *
  2806. *
  2807. * ##### Example:
  2808. *
  2809. * ```javascript
  2810. * const pako = require('pako');
  2811. * const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9]));
  2812. * let output;
  2813. *
  2814. * try {
  2815. * output = pako.inflate(input);
  2816. * } catch (err) {
  2817. * console.log(err);
  2818. * }
  2819. * ```
  2820. **/
  2821. function inflate(input, options) {
  2822. const inflator = new Inflate(options);
  2823. inflator.push(input);
  2824. // That will never happens, if you don't cheat with options :)
  2825. if (inflator.err) throw inflator.msg || messages[inflator.err];
  2826. return inflator.result;
  2827. }
  2828. /**
  2829. * inflateRaw(data[, options]) -> Uint8Array|String
  2830. * - data (Uint8Array): input data to decompress.
  2831. * - options (Object): zlib inflate options.
  2832. *
  2833. * The same as [[inflate]], but creates raw data, without wrapper
  2834. * (header and adler32 crc).
  2835. **/
  2836. function inflateRaw(input, options) {
  2837. options = options || {};
  2838. options.raw = true;
  2839. return inflate(input, options);
  2840. }
  2841. /**
  2842. * ungzip(data[, options]) -> Uint8Array|String
  2843. * - data (Uint8Array): input data to decompress.
  2844. * - options (Object): zlib inflate options.
  2845. *
  2846. * Just shortcut to [[inflate]], because it autodetects format
  2847. * by header.content. Done for convenience.
  2848. **/
  2849. var Inflate_1 = Inflate;
  2850. var inflate_2 = inflate;
  2851. var inflateRaw_1 = inflateRaw;
  2852. var ungzip = inflate;
  2853. var constants = constants$1;
  2854. var inflate_1 = {
  2855. Inflate: Inflate_1,
  2856. inflate: inflate_2,
  2857. inflateRaw: inflateRaw_1,
  2858. ungzip: ungzip,
  2859. constants: constants
  2860. };
  2861. exports.Inflate = Inflate_1;
  2862. exports.constants = constants;
  2863. exports['default'] = inflate_1;
  2864. exports.inflate = inflate_2;
  2865. exports.inflateRaw = inflateRaw_1;
  2866. exports.ungzip = ungzip;
  2867. Object.defineProperty(exports, '__esModule', { value: true });
  2868. })));