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