162306a36Sopenharmony_ci/* inftrees.c -- generate Huffman trees for efficient decoding 262306a36Sopenharmony_ci * Copyright (C) 1995-2005 Mark Adler 362306a36Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/zutil.h> 762306a36Sopenharmony_ci#include "inftrees.h" 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#define MAXBITS 15 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/* 1262306a36Sopenharmony_ci Build a set of tables to decode the provided canonical Huffman code. 1362306a36Sopenharmony_ci The code lengths are lens[0..codes-1]. The result starts at *table, 1462306a36Sopenharmony_ci whose indices are 0..2^bits-1. work is a writable array of at least 1562306a36Sopenharmony_ci lens shorts, which is used as a work area. type is the type of code 1662306a36Sopenharmony_ci to be generated, CODES, LENS, or DISTS. On return, zero is success, 1762306a36Sopenharmony_ci -1 is an invalid code, and +1 means that ENOUGH isn't enough. table 1862306a36Sopenharmony_ci on return points to the next available entry's address. bits is the 1962306a36Sopenharmony_ci requested root table index bits, and on return it is the actual root 2062306a36Sopenharmony_ci table index bits. It will differ if the request is greater than the 2162306a36Sopenharmony_ci longest code or if it is less than the shortest code. 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ciint zlib_inflate_table(codetype type, unsigned short *lens, unsigned codes, 2462306a36Sopenharmony_ci code **table, unsigned *bits, unsigned short *work) 2562306a36Sopenharmony_ci{ 2662306a36Sopenharmony_ci unsigned len; /* a code's length in bits */ 2762306a36Sopenharmony_ci unsigned sym; /* index of code symbols */ 2862306a36Sopenharmony_ci unsigned min, max; /* minimum and maximum code lengths */ 2962306a36Sopenharmony_ci unsigned root; /* number of index bits for root table */ 3062306a36Sopenharmony_ci unsigned curr; /* number of index bits for current table */ 3162306a36Sopenharmony_ci unsigned drop; /* code bits to drop for sub-table */ 3262306a36Sopenharmony_ci int left; /* number of prefix codes available */ 3362306a36Sopenharmony_ci unsigned used; /* code entries in table used */ 3462306a36Sopenharmony_ci unsigned huff; /* Huffman code */ 3562306a36Sopenharmony_ci unsigned incr; /* for incrementing code, index */ 3662306a36Sopenharmony_ci unsigned fill; /* index for replicating entries */ 3762306a36Sopenharmony_ci unsigned low; /* low bits for current root entry */ 3862306a36Sopenharmony_ci unsigned mask; /* mask for low root bits */ 3962306a36Sopenharmony_ci code this; /* table entry for duplication */ 4062306a36Sopenharmony_ci code *next; /* next available space in table */ 4162306a36Sopenharmony_ci const unsigned short *base; /* base value table to use */ 4262306a36Sopenharmony_ci const unsigned short *extra; /* extra bits table to use */ 4362306a36Sopenharmony_ci int end; /* use base and extra for symbol > end */ 4462306a36Sopenharmony_ci unsigned short count[MAXBITS+1]; /* number of codes of each length */ 4562306a36Sopenharmony_ci unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ 4662306a36Sopenharmony_ci static const unsigned short lbase[31] = { /* Length codes 257..285 base */ 4762306a36Sopenharmony_ci 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 4862306a36Sopenharmony_ci 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 4962306a36Sopenharmony_ci static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 5062306a36Sopenharmony_ci 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 5162306a36Sopenharmony_ci 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196}; 5262306a36Sopenharmony_ci static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 5362306a36Sopenharmony_ci 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 5462306a36Sopenharmony_ci 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 5562306a36Sopenharmony_ci 8193, 12289, 16385, 24577, 0, 0}; 5662306a36Sopenharmony_ci static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ 5762306a36Sopenharmony_ci 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 5862306a36Sopenharmony_ci 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 5962306a36Sopenharmony_ci 28, 28, 29, 29, 64, 64}; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci /* 6262306a36Sopenharmony_ci Process a set of code lengths to create a canonical Huffman code. The 6362306a36Sopenharmony_ci code lengths are lens[0..codes-1]. Each length corresponds to the 6462306a36Sopenharmony_ci symbols 0..codes-1. The Huffman code is generated by first sorting the 6562306a36Sopenharmony_ci symbols by length from short to long, and retaining the symbol order 6662306a36Sopenharmony_ci for codes with equal lengths. Then the code starts with all zero bits 6762306a36Sopenharmony_ci for the first code of the shortest length, and the codes are integer 6862306a36Sopenharmony_ci increments for the same length, and zeros are appended as the length 6962306a36Sopenharmony_ci increases. For the deflate format, these bits are stored backwards 7062306a36Sopenharmony_ci from their more natural integer increment ordering, and so when the 7162306a36Sopenharmony_ci decoding tables are built in the large loop below, the integer codes 7262306a36Sopenharmony_ci are incremented backwards. 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci This routine assumes, but does not check, that all of the entries in 7562306a36Sopenharmony_ci lens[] are in the range 0..MAXBITS. The caller must assure this. 7662306a36Sopenharmony_ci 1..MAXBITS is interpreted as that code length. zero means that that 7762306a36Sopenharmony_ci symbol does not occur in this code. 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci The codes are sorted by computing a count of codes for each length, 8062306a36Sopenharmony_ci creating from that a table of starting indices for each length in the 8162306a36Sopenharmony_ci sorted table, and then entering the symbols in order in the sorted 8262306a36Sopenharmony_ci table. The sorted table is work[], with that space being provided by 8362306a36Sopenharmony_ci the caller. 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci The length counts are used for other purposes as well, i.e. finding 8662306a36Sopenharmony_ci the minimum and maximum length codes, determining if there are any 8762306a36Sopenharmony_ci codes at all, checking for a valid set of lengths, and looking ahead 8862306a36Sopenharmony_ci at length counts to determine sub-table sizes when building the 8962306a36Sopenharmony_ci decoding tables. 9062306a36Sopenharmony_ci */ 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ 9362306a36Sopenharmony_ci for (len = 0; len <= MAXBITS; len++) 9462306a36Sopenharmony_ci count[len] = 0; 9562306a36Sopenharmony_ci for (sym = 0; sym < codes; sym++) 9662306a36Sopenharmony_ci count[lens[sym]]++; 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci /* bound code lengths, force root to be within code lengths */ 9962306a36Sopenharmony_ci root = *bits; 10062306a36Sopenharmony_ci for (max = MAXBITS; max >= 1; max--) 10162306a36Sopenharmony_ci if (count[max] != 0) break; 10262306a36Sopenharmony_ci if (root > max) root = max; 10362306a36Sopenharmony_ci if (max == 0) { /* no symbols to code at all */ 10462306a36Sopenharmony_ci this.op = (unsigned char)64; /* invalid code marker */ 10562306a36Sopenharmony_ci this.bits = (unsigned char)1; 10662306a36Sopenharmony_ci this.val = (unsigned short)0; 10762306a36Sopenharmony_ci *(*table)++ = this; /* make a table to force an error */ 10862306a36Sopenharmony_ci *(*table)++ = this; 10962306a36Sopenharmony_ci *bits = 1; 11062306a36Sopenharmony_ci return 0; /* no symbols, but wait for decoding to report error */ 11162306a36Sopenharmony_ci } 11262306a36Sopenharmony_ci for (min = 1; min < MAXBITS; min++) 11362306a36Sopenharmony_ci if (count[min] != 0) break; 11462306a36Sopenharmony_ci if (root < min) root = min; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci /* check for an over-subscribed or incomplete set of lengths */ 11762306a36Sopenharmony_ci left = 1; 11862306a36Sopenharmony_ci for (len = 1; len <= MAXBITS; len++) { 11962306a36Sopenharmony_ci left <<= 1; 12062306a36Sopenharmony_ci left -= count[len]; 12162306a36Sopenharmony_ci if (left < 0) return -1; /* over-subscribed */ 12262306a36Sopenharmony_ci } 12362306a36Sopenharmony_ci if (left > 0 && (type == CODES || max != 1)) 12462306a36Sopenharmony_ci return -1; /* incomplete set */ 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci /* generate offsets into symbol table for each length for sorting */ 12762306a36Sopenharmony_ci offs[1] = 0; 12862306a36Sopenharmony_ci for (len = 1; len < MAXBITS; len++) 12962306a36Sopenharmony_ci offs[len + 1] = offs[len] + count[len]; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci /* sort symbols by length, by symbol order within each length */ 13262306a36Sopenharmony_ci for (sym = 0; sym < codes; sym++) 13362306a36Sopenharmony_ci if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci /* 13662306a36Sopenharmony_ci Create and fill in decoding tables. In this loop, the table being 13762306a36Sopenharmony_ci filled is at next and has curr index bits. The code being used is huff 13862306a36Sopenharmony_ci with length len. That code is converted to an index by dropping drop 13962306a36Sopenharmony_ci bits off of the bottom. For codes where len is less than drop + curr, 14062306a36Sopenharmony_ci those top drop + curr - len bits are incremented through all values to 14162306a36Sopenharmony_ci fill the table with replicated entries. 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci root is the number of index bits for the root table. When len exceeds 14462306a36Sopenharmony_ci root, sub-tables are created pointed to by the root entry with an index 14562306a36Sopenharmony_ci of the low root bits of huff. This is saved in low to check for when a 14662306a36Sopenharmony_ci new sub-table should be started. drop is zero when the root table is 14762306a36Sopenharmony_ci being filled, and drop is root when sub-tables are being filled. 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci When a new sub-table is needed, it is necessary to look ahead in the 15062306a36Sopenharmony_ci code lengths to determine what size sub-table is needed. The length 15162306a36Sopenharmony_ci counts are used for this, and so count[] is decremented as codes are 15262306a36Sopenharmony_ci entered in the tables. 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci used keeps track of how many table entries have been allocated from the 15562306a36Sopenharmony_ci provided *table space. It is checked when a LENS table is being made 15662306a36Sopenharmony_ci against the space in *table, ENOUGH, minus the maximum space needed by 15762306a36Sopenharmony_ci the worst case distance code, MAXD. This should never happen, but the 15862306a36Sopenharmony_ci sufficiency of ENOUGH has not been proven exhaustively, hence the check. 15962306a36Sopenharmony_ci This assumes that when type == LENS, bits == 9. 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci sym increments through all symbols, and the loop terminates when 16262306a36Sopenharmony_ci all codes of length max, i.e. all codes, have been processed. This 16362306a36Sopenharmony_ci routine permits incomplete codes, so another loop after this one fills 16462306a36Sopenharmony_ci in the rest of the decoding tables with invalid code markers. 16562306a36Sopenharmony_ci */ 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci /* set up for code type */ 16862306a36Sopenharmony_ci switch (type) { 16962306a36Sopenharmony_ci case CODES: 17062306a36Sopenharmony_ci base = extra = work; /* dummy value--not used */ 17162306a36Sopenharmony_ci end = 19; 17262306a36Sopenharmony_ci break; 17362306a36Sopenharmony_ci case LENS: 17462306a36Sopenharmony_ci base = lbase; 17562306a36Sopenharmony_ci base -= 257; 17662306a36Sopenharmony_ci extra = lext; 17762306a36Sopenharmony_ci extra -= 257; 17862306a36Sopenharmony_ci end = 256; 17962306a36Sopenharmony_ci break; 18062306a36Sopenharmony_ci default: /* DISTS */ 18162306a36Sopenharmony_ci base = dbase; 18262306a36Sopenharmony_ci extra = dext; 18362306a36Sopenharmony_ci end = -1; 18462306a36Sopenharmony_ci } 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci /* initialize state for loop */ 18762306a36Sopenharmony_ci huff = 0; /* starting code */ 18862306a36Sopenharmony_ci sym = 0; /* starting code symbol */ 18962306a36Sopenharmony_ci len = min; /* starting code length */ 19062306a36Sopenharmony_ci next = *table; /* current table to fill in */ 19162306a36Sopenharmony_ci curr = root; /* current table index bits */ 19262306a36Sopenharmony_ci drop = 0; /* current bits to drop from code for index */ 19362306a36Sopenharmony_ci low = (unsigned)(-1); /* trigger new sub-table when len > root */ 19462306a36Sopenharmony_ci used = 1U << root; /* use root table entries */ 19562306a36Sopenharmony_ci mask = used - 1; /* mask for comparing low */ 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci /* check available table space */ 19862306a36Sopenharmony_ci if (type == LENS && used >= ENOUGH - MAXD) 19962306a36Sopenharmony_ci return 1; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci /* process all codes and make table entries */ 20262306a36Sopenharmony_ci for (;;) { 20362306a36Sopenharmony_ci /* create table entry */ 20462306a36Sopenharmony_ci this.bits = (unsigned char)(len - drop); 20562306a36Sopenharmony_ci if ((int)(work[sym]) < end) { 20662306a36Sopenharmony_ci this.op = (unsigned char)0; 20762306a36Sopenharmony_ci this.val = work[sym]; 20862306a36Sopenharmony_ci } 20962306a36Sopenharmony_ci else if ((int)(work[sym]) > end) { 21062306a36Sopenharmony_ci this.op = (unsigned char)(extra[work[sym]]); 21162306a36Sopenharmony_ci this.val = base[work[sym]]; 21262306a36Sopenharmony_ci } 21362306a36Sopenharmony_ci else { 21462306a36Sopenharmony_ci this.op = (unsigned char)(32 + 64); /* end of block */ 21562306a36Sopenharmony_ci this.val = 0; 21662306a36Sopenharmony_ci } 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci /* replicate for those indices with low len bits equal to huff */ 21962306a36Sopenharmony_ci incr = 1U << (len - drop); 22062306a36Sopenharmony_ci fill = 1U << curr; 22162306a36Sopenharmony_ci min = fill; /* save offset to next table */ 22262306a36Sopenharmony_ci do { 22362306a36Sopenharmony_ci fill -= incr; 22462306a36Sopenharmony_ci next[(huff >> drop) + fill] = this; 22562306a36Sopenharmony_ci } while (fill != 0); 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci /* backwards increment the len-bit code huff */ 22862306a36Sopenharmony_ci incr = 1U << (len - 1); 22962306a36Sopenharmony_ci while (huff & incr) 23062306a36Sopenharmony_ci incr >>= 1; 23162306a36Sopenharmony_ci if (incr != 0) { 23262306a36Sopenharmony_ci huff &= incr - 1; 23362306a36Sopenharmony_ci huff += incr; 23462306a36Sopenharmony_ci } 23562306a36Sopenharmony_ci else 23662306a36Sopenharmony_ci huff = 0; 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci /* go to next symbol, update count, len */ 23962306a36Sopenharmony_ci sym++; 24062306a36Sopenharmony_ci if (--(count[len]) == 0) { 24162306a36Sopenharmony_ci if (len == max) break; 24262306a36Sopenharmony_ci len = lens[work[sym]]; 24362306a36Sopenharmony_ci } 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci /* create new sub-table if needed */ 24662306a36Sopenharmony_ci if (len > root && (huff & mask) != low) { 24762306a36Sopenharmony_ci /* if first time, transition to sub-tables */ 24862306a36Sopenharmony_ci if (drop == 0) 24962306a36Sopenharmony_ci drop = root; 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci /* increment past last table */ 25262306a36Sopenharmony_ci next += min; /* here min is 1 << curr */ 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci /* determine length of next table */ 25562306a36Sopenharmony_ci curr = len - drop; 25662306a36Sopenharmony_ci left = (int)(1 << curr); 25762306a36Sopenharmony_ci while (curr + drop < max) { 25862306a36Sopenharmony_ci left -= count[curr + drop]; 25962306a36Sopenharmony_ci if (left <= 0) break; 26062306a36Sopenharmony_ci curr++; 26162306a36Sopenharmony_ci left <<= 1; 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci /* check for enough space */ 26562306a36Sopenharmony_ci used += 1U << curr; 26662306a36Sopenharmony_ci if (type == LENS && used >= ENOUGH - MAXD) 26762306a36Sopenharmony_ci return 1; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci /* point entry in root table to sub-table */ 27062306a36Sopenharmony_ci low = huff & mask; 27162306a36Sopenharmony_ci (*table)[low].op = (unsigned char)curr; 27262306a36Sopenharmony_ci (*table)[low].bits = (unsigned char)root; 27362306a36Sopenharmony_ci (*table)[low].val = (unsigned short)(next - *table); 27462306a36Sopenharmony_ci } 27562306a36Sopenharmony_ci } 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci /* 27862306a36Sopenharmony_ci Fill in rest of table for incomplete codes. This loop is similar to the 27962306a36Sopenharmony_ci loop above in incrementing huff for table indices. It is assumed that 28062306a36Sopenharmony_ci len is equal to curr + drop, so there is no loop needed to increment 28162306a36Sopenharmony_ci through high index bits. When the current sub-table is filled, the loop 28262306a36Sopenharmony_ci drops back to the root table to fill in any remaining entries there. 28362306a36Sopenharmony_ci */ 28462306a36Sopenharmony_ci this.op = (unsigned char)64; /* invalid code marker */ 28562306a36Sopenharmony_ci this.bits = (unsigned char)(len - drop); 28662306a36Sopenharmony_ci this.val = (unsigned short)0; 28762306a36Sopenharmony_ci while (huff != 0) { 28862306a36Sopenharmony_ci /* when done with sub-table, drop back to root table */ 28962306a36Sopenharmony_ci if (drop != 0 && (huff & mask) != low) { 29062306a36Sopenharmony_ci drop = 0; 29162306a36Sopenharmony_ci len = root; 29262306a36Sopenharmony_ci next = *table; 29362306a36Sopenharmony_ci this.bits = (unsigned char)len; 29462306a36Sopenharmony_ci } 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci /* put invalid code marker in table */ 29762306a36Sopenharmony_ci next[huff >> drop] = this; 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ci /* backwards increment the len-bit code huff */ 30062306a36Sopenharmony_ci incr = 1U << (len - 1); 30162306a36Sopenharmony_ci while (huff & incr) 30262306a36Sopenharmony_ci incr >>= 1; 30362306a36Sopenharmony_ci if (incr != 0) { 30462306a36Sopenharmony_ci huff &= incr - 1; 30562306a36Sopenharmony_ci huff += incr; 30662306a36Sopenharmony_ci } 30762306a36Sopenharmony_ci else 30862306a36Sopenharmony_ci huff = 0; 30962306a36Sopenharmony_ci } 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci /* set return parameters */ 31262306a36Sopenharmony_ci *table += used; 31362306a36Sopenharmony_ci *bits = root; 31462306a36Sopenharmony_ci return 0; 31562306a36Sopenharmony_ci} 316