Back to home page

OSCL-LXR

 
 

    


0001 /* inftrees.c -- generate Huffman trees for efficient decoding
0002  * Copyright (C) 1995-2005 Mark Adler
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 #include <linux/zutil.h>
0007 #include "inftrees.h"
0008 
0009 #define MAXBITS 15
0010 
0011 /*
0012    Build a set of tables to decode the provided canonical Huffman code.
0013    The code lengths are lens[0..codes-1].  The result starts at *table,
0014    whose indices are 0..2^bits-1.  work is a writable array of at least
0015    lens shorts, which is used as a work area.  type is the type of code
0016    to be generated, CODES, LENS, or DISTS.  On return, zero is success,
0017    -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
0018    on return points to the next available entry's address.  bits is the
0019    requested root table index bits, and on return it is the actual root
0020    table index bits.  It will differ if the request is greater than the
0021    longest code or if it is less than the shortest code.
0022  */
0023 int zlib_inflate_table(codetype type, unsigned short *lens, unsigned codes,
0024             code **table, unsigned *bits, unsigned short *work)
0025 {
0026     unsigned len;               /* a code's length in bits */
0027     unsigned sym;               /* index of code symbols */
0028     unsigned min, max;          /* minimum and maximum code lengths */
0029     unsigned root;              /* number of index bits for root table */
0030     unsigned curr;              /* number of index bits for current table */
0031     unsigned drop;              /* code bits to drop for sub-table */
0032     int left;                   /* number of prefix codes available */
0033     unsigned used;              /* code entries in table used */
0034     unsigned huff;              /* Huffman code */
0035     unsigned incr;              /* for incrementing code, index */
0036     unsigned fill;              /* index for replicating entries */
0037     unsigned low;               /* low bits for current root entry */
0038     unsigned mask;              /* mask for low root bits */
0039     code this;                  /* table entry for duplication */
0040     code *next;             /* next available space in table */
0041     const unsigned short *base;     /* base value table to use */
0042     const unsigned short *extra;    /* extra bits table to use */
0043     int end;                    /* use base and extra for symbol > end */
0044     unsigned short count[MAXBITS+1];    /* number of codes of each length */
0045     unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
0046     static const unsigned short lbase[31] = { /* Length codes 257..285 base */
0047         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
0048         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
0049     static const unsigned short lext[31] = { /* Length codes 257..285 extra */
0050         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
0051         19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
0052     static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
0053         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
0054         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
0055         8193, 12289, 16385, 24577, 0, 0};
0056     static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
0057         16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
0058         23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
0059         28, 28, 29, 29, 64, 64};
0060 
0061     /*
0062        Process a set of code lengths to create a canonical Huffman code.  The
0063        code lengths are lens[0..codes-1].  Each length corresponds to the
0064        symbols 0..codes-1.  The Huffman code is generated by first sorting the
0065        symbols by length from short to long, and retaining the symbol order
0066        for codes with equal lengths.  Then the code starts with all zero bits
0067        for the first code of the shortest length, and the codes are integer
0068        increments for the same length, and zeros are appended as the length
0069        increases.  For the deflate format, these bits are stored backwards
0070        from their more natural integer increment ordering, and so when the
0071        decoding tables are built in the large loop below, the integer codes
0072        are incremented backwards.
0073 
0074        This routine assumes, but does not check, that all of the entries in
0075        lens[] are in the range 0..MAXBITS.  The caller must assure this.
0076        1..MAXBITS is interpreted as that code length.  zero means that that
0077        symbol does not occur in this code.
0078 
0079        The codes are sorted by computing a count of codes for each length,
0080        creating from that a table of starting indices for each length in the
0081        sorted table, and then entering the symbols in order in the sorted
0082        table.  The sorted table is work[], with that space being provided by
0083        the caller.
0084 
0085        The length counts are used for other purposes as well, i.e. finding
0086        the minimum and maximum length codes, determining if there are any
0087        codes at all, checking for a valid set of lengths, and looking ahead
0088        at length counts to determine sub-table sizes when building the
0089        decoding tables.
0090      */
0091 
0092     /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
0093     for (len = 0; len <= MAXBITS; len++)
0094         count[len] = 0;
0095     for (sym = 0; sym < codes; sym++)
0096         count[lens[sym]]++;
0097 
0098     /* bound code lengths, force root to be within code lengths */
0099     root = *bits;
0100     for (max = MAXBITS; max >= 1; max--)
0101         if (count[max] != 0) break;
0102     if (root > max) root = max;
0103     if (max == 0) {                     /* no symbols to code at all */
0104         this.op = (unsigned char)64;    /* invalid code marker */
0105         this.bits = (unsigned char)1;
0106         this.val = (unsigned short)0;
0107         *(*table)++ = this;             /* make a table to force an error */
0108         *(*table)++ = this;
0109         *bits = 1;
0110         return 0;     /* no symbols, but wait for decoding to report error */
0111     }
0112     for (min = 1; min < MAXBITS; min++)
0113         if (count[min] != 0) break;
0114     if (root < min) root = min;
0115 
0116     /* check for an over-subscribed or incomplete set of lengths */
0117     left = 1;
0118     for (len = 1; len <= MAXBITS; len++) {
0119         left <<= 1;
0120         left -= count[len];
0121         if (left < 0) return -1;        /* over-subscribed */
0122     }
0123     if (left > 0 && (type == CODES || max != 1))
0124         return -1;                      /* incomplete set */
0125 
0126     /* generate offsets into symbol table for each length for sorting */
0127     offs[1] = 0;
0128     for (len = 1; len < MAXBITS; len++)
0129         offs[len + 1] = offs[len] + count[len];
0130 
0131     /* sort symbols by length, by symbol order within each length */
0132     for (sym = 0; sym < codes; sym++)
0133         if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
0134 
0135     /*
0136        Create and fill in decoding tables.  In this loop, the table being
0137        filled is at next and has curr index bits.  The code being used is huff
0138        with length len.  That code is converted to an index by dropping drop
0139        bits off of the bottom.  For codes where len is less than drop + curr,
0140        those top drop + curr - len bits are incremented through all values to
0141        fill the table with replicated entries.
0142 
0143        root is the number of index bits for the root table.  When len exceeds
0144        root, sub-tables are created pointed to by the root entry with an index
0145        of the low root bits of huff.  This is saved in low to check for when a
0146        new sub-table should be started.  drop is zero when the root table is
0147        being filled, and drop is root when sub-tables are being filled.
0148 
0149        When a new sub-table is needed, it is necessary to look ahead in the
0150        code lengths to determine what size sub-table is needed.  The length
0151        counts are used for this, and so count[] is decremented as codes are
0152        entered in the tables.
0153 
0154        used keeps track of how many table entries have been allocated from the
0155        provided *table space.  It is checked when a LENS table is being made
0156        against the space in *table, ENOUGH, minus the maximum space needed by
0157        the worst case distance code, MAXD.  This should never happen, but the
0158        sufficiency of ENOUGH has not been proven exhaustively, hence the check.
0159        This assumes that when type == LENS, bits == 9.
0160 
0161        sym increments through all symbols, and the loop terminates when
0162        all codes of length max, i.e. all codes, have been processed.  This
0163        routine permits incomplete codes, so another loop after this one fills
0164        in the rest of the decoding tables with invalid code markers.
0165      */
0166 
0167     /* set up for code type */
0168     switch (type) {
0169     case CODES:
0170         base = extra = work;    /* dummy value--not used */
0171         end = 19;
0172         break;
0173     case LENS:
0174         base = lbase;
0175         base -= 257;
0176         extra = lext;
0177         extra -= 257;
0178         end = 256;
0179         break;
0180     default:            /* DISTS */
0181         base = dbase;
0182         extra = dext;
0183         end = -1;
0184     }
0185 
0186     /* initialize state for loop */
0187     huff = 0;                   /* starting code */
0188     sym = 0;                    /* starting code symbol */
0189     len = min;                  /* starting code length */
0190     next = *table;              /* current table to fill in */
0191     curr = root;                /* current table index bits */
0192     drop = 0;                   /* current bits to drop from code for index */
0193     low = (unsigned)(-1);       /* trigger new sub-table when len > root */
0194     used = 1U << root;          /* use root table entries */
0195     mask = used - 1;            /* mask for comparing low */
0196 
0197     /* check available table space */
0198     if (type == LENS && used >= ENOUGH - MAXD)
0199         return 1;
0200 
0201     /* process all codes and make table entries */
0202     for (;;) {
0203         /* create table entry */
0204         this.bits = (unsigned char)(len - drop);
0205         if ((int)(work[sym]) < end) {
0206             this.op = (unsigned char)0;
0207             this.val = work[sym];
0208         }
0209         else if ((int)(work[sym]) > end) {
0210             this.op = (unsigned char)(extra[work[sym]]);
0211             this.val = base[work[sym]];
0212         }
0213         else {
0214             this.op = (unsigned char)(32 + 64);         /* end of block */
0215             this.val = 0;
0216         }
0217 
0218         /* replicate for those indices with low len bits equal to huff */
0219         incr = 1U << (len - drop);
0220         fill = 1U << curr;
0221         min = fill;                 /* save offset to next table */
0222         do {
0223             fill -= incr;
0224             next[(huff >> drop) + fill] = this;
0225         } while (fill != 0);
0226 
0227         /* backwards increment the len-bit code huff */
0228         incr = 1U << (len - 1);
0229         while (huff & incr)
0230             incr >>= 1;
0231         if (incr != 0) {
0232             huff &= incr - 1;
0233             huff += incr;
0234         }
0235         else
0236             huff = 0;
0237 
0238         /* go to next symbol, update count, len */
0239         sym++;
0240         if (--(count[len]) == 0) {
0241             if (len == max) break;
0242             len = lens[work[sym]];
0243         }
0244 
0245         /* create new sub-table if needed */
0246         if (len > root && (huff & mask) != low) {
0247             /* if first time, transition to sub-tables */
0248             if (drop == 0)
0249                 drop = root;
0250 
0251             /* increment past last table */
0252             next += min;            /* here min is 1 << curr */
0253 
0254             /* determine length of next table */
0255             curr = len - drop;
0256             left = (int)(1 << curr);
0257             while (curr + drop < max) {
0258                 left -= count[curr + drop];
0259                 if (left <= 0) break;
0260                 curr++;
0261                 left <<= 1;
0262             }
0263 
0264             /* check for enough space */
0265             used += 1U << curr;
0266             if (type == LENS && used >= ENOUGH - MAXD)
0267                 return 1;
0268 
0269             /* point entry in root table to sub-table */
0270             low = huff & mask;
0271             (*table)[low].op = (unsigned char)curr;
0272             (*table)[low].bits = (unsigned char)root;
0273             (*table)[low].val = (unsigned short)(next - *table);
0274         }
0275     }
0276 
0277     /*
0278        Fill in rest of table for incomplete codes.  This loop is similar to the
0279        loop above in incrementing huff for table indices.  It is assumed that
0280        len is equal to curr + drop, so there is no loop needed to increment
0281        through high index bits.  When the current sub-table is filled, the loop
0282        drops back to the root table to fill in any remaining entries there.
0283      */
0284     this.op = (unsigned char)64;                /* invalid code marker */
0285     this.bits = (unsigned char)(len - drop);
0286     this.val = (unsigned short)0;
0287     while (huff != 0) {
0288         /* when done with sub-table, drop back to root table */
0289         if (drop != 0 && (huff & mask) != low) {
0290             drop = 0;
0291             len = root;
0292             next = *table;
0293             this.bits = (unsigned char)len;
0294         }
0295 
0296         /* put invalid code marker in table */
0297         next[huff >> drop] = this;
0298 
0299         /* backwards increment the len-bit code huff */
0300         incr = 1U << (len - 1);
0301         while (huff & incr)
0302             incr >>= 1;
0303         if (incr != 0) {
0304             huff &= incr - 1;
0305             huff += incr;
0306         }
0307         else
0308             huff = 0;
0309     }
0310 
0311     /* set return parameters */
0312     *table += used;
0313     *bits = root;
0314     return 0;
0315 }