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