trees.c 40 KB

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