trees.c 40 KB

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