zip.js

Source code from https://gildas-lormeau.github.io/zip.js/

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/441873/1030820/zipjs.js

  1. //Source code from https://gildas-lormeau.github.io/zip.js/
  2. //源码来自https://gildas-lormeau.github.io/zip.js/
  3. (function (global, factory) {
  4. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  5. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  6. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.zip = {}));
  7. })(this, (function (exports) { 'use strict';
  8.  
  9. const { Array, Object, String, BigInt, Math, Date, Map, URL, Error, Uint8Array, Uint16Array, Uint32Array, DataView, Blob, Promise, TextEncoder, TextDecoder, FileReader, document, crypto, btoa } = globalThis;
  10.  
  11. /*
  12. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  13.  
  14. Redistribution and use in source and binary forms, with or without
  15. modification, are permitted provided that the following conditions are met:
  16.  
  17. 1. Redistributions of source code must retain the above copyright notice,
  18. this list of conditions and the following disclaimer.
  19.  
  20. 2. Redistributions in binary form must reproduce the above copyright
  21. notice, this list of conditions and the following disclaimer in
  22. the documentation and/or other materials provided with the distribution.
  23.  
  24. 3. The names of the authors may not be used to endorse or promote products
  25. derived from this software without specific prior written permission.
  26.  
  27. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  28. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  29. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  30. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  33. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  36. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38.  
  39. /*
  40. * This program is based on JZlib 1.0.2 ymnk, JCraft,Inc.
  41. * JZlib is based on zlib-1.1.3, so all credit should go authors
  42. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  43. * and contributors of zlib.
  44. */
  45.  
  46. // Global
  47.  
  48. const MAX_BITS$1 = 15;
  49. const D_CODES = 30;
  50. const BL_CODES = 19;
  51.  
  52. const LENGTH_CODES = 29;
  53. const LITERALS = 256;
  54. const L_CODES = (LITERALS + 1 + LENGTH_CODES);
  55. const HEAP_SIZE = (2 * L_CODES + 1);
  56.  
  57. const END_BLOCK = 256;
  58.  
  59. // Bit length codes must not exceed MAX_BL_BITS bits
  60. const MAX_BL_BITS = 7;
  61.  
  62. // repeat previous bit length 3-6 times (2 bits of repeat count)
  63. const REP_3_6 = 16;
  64.  
  65. // repeat a zero length 3-10 times (3 bits of repeat count)
  66. const REPZ_3_10 = 17;
  67.  
  68. // repeat a zero length 11-138 times (7 bits of repeat count)
  69. const REPZ_11_138 = 18;
  70.  
  71. // The lengths of the bit length codes are sent in order of decreasing
  72. // probability, to avoid transmitting the lengths for unused bit
  73. // length codes.
  74.  
  75. const Buf_size = 8 * 2;
  76.  
  77. // JZlib version : "1.0.2"
  78. const Z_DEFAULT_COMPRESSION = -1;
  79.  
  80. // compression strategy
  81. const Z_FILTERED = 1;
  82. const Z_HUFFMAN_ONLY = 2;
  83. const Z_DEFAULT_STRATEGY = 0;
  84.  
  85. const Z_NO_FLUSH$1 = 0;
  86. const Z_PARTIAL_FLUSH = 1;
  87. const Z_FULL_FLUSH = 3;
  88. const Z_FINISH$1 = 4;
  89.  
  90. const Z_OK$1 = 0;
  91. const Z_STREAM_END$1 = 1;
  92. const Z_NEED_DICT$1 = 2;
  93. const Z_STREAM_ERROR$1 = -2;
  94. const Z_DATA_ERROR$1 = -3;
  95. const Z_BUF_ERROR$1 = -5;
  96.  
  97. // Tree
  98.  
  99. function extractArray(array) {
  100. return flatArray(array.map(([length, value]) => (new Array(length)).fill(value, 0, length)));
  101. }
  102.  
  103. function flatArray(array) {
  104. return array.reduce((a, b) => a.concat(Array.isArray(b) ? flatArray(b) : b), []);
  105. }
  106.  
  107. // see definition of array dist_code below
  108. const _dist_code = [0, 1, 2, 3].concat(...extractArray([
  109. [2, 4], [2, 5], [4, 6], [4, 7], [8, 8], [8, 9], [16, 10], [16, 11], [32, 12], [32, 13], [64, 14], [64, 15], [2, 0], [1, 16],
  110. [1, 17], [2, 18], [2, 19], [4, 20], [4, 21], [8, 22], [8, 23], [16, 24], [16, 25], [32, 26], [32, 27], [64, 28], [64, 29]
  111. ]));
  112.  
  113. function Tree() {
  114. const that = this;
  115.  
  116. // dyn_tree; // the dynamic tree
  117. // max_code; // largest code with non zero frequency
  118. // stat_desc; // the corresponding static tree
  119.  
  120. // Compute the optimal bit lengths for a tree and update the total bit
  121. // length
  122. // for the current block.
  123. // IN assertion: the fields freq and dad are set, heap[heap_max] and
  124. // above are the tree nodes sorted by increasing frequency.
  125. // OUT assertions: the field len is set to the optimal bit length, the
  126. // array bl_count contains the frequencies for each bit length.
  127. // The length opt_len is updated; static_len is also updated if stree is
  128. // not null.
  129. function gen_bitlen(s) {
  130. const tree = that.dyn_tree;
  131. const stree = that.stat_desc.static_tree;
  132. const extra = that.stat_desc.extra_bits;
  133. const base = that.stat_desc.extra_base;
  134. const max_length = that.stat_desc.max_length;
  135. let h; // heap index
  136. let n, m; // iterate over the tree elements
  137. let bits; // bit length
  138. let xbits; // extra bits
  139. let f; // frequency
  140. let overflow = 0; // number of elements with bit length too large
  141.  
  142. for (bits = 0; bits <= MAX_BITS$1; bits++)
  143. s.bl_count[bits] = 0;
  144.  
  145. // In a first pass, compute the optimal bit lengths (which may
  146. // overflow in the case of the bit length tree).
  147. tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
  148.  
  149. for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
  150. n = s.heap[h];
  151. bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
  152. if (bits > max_length) {
  153. bits = max_length;
  154. overflow++;
  155. }
  156. tree[n * 2 + 1] = bits;
  157. // We overwrite tree[n*2+1] which is no longer needed
  158.  
  159. if (n > that.max_code)
  160. continue; // not a leaf node
  161.  
  162. s.bl_count[bits]++;
  163. xbits = 0;
  164. if (n >= base)
  165. xbits = extra[n - base];
  166. f = tree[n * 2];
  167. s.opt_len += f * (bits + xbits);
  168. if (stree)
  169. s.static_len += f * (stree[n * 2 + 1] + xbits);
  170. }
  171. if (overflow === 0)
  172. return;
  173.  
  174. // This happens for example on obj2 and pic of the Calgary corpus
  175. // Find the first bit length which could increase:
  176. do {
  177. bits = max_length - 1;
  178. while (s.bl_count[bits] === 0)
  179. bits--;
  180. s.bl_count[bits]--; // move one leaf down the tree
  181. s.bl_count[bits + 1] += 2; // move one overflow item as its brother
  182. s.bl_count[max_length]--;
  183. // The brother of the overflow item also moves one step up,
  184. // but this does not affect bl_count[max_length]
  185. overflow -= 2;
  186. } while (overflow > 0);
  187.  
  188. for (bits = max_length; bits !== 0; bits--) {
  189. n = s.bl_count[bits];
  190. while (n !== 0) {
  191. m = s.heap[--h];
  192. if (m > that.max_code)
  193. continue;
  194. if (tree[m * 2 + 1] != bits) {
  195. s.opt_len += (bits - tree[m * 2 + 1]) * tree[m * 2];
  196. tree[m * 2 + 1] = bits;
  197. }
  198. n--;
  199. }
  200. }
  201. }
  202.  
  203. // Reverse the first len bits of a code, using straightforward code (a
  204. // faster
  205. // method would use a table)
  206. // IN assertion: 1 <= len <= 15
  207. function bi_reverse(code, // the value to invert
  208. len // its bit length
  209. ) {
  210. let res = 0;
  211. do {
  212. res |= code & 1;
  213. code >>>= 1;
  214. res <<= 1;
  215. } while (--len > 0);
  216. return res >>> 1;
  217. }
  218.  
  219. // Generate the codes for a given tree and bit counts (which need not be
  220. // optimal).
  221. // IN assertion: the array bl_count contains the bit length statistics for
  222. // the given tree and the field len is set for all tree elements.
  223. // OUT assertion: the field code is set for all tree elements of non
  224. // zero code length.
  225. function gen_codes(tree, // the tree to decorate
  226. max_code, // largest code with non zero frequency
  227. bl_count // number of codes at each bit length
  228. ) {
  229. const next_code = []; // next code value for each
  230. // bit length
  231. let code = 0; // running code value
  232. let bits; // bit index
  233. let n; // code index
  234. let len;
  235.  
  236. // The distribution counts are first used to generate the code values
  237. // without bit reversal.
  238. for (bits = 1; bits <= MAX_BITS$1; bits++) {
  239. next_code[bits] = code = ((code + bl_count[bits - 1]) << 1);
  240. }
  241.  
  242. // Check that the bit counts in bl_count are consistent. The last code
  243. // must be all ones.
  244. // Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  245. // "inconsistent bit counts");
  246. // Tracev((stderr,"gen_codes: max_code %d ", max_code));
  247.  
  248. for (n = 0; n <= max_code; n++) {
  249. len = tree[n * 2 + 1];
  250. if (len === 0)
  251. continue;
  252. // Now reverse the bits
  253. tree[n * 2] = bi_reverse(next_code[len]++, len);
  254. }
  255. }
  256.  
  257. // Construct one Huffman tree and assigns the code bit strings and lengths.
  258. // Update the total bit length for the current block.
  259. // IN assertion: the field freq is set for all tree elements.
  260. // OUT assertions: the fields len and code are set to the optimal bit length
  261. // and corresponding code. The length opt_len is updated; static_len is
  262. // also updated if stree is not null. The field max_code is set.
  263. that.build_tree = function (s) {
  264. const tree = that.dyn_tree;
  265. const stree = that.stat_desc.static_tree;
  266. const elems = that.stat_desc.elems;
  267. let n, m; // iterate over heap elements
  268. let max_code = -1; // largest code with non zero frequency
  269. let node; // new node being created
  270.  
  271. // Construct the initial heap, with least frequent element in
  272. // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  273. // heap[0] is not used.
  274. s.heap_len = 0;
  275. s.heap_max = HEAP_SIZE;
  276.  
  277. for (n = 0; n < elems; n++) {
  278. if (tree[n * 2] !== 0) {
  279. s.heap[++s.heap_len] = max_code = n;
  280. s.depth[n] = 0;
  281. } else {
  282. tree[n * 2 + 1] = 0;
  283. }
  284. }
  285.  
  286. // The pkzip format requires that at least one distance code exists,
  287. // and that at least one bit should be sent even if there is only one
  288. // possible code. So to avoid special checks later on we force at least
  289. // two codes of non zero frequency.
  290. while (s.heap_len < 2) {
  291. node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
  292. tree[node * 2] = 1;
  293. s.depth[node] = 0;
  294. s.opt_len--;
  295. if (stree)
  296. s.static_len -= stree[node * 2 + 1];
  297. // node is 0 or 1 so it does not have extra bits
  298. }
  299. that.max_code = max_code;
  300.  
  301. // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  302. // establish sub-heaps of increasing lengths:
  303.  
  304. for (n = Math.floor(s.heap_len / 2); n >= 1; n--)
  305. s.pqdownheap(tree, n);
  306.  
  307. // Construct the Huffman tree by repeatedly combining the least two
  308. // frequent nodes.
  309.  
  310. node = elems; // next internal node of the tree
  311. do {
  312. // n = node of least frequency
  313. n = s.heap[1];
  314. s.heap[1] = s.heap[s.heap_len--];
  315. s.pqdownheap(tree, 1);
  316. m = s.heap[1]; // m = node of next least frequency
  317.  
  318. s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
  319. s.heap[--s.heap_max] = m;
  320.  
  321. // Create a new node father of n and m
  322. tree[node * 2] = (tree[n * 2] + tree[m * 2]);
  323. s.depth[node] = Math.max(s.depth[n], s.depth[m]) + 1;
  324. tree[n * 2 + 1] = tree[m * 2 + 1] = node;
  325.  
  326. // and insert the new node in the heap
  327. s.heap[1] = node++;
  328. s.pqdownheap(tree, 1);
  329. } while (s.heap_len >= 2);
  330.  
  331. s.heap[--s.heap_max] = s.heap[1];
  332.  
  333. // At this point, the fields freq and dad are set. We can now
  334. // generate the bit lengths.
  335.  
  336. gen_bitlen(s);
  337.  
  338. // The field len is now set, we can generate the bit codes
  339. gen_codes(tree, that.max_code, s.bl_count);
  340. };
  341.  
  342. }
  343.  
  344. Tree._length_code = [0, 1, 2, 3, 4, 5, 6, 7].concat(...extractArray([
  345. [2, 8], [2, 9], [2, 10], [2, 11], [4, 12], [4, 13], [4, 14], [4, 15], [8, 16], [8, 17], [8, 18], [8, 19],
  346. [16, 20], [16, 21], [16, 22], [16, 23], [32, 24], [32, 25], [32, 26], [31, 27], [1, 28]]));
  347.  
  348. Tree.base_length = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0];
  349.  
  350. Tree.base_dist = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384,
  351. 24576];
  352.  
  353. // Mapping from a distance to a distance code. dist is the distance - 1 and
  354. // must not have side effects. _dist_code[256] and _dist_code[257] are never
  355. // used.
  356. Tree.d_code = function (dist) {
  357. return ((dist) < 256 ? _dist_code[dist] : _dist_code[256 + ((dist) >>> 7)]);
  358. };
  359.  
  360. // extra bits for each length code
  361. Tree.extra_lbits = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];
  362.  
  363. // extra bits for each distance code
  364. Tree.extra_dbits = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
  365.  
  366. // extra bits for each bit length code
  367. Tree.extra_blbits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7];
  368.  
  369. Tree.bl_order = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
  370.  
  371. // StaticTree
  372.  
  373. function StaticTree(static_tree, extra_bits, extra_base, elems, max_length) {
  374. const that = this;
  375. that.static_tree = static_tree;
  376. that.extra_bits = extra_bits;
  377. that.extra_base = extra_base;
  378. that.elems = elems;
  379. that.max_length = max_length;
  380. }
  381.  
  382. const static_ltree2_first_part = [12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82,
  383. 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86,
  384. 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81,
  385. 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85,
  386. 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 19, 275, 147, 403, 83, 339, 211, 467, 51, 307,
  387. 179, 435, 115, 371, 243, 499, 11, 267, 139, 395, 75, 331, 203, 459, 43, 299, 171, 427, 107, 363, 235, 491, 27, 283, 155, 411, 91, 347, 219, 475,
  388. 59, 315, 187, 443, 123, 379, 251, 507, 7, 263, 135, 391, 71, 327, 199, 455, 39, 295, 167, 423, 103, 359, 231, 487, 23, 279, 151, 407, 87, 343, 215,
  389. 471, 55, 311, 183, 439, 119, 375, 247, 503, 15, 271, 143, 399, 79, 335, 207, 463, 47, 303, 175, 431, 111, 367, 239, 495, 31, 287, 159, 415, 95,
  390. 351, 223, 479, 63, 319, 191, 447, 127, 383, 255, 511, 0, 64, 32, 96, 16, 80, 48, 112, 8, 72, 40, 104, 24, 88, 56, 120, 4, 68, 36, 100, 20, 84, 52,
  391. 116, 3, 131, 67, 195, 35, 163, 99, 227];
  392. const static_ltree2_second_part = extractArray([[144, 8], [112, 9], [24, 7], [8, 8]]);
  393. StaticTree.static_ltree = flatArray(static_ltree2_first_part.map((value, index) => [value, static_ltree2_second_part[index]]));
  394.  
  395. const static_dtree_first_part = [0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30, 1, 17, 9, 25, 5, 21, 13, 29, 3, 19, 11, 27, 7, 23];
  396. const static_dtree_second_part = extractArray([[30, 5]]);
  397. StaticTree.static_dtree = flatArray(static_dtree_first_part.map((value, index) => [value, static_dtree_second_part[index]]));
  398.  
  399. StaticTree.static_l_desc = new StaticTree(StaticTree.static_ltree, Tree.extra_lbits, LITERALS + 1, L_CODES, MAX_BITS$1);
  400.  
  401. StaticTree.static_d_desc = new StaticTree(StaticTree.static_dtree, Tree.extra_dbits, 0, D_CODES, MAX_BITS$1);
  402.  
  403. StaticTree.static_bl_desc = new StaticTree(null, Tree.extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  404.  
  405. // Deflate
  406.  
  407. const MAX_MEM_LEVEL = 9;
  408. const DEF_MEM_LEVEL = 8;
  409.  
  410. function Config(good_length, max_lazy, nice_length, max_chain, func) {
  411. const that = this;
  412. that.good_length = good_length;
  413. that.max_lazy = max_lazy;
  414. that.nice_length = nice_length;
  415. that.max_chain = max_chain;
  416. that.func = func;
  417. }
  418.  
  419. const STORED$1 = 0;
  420. const FAST = 1;
  421. const SLOW = 2;
  422. const config_table = [
  423. new Config(0, 0, 0, 0, STORED$1),
  424. new Config(4, 4, 8, 4, FAST),
  425. new Config(4, 5, 16, 8, FAST),
  426. new Config(4, 6, 32, 32, FAST),
  427. new Config(4, 4, 16, 16, SLOW),
  428. new Config(8, 16, 32, 32, SLOW),
  429. new Config(8, 16, 128, 128, SLOW),
  430. new Config(8, 32, 128, 256, SLOW),
  431. new Config(32, 128, 258, 1024, SLOW),
  432. new Config(32, 258, 258, 4096, SLOW)
  433. ];
  434.  
  435. const z_errmsg = ["need dictionary", // Z_NEED_DICT
  436. // 2
  437. "stream end", // Z_STREAM_END 1
  438. "", // Z_OK 0
  439. "", // Z_ERRNO (-1)
  440. "stream error", // Z_STREAM_ERROR (-2)
  441. "data error", // Z_DATA_ERROR (-3)
  442. "", // Z_MEM_ERROR (-4)
  443. "buffer error", // Z_BUF_ERROR (-5)
  444. "",// Z_VERSION_ERROR (-6)
  445. ""];
  446.  
  447. // block not completed, need more input or more output
  448. const NeedMore = 0;
  449.  
  450. // block flush performed
  451. const BlockDone = 1;
  452.  
  453. // finish started, need only more output at next deflate
  454. const FinishStarted = 2;
  455.  
  456. // finish done, accept no more input or output
  457. const FinishDone = 3;
  458.  
  459. // preset dictionary flag in zlib header
  460. const PRESET_DICT$1 = 0x20;
  461.  
  462. const INIT_STATE = 42;
  463. const BUSY_STATE = 113;
  464. const FINISH_STATE = 666;
  465.  
  466. // The deflate compression method
  467. const Z_DEFLATED$1 = 8;
  468.  
  469. const STORED_BLOCK = 0;
  470. const STATIC_TREES = 1;
  471. const DYN_TREES = 2;
  472.  
  473. const MIN_MATCH = 3;
  474. const MAX_MATCH = 258;
  475. const MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  476.  
  477. function smaller(tree, n, m, depth) {
  478. const tn2 = tree[n * 2];
  479. const tm2 = tree[m * 2];
  480. return (tn2 < tm2 || (tn2 == tm2 && depth[n] <= depth[m]));
  481. }
  482.  
  483. function Deflate$1() {
  484.  
  485. const that = this;
  486. let strm; // pointer back to this zlib stream
  487. let status; // as the name implies
  488. // pending_buf; // output still pending
  489. let pending_buf_size; // size of pending_buf
  490. // pending_out; // next pending byte to output to the stream
  491. // pending; // nb of bytes in the pending buffer
  492.  
  493. // dist_buf; // buffer for distances
  494. // lc_buf; // buffer for literals or lengths
  495. // To simplify the code, dist_buf and lc_buf have the same number of elements.
  496. // To use different lengths, an extra flag array would be necessary.
  497.  
  498. let last_flush; // value of flush param for previous deflate call
  499.  
  500. let w_size; // LZ77 win size (32K by default)
  501. let w_bits; // log2(w_size) (8..16)
  502. let w_mask; // w_size - 1
  503.  
  504. let win;
  505. // Sliding win. Input bytes are read into the second half of the win,
  506. // and move to the first half later to keep a dictionary of at least wSize
  507. // bytes. With this organization, matches are limited to a distance of
  508. // wSize-MAX_MATCH bytes, but this ensures that IO is always
  509. // performed with a length multiple of the block size. Also, it limits
  510. // the win size to 64K, which is quite useful on MSDOS.
  511. // To do: use the user input buffer as sliding win.
  512.  
  513. let window_size;
  514. // Actual size of win: 2*wSize, except when the user input buffer
  515. // is directly used as sliding win.
  516.  
  517. let prev;
  518. // Link to older string with same hash index. To limit the size of this
  519. // array to 64K, this link is maintained only for the last 32K strings.
  520. // An index in this array is thus a win index modulo 32K.
  521.  
  522. let head; // Heads of the hash chains or NIL.
  523.  
  524. let ins_h; // hash index of string to be inserted
  525. let hash_size; // number of elements in hash table
  526. let hash_bits; // log2(hash_size)
  527. let hash_mask; // hash_size-1
  528.  
  529. // Number of bits by which ins_h must be shifted at each input
  530. // step. It must be such that after MIN_MATCH steps, the oldest
  531. // byte no longer takes part in the hash key, that is:
  532. // hash_shift * MIN_MATCH >= hash_bits
  533. let hash_shift;
  534.  
  535. // Window position at the beginning of the current output block. Gets
  536. // negative when the win is moved backwards.
  537.  
  538. let block_start;
  539.  
  540. let match_length; // length of best match
  541. let prev_match; // previous match
  542. let match_available; // set if previous match exists
  543. let strstart; // start of string to insert
  544. let match_start; // start of matching string
  545. let lookahead; // number of valid bytes ahead in win
  546.  
  547. // Length of the best match at previous step. Matches not greater than this
  548. // are discarded. This is used in the lazy match evaluation.
  549. let prev_length;
  550.  
  551. // To speed up deflation, hash chains are never searched beyond this
  552. // length. A higher limit improves compression ratio but degrades the speed.
  553. let max_chain_length;
  554.  
  555. // Attempt to find a better match only when the current match is strictly
  556. // smaller than this value. This mechanism is used only for compression
  557. // levels >= 4.
  558. let max_lazy_match;
  559.  
  560. // Insert new strings in the hash table only if the match length is not
  561. // greater than this length. This saves time but degrades compression.
  562. // max_insert_length is used only for compression levels <= 3.
  563.  
  564. let level; // compression level (1..9)
  565. let strategy; // favor or force Huffman coding
  566.  
  567. // Use a faster search when the previous match is longer than this
  568. let good_match;
  569.  
  570. // Stop searching when current match exceeds this
  571. let nice_match;
  572.  
  573. let dyn_ltree; // literal and length tree
  574. let dyn_dtree; // distance tree
  575. let bl_tree; // Huffman tree for bit lengths
  576.  
  577. const l_desc = new Tree(); // desc for literal tree
  578. const d_desc = new Tree(); // desc for distance tree
  579. const bl_desc = new Tree(); // desc for bit length tree
  580.  
  581. // that.heap_len; // number of elements in the heap
  582. // that.heap_max; // element of largest frequency
  583. // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  584. // The same heap array is used to build all trees.
  585.  
  586. // Depth of each subtree used as tie breaker for trees of equal frequency
  587. that.depth = [];
  588.  
  589. // Size of match buffer for literals/lengths. There are 4 reasons for
  590. // limiting lit_bufsize to 64K:
  591. // - frequencies can be kept in 16 bit counters
  592. // - if compression is not successful for the first block, all input
  593. // data is still in the win so we can still emit a stored block even
  594. // when input comes from standard input. (This can also be done for
  595. // all blocks if lit_bufsize is not greater than 32K.)
  596. // - if compression is not successful for a file smaller than 64K, we can
  597. // even emit a stored file instead of a stored block (saving 5 bytes).
  598. // This is applicable only for zip (not gzip or zlib).
  599. // - creating new Huffman trees less frequently may not provide fast
  600. // adaptation to changes in the input data statistics. (Take for
  601. // example a binary file with poorly compressible code followed by
  602. // a highly compressible string table.) Smaller buffer sizes give
  603. // fast adaptation but have of course the overhead of transmitting
  604. // trees more frequently.
  605. // - I can't count above 4
  606. let lit_bufsize;
  607.  
  608. let last_lit; // running index in dist_buf and lc_buf
  609.  
  610. // that.opt_len; // bit length of current block with optimal trees
  611. // that.static_len; // bit length of current block with static trees
  612. let matches; // number of string matches in current block
  613. let last_eob_len; // bit length of EOB code for last block
  614.  
  615. // Output buffer. bits are inserted starting at the bottom (least
  616. // significant bits).
  617. let bi_buf;
  618.  
  619. // Number of valid bits in bi_buf. All bits above the last valid bit
  620. // are always zero.
  621. let bi_valid;
  622.  
  623. // number of codes at each bit length for an optimal tree
  624. that.bl_count = [];
  625.  
  626. // heap used to build the Huffman trees
  627. that.heap = [];
  628.  
  629. dyn_ltree = [];
  630. dyn_dtree = [];
  631. bl_tree = [];
  632.  
  633. function lm_init() {
  634. window_size = 2 * w_size;
  635.  
  636. head[hash_size - 1] = 0;
  637. for (let i = 0; i < hash_size - 1; i++) {
  638. head[i] = 0;
  639. }
  640.  
  641. // Set the default configuration parameters:
  642. max_lazy_match = config_table[level].max_lazy;
  643. good_match = config_table[level].good_length;
  644. nice_match = config_table[level].nice_length;
  645. max_chain_length = config_table[level].max_chain;
  646.  
  647. strstart = 0;
  648. block_start = 0;
  649. lookahead = 0;
  650. match_length = prev_length = MIN_MATCH - 1;
  651. match_available = 0;
  652. ins_h = 0;
  653. }
  654.  
  655. function init_block() {
  656. let i;
  657. // Initialize the trees.
  658. for (i = 0; i < L_CODES; i++)
  659. dyn_ltree[i * 2] = 0;
  660. for (i = 0; i < D_CODES; i++)
  661. dyn_dtree[i * 2] = 0;
  662. for (i = 0; i < BL_CODES; i++)
  663. bl_tree[i * 2] = 0;
  664.  
  665. dyn_ltree[END_BLOCK * 2] = 1;
  666. that.opt_len = that.static_len = 0;
  667. last_lit = matches = 0;
  668. }
  669.  
  670. // Initialize the tree data structures for a new zlib stream.
  671. function tr_init() {
  672.  
  673. l_desc.dyn_tree = dyn_ltree;
  674. l_desc.stat_desc = StaticTree.static_l_desc;
  675.  
  676. d_desc.dyn_tree = dyn_dtree;
  677. d_desc.stat_desc = StaticTree.static_d_desc;
  678.  
  679. bl_desc.dyn_tree = bl_tree;
  680. bl_desc.stat_desc = StaticTree.static_bl_desc;
  681.  
  682. bi_buf = 0;
  683. bi_valid = 0;
  684. last_eob_len = 8; // enough lookahead for inflate
  685.  
  686. // Initialize the first block of the first file:
  687. init_block();
  688. }
  689.  
  690. // Restore the heap property by moving down the tree starting at node k,
  691. // exchanging a node with the smallest of its two sons if necessary,
  692. // stopping
  693. // when the heap property is re-established (each father smaller than its
  694. // two sons).
  695. that.pqdownheap = function (tree, // the tree to restore
  696. k // node to move down
  697. ) {
  698. const heap = that.heap;
  699. const v = heap[k];
  700. let j = k << 1; // left son of k
  701. while (j <= that.heap_len) {
  702. // Set j to the smallest of the two sons:
  703. if (j < that.heap_len && smaller(tree, heap[j + 1], heap[j], that.depth)) {
  704. j++;
  705. }
  706. // Exit if v is smaller than both sons
  707. if (smaller(tree, v, heap[j], that.depth))
  708. break;
  709.  
  710. // Exchange v with the smallest son
  711. heap[k] = heap[j];
  712. k = j;
  713. // And continue down the tree, setting j to the left son of k
  714. j <<= 1;
  715. }
  716. heap[k] = v;
  717. };
  718.  
  719. // Scan a literal or distance tree to determine the frequencies of the codes
  720. // in the bit length tree.
  721. function scan_tree(tree,// the tree to be scanned
  722. max_code // and its largest code of non zero frequency
  723. ) {
  724. let prevlen = -1; // last emitted length
  725. let curlen; // length of current code
  726. let nextlen = tree[0 * 2 + 1]; // length of next code
  727. let count = 0; // repeat count of the current code
  728. let max_count = 7; // max repeat count
  729. let min_count = 4; // min repeat count
  730.  
  731. if (nextlen === 0) {
  732. max_count = 138;
  733. min_count = 3;
  734. }
  735. tree[(max_code + 1) * 2 + 1] = 0xffff; // guard
  736.  
  737. for (let n = 0; n <= max_code; n++) {
  738. curlen = nextlen;
  739. nextlen = tree[(n + 1) * 2 + 1];
  740. if (++count < max_count && curlen == nextlen) {
  741. continue;
  742. } else if (count < min_count) {
  743. bl_tree[curlen * 2] += count;
  744. } else if (curlen !== 0) {
  745. if (curlen != prevlen)
  746. bl_tree[curlen * 2]++;
  747. bl_tree[REP_3_6 * 2]++;
  748. } else if (count <= 10) {
  749. bl_tree[REPZ_3_10 * 2]++;
  750. } else {
  751. bl_tree[REPZ_11_138 * 2]++;
  752. }
  753. count = 0;
  754. prevlen = curlen;
  755. if (nextlen === 0) {
  756. max_count = 138;
  757. min_count = 3;
  758. } else if (curlen == nextlen) {
  759. max_count = 6;
  760. min_count = 3;
  761. } else {
  762. max_count = 7;
  763. min_count = 4;
  764. }
  765. }
  766. }
  767.  
  768. // Construct the Huffman tree for the bit lengths and return the index in
  769. // bl_order of the last bit length code to send.
  770. function build_bl_tree() {
  771. let max_blindex; // index of last bit length code of non zero freq
  772.  
  773. // Determine the bit length frequencies for literal and distance trees
  774. scan_tree(dyn_ltree, l_desc.max_code);
  775. scan_tree(dyn_dtree, d_desc.max_code);
  776.  
  777. // Build the bit length tree:
  778. bl_desc.build_tree(that);
  779. // opt_len now includes the length of the tree representations, except
  780. // the lengths of the bit lengths codes and the 5+5+4 bits for the
  781. // counts.
  782.  
  783. // Determine the number of bit length codes to send. The pkzip format
  784. // requires that at least 4 bit length codes be sent. (appnote.txt says
  785. // 3 but the actual value used is 4.)
  786. for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
  787. if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] !== 0)
  788. break;
  789. }
  790. // Update opt_len to include the bit length tree and counts
  791. that.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  792.  
  793. return max_blindex;
  794. }
  795.  
  796. // Output a byte on the stream.
  797. // IN assertion: there is enough room in pending_buf.
  798. function put_byte(p) {
  799. that.pending_buf[that.pending++] = p;
  800. }
  801.  
  802. function put_short(w) {
  803. put_byte(w & 0xff);
  804. put_byte((w >>> 8) & 0xff);
  805. }
  806.  
  807. function putShortMSB(b) {
  808. put_byte((b >> 8) & 0xff);
  809. put_byte((b & 0xff) & 0xff);
  810. }
  811.  
  812. function send_bits(value, length) {
  813. let val;
  814. const len = length;
  815. if (bi_valid > Buf_size - len) {
  816. val = value;
  817. // bi_buf |= (val << bi_valid);
  818. bi_buf |= ((val << bi_valid) & 0xffff);
  819. put_short(bi_buf);
  820. bi_buf = val >>> (Buf_size - bi_valid);
  821. bi_valid += len - Buf_size;
  822. } else {
  823. // bi_buf |= (value) << bi_valid;
  824. bi_buf |= (((value) << bi_valid) & 0xffff);
  825. bi_valid += len;
  826. }
  827. }
  828.  
  829. function send_code(c, tree) {
  830. const c2 = c * 2;
  831. send_bits(tree[c2] & 0xffff, tree[c2 + 1] & 0xffff);
  832. }
  833.  
  834. // Send a literal or distance tree in compressed form, using the codes in
  835. // bl_tree.
  836. function send_tree(tree,// the tree to be sent
  837. max_code // and its largest code of non zero frequency
  838. ) {
  839. let n; // iterates over all tree elements
  840. let prevlen = -1; // last emitted length
  841. let curlen; // length of current code
  842. let nextlen = tree[0 * 2 + 1]; // length of next code
  843. let count = 0; // repeat count of the current code
  844. let max_count = 7; // max repeat count
  845. let min_count = 4; // min repeat count
  846.  
  847. if (nextlen === 0) {
  848. max_count = 138;
  849. min_count = 3;
  850. }
  851.  
  852. for (n = 0; n <= max_code; n++) {
  853. curlen = nextlen;
  854. nextlen = tree[(n + 1) * 2 + 1];
  855. if (++count < max_count && curlen == nextlen) {
  856. continue;
  857. } else if (count < min_count) {
  858. do {
  859. send_code(curlen, bl_tree);
  860. } while (--count !== 0);
  861. } else if (curlen !== 0) {
  862. if (curlen != prevlen) {
  863. send_code(curlen, bl_tree);
  864. count--;
  865. }
  866. send_code(REP_3_6, bl_tree);
  867. send_bits(count - 3, 2);
  868. } else if (count <= 10) {
  869. send_code(REPZ_3_10, bl_tree);
  870. send_bits(count - 3, 3);
  871. } else {
  872. send_code(REPZ_11_138, bl_tree);
  873. send_bits(count - 11, 7);
  874. }
  875. count = 0;
  876. prevlen = curlen;
  877. if (nextlen === 0) {
  878. max_count = 138;
  879. min_count = 3;
  880. } else if (curlen == nextlen) {
  881. max_count = 6;
  882. min_count = 3;
  883. } else {
  884. max_count = 7;
  885. min_count = 4;
  886. }
  887. }
  888. }
  889.  
  890. // Send the header for a block using dynamic Huffman trees: the counts, the
  891. // lengths of the bit length codes, the literal tree and the distance tree.
  892. // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  893. function send_all_trees(lcodes, dcodes, blcodes) {
  894. let rank; // index in bl_order
  895.  
  896. send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt
  897. send_bits(dcodes - 1, 5);
  898. send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt
  899. for (rank = 0; rank < blcodes; rank++) {
  900. send_bits(bl_tree[Tree.bl_order[rank] * 2 + 1], 3);
  901. }
  902. send_tree(dyn_ltree, lcodes - 1); // literal tree
  903. send_tree(dyn_dtree, dcodes - 1); // distance tree
  904. }
  905.  
  906. // Flush the bit buffer, keeping at most 7 bits in it.
  907. function bi_flush() {
  908. if (bi_valid == 16) {
  909. put_short(bi_buf);
  910. bi_buf = 0;
  911. bi_valid = 0;
  912. } else if (bi_valid >= 8) {
  913. put_byte(bi_buf & 0xff);
  914. bi_buf >>>= 8;
  915. bi_valid -= 8;
  916. }
  917. }
  918.  
  919. // Send one empty static block to give enough lookahead for inflate.
  920. // This takes 10 bits, of which 7 may remain in the bit buffer.
  921. // The current inflate code requires 9 bits of lookahead. If the
  922. // last two codes for the previous block (real code plus EOB) were coded
  923. // on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  924. // the last real code. In this case we send two empty static blocks instead
  925. // of one. (There are no problems if the previous block is stored or fixed.)
  926. // To simplify the code, we assume the worst case of last real code encoded
  927. // on one bit only.
  928. function _tr_align() {
  929. send_bits(STATIC_TREES << 1, 3);
  930. send_code(END_BLOCK, StaticTree.static_ltree);
  931.  
  932. bi_flush();
  933.  
  934. // Of the 10 bits for the empty block, we have already sent
  935. // (10 - bi_valid) bits. The lookahead for the last real code (before
  936. // the EOB of the previous block) was thus at least one plus the length
  937. // of the EOB plus what we have just sent of the empty static block.
  938. if (1 + last_eob_len + 10 - bi_valid < 9) {
  939. send_bits(STATIC_TREES << 1, 3);
  940. send_code(END_BLOCK, StaticTree.static_ltree);
  941. bi_flush();
  942. }
  943. last_eob_len = 7;
  944. }
  945.  
  946. // Save the match info and tally the frequency counts. Return true if
  947. // the current block must be flushed.
  948. function _tr_tally(dist, // distance of matched string
  949. lc // match length-MIN_MATCH or unmatched char (if dist==0)
  950. ) {
  951. let out_length, in_length, dcode;
  952. that.dist_buf[last_lit] = dist;
  953. that.lc_buf[last_lit] = lc & 0xff;
  954. last_lit++;
  955.  
  956. if (dist === 0) {
  957. // lc is the unmatched char
  958. dyn_ltree[lc * 2]++;
  959. } else {
  960. matches++;
  961. // Here, lc is the match length - MIN_MATCH
  962. dist--; // dist = match distance - 1
  963. dyn_ltree[(Tree._length_code[lc] + LITERALS + 1) * 2]++;
  964. dyn_dtree[Tree.d_code(dist) * 2]++;
  965. }
  966.  
  967. if ((last_lit & 0x1fff) === 0 && level > 2) {
  968. // Compute an upper bound for the compressed length
  969. out_length = last_lit * 8;
  970. in_length = strstart - block_start;
  971. for (dcode = 0; dcode < D_CODES; dcode++) {
  972. out_length += dyn_dtree[dcode * 2] * (5 + Tree.extra_dbits[dcode]);
  973. }
  974. out_length >>>= 3;
  975. if ((matches < Math.floor(last_lit / 2)) && out_length < Math.floor(in_length / 2))
  976. return true;
  977. }
  978.  
  979. return (last_lit == lit_bufsize - 1);
  980. // We avoid equality with lit_bufsize because of wraparound at 64K
  981. // on 16 bit machines and because stored blocks are restricted to
  982. // 64K-1 bytes.
  983. }
  984.  
  985. // Send the block data compressed using the given Huffman trees
  986. function compress_block(ltree, dtree) {
  987. let dist; // distance of matched string
  988. let lc; // match length or unmatched char (if dist === 0)
  989. let lx = 0; // running index in dist_buf and lc_buf
  990. let code; // the code to send
  991. let extra; // number of extra bits to send
  992.  
  993. if (last_lit !== 0) {
  994. do {
  995. dist = that.dist_buf[lx];
  996. lc = that.lc_buf[lx];
  997. lx++;
  998.  
  999. if (dist === 0) {
  1000. send_code(lc, ltree); // send a literal byte
  1001. } else {
  1002. // Here, lc is the match length - MIN_MATCH
  1003. code = Tree._length_code[lc];
  1004.  
  1005. send_code(code + LITERALS + 1, ltree); // send the length
  1006. // code
  1007. extra = Tree.extra_lbits[code];
  1008. if (extra !== 0) {
  1009. lc -= Tree.base_length[code];
  1010. send_bits(lc, extra); // send the extra length bits
  1011. }
  1012. dist--; // dist is now the match distance - 1
  1013. code = Tree.d_code(dist);
  1014.  
  1015. send_code(code, dtree); // send the distance code
  1016. extra = Tree.extra_dbits[code];
  1017. if (extra !== 0) {
  1018. dist -= Tree.base_dist[code];
  1019. send_bits(dist, extra); // send the extra distance bits
  1020. }
  1021. } // literal or match pair ?
  1022. } while (lx < last_lit);
  1023. }
  1024.  
  1025. send_code(END_BLOCK, ltree);
  1026. last_eob_len = ltree[END_BLOCK * 2 + 1];
  1027. }
  1028.  
  1029. // Flush the bit buffer and align the output on a byte boundary
  1030. function bi_windup() {
  1031. if (bi_valid > 8) {
  1032. put_short(bi_buf);
  1033. } else if (bi_valid > 0) {
  1034. put_byte(bi_buf & 0xff);
  1035. }
  1036. bi_buf = 0;
  1037. bi_valid = 0;
  1038. }
  1039.  
  1040. // Copy a stored block, storing first the length and its
  1041. // one's complement if requested.
  1042. function copy_block(buf, // the input data
  1043. len, // its length
  1044. header // true if block header must be written
  1045. ) {
  1046. bi_windup(); // align on byte boundary
  1047. last_eob_len = 8; // enough lookahead for inflate
  1048.  
  1049. if (header) {
  1050. put_short(len);
  1051. put_short(~len);
  1052. }
  1053.  
  1054. that.pending_buf.set(win.subarray(buf, buf + len), that.pending);
  1055. that.pending += len;
  1056. }
  1057.  
  1058. // Send a stored block
  1059. function _tr_stored_block(buf, // input block
  1060. stored_len, // length of input block
  1061. eof // true if this is the last block for a file
  1062. ) {
  1063. send_bits((STORED_BLOCK << 1) + (eof ? 1 : 0), 3); // send block type
  1064. copy_block(buf, stored_len, true); // with header
  1065. }
  1066.  
  1067. // Determine the best encoding for the current block: dynamic trees, static
  1068. // trees or store, and output the encoded block to the zip file.
  1069. function _tr_flush_block(buf, // input block, or NULL if too old
  1070. stored_len, // length of input block
  1071. eof // true if this is the last block for a file
  1072. ) {
  1073. let opt_lenb, static_lenb;// opt_len and static_len in bytes
  1074. let max_blindex = 0; // index of last bit length code of non zero freq
  1075.  
  1076. // Build the Huffman trees unless a stored block is forced
  1077. if (level > 0) {
  1078. // Construct the literal and distance trees
  1079. l_desc.build_tree(that);
  1080.  
  1081. d_desc.build_tree(that);
  1082.  
  1083. // At this point, opt_len and static_len are the total bit lengths
  1084. // of
  1085. // the compressed block data, excluding the tree representations.
  1086.  
  1087. // Build the bit length tree for the above two trees, and get the
  1088. // index
  1089. // in bl_order of the last bit length code to send.
  1090. max_blindex = build_bl_tree();
  1091.  
  1092. // Determine the best encoding. Compute first the block length in
  1093. // bytes
  1094. opt_lenb = (that.opt_len + 3 + 7) >>> 3;
  1095. static_lenb = (that.static_len + 3 + 7) >>> 3;
  1096.  
  1097. if (static_lenb <= opt_lenb)
  1098. opt_lenb = static_lenb;
  1099. } else {
  1100. opt_lenb = static_lenb = stored_len + 5; // force a stored block
  1101. }
  1102.  
  1103. if ((stored_len + 4 <= opt_lenb) && buf != -1) {
  1104. // 4: two words for the lengths
  1105. // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1106. // Otherwise we can't have processed more than WSIZE input bytes
  1107. // since
  1108. // the last block flush, because compression would have been
  1109. // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1110. // transform a block into a stored block.
  1111. _tr_stored_block(buf, stored_len, eof);
  1112. } else if (static_lenb == opt_lenb) {
  1113. send_bits((STATIC_TREES << 1) + (eof ? 1 : 0), 3);
  1114. compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
  1115. } else {
  1116. send_bits((DYN_TREES << 1) + (eof ? 1 : 0), 3);
  1117. send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1);
  1118. compress_block(dyn_ltree, dyn_dtree);
  1119. }
  1120.  
  1121. // The above check is made mod 2^32, for files larger than 512 MB
  1122. // and uLong implemented on 32 bits.
  1123.  
  1124. init_block();
  1125.  
  1126. if (eof) {
  1127. bi_windup();
  1128. }
  1129. }
  1130.  
  1131. function flush_block_only(eof) {
  1132. _tr_flush_block(block_start >= 0 ? block_start : -1, strstart - block_start, eof);
  1133. block_start = strstart;
  1134. strm.flush_pending();
  1135. }
  1136.  
  1137. // Fill the win when the lookahead becomes insufficient.
  1138. // Updates strstart and lookahead.
  1139. //
  1140. // IN assertion: lookahead < MIN_LOOKAHEAD
  1141. // OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1142. // At least one byte has been read, or avail_in === 0; reads are
  1143. // performed for at least two bytes (required for the zip translate_eol
  1144. // option -- not supported here).
  1145. function fill_window() {
  1146. let n, m;
  1147. let p;
  1148. let more; // Amount of free space at the end of the win.
  1149.  
  1150. do {
  1151. more = (window_size - lookahead - strstart);
  1152.  
  1153. // Deal with !@#$% 64K limit:
  1154. if (more === 0 && strstart === 0 && lookahead === 0) {
  1155. more = w_size;
  1156. } else if (more == -1) {
  1157. // Very unlikely, but possible on 16 bit machine if strstart ==
  1158. // 0
  1159. // and lookahead == 1 (input done one byte at time)
  1160. more--;
  1161.  
  1162. // If the win is almost full and there is insufficient
  1163. // lookahead,
  1164. // move the upper half to the lower one to make room in the
  1165. // upper half.
  1166. } else if (strstart >= w_size + w_size - MIN_LOOKAHEAD) {
  1167. win.set(win.subarray(w_size, w_size + w_size), 0);
  1168.  
  1169. match_start -= w_size;
  1170. strstart -= w_size; // we now have strstart >= MAX_DIST
  1171. block_start -= w_size;
  1172.  
  1173. // Slide the hash table (could be avoided with 32 bit values
  1174. // at the expense of memory usage). We slide even when level ==
  1175. // 0
  1176. // to keep the hash table consistent if we switch back to level
  1177. // > 0
  1178. // later. (Using level 0 permanently is not an optimal usage of
  1179. // zlib, so we don't care about this pathological case.)
  1180.  
  1181. n = hash_size;
  1182. p = n;
  1183. do {
  1184. m = (head[--p] & 0xffff);
  1185. head[p] = (m >= w_size ? m - w_size : 0);
  1186. } while (--n !== 0);
  1187.  
  1188. n = w_size;
  1189. p = n;
  1190. do {
  1191. m = (prev[--p] & 0xffff);
  1192. prev[p] = (m >= w_size ? m - w_size : 0);
  1193. // If n is not on any hash chain, prev[n] is garbage but
  1194. // its value will never be used.
  1195. } while (--n !== 0);
  1196. more += w_size;
  1197. }
  1198.  
  1199. if (strm.avail_in === 0)
  1200. return;
  1201.  
  1202. // If there was no sliding:
  1203. // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1204. // more == window_size - lookahead - strstart
  1205. // => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1206. // => more >= window_size - 2*WSIZE + 2
  1207. // In the BIG_MEM or MMAP case (not yet supported),
  1208. // window_size == input_size + MIN_LOOKAHEAD &&
  1209. // strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1210. // Otherwise, window_size == 2*WSIZE so more >= 2.
  1211. // If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1212.  
  1213. n = strm.read_buf(win, strstart + lookahead, more);
  1214. lookahead += n;
  1215.  
  1216. // Initialize the hash value now that we have some input:
  1217. if (lookahead >= MIN_MATCH) {
  1218. ins_h = win[strstart] & 0xff;
  1219. ins_h = (((ins_h) << hash_shift) ^ (win[strstart + 1] & 0xff)) & hash_mask;
  1220. }
  1221. // If the whole input has less than MIN_MATCH bytes, ins_h is
  1222. // garbage,
  1223. // but this is not important since only literal bytes will be
  1224. // emitted.
  1225. } while (lookahead < MIN_LOOKAHEAD && strm.avail_in !== 0);
  1226. }
  1227.  
  1228. // Copy without compression as much as possible from the input stream,
  1229. // return
  1230. // the current block state.
  1231. // This function does not insert new strings in the dictionary since
  1232. // uncompressible data is probably not useful. This function is used
  1233. // only for the level=0 compression option.
  1234. // NOTE: this function should be optimized to avoid extra copying from
  1235. // win to pending_buf.
  1236. function deflate_stored(flush) {
  1237. // Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1238. // to pending_buf_size, and each stored block has a 5 byte header:
  1239.  
  1240. let max_block_size = 0xffff;
  1241. let max_start;
  1242.  
  1243. if (max_block_size > pending_buf_size - 5) {
  1244. max_block_size = pending_buf_size - 5;
  1245. }
  1246.  
  1247. // Copy as much as possible from input to output:
  1248. // eslint-disable-next-line no-constant-condition
  1249. while (true) {
  1250. // Fill the win as much as possible:
  1251. if (lookahead <= 1) {
  1252. fill_window();
  1253. if (lookahead === 0 && flush == Z_NO_FLUSH$1)
  1254. return NeedMore;
  1255. if (lookahead === 0)
  1256. break; // flush the current block
  1257. }
  1258.  
  1259. strstart += lookahead;
  1260. lookahead = 0;
  1261.  
  1262. // Emit a stored block if pending_buf will be full:
  1263. max_start = block_start + max_block_size;
  1264. if (strstart === 0 || strstart >= max_start) {
  1265. // strstart === 0 is possible when wraparound on 16-bit machine
  1266. lookahead = (strstart - max_start);
  1267. strstart = max_start;
  1268.  
  1269. flush_block_only(false);
  1270. if (strm.avail_out === 0)
  1271. return NeedMore;
  1272.  
  1273. }
  1274.  
  1275. // Flush if we may have to slide, otherwise block_start may become
  1276. // negative and the data will be gone:
  1277. if (strstart - block_start >= w_size - MIN_LOOKAHEAD) {
  1278. flush_block_only(false);
  1279. if (strm.avail_out === 0)
  1280. return NeedMore;
  1281. }
  1282. }
  1283.  
  1284. flush_block_only(flush == Z_FINISH$1);
  1285. if (strm.avail_out === 0)
  1286. return (flush == Z_FINISH$1) ? FinishStarted : NeedMore;
  1287.  
  1288. return flush == Z_FINISH$1 ? FinishDone : BlockDone;
  1289. }
  1290.  
  1291. function longest_match(cur_match) {
  1292. let chain_length = max_chain_length; // max hash chain length
  1293. let scan = strstart; // current string
  1294. let match; // matched string
  1295. let len; // length of current match
  1296. let best_len = prev_length; // best match length so far
  1297. const limit = strstart > (w_size - MIN_LOOKAHEAD) ? strstart - (w_size - MIN_LOOKAHEAD) : 0;
  1298. let _nice_match = nice_match;
  1299.  
  1300. // Stop when cur_match becomes <= limit. To simplify the code,
  1301. // we prevent matches with the string of win index 0.
  1302.  
  1303. const wmask = w_mask;
  1304.  
  1305. const strend = strstart + MAX_MATCH;
  1306. let scan_end1 = win[scan + best_len - 1];
  1307. let scan_end = win[scan + best_len];
  1308.  
  1309. // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of
  1310. // 16.
  1311. // It is easy to get rid of this optimization if necessary.
  1312.  
  1313. // Do not waste too much time if we already have a good match:
  1314. if (prev_length >= good_match) {
  1315. chain_length >>= 2;
  1316. }
  1317.  
  1318. // Do not look for matches beyond the end of the input. This is
  1319. // necessary
  1320. // to make deflate deterministic.
  1321. if (_nice_match > lookahead)
  1322. _nice_match = lookahead;
  1323.  
  1324. do {
  1325. match = cur_match;
  1326.  
  1327. // Skip to next match if the match length cannot increase
  1328. // or if the match length is less than 2:
  1329. if (win[match + best_len] != scan_end || win[match + best_len - 1] != scan_end1 || win[match] != win[scan]
  1330. || win[++match] != win[scan + 1])
  1331. continue;
  1332.  
  1333. // The check at best_len-1 can be removed because it will be made
  1334. // again later. (This heuristic is not always a win.)
  1335. // It is not necessary to compare scan[2] and match[2] since they
  1336. // are always equal when the other bytes match, given that
  1337. // the hash keys are equal and that HASH_BITS >= 8.
  1338. scan += 2;
  1339. match++;
  1340.  
  1341. // We check for insufficient lookahead only every 8th comparison;
  1342. // the 256th check will be made at strstart+258.
  1343. // eslint-disable-next-line no-empty
  1344. do {
  1345. } while (win[++scan] == win[++match] && win[++scan] == win[++match] && win[++scan] == win[++match]
  1346. && win[++scan] == win[++match] && win[++scan] == win[++match] && win[++scan] == win[++match]
  1347. && win[++scan] == win[++match] && win[++scan] == win[++match] && scan < strend);
  1348.  
  1349. len = MAX_MATCH - (strend - scan);
  1350. scan = strend - MAX_MATCH;
  1351.  
  1352. if (len > best_len) {
  1353. match_start = cur_match;
  1354. best_len = len;
  1355. if (len >= _nice_match)
  1356. break;
  1357. scan_end1 = win[scan + best_len - 1];
  1358. scan_end = win[scan + best_len];
  1359. }
  1360.  
  1361. } while ((cur_match = (prev[cur_match & wmask] & 0xffff)) > limit && --chain_length !== 0);
  1362.  
  1363. if (best_len <= lookahead)
  1364. return best_len;
  1365. return lookahead;
  1366. }
  1367.  
  1368. // Compress as much as possible from the input stream, return the current
  1369. // block state.
  1370. // This function does not perform lazy evaluation of matches and inserts
  1371. // new strings in the dictionary only for unmatched strings or for short
  1372. // matches. It is used only for the fast compression options.
  1373. function deflate_fast(flush) {
  1374. // short hash_head = 0; // head of the hash chain
  1375. let hash_head = 0; // head of the hash chain
  1376. let bflush; // set if current block must be flushed
  1377.  
  1378. // eslint-disable-next-line no-constant-condition
  1379. while (true) {
  1380. // Make sure that we always have enough lookahead, except
  1381. // at the end of the input file. We need MAX_MATCH bytes
  1382. // for the next match, plus MIN_MATCH bytes to insert the
  1383. // string following the next match.
  1384. if (lookahead < MIN_LOOKAHEAD) {
  1385. fill_window();
  1386. if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH$1) {
  1387. return NeedMore;
  1388. }
  1389. if (lookahead === 0)
  1390. break; // flush the current block
  1391. }
  1392.  
  1393. // Insert the string win[strstart .. strstart+2] in the
  1394. // dictionary, and set hash_head to the head of the hash chain:
  1395. if (lookahead >= MIN_MATCH) {
  1396. ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1397.  
  1398. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1399. hash_head = (head[ins_h] & 0xffff);
  1400. prev[strstart & w_mask] = head[ins_h];
  1401. head[ins_h] = strstart;
  1402. }
  1403.  
  1404. // Find the longest match, discarding those <= prev_length.
  1405. // At this point we have always match_length < MIN_MATCH
  1406.  
  1407. if (hash_head !== 0 && ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD) {
  1408. // To simplify the code, we prevent matches with the string
  1409. // of win index 0 (in particular we have to avoid a match
  1410. // of the string with itself at the start of the input file).
  1411. if (strategy != Z_HUFFMAN_ONLY) {
  1412. match_length = longest_match(hash_head);
  1413. }
  1414. // longest_match() sets match_start
  1415. }
  1416. if (match_length >= MIN_MATCH) {
  1417. // check_match(strstart, match_start, match_length);
  1418.  
  1419. bflush = _tr_tally(strstart - match_start, match_length - MIN_MATCH);
  1420.  
  1421. lookahead -= match_length;
  1422.  
  1423. // Insert new strings in the hash table only if the match length
  1424. // is not too large. This saves time but degrades compression.
  1425. if (match_length <= max_lazy_match && lookahead >= MIN_MATCH) {
  1426. match_length--; // string at strstart already in hash table
  1427. do {
  1428. strstart++;
  1429.  
  1430. ins_h = ((ins_h << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1431. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1432. hash_head = (head[ins_h] & 0xffff);
  1433. prev[strstart & w_mask] = head[ins_h];
  1434. head[ins_h] = strstart;
  1435.  
  1436. // strstart never exceeds WSIZE-MAX_MATCH, so there are
  1437. // always MIN_MATCH bytes ahead.
  1438. } while (--match_length !== 0);
  1439. strstart++;
  1440. } else {
  1441. strstart += match_length;
  1442. match_length = 0;
  1443. ins_h = win[strstart] & 0xff;
  1444.  
  1445. ins_h = (((ins_h) << hash_shift) ^ (win[strstart + 1] & 0xff)) & hash_mask;
  1446. // If lookahead < MIN_MATCH, ins_h is garbage, but it does
  1447. // not
  1448. // matter since it will be recomputed at next deflate call.
  1449. }
  1450. } else {
  1451. // No match, output a literal byte
  1452.  
  1453. bflush = _tr_tally(0, win[strstart] & 0xff);
  1454. lookahead--;
  1455. strstart++;
  1456. }
  1457. if (bflush) {
  1458.  
  1459. flush_block_only(false);
  1460. if (strm.avail_out === 0)
  1461. return NeedMore;
  1462. }
  1463. }
  1464.  
  1465. flush_block_only(flush == Z_FINISH$1);
  1466. if (strm.avail_out === 0) {
  1467. if (flush == Z_FINISH$1)
  1468. return FinishStarted;
  1469. else
  1470. return NeedMore;
  1471. }
  1472. return flush == Z_FINISH$1 ? FinishDone : BlockDone;
  1473. }
  1474.  
  1475. // Same as above, but achieves better compression. We use a lazy
  1476. // evaluation for matches: a match is finally adopted only if there is
  1477. // no better match at the next win position.
  1478. function deflate_slow(flush) {
  1479. // short hash_head = 0; // head of hash chain
  1480. let hash_head = 0; // head of hash chain
  1481. let bflush; // set if current block must be flushed
  1482. let max_insert;
  1483.  
  1484. // Process the input block.
  1485. // eslint-disable-next-line no-constant-condition
  1486. while (true) {
  1487. // Make sure that we always have enough lookahead, except
  1488. // at the end of the input file. We need MAX_MATCH bytes
  1489. // for the next match, plus MIN_MATCH bytes to insert the
  1490. // string following the next match.
  1491.  
  1492. if (lookahead < MIN_LOOKAHEAD) {
  1493. fill_window();
  1494. if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH$1) {
  1495. return NeedMore;
  1496. }
  1497. if (lookahead === 0)
  1498. break; // flush the current block
  1499. }
  1500.  
  1501. // Insert the string win[strstart .. strstart+2] in the
  1502. // dictionary, and set hash_head to the head of the hash chain:
  1503.  
  1504. if (lookahead >= MIN_MATCH) {
  1505. ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1506. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1507. hash_head = (head[ins_h] & 0xffff);
  1508. prev[strstart & w_mask] = head[ins_h];
  1509. head[ins_h] = strstart;
  1510. }
  1511.  
  1512. // Find the longest match, discarding those <= prev_length.
  1513. prev_length = match_length;
  1514. prev_match = match_start;
  1515. match_length = MIN_MATCH - 1;
  1516.  
  1517. if (hash_head !== 0 && prev_length < max_lazy_match && ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD) {
  1518. // To simplify the code, we prevent matches with the string
  1519. // of win index 0 (in particular we have to avoid a match
  1520. // of the string with itself at the start of the input file).
  1521.  
  1522. if (strategy != Z_HUFFMAN_ONLY) {
  1523. match_length = longest_match(hash_head);
  1524. }
  1525. // longest_match() sets match_start
  1526.  
  1527. if (match_length <= 5 && (strategy == Z_FILTERED || (match_length == MIN_MATCH && strstart - match_start > 4096))) {
  1528.  
  1529. // If prev_match is also MIN_MATCH, match_start is garbage
  1530. // but we will ignore the current match anyway.
  1531. match_length = MIN_MATCH - 1;
  1532. }
  1533. }
  1534.  
  1535. // If there was a match at the previous step and the current
  1536. // match is not better, output the previous match:
  1537. if (prev_length >= MIN_MATCH && match_length <= prev_length) {
  1538. max_insert = strstart + lookahead - MIN_MATCH;
  1539. // Do not insert strings in hash table beyond this.
  1540.  
  1541. // check_match(strstart-1, prev_match, prev_length);
  1542.  
  1543. bflush = _tr_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH);
  1544.  
  1545. // Insert in hash table all strings up to the end of the match.
  1546. // strstart-1 and strstart are already inserted. If there is not
  1547. // enough lookahead, the last two strings are not inserted in
  1548. // the hash table.
  1549. lookahead -= prev_length - 1;
  1550. prev_length -= 2;
  1551. do {
  1552. if (++strstart <= max_insert) {
  1553. ins_h = (((ins_h) << hash_shift) ^ (win[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1554. // prev[strstart&w_mask]=hash_head=head[ins_h];
  1555. hash_head = (head[ins_h] & 0xffff);
  1556. prev[strstart & w_mask] = head[ins_h];
  1557. head[ins_h] = strstart;
  1558. }
  1559. } while (--prev_length !== 0);
  1560. match_available = 0;
  1561. match_length = MIN_MATCH - 1;
  1562. strstart++;
  1563.  
  1564. if (bflush) {
  1565. flush_block_only(false);
  1566. if (strm.avail_out === 0)
  1567. return NeedMore;
  1568. }
  1569. } else if (match_available !== 0) {
  1570.  
  1571. // If there was no match at the previous position, output a
  1572. // single literal. If there was a match but the current match
  1573. // is longer, truncate the previous match to a single literal.
  1574.  
  1575. bflush = _tr_tally(0, win[strstart - 1] & 0xff);
  1576.  
  1577. if (bflush) {
  1578. flush_block_only(false);
  1579. }
  1580. strstart++;
  1581. lookahead--;
  1582. if (strm.avail_out === 0)
  1583. return NeedMore;
  1584. } else {
  1585. // There is no previous match to compare with, wait for
  1586. // the next step to decide.
  1587.  
  1588. match_available = 1;
  1589. strstart++;
  1590. lookahead--;
  1591. }
  1592. }
  1593.  
  1594. if (match_available !== 0) {
  1595. bflush = _tr_tally(0, win[strstart - 1] & 0xff);
  1596. match_available = 0;
  1597. }
  1598. flush_block_only(flush == Z_FINISH$1);
  1599.  
  1600. if (strm.avail_out === 0) {
  1601. if (flush == Z_FINISH$1)
  1602. return FinishStarted;
  1603. else
  1604. return NeedMore;
  1605. }
  1606.  
  1607. return flush == Z_FINISH$1 ? FinishDone : BlockDone;
  1608. }
  1609.  
  1610. function deflateReset(strm) {
  1611. strm.total_in = strm.total_out = 0;
  1612. strm.msg = null; //
  1613.  
  1614. that.pending = 0;
  1615. that.pending_out = 0;
  1616.  
  1617. status = BUSY_STATE;
  1618.  
  1619. last_flush = Z_NO_FLUSH$1;
  1620.  
  1621. tr_init();
  1622. lm_init();
  1623. return Z_OK$1;
  1624. }
  1625.  
  1626. that.deflateInit = function (strm, _level, bits, _method, memLevel, _strategy) {
  1627. if (!_method)
  1628. _method = Z_DEFLATED$1;
  1629. if (!memLevel)
  1630. memLevel = DEF_MEM_LEVEL;
  1631. if (!_strategy)
  1632. _strategy = Z_DEFAULT_STRATEGY;
  1633.  
  1634. // byte[] my_version=ZLIB_VERSION;
  1635.  
  1636. //
  1637. // if (!version || version[0] != my_version[0]
  1638. // || stream_size != sizeof(z_stream)) {
  1639. // return Z_VERSION_ERROR;
  1640. // }
  1641.  
  1642. strm.msg = null;
  1643.  
  1644. if (_level == Z_DEFAULT_COMPRESSION)
  1645. _level = 6;
  1646.  
  1647. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || _method != Z_DEFLATED$1 || bits < 9 || bits > 15 || _level < 0 || _level > 9 || _strategy < 0
  1648. || _strategy > Z_HUFFMAN_ONLY) {
  1649. return Z_STREAM_ERROR$1;
  1650. }
  1651.  
  1652. strm.dstate = that;
  1653.  
  1654. w_bits = bits;
  1655. w_size = 1 << w_bits;
  1656. w_mask = w_size - 1;
  1657.  
  1658. hash_bits = memLevel + 7;
  1659. hash_size = 1 << hash_bits;
  1660. hash_mask = hash_size - 1;
  1661. hash_shift = Math.floor((hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  1662.  
  1663. win = new Uint8Array(w_size * 2);
  1664. prev = [];
  1665. head = [];
  1666.  
  1667. lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
  1668.  
  1669. that.pending_buf = new Uint8Array(lit_bufsize * 4);
  1670. pending_buf_size = lit_bufsize * 4;
  1671.  
  1672. that.dist_buf = new Uint16Array(lit_bufsize);
  1673. that.lc_buf = new Uint8Array(lit_bufsize);
  1674.  
  1675. level = _level;
  1676.  
  1677. strategy = _strategy;
  1678.  
  1679. return deflateReset(strm);
  1680. };
  1681.  
  1682. that.deflateEnd = function () {
  1683. if (status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE) {
  1684. return Z_STREAM_ERROR$1;
  1685. }
  1686. // Deallocate in reverse order of allocations:
  1687. that.lc_buf = null;
  1688. that.dist_buf = null;
  1689. that.pending_buf = null;
  1690. head = null;
  1691. prev = null;
  1692. win = null;
  1693. // free
  1694. that.dstate = null;
  1695. return status == BUSY_STATE ? Z_DATA_ERROR$1 : Z_OK$1;
  1696. };
  1697.  
  1698. that.deflateParams = function (strm, _level, _strategy) {
  1699. let err = Z_OK$1;
  1700.  
  1701. if (_level == Z_DEFAULT_COMPRESSION) {
  1702. _level = 6;
  1703. }
  1704. if (_level < 0 || _level > 9 || _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
  1705. return Z_STREAM_ERROR$1;
  1706. }
  1707.  
  1708. if (config_table[level].func != config_table[_level].func && strm.total_in !== 0) {
  1709. // Flush the last buffer:
  1710. err = strm.deflate(Z_PARTIAL_FLUSH);
  1711. }
  1712.  
  1713. if (level != _level) {
  1714. level = _level;
  1715. max_lazy_match = config_table[level].max_lazy;
  1716. good_match = config_table[level].good_length;
  1717. nice_match = config_table[level].nice_length;
  1718. max_chain_length = config_table[level].max_chain;
  1719. }
  1720. strategy = _strategy;
  1721. return err;
  1722. };
  1723.  
  1724. that.deflateSetDictionary = function (strm, dictionary, dictLength) {
  1725. let length = dictLength;
  1726. let n, index = 0;
  1727.  
  1728. if (!dictionary || status != INIT_STATE)
  1729. return Z_STREAM_ERROR$1;
  1730.  
  1731. if (length < MIN_MATCH)
  1732. return Z_OK$1;
  1733. if (length > w_size - MIN_LOOKAHEAD) {
  1734. length = w_size - MIN_LOOKAHEAD;
  1735. index = dictLength - length; // use the tail of the dictionary
  1736. }
  1737. win.set(dictionary.subarray(index, index + length), 0);
  1738.  
  1739. strstart = length;
  1740. block_start = length;
  1741.  
  1742. // Insert all strings in the hash table (except for the last two bytes).
  1743. // s->lookahead stays null, so s->ins_h will be recomputed at the next
  1744. // call of fill_window.
  1745.  
  1746. ins_h = win[0] & 0xff;
  1747. ins_h = (((ins_h) << hash_shift) ^ (win[1] & 0xff)) & hash_mask;
  1748.  
  1749. for (n = 0; n <= length - MIN_MATCH; n++) {
  1750. ins_h = (((ins_h) << hash_shift) ^ (win[(n) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
  1751. prev[n & w_mask] = head[ins_h];
  1752. head[ins_h] = n;
  1753. }
  1754. return Z_OK$1;
  1755. };
  1756.  
  1757. that.deflate = function (_strm, flush) {
  1758. let i, header, level_flags, old_flush, bstate;
  1759.  
  1760. if (flush > Z_FINISH$1 || flush < 0) {
  1761. return Z_STREAM_ERROR$1;
  1762. }
  1763.  
  1764. if (!_strm.next_out || (!_strm.next_in && _strm.avail_in !== 0) || (status == FINISH_STATE && flush != Z_FINISH$1)) {
  1765. _strm.msg = z_errmsg[Z_NEED_DICT$1 - (Z_STREAM_ERROR$1)];
  1766. return Z_STREAM_ERROR$1;
  1767. }
  1768. if (_strm.avail_out === 0) {
  1769. _strm.msg = z_errmsg[Z_NEED_DICT$1 - (Z_BUF_ERROR$1)];
  1770. return Z_BUF_ERROR$1;
  1771. }
  1772.  
  1773. strm = _strm; // just in case
  1774. old_flush = last_flush;
  1775. last_flush = flush;
  1776.  
  1777. // Write the zlib header
  1778. if (status == INIT_STATE) {
  1779. header = (Z_DEFLATED$1 + ((w_bits - 8) << 4)) << 8;
  1780. level_flags = ((level - 1) & 0xff) >> 1;
  1781.  
  1782. if (level_flags > 3)
  1783. level_flags = 3;
  1784. header |= (level_flags << 6);
  1785. if (strstart !== 0)
  1786. header |= PRESET_DICT$1;
  1787. header += 31 - (header % 31);
  1788.  
  1789. status = BUSY_STATE;
  1790. putShortMSB(header);
  1791. }
  1792.  
  1793. // Flush as much pending output as possible
  1794. if (that.pending !== 0) {
  1795. strm.flush_pending();
  1796. if (strm.avail_out === 0) {
  1797. // console.log(" avail_out==0");
  1798. // Since avail_out is 0, deflate will be called again with
  1799. // more output space, but possibly with both pending and
  1800. // avail_in equal to zero. There won't be anything to do,
  1801. // but this is not an error situation so make sure we
  1802. // return OK instead of BUF_ERROR at next call of deflate:
  1803. last_flush = -1;
  1804. return Z_OK$1;
  1805. }
  1806.  
  1807. // Make sure there is something to do and avoid duplicate
  1808. // consecutive
  1809. // flushes. For repeated and useless calls with Z_FINISH, we keep
  1810. // returning Z_STREAM_END instead of Z_BUFF_ERROR.
  1811. } else if (strm.avail_in === 0 && flush <= old_flush && flush != Z_FINISH$1) {
  1812. strm.msg = z_errmsg[Z_NEED_DICT$1 - (Z_BUF_ERROR$1)];
  1813. return Z_BUF_ERROR$1;
  1814. }
  1815.  
  1816. // User must not provide more input after the first FINISH:
  1817. if (status == FINISH_STATE && strm.avail_in !== 0) {
  1818. _strm.msg = z_errmsg[Z_NEED_DICT$1 - (Z_BUF_ERROR$1)];
  1819. return Z_BUF_ERROR$1;
  1820. }
  1821.  
  1822. // Start a new block or continue the current one.
  1823. if (strm.avail_in !== 0 || lookahead !== 0 || (flush != Z_NO_FLUSH$1 && status != FINISH_STATE)) {
  1824. bstate = -1;
  1825. switch (config_table[level].func) {
  1826. case STORED$1:
  1827. bstate = deflate_stored(flush);
  1828. break;
  1829. case FAST:
  1830. bstate = deflate_fast(flush);
  1831. break;
  1832. case SLOW:
  1833. bstate = deflate_slow(flush);
  1834. break;
  1835. }
  1836.  
  1837. if (bstate == FinishStarted || bstate == FinishDone) {
  1838. status = FINISH_STATE;
  1839. }
  1840. if (bstate == NeedMore || bstate == FinishStarted) {
  1841. if (strm.avail_out === 0) {
  1842. last_flush = -1; // avoid BUF_ERROR next call, see above
  1843. }
  1844. return Z_OK$1;
  1845. // If flush != Z_NO_FLUSH && avail_out === 0, the next call
  1846. // of deflate should use the same flush parameter to make sure
  1847. // that the flush is complete. So we don't have to output an
  1848. // empty block here, this will be done at next call. This also
  1849. // ensures that for a very small output buffer, we emit at most
  1850. // one empty block.
  1851. }
  1852.  
  1853. if (bstate == BlockDone) {
  1854. if (flush == Z_PARTIAL_FLUSH) {
  1855. _tr_align();
  1856. } else { // FULL_FLUSH or SYNC_FLUSH
  1857. _tr_stored_block(0, 0, false);
  1858. // For a full flush, this empty block will be recognized
  1859. // as a special marker by inflate_sync().
  1860. if (flush == Z_FULL_FLUSH) {
  1861. // state.head[s.hash_size-1]=0;
  1862. for (i = 0; i < hash_size/*-1*/; i++)
  1863. // forget history
  1864. head[i] = 0;
  1865. }
  1866. }
  1867. strm.flush_pending();
  1868. if (strm.avail_out === 0) {
  1869. last_flush = -1; // avoid BUF_ERROR at next call, see above
  1870. return Z_OK$1;
  1871. }
  1872. }
  1873. }
  1874.  
  1875. if (flush != Z_FINISH$1)
  1876. return Z_OK$1;
  1877. return Z_STREAM_END$1;
  1878. };
  1879. }
  1880.  
  1881. // ZStream
  1882.  
  1883. function ZStream$1() {
  1884. const that = this;
  1885. that.next_in_index = 0;
  1886. that.next_out_index = 0;
  1887. // that.next_in; // next input byte
  1888. that.avail_in = 0; // number of bytes available at next_in
  1889. that.total_in = 0; // total nb of input bytes read so far
  1890. // that.next_out; // next output byte should be put there
  1891. that.avail_out = 0; // remaining free space at next_out
  1892. that.total_out = 0; // total nb of bytes output so far
  1893. // that.msg;
  1894. // that.dstate;
  1895. }
  1896.  
  1897. ZStream$1.prototype = {
  1898. deflateInit: function (level, bits) {
  1899. const that = this;
  1900. that.dstate = new Deflate$1();
  1901. if (!bits)
  1902. bits = MAX_BITS$1;
  1903. return that.dstate.deflateInit(that, level, bits);
  1904. },
  1905.  
  1906. deflate: function (flush) {
  1907. const that = this;
  1908. if (!that.dstate) {
  1909. return Z_STREAM_ERROR$1;
  1910. }
  1911. return that.dstate.deflate(that, flush);
  1912. },
  1913.  
  1914. deflateEnd: function () {
  1915. const that = this;
  1916. if (!that.dstate)
  1917. return Z_STREAM_ERROR$1;
  1918. const ret = that.dstate.deflateEnd();
  1919. that.dstate = null;
  1920. return ret;
  1921. },
  1922.  
  1923. deflateParams: function (level, strategy) {
  1924. const that = this;
  1925. if (!that.dstate)
  1926. return Z_STREAM_ERROR$1;
  1927. return that.dstate.deflateParams(that, level, strategy);
  1928. },
  1929.  
  1930. deflateSetDictionary: function (dictionary, dictLength) {
  1931. const that = this;
  1932. if (!that.dstate)
  1933. return Z_STREAM_ERROR$1;
  1934. return that.dstate.deflateSetDictionary(that, dictionary, dictLength);
  1935. },
  1936.  
  1937. // Read a new buffer from the current input stream, update the
  1938. // total number of bytes read. All deflate() input goes through
  1939. // this function so some applications may wish to modify it to avoid
  1940. // allocating a large strm->next_in buffer and copying from it.
  1941. // (See also flush_pending()).
  1942. read_buf: function (buf, start, size) {
  1943. const that = this;
  1944. let len = that.avail_in;
  1945. if (len > size)
  1946. len = size;
  1947. if (len === 0)
  1948. return 0;
  1949. that.avail_in -= len;
  1950. buf.set(that.next_in.subarray(that.next_in_index, that.next_in_index + len), start);
  1951. that.next_in_index += len;
  1952. that.total_in += len;
  1953. return len;
  1954. },
  1955.  
  1956. // Flush as much pending output as possible. All deflate() output goes
  1957. // through this function so some applications may wish to modify it
  1958. // to avoid allocating a large strm->next_out buffer and copying into it.
  1959. // (See also read_buf()).
  1960. flush_pending: function () {
  1961. const that = this;
  1962. let len = that.dstate.pending;
  1963.  
  1964. if (len > that.avail_out)
  1965. len = that.avail_out;
  1966. if (len === 0)
  1967. return;
  1968.  
  1969. // if (that.dstate.pending_buf.length <= that.dstate.pending_out || that.next_out.length <= that.next_out_index
  1970. // || that.dstate.pending_buf.length < (that.dstate.pending_out + len) || that.next_out.length < (that.next_out_index +
  1971. // len)) {
  1972. // console.log(that.dstate.pending_buf.length + ", " + that.dstate.pending_out + ", " + that.next_out.length + ", " +
  1973. // that.next_out_index + ", " + len);
  1974. // console.log("avail_out=" + that.avail_out);
  1975. // }
  1976.  
  1977. that.next_out.set(that.dstate.pending_buf.subarray(that.dstate.pending_out, that.dstate.pending_out + len), that.next_out_index);
  1978.  
  1979. that.next_out_index += len;
  1980. that.dstate.pending_out += len;
  1981. that.total_out += len;
  1982. that.avail_out -= len;
  1983. that.dstate.pending -= len;
  1984. if (that.dstate.pending === 0) {
  1985. that.dstate.pending_out = 0;
  1986. }
  1987. }
  1988. };
  1989.  
  1990. // Deflate
  1991.  
  1992. function ZipDeflate(options) {
  1993. const that = this;
  1994. const z = new ZStream$1();
  1995. const bufsize = getMaximumCompressedSize$1(options && options.chunkSize ? options.chunkSize : 64 * 1024);
  1996. const flush = Z_NO_FLUSH$1;
  1997. const buf = new Uint8Array(bufsize);
  1998. let level = options ? options.level : Z_DEFAULT_COMPRESSION;
  1999. if (typeof level == "undefined")
  2000. level = Z_DEFAULT_COMPRESSION;
  2001. z.deflateInit(level);
  2002. z.next_out = buf;
  2003.  
  2004. that.append = function (data, onprogress) {
  2005. let err, array, lastIndex = 0, bufferIndex = 0, bufferSize = 0;
  2006. const buffers = [];
  2007. if (!data.length)
  2008. return;
  2009. z.next_in_index = 0;
  2010. z.next_in = data;
  2011. z.avail_in = data.length;
  2012. do {
  2013. z.next_out_index = 0;
  2014. z.avail_out = bufsize;
  2015. err = z.deflate(flush);
  2016. if (err != Z_OK$1)
  2017. throw new Error("deflating: " + z.msg);
  2018. if (z.next_out_index)
  2019. if (z.next_out_index == bufsize)
  2020. buffers.push(new Uint8Array(buf));
  2021. else
  2022. buffers.push(buf.slice(0, z.next_out_index));
  2023. bufferSize += z.next_out_index;
  2024. if (onprogress && z.next_in_index > 0 && z.next_in_index != lastIndex) {
  2025. onprogress(z.next_in_index);
  2026. lastIndex = z.next_in_index;
  2027. }
  2028. } while (z.avail_in > 0 || z.avail_out === 0);
  2029. if (buffers.length > 1) {
  2030. array = new Uint8Array(bufferSize);
  2031. buffers.forEach(function (chunk) {
  2032. array.set(chunk, bufferIndex);
  2033. bufferIndex += chunk.length;
  2034. });
  2035. } else {
  2036. array = buffers[0] || new Uint8Array(0);
  2037. }
  2038. return array;
  2039. };
  2040. that.flush = function () {
  2041. let err, array, bufferIndex = 0, bufferSize = 0;
  2042. const buffers = [];
  2043. do {
  2044. z.next_out_index = 0;
  2045. z.avail_out = bufsize;
  2046. err = z.deflate(Z_FINISH$1);
  2047. if (err != Z_STREAM_END$1 && err != Z_OK$1)
  2048. throw new Error("deflating: " + z.msg);
  2049. if (bufsize - z.avail_out > 0)
  2050. buffers.push(buf.slice(0, z.next_out_index));
  2051. bufferSize += z.next_out_index;
  2052. } while (z.avail_in > 0 || z.avail_out === 0);
  2053. z.deflateEnd();
  2054. array = new Uint8Array(bufferSize);
  2055. buffers.forEach(function (chunk) {
  2056. array.set(chunk, bufferIndex);
  2057. bufferIndex += chunk.length;
  2058. });
  2059. return array;
  2060. };
  2061. }
  2062.  
  2063. function getMaximumCompressedSize$1(uncompressedSize) {
  2064. return uncompressedSize + (5 * (Math.floor(uncompressedSize / 16383) + 1));
  2065. }
  2066.  
  2067. /*
  2068. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  2069.  
  2070. Redistribution and use in source and binary forms, with or without
  2071. modification, are permitted provided that the following conditions are met:
  2072.  
  2073. 1. Redistributions of source code must retain the above copyright notice,
  2074. this list of conditions and the following disclaimer.
  2075.  
  2076. 2. Redistributions in binary form must reproduce the above copyright
  2077. notice, this list of conditions and the following disclaimer in
  2078. the documentation and/or other materials provided with the distribution.
  2079.  
  2080. 3. The names of the authors may not be used to endorse or promote products
  2081. derived from this software without specific prior written permission.
  2082.  
  2083. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  2084. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2085. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  2086. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  2087. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2088. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  2089. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2090. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2091. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  2092. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2093. */
  2094.  
  2095. /*
  2096. * This program is based on JZlib 1.0.2 ymnk, JCraft,Inc.
  2097. * JZlib is based on zlib-1.1.3, so all credit should go authors
  2098. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  2099. * and contributors of zlib.
  2100. */
  2101.  
  2102. // Global
  2103. const MAX_BITS = 15;
  2104.  
  2105. const Z_OK = 0;
  2106. const Z_STREAM_END = 1;
  2107. const Z_NEED_DICT = 2;
  2108. const Z_STREAM_ERROR = -2;
  2109. const Z_DATA_ERROR = -3;
  2110. const Z_MEM_ERROR = -4;
  2111. const Z_BUF_ERROR = -5;
  2112.  
  2113. const inflate_mask = [0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff,
  2114. 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff];
  2115.  
  2116. const MANY = 1440;
  2117.  
  2118. // JZlib version : "1.0.2"
  2119. const Z_NO_FLUSH = 0;
  2120. const Z_FINISH = 4;
  2121.  
  2122. // InfTree
  2123. const fixed_bl = 9;
  2124. const fixed_bd = 5;
  2125.  
  2126. const fixed_tl = [96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0,
  2127. 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40,
  2128. 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13,
  2129. 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60,
  2130. 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7,
  2131. 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8,
  2132. 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80,
  2133. 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0,
  2134. 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0,
  2135. 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97,
  2136. 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210,
  2137. 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117,
  2138. 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154,
  2139. 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83,
  2140. 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230,
  2141. 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139,
  2142. 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174,
  2143. 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111,
  2144. 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9,
  2145. 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8,
  2146. 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8,
  2147. 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8,
  2148. 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9,
  2149. 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8,
  2150. 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9,
  2151. 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8,
  2152. 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9,
  2153. 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0,
  2154. 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9,
  2155. 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8,
  2156. 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9,
  2157. 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8,
  2158. 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9,
  2159. 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8,
  2160. 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9,
  2161. 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8,
  2162. 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255];
  2163. const fixed_td = [80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5,
  2164. 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5,
  2165. 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577];
  2166.  
  2167. // Tables for deflate from PKZIP's appnote.txt.
  2168. const cplens = [ // Copy lengths for literal codes 257..285
  2169. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0];
  2170.  
  2171. // see note #13 above about 258
  2172. const cplext = [ // Extra bits for literal codes 257..285
  2173. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 // 112==invalid
  2174. ];
  2175.  
  2176. const cpdist = [ // Copy offsets for distance codes 0..29
  2177. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577];
  2178.  
  2179. const cpdext = [ // Extra bits for distance codes
  2180. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
  2181.  
  2182. // If BMAX needs to be larger than 16, then h and x[] should be uLong.
  2183. const BMAX = 15; // maximum bit length of any code
  2184.  
  2185. function InfTree() {
  2186. const that = this;
  2187.  
  2188. let hn; // hufts used in space
  2189. let v; // work area for huft_build
  2190. let c; // bit length count table
  2191. let r; // table entry for structure assignment
  2192. let u; // table stack
  2193. let x; // bit offsets, then code stack
  2194.  
  2195. function huft_build(b, // code lengths in bits (all assumed <=
  2196. // BMAX)
  2197. bindex, n, // number of codes (assumed <= 288)
  2198. s, // number of simple-valued codes (0..s-1)
  2199. d, // list of base values for non-simple codes
  2200. e, // list of extra bits for non-simple codes
  2201. t, // result: starting table
  2202. m, // maximum lookup bits, returns actual
  2203. hp,// space for trees
  2204. hn,// hufts used in space
  2205. v // working area: values in order of bit length
  2206. ) {
  2207. // Given a list of code lengths and a maximum table size, make a set of
  2208. // tables to decode that set of codes. Return Z_OK on success,
  2209. // Z_BUF_ERROR
  2210. // if the given code set is incomplete (the tables are still built in
  2211. // this
  2212. // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set
  2213. // of
  2214. // lengths), or Z_MEM_ERROR if not enough memory.
  2215.  
  2216. let a; // counter for codes of length k
  2217. let f; // i repeats in table every f entries
  2218. let g; // maximum code length
  2219. let h; // table level
  2220. let i; // counter, current code
  2221. let j; // counter
  2222. let k; // number of bits in current code
  2223. let l; // bits per table (returned in m)
  2224. let mask; // (1 << w) - 1, to avoid cc -O bug on HP
  2225. let p; // pointer into c[], b[], or v[]
  2226. let q; // points to current table
  2227. let w; // bits before this table == (l * h)
  2228. let xp; // pointer into x
  2229. let y; // number of dummy codes added
  2230. let z; // number of entries in current table
  2231.  
  2232. // Generate counts for each bit length
  2233.  
  2234. p = 0;
  2235. i = n;
  2236. do {
  2237. c[b[bindex + p]]++;
  2238. p++;
  2239. i--; // assume all entries <= BMAX
  2240. } while (i !== 0);
  2241.  
  2242. if (c[0] == n) { // null input--all zero length codes
  2243. t[0] = -1;
  2244. m[0] = 0;
  2245. return Z_OK;
  2246. }
  2247.  
  2248. // Find minimum and maximum length, bound *m by those
  2249. l = m[0];
  2250. for (j = 1; j <= BMAX; j++)
  2251. if (c[j] !== 0)
  2252. break;
  2253. k = j; // minimum code length
  2254. if (l < j) {
  2255. l = j;
  2256. }
  2257. for (i = BMAX; i !== 0; i--) {
  2258. if (c[i] !== 0)
  2259. break;
  2260. }
  2261. g = i; // maximum code length
  2262. if (l > i) {
  2263. l = i;
  2264. }
  2265. m[0] = l;
  2266.  
  2267. // Adjust last length count to fill out codes, if needed
  2268. for (y = 1 << j; j < i; j++, y <<= 1) {
  2269. if ((y -= c[j]) < 0) {
  2270. return Z_DATA_ERROR;
  2271. }
  2272. }
  2273. if ((y -= c[i]) < 0) {
  2274. return Z_DATA_ERROR;
  2275. }
  2276. c[i] += y;
  2277.  
  2278. // Generate starting offsets into the value table for each length
  2279. x[1] = j = 0;
  2280. p = 1;
  2281. xp = 2;
  2282. while (--i !== 0) { // note that i == g from above
  2283. x[xp] = (j += c[p]);
  2284. xp++;
  2285. p++;
  2286. }
  2287.  
  2288. // Make a table of values in order of bit lengths
  2289. i = 0;
  2290. p = 0;
  2291. do {
  2292. if ((j = b[bindex + p]) !== 0) {
  2293. v[x[j]++] = i;
  2294. }
  2295. p++;
  2296. } while (++i < n);
  2297. n = x[g]; // set n to length of v
  2298.  
  2299. // Generate the Huffman codes and for each, make the table entries
  2300. x[0] = i = 0; // first Huffman code is zero
  2301. p = 0; // grab values in bit order
  2302. h = -1; // no tables yet--level -1
  2303. w = -l; // bits decoded == (l * h)
  2304. u[0] = 0; // just to keep compilers happy
  2305. q = 0; // ditto
  2306. z = 0; // ditto
  2307.  
  2308. // go through the bit lengths (k already is bits in shortest code)
  2309. for (; k <= g; k++) {
  2310. a = c[k];
  2311. while (a-- !== 0) {
  2312. // here i is the Huffman code of length k bits for value *p
  2313. // make tables up to required level
  2314. while (k > w + l) {
  2315. h++;
  2316. w += l; // previous table always l bits
  2317. // compute minimum size table less than or equal to l bits
  2318. z = g - w;
  2319. z = (z > l) ? l : z; // table size upper limit
  2320. if ((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
  2321. // too few codes for
  2322. // k-w bit table
  2323. f -= a + 1; // deduct codes from patterns left
  2324. xp = k;
  2325. if (j < z) {
  2326. while (++j < z) { // try smaller tables up to z bits
  2327. if ((f <<= 1) <= c[++xp])
  2328. break; // enough codes to use up j bits
  2329. f -= c[xp]; // else deduct codes from patterns
  2330. }
  2331. }
  2332. }
  2333. z = 1 << j; // table entries for j-bit table
  2334.  
  2335. // allocate new table
  2336. if (hn[0] + z > MANY) { // (note: doesn't matter for fixed)
  2337. return Z_DATA_ERROR; // overflow of MANY
  2338. }
  2339. u[h] = q = /* hp+ */hn[0]; // DEBUG
  2340. hn[0] += z;
  2341.  
  2342. // connect to last table, if there is one
  2343. if (h !== 0) {
  2344. x[h] = i; // save pattern for backing up
  2345. r[0] = /* (byte) */j; // bits in this table
  2346. r[1] = /* (byte) */l; // bits to dump before this table
  2347. j = i >>> (w - l);
  2348. r[2] = /* (int) */(q - u[h - 1] - j); // offset to this table
  2349. hp.set(r, (u[h - 1] + j) * 3);
  2350. // to
  2351. // last
  2352. // table
  2353. } else {
  2354. t[0] = q; // first table is returned result
  2355. }
  2356. }
  2357.  
  2358. // set up table entry in r
  2359. r[1] = /* (byte) */(k - w);
  2360. if (p >= n) {
  2361. r[0] = 128 + 64; // out of values--invalid code
  2362. } else if (v[p] < s) {
  2363. r[0] = /* (byte) */(v[p] < 256 ? 0 : 32 + 64); // 256 is
  2364. // end-of-block
  2365. r[2] = v[p++]; // simple code is just the value
  2366. } else {
  2367. r[0] = /* (byte) */(e[v[p] - s] + 16 + 64); // non-simple--look
  2368. // up in lists
  2369. r[2] = d[v[p++] - s];
  2370. }
  2371.  
  2372. // fill code-like entries with r
  2373. f = 1 << (k - w);
  2374. for (j = i >>> w; j < z; j += f) {
  2375. hp.set(r, (q + j) * 3);
  2376. }
  2377.  
  2378. // backwards increment the k-bit code i
  2379. for (j = 1 << (k - 1); (i & j) !== 0; j >>>= 1) {
  2380. i ^= j;
  2381. }
  2382. i ^= j;
  2383.  
  2384. // backup over finished tables
  2385. mask = (1 << w) - 1; // needed on HP, cc -O bug
  2386. while ((i & mask) != x[h]) {
  2387. h--; // don't need to update q
  2388. w -= l;
  2389. mask = (1 << w) - 1;
  2390. }
  2391. }
  2392. }
  2393. // Return Z_BUF_ERROR if we were given an incomplete table
  2394. return y !== 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  2395. }
  2396.  
  2397. function initWorkArea(vsize) {
  2398. let i;
  2399. if (!hn) {
  2400. hn = []; // []; //new Array(1);
  2401. v = []; // new Array(vsize);
  2402. c = new Int32Array(BMAX + 1); // new Array(BMAX + 1);
  2403. r = []; // new Array(3);
  2404. u = new Int32Array(BMAX); // new Array(BMAX);
  2405. x = new Int32Array(BMAX + 1); // new Array(BMAX + 1);
  2406. }
  2407. if (v.length < vsize) {
  2408. v = []; // new Array(vsize);
  2409. }
  2410. for (i = 0; i < vsize; i++) {
  2411. v[i] = 0;
  2412. }
  2413. for (i = 0; i < BMAX + 1; i++) {
  2414. c[i] = 0;
  2415. }
  2416. for (i = 0; i < 3; i++) {
  2417. r[i] = 0;
  2418. }
  2419. // for(int i=0; i<BMAX; i++){u[i]=0;}
  2420. u.set(c.subarray(0, BMAX), 0);
  2421. // for(int i=0; i<BMAX+1; i++){x[i]=0;}
  2422. x.set(c.subarray(0, BMAX + 1), 0);
  2423. }
  2424.  
  2425. that.inflate_trees_bits = function (c, // 19 code lengths
  2426. bb, // bits tree desired/actual depth
  2427. tb, // bits tree result
  2428. hp, // space for trees
  2429. z // for messages
  2430. ) {
  2431. let result;
  2432. initWorkArea(19);
  2433. hn[0] = 0;
  2434. result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
  2435.  
  2436. if (result == Z_DATA_ERROR) {
  2437. z.msg = "oversubscribed dynamic bit lengths tree";
  2438. } else if (result == Z_BUF_ERROR || bb[0] === 0) {
  2439. z.msg = "incomplete dynamic bit lengths tree";
  2440. result = Z_DATA_ERROR;
  2441. }
  2442. return result;
  2443. };
  2444.  
  2445. that.inflate_trees_dynamic = function (nl, // number of literal/length codes
  2446. nd, // number of distance codes
  2447. c, // that many (total) code lengths
  2448. bl, // literal desired/actual bit depth
  2449. bd, // distance desired/actual bit depth
  2450. tl, // literal/length tree result
  2451. td, // distance tree result
  2452. hp, // space for trees
  2453. z // for messages
  2454. ) {
  2455. let result;
  2456.  
  2457. // build literal/length tree
  2458. initWorkArea(288);
  2459. hn[0] = 0;
  2460. result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
  2461. if (result != Z_OK || bl[0] === 0) {
  2462. if (result == Z_DATA_ERROR) {
  2463. z.msg = "oversubscribed literal/length tree";
  2464. } else if (result != Z_MEM_ERROR) {
  2465. z.msg = "incomplete literal/length tree";
  2466. result = Z_DATA_ERROR;
  2467. }
  2468. return result;
  2469. }
  2470.  
  2471. // build distance tree
  2472. initWorkArea(288);
  2473. result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
  2474.  
  2475. if (result != Z_OK || (bd[0] === 0 && nl > 257)) {
  2476. if (result == Z_DATA_ERROR) {
  2477. z.msg = "oversubscribed distance tree";
  2478. } else if (result == Z_BUF_ERROR) {
  2479. z.msg = "incomplete distance tree";
  2480. result = Z_DATA_ERROR;
  2481. } else if (result != Z_MEM_ERROR) {
  2482. z.msg = "empty distance tree with lengths";
  2483. result = Z_DATA_ERROR;
  2484. }
  2485. return result;
  2486. }
  2487.  
  2488. return Z_OK;
  2489. };
  2490.  
  2491. }
  2492.  
  2493. InfTree.inflate_trees_fixed = function (bl, // literal desired/actual bit depth
  2494. bd, // distance desired/actual bit depth
  2495. tl,// literal/length tree result
  2496. td// distance tree result
  2497. ) {
  2498. bl[0] = fixed_bl;
  2499. bd[0] = fixed_bd;
  2500. tl[0] = fixed_tl;
  2501. td[0] = fixed_td;
  2502. return Z_OK;
  2503. };
  2504.  
  2505. // InfCodes
  2506.  
  2507. // waiting for "i:"=input,
  2508. // "o:"=output,
  2509. // "x:"=nothing
  2510. const START = 0; // x: set up for LEN
  2511. const LEN = 1; // i: get length/literal/eob next
  2512. const LENEXT = 2; // i: getting length extra (have base)
  2513. const DIST = 3; // i: get distance next
  2514. const DISTEXT = 4;// i: getting distance extra
  2515. const COPY = 5; // o: copying bytes in win, waiting
  2516. // for space
  2517. const LIT = 6; // o: got literal, waiting for output
  2518. // space
  2519. const WASH = 7; // o: got eob, possibly still output
  2520. // waiting
  2521. const END = 8; // x: got eob and all data flushed
  2522. const BADCODE = 9;// x: got error
  2523.  
  2524. function InfCodes() {
  2525. const that = this;
  2526.  
  2527. let mode; // current inflate_codes mode
  2528.  
  2529. // mode dependent information
  2530. let len = 0;
  2531.  
  2532. let tree; // pointer into tree
  2533. let tree_index = 0;
  2534. let need = 0; // bits needed
  2535.  
  2536. let lit = 0;
  2537.  
  2538. // if EXT or COPY, where and how much
  2539. let get = 0; // bits to get for extra
  2540. let dist = 0; // distance back to copy from
  2541.  
  2542. let lbits = 0; // ltree bits decoded per branch
  2543. let dbits = 0; // dtree bits decoder per branch
  2544. let ltree; // literal/length/eob tree
  2545. let ltree_index = 0; // literal/length/eob tree
  2546. let dtree; // distance tree
  2547. let dtree_index = 0; // distance tree
  2548.  
  2549. // Called with number of bytes left to write in win at least 258
  2550. // (the maximum string length) and number of input bytes available
  2551. // at least ten. The ten bytes are six bytes for the longest length/
  2552. // distance pair plus four bytes for overloading the bit buffer.
  2553.  
  2554. function inflate_fast(bl, bd, tl, tl_index, td, td_index, s, z) {
  2555. let t; // temporary pointer
  2556. let tp; // temporary pointer
  2557. let tp_index; // temporary pointer
  2558. let e; // extra bits or operation
  2559. let b; // bit buffer
  2560. let k; // bits in bit buffer
  2561. let p; // input data pointer
  2562. let n; // bytes available there
  2563. let q; // output win write pointer
  2564. let m; // bytes to end of win or read pointer
  2565. let ml; // mask for literal/length tree
  2566. let md; // mask for distance tree
  2567. let c; // bytes to copy
  2568. let d; // distance back to copy from
  2569. let r; // copy source pointer
  2570.  
  2571. let tp_index_t_3; // (tp_index+t)*3
  2572.  
  2573. // load input, output, bit values
  2574. p = z.next_in_index;
  2575. n = z.avail_in;
  2576. b = s.bitb;
  2577. k = s.bitk;
  2578. q = s.write;
  2579. m = q < s.read ? s.read - q - 1 : s.end - q;
  2580.  
  2581. // initialize masks
  2582. ml = inflate_mask[bl];
  2583. md = inflate_mask[bd];
  2584.  
  2585. // do until not enough input or output space for fast loop
  2586. do { // assume called with m >= 258 && n >= 10
  2587. // get literal/length code
  2588. while (k < (20)) { // max bits for literal/length code
  2589. n--;
  2590. b |= (z.read_byte(p++) & 0xff) << k;
  2591. k += 8;
  2592. }
  2593.  
  2594. t = b & ml;
  2595. tp = tl;
  2596. tp_index = tl_index;
  2597. tp_index_t_3 = (tp_index + t) * 3;
  2598. if ((e = tp[tp_index_t_3]) === 0) {
  2599. b >>= (tp[tp_index_t_3 + 1]);
  2600. k -= (tp[tp_index_t_3 + 1]);
  2601.  
  2602. s.win[q++] = /* (byte) */tp[tp_index_t_3 + 2];
  2603. m--;
  2604. continue;
  2605. }
  2606. do {
  2607.  
  2608. b >>= (tp[tp_index_t_3 + 1]);
  2609. k -= (tp[tp_index_t_3 + 1]);
  2610.  
  2611. if ((e & 16) !== 0) {
  2612. e &= 15;
  2613. c = tp[tp_index_t_3 + 2] + (/* (int) */b & inflate_mask[e]);
  2614.  
  2615. b >>= e;
  2616. k -= e;
  2617.  
  2618. // decode distance base of block to copy
  2619. while (k < (15)) { // max bits for distance code
  2620. n--;
  2621. b |= (z.read_byte(p++) & 0xff) << k;
  2622. k += 8;
  2623. }
  2624.  
  2625. t = b & md;
  2626. tp = td;
  2627. tp_index = td_index;
  2628. tp_index_t_3 = (tp_index + t) * 3;
  2629. e = tp[tp_index_t_3];
  2630.  
  2631. do {
  2632.  
  2633. b >>= (tp[tp_index_t_3 + 1]);
  2634. k -= (tp[tp_index_t_3 + 1]);
  2635.  
  2636. if ((e & 16) !== 0) {
  2637. // get extra bits to add to distance base
  2638. e &= 15;
  2639. while (k < (e)) { // get extra bits (up to 13)
  2640. n--;
  2641. b |= (z.read_byte(p++) & 0xff) << k;
  2642. k += 8;
  2643. }
  2644.  
  2645. d = tp[tp_index_t_3 + 2] + (b & inflate_mask[e]);
  2646.  
  2647. b >>= (e);
  2648. k -= (e);
  2649.  
  2650. // do the copy
  2651. m -= c;
  2652. if (q >= d) { // offset before dest
  2653. // just copy
  2654. r = q - d;
  2655. if (q - r > 0 && 2 > (q - r)) {
  2656. s.win[q++] = s.win[r++]; // minimum
  2657. // count is
  2658. // three,
  2659. s.win[q++] = s.win[r++]; // so unroll
  2660. // loop a
  2661. // little
  2662. c -= 2;
  2663. } else {
  2664. s.win.set(s.win.subarray(r, r + 2), q);
  2665. q += 2;
  2666. r += 2;
  2667. c -= 2;
  2668. }
  2669. } else { // else offset after destination
  2670. r = q - d;
  2671. do {
  2672. r += s.end; // force pointer in win
  2673. } while (r < 0); // covers invalid distances
  2674. e = s.end - r;
  2675. if (c > e) { // if source crosses,
  2676. c -= e; // wrapped copy
  2677. if (q - r > 0 && e > (q - r)) {
  2678. do {
  2679. s.win[q++] = s.win[r++];
  2680. } while (--e !== 0);
  2681. } else {
  2682. s.win.set(s.win.subarray(r, r + e), q);
  2683. q += e;
  2684. r += e;
  2685. e = 0;
  2686. }
  2687. r = 0; // copy rest from start of win
  2688. }
  2689.  
  2690. }
  2691.  
  2692. // copy all or what's left
  2693. if (q - r > 0 && c > (q - r)) {
  2694. do {
  2695. s.win[q++] = s.win[r++];
  2696. } while (--c !== 0);
  2697. } else {
  2698. s.win.set(s.win.subarray(r, r + c), q);
  2699. q += c;
  2700. r += c;
  2701. c = 0;
  2702. }
  2703. break;
  2704. } else if ((e & 64) === 0) {
  2705. t += tp[tp_index_t_3 + 2];
  2706. t += (b & inflate_mask[e]);
  2707. tp_index_t_3 = (tp_index + t) * 3;
  2708. e = tp[tp_index_t_3];
  2709. } else {
  2710. z.msg = "invalid distance code";
  2711.  
  2712. c = z.avail_in - n;
  2713. c = (k >> 3) < c ? k >> 3 : c;
  2714. n += c;
  2715. p -= c;
  2716. k -= c << 3;
  2717.  
  2718. s.bitb = b;
  2719. s.bitk = k;
  2720. z.avail_in = n;
  2721. z.total_in += p - z.next_in_index;
  2722. z.next_in_index = p;
  2723. s.write = q;
  2724.  
  2725. return Z_DATA_ERROR;
  2726. }
  2727. // eslint-disable-next-line no-constant-condition
  2728. } while (true);
  2729. break;
  2730. }
  2731.  
  2732. if ((e & 64) === 0) {
  2733. t += tp[tp_index_t_3 + 2];
  2734. t += (b & inflate_mask[e]);
  2735. tp_index_t_3 = (tp_index + t) * 3;
  2736. if ((e = tp[tp_index_t_3]) === 0) {
  2737.  
  2738. b >>= (tp[tp_index_t_3 + 1]);
  2739. k -= (tp[tp_index_t_3 + 1]);
  2740.  
  2741. s.win[q++] = /* (byte) */tp[tp_index_t_3 + 2];
  2742. m--;
  2743. break;
  2744. }
  2745. } else if ((e & 32) !== 0) {
  2746.  
  2747. c = z.avail_in - n;
  2748. c = (k >> 3) < c ? k >> 3 : c;
  2749. n += c;
  2750. p -= c;
  2751. k -= c << 3;
  2752.  
  2753. s.bitb = b;
  2754. s.bitk = k;
  2755. z.avail_in = n;
  2756. z.total_in += p - z.next_in_index;
  2757. z.next_in_index = p;
  2758. s.write = q;
  2759.  
  2760. return Z_STREAM_END;
  2761. } else {
  2762. z.msg = "invalid literal/length code";
  2763.  
  2764. c = z.avail_in - n;
  2765. c = (k >> 3) < c ? k >> 3 : c;
  2766. n += c;
  2767. p -= c;
  2768. k -= c << 3;
  2769.  
  2770. s.bitb = b;
  2771. s.bitk = k;
  2772. z.avail_in = n;
  2773. z.total_in += p - z.next_in_index;
  2774. z.next_in_index = p;
  2775. s.write = q;
  2776.  
  2777. return Z_DATA_ERROR;
  2778. }
  2779. // eslint-disable-next-line no-constant-condition
  2780. } while (true);
  2781. } while (m >= 258 && n >= 10);
  2782.  
  2783. // not enough input or output--restore pointers and return
  2784. c = z.avail_in - n;
  2785. c = (k >> 3) < c ? k >> 3 : c;
  2786. n += c;
  2787. p -= c;
  2788. k -= c << 3;
  2789.  
  2790. s.bitb = b;
  2791. s.bitk = k;
  2792. z.avail_in = n;
  2793. z.total_in += p - z.next_in_index;
  2794. z.next_in_index = p;
  2795. s.write = q;
  2796.  
  2797. return Z_OK;
  2798. }
  2799.  
  2800. that.init = function (bl, bd, tl, tl_index, td, td_index) {
  2801. mode = START;
  2802. lbits = /* (byte) */bl;
  2803. dbits = /* (byte) */bd;
  2804. ltree = tl;
  2805. ltree_index = tl_index;
  2806. dtree = td;
  2807. dtree_index = td_index;
  2808. tree = null;
  2809. };
  2810.  
  2811. that.proc = function (s, z, r) {
  2812. let j; // temporary storage
  2813. let tindex; // temporary pointer
  2814. let e; // extra bits or operation
  2815. let b = 0; // bit buffer
  2816. let k = 0; // bits in bit buffer
  2817. let p = 0; // input data pointer
  2818. let n; // bytes available there
  2819. let q; // output win write pointer
  2820. let m; // bytes to end of win or read pointer
  2821. let f; // pointer to copy strings from
  2822.  
  2823. // copy input/output information to locals (UPDATE macro restores)
  2824. p = z.next_in_index;
  2825. n = z.avail_in;
  2826. b = s.bitb;
  2827. k = s.bitk;
  2828. q = s.write;
  2829. m = q < s.read ? s.read - q - 1 : s.end - q;
  2830.  
  2831. // process input and output based on current state
  2832. // eslint-disable-next-line no-constant-condition
  2833. while (true) {
  2834. switch (mode) {
  2835. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  2836. case START: // x: set up for LEN
  2837. if (m >= 258 && n >= 10) {
  2838.  
  2839. s.bitb = b;
  2840. s.bitk = k;
  2841. z.avail_in = n;
  2842. z.total_in += p - z.next_in_index;
  2843. z.next_in_index = p;
  2844. s.write = q;
  2845. r = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z);
  2846.  
  2847. p = z.next_in_index;
  2848. n = z.avail_in;
  2849. b = s.bitb;
  2850. k = s.bitk;
  2851. q = s.write;
  2852. m = q < s.read ? s.read - q - 1 : s.end - q;
  2853.  
  2854. if (r != Z_OK) {
  2855. mode = r == Z_STREAM_END ? WASH : BADCODE;
  2856. break;
  2857. }
  2858. }
  2859. need = lbits;
  2860. tree = ltree;
  2861. tree_index = ltree_index;
  2862.  
  2863. mode = LEN;
  2864. /* falls through */
  2865. case LEN: // i: get length/literal/eob next
  2866. j = need;
  2867.  
  2868. while (k < (j)) {
  2869. if (n !== 0)
  2870. r = Z_OK;
  2871. else {
  2872.  
  2873. s.bitb = b;
  2874. s.bitk = k;
  2875. z.avail_in = n;
  2876. z.total_in += p - z.next_in_index;
  2877. z.next_in_index = p;
  2878. s.write = q;
  2879. return s.inflate_flush(z, r);
  2880. }
  2881. n--;
  2882. b |= (z.read_byte(p++) & 0xff) << k;
  2883. k += 8;
  2884. }
  2885.  
  2886. tindex = (tree_index + (b & inflate_mask[j])) * 3;
  2887.  
  2888. b >>>= (tree[tindex + 1]);
  2889. k -= (tree[tindex + 1]);
  2890.  
  2891. e = tree[tindex];
  2892.  
  2893. if (e === 0) { // literal
  2894. lit = tree[tindex + 2];
  2895. mode = LIT;
  2896. break;
  2897. }
  2898. if ((e & 16) !== 0) { // length
  2899. get = e & 15;
  2900. len = tree[tindex + 2];
  2901. mode = LENEXT;
  2902. break;
  2903. }
  2904. if ((e & 64) === 0) { // next table
  2905. need = e;
  2906. tree_index = tindex / 3 + tree[tindex + 2];
  2907. break;
  2908. }
  2909. if ((e & 32) !== 0) { // end of block
  2910. mode = WASH;
  2911. break;
  2912. }
  2913. mode = BADCODE; // invalid code
  2914. z.msg = "invalid literal/length code";
  2915. r = Z_DATA_ERROR;
  2916.  
  2917. s.bitb = b;
  2918. s.bitk = k;
  2919. z.avail_in = n;
  2920. z.total_in += p - z.next_in_index;
  2921. z.next_in_index = p;
  2922. s.write = q;
  2923. return s.inflate_flush(z, r);
  2924.  
  2925. case LENEXT: // i: getting length extra (have base)
  2926. j = get;
  2927.  
  2928. while (k < (j)) {
  2929. if (n !== 0)
  2930. r = Z_OK;
  2931. else {
  2932.  
  2933. s.bitb = b;
  2934. s.bitk = k;
  2935. z.avail_in = n;
  2936. z.total_in += p - z.next_in_index;
  2937. z.next_in_index = p;
  2938. s.write = q;
  2939. return s.inflate_flush(z, r);
  2940. }
  2941. n--;
  2942. b |= (z.read_byte(p++) & 0xff) << k;
  2943. k += 8;
  2944. }
  2945.  
  2946. len += (b & inflate_mask[j]);
  2947.  
  2948. b >>= j;
  2949. k -= j;
  2950.  
  2951. need = dbits;
  2952. tree = dtree;
  2953. tree_index = dtree_index;
  2954. mode = DIST;
  2955. /* falls through */
  2956. case DIST: // i: get distance next
  2957. j = need;
  2958.  
  2959. while (k < (j)) {
  2960. if (n !== 0)
  2961. r = Z_OK;
  2962. else {
  2963.  
  2964. s.bitb = b;
  2965. s.bitk = k;
  2966. z.avail_in = n;
  2967. z.total_in += p - z.next_in_index;
  2968. z.next_in_index = p;
  2969. s.write = q;
  2970. return s.inflate_flush(z, r);
  2971. }
  2972. n--;
  2973. b |= (z.read_byte(p++) & 0xff) << k;
  2974. k += 8;
  2975. }
  2976.  
  2977. tindex = (tree_index + (b & inflate_mask[j])) * 3;
  2978.  
  2979. b >>= tree[tindex + 1];
  2980. k -= tree[tindex + 1];
  2981.  
  2982. e = (tree[tindex]);
  2983. if ((e & 16) !== 0) { // distance
  2984. get = e & 15;
  2985. dist = tree[tindex + 2];
  2986. mode = DISTEXT;
  2987. break;
  2988. }
  2989. if ((e & 64) === 0) { // next table
  2990. need = e;
  2991. tree_index = tindex / 3 + tree[tindex + 2];
  2992. break;
  2993. }
  2994. mode = BADCODE; // invalid code
  2995. z.msg = "invalid distance code";
  2996. r = Z_DATA_ERROR;
  2997.  
  2998. s.bitb = b;
  2999. s.bitk = k;
  3000. z.avail_in = n;
  3001. z.total_in += p - z.next_in_index;
  3002. z.next_in_index = p;
  3003. s.write = q;
  3004. return s.inflate_flush(z, r);
  3005.  
  3006. case DISTEXT: // i: getting distance extra
  3007. j = get;
  3008.  
  3009. while (k < (j)) {
  3010. if (n !== 0)
  3011. r = Z_OK;
  3012. else {
  3013.  
  3014. s.bitb = b;
  3015. s.bitk = k;
  3016. z.avail_in = n;
  3017. z.total_in += p - z.next_in_index;
  3018. z.next_in_index = p;
  3019. s.write = q;
  3020. return s.inflate_flush(z, r);
  3021. }
  3022. n--;
  3023. b |= (z.read_byte(p++) & 0xff) << k;
  3024. k += 8;
  3025. }
  3026.  
  3027. dist += (b & inflate_mask[j]);
  3028.  
  3029. b >>= j;
  3030. k -= j;
  3031.  
  3032. mode = COPY;
  3033. /* falls through */
  3034. case COPY: // o: copying bytes in win, waiting for space
  3035. f = q - dist;
  3036. while (f < 0) { // modulo win size-"while" instead
  3037. f += s.end; // of "if" handles invalid distances
  3038. }
  3039. while (len !== 0) {
  3040.  
  3041. if (m === 0) {
  3042. if (q == s.end && s.read !== 0) {
  3043. q = 0;
  3044. m = q < s.read ? s.read - q - 1 : s.end - q;
  3045. }
  3046. if (m === 0) {
  3047. s.write = q;
  3048. r = s.inflate_flush(z, r);
  3049. q = s.write;
  3050. m = q < s.read ? s.read - q - 1 : s.end - q;
  3051.  
  3052. if (q == s.end && s.read !== 0) {
  3053. q = 0;
  3054. m = q < s.read ? s.read - q - 1 : s.end - q;
  3055. }
  3056.  
  3057. if (m === 0) {
  3058. s.bitb = b;
  3059. s.bitk = k;
  3060. z.avail_in = n;
  3061. z.total_in += p - z.next_in_index;
  3062. z.next_in_index = p;
  3063. s.write = q;
  3064. return s.inflate_flush(z, r);
  3065. }
  3066. }
  3067. }
  3068.  
  3069. s.win[q++] = s.win[f++];
  3070. m--;
  3071.  
  3072. if (f == s.end)
  3073. f = 0;
  3074. len--;
  3075. }
  3076. mode = START;
  3077. break;
  3078. case LIT: // o: got literal, waiting for output space
  3079. if (m === 0) {
  3080. if (q == s.end && s.read !== 0) {
  3081. q = 0;
  3082. m = q < s.read ? s.read - q - 1 : s.end - q;
  3083. }
  3084. if (m === 0) {
  3085. s.write = q;
  3086. r = s.inflate_flush(z, r);
  3087. q = s.write;
  3088. m = q < s.read ? s.read - q - 1 : s.end - q;
  3089.  
  3090. if (q == s.end && s.read !== 0) {
  3091. q = 0;
  3092. m = q < s.read ? s.read - q - 1 : s.end - q;
  3093. }
  3094. if (m === 0) {
  3095. s.bitb = b;
  3096. s.bitk = k;
  3097. z.avail_in = n;
  3098. z.total_in += p - z.next_in_index;
  3099. z.next_in_index = p;
  3100. s.write = q;
  3101. return s.inflate_flush(z, r);
  3102. }
  3103. }
  3104. }
  3105. r = Z_OK;
  3106.  
  3107. s.win[q++] = /* (byte) */lit;
  3108. m--;
  3109.  
  3110. mode = START;
  3111. break;
  3112. case WASH: // o: got eob, possibly more output
  3113. if (k > 7) { // return unused byte, if any
  3114. k -= 8;
  3115. n++;
  3116. p--; // can always return one
  3117. }
  3118.  
  3119. s.write = q;
  3120. r = s.inflate_flush(z, r);
  3121. q = s.write;
  3122. m = q < s.read ? s.read - q - 1 : s.end - q;
  3123.  
  3124. if (s.read != s.write) {
  3125. s.bitb = b;
  3126. s.bitk = k;
  3127. z.avail_in = n;
  3128. z.total_in += p - z.next_in_index;
  3129. z.next_in_index = p;
  3130. s.write = q;
  3131. return s.inflate_flush(z, r);
  3132. }
  3133. mode = END;
  3134. /* falls through */
  3135. case END:
  3136. r = Z_STREAM_END;
  3137. s.bitb = b;
  3138. s.bitk = k;
  3139. z.avail_in = n;
  3140. z.total_in += p - z.next_in_index;
  3141. z.next_in_index = p;
  3142. s.write = q;
  3143. return s.inflate_flush(z, r);
  3144.  
  3145. case BADCODE: // x: got error
  3146.  
  3147. r = Z_DATA_ERROR;
  3148.  
  3149. s.bitb = b;
  3150. s.bitk = k;
  3151. z.avail_in = n;
  3152. z.total_in += p - z.next_in_index;
  3153. z.next_in_index = p;
  3154. s.write = q;
  3155. return s.inflate_flush(z, r);
  3156.  
  3157. default:
  3158. r = Z_STREAM_ERROR;
  3159.  
  3160. s.bitb = b;
  3161. s.bitk = k;
  3162. z.avail_in = n;
  3163. z.total_in += p - z.next_in_index;
  3164. z.next_in_index = p;
  3165. s.write = q;
  3166. return s.inflate_flush(z, r);
  3167. }
  3168. }
  3169. };
  3170.  
  3171. that.free = function () {
  3172. // ZFREE(z, c);
  3173. };
  3174.  
  3175. }
  3176.  
  3177. // InfBlocks
  3178.  
  3179. // Table for deflate from PKZIP's appnote.txt.
  3180. const border = [ // Order of the bit length code lengths
  3181. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
  3182.  
  3183. const TYPE = 0; // get type bits (3, including end bit)
  3184. const LENS = 1; // get lengths for stored
  3185. const STORED = 2;// processing stored block
  3186. const TABLE = 3; // get table lengths
  3187. const BTREE = 4; // get bit lengths tree for a dynamic
  3188. // block
  3189. const DTREE = 5; // get length, distance trees for a
  3190. // dynamic block
  3191. const CODES = 6; // processing fixed or dynamic block
  3192. const DRY = 7; // output remaining win bytes
  3193. const DONELOCKS = 8; // finished last block, done
  3194. const BADBLOCKS = 9; // ot a data error--stuck here
  3195.  
  3196. function InfBlocks(z, w) {
  3197. const that = this;
  3198.  
  3199. let mode = TYPE; // current inflate_block mode
  3200.  
  3201. let left = 0; // if STORED, bytes left to copy
  3202.  
  3203. let table = 0; // table lengths (14 bits)
  3204. let index = 0; // index into blens (or border)
  3205. let blens; // bit lengths of codes
  3206. const bb = [0]; // bit length tree depth
  3207. const tb = [0]; // bit length decoding tree
  3208.  
  3209. const codes = new InfCodes(); // if CODES, current state
  3210.  
  3211. let last = 0; // true if this block is the last block
  3212.  
  3213. let hufts = new Int32Array(MANY * 3); // single malloc for tree space
  3214. const check = 0; // check on output
  3215. const inftree = new InfTree();
  3216.  
  3217. that.bitk = 0; // bits in bit buffer
  3218. that.bitb = 0; // bit buffer
  3219. that.win = new Uint8Array(w); // sliding win
  3220. that.end = w; // one byte after sliding win
  3221. that.read = 0; // win read pointer
  3222. that.write = 0; // win write pointer
  3223.  
  3224. that.reset = function (z, c) {
  3225. if (c)
  3226. c[0] = check;
  3227. // if (mode == BTREE || mode == DTREE) {
  3228. // }
  3229. if (mode == CODES) {
  3230. codes.free(z);
  3231. }
  3232. mode = TYPE;
  3233. that.bitk = 0;
  3234. that.bitb = 0;
  3235. that.read = that.write = 0;
  3236. };
  3237.  
  3238. that.reset(z, null);
  3239.  
  3240. // copy as much as possible from the sliding win to the output area
  3241. that.inflate_flush = function (z, r) {
  3242. let n;
  3243. let p;
  3244. let q;
  3245.  
  3246. // local copies of source and destination pointers
  3247. p = z.next_out_index;
  3248. q = that.read;
  3249.  
  3250. // compute number of bytes to copy as far as end of win
  3251. n = /* (int) */((q <= that.write ? that.write : that.end) - q);
  3252. if (n > z.avail_out)
  3253. n = z.avail_out;
  3254. if (n !== 0 && r == Z_BUF_ERROR)
  3255. r = Z_OK;
  3256.  
  3257. // update counters
  3258. z.avail_out -= n;
  3259. z.total_out += n;
  3260.  
  3261. // copy as far as end of win
  3262. z.next_out.set(that.win.subarray(q, q + n), p);
  3263. p += n;
  3264. q += n;
  3265.  
  3266. // see if more to copy at beginning of win
  3267. if (q == that.end) {
  3268. // wrap pointers
  3269. q = 0;
  3270. if (that.write == that.end)
  3271. that.write = 0;
  3272.  
  3273. // compute bytes to copy
  3274. n = that.write - q;
  3275. if (n > z.avail_out)
  3276. n = z.avail_out;
  3277. if (n !== 0 && r == Z_BUF_ERROR)
  3278. r = Z_OK;
  3279.  
  3280. // update counters
  3281. z.avail_out -= n;
  3282. z.total_out += n;
  3283.  
  3284. // copy
  3285. z.next_out.set(that.win.subarray(q, q + n), p);
  3286. p += n;
  3287. q += n;
  3288. }
  3289.  
  3290. // update pointers
  3291. z.next_out_index = p;
  3292. that.read = q;
  3293.  
  3294. // done
  3295. return r;
  3296. };
  3297.  
  3298. that.proc = function (z, r) {
  3299. let t; // temporary storage
  3300. let b; // bit buffer
  3301. let k; // bits in bit buffer
  3302. let p; // input data pointer
  3303. let n; // bytes available there
  3304. let q; // output win write pointer
  3305. let m; // bytes to end of win or read pointer
  3306.  
  3307. let i;
  3308.  
  3309. // copy input/output information to locals (UPDATE macro restores)
  3310. // {
  3311. p = z.next_in_index;
  3312. n = z.avail_in;
  3313. b = that.bitb;
  3314. k = that.bitk;
  3315. // }
  3316. // {
  3317. q = that.write;
  3318. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  3319. // }
  3320.  
  3321. // process input based on current state
  3322. // DEBUG dtree
  3323. // eslint-disable-next-line no-constant-condition
  3324. while (true) {
  3325. let bl, bd, tl, td, bl_, bd_, tl_, td_;
  3326. switch (mode) {
  3327. case TYPE:
  3328.  
  3329. while (k < (3)) {
  3330. if (n !== 0) {
  3331. r = Z_OK;
  3332. } else {
  3333. that.bitb = b;
  3334. that.bitk = k;
  3335. z.avail_in = n;
  3336. z.total_in += p - z.next_in_index;
  3337. z.next_in_index = p;
  3338. that.write = q;
  3339. return that.inflate_flush(z, r);
  3340. }
  3341. n--;
  3342. b |= (z.read_byte(p++) & 0xff) << k;
  3343. k += 8;
  3344. }
  3345. t = /* (int) */(b & 7);
  3346. last = t & 1;
  3347.  
  3348. switch (t >>> 1) {
  3349. case 0: // stored
  3350. // {
  3351. b >>>= (3);
  3352. k -= (3);
  3353. // }
  3354. t = k & 7; // go to byte boundary
  3355.  
  3356. // {
  3357. b >>>= (t);
  3358. k -= (t);
  3359. // }
  3360. mode = LENS; // get length of stored block
  3361. break;
  3362. case 1: // fixed
  3363. // {
  3364. bl = []; // new Array(1);
  3365. bd = []; // new Array(1);
  3366. tl = [[]]; // new Array(1);
  3367. td = [[]]; // new Array(1);
  3368.  
  3369. InfTree.inflate_trees_fixed(bl, bd, tl, td);
  3370. codes.init(bl[0], bd[0], tl[0], 0, td[0], 0);
  3371. // }
  3372.  
  3373. // {
  3374. b >>>= (3);
  3375. k -= (3);
  3376. // }
  3377.  
  3378. mode = CODES;
  3379. break;
  3380. case 2: // dynamic
  3381.  
  3382. // {
  3383. b >>>= (3);
  3384. k -= (3);
  3385. // }
  3386.  
  3387. mode = TABLE;
  3388. break;
  3389. case 3: // illegal
  3390.  
  3391. // {
  3392. b >>>= (3);
  3393. k -= (3);
  3394. // }
  3395. mode = BADBLOCKS;
  3396. z.msg = "invalid block type";
  3397. r = Z_DATA_ERROR;
  3398.  
  3399. that.bitb = b;
  3400. that.bitk = k;
  3401. z.avail_in = n;
  3402. z.total_in += p - z.next_in_index;
  3403. z.next_in_index = p;
  3404. that.write = q;
  3405. return that.inflate_flush(z, r);
  3406. }
  3407. break;
  3408. case LENS:
  3409.  
  3410. while (k < (32)) {
  3411. if (n !== 0) {
  3412. r = Z_OK;
  3413. } else {
  3414. that.bitb = b;
  3415. that.bitk = k;
  3416. z.avail_in = n;
  3417. z.total_in += p - z.next_in_index;
  3418. z.next_in_index = p;
  3419. that.write = q;
  3420. return that.inflate_flush(z, r);
  3421. }
  3422. n--;
  3423. b |= (z.read_byte(p++) & 0xff) << k;
  3424. k += 8;
  3425. }
  3426.  
  3427. if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)) {
  3428. mode = BADBLOCKS;
  3429. z.msg = "invalid stored block lengths";
  3430. r = Z_DATA_ERROR;
  3431.  
  3432. that.bitb = b;
  3433. that.bitk = k;
  3434. z.avail_in = n;
  3435. z.total_in += p - z.next_in_index;
  3436. z.next_in_index = p;
  3437. that.write = q;
  3438. return that.inflate_flush(z, r);
  3439. }
  3440. left = (b & 0xffff);
  3441. b = k = 0; // dump bits
  3442. mode = left !== 0 ? STORED : (last !== 0 ? DRY : TYPE);
  3443. break;
  3444. case STORED:
  3445. if (n === 0) {
  3446. that.bitb = b;
  3447. that.bitk = k;
  3448. z.avail_in = n;
  3449. z.total_in += p - z.next_in_index;
  3450. z.next_in_index = p;
  3451. that.write = q;
  3452. return that.inflate_flush(z, r);
  3453. }
  3454.  
  3455. if (m === 0) {
  3456. if (q == that.end && that.read !== 0) {
  3457. q = 0;
  3458. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  3459. }
  3460. if (m === 0) {
  3461. that.write = q;
  3462. r = that.inflate_flush(z, r);
  3463. q = that.write;
  3464. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  3465. if (q == that.end && that.read !== 0) {
  3466. q = 0;
  3467. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  3468. }
  3469. if (m === 0) {
  3470. that.bitb = b;
  3471. that.bitk = k;
  3472. z.avail_in = n;
  3473. z.total_in += p - z.next_in_index;
  3474. z.next_in_index = p;
  3475. that.write = q;
  3476. return that.inflate_flush(z, r);
  3477. }
  3478. }
  3479. }
  3480. r = Z_OK;
  3481.  
  3482. t = left;
  3483. if (t > n)
  3484. t = n;
  3485. if (t > m)
  3486. t = m;
  3487. that.win.set(z.read_buf(p, t), q);
  3488. p += t;
  3489. n -= t;
  3490. q += t;
  3491. m -= t;
  3492. if ((left -= t) !== 0)
  3493. break;
  3494. mode = last !== 0 ? DRY : TYPE;
  3495. break;
  3496. case TABLE:
  3497.  
  3498. while (k < (14)) {
  3499. if (n !== 0) {
  3500. r = Z_OK;
  3501. } else {
  3502. that.bitb = b;
  3503. that.bitk = k;
  3504. z.avail_in = n;
  3505. z.total_in += p - z.next_in_index;
  3506. z.next_in_index = p;
  3507. that.write = q;
  3508. return that.inflate_flush(z, r);
  3509. }
  3510.  
  3511. n--;
  3512. b |= (z.read_byte(p++) & 0xff) << k;
  3513. k += 8;
  3514. }
  3515.  
  3516. table = t = (b & 0x3fff);
  3517. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
  3518. mode = BADBLOCKS;
  3519. z.msg = "too many length or distance symbols";
  3520. r = Z_DATA_ERROR;
  3521.  
  3522. that.bitb = b;
  3523. that.bitk = k;
  3524. z.avail_in = n;
  3525. z.total_in += p - z.next_in_index;
  3526. z.next_in_index = p;
  3527. that.write = q;
  3528. return that.inflate_flush(z, r);
  3529. }
  3530. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  3531. if (!blens || blens.length < t) {
  3532. blens = []; // new Array(t);
  3533. } else {
  3534. for (i = 0; i < t; i++) {
  3535. blens[i] = 0;
  3536. }
  3537. }
  3538.  
  3539. // {
  3540. b >>>= (14);
  3541. k -= (14);
  3542. // }
  3543.  
  3544. index = 0;
  3545. mode = BTREE;
  3546. /* falls through */
  3547. case BTREE:
  3548. while (index < 4 + (table >>> 10)) {
  3549. while (k < (3)) {
  3550. if (n !== 0) {
  3551. r = Z_OK;
  3552. } else {
  3553. that.bitb = b;
  3554. that.bitk = k;
  3555. z.avail_in = n;
  3556. z.total_in += p - z.next_in_index;
  3557. z.next_in_index = p;
  3558. that.write = q;
  3559. return that.inflate_flush(z, r);
  3560. }
  3561. n--;
  3562. b |= (z.read_byte(p++) & 0xff) << k;
  3563. k += 8;
  3564. }
  3565.  
  3566. blens[border[index++]] = b & 7;
  3567.  
  3568. // {
  3569. b >>>= (3);
  3570. k -= (3);
  3571. // }
  3572. }
  3573.  
  3574. while (index < 19) {
  3575. blens[border[index++]] = 0;
  3576. }
  3577.  
  3578. bb[0] = 7;
  3579. t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
  3580. if (t != Z_OK) {
  3581. r = t;
  3582. if (r == Z_DATA_ERROR) {
  3583. blens = null;
  3584. mode = BADBLOCKS;
  3585. }
  3586.  
  3587. that.bitb = b;
  3588. that.bitk = k;
  3589. z.avail_in = n;
  3590. z.total_in += p - z.next_in_index;
  3591. z.next_in_index = p;
  3592. that.write = q;
  3593. return that.inflate_flush(z, r);
  3594. }
  3595.  
  3596. index = 0;
  3597. mode = DTREE;
  3598. /* falls through */
  3599. case DTREE:
  3600. // eslint-disable-next-line no-constant-condition
  3601. while (true) {
  3602. t = table;
  3603. if (index >= 258 + (t & 0x1f) + ((t >> 5) & 0x1f)) {
  3604. break;
  3605. }
  3606.  
  3607. let j, c;
  3608.  
  3609. t = bb[0];
  3610.  
  3611. while (k < (t)) {
  3612. if (n !== 0) {
  3613. r = Z_OK;
  3614. } else {
  3615. that.bitb = b;
  3616. that.bitk = k;
  3617. z.avail_in = n;
  3618. z.total_in += p - z.next_in_index;
  3619. z.next_in_index = p;
  3620. that.write = q;
  3621. return that.inflate_flush(z, r);
  3622. }
  3623. n--;
  3624. b |= (z.read_byte(p++) & 0xff) << k;
  3625. k += 8;
  3626. }
  3627.  
  3628. // if (tb[0] == -1) {
  3629. // System.err.println("null...");
  3630. // }
  3631.  
  3632. t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
  3633. c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];
  3634.  
  3635. if (c < 16) {
  3636. b >>>= (t);
  3637. k -= (t);
  3638. blens[index++] = c;
  3639. } else { // c == 16..18
  3640. i = c == 18 ? 7 : c - 14;
  3641. j = c == 18 ? 11 : 3;
  3642.  
  3643. while (k < (t + i)) {
  3644. if (n !== 0) {
  3645. r = Z_OK;
  3646. } else {
  3647. that.bitb = b;
  3648. that.bitk = k;
  3649. z.avail_in = n;
  3650. z.total_in += p - z.next_in_index;
  3651. z.next_in_index = p;
  3652. that.write = q;
  3653. return that.inflate_flush(z, r);
  3654. }
  3655. n--;
  3656. b |= (z.read_byte(p++) & 0xff) << k;
  3657. k += 8;
  3658. }
  3659.  
  3660. b >>>= (t);
  3661. k -= (t);
  3662.  
  3663. j += (b & inflate_mask[i]);
  3664.  
  3665. b >>>= (i);
  3666. k -= (i);
  3667.  
  3668. i = index;
  3669. t = table;
  3670. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1)) {
  3671. blens = null;
  3672. mode = BADBLOCKS;
  3673. z.msg = "invalid bit length repeat";
  3674. r = Z_DATA_ERROR;
  3675.  
  3676. that.bitb = b;
  3677. that.bitk = k;
  3678. z.avail_in = n;
  3679. z.total_in += p - z.next_in_index;
  3680. z.next_in_index = p;
  3681. that.write = q;
  3682. return that.inflate_flush(z, r);
  3683. }
  3684.  
  3685. c = c == 16 ? blens[i - 1] : 0;
  3686. do {
  3687. blens[i++] = c;
  3688. } while (--j !== 0);
  3689. index = i;
  3690. }
  3691. }
  3692.  
  3693. tb[0] = -1;
  3694. // {
  3695. bl_ = []; // new Array(1);
  3696. bd_ = []; // new Array(1);
  3697. tl_ = []; // new Array(1);
  3698. td_ = []; // new Array(1);
  3699. bl_[0] = 9; // must be <= 9 for lookahead assumptions
  3700. bd_[0] = 6; // must be <= 9 for lookahead assumptions
  3701.  
  3702. t = table;
  3703. t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl_, bd_, tl_, td_, hufts, z);
  3704.  
  3705. if (t != Z_OK) {
  3706. if (t == Z_DATA_ERROR) {
  3707. blens = null;
  3708. mode = BADBLOCKS;
  3709. }
  3710. r = t;
  3711.  
  3712. that.bitb = b;
  3713. that.bitk = k;
  3714. z.avail_in = n;
  3715. z.total_in += p - z.next_in_index;
  3716. z.next_in_index = p;
  3717. that.write = q;
  3718. return that.inflate_flush(z, r);
  3719. }
  3720. codes.init(bl_[0], bd_[0], hufts, tl_[0], hufts, td_[0]);
  3721. // }
  3722. mode = CODES;
  3723. /* falls through */
  3724. case CODES:
  3725. that.bitb = b;
  3726. that.bitk = k;
  3727. z.avail_in = n;
  3728. z.total_in += p - z.next_in_index;
  3729. z.next_in_index = p;
  3730. that.write = q;
  3731.  
  3732. if ((r = codes.proc(that, z, r)) != Z_STREAM_END) {
  3733. return that.inflate_flush(z, r);
  3734. }
  3735. r = Z_OK;
  3736. codes.free(z);
  3737.  
  3738. p = z.next_in_index;
  3739. n = z.avail_in;
  3740. b = that.bitb;
  3741. k = that.bitk;
  3742. q = that.write;
  3743. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  3744.  
  3745. if (last === 0) {
  3746. mode = TYPE;
  3747. break;
  3748. }
  3749. mode = DRY;
  3750. /* falls through */
  3751. case DRY:
  3752. that.write = q;
  3753. r = that.inflate_flush(z, r);
  3754. q = that.write;
  3755. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  3756. if (that.read != that.write) {
  3757. that.bitb = b;
  3758. that.bitk = k;
  3759. z.avail_in = n;
  3760. z.total_in += p - z.next_in_index;
  3761. z.next_in_index = p;
  3762. that.write = q;
  3763. return that.inflate_flush(z, r);
  3764. }
  3765. mode = DONELOCKS;
  3766. /* falls through */
  3767. case DONELOCKS:
  3768. r = Z_STREAM_END;
  3769.  
  3770. that.bitb = b;
  3771. that.bitk = k;
  3772. z.avail_in = n;
  3773. z.total_in += p - z.next_in_index;
  3774. z.next_in_index = p;
  3775. that.write = q;
  3776. return that.inflate_flush(z, r);
  3777. case BADBLOCKS:
  3778. r = Z_DATA_ERROR;
  3779.  
  3780. that.bitb = b;
  3781. that.bitk = k;
  3782. z.avail_in = n;
  3783. z.total_in += p - z.next_in_index;
  3784. z.next_in_index = p;
  3785. that.write = q;
  3786. return that.inflate_flush(z, r);
  3787.  
  3788. default:
  3789. r = Z_STREAM_ERROR;
  3790.  
  3791. that.bitb = b;
  3792. that.bitk = k;
  3793. z.avail_in = n;
  3794. z.total_in += p - z.next_in_index;
  3795. z.next_in_index = p;
  3796. that.write = q;
  3797. return that.inflate_flush(z, r);
  3798. }
  3799. }
  3800. };
  3801.  
  3802. that.free = function (z) {
  3803. that.reset(z, null);
  3804. that.win = null;
  3805. hufts = null;
  3806. // ZFREE(z, s);
  3807. };
  3808.  
  3809. that.set_dictionary = function (d, start, n) {
  3810. that.win.set(d.subarray(start, start + n), 0);
  3811. that.read = that.write = n;
  3812. };
  3813.  
  3814. // Returns true if inflate is currently at the end of a block generated
  3815. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  3816. that.sync_point = function () {
  3817. return mode == LENS ? 1 : 0;
  3818. };
  3819.  
  3820. }
  3821.  
  3822. // Inflate
  3823.  
  3824. // preset dictionary flag in zlib header
  3825. const PRESET_DICT = 0x20;
  3826.  
  3827. const Z_DEFLATED = 8;
  3828.  
  3829. const METHOD = 0; // waiting for method byte
  3830. const FLAG = 1; // waiting for flag byte
  3831. const DICT4 = 2; // four dictionary check bytes to go
  3832. const DICT3 = 3; // three dictionary check bytes to go
  3833. const DICT2 = 4; // two dictionary check bytes to go
  3834. const DICT1 = 5; // one dictionary check byte to go
  3835. const DICT0 = 6; // waiting for inflateSetDictionary
  3836. const BLOCKS = 7; // decompressing blocks
  3837. const DONE = 12; // finished check, done
  3838. const BAD = 13; // got an error--stay here
  3839.  
  3840. const mark = [0, 0, 0xff, 0xff];
  3841.  
  3842. function Inflate$1() {
  3843. const that = this;
  3844.  
  3845. that.mode = 0; // current inflate mode
  3846.  
  3847. // mode dependent information
  3848. that.method = 0; // if FLAGS, method byte
  3849.  
  3850. // if CHECK, check values to compare
  3851. that.was = [0]; // new Array(1); // computed check value
  3852. that.need = 0; // stream check value
  3853.  
  3854. // if BAD, inflateSync's marker bytes count
  3855. that.marker = 0;
  3856.  
  3857. // mode independent information
  3858. that.wbits = 0; // log2(win size) (8..15, defaults to 15)
  3859.  
  3860. // this.blocks; // current inflate_blocks state
  3861.  
  3862. function inflateReset(z) {
  3863. if (!z || !z.istate)
  3864. return Z_STREAM_ERROR;
  3865.  
  3866. z.total_in = z.total_out = 0;
  3867. z.msg = null;
  3868. z.istate.mode = BLOCKS;
  3869. z.istate.blocks.reset(z, null);
  3870. return Z_OK;
  3871. }
  3872.  
  3873. that.inflateEnd = function (z) {
  3874. if (that.blocks)
  3875. that.blocks.free(z);
  3876. that.blocks = null;
  3877. // ZFREE(z, z->state);
  3878. return Z_OK;
  3879. };
  3880.  
  3881. that.inflateInit = function (z, w) {
  3882. z.msg = null;
  3883. that.blocks = null;
  3884.  
  3885. // set win size
  3886. if (w < 8 || w > 15) {
  3887. that.inflateEnd(z);
  3888. return Z_STREAM_ERROR;
  3889. }
  3890. that.wbits = w;
  3891.  
  3892. z.istate.blocks = new InfBlocks(z, 1 << w);
  3893.  
  3894. // reset state
  3895. inflateReset(z);
  3896. return Z_OK;
  3897. };
  3898.  
  3899. that.inflate = function (z, f) {
  3900. let r;
  3901. let b;
  3902.  
  3903. if (!z || !z.istate || !z.next_in)
  3904. return Z_STREAM_ERROR;
  3905. const istate = z.istate;
  3906. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  3907. r = Z_BUF_ERROR;
  3908. // eslint-disable-next-line no-constant-condition
  3909. while (true) {
  3910. switch (istate.mode) {
  3911. case METHOD:
  3912.  
  3913. if (z.avail_in === 0)
  3914. return r;
  3915. r = f;
  3916.  
  3917. z.avail_in--;
  3918. z.total_in++;
  3919. if (((istate.method = z.read_byte(z.next_in_index++)) & 0xf) != Z_DEFLATED) {
  3920. istate.mode = BAD;
  3921. z.msg = "unknown compression method";
  3922. istate.marker = 5; // can't try inflateSync
  3923. break;
  3924. }
  3925. if ((istate.method >> 4) + 8 > istate.wbits) {
  3926. istate.mode = BAD;
  3927. z.msg = "invalid win size";
  3928. istate.marker = 5; // can't try inflateSync
  3929. break;
  3930. }
  3931. istate.mode = FLAG;
  3932. /* falls through */
  3933. case FLAG:
  3934.  
  3935. if (z.avail_in === 0)
  3936. return r;
  3937. r = f;
  3938.  
  3939. z.avail_in--;
  3940. z.total_in++;
  3941. b = (z.read_byte(z.next_in_index++)) & 0xff;
  3942.  
  3943. if ((((istate.method << 8) + b) % 31) !== 0) {
  3944. istate.mode = BAD;
  3945. z.msg = "incorrect header check";
  3946. istate.marker = 5; // can't try inflateSync
  3947. break;
  3948. }
  3949.  
  3950. if ((b & PRESET_DICT) === 0) {
  3951. istate.mode = BLOCKS;
  3952. break;
  3953. }
  3954. istate.mode = DICT4;
  3955. /* falls through */
  3956. case DICT4:
  3957.  
  3958. if (z.avail_in === 0)
  3959. return r;
  3960. r = f;
  3961.  
  3962. z.avail_in--;
  3963. z.total_in++;
  3964. istate.need = ((z.read_byte(z.next_in_index++) & 0xff) << 24) & 0xff000000;
  3965. istate.mode = DICT3;
  3966. /* falls through */
  3967. case DICT3:
  3968.  
  3969. if (z.avail_in === 0)
  3970. return r;
  3971. r = f;
  3972.  
  3973. z.avail_in--;
  3974. z.total_in++;
  3975. istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 16) & 0xff0000;
  3976. istate.mode = DICT2;
  3977. /* falls through */
  3978. case DICT2:
  3979.  
  3980. if (z.avail_in === 0)
  3981. return r;
  3982. r = f;
  3983.  
  3984. z.avail_in--;
  3985. z.total_in++;
  3986. istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 8) & 0xff00;
  3987. istate.mode = DICT1;
  3988. /* falls through */
  3989. case DICT1:
  3990.  
  3991. if (z.avail_in === 0)
  3992. return r;
  3993. r = f;
  3994.  
  3995. z.avail_in--;
  3996. z.total_in++;
  3997. istate.need += (z.read_byte(z.next_in_index++) & 0xff);
  3998. istate.mode = DICT0;
  3999. return Z_NEED_DICT;
  4000. case DICT0:
  4001. istate.mode = BAD;
  4002. z.msg = "need dictionary";
  4003. istate.marker = 0; // can try inflateSync
  4004. return Z_STREAM_ERROR;
  4005. case BLOCKS:
  4006.  
  4007. r = istate.blocks.proc(z, r);
  4008. if (r == Z_DATA_ERROR) {
  4009. istate.mode = BAD;
  4010. istate.marker = 0; // can try inflateSync
  4011. break;
  4012. }
  4013. if (r == Z_OK) {
  4014. r = f;
  4015. }
  4016. if (r != Z_STREAM_END) {
  4017. return r;
  4018. }
  4019. r = f;
  4020. istate.blocks.reset(z, istate.was);
  4021. istate.mode = DONE;
  4022. /* falls through */
  4023. case DONE:
  4024. z.avail_in = 0;
  4025. return Z_STREAM_END;
  4026. case BAD:
  4027. return Z_DATA_ERROR;
  4028. default:
  4029. return Z_STREAM_ERROR;
  4030. }
  4031. }
  4032. };
  4033.  
  4034. that.inflateSetDictionary = function (z, dictionary, dictLength) {
  4035. let index = 0, length = dictLength;
  4036. if (!z || !z.istate || z.istate.mode != DICT0)
  4037. return Z_STREAM_ERROR;
  4038. const istate = z.istate;
  4039. if (length >= (1 << istate.wbits)) {
  4040. length = (1 << istate.wbits) - 1;
  4041. index = dictLength - length;
  4042. }
  4043. istate.blocks.set_dictionary(dictionary, index, length);
  4044. istate.mode = BLOCKS;
  4045. return Z_OK;
  4046. };
  4047.  
  4048. that.inflateSync = function (z) {
  4049. let n; // number of bytes to look at
  4050. let p; // pointer to bytes
  4051. let m; // number of marker bytes found in a row
  4052. let r, w; // temporaries to save total_in and total_out
  4053.  
  4054. // set up
  4055. if (!z || !z.istate)
  4056. return Z_STREAM_ERROR;
  4057. const istate = z.istate;
  4058. if (istate.mode != BAD) {
  4059. istate.mode = BAD;
  4060. istate.marker = 0;
  4061. }
  4062. if ((n = z.avail_in) === 0)
  4063. return Z_BUF_ERROR;
  4064. p = z.next_in_index;
  4065. m = istate.marker;
  4066.  
  4067. // search
  4068. while (n !== 0 && m < 4) {
  4069. if (z.read_byte(p) == mark[m]) {
  4070. m++;
  4071. } else if (z.read_byte(p) !== 0) {
  4072. m = 0;
  4073. } else {
  4074. m = 4 - m;
  4075. }
  4076. p++;
  4077. n--;
  4078. }
  4079.  
  4080. // restore
  4081. z.total_in += p - z.next_in_index;
  4082. z.next_in_index = p;
  4083. z.avail_in = n;
  4084. istate.marker = m;
  4085.  
  4086. // return no joy or set up to restart on a new block
  4087. if (m != 4) {
  4088. return Z_DATA_ERROR;
  4089. }
  4090. r = z.total_in;
  4091. w = z.total_out;
  4092. inflateReset(z);
  4093. z.total_in = r;
  4094. z.total_out = w;
  4095. istate.mode = BLOCKS;
  4096. return Z_OK;
  4097. };
  4098.  
  4099. // Returns true if inflate is currently at the end of a block generated
  4100. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  4101. // implementation to provide an additional safety check. PPP uses
  4102. // Z_SYNC_FLUSH
  4103. // but removes the length bytes of the resulting empty stored block. When
  4104. // decompressing, PPP checks that at the end of input packet, inflate is
  4105. // waiting for these length bytes.
  4106. that.inflateSyncPoint = function (z) {
  4107. if (!z || !z.istate || !z.istate.blocks)
  4108. return Z_STREAM_ERROR;
  4109. return z.istate.blocks.sync_point();
  4110. };
  4111. }
  4112.  
  4113. // ZStream
  4114.  
  4115. function ZStream() {
  4116. }
  4117.  
  4118. ZStream.prototype = {
  4119. inflateInit: function (bits) {
  4120. const that = this;
  4121. that.istate = new Inflate$1();
  4122. if (!bits)
  4123. bits = MAX_BITS;
  4124. return that.istate.inflateInit(that, bits);
  4125. },
  4126.  
  4127. inflate: function (f) {
  4128. const that = this;
  4129. if (!that.istate)
  4130. return Z_STREAM_ERROR;
  4131. return that.istate.inflate(that, f);
  4132. },
  4133.  
  4134. inflateEnd: function () {
  4135. const that = this;
  4136. if (!that.istate)
  4137. return Z_STREAM_ERROR;
  4138. const ret = that.istate.inflateEnd(that);
  4139. that.istate = null;
  4140. return ret;
  4141. },
  4142.  
  4143. inflateSync: function () {
  4144. const that = this;
  4145. if (!that.istate)
  4146. return Z_STREAM_ERROR;
  4147. return that.istate.inflateSync(that);
  4148. },
  4149. inflateSetDictionary: function (dictionary, dictLength) {
  4150. const that = this;
  4151. if (!that.istate)
  4152. return Z_STREAM_ERROR;
  4153. return that.istate.inflateSetDictionary(that, dictionary, dictLength);
  4154. },
  4155. read_byte: function (start) {
  4156. const that = this;
  4157. return that.next_in[start];
  4158. },
  4159. read_buf: function (start, size) {
  4160. const that = this;
  4161. return that.next_in.subarray(start, start + size);
  4162. }
  4163. };
  4164.  
  4165. // Inflater
  4166.  
  4167. function ZipInflate(options) {
  4168. const that = this;
  4169. const z = new ZStream();
  4170. const bufsize = options && options.chunkSize ? Math.floor(options.chunkSize * 2) : 128 * 1024;
  4171. const flush = Z_NO_FLUSH;
  4172. const buf = new Uint8Array(bufsize);
  4173. let nomoreinput = false;
  4174.  
  4175. z.inflateInit();
  4176. z.next_out = buf;
  4177.  
  4178. that.append = function (data, onprogress) {
  4179. const buffers = [];
  4180. let err, array, lastIndex = 0, bufferIndex = 0, bufferSize = 0;
  4181. if (data.length === 0)
  4182. return;
  4183. z.next_in_index = 0;
  4184. z.next_in = data;
  4185. z.avail_in = data.length;
  4186. do {
  4187. z.next_out_index = 0;
  4188. z.avail_out = bufsize;
  4189. if ((z.avail_in === 0) && (!nomoreinput)) { // if buffer is empty and more input is available, refill it
  4190. z.next_in_index = 0;
  4191. nomoreinput = true;
  4192. }
  4193. err = z.inflate(flush);
  4194. if (nomoreinput && (err === Z_BUF_ERROR)) {
  4195. if (z.avail_in !== 0)
  4196. throw new Error("inflating: bad input");
  4197. } else if (err !== Z_OK && err !== Z_STREAM_END)
  4198. throw new Error("inflating: " + z.msg);
  4199. if ((nomoreinput || err === Z_STREAM_END) && (z.avail_in === data.length))
  4200. throw new Error("inflating: bad input");
  4201. if (z.next_out_index)
  4202. if (z.next_out_index === bufsize)
  4203. buffers.push(new Uint8Array(buf));
  4204. else
  4205. buffers.push(buf.slice(0, z.next_out_index));
  4206. bufferSize += z.next_out_index;
  4207. if (onprogress && z.next_in_index > 0 && z.next_in_index != lastIndex) {
  4208. onprogress(z.next_in_index);
  4209. lastIndex = z.next_in_index;
  4210. }
  4211. } while (z.avail_in > 0 || z.avail_out === 0);
  4212. if (buffers.length > 1) {
  4213. array = new Uint8Array(bufferSize);
  4214. buffers.forEach(function (chunk) {
  4215. array.set(chunk, bufferIndex);
  4216. bufferIndex += chunk.length;
  4217. });
  4218. } else {
  4219. array = buffers[0] || new Uint8Array(0);
  4220. }
  4221. return array;
  4222. };
  4223. that.flush = function () {
  4224. z.inflateEnd();
  4225. };
  4226. }
  4227.  
  4228. /*
  4229. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  4230.  
  4231. Redistribution and use in source and binary forms, with or without
  4232. modification, are permitted provided that the following conditions are met:
  4233.  
  4234. 1. Redistributions of source code must retain the above copyright notice,
  4235. this list of conditions and the following disclaimer.
  4236.  
  4237. 2. Redistributions in binary form must reproduce the above copyright
  4238. notice, this list of conditions and the following disclaimer in
  4239. the documentation and/or other materials provided with the distribution.
  4240.  
  4241. 3. The names of the authors may not be used to endorse or promote products
  4242. derived from this software without specific prior written permission.
  4243.  
  4244. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  4245. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  4246. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  4247. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  4248. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4249. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  4250. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4251. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4252. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4253. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4254. */
  4255.  
  4256. /* global navigator */
  4257.  
  4258. const DEFAULT_CONFIGURATION = {
  4259. chunkSize: 512 * 1024,
  4260. maxWorkers: (typeof navigator != "undefined" && navigator.hardwareConcurrency) || 2,
  4261. terminateWorkerTimeout: 5000,
  4262. useWebWorkers: true,
  4263. workerScripts: undefined
  4264. };
  4265.  
  4266. const config = Object.assign({}, DEFAULT_CONFIGURATION);
  4267.  
  4268. function getConfiguration() {
  4269. return config;
  4270. }
  4271.  
  4272. function configure(configuration) {
  4273. if (configuration.baseURL !== undefined) {
  4274. config.baseURL = configuration.baseURL;
  4275. }
  4276. if (configuration.chunkSize !== undefined) {
  4277. config.chunkSize = configuration.chunkSize;
  4278. }
  4279. if (configuration.maxWorkers !== undefined) {
  4280. config.maxWorkers = configuration.maxWorkers;
  4281. }
  4282. if (configuration.terminateWorkerTimeout !== undefined) {
  4283. config.terminateWorkerTimeout = configuration.terminateWorkerTimeout;
  4284. }
  4285. if (configuration.useWebWorkers !== undefined) {
  4286. config.useWebWorkers = configuration.useWebWorkers;
  4287. }
  4288. if (configuration.Deflate !== undefined) {
  4289. config.Deflate = configuration.Deflate;
  4290. }
  4291. if (configuration.Inflate !== undefined) {
  4292. config.Inflate = configuration.Inflate;
  4293. }
  4294. if (configuration.workerScripts !== undefined) {
  4295. if (configuration.workerScripts.deflate) {
  4296. if (!Array.isArray(configuration.workerScripts.deflate)) {
  4297. throw new Error("workerScripts.deflate must be an array");
  4298. }
  4299. if (!config.workerScripts) {
  4300. config.workerScripts = {};
  4301. }
  4302. config.workerScripts.deflate = configuration.workerScripts.deflate;
  4303. }
  4304. if (configuration.workerScripts.inflate) {
  4305. if (!Array.isArray(configuration.workerScripts.inflate)) {
  4306. throw new Error("workerScripts.inflate must be an array");
  4307. }
  4308. if (!config.workerScripts) {
  4309. config.workerScripts = {};
  4310. }
  4311. config.workerScripts.inflate = configuration.workerScripts.inflate;
  4312. }
  4313. }
  4314. }
  4315.  
  4316. var t=t=>{if("function"==typeof URL.createObjectURL){const e=()=>URL.createObjectURL(new Blob(['const{Array:t,Object:e,Math:n,Error:r,Uint8Array:i,Uint16Array:s,Uint32Array:o,Int32Array:f,DataView:c,TextEncoder:l,crypto:u,postMessage:a}=globalThis,w=[];for(let t=0;256>t;t++){let e=t;for(let t=0;8>t;t++)1&e?e=e>>>1^3988292384:e>>>=1;w[t]=e}class h{constructor(t){this.t=t||-1}append(t){let e=0|this.t;for(let n=0,r=0|t.length;r>n;n++)e=e>>>8^w[255&(e^t[n])];this.t=e}get(){return~this.t}}const d={concat(t,e){if(0===t.length||0===e.length)return t.concat(e);const n=t[t.length-1],r=d.i(n);return 32===r?t.concat(e):d.o(e,r,0|n,t.slice(0,t.length-1))},l(t){const e=t.length;if(0===e)return 0;const n=t[e-1];return 32*(e-1)+d.i(n)},u(t,e){if(32*t.length<e)return t;const r=(t=t.slice(0,n.ceil(e/32))).length;return e&=31,r>0&&e&&(t[r-1]=d.h(e,t[r-1]&2147483648>>e-1,1)),t},h:(t,e,n)=>32===t?e:(n?0|e:e<<32-t)+1099511627776*t,i:t=>n.round(t/1099511627776)||32,o(t,e,n,r){for(void 0===r&&(r=[]);e>=32;e-=32)r.push(n),n=0;if(0===e)return r.concat(t);for(let i=0;i<t.length;i++)r.push(n|t[i]>>>e),n=t[i]<<32-e;const i=t.length?t[t.length-1]:0,s=d.i(i);return r.push(d.h(e+s&31,e+s>32?n:r.pop(),1)),r}},p={p:{k(t){const e=d.l(t)/8,n=new i(e);let r;for(let i=0;e>i;i++)0==(3&i)&&(r=t[i/4]),n[i]=r>>>24,r<<=8;return n},g(t){const e=[];let n,r=0;for(n=0;n<t.length;n++)r=r<<8|t[n],3==(3&n)&&(e.push(r),r=0);return 3&n&&e.push(d.h(8*(3&n),r)),e}}},b={v:function(t){t?(this.m=t.m.slice(0),this.S=t.S.slice(0),this._=t._):this.reset()}};b.v.prototype={blockSize:512,reset:function(){const t=this;return t.m=this.I.slice(0),t.S=[],t._=0,t},update:function(t){const e=this;"string"==typeof t&&(t=p.C.g(t));const n=e.S=d.concat(e.S,t),i=e._,s=e._=i+d.l(t);if(s>9007199254740991)throw new r("Cannot hash more than 2^53 - 1 bits");const f=new o(n);let c=0;for(let t=e.blockSize+i-(e.blockSize+i&e.blockSize-1);s>=t;t+=e.blockSize)e.A(f.subarray(16*c,16*(c+1))),c+=1;return n.splice(0,16*c),e},V:function(){const t=this;let e=t.S;const r=t.m;e=d.concat(e,[d.h(1,1)]);for(let t=e.length+2;15&t;t++)e.push(0);for(e.push(n.floor(t._/4294967296)),e.push(0|t._);e.length;)t.A(e.splice(0,16));return t.reset(),r},I:[1732584193,4023233417,2562383102,271733878,3285377520],B:[1518500249,1859775393,2400959708,3395469782],D:(t,e,n,r)=>t>19?t>39?t>59?t>79?void 0:e^n^r:e&n|e&r|n&r:e^n^r:e&n|~e&r,U:(t,e)=>e<<t|e>>>32-t,A:function(e){const r=this,i=r.m,s=t(80);for(let t=0;16>t;t++)s[t]=e[t];let o=i[0],f=i[1],c=i[2],l=i[3],u=i[4];for(let t=0;79>=t;t++){16>t||(s[t]=r.U(1,s[t-3]^s[t-8]^s[t-14]^s[t-16]));const e=r.U(5,o)+r.D(t,f,c,l)+u+s[t]+r.B[n.floor(t/20)]|0;u=l,l=c,c=r.U(30,f),f=o,o=e}i[0]=i[0]+o|0,i[1]=i[1]+f|0,i[2]=i[2]+c|0,i[3]=i[3]+l|0,i[4]=i[4]+u|0}};const y={name:"PBKDF2"},k=e.assign({hash:{name:"HMAC"}},y),g=e.assign({iterations:1e3,hash:{name:"SHA-1"}},y),v=["deriveBits"],m=[8,12,16],z=[16,24,32],S=[0,0,0,0],_=p.p,I=class{constructor(t){const e=this;e.M=[[[],[],[],[],[]],[[],[],[],[],[]]],e.M[0][0][0]||e.P();const n=e.M[0][4],i=e.M[1],s=t.length;let o,f,c,l=1;if(4!==s&&6!==s&&8!==s)throw new r("invalid aes key size");for(e.B=[f=t.slice(0),c=[]],o=s;4*s+28>o;o++){let t=f[o-1];(o%s==0||8===s&&o%s==4)&&(t=n[t>>>24]<<24^n[t>>16&255]<<16^n[t>>8&255]<<8^n[255&t],o%s==0&&(t=t<<8^t>>>24^l<<24,l=l<<1^283*(l>>7))),f[o]=f[o-s]^t}for(let t=0;o;t++,o--){const e=f[3&t?o:o-4];c[t]=4>=o||4>t?e:i[0][n[e>>>24]]^i[1][n[e>>16&255]]^i[2][n[e>>8&255]]^i[3][n[255&e]]}}encrypt(t){return this.H(t,0)}decrypt(t){return this.H(t,1)}P(){const t=this.M[0],e=this.M[1],n=t[4],r=e[4],i=[],s=[];let o,f,c,l;for(let t=0;256>t;t++)s[(i[t]=t<<1^283*(t>>7))^t]=t;for(let u=o=0;!n[u];u^=f||1,o=s[o]||1){let s=o^o<<1^o<<2^o<<3^o<<4;s=s>>8^255&s^99,n[u]=s,r[s]=u,l=i[c=i[f=i[u]]];let a=16843009*l^65537*c^257*f^16843008*u,w=257*i[s]^16843008*s;for(let n=0;4>n;n++)t[n][u]=w=w<<24^w>>>8,e[n][s]=a=a<<24^a>>>8}for(let n=0;5>n;n++)t[n]=t[n].slice(0),e[n]=e[n].slice(0)}H(t,e){if(4!==t.length)throw new r("invalid aes block size");const n=this.B[e],i=n.length/4-2,s=[0,0,0,0],o=this.M[e],f=o[0],c=o[1],l=o[2],u=o[3],a=o[4];let w,h,d,p=t[0]^n[0],b=t[e?3:1]^n[1],y=t[2]^n[2],k=t[e?1:3]^n[3],g=4;for(let t=0;i>t;t++)w=f[p>>>24]^c[b>>16&255]^l[y>>8&255]^u[255&k]^n[g],h=f[b>>>24]^c[y>>16&255]^l[k>>8&255]^u[255&p]^n[g+1],d=f[y>>>24]^c[k>>16&255]^l[p>>8&255]^u[255&b]^n[g+2],k=f[k>>>24]^c[p>>16&255]^l[b>>8&255]^u[255&y]^n[g+3],g+=4,p=w,b=h,y=d;for(let t=0;4>t;t++)s[e?3&-t:t]=a[p>>>24]<<24^a[b>>16&255]<<16^a[y>>8&255]<<8^a[255&k]^n[g++],w=p,p=b,b=y,y=k,k=w;return s}},C=class{constructor(t,e){this.L=t,this.R=e,this.T=e}reset(){this.T=this.R}update(t){return this.j(this.L,t,this.T)}F(t){if(255==(t>>24&255)){let e=t>>16&255,n=t>>8&255,r=255&t;255===e?(e=0,255===n?(n=0,255===r?r=0:++r):++n):++e,t=0,t+=e<<16,t+=n<<8,t+=r}else t+=1<<24;return t}K(t){0===(t[0]=this.F(t[0]))&&(t[1]=this.F(t[1]))}j(t,e,n){let r;if(!(r=e.length))return[];const i=d.l(e);for(let i=0;r>i;i+=4){this.K(n);const r=t.encrypt(n);e[i]^=r[0],e[i+1]^=r[1],e[i+2]^=r[2],e[i+3]^=r[3]}return d.u(e,i)}},A=class{constructor(t){const e=this,n=e.O=b.v,r=[[],[]],i=n.prototype.blockSize/32;e.W=[new n,new n],t.length>i&&(t=n.hash(t));for(let e=0;i>e;e++)r[0][e]=909522486^t[e],r[1][e]=1549556828^t[e];e.W[0].update(r[0]),e.W[1].update(r[1]),e.q=new n(e.W[0])}reset(){const t=this;t.q=new t.O(t.W[0]),t.G=!1}update(t){this.G=!0,this.q.update(t)}digest(){const t=this,e=t.q.V(),n=new t.O(t.W[1]).update(e).V();return t.reset(),n}};class V{constructor(t,n,r){e.assign(this,{password:t,signed:n,J:r-1,N:new i(0)})}async append(e){const n=this;if(n.password){const i=M(e,0,m[n.J]+2);await(async(t,e,n)=>{await D(t,n,M(e,0,m[t.J]));const i=M(e,m[t.J]),s=t.keys.passwordVerification;if(s[0]!=i[0]||s[1]!=i[1])throw new r("Invalid pasword")})(n,i,n.password),n.password=null,n.X=new C(new I(n.keys.key),t.from(S)),n.Y=new A(n.keys.Z),e=M(e,m[n.J]+2)}return B(n,e,new i(e.length-10-(e.length-10)%16),0,10,!0)}flush(){const t=this,e=t.N,n=M(e,0,e.length-10),r=M(e,e.length-10);let s=new i(0);if(n.length){const e=_.g(n);t.Y.update(e);const r=t.X.update(e);s=_.k(r)}let o=!0;if(t.signed){const e=M(_.k(t.Y.digest()),0,10);for(let t=0;10>t;t++)e[t]!=r[t]&&(o=!1)}return{valid:o,data:s}}}class E{constructor(t,n){e.assign(this,{password:t,J:n-1,N:new i(0)})}async append(e){const n=this;let r=new i(0);n.password&&(r=await(async(t,e)=>{const n=u.getRandomValues(new i(m[t.J]));return await D(t,e,n),U(n,t.keys.passwordVerification)})(n,n.password),n.password=null,n.X=new C(new I(n.keys.key),t.from(S)),n.Y=new A(n.keys.Z));const s=new i(r.length+e.length-e.length%16);return s.set(r,0),B(n,e,s,r.length,0)}flush(){const t=this;let e=new i(0);if(t.N.length){const n=t.X.update(_.g(t.N));t.Y.update(n),e=_.k(n)}const n=M(_.k(t.Y.digest()),0,10);return{data:U(e,n),signature:n}}}function B(t,e,n,r,s,o){const f=e.length-s;let c;for(t.N.length&&(e=U(t.N,e),n=((t,e)=>{if(e&&e>t.length){const n=t;(t=new i(e)).set(n,0)}return t})(n,f-f%16)),c=0;f-16>=c;c+=16){const i=_.g(M(e,c,c+16));o&&t.Y.update(i);const s=t.X.update(i);o||t.Y.update(s),n.set(_.k(s),c+r)}return t.N=M(e,c),n}async function D(t,n,r){const s=(t=>{if(void 0===l){const e=new i((t=unescape(encodeURIComponent(t))).length);for(let n=0;n<e.length;n++)e[n]=t.charCodeAt(n);return e}return(new l).encode(t)})(n),o=await u.subtle.importKey("raw",s,k,!1,v),f=await u.subtle.deriveBits(e.assign({salt:r},g),o,8*(2*z[t.J]+2)),c=new i(f);t.keys={key:_.g(M(c,0,z[t.J])),Z:_.g(M(c,z[t.J],2*z[t.J])),passwordVerification:M(c,2*z[t.J])}}function U(t,e){let n=t;return t.length+e.length&&(n=new i(t.length+e.length),n.set(t,0),n.set(e,t.length)),n}function M(t,e,n){return t.subarray(e,n)}class P{constructor(t,n){e.assign(this,{password:t,passwordVerification:n}),T(this,t)}append(t){const e=this;if(e.password){const n=L(e,t.subarray(0,12));if(e.password=null,n[11]!=e.passwordVerification)throw new r("Invalid pasword");t=t.subarray(12)}return L(e,t)}flush(){return{valid:!0,data:new i(0)}}}class H{constructor(t,n){e.assign(this,{password:t,passwordVerification:n}),T(this,t)}append(t){const e=this;let n,r;if(e.password){e.password=null;const s=u.getRandomValues(new i(12));s[11]=e.passwordVerification,n=new i(t.length+s.length),n.set(R(e,s),0),r=12}else n=new i(t.length),r=0;return n.set(R(e,t),r),n}flush(){return{data:new i(0)}}}function L(t,e){const n=new i(e.length);for(let r=0;r<e.length;r++)n[r]=x(t)^e[r],j(t,n[r]);return n}function R(t,e){const n=new i(e.length);for(let r=0;r<e.length;r++)n[r]=x(t)^e[r],j(t,e[r]);return n}function T(t,e){t.keys=[305419896,591751049,878082192],t.$=new h(t.keys[0]),t.tt=new h(t.keys[2]);for(let n=0;n<e.length;n++)j(t,e.charCodeAt(n))}function j(t,e){t.$.append([e]),t.keys[0]=~t.$.get(),t.keys[1]=K(t.keys[1]+F(t.keys[0])),t.keys[1]=K(n.imul(t.keys[1],134775813)+1),t.tt.append([t.keys[1]>>>24]),t.keys[2]=~t.tt.get()}function x(t){const e=2|t.keys[2];return F(n.imul(e,1^e)>>>8)}function F(t){return 255&t}function K(t){return 4294967295&t}class O{constructor(t,{signature:n,password:r,signed:i,compressed:s,zipCrypto:o,passwordVerification:f,encryptionStrength:c},{et:l}){const u=!!r;e.assign(this,{signature:n,encrypted:u,signed:i,compressed:s,nt:s&&new t({et:l}),rt:i&&new h,zipCrypto:o,decrypt:u&&o?new P(r,f):new V(r,i,c)})}async append(t){const e=this;return e.encrypted&&t.length&&(t=await e.decrypt.append(t)),e.compressed&&t.length&&(t=await e.nt.append(t)),(!e.encrypted||e.zipCrypto)&&e.signed&&t.length&&e.rt.append(t),t}async flush(){const t=this;let e,n=new i(0);if(t.encrypted){const e=t.decrypt.flush();if(!e.valid)throw new r("Invalid signature");n=e.data}if((!t.encrypted||t.zipCrypto)&&t.signed){const n=new c(new i(4).buffer);if(e=t.rt.get(),n.setUint32(0,e),t.signature!=n.getUint32(0,!1))throw new r("Invalid signature")}return t.compressed&&(n=await t.nt.append(n)||new i(0),await t.nt.flush()),{data:n,signature:e}}}class W{constructor(t,{encrypted:n,signed:r,compressed:i,level:s,zipCrypto:o,password:f,passwordVerification:c,encryptionStrength:l},{et:u}){e.assign(this,{encrypted:n,signed:r,compressed:i,it:i&&new t({level:s||5,et:u}),rt:r&&new h,zipCrypto:o,encrypt:n&&o?new H(f,c):new E(f,l)})}async append(t){const e=this;let n=t;return e.compressed&&t.length&&(n=await e.it.append(t)),e.encrypted&&n.length&&(n=await e.encrypt.append(n)),(!e.encrypted||e.zipCrypto)&&e.signed&&t.length&&e.rt.append(t),n}async flush(){const t=this;let e,n=new i(0);if(t.compressed&&(n=await t.it.flush()||new i(0)),t.encrypted){n=await t.encrypt.append(n);const r=t.encrypt.flush();e=r.signature;const s=new i(n.length+r.data.length);s.set(n,0),s.set(r.data,n.length),n=s}return t.encrypted&&!t.zipCrypto||!t.signed||(e=t.rt.get()),{data:n,signature:e}}}const q={init(t){t.scripts&&t.scripts.length&&importScripts.apply(void 0,t.scripts);const e=t.options;let n;self.initCodec&&self.initCodec(),e.codecType.startsWith("deflate")?n=self.Deflate:e.codecType.startsWith("inflate")&&(n=self.Inflate),G=((t,e,n)=>e.codecType.startsWith("deflate")?new W(t,e,n):e.codecType.startsWith("inflate")?new O(t,e,n):void 0)(n,e,t.config)},append:async t=>({data:await G.append(t.data)}),flush:()=>G.flush()};let G;function J(e){return N(e.map((([e,n])=>new t(e).fill(n,0,e))))}function N(e){return e.reduce(((e,n)=>e.concat(t.isArray(n)?N(n):n)),[])}addEventListener("message",(async t=>{const e=t.data,n=e.type,r=q[n];if(r)try{e.data&&(e.data=new i(e.data));const t=await r(e)||{};if(t.type=n,t.data)try{t.data=t.data.buffer,a(t,[t.data])}catch(e){a(t)}else a(t)}catch(t){a({type:n,error:{message:t.message,stack:t.stack}})}}));const Q=[0,1,2,3].concat(...J([[2,4],[2,5],[4,6],[4,7],[8,8],[8,9],[16,10],[16,11],[32,12],[32,13],[64,14],[64,15],[2,0],[1,16],[1,17],[2,18],[2,19],[4,20],[4,21],[8,22],[8,23],[16,24],[16,25],[32,26],[32,27],[64,28],[64,29]]));function X(){const t=this;function e(t,e){let n=0;do{n|=1&t,t>>>=1,n<<=1}while(--e>0);return n>>>1}t.st=r=>{const i=t.ot,s=t.ct.ft,o=t.ct.lt;let f,c,l,u=-1;for(r.ut=0,r.at=573,f=0;o>f;f++)0!==i[2*f]?(r.wt[++r.ut]=u=f,r.ht[f]=0):i[2*f+1]=0;for(;2>r.ut;)l=r.wt[++r.ut]=2>u?++u:0,i[2*l]=1,r.ht[l]=0,r.dt--,s&&(r.bt-=s[2*l+1]);for(t.yt=u,f=n.floor(r.ut/2);f>=1;f--)r.kt(i,f);l=o;do{f=r.wt[1],r.wt[1]=r.wt[r.ut--],r.kt(i,1),c=r.wt[1],r.wt[--r.at]=f,r.wt[--r.at]=c,i[2*l]=i[2*f]+i[2*c],r.ht[l]=n.max(r.ht[f],r.ht[c])+1,i[2*f+1]=i[2*c+1]=l,r.wt[1]=l++,r.kt(i,1)}while(r.ut>=2);r.wt[--r.at]=r.wt[1],(e=>{const n=t.ot,r=t.ct.ft,i=t.ct.gt,s=t.ct.vt,o=t.ct.zt;let f,c,l,u,a,w,h=0;for(u=0;15>=u;u++)e.St[u]=0;for(n[2*e.wt[e.at]+1]=0,f=e.at+1;573>f;f++)c=e.wt[f],u=n[2*n[2*c+1]+1]+1,u>o&&(u=o,h++),n[2*c+1]=u,c>t.yt||(e.St[u]++,a=0,s>c||(a=i[c-s]),w=n[2*c],e.dt+=w*(u+a),r&&(e.bt+=w*(r[2*c+1]+a)));if(0!==h){do{for(u=o-1;0===e.St[u];)u--;e.St[u]--,e.St[u+1]+=2,e.St[o]--,h-=2}while(h>0);for(u=o;0!==u;u--)for(c=e.St[u];0!==c;)l=e.wt[--f],l>t.yt||(n[2*l+1]!=u&&(e.dt+=(u-n[2*l+1])*n[2*l],n[2*l+1]=u),c--)}})(r),((t,n,r)=>{const i=[];let s,o,f,c=0;for(s=1;15>=s;s++)i[s]=c=c+r[s-1]<<1;for(o=0;n>=o;o++)f=t[2*o+1],0!==f&&(t[2*o]=e(i[f]++,f))})(i,t.yt,r.St)}}function Y(t,e,n,r,i){const s=this;s.ft=t,s.gt=e,s.vt=n,s.lt=r,s.zt=i}X._t=[0,1,2,3,4,5,6,7].concat(...J([[2,8],[2,9],[2,10],[2,11],[4,12],[4,13],[4,14],[4,15],[8,16],[8,17],[8,18],[8,19],[16,20],[16,21],[16,22],[16,23],[32,24],[32,25],[32,26],[31,27],[1,28]])),X.It=[0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224,0],X.Ct=[0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576],X.At=t=>256>t?Q[t]:Q[256+(t>>>7)],X.Vt=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0],X.Et=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13],X.Bt=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7],X.Dt=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];const Z=J([[144,8],[112,9],[24,7],[8,8]]);Y.Ut=N([12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241,9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249,5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245,13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253,19,275,147,403,83,339,211,467,51,307,179,435,115,371,243,499,11,267,139,395,75,331,203,459,43,299,171,427,107,363,235,491,27,283,155,411,91,347,219,475,59,315,187,443,123,379,251,507,7,263,135,391,71,327,199,455,39,295,167,423,103,359,231,487,23,279,151,407,87,343,215,471,55,311,183,439,119,375,247,503,15,271,143,399,79,335,207,463,47,303,175,431,111,367,239,495,31,287,159,415,95,351,223,479,63,319,191,447,127,383,255,511,0,64,32,96,16,80,48,112,8,72,40,104,24,88,56,120,4,68,36,100,20,84,52,116,3,131,67,195,35,163,99,227].map(((t,e)=>[t,Z[e]])));const $=J([[30,5]]);function tt(t,e,n,r,i){const s=this;s.Mt=t,s.Pt=e,s.Ht=n,s.Lt=r,s.Rt=i}Y.Tt=N([0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30,1,17,9,25,5,21,13,29,3,19,11,27,7,23].map(((t,e)=>[t,$[e]]))),Y.jt=new Y(Y.Ut,X.Vt,257,286,15),Y.xt=new Y(Y.Tt,X.Et,0,30,15),Y.Ft=new Y(null,X.Bt,0,19,7);const et=[new tt(0,0,0,0,0),new tt(4,4,8,4,1),new tt(4,5,16,8,1),new tt(4,6,32,32,1),new tt(4,4,16,16,2),new tt(8,16,32,32,2),new tt(8,16,128,128,2),new tt(8,32,128,256,2),new tt(32,128,258,1024,2),new tt(32,258,258,4096,2)],nt=["need dictionary","stream end","","","stream error","data error","","buffer error","",""];function rt(t,e,n,r){const i=t[2*e],s=t[2*n];return s>i||i==s&&r[e]<=r[n]}function it(){const t=this;let e,r,o,f,c,l,u,a,w,h,d,p,b,y,k,g,v,m,z,S,_,I,C,A,V,E,B,D,U,M,P,H,L;const R=new X,T=new X,j=new X;let x,F,K,O,W,q;function G(){let e;for(e=0;286>e;e++)P[2*e]=0;for(e=0;30>e;e++)H[2*e]=0;for(e=0;19>e;e++)L[2*e]=0;P[512]=1,t.dt=t.bt=0,F=K=0}function J(t,e){let n,r=-1,i=t[1],s=0,o=7,f=4;0===i&&(o=138,f=3),t[2*(e+1)+1]=65535;for(let c=0;e>=c;c++)n=i,i=t[2*(c+1)+1],++s<o&&n==i||(f>s?L[2*n]+=s:0!==n?(n!=r&&L[2*n]++,L[32]++):s>10?L[36]++:L[34]++,s=0,r=n,0===i?(o=138,f=3):n==i?(o=6,f=3):(o=7,f=4))}function N(e){t.Kt[t.pending++]=e}function Q(t){N(255&t),N(t>>>8&255)}function Z(t,e){let n;const r=e;q>16-r?(n=t,W|=n<<q&65535,Q(W),W=n>>>16-q,q+=r-16):(W|=t<<q&65535,q+=r)}function $(t,e){const n=2*t;Z(65535&e[n],65535&e[n+1])}function tt(t,e){let n,r,i=-1,s=t[1],o=0,f=7,c=4;for(0===s&&(f=138,c=3),n=0;e>=n;n++)if(r=s,s=t[2*(n+1)+1],++o>=f||r!=s){if(c>o)do{$(r,L)}while(0!=--o);else 0!==r?(r!=i&&($(r,L),o--),$(16,L),Z(o-3,2)):o>10?($(18,L),Z(o-11,7)):($(17,L),Z(o-3,3));o=0,i=r,0===s?(f=138,c=3):r==s?(f=6,c=3):(f=7,c=4)}}function it(){16==q?(Q(W),W=0,q=0):8>q||(N(255&W),W>>>=8,q-=8)}function st(e,r){let i,s,o;if(t.Ot[F]=e,t.Wt[F]=255&r,F++,0===e?P[2*r]++:(K++,e--,P[2*(X._t[r]+256+1)]++,H[2*X.At(e)]++),0==(8191&F)&&B>2){for(i=8*F,s=_-v,o=0;30>o;o++)i+=H[2*o]*(5+X.Et[o]);if(i>>>=3,K<n.floor(F/2)&&i<n.floor(s/2))return!0}return F==x-1}function ot(e,n){let r,i,s,o,f=0;if(0!==F)do{r=t.Ot[f],i=t.Wt[f],f++,0===r?$(i,e):(s=X._t[i],$(s+256+1,e),o=X.Vt[s],0!==o&&(i-=X.It[s],Z(i,o)),r--,s=X.At(r),$(s,n),o=X.Et[s],0!==o&&(r-=X.Ct[s],Z(r,o)))}while(F>f);$(256,e),O=e[513]}function ft(){q>8?Q(W):q>0&&N(255&W),W=0,q=0}function ct(e,n,r){Z(0+(r?1:0),3),((e,n)=>{ft(),O=8,Q(n),Q(~n),t.Kt.set(a.subarray(e,e+n),t.pending),t.pending+=n})(e,n)}function lt(n){((e,n,r)=>{let i,s,o=0;B>0?(R.st(t),T.st(t),o=(()=>{let e;for(J(P,R.yt),J(H,T.yt),j.st(t),e=18;e>=3&&0===L[2*X.Dt[e]+1];e--);return t.dt+=14+3*(e+1),e})(),i=t.dt+3+7>>>3,s=t.bt+3+7>>>3,s>i||(i=s)):i=s=n+5,n+4>i||-1==e?s==i?(Z(2+(r?1:0),3),ot(Y.Ut,Y.Tt)):(Z(4+(r?1:0),3),((t,e,n)=>{let r;for(Z(t-257,5),Z(e-1,5),Z(n-4,4),r=0;n>r;r++)Z(L[2*X.Dt[r]+1],3);tt(P,t-1),tt(H,e-1)})(R.yt+1,T.yt+1,o+1),ot(P,H)):ct(e,n,r),G(),r&&ft()})(0>v?-1:v,_-v,n),v=_,e.qt()}function ut(){let t,n,r,i;do{if(i=w-C-_,0===i&&0===_&&0===C)i=c;else if(-1==i)i--;else if(_>=c+c-262){a.set(a.subarray(c,c+c),0),I-=c,_-=c,v-=c,t=b,r=t;do{n=65535&d[--r],d[r]=c>n?0:n-c}while(0!=--t);t=c,r=t;do{n=65535&h[--r],h[r]=c>n?0:n-c}while(0!=--t);i+=c}if(0===e.Gt)return;t=e.Jt(a,_+C,i),C+=t,3>C||(p=255&a[_],p=(p<<g^255&a[_+1])&k)}while(262>C&&0!==e.Gt)}function at(t){let e,n,r=V,i=_,s=A;const o=_>c-262?_-(c-262):0;let f=M;const l=u,w=_+258;let d=a[i+s-1],p=a[i+s];U>A||(r>>=2),f>C&&(f=C);do{if(e=t,a[e+s]==p&&a[e+s-1]==d&&a[e]==a[i]&&a[++e]==a[i+1]){i+=2,e++;do{}while(a[++i]==a[++e]&&a[++i]==a[++e]&&a[++i]==a[++e]&&a[++i]==a[++e]&&a[++i]==a[++e]&&a[++i]==a[++e]&&a[++i]==a[++e]&&a[++i]==a[++e]&&w>i);if(n=258-(w-i),i=w-258,n>s){if(I=t,s=n,n>=f)break;d=a[i+s-1],p=a[i+s]}}}while((t=65535&h[t&l])>o&&0!=--r);return s>C?C:s}t.ht=[],t.St=[],t.wt=[],P=[],H=[],L=[],t.kt=(e,n)=>{const r=t.wt,i=r[n];let s=n<<1;for(;s<=t.ut&&(s<t.ut&&rt(e,r[s+1],r[s],t.ht)&&s++,!rt(e,i,r[s],t.ht));)r[n]=r[s],n=s,s<<=1;r[n]=i},t.Nt=(e,z,I,F,K,J)=>(F||(F=8),K||(K=8),J||(J=0),e.Qt=null,-1==z&&(z=6),1>K||K>9||8!=F||9>I||I>15||0>z||z>9||0>J||J>2?-2:(e.Xt=t,l=I,c=1<<l,u=c-1,y=K+7,b=1<<y,k=b-1,g=n.floor((y+3-1)/3),a=new i(2*c),h=[],d=[],x=1<<K+6,t.Kt=new i(4*x),o=4*x,t.Ot=new s(x),t.Wt=new i(x),B=z,D=J,(e=>(e.Yt=e.Zt=0,e.Qt=null,t.pending=0,t.$t=0,r=113,f=0,R.ot=P,R.ct=Y.jt,T.ot=H,T.ct=Y.xt,j.ot=L,j.ct=Y.Ft,W=0,q=0,O=8,G(),(()=>{w=2*c,d[b-1]=0;for(let t=0;b-1>t;t++)d[t]=0;E=et[B].Pt,U=et[B].Mt,M=et[B].Ht,V=et[B].Lt,_=0,v=0,C=0,m=A=2,S=0,p=0})(),0))(e))),t.te=()=>42!=r&&113!=r&&666!=r?-2:(t.Wt=null,t.Ot=null,t.Kt=null,d=null,h=null,a=null,t.Xt=null,113==r?-3:0),t.ee=(t,e,n)=>{let r=0;return-1==e&&(e=6),0>e||e>9||0>n||n>2?-2:(et[B].Rt!=et[e].Rt&&0!==t.Yt&&(r=t.it(1)),B!=e&&(B=e,E=et[B].Pt,U=et[B].Mt,M=et[B].Ht,V=et[B].Lt),D=n,r)},t.ne=(t,e,n)=>{let i,s=n,o=0;if(!e||42!=r)return-2;if(3>s)return 0;for(s>c-262&&(s=c-262,o=n-s),a.set(e.subarray(o,o+s),0),_=s,v=s,p=255&a[0],p=(p<<g^255&a[1])&k,i=0;s-3>=i;i++)p=(p<<g^255&a[i+2])&k,h[i&u]=d[p],d[p]=i;return 0},t.it=(n,i)=>{let s,w,y,V,U;if(i>4||0>i)return-2;if(!n.re||!n.ie&&0!==n.Gt||666==r&&4!=i)return n.Qt=nt[4],-2;if(0===n.se)return n.Qt=nt[7],-5;var M;if(e=n,V=f,f=i,42==r&&(w=8+(l-8<<4)<<8,y=(B-1&255)>>1,y>3&&(y=3),w|=y<<6,0!==_&&(w|=32),w+=31-w%31,r=113,N((M=w)>>8&255),N(255&M)),0!==t.pending){if(e.qt(),0===e.se)return f=-1,0}else if(0===e.Gt&&V>=i&&4!=i)return e.Qt=nt[7],-5;if(666==r&&0!==e.Gt)return n.Qt=nt[7],-5;if(0!==e.Gt||0!==C||0!=i&&666!=r){switch(U=-1,et[B].Rt){case 0:U=(t=>{let n,r=65535;for(r>o-5&&(r=o-5);;){if(1>=C){if(ut(),0===C&&0==t)return 0;if(0===C)break}if(_+=C,C=0,n=v+r,(0===_||_>=n)&&(C=_-n,_=n,lt(!1),0===e.se))return 0;if(_-v>=c-262&&(lt(!1),0===e.se))return 0}return lt(4==t),0===e.se?4==t?2:0:4==t?3:1})(i);break;case 1:U=(t=>{let n,r=0;for(;;){if(262>C){if(ut(),262>C&&0==t)return 0;if(0===C)break}if(3>C||(p=(p<<g^255&a[_+2])&k,r=65535&d[p],h[_&u]=d[p],d[p]=_),0===r||(_-r&65535)>c-262||2!=D&&(m=at(r)),3>m)n=st(0,255&a[_]),C--,_++;else if(n=st(_-I,m-3),C-=m,m>E||3>C)_+=m,m=0,p=255&a[_],p=(p<<g^255&a[_+1])&k;else{m--;do{_++,p=(p<<g^255&a[_+2])&k,r=65535&d[p],h[_&u]=d[p],d[p]=_}while(0!=--m);_++}if(n&&(lt(!1),0===e.se))return 0}return lt(4==t),0===e.se?4==t?2:0:4==t?3:1})(i);break;case 2:U=(t=>{let n,r,i=0;for(;;){if(262>C){if(ut(),262>C&&0==t)return 0;if(0===C)break}if(3>C||(p=(p<<g^255&a[_+2])&k,i=65535&d[p],h[_&u]=d[p],d[p]=_),A=m,z=I,m=2,0!==i&&E>A&&c-262>=(_-i&65535)&&(2!=D&&(m=at(i)),5>=m&&(1==D||3==m&&_-I>4096)&&(m=2)),3>A||m>A)if(0!==S){if(n=st(0,255&a[_-1]),n&&lt(!1),_++,C--,0===e.se)return 0}else S=1,_++,C--;else{r=_+C-3,n=st(_-1-z,A-3),C-=A-1,A-=2;do{++_>r||(p=(p<<g^255&a[_+2])&k,i=65535&d[p],h[_&u]=d[p],d[p]=_)}while(0!=--A);if(S=0,m=2,_++,n&&(lt(!1),0===e.se))return 0}}return 0!==S&&(n=st(0,255&a[_-1]),S=0),lt(4==t),0===e.se?4==t?2:0:4==t?3:1})(i)}if(2!=U&&3!=U||(r=666),0==U||2==U)return 0===e.se&&(f=-1),0;if(1==U){if(1==i)Z(2,3),$(256,Y.Ut),it(),9>1+O+10-q&&(Z(2,3),$(256,Y.Ut),it()),O=7;else if(ct(0,0,!1),3==i)for(s=0;b>s;s++)d[s]=0;if(e.qt(),0===e.se)return f=-1,0}}return 4!=i?0:1}}function st(){const t=this;t.oe=0,t.fe=0,t.Gt=0,t.Yt=0,t.se=0,t.Zt=0}function ot(t){const e=new st,s=(o=t&&t.et?t.et:65536)+5*(n.floor(o/16383)+1);var o;const f=new i(s);let c=t?t.level:-1;void 0===c&&(c=-1),e.Nt(c),e.re=f,this.append=(t,n)=>{let o,c,l=0,u=0,a=0;const w=[];if(t.length){e.oe=0,e.ie=t,e.Gt=t.length;do{if(e.fe=0,e.se=s,o=e.it(0),0!=o)throw new r("deflating: "+e.Qt);e.fe&&(e.fe==s?w.push(new i(f)):w.push(f.slice(0,e.fe))),a+=e.fe,n&&e.oe>0&&e.oe!=l&&(n(e.oe),l=e.oe)}while(e.Gt>0||0===e.se);return w.length>1?(c=new i(a),w.forEach((t=>{c.set(t,u),u+=t.length}))):c=w[0]||new i(0),c}},this.flush=()=>{let t,n,o=0,c=0;const l=[];do{if(e.fe=0,e.se=s,t=e.it(4),1!=t&&0!=t)throw new r("deflating: "+e.Qt);s-e.se>0&&l.push(f.slice(0,e.fe)),c+=e.fe}while(e.Gt>0||0===e.se);return e.te(),n=new i(c),l.forEach((t=>{n.set(t,o),o+=t.length})),n}}st.prototype={Nt:function(t,e){const n=this;return n.Xt=new it,e||(e=15),n.Xt.Nt(n,t,e)},it:function(t){const e=this;return e.Xt?e.Xt.it(e,t):-2},te:function(){const t=this;if(!t.Xt)return-2;const e=t.Xt.te();return t.Xt=null,e},ee:function(t,e){const n=this;return n.Xt?n.Xt.ee(n,t,e):-2},ne:function(t,e){const n=this;return n.Xt?n.Xt.ne(n,t,e):-2},Jt:function(t,e,n){const r=this;let i=r.Gt;return i>n&&(i=n),0===i?0:(r.Gt-=i,t.set(r.ie.subarray(r.oe,r.oe+i),e),r.oe+=i,r.Yt+=i,i)},qt:function(){const t=this;let e=t.Xt.pending;e>t.se&&(e=t.se),0!==e&&(t.re.set(t.Xt.Kt.subarray(t.Xt.$t,t.Xt.$t+e),t.fe),t.fe+=e,t.Xt.$t+=e,t.Zt+=e,t.se-=e,t.Xt.pending-=e,0===t.Xt.pending&&(t.Xt.$t=0))}};const ft=[0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535],ct=[96,7,256,0,8,80,0,8,16,84,8,115,82,7,31,0,8,112,0,8,48,0,9,192,80,7,10,0,8,96,0,8,32,0,9,160,0,8,0,0,8,128,0,8,64,0,9,224,80,7,6,0,8,88,0,8,24,0,9,144,83,7,59,0,8,120,0,8,56,0,9,208,81,7,17,0,8,104,0,8,40,0,9,176,0,8,8,0,8,136,0,8,72,0,9,240,80,7,4,0,8,84,0,8,20,85,8,227,83,7,43,0,8,116,0,8,52,0,9,200,81,7,13,0,8,100,0,8,36,0,9,168,0,8,4,0,8,132,0,8,68,0,9,232,80,7,8,0,8,92,0,8,28,0,9,152,84,7,83,0,8,124,0,8,60,0,9,216,82,7,23,0,8,108,0,8,44,0,9,184,0,8,12,0,8,140,0,8,76,0,9,248,80,7,3,0,8,82,0,8,18,85,8,163,83,7,35,0,8,114,0,8,50,0,9,196,81,7,11,0,8,98,0,8,34,0,9,164,0,8,2,0,8,130,0,8,66,0,9,228,80,7,7,0,8,90,0,8,26,0,9,148,84,7,67,0,8,122,0,8,58,0,9,212,82,7,19,0,8,106,0,8,42,0,9,180,0,8,10,0,8,138,0,8,74,0,9,244,80,7,5,0,8,86,0,8,22,192,8,0,83,7,51,0,8,118,0,8,54,0,9,204,81,7,15,0,8,102,0,8,38,0,9,172,0,8,6,0,8,134,0,8,70,0,9,236,80,7,9,0,8,94,0,8,30,0,9,156,84,7,99,0,8,126,0,8,62,0,9,220,82,7,27,0,8,110,0,8,46,0,9,188,0,8,14,0,8,142,0,8,78,0,9,252,96,7,256,0,8,81,0,8,17,85,8,131,82,7,31,0,8,113,0,8,49,0,9,194,80,7,10,0,8,97,0,8,33,0,9,162,0,8,1,0,8,129,0,8,65,0,9,226,80,7,6,0,8,89,0,8,25,0,9,146,83,7,59,0,8,121,0,8,57,0,9,210,81,7,17,0,8,105,0,8,41,0,9,178,0,8,9,0,8,137,0,8,73,0,9,242,80,7,4,0,8,85,0,8,21,80,8,258,83,7,43,0,8,117,0,8,53,0,9,202,81,7,13,0,8,101,0,8,37,0,9,170,0,8,5,0,8,133,0,8,69,0,9,234,80,7,8,0,8,93,0,8,29,0,9,154,84,7,83,0,8,125,0,8,61,0,9,218,82,7,23,0,8,109,0,8,45,0,9,186,0,8,13,0,8,141,0,8,77,0,9,250,80,7,3,0,8,83,0,8,19,85,8,195,83,7,35,0,8,115,0,8,51,0,9,198,81,7,11,0,8,99,0,8,35,0,9,166,0,8,3,0,8,131,0,8,67,0,9,230,80,7,7,0,8,91,0,8,27,0,9,150,84,7,67,0,8,123,0,8,59,0,9,214,82,7,19,0,8,107,0,8,43,0,9,182,0,8,11,0,8,139,0,8,75,0,9,246,80,7,5,0,8,87,0,8,23,192,8,0,83,7,51,0,8,119,0,8,55,0,9,206,81,7,15,0,8,103,0,8,39,0,9,174,0,8,7,0,8,135,0,8,71,0,9,238,80,7,9,0,8,95,0,8,31,0,9,158,84,7,99,0,8,127,0,8,63,0,9,222,82,7,27,0,8,111,0,8,47,0,9,190,0,8,15,0,8,143,0,8,79,0,9,254,96,7,256,0,8,80,0,8,16,84,8,115,82,7,31,0,8,112,0,8,48,0,9,193,80,7,10,0,8,96,0,8,32,0,9,161,0,8,0,0,8,128,0,8,64,0,9,225,80,7,6,0,8,88,0,8,24,0,9,145,83,7,59,0,8,120,0,8,56,0,9,209,81,7,17,0,8,104,0,8,40,0,9,177,0,8,8,0,8,136,0,8,72,0,9,241,80,7,4,0,8,84,0,8,20,85,8,227,83,7,43,0,8,116,0,8,52,0,9,201,81,7,13,0,8,100,0,8,36,0,9,169,0,8,4,0,8,132,0,8,68,0,9,233,80,7,8,0,8,92,0,8,28,0,9,153,84,7,83,0,8,124,0,8,60,0,9,217,82,7,23,0,8,108,0,8,44,0,9,185,0,8,12,0,8,140,0,8,76,0,9,249,80,7,3,0,8,82,0,8,18,85,8,163,83,7,35,0,8,114,0,8,50,0,9,197,81,7,11,0,8,98,0,8,34,0,9,165,0,8,2,0,8,130,0,8,66,0,9,229,80,7,7,0,8,90,0,8,26,0,9,149,84,7,67,0,8,122,0,8,58,0,9,213,82,7,19,0,8,106,0,8,42,0,9,181,0,8,10,0,8,138,0,8,74,0,9,245,80,7,5,0,8,86,0,8,22,192,8,0,83,7,51,0,8,118,0,8,54,0,9,205,81,7,15,0,8,102,0,8,38,0,9,173,0,8,6,0,8,134,0,8,70,0,9,237,80,7,9,0,8,94,0,8,30,0,9,157,84,7,99,0,8,126,0,8,62,0,9,221,82,7,27,0,8,110,0,8,46,0,9,189,0,8,14,0,8,142,0,8,78,0,9,253,96,7,256,0,8,81,0,8,17,85,8,131,82,7,31,0,8,113,0,8,49,0,9,195,80,7,10,0,8,97,0,8,33,0,9,163,0,8,1,0,8,129,0,8,65,0,9,227,80,7,6,0,8,89,0,8,25,0,9,147,83,7,59,0,8,121,0,8,57,0,9,211,81,7,17,0,8,105,0,8,41,0,9,179,0,8,9,0,8,137,0,8,73,0,9,243,80,7,4,0,8,85,0,8,21,80,8,258,83,7,43,0,8,117,0,8,53,0,9,203,81,7,13,0,8,101,0,8,37,0,9,171,0,8,5,0,8,133,0,8,69,0,9,235,80,7,8,0,8,93,0,8,29,0,9,155,84,7,83,0,8,125,0,8,61,0,9,219,82,7,23,0,8,109,0,8,45,0,9,187,0,8,13,0,8,141,0,8,77,0,9,251,80,7,3,0,8,83,0,8,19,85,8,195,83,7,35,0,8,115,0,8,51,0,9,199,81,7,11,0,8,99,0,8,35,0,9,167,0,8,3,0,8,131,0,8,67,0,9,231,80,7,7,0,8,91,0,8,27,0,9,151,84,7,67,0,8,123,0,8,59,0,9,215,82,7,19,0,8,107,0,8,43,0,9,183,0,8,11,0,8,139,0,8,75,0,9,247,80,7,5,0,8,87,0,8,23,192,8,0,83,7,51,0,8,119,0,8,55,0,9,207,81,7,15,0,8,103,0,8,39,0,9,175,0,8,7,0,8,135,0,8,71,0,9,239,80,7,9,0,8,95,0,8,31,0,9,159,84,7,99,0,8,127,0,8,63,0,9,223,82,7,27,0,8,111,0,8,47,0,9,191,0,8,15,0,8,143,0,8,79,0,9,255],lt=[80,5,1,87,5,257,83,5,17,91,5,4097,81,5,5,89,5,1025,85,5,65,93,5,16385,80,5,3,88,5,513,84,5,33,92,5,8193,82,5,9,90,5,2049,86,5,129,192,5,24577,80,5,2,87,5,385,83,5,25,91,5,6145,81,5,7,89,5,1537,85,5,97,93,5,24577,80,5,4,88,5,769,84,5,49,92,5,12289,82,5,13,90,5,3073,86,5,193,192,5,24577],ut=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,0,0],at=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,112,112],wt=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],ht=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];function dt(){let t,e,n,r,i,s;function o(t,e,o,f,c,l,u,a,w,h,d){let p,b,y,k,g,v,m,z,S,_,I,C,A,V,E;_=0,g=o;do{n[t[e+_]]++,_++,g--}while(0!==g);if(n[0]==o)return u[0]=-1,a[0]=0,0;for(z=a[0],v=1;15>=v&&0===n[v];v++);for(m=v,v>z&&(z=v),g=15;0!==g&&0===n[g];g--);for(y=g,z>g&&(z=g),a[0]=z,V=1<<v;g>v;v++,V<<=1)if(0>(V-=n[v]))return-3;if(0>(V-=n[g]))return-3;for(n[g]+=V,s[1]=v=0,_=1,A=2;0!=--g;)s[A]=v+=n[_],A++,_++;g=0,_=0;do{0!==(v=t[e+_])&&(d[s[v]++]=g),_++}while(++g<o);for(o=s[y],s[0]=g=0,_=0,k=-1,C=-z,i[0]=0,I=0,E=0;y>=m;m++)for(p=n[m];0!=p--;){for(;m>C+z;){if(k++,C+=z,E=y-C,E=E>z?z:E,(b=1<<(v=m-C))>p+1&&(b-=p+1,A=m,E>v))for(;++v<E&&(b<<=1)>n[++A];)b-=n[A];if(E=1<<v,h[0]+E>1440)return-3;i[k]=I=h[0],h[0]+=E,0!==k?(s[k]=g,r[0]=v,r[1]=z,v=g>>>C-z,r[2]=I-i[k-1]-v,w.set(r,3*(i[k-1]+v))):u[0]=I}for(r[1]=m-C,o>_?d[_]<f?(r[0]=256>d[_]?0:96,r[2]=d[_++]):(r[0]=l[d[_]-f]+16+64,r[2]=c[d[_++]-f]):r[0]=192,b=1<<m-C,v=g>>>C;E>v;v+=b)w.set(r,3*(I+v));for(v=1<<m-1;0!=(g&v);v>>>=1)g^=v;for(g^=v,S=(1<<C)-1;(g&S)!=s[k];)k--,C-=z,S=(1<<C)-1}return 0!==V&&1!=y?-5:0}function c(o){let c;for(t||(t=[],e=[],n=new f(16),r=[],i=new f(15),s=new f(16)),e.length<o&&(e=[]),c=0;o>c;c++)e[c]=0;for(c=0;16>c;c++)n[c]=0;for(c=0;3>c;c++)r[c]=0;i.set(n.subarray(0,15),0),s.set(n.subarray(0,16),0)}this.ce=(n,r,i,s,f)=>{let l;return c(19),t[0]=0,l=o(n,0,19,19,null,null,i,r,s,t,e),-3==l?f.Qt="oversubscribed dynamic bit lengths tree":-5!=l&&0!==r[0]||(f.Qt="incomplete dynamic bit lengths tree",l=-3),l},this.le=(n,r,i,s,f,l,u,a,w)=>{let h;return c(288),t[0]=0,h=o(i,0,n,257,ut,at,l,s,a,t,e),0!=h||0===s[0]?(-3==h?w.Qt="oversubscribed literal/length tree":-4!=h&&(w.Qt="incomplete literal/length tree",h=-3),h):(c(288),h=o(i,n,r,0,wt,ht,u,f,a,t,e),0!=h||0===f[0]&&n>257?(-3==h?w.Qt="oversubscribed distance tree":-5==h?(w.Qt="incomplete distance tree",h=-3):-4!=h&&(w.Qt="empty distance tree with lengths",h=-3),h):0)}}function pt(){const t=this;let e,n,r,i,s=0,o=0,f=0,c=0,l=0,u=0,a=0,w=0,h=0,d=0;function p(t,e,n,r,i,s,o,f){let c,l,u,a,w,h,d,p,b,y,k,g,v,m,z,S;d=f.oe,p=f.Gt,w=o.ue,h=o.ae,b=o.write,y=b<o.read?o.read-b-1:o.end-b,k=ft[t],g=ft[e];do{for(;20>h;)p--,w|=(255&f.we(d++))<<h,h+=8;if(c=w&k,l=n,u=r,S=3*(u+c),0!==(a=l[S]))for(;;){if(w>>=l[S+1],h-=l[S+1],0!=(16&a)){for(a&=15,v=l[S+2]+(w&ft[a]),w>>=a,h-=a;15>h;)p--,w|=(255&f.we(d++))<<h,h+=8;for(c=w&g,l=i,u=s,S=3*(u+c),a=l[S];;){if(w>>=l[S+1],h-=l[S+1],0!=(16&a)){for(a&=15;a>h;)p--,w|=(255&f.we(d++))<<h,h+=8;if(m=l[S+2]+(w&ft[a]),w>>=a,h-=a,y-=v,m>b){z=b-m;do{z+=o.end}while(0>z);if(a=o.end-z,v>a){if(v-=a,b-z>0&&a>b-z)do{o.he[b++]=o.he[z++]}while(0!=--a);else o.he.set(o.he.subarray(z,z+a),b),b+=a,z+=a,a=0;z=0}}else z=b-m,b-z>0&&2>b-z?(o.he[b++]=o.he[z++],o.he[b++]=o.he[z++],v-=2):(o.he.set(o.he.subarray(z,z+2),b),b+=2,z+=2,v-=2);if(b-z>0&&v>b-z)do{o.he[b++]=o.he[z++]}while(0!=--v);else o.he.set(o.he.subarray(z,z+v),b),b+=v,z+=v,v=0;break}if(0!=(64&a))return f.Qt="invalid distance code",v=f.Gt-p,v=v>h>>3?h>>3:v,p+=v,d-=v,h-=v<<3,o.ue=w,o.ae=h,f.Gt=p,f.Yt+=d-f.oe,f.oe=d,o.write=b,-3;c+=l[S+2],c+=w&ft[a],S=3*(u+c),a=l[S]}break}if(0!=(64&a))return 0!=(32&a)?(v=f.Gt-p,v=v>h>>3?h>>3:v,p+=v,d-=v,h-=v<<3,o.ue=w,o.ae=h,f.Gt=p,f.Yt+=d-f.oe,f.oe=d,o.write=b,1):(f.Qt="invalid literal/length code",v=f.Gt-p,v=v>h>>3?h>>3:v,p+=v,d-=v,h-=v<<3,o.ue=w,o.ae=h,f.Gt=p,f.Yt+=d-f.oe,f.oe=d,o.write=b,-3);if(c+=l[S+2],c+=w&ft[a],S=3*(u+c),0===(a=l[S])){w>>=l[S+1],h-=l[S+1],o.he[b++]=l[S+2],y--;break}}else w>>=l[S+1],h-=l[S+1],o.he[b++]=l[S+2],y--}while(y>=258&&p>=10);return v=f.Gt-p,v=v>h>>3?h>>3:v,p+=v,d-=v,h-=v<<3,o.ue=w,o.ae=h,f.Gt=p,f.Yt+=d-f.oe,f.oe=d,o.write=b,0}t.init=(t,s,o,f,c,l)=>{e=0,a=t,w=s,r=o,h=f,i=c,d=l,n=null},t.de=(t,b,y)=>{let k,g,v,m,z,S,_,I=0,C=0,A=0;for(A=b.oe,m=b.Gt,I=t.ue,C=t.ae,z=t.write,S=z<t.read?t.read-z-1:t.end-z;;)switch(e){case 0:if(S>=258&&m>=10&&(t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,y=p(a,w,r,h,i,d,t,b),A=b.oe,m=b.Gt,I=t.ue,C=t.ae,z=t.write,S=z<t.read?t.read-z-1:t.end-z,0!=y)){e=1==y?7:9;break}f=a,n=r,o=h,e=1;case 1:for(k=f;k>C;){if(0===m)return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);y=0,m--,I|=(255&b.we(A++))<<C,C+=8}if(g=3*(o+(I&ft[k])),I>>>=n[g+1],C-=n[g+1],v=n[g],0===v){c=n[g+2],e=6;break}if(0!=(16&v)){l=15&v,s=n[g+2],e=2;break}if(0==(64&v)){f=v,o=g/3+n[g+2];break}if(0!=(32&v)){e=7;break}return e=9,b.Qt="invalid literal/length code",y=-3,t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);case 2:for(k=l;k>C;){if(0===m)return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);y=0,m--,I|=(255&b.we(A++))<<C,C+=8}s+=I&ft[k],I>>=k,C-=k,f=w,n=i,o=d,e=3;case 3:for(k=f;k>C;){if(0===m)return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);y=0,m--,I|=(255&b.we(A++))<<C,C+=8}if(g=3*(o+(I&ft[k])),I>>=n[g+1],C-=n[g+1],v=n[g],0!=(16&v)){l=15&v,u=n[g+2],e=4;break}if(0==(64&v)){f=v,o=g/3+n[g+2];break}return e=9,b.Qt="invalid distance code",y=-3,t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);case 4:for(k=l;k>C;){if(0===m)return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);y=0,m--,I|=(255&b.we(A++))<<C,C+=8}u+=I&ft[k],I>>=k,C-=k,e=5;case 5:for(_=z-u;0>_;)_+=t.end;for(;0!==s;){if(0===S&&(z==t.end&&0!==t.read&&(z=0,S=z<t.read?t.read-z-1:t.end-z),0===S&&(t.write=z,y=t.pe(b,y),z=t.write,S=z<t.read?t.read-z-1:t.end-z,z==t.end&&0!==t.read&&(z=0,S=z<t.read?t.read-z-1:t.end-z),0===S)))return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);t.he[z++]=t.he[_++],S--,_==t.end&&(_=0),s--}e=0;break;case 6:if(0===S&&(z==t.end&&0!==t.read&&(z=0,S=z<t.read?t.read-z-1:t.end-z),0===S&&(t.write=z,y=t.pe(b,y),z=t.write,S=z<t.read?t.read-z-1:t.end-z,z==t.end&&0!==t.read&&(z=0,S=z<t.read?t.read-z-1:t.end-z),0===S)))return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);y=0,t.he[z++]=c,S--,e=0;break;case 7:if(C>7&&(C-=8,m++,A--),t.write=z,y=t.pe(b,y),z=t.write,S=z<t.read?t.read-z-1:t.end-z,t.read!=t.write)return t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);e=8;case 8:return y=1,t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);case 9:return y=-3,t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y);default:return y=-2,t.ue=I,t.ae=C,b.Gt=m,b.Yt+=A-b.oe,b.oe=A,t.write=z,t.pe(b,y)}},t.be=()=>{}}dt.ye=(t,e,n,r)=>(t[0]=9,e[0]=5,n[0]=ct,r[0]=lt,0);const bt=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];function yt(t,e){const n=this;let r,s=0,o=0,c=0,l=0;const u=[0],a=[0],w=new pt;let h=0,d=new f(4320);const p=new dt;n.ae=0,n.ue=0,n.he=new i(e),n.end=e,n.read=0,n.write=0,n.reset=(t,e)=>{e&&(e[0]=0),6==s&&w.be(t),s=0,n.ae=0,n.ue=0,n.read=n.write=0},n.reset(t,null),n.pe=(t,e)=>{let r,i,s;return i=t.fe,s=n.read,r=(s>n.write?n.end:n.write)-s,r>t.se&&(r=t.se),0!==r&&-5==e&&(e=0),t.se-=r,t.Zt+=r,t.re.set(n.he.subarray(s,s+r),i),i+=r,s+=r,s==n.end&&(s=0,n.write==n.end&&(n.write=0),r=n.write-s,r>t.se&&(r=t.se),0!==r&&-5==e&&(e=0),t.se-=r,t.Zt+=r,t.re.set(n.he.subarray(s,s+r),i),i+=r,s+=r),t.fe=i,n.read=s,e},n.de=(t,e)=>{let i,f,b,y,k,g,v,m;for(y=t.oe,k=t.Gt,f=n.ue,b=n.ae,g=n.write,v=g<n.read?n.read-g-1:n.end-g;;){let z,S,_,I,C,A,V,E;switch(s){case 0:for(;3>b;){if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);e=0,k--,f|=(255&t.we(y++))<<b,b+=8}switch(i=7&f,h=1&i,i>>>1){case 0:f>>>=3,b-=3,i=7&b,f>>>=i,b-=i,s=1;break;case 1:z=[],S=[],_=[[]],I=[[]],dt.ye(z,S,_,I),w.init(z[0],S[0],_[0],0,I[0],0),f>>>=3,b-=3,s=6;break;case 2:f>>>=3,b-=3,s=3;break;case 3:return f>>>=3,b-=3,s=9,t.Qt="invalid block type",e=-3,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e)}break;case 1:for(;32>b;){if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);e=0,k--,f|=(255&t.we(y++))<<b,b+=8}if((~f>>>16&65535)!=(65535&f))return s=9,t.Qt="invalid stored block lengths",e=-3,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);o=65535&f,f=b=0,s=0!==o?2:0!==h?7:0;break;case 2:if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);if(0===v&&(g==n.end&&0!==n.read&&(g=0,v=g<n.read?n.read-g-1:n.end-g),0===v&&(n.write=g,e=n.pe(t,e),g=n.write,v=g<n.read?n.read-g-1:n.end-g,g==n.end&&0!==n.read&&(g=0,v=g<n.read?n.read-g-1:n.end-g),0===v)))return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);if(e=0,i=o,i>k&&(i=k),i>v&&(i=v),n.he.set(t.Jt(y,i),g),y+=i,k-=i,g+=i,v-=i,0!=(o-=i))break;s=0!==h?7:0;break;case 3:for(;14>b;){if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);e=0,k--,f|=(255&t.we(y++))<<b,b+=8}if(c=i=16383&f,(31&i)>29||(i>>5&31)>29)return s=9,t.Qt="too many length or distance symbols",e=-3,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);if(i=258+(31&i)+(i>>5&31),!r||r.length<i)r=[];else for(m=0;i>m;m++)r[m]=0;f>>>=14,b-=14,l=0,s=4;case 4:for(;4+(c>>>10)>l;){for(;3>b;){if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);e=0,k--,f|=(255&t.we(y++))<<b,b+=8}r[bt[l++]]=7&f,f>>>=3,b-=3}for(;19>l;)r[bt[l++]]=0;if(u[0]=7,i=p.ce(r,u,a,d,t),0!=i)return-3==(e=i)&&(r=null,s=9),n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);l=0,s=5;case 5:for(;i=c,258+(31&i)+(i>>5&31)>l;){let o,w;for(i=u[0];i>b;){if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);e=0,k--,f|=(255&t.we(y++))<<b,b+=8}if(i=d[3*(a[0]+(f&ft[i]))+1],w=d[3*(a[0]+(f&ft[i]))+2],16>w)f>>>=i,b-=i,r[l++]=w;else{for(m=18==w?7:w-14,o=18==w?11:3;i+m>b;){if(0===k)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);e=0,k--,f|=(255&t.we(y++))<<b,b+=8}if(f>>>=i,b-=i,o+=f&ft[m],f>>>=m,b-=m,m=l,i=c,m+o>258+(31&i)+(i>>5&31)||16==w&&1>m)return r=null,s=9,t.Qt="invalid bit length repeat",e=-3,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);w=16==w?r[m-1]:0;do{r[m++]=w}while(0!=--o);l=m}}if(a[0]=-1,C=[],A=[],V=[],E=[],C[0]=9,A[0]=6,i=c,i=p.le(257+(31&i),1+(i>>5&31),r,C,A,V,E,d,t),0!=i)return-3==i&&(r=null,s=9),e=i,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);w.init(C[0],A[0],d,V[0],d,E[0]),s=6;case 6:if(n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,1!=(e=w.de(n,t,e)))return n.pe(t,e);if(e=0,w.be(t),y=t.oe,k=t.Gt,f=n.ue,b=n.ae,g=n.write,v=g<n.read?n.read-g-1:n.end-g,0===h){s=0;break}s=7;case 7:if(n.write=g,e=n.pe(t,e),g=n.write,v=g<n.read?n.read-g-1:n.end-g,n.read!=n.write)return n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);s=8;case 8:return e=1,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);case 9:return e=-3,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e);default:return e=-2,n.ue=f,n.ae=b,t.Gt=k,t.Yt+=y-t.oe,t.oe=y,n.write=g,n.pe(t,e)}}},n.be=t=>{n.reset(t,null),n.he=null,d=null},n.ke=(t,e,r)=>{n.he.set(t.subarray(e,e+r),0),n.read=n.write=r},n.ge=()=>1==s?1:0}const kt=[0,0,255,255];function gt(){const t=this;function e(t){return t&&t.ve?(t.Yt=t.Zt=0,t.Qt=null,t.ve.mode=7,t.ve.me.reset(t,null),0):-2}t.mode=0,t.method=0,t.ze=[0],t.Se=0,t.marker=0,t._e=0,t.Ie=e=>(t.me&&t.me.be(e),t.me=null,0),t.Ce=(n,r)=>(n.Qt=null,t.me=null,8>r||r>15?(t.Ie(n),-2):(t._e=r,n.ve.me=new yt(n,1<<r),e(n),0)),t.nt=(t,e)=>{let n,r;if(!t||!t.ve||!t.ie)return-2;const i=t.ve;for(e=4==e?-5:0,n=-5;;)switch(i.mode){case 0:if(0===t.Gt)return n;if(n=e,t.Gt--,t.Yt++,8!=(15&(i.method=t.we(t.oe++)))){i.mode=13,t.Qt="unknown compression method",i.marker=5;break}if(8+(i.method>>4)>i._e){i.mode=13,t.Qt="invalid win size",i.marker=5;break}i.mode=1;case 1:if(0===t.Gt)return n;if(n=e,t.Gt--,t.Yt++,r=255&t.we(t.oe++),((i.method<<8)+r)%31!=0){i.mode=13,t.Qt="incorrect header check",i.marker=5;break}if(0==(32&r)){i.mode=7;break}i.mode=2;case 2:if(0===t.Gt)return n;n=e,t.Gt--,t.Yt++,i.Se=(255&t.we(t.oe++))<<24&4278190080,i.mode=3;case 3:if(0===t.Gt)return n;n=e,t.Gt--,t.Yt++,i.Se+=(255&t.we(t.oe++))<<16&16711680,i.mode=4;case 4:if(0===t.Gt)return n;n=e,t.Gt--,t.Yt++,i.Se+=(255&t.we(t.oe++))<<8&65280,i.mode=5;case 5:return 0===t.Gt?n:(n=e,t.Gt--,t.Yt++,i.Se+=255&t.we(t.oe++),i.mode=6,2);case 6:return i.mode=13,t.Qt="need dictionary",i.marker=0,-2;case 7:if(n=i.me.de(t,n),-3==n){i.mode=13,i.marker=0;break}if(0==n&&(n=e),1!=n)return n;n=e,i.me.reset(t,i.ze),i.mode=12;case 12:return t.Gt=0,1;case 13:return-3;default:return-2}},t.Ae=(t,e,n)=>{let r=0,i=n;if(!t||!t.ve||6!=t.ve.mode)return-2;const s=t.ve;return i<1<<s._e||(i=(1<<s._e)-1,r=n-i),s.me.ke(e,r,i),s.mode=7,0},t.Ve=t=>{let n,r,i,s,o;if(!t||!t.ve)return-2;const f=t.ve;if(13!=f.mode&&(f.mode=13,f.marker=0),0===(n=t.Gt))return-5;for(r=t.oe,i=f.marker;0!==n&&4>i;)t.we(r)==kt[i]?i++:i=0!==t.we(r)?0:4-i,r++,n--;return t.Yt+=r-t.oe,t.oe=r,t.Gt=n,f.marker=i,4!=i?-3:(s=t.Yt,o=t.Zt,e(t),t.Yt=s,t.Zt=o,f.mode=7,0)},t.Ee=t=>t&&t.ve&&t.ve.me?t.ve.me.ge():-2}function vt(){}function mt(t){const e=new vt,s=t&&t.et?n.floor(2*t.et):131072,o=new i(s);let f=!1;e.Ce(),e.re=o,this.append=(t,n)=>{const c=[];let l,u,a=0,w=0,h=0;if(0!==t.length){e.oe=0,e.ie=t,e.Gt=t.length;do{if(e.fe=0,e.se=s,0!==e.Gt||f||(e.oe=0,f=!0),l=e.nt(0),f&&-5===l){if(0!==e.Gt)throw new r("inflating: bad input")}else if(0!==l&&1!==l)throw new r("inflating: "+e.Qt);if((f||1===l)&&e.Gt===t.length)throw new r("inflating: bad input");e.fe&&(e.fe===s?c.push(new i(o)):c.push(o.slice(0,e.fe))),h+=e.fe,n&&e.oe>0&&e.oe!=a&&(n(e.oe),a=e.oe)}while(e.Gt>0||0===e.se);return c.length>1?(u=new i(h),c.forEach((t=>{u.set(t,w),w+=t.length}))):u=c[0]||new i(0),u}},this.flush=()=>{e.Ie()}}vt.prototype={Ce:function(t){const e=this;return e.ve=new gt,t||(t=15),e.ve.Ce(e,t)},nt:function(t){const e=this;return e.ve?e.ve.nt(e,t):-2},Ie:function(){const t=this;if(!t.ve)return-2;const e=t.ve.Ie(t);return t.ve=null,e},Ve:function(){const t=this;return t.ve?t.ve.Ve(t):-2},Ae:function(t,e){const n=this;return n.ve?n.ve.Ae(n,t,e):-2},we:function(t){return this.ie[t]},Jt:function(t,e){return this.ie.subarray(t,t+e)}},self.initCodec=()=>{self.Deflate=ot,self.Inflate=mt};\n'],{type:"text/javascript"}));t({workerScripts:{inflate:[e],deflate:[e]}});}};
  4317.  
  4318. /*
  4319. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  4320.  
  4321. Redistribution and use in source and binary forms, with or without
  4322. modification, are permitted provided that the following conditions are met:
  4323.  
  4324. 1. Redistributions of source code must retain the above copyright notice,
  4325. this list of conditions and the following disclaimer.
  4326.  
  4327. 2. Redistributions in binary form must reproduce the above copyright
  4328. notice, this list of conditions and the following disclaimer in
  4329. the documentation and/or other materials provided with the distribution.
  4330.  
  4331. 3. The names of the authors may not be used to endorse or promote products
  4332. derived from this software without specific prior written permission.
  4333.  
  4334. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  4335. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  4336. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  4337. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  4338. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4339. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  4340. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4341. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4342. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4343. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4344. */
  4345.  
  4346. function getMimeType() {
  4347. return "application/octet-stream";
  4348. }
  4349.  
  4350. /*
  4351. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  4352.  
  4353. Redistribution and use in source and binary forms, with or without
  4354. modification, are permitted provided that the following conditions are met:
  4355.  
  4356. 1. Redistributions of source code must retain the above copyright notice,
  4357. this list of conditions and the following disclaimer.
  4358.  
  4359. 2. Redistributions in binary form must reproduce the above copyright
  4360. notice, this list of conditions and the following disclaimer in
  4361. the documentation and/or other materials provided with the distribution.
  4362.  
  4363. 3. The names of the authors may not be used to endorse or promote products
  4364. derived from this software without specific prior written permission.
  4365.  
  4366. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  4367. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  4368. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  4369. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  4370. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4371. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  4372. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4373. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4374. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4375. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4376. */
  4377.  
  4378. var streamCodecShim = (library, options = {}, registerDataHandler) => {
  4379. return {
  4380. Deflate: createCodecClass(library.Deflate, options.deflate, registerDataHandler),
  4381. Inflate: createCodecClass(library.Inflate, options.inflate, registerDataHandler)
  4382. };
  4383. };
  4384.  
  4385. function createCodecClass(constructor, constructorOptions, registerDataHandler) {
  4386. return class {
  4387.  
  4388. constructor(options) {
  4389. const codecAdapter = this;
  4390. const onData = data => {
  4391. if (codecAdapter.pendingData) {
  4392. const pendingData = codecAdapter.pendingData;
  4393. codecAdapter.pendingData = new Uint8Array(pendingData.length + data.length);
  4394. codecAdapter.pendingData.set(pendingData, 0);
  4395. codecAdapter.pendingData.set(data, pendingData.length);
  4396. } else {
  4397. codecAdapter.pendingData = new Uint8Array(data);
  4398. }
  4399. };
  4400. codecAdapter.codec = new constructor(Object.assign({}, constructorOptions, options));
  4401. registerDataHandler(codecAdapter.codec, onData);
  4402. }
  4403. async append(data) {
  4404. this.codec.push(data);
  4405. return getResponse(this);
  4406. }
  4407. async flush() {
  4408. this.codec.push(new Uint8Array(0), true);
  4409. return getResponse(this);
  4410. }
  4411. };
  4412.  
  4413. function getResponse(codec) {
  4414. if (codec.pendingData) {
  4415. const output = codec.pendingData;
  4416. codec.pendingData = null;
  4417. return output;
  4418. } else {
  4419. return new Uint8Array(0);
  4420. }
  4421. }
  4422. }
  4423.  
  4424. /*
  4425. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  4426.  
  4427. Redistribution and use in source and binary forms, with or without
  4428. modification, are permitted provided that the following conditions are met:
  4429.  
  4430. 1. Redistributions of source code must retain the above copyright notice,
  4431. this list of conditions and the following disclaimer.
  4432.  
  4433. 2. Redistributions in binary form must reproduce the above copyright
  4434. notice, this list of conditions and the following disclaimer in
  4435. the documentation and/or other materials provided with the distribution.
  4436.  
  4437. 3. The names of the authors may not be used to endorse or promote products
  4438. derived from this software without specific prior written permission.
  4439.  
  4440. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  4441. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  4442. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  4443. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  4444. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4445. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  4446. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4447. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4448. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4449. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4450. */
  4451.  
  4452. const table = [];
  4453. for (let i = 0; i < 256; i++) {
  4454. let t = i;
  4455. for (let j = 0; j < 8; j++) {
  4456. if (t & 1) {
  4457. t = (t >>> 1) ^ 0xEDB88320;
  4458. } else {
  4459. t = t >>> 1;
  4460. }
  4461. }
  4462. table[i] = t;
  4463. }
  4464.  
  4465. class Crc32 {
  4466.  
  4467. constructor(crc) {
  4468. this.crc = crc || -1;
  4469. }
  4470.  
  4471. append(data) {
  4472. let crc = this.crc | 0;
  4473. for (let offset = 0, length = data.length | 0; offset < length; offset++) {
  4474. crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF];
  4475. }
  4476. this.crc = crc;
  4477. }
  4478.  
  4479. get() {
  4480. return ~this.crc;
  4481. }
  4482. }
  4483.  
  4484. /*
  4485. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  4486.  
  4487. Redistribution and use in source and binary forms, with or without
  4488. modification, are permitted provided that the following conditions are met:
  4489.  
  4490. 1. Redistributions of source code must retain the above copyright notice,
  4491. this list of conditions and the following disclaimer.
  4492.  
  4493. 2. Redistributions in binary form must reproduce the above copyright
  4494. notice, this list of conditions and the following disclaimer in
  4495. the documentation and/or other materials provided with the distribution.
  4496.  
  4497. 3. The names of the authors may not be used to endorse or promote products
  4498. derived from this software without specific prior written permission.
  4499.  
  4500. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  4501. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  4502. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  4503. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  4504. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4505. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  4506. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4507. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4508. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4509. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4510. */
  4511.  
  4512. function encodeText(value) {
  4513. if (typeof TextEncoder == "undefined") {
  4514. value = unescape(encodeURIComponent(value));
  4515. const result = new Uint8Array(value.length);
  4516. for (let i = 0; i < result.length; i++) {
  4517. result[i] = value.charCodeAt(i);
  4518. }
  4519. return result;
  4520. } else {
  4521. return new TextEncoder().encode(value);
  4522. }
  4523. }
  4524.  
  4525. // Derived from https://github.com/xqdoo00o/jszip/blob/master/lib/sjcl.js
  4526.  
  4527. /** @fileOverview Javascript cryptography implementation.
  4528. *
  4529. * Crush to remove comments, shorten variable names and
  4530. * generally reduce transmission size.
  4531. *
  4532. * @author Emily Stark
  4533. * @author Mike Hamburg
  4534. * @author Dan Boneh
  4535. */
  4536.  
  4537. /*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
  4538.  
  4539. /** @fileOverview Arrays of bits, encoded as arrays of Numbers.
  4540. *
  4541. * @author Emily Stark
  4542. * @author Mike Hamburg
  4543. * @author Dan Boneh
  4544. */
  4545.  
  4546. /**
  4547. * Arrays of bits, encoded as arrays of Numbers.
  4548. * @namespace
  4549. * @description
  4550. * <p>
  4551. * These objects are the currency accepted by SJCL's crypto functions.
  4552. * </p>
  4553. *
  4554. * <p>
  4555. * Most of our crypto primitives operate on arrays of 4-byte words internally,
  4556. * but many of them can take arguments that are not a multiple of 4 bytes.
  4557. * This library encodes arrays of bits (whose size need not be a multiple of 8
  4558. * bits) as arrays of 32-bit words. The bits are packed, big-endian, into an
  4559. * array of words, 32 bits at a time. Since the words are double-precision
  4560. * floating point numbers, they fit some extra data. We use this (in a private,
  4561. * possibly-changing manner) to encode the number of bits actually present
  4562. * in the last word of the array.
  4563. * </p>
  4564. *
  4565. * <p>
  4566. * Because bitwise ops clear this out-of-band data, these arrays can be passed
  4567. * to ciphers like AES which want arrays of words.
  4568. * </p>
  4569. */
  4570. const bitArray = {
  4571. /**
  4572. * Concatenate two bit arrays.
  4573. * @param {bitArray} a1 The first array.
  4574. * @param {bitArray} a2 The second array.
  4575. * @return {bitArray} The concatenation of a1 and a2.
  4576. */
  4577. concat(a1, a2) {
  4578. if (a1.length === 0 || a2.length === 0) {
  4579. return a1.concat(a2);
  4580. }
  4581.  
  4582. const last = a1[a1.length - 1], shift = bitArray.getPartial(last);
  4583. if (shift === 32) {
  4584. return a1.concat(a2);
  4585. } else {
  4586. return bitArray._shiftRight(a2, shift, last | 0, a1.slice(0, a1.length - 1));
  4587. }
  4588. },
  4589.  
  4590. /**
  4591. * Find the length of an array of bits.
  4592. * @param {bitArray} a The array.
  4593. * @return {Number} The length of a, in bits.
  4594. */
  4595. bitLength(a) {
  4596. const l = a.length;
  4597. if (l === 0) {
  4598. return 0;
  4599. }
  4600. const x = a[l - 1];
  4601. return (l - 1) * 32 + bitArray.getPartial(x);
  4602. },
  4603.  
  4604. /**
  4605. * Truncate an array.
  4606. * @param {bitArray} a The array.
  4607. * @param {Number} len The length to truncate to, in bits.
  4608. * @return {bitArray} A new array, truncated to len bits.
  4609. */
  4610. clamp(a, len) {
  4611. if (a.length * 32 < len) {
  4612. return a;
  4613. }
  4614. a = a.slice(0, Math.ceil(len / 32));
  4615. const l = a.length;
  4616. len = len & 31;
  4617. if (l > 0 && len) {
  4618. a[l - 1] = bitArray.partial(len, a[l - 1] & 0x80000000 >> (len - 1), 1);
  4619. }
  4620. return a;
  4621. },
  4622.  
  4623. /**
  4624. * Make a partial word for a bit array.
  4625. * @param {Number} len The number of bits in the word.
  4626. * @param {Number} x The bits.
  4627. * @param {Number} [_end=0] Pass 1 if x has already been shifted to the high side.
  4628. * @return {Number} The partial word.
  4629. */
  4630. partial(len, x, _end) {
  4631. if (len === 32) {
  4632. return x;
  4633. }
  4634. return (_end ? x | 0 : x << (32 - len)) + len * 0x10000000000;
  4635. },
  4636.  
  4637. /**
  4638. * Get the number of bits used by a partial word.
  4639. * @param {Number} x The partial word.
  4640. * @return {Number} The number of bits used by the partial word.
  4641. */
  4642. getPartial(x) {
  4643. return Math.round(x / 0x10000000000) || 32;
  4644. },
  4645.  
  4646. /** Shift an array right.
  4647. * @param {bitArray} a The array to shift.
  4648. * @param {Number} shift The number of bits to shift.
  4649. * @param {Number} [carry=0] A byte to carry in
  4650. * @param {bitArray} [out=[]] An array to prepend to the output.
  4651. * @private
  4652. */
  4653. _shiftRight(a, shift, carry, out) {
  4654. if (out === undefined) {
  4655. out = [];
  4656. }
  4657.  
  4658. for (; shift >= 32; shift -= 32) {
  4659. out.push(carry);
  4660. carry = 0;
  4661. }
  4662. if (shift === 0) {
  4663. return out.concat(a);
  4664. }
  4665.  
  4666. for (let i = 0; i < a.length; i++) {
  4667. out.push(carry | a[i] >>> shift);
  4668. carry = a[i] << (32 - shift);
  4669. }
  4670. const last2 = a.length ? a[a.length - 1] : 0;
  4671. const shift2 = bitArray.getPartial(last2);
  4672. out.push(bitArray.partial(shift + shift2 & 31, (shift + shift2 > 32) ? carry : out.pop(), 1));
  4673. return out;
  4674. }
  4675. };
  4676.  
  4677. /** @fileOverview Bit array codec implementations.
  4678. *
  4679. * @author Emily Stark
  4680. * @author Mike Hamburg
  4681. * @author Dan Boneh
  4682. */
  4683.  
  4684. /**
  4685. * Arrays of bytes
  4686. * @namespace
  4687. */
  4688. const codec = {
  4689. bytes: {
  4690. /** Convert from a bitArray to an array of bytes. */
  4691. fromBits(arr) {
  4692. const bl = bitArray.bitLength(arr);
  4693. const byteLength = bl / 8;
  4694. const out = new Uint8Array(byteLength);
  4695. let tmp;
  4696. for (let i = 0; i < byteLength; i++) {
  4697. if ((i & 3) === 0) {
  4698. tmp = arr[i / 4];
  4699. }
  4700. out[i] = tmp >>> 24;
  4701. tmp <<= 8;
  4702. }
  4703. return out;
  4704. },
  4705. /** Convert from an array of bytes to a bitArray. */
  4706. toBits(bytes) {
  4707. const out = [];
  4708. let i;
  4709. let tmp = 0;
  4710. for (i = 0; i < bytes.length; i++) {
  4711. tmp = tmp << 8 | bytes[i];
  4712. if ((i & 3) === 3) {
  4713. out.push(tmp);
  4714. tmp = 0;
  4715. }
  4716. }
  4717. if (i & 3) {
  4718. out.push(bitArray.partial(8 * (i & 3), tmp));
  4719. }
  4720. return out;
  4721. }
  4722. }
  4723. };
  4724.  
  4725. const hash = {};
  4726.  
  4727. /**
  4728. * Context for a SHA-1 operation in progress.
  4729. * @constructor
  4730. */
  4731. hash.sha1 = function (hash) {
  4732. if (hash) {
  4733. this._h = hash._h.slice(0);
  4734. this._buffer = hash._buffer.slice(0);
  4735. this._length = hash._length;
  4736. } else {
  4737. this.reset();
  4738. }
  4739. };
  4740.  
  4741. hash.sha1.prototype = {
  4742. /**
  4743. * The hash's block size, in bits.
  4744. * @constant
  4745. */
  4746. blockSize: 512,
  4747.  
  4748. /**
  4749. * Reset the hash state.
  4750. * @return this
  4751. */
  4752. reset: function () {
  4753. const sha1 = this;
  4754. sha1._h = this._init.slice(0);
  4755. sha1._buffer = [];
  4756. sha1._length = 0;
  4757. return sha1;
  4758. },
  4759.  
  4760. /**
  4761. * Input several words to the hash.
  4762. * @param {bitArray|String} data the data to hash.
  4763. * @return this
  4764. */
  4765. update: function (data) {
  4766. const sha1 = this;
  4767. if (typeof data === "string") {
  4768. data = codec.utf8String.toBits(data);
  4769. }
  4770. const b = sha1._buffer = bitArray.concat(sha1._buffer, data);
  4771. const ol = sha1._length;
  4772. const nl = sha1._length = ol + bitArray.bitLength(data);
  4773. if (nl > 9007199254740991) {
  4774. throw new Error("Cannot hash more than 2^53 - 1 bits");
  4775. }
  4776. const c = new Uint32Array(b);
  4777. let j = 0;
  4778. for (let i = sha1.blockSize + ol - ((sha1.blockSize + ol) & (sha1.blockSize - 1)); i <= nl;
  4779. i += sha1.blockSize) {
  4780. sha1._block(c.subarray(16 * j, 16 * (j + 1)));
  4781. j += 1;
  4782. }
  4783. b.splice(0, 16 * j);
  4784. return sha1;
  4785. },
  4786.  
  4787. /**
  4788. * Complete hashing and output the hash value.
  4789. * @return {bitArray} The hash value, an array of 5 big-endian words. TODO
  4790. */
  4791. finalize: function () {
  4792. const sha1 = this;
  4793. let b = sha1._buffer;
  4794. const h = sha1._h;
  4795.  
  4796. // Round out and push the buffer
  4797. b = bitArray.concat(b, [bitArray.partial(1, 1)]);
  4798. // Round out the buffer to a multiple of 16 words, less the 2 length words.
  4799. for (let i = b.length + 2; i & 15; i++) {
  4800. b.push(0);
  4801. }
  4802.  
  4803. // append the length
  4804. b.push(Math.floor(sha1._length / 0x100000000));
  4805. b.push(sha1._length | 0);
  4806.  
  4807. while (b.length) {
  4808. sha1._block(b.splice(0, 16));
  4809. }
  4810.  
  4811. sha1.reset();
  4812. return h;
  4813. },
  4814.  
  4815. /**
  4816. * The SHA-1 initialization vector.
  4817. * @private
  4818. */
  4819. _init: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
  4820.  
  4821. /**
  4822. * The SHA-1 hash key.
  4823. * @private
  4824. */
  4825. _key: [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6],
  4826.  
  4827. /**
  4828. * The SHA-1 logical functions f(0), f(1), ..., f(79).
  4829. * @private
  4830. */
  4831. _f: function (t, b, c, d) {
  4832. if (t <= 19) {
  4833. return (b & c) | (~b & d);
  4834. } else if (t <= 39) {
  4835. return b ^ c ^ d;
  4836. } else if (t <= 59) {
  4837. return (b & c) | (b & d) | (c & d);
  4838. } else if (t <= 79) {
  4839. return b ^ c ^ d;
  4840. }
  4841. },
  4842.  
  4843. /**
  4844. * Circular left-shift operator.
  4845. * @private
  4846. */
  4847. _S: function (n, x) {
  4848. return (x << n) | (x >>> 32 - n);
  4849. },
  4850.  
  4851. /**
  4852. * Perform one cycle of SHA-1.
  4853. * @param {Uint32Array|bitArray} words one block of words.
  4854. * @private
  4855. */
  4856. _block: function (words) {
  4857. const sha1 = this;
  4858. const h = sha1._h;
  4859. // When words is passed to _block, it has 16 elements. SHA1 _block
  4860. // function extends words with new elements (at the end there are 80 elements).
  4861. // The problem is that if we use Uint32Array instead of Array,
  4862. // the length of Uint32Array cannot be changed. Thus, we replace words with a
  4863. // normal Array here.
  4864. const w = Array(80); // do not use Uint32Array here as the instantiation is slower
  4865. for (let j = 0; j < 16; j++) {
  4866. w[j] = words[j];
  4867. }
  4868.  
  4869. let a = h[0];
  4870. let b = h[1];
  4871. let c = h[2];
  4872. let d = h[3];
  4873. let e = h[4];
  4874.  
  4875. for (let t = 0; t <= 79; t++) {
  4876. if (t >= 16) {
  4877. w[t] = sha1._S(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
  4878. }
  4879. const tmp = (sha1._S(5, a) + sha1._f(t, b, c, d) + e + w[t] +
  4880. sha1._key[Math.floor(t / 20)]) | 0;
  4881. e = d;
  4882. d = c;
  4883. c = sha1._S(30, b);
  4884. b = a;
  4885. a = tmp;
  4886. }
  4887.  
  4888. h[0] = (h[0] + a) | 0;
  4889. h[1] = (h[1] + b) | 0;
  4890. h[2] = (h[2] + c) | 0;
  4891. h[3] = (h[3] + d) | 0;
  4892. h[4] = (h[4] + e) | 0;
  4893. }
  4894. };
  4895.  
  4896. /** @fileOverview Low-level AES implementation.
  4897. *
  4898. * This file contains a low-level implementation of AES, optimized for
  4899. * size and for efficiency on several browsers. It is based on
  4900. * OpenSSL's aes_core.c, a public-domain implementation by Vincent
  4901. * Rijmen, Antoon Bosselaers and Paulo Barreto.
  4902. *
  4903. * An older version of this implementation is available in the public
  4904. * domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
  4905. * Stanford University 2008-2010 and BSD-licensed for liability
  4906. * reasons.
  4907. *
  4908. * @author Emily Stark
  4909. * @author Mike Hamburg
  4910. * @author Dan Boneh
  4911. */
  4912.  
  4913. const cipher = {};
  4914.  
  4915. /**
  4916. * Schedule out an AES key for both encryption and decryption. This
  4917. * is a low-level class. Use a cipher mode to do bulk encryption.
  4918. *
  4919. * @constructor
  4920. * @param {Array} key The key as an array of 4, 6 or 8 words.
  4921. */
  4922. cipher.aes = class {
  4923. constructor(key) {
  4924. /**
  4925. * The expanded S-box and inverse S-box tables. These will be computed
  4926. * on the client so that we don't have to send them down the wire.
  4927. *
  4928. * There are two tables, _tables[0] is for encryption and
  4929. * _tables[1] is for decryption.
  4930. *
  4931. * The first 4 sub-tables are the expanded S-box with MixColumns. The
  4932. * last (_tables[01][4]) is the S-box itself.
  4933. *
  4934. * @private
  4935. */
  4936. const aes = this;
  4937. aes._tables = [[[], [], [], [], []], [[], [], [], [], []]];
  4938.  
  4939. if (!aes._tables[0][0][0]) {
  4940. aes._precompute();
  4941. }
  4942.  
  4943. const sbox = aes._tables[0][4];
  4944. const decTable = aes._tables[1];
  4945. const keyLen = key.length;
  4946.  
  4947. let i, encKey, decKey, rcon = 1;
  4948.  
  4949. if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
  4950. throw new Error("invalid aes key size");
  4951. }
  4952.  
  4953. aes._key = [encKey = key.slice(0), decKey = []];
  4954.  
  4955. // schedule encryption keys
  4956. for (i = keyLen; i < 4 * keyLen + 28; i++) {
  4957. let tmp = encKey[i - 1];
  4958.  
  4959. // apply sbox
  4960. if (i % keyLen === 0 || (keyLen === 8 && i % keyLen === 4)) {
  4961. tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
  4962.  
  4963. // shift rows and add rcon
  4964. if (i % keyLen === 0) {
  4965. tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
  4966. rcon = rcon << 1 ^ (rcon >> 7) * 283;
  4967. }
  4968. }
  4969.  
  4970. encKey[i] = encKey[i - keyLen] ^ tmp;
  4971. }
  4972.  
  4973. // schedule decryption keys
  4974. for (let j = 0; i; j++, i--) {
  4975. const tmp = encKey[j & 3 ? i : i - 4];
  4976. if (i <= 4 || j < 4) {
  4977. decKey[j] = tmp;
  4978. } else {
  4979. decKey[j] = decTable[0][sbox[tmp >>> 24]] ^
  4980. decTable[1][sbox[tmp >> 16 & 255]] ^
  4981. decTable[2][sbox[tmp >> 8 & 255]] ^
  4982. decTable[3][sbox[tmp & 255]];
  4983. }
  4984. }
  4985. }
  4986. // public
  4987. /* Something like this might appear here eventually
  4988. name: "AES",
  4989. blockSize: 4,
  4990. keySizes: [4,6,8],
  4991. */
  4992.  
  4993. /**
  4994. * Encrypt an array of 4 big-endian words.
  4995. * @param {Array} data The plaintext.
  4996. * @return {Array} The ciphertext.
  4997. */
  4998. encrypt(data) {
  4999. return this._crypt(data, 0);
  5000. }
  5001.  
  5002. /**
  5003. * Decrypt an array of 4 big-endian words.
  5004. * @param {Array} data The ciphertext.
  5005. * @return {Array} The plaintext.
  5006. */
  5007. decrypt(data) {
  5008. return this._crypt(data, 1);
  5009. }
  5010.  
  5011. /**
  5012. * Expand the S-box tables.
  5013. *
  5014. * @private
  5015. */
  5016. _precompute() {
  5017. const encTable = this._tables[0];
  5018. const decTable = this._tables[1];
  5019. const sbox = encTable[4];
  5020. const sboxInv = decTable[4];
  5021. const d = [];
  5022. const th = [];
  5023. let xInv, x2, x4, x8;
  5024.  
  5025. // Compute double and third tables
  5026. for (let i = 0; i < 256; i++) {
  5027. th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
  5028. }
  5029.  
  5030. for (let x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
  5031. // Compute sbox
  5032. let s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
  5033. s = s >> 8 ^ s & 255 ^ 99;
  5034. sbox[x] = s;
  5035. sboxInv[s] = x;
  5036.  
  5037. // Compute MixColumns
  5038. x8 = d[x4 = d[x2 = d[x]]];
  5039. let tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
  5040. let tEnc = d[s] * 0x101 ^ s * 0x1010100;
  5041.  
  5042. for (let i = 0; i < 4; i++) {
  5043. encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
  5044. decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
  5045. }
  5046. }
  5047.  
  5048. // Compactify. Considerable speedup on Firefox.
  5049. for (let i = 0; i < 5; i++) {
  5050. encTable[i] = encTable[i].slice(0);
  5051. decTable[i] = decTable[i].slice(0);
  5052. }
  5053. }
  5054.  
  5055. /**
  5056. * Encryption and decryption core.
  5057. * @param {Array} input Four words to be encrypted or decrypted.
  5058. * @param dir The direction, 0 for encrypt and 1 for decrypt.
  5059. * @return {Array} The four encrypted or decrypted words.
  5060. * @private
  5061. */
  5062. _crypt(input, dir) {
  5063. if (input.length !== 4) {
  5064. throw new Error("invalid aes block size");
  5065. }
  5066.  
  5067. const key = this._key[dir];
  5068.  
  5069. const nInnerRounds = key.length / 4 - 2;
  5070. const out = [0, 0, 0, 0];
  5071. const table = this._tables[dir];
  5072.  
  5073. // load up the tables
  5074. const t0 = table[0];
  5075. const t1 = table[1];
  5076. const t2 = table[2];
  5077. const t3 = table[3];
  5078. const sbox = table[4];
  5079.  
  5080. // state variables a,b,c,d are loaded with pre-whitened data
  5081. let a = input[0] ^ key[0];
  5082. let b = input[dir ? 3 : 1] ^ key[1];
  5083. let c = input[2] ^ key[2];
  5084. let d = input[dir ? 1 : 3] ^ key[3];
  5085. let kIndex = 4;
  5086. let a2, b2, c2;
  5087.  
  5088. // Inner rounds. Cribbed from OpenSSL.
  5089. for (let i = 0; i < nInnerRounds; i++) {
  5090. a2 = t0[a >>> 24] ^ t1[b >> 16 & 255] ^ t2[c >> 8 & 255] ^ t3[d & 255] ^ key[kIndex];
  5091. b2 = t0[b >>> 24] ^ t1[c >> 16 & 255] ^ t2[d >> 8 & 255] ^ t3[a & 255] ^ key[kIndex + 1];
  5092. c2 = t0[c >>> 24] ^ t1[d >> 16 & 255] ^ t2[a >> 8 & 255] ^ t3[b & 255] ^ key[kIndex + 2];
  5093. d = t0[d >>> 24] ^ t1[a >> 16 & 255] ^ t2[b >> 8 & 255] ^ t3[c & 255] ^ key[kIndex + 3];
  5094. kIndex += 4;
  5095. a = a2; b = b2; c = c2;
  5096. }
  5097.  
  5098. // Last round.
  5099. for (let i = 0; i < 4; i++) {
  5100. out[dir ? 3 & -i : i] =
  5101. sbox[a >>> 24] << 24 ^
  5102. sbox[b >> 16 & 255] << 16 ^
  5103. sbox[c >> 8 & 255] << 8 ^
  5104. sbox[d & 255] ^
  5105. key[kIndex++];
  5106. a2 = a; a = b; b = c; c = d; d = a2;
  5107. }
  5108.  
  5109. return out;
  5110. }
  5111. };
  5112.  
  5113. /** @fileOverview CTR mode implementation.
  5114. *
  5115. * Special thanks to Roy Nicholson for pointing out a bug in our
  5116. * implementation.
  5117. *
  5118. * @author Emily Stark
  5119. * @author Mike Hamburg
  5120. * @author Dan Boneh
  5121. */
  5122.  
  5123. /** Brian Gladman's CTR Mode.
  5124. * @constructor
  5125. * @param {Object} _prf The aes instance to generate key.
  5126. * @param {bitArray} _iv The iv for ctr mode, it must be 128 bits.
  5127. */
  5128.  
  5129. const mode = {};
  5130.  
  5131. /**
  5132. * Brian Gladman's CTR Mode.
  5133. * @namespace
  5134. */
  5135. mode.ctrGladman = class {
  5136. constructor(prf, iv) {
  5137. this._prf = prf;
  5138. this._initIv = iv;
  5139. this._iv = iv;
  5140. }
  5141.  
  5142. reset() {
  5143. this._iv = this._initIv;
  5144. }
  5145.  
  5146. /** Input some data to calculate.
  5147. * @param {bitArray} data the data to process, it must be intergral multiple of 128 bits unless it's the last.
  5148. */
  5149. update(data) {
  5150. return this.calculate(this._prf, data, this._iv);
  5151. }
  5152.  
  5153. incWord(word) {
  5154. if (((word >> 24) & 0xff) === 0xff) { //overflow
  5155. let b1 = (word >> 16) & 0xff;
  5156. let b2 = (word >> 8) & 0xff;
  5157. let b3 = word & 0xff;
  5158.  
  5159. if (b1 === 0xff) { // overflow b1
  5160. b1 = 0;
  5161. if (b2 === 0xff) {
  5162. b2 = 0;
  5163. if (b3 === 0xff) {
  5164. b3 = 0;
  5165. } else {
  5166. ++b3;
  5167. }
  5168. } else {
  5169. ++b2;
  5170. }
  5171. } else {
  5172. ++b1;
  5173. }
  5174.  
  5175. word = 0;
  5176. word += (b1 << 16);
  5177. word += (b2 << 8);
  5178. word += b3;
  5179. } else {
  5180. word += (0x01 << 24);
  5181. }
  5182. return word;
  5183. }
  5184.  
  5185. incCounter(counter) {
  5186. if ((counter[0] = this.incWord(counter[0])) === 0) {
  5187. // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
  5188. counter[1] = this.incWord(counter[1]);
  5189. }
  5190. }
  5191.  
  5192. calculate(prf, data, iv) {
  5193. let l;
  5194. if (!(l = data.length)) {
  5195. return [];
  5196. }
  5197. const bl = bitArray.bitLength(data);
  5198. for (let i = 0; i < l; i += 4) {
  5199. this.incCounter(iv);
  5200. const e = prf.encrypt(iv);
  5201. data[i] ^= e[0];
  5202. data[i + 1] ^= e[1];
  5203. data[i + 2] ^= e[2];
  5204. data[i + 3] ^= e[3];
  5205. }
  5206. return bitArray.clamp(data, bl);
  5207. }
  5208. };
  5209.  
  5210.  
  5211. const misc = {};
  5212.  
  5213. /** @fileOverview HMAC implementation.
  5214. *
  5215. * @author Emily Stark
  5216. * @author Mike Hamburg
  5217. * @author Dan Boneh
  5218. */
  5219.  
  5220. /** HMAC with the specified hash function.
  5221. * @constructor
  5222. * @param {bitArray} key the key for HMAC.
  5223. * @param {Object} [Hash=hash.sha1] The hash function to use.
  5224. */
  5225. misc.hmacSha1 = class {
  5226.  
  5227. constructor(key) {
  5228. const hmac = this;
  5229. const Hash = hmac._hash = hash.sha1;
  5230. const exKey = [[], []];
  5231. const bs = Hash.prototype.blockSize / 32;
  5232. hmac._baseHash = [new Hash(), new Hash()];
  5233.  
  5234. if (key.length > bs) {
  5235. key = Hash.hash(key);
  5236. }
  5237.  
  5238. for (let i = 0; i < bs; i++) {
  5239. exKey[0][i] = key[i] ^ 0x36363636;
  5240. exKey[1][i] = key[i] ^ 0x5C5C5C5C;
  5241. }
  5242.  
  5243. hmac._baseHash[0].update(exKey[0]);
  5244. hmac._baseHash[1].update(exKey[1]);
  5245. hmac._resultHash = new Hash(hmac._baseHash[0]);
  5246. }
  5247. reset() {
  5248. const hmac = this;
  5249. hmac._resultHash = new hmac._hash(hmac._baseHash[0]);
  5250. hmac._updated = false;
  5251. }
  5252.  
  5253. update(data) {
  5254. const hmac = this;
  5255. hmac._updated = true;
  5256. hmac._resultHash.update(data);
  5257. }
  5258.  
  5259. digest() {
  5260. const hmac = this;
  5261. const w = hmac._resultHash.finalize();
  5262. const result = new (hmac._hash)(hmac._baseHash[1]).update(w).finalize();
  5263.  
  5264. hmac.reset();
  5265.  
  5266. return result;
  5267. }
  5268. };
  5269.  
  5270. /*
  5271. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  5272.  
  5273. Redistribution and use in source and binary forms, with or without
  5274. modification, are permitted provided that the following conditions are met:
  5275.  
  5276. 1. Redistributions of source code must retain the above copyright notice,
  5277. this list of conditions and the following disclaimer.
  5278.  
  5279. 2. Redistributions in binary form must reproduce the above copyright
  5280. notice, this list of conditions and the following disclaimer in
  5281. the documentation and/or other materials provided with the distribution.
  5282.  
  5283. 3. The names of the authors may not be used to endorse or promote products
  5284. derived from this software without specific prior written permission.
  5285.  
  5286. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  5287. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  5288. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  5289. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  5290. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5291. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  5292. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5293. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5294. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5295. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5296. */
  5297.  
  5298. const ERR_INVALID_PASSWORD = "Invalid pasword";
  5299. const BLOCK_LENGTH = 16;
  5300. const RAW_FORMAT = "raw";
  5301. const PBKDF2_ALGORITHM = { name: "PBKDF2" };
  5302. const HASH_ALGORITHM = { name: "HMAC" };
  5303. const HASH_FUNCTION = "SHA-1";
  5304. const BASE_KEY_ALGORITHM = Object.assign({ hash: HASH_ALGORITHM }, PBKDF2_ALGORITHM);
  5305. const DERIVED_BITS_ALGORITHM = Object.assign({ iterations: 1000, hash: { name: HASH_FUNCTION } }, PBKDF2_ALGORITHM);
  5306. const DERIVED_BITS_USAGE = ["deriveBits"];
  5307. const SALT_LENGTH = [8, 12, 16];
  5308. const KEY_LENGTH = [16, 24, 32];
  5309. const SIGNATURE_LENGTH = 10;
  5310. const COUNTER_DEFAULT_VALUE = [0, 0, 0, 0];
  5311. const codecBytes = codec.bytes;
  5312. const Aes = cipher.aes;
  5313. const CtrGladman = mode.ctrGladman;
  5314. const HmacSha1 = misc.hmacSha1;
  5315. class AESDecrypt {
  5316.  
  5317. constructor(password, signed, strength) {
  5318. Object.assign(this, {
  5319. password,
  5320. signed,
  5321. strength: strength - 1,
  5322. pendingInput: new Uint8Array(0)
  5323. });
  5324. }
  5325.  
  5326. async append(input) {
  5327. const aesCrypto = this;
  5328. if (aesCrypto.password) {
  5329. const preamble = subarray(input, 0, SALT_LENGTH[aesCrypto.strength] + 2);
  5330. await createDecryptionKeys(aesCrypto, preamble, aesCrypto.password);
  5331. aesCrypto.password = null;
  5332. aesCrypto.aesCtrGladman = new CtrGladman(new Aes(aesCrypto.keys.key), Array.from(COUNTER_DEFAULT_VALUE));
  5333. aesCrypto.hmac = new HmacSha1(aesCrypto.keys.authentication);
  5334. input = subarray(input, SALT_LENGTH[aesCrypto.strength] + 2);
  5335. }
  5336. const output = new Uint8Array(input.length - SIGNATURE_LENGTH - ((input.length - SIGNATURE_LENGTH) % BLOCK_LENGTH));
  5337. return append(aesCrypto, input, output, 0, SIGNATURE_LENGTH, true);
  5338. }
  5339.  
  5340. flush() {
  5341. const aesCrypto = this;
  5342. const pendingInput = aesCrypto.pendingInput;
  5343. const chunkToDecrypt = subarray(pendingInput, 0, pendingInput.length - SIGNATURE_LENGTH);
  5344. const originalSignature = subarray(pendingInput, pendingInput.length - SIGNATURE_LENGTH);
  5345. let decryptedChunkArray = new Uint8Array(0);
  5346. if (chunkToDecrypt.length) {
  5347. const encryptedChunk = codecBytes.toBits(chunkToDecrypt);
  5348. aesCrypto.hmac.update(encryptedChunk);
  5349. const decryptedChunk = aesCrypto.aesCtrGladman.update(encryptedChunk);
  5350. decryptedChunkArray = codecBytes.fromBits(decryptedChunk);
  5351. }
  5352. let valid = true;
  5353. if (aesCrypto.signed) {
  5354. const signature = subarray(codecBytes.fromBits(aesCrypto.hmac.digest()), 0, SIGNATURE_LENGTH);
  5355. for (let indexSignature = 0; indexSignature < SIGNATURE_LENGTH; indexSignature++) {
  5356. if (signature[indexSignature] != originalSignature[indexSignature]) {
  5357. valid = false;
  5358. }
  5359. }
  5360. }
  5361. return {
  5362. valid,
  5363. data: decryptedChunkArray
  5364. };
  5365. }
  5366. }
  5367.  
  5368. class AESEncrypt {
  5369.  
  5370. constructor(password, strength) {
  5371. Object.assign(this, {
  5372. password,
  5373. strength: strength - 1,
  5374. pendingInput: new Uint8Array(0)
  5375. });
  5376. }
  5377.  
  5378. async append(input) {
  5379. const aesCrypto = this;
  5380. let preamble = new Uint8Array(0);
  5381. if (aesCrypto.password) {
  5382. preamble = await createEncryptionKeys(aesCrypto, aesCrypto.password);
  5383. aesCrypto.password = null;
  5384. aesCrypto.aesCtrGladman = new CtrGladman(new Aes(aesCrypto.keys.key), Array.from(COUNTER_DEFAULT_VALUE));
  5385. aesCrypto.hmac = new HmacSha1(aesCrypto.keys.authentication);
  5386. }
  5387. const output = new Uint8Array(preamble.length + input.length - (input.length % BLOCK_LENGTH));
  5388. output.set(preamble, 0);
  5389. return append(aesCrypto, input, output, preamble.length, 0);
  5390. }
  5391.  
  5392. flush() {
  5393. const aesCrypto = this;
  5394. let encryptedChunkArray = new Uint8Array(0);
  5395. if (aesCrypto.pendingInput.length) {
  5396. const encryptedChunk = aesCrypto.aesCtrGladman.update(codecBytes.toBits(aesCrypto.pendingInput));
  5397. aesCrypto.hmac.update(encryptedChunk);
  5398. encryptedChunkArray = codecBytes.fromBits(encryptedChunk);
  5399. }
  5400. const signature = subarray(codecBytes.fromBits(aesCrypto.hmac.digest()), 0, SIGNATURE_LENGTH);
  5401. return {
  5402. data: concat(encryptedChunkArray, signature),
  5403. signature
  5404. };
  5405. }
  5406. }
  5407.  
  5408. function append(aesCrypto, input, output, paddingStart, paddingEnd, verifySignature) {
  5409. const inputLength = input.length - paddingEnd;
  5410. if (aesCrypto.pendingInput.length) {
  5411. input = concat(aesCrypto.pendingInput, input);
  5412. output = expand(output, inputLength - (inputLength % BLOCK_LENGTH));
  5413. }
  5414. let offset;
  5415. for (offset = 0; offset <= inputLength - BLOCK_LENGTH; offset += BLOCK_LENGTH) {
  5416. const inputChunk = codecBytes.toBits(subarray(input, offset, offset + BLOCK_LENGTH));
  5417. if (verifySignature) {
  5418. aesCrypto.hmac.update(inputChunk);
  5419. }
  5420. const outputChunk = aesCrypto.aesCtrGladman.update(inputChunk);
  5421. if (!verifySignature) {
  5422. aesCrypto.hmac.update(outputChunk);
  5423. }
  5424. output.set(codecBytes.fromBits(outputChunk), offset + paddingStart);
  5425. }
  5426. aesCrypto.pendingInput = subarray(input, offset);
  5427. return output;
  5428. }
  5429.  
  5430. async function createDecryptionKeys(decrypt, preambleArray, password) {
  5431. await createKeys$1(decrypt, password, subarray(preambleArray, 0, SALT_LENGTH[decrypt.strength]));
  5432. const passwordVerification = subarray(preambleArray, SALT_LENGTH[decrypt.strength]);
  5433. const passwordVerificationKey = decrypt.keys.passwordVerification;
  5434. if (passwordVerificationKey[0] != passwordVerification[0] || passwordVerificationKey[1] != passwordVerification[1]) {
  5435. throw new Error(ERR_INVALID_PASSWORD);
  5436. }
  5437. }
  5438.  
  5439. async function createEncryptionKeys(encrypt, password) {
  5440. const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH[encrypt.strength]));
  5441. await createKeys$1(encrypt, password, salt);
  5442. return concat(salt, encrypt.keys.passwordVerification);
  5443. }
  5444.  
  5445. async function createKeys$1(target, password, salt) {
  5446. const encodedPassword = encodeText(password);
  5447. const basekey = await crypto.subtle.importKey(RAW_FORMAT, encodedPassword, BASE_KEY_ALGORITHM, false, DERIVED_BITS_USAGE);
  5448. const derivedBits = await crypto.subtle.deriveBits(Object.assign({ salt }, DERIVED_BITS_ALGORITHM), basekey, 8 * ((KEY_LENGTH[target.strength] * 2) + 2));
  5449. const compositeKey = new Uint8Array(derivedBits);
  5450. target.keys = {
  5451. key: codecBytes.toBits(subarray(compositeKey, 0, KEY_LENGTH[target.strength])),
  5452. authentication: codecBytes.toBits(subarray(compositeKey, KEY_LENGTH[target.strength], KEY_LENGTH[target.strength] * 2)),
  5453. passwordVerification: subarray(compositeKey, KEY_LENGTH[target.strength] * 2)
  5454. };
  5455. }
  5456.  
  5457. function concat(leftArray, rightArray) {
  5458. let array = leftArray;
  5459. if (leftArray.length + rightArray.length) {
  5460. array = new Uint8Array(leftArray.length + rightArray.length);
  5461. array.set(leftArray, 0);
  5462. array.set(rightArray, leftArray.length);
  5463. }
  5464. return array;
  5465. }
  5466.  
  5467. function expand(inputArray, length) {
  5468. if (length && length > inputArray.length) {
  5469. const array = inputArray;
  5470. inputArray = new Uint8Array(length);
  5471. inputArray.set(array, 0);
  5472. }
  5473. return inputArray;
  5474. }
  5475.  
  5476. function subarray(array, begin, end) {
  5477. return array.subarray(begin, end);
  5478. }
  5479.  
  5480. /*
  5481. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  5482.  
  5483. Redistribution and use in source and binary forms, with or without
  5484. modification, are permitted provided that the following conditions are met:
  5485.  
  5486. 1. Redistributions of source code must retain the above copyright notice,
  5487. this list of conditions and the following disclaimer.
  5488.  
  5489. 2. Redistributions in binary form must reproduce the above copyright
  5490. notice, this list of conditions and the following disclaimer in
  5491. the documentation and/or other materials provided with the distribution.
  5492.  
  5493. 3. The names of the authors may not be used to endorse or promote products
  5494. derived from this software without specific prior written permission.
  5495.  
  5496. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  5497. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  5498. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  5499. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  5500. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5501. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  5502. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5503. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5504. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5505. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5506. */
  5507.  
  5508. const HEADER_LENGTH = 12;
  5509.  
  5510. class ZipCryptoDecrypt {
  5511.  
  5512. constructor(password, passwordVerification) {
  5513. const zipCrypto = this;
  5514. Object.assign(zipCrypto, {
  5515. password,
  5516. passwordVerification
  5517. });
  5518. createKeys(zipCrypto, password);
  5519. }
  5520.  
  5521. append(input) {
  5522. const zipCrypto = this;
  5523. if (zipCrypto.password) {
  5524. const decryptedHeader = decrypt(zipCrypto, input.subarray(0, HEADER_LENGTH));
  5525. zipCrypto.password = null;
  5526. if (decryptedHeader[HEADER_LENGTH - 1] != zipCrypto.passwordVerification) {
  5527. throw new Error(ERR_INVALID_PASSWORD);
  5528. }
  5529. input = input.subarray(HEADER_LENGTH);
  5530. }
  5531. return decrypt(zipCrypto, input);
  5532. }
  5533.  
  5534. flush() {
  5535. return {
  5536. valid: true,
  5537. data: new Uint8Array(0)
  5538. };
  5539. }
  5540. }
  5541.  
  5542. class ZipCryptoEncrypt {
  5543.  
  5544. constructor(password, passwordVerification) {
  5545. const zipCrypto = this;
  5546. Object.assign(zipCrypto, {
  5547. password,
  5548. passwordVerification
  5549. });
  5550. createKeys(zipCrypto, password);
  5551. }
  5552.  
  5553. append(input) {
  5554. const zipCrypto = this;
  5555. let output;
  5556. let offset;
  5557. if (zipCrypto.password) {
  5558. zipCrypto.password = null;
  5559. const header = crypto.getRandomValues(new Uint8Array(HEADER_LENGTH));
  5560. header[HEADER_LENGTH - 1] = zipCrypto.passwordVerification;
  5561. output = new Uint8Array(input.length + header.length);
  5562. output.set(encrypt(zipCrypto, header), 0);
  5563. offset = HEADER_LENGTH;
  5564. } else {
  5565. output = new Uint8Array(input.length);
  5566. offset = 0;
  5567. }
  5568. output.set(encrypt(zipCrypto, input), offset);
  5569. return output;
  5570. }
  5571.  
  5572. flush() {
  5573. return {
  5574. data: new Uint8Array(0)
  5575. };
  5576. }
  5577. }
  5578.  
  5579. function decrypt(target, input) {
  5580. const output = new Uint8Array(input.length);
  5581. for (let index = 0; index < input.length; index++) {
  5582. output[index] = getByte(target) ^ input[index];
  5583. updateKeys(target, output[index]);
  5584. }
  5585. return output;
  5586. }
  5587.  
  5588. function encrypt(target, input) {
  5589. const output = new Uint8Array(input.length);
  5590. for (let index = 0; index < input.length; index++) {
  5591. output[index] = getByte(target) ^ input[index];
  5592. updateKeys(target, input[index]);
  5593. }
  5594. return output;
  5595. }
  5596.  
  5597. function createKeys(target, password) {
  5598. target.keys = [0x12345678, 0x23456789, 0x34567890];
  5599. target.crcKey0 = new Crc32(target.keys[0]);
  5600. target.crcKey2 = new Crc32(target.keys[2]);
  5601. for (let index = 0; index < password.length; index++) {
  5602. updateKeys(target, password.charCodeAt(index));
  5603. }
  5604. }
  5605.  
  5606. function updateKeys(target, byte) {
  5607. target.crcKey0.append([byte]);
  5608. target.keys[0] = ~target.crcKey0.get();
  5609. target.keys[1] = getInt32(target.keys[1] + getInt8(target.keys[0]));
  5610. target.keys[1] = getInt32(Math.imul(target.keys[1], 134775813) + 1);
  5611. target.crcKey2.append([target.keys[1] >>> 24]);
  5612. target.keys[2] = ~target.crcKey2.get();
  5613. }
  5614.  
  5615. function getByte(target) {
  5616. const temp = target.keys[2] | 2;
  5617. return getInt8(Math.imul(temp, (temp ^ 1)) >>> 8);
  5618. }
  5619.  
  5620. function getInt8(number) {
  5621. return number & 0xFF;
  5622. }
  5623.  
  5624. function getInt32(number) {
  5625. return number & 0xFFFFFFFF;
  5626. }
  5627.  
  5628. /*
  5629. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  5630.  
  5631. Redistribution and use in source and binary forms, with or without
  5632. modification, are permitted provided that the following conditions are met:
  5633.  
  5634. 1. Redistributions of source code must retain the above copyright notice,
  5635. this list of conditions and the following disclaimer.
  5636.  
  5637. 2. Redistributions in binary form must reproduce the above copyright
  5638. notice, this list of conditions and the following disclaimer in
  5639. the documentation and/or other materials provided with the distribution.
  5640.  
  5641. 3. The names of the authors may not be used to endorse or promote products
  5642. derived from this software without specific prior written permission.
  5643.  
  5644. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  5645. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  5646. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  5647. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  5648. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5649. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  5650. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5651. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5652. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5653. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5654. */
  5655.  
  5656. const CODEC_DEFLATE = "deflate";
  5657. const CODEC_INFLATE = "inflate";
  5658. const ERR_INVALID_SIGNATURE = "Invalid signature";
  5659.  
  5660. class Inflate {
  5661.  
  5662. constructor(codecConstructor, {
  5663. signature,
  5664. password,
  5665. signed,
  5666. compressed,
  5667. zipCrypto,
  5668. passwordVerification,
  5669. encryptionStrength
  5670. }, { chunkSize }) {
  5671. const encrypted = Boolean(password);
  5672. Object.assign(this, {
  5673. signature,
  5674. encrypted,
  5675. signed,
  5676. compressed,
  5677. inflate: compressed && new codecConstructor({ chunkSize }),
  5678. crc32: signed && new Crc32(),
  5679. zipCrypto,
  5680. decrypt: encrypted && zipCrypto ?
  5681. new ZipCryptoDecrypt(password, passwordVerification) :
  5682. new AESDecrypt(password, signed, encryptionStrength)
  5683. });
  5684. }
  5685.  
  5686. async append(data) {
  5687. const codec = this;
  5688. if (codec.encrypted && data.length) {
  5689. data = await codec.decrypt.append(data);
  5690. }
  5691. if (codec.compressed && data.length) {
  5692. data = await codec.inflate.append(data);
  5693. }
  5694. if ((!codec.encrypted || codec.zipCrypto) && codec.signed && data.length) {
  5695. codec.crc32.append(data);
  5696. }
  5697. return data;
  5698. }
  5699.  
  5700. async flush() {
  5701. const codec = this;
  5702. let signature;
  5703. let data = new Uint8Array(0);
  5704. if (codec.encrypted) {
  5705. const result = codec.decrypt.flush();
  5706. if (!result.valid) {
  5707. throw new Error(ERR_INVALID_SIGNATURE);
  5708. }
  5709. data = result.data;
  5710. }
  5711. if ((!codec.encrypted || codec.zipCrypto) && codec.signed) {
  5712. const dataViewSignature = new DataView(new Uint8Array(4).buffer);
  5713. signature = codec.crc32.get();
  5714. dataViewSignature.setUint32(0, signature);
  5715. if (codec.signature != dataViewSignature.getUint32(0, false)) {
  5716. throw new Error(ERR_INVALID_SIGNATURE);
  5717. }
  5718. }
  5719. if (codec.compressed) {
  5720. data = (await codec.inflate.append(data)) || new Uint8Array(0);
  5721. await codec.inflate.flush();
  5722. }
  5723. return { data, signature };
  5724. }
  5725. }
  5726.  
  5727. class Deflate {
  5728.  
  5729. constructor(codecConstructor, {
  5730. encrypted,
  5731. signed,
  5732. compressed,
  5733. level,
  5734. zipCrypto,
  5735. password,
  5736. passwordVerification,
  5737. encryptionStrength
  5738. }, { chunkSize }) {
  5739. Object.assign(this, {
  5740. encrypted,
  5741. signed,
  5742. compressed,
  5743. deflate: compressed && new codecConstructor({ level: level || 5, chunkSize }),
  5744. crc32: signed && new Crc32(),
  5745. zipCrypto,
  5746. encrypt: encrypted && zipCrypto ?
  5747. new ZipCryptoEncrypt(password, passwordVerification) :
  5748. new AESEncrypt(password, encryptionStrength)
  5749. });
  5750. }
  5751.  
  5752. async append(inputData) {
  5753. const codec = this;
  5754. let data = inputData;
  5755. if (codec.compressed && inputData.length) {
  5756. data = await codec.deflate.append(inputData);
  5757. }
  5758. if (codec.encrypted && data.length) {
  5759. data = await codec.encrypt.append(data);
  5760. }
  5761. if ((!codec.encrypted || codec.zipCrypto) && codec.signed && inputData.length) {
  5762. codec.crc32.append(inputData);
  5763. }
  5764. return data;
  5765. }
  5766.  
  5767. async flush() {
  5768. const codec = this;
  5769. let signature;
  5770. let data = new Uint8Array(0);
  5771. if (codec.compressed) {
  5772. data = (await codec.deflate.flush()) || new Uint8Array(0);
  5773. }
  5774. if (codec.encrypted) {
  5775. data = await codec.encrypt.append(data);
  5776. const result = codec.encrypt.flush();
  5777. signature = result.signature;
  5778. const newData = new Uint8Array(data.length + result.data.length);
  5779. newData.set(data, 0);
  5780. newData.set(result.data, data.length);
  5781. data = newData;
  5782. }
  5783. if ((!codec.encrypted || codec.zipCrypto) && codec.signed) {
  5784. signature = codec.crc32.get();
  5785. }
  5786. return { data, signature };
  5787. }
  5788. }
  5789.  
  5790. function createCodec$1(codecConstructor, options, config) {
  5791. if (options.codecType.startsWith(CODEC_DEFLATE)) {
  5792. return new Deflate(codecConstructor, options, config);
  5793. } else if (options.codecType.startsWith(CODEC_INFLATE)) {
  5794. return new Inflate(codecConstructor, options, config);
  5795. }
  5796. }
  5797.  
  5798. /*
  5799. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  5800.  
  5801. Redistribution and use in source and binary forms, with or without
  5802. modification, are permitted provided that the following conditions are met:
  5803.  
  5804. 1. Redistributions of source code must retain the above copyright notice,
  5805. this list of conditions and the following disclaimer.
  5806.  
  5807. 2. Redistributions in binary form must reproduce the above copyright
  5808. notice, this list of conditions and the following disclaimer in
  5809. the documentation and/or other materials provided with the distribution.
  5810.  
  5811. 3. The names of the authors may not be used to endorse or promote products
  5812. derived from this software without specific prior written permission.
  5813.  
  5814. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  5815. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  5816. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  5817. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  5818. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5819. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  5820. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5821. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5822. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5823. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5824. */
  5825.  
  5826. const MESSAGE_INIT = "init";
  5827. const MESSAGE_APPEND = "append";
  5828. const MESSAGE_FLUSH = "flush";
  5829. const MESSAGE_EVENT_TYPE = "message";
  5830.  
  5831. let classicWorkersSupported = true;
  5832.  
  5833. var getWorker = (workerData, codecConstructor, options, config, onTaskFinished, webWorker, scripts) => {
  5834. Object.assign(workerData, {
  5835. busy: true,
  5836. codecConstructor,
  5837. options: Object.assign({}, options),
  5838. scripts,
  5839. terminate() {
  5840. if (workerData.worker && !workerData.busy) {
  5841. workerData.worker.terminate();
  5842. workerData.interface = null;
  5843. }
  5844. },
  5845. onTaskFinished() {
  5846. workerData.busy = false;
  5847. onTaskFinished(workerData);
  5848. }
  5849. });
  5850. return webWorker ? createWebWorkerInterface(workerData, config) : createWorkerInterface(workerData, config);
  5851. };
  5852.  
  5853. function createWorkerInterface(workerData, config) {
  5854. const interfaceCodec = createCodec$1(workerData.codecConstructor, workerData.options, config);
  5855. return {
  5856. async append(data) {
  5857. try {
  5858. return await interfaceCodec.append(data);
  5859. } catch (error) {
  5860. workerData.onTaskFinished();
  5861. throw error;
  5862. }
  5863. },
  5864. async flush() {
  5865. try {
  5866. return await interfaceCodec.flush();
  5867. } finally {
  5868. workerData.onTaskFinished();
  5869. }
  5870. },
  5871. abort() {
  5872. workerData.onTaskFinished();
  5873. }
  5874. };
  5875. }
  5876.  
  5877. function createWebWorkerInterface(workerData, config) {
  5878. let messageTask;
  5879. const workerOptions = { type: "module" };
  5880. if (!workerData.interface) {
  5881. if (!classicWorkersSupported) {
  5882. workerData.worker = getWorker(workerOptions, config.baseURL);
  5883. } else {
  5884. try {
  5885. workerData.worker = getWorker({}, config.baseURL);
  5886. } catch (error) {
  5887. classicWorkersSupported = false;
  5888. workerData.worker = getWorker(workerOptions, config.baseURL);
  5889. }
  5890. }
  5891. workerData.worker.addEventListener(MESSAGE_EVENT_TYPE, onMessage, false);
  5892. workerData.interface = {
  5893. append(data) {
  5894. return initAndSendMessage({ type: MESSAGE_APPEND, data });
  5895. },
  5896. flush() {
  5897. return initAndSendMessage({ type: MESSAGE_FLUSH });
  5898. },
  5899. abort() {
  5900. workerData.onTaskFinished();
  5901. }
  5902. };
  5903. }
  5904. return workerData.interface;
  5905.  
  5906. function getWorker(options, baseURL) {
  5907. let url, scriptUrl;
  5908. url = workerData.scripts[0];
  5909. if (typeof url == "function") {
  5910. url = url();
  5911. }
  5912. try {
  5913. scriptUrl = new URL(url, baseURL);
  5914. } catch (error) {
  5915. scriptUrl = url;
  5916. }
  5917. return new Worker(scriptUrl, options);
  5918. }
  5919.  
  5920. async function initAndSendMessage(message) {
  5921. if (!messageTask) {
  5922. const options = workerData.options;
  5923. const scripts = workerData.scripts.slice(1);
  5924. await sendMessage({ scripts, type: MESSAGE_INIT, options, config: { chunkSize: config.chunkSize } });
  5925. }
  5926. return sendMessage(message);
  5927. }
  5928.  
  5929. function sendMessage(message) {
  5930. const worker = workerData.worker;
  5931. const result = new Promise((resolve, reject) => messageTask = { resolve, reject });
  5932. try {
  5933. if (message.data) {
  5934. try {
  5935. message.data = message.data.buffer;
  5936. worker.postMessage(message, [message.data]);
  5937. } catch (error) {
  5938. worker.postMessage(message);
  5939. }
  5940. } else {
  5941. worker.postMessage(message);
  5942. }
  5943. } catch (error) {
  5944. messageTask.reject(error);
  5945. messageTask = null;
  5946. workerData.onTaskFinished();
  5947. }
  5948. return result;
  5949. }
  5950.  
  5951. function onMessage(event) {
  5952. const message = event.data;
  5953. if (messageTask) {
  5954. const reponseError = message.error;
  5955. const type = message.type;
  5956. if (reponseError) {
  5957. const error = new Error(reponseError.message);
  5958. error.stack = reponseError.stack;
  5959. messageTask.reject(error);
  5960. messageTask = null;
  5961. workerData.onTaskFinished();
  5962. } else if (type == MESSAGE_INIT || type == MESSAGE_FLUSH || type == MESSAGE_APPEND) {
  5963. const data = message.data;
  5964. if (type == MESSAGE_FLUSH) {
  5965. messageTask.resolve({ data: new Uint8Array(data), signature: message.signature });
  5966. messageTask = null;
  5967. workerData.onTaskFinished();
  5968. } else {
  5969. messageTask.resolve(data && new Uint8Array(data));
  5970. }
  5971. }
  5972. }
  5973. }
  5974. }
  5975.  
  5976. /*
  5977. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  5978.  
  5979. Redistribution and use in source and binary forms, with or without
  5980. modification, are permitted provided that the following conditions are met:
  5981.  
  5982. 1. Redistributions of source code must retain the above copyright notice,
  5983. this list of conditions and the following disclaimer.
  5984.  
  5985. 2. Redistributions in binary form must reproduce the above copyright
  5986. notice, this list of conditions and the following disclaimer in
  5987. the documentation and/or other materials provided with the distribution.
  5988.  
  5989. 3. The names of the authors may not be used to endorse or promote products
  5990. derived from this software without specific prior written permission.
  5991.  
  5992. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  5993. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  5994. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  5995. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  5996. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5997. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  5998. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5999. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6000. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6001. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6002. */
  6003.  
  6004. let pool = [];
  6005. let pendingRequests = [];
  6006.  
  6007. function createCodec(codecConstructor, options, config) {
  6008. const streamCopy = !options.compressed && !options.signed && !options.encrypted;
  6009. const webWorker = !streamCopy && (options.useWebWorkers || (options.useWebWorkers === undefined && config.useWebWorkers));
  6010. const scripts = webWorker && config.workerScripts ? config.workerScripts[options.codecType] : [];
  6011. if (pool.length < config.maxWorkers) {
  6012. const workerData = {};
  6013. pool.push(workerData);
  6014. return getWorker(workerData, codecConstructor, options, config, onTaskFinished, webWorker, scripts);
  6015. } else {
  6016. const workerData = pool.find(workerData => !workerData.busy);
  6017. if (workerData) {
  6018. clearTerminateTimeout(workerData);
  6019. return getWorker(workerData, codecConstructor, options, config, onTaskFinished, webWorker, scripts);
  6020. } else {
  6021. return new Promise(resolve => pendingRequests.push({ resolve, codecConstructor, options, webWorker, scripts }));
  6022. }
  6023. }
  6024.  
  6025. function onTaskFinished(workerData) {
  6026. if (pendingRequests.length) {
  6027. const [{ resolve, codecConstructor, options, webWorker, scripts }] = pendingRequests.splice(0, 1);
  6028. resolve(getWorker(workerData, codecConstructor, options, config, onTaskFinished, webWorker, scripts));
  6029. } else if (workerData.worker) {
  6030. clearTerminateTimeout(workerData);
  6031. if (Number.isFinite(config.terminateWorkerTimeout) && config.terminateWorkerTimeout >= 0) {
  6032. workerData.terminateTimeout = setTimeout(() => {
  6033. pool = pool.filter(data => data != workerData);
  6034. workerData.terminate();
  6035. }, config.terminateWorkerTimeout);
  6036. }
  6037. } else {
  6038. pool = pool.filter(data => data != workerData);
  6039. }
  6040. }
  6041. }
  6042.  
  6043. function clearTerminateTimeout(workerData) {
  6044. if (workerData.terminateTimeout) {
  6045. clearTimeout(workerData.terminateTimeout);
  6046. workerData.terminateTimeout = null;
  6047. }
  6048. }
  6049.  
  6050. function terminateWorkers() {
  6051. pool.forEach(workerData => {
  6052. clearTerminateTimeout(workerData);
  6053. workerData.terminate();
  6054. });
  6055. }
  6056.  
  6057. /*
  6058. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6059.  
  6060. Redistribution and use in source and binary forms, with or without
  6061. modification, are permitted provided that the following conditions are met:
  6062.  
  6063. 1. Redistributions of source code must retain the above copyright notice,
  6064. this list of conditions and the following disclaimer.
  6065.  
  6066. 2. Redistributions in binary form must reproduce the above copyright
  6067. notice, this list of conditions and the following disclaimer in
  6068. the documentation and/or other materials provided with the distribution.
  6069.  
  6070. 3. The names of the authors may not be used to endorse or promote products
  6071. derived from this software without specific prior written permission.
  6072.  
  6073. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6074. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6075. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6076. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6077. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6078. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6079. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6080. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6081. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6082. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6083. */
  6084.  
  6085. const MINIMUM_CHUNK_SIZE = 64;
  6086. const ERR_ABORT = "Abort error";
  6087.  
  6088. async function processData(codec, reader, writer, offset, inputLength, config, options) {
  6089. const chunkSize = Math.max(config.chunkSize, MINIMUM_CHUNK_SIZE);
  6090. return processChunk();
  6091.  
  6092. async function processChunk(chunkOffset = 0, outputLength = 0) {
  6093. const signal = options.signal;
  6094. if (chunkOffset < inputLength) {
  6095. testAborted(signal, codec);
  6096. const inputData = await reader.readUint8Array(chunkOffset + offset, Math.min(chunkSize, inputLength - chunkOffset));
  6097. const chunkLength = inputData.length;
  6098. testAborted(signal, codec);
  6099. const data = await codec.append(inputData);
  6100. testAborted(signal, codec);
  6101. outputLength += await writeData(writer, data);
  6102. if (options.onprogress) {
  6103. try {
  6104. options.onprogress(chunkOffset + chunkLength, inputLength);
  6105. } catch (error) {
  6106. // ignored
  6107. }
  6108. }
  6109. return processChunk(chunkOffset + chunkSize, outputLength);
  6110. } else {
  6111. const result = await codec.flush();
  6112. outputLength += await writeData(writer, result.data);
  6113. return { signature: result.signature, length: outputLength };
  6114. }
  6115. }
  6116. }
  6117.  
  6118. function testAborted(signal, codec) {
  6119. if (signal && signal.aborted) {
  6120. codec.abort();
  6121. throw new Error(ERR_ABORT);
  6122. }
  6123. }
  6124.  
  6125. async function writeData(writer, data) {
  6126. if (data.length) {
  6127. await writer.writeUint8Array(data);
  6128. }
  6129. return data.length;
  6130. }
  6131.  
  6132. /*
  6133. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6134.  
  6135. Redistribution and use in source and binary forms, with or without
  6136. modification, are permitted provided that the following conditions are met:
  6137.  
  6138. 1. Redistributions of source code must retain the above copyright notice,
  6139. this list of conditions and the following disclaimer.
  6140.  
  6141. 2. Redistributions in binary form must reproduce the above copyright
  6142. notice, this list of conditions and the following disclaimer in
  6143. the documentation and/or other materials provided with the distribution.
  6144.  
  6145. 3. The names of the authors may not be used to endorse or promote products
  6146. derived from this software without specific prior written permission.
  6147.  
  6148. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6149. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6150. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6151. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6152. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6153. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6154. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6155. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6156. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6157. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6158. */
  6159.  
  6160. /* global Blob, FileReader, atob, btoa, XMLHttpRequest, document, fetch */
  6161.  
  6162. const ERR_HTTP_STATUS = "HTTP error ";
  6163. const ERR_HTTP_RANGE = "HTTP Range not supported";
  6164.  
  6165. const CONTENT_TYPE_TEXT_PLAIN = "text/plain";
  6166. const HTTP_HEADER_CONTENT_LENGTH = "Content-Length";
  6167. const HTTP_HEADER_CONTENT_RANGE = "Content-Range";
  6168. const HTTP_HEADER_ACCEPT_RANGES = "Accept-Ranges";
  6169. const HTTP_HEADER_RANGE = "Range";
  6170. const HTTP_METHOD_HEAD = "HEAD";
  6171. const HTTP_METHOD_GET = "GET";
  6172. const HTTP_RANGE_UNIT = "bytes";
  6173.  
  6174. class Stream {
  6175.  
  6176. constructor() {
  6177. this.size = 0;
  6178. }
  6179.  
  6180. init() {
  6181. this.initialized = true;
  6182. }
  6183. }
  6184.  
  6185. class Reader extends Stream {
  6186. }
  6187.  
  6188. class Writer extends Stream {
  6189.  
  6190. writeUint8Array(array) {
  6191. this.size += array.length;
  6192. }
  6193. }
  6194.  
  6195. class TextReader extends Reader {
  6196.  
  6197. constructor(text) {
  6198. super();
  6199. this.blobReader = new BlobReader(new Blob([text], { type: CONTENT_TYPE_TEXT_PLAIN }));
  6200. }
  6201.  
  6202. async init() {
  6203. super.init();
  6204. this.blobReader.init();
  6205. this.size = this.blobReader.size;
  6206. }
  6207.  
  6208. async readUint8Array(offset, length) {
  6209. return this.blobReader.readUint8Array(offset, length);
  6210. }
  6211. }
  6212.  
  6213. class TextWriter extends Writer {
  6214.  
  6215. constructor(encoding) {
  6216. super();
  6217. this.encoding = encoding;
  6218. this.blob = new Blob([], { type: CONTENT_TYPE_TEXT_PLAIN });
  6219. }
  6220.  
  6221. async writeUint8Array(array) {
  6222. super.writeUint8Array(array);
  6223. this.blob = new Blob([this.blob, array.buffer], { type: CONTENT_TYPE_TEXT_PLAIN });
  6224. }
  6225.  
  6226. getData() {
  6227. if (this.blob.text) {
  6228. return this.blob.text();
  6229. } else {
  6230. const reader = new FileReader();
  6231. return new Promise((resolve, reject) => {
  6232. reader.onload = event => resolve(event.target.result);
  6233. reader.onerror = () => reject(reader.error);
  6234. reader.readAsText(this.blob, this.encoding);
  6235. });
  6236. }
  6237. }
  6238. }
  6239.  
  6240. class Data64URIReader extends Reader {
  6241.  
  6242. constructor(dataURI) {
  6243. super();
  6244. this.dataURI = dataURI;
  6245. let dataEnd = dataURI.length;
  6246. while (dataURI.charAt(dataEnd - 1) == "=") {
  6247. dataEnd--;
  6248. }
  6249. this.dataStart = dataURI.indexOf(",") + 1;
  6250. this.size = Math.floor((dataEnd - this.dataStart) * 0.75);
  6251. }
  6252.  
  6253. async readUint8Array(offset, length) {
  6254. const dataArray = new Uint8Array(length);
  6255. const start = Math.floor(offset / 3) * 4;
  6256. const bytes = atob(this.dataURI.substring(start + this.dataStart, Math.ceil((offset + length) / 3) * 4 + this.dataStart));
  6257. const delta = offset - Math.floor(start / 4) * 3;
  6258. for (let indexByte = delta; indexByte < delta + length; indexByte++) {
  6259. dataArray[indexByte - delta] = bytes.charCodeAt(indexByte);
  6260. }
  6261. return dataArray;
  6262. }
  6263. }
  6264.  
  6265. class Data64URIWriter extends Writer {
  6266.  
  6267. constructor(contentType) {
  6268. super();
  6269. this.data = "data:" + (contentType || "") + ";base64,";
  6270. this.pending = [];
  6271. }
  6272.  
  6273. async writeUint8Array(array) {
  6274. super.writeUint8Array(array);
  6275. let indexArray = 0;
  6276. let dataString = this.pending;
  6277. const delta = this.pending.length;
  6278. this.pending = "";
  6279. for (indexArray = 0; indexArray < (Math.floor((delta + array.length) / 3) * 3) - delta; indexArray++) {
  6280. dataString += String.fromCharCode(array[indexArray]);
  6281. }
  6282. for (; indexArray < array.length; indexArray++) {
  6283. this.pending += String.fromCharCode(array[indexArray]);
  6284. }
  6285. if (dataString.length > 2) {
  6286. this.data += btoa(dataString);
  6287. } else {
  6288. this.pending = dataString;
  6289. }
  6290. }
  6291.  
  6292. getData() {
  6293. return this.data + btoa(this.pending);
  6294. }
  6295. }
  6296.  
  6297. class BlobReader extends Reader {
  6298.  
  6299. constructor(blob) {
  6300. super();
  6301. this.blob = blob;
  6302. this.size = blob.size;
  6303. }
  6304.  
  6305. async readUint8Array(offset, length) {
  6306. if (this.blob.arrayBuffer) {
  6307. return new Uint8Array(await this.blob.slice(offset, offset + length).arrayBuffer());
  6308. } else {
  6309. const reader = new FileReader();
  6310. return new Promise((resolve, reject) => {
  6311. reader.onload = event => resolve(new Uint8Array(event.target.result));
  6312. reader.onerror = () => reject(reader.error);
  6313. reader.readAsArrayBuffer(this.blob.slice(offset, offset + length));
  6314. });
  6315. }
  6316. }
  6317. }
  6318.  
  6319. class BlobWriter extends Writer {
  6320.  
  6321. constructor(contentType) {
  6322. super();
  6323. this.contentType = contentType;
  6324. this.arrayBuffers = [];
  6325. }
  6326.  
  6327. async writeUint8Array(array) {
  6328. super.writeUint8Array(array);
  6329. this.arrayBuffers.push(array.buffer);
  6330. }
  6331.  
  6332. getData() {
  6333. if (!this.blob) {
  6334. this.blob = new Blob(this.arrayBuffers, { type: this.contentType });
  6335. }
  6336. return this.blob;
  6337. }
  6338. }
  6339.  
  6340. class WritableStreamWriter extends Writer {
  6341. constructor(writableStream) {
  6342. super();
  6343. this.writableStream = writableStream;
  6344. this.writer = writableStream.getWriter();
  6345. }
  6346.  
  6347. async writeUint8Array(array) {
  6348. await this.writer.ready;
  6349. return this.writer.write(array);
  6350. }
  6351.  
  6352. async getData() {
  6353. await this.writer.ready;
  6354. await this.writer.close();
  6355. return this.writableStream;
  6356. }
  6357. }
  6358.  
  6359. class FetchReader extends Reader {
  6360.  
  6361. constructor(url, options) {
  6362. super();
  6363. this.url = url;
  6364. this.preventHeadRequest = options.preventHeadRequest;
  6365. this.useRangeHeader = options.useRangeHeader;
  6366. this.forceRangeRequests = options.forceRangeRequests;
  6367. this.options = Object.assign({}, options);
  6368. delete this.options.preventHeadRequest;
  6369. delete this.options.useRangeHeader;
  6370. delete this.options.forceRangeRequests;
  6371. delete this.options.useXHR;
  6372. }
  6373.  
  6374. async init() {
  6375. super.init();
  6376. await initHttpReader(this, sendFetchRequest, getFetchRequestData);
  6377. }
  6378.  
  6379. async readUint8Array(index, length) {
  6380. return readUint8ArrayHttpReader(this, index, length, sendFetchRequest, getFetchRequestData);
  6381. }
  6382. }
  6383.  
  6384. class XHRReader extends Reader {
  6385.  
  6386. constructor(url, options) {
  6387. super();
  6388. this.url = url;
  6389. this.preventHeadRequest = options.preventHeadRequest;
  6390. this.useRangeHeader = options.useRangeHeader;
  6391. this.forceRangeRequests = options.forceRangeRequests;
  6392. this.options = options;
  6393. }
  6394.  
  6395. async init() {
  6396. super.init();
  6397. await initHttpReader(this, sendXMLHttpRequest, getXMLHttpRequestData);
  6398. }
  6399.  
  6400. async readUint8Array(index, length) {
  6401. return readUint8ArrayHttpReader(this, index, length, sendXMLHttpRequest, getXMLHttpRequestData);
  6402. }
  6403. }
  6404.  
  6405. async function initHttpReader(httpReader, sendRequest, getRequestData) {
  6406. if (isHttpFamily(httpReader.url) && (httpReader.useRangeHeader || httpReader.forceRangeRequests)) {
  6407. const response = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader));
  6408. if (!httpReader.forceRangeRequests && response.headers.get(HTTP_HEADER_ACCEPT_RANGES) != HTTP_RANGE_UNIT) {
  6409. throw new Error(ERR_HTTP_RANGE);
  6410. } else {
  6411. let contentSize;
  6412. const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
  6413. if (contentRangeHeader) {
  6414. const splitHeader = contentRangeHeader.trim().split(/\s*\/\s*/);
  6415. if (splitHeader.length) {
  6416. const headerValue = splitHeader[1];
  6417. if (headerValue && headerValue != "*") {
  6418. contentSize = Number(headerValue);
  6419. }
  6420. }
  6421. }
  6422. if (contentSize === undefined) {
  6423. await getContentLength(httpReader, sendRequest, getRequestData);
  6424. } else {
  6425. httpReader.size = contentSize;
  6426. }
  6427. }
  6428. } else {
  6429. await getContentLength(httpReader, sendRequest, getRequestData);
  6430. }
  6431. }
  6432.  
  6433. async function readUint8ArrayHttpReader(httpReader, index, length, sendRequest, getRequestData) {
  6434. if (httpReader.useRangeHeader || httpReader.forceRangeRequests) {
  6435. const response = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader, index, length));
  6436. if (response.status != 206) {
  6437. throw new Error(ERR_HTTP_RANGE);
  6438. }
  6439. return new Uint8Array(await response.arrayBuffer());
  6440. } else {
  6441. if (!httpReader.data) {
  6442. await getRequestData(httpReader, httpReader.options);
  6443. }
  6444. return new Uint8Array(httpReader.data.subarray(index, index + length));
  6445. }
  6446. }
  6447.  
  6448. function getRangeHeaders(httpReader, index = 0, length = 1) {
  6449. return Object.assign({}, getHeaders(httpReader), { [HTTP_HEADER_RANGE]: HTTP_RANGE_UNIT + "=" + index + "-" + (index + length - 1) });
  6450. }
  6451.  
  6452. function getHeaders(httpReader) {
  6453. let headers = httpReader.options.headers;
  6454. if (headers) {
  6455. if (Symbol.iterator in headers) {
  6456. return Object.fromEntries(headers);
  6457. } else {
  6458. return headers;
  6459. }
  6460. }
  6461. }
  6462.  
  6463. async function getFetchRequestData(httpReader) {
  6464. await getRequestData(httpReader, sendFetchRequest);
  6465. }
  6466.  
  6467. async function getXMLHttpRequestData(httpReader) {
  6468. await getRequestData(httpReader, sendXMLHttpRequest);
  6469. }
  6470.  
  6471. async function getRequestData(httpReader, sendRequest) {
  6472. const response = await sendRequest(HTTP_METHOD_GET, httpReader, getHeaders(httpReader));
  6473. httpReader.data = new Uint8Array(await response.arrayBuffer());
  6474. if (!httpReader.size) {
  6475. httpReader.size = httpReader.data.length;
  6476. }
  6477. }
  6478.  
  6479. async function getContentLength(httpReader, sendRequest, getRequestData) {
  6480. if (httpReader.preventHeadRequest) {
  6481. await getRequestData(httpReader, httpReader.options);
  6482. } else {
  6483. const response = await sendRequest(HTTP_METHOD_HEAD, httpReader, getHeaders(httpReader));
  6484. const contentLength = response.headers.get(HTTP_HEADER_CONTENT_LENGTH);
  6485. if (contentLength) {
  6486. httpReader.size = Number(contentLength);
  6487. } else {
  6488. await getRequestData(httpReader, httpReader.options);
  6489. }
  6490. }
  6491. }
  6492.  
  6493. async function sendFetchRequest(method, { options, url }, headers) {
  6494. const response = await fetch(url, Object.assign({}, options, { method, headers }));
  6495. if (response.status < 400) {
  6496. return response;
  6497. } else {
  6498. throw new Error(ERR_HTTP_STATUS + (response.statusText || response.status));
  6499. }
  6500. }
  6501.  
  6502. function sendXMLHttpRequest(method, { url }, headers) {
  6503. return new Promise((resolve, reject) => {
  6504. const request = new XMLHttpRequest();
  6505. request.addEventListener("load", () => {
  6506. if (request.status < 400) {
  6507. const headers = [];
  6508. request.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach(header => {
  6509. const splitHeader = header.trim().split(/\s*:\s*/);
  6510. splitHeader[0] = splitHeader[0].trim().replace(/^[a-z]|-[a-z]/g, value => value.toUpperCase());
  6511. headers.push(splitHeader);
  6512. });
  6513. resolve({
  6514. status: request.status,
  6515. arrayBuffer: () => request.response,
  6516. headers: new Map(headers)
  6517. });
  6518. } else {
  6519. reject(new Error(ERR_HTTP_STATUS + (request.statusText || request.status)));
  6520. }
  6521. }, false);
  6522. request.addEventListener("error", event => reject(event.detail.error), false);
  6523. request.open(method, url);
  6524. if (headers) {
  6525. for (const entry of Object.entries(headers)) {
  6526. request.setRequestHeader(entry[0], entry[1]);
  6527. }
  6528. }
  6529. request.responseType = "arraybuffer";
  6530. request.send();
  6531. });
  6532. }
  6533.  
  6534. class HttpReader extends Reader {
  6535.  
  6536. constructor(url, options = {}) {
  6537. super();
  6538. this.url = url;
  6539. if (options.useXHR) {
  6540. this.reader = new XHRReader(url, options);
  6541. } else {
  6542. this.reader = new FetchReader(url, options);
  6543. }
  6544. }
  6545.  
  6546. set size(value) {
  6547. // ignored
  6548. }
  6549.  
  6550. get size() {
  6551. return this.reader.size;
  6552. }
  6553.  
  6554. async init() {
  6555. super.init();
  6556. await this.reader.init();
  6557. }
  6558.  
  6559. async readUint8Array(index, length) {
  6560. return this.reader.readUint8Array(index, length);
  6561. }
  6562. }
  6563.  
  6564. class HttpRangeReader extends HttpReader {
  6565.  
  6566. constructor(url, options = {}) {
  6567. options.useRangeHeader = true;
  6568. super(url, options);
  6569. }
  6570. }
  6571.  
  6572.  
  6573. class Uint8ArrayReader extends Reader {
  6574.  
  6575. constructor(array) {
  6576. super();
  6577. this.array = array;
  6578. this.size = array.length;
  6579. }
  6580.  
  6581. async readUint8Array(index, length) {
  6582. return this.array.slice(index, index + length);
  6583. }
  6584. }
  6585.  
  6586. class Uint8ArrayWriter extends Writer {
  6587.  
  6588. constructor() {
  6589. super();
  6590. this.array = new Uint8Array(0);
  6591. }
  6592.  
  6593. async writeUint8Array(array) {
  6594. super.writeUint8Array(array);
  6595. const previousArray = this.array;
  6596. this.array = new Uint8Array(previousArray.length + array.length);
  6597. this.array.set(previousArray);
  6598. this.array.set(array, previousArray.length);
  6599. }
  6600.  
  6601. getData() {
  6602. return this.array;
  6603. }
  6604. }
  6605.  
  6606. function isHttpFamily(url) {
  6607. if (typeof document != "undefined") {
  6608. const anchor = document.createElement("a");
  6609. anchor.href = url;
  6610. return anchor.protocol == "http:" || anchor.protocol == "https:";
  6611. } else {
  6612. return /^https?:\/\//i.test(url);
  6613. }
  6614. }
  6615.  
  6616. /*
  6617. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6618.  
  6619. Redistribution and use in source and binary forms, with or without
  6620. modification, are permitted provided that the following conditions are met:
  6621.  
  6622. 1. Redistributions of source code must retain the above copyright notice,
  6623. this list of conditions and the following disclaimer.
  6624.  
  6625. 2. Redistributions in binary form must reproduce the above copyright
  6626. notice, this list of conditions and the following disclaimer in
  6627. the documentation and/or other materials provided with the distribution.
  6628.  
  6629. 3. The names of the authors may not be used to endorse or promote products
  6630. derived from this software without specific prior written permission.
  6631.  
  6632. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6633. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6634. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6635. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6636. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6637. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6638. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6639. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6640. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6641. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6642. */
  6643.  
  6644. const MAX_32_BITS = 0xffffffff;
  6645. const MAX_16_BITS = 0xffff;
  6646. const COMPRESSION_METHOD_DEFLATE = 0x08;
  6647. const COMPRESSION_METHOD_STORE = 0x00;
  6648. const COMPRESSION_METHOD_AES = 0x63;
  6649.  
  6650. const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
  6651. const DATA_DESCRIPTOR_RECORD_SIGNATURE = 0x08074b50;
  6652. const CENTRAL_FILE_HEADER_SIGNATURE = 0x02014b50;
  6653. const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
  6654. const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
  6655. const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE = 0x07064b50;
  6656. const END_OF_CENTRAL_DIR_LENGTH = 22;
  6657. const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH = 20;
  6658. const ZIP64_END_OF_CENTRAL_DIR_LENGTH = 56;
  6659. const ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH = END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LENGTH;
  6660.  
  6661. const ZIP64_TOTAL_NUMBER_OF_DISKS = 1;
  6662.  
  6663. const EXTRAFIELD_TYPE_ZIP64 = 0x0001;
  6664. const EXTRAFIELD_TYPE_AES = 0x9901;
  6665. const EXTRAFIELD_TYPE_NTFS = 0x000a;
  6666. const EXTRAFIELD_TYPE_NTFS_TAG1 = 0x0001;
  6667. const EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP = 0x5455;
  6668. const EXTRAFIELD_TYPE_UNICODE_PATH = 0x7075;
  6669. const EXTRAFIELD_TYPE_UNICODE_COMMENT = 0x6375;
  6670.  
  6671. const BITFLAG_ENCRYPTED = 0x01;
  6672. const BITFLAG_LEVEL = 0x06;
  6673. const BITFLAG_DATA_DESCRIPTOR = 0x0008;
  6674. const BITFLAG_LANG_ENCODING_FLAG = 0x0800;
  6675. const FILE_ATTR_MSDOS_DIR_MASK = 0x10;
  6676.  
  6677. const VERSION_DEFLATE = 0x14;
  6678. const VERSION_ZIP64 = 0x2D;
  6679. const VERSION_AES = 0x33;
  6680.  
  6681. const DIRECTORY_SIGNATURE = "/";
  6682.  
  6683. const MAX_DATE = new Date(2107, 11, 31);
  6684. const MIN_DATE = new Date(1980, 0, 1);
  6685.  
  6686. /*
  6687. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6688.  
  6689. Redistribution and use in source and binary forms, with or without
  6690. modification, are permitted provided that the following conditions are met:
  6691.  
  6692. 1. Redistributions of source code must retain the above copyright notice,
  6693. this list of conditions and the following disclaimer.
  6694.  
  6695. 2. Redistributions in binary form must reproduce the above copyright
  6696. notice, this list of conditions and the following disclaimer in
  6697. the documentation and/or other materials provided with the distribution.
  6698.  
  6699. 3. The names of the authors may not be used to endorse or promote products
  6700. derived from this software without specific prior written permission.
  6701.  
  6702. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6703. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6704. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6705. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6706. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6707. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6708. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6709. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6710. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6711. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6712. */
  6713.  
  6714. const CP437 = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ".split("");
  6715.  
  6716. var decodeCP437 = stringValue => {
  6717. let result = "";
  6718. for (let indexCharacter = 0; indexCharacter < stringValue.length; indexCharacter++) {
  6719. result += CP437[stringValue[indexCharacter]];
  6720. }
  6721. return result;
  6722. };
  6723.  
  6724. /*
  6725. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6726.  
  6727. Redistribution and use in source and binary forms, with or without
  6728. modification, are permitted provided that the following conditions are met:
  6729.  
  6730. 1. Redistributions of source code must retain the above copyright notice,
  6731. this list of conditions and the following disclaimer.
  6732.  
  6733. 2. Redistributions in binary form must reproduce the above copyright
  6734. notice, this list of conditions and the following disclaimer in
  6735. the documentation and/or other materials provided with the distribution.
  6736.  
  6737. 3. The names of the authors may not be used to endorse or promote products
  6738. derived from this software without specific prior written permission.
  6739.  
  6740. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6741. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6742. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6743. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6744. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6745. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6746. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6747. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6748. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6749. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6750. */
  6751.  
  6752. async function decodeText(value, encoding) {
  6753. if (encoding && encoding.trim().toLowerCase() == "cp437") {
  6754. return decodeCP437(value);
  6755. } else if (typeof TextDecoder == "undefined") {
  6756. const fileReader = new FileReader();
  6757. return new Promise((resolve, reject) => {
  6758. fileReader.onload = event => resolve(event.target.result);
  6759. fileReader.onerror = () => reject(fileReader.error);
  6760. fileReader.readAsText(new Blob([value]));
  6761. });
  6762. } else {
  6763. return new TextDecoder(encoding).decode(value);
  6764. }
  6765. }
  6766.  
  6767. /*
  6768. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6769.  
  6770. Redistribution and use in source and binary forms, with or without
  6771. modification, are permitted provided that the following conditions are met:
  6772.  
  6773. 1. Redistributions of source code must retain the above copyright notice,
  6774. this list of conditions and the following disclaimer.
  6775.  
  6776. 2. Redistributions in binary form must reproduce the above copyright
  6777. notice, this list of conditions and the following disclaimer in
  6778. the documentation and/or other materials provided with the distribution.
  6779.  
  6780. 3. The names of the authors may not be used to endorse or promote products
  6781. derived from this software without specific prior written permission.
  6782.  
  6783. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6784. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6785. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6786. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6787. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6788. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6789. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6790. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6791. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6792. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6793. */
  6794.  
  6795. const PROPERTY_NAMES = [
  6796. "filename", "rawFilename", "directory", "encrypted", "compressedSize", "uncompressedSize",
  6797. "lastModDate", "rawLastModDate", "comment", "rawComment", "signature", "extraField",
  6798. "rawExtraField", "bitFlag", "extraFieldZip64", "extraFieldUnicodePath", "extraFieldUnicodeComment",
  6799. "extraFieldAES", "filenameUTF8", "commentUTF8", "offset", "zip64", "compressionMethod",
  6800. "extraFieldNTFS", "lastAccessDate", "creationDate", "extraFieldExtendedTimestamp",
  6801. "version", "versionMadeBy", "msDosCompatible", "internalFileAttribute", "externalFileAttribute"];
  6802.  
  6803. class Entry {
  6804.  
  6805. constructor(data) {
  6806. PROPERTY_NAMES.forEach(name => this[name] = data[name]);
  6807. }
  6808.  
  6809. }
  6810.  
  6811. /*
  6812. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  6813.  
  6814. Redistribution and use in source and binary forms, with or without
  6815. modification, are permitted provided that the following conditions are met:
  6816.  
  6817. 1. Redistributions of source code must retain the above copyright notice,
  6818. this list of conditions and the following disclaimer.
  6819.  
  6820. 2. Redistributions in binary form must reproduce the above copyright
  6821. notice, this list of conditions and the following disclaimer in
  6822. the documentation and/or other materials provided with the distribution.
  6823.  
  6824. 3. The names of the authors may not be used to endorse or promote products
  6825. derived from this software without specific prior written permission.
  6826.  
  6827. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  6828. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  6829. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  6830. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  6831. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6832. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  6833. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6834. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6835. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6836. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6837. */
  6838.  
  6839. const ERR_BAD_FORMAT = "File format is not recognized";
  6840. const ERR_EOCDR_NOT_FOUND = "End of central directory not found";
  6841. const ERR_EOCDR_ZIP64_NOT_FOUND = "End of Zip64 central directory not found";
  6842. const ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND = "End of Zip64 central directory locator not found";
  6843. const ERR_CENTRAL_DIRECTORY_NOT_FOUND = "Central directory header not found";
  6844. const ERR_LOCAL_FILE_HEADER_NOT_FOUND = "Local file header not found";
  6845. const ERR_EXTRAFIELD_ZIP64_NOT_FOUND = "Zip64 extra field not found";
  6846. const ERR_ENCRYPTED = "File contains encrypted entry";
  6847. const ERR_UNSUPPORTED_ENCRYPTION = "Encryption method not supported";
  6848. const ERR_UNSUPPORTED_COMPRESSION = "Compression method not supported";
  6849. const CHARSET_UTF8 = "utf-8";
  6850. const CHARSET_CP437 = "cp437";
  6851. const ZIP64_PROPERTIES = ["uncompressedSize", "compressedSize", "offset"];
  6852.  
  6853. class ZipReader {
  6854.  
  6855. constructor(reader, options = {}) {
  6856. Object.assign(this, {
  6857. reader,
  6858. options,
  6859. config: getConfiguration()
  6860. });
  6861. }
  6862.  
  6863. async getEntries(options = {}) {
  6864. const zipReader = this;
  6865. const reader = zipReader.reader;
  6866. if (!reader.initialized) {
  6867. await reader.init();
  6868. }
  6869. if (reader.size < END_OF_CENTRAL_DIR_LENGTH) {
  6870. throw new Error(ERR_BAD_FORMAT);
  6871. }
  6872. const endOfDirectoryInfo = await seekSignature(reader, END_OF_CENTRAL_DIR_SIGNATURE, reader.size, END_OF_CENTRAL_DIR_LENGTH, MAX_16_BITS * 16);
  6873. if (!endOfDirectoryInfo) {
  6874. throw new Error(ERR_EOCDR_NOT_FOUND);
  6875. }
  6876. const endOfDirectoryView = getDataView$1(endOfDirectoryInfo);
  6877. let directoryDataLength = getUint32(endOfDirectoryView, 12);
  6878. let directoryDataOffset = getUint32(endOfDirectoryView, 16);
  6879. let filesLength = getUint16(endOfDirectoryView, 8);
  6880. let prependedDataLength = 0;
  6881. if (directoryDataOffset == MAX_32_BITS || directoryDataLength == MAX_32_BITS || filesLength == MAX_16_BITS) {
  6882. const endOfDirectoryLocatorArray = await readUint8Array(reader, endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH);
  6883. const endOfDirectoryLocatorView = getDataView$1(endOfDirectoryLocatorArray);
  6884. if (getUint32(endOfDirectoryLocatorView, 0) != ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE) {
  6885. throw new Error(ERR_EOCDR_ZIP64_NOT_FOUND);
  6886. }
  6887. directoryDataOffset = getBigUint64(endOfDirectoryLocatorView, 8);
  6888. let endOfDirectoryArray = await readUint8Array(reader, directoryDataOffset, ZIP64_END_OF_CENTRAL_DIR_LENGTH);
  6889. let endOfDirectoryView = getDataView$1(endOfDirectoryArray);
  6890. const expectedDirectoryDataOffset = endOfDirectoryInfo.offset - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH - ZIP64_END_OF_CENTRAL_DIR_LENGTH;
  6891. if (getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE && directoryDataOffset != expectedDirectoryDataOffset) {
  6892. const originalDirectoryDataOffset = directoryDataOffset;
  6893. directoryDataOffset = expectedDirectoryDataOffset;
  6894. prependedDataLength = directoryDataOffset - originalDirectoryDataOffset;
  6895. endOfDirectoryArray = await readUint8Array(reader, directoryDataOffset, ZIP64_END_OF_CENTRAL_DIR_LENGTH);
  6896. endOfDirectoryView = getDataView$1(endOfDirectoryArray);
  6897. }
  6898. if (getUint32(endOfDirectoryView, 0) != ZIP64_END_OF_CENTRAL_DIR_SIGNATURE) {
  6899. throw new Error(ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND);
  6900. }
  6901. filesLength = getBigUint64(endOfDirectoryView, 32);
  6902. directoryDataLength = getBigUint64(endOfDirectoryView, 40);
  6903. directoryDataOffset -= directoryDataLength;
  6904. }
  6905. if (directoryDataOffset < 0 || directoryDataOffset >= reader.size) {
  6906. throw new Error(ERR_BAD_FORMAT);
  6907. }
  6908. let offset = 0;
  6909. let directoryArray = await readUint8Array(reader, directoryDataOffset, directoryDataLength);
  6910. let directoryView = getDataView$1(directoryArray);
  6911. if (directoryDataLength) {
  6912. const expectedDirectoryDataOffset = endOfDirectoryInfo.offset - directoryDataLength;
  6913. if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE && directoryDataOffset != expectedDirectoryDataOffset) {
  6914. const originalDirectoryDataOffset = directoryDataOffset;
  6915. directoryDataOffset = expectedDirectoryDataOffset;
  6916. prependedDataLength = directoryDataOffset - originalDirectoryDataOffset;
  6917. directoryArray = await readUint8Array(reader, directoryDataOffset, directoryDataLength);
  6918. directoryView = getDataView$1(directoryArray);
  6919. }
  6920. }
  6921. if (directoryDataOffset < 0 || directoryDataOffset >= reader.size) {
  6922. throw new Error(ERR_BAD_FORMAT);
  6923. }
  6924. const entries = [];
  6925. for (let indexFile = 0; indexFile < filesLength; indexFile++) {
  6926. const fileEntry = new ZipEntry(reader, zipReader.config, zipReader.options);
  6927. if (getUint32(directoryView, offset) != CENTRAL_FILE_HEADER_SIGNATURE) {
  6928. throw new Error(ERR_CENTRAL_DIRECTORY_NOT_FOUND);
  6929. }
  6930. readCommonHeader(fileEntry, directoryView, offset + 6);
  6931. const languageEncodingFlag = Boolean(fileEntry.bitFlag.languageEncodingFlag);
  6932. const filenameOffset = offset + 46;
  6933. const extraFieldOffset = filenameOffset + fileEntry.filenameLength;
  6934. const commentOffset = extraFieldOffset + fileEntry.extraFieldLength;
  6935. const versionMadeBy = getUint16(directoryView, offset + 4);
  6936. const msDosCompatible = (versionMadeBy & 0) == 0;
  6937. Object.assign(fileEntry, {
  6938. versionMadeBy,
  6939. msDosCompatible,
  6940. compressedSize: 0,
  6941. uncompressedSize: 0,
  6942. commentLength: getUint16(directoryView, offset + 32),
  6943. directory: msDosCompatible && ((getUint8(directoryView, offset + 38) & FILE_ATTR_MSDOS_DIR_MASK) == FILE_ATTR_MSDOS_DIR_MASK),
  6944. offset: getUint32(directoryView, offset + 42) + prependedDataLength,
  6945. internalFileAttribute: getUint32(directoryView, offset + 34),
  6946. externalFileAttribute: getUint32(directoryView, offset + 38),
  6947. rawFilename: directoryArray.subarray(filenameOffset, extraFieldOffset),
  6948. filenameUTF8: languageEncodingFlag,
  6949. commentUTF8: languageEncodingFlag,
  6950. rawExtraField: directoryArray.subarray(extraFieldOffset, commentOffset)
  6951. });
  6952. const endOffset = commentOffset + fileEntry.commentLength;
  6953. fileEntry.rawComment = directoryArray.subarray(commentOffset, endOffset);
  6954. const filenameEncoding = getOptionValue$1(zipReader, options, "filenameEncoding");
  6955. const commentEncoding = getOptionValue$1(zipReader, options, "commentEncoding");
  6956. const [filename, comment] = await Promise.all([
  6957. decodeText(fileEntry.rawFilename, fileEntry.filenameUTF8 ? CHARSET_UTF8 : filenameEncoding || CHARSET_CP437),
  6958. decodeText(fileEntry.rawComment, fileEntry.commentUTF8 ? CHARSET_UTF8 : commentEncoding || CHARSET_CP437)
  6959. ]);
  6960. fileEntry.filename = filename;
  6961. fileEntry.comment = comment;
  6962. if (!fileEntry.directory && fileEntry.filename.endsWith(DIRECTORY_SIGNATURE)) {
  6963. fileEntry.directory = true;
  6964. }
  6965. await readCommonFooter(fileEntry, fileEntry, directoryView, offset + 6);
  6966. const entry = new Entry(fileEntry);
  6967. entry.getData = (writer, options) => fileEntry.getData(writer, entry, options);
  6968. entries.push(entry);
  6969. offset = endOffset;
  6970. if (options.onprogress) {
  6971. try {
  6972. options.onprogress(indexFile + 1, filesLength, new Entry(fileEntry));
  6973. } catch (error) {
  6974. // ignored
  6975. }
  6976. }
  6977. }
  6978. return entries;
  6979. }
  6980.  
  6981. async close() {
  6982. }
  6983. }
  6984.  
  6985. class ZipEntry {
  6986.  
  6987. constructor(reader, config, options) {
  6988. Object.assign(this, {
  6989. reader,
  6990. config,
  6991. options
  6992. });
  6993. }
  6994.  
  6995. async getData(writer, fileEntry, options = {}) {
  6996. const zipEntry = this;
  6997. const {
  6998. reader,
  6999. offset,
  7000. extraFieldAES,
  7001. compressionMethod,
  7002. config,
  7003. bitFlag,
  7004. signature,
  7005. rawLastModDate,
  7006. compressedSize
  7007. } = zipEntry;
  7008. const localDirectory = zipEntry.localDirectory = {};
  7009. if (!reader.initialized) {
  7010. await reader.init();
  7011. }
  7012. let dataArray = await readUint8Array(reader, offset, 30);
  7013. const dataView = getDataView$1(dataArray);
  7014. let password = getOptionValue$1(zipEntry, options, "password");
  7015. password = password && password.length && password;
  7016. if (extraFieldAES) {
  7017. if (extraFieldAES.originalCompressionMethod != COMPRESSION_METHOD_AES) {
  7018. throw new Error(ERR_UNSUPPORTED_COMPRESSION);
  7019. }
  7020. }
  7021. if (compressionMethod != COMPRESSION_METHOD_STORE && compressionMethod != COMPRESSION_METHOD_DEFLATE) {
  7022. throw new Error(ERR_UNSUPPORTED_COMPRESSION);
  7023. }
  7024. if (getUint32(dataView, 0) != LOCAL_FILE_HEADER_SIGNATURE) {
  7025. throw new Error(ERR_LOCAL_FILE_HEADER_NOT_FOUND);
  7026. }
  7027. readCommonHeader(localDirectory, dataView, 4);
  7028. dataArray = await readUint8Array(reader, offset, 30 + localDirectory.filenameLength + localDirectory.extraFieldLength);
  7029. localDirectory.rawExtraField = dataArray.subarray(30 + localDirectory.filenameLength);
  7030. await readCommonFooter(zipEntry, localDirectory, dataView, 4);
  7031. fileEntry.lastAccessDate = localDirectory.lastAccessDate;
  7032. fileEntry.creationDate = localDirectory.creationDate;
  7033. const encrypted = zipEntry.encrypted && localDirectory.encrypted;
  7034. const zipCrypto = encrypted && !extraFieldAES;
  7035. if (encrypted) {
  7036. if (!zipCrypto && extraFieldAES.strength === undefined) {
  7037. throw new Error(ERR_UNSUPPORTED_ENCRYPTION);
  7038. } else if (!password) {
  7039. throw new Error(ERR_ENCRYPTED);
  7040. }
  7041. }
  7042. const codec = await createCodec(config.Inflate, {
  7043. codecType: CODEC_INFLATE,
  7044. password,
  7045. zipCrypto,
  7046. encryptionStrength: extraFieldAES && extraFieldAES.strength,
  7047. signed: getOptionValue$1(zipEntry, options, "checkSignature"),
  7048. passwordVerification: zipCrypto && (bitFlag.dataDescriptor ? ((rawLastModDate >>> 8) & 0xFF) : ((signature >>> 24) & 0xFF)),
  7049. signature,
  7050. compressed: compressionMethod != 0,
  7051. encrypted,
  7052. useWebWorkers: getOptionValue$1(zipEntry, options, "useWebWorkers")
  7053. }, config);
  7054. if (!writer.initialized) {
  7055. await writer.init();
  7056. }
  7057. const signal = getOptionValue$1(zipEntry, options, "signal");
  7058. const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
  7059. await processData(codec, reader, writer, dataOffset, compressedSize, config, { onprogress: options.onprogress, signal });
  7060. return writer.getData();
  7061. }
  7062. }
  7063.  
  7064. function readCommonHeader(directory, dataView, offset) {
  7065. const rawBitFlag = directory.rawBitFlag = getUint16(dataView, offset + 2);
  7066. const encrypted = (rawBitFlag & BITFLAG_ENCRYPTED) == BITFLAG_ENCRYPTED;
  7067. const rawLastModDate = getUint32(dataView, offset + 6);
  7068. Object.assign(directory, {
  7069. encrypted,
  7070. version: getUint16(dataView, offset),
  7071. bitFlag: {
  7072. level: (rawBitFlag & BITFLAG_LEVEL) >> 1,
  7073. dataDescriptor: (rawBitFlag & BITFLAG_DATA_DESCRIPTOR) == BITFLAG_DATA_DESCRIPTOR,
  7074. languageEncodingFlag: (rawBitFlag & BITFLAG_LANG_ENCODING_FLAG) == BITFLAG_LANG_ENCODING_FLAG
  7075. },
  7076. rawLastModDate,
  7077. lastModDate: getDate(rawLastModDate),
  7078. filenameLength: getUint16(dataView, offset + 22),
  7079. extraFieldLength: getUint16(dataView, offset + 24)
  7080. });
  7081. }
  7082.  
  7083. async function readCommonFooter(fileEntry, directory, dataView, offset) {
  7084. const rawExtraField = directory.rawExtraField;
  7085. const extraField = directory.extraField = new Map();
  7086. const rawExtraFieldView = getDataView$1(new Uint8Array(rawExtraField));
  7087. let offsetExtraField = 0;
  7088. try {
  7089. while (offsetExtraField < rawExtraField.length) {
  7090. const type = getUint16(rawExtraFieldView, offsetExtraField);
  7091. const size = getUint16(rawExtraFieldView, offsetExtraField + 2);
  7092. extraField.set(type, {
  7093. type,
  7094. data: rawExtraField.slice(offsetExtraField + 4, offsetExtraField + 4 + size)
  7095. });
  7096. offsetExtraField += 4 + size;
  7097. }
  7098. } catch (error) {
  7099. // ignored
  7100. }
  7101. const compressionMethod = getUint16(dataView, offset + 4);
  7102. directory.signature = getUint32(dataView, offset + 10);
  7103. directory.uncompressedSize = getUint32(dataView, offset + 18);
  7104. directory.compressedSize = getUint32(dataView, offset + 14);
  7105. const extraFieldZip64 = extraField.get(EXTRAFIELD_TYPE_ZIP64);
  7106. if (extraFieldZip64) {
  7107. readExtraFieldZip64(extraFieldZip64, directory);
  7108. directory.extraFieldZip64 = extraFieldZip64;
  7109. }
  7110. const extraFieldUnicodePath = extraField.get(EXTRAFIELD_TYPE_UNICODE_PATH);
  7111. if (extraFieldUnicodePath) {
  7112. await readExtraFieldUnicode(extraFieldUnicodePath, "filename", "rawFilename", directory, fileEntry);
  7113. directory.extraFieldUnicodePath = extraFieldUnicodePath;
  7114. }
  7115. const extraFieldUnicodeComment = extraField.get(EXTRAFIELD_TYPE_UNICODE_COMMENT);
  7116. if (extraFieldUnicodeComment) {
  7117. await readExtraFieldUnicode(extraFieldUnicodeComment, "comment", "rawComment", directory, fileEntry);
  7118. directory.extraFieldUnicodeComment = extraFieldUnicodeComment;
  7119. }
  7120. const extraFieldAES = extraField.get(EXTRAFIELD_TYPE_AES);
  7121. if (extraFieldAES) {
  7122. readExtraFieldAES(extraFieldAES, directory, compressionMethod);
  7123. directory.extraFieldAES = extraFieldAES;
  7124. } else {
  7125. directory.compressionMethod = compressionMethod;
  7126. }
  7127. const extraFieldNTFS = extraField.get(EXTRAFIELD_TYPE_NTFS);
  7128. if (extraFieldNTFS) {
  7129. readExtraFieldNTFS(extraFieldNTFS, directory);
  7130. directory.extraFieldNTFS = extraFieldNTFS;
  7131. }
  7132. const extraFieldExtendedTimestamp = extraField.get(EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
  7133. if (extraFieldExtendedTimestamp) {
  7134. readExtraFieldExtendedTimestamp(extraFieldExtendedTimestamp, directory);
  7135. directory.extraFieldExtendedTimestamp = extraFieldExtendedTimestamp;
  7136. }
  7137. }
  7138.  
  7139. function readExtraFieldZip64(extraFieldZip64, directory) {
  7140. directory.zip64 = true;
  7141. const extraFieldView = getDataView$1(extraFieldZip64.data);
  7142. extraFieldZip64.values = [];
  7143. for (let indexValue = 0; indexValue < Math.floor(extraFieldZip64.data.length / 8); indexValue++) {
  7144. extraFieldZip64.values.push(getBigUint64(extraFieldView, 0 + indexValue * 8));
  7145. }
  7146. const missingProperties = ZIP64_PROPERTIES.filter(propertyName => directory[propertyName] == MAX_32_BITS);
  7147. for (let indexMissingProperty = 0; indexMissingProperty < missingProperties.length; indexMissingProperty++) {
  7148. extraFieldZip64[missingProperties[indexMissingProperty]] = extraFieldZip64.values[indexMissingProperty];
  7149. }
  7150. ZIP64_PROPERTIES.forEach(propertyName => {
  7151. if (directory[propertyName] == MAX_32_BITS) {
  7152. if (extraFieldZip64[propertyName] !== undefined) {
  7153. directory[propertyName] = extraFieldZip64[propertyName];
  7154. } else {
  7155. throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
  7156. }
  7157. }
  7158. });
  7159. }
  7160.  
  7161. async function readExtraFieldUnicode(extraFieldUnicode, propertyName, rawPropertyName, directory, fileEntry) {
  7162. const extraFieldView = getDataView$1(extraFieldUnicode.data);
  7163. extraFieldUnicode.version = getUint8(extraFieldView, 0);
  7164. extraFieldUnicode.signature = getUint32(extraFieldView, 1);
  7165. const crc32 = new Crc32();
  7166. crc32.append(fileEntry[rawPropertyName]);
  7167. const dataViewSignature = getDataView$1(new Uint8Array(4));
  7168. dataViewSignature.setUint32(0, crc32.get(), true);
  7169. extraFieldUnicode[propertyName] = await decodeText(extraFieldUnicode.data.subarray(5));
  7170. extraFieldUnicode.valid = !fileEntry.bitFlag.languageEncodingFlag && extraFieldUnicode.signature == getUint32(dataViewSignature, 0);
  7171. if (extraFieldUnicode.valid) {
  7172. directory[propertyName] = extraFieldUnicode[propertyName];
  7173. directory[propertyName + "UTF8"] = true;
  7174. }
  7175. }
  7176.  
  7177. function readExtraFieldAES(extraFieldAES, directory, compressionMethod) {
  7178. const extraFieldView = getDataView$1(extraFieldAES.data);
  7179. extraFieldAES.vendorVersion = getUint8(extraFieldView, 0);
  7180. extraFieldAES.vendorId = getUint8(extraFieldView, 2);
  7181. const strength = getUint8(extraFieldView, 4);
  7182. extraFieldAES.strength = strength;
  7183. extraFieldAES.originalCompressionMethod = compressionMethod;
  7184. directory.compressionMethod = extraFieldAES.compressionMethod = getUint16(extraFieldView, 5);
  7185. }
  7186.  
  7187. function readExtraFieldNTFS(extraFieldNTFS, directory) {
  7188. const extraFieldView = getDataView$1(extraFieldNTFS.data);
  7189. let offsetExtraField = 4;
  7190. let tag1Data;
  7191. try {
  7192. while (offsetExtraField < extraFieldNTFS.data.length && !tag1Data) {
  7193. const tagValue = getUint16(extraFieldView, offsetExtraField);
  7194. const attributeSize = getUint16(extraFieldView, offsetExtraField + 2);
  7195. if (tagValue == EXTRAFIELD_TYPE_NTFS_TAG1) {
  7196. tag1Data = extraFieldNTFS.data.slice(offsetExtraField + 4, offsetExtraField + 4 + attributeSize);
  7197. }
  7198. offsetExtraField += 4 + attributeSize;
  7199. }
  7200. } catch (error) {
  7201. // ignored
  7202. }
  7203. try {
  7204. if (tag1Data && tag1Data.length == 24) {
  7205. const tag1View = getDataView$1(tag1Data);
  7206. const rawLastModDate = tag1View.getBigUint64(0, true);
  7207. const rawLastAccessDate = tag1View.getBigUint64(8, true);
  7208. const rawCreationDate = tag1View.getBigUint64(16, true);
  7209. Object.assign(extraFieldNTFS, {
  7210. rawLastModDate,
  7211. rawLastAccessDate,
  7212. rawCreationDate
  7213. });
  7214. const lastModDate = getDateNTFS(rawLastModDate);
  7215. const lastAccessDate = getDateNTFS(rawLastAccessDate);
  7216. const creationDate = getDateNTFS(rawCreationDate);
  7217. const extraFieldData = { lastModDate, lastAccessDate, creationDate };
  7218. Object.assign(extraFieldNTFS, extraFieldData);
  7219. Object.assign(directory, extraFieldData);
  7220. }
  7221. } catch (error) {
  7222. // ignored
  7223. }
  7224. }
  7225.  
  7226. function readExtraFieldExtendedTimestamp(extraFieldExtendedTimestamp, directory) {
  7227. const extraFieldView = getDataView$1(extraFieldExtendedTimestamp.data);
  7228. const flags = getUint8(extraFieldView, 0);
  7229. const timeProperties = [];
  7230. const timeRawProperties = [];
  7231. if ((flags & 0x1) == 0x1) {
  7232. timeProperties.push("lastModDate");
  7233. timeRawProperties.push("rawLastModDate");
  7234. }
  7235. if ((flags & 0x2) == 0x2) {
  7236. timeProperties.push("lastAccessDate");
  7237. timeRawProperties.push("rawLastAccessDate");
  7238. }
  7239. if ((flags & 0x4) == 0x4) {
  7240. timeProperties.push("creationDate");
  7241. timeRawProperties.push("rawCreationDate");
  7242. }
  7243. let offset = 1;
  7244. timeProperties.forEach((propertyName, indexProperty) => {
  7245. if (extraFieldExtendedTimestamp.data.length >= offset + 4) {
  7246. const time = getUint32(extraFieldView, offset);
  7247. directory[propertyName] = extraFieldExtendedTimestamp[propertyName] = new Date(time * 1000);
  7248. const rawPropertyName = timeRawProperties[indexProperty];
  7249. extraFieldExtendedTimestamp[rawPropertyName] = time;
  7250. }
  7251. offset += 4;
  7252. });
  7253. }
  7254.  
  7255. async function seekSignature(reader, signature, startOffset, minimumBytes, maximumLength) {
  7256. const signatureArray = new Uint8Array(4);
  7257. const signatureView = getDataView$1(signatureArray);
  7258. setUint32$1(signatureView, 0, signature);
  7259. const maximumBytes = minimumBytes + maximumLength;
  7260. return (await seek(minimumBytes)) || await seek(Math.min(maximumBytes, startOffset));
  7261.  
  7262. async function seek(length) {
  7263. const offset = startOffset - length;
  7264. const bytes = await readUint8Array(reader, offset, length);
  7265. for (let indexByte = bytes.length - minimumBytes; indexByte >= 0; indexByte--) {
  7266. if (bytes[indexByte] == signatureArray[0] && bytes[indexByte + 1] == signatureArray[1] &&
  7267. bytes[indexByte + 2] == signatureArray[2] && bytes[indexByte + 3] == signatureArray[3]) {
  7268. return {
  7269. offset: offset + indexByte,
  7270. buffer: bytes.slice(indexByte, indexByte + minimumBytes).buffer
  7271. };
  7272. }
  7273. }
  7274. }
  7275. }
  7276.  
  7277. function getOptionValue$1(zipReader, options, name) {
  7278. return options[name] === undefined ? zipReader.options[name] : options[name];
  7279. }
  7280.  
  7281. function getDate(timeRaw) {
  7282. const date = (timeRaw & 0xffff0000) >> 16, time = timeRaw & 0x0000ffff;
  7283. try {
  7284. return new Date(1980 + ((date & 0xFE00) >> 9), ((date & 0x01E0) >> 5) - 1, date & 0x001F, (time & 0xF800) >> 11, (time & 0x07E0) >> 5, (time & 0x001F) * 2, 0);
  7285. } catch (error) {
  7286. // ignored
  7287. }
  7288. }
  7289.  
  7290. function getDateNTFS(timeRaw) {
  7291. return new Date((Number((timeRaw / BigInt(10000)) - BigInt(11644473600000))));
  7292. }
  7293.  
  7294. function getUint8(view, offset) {
  7295. return view.getUint8(offset);
  7296. }
  7297.  
  7298. function getUint16(view, offset) {
  7299. return view.getUint16(offset, true);
  7300. }
  7301.  
  7302. function getUint32(view, offset) {
  7303. return view.getUint32(offset, true);
  7304. }
  7305.  
  7306. function getBigUint64(view, offset) {
  7307. return Number(view.getBigUint64(offset, true));
  7308. }
  7309.  
  7310. function setUint32$1(view, offset, value) {
  7311. view.setUint32(offset, value, true);
  7312. }
  7313.  
  7314. function getDataView$1(array) {
  7315. return new DataView(array.buffer);
  7316. }
  7317.  
  7318. function readUint8Array(reader, offset, size) {
  7319. return reader.readUint8Array(offset, size);
  7320. }
  7321.  
  7322. /*
  7323. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  7324.  
  7325. Redistribution and use in source and binary forms, with or without
  7326. modification, are permitted provided that the following conditions are met:
  7327.  
  7328. 1. Redistributions of source code must retain the above copyright notice,
  7329. this list of conditions and the following disclaimer.
  7330.  
  7331. 2. Redistributions in binary form must reproduce the above copyright
  7332. notice, this list of conditions and the following disclaimer in
  7333. the documentation and/or other materials provided with the distribution.
  7334.  
  7335. 3. The names of the authors may not be used to endorse or promote products
  7336. derived from this software without specific prior written permission.
  7337.  
  7338. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  7339. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  7340. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  7341. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  7342. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7343. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  7344. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  7345. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  7346. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  7347. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7348. */
  7349.  
  7350. const ERR_DUPLICATED_NAME = "File already exists";
  7351. const ERR_INVALID_COMMENT = "Zip file comment exceeds 64KB";
  7352. const ERR_INVALID_ENTRY_COMMENT = "File entry comment exceeds 64KB";
  7353. const ERR_INVALID_ENTRY_NAME = "File entry name exceeds 64KB";
  7354. const ERR_INVALID_VERSION = "Version exceeds 65535";
  7355. const ERR_INVALID_ENCRYPTION_STRENGTH = "The strength must equal 1, 2, or 3";
  7356. const ERR_INVALID_EXTRAFIELD_TYPE = "Extra field type exceeds 65535";
  7357. const ERR_INVALID_EXTRAFIELD_DATA = "Extra field data exceeds 64KB";
  7358. const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported";
  7359.  
  7360. const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
  7361. const EXTRAFIELD_LENGTH_ZIP64 = 24;
  7362.  
  7363. let workers = 0;
  7364.  
  7365. class ZipWriter {
  7366.  
  7367. constructor(writer, options = {}) {
  7368. Object.assign(this, {
  7369. writer,
  7370. options,
  7371. config: getConfiguration(),
  7372. files: new Map(),
  7373. offset: writer.size,
  7374. pendingCompressedSize: 0,
  7375. pendingEntries: []
  7376. });
  7377. }
  7378.  
  7379. async add(name = "", reader, options = {}) {
  7380. const zipWriter = this;
  7381. if (workers < zipWriter.config.maxWorkers) {
  7382. workers++;
  7383. try {
  7384. return await addFile(zipWriter, name, reader, options);
  7385. } finally {
  7386. workers--;
  7387. const pendingEntry = zipWriter.pendingEntries.shift();
  7388. if (pendingEntry) {
  7389. zipWriter.add(pendingEntry.name, pendingEntry.reader, pendingEntry.options)
  7390. .then(pendingEntry.resolve)
  7391. .catch(pendingEntry.reject);
  7392. }
  7393. }
  7394. } else {
  7395. return new Promise((resolve, reject) => zipWriter.pendingEntries.push({ name, reader, options, resolve, reject }));
  7396. }
  7397. }
  7398.  
  7399. async close(comment = new Uint8Array(0), options = {}) {
  7400. await closeFile(this, comment, options);
  7401. return this.writer.getData();
  7402. }
  7403. }
  7404.  
  7405. async function addFile(zipWriter, name, reader, options) {
  7406. name = name.trim();
  7407. if (options.directory && (!name.endsWith(DIRECTORY_SIGNATURE))) {
  7408. name += DIRECTORY_SIGNATURE;
  7409. } else {
  7410. options.directory = name.endsWith(DIRECTORY_SIGNATURE);
  7411. }
  7412. if (zipWriter.files.has(name)) {
  7413. throw new Error(ERR_DUPLICATED_NAME);
  7414. }
  7415. const rawFilename = encodeText(name);
  7416. if (rawFilename.length > MAX_16_BITS) {
  7417. throw new Error(ERR_INVALID_ENTRY_NAME);
  7418. }
  7419. const comment = options.comment || "";
  7420. const rawComment = encodeText(comment);
  7421. if (rawComment.length > MAX_16_BITS) {
  7422. throw new Error(ERR_INVALID_ENTRY_COMMENT);
  7423. }
  7424. const version = zipWriter.options.version || options.version || 0;
  7425. if (version > MAX_16_BITS) {
  7426. throw new Error(ERR_INVALID_VERSION);
  7427. }
  7428. const versionMadeBy = zipWriter.options.versionMadeBy || options.versionMadeBy || 20;
  7429. if (versionMadeBy > MAX_16_BITS) {
  7430. throw new Error(ERR_INVALID_VERSION);
  7431. }
  7432. const lastModDate = getOptionValue(zipWriter, options, "lastModDate") || new Date();
  7433. const lastAccessDate = getOptionValue(zipWriter, options, "lastAccessDate");
  7434. const creationDate = getOptionValue(zipWriter, options, "creationDate");
  7435. const password = getOptionValue(zipWriter, options, "password");
  7436. const encryptionStrength = getOptionValue(zipWriter, options, "encryptionStrength") || 3;
  7437. const zipCrypto = getOptionValue(zipWriter, options, "zipCrypto");
  7438. if (password !== undefined && encryptionStrength !== undefined && (encryptionStrength < 1 || encryptionStrength > 3)) {
  7439. throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
  7440. }
  7441. let rawExtraField = new Uint8Array(0);
  7442. const extraField = options.extraField;
  7443. if (extraField) {
  7444. let extraFieldSize = 0;
  7445. let offset = 0;
  7446. extraField.forEach(data => extraFieldSize += 4 + data.length);
  7447. rawExtraField = new Uint8Array(extraFieldSize);
  7448. extraField.forEach((data, type) => {
  7449. if (type > MAX_16_BITS) {
  7450. throw new Error(ERR_INVALID_EXTRAFIELD_TYPE);
  7451. }
  7452. if (data.length > MAX_16_BITS) {
  7453. throw new Error(ERR_INVALID_EXTRAFIELD_DATA);
  7454. }
  7455. arraySet(rawExtraField, new Uint16Array([type]), offset);
  7456. arraySet(rawExtraField, new Uint16Array([data.length]), offset + 2);
  7457. arraySet(rawExtraField, data, offset + 4);
  7458. offset += 4 + data.length;
  7459. });
  7460. }
  7461. let extendedTimestamp = getOptionValue(zipWriter, options, "extendedTimestamp");
  7462. if (extendedTimestamp === undefined) {
  7463. extendedTimestamp = true;
  7464. }
  7465. let maximumCompressedSize = 0;
  7466. let keepOrder = getOptionValue(zipWriter, options, "keepOrder");
  7467. if (keepOrder === undefined) {
  7468. keepOrder = true;
  7469. }
  7470. let uncompressedSize = 0;
  7471. let msDosCompatible = getOptionValue(zipWriter, options, "msDosCompatible");
  7472. if (msDosCompatible === undefined) {
  7473. msDosCompatible = true;
  7474. }
  7475. const internalFileAttribute = getOptionValue(zipWriter, options, "internalFileAttribute") || 0;
  7476. const externalFileAttribute = getOptionValue(zipWriter, options, "externalFileAttribute") || 0;
  7477. if (reader) {
  7478. if (!reader.initialized) {
  7479. await reader.init();
  7480. }
  7481. uncompressedSize = reader.size;
  7482. maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
  7483. }
  7484. let zip64 = options.zip64 || zipWriter.options.zip64 || false;
  7485. if (zipWriter.offset + zipWriter.pendingCompressedSize >= MAX_32_BITS ||
  7486. uncompressedSize >= MAX_32_BITS ||
  7487. maximumCompressedSize >= MAX_32_BITS) {
  7488. if (options.zip64 === false || zipWriter.options.zip64 === false || !keepOrder) {
  7489. throw new Error(ERR_UNSUPPORTED_FORMAT);
  7490. } else {
  7491. zip64 = true;
  7492. }
  7493. }
  7494. zipWriter.pendingCompressedSize += maximumCompressedSize;
  7495. await Promise.resolve();
  7496. const level = getOptionValue(zipWriter, options, "level");
  7497. const useWebWorkers = getOptionValue(zipWriter, options, "useWebWorkers");
  7498. const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
  7499. let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor");
  7500. let dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature");
  7501. const signal = getOptionValue(zipWriter, options, "signal");
  7502. if (dataDescriptor === undefined) {
  7503. dataDescriptor = true;
  7504. }
  7505. if (dataDescriptor && dataDescriptorSignature === undefined) {
  7506. dataDescriptorSignature = true;
  7507. }
  7508. const fileEntry = await getFileEntry(zipWriter, name, reader, Object.assign({}, options, {
  7509. rawFilename,
  7510. rawComment,
  7511. version,
  7512. versionMadeBy,
  7513. lastModDate,
  7514. lastAccessDate,
  7515. creationDate,
  7516. rawExtraField,
  7517. zip64,
  7518. password,
  7519. level,
  7520. useWebWorkers,
  7521. encryptionStrength,
  7522. extendedTimestamp,
  7523. zipCrypto,
  7524. bufferedWrite,
  7525. keepOrder,
  7526. dataDescriptor,
  7527. dataDescriptorSignature,
  7528. signal,
  7529. msDosCompatible,
  7530. internalFileAttribute,
  7531. externalFileAttribute
  7532. }));
  7533. if (maximumCompressedSize) {
  7534. zipWriter.pendingCompressedSize -= maximumCompressedSize;
  7535. }
  7536. Object.assign(fileEntry, { name, comment, extraField });
  7537. return new Entry(fileEntry);
  7538. }
  7539.  
  7540. async function getFileEntry(zipWriter, name, reader, options) {
  7541. const files = zipWriter.files;
  7542. const writer = zipWriter.writer;
  7543. const previousFileEntry = Array.from(files.values()).pop();
  7544. let fileEntry = {};
  7545. let bufferedWrite;
  7546. let resolveLockUnbufferedWrite;
  7547. let resolveLockCurrentFileEntry;
  7548. files.set(name, fileEntry);
  7549. try {
  7550. let lockPreviousFileEntry;
  7551. let fileWriter;
  7552. let lockCurrentFileEntry;
  7553. if (options.keepOrder) {
  7554. lockPreviousFileEntry = previousFileEntry && previousFileEntry.lock;
  7555. }
  7556. fileEntry.lock = lockCurrentFileEntry = new Promise(resolve => resolveLockCurrentFileEntry = resolve);
  7557. if (options.bufferedWrite || zipWriter.lockWrite || !options.dataDescriptor) {
  7558. fileWriter = new BlobWriter();
  7559. fileWriter.init();
  7560. bufferedWrite = true;
  7561. } else {
  7562. zipWriter.lockWrite = new Promise(resolve => resolveLockUnbufferedWrite = resolve);
  7563. if (!writer.initialized) {
  7564. await writer.init();
  7565. }
  7566. fileWriter = writer;
  7567. }
  7568. fileEntry = await createFileEntry(reader, fileWriter, zipWriter.config, options);
  7569. fileEntry.lock = lockCurrentFileEntry;
  7570. files.set(name, fileEntry);
  7571. fileEntry.filename = name;
  7572. if (bufferedWrite) {
  7573. let indexWrittenData = 0;
  7574. const blob = fileWriter.getData();
  7575. await Promise.all([zipWriter.lockWrite, lockPreviousFileEntry]);
  7576. let pendingFileEntry;
  7577. do {
  7578. pendingFileEntry = Array.from(files.values()).find(fileEntry => fileEntry.writingBufferedData);
  7579. if (pendingFileEntry) {
  7580. await pendingFileEntry.lock;
  7581. }
  7582. } while (pendingFileEntry && pendingFileEntry.lock);
  7583. fileEntry.writingBufferedData = true;
  7584. if (!options.dataDescriptor) {
  7585. const headerLength = 26;
  7586. const arrayBuffer = await sliceAsArrayBuffer(blob, 0, headerLength);
  7587. const arrayBufferView = new DataView(arrayBuffer);
  7588. if (!fileEntry.encrypted || options.zipCrypto) {
  7589. setUint32(arrayBufferView, 14, fileEntry.signature);
  7590. }
  7591. if (fileEntry.zip64) {
  7592. setUint32(arrayBufferView, 18, MAX_32_BITS);
  7593. setUint32(arrayBufferView, 22, MAX_32_BITS);
  7594. } else {
  7595. setUint32(arrayBufferView, 18, fileEntry.compressedSize);
  7596. setUint32(arrayBufferView, 22, fileEntry.uncompressedSize);
  7597. }
  7598. await writer.writeUint8Array(new Uint8Array(arrayBuffer));
  7599. indexWrittenData = headerLength;
  7600. }
  7601. await writeBlob(writer, blob, indexWrittenData);
  7602. delete fileEntry.writingBufferedData;
  7603. }
  7604. fileEntry.offset = zipWriter.offset;
  7605. if (fileEntry.zip64) {
  7606. const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
  7607. setBigUint64(rawExtraFieldZip64View, 20, BigInt(fileEntry.offset));
  7608. } else if (fileEntry.offset >= MAX_32_BITS) {
  7609. throw new Error(ERR_UNSUPPORTED_FORMAT);
  7610. }
  7611. zipWriter.offset += fileEntry.length;
  7612. return fileEntry;
  7613. } catch (error) {
  7614. if ((bufferedWrite && fileEntry.writingBufferedData) || (!bufferedWrite && fileEntry.dataWritten)) {
  7615. error.corruptedEntry = zipWriter.hasCorruptedEntries = true;
  7616. if (fileEntry.uncompressedSize) {
  7617. zipWriter.offset += fileEntry.uncompressedSize;
  7618. }
  7619. }
  7620. files.delete(name);
  7621. throw error;
  7622. } finally {
  7623. resolveLockCurrentFileEntry();
  7624. if (resolveLockUnbufferedWrite) {
  7625. resolveLockUnbufferedWrite();
  7626. }
  7627. }
  7628. }
  7629.  
  7630. async function createFileEntry(reader, writer, config, options) {
  7631. const {
  7632. rawFilename,
  7633. lastAccessDate,
  7634. creationDate,
  7635. password,
  7636. level,
  7637. zip64,
  7638. zipCrypto,
  7639. dataDescriptor,
  7640. dataDescriptorSignature,
  7641. directory,
  7642. version,
  7643. versionMadeBy,
  7644. rawComment,
  7645. rawExtraField,
  7646. useWebWorkers,
  7647. onprogress,
  7648. signal,
  7649. encryptionStrength,
  7650. extendedTimestamp,
  7651. msDosCompatible,
  7652. internalFileAttribute,
  7653. externalFileAttribute
  7654. } = options;
  7655. const encrypted = Boolean(password && password.length);
  7656. const compressed = level !== 0 && !directory;
  7657. let rawExtraFieldAES;
  7658. if (encrypted && !zipCrypto) {
  7659. rawExtraFieldAES = new Uint8Array(EXTRAFIELD_DATA_AES.length + 2);
  7660. const extraFieldAESView = getDataView(rawExtraFieldAES);
  7661. setUint16(extraFieldAESView, 0, EXTRAFIELD_TYPE_AES);
  7662. arraySet(rawExtraFieldAES, EXTRAFIELD_DATA_AES, 2);
  7663. setUint8(extraFieldAESView, 8, encryptionStrength);
  7664. } else {
  7665. rawExtraFieldAES = new Uint8Array(0);
  7666. }
  7667. let rawExtraFieldNTFS;
  7668. let rawExtraFieldExtendedTimestamp;
  7669. if (extendedTimestamp) {
  7670. rawExtraFieldExtendedTimestamp = new Uint8Array(9 + (lastAccessDate ? 4 : 0) + (creationDate ? 4 : 0));
  7671. const extraFieldExtendedTimestampView = getDataView(rawExtraFieldExtendedTimestamp);
  7672. setUint16(extraFieldExtendedTimestampView, 0, EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
  7673. setUint16(extraFieldExtendedTimestampView, 2, rawExtraFieldExtendedTimestamp.length - 4);
  7674. const extraFieldExtendedTimestampFlag = 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0);
  7675. setUint8(extraFieldExtendedTimestampView, 4, extraFieldExtendedTimestampFlag);
  7676. setUint32(extraFieldExtendedTimestampView, 5, Math.floor(options.lastModDate.getTime() / 1000));
  7677. if (lastAccessDate) {
  7678. setUint32(extraFieldExtendedTimestampView, 9, Math.floor(lastAccessDate.getTime() / 1000));
  7679. }
  7680. if (creationDate) {
  7681. setUint32(extraFieldExtendedTimestampView, 13, Math.floor(creationDate.getTime() / 1000));
  7682. }
  7683. try {
  7684. rawExtraFieldNTFS = new Uint8Array(36);
  7685. const extraFieldNTFSView = getDataView(rawExtraFieldNTFS);
  7686. const lastModTimeNTFS = getTimeNTFS(options.lastModDate);
  7687. setUint16(extraFieldNTFSView, 0, EXTRAFIELD_TYPE_NTFS);
  7688. setUint16(extraFieldNTFSView, 2, 32);
  7689. setUint16(extraFieldNTFSView, 8, EXTRAFIELD_TYPE_NTFS_TAG1);
  7690. setUint16(extraFieldNTFSView, 10, 24);
  7691. setBigUint64(extraFieldNTFSView, 12, lastModTimeNTFS);
  7692. setBigUint64(extraFieldNTFSView, 20, getTimeNTFS(lastAccessDate) || lastModTimeNTFS);
  7693. setBigUint64(extraFieldNTFSView, 28, getTimeNTFS(creationDate) || lastModTimeNTFS);
  7694. } catch (error) {
  7695. rawExtraFieldNTFS = new Uint8Array(0);
  7696. }
  7697. } else {
  7698. rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array(0);
  7699. }
  7700. const fileEntry = {
  7701. version: version || VERSION_DEFLATE,
  7702. versionMadeBy,
  7703. zip64,
  7704. directory: Boolean(directory),
  7705. filenameUTF8: true,
  7706. rawFilename,
  7707. commentUTF8: true,
  7708. rawComment,
  7709. rawExtraFieldZip64: zip64 ? new Uint8Array(EXTRAFIELD_LENGTH_ZIP64 + 4) : new Uint8Array(0),
  7710. rawExtraFieldExtendedTimestamp,
  7711. rawExtraFieldNTFS,
  7712. rawExtraFieldAES,
  7713. rawExtraField,
  7714. extendedTimestamp,
  7715. msDosCompatible,
  7716. internalFileAttribute,
  7717. externalFileAttribute
  7718. };
  7719. let uncompressedSize = fileEntry.uncompressedSize = 0;
  7720. let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
  7721. if (dataDescriptor) {
  7722. bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
  7723. }
  7724. let compressionMethod = COMPRESSION_METHOD_STORE;
  7725. if (compressed) {
  7726. compressionMethod = COMPRESSION_METHOD_DEFLATE;
  7727. }
  7728. if (zip64) {
  7729. fileEntry.version = fileEntry.version > VERSION_ZIP64 ? fileEntry.version : VERSION_ZIP64;
  7730. }
  7731. if (encrypted) {
  7732. bitFlag = bitFlag | BITFLAG_ENCRYPTED;
  7733. if (!zipCrypto) {
  7734. fileEntry.version = fileEntry.version > VERSION_AES ? fileEntry.version : VERSION_AES;
  7735. compressionMethod = COMPRESSION_METHOD_AES;
  7736. if (compressed) {
  7737. fileEntry.rawExtraFieldAES[9] = COMPRESSION_METHOD_DEFLATE;
  7738. }
  7739. }
  7740. }
  7741. fileEntry.compressionMethod = compressionMethod;
  7742. const headerArray = fileEntry.headerArray = new Uint8Array(26);
  7743. const headerView = getDataView(headerArray);
  7744. setUint16(headerView, 0, fileEntry.version);
  7745. setUint16(headerView, 2, bitFlag);
  7746. setUint16(headerView, 4, compressionMethod);
  7747. const dateArray = new Uint32Array(1);
  7748. const dateView = getDataView(dateArray);
  7749. let lastModDate;
  7750. if (options.lastModDate < MIN_DATE) {
  7751. lastModDate = MIN_DATE;
  7752. } else if (options.lastModDate > MAX_DATE) {
  7753. lastModDate = MAX_DATE;
  7754. } else {
  7755. lastModDate = options.lastModDate;
  7756. }
  7757. setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
  7758. setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
  7759. const rawLastModDate = dateArray[0];
  7760. setUint32(headerView, 6, rawLastModDate);
  7761. setUint16(headerView, 22, rawFilename.length);
  7762. const extraFieldLength = rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length + rawExtraFieldNTFS.length + fileEntry.rawExtraField.length;
  7763. setUint16(headerView, 24, extraFieldLength);
  7764. const localHeaderArray = new Uint8Array(30 + rawFilename.length + extraFieldLength);
  7765. const localHeaderView = getDataView(localHeaderArray);
  7766. setUint32(localHeaderView, 0, LOCAL_FILE_HEADER_SIGNATURE);
  7767. arraySet(localHeaderArray, headerArray, 4);
  7768. arraySet(localHeaderArray, rawFilename, 30);
  7769. arraySet(localHeaderArray, rawExtraFieldAES, 30 + rawFilename.length);
  7770. arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + rawFilename.length + rawExtraFieldAES.length);
  7771. arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length);
  7772. arraySet(localHeaderArray, fileEntry.rawExtraField, 30 + rawFilename.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length + rawExtraFieldNTFS.length);
  7773. let result;
  7774. let compressedSize = 0;
  7775. if (reader) {
  7776. uncompressedSize = fileEntry.uncompressedSize = reader.size;
  7777. const codec = await createCodec(config.Deflate, {
  7778. codecType: CODEC_DEFLATE,
  7779. level,
  7780. password,
  7781. encryptionStrength,
  7782. zipCrypto: encrypted && zipCrypto,
  7783. passwordVerification: encrypted && zipCrypto && (rawLastModDate >> 8) & 0xFF,
  7784. signed: true,
  7785. compressed,
  7786. encrypted,
  7787. useWebWorkers
  7788. }, config);
  7789. await writer.writeUint8Array(localHeaderArray);
  7790. fileEntry.dataWritten = true;
  7791. result = await processData(codec, reader, writer, 0, uncompressedSize, config, { onprogress, signal });
  7792. compressedSize = result.length;
  7793. } else {
  7794. await writer.writeUint8Array(localHeaderArray);
  7795. fileEntry.dataWritten = true;
  7796. }
  7797. let dataDescriptorArray = new Uint8Array(0);
  7798. let dataDescriptorView, dataDescriptorOffset = 0;
  7799. if (dataDescriptor) {
  7800. dataDescriptorArray = new Uint8Array(zip64 ? (dataDescriptorSignature ? 24 : 20) : (dataDescriptorSignature ? 16 : 12));
  7801. dataDescriptorView = getDataView(dataDescriptorArray);
  7802. if (dataDescriptorSignature) {
  7803. dataDescriptorOffset = 4;
  7804. setUint32(dataDescriptorView, 0, DATA_DESCRIPTOR_RECORD_SIGNATURE);
  7805. }
  7806. }
  7807. if (reader) {
  7808. const signature = result.signature;
  7809. if ((!encrypted || zipCrypto) && signature !== undefined) {
  7810. setUint32(headerView, 10, signature);
  7811. fileEntry.signature = signature;
  7812. if (dataDescriptor) {
  7813. setUint32(dataDescriptorView, dataDescriptorOffset, signature);
  7814. }
  7815. }
  7816. if (zip64) {
  7817. const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
  7818. setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
  7819. setUint16(rawExtraFieldZip64View, 2, EXTRAFIELD_LENGTH_ZIP64);
  7820. setUint32(headerView, 14, MAX_32_BITS);
  7821. setBigUint64(rawExtraFieldZip64View, 12, BigInt(compressedSize));
  7822. setUint32(headerView, 18, MAX_32_BITS);
  7823. setBigUint64(rawExtraFieldZip64View, 4, BigInt(uncompressedSize));
  7824. if (dataDescriptor) {
  7825. setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
  7826. setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
  7827. }
  7828. } else {
  7829. setUint32(headerView, 14, compressedSize);
  7830. setUint32(headerView, 18, uncompressedSize);
  7831. if (dataDescriptor) {
  7832. setUint32(dataDescriptorView, dataDescriptorOffset + 4, compressedSize);
  7833. setUint32(dataDescriptorView, dataDescriptorOffset + 8, uncompressedSize);
  7834. }
  7835. }
  7836. }
  7837. if (dataDescriptor) {
  7838. await writer.writeUint8Array(dataDescriptorArray);
  7839. }
  7840. const length = localHeaderArray.length + compressedSize + dataDescriptorArray.length;
  7841. Object.assign(fileEntry, { compressedSize, lastModDate, rawLastModDate, creationDate, lastAccessDate, encrypted, length });
  7842. return fileEntry;
  7843. }
  7844.  
  7845. async function closeFile(zipWriter, comment, options) {
  7846. const writer = zipWriter.writer;
  7847. const files = zipWriter.files;
  7848. let offset = 0;
  7849. let directoryDataLength = 0;
  7850. let directoryOffset = zipWriter.offset;
  7851. let filesLength = files.size;
  7852. for (const [, fileEntry] of files) {
  7853. directoryDataLength += 46 +
  7854. fileEntry.rawFilename.length +
  7855. fileEntry.rawComment.length +
  7856. fileEntry.rawExtraFieldZip64.length +
  7857. fileEntry.rawExtraFieldAES.length +
  7858. fileEntry.rawExtraFieldExtendedTimestamp.length +
  7859. fileEntry.rawExtraFieldNTFS.length +
  7860. fileEntry.rawExtraField.length;
  7861. }
  7862. let zip64 = options.zip64 || zipWriter.options.zip64 || false;
  7863. if (directoryOffset >= MAX_32_BITS || directoryDataLength >= MAX_32_BITS || filesLength >= MAX_16_BITS) {
  7864. if (options.zip64 === false || zipWriter.options.zip64 === false) {
  7865. throw new Error(ERR_UNSUPPORTED_FORMAT);
  7866. } else {
  7867. zip64 = true;
  7868. }
  7869. }
  7870. const directoryArray = new Uint8Array(directoryDataLength + (zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH));
  7871. const directoryView = getDataView(directoryArray);
  7872. if (comment && comment.length) {
  7873. if (comment.length <= MAX_16_BITS) {
  7874. setUint16(directoryView, offset + 20, comment.length);
  7875. } else {
  7876. throw new Error(ERR_INVALID_COMMENT);
  7877. }
  7878. }
  7879. for (const [indexFileEntry, fileEntry] of Array.from(files.values()).entries()) {
  7880. const {
  7881. rawFilename,
  7882. rawExtraFieldZip64,
  7883. rawExtraFieldAES,
  7884. rawExtraField,
  7885. rawComment,
  7886. versionMadeBy,
  7887. headerArray,
  7888. directory,
  7889. zip64,
  7890. msDosCompatible,
  7891. internalFileAttribute,
  7892. externalFileAttribute
  7893. } = fileEntry;
  7894. let rawExtraFieldExtendedTimestamp;
  7895. let rawExtraFieldNTFS;
  7896. if (fileEntry.extendedTimestamp) {
  7897. rawExtraFieldNTFS = fileEntry.rawExtraFieldNTFS;
  7898. rawExtraFieldExtendedTimestamp = new Uint8Array(9);
  7899. const extraFieldExtendedTimestampView = getDataView(rawExtraFieldExtendedTimestamp);
  7900. setUint16(extraFieldExtendedTimestampView, 0, EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP);
  7901. setUint16(extraFieldExtendedTimestampView, 2, rawExtraFieldExtendedTimestamp.length - 4);
  7902. setUint8(extraFieldExtendedTimestampView, 4, 0x1);
  7903. setUint32(extraFieldExtendedTimestampView, 5, Math.floor(fileEntry.lastModDate.getTime() / 1000));
  7904. } else {
  7905. rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array(0);
  7906. }
  7907. const extraFieldLength = rawExtraFieldZip64.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length + rawExtraFieldNTFS.length + rawExtraField.length;
  7908. setUint32(directoryView, offset, CENTRAL_FILE_HEADER_SIGNATURE);
  7909. setUint16(directoryView, offset + 4, versionMadeBy);
  7910. arraySet(directoryArray, headerArray, offset + 6);
  7911. setUint16(directoryView, offset + 30, extraFieldLength);
  7912. setUint16(directoryView, offset + 32, rawComment.length);
  7913. setUint32(directoryView, offset + 34, internalFileAttribute);
  7914. if (externalFileAttribute) {
  7915. setUint32(directoryView, offset + 38, externalFileAttribute);
  7916. } else if (directory && msDosCompatible) {
  7917. setUint8(directoryView, offset + 38, FILE_ATTR_MSDOS_DIR_MASK);
  7918. }
  7919. if (zip64) {
  7920. setUint32(directoryView, offset + 42, MAX_32_BITS);
  7921. } else {
  7922. setUint32(directoryView, offset + 42, fileEntry.offset);
  7923. }
  7924. arraySet(directoryArray, rawFilename, offset + 46);
  7925. arraySet(directoryArray, rawExtraFieldZip64, offset + 46 + rawFilename.length);
  7926. arraySet(directoryArray, rawExtraFieldAES, offset + 46 + rawFilename.length + rawExtraFieldZip64.length);
  7927. arraySet(directoryArray, rawExtraFieldExtendedTimestamp, offset + 46 + rawFilename.length + rawExtraFieldZip64.length + rawExtraFieldAES.length);
  7928. arraySet(directoryArray, rawExtraFieldNTFS, offset + 46 + rawFilename.length + rawExtraFieldZip64.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length);
  7929. arraySet(directoryArray, rawExtraField, offset + 46 + rawFilename.length + rawExtraFieldZip64.length + rawExtraFieldAES.length + rawExtraFieldExtendedTimestamp.length + rawExtraFieldNTFS.length);
  7930. arraySet(directoryArray, rawComment, offset + 46 + rawFilename.length + extraFieldLength);
  7931. offset += 46 + rawFilename.length + extraFieldLength + rawComment.length;
  7932. if (options.onprogress) {
  7933. try {
  7934. options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
  7935. } catch (error) {
  7936. // ignored
  7937. }
  7938. }
  7939. }
  7940. if (zip64) {
  7941. setUint32(directoryView, offset, ZIP64_END_OF_CENTRAL_DIR_SIGNATURE);
  7942. setBigUint64(directoryView, offset + 4, BigInt(44));
  7943. setUint16(directoryView, offset + 12, 45);
  7944. setUint16(directoryView, offset + 14, 45);
  7945. setBigUint64(directoryView, offset + 24, BigInt(filesLength));
  7946. setBigUint64(directoryView, offset + 32, BigInt(filesLength));
  7947. setBigUint64(directoryView, offset + 40, BigInt(directoryDataLength));
  7948. setBigUint64(directoryView, offset + 48, BigInt(directoryOffset));
  7949. setUint32(directoryView, offset + 56, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
  7950. setBigUint64(directoryView, offset + 64, BigInt(directoryOffset) + BigInt(directoryDataLength));
  7951. setUint32(directoryView, offset + 72, ZIP64_TOTAL_NUMBER_OF_DISKS);
  7952. filesLength = MAX_16_BITS;
  7953. directoryOffset = MAX_32_BITS;
  7954. directoryDataLength = MAX_32_BITS;
  7955. offset += 76;
  7956. }
  7957. setUint32(directoryView, offset, END_OF_CENTRAL_DIR_SIGNATURE);
  7958. setUint16(directoryView, offset + 8, filesLength);
  7959. setUint16(directoryView, offset + 10, filesLength);
  7960. setUint32(directoryView, offset + 12, directoryDataLength);
  7961. setUint32(directoryView, offset + 16, directoryOffset);
  7962. await writer.writeUint8Array(directoryArray);
  7963. if (comment && comment.length) {
  7964. await writer.writeUint8Array(comment);
  7965. }
  7966. }
  7967.  
  7968. function sliceAsArrayBuffer(blob, start, end) {
  7969. if (blob.arrayBuffer) {
  7970. if (start || end) {
  7971. return blob.slice(start, end).arrayBuffer();
  7972. } else {
  7973. return blob.arrayBuffer();
  7974. }
  7975. } else {
  7976. const fileReader = new FileReader();
  7977. return new Promise((resolve, reject) => {
  7978. fileReader.onload = event => resolve(event.target.result);
  7979. fileReader.onerror = () => reject(fileReader.error);
  7980. fileReader.readAsArrayBuffer(start || end ? blob.slice(start, end) : blob);
  7981. });
  7982. }
  7983. }
  7984.  
  7985. async function writeBlob(writer, blob, start = 0) {
  7986. const blockSize = 512 * 1024 * 1024;
  7987. await writeSlice();
  7988.  
  7989. async function writeSlice() {
  7990. if (start < blob.size) {
  7991. const arrayBuffer = await sliceAsArrayBuffer(blob, start, start + blockSize);
  7992. await writer.writeUint8Array(new Uint8Array(arrayBuffer));
  7993. start += blockSize;
  7994. await writeSlice();
  7995. }
  7996. }
  7997. }
  7998.  
  7999. function getTimeNTFS(date) {
  8000. if (date) {
  8001. return ((BigInt(date.getTime()) + BigInt(11644473600000)) * BigInt(10000));
  8002. }
  8003. }
  8004.  
  8005. function getOptionValue(zipWriter, options, name) {
  8006. return options[name] === undefined ? zipWriter.options[name] : options[name];
  8007. }
  8008.  
  8009. function getMaximumCompressedSize(uncompressedSize) {
  8010. return uncompressedSize + (5 * (Math.floor(uncompressedSize / 16383) + 1));
  8011. }
  8012.  
  8013. function setUint8(view, offset, value) {
  8014. view.setUint8(offset, value);
  8015. }
  8016.  
  8017. function setUint16(view, offset, value) {
  8018. view.setUint16(offset, value, true);
  8019. }
  8020.  
  8021. function setUint32(view, offset, value) {
  8022. view.setUint32(offset, value, true);
  8023. }
  8024.  
  8025. function setBigUint64(view, offset, value) {
  8026. view.setBigUint64(offset, value, true);
  8027. }
  8028.  
  8029. function arraySet(array, typedArray, offset) {
  8030. array.set(typedArray, offset);
  8031. }
  8032.  
  8033. function getDataView(array) {
  8034. return new DataView(array.buffer);
  8035. }
  8036.  
  8037. /*
  8038. Copyright (c) 2022 Gildas Lormeau. All rights reserved.
  8039.  
  8040. Redistribution and use in source and binary forms, with or without
  8041. modification, are permitted provided that the following conditions are met:
  8042.  
  8043. 1. Redistributions of source code must retain the above copyright notice,
  8044. this list of conditions and the following disclaimer.
  8045.  
  8046. 2. Redistributions in binary form must reproduce the above copyright
  8047. notice, this list of conditions and the following disclaimer in
  8048. the documentation and/or other materials provided with the distribution.
  8049.  
  8050. 3. The names of the authors may not be used to endorse or promote products
  8051. derived from this software without specific prior written permission.
  8052.  
  8053. THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  8054. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  8055. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  8056. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  8057. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8058. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  8059. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  8060. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  8061. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  8062. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8063. */
  8064.  
  8065. let baseURL;
  8066. try {
  8067. baseURL = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('zip-full.js', document.baseURI).href));
  8068. } catch (error) {
  8069. // ignored
  8070. }
  8071. t(configure);
  8072. configure({ Deflate: ZipDeflate, Inflate: ZipInflate, baseURL });
  8073.  
  8074. exports.BlobReader = BlobReader;
  8075. exports.BlobWriter = BlobWriter;
  8076. exports.Data64URIReader = Data64URIReader;
  8077. exports.Data64URIWriter = Data64URIWriter;
  8078. exports.ERR_ABORT = ERR_ABORT;
  8079. exports.ERR_BAD_FORMAT = ERR_BAD_FORMAT;
  8080. exports.ERR_CENTRAL_DIRECTORY_NOT_FOUND = ERR_CENTRAL_DIRECTORY_NOT_FOUND;
  8081. exports.ERR_DUPLICATED_NAME = ERR_DUPLICATED_NAME;
  8082. exports.ERR_ENCRYPTED = ERR_ENCRYPTED;
  8083. exports.ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND = ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND;
  8084. exports.ERR_EOCDR_NOT_FOUND = ERR_EOCDR_NOT_FOUND;
  8085. exports.ERR_EOCDR_ZIP64_NOT_FOUND = ERR_EOCDR_ZIP64_NOT_FOUND;
  8086. exports.ERR_EXTRAFIELD_ZIP64_NOT_FOUND = ERR_EXTRAFIELD_ZIP64_NOT_FOUND;
  8087. exports.ERR_HTTP_RANGE = ERR_HTTP_RANGE;
  8088. exports.ERR_INVALID_COMMENT = ERR_INVALID_COMMENT;
  8089. exports.ERR_INVALID_ENCRYPTION_STRENGTH = ERR_INVALID_ENCRYPTION_STRENGTH;
  8090. exports.ERR_INVALID_ENTRY_COMMENT = ERR_INVALID_ENTRY_COMMENT;
  8091. exports.ERR_INVALID_ENTRY_NAME = ERR_INVALID_ENTRY_NAME;
  8092. exports.ERR_INVALID_EXTRAFIELD_DATA = ERR_INVALID_EXTRAFIELD_DATA;
  8093. exports.ERR_INVALID_EXTRAFIELD_TYPE = ERR_INVALID_EXTRAFIELD_TYPE;
  8094. exports.ERR_INVALID_PASSWORD = ERR_INVALID_PASSWORD;
  8095. exports.ERR_INVALID_SIGNATURE = ERR_INVALID_SIGNATURE;
  8096. exports.ERR_INVALID_VERSION = ERR_INVALID_VERSION;
  8097. exports.ERR_LOCAL_FILE_HEADER_NOT_FOUND = ERR_LOCAL_FILE_HEADER_NOT_FOUND;
  8098. exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION;
  8099. exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
  8100. exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
  8101. exports.HttpRangeReader = HttpRangeReader;
  8102. exports.HttpReader = HttpReader;
  8103. exports.Reader = Reader;
  8104. exports.TextReader = TextReader;
  8105. exports.TextWriter = TextWriter;
  8106. exports.Uint8ArrayReader = Uint8ArrayReader;
  8107. exports.Uint8ArrayWriter = Uint8ArrayWriter;
  8108. exports.WritableStreamWriter = WritableStreamWriter;
  8109. exports.Writer = Writer;
  8110. exports.ZipReader = ZipReader;
  8111. exports.ZipWriter = ZipWriter;
  8112. exports.configure = configure;
  8113. exports.getMimeType = getMimeType;
  8114. exports.initShimAsyncCodec = streamCodecShim;
  8115. exports.terminateWorkers = terminateWorkers;
  8116.  
  8117. Object.defineProperty(exports, '__esModule', { value: true });
  8118.  
  8119. }));

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址