enough.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* enough.c -- determine the maximum size of inflate's Huffman code tables over
  2. * all possible valid and complete prefix codes, subject to a length limit.
  3. * Copyright (C) 2007, 2008, 2012, 2018 Mark Adler
  4. * Version 1.5 5 August 2018 Mark Adler
  5. */
  6. /* Version history:
  7. 1.0 3 Jan 2007 First version (derived from codecount.c version 1.4)
  8. 1.1 4 Jan 2007 Use faster incremental table usage computation
  9. Prune examine() search on previously visited states
  10. 1.2 5 Jan 2007 Comments clean up
  11. As inflate does, decrease root for short codes
  12. Refuse cases where inflate would increase root
  13. 1.3 17 Feb 2008 Add argument for initial root table size
  14. Fix bug for initial root table size == max - 1
  15. Use a macro to compute the history index
  16. 1.4 18 Aug 2012 Avoid shifts more than bits in type (caused endless loop!)
  17. Clean up comparisons of different types
  18. Clean up code indentation
  19. 1.5 5 Aug 2018 Clean up code style, formatting, and comments
  20. Show all the codes for the maximum, and only the maximum
  21. */
  22. /*
  23. Examine all possible prefix codes for a given number of symbols and a
  24. maximum code length in bits to determine the maximum table size for zlib's
  25. inflate. Only complete prefix codes are counted.
  26. Two codes are considered distinct if the vectors of the number of codes per
  27. length are not identical. So permutations of the symbol assignments result
  28. in the same code for the counting, as do permutations of the assignments of
  29. the bit values to the codes (i.e. only canonical codes are counted).
  30. We build a code from shorter to longer lengths, determining how many symbols
  31. are coded at each length. At each step, we have how many symbols remain to
  32. be coded, what the last code length used was, and how many bit patterns of
  33. that length remain unused. Then we add one to the code length and double the
  34. number of unused patterns to graduate to the next code length. We then
  35. assign all portions of the remaining symbols to that code length that
  36. preserve the properties of a correct and eventually complete code. Those
  37. properties are: we cannot use more bit patterns than are available; and when
  38. all the symbols are used, there are exactly zero possible bit patterns left
  39. unused.
  40. The inflate Huffman decoding algorithm uses two-level lookup tables for
  41. speed. There is a single first-level table to decode codes up to root bits
  42. in length (root == 9 for literal/length codes and root == 6 for distance
  43. codes, in the current inflate implementation). The base table has 1 << root
  44. entries and is indexed by the next root bits of input. Codes shorter than
  45. root bits have replicated table entries, so that the correct entry is
  46. pointed to regardless of the bits that follow the short code. If the code is
  47. longer than root bits, then the table entry points to a second-level table.
  48. The size of that table is determined by the longest code with that root-bit
  49. prefix. If that longest code has length len, then the table has size 1 <<
  50. (len - root), to index the remaining bits in that set of codes. Each
  51. subsequent root-bit prefix then has its own sub-table. The total number of
  52. table entries required by the code is calculated incrementally as the number
  53. of codes at each bit length is populated. When all of the codes are shorter
  54. than root bits, then root is reduced to the longest code length, resulting
  55. in a single, smaller, one-level table.
  56. The inflate algorithm also provides for small values of root (relative to
  57. the log2 of the number of symbols), where the shortest code has more bits
  58. than root. In that case, root is increased to the length of the shortest
  59. code. This program, by design, does not handle that case, so it is verified
  60. that the number of symbols is less than 1 << (root + 1).
  61. In order to speed up the examination (by about ten orders of magnitude for
  62. the default arguments), the intermediate states in the build-up of a code
  63. are remembered and previously visited branches are pruned. The memory
  64. required for this will increase rapidly with the total number of symbols and
  65. the maximum code length in bits. However this is a very small price to pay
  66. for the vast speedup.
  67. First, all of the possible prefix codes are counted, and reachable
  68. intermediate states are noted by a non-zero count in a saved-results array.
  69. Second, the intermediate states that lead to (root + 1) bit or longer codes
  70. are used to look at all sub-codes from those junctures for their inflate
  71. memory usage. (The amount of memory used is not affected by the number of
  72. codes of root bits or less in length.) Third, the visited states in the
  73. construction of those sub-codes and the associated calculation of the table
  74. size is recalled in order to avoid recalculating from the same juncture.
  75. Beginning the code examination at (root + 1) bit codes, which is enabled by
  76. identifying the reachable nodes, accounts for about six of the orders of
  77. magnitude of improvement for the default arguments. About another four
  78. orders of magnitude come from not revisiting previous states. Out of
  79. approximately 2x10^16 possible prefix codes, only about 2x10^6 sub-codes
  80. need to be examined to cover all of the possible table memory usage cases
  81. for the default arguments of 286 symbols limited to 15-bit codes.
  82. Note that the uintmax_t type is used for counting. It is quite easy to
  83. exceed the capacity of an eight-byte integer with a large number of symbols
  84. and a large maximum code length, so multiple-precision arithmetic would need
  85. to replace the integer arithmetic in that case. This program will abort if
  86. an overflow occurs. The big_t type identifies where the counting takes
  87. place.
  88. The uintmax_t type is also used for calculating the number of possible codes
  89. remaining at the maximum length. This limits the maximum code length to the
  90. number of bits in a long long minus the number of bits needed to represent
  91. the symbols in a flat code. The code_t type identifies where the bit-pattern
  92. counting takes place.
  93. */
  94. #include <stdio.h>
  95. #include <stdlib.h>
  96. #include <string.h>
  97. #include <stdarg.h>
  98. #include <stdint.h>
  99. #include <assert.h>
  100. #define local static
  101. // Special data types.
  102. typedef uintmax_t big_t; // type for code counting
  103. #define PRIbig "ju" // printf format for big_t
  104. typedef uintmax_t code_t; // type for bit pattern counting
  105. struct tab { // type for been-here check
  106. size_t len; // allocated length of bit vector in octets
  107. char *vec; // allocated bit vector
  108. };
  109. /* The array for saving results, num[], is indexed with this triplet:
  110. syms: number of symbols remaining to code
  111. left: number of available bit patterns at length len
  112. len: number of bits in the codes currently being assigned
  113. Those indices are constrained thusly when saving results:
  114. syms: 3..totsym (totsym == total symbols to code)
  115. left: 2..syms - 1, but only the evens (so syms == 8 -> 2, 4, 6)
  116. len: 1..max - 1 (max == maximum code length in bits)
  117. syms == 2 is not saved since that immediately leads to a single code. left
  118. must be even, since it represents the number of available bit patterns at
  119. the current length, which is double the number at the previous length. left
  120. ends at syms-1 since left == syms immediately results in a single code.
  121. (left > sym is not allowed since that would result in an incomplete code.)
  122. len is less than max, since the code completes immediately when len == max.
  123. The offset into the array is calculated for the three indices with the first
  124. one (syms) being outermost, and the last one (len) being innermost. We build
  125. the array with length max-1 lists for the len index, with syms-3 of those
  126. for each symbol. There are totsym-2 of those, with each one varying in
  127. length as a function of sym. See the calculation of index in map() for the
  128. index, and the calculation of size in main() for the size of the array.
  129. For the deflate example of 286 symbols limited to 15-bit codes, the array
  130. has 284,284 entries, taking up 2.17 MB for an 8-byte big_t. More than half
  131. of the space allocated for saved results is actually used -- not all
  132. possible triplets are reached in the generation of valid prefix codes.
  133. */
  134. /* The array for tracking visited states, done[], is itself indexed identically
  135. to the num[] array as described above for the (syms, left, len) triplet.
  136. Each element in the array is further indexed by the (mem, rem) doublet,
  137. where mem is the amount of inflate table space used so far, and rem is the
  138. remaining unused entries in the current inflate sub-table. Each indexed
  139. element is simply one bit indicating whether the state has been visited or
  140. not. Since the ranges for mem and rem are not known a priori, each bit
  141. vector is of a variable size, and grows as needed to accommodate the visited
  142. states. mem and rem are used to calculate a single index in a triangular
  143. array. Since the range of mem is expected in the default case to be about
  144. ten times larger than the range of rem, the array is skewed to reduce the
  145. memory usage, with eight times the range for mem than for rem. See the
  146. calculations for offset and bit in been_here() for the details.
  147. For the deflate example of 286 symbols limited to 15-bit codes, the bit
  148. vectors grow to total 5.5 MB, in addition to the 4.3 MB done array itself.
  149. */
  150. // Type for a variable-length, allocated string.
  151. typedef struct {
  152. char *str; // pointer to allocated string
  153. size_t size; // size of allocation
  154. size_t len; // length of string, not including terminating zero
  155. } string_t;
  156. // Clear a string_t.
  157. local void string_clear(string_t *s) {
  158. s->str[0] = 0;
  159. s->len = 0;
  160. }
  161. // Initialize a string_t.
  162. local void string_init(string_t *s) {
  163. s->size = 16;
  164. s->str = malloc(s->size);
  165. assert(s->str != NULL && "out of memory");
  166. string_clear(s);
  167. }
  168. // Release the allocation of a string_t.
  169. local void string_free(string_t *s) {
  170. free(s->str);
  171. s->str = NULL;
  172. s->size = 0;
  173. s->len = 0;
  174. }
  175. // Save the results of printf with fmt and the subsequent argument list to s.
  176. // Each call appends to s. The allocated space for s is increased as needed.
  177. local void string_printf(string_t *s, char *fmt, ...) {
  178. va_list ap;
  179. va_start(ap, fmt);
  180. size_t len = s->len;
  181. int ret = vsnprintf(s->str + len, s->size - len, fmt, ap);
  182. assert(ret >= 0 && "out of memory");
  183. s->len += ret;
  184. if (s->size < s->len + 1) {
  185. do {
  186. s->size <<= 1;
  187. assert(s->size != 0 && "overflow");
  188. } while (s->size < s->len + 1);
  189. s->str = realloc(s->str, s->size);
  190. assert(s->str != NULL && "out of memory");
  191. vsnprintf(s->str + len, s->size - len, fmt, ap);
  192. }
  193. va_end(ap);
  194. }
  195. // Globals to avoid propagating constants or constant pointers recursively.
  196. struct {
  197. int max; // maximum allowed bit length for the codes
  198. int root; // size of base code table in bits
  199. int large; // largest code table so far
  200. size_t size; // number of elements in num and done
  201. big_t tot; // total number of codes with maximum tables size
  202. string_t out; // display of subcodes for maximum tables size
  203. int *code; // number of symbols assigned to each bit length
  204. big_t *num; // saved results array for code counting
  205. struct tab *done; // states already evaluated array
  206. } g;
  207. // Index function for num[] and done[].
  208. local inline size_t map(int syms, int left, int len) {
  209. return ((size_t)((syms - 1) >> 1) * ((syms - 2) >> 1) +
  210. (left >> 1) - 1) * (g.max - 1) +
  211. len - 1;
  212. }
  213. // Free allocated space in globals.
  214. local void cleanup(void) {
  215. if (g.done != NULL) {
  216. for (size_t n = 0; n < g.size; n++)
  217. if (g.done[n].len)
  218. free(g.done[n].vec);
  219. g.size = 0;
  220. free(g.done); g.done = NULL;
  221. }
  222. free(g.num); g.num = NULL;
  223. free(g.code); g.code = NULL;
  224. string_free(&g.out);
  225. }
  226. // Return the number of possible prefix codes using bit patterns of lengths len
  227. // through max inclusive, coding syms symbols, with left bit patterns of length
  228. // len unused -- return -1 if there is an overflow in the counting. Keep a
  229. // record of previous results in num to prevent repeating the same calculation.
  230. local big_t count(int syms, int left, int len) {
  231. // see if only one possible code
  232. if (syms == left)
  233. return 1;
  234. // note and verify the expected state
  235. assert(syms > left && left > 0 && len < g.max);
  236. // see if we've done this one already
  237. size_t index = map(syms, left, len);
  238. big_t got = g.num[index];
  239. if (got)
  240. return got; // we have -- return the saved result
  241. // we need to use at least this many bit patterns so that the code won't be
  242. // incomplete at the next length (more bit patterns than symbols)
  243. int least = (left << 1) - syms;
  244. if (least < 0)
  245. least = 0;
  246. // we can use at most this many bit patterns, lest there not be enough
  247. // available for the remaining symbols at the maximum length (if there were
  248. // no limit to the code length, this would become: most = left - 1)
  249. int most = (((code_t)left << (g.max - len)) - syms) /
  250. (((code_t)1 << (g.max - len)) - 1);
  251. // count all possible codes from this juncture and add them up
  252. big_t sum = 0;
  253. for (int use = least; use <= most; use++) {
  254. got = count(syms - use, (left - use) << 1, len + 1);
  255. sum += got;
  256. if (got == (big_t)-1 || sum < got) // overflow
  257. return (big_t)-1;
  258. }
  259. // verify that all recursive calls are productive
  260. assert(sum != 0);
  261. // save the result and return it
  262. g.num[index] = sum;
  263. return sum;
  264. }
  265. // Return true if we've been here before, set to true if not. Set a bit in a
  266. // bit vector to indicate visiting this state. Each (syms,len,left) state has a
  267. // variable size bit vector indexed by (mem,rem). The bit vector is lengthened
  268. // as needed to allow setting the (mem,rem) bit.
  269. local int been_here(int syms, int left, int len, int mem, int rem) {
  270. // point to vector for (syms,left,len), bit in vector for (mem,rem)
  271. size_t index = map(syms, left, len);
  272. mem -= 1 << g.root; // mem always includes the root table
  273. mem >>= 1; // mem and rem are always even
  274. rem >>= 1;
  275. size_t offset = (mem >> 3) + rem;
  276. offset = ((offset * (offset + 1)) >> 1) + rem;
  277. int bit = 1 << (mem & 7);
  278. // see if we've been here
  279. size_t length = g.done[index].len;
  280. if (offset < length && (g.done[index].vec[offset] & bit) != 0)
  281. return 1; // done this!
  282. // we haven't been here before -- set the bit to show we have now
  283. // see if we need to lengthen the vector in order to set the bit
  284. if (length <= offset) {
  285. // if we have one already, enlarge it, zero out the appended space
  286. char *vector;
  287. if (length) {
  288. do {
  289. length <<= 1;
  290. } while (length <= offset);
  291. vector = realloc(g.done[index].vec, length);
  292. assert(vector != NULL && "out of memory");
  293. memset(vector + g.done[index].len, 0, length - g.done[index].len);
  294. }
  295. // otherwise we need to make a new vector and zero it out
  296. else {
  297. length = 16;
  298. while (length <= offset)
  299. length <<= 1;
  300. vector = calloc(length, 1);
  301. assert(vector != NULL && "out of memory");
  302. }
  303. // install the new vector
  304. g.done[index].len = length;
  305. g.done[index].vec = vector;
  306. }
  307. // set the bit
  308. g.done[index].vec[offset] |= bit;
  309. return 0;
  310. }
  311. // Examine all possible codes from the given node (syms, len, left). Compute
  312. // the amount of memory required to build inflate's decoding tables, where the
  313. // number of code structures used so far is mem, and the number remaining in
  314. // the current sub-table is rem.
  315. local void examine(int syms, int left, int len, int mem, int rem) {
  316. // see if we have a complete code
  317. if (syms == left) {
  318. // set the last code entry
  319. g.code[len] = left;
  320. // complete computation of memory used by this code
  321. while (rem < left) {
  322. left -= rem;
  323. rem = 1 << (len - g.root);
  324. mem += rem;
  325. }
  326. assert(rem == left);
  327. // if this is at the maximum, show the sub-code
  328. if (mem >= g.large) {
  329. // if this is a new maximum, update the maximum and clear out the
  330. // printed sub-codes from the previous maximum
  331. if (mem > g.large) {
  332. g.large = mem;
  333. string_clear(&g.out);
  334. }
  335. // compute the starting state for this sub-code
  336. syms = 0;
  337. left = 1 << g.max;
  338. for (int bits = g.max; bits > g.root; bits--) {
  339. syms += g.code[bits];
  340. left -= g.code[bits];
  341. assert((left & 1) == 0);
  342. left >>= 1;
  343. }
  344. // print the starting state and the resulting sub-code to g.out
  345. string_printf(&g.out, "<%u, %u, %u>:",
  346. syms, g.root + 1, ((1 << g.root) - left) << 1);
  347. for (int bits = g.root + 1; bits <= g.max; bits++)
  348. if (g.code[bits])
  349. string_printf(&g.out, " %d[%d]", g.code[bits], bits);
  350. string_printf(&g.out, "\n");
  351. }
  352. // remove entries as we drop back down in the recursion
  353. g.code[len] = 0;
  354. return;
  355. }
  356. // prune the tree if we can
  357. if (been_here(syms, left, len, mem, rem))
  358. return;
  359. // we need to use at least this many bit patterns so that the code won't be
  360. // incomplete at the next length (more bit patterns than symbols)
  361. int least = (left << 1) - syms;
  362. if (least < 0)
  363. least = 0;
  364. // we can use at most this many bit patterns, lest there not be enough
  365. // available for the remaining symbols at the maximum length (if there were
  366. // no limit to the code length, this would become: most = left - 1)
  367. int most = (((code_t)left << (g.max - len)) - syms) /
  368. (((code_t)1 << (g.max - len)) - 1);
  369. // occupy least table spaces, creating new sub-tables as needed
  370. int use = least;
  371. while (rem < use) {
  372. use -= rem;
  373. rem = 1 << (len - g.root);
  374. mem += rem;
  375. }
  376. rem -= use;
  377. // examine codes from here, updating table space as we go
  378. for (use = least; use <= most; use++) {
  379. g.code[len] = use;
  380. examine(syms - use, (left - use) << 1, len + 1,
  381. mem + (rem ? 1 << (len - g.root) : 0), rem << 1);
  382. if (rem == 0) {
  383. rem = 1 << (len - g.root);
  384. mem += rem;
  385. }
  386. rem--;
  387. }
  388. // remove entries as we drop back down in the recursion
  389. g.code[len] = 0;
  390. }
  391. // Look at all sub-codes starting with root + 1 bits. Look at only the valid
  392. // intermediate code states (syms, left, len). For each completed code,
  393. // calculate the amount of memory required by inflate to build the decoding
  394. // tables. Find the maximum amount of memory required and show the codes that
  395. // require that maximum.
  396. local void enough(int syms) {
  397. // clear code
  398. for (int n = 0; n <= g.max; n++)
  399. g.code[n] = 0;
  400. // look at all (root + 1) bit and longer codes
  401. string_clear(&g.out); // empty saved results
  402. g.large = 1 << g.root; // base table
  403. if (g.root < g.max) // otherwise, there's only a base table
  404. for (int n = 3; n <= syms; n++)
  405. for (int left = 2; left < n; left += 2) {
  406. // look at all reachable (root + 1) bit nodes, and the
  407. // resulting codes (complete at root + 2 or more)
  408. size_t index = map(n, left, g.root + 1);
  409. if (g.root + 1 < g.max && g.num[index]) // reachable node
  410. examine(n, left, g.root + 1, 1 << g.root, 0);
  411. // also look at root bit codes with completions at root + 1
  412. // bits (not saved in num, since complete), just in case
  413. if (g.num[index - 1] && n <= left << 1)
  414. examine((n - left) << 1, (n - left) << 1, g.root + 1,
  415. 1 << g.root, 0);
  416. }
  417. // done
  418. printf("maximum of %d table entries for root = %d\n", g.large, g.root);
  419. fputs(g.out.str, stdout);
  420. }
  421. // Examine and show the total number of possible prefix codes for a given
  422. // maximum number of symbols, initial root table size, and maximum code length
  423. // in bits -- those are the command arguments in that order. The default values
  424. // are 286, 9, and 15 respectively, for the deflate literal/length code. The
  425. // possible codes are counted for each number of coded symbols from two to the
  426. // maximum. The counts for each of those and the total number of codes are
  427. // shown. The maximum number of inflate table entries is then calculated across
  428. // all possible codes. Each new maximum number of table entries and the
  429. // associated sub-code (starting at root + 1 == 10 bits) is shown.
  430. //
  431. // To count and examine prefix codes that are not length-limited, provide a
  432. // maximum length equal to the number of symbols minus one.
  433. //
  434. // For the deflate literal/length code, use "enough". For the deflate distance
  435. // code, use "enough 30 6".
  436. int main(int argc, char **argv) {
  437. // set up globals for cleanup()
  438. g.code = NULL;
  439. g.num = NULL;
  440. g.done = NULL;
  441. string_init(&g.out);
  442. // get arguments -- default to the deflate literal/length code
  443. int syms = 286;
  444. g.root = 9;
  445. g.max = 15;
  446. if (argc > 1) {
  447. syms = atoi(argv[1]);
  448. if (argc > 2) {
  449. g.root = atoi(argv[2]);
  450. if (argc > 3)
  451. g.max = atoi(argv[3]);
  452. }
  453. }
  454. if (argc > 4 || syms < 2 || g.root < 1 || g.max < 1) {
  455. fputs("invalid arguments, need: [sym >= 2 [root >= 1 [max >= 1]]]\n",
  456. stderr);
  457. return 1;
  458. }
  459. // if not restricting the code length, the longest is syms - 1
  460. if (g.max > syms - 1)
  461. g.max = syms - 1;
  462. // determine the number of bits in a code_t
  463. int bits = 0;
  464. for (code_t word = 1; word; word <<= 1)
  465. bits++;
  466. // make sure that the calculation of most will not overflow
  467. if (g.max > bits || (code_t)(syms - 2) >= ((code_t)-1 >> (g.max - 1))) {
  468. fputs("abort: code length too long for internal types\n", stderr);
  469. return 1;
  470. }
  471. // reject impossible code requests
  472. if ((code_t)(syms - 1) > ((code_t)1 << g.max) - 1) {
  473. fprintf(stderr, "%d symbols cannot be coded in %d bits\n",
  474. syms, g.max);
  475. return 1;
  476. }
  477. // allocate code vector
  478. g.code = calloc(g.max + 1, sizeof(int));
  479. assert(g.code != NULL && "out of memory");
  480. // determine size of saved results array, checking for overflows,
  481. // allocate and clear the array (set all to zero with calloc())
  482. if (syms == 2) // iff max == 1
  483. g.num = NULL; // won't be saving any results
  484. else {
  485. g.size = syms >> 1;
  486. int n = (syms - 1) >> 1;
  487. assert(g.size <= (size_t)-1 / n && "overflow");
  488. g.size *= n;
  489. n = g.max - 1;
  490. assert(g.size <= (size_t)-1 / n && "overflow");
  491. g.size *= n;
  492. g.num = calloc(g.size, sizeof(big_t));
  493. assert(g.num != NULL && "out of memory");
  494. }
  495. // count possible codes for all numbers of symbols, add up counts
  496. big_t sum = 0;
  497. for (int n = 2; n <= syms; n++) {
  498. big_t got = count(n, 2, 1);
  499. sum += got;
  500. assert(got != (big_t)-1 && sum >= got && "overflow");
  501. }
  502. printf("%"PRIbig" total codes for 2 to %d symbols", sum, syms);
  503. if (g.max < syms - 1)
  504. printf(" (%d-bit length limit)\n", g.max);
  505. else
  506. puts(" (no length limit)");
  507. // allocate and clear done array for been_here()
  508. if (syms == 2)
  509. g.done = NULL;
  510. else {
  511. g.done = calloc(g.size, sizeof(struct tab));
  512. assert(g.done != NULL && "out of memory");
  513. }
  514. // find and show maximum inflate table usage
  515. if (g.root > g.max) // reduce root to max length
  516. g.root = g.max;
  517. if ((code_t)syms < ((code_t)1 << (g.root + 1)))
  518. enough(syms);
  519. else
  520. fputs("cannot handle minimum code lengths > root", stderr);
  521. // done
  522. cleanup();
  523. return 0;
  524. }