mirror of https://github.com/madler/zlib.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
309 lines
13 KiB
309 lines
13 KiB
/* inftrees.c -- generate Huffman trees for efficient decoding |
|
* Copyright (C) 1995-2022 Mark Adler |
|
* For conditions of distribution and use, see copyright notice in zlib.h |
|
*/ |
|
|
|
#include "zutil.h" |
|
#include "inftrees.h" |
|
|
|
#define MAXBITS 15 |
|
|
|
const char inflate_copyright[] = |
|
" inflate 1.2.13 Copyright 1995-2022 Mark Adler "; |
|
/* |
|
If you use the zlib library in a product, an acknowledgment is welcome |
|
in the documentation of your product. If for some reason you cannot |
|
include such an acknowledgment, I would appreciate that you keep this |
|
copyright string in the executable of your product. |
|
*/ |
|
|
|
/* |
|
Build a set of tables to decode the provided canonical Huffman code. |
|
The code lengths are lens[0..codeSize-1]. The result starts at *table, |
|
whose indices are 0..2^bits-1. work is a writable array of at least |
|
lens shorts, which is used as a work area. type is the type of code |
|
to be generated, CODES, LENS, or DISTS. On return, zero is success, |
|
-1 is an invalid code, and +1 means that ENOUGH isn't enough. table |
|
on return points to the next available entry's address. |
|
*/ |
|
int ZLIB_INTERNAL inflate_table(type, lens, codeSize, rootMaskPtr, rootTable, extTable) |
|
codetype type; |
|
unsigned char FAR* lens; |
|
unsigned codeSize; |
|
unsigned FAR* rootMaskPtr; |
|
DecodeStr FAR* rootTable; |
|
DecodeStr FAR* extTable; |
|
{ |
|
unsigned len; /* a code's length in bits */ |
|
unsigned sym; /* index of code symbols */ |
|
unsigned min, maxHufBits; /* minimum and maximum code lengths */ |
|
unsigned root, rootMask; /* number of index bits for root table */ |
|
unsigned curr; /* number of index bits for current table */ |
|
unsigned drop; /* code bits to drop for sub-table */ |
|
int left; /* number of prefix codes available */ |
|
unsigned used; /* code entries in table used */ |
|
unsigned hufCode; /* Huffman code */ |
|
unsigned incr; /* for incrementing code, index */ |
|
unsigned fill; /* index for replicating entries */ |
|
unsigned low; /* low bits for current root entry */ |
|
unsigned mask; /* mask for low root bits */ |
|
DecodeStr here; /* table entry for duplication */ |
|
DecodeStr FAR* next; /* next available space in table */ |
|
const unsigned short FAR* base; /* base value table to use */ |
|
const unsigned short FAR* extra; /* extra bits table to use */ |
|
unsigned match; /* use base and extra for symbol >= match */ |
|
unsigned short count[MAX_HUFBITS + 1]; /* number of codes of each length */ |
|
unsigned short offs[MAX_HUFBITS + 2]; /* offsets in table for each length */ |
|
unsigned short symByLen[288]; /* sorted symbol sequence by increasing order of Huffman bit length */ |
|
|
|
|
|
static const unsigned short lbase[31] = { /* Length codes 257..285 base */ |
|
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
|
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; |
|
static const unsigned short lext[31] = { /* Length codes 257..285 extra */ |
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
|
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 77, 202 }; |
|
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ |
|
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
|
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
|
8193, 12289, 16385, 24577, 0, 0 }; |
|
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ |
|
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
|
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
|
12, 12, 13, 13, 64, 64 }; |
|
|
|
/* |
|
Process a set of code lengths to create a canonical Huffman code. The |
|
code lengths are lens[0..codeSize-1]. Each length corresponds to the |
|
symbols 0..codes-1. The Huffman code is generated by first sorting the |
|
symbols by length from short to long, and retaining the symbol order |
|
for codes with equal lengths. Then the code starts with all zero bits |
|
for the first code of the shortest length, and the codes are integer |
|
increments for the same length, and zeros are appended as the length |
|
increases. For the deflate format, these bits are stored backwards |
|
from their more natural integer increment ordering, and so when the |
|
decoding tables are built in the large loop below, the integer codes |
|
are incremented backwards. |
|
|
|
This routine assumes, but does not check, that all of the entries in |
|
lens[] are in the range 0..MAX_HUFBITS. The caller must assure this. |
|
1..MAX_HUFBITS is interpreted as that code length. zero means that that |
|
symbol does not occur in this code. |
|
|
|
The codes are sorted by computing a count of codes for each length, |
|
creating from that a table of starting indices for each length in the |
|
sorted table, and then entering the symbols in order in the sorted |
|
table. The sorted table is work[], with that space being provided by |
|
the caller. |
|
|
|
The length counts are used for other purposes as well, i.e. finding |
|
the minimum and maximum length codes, determining if there are any |
|
codes at all, checking for a valid set of lengths, and looking ahead |
|
at length counts to determine sub-table sizes when building the |
|
decoding tables. |
|
*/ |
|
|
|
/* accumulate lengths for codes (assumes lens[] all in 0..MAX_HUFBITS) */ |
|
for (len = 0; len <= MAX_HUFBITS; len++) |
|
count[len] = 0; |
|
for (sym = 0; sym < codeSize; sym++) |
|
count[lens[sym]]++; |
|
|
|
/* bound code lengths, force root to be within code lengths */ |
|
maxHufBits = MAX_HUFBITS; |
|
while (maxHufBits > 0 && count[maxHufBits] == 0) |
|
maxHufBits--; |
|
|
|
if (maxHufBits == 0) { /* no symbols to code at all */ |
|
here.val = 0xFF00; /* invalid code marker */ |
|
here.allBits = 0; |
|
here.hufBits = 0; |
|
rootTable[0] = here; /* make a table to force an error */ |
|
rootTable[1] = here; |
|
|
|
return 0; /* no symbols, but wait for decoding to report error */ |
|
} |
|
|
|
/* set up for code type */ |
|
switch (type) { |
|
case LENS: |
|
root = LEN_ROOT; |
|
base = lbase; |
|
extra = lext; |
|
match = 257; |
|
rootMask = (1 << (1 + root)) - 1; |
|
break; |
|
case DISTS: /* DISTS */ |
|
root = DIST_ROOT; |
|
base = dbase; |
|
extra = dext; |
|
match = 0; |
|
rootMask = (1 << (1 + root)) - 1; |
|
|
|
break; |
|
default: /* LLENS */ |
|
match = 20; |
|
root = min(maxHufBits, LLEN_ROOT); |
|
rootMask = root; //repurposed as root to be returned back |
|
} |
|
|
|
|
|
for (min = 1; min < maxHufBits; min++) |
|
if (count[min] != 0) break; |
|
|
|
/* check for an over-subscribed or incomplete set of lengths */ |
|
left = 1; |
|
for (len = 1; len <= MAX_HUFBITS; len++) { |
|
left <<= 1; |
|
left -= count[len]; |
|
if (left < 0) return -1; /* over-subscribed */ |
|
} |
|
if (left > 0 && (type == LLENS || maxHufBits != 1)) |
|
return -1; /* incomplete set */ |
|
|
|
/* generate offsets into symbol table for each length for sorting */ |
|
offs[1] = 0; |
|
for (len = 1; len <= MAX_HUFBITS; len++) |
|
offs[len + 1] = offs[len] + count[len]; |
|
offs[0] = offs[MAX_HUFBITS + 1]; |
|
|
|
/* sort symbols by length, by symbol order within each length */ |
|
for (sym = 0; sym < codeSize; sym++) |
|
symByLen[offs[lens[sym]]++] = (unsigned short)sym; |
|
|
|
/* |
|
Create and fill in decoding tables. In this loop, the table being |
|
filled is at next and has curr index bits. The code being used is hufCode |
|
with length len. That code is converted to an index by dropping drop |
|
bits off of the bottom. For codes where len is less than drop + curr, |
|
those top drop + curr - len bits are incremented through all values to |
|
fill the table with replicated entries. |
|
|
|
root is the number of index bits for the root table. When len exceeds |
|
root, sub-tables are created pointed to by the root entry with an index |
|
of the low root bits of hufCode. This is saved in low to check for when a |
|
new sub-table should be started. drop is zero when the root table is |
|
being filled, and drop is root when sub-tables are being filled. |
|
|
|
When a new sub-table is needed, it is necessary to look ahead in the |
|
code lengths to determine what size sub-table is needed. The length |
|
counts are used for this, and so count[] is decremented as codes are |
|
entered in the tables. |
|
|
|
used keeps track of how many table entries have been allocated from the |
|
provided *table space. It is checked for LENS and DIST tables against |
|
the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in |
|
the initial root table size constants. See the comments in inftrees.h |
|
for more information. |
|
|
|
sym increments through all symbols, and the loop terminates when |
|
all codes of length max, i.e. all codes, have been processed. This |
|
routine permits incomplete codes, so another loop after this one fills |
|
in the rest of the decoding tables with invalid code markers. |
|
*/ |
|
|
|
|
|
/* initialize state for loop */ |
|
hufCode = 0; /* starting code */ |
|
sym = 0; /* starting code symbol */ |
|
len = min; /* starting code length */ |
|
curr = root; /* current table index bits */ |
|
drop = 0; /* current bits to drop from code for index */ |
|
next = rootTable; /* current table to fill in */ |
|
low = (unsigned)(-1); /* trigger new sub-table when len > root */ |
|
used = 1U << root; /* use root table entries */ |
|
mask = used - 1; /* mask for comparing low */ |
|
|
|
/* process all codes and make table entries */ |
|
while (1) { |
|
/* create table entry */ |
|
here.hufBits = (unsigned char)len; |
|
if (symByLen[sym] + 1U < match) { |
|
here.allBits = (unsigned char)len; |
|
here.val = 0x8000 ^ symByLen[sym]; |
|
} |
|
else if (symByLen[sym] >= match) { |
|
here.val = base[symByLen[sym] - match]; |
|
here.allBits = len + (unsigned char)(extra[symByLen[sym] - match]); |
|
} |
|
else { |
|
here.val = 0xFFFF; /* end of block */ |
|
here.allBits = (unsigned char)len; |
|
} |
|
|
|
/* replicate for those indices with low len bits equal to hufCode */ |
|
incr = 1U << (len - drop); |
|
fill = 1U << curr; |
|
min = fill; /* save offset to next table */ |
|
do { |
|
fill -= incr; |
|
next[(hufCode >> drop) + fill] = here; |
|
} while (fill != 0); |
|
|
|
/* backwards increment the len-bit code hufCode */ |
|
incr = 1U << (len - 1); |
|
while (hufCode & incr) |
|
incr >>= 1; |
|
if (incr != 0) { |
|
hufCode &= incr - 1; |
|
hufCode += incr; |
|
} |
|
else |
|
hufCode = 0; |
|
|
|
/* go to next symbol, update count, len */ |
|
sym++; |
|
if (--(count[len]) == 0) { |
|
if (len == maxHufBits) break; |
|
len = lens[symByLen[sym]]; |
|
} |
|
|
|
/* create new sub-table if needed */ |
|
if (len > root && (hufCode & mask) != low) { |
|
/* increment past last table */ |
|
next += min; /* here min is 1 << curr */ |
|
if (drop == 0) { /* if first time, transition to sub-tables */ |
|
drop = root; |
|
next = extTable; |
|
} |
|
|
|
/* determine length of next table */ |
|
curr = len - drop; |
|
left = (int)(1 << curr); |
|
while (curr + drop < maxHufBits) { |
|
left -= count[curr + drop]; |
|
if (left <= 0) break; |
|
curr++; |
|
left <<= 1; |
|
} |
|
used += 1U << curr; |
|
/* check for enough space */ |
|
if ((type == LENS && used > (1 << root) + ENOUGH_LENS) || |
|
(type == DISTS && used > (1 << root) + ENOUGH_DISTS)) |
|
return 1; |
|
|
|
/* point entry in root table to sub-table */ |
|
low = hufCode & mask; |
|
rootTable[low].val = (unsigned short)(next - extTable); |
|
rootTable[low].allBits = 0; /* curr + root + 128; 1<<7 is the indicator bit */ |
|
rootTable[low].hufBits = (1 << curr) - 1; /* repurposed as mask */ |
|
while ((low & rootMask) != rootMask) |
|
rootMask >>= 1; |
|
} |
|
} |
|
|
|
/* fill in remaining table entry if code is incomplete (guaranteed to have |
|
at most one remaining entry, since if the code is incomplete, the |
|
maximum code length that was allowed to get this far is one bit) */ |
|
if (hufCode != 0) { |
|
here.val = 0xFF00; |
|
here.allBits = maxHufBits; /* invalid code marker */ |
|
here.hufBits = maxHufBits; |
|
next[hufCode] = here; |
|
} |
|
|
|
/* set return parameters */ |
|
*rootMaskPtr = rootMask; |
|
return 0; |
|
} |
|
|
|
|