|
kusano |
fc6ab3 |
/* enough.c -- determine the maximum size of inflate's Huffman code tables over
|
|
kusano |
fc6ab3 |
* all possible valid and complete Huffman codes, subject to a length limit.
|
|
kusano |
fc6ab3 |
* Copyright (C) 2007, 2008, 2012 Mark Adler
|
|
kusano |
fc6ab3 |
* Version 1.4 18 August 2012 Mark Adler
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Version history:
|
|
kusano |
fc6ab3 |
1.0 3 Jan 2007 First version (derived from codecount.c version 1.4)
|
|
kusano |
fc6ab3 |
1.1 4 Jan 2007 Use faster incremental table usage computation
|
|
kusano |
fc6ab3 |
Prune examine() search on previously visited states
|
|
kusano |
fc6ab3 |
1.2 5 Jan 2007 Comments clean up
|
|
kusano |
fc6ab3 |
As inflate does, decrease root for short codes
|
|
kusano |
fc6ab3 |
Refuse cases where inflate would increase root
|
|
kusano |
fc6ab3 |
1.3 17 Feb 2008 Add argument for initial root table size
|
|
kusano |
fc6ab3 |
Fix bug for initial root table size == max - 1
|
|
kusano |
fc6ab3 |
Use a macro to compute the history index
|
|
kusano |
fc6ab3 |
1.4 18 Aug 2012 Avoid shifts more than bits in type (caused endless loop!)
|
|
kusano |
fc6ab3 |
Clean up comparisons of different types
|
|
kusano |
fc6ab3 |
Clean up code indentation
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/*
|
|
kusano |
fc6ab3 |
Examine all possible Huffman codes for a given number of symbols and a
|
|
kusano |
fc6ab3 |
maximum code length in bits to determine the maximum table size for zilb's
|
|
kusano |
fc6ab3 |
inflate. Only complete Huffman codes are counted.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Two codes are considered distinct if the vectors of the number of codes per
|
|
kusano |
fc6ab3 |
length are not identical. So permutations of the symbol assignments result
|
|
kusano |
fc6ab3 |
in the same code for the counting, as do permutations of the assignments of
|
|
kusano |
fc6ab3 |
the bit values to the codes (i.e. only canonical codes are counted).
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
We build a code from shorter to longer lengths, determining how many symbols
|
|
kusano |
fc6ab3 |
are coded at each length. At each step, we have how many symbols remain to
|
|
kusano |
fc6ab3 |
be coded, what the last code length used was, and how many bit patterns of
|
|
kusano |
fc6ab3 |
that length remain unused. Then we add one to the code length and double the
|
|
kusano |
fc6ab3 |
number of unused patterns to graduate to the next code length. We then
|
|
kusano |
fc6ab3 |
assign all portions of the remaining symbols to that code length that
|
|
kusano |
fc6ab3 |
preserve the properties of a correct and eventually complete code. Those
|
|
kusano |
fc6ab3 |
properties are: we cannot use more bit patterns than are available; and when
|
|
kusano |
fc6ab3 |
all the symbols are used, there are exactly zero possible bit patterns
|
|
kusano |
fc6ab3 |
remaining.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The inflate Huffman decoding algorithm uses two-level lookup tables for
|
|
kusano |
fc6ab3 |
speed. There is a single first-level table to decode codes up to root bits
|
|
kusano |
fc6ab3 |
in length (root == 9 in the current inflate implementation). The table
|
|
kusano |
fc6ab3 |
has 1 << root entries and is indexed by the next root bits of input. Codes
|
|
kusano |
fc6ab3 |
shorter than root bits have replicated table entries, so that the correct
|
|
kusano |
fc6ab3 |
entry is pointed to regardless of the bits that follow the short code. If
|
|
kusano |
fc6ab3 |
the code is longer than root bits, then the table entry points to a second-
|
|
kusano |
fc6ab3 |
level table. The size of that table is determined by the longest code with
|
|
kusano |
fc6ab3 |
that root-bit prefix. If that longest code has length len, then the table
|
|
kusano |
fc6ab3 |
has size 1 << (len - root), to index the remaining bits in that set of
|
|
kusano |
fc6ab3 |
codes. Each subsequent root-bit prefix then has its own sub-table. The
|
|
kusano |
fc6ab3 |
total number of table entries required by the code is calculated
|
|
kusano |
fc6ab3 |
incrementally as the number of codes at each bit length is populated. When
|
|
kusano |
fc6ab3 |
all of the codes are shorter than root bits, then root is reduced to the
|
|
kusano |
fc6ab3 |
longest code length, resulting in a single, smaller, one-level table.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The inflate algorithm also provides for small values of root (relative to
|
|
kusano |
fc6ab3 |
the log2 of the number of symbols), where the shortest code has more bits
|
|
kusano |
fc6ab3 |
than root. In that case, root is increased to the length of the shortest
|
|
kusano |
fc6ab3 |
code. This program, by design, does not handle that case, so it is verified
|
|
kusano |
fc6ab3 |
that the number of symbols is less than 2^(root + 1).
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
In order to speed up the examination (by about ten orders of magnitude for
|
|
kusano |
fc6ab3 |
the default arguments), the intermediate states in the build-up of a code
|
|
kusano |
fc6ab3 |
are remembered and previously visited branches are pruned. The memory
|
|
kusano |
fc6ab3 |
required for this will increase rapidly with the total number of symbols and
|
|
kusano |
fc6ab3 |
the maximum code length in bits. However this is a very small price to pay
|
|
kusano |
fc6ab3 |
for the vast speedup.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
First, all of the possible Huffman codes are counted, and reachable
|
|
kusano |
fc6ab3 |
intermediate states are noted by a non-zero count in a saved-results array.
|
|
kusano |
fc6ab3 |
Second, the intermediate states that lead to (root + 1) bit or longer codes
|
|
kusano |
fc6ab3 |
are used to look at all sub-codes from those junctures for their inflate
|
|
kusano |
fc6ab3 |
memory usage. (The amount of memory used is not affected by the number of
|
|
kusano |
fc6ab3 |
codes of root bits or less in length.) Third, the visited states in the
|
|
kusano |
fc6ab3 |
construction of those sub-codes and the associated calculation of the table
|
|
kusano |
fc6ab3 |
size is recalled in order to avoid recalculating from the same juncture.
|
|
kusano |
fc6ab3 |
Beginning the code examination at (root + 1) bit codes, which is enabled by
|
|
kusano |
fc6ab3 |
identifying the reachable nodes, accounts for about six of the orders of
|
|
kusano |
fc6ab3 |
magnitude of improvement for the default arguments. About another four
|
|
kusano |
fc6ab3 |
orders of magnitude come from not revisiting previous states. Out of
|
|
kusano |
fc6ab3 |
approximately 2x10^16 possible Huffman codes, only about 2x10^6 sub-codes
|
|
kusano |
fc6ab3 |
need to be examined to cover all of the possible table memory usage cases
|
|
kusano |
fc6ab3 |
for the default arguments of 286 symbols limited to 15-bit codes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Note that an unsigned long long type is used for counting. It is quite easy
|
|
kusano |
fc6ab3 |
to exceed the capacity of an eight-byte integer with a large number of
|
|
kusano |
fc6ab3 |
symbols and a large maximum code length, so multiple-precision arithmetic
|
|
kusano |
fc6ab3 |
would need to replace the unsigned long long arithmetic in that case. This
|
|
kusano |
fc6ab3 |
program will abort if an overflow occurs. The big_t type identifies where
|
|
kusano |
fc6ab3 |
the counting takes place.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
An unsigned long long type is also used for calculating the number of
|
|
kusano |
fc6ab3 |
possible codes remaining at the maximum length. This limits the maximum
|
|
kusano |
fc6ab3 |
code length to the number of bits in a long long minus the number of bits
|
|
kusano |
fc6ab3 |
needed to represent the symbols in a flat code. The code_t type identifies
|
|
kusano |
fc6ab3 |
where the bit pattern counting takes place.
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
#include <stdio.h></stdio.h>
|
|
kusano |
fc6ab3 |
#include <stdlib.h></stdlib.h>
|
|
kusano |
fc6ab3 |
#include <string.h></string.h>
|
|
kusano |
fc6ab3 |
#include <assert.h></assert.h>
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
#define local static
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* special data types */
|
|
kusano |
fc6ab3 |
typedef unsigned long long big_t; /* type for code counting */
|
|
kusano |
fc6ab3 |
typedef unsigned long long code_t; /* type for bit pattern counting */
|
|
kusano |
fc6ab3 |
struct tab { /* type for been here check */
|
|
kusano |
fc6ab3 |
size_t len; /* length of bit vector in char's */
|
|
kusano |
fc6ab3 |
char *vec; /* allocated bit vector */
|
|
kusano |
fc6ab3 |
};
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* The array for saving results, num[], is indexed with this triplet:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
syms: number of symbols remaining to code
|
|
kusano |
fc6ab3 |
left: number of available bit patterns at length len
|
|
kusano |
fc6ab3 |
len: number of bits in the codes currently being assigned
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Those indices are constrained thusly when saving results:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
syms: 3..totsym (totsym == total symbols to code)
|
|
kusano |
fc6ab3 |
left: 2..syms - 1, but only the evens (so syms == 8 -> 2, 4, 6)
|
|
kusano |
fc6ab3 |
len: 1..max - 1 (max == maximum code length in bits)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
syms == 2 is not saved since that immediately leads to a single code. left
|
|
kusano |
fc6ab3 |
must be even, since it represents the number of available bit patterns at
|
|
kusano |
fc6ab3 |
the current length, which is double the number at the previous length.
|
|
kusano |
fc6ab3 |
left ends at syms-1 since left == syms immediately results in a single code.
|
|
kusano |
fc6ab3 |
(left > sym is not allowed since that would result in an incomplete code.)
|
|
kusano |
fc6ab3 |
len is less than max, since the code completes immediately when len == max.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The offset into the array is calculated for the three indices with the
|
|
kusano |
fc6ab3 |
first one (syms) being outermost, and the last one (len) being innermost.
|
|
kusano |
fc6ab3 |
We build the array with length max-1 lists for the len index, with syms-3
|
|
kusano |
fc6ab3 |
of those for each symbol. There are totsym-2 of those, with each one
|
|
kusano |
fc6ab3 |
varying in length as a function of sym. See the calculation of index in
|
|
kusano |
fc6ab3 |
count() for the index, and the calculation of size in main() for the size
|
|
kusano |
fc6ab3 |
of the array.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
For the deflate example of 286 symbols limited to 15-bit codes, the array
|
|
kusano |
fc6ab3 |
has 284,284 entries, taking up 2.17 MB for an 8-byte big_t. More than
|
|
kusano |
fc6ab3 |
half of the space allocated for saved results is actually used -- not all
|
|
kusano |
fc6ab3 |
possible triplets are reached in the generation of valid Huffman codes.
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* The array for tracking visited states, done[], is itself indexed identically
|
|
kusano |
fc6ab3 |
to the num[] array as described above for the (syms, left, len) triplet.
|
|
kusano |
fc6ab3 |
Each element in the array is further indexed by the (mem, rem) doublet,
|
|
kusano |
fc6ab3 |
where mem is the amount of inflate table space used so far, and rem is the
|
|
kusano |
fc6ab3 |
remaining unused entries in the current inflate sub-table. Each indexed
|
|
kusano |
fc6ab3 |
element is simply one bit indicating whether the state has been visited or
|
|
kusano |
fc6ab3 |
not. Since the ranges for mem and rem are not known a priori, each bit
|
|
kusano |
fc6ab3 |
vector is of a variable size, and grows as needed to accommodate the visited
|
|
kusano |
fc6ab3 |
states. mem and rem are used to calculate a single index in a triangular
|
|
kusano |
fc6ab3 |
array. Since the range of mem is expected in the default case to be about
|
|
kusano |
fc6ab3 |
ten times larger than the range of rem, the array is skewed to reduce the
|
|
kusano |
fc6ab3 |
memory usage, with eight times the range for mem than for rem. See the
|
|
kusano |
fc6ab3 |
calculations for offset and bit in beenhere() for the details.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
For the deflate example of 286 symbols limited to 15-bit codes, the bit
|
|
kusano |
fc6ab3 |
vectors grow to total approximately 21 MB, in addition to the 4.3 MB done[]
|
|
kusano |
fc6ab3 |
array itself.
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Globals to avoid propagating constants or constant pointers recursively */
|
|
kusano |
fc6ab3 |
local int max; /* maximum allowed bit length for the codes */
|
|
kusano |
fc6ab3 |
local int root; /* size of base code table in bits */
|
|
kusano |
fc6ab3 |
local int large; /* largest code table so far */
|
|
kusano |
fc6ab3 |
local size_t size; /* number of elements in num and done */
|
|
kusano |
fc6ab3 |
local int *code; /* number of symbols assigned to each bit length */
|
|
kusano |
fc6ab3 |
local big_t *num; /* saved results array for code counting */
|
|
kusano |
fc6ab3 |
local struct tab *done; /* states already evaluated array */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Index function for num[] and done[] */
|
|
kusano |
fc6ab3 |
#define INDEX(i,j,k) (((size_t)((i-1)>>1)*((i-2)>>1)+(j>>1)-1)*(max-1)+k-1)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Free allocated space. Uses globals code, num, and done. */
|
|
kusano |
fc6ab3 |
local void cleanup(void)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
size_t n;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
if (done != NULL) {
|
|
kusano |
fc6ab3 |
for (n = 0; n < size; n++)
|
|
kusano |
fc6ab3 |
if (done[n].len)
|
|
kusano |
fc6ab3 |
free(done[n].vec);
|
|
kusano |
fc6ab3 |
free(done);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
if (num != NULL)
|
|
kusano |
fc6ab3 |
free(num);
|
|
kusano |
fc6ab3 |
if (code != NULL)
|
|
kusano |
fc6ab3 |
free(code);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Return the number of possible Huffman codes using bit patterns of lengths
|
|
kusano |
fc6ab3 |
len through max inclusive, coding syms symbols, with left bit patterns of
|
|
kusano |
fc6ab3 |
length len unused -- return -1 if there is an overflow in the counting.
|
|
kusano |
fc6ab3 |
Keep a record of previous results in num to prevent repeating the same
|
|
kusano |
fc6ab3 |
calculation. Uses the globals max and num. */
|
|
kusano |
fc6ab3 |
local big_t count(int syms, int len, int left)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
big_t sum; /* number of possible codes from this juncture */
|
|
kusano |
fc6ab3 |
big_t got; /* value returned from count() */
|
|
kusano |
fc6ab3 |
int least; /* least number of syms to use at this juncture */
|
|
kusano |
fc6ab3 |
int most; /* most number of syms to use at this juncture */
|
|
kusano |
fc6ab3 |
int use; /* number of bit patterns to use in next call */
|
|
kusano |
fc6ab3 |
size_t index; /* index of this case in *num */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* see if only one possible code */
|
|
kusano |
fc6ab3 |
if (syms == left)
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* note and verify the expected state */
|
|
kusano |
fc6ab3 |
assert(syms > left && left > 0 && len < max);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* see if we've done this one already */
|
|
kusano |
fc6ab3 |
index = INDEX(syms, left, len);
|
|
kusano |
fc6ab3 |
got = num[index];
|
|
kusano |
fc6ab3 |
if (got)
|
|
kusano |
fc6ab3 |
return got; /* we have -- return the saved result */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* we need to use at least this many bit patterns so that the code won't be
|
|
kusano |
fc6ab3 |
incomplete at the next length (more bit patterns than symbols) */
|
|
kusano |
fc6ab3 |
least = (left << 1) - syms;
|
|
kusano |
fc6ab3 |
if (least < 0)
|
|
kusano |
fc6ab3 |
least = 0;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* we can use at most this many bit patterns, lest there not be enough
|
|
kusano |
fc6ab3 |
available for the remaining symbols at the maximum length (if there were
|
|
kusano |
fc6ab3 |
no limit to the code length, this would become: most = left - 1) */
|
|
kusano |
fc6ab3 |
most = (((code_t)left << (max - len)) - syms) /
|
|
kusano |
fc6ab3 |
(((code_t)1 << (max - len)) - 1);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* count all possible codes from this juncture and add them up */
|
|
kusano |
fc6ab3 |
sum = 0;
|
|
kusano |
fc6ab3 |
for (use = least; use <= most; use++) {
|
|
kusano |
fc6ab3 |
got = count(syms - use, len + 1, (left - use) << 1);
|
|
kusano |
fc6ab3 |
sum += got;
|
|
kusano |
fc6ab3 |
if (got == (big_t)0 - 1 || sum < got) /* overflow */
|
|
kusano |
fc6ab3 |
return (big_t)0 - 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* verify that all recursive calls are productive */
|
|
kusano |
fc6ab3 |
assert(sum != 0);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* save the result and return it */
|
|
kusano |
fc6ab3 |
num[index] = sum;
|
|
kusano |
fc6ab3 |
return sum;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Return true if we've been here before, set to true if not. Set a bit in a
|
|
kusano |
fc6ab3 |
bit vector to indicate visiting this state. Each (syms,len,left) state
|
|
kusano |
fc6ab3 |
has a variable size bit vector indexed by (mem,rem). The bit vector is
|
|
kusano |
fc6ab3 |
lengthened if needed to allow setting the (mem,rem) bit. */
|
|
kusano |
fc6ab3 |
local int beenhere(int syms, int len, int left, int mem, int rem)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
size_t index; /* index for this state's bit vector */
|
|
kusano |
fc6ab3 |
size_t offset; /* offset in this state's bit vector */
|
|
kusano |
fc6ab3 |
int bit; /* mask for this state's bit */
|
|
kusano |
fc6ab3 |
size_t length; /* length of the bit vector in bytes */
|
|
kusano |
fc6ab3 |
char *vector; /* new or enlarged bit vector */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* point to vector for (syms,left,len), bit in vector for (mem,rem) */
|
|
kusano |
fc6ab3 |
index = INDEX(syms, left, len);
|
|
kusano |
fc6ab3 |
mem -= 1 << root;
|
|
kusano |
fc6ab3 |
offset = (mem >> 3) + rem;
|
|
kusano |
fc6ab3 |
offset = ((offset * (offset + 1)) >> 1) + rem;
|
|
kusano |
fc6ab3 |
bit = 1 << (mem & 7);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* see if we've been here */
|
|
kusano |
fc6ab3 |
length = done[index].len;
|
|
kusano |
fc6ab3 |
if (offset < length && (done[index].vec[offset] & bit) != 0)
|
|
kusano |
fc6ab3 |
return 1; /* done this! */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* we haven't been here before -- set the bit to show we have now */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* see if we need to lengthen the vector in order to set the bit */
|
|
kusano |
fc6ab3 |
if (length <= offset) {
|
|
kusano |
fc6ab3 |
/* if we have one already, enlarge it, zero out the appended space */
|
|
kusano |
fc6ab3 |
if (length) {
|
|
kusano |
fc6ab3 |
do {
|
|
kusano |
fc6ab3 |
length <<= 1;
|
|
kusano |
fc6ab3 |
} while (length <= offset);
|
|
kusano |
fc6ab3 |
vector = realloc(done[index].vec, length);
|
|
kusano |
fc6ab3 |
if (vector != NULL)
|
|
kusano |
fc6ab3 |
memset(vector + done[index].len, 0, length - done[index].len);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* otherwise we need to make a new vector and zero it out */
|
|
kusano |
fc6ab3 |
else {
|
|
kusano |
fc6ab3 |
length = 1 << (len - root);
|
|
kusano |
fc6ab3 |
while (length <= offset)
|
|
kusano |
fc6ab3 |
length <<= 1;
|
|
kusano |
fc6ab3 |
vector = calloc(length, sizeof(char));
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* in either case, bail if we can't get the memory */
|
|
kusano |
fc6ab3 |
if (vector == NULL) {
|
|
kusano |
fc6ab3 |
fputs("abort: unable to allocate enough memory\n", stderr);
|
|
kusano |
fc6ab3 |
cleanup();
|
|
kusano |
fc6ab3 |
exit(1);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* install the new vector */
|
|
kusano |
fc6ab3 |
done[index].len = length;
|
|
kusano |
fc6ab3 |
done[index].vec = vector;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* set the bit */
|
|
kusano |
fc6ab3 |
done[index].vec[offset] |= bit;
|
|
kusano |
fc6ab3 |
return 0;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Examine all possible codes from the given node (syms, len, left). Compute
|
|
kusano |
fc6ab3 |
the amount of memory required to build inflate's decoding tables, where the
|
|
kusano |
fc6ab3 |
number of code structures used so far is mem, and the number remaining in
|
|
kusano |
fc6ab3 |
the current sub-table is rem. Uses the globals max, code, root, large, and
|
|
kusano |
fc6ab3 |
done. */
|
|
kusano |
fc6ab3 |
local void examine(int syms, int len, int left, int mem, int rem)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
int least; /* least number of syms to use at this juncture */
|
|
kusano |
fc6ab3 |
int most; /* most number of syms to use at this juncture */
|
|
kusano |
fc6ab3 |
int use; /* number of bit patterns to use in next call */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* see if we have a complete code */
|
|
kusano |
fc6ab3 |
if (syms == left) {
|
|
kusano |
fc6ab3 |
/* set the last code entry */
|
|
kusano |
fc6ab3 |
code[len] = left;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* complete computation of memory used by this code */
|
|
kusano |
fc6ab3 |
while (rem < left) {
|
|
kusano |
fc6ab3 |
left -= rem;
|
|
kusano |
fc6ab3 |
rem = 1 << (len - root);
|
|
kusano |
fc6ab3 |
mem += rem;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
assert(rem == left);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* if this is a new maximum, show the entries used and the sub-code */
|
|
kusano |
fc6ab3 |
if (mem > large) {
|
|
kusano |
fc6ab3 |
large = mem;
|
|
kusano |
fc6ab3 |
printf("max %d: ", mem);
|
|
kusano |
fc6ab3 |
for (use = root + 1; use <= max; use++)
|
|
kusano |
fc6ab3 |
if (code[use])
|
|
kusano |
fc6ab3 |
printf("%d[%d] ", code[use], use);
|
|
kusano |
fc6ab3 |
putchar('\n');
|
|
kusano |
fc6ab3 |
fflush(stdout);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* remove entries as we drop back down in the recursion */
|
|
kusano |
fc6ab3 |
code[len] = 0;
|
|
kusano |
fc6ab3 |
return;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* prune the tree if we can */
|
|
kusano |
fc6ab3 |
if (beenhere(syms, len, left, mem, rem))
|
|
kusano |
fc6ab3 |
return;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* we need to use at least this many bit patterns so that the code won't be
|
|
kusano |
fc6ab3 |
incomplete at the next length (more bit patterns than symbols) */
|
|
kusano |
fc6ab3 |
least = (left << 1) - syms;
|
|
kusano |
fc6ab3 |
if (least < 0)
|
|
kusano |
fc6ab3 |
least = 0;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* we can use at most this many bit patterns, lest there not be enough
|
|
kusano |
fc6ab3 |
available for the remaining symbols at the maximum length (if there were
|
|
kusano |
fc6ab3 |
no limit to the code length, this would become: most = left - 1) */
|
|
kusano |
fc6ab3 |
most = (((code_t)left << (max - len)) - syms) /
|
|
kusano |
fc6ab3 |
(((code_t)1 << (max - len)) - 1);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* occupy least table spaces, creating new sub-tables as needed */
|
|
kusano |
fc6ab3 |
use = least;
|
|
kusano |
fc6ab3 |
while (rem < use) {
|
|
kusano |
fc6ab3 |
use -= rem;
|
|
kusano |
fc6ab3 |
rem = 1 << (len - root);
|
|
kusano |
fc6ab3 |
mem += rem;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
rem -= use;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* examine codes from here, updating table space as we go */
|
|
kusano |
fc6ab3 |
for (use = least; use <= most; use++) {
|
|
kusano |
fc6ab3 |
code[len] = use;
|
|
kusano |
fc6ab3 |
examine(syms - use, len + 1, (left - use) << 1,
|
|
kusano |
fc6ab3 |
mem + (rem ? 1 << (len - root) : 0), rem << 1);
|
|
kusano |
fc6ab3 |
if (rem == 0) {
|
|
kusano |
fc6ab3 |
rem = 1 << (len - root);
|
|
kusano |
fc6ab3 |
mem += rem;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
rem--;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* remove entries as we drop back down in the recursion */
|
|
kusano |
fc6ab3 |
code[len] = 0;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* Look at all sub-codes starting with root + 1 bits. Look at only the valid
|
|
kusano |
fc6ab3 |
intermediate code states (syms, left, len). For each completed code,
|
|
kusano |
fc6ab3 |
calculate the amount of memory required by inflate to build the decoding
|
|
kusano |
fc6ab3 |
tables. Find the maximum amount of memory required and show the code that
|
|
kusano |
fc6ab3 |
requires that maximum. Uses the globals max, root, and num. */
|
|
kusano |
fc6ab3 |
local void enough(int syms)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
int n; /* number of remaing symbols for this node */
|
|
kusano |
fc6ab3 |
int left; /* number of unused bit patterns at this length */
|
|
kusano |
fc6ab3 |
size_t index; /* index of this case in *num */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* clear code */
|
|
kusano |
fc6ab3 |
for (n = 0; n <= max; n++)
|
|
kusano |
fc6ab3 |
code[n] = 0;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* look at all (root + 1) bit and longer codes */
|
|
kusano |
fc6ab3 |
large = 1 << root; /* base table */
|
|
kusano |
fc6ab3 |
if (root < max) /* otherwise, there's only a base table */
|
|
kusano |
fc6ab3 |
for (n = 3; n <= syms; n++)
|
|
kusano |
fc6ab3 |
for (left = 2; left < n; left += 2)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
/* look at all reachable (root + 1) bit nodes, and the
|
|
kusano |
fc6ab3 |
resulting codes (complete at root + 2 or more) */
|
|
kusano |
fc6ab3 |
index = INDEX(n, left, root + 1);
|
|
kusano |
fc6ab3 |
if (root + 1 < max && num[index]) /* reachable node */
|
|
kusano |
fc6ab3 |
examine(n, root + 1, left, 1 << root, 0);
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* also look at root bit codes with completions at root + 1
|
|
kusano |
fc6ab3 |
bits (not saved in num, since complete), just in case */
|
|
kusano |
fc6ab3 |
if (num[index - 1] && n <= left << 1)
|
|
kusano |
fc6ab3 |
examine((n - left) << 1, root + 1, (n - left) << 1,
|
|
kusano |
fc6ab3 |
1 << root, 0);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* done */
|
|
kusano |
fc6ab3 |
printf("done: maximum of %d table entries\n", large);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/*
|
|
kusano |
fc6ab3 |
Examine and show the total number of possible Huffman codes for a given
|
|
kusano |
fc6ab3 |
maximum number of symbols, initial root table size, and maximum code length
|
|
kusano |
fc6ab3 |
in bits -- those are the command arguments in that order. The default
|
|
kusano |
fc6ab3 |
values are 286, 9, and 15 respectively, for the deflate literal/length code.
|
|
kusano |
fc6ab3 |
The possible codes are counted for each number of coded symbols from two to
|
|
kusano |
fc6ab3 |
the maximum. The counts for each of those and the total number of codes are
|
|
kusano |
fc6ab3 |
shown. The maximum number of inflate table entires is then calculated
|
|
kusano |
fc6ab3 |
across all possible codes. Each new maximum number of table entries and the
|
|
kusano |
fc6ab3 |
associated sub-code (starting at root + 1 == 10 bits) is shown.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
To count and examine Huffman codes that are not length-limited, provide a
|
|
kusano |
fc6ab3 |
maximum length equal to the number of symbols minus one.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
For the deflate literal/length code, use "enough". For the deflate distance
|
|
kusano |
fc6ab3 |
code, use "enough 30 6".
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
This uses the %llu printf format to print big_t numbers, which assumes that
|
|
kusano |
fc6ab3 |
big_t is an unsigned long long. If the big_t type is changed (for example
|
|
kusano |
fc6ab3 |
to a multiple precision type), the method of printing will also need to be
|
|
kusano |
fc6ab3 |
updated.
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
int main(int argc, char **argv)
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
int syms; /* total number of symbols to code */
|
|
kusano |
fc6ab3 |
int n; /* number of symbols to code for this run */
|
|
kusano |
fc6ab3 |
big_t got; /* return value of count() */
|
|
kusano |
fc6ab3 |
big_t sum; /* accumulated number of codes over n */
|
|
kusano |
fc6ab3 |
code_t word; /* for counting bits in code_t */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* set up globals for cleanup() */
|
|
kusano |
fc6ab3 |
code = NULL;
|
|
kusano |
fc6ab3 |
num = NULL;
|
|
kusano |
fc6ab3 |
done = NULL;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* get arguments -- default to the deflate literal/length code */
|
|
kusano |
fc6ab3 |
syms = 286;
|
|
kusano |
fc6ab3 |
root = 9;
|
|
kusano |
fc6ab3 |
max = 15;
|
|
kusano |
fc6ab3 |
if (argc > 1) {
|
|
kusano |
fc6ab3 |
syms = atoi(argv[1]);
|
|
kusano |
fc6ab3 |
if (argc > 2) {
|
|
kusano |
fc6ab3 |
root = atoi(argv[2]);
|
|
kusano |
fc6ab3 |
if (argc > 3)
|
|
kusano |
fc6ab3 |
max = atoi(argv[3]);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
if (argc > 4 || syms < 2 || root < 1 || max < 1) {
|
|
kusano |
fc6ab3 |
fputs("invalid arguments, need: [sym >= 2 [root >= 1 [max >= 1]]]\n",
|
|
kusano |
fc6ab3 |
stderr);
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* if not restricting the code length, the longest is syms - 1 */
|
|
kusano |
fc6ab3 |
if (max > syms - 1)
|
|
kusano |
fc6ab3 |
max = syms - 1;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* determine the number of bits in a code_t */
|
|
kusano |
fc6ab3 |
for (n = 0, word = 1; word; n++, word <<= 1)
|
|
kusano |
fc6ab3 |
;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* make sure that the calculation of most will not overflow */
|
|
kusano |
fc6ab3 |
if (max > n || (code_t)(syms - 2) >= (((code_t)0 - 1) >> (max - 1))) {
|
|
kusano |
fc6ab3 |
fputs("abort: code length too long for internal types\n", stderr);
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* reject impossible code requests */
|
|
kusano |
fc6ab3 |
if ((code_t)(syms - 1) > ((code_t)1 << max) - 1) {
|
|
kusano |
fc6ab3 |
fprintf(stderr, "%d symbols cannot be coded in %d bits\n",
|
|
kusano |
fc6ab3 |
syms, max);
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* allocate code vector */
|
|
kusano |
fc6ab3 |
code = calloc(max + 1, sizeof(int));
|
|
kusano |
fc6ab3 |
if (code == NULL) {
|
|
kusano |
fc6ab3 |
fputs("abort: unable to allocate enough memory\n", stderr);
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* determine size of saved results array, checking for overflows,
|
|
kusano |
fc6ab3 |
allocate and clear the array (set all to zero with calloc()) */
|
|
kusano |
fc6ab3 |
if (syms == 2) /* iff max == 1 */
|
|
kusano |
fc6ab3 |
num = NULL; /* won't be saving any results */
|
|
kusano |
fc6ab3 |
else {
|
|
kusano |
fc6ab3 |
size = syms >> 1;
|
|
kusano |
fc6ab3 |
if (size > ((size_t)0 - 1) / (n = (syms - 1) >> 1) ||
|
|
kusano |
fc6ab3 |
(size *= n, size > ((size_t)0 - 1) / (n = max - 1)) ||
|
|
kusano |
fc6ab3 |
(size *= n, size > ((size_t)0 - 1) / sizeof(big_t)) ||
|
|
kusano |
fc6ab3 |
(num = calloc(size, sizeof(big_t))) == NULL) {
|
|
kusano |
fc6ab3 |
fputs("abort: unable to allocate enough memory\n", stderr);
|
|
kusano |
fc6ab3 |
cleanup();
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* count possible codes for all numbers of symbols, add up counts */
|
|
kusano |
fc6ab3 |
sum = 0;
|
|
kusano |
fc6ab3 |
for (n = 2; n <= syms; n++) {
|
|
kusano |
fc6ab3 |
got = count(n, 1, 2);
|
|
kusano |
fc6ab3 |
sum += got;
|
|
kusano |
fc6ab3 |
if (got == (big_t)0 - 1 || sum < got) { /* overflow */
|
|
kusano |
fc6ab3 |
fputs("abort: can't count that high!\n", stderr);
|
|
kusano |
fc6ab3 |
cleanup();
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
printf("%llu %d-codes\n", got, n);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
printf("%llu total codes for 2 to %d symbols", sum, syms);
|
|
kusano |
fc6ab3 |
if (max < syms - 1)
|
|
kusano |
fc6ab3 |
printf(" (%d-bit length limit)\n", max);
|
|
kusano |
fc6ab3 |
else
|
|
kusano |
fc6ab3 |
puts(" (no length limit)");
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* allocate and clear done array for beenhere() */
|
|
kusano |
fc6ab3 |
if (syms == 2)
|
|
kusano |
fc6ab3 |
done = NULL;
|
|
kusano |
fc6ab3 |
else if (size > ((size_t)0 - 1) / sizeof(struct tab) ||
|
|
kusano |
fc6ab3 |
(done = calloc(size, sizeof(struct tab))) == NULL) {
|
|
kusano |
fc6ab3 |
fputs("abort: unable to allocate enough memory\n", stderr);
|
|
kusano |
fc6ab3 |
cleanup();
|
|
kusano |
fc6ab3 |
return 1;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* find and show maximum inflate table usage */
|
|
kusano |
fc6ab3 |
if (root > max) /* reduce root to max length */
|
|
kusano |
fc6ab3 |
root = max;
|
|
kusano |
fc6ab3 |
if ((code_t)syms < ((code_t)1 << (root + 1)))
|
|
kusano |
fc6ab3 |
enough(syms);
|
|
kusano |
fc6ab3 |
else
|
|
kusano |
fc6ab3 |
puts("cannot handle minimum code lengths > root");
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* done */
|
|
kusano |
fc6ab3 |
cleanup();
|
|
kusano |
fc6ab3 |
return 0;
|
|
kusano |
fc6ab3 |
}
|