trees.js 39 KB

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