116dce513Schristos /* enough.c -- determine the maximum size of inflate's Huffman code tables over
2*e992f068Schristos * all possible valid and complete prefix codes, subject to a length limit.
3*e992f068Schristos * Copyright (C) 2007, 2008, 2012, 2018 Mark Adler
4*e992f068Schristos * Version 1.5 5 August 2018 Mark Adler
516dce513Schristos */
616dce513Schristos
716dce513Schristos /* Version history:
816dce513Schristos 1.0 3 Jan 2007 First version (derived from codecount.c version 1.4)
916dce513Schristos 1.1 4 Jan 2007 Use faster incremental table usage computation
1016dce513Schristos Prune examine() search on previously visited states
1116dce513Schristos 1.2 5 Jan 2007 Comments clean up
1216dce513Schristos As inflate does, decrease root for short codes
1316dce513Schristos Refuse cases where inflate would increase root
1416dce513Schristos 1.3 17 Feb 2008 Add argument for initial root table size
1516dce513Schristos Fix bug for initial root table size == max - 1
1616dce513Schristos Use a macro to compute the history index
1716dce513Schristos 1.4 18 Aug 2012 Avoid shifts more than bits in type (caused endless loop!)
1816dce513Schristos Clean up comparisons of different types
1916dce513Schristos Clean up code indentation
20*e992f068Schristos 1.5 5 Aug 2018 Clean up code style, formatting, and comments
21*e992f068Schristos Show all the codes for the maximum, and only the maximum
2216dce513Schristos */
2316dce513Schristos
2416dce513Schristos /*
25*e992f068Schristos Examine all possible prefix codes for a given number of symbols and a
26*e992f068Schristos maximum code length in bits to determine the maximum table size for zlib's
27*e992f068Schristos inflate. Only complete prefix codes are counted.
2816dce513Schristos
2916dce513Schristos Two codes are considered distinct if the vectors of the number of codes per
3016dce513Schristos length are not identical. So permutations of the symbol assignments result
3116dce513Schristos in the same code for the counting, as do permutations of the assignments of
3216dce513Schristos the bit values to the codes (i.e. only canonical codes are counted).
3316dce513Schristos
3416dce513Schristos We build a code from shorter to longer lengths, determining how many symbols
3516dce513Schristos are coded at each length. At each step, we have how many symbols remain to
3616dce513Schristos be coded, what the last code length used was, and how many bit patterns of
3716dce513Schristos that length remain unused. Then we add one to the code length and double the
3816dce513Schristos number of unused patterns to graduate to the next code length. We then
3916dce513Schristos assign all portions of the remaining symbols to that code length that
4016dce513Schristos preserve the properties of a correct and eventually complete code. Those
4116dce513Schristos properties are: we cannot use more bit patterns than are available; and when
42*e992f068Schristos all the symbols are used, there are exactly zero possible bit patterns left
43*e992f068Schristos unused.
4416dce513Schristos
4516dce513Schristos The inflate Huffman decoding algorithm uses two-level lookup tables for
4616dce513Schristos speed. There is a single first-level table to decode codes up to root bits
47*e992f068Schristos in length (root == 9 for literal/length codes and root == 6 for distance
48*e992f068Schristos codes, in the current inflate implementation). The base table has 1 << root
49*e992f068Schristos entries and is indexed by the next root bits of input. Codes shorter than
50*e992f068Schristos root bits have replicated table entries, so that the correct entry is
51*e992f068Schristos pointed to regardless of the bits that follow the short code. If the code is
52*e992f068Schristos longer than root bits, then the table entry points to a second-level table.
53*e992f068Schristos The size of that table is determined by the longest code with that root-bit
54*e992f068Schristos prefix. If that longest code has length len, then the table has size 1 <<
55*e992f068Schristos (len - root), to index the remaining bits in that set of codes. Each
56*e992f068Schristos subsequent root-bit prefix then has its own sub-table. The total number of
57*e992f068Schristos table entries required by the code is calculated incrementally as the number
58*e992f068Schristos of codes at each bit length is populated. When all of the codes are shorter
59*e992f068Schristos than root bits, then root is reduced to the longest code length, resulting
60*e992f068Schristos in a single, smaller, one-level table.
6116dce513Schristos
6216dce513Schristos The inflate algorithm also provides for small values of root (relative to
6316dce513Schristos the log2 of the number of symbols), where the shortest code has more bits
6416dce513Schristos than root. In that case, root is increased to the length of the shortest
6516dce513Schristos code. This program, by design, does not handle that case, so it is verified
66*e992f068Schristos that the number of symbols is less than 1 << (root + 1).
6716dce513Schristos
6816dce513Schristos In order to speed up the examination (by about ten orders of magnitude for
6916dce513Schristos the default arguments), the intermediate states in the build-up of a code
7016dce513Schristos are remembered and previously visited branches are pruned. The memory
7116dce513Schristos required for this will increase rapidly with the total number of symbols and
7216dce513Schristos the maximum code length in bits. However this is a very small price to pay
7316dce513Schristos for the vast speedup.
7416dce513Schristos
75*e992f068Schristos First, all of the possible prefix codes are counted, and reachable
7616dce513Schristos intermediate states are noted by a non-zero count in a saved-results array.
7716dce513Schristos Second, the intermediate states that lead to (root + 1) bit or longer codes
7816dce513Schristos are used to look at all sub-codes from those junctures for their inflate
7916dce513Schristos memory usage. (The amount of memory used is not affected by the number of
8016dce513Schristos codes of root bits or less in length.) Third, the visited states in the
8116dce513Schristos construction of those sub-codes and the associated calculation of the table
8216dce513Schristos size is recalled in order to avoid recalculating from the same juncture.
8316dce513Schristos Beginning the code examination at (root + 1) bit codes, which is enabled by
8416dce513Schristos identifying the reachable nodes, accounts for about six of the orders of
8516dce513Schristos magnitude of improvement for the default arguments. About another four
8616dce513Schristos orders of magnitude come from not revisiting previous states. Out of
87*e992f068Schristos approximately 2x10^16 possible prefix codes, only about 2x10^6 sub-codes
8816dce513Schristos need to be examined to cover all of the possible table memory usage cases
8916dce513Schristos for the default arguments of 286 symbols limited to 15-bit codes.
9016dce513Schristos
91*e992f068Schristos Note that the uintmax_t type is used for counting. It is quite easy to
92*e992f068Schristos exceed the capacity of an eight-byte integer with a large number of symbols
93*e992f068Schristos and a large maximum code length, so multiple-precision arithmetic would need
94*e992f068Schristos to replace the integer arithmetic in that case. This program will abort if
95*e992f068Schristos an overflow occurs. The big_t type identifies where the counting takes
96*e992f068Schristos place.
9716dce513Schristos
98*e992f068Schristos The uintmax_t type is also used for calculating the number of possible codes
99*e992f068Schristos remaining at the maximum length. This limits the maximum code length to the
100*e992f068Schristos number of bits in a long long minus the number of bits needed to represent
101*e992f068Schristos the symbols in a flat code. The code_t type identifies where the bit-pattern
102*e992f068Schristos counting takes place.
10316dce513Schristos */
10416dce513Schristos
10516dce513Schristos #include <stdio.h>
10616dce513Schristos #include <stdlib.h>
10716dce513Schristos #include <string.h>
108*e992f068Schristos #include <stdarg.h>
109*e992f068Schristos #include <stdint.h>
11016dce513Schristos #include <assert.h>
11116dce513Schristos
11216dce513Schristos #define local static
11316dce513Schristos
114*e992f068Schristos // Special data types.
115*e992f068Schristos typedef uintmax_t big_t; // type for code counting
116*e992f068Schristos #define PRIbig "ju" // printf format for big_t
117*e992f068Schristos typedef uintmax_t code_t; // type for bit pattern counting
118*e992f068Schristos struct tab { // type for been-here check
119*e992f068Schristos size_t len; // allocated length of bit vector in octets
120*e992f068Schristos char *vec; // allocated bit vector
12116dce513Schristos };
12216dce513Schristos
12316dce513Schristos /* The array for saving results, num[], is indexed with this triplet:
12416dce513Schristos
12516dce513Schristos syms: number of symbols remaining to code
12616dce513Schristos left: number of available bit patterns at length len
12716dce513Schristos len: number of bits in the codes currently being assigned
12816dce513Schristos
12916dce513Schristos Those indices are constrained thusly when saving results:
13016dce513Schristos
13116dce513Schristos syms: 3..totsym (totsym == total symbols to code)
13216dce513Schristos left: 2..syms - 1, but only the evens (so syms == 8 -> 2, 4, 6)
13316dce513Schristos len: 1..max - 1 (max == maximum code length in bits)
13416dce513Schristos
13516dce513Schristos syms == 2 is not saved since that immediately leads to a single code. left
13616dce513Schristos must be even, since it represents the number of available bit patterns at
137*e992f068Schristos the current length, which is double the number at the previous length. left
138*e992f068Schristos ends at syms-1 since left == syms immediately results in a single code.
13916dce513Schristos (left > sym is not allowed since that would result in an incomplete code.)
14016dce513Schristos len is less than max, since the code completes immediately when len == max.
14116dce513Schristos
142*e992f068Schristos The offset into the array is calculated for the three indices with the first
143*e992f068Schristos one (syms) being outermost, and the last one (len) being innermost. We build
144*e992f068Schristos the array with length max-1 lists for the len index, with syms-3 of those
145*e992f068Schristos for each symbol. There are totsym-2 of those, with each one varying in
146*e992f068Schristos length as a function of sym. See the calculation of index in map() for the
147*e992f068Schristos index, and the calculation of size in main() for the size of the array.
14816dce513Schristos
14916dce513Schristos For the deflate example of 286 symbols limited to 15-bit codes, the array
150*e992f068Schristos has 284,284 entries, taking up 2.17 MB for an 8-byte big_t. More than half
151*e992f068Schristos of the space allocated for saved results is actually used -- not all
152*e992f068Schristos possible triplets are reached in the generation of valid prefix codes.
15316dce513Schristos */
15416dce513Schristos
15516dce513Schristos /* The array for tracking visited states, done[], is itself indexed identically
15616dce513Schristos to the num[] array as described above for the (syms, left, len) triplet.
15716dce513Schristos Each element in the array is further indexed by the (mem, rem) doublet,
15816dce513Schristos where mem is the amount of inflate table space used so far, and rem is the
15916dce513Schristos remaining unused entries in the current inflate sub-table. Each indexed
16016dce513Schristos element is simply one bit indicating whether the state has been visited or
16116dce513Schristos not. Since the ranges for mem and rem are not known a priori, each bit
16216dce513Schristos vector is of a variable size, and grows as needed to accommodate the visited
16316dce513Schristos states. mem and rem are used to calculate a single index in a triangular
16416dce513Schristos array. Since the range of mem is expected in the default case to be about
16516dce513Schristos ten times larger than the range of rem, the array is skewed to reduce the
16616dce513Schristos memory usage, with eight times the range for mem than for rem. See the
167*e992f068Schristos calculations for offset and bit in been_here() for the details.
16816dce513Schristos
16916dce513Schristos For the deflate example of 286 symbols limited to 15-bit codes, the bit
170*e992f068Schristos vectors grow to total 5.5 MB, in addition to the 4.3 MB done array itself.
17116dce513Schristos */
17216dce513Schristos
173*e992f068Schristos // Type for a variable-length, allocated string.
174*e992f068Schristos typedef struct {
175*e992f068Schristos char *str; // pointer to allocated string
176*e992f068Schristos size_t size; // size of allocation
177*e992f068Schristos size_t len; // length of string, not including terminating zero
178*e992f068Schristos } string_t;
17916dce513Schristos
180*e992f068Schristos // Clear a string_t.
string_clear(string_t * s)181*e992f068Schristos local void string_clear(string_t *s) {
182*e992f068Schristos s->str[0] = 0;
183*e992f068Schristos s->len = 0;
18416dce513Schristos }
18516dce513Schristos
186*e992f068Schristos // Initialize a string_t.
string_init(string_t * s)187*e992f068Schristos local void string_init(string_t *s) {
188*e992f068Schristos s->size = 16;
189*e992f068Schristos s->str = malloc(s->size);
190*e992f068Schristos assert(s->str != NULL && "out of memory");
191*e992f068Schristos string_clear(s);
192*e992f068Schristos }
19316dce513Schristos
194*e992f068Schristos // Release the allocation of a string_t.
string_free(string_t * s)195*e992f068Schristos local void string_free(string_t *s) {
196*e992f068Schristos free(s->str);
197*e992f068Schristos s->str = NULL;
198*e992f068Schristos s->size = 0;
199*e992f068Schristos s->len = 0;
200*e992f068Schristos }
201*e992f068Schristos
202*e992f068Schristos // Save the results of printf with fmt and the subsequent argument list to s.
203*e992f068Schristos // Each call appends to s. The allocated space for s is increased as needed.
string_printf(string_t * s,char * fmt,...)204*e992f068Schristos local void string_printf(string_t *s, char *fmt, ...) {
205*e992f068Schristos va_list ap;
206*e992f068Schristos va_start(ap, fmt);
207*e992f068Schristos size_t len = s->len;
208*e992f068Schristos int ret = vsnprintf(s->str + len, s->size - len, fmt, ap);
209*e992f068Schristos assert(ret >= 0 && "out of memory");
210*e992f068Schristos s->len += ret;
211*e992f068Schristos if (s->size < s->len + 1) {
212*e992f068Schristos do {
213*e992f068Schristos s->size <<= 1;
214*e992f068Schristos assert(s->size != 0 && "overflow");
215*e992f068Schristos } while (s->size < s->len + 1);
216*e992f068Schristos s->str = realloc(s->str, s->size);
217*e992f068Schristos assert(s->str != NULL && "out of memory");
218*e992f068Schristos vsnprintf(s->str + len, s->size - len, fmt, ap);
219*e992f068Schristos }
220*e992f068Schristos va_end(ap);
221*e992f068Schristos }
222*e992f068Schristos
223*e992f068Schristos // Globals to avoid propagating constants or constant pointers recursively.
224*e992f068Schristos struct {
225*e992f068Schristos int max; // maximum allowed bit length for the codes
226*e992f068Schristos int root; // size of base code table in bits
227*e992f068Schristos int large; // largest code table so far
228*e992f068Schristos size_t size; // number of elements in num and done
229*e992f068Schristos big_t tot; // total number of codes with maximum tables size
230*e992f068Schristos string_t out; // display of subcodes for maximum tables size
231*e992f068Schristos int *code; // number of symbols assigned to each bit length
232*e992f068Schristos big_t *num; // saved results array for code counting
233*e992f068Schristos struct tab *done; // states already evaluated array
234*e992f068Schristos } g;
235*e992f068Schristos
236*e992f068Schristos // Index function for num[] and done[].
map(int syms,int left,int len)237*e992f068Schristos local inline size_t map(int syms, int left, int len) {
238*e992f068Schristos return ((size_t)((syms - 1) >> 1) * ((syms - 2) >> 1) +
239*e992f068Schristos (left >> 1) - 1) * (g.max - 1) +
240*e992f068Schristos len - 1;
241*e992f068Schristos }
242*e992f068Schristos
243*e992f068Schristos // Free allocated space in globals.
cleanup(void)244*e992f068Schristos local void cleanup(void) {
245*e992f068Schristos if (g.done != NULL) {
246*e992f068Schristos for (size_t n = 0; n < g.size; n++)
247*e992f068Schristos if (g.done[n].len)
248*e992f068Schristos free(g.done[n].vec);
249*e992f068Schristos g.size = 0;
250*e992f068Schristos free(g.done); g.done = NULL;
251*e992f068Schristos }
252*e992f068Schristos free(g.num); g.num = NULL;
253*e992f068Schristos free(g.code); g.code = NULL;
254*e992f068Schristos string_free(&g.out);
255*e992f068Schristos }
256*e992f068Schristos
257*e992f068Schristos // Return the number of possible prefix codes using bit patterns of lengths len
258*e992f068Schristos // through max inclusive, coding syms symbols, with left bit patterns of length
259*e992f068Schristos // len unused -- return -1 if there is an overflow in the counting. Keep a
260*e992f068Schristos // record of previous results in num to prevent repeating the same calculation.
count(int syms,int left,int len)261*e992f068Schristos local big_t count(int syms, int left, int len) {
262*e992f068Schristos // see if only one possible code
26316dce513Schristos if (syms == left)
26416dce513Schristos return 1;
26516dce513Schristos
266*e992f068Schristos // note and verify the expected state
267*e992f068Schristos assert(syms > left && left > 0 && len < g.max);
26816dce513Schristos
269*e992f068Schristos // see if we've done this one already
270*e992f068Schristos size_t index = map(syms, left, len);
271*e992f068Schristos big_t got = g.num[index];
27216dce513Schristos if (got)
273*e992f068Schristos return got; // we have -- return the saved result
27416dce513Schristos
275*e992f068Schristos // we need to use at least this many bit patterns so that the code won't be
276*e992f068Schristos // incomplete at the next length (more bit patterns than symbols)
277*e992f068Schristos int least = (left << 1) - syms;
27816dce513Schristos if (least < 0)
27916dce513Schristos least = 0;
28016dce513Schristos
281*e992f068Schristos // we can use at most this many bit patterns, lest there not be enough
282*e992f068Schristos // available for the remaining symbols at the maximum length (if there were
283*e992f068Schristos // no limit to the code length, this would become: most = left - 1)
284*e992f068Schristos int most = (((code_t)left << (g.max - len)) - syms) /
285*e992f068Schristos (((code_t)1 << (g.max - len)) - 1);
28616dce513Schristos
287*e992f068Schristos // count all possible codes from this juncture and add them up
288*e992f068Schristos big_t sum = 0;
289*e992f068Schristos for (int use = least; use <= most; use++) {
290*e992f068Schristos got = count(syms - use, (left - use) << 1, len + 1);
29116dce513Schristos sum += got;
292*e992f068Schristos if (got == (big_t)-1 || sum < got) // overflow
293*e992f068Schristos return (big_t)-1;
29416dce513Schristos }
29516dce513Schristos
296*e992f068Schristos // verify that all recursive calls are productive
29716dce513Schristos assert(sum != 0);
29816dce513Schristos
299*e992f068Schristos // save the result and return it
300*e992f068Schristos g.num[index] = sum;
30116dce513Schristos return sum;
30216dce513Schristos }
30316dce513Schristos
304*e992f068Schristos // Return true if we've been here before, set to true if not. Set a bit in a
305*e992f068Schristos // bit vector to indicate visiting this state. Each (syms,len,left) state has a
306*e992f068Schristos // variable size bit vector indexed by (mem,rem). The bit vector is lengthened
307*e992f068Schristos // as needed to allow setting the (mem,rem) bit.
been_here(int syms,int left,int len,int mem,int rem)308*e992f068Schristos local int been_here(int syms, int left, int len, int mem, int rem) {
309*e992f068Schristos // point to vector for (syms,left,len), bit in vector for (mem,rem)
310*e992f068Schristos size_t index = map(syms, left, len);
311*e992f068Schristos mem -= 1 << g.root; // mem always includes the root table
312*e992f068Schristos mem >>= 1; // mem and rem are always even
313*e992f068Schristos rem >>= 1;
314*e992f068Schristos size_t offset = (mem >> 3) + rem;
31516dce513Schristos offset = ((offset * (offset + 1)) >> 1) + rem;
316*e992f068Schristos int bit = 1 << (mem & 7);
31716dce513Schristos
318*e992f068Schristos // see if we've been here
319*e992f068Schristos size_t length = g.done[index].len;
320*e992f068Schristos if (offset < length && (g.done[index].vec[offset] & bit) != 0)
321*e992f068Schristos return 1; // done this!
32216dce513Schristos
323*e992f068Schristos // we haven't been here before -- set the bit to show we have now
32416dce513Schristos
325*e992f068Schristos // see if we need to lengthen the vector in order to set the bit
32616dce513Schristos if (length <= offset) {
327*e992f068Schristos // if we have one already, enlarge it, zero out the appended space
328*e992f068Schristos char *vector;
32916dce513Schristos if (length) {
33016dce513Schristos do {
33116dce513Schristos length <<= 1;
33216dce513Schristos } while (length <= offset);
333*e992f068Schristos vector = realloc(g.done[index].vec, length);
334*e992f068Schristos assert(vector != NULL && "out of memory");
335*e992f068Schristos memset(vector + g.done[index].len, 0, length - g.done[index].len);
33616dce513Schristos }
33716dce513Schristos
338*e992f068Schristos // otherwise we need to make a new vector and zero it out
33916dce513Schristos else {
340*e992f068Schristos length = 16;
34116dce513Schristos while (length <= offset)
34216dce513Schristos length <<= 1;
343*e992f068Schristos vector = calloc(length, 1);
344*e992f068Schristos assert(vector != NULL && "out of memory");
34516dce513Schristos }
34616dce513Schristos
347*e992f068Schristos // install the new vector
348*e992f068Schristos g.done[index].len = length;
349*e992f068Schristos g.done[index].vec = vector;
35016dce513Schristos }
35116dce513Schristos
352*e992f068Schristos // set the bit
353*e992f068Schristos g.done[index].vec[offset] |= bit;
35416dce513Schristos return 0;
35516dce513Schristos }
35616dce513Schristos
357*e992f068Schristos // Examine all possible codes from the given node (syms, len, left). Compute
358*e992f068Schristos // the amount of memory required to build inflate's decoding tables, where the
359*e992f068Schristos // number of code structures used so far is mem, and the number remaining in
360*e992f068Schristos // the current sub-table is rem.
examine(int syms,int left,int len,int mem,int rem)361*e992f068Schristos local void examine(int syms, int left, int len, int mem, int rem) {
362*e992f068Schristos // see if we have a complete code
36316dce513Schristos if (syms == left) {
364*e992f068Schristos // set the last code entry
365*e992f068Schristos g.code[len] = left;
36616dce513Schristos
367*e992f068Schristos // complete computation of memory used by this code
36816dce513Schristos while (rem < left) {
36916dce513Schristos left -= rem;
370*e992f068Schristos rem = 1 << (len - g.root);
37116dce513Schristos mem += rem;
37216dce513Schristos }
37316dce513Schristos assert(rem == left);
37416dce513Schristos
375*e992f068Schristos // if this is at the maximum, show the sub-code
376*e992f068Schristos if (mem >= g.large) {
377*e992f068Schristos // if this is a new maximum, update the maximum and clear out the
378*e992f068Schristos // printed sub-codes from the previous maximum
379*e992f068Schristos if (mem > g.large) {
380*e992f068Schristos g.large = mem;
381*e992f068Schristos string_clear(&g.out);
38216dce513Schristos }
38316dce513Schristos
384*e992f068Schristos // compute the starting state for this sub-code
385*e992f068Schristos syms = 0;
386*e992f068Schristos left = 1 << g.max;
387*e992f068Schristos for (int bits = g.max; bits > g.root; bits--) {
388*e992f068Schristos syms += g.code[bits];
389*e992f068Schristos left -= g.code[bits];
390*e992f068Schristos assert((left & 1) == 0);
391*e992f068Schristos left >>= 1;
392*e992f068Schristos }
393*e992f068Schristos
394*e992f068Schristos // print the starting state and the resulting sub-code to g.out
395*e992f068Schristos string_printf(&g.out, "<%u, %u, %u>:",
396*e992f068Schristos syms, g.root + 1, ((1 << g.root) - left) << 1);
397*e992f068Schristos for (int bits = g.root + 1; bits <= g.max; bits++)
398*e992f068Schristos if (g.code[bits])
399*e992f068Schristos string_printf(&g.out, " %d[%d]", g.code[bits], bits);
400*e992f068Schristos string_printf(&g.out, "\n");
401*e992f068Schristos }
402*e992f068Schristos
403*e992f068Schristos // remove entries as we drop back down in the recursion
404*e992f068Schristos g.code[len] = 0;
40516dce513Schristos return;
40616dce513Schristos }
40716dce513Schristos
408*e992f068Schristos // prune the tree if we can
409*e992f068Schristos if (been_here(syms, left, len, mem, rem))
41016dce513Schristos return;
41116dce513Schristos
412*e992f068Schristos // we need to use at least this many bit patterns so that the code won't be
413*e992f068Schristos // incomplete at the next length (more bit patterns than symbols)
414*e992f068Schristos int least = (left << 1) - syms;
41516dce513Schristos if (least < 0)
41616dce513Schristos least = 0;
41716dce513Schristos
418*e992f068Schristos // we can use at most this many bit patterns, lest there not be enough
419*e992f068Schristos // available for the remaining symbols at the maximum length (if there were
420*e992f068Schristos // no limit to the code length, this would become: most = left - 1)
421*e992f068Schristos int most = (((code_t)left << (g.max - len)) - syms) /
422*e992f068Schristos (((code_t)1 << (g.max - len)) - 1);
42316dce513Schristos
424*e992f068Schristos // occupy least table spaces, creating new sub-tables as needed
425*e992f068Schristos int use = least;
42616dce513Schristos while (rem < use) {
42716dce513Schristos use -= rem;
428*e992f068Schristos rem = 1 << (len - g.root);
42916dce513Schristos mem += rem;
43016dce513Schristos }
43116dce513Schristos rem -= use;
43216dce513Schristos
433*e992f068Schristos // examine codes from here, updating table space as we go
43416dce513Schristos for (use = least; use <= most; use++) {
435*e992f068Schristos g.code[len] = use;
436*e992f068Schristos examine(syms - use, (left - use) << 1, len + 1,
437*e992f068Schristos mem + (rem ? 1 << (len - g.root) : 0), rem << 1);
43816dce513Schristos if (rem == 0) {
439*e992f068Schristos rem = 1 << (len - g.root);
44016dce513Schristos mem += rem;
44116dce513Schristos }
44216dce513Schristos rem--;
44316dce513Schristos }
44416dce513Schristos
445*e992f068Schristos // remove entries as we drop back down in the recursion
446*e992f068Schristos g.code[len] = 0;
44716dce513Schristos }
44816dce513Schristos
449*e992f068Schristos // Look at all sub-codes starting with root + 1 bits. Look at only the valid
450*e992f068Schristos // intermediate code states (syms, left, len). For each completed code,
451*e992f068Schristos // calculate the amount of memory required by inflate to build the decoding
452*e992f068Schristos // tables. Find the maximum amount of memory required and show the codes that
453*e992f068Schristos // require that maximum.
enough(int syms)454*e992f068Schristos local void enough(int syms) {
455*e992f068Schristos // clear code
456*e992f068Schristos for (int n = 0; n <= g.max; n++)
457*e992f068Schristos g.code[n] = 0;
45816dce513Schristos
459*e992f068Schristos // look at all (root + 1) bit and longer codes
460*e992f068Schristos string_clear(&g.out); // empty saved results
461*e992f068Schristos g.large = 1 << g.root; // base table
462*e992f068Schristos if (g.root < g.max) // otherwise, there's only a base table
463*e992f068Schristos for (int n = 3; n <= syms; n++)
464*e992f068Schristos for (int left = 2; left < n; left += 2) {
465*e992f068Schristos // look at all reachable (root + 1) bit nodes, and the
466*e992f068Schristos // resulting codes (complete at root + 2 or more)
467*e992f068Schristos size_t index = map(n, left, g.root + 1);
468*e992f068Schristos if (g.root + 1 < g.max && g.num[index]) // reachable node
469*e992f068Schristos examine(n, left, g.root + 1, 1 << g.root, 0);
47016dce513Schristos
471*e992f068Schristos // also look at root bit codes with completions at root + 1
472*e992f068Schristos // bits (not saved in num, since complete), just in case
473*e992f068Schristos if (g.num[index - 1] && n <= left << 1)
474*e992f068Schristos examine((n - left) << 1, (n - left) << 1, g.root + 1,
475*e992f068Schristos 1 << g.root, 0);
47616dce513Schristos }
47716dce513Schristos
478*e992f068Schristos // done
479*e992f068Schristos printf("maximum of %d table entries for root = %d\n", g.large, g.root);
480*e992f068Schristos fputs(g.out.str, stdout);
48116dce513Schristos }
48216dce513Schristos
483*e992f068Schristos // Examine and show the total number of possible prefix codes for a given
484*e992f068Schristos // maximum number of symbols, initial root table size, and maximum code length
485*e992f068Schristos // in bits -- those are the command arguments in that order. The default values
486*e992f068Schristos // are 286, 9, and 15 respectively, for the deflate literal/length code. The
487*e992f068Schristos // possible codes are counted for each number of coded symbols from two to the
488*e992f068Schristos // maximum. The counts for each of those and the total number of codes are
489*e992f068Schristos // shown. The maximum number of inflate table entires is then calculated across
490*e992f068Schristos // all possible codes. Each new maximum number of table entries and the
491*e992f068Schristos // associated sub-code (starting at root + 1 == 10 bits) is shown.
492*e992f068Schristos //
493*e992f068Schristos // To count and examine prefix codes that are not length-limited, provide a
494*e992f068Schristos // maximum length equal to the number of symbols minus one.
495*e992f068Schristos //
496*e992f068Schristos // For the deflate literal/length code, use "enough". For the deflate distance
497*e992f068Schristos // code, use "enough 30 6".
main(int argc,char ** argv)498*e992f068Schristos int main(int argc, char **argv) {
499*e992f068Schristos // set up globals for cleanup()
500*e992f068Schristos g.code = NULL;
501*e992f068Schristos g.num = NULL;
502*e992f068Schristos g.done = NULL;
503*e992f068Schristos string_init(&g.out);
50416dce513Schristos
505*e992f068Schristos // get arguments -- default to the deflate literal/length code
506*e992f068Schristos int syms = 286;
507*e992f068Schristos g.root = 9;
508*e992f068Schristos g.max = 15;
50916dce513Schristos if (argc > 1) {
51016dce513Schristos syms = atoi(argv[1]);
51116dce513Schristos if (argc > 2) {
512*e992f068Schristos g.root = atoi(argv[2]);
51316dce513Schristos if (argc > 3)
514*e992f068Schristos g.max = atoi(argv[3]);
51516dce513Schristos }
51616dce513Schristos }
517*e992f068Schristos if (argc > 4 || syms < 2 || g.root < 1 || g.max < 1) {
51816dce513Schristos fputs("invalid arguments, need: [sym >= 2 [root >= 1 [max >= 1]]]\n",
51916dce513Schristos stderr);
52016dce513Schristos return 1;
52116dce513Schristos }
52216dce513Schristos
523*e992f068Schristos // if not restricting the code length, the longest is syms - 1
524*e992f068Schristos if (g.max > syms - 1)
525*e992f068Schristos g.max = syms - 1;
52616dce513Schristos
527*e992f068Schristos // determine the number of bits in a code_t
528*e992f068Schristos int bits = 0;
529*e992f068Schristos for (code_t word = 1; word; word <<= 1)
530*e992f068Schristos bits++;
53116dce513Schristos
532*e992f068Schristos // make sure that the calculation of most will not overflow
533*e992f068Schristos if (g.max > bits || (code_t)(syms - 2) >= ((code_t)-1 >> (g.max - 1))) {
53416dce513Schristos fputs("abort: code length too long for internal types\n", stderr);
53516dce513Schristos return 1;
53616dce513Schristos }
53716dce513Schristos
538*e992f068Schristos // reject impossible code requests
539*e992f068Schristos if ((code_t)(syms - 1) > ((code_t)1 << g.max) - 1) {
54016dce513Schristos fprintf(stderr, "%d symbols cannot be coded in %d bits\n",
541*e992f068Schristos syms, g.max);
54216dce513Schristos return 1;
54316dce513Schristos }
54416dce513Schristos
545*e992f068Schristos // allocate code vector
546*e992f068Schristos g.code = calloc(g.max + 1, sizeof(int));
547*e992f068Schristos assert(g.code != NULL && "out of memory");
54816dce513Schristos
549*e992f068Schristos // determine size of saved results array, checking for overflows,
550*e992f068Schristos // allocate and clear the array (set all to zero with calloc())
551*e992f068Schristos if (syms == 2) // iff max == 1
552*e992f068Schristos g.num = NULL; // won't be saving any results
55316dce513Schristos else {
554*e992f068Schristos g.size = syms >> 1;
555*e992f068Schristos int n = (syms - 1) >> 1;
556*e992f068Schristos assert(g.size <= (size_t)-1 / n && "overflow");
557*e992f068Schristos g.size *= n;
558*e992f068Schristos n = g.max - 1;
559*e992f068Schristos assert(g.size <= (size_t)-1 / n && "overflow");
560*e992f068Schristos g.size *= n;
561*e992f068Schristos g.num = calloc(g.size, sizeof(big_t));
562*e992f068Schristos assert(g.num != NULL && "out of memory");
56316dce513Schristos }
56416dce513Schristos
565*e992f068Schristos // count possible codes for all numbers of symbols, add up counts
566*e992f068Schristos big_t sum = 0;
567*e992f068Schristos for (int n = 2; n <= syms; n++) {
568*e992f068Schristos big_t got = count(n, 2, 1);
56916dce513Schristos sum += got;
570*e992f068Schristos assert(got != (big_t)-1 && sum >= got && "overflow");
57116dce513Schristos }
572*e992f068Schristos printf("%"PRIbig" total codes for 2 to %d symbols", sum, syms);
573*e992f068Schristos if (g.max < syms - 1)
574*e992f068Schristos printf(" (%d-bit length limit)\n", g.max);
57516dce513Schristos else
57616dce513Schristos puts(" (no length limit)");
57716dce513Schristos
578*e992f068Schristos // allocate and clear done array for been_here()
57916dce513Schristos if (syms == 2)
580*e992f068Schristos g.done = NULL;
581*e992f068Schristos else {
582*e992f068Schristos g.done = calloc(g.size, sizeof(struct tab));
583*e992f068Schristos assert(g.done != NULL && "out of memory");
58416dce513Schristos }
58516dce513Schristos
586*e992f068Schristos // find and show maximum inflate table usage
587*e992f068Schristos if (g.root > g.max) // reduce root to max length
588*e992f068Schristos g.root = g.max;
589*e992f068Schristos if ((code_t)syms < ((code_t)1 << (g.root + 1)))
59016dce513Schristos enough(syms);
59116dce513Schristos else
592*e992f068Schristos fputs("cannot handle minimum code lengths > root", stderr);
59316dce513Schristos
594*e992f068Schristos // done
59516dce513Schristos cleanup();
59616dce513Schristos return 0;
59716dce513Schristos }
598