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