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