annotate src/zlib-1.2.7/examples/enough.c @ 83:ae30d91d2ffe

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