annotate src/zlib-1.2.7/examples/enough.c @ 169:223a55898ab9 tip default

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