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