inftrees.c 12.8 KB
Newer Older
Mark Adler's avatar
Mark Adler committed
1
/* inftrees.c -- generate Huffman trees for efficient decoding
Mark Adler's avatar
Mark Adler committed
2
 * Copyright (C) 1995-2013 Mark Adler
Mark Adler's avatar
Mark Adler committed
3
 * For conditions of distribution and use, see copyright notice in zlib.h
Mark Adler's avatar
Mark Adler committed
4 5 6 7 8
 */

#include "zutil.h"
#include "inftrees.h"

Mark Adler's avatar
Mark Adler committed
9
#define MAXBITS 15
Mark Adler's avatar
Mark Adler committed
10

11
ZLIB_INTERNAL
Mark Adler's avatar
Mark Adler committed
12
const char inflate_copyright[] =
13
   " inflate 1.2.8 Copyright 1995-2013 Mark Adler ";
Mark Adler's avatar
Mark Adler committed
14 15 16 17 18 19
/*
  If you use the zlib library in a product, an acknowledgment is welcome
  in the documentation of your product. If for some reason you cannot
  include such an acknowledgment, I would appreciate that you keep this
  copyright string in the executable of your product.
 */
Mark Adler's avatar
Mark Adler committed
20 21

/*
Mark Adler's avatar
Mark Adler committed
22 23 24 25 26 27 28 29 30 31
   Build a set of tables to decode the provided canonical Huffman code.
   The code lengths are lens[0..codes-1].  The result starts at *table,
   whose indices are 0..2^bits-1.  work is a writable array of at least
   lens shorts, which is used as a work area.  type is the type of code
   to be generated, CODES, LENS, or DISTS.  On return, zero is success,
   -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
   on return points to the next available entry's address.  bits is the
   requested root table index bits, and on return it is the actual root
   table index bits.  It will differ if the request is greater than the
   longest code or if it is less than the shortest code.
Mark Adler's avatar
Mark Adler committed
32
 */
33 34 35 36 37 38
int ZLIB_INTERNAL inflate_table(codetype type,
                                unsigned short *lens,
                                unsigned int codes,
                                code **table,
                                unsigned int *bits,
                                unsigned short *work)
Mark Adler's avatar
Mark Adler committed
39
{
40 41 42 43 44 45
    unsigned int len;           /* a code's length in bits */
    unsigned int sym;           /* index of code symbols */
    unsigned int min, max;      /* minimum and maximum code lengths */
    unsigned int root;          /* number of index bits for root table */
    unsigned int curr;          /* number of index bits for current table */
    unsigned int drop;          /* code bits to drop for sub-table */
Mark Adler's avatar
Mark Adler committed
46
    int left;                   /* number of prefix codes available */
47 48 49 50 51 52
    unsigned int used;          /* code entries in table used */
    unsigned int huff;          /* Huffman code */
    unsigned int incr;          /* for incrementing code, index */
    unsigned int fill;          /* index for replicating entries */
    unsigned int low;           /* low bits for current root entry */
    unsigned int mask;          /* mask for low root bits */
Mark Adler's avatar
Mark Adler committed
53
    code here;                  /* table entry for duplication */
54 55 56
    code *next;                 /* next available space in table */
    const unsigned short *base; /* base value table to use */
    const unsigned short *extra;/* extra bits table to use */
Mark Adler's avatar
Mark Adler committed
57 58 59 60 61 62 63 64
    int end;                    /* use base and extra for symbol > end */
    unsigned short count[MAXBITS+1];    /* number of codes of each length */
    unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
    static const unsigned short lbase[31] = { /* Length codes 257..285 base */
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
    static const unsigned short lext[31] = { /* Length codes 257..285 extra */
        16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
65
        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
Mark Adler's avatar
Mark Adler committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
        8193, 12289, 16385, 24577, 0, 0};
    static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
        16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
        23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
        28, 28, 29, 29, 64, 64};

    /*
       Process a set of code lengths to create a canonical Huffman code.  The
       code lengths are lens[0..codes-1].  Each length corresponds to the
       symbols 0..codes-1.  The Huffman code is generated by first sorting the
       symbols by length from short to long, and retaining the symbol order
       for codes with equal lengths.  Then the code starts with all zero bits
       for the first code of the shortest length, and the codes are integer
       increments for the same length, and zeros are appended as the length
       increases.  For the deflate format, these bits are stored backwards
       from their more natural integer increment ordering, and so when the
       decoding tables are built in the large loop below, the integer codes
       are incremented backwards.

       This routine assumes, but does not check, that all of the entries in
       lens[] are in the range 0..MAXBITS.  The caller must assure this.
       1..MAXBITS is interpreted as that code length.  zero means that that
       symbol does not occur in this code.

       The codes are sorted by computing a count of codes for each length,
       creating from that a table of starting indices for each length in the
       sorted table, and then entering the symbols in order in the sorted
       table.  The sorted table is work[], with that space being provided by
       the caller.

       The length counts are used for other purposes as well, i.e. finding
       the minimum and maximum length codes, determining if there are any
       codes at all, checking for a valid set of lengths, and looking ahead
       at length counts to determine sub-table sizes when building the
       decoding tables.
     */

    /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
    for (len = 0; len <= MAXBITS; len++)
        count[len] = 0;
    for (sym = 0; sym < codes; sym++)
        count[lens[sym]]++;

    /* bound code lengths, force root to be within code lengths */
    root = *bits;
    for (max = MAXBITS; max >= 1; max--)
        if (count[max] != 0) break;
    if (root > max) root = max;
Mark Adler's avatar
Mark Adler committed
117
    if (max == 0) {                     /* no symbols to code at all */
Mark Adler's avatar
Mark Adler committed
118 119 120 121 122
        here.op = (unsigned char)64;    /* invalid code marker */
        here.bits = (unsigned char)1;
        here.val = (unsigned short)0;
        *(*table)++ = here;             /* make a table to force an error */
        *(*table)++ = here;
Mark Adler's avatar
Mark Adler committed
123 124 125
        *bits = 1;
        return 0;     /* no symbols, but wait for decoding to report error */
    }
Mark Adler's avatar
Mark Adler committed
126
    for (min = 1; min < max; min++)
Mark Adler's avatar
Mark Adler committed
127 128 129 130 131 132 133 134 135 136
        if (count[min] != 0) break;
    if (root < min) root = min;

    /* check for an over-subscribed or incomplete set of lengths */
    left = 1;
    for (len = 1; len <= MAXBITS; len++) {
        left <<= 1;
        left -= count[len];
        if (left < 0) return -1;        /* over-subscribed */
    }
Mark Adler's avatar
Mark Adler committed
137
    if (left > 0 && (type == CODES || max != 1))
Mark Adler's avatar
Mark Adler committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        return -1;                      /* incomplete set */

    /* generate offsets into symbol table for each length for sorting */
    offs[1] = 0;
    for (len = 1; len < MAXBITS; len++)
        offs[len + 1] = offs[len] + count[len];

    /* sort symbols by length, by symbol order within each length */
    for (sym = 0; sym < codes; sym++)
        if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;

    /*
       Create and fill in decoding tables.  In this loop, the table being
       filled is at next and has curr index bits.  The code being used is huff
       with length len.  That code is converted to an index by dropping drop
       bits off of the bottom.  For codes where len is less than drop + curr,
       those top drop + curr - len bits are incremented through all values to
       fill the table with replicated entries.

       root is the number of index bits for the root table.  When len exceeds
       root, sub-tables are created pointed to by the root entry with an index
       of the low root bits of huff.  This is saved in low to check for when a
       new sub-table should be started.  drop is zero when the root table is
       being filled, and drop is root when sub-tables are being filled.

       When a new sub-table is needed, it is necessary to look ahead in the
       code lengths to determine what size sub-table is needed.  The length
       counts are used for this, and so count[] is decremented as codes are
       entered in the tables.

       used keeps track of how many table entries have been allocated from the
Mark Adler's avatar
Mark Adler committed
169 170 171 172
       provided *table space.  It is checked for LENS and DIST tables against
       the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
       the initial root table size constants.  See the comments in inftrees.h
       for more information.
Mark Adler's avatar
Mark Adler committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

       sym increments through all symbols, and the loop terminates when
       all codes of length max, i.e. all codes, have been processed.  This
       routine permits incomplete codes, so another loop after this one fills
       in the rest of the decoding tables with invalid code markers.
     */

    /* set up for code type */
    switch (type) {
    case CODES:
        base = extra = work;    /* dummy value--not used */
        end = 19;
        break;
    case LENS:
        base = lbase;
        base -= 257;
        extra = lext;
        extra -= 257;
        end = 256;
        break;
    default:            /* DISTS */
        base = dbase;
        extra = dext;
        end = -1;
    }
Mark Adler's avatar
Mark Adler committed
198

Mark Adler's avatar
Mark Adler committed
199 200 201 202 203 204 205
    /* initialize state for loop */
    huff = 0;                   /* starting code */
    sym = 0;                    /* starting code symbol */
    len = min;                  /* starting code length */
    next = *table;              /* current table to fill in */
    curr = root;                /* current table index bits */
    drop = 0;                   /* current bits to drop from code for index */
206
    low = (unsigned int)(-1);   /* trigger new sub-table when len > root */
Mark Adler's avatar
Mark Adler committed
207 208 209 210
    used = 1U << root;          /* use root table entries */
    mask = used - 1;            /* mask for comparing low */

    /* check available table space */
211 212
    if ((type == LENS && used > ENOUGH_LENS) ||
        (type == DISTS && used > ENOUGH_DISTS))
Mark Adler's avatar
Mark Adler committed
213 214 215 216 217
        return 1;

    /* process all codes and make table entries */
    for (;;) {
        /* create table entry */
Mark Adler's avatar
Mark Adler committed
218
        here.bits = (unsigned char)(len - drop);
Mark Adler's avatar
Mark Adler committed
219
        if ((int)(work[sym]) < end) {
Mark Adler's avatar
Mark Adler committed
220 221
            here.op = (unsigned char)0;
            here.val = work[sym];
Mark Adler's avatar
Mark Adler committed
222
        }
Mark Adler's avatar
Mark Adler committed
223
        else if ((int)(work[sym]) > end) {
Mark Adler's avatar
Mark Adler committed
224 225
            here.op = (unsigned char)(extra[work[sym]]);
            here.val = base[work[sym]];
Mark Adler's avatar
Mark Adler committed
226 227
        }
        else {
Mark Adler's avatar
Mark Adler committed
228 229
            here.op = (unsigned char)(32 + 64);         /* end of block */
            here.val = 0;
Mark Adler's avatar
Mark Adler committed
230 231
        }

Mark Adler's avatar
Mark Adler committed
232 233 234
        /* replicate for those indices with low len bits equal to huff */
        incr = 1U << (len - drop);
        fill = 1U << curr;
Mark Adler's avatar
Mark Adler committed
235
        min = fill;                 /* save offset to next table */
Mark Adler's avatar
Mark Adler committed
236 237
        do {
            fill -= incr;
Mark Adler's avatar
Mark Adler committed
238
            next[(huff >> drop) + fill] = here;
Mark Adler's avatar
Mark Adler committed
239 240 241 242 243 244 245 246 247 248 249 250
        } while (fill != 0);

        /* backwards increment the len-bit code huff */
        incr = 1U << (len - 1);
        while (huff & incr)
            incr >>= 1;
        if (incr != 0) {
            huff &= incr - 1;
            huff += incr;
        }
        else
            huff = 0;
Mark Adler's avatar
Mark Adler committed
251

Mark Adler's avatar
Mark Adler committed
252 253 254 255 256 257
        /* go to next symbol, update count, len */
        sym++;
        if (--(count[len]) == 0) {
            if (len == max) break;
            len = lens[work[sym]];
        }
Mark Adler's avatar
Mark Adler committed
258

Mark Adler's avatar
Mark Adler committed
259 260 261 262 263 264 265
        /* create new sub-table if needed */
        if (len > root && (huff & mask) != low) {
            /* if first time, transition to sub-tables */
            if (drop == 0)
                drop = root;

            /* increment past last table */
Mark Adler's avatar
Mark Adler committed
266
            next += min;            /* here min is 1 << curr */
Mark Adler's avatar
Mark Adler committed
267 268 269 270 271 272 273 274 275 276

            /* determine length of next table */
            curr = len - drop;
            left = (int)(1 << curr);
            while (curr + drop < max) {
                left -= count[curr + drop];
                if (left <= 0) break;
                curr++;
                left <<= 1;
            }
Mark Adler's avatar
Mark Adler committed
277

Mark Adler's avatar
Mark Adler committed
278 279
            /* check for enough space */
            used += 1U << curr;
280 281
            if ((type == LENS && used > ENOUGH_LENS) ||
                (type == DISTS && used > ENOUGH_DISTS))
Mark Adler's avatar
Mark Adler committed
282
                return 1;
Mark Adler's avatar
Mark Adler committed
283

Mark Adler's avatar
Mark Adler committed
284 285 286 287 288 289
            /* point entry in root table to sub-table */
            low = huff & mask;
            (*table)[low].op = (unsigned char)curr;
            (*table)[low].bits = (unsigned char)root;
            (*table)[low].val = (unsigned short)(next - *table);
        }
Mark Adler's avatar
Mark Adler committed
290 291
    }

292 293 294 295 296 297 298 299
    /* fill in remaining table entry if code is incomplete (guaranteed to have
       at most one remaining entry, since if the code is incomplete, the
       maximum code length that was allowed to get this far is one bit) */
    if (huff != 0) {
        here.op = (unsigned char)64;            /* invalid code marker */
        here.bits = (unsigned char)(len - drop);
        here.val = (unsigned short)0;
        next[huff] = here;
Mark Adler's avatar
Mark Adler committed
300
    }
Mark Adler's avatar
Mark Adler committed
301

Mark Adler's avatar
Mark Adler committed
302 303 304 305
    /* set return parameters */
    *table += used;
    *bits = root;
    return 0;
Mark Adler's avatar
Mark Adler committed
306
}