trees.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. 'use strict';
  2. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. // 2. Altered source versions must be plainly marked as such, and must not be
  18. // misrepresented as being the original software.
  19. // 3. This notice may not be removed or altered from any source distribution.
  20. /* eslint-disable space-unary-ops */
  21. /* Public constants ==========================================================*/
  22. /* ===========================================================================*/
  23. //const Z_FILTERED = 1;
  24. //const Z_HUFFMAN_ONLY = 2;
  25. //const Z_RLE = 3;
  26. const Z_FIXED = 4;
  27. //const Z_DEFAULT_STRATEGY = 0;
  28. /* Possible values of the data_type field (though see inflate()) */
  29. const Z_BINARY = 0;
  30. const Z_TEXT = 1;
  31. //const Z_ASCII = 1; // = Z_TEXT
  32. const Z_UNKNOWN = 2;
  33. /*============================================================================*/
  34. function zero(buf) { let len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  35. // From zutil.h
  36. const STORED_BLOCK = 0;
  37. const STATIC_TREES = 1;
  38. const DYN_TREES = 2;
  39. /* The three kinds of block type */
  40. const MIN_MATCH = 3;
  41. const MAX_MATCH = 258;
  42. /* The minimum and maximum match lengths */
  43. // From deflate.h
  44. /* ===========================================================================
  45. * Internal compression state.
  46. */
  47. const LENGTH_CODES = 29;
  48. /* number of length codes, not counting the special END_BLOCK code */
  49. const LITERALS = 256;
  50. /* number of literal bytes 0..255 */
  51. const L_CODES = LITERALS + 1 + LENGTH_CODES;
  52. /* number of Literal or Length codes, including the END_BLOCK code */
  53. const D_CODES = 30;
  54. /* number of distance codes */
  55. const BL_CODES = 19;
  56. /* number of codes used to transfer the bit lengths */
  57. const HEAP_SIZE = 2 * L_CODES + 1;
  58. /* maximum heap size */
  59. const MAX_BITS = 15;
  60. /* All codes must not exceed MAX_BITS bits */
  61. const Buf_size = 16;
  62. /* size of bit buffer in bi_buf */
  63. /* ===========================================================================
  64. * Constants
  65. */
  66. const MAX_BL_BITS = 7;
  67. /* Bit length codes must not exceed MAX_BL_BITS bits */
  68. const END_BLOCK = 256;
  69. /* end of block literal code */
  70. const REP_3_6 = 16;
  71. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  72. const REPZ_3_10 = 17;
  73. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  74. const REPZ_11_138 = 18;
  75. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  76. /* eslint-disable comma-spacing,array-bracket-spacing */
  77. const extra_lbits = /* extra bits for each length code */
  78. 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]);
  79. const extra_dbits = /* extra bits for each distance code */
  80. 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]);
  81. const extra_blbits = /* extra bits for each bit length code */
  82. new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]);
  83. const bl_order =
  84. new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);
  85. /* eslint-enable comma-spacing,array-bracket-spacing */
  86. /* The lengths of the bit length codes are sent in order of decreasing
  87. * probability, to avoid transmitting the lengths for unused bit length codes.
  88. */
  89. /* ===========================================================================
  90. * Local data. These are initialized only once.
  91. */
  92. // We pre-fill arrays with 0 to avoid uninitialized gaps
  93. const DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  94. // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
  95. const static_ltree = new Array((L_CODES + 2) * 2);
  96. zero(static_ltree);
  97. /* The static literal tree. Since the bit lengths are imposed, there is no
  98. * need for the L_CODES extra codes used during heap construction. However
  99. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  100. * below).
  101. */
  102. const static_dtree = new Array(D_CODES * 2);
  103. zero(static_dtree);
  104. /* The static distance tree. (Actually a trivial tree since all codes use
  105. * 5 bits.)
  106. */
  107. const _dist_code = new Array(DIST_CODE_LEN);
  108. zero(_dist_code);
  109. /* Distance codes. The first 256 values correspond to the distances
  110. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  111. * the 15 bit distances.
  112. */
  113. const _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
  114. zero(_length_code);
  115. /* length code for each normalized match length (0 == MIN_MATCH) */
  116. const base_length = new Array(LENGTH_CODES);
  117. zero(base_length);
  118. /* First normalized length for each code (0 = MIN_MATCH) */
  119. const base_dist = new Array(D_CODES);
  120. zero(base_dist);
  121. /* First normalized distance for each code (0 = distance of 1) */
  122. function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
  123. this.static_tree = static_tree; /* static tree or NULL */
  124. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  125. this.extra_base = extra_base; /* base index for extra_bits */
  126. this.elems = elems; /* max number of elements in the tree */
  127. this.max_length = max_length; /* max bit length for the codes */
  128. // show if `static_tree` has data or dummy - needed for monomorphic objects
  129. this.has_stree = static_tree && static_tree.length;
  130. }
  131. let static_l_desc;
  132. let static_d_desc;
  133. let static_bl_desc;
  134. function TreeDesc(dyn_tree, stat_desc) {
  135. this.dyn_tree = dyn_tree; /* the dynamic tree */
  136. this.max_code = 0; /* largest code with non zero frequency */
  137. this.stat_desc = stat_desc; /* the corresponding static tree */
  138. }
  139. const d_code = (dist) => {
  140. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  141. };
  142. /* ===========================================================================
  143. * Output a short LSB first on the stream.
  144. * IN assertion: there is enough room in pendingBuf.
  145. */
  146. const put_short = (s, w) => {
  147. // put_byte(s, (uch)((w) & 0xff));
  148. // put_byte(s, (uch)((ush)(w) >> 8));
  149. s.pending_buf[s.pending++] = (w) & 0xff;
  150. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  151. };
  152. /* ===========================================================================
  153. * Send a value on a given number of bits.
  154. * IN assertion: length <= 16 and value fits in length bits.
  155. */
  156. const send_bits = (s, value, length) => {
  157. if (s.bi_valid > (Buf_size - length)) {
  158. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  159. put_short(s, s.bi_buf);
  160. s.bi_buf = value >> (Buf_size - s.bi_valid);
  161. s.bi_valid += length - Buf_size;
  162. } else {
  163. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  164. s.bi_valid += length;
  165. }
  166. };
  167. const send_code = (s, c, tree) => {
  168. send_bits(s, tree[c * 2]/*.Code*/, tree[c * 2 + 1]/*.Len*/);
  169. };
  170. /* ===========================================================================
  171. * Reverse the first len bits of a code, using straightforward code (a faster
  172. * method would use a table)
  173. * IN assertion: 1 <= len <= 15
  174. */
  175. const bi_reverse = (code, len) => {
  176. let res = 0;
  177. do {
  178. res |= code & 1;
  179. code >>>= 1;
  180. res <<= 1;
  181. } while (--len > 0);
  182. return res >>> 1;
  183. };
  184. /* ===========================================================================
  185. * Flush the bit buffer, keeping at most 7 bits in it.
  186. */
  187. const bi_flush = (s) => {
  188. if (s.bi_valid === 16) {
  189. put_short(s, s.bi_buf);
  190. s.bi_buf = 0;
  191. s.bi_valid = 0;
  192. } else if (s.bi_valid >= 8) {
  193. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  194. s.bi_buf >>= 8;
  195. s.bi_valid -= 8;
  196. }
  197. };
  198. /* ===========================================================================
  199. * Compute the optimal bit lengths for a tree and update the total bit length
  200. * for the current block.
  201. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  202. * above are the tree nodes sorted by increasing frequency.
  203. * OUT assertions: the field len is set to the optimal bit length, the
  204. * array bl_count contains the frequencies for each bit length.
  205. * The length opt_len is updated; static_len is also updated if stree is
  206. * not null.
  207. */
  208. const gen_bitlen = (s, desc) => {
  209. // deflate_state *s;
  210. // tree_desc *desc; /* the tree descriptor */
  211. const tree = desc.dyn_tree;
  212. const max_code = desc.max_code;
  213. const stree = desc.stat_desc.static_tree;
  214. const has_stree = desc.stat_desc.has_stree;
  215. const extra = desc.stat_desc.extra_bits;
  216. const base = desc.stat_desc.extra_base;
  217. const max_length = desc.stat_desc.max_length;
  218. let h; /* heap index */
  219. let n, m; /* iterate over the tree elements */
  220. let bits; /* bit length */
  221. let xbits; /* extra bits */
  222. let f; /* frequency */
  223. let overflow = 0; /* number of elements with bit length too large */
  224. for (bits = 0; bits <= MAX_BITS; bits++) {
  225. s.bl_count[bits] = 0;
  226. }
  227. /* In a first pass, compute the optimal bit lengths (which may
  228. * overflow in the case of the bit length tree).
  229. */
  230. tree[s.heap[s.heap_max] * 2 + 1]/*.Len*/ = 0; /* root of the heap */
  231. for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
  232. n = s.heap[h];
  233. bits = tree[tree[n * 2 + 1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  234. if (bits > max_length) {
  235. bits = max_length;
  236. overflow++;
  237. }
  238. tree[n * 2 + 1]/*.Len*/ = bits;
  239. /* We overwrite tree[n].Dad which is no longer needed */
  240. if (n > max_code) { continue; } /* not a leaf node */
  241. s.bl_count[bits]++;
  242. xbits = 0;
  243. if (n >= base) {
  244. xbits = extra[n - base];
  245. }
  246. f = tree[n * 2]/*.Freq*/;
  247. s.opt_len += f * (bits + xbits);
  248. if (has_stree) {
  249. s.static_len += f * (stree[n * 2 + 1]/*.Len*/ + xbits);
  250. }
  251. }
  252. if (overflow === 0) { return; }
  253. // Tracev((stderr,"\nbit length overflow\n"));
  254. /* This happens for example on obj2 and pic of the Calgary corpus */
  255. /* Find the first bit length which could increase: */
  256. do {
  257. bits = max_length - 1;
  258. while (s.bl_count[bits] === 0) { bits--; }
  259. s.bl_count[bits]--; /* move one leaf down the tree */
  260. s.bl_count[bits + 1] += 2; /* move one overflow item as its brother */
  261. s.bl_count[max_length]--;
  262. /* The brother of the overflow item also moves one step up,
  263. * but this does not affect bl_count[max_length]
  264. */
  265. overflow -= 2;
  266. } while (overflow > 0);
  267. /* Now recompute all bit lengths, scanning in increasing frequency.
  268. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  269. * lengths instead of fixing only the wrong ones. This idea is taken
  270. * from 'ar' written by Haruhiko Okumura.)
  271. */
  272. for (bits = max_length; bits !== 0; bits--) {
  273. n = s.bl_count[bits];
  274. while (n !== 0) {
  275. m = s.heap[--h];
  276. if (m > max_code) { continue; }
  277. if (tree[m * 2 + 1]/*.Len*/ !== bits) {
  278. // Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  279. s.opt_len += (bits - tree[m * 2 + 1]/*.Len*/) * tree[m * 2]/*.Freq*/;
  280. tree[m * 2 + 1]/*.Len*/ = bits;
  281. }
  282. n--;
  283. }
  284. }
  285. };
  286. /* ===========================================================================
  287. * Generate the codes for a given tree and bit counts (which need not be
  288. * optimal).
  289. * IN assertion: the array bl_count contains the bit length statistics for
  290. * the given tree and the field len is set for all tree elements.
  291. * OUT assertion: the field code is set for all tree elements of non
  292. * zero code length.
  293. */
  294. const gen_codes = (tree, max_code, bl_count) => {
  295. // ct_data *tree; /* the tree to decorate */
  296. // int max_code; /* largest code with non zero frequency */
  297. // ushf *bl_count; /* number of codes at each bit length */
  298. const next_code = new Array(MAX_BITS + 1); /* next code value for each bit length */
  299. let code = 0; /* running code value */
  300. let bits; /* bit index */
  301. let n; /* code index */
  302. /* The distribution counts are first used to generate the code values
  303. * without bit reversal.
  304. */
  305. for (bits = 1; bits <= MAX_BITS; bits++) {
  306. code = (code + bl_count[bits - 1]) << 1;
  307. next_code[bits] = code;
  308. }
  309. /* Check that the bit counts in bl_count are consistent. The last code
  310. * must be all ones.
  311. */
  312. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  313. // "inconsistent bit counts");
  314. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  315. for (n = 0; n <= max_code; n++) {
  316. let len = tree[n * 2 + 1]/*.Len*/;
  317. if (len === 0) { continue; }
  318. /* Now reverse the bits */
  319. tree[n * 2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  320. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  321. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  322. }
  323. };
  324. /* ===========================================================================
  325. * Initialize the various 'constant' tables.
  326. */
  327. const tr_static_init = () => {
  328. let n; /* iterates over tree elements */
  329. let bits; /* bit counter */
  330. let length; /* length value */
  331. let code; /* code value */
  332. let dist; /* distance index */
  333. const bl_count = new Array(MAX_BITS + 1);
  334. /* number of codes at each bit length for an optimal tree */
  335. // do check in _tr_init()
  336. //if (static_init_done) return;
  337. /* For some embedded targets, global variables are not initialized: */
  338. /*#ifdef NO_INIT_GLOBAL_POINTERS
  339. static_l_desc.static_tree = static_ltree;
  340. static_l_desc.extra_bits = extra_lbits;
  341. static_d_desc.static_tree = static_dtree;
  342. static_d_desc.extra_bits = extra_dbits;
  343. static_bl_desc.extra_bits = extra_blbits;
  344. #endif*/
  345. /* Initialize the mapping length (0..255) -> length code (0..28) */
  346. length = 0;
  347. for (code = 0; code < LENGTH_CODES - 1; code++) {
  348. base_length[code] = length;
  349. for (n = 0; n < (1 << extra_lbits[code]); n++) {
  350. _length_code[length++] = code;
  351. }
  352. }
  353. //Assert (length == 256, "tr_static_init: length != 256");
  354. /* Note that the length 255 (match length 258) can be represented
  355. * in two different ways: code 284 + 5 bits or code 285, so we
  356. * overwrite length_code[255] to use the best encoding:
  357. */
  358. _length_code[length - 1] = code;
  359. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  360. dist = 0;
  361. for (code = 0; code < 16; code++) {
  362. base_dist[code] = dist;
  363. for (n = 0; n < (1 << extra_dbits[code]); n++) {
  364. _dist_code[dist++] = code;
  365. }
  366. }
  367. //Assert (dist == 256, "tr_static_init: dist != 256");
  368. dist >>= 7; /* from now on, all distances are divided by 128 */
  369. for (; code < D_CODES; code++) {
  370. base_dist[code] = dist << 7;
  371. for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
  372. _dist_code[256 + dist++] = code;
  373. }
  374. }
  375. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  376. /* Construct the codes of the static literal tree */
  377. for (bits = 0; bits <= MAX_BITS; bits++) {
  378. bl_count[bits] = 0;
  379. }
  380. n = 0;
  381. while (n <= 143) {
  382. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  383. n++;
  384. bl_count[8]++;
  385. }
  386. while (n <= 255) {
  387. static_ltree[n * 2 + 1]/*.Len*/ = 9;
  388. n++;
  389. bl_count[9]++;
  390. }
  391. while (n <= 279) {
  392. static_ltree[n * 2 + 1]/*.Len*/ = 7;
  393. n++;
  394. bl_count[7]++;
  395. }
  396. while (n <= 287) {
  397. static_ltree[n * 2 + 1]/*.Len*/ = 8;
  398. n++;
  399. bl_count[8]++;
  400. }
  401. /* Codes 286 and 287 do not exist, but we must include them in the
  402. * tree construction to get a canonical Huffman tree (longest code
  403. * all ones)
  404. */
  405. gen_codes(static_ltree, L_CODES + 1, bl_count);
  406. /* The static distance tree is trivial: */
  407. for (n = 0; n < D_CODES; n++) {
  408. static_dtree[n * 2 + 1]/*.Len*/ = 5;
  409. static_dtree[n * 2]/*.Code*/ = bi_reverse(n, 5);
  410. }
  411. // Now data ready and we can init static trees
  412. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
  413. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
  414. static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  415. //static_init_done = true;
  416. };
  417. /* ===========================================================================
  418. * Initialize a new block.
  419. */
  420. const init_block = (s) => {
  421. let n; /* iterates over tree elements */
  422. /* Initialize the trees. */
  423. for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n * 2]/*.Freq*/ = 0; }
  424. for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n * 2]/*.Freq*/ = 0; }
  425. for (n = 0; n < BL_CODES; n++) { s.bl_tree[n * 2]/*.Freq*/ = 0; }
  426. s.dyn_ltree[END_BLOCK * 2]/*.Freq*/ = 1;
  427. s.opt_len = s.static_len = 0;
  428. s.sym_next = s.matches = 0;
  429. };
  430. /* ===========================================================================
  431. * Flush the bit buffer and align the output on a byte boundary
  432. */
  433. const bi_windup = (s) =>
  434. {
  435. if (s.bi_valid > 8) {
  436. put_short(s, s.bi_buf);
  437. } else if (s.bi_valid > 0) {
  438. //put_byte(s, (Byte)s->bi_buf);
  439. s.pending_buf[s.pending++] = s.bi_buf;
  440. }
  441. s.bi_buf = 0;
  442. s.bi_valid = 0;
  443. };
  444. /* ===========================================================================
  445. * Compares to subtrees, using the tree depth as tie breaker when
  446. * the subtrees have equal frequency. This minimizes the worst case length.
  447. */
  448. const smaller = (tree, n, m, depth) => {
  449. const _n2 = n * 2;
  450. const _m2 = m * 2;
  451. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  452. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  453. };
  454. /* ===========================================================================
  455. * Restore the heap property by moving down the tree starting at node k,
  456. * exchanging a node with the smallest of its two sons if necessary, stopping
  457. * when the heap property is re-established (each father smaller than its
  458. * two sons).
  459. */
  460. const pqdownheap = (s, tree, k) => {
  461. // deflate_state *s;
  462. // ct_data *tree; /* the tree to restore */
  463. // int k; /* node to move down */
  464. const v = s.heap[k];
  465. let j = k << 1; /* left son of k */
  466. while (j <= s.heap_len) {
  467. /* Set j to the smallest of the two sons: */
  468. if (j < s.heap_len &&
  469. smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
  470. j++;
  471. }
  472. /* Exit if v is smaller than both sons */
  473. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  474. /* Exchange v with the smallest son */
  475. s.heap[k] = s.heap[j];
  476. k = j;
  477. /* And continue down the tree, setting j to the left son of k */
  478. j <<= 1;
  479. }
  480. s.heap[k] = v;
  481. };
  482. // inlined manually
  483. // const SMALLEST = 1;
  484. /* ===========================================================================
  485. * Send the block data compressed using the given Huffman trees
  486. */
  487. const compress_block = (s, ltree, dtree) => {
  488. // deflate_state *s;
  489. // const ct_data *ltree; /* literal tree */
  490. // const ct_data *dtree; /* distance tree */
  491. let dist; /* distance of matched string */
  492. let lc; /* match length or unmatched char (if dist == 0) */
  493. let sx = 0; /* running index in sym_buf */
  494. let code; /* the code to send */
  495. let extra; /* number of extra bits to send */
  496. if (s.sym_next !== 0) {
  497. do {
  498. dist = s.pending_buf[s.sym_buf + sx++] & 0xff;
  499. dist += (s.pending_buf[s.sym_buf + sx++] & 0xff) << 8;
  500. lc = s.pending_buf[s.sym_buf + sx++];
  501. if (dist === 0) {
  502. send_code(s, lc, ltree); /* send a literal byte */
  503. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  504. } else {
  505. /* Here, lc is the match length - MIN_MATCH */
  506. code = _length_code[lc];
  507. send_code(s, code + LITERALS + 1, ltree); /* send the length code */
  508. extra = extra_lbits[code];
  509. if (extra !== 0) {
  510. lc -= base_length[code];
  511. send_bits(s, lc, extra); /* send the extra length bits */
  512. }
  513. dist--; /* dist is now the match distance - 1 */
  514. code = d_code(dist);
  515. //Assert (code < D_CODES, "bad d_code");
  516. send_code(s, code, dtree); /* send the distance code */
  517. extra = extra_dbits[code];
  518. if (extra !== 0) {
  519. dist -= base_dist[code];
  520. send_bits(s, dist, extra); /* send the extra distance bits */
  521. }
  522. } /* literal or match pair ? */
  523. /* Check that the overlay between pending_buf and sym_buf is ok: */
  524. //Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
  525. } while (sx < s.sym_next);
  526. }
  527. send_code(s, END_BLOCK, ltree);
  528. };
  529. /* ===========================================================================
  530. * Construct one Huffman tree and assigns the code bit strings and lengths.
  531. * Update the total bit length for the current block.
  532. * IN assertion: the field freq is set for all tree elements.
  533. * OUT assertions: the fields len and code are set to the optimal bit length
  534. * and corresponding code. The length opt_len is updated; static_len is
  535. * also updated if stree is not null. The field max_code is set.
  536. */
  537. const build_tree = (s, desc) => {
  538. // deflate_state *s;
  539. // tree_desc *desc; /* the tree descriptor */
  540. const tree = desc.dyn_tree;
  541. const stree = desc.stat_desc.static_tree;
  542. const has_stree = desc.stat_desc.has_stree;
  543. const elems = desc.stat_desc.elems;
  544. let n, m; /* iterate over heap elements */
  545. let max_code = -1; /* largest code with non zero frequency */
  546. let node; /* new node being created */
  547. /* Construct the initial heap, with least frequent element in
  548. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  549. * heap[0] is not used.
  550. */
  551. s.heap_len = 0;
  552. s.heap_max = HEAP_SIZE;
  553. for (n = 0; n < elems; n++) {
  554. if (tree[n * 2]/*.Freq*/ !== 0) {
  555. s.heap[++s.heap_len] = max_code = n;
  556. s.depth[n] = 0;
  557. } else {
  558. tree[n * 2 + 1]/*.Len*/ = 0;
  559. }
  560. }
  561. /* The pkzip format requires that at least one distance code exists,
  562. * and that at least one bit should be sent even if there is only one
  563. * possible code. So to avoid special checks later on we force at least
  564. * two codes of non zero frequency.
  565. */
  566. while (s.heap_len < 2) {
  567. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  568. tree[node * 2]/*.Freq*/ = 1;
  569. s.depth[node] = 0;
  570. s.opt_len--;
  571. if (has_stree) {
  572. s.static_len -= stree[node * 2 + 1]/*.Len*/;
  573. }
  574. /* node is 0 or 1 so it does not have extra bits */
  575. }
  576. desc.max_code = max_code;
  577. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  578. * establish sub-heaps of increasing lengths:
  579. */
  580. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  581. /* Construct the Huffman tree by repeatedly combining the least two
  582. * frequent nodes.
  583. */
  584. node = elems; /* next internal node of the tree */
  585. do {
  586. //pqremove(s, tree, n); /* n = node of least frequency */
  587. /*** pqremove ***/
  588. n = s.heap[1/*SMALLEST*/];
  589. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  590. pqdownheap(s, tree, 1/*SMALLEST*/);
  591. /***/
  592. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  593. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  594. s.heap[--s.heap_max] = m;
  595. /* Create a new node father of n and m */
  596. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  597. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  598. tree[n * 2 + 1]/*.Dad*/ = tree[m * 2 + 1]/*.Dad*/ = node;
  599. /* and insert the new node in the heap */
  600. s.heap[1/*SMALLEST*/] = node++;
  601. pqdownheap(s, tree, 1/*SMALLEST*/);
  602. } while (s.heap_len >= 2);
  603. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  604. /* At this point, the fields freq and dad are set. We can now
  605. * generate the bit lengths.
  606. */
  607. gen_bitlen(s, desc);
  608. /* The field len is now set, we can generate the bit codes */
  609. gen_codes(tree, max_code, s.bl_count);
  610. };
  611. /* ===========================================================================
  612. * Scan a literal or distance tree to determine the frequencies of the codes
  613. * in the bit length tree.
  614. */
  615. const scan_tree = (s, tree, max_code) => {
  616. // deflate_state *s;
  617. // ct_data *tree; /* the tree to be scanned */
  618. // int max_code; /* and its largest code of non zero frequency */
  619. let n; /* iterates over all tree elements */
  620. let prevlen = -1; /* last emitted length */
  621. let curlen; /* length of current code */
  622. let nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  623. let count = 0; /* repeat count of the current code */
  624. let max_count = 7; /* max repeat count */
  625. let min_count = 4; /* min repeat count */
  626. if (nextlen === 0) {
  627. max_count = 138;
  628. min_count = 3;
  629. }
  630. tree[(max_code + 1) * 2 + 1]/*.Len*/ = 0xffff; /* guard */
  631. for (n = 0; n <= max_code; n++) {
  632. curlen = nextlen;
  633. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  634. if (++count < max_count && curlen === nextlen) {
  635. continue;
  636. } else if (count < min_count) {
  637. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  638. } else if (curlen !== 0) {
  639. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  640. s.bl_tree[REP_3_6 * 2]/*.Freq*/++;
  641. } else if (count <= 10) {
  642. s.bl_tree[REPZ_3_10 * 2]/*.Freq*/++;
  643. } else {
  644. s.bl_tree[REPZ_11_138 * 2]/*.Freq*/++;
  645. }
  646. count = 0;
  647. prevlen = curlen;
  648. if (nextlen === 0) {
  649. max_count = 138;
  650. min_count = 3;
  651. } else if (curlen === nextlen) {
  652. max_count = 6;
  653. min_count = 3;
  654. } else {
  655. max_count = 7;
  656. min_count = 4;
  657. }
  658. }
  659. };
  660. /* ===========================================================================
  661. * Send a literal or distance tree in compressed form, using the codes in
  662. * bl_tree.
  663. */
  664. const send_tree = (s, tree, max_code) => {
  665. // deflate_state *s;
  666. // ct_data *tree; /* the tree to be scanned */
  667. // int max_code; /* and its largest code of non zero frequency */
  668. let n; /* iterates over all tree elements */
  669. let prevlen = -1; /* last emitted length */
  670. let curlen; /* length of current code */
  671. let nextlen = tree[0 * 2 + 1]/*.Len*/; /* length of next code */
  672. let count = 0; /* repeat count of the current code */
  673. let max_count = 7; /* max repeat count */
  674. let min_count = 4; /* min repeat count */
  675. /* tree[max_code+1].Len = -1; */ /* guard already set */
  676. if (nextlen === 0) {
  677. max_count = 138;
  678. min_count = 3;
  679. }
  680. for (n = 0; n <= max_code; n++) {
  681. curlen = nextlen;
  682. nextlen = tree[(n + 1) * 2 + 1]/*.Len*/;
  683. if (++count < max_count && curlen === nextlen) {
  684. continue;
  685. } else if (count < min_count) {
  686. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  687. } else if (curlen !== 0) {
  688. if (curlen !== prevlen) {
  689. send_code(s, curlen, s.bl_tree);
  690. count--;
  691. }
  692. //Assert(count >= 3 && count <= 6, " 3_6?");
  693. send_code(s, REP_3_6, s.bl_tree);
  694. send_bits(s, count - 3, 2);
  695. } else if (count <= 10) {
  696. send_code(s, REPZ_3_10, s.bl_tree);
  697. send_bits(s, count - 3, 3);
  698. } else {
  699. send_code(s, REPZ_11_138, s.bl_tree);
  700. send_bits(s, count - 11, 7);
  701. }
  702. count = 0;
  703. prevlen = curlen;
  704. if (nextlen === 0) {
  705. max_count = 138;
  706. min_count = 3;
  707. } else if (curlen === nextlen) {
  708. max_count = 6;
  709. min_count = 3;
  710. } else {
  711. max_count = 7;
  712. min_count = 4;
  713. }
  714. }
  715. };
  716. /* ===========================================================================
  717. * Construct the Huffman tree for the bit lengths and return the index in
  718. * bl_order of the last bit length code to send.
  719. */
  720. const build_bl_tree = (s) => {
  721. let max_blindex; /* index of last bit length code of non zero freq */
  722. /* Determine the bit length frequencies for literal and distance trees */
  723. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  724. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  725. /* Build the bit length tree: */
  726. build_tree(s, s.bl_desc);
  727. /* opt_len now includes the length of the tree representations, except
  728. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  729. */
  730. /* Determine the number of bit length codes to send. The pkzip format
  731. * requires that at least 4 bit length codes be sent. (appnote.txt says
  732. * 3 but the actual value used is 4.)
  733. */
  734. for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
  735. if (s.bl_tree[bl_order[max_blindex] * 2 + 1]/*.Len*/ !== 0) {
  736. break;
  737. }
  738. }
  739. /* Update opt_len to include the bit length tree and counts */
  740. s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
  741. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  742. // s->opt_len, s->static_len));
  743. return max_blindex;
  744. };
  745. /* ===========================================================================
  746. * Send the header for a block using dynamic Huffman trees: the counts, the
  747. * lengths of the bit length codes, the literal tree and the distance tree.
  748. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  749. */
  750. const send_all_trees = (s, lcodes, dcodes, blcodes) => {
  751. // deflate_state *s;
  752. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  753. let rank; /* index in bl_order */
  754. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  755. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  756. // "too many codes");
  757. //Tracev((stderr, "\nbl counts: "));
  758. send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
  759. send_bits(s, dcodes - 1, 5);
  760. send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
  761. for (rank = 0; rank < blcodes; rank++) {
  762. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  763. send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1]/*.Len*/, 3);
  764. }
  765. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  766. send_tree(s, s.dyn_ltree, lcodes - 1); /* literal tree */
  767. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  768. send_tree(s, s.dyn_dtree, dcodes - 1); /* distance tree */
  769. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  770. };
  771. /* ===========================================================================
  772. * Check if the data type is TEXT or BINARY, using the following algorithm:
  773. * - TEXT if the two conditions below are satisfied:
  774. * a) There are no non-portable control characters belonging to the
  775. * "block list" (0..6, 14..25, 28..31).
  776. * b) There is at least one printable character belonging to the
  777. * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  778. * - BINARY otherwise.
  779. * - The following partially-portable control characters form a
  780. * "gray list" that is ignored in this detection algorithm:
  781. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  782. * IN assertion: the fields Freq of dyn_ltree are set.
  783. */
  784. const detect_data_type = (s) => {
  785. /* block_mask is the bit mask of block-listed bytes
  786. * set bits 0..6, 14..25, and 28..31
  787. * 0xf3ffc07f = binary 11110011111111111100000001111111
  788. */
  789. let block_mask = 0xf3ffc07f;
  790. let n;
  791. /* Check for non-textual ("block-listed") bytes. */
  792. for (n = 0; n <= 31; n++, block_mask >>>= 1) {
  793. if ((block_mask & 1) && (s.dyn_ltree[n * 2]/*.Freq*/ !== 0)) {
  794. return Z_BINARY;
  795. }
  796. }
  797. /* Check for textual ("allow-listed") bytes. */
  798. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  799. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  800. return Z_TEXT;
  801. }
  802. for (n = 32; n < LITERALS; n++) {
  803. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  804. return Z_TEXT;
  805. }
  806. }
  807. /* There are no "block-listed" or "allow-listed" bytes:
  808. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  809. */
  810. return Z_BINARY;
  811. };
  812. let static_init_done = false;
  813. /* ===========================================================================
  814. * Initialize the tree data structures for a new zlib stream.
  815. */
  816. const _tr_init = (s) =>
  817. {
  818. if (!static_init_done) {
  819. tr_static_init();
  820. static_init_done = true;
  821. }
  822. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  823. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  824. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  825. s.bi_buf = 0;
  826. s.bi_valid = 0;
  827. /* Initialize the first block of the first file: */
  828. init_block(s);
  829. };
  830. /* ===========================================================================
  831. * Send a stored block
  832. */
  833. const _tr_stored_block = (s, buf, stored_len, last) => {
  834. //DeflateState *s;
  835. //charf *buf; /* input block */
  836. //ulg stored_len; /* length of input block */
  837. //int last; /* one if this is the last block for a file */
  838. send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); /* send block type */
  839. bi_windup(s); /* align on byte boundary */
  840. put_short(s, stored_len);
  841. put_short(s, ~stored_len);
  842. if (stored_len) {
  843. s.pending_buf.set(s.window.subarray(buf, buf + stored_len), s.pending);
  844. }
  845. s.pending += stored_len;
  846. };
  847. /* ===========================================================================
  848. * Send one empty static block to give enough lookahead for inflate.
  849. * This takes 10 bits, of which 7 may remain in the bit buffer.
  850. */
  851. const _tr_align = (s) => {
  852. send_bits(s, STATIC_TREES << 1, 3);
  853. send_code(s, END_BLOCK, static_ltree);
  854. bi_flush(s);
  855. };
  856. /* ===========================================================================
  857. * Determine the best encoding for the current block: dynamic trees, static
  858. * trees or store, and write out the encoded block.
  859. */
  860. const _tr_flush_block = (s, buf, stored_len, last) => {
  861. //DeflateState *s;
  862. //charf *buf; /* input block, or NULL if too old */
  863. //ulg stored_len; /* length of input block */
  864. //int last; /* one if this is the last block for a file */
  865. let opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  866. let max_blindex = 0; /* index of last bit length code of non zero freq */
  867. /* Build the Huffman trees unless a stored block is forced */
  868. if (s.level > 0) {
  869. /* Check if the file is binary or text */
  870. if (s.strm.data_type === Z_UNKNOWN) {
  871. s.strm.data_type = detect_data_type(s);
  872. }
  873. /* Construct the literal and distance trees */
  874. build_tree(s, s.l_desc);
  875. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  876. // s->static_len));
  877. build_tree(s, s.d_desc);
  878. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  879. // s->static_len));
  880. /* At this point, opt_len and static_len are the total bit lengths of
  881. * the compressed block data, excluding the tree representations.
  882. */
  883. /* Build the bit length tree for the above two trees, and get the index
  884. * in bl_order of the last bit length code to send.
  885. */
  886. max_blindex = build_bl_tree(s);
  887. /* Determine the best encoding. Compute the block lengths in bytes. */
  888. opt_lenb = (s.opt_len + 3 + 7) >>> 3;
  889. static_lenb = (s.static_len + 3 + 7) >>> 3;
  890. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  891. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  892. // s->sym_next / 3));
  893. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  894. } else {
  895. // Assert(buf != (char*)0, "lost buf");
  896. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  897. }
  898. if ((stored_len + 4 <= opt_lenb) && (buf !== -1)) {
  899. /* 4: two words for the lengths */
  900. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  901. * Otherwise we can't have processed more than WSIZE input bytes since
  902. * the last block flush, because compression would have been
  903. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  904. * transform a block into a stored block.
  905. */
  906. _tr_stored_block(s, buf, stored_len, last);
  907. } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
  908. send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
  909. compress_block(s, static_ltree, static_dtree);
  910. } else {
  911. send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
  912. send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
  913. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  914. }
  915. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  916. /* The above check is made mod 2^32, for files larger than 512 MB
  917. * and uLong implemented on 32 bits.
  918. */
  919. init_block(s);
  920. if (last) {
  921. bi_windup(s);
  922. }
  923. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  924. // s->compressed_len-7*last));
  925. };
  926. /* ===========================================================================
  927. * Save the match info and tally the frequency counts. Return true if
  928. * the current block must be flushed.
  929. */
  930. const _tr_tally = (s, dist, lc) => {
  931. // deflate_state *s;
  932. // unsigned dist; /* distance of matched string */
  933. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  934. s.pending_buf[s.sym_buf + s.sym_next++] = dist;
  935. s.pending_buf[s.sym_buf + s.sym_next++] = dist >> 8;
  936. s.pending_buf[s.sym_buf + s.sym_next++] = lc;
  937. if (dist === 0) {
  938. /* lc is the unmatched char */
  939. s.dyn_ltree[lc * 2]/*.Freq*/++;
  940. } else {
  941. s.matches++;
  942. /* Here, lc is the match length - MIN_MATCH */
  943. dist--; /* dist = match distance - 1 */
  944. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  945. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  946. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  947. s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]/*.Freq*/++;
  948. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  949. }
  950. return (s.sym_next === s.sym_end);
  951. };
  952. module.exports._tr_init = _tr_init;
  953. module.exports._tr_stored_block = _tr_stored_block;
  954. module.exports._tr_flush_block = _tr_flush_block;
  955. module.exports._tr_tally = _tr_tally;
  956. module.exports._tr_align = _tr_align;