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