root/lib/libz/inftrees.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. inflate_table

    1 /*      $OpenBSD: inftrees.c,v 1.14 2005/07/20 15:56:46 millert Exp $   */
    2 /* inftrees.c -- generate Huffman trees for efficient decoding
    3  * Copyright (C) 1995-2005 Mark Adler
    4  * For conditions of distribution and use, see copyright notice in zlib.h
    5  */
    6 
    7 #include "zutil.h"
    8 #include "inftrees.h"
    9 
   10 #define MAXBITS 15
   11 
   12 const char inflate_copyright[] =
   13    " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
   14 /*
   15   If you use the zlib library in a product, an acknowledgment is welcome
   16   in the documentation of your product. If for some reason you cannot
   17   include such an acknowledgment, I would appreciate that you keep this
   18   copyright string in the executable of your product.
   19  */
   20 
   21 /*
   22    Build a set of tables to decode the provided canonical Huffman code.
   23    The code lengths are lens[0..codes-1].  The result starts at *table,
   24    whose indices are 0..2^bits-1.  work is a writable array of at least
   25    lens shorts, which is used as a work area.  type is the type of code
   26    to be generated, CODES, LENS, or DISTS.  On return, zero is success,
   27    -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
   28    on return points to the next available entry's address.  bits is the
   29    requested root table index bits, and on return it is the actual root
   30    table index bits.  It will differ if the request is greater than the
   31    longest code or if it is less than the shortest code.
   32  */
   33 int inflate_table(type, lens, codes, table, bits, work)
   34 codetype type;
   35 unsigned short FAR *lens;
   36 unsigned codes;
   37 code FAR * FAR *table;
   38 unsigned FAR *bits;
   39 unsigned short FAR *work;
   40 {
   41     unsigned len;               /* a code's length in bits */
   42     unsigned sym;               /* index of code symbols */
   43     unsigned min, max;          /* minimum and maximum code lengths */
   44     unsigned root;              /* number of index bits for root table */
   45     unsigned curr;              /* number of index bits for current table */
   46     unsigned drop;              /* code bits to drop for sub-table */
   47     int left;                   /* number of prefix codes available */
   48     unsigned used;              /* code entries in table used */
   49     unsigned huff;              /* Huffman code */
   50     unsigned incr;              /* for incrementing code, index */
   51     unsigned fill;              /* index for replicating entries */
   52     unsigned low;               /* low bits for current root entry */
   53     unsigned mask;              /* mask for low root bits */
   54     code this;                  /* table entry for duplication */
   55     code FAR *next;             /* next available space in table */
   56     const unsigned short FAR *base;     /* base value table to use */
   57     const unsigned short FAR *extra;    /* extra bits table to use */
   58     int end;                    /* use base and extra for symbol > end */
   59     unsigned short count[MAXBITS+1];    /* number of codes of each length */
   60     unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
   61     static const unsigned short lbase[31] = { /* Length codes 257..285 base */
   62         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
   63         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
   64     static const unsigned short lext[31] = { /* Length codes 257..285 extra */
   65         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
   66         19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
   67     static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
   68         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
   69         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
   70         8193, 12289, 16385, 24577, 0, 0};
   71     static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
   72         16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
   73         23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
   74         28, 28, 29, 29, 64, 64};
   75 
   76     /*
   77        Process a set of code lengths to create a canonical Huffman code.  The
   78        code lengths are lens[0..codes-1].  Each length corresponds to the
   79        symbols 0..codes-1.  The Huffman code is generated by first sorting the
   80        symbols by length from short to long, and retaining the symbol order
   81        for codes with equal lengths.  Then the code starts with all zero bits
   82        for the first code of the shortest length, and the codes are integer
   83        increments for the same length, and zeros are appended as the length
   84        increases.  For the deflate format, these bits are stored backwards
   85        from their more natural integer increment ordering, and so when the
   86        decoding tables are built in the large loop below, the integer codes
   87        are incremented backwards.
   88 
   89        This routine assumes, but does not check, that all of the entries in
   90        lens[] are in the range 0..MAXBITS.  The caller must assure this.
   91        1..MAXBITS is interpreted as that code length.  zero means that that
   92        symbol does not occur in this code.
   93 
   94        The codes are sorted by computing a count of codes for each length,
   95        creating from that a table of starting indices for each length in the
   96        sorted table, and then entering the symbols in order in the sorted
   97        table.  The sorted table is work[], with that space being provided by
   98        the caller.
   99 
  100        The length counts are used for other purposes as well, i.e. finding
  101        the minimum and maximum length codes, determining if there are any
  102        codes at all, checking for a valid set of lengths, and looking ahead
  103        at length counts to determine sub-table sizes when building the
  104        decoding tables.
  105      */
  106 
  107     /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  108     for (len = 0; len <= MAXBITS; len++)
  109         count[len] = 0;
  110     for (sym = 0; sym < codes; sym++)
  111         count[lens[sym]]++;
  112 
  113     /* bound code lengths, force root to be within code lengths */
  114     root = *bits;
  115     for (max = MAXBITS; max >= 1; max--)
  116         if (count[max] != 0) break;
  117     if (root > max) root = max;
  118     if (max == 0) {                     /* no symbols to code at all */
  119         this.op = (unsigned char)64;    /* invalid code marker */
  120         this.bits = (unsigned char)1;
  121         this.val = (unsigned short)0;
  122         *(*table)++ = this;             /* make a table to force an error */
  123         *(*table)++ = this;
  124         *bits = 1;
  125         return 0;     /* no symbols, but wait for decoding to report error */
  126     }
  127     for (min = 1; min <= MAXBITS; min++)
  128         if (count[min] != 0) break;
  129     if (root < min) root = min;
  130 
  131     /* check for an over-subscribed or incomplete set of lengths */
  132     left = 1;
  133     for (len = 1; len <= MAXBITS; len++) {
  134         left <<= 1;
  135         left -= count[len];
  136         if (left < 0) return -1;        /* over-subscribed */
  137     }
  138     if (left > 0 && (type == CODES || max != 1))
  139         return -1;                      /* incomplete set */
  140 
  141     /* generate offsets into symbol table for each length for sorting */
  142     offs[1] = 0;
  143     for (len = 1; len < MAXBITS; len++)
  144         offs[len + 1] = offs[len] + count[len];
  145 
  146     /* sort symbols by length, by symbol order within each length */
  147     for (sym = 0; sym < codes; sym++)
  148         if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
  149 
  150     /*
  151        Create and fill in decoding tables.  In this loop, the table being
  152        filled is at next and has curr index bits.  The code being used is huff
  153        with length len.  That code is converted to an index by dropping drop
  154        bits off of the bottom.  For codes where len is less than drop + curr,
  155        those top drop + curr - len bits are incremented through all values to
  156        fill the table with replicated entries.
  157 
  158        root is the number of index bits for the root table.  When len exceeds
  159        root, sub-tables are created pointed to by the root entry with an index
  160        of the low root bits of huff.  This is saved in low to check for when a
  161        new sub-table should be started.  drop is zero when the root table is
  162        being filled, and drop is root when sub-tables are being filled.
  163 
  164        When a new sub-table is needed, it is necessary to look ahead in the
  165        code lengths to determine what size sub-table is needed.  The length
  166        counts are used for this, and so count[] is decremented as codes are
  167        entered in the tables.
  168 
  169        used keeps track of how many table entries have been allocated from the
  170        provided *table space.  It is checked when a LENS table is being made
  171        against the space in *table, ENOUGH, minus the maximum space needed by
  172        the worst case distance code, MAXD.  This should never happen, but the
  173        sufficiency of ENOUGH has not been proven exhaustively, hence the check.
  174        This assumes that when type == LENS, bits == 9.
  175 
  176        sym increments through all symbols, and the loop terminates when
  177        all codes of length max, i.e. all codes, have been processed.  This
  178        routine permits incomplete codes, so another loop after this one fills
  179        in the rest of the decoding tables with invalid code markers.
  180      */
  181 
  182     /* set up for code type */
  183     switch (type) {
  184     case CODES:
  185         base = extra = work;    /* dummy value--not used */
  186         end = 19;
  187         break;
  188     case LENS:
  189         base = lbase;
  190         base -= 257;
  191         extra = lext;
  192         extra -= 257;
  193         end = 256;
  194         break;
  195     default:            /* DISTS */
  196         base = dbase;
  197         extra = dext;
  198         end = -1;
  199     }
  200 
  201     /* initialize state for loop */
  202     huff = 0;                   /* starting code */
  203     sym = 0;                    /* starting code symbol */
  204     len = min;                  /* starting code length */
  205     next = *table;              /* current table to fill in */
  206     curr = root;                /* current table index bits */
  207     drop = 0;                   /* current bits to drop from code for index */
  208     low = (unsigned)(-1);       /* trigger new sub-table when len > root */
  209     used = 1U << root;          /* use root table entries */
  210     mask = used - 1;            /* mask for comparing low */
  211 
  212     /* check available table space */
  213     if (type == LENS && used >= ENOUGH - MAXD)
  214         return 1;
  215 
  216     /* process all codes and make table entries */
  217     for (;;) {
  218         /* create table entry */
  219         this.bits = (unsigned char)(len - drop);
  220         if ((int)(work[sym]) < end) {
  221             this.op = (unsigned char)0;
  222             this.val = work[sym];
  223         }
  224         else if ((int)(work[sym]) > end) {
  225             this.op = (unsigned char)(extra[work[sym]]);
  226             this.val = base[work[sym]];
  227         }
  228         else {
  229             this.op = (unsigned char)(32 + 64);         /* end of block */
  230             this.val = 0;
  231         }
  232 
  233         /* replicate for those indices with low len bits equal to huff */
  234         incr = 1U << (len - drop);
  235         fill = 1U << curr;
  236         min = fill;                 /* save offset to next table */
  237         do {
  238             fill -= incr;
  239             next[(huff >> drop) + fill] = this;
  240         } while (fill != 0);
  241 
  242         /* backwards increment the len-bit code huff */
  243         incr = 1U << (len - 1);
  244         while (huff & incr)
  245             incr >>= 1;
  246         if (incr != 0) {
  247             huff &= incr - 1;
  248             huff += incr;
  249         }
  250         else
  251             huff = 0;
  252 
  253         /* go to next symbol, update count, len */
  254         sym++;
  255         if (--(count[len]) == 0) {
  256             if (len == max) break;
  257             len = lens[work[sym]];
  258         }
  259 
  260         /* create new sub-table if needed */
  261         if (len > root && (huff & mask) != low) {
  262             /* if first time, transition to sub-tables */
  263             if (drop == 0)
  264                 drop = root;
  265 
  266             /* increment past last table */
  267             next += min;            /* here min is 1 << curr */
  268 
  269             /* determine length of next table */
  270             curr = len - drop;
  271             left = (int)(1 << curr);
  272             while (curr + drop < max) {
  273                 left -= count[curr + drop];
  274                 if (left <= 0) break;
  275                 curr++;
  276                 left <<= 1;
  277             }
  278 
  279             /* check for enough space */
  280             used += 1U << curr;
  281             if (type == LENS && used >= ENOUGH - MAXD)
  282                 return 1;
  283 
  284             /* point entry in root table to sub-table */
  285             low = huff & mask;
  286             (*table)[low].op = (unsigned char)curr;
  287             (*table)[low].bits = (unsigned char)root;
  288             (*table)[low].val = (unsigned short)(next - *table);
  289         }
  290     }
  291 
  292     /*
  293        Fill in rest of table for incomplete codes.  This loop is similar to the
  294        loop above in incrementing huff for table indices.  It is assumed that
  295        len is equal to curr + drop, so there is no loop needed to increment
  296        through high index bits.  When the current sub-table is filled, the loop
  297        drops back to the root table to fill in any remaining entries there.
  298      */
  299     this.op = (unsigned char)64;                /* invalid code marker */
  300     this.bits = (unsigned char)(len - drop);
  301     this.val = (unsigned short)0;
  302     while (huff != 0) {
  303         /* when done with sub-table, drop back to root table */
  304         if (drop != 0 && (huff & mask) != low) {
  305             drop = 0;
  306             len = root;
  307             next = *table;
  308             this.bits = (unsigned char)len;
  309         }
  310 
  311         /* put invalid code marker in table */
  312         next[huff >> drop] = this;
  313 
  314         /* backwards increment the len-bit code huff */
  315         incr = 1U << (len - 1);
  316         while (huff & incr)
  317             incr >>= 1;
  318         if (incr != 0) {
  319             huff &= incr - 1;
  320             huff += incr;
  321         }
  322         else
  323             huff = 0;
  324     }
  325 
  326     /* set return parameters */
  327     *table += used;
  328     *bits = root;
  329     return 0;
  330 }

/* [<][>][^][v][top][bottom][index][help] */