1221b1638SMasahiro Yamada /* inftrees.c -- generate Huffman trees for efficient decoding 2*a8c877cbSChris Kay * Copyright (C) 1995-2024 Mark Adler 3221b1638SMasahiro Yamada * For conditions of distribution and use, see copyright notice in zlib.h 4221b1638SMasahiro Yamada */ 5221b1638SMasahiro Yamada 6221b1638SMasahiro Yamada #include "zutil.h" 7221b1638SMasahiro Yamada #include "inftrees.h" 8221b1638SMasahiro Yamada 9221b1638SMasahiro Yamada #define MAXBITS 15 10221b1638SMasahiro Yamada 11221b1638SMasahiro Yamada const char inflate_copyright[] = 12*a8c877cbSChris Kay " inflate 1.3.1.1 Copyright 1995-2024 Mark Adler "; 13221b1638SMasahiro Yamada /* 14221b1638SMasahiro Yamada If you use the zlib library in a product, an acknowledgment is welcome 15221b1638SMasahiro Yamada in the documentation of your product. If for some reason you cannot 16221b1638SMasahiro Yamada include such an acknowledgment, I would appreciate that you keep this 17221b1638SMasahiro Yamada copyright string in the executable of your product. 18221b1638SMasahiro Yamada */ 19221b1638SMasahiro Yamada 20221b1638SMasahiro Yamada /* 21221b1638SMasahiro Yamada Build a set of tables to decode the provided canonical Huffman code. 22221b1638SMasahiro Yamada The code lengths are lens[0..codes-1]. The result starts at *table, 23221b1638SMasahiro Yamada whose indices are 0..2^bits-1. work is a writable array of at least 24221b1638SMasahiro Yamada lens shorts, which is used as a work area. type is the type of code 25221b1638SMasahiro Yamada to be generated, CODES, LENS, or DISTS. On return, zero is success, 26221b1638SMasahiro Yamada -1 is an invalid code, and +1 means that ENOUGH isn't enough. table 27221b1638SMasahiro Yamada on return points to the next available entry's address. bits is the 28221b1638SMasahiro Yamada requested root table index bits, and on return it is the actual root 29221b1638SMasahiro Yamada table index bits. It will differ if the request is greater than the 30221b1638SMasahiro Yamada longest code or if it is less than the shortest code. 31221b1638SMasahiro Yamada */ 32fd39217aSManish Pandey int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, 33fd39217aSManish Pandey unsigned codes, code FAR * FAR *table, 34fd39217aSManish Pandey unsigned FAR *bits, unsigned short FAR *work) { 35221b1638SMasahiro Yamada unsigned len; /* a code's length in bits */ 36221b1638SMasahiro Yamada unsigned sym; /* index of code symbols */ 37221b1638SMasahiro Yamada unsigned min, max; /* minimum and maximum code lengths */ 38221b1638SMasahiro Yamada unsigned root; /* number of index bits for root table */ 39221b1638SMasahiro Yamada unsigned curr; /* number of index bits for current table */ 40221b1638SMasahiro Yamada unsigned drop; /* code bits to drop for sub-table */ 41221b1638SMasahiro Yamada int left; /* number of prefix codes available */ 42221b1638SMasahiro Yamada unsigned used; /* code entries in table used */ 43221b1638SMasahiro Yamada unsigned huff; /* Huffman code */ 44221b1638SMasahiro Yamada unsigned incr; /* for incrementing code, index */ 45221b1638SMasahiro Yamada unsigned fill; /* index for replicating entries */ 46221b1638SMasahiro Yamada unsigned low; /* low bits for current root entry */ 47221b1638SMasahiro Yamada unsigned mask; /* mask for low root bits */ 48221b1638SMasahiro Yamada code here; /* table entry for duplication */ 49221b1638SMasahiro Yamada code FAR *next; /* next available space in table */ 50221b1638SMasahiro Yamada const unsigned short FAR *base; /* base value table to use */ 51221b1638SMasahiro Yamada const unsigned short FAR *extra; /* extra bits table to use */ 52221b1638SMasahiro Yamada unsigned match; /* use base and extra for symbol >= match */ 53221b1638SMasahiro Yamada unsigned short count[MAXBITS+1]; /* number of codes of each length */ 54221b1638SMasahiro Yamada unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ 55221b1638SMasahiro Yamada static const unsigned short lbase[31] = { /* Length codes 257..285 base */ 56221b1638SMasahiro Yamada 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 57221b1638SMasahiro Yamada 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 58221b1638SMasahiro Yamada static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 59221b1638SMasahiro Yamada 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 60*a8c877cbSChris Kay 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 200}; 61221b1638SMasahiro Yamada static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 62221b1638SMasahiro Yamada 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 63221b1638SMasahiro Yamada 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 64221b1638SMasahiro Yamada 8193, 12289, 16385, 24577, 0, 0}; 65221b1638SMasahiro Yamada static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ 66221b1638SMasahiro Yamada 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 67221b1638SMasahiro Yamada 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 68221b1638SMasahiro Yamada 28, 28, 29, 29, 64, 64}; 69221b1638SMasahiro Yamada 70221b1638SMasahiro Yamada /* 71221b1638SMasahiro Yamada Process a set of code lengths to create a canonical Huffman code. The 72221b1638SMasahiro Yamada code lengths are lens[0..codes-1]. Each length corresponds to the 73221b1638SMasahiro Yamada symbols 0..codes-1. The Huffman code is generated by first sorting the 74221b1638SMasahiro Yamada symbols by length from short to long, and retaining the symbol order 75221b1638SMasahiro Yamada for codes with equal lengths. Then the code starts with all zero bits 76221b1638SMasahiro Yamada for the first code of the shortest length, and the codes are integer 77221b1638SMasahiro Yamada increments for the same length, and zeros are appended as the length 78221b1638SMasahiro Yamada increases. For the deflate format, these bits are stored backwards 79221b1638SMasahiro Yamada from their more natural integer increment ordering, and so when the 80221b1638SMasahiro Yamada decoding tables are built in the large loop below, the integer codes 81221b1638SMasahiro Yamada are incremented backwards. 82221b1638SMasahiro Yamada 83221b1638SMasahiro Yamada This routine assumes, but does not check, that all of the entries in 84221b1638SMasahiro Yamada lens[] are in the range 0..MAXBITS. The caller must assure this. 85221b1638SMasahiro Yamada 1..MAXBITS is interpreted as that code length. zero means that that 86221b1638SMasahiro Yamada symbol does not occur in this code. 87221b1638SMasahiro Yamada 88221b1638SMasahiro Yamada The codes are sorted by computing a count of codes for each length, 89221b1638SMasahiro Yamada creating from that a table of starting indices for each length in the 90221b1638SMasahiro Yamada sorted table, and then entering the symbols in order in the sorted 91221b1638SMasahiro Yamada table. The sorted table is work[], with that space being provided by 92221b1638SMasahiro Yamada the caller. 93221b1638SMasahiro Yamada 94221b1638SMasahiro Yamada The length counts are used for other purposes as well, i.e. finding 95221b1638SMasahiro Yamada the minimum and maximum length codes, determining if there are any 96221b1638SMasahiro Yamada codes at all, checking for a valid set of lengths, and looking ahead 97221b1638SMasahiro Yamada at length counts to determine sub-table sizes when building the 98221b1638SMasahiro Yamada decoding tables. 99221b1638SMasahiro Yamada */ 100221b1638SMasahiro Yamada 101221b1638SMasahiro Yamada /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ 102221b1638SMasahiro Yamada for (len = 0; len <= MAXBITS; len++) 103221b1638SMasahiro Yamada count[len] = 0; 104221b1638SMasahiro Yamada for (sym = 0; sym < codes; sym++) 105221b1638SMasahiro Yamada count[lens[sym]]++; 106221b1638SMasahiro Yamada 107221b1638SMasahiro Yamada /* bound code lengths, force root to be within code lengths */ 108221b1638SMasahiro Yamada root = *bits; 109221b1638SMasahiro Yamada for (max = MAXBITS; max >= 1; max--) 110221b1638SMasahiro Yamada if (count[max] != 0) break; 111221b1638SMasahiro Yamada if (root > max) root = max; 112221b1638SMasahiro Yamada if (max == 0) { /* no symbols to code at all */ 113221b1638SMasahiro Yamada here.op = (unsigned char)64; /* invalid code marker */ 114221b1638SMasahiro Yamada here.bits = (unsigned char)1; 115221b1638SMasahiro Yamada here.val = (unsigned short)0; 116221b1638SMasahiro Yamada *(*table)++ = here; /* make a table to force an error */ 117221b1638SMasahiro Yamada *(*table)++ = here; 118221b1638SMasahiro Yamada *bits = 1; 119221b1638SMasahiro Yamada return 0; /* no symbols, but wait for decoding to report error */ 120221b1638SMasahiro Yamada } 121221b1638SMasahiro Yamada for (min = 1; min < max; min++) 122221b1638SMasahiro Yamada if (count[min] != 0) break; 123221b1638SMasahiro Yamada if (root < min) root = min; 124221b1638SMasahiro Yamada 125221b1638SMasahiro Yamada /* check for an over-subscribed or incomplete set of lengths */ 126221b1638SMasahiro Yamada left = 1; 127221b1638SMasahiro Yamada for (len = 1; len <= MAXBITS; len++) { 128221b1638SMasahiro Yamada left <<= 1; 129221b1638SMasahiro Yamada left -= count[len]; 130221b1638SMasahiro Yamada if (left < 0) return -1; /* over-subscribed */ 131221b1638SMasahiro Yamada } 132221b1638SMasahiro Yamada if (left > 0 && (type == CODES || max != 1)) 133221b1638SMasahiro Yamada return -1; /* incomplete set */ 134221b1638SMasahiro Yamada 135221b1638SMasahiro Yamada /* generate offsets into symbol table for each length for sorting */ 136221b1638SMasahiro Yamada offs[1] = 0; 137221b1638SMasahiro Yamada for (len = 1; len < MAXBITS; len++) 138221b1638SMasahiro Yamada offs[len + 1] = offs[len] + count[len]; 139221b1638SMasahiro Yamada 140221b1638SMasahiro Yamada /* sort symbols by length, by symbol order within each length */ 141221b1638SMasahiro Yamada for (sym = 0; sym < codes; sym++) 142221b1638SMasahiro Yamada if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; 143221b1638SMasahiro Yamada 144221b1638SMasahiro Yamada /* 145221b1638SMasahiro Yamada Create and fill in decoding tables. In this loop, the table being 146221b1638SMasahiro Yamada filled is at next and has curr index bits. The code being used is huff 147221b1638SMasahiro Yamada with length len. That code is converted to an index by dropping drop 148221b1638SMasahiro Yamada bits off of the bottom. For codes where len is less than drop + curr, 149221b1638SMasahiro Yamada those top drop + curr - len bits are incremented through all values to 150221b1638SMasahiro Yamada fill the table with replicated entries. 151221b1638SMasahiro Yamada 152221b1638SMasahiro Yamada root is the number of index bits for the root table. When len exceeds 153221b1638SMasahiro Yamada root, sub-tables are created pointed to by the root entry with an index 154221b1638SMasahiro Yamada of the low root bits of huff. This is saved in low to check for when a 155221b1638SMasahiro Yamada new sub-table should be started. drop is zero when the root table is 156221b1638SMasahiro Yamada being filled, and drop is root when sub-tables are being filled. 157221b1638SMasahiro Yamada 158221b1638SMasahiro Yamada When a new sub-table is needed, it is necessary to look ahead in the 159221b1638SMasahiro Yamada code lengths to determine what size sub-table is needed. The length 160221b1638SMasahiro Yamada counts are used for this, and so count[] is decremented as codes are 161221b1638SMasahiro Yamada entered in the tables. 162221b1638SMasahiro Yamada 163221b1638SMasahiro Yamada used keeps track of how many table entries have been allocated from the 164221b1638SMasahiro Yamada provided *table space. It is checked for LENS and DIST tables against 165221b1638SMasahiro Yamada the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in 166221b1638SMasahiro Yamada the initial root table size constants. See the comments in inftrees.h 167221b1638SMasahiro Yamada for more information. 168221b1638SMasahiro Yamada 169221b1638SMasahiro Yamada sym increments through all symbols, and the loop terminates when 170221b1638SMasahiro Yamada all codes of length max, i.e. all codes, have been processed. This 171221b1638SMasahiro Yamada routine permits incomplete codes, so another loop after this one fills 172221b1638SMasahiro Yamada in the rest of the decoding tables with invalid code markers. 173221b1638SMasahiro Yamada */ 174221b1638SMasahiro Yamada 175221b1638SMasahiro Yamada /* set up for code type */ 176221b1638SMasahiro Yamada switch (type) { 177221b1638SMasahiro Yamada case CODES: 178221b1638SMasahiro Yamada base = extra = work; /* dummy value--not used */ 179221b1638SMasahiro Yamada match = 20; 180221b1638SMasahiro Yamada break; 181221b1638SMasahiro Yamada case LENS: 182221b1638SMasahiro Yamada base = lbase; 183221b1638SMasahiro Yamada extra = lext; 184221b1638SMasahiro Yamada match = 257; 185221b1638SMasahiro Yamada break; 186221b1638SMasahiro Yamada default: /* DISTS */ 187221b1638SMasahiro Yamada base = dbase; 188221b1638SMasahiro Yamada extra = dext; 189221b1638SMasahiro Yamada match = 0; 190221b1638SMasahiro Yamada } 191221b1638SMasahiro Yamada 192221b1638SMasahiro Yamada /* initialize state for loop */ 193221b1638SMasahiro Yamada huff = 0; /* starting code */ 194221b1638SMasahiro Yamada sym = 0; /* starting code symbol */ 195221b1638SMasahiro Yamada len = min; /* starting code length */ 196221b1638SMasahiro Yamada next = *table; /* current table to fill in */ 197221b1638SMasahiro Yamada curr = root; /* current table index bits */ 198221b1638SMasahiro Yamada drop = 0; /* current bits to drop from code for index */ 199221b1638SMasahiro Yamada low = (unsigned)(-1); /* trigger new sub-table when len > root */ 200221b1638SMasahiro Yamada used = 1U << root; /* use root table entries */ 201221b1638SMasahiro Yamada mask = used - 1; /* mask for comparing low */ 202221b1638SMasahiro Yamada 203221b1638SMasahiro Yamada /* check available table space */ 204221b1638SMasahiro Yamada if ((type == LENS && used > ENOUGH_LENS) || 205221b1638SMasahiro Yamada (type == DISTS && used > ENOUGH_DISTS)) 206221b1638SMasahiro Yamada return 1; 207221b1638SMasahiro Yamada 208221b1638SMasahiro Yamada /* process all codes and make table entries */ 209221b1638SMasahiro Yamada for (;;) { 210221b1638SMasahiro Yamada /* create table entry */ 211221b1638SMasahiro Yamada here.bits = (unsigned char)(len - drop); 212221b1638SMasahiro Yamada if (work[sym] + 1U < match) { 213221b1638SMasahiro Yamada here.op = (unsigned char)0; 214221b1638SMasahiro Yamada here.val = work[sym]; 215221b1638SMasahiro Yamada } 216221b1638SMasahiro Yamada else if (work[sym] >= match) { 217221b1638SMasahiro Yamada here.op = (unsigned char)(extra[work[sym] - match]); 218221b1638SMasahiro Yamada here.val = base[work[sym] - match]; 219221b1638SMasahiro Yamada } 220221b1638SMasahiro Yamada else { 221221b1638SMasahiro Yamada here.op = (unsigned char)(32 + 64); /* end of block */ 222221b1638SMasahiro Yamada here.val = 0; 223221b1638SMasahiro Yamada } 224221b1638SMasahiro Yamada 225221b1638SMasahiro Yamada /* replicate for those indices with low len bits equal to huff */ 226221b1638SMasahiro Yamada incr = 1U << (len - drop); 227221b1638SMasahiro Yamada fill = 1U << curr; 228221b1638SMasahiro Yamada min = fill; /* save offset to next table */ 229221b1638SMasahiro Yamada do { 230221b1638SMasahiro Yamada fill -= incr; 231221b1638SMasahiro Yamada next[(huff >> drop) + fill] = here; 232221b1638SMasahiro Yamada } while (fill != 0); 233221b1638SMasahiro Yamada 234221b1638SMasahiro Yamada /* backwards increment the len-bit code huff */ 235221b1638SMasahiro Yamada incr = 1U << (len - 1); 236221b1638SMasahiro Yamada while (huff & incr) 237221b1638SMasahiro Yamada incr >>= 1; 238221b1638SMasahiro Yamada if (incr != 0) { 239221b1638SMasahiro Yamada huff &= incr - 1; 240221b1638SMasahiro Yamada huff += incr; 241221b1638SMasahiro Yamada } 242221b1638SMasahiro Yamada else 243221b1638SMasahiro Yamada huff = 0; 244221b1638SMasahiro Yamada 245221b1638SMasahiro Yamada /* go to next symbol, update count, len */ 246221b1638SMasahiro Yamada sym++; 247221b1638SMasahiro Yamada if (--(count[len]) == 0) { 248221b1638SMasahiro Yamada if (len == max) break; 249221b1638SMasahiro Yamada len = lens[work[sym]]; 250221b1638SMasahiro Yamada } 251221b1638SMasahiro Yamada 252221b1638SMasahiro Yamada /* create new sub-table if needed */ 253221b1638SMasahiro Yamada if (len > root && (huff & mask) != low) { 254221b1638SMasahiro Yamada /* if first time, transition to sub-tables */ 255221b1638SMasahiro Yamada if (drop == 0) 256221b1638SMasahiro Yamada drop = root; 257221b1638SMasahiro Yamada 258221b1638SMasahiro Yamada /* increment past last table */ 259221b1638SMasahiro Yamada next += min; /* here min is 1 << curr */ 260221b1638SMasahiro Yamada 261221b1638SMasahiro Yamada /* determine length of next table */ 262221b1638SMasahiro Yamada curr = len - drop; 263221b1638SMasahiro Yamada left = (int)(1 << curr); 264221b1638SMasahiro Yamada while (curr + drop < max) { 265221b1638SMasahiro Yamada left -= count[curr + drop]; 266221b1638SMasahiro Yamada if (left <= 0) break; 267221b1638SMasahiro Yamada curr++; 268221b1638SMasahiro Yamada left <<= 1; 269221b1638SMasahiro Yamada } 270221b1638SMasahiro Yamada 271221b1638SMasahiro Yamada /* check for enough space */ 272221b1638SMasahiro Yamada used += 1U << curr; 273221b1638SMasahiro Yamada if ((type == LENS && used > ENOUGH_LENS) || 274221b1638SMasahiro Yamada (type == DISTS && used > ENOUGH_DISTS)) 275221b1638SMasahiro Yamada return 1; 276221b1638SMasahiro Yamada 277221b1638SMasahiro Yamada /* point entry in root table to sub-table */ 278221b1638SMasahiro Yamada low = huff & mask; 279221b1638SMasahiro Yamada (*table)[low].op = (unsigned char)curr; 280221b1638SMasahiro Yamada (*table)[low].bits = (unsigned char)root; 281221b1638SMasahiro Yamada (*table)[low].val = (unsigned short)(next - *table); 282221b1638SMasahiro Yamada } 283221b1638SMasahiro Yamada } 284221b1638SMasahiro Yamada 285221b1638SMasahiro Yamada /* fill in remaining table entry if code is incomplete (guaranteed to have 286221b1638SMasahiro Yamada at most one remaining entry, since if the code is incomplete, the 287221b1638SMasahiro Yamada maximum code length that was allowed to get this far is one bit) */ 288221b1638SMasahiro Yamada if (huff != 0) { 289221b1638SMasahiro Yamada here.op = (unsigned char)64; /* invalid code marker */ 290221b1638SMasahiro Yamada here.bits = (unsigned char)(len - drop); 291221b1638SMasahiro Yamada here.val = (unsigned short)0; 292221b1638SMasahiro Yamada next[huff] = here; 293221b1638SMasahiro Yamada } 294221b1638SMasahiro Yamada 295221b1638SMasahiro Yamada /* set return parameters */ 296221b1638SMasahiro Yamada *table += used; 297221b1638SMasahiro Yamada *bits = root; 298221b1638SMasahiro Yamada return 0; 299221b1638SMasahiro Yamada } 300