trees.js 37 KB

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