Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/codexlib/zip/huff.c
1810 views
1
#pragma prototyped
2
3
#include "huff.h"
4
5
/*
6
Huffman code decoding is performed using a multi-level table lookup.
7
The fastest way to decode is to simply build a lookup table whose
8
size is determined by the longest code. However, the time it takes
9
to build this table can also be a factor if the data being decoded
10
are not very long. The most common codes are necessarily the
11
shortest codes, so those codes dominate the decoding time, and hence
12
the speed. The idea is you can have a shorter table that decodes the
13
shorter, more probable codes, and then point to subsidiary tables for
14
the longer codes. The time it costs to decode the longer codes is
15
then traded against the time it takes to make longer tables.
16
17
This results of this trade are in the variables lbits and dbits
18
below. lbits is the number of bits the first level table for literal/
19
length codes can decode in one step, and dbits is the same thing for
20
the distance codes. Subsequent tables are also less than or equal to
21
those sizes. These values may be adjusted either when all of the
22
codes are shorter than that, in which case the longest code length in
23
bits is used, or when the shortest code is *longer* than the requested
24
table size, in which case the length of the shortest code in bits is
25
used.
26
27
There are two different values for the two tables, since they code a
28
different number of possibilities each. The literal/length table
29
codes 286 possible values, or in a flat code, a little over eight
30
bits. The distance table codes 30 possible values, or a little less
31
than five bits, flat. The optimum values for speed end up being
32
about one bit more than those, so lbits is 8+1 and dbits is 5+1.
33
The optimum values may differ though from machine to machine, and
34
possibly even between compilers. Your mileage may vary.
35
*/
36
37
/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
38
#define BMAX 16 /* maximum bit length of any code (16 for explode) */
39
#define N_MAX 288 /* maximum number of codes in any set */
40
41
int huff(
42
ulg *b, /* code lengths in bits (all assumed <= BMAX) */
43
ulg n, /* number of codes (assumed <= N_MAX) */
44
ulg s, /* number of simple-valued codes (0..s-1) */
45
ush *d, /* list of base values for non-simple codes */
46
ush *e, /* list of extra bits for non-simple codes */
47
Huff_t **t, /* result: starting table */
48
int *m, /* maximum lookup bits, returns actual */
49
Vmalloc_t *vm) /* memory pool */
50
/* Given a list of code lengths and a maximum table size, make a set of
51
tables to decode that set of codes. Return zero on success, one if
52
the given code set is incomplete (the tables are still built in this
53
case), two if the input is invalid (all zero length codes or an
54
oversubscribed set of lengths), and three if not enough memory.
55
The code with value 256 is special, and the tables are constructed
56
so that no bits beyond that code are fetched when that code is
57
decoded. */
58
{
59
ulg a; /* counter for codes of length k */
60
ulg c[BMAX+1]; /* bit length count table */
61
ulg el; /* length of EOB code (value 256) */
62
ulg f; /* i repeats in table every f entries */
63
int g; /* maximum code length */
64
int h; /* table level */
65
register ulg i; /* counter, current code */
66
register ulg j; /* counter */
67
register int k; /* number of bits in current code */
68
int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */
69
int *l = lx+1; /* stack of bits per table */
70
register ulg *p; /* pointer into c[], b[], or v[] */
71
register Huff_t *q; /* points to current table */
72
Huff_t r; /* table entry for structure assignment */
73
Huff_t *u[BMAX]; /* table stack */
74
ulg v[N_MAX]; /* values in order of bit length */
75
register int w; /* bits before this table == (l * h) */
76
ulg x[BMAX+1]; /* bit offsets, then code stack */
77
ulg *xp; /* pointer into x */
78
int y; /* number of dummy codes added */
79
ulg z; /* number of entries in current table */
80
81
/* Generate counts for each bit length */
82
el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
83
memset(c, 0, sizeof(c));
84
p = b;
85
i = n;
86
do
87
{
88
c[*p]++; /* assume all entries <= BMAX */
89
p++; /* Can't combine with above line (Solaris bug) */
90
} while(--i);
91
if(c[0] == n) /* null input--all zero length codes */
92
{
93
*t = (Huff_t *)NULL;
94
*m = 0;
95
return 0;
96
}
97
98
/* Find minimum and maximum length, bound *m by those */
99
for(j = 1; j <= BMAX; j++)
100
if(c[j])
101
break;
102
k = j; /* minimum code length */
103
if((ulg)*m < j)
104
*m = j;
105
for(i = BMAX; i; i--)
106
if(c[i])
107
break;
108
g = i; /* maximum code length */
109
if((ulg)*m > i)
110
*m = i;
111
112
/* Adjust last length count to fill out codes, if needed */
113
for(y = 1 << j; j < i; j++, y <<= 1)
114
if((y -= c[j]) < 0)
115
return 2; /* bad input: more codes than bits */
116
if((y -= c[i]) < 0)
117
return 2;
118
c[i] += y;
119
120
/* Generate starting offsets into the value table for each length */
121
x[1] = j = 0;
122
p = c + 1; xp = x + 2;
123
while(--i) /* note that i == g from above */
124
*xp++ = (j += *p++);
125
126
/* Make a table of values in order of bit lengths */
127
memset(v, 0, sizeof(v));
128
p = b;
129
i = 0;
130
do
131
{
132
if((j = *p++) != 0)
133
v[x[j]++] = i;
134
} while(++i < n);
135
n = x[g]; /* set n to length of v */
136
137
/* Generate the Huffman codes and for each, make the table entries */
138
x[0] = i = 0; /* first Huffman code is zero */
139
p = v; /* grab values in bit order */
140
h = -1; /* no tables yet--level -1 */
141
w = l[-1] = 0; /* no bits decoded yet */
142
u[0] = (Huff_t *)NULL; /* just to keep compilers happy */
143
q = (Huff_t *)NULL; /* ditto */
144
z = 0; /* ditto */
145
146
/* go through the bit lengths (k already is bits in shortest code) */
147
for(; k <= g; k++)
148
{
149
a = c[k];
150
while(a--)
151
{
152
/* here i is the Huffman code of length k bits for value *p */
153
/* make tables up to required level */
154
while(k > w + l[h])
155
{
156
w += l[h++]; /* add bits already decoded */
157
158
/* compute minimum size table less than or equal to *m bits */
159
z = (z = g - w) > (ulg)*m ? *m : z; /* upper limit */
160
if((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
161
{ /* too few codes for k-w bit table */
162
f -= a + 1; /* deduct codes from patterns left */
163
xp = c + k;
164
while(++j < z)/* try smaller tables up to z bits */
165
{
166
if((f <<= 1) <= *++xp)
167
break; /* enough codes to use up j bits */
168
f -= *xp; /* else deduct codes from patterns */
169
}
170
}
171
if((ulg)w + j > el && (ulg)w < el)
172
j = el - w; /* make EOB code end at table */
173
z = 1 << j; /* table entries for j-bit table */
174
l[h] = j; /* set table size in stack */
175
176
/* allocate and link in new table */
177
q = (Huff_t *)vmalloc(vm, (z + 1)*sizeof(Huff_t));
178
if(q == NULL)
179
{
180
return 3; /* not enough memory */
181
}
182
183
*t = q + 1; /* link to list for huft_free() */
184
*(t = &(q->v.t)) = (Huff_t *)NULL;
185
u[h] = ++q; /* table starts after link */
186
187
/* connect to last table, if there is one */
188
if(h)
189
{
190
x[h] = i; /* save pattern for backing up */
191
r.b = (uch)l[h-1]; /* bits to dump before this table */
192
r.e = (uch)(16 + j);/* bits in this table */
193
r.v.t = q; /* pointer to this table */
194
j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
195
u[h-1][j] = r; /* connect to last table */
196
}
197
}
198
199
/* set up table entry in r */
200
r.b = (uch)(k - w);
201
if(p >= v + n)
202
r.e = 99; /* out of values--invalid code */
203
else if(*p < s)
204
{
205
r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
206
r.v.n = (ush)*p++; /* simple code is just the value */
207
}
208
else
209
{
210
r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
211
r.v.n = d[*p++ - s];
212
}
213
214
/* fill code-like entries with r */
215
f = 1 << (k - w);
216
for(j = i >> w; j < z; j += f)
217
q[j] = r;
218
219
/* backwards increment the k-bit code i */
220
for(j = 1 << (k - 1); i & j; j >>= 1)
221
i ^= j;
222
i ^= j;
223
224
/* backup over finished tables */
225
while((i & ((1 << w) - 1)) != x[h])
226
w -= l[--h]; /* don't need to update q */
227
}
228
}
229
230
/* return actual size of base table */
231
*m = l[0];
232
233
/* Return true (1) if we were given an incomplete table */
234
return y != 0 && g != 1;
235
}
236
237