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

6725 lines
215 KiB

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