11cb0ef41Sopenharmony_ci/* inftrees.c -- generate Huffman trees for efficient decoding 21cb0ef41Sopenharmony_ci * Copyright (C) 1995-2023 Mark Adler 31cb0ef41Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h 41cb0ef41Sopenharmony_ci */ 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci#include "zutil.h" 71cb0ef41Sopenharmony_ci#include "inftrees.h" 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci#define MAXBITS 15 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst char inflate_copyright[] = 121cb0ef41Sopenharmony_ci " inflate 1.3.0.1 Copyright 1995-2023 Mark Adler "; 131cb0ef41Sopenharmony_ci/* 141cb0ef41Sopenharmony_ci If you use the zlib library in a product, an acknowledgment is welcome 151cb0ef41Sopenharmony_ci in the documentation of your product. If for some reason you cannot 161cb0ef41Sopenharmony_ci include such an acknowledgment, I would appreciate that you keep this 171cb0ef41Sopenharmony_ci copyright string in the executable of your product. 181cb0ef41Sopenharmony_ci */ 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci/* 211cb0ef41Sopenharmony_ci Build a set of tables to decode the provided canonical Huffman code. 221cb0ef41Sopenharmony_ci The code lengths are lens[0..codes-1]. The result starts at *table, 231cb0ef41Sopenharmony_ci whose indices are 0..2^bits-1. work is a writable array of at least 241cb0ef41Sopenharmony_ci lens shorts, which is used as a work area. type is the type of code 251cb0ef41Sopenharmony_ci to be generated, CODES, LENS, or DISTS. On return, zero is success, 261cb0ef41Sopenharmony_ci -1 is an invalid code, and +1 means that ENOUGH isn't enough. table 271cb0ef41Sopenharmony_ci on return points to the next available entry's address. bits is the 281cb0ef41Sopenharmony_ci requested root table index bits, and on return it is the actual root 291cb0ef41Sopenharmony_ci table index bits. It will differ if the request is greater than the 301cb0ef41Sopenharmony_ci longest code or if it is less than the shortest code. 311cb0ef41Sopenharmony_ci */ 321cb0ef41Sopenharmony_ciint ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, 331cb0ef41Sopenharmony_ci unsigned codes, code FAR * FAR *table, 341cb0ef41Sopenharmony_ci unsigned FAR *bits, unsigned short FAR *work) { 351cb0ef41Sopenharmony_ci unsigned len; /* a code's length in bits */ 361cb0ef41Sopenharmony_ci unsigned sym; /* index of code symbols */ 371cb0ef41Sopenharmony_ci unsigned min, max; /* minimum and maximum code lengths */ 381cb0ef41Sopenharmony_ci unsigned root; /* number of index bits for root table */ 391cb0ef41Sopenharmony_ci unsigned curr; /* number of index bits for current table */ 401cb0ef41Sopenharmony_ci unsigned drop; /* code bits to drop for sub-table */ 411cb0ef41Sopenharmony_ci int left; /* number of prefix codes available */ 421cb0ef41Sopenharmony_ci unsigned used; /* code entries in table used */ 431cb0ef41Sopenharmony_ci unsigned huff; /* Huffman code */ 441cb0ef41Sopenharmony_ci unsigned incr; /* for incrementing code, index */ 451cb0ef41Sopenharmony_ci unsigned fill; /* index for replicating entries */ 461cb0ef41Sopenharmony_ci unsigned low; /* low bits for current root entry */ 471cb0ef41Sopenharmony_ci unsigned mask; /* mask for low root bits */ 481cb0ef41Sopenharmony_ci code here; /* table entry for duplication */ 491cb0ef41Sopenharmony_ci code FAR *next; /* next available space in table */ 501cb0ef41Sopenharmony_ci const unsigned short FAR *base; /* base value table to use */ 511cb0ef41Sopenharmony_ci const unsigned short FAR *extra; /* extra bits table to use */ 521cb0ef41Sopenharmony_ci unsigned match; /* use base and extra for symbol >= match */ 531cb0ef41Sopenharmony_ci unsigned short count[MAXBITS+1]; /* number of codes of each length */ 541cb0ef41Sopenharmony_ci unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ 551cb0ef41Sopenharmony_ci static const unsigned short lbase[31] = { /* Length codes 257..285 base */ 561cb0ef41Sopenharmony_ci 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 571cb0ef41Sopenharmony_ci 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 581cb0ef41Sopenharmony_ci static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 591cb0ef41Sopenharmony_ci 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 601cb0ef41Sopenharmony_ci 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 70, 200}; 611cb0ef41Sopenharmony_ci static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 621cb0ef41Sopenharmony_ci 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 631cb0ef41Sopenharmony_ci 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 641cb0ef41Sopenharmony_ci 8193, 12289, 16385, 24577, 0, 0}; 651cb0ef41Sopenharmony_ci static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ 661cb0ef41Sopenharmony_ci 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 671cb0ef41Sopenharmony_ci 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 681cb0ef41Sopenharmony_ci 28, 28, 29, 29, 64, 64}; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci /* 711cb0ef41Sopenharmony_ci Process a set of code lengths to create a canonical Huffman code. The 721cb0ef41Sopenharmony_ci code lengths are lens[0..codes-1]. Each length corresponds to the 731cb0ef41Sopenharmony_ci symbols 0..codes-1. The Huffman code is generated by first sorting the 741cb0ef41Sopenharmony_ci symbols by length from short to long, and retaining the symbol order 751cb0ef41Sopenharmony_ci for codes with equal lengths. Then the code starts with all zero bits 761cb0ef41Sopenharmony_ci for the first code of the shortest length, and the codes are integer 771cb0ef41Sopenharmony_ci increments for the same length, and zeros are appended as the length 781cb0ef41Sopenharmony_ci increases. For the deflate format, these bits are stored backwards 791cb0ef41Sopenharmony_ci from their more natural integer increment ordering, and so when the 801cb0ef41Sopenharmony_ci decoding tables are built in the large loop below, the integer codes 811cb0ef41Sopenharmony_ci are incremented backwards. 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci This routine assumes, but does not check, that all of the entries in 841cb0ef41Sopenharmony_ci lens[] are in the range 0..MAXBITS. The caller must assure this. 851cb0ef41Sopenharmony_ci 1..MAXBITS is interpreted as that code length. zero means that that 861cb0ef41Sopenharmony_ci symbol does not occur in this code. 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci The codes are sorted by computing a count of codes for each length, 891cb0ef41Sopenharmony_ci creating from that a table of starting indices for each length in the 901cb0ef41Sopenharmony_ci sorted table, and then entering the symbols in order in the sorted 911cb0ef41Sopenharmony_ci table. The sorted table is work[], with that space being provided by 921cb0ef41Sopenharmony_ci the caller. 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci The length counts are used for other purposes as well, i.e. finding 951cb0ef41Sopenharmony_ci the minimum and maximum length codes, determining if there are any 961cb0ef41Sopenharmony_ci codes at all, checking for a valid set of lengths, and looking ahead 971cb0ef41Sopenharmony_ci at length counts to determine sub-table sizes when building the 981cb0ef41Sopenharmony_ci decoding tables. 991cb0ef41Sopenharmony_ci */ 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ 1021cb0ef41Sopenharmony_ci for (len = 0; len <= MAXBITS; len++) 1031cb0ef41Sopenharmony_ci count[len] = 0; 1041cb0ef41Sopenharmony_ci for (sym = 0; sym < codes; sym++) 1051cb0ef41Sopenharmony_ci count[lens[sym]]++; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci /* bound code lengths, force root to be within code lengths */ 1081cb0ef41Sopenharmony_ci root = *bits; 1091cb0ef41Sopenharmony_ci for (max = MAXBITS; max >= 1; max--) 1101cb0ef41Sopenharmony_ci if (count[max] != 0) break; 1111cb0ef41Sopenharmony_ci if (root > max) root = max; 1121cb0ef41Sopenharmony_ci if (max == 0) { /* no symbols to code at all */ 1131cb0ef41Sopenharmony_ci here.op = (unsigned char)64; /* invalid code marker */ 1141cb0ef41Sopenharmony_ci here.bits = (unsigned char)1; 1151cb0ef41Sopenharmony_ci here.val = (unsigned short)0; 1161cb0ef41Sopenharmony_ci *(*table)++ = here; /* make a table to force an error */ 1171cb0ef41Sopenharmony_ci *(*table)++ = here; 1181cb0ef41Sopenharmony_ci *bits = 1; 1191cb0ef41Sopenharmony_ci return 0; /* no symbols, but wait for decoding to report error */ 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci for (min = 1; min < max; min++) 1221cb0ef41Sopenharmony_ci if (count[min] != 0) break; 1231cb0ef41Sopenharmony_ci if (root < min) root = min; 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci /* check for an over-subscribed or incomplete set of lengths */ 1261cb0ef41Sopenharmony_ci left = 1; 1271cb0ef41Sopenharmony_ci for (len = 1; len <= MAXBITS; len++) { 1281cb0ef41Sopenharmony_ci left <<= 1; 1291cb0ef41Sopenharmony_ci left -= count[len]; 1301cb0ef41Sopenharmony_ci if (left < 0) return -1; /* over-subscribed */ 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci if (left > 0 && (type == CODES || max != 1)) 1331cb0ef41Sopenharmony_ci return -1; /* incomplete set */ 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci /* generate offsets into symbol table for each length for sorting */ 1361cb0ef41Sopenharmony_ci offs[1] = 0; 1371cb0ef41Sopenharmony_ci for (len = 1; len < MAXBITS; len++) 1381cb0ef41Sopenharmony_ci offs[len + 1] = offs[len] + count[len]; 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci /* sort symbols by length, by symbol order within each length */ 1411cb0ef41Sopenharmony_ci for (sym = 0; sym < codes; sym++) 1421cb0ef41Sopenharmony_ci if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci /* 1451cb0ef41Sopenharmony_ci Create and fill in decoding tables. In this loop, the table being 1461cb0ef41Sopenharmony_ci filled is at next and has curr index bits. The code being used is huff 1471cb0ef41Sopenharmony_ci with length len. That code is converted to an index by dropping drop 1481cb0ef41Sopenharmony_ci bits off of the bottom. For codes where len is less than drop + curr, 1491cb0ef41Sopenharmony_ci those top drop + curr - len bits are incremented through all values to 1501cb0ef41Sopenharmony_ci fill the table with replicated entries. 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci root is the number of index bits for the root table. When len exceeds 1531cb0ef41Sopenharmony_ci root, sub-tables are created pointed to by the root entry with an index 1541cb0ef41Sopenharmony_ci of the low root bits of huff. This is saved in low to check for when a 1551cb0ef41Sopenharmony_ci new sub-table should be started. drop is zero when the root table is 1561cb0ef41Sopenharmony_ci being filled, and drop is root when sub-tables are being filled. 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci When a new sub-table is needed, it is necessary to look ahead in the 1591cb0ef41Sopenharmony_ci code lengths to determine what size sub-table is needed. The length 1601cb0ef41Sopenharmony_ci counts are used for this, and so count[] is decremented as codes are 1611cb0ef41Sopenharmony_ci entered in the tables. 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci used keeps track of how many table entries have been allocated from the 1641cb0ef41Sopenharmony_ci provided *table space. It is checked for LENS and DIST tables against 1651cb0ef41Sopenharmony_ci the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in 1661cb0ef41Sopenharmony_ci the initial root table size constants. See the comments in inftrees.h 1671cb0ef41Sopenharmony_ci for more information. 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci sym increments through all symbols, and the loop terminates when 1701cb0ef41Sopenharmony_ci all codes of length max, i.e. all codes, have been processed. This 1711cb0ef41Sopenharmony_ci routine permits incomplete codes, so another loop after this one fills 1721cb0ef41Sopenharmony_ci in the rest of the decoding tables with invalid code markers. 1731cb0ef41Sopenharmony_ci */ 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci /* set up for code type */ 1761cb0ef41Sopenharmony_ci switch (type) { 1771cb0ef41Sopenharmony_ci case CODES: 1781cb0ef41Sopenharmony_ci base = extra = work; /* dummy value--not used */ 1791cb0ef41Sopenharmony_ci match = 20; 1801cb0ef41Sopenharmony_ci break; 1811cb0ef41Sopenharmony_ci case LENS: 1821cb0ef41Sopenharmony_ci base = lbase; 1831cb0ef41Sopenharmony_ci extra = lext; 1841cb0ef41Sopenharmony_ci match = 257; 1851cb0ef41Sopenharmony_ci break; 1861cb0ef41Sopenharmony_ci default: /* DISTS */ 1871cb0ef41Sopenharmony_ci base = dbase; 1881cb0ef41Sopenharmony_ci extra = dext; 1891cb0ef41Sopenharmony_ci match = 0; 1901cb0ef41Sopenharmony_ci } 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci /* initialize state for loop */ 1931cb0ef41Sopenharmony_ci huff = 0; /* starting code */ 1941cb0ef41Sopenharmony_ci sym = 0; /* starting code symbol */ 1951cb0ef41Sopenharmony_ci len = min; /* starting code length */ 1961cb0ef41Sopenharmony_ci next = *table; /* current table to fill in */ 1971cb0ef41Sopenharmony_ci curr = root; /* current table index bits */ 1981cb0ef41Sopenharmony_ci drop = 0; /* current bits to drop from code for index */ 1991cb0ef41Sopenharmony_ci low = (unsigned)(-1); /* trigger new sub-table when len > root */ 2001cb0ef41Sopenharmony_ci used = 1U << root; /* use root table entries */ 2011cb0ef41Sopenharmony_ci mask = used - 1; /* mask for comparing low */ 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci /* check available table space */ 2041cb0ef41Sopenharmony_ci if ((type == LENS && used > ENOUGH_LENS) || 2051cb0ef41Sopenharmony_ci (type == DISTS && used > ENOUGH_DISTS)) 2061cb0ef41Sopenharmony_ci return 1; 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci /* process all codes and make table entries */ 2091cb0ef41Sopenharmony_ci for (;;) { 2101cb0ef41Sopenharmony_ci /* create table entry */ 2111cb0ef41Sopenharmony_ci here.bits = (unsigned char)(len - drop); 2121cb0ef41Sopenharmony_ci if (work[sym] + 1U < match) { 2131cb0ef41Sopenharmony_ci here.op = (unsigned char)0; 2141cb0ef41Sopenharmony_ci here.val = work[sym]; 2151cb0ef41Sopenharmony_ci } 2161cb0ef41Sopenharmony_ci else if (work[sym] >= match) { 2171cb0ef41Sopenharmony_ci here.op = (unsigned char)(extra[work[sym] - match]); 2181cb0ef41Sopenharmony_ci here.val = base[work[sym] - match]; 2191cb0ef41Sopenharmony_ci } 2201cb0ef41Sopenharmony_ci else { 2211cb0ef41Sopenharmony_ci here.op = (unsigned char)(32 + 64); /* end of block */ 2221cb0ef41Sopenharmony_ci here.val = 0; 2231cb0ef41Sopenharmony_ci } 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci /* replicate for those indices with low len bits equal to huff */ 2261cb0ef41Sopenharmony_ci incr = 1U << (len - drop); 2271cb0ef41Sopenharmony_ci fill = 1U << curr; 2281cb0ef41Sopenharmony_ci min = fill; /* save offset to next table */ 2291cb0ef41Sopenharmony_ci do { 2301cb0ef41Sopenharmony_ci fill -= incr; 2311cb0ef41Sopenharmony_ci next[(huff >> drop) + fill] = here; 2321cb0ef41Sopenharmony_ci } while (fill != 0); 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci /* backwards increment the len-bit code huff */ 2351cb0ef41Sopenharmony_ci incr = 1U << (len - 1); 2361cb0ef41Sopenharmony_ci while (huff & incr) 2371cb0ef41Sopenharmony_ci incr >>= 1; 2381cb0ef41Sopenharmony_ci if (incr != 0) { 2391cb0ef41Sopenharmony_ci huff &= incr - 1; 2401cb0ef41Sopenharmony_ci huff += incr; 2411cb0ef41Sopenharmony_ci } 2421cb0ef41Sopenharmony_ci else 2431cb0ef41Sopenharmony_ci huff = 0; 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci /* go to next symbol, update count, len */ 2461cb0ef41Sopenharmony_ci sym++; 2471cb0ef41Sopenharmony_ci if (--(count[len]) == 0) { 2481cb0ef41Sopenharmony_ci if (len == max) break; 2491cb0ef41Sopenharmony_ci len = lens[work[sym]]; 2501cb0ef41Sopenharmony_ci } 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci /* create new sub-table if needed */ 2531cb0ef41Sopenharmony_ci if (len > root && (huff & mask) != low) { 2541cb0ef41Sopenharmony_ci /* if first time, transition to sub-tables */ 2551cb0ef41Sopenharmony_ci if (drop == 0) 2561cb0ef41Sopenharmony_ci drop = root; 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci /* increment past last table */ 2591cb0ef41Sopenharmony_ci next += min; /* here min is 1 << curr */ 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ci /* determine length of next table */ 2621cb0ef41Sopenharmony_ci curr = len - drop; 2631cb0ef41Sopenharmony_ci left = (int)(1 << curr); 2641cb0ef41Sopenharmony_ci while (curr + drop < max) { 2651cb0ef41Sopenharmony_ci left -= count[curr + drop]; 2661cb0ef41Sopenharmony_ci if (left <= 0) break; 2671cb0ef41Sopenharmony_ci curr++; 2681cb0ef41Sopenharmony_ci left <<= 1; 2691cb0ef41Sopenharmony_ci } 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci /* check for enough space */ 2721cb0ef41Sopenharmony_ci used += 1U << curr; 2731cb0ef41Sopenharmony_ci if ((type == LENS && used > ENOUGH_LENS) || 2741cb0ef41Sopenharmony_ci (type == DISTS && used > ENOUGH_DISTS)) 2751cb0ef41Sopenharmony_ci return 1; 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci /* point entry in root table to sub-table */ 2781cb0ef41Sopenharmony_ci low = huff & mask; 2791cb0ef41Sopenharmony_ci (*table)[low].op = (unsigned char)curr; 2801cb0ef41Sopenharmony_ci (*table)[low].bits = (unsigned char)root; 2811cb0ef41Sopenharmony_ci (*table)[low].val = (unsigned short)(next - *table); 2821cb0ef41Sopenharmony_ci } 2831cb0ef41Sopenharmony_ci } 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ci /* fill in remaining table entry if code is incomplete (guaranteed to have 2861cb0ef41Sopenharmony_ci at most one remaining entry, since if the code is incomplete, the 2871cb0ef41Sopenharmony_ci maximum code length that was allowed to get this far is one bit) */ 2881cb0ef41Sopenharmony_ci if (huff != 0) { 2891cb0ef41Sopenharmony_ci here.op = (unsigned char)64; /* invalid code marker */ 2901cb0ef41Sopenharmony_ci here.bits = (unsigned char)(len - drop); 2911cb0ef41Sopenharmony_ci here.val = (unsigned short)0; 2921cb0ef41Sopenharmony_ci next[huff] = here; 2931cb0ef41Sopenharmony_ci } 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci /* set return parameters */ 2961cb0ef41Sopenharmony_ci *table += used; 2971cb0ef41Sopenharmony_ci *bits = root; 2981cb0ef41Sopenharmony_ci return 0; 2991cb0ef41Sopenharmony_ci} 300