Blame deps/zlib/inftrees.c

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