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