Blame deps/zlib/inftrees.c

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