#pragma prototyped12#include "huff.h"34/*5Huffman code decoding is performed using a multi-level table lookup.6The fastest way to decode is to simply build a lookup table whose7size is determined by the longest code. However, the time it takes8to build this table can also be a factor if the data being decoded9are not very long. The most common codes are necessarily the10shortest codes, so those codes dominate the decoding time, and hence11the speed. The idea is you can have a shorter table that decodes the12shorter, more probable codes, and then point to subsidiary tables for13the longer codes. The time it costs to decode the longer codes is14then traded against the time it takes to make longer tables.1516This results of this trade are in the variables lbits and dbits17below. lbits is the number of bits the first level table for literal/18length codes can decode in one step, and dbits is the same thing for19the distance codes. Subsequent tables are also less than or equal to20those sizes. These values may be adjusted either when all of the21codes are shorter than that, in which case the longest code length in22bits is used, or when the shortest code is *longer* than the requested23table size, in which case the length of the shortest code in bits is24used.2526There are two different values for the two tables, since they code a27different number of possibilities each. The literal/length table28codes 286 possible values, or in a flat code, a little over eight29bits. The distance table codes 30 possible values, or a little less30than five bits, flat. The optimum values for speed end up being31about one bit more than those, so lbits is 8+1 and dbits is 5+1.32The optimum values may differ though from machine to machine, and33possibly even between compilers. Your mileage may vary.34*/3536/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */37#define BMAX 16 /* maximum bit length of any code (16 for explode) */38#define N_MAX 288 /* maximum number of codes in any set */3940int huff(41ulg *b, /* code lengths in bits (all assumed <= BMAX) */42ulg n, /* number of codes (assumed <= N_MAX) */43ulg s, /* number of simple-valued codes (0..s-1) */44ush *d, /* list of base values for non-simple codes */45ush *e, /* list of extra bits for non-simple codes */46Huff_t **t, /* result: starting table */47int *m, /* maximum lookup bits, returns actual */48Vmalloc_t *vm) /* memory pool */49/* Given a list of code lengths and a maximum table size, make a set of50tables to decode that set of codes. Return zero on success, one if51the given code set is incomplete (the tables are still built in this52case), two if the input is invalid (all zero length codes or an53oversubscribed set of lengths), and three if not enough memory.54The code with value 256 is special, and the tables are constructed55so that no bits beyond that code are fetched when that code is56decoded. */57{58ulg a; /* counter for codes of length k */59ulg c[BMAX+1]; /* bit length count table */60ulg el; /* length of EOB code (value 256) */61ulg f; /* i repeats in table every f entries */62int g; /* maximum code length */63int h; /* table level */64register ulg i; /* counter, current code */65register ulg j; /* counter */66register int k; /* number of bits in current code */67int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */68int *l = lx+1; /* stack of bits per table */69register ulg *p; /* pointer into c[], b[], or v[] */70register Huff_t *q; /* points to current table */71Huff_t r; /* table entry for structure assignment */72Huff_t *u[BMAX]; /* table stack */73ulg v[N_MAX]; /* values in order of bit length */74register int w; /* bits before this table == (l * h) */75ulg x[BMAX+1]; /* bit offsets, then code stack */76ulg *xp; /* pointer into x */77int y; /* number of dummy codes added */78ulg z; /* number of entries in current table */7980/* Generate counts for each bit length */81el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */82memset(c, 0, sizeof(c));83p = b;84i = n;85do86{87c[*p]++; /* assume all entries <= BMAX */88p++; /* Can't combine with above line (Solaris bug) */89} while(--i);90if(c[0] == n) /* null input--all zero length codes */91{92*t = (Huff_t *)NULL;93*m = 0;94return 0;95}9697/* Find minimum and maximum length, bound *m by those */98for(j = 1; j <= BMAX; j++)99if(c[j])100break;101k = j; /* minimum code length */102if((ulg)*m < j)103*m = j;104for(i = BMAX; i; i--)105if(c[i])106break;107g = i; /* maximum code length */108if((ulg)*m > i)109*m = i;110111/* Adjust last length count to fill out codes, if needed */112for(y = 1 << j; j < i; j++, y <<= 1)113if((y -= c[j]) < 0)114return 2; /* bad input: more codes than bits */115if((y -= c[i]) < 0)116return 2;117c[i] += y;118119/* Generate starting offsets into the value table for each length */120x[1] = j = 0;121p = c + 1; xp = x + 2;122while(--i) /* note that i == g from above */123*xp++ = (j += *p++);124125/* Make a table of values in order of bit lengths */126memset(v, 0, sizeof(v));127p = b;128i = 0;129do130{131if((j = *p++) != 0)132v[x[j]++] = i;133} while(++i < n);134n = x[g]; /* set n to length of v */135136/* Generate the Huffman codes and for each, make the table entries */137x[0] = i = 0; /* first Huffman code is zero */138p = v; /* grab values in bit order */139h = -1; /* no tables yet--level -1 */140w = l[-1] = 0; /* no bits decoded yet */141u[0] = (Huff_t *)NULL; /* just to keep compilers happy */142q = (Huff_t *)NULL; /* ditto */143z = 0; /* ditto */144145/* go through the bit lengths (k already is bits in shortest code) */146for(; k <= g; k++)147{148a = c[k];149while(a--)150{151/* here i is the Huffman code of length k bits for value *p */152/* make tables up to required level */153while(k > w + l[h])154{155w += l[h++]; /* add bits already decoded */156157/* compute minimum size table less than or equal to *m bits */158z = (z = g - w) > (ulg)*m ? *m : z; /* upper limit */159if((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */160{ /* too few codes for k-w bit table */161f -= a + 1; /* deduct codes from patterns left */162xp = c + k;163while(++j < z)/* try smaller tables up to z bits */164{165if((f <<= 1) <= *++xp)166break; /* enough codes to use up j bits */167f -= *xp; /* else deduct codes from patterns */168}169}170if((ulg)w + j > el && (ulg)w < el)171j = el - w; /* make EOB code end at table */172z = 1 << j; /* table entries for j-bit table */173l[h] = j; /* set table size in stack */174175/* allocate and link in new table */176q = (Huff_t *)vmalloc(vm, (z + 1)*sizeof(Huff_t));177if(q == NULL)178{179return 3; /* not enough memory */180}181182*t = q + 1; /* link to list for huft_free() */183*(t = &(q->v.t)) = (Huff_t *)NULL;184u[h] = ++q; /* table starts after link */185186/* connect to last table, if there is one */187if(h)188{189x[h] = i; /* save pattern for backing up */190r.b = (uch)l[h-1]; /* bits to dump before this table */191r.e = (uch)(16 + j);/* bits in this table */192r.v.t = q; /* pointer to this table */193j = (i & ((1 << w) - 1)) >> (w - l[h-1]);194u[h-1][j] = r; /* connect to last table */195}196}197198/* set up table entry in r */199r.b = (uch)(k - w);200if(p >= v + n)201r.e = 99; /* out of values--invalid code */202else if(*p < s)203{204r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */205r.v.n = (ush)*p++; /* simple code is just the value */206}207else208{209r.e = (uch)e[*p - s]; /* non-simple--look up in lists */210r.v.n = d[*p++ - s];211}212213/* fill code-like entries with r */214f = 1 << (k - w);215for(j = i >> w; j < z; j += f)216q[j] = r;217218/* backwards increment the k-bit code i */219for(j = 1 << (k - 1); i & j; j >>= 1)220i ^= j;221i ^= j;222223/* backup over finished tables */224while((i & ((1 << w) - 1)) != x[h])225w -= l[--h]; /* don't need to update q */226}227}228229/* return actual size of base table */230*m = l[0];231232/* Return true (1) if we were given an incomplete table */233return y != 0 && g != 1;234}235236237