trees.js 38 KB

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