8 MaxHuffBits= 17, /* maximum bits in a encoded code */
9 Nlitlen= 288, /* number of litlen codes */
10 Noff= 32, /* number of offset codes */
11 Nclen= 19, /* number of codelen codes */
12 LenShift= 10, /* code = len<<LenShift|code */
13 LitlenBits= 7, /* number of bits in litlen decode table */
14 OffBits= 6, /* number of bits in offset decode table */
15 ClenBits= 6, /* number of bits in code len decode table */
16 MaxFlatBits= LitlenBits,
20 typedef struct Input Input;
21 typedef struct History History;
22 typedef struct Huff Huff;
26 int error; /* first error encountered, or FlateOk */
28 int (*w)(void*, void*, int);
37 uchar his[HistorySize];
38 uchar *cp; /* current pointer in history */
39 int full; /* his has been filled up at least once */
44 int maxbits; /* max bits for any code */
45 int minbits; /* min bits to get before looking in flat */
46 int flatmask; /* bits used in "flat" fast decoding table */
47 ulong flat[1<<MaxFlatBits];
48 ulong maxcode[MaxHuffBits];
49 ulong last[MaxHuffBits];
50 ulong decode[MaxLeaf];
54 /* litlen code words 257-285 extra bits */
55 static int litlenextra[Nlitlen-257] =
58 /* 260 */ 0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
59 /* 270 */ 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
60 /* 280 */ 4, 5, 5, 5, 5, 0, 0, 0
63 static int litlenbase[Nlitlen-257];
65 /* offset code word extra bits */
66 static int offextra[Noff] =
68 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
69 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
70 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
73 static int offbase[Noff];
75 /* order code lengths */
76 static int clenorder[Nclen] =
78 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
81 /* for static huffman tables */
82 static Huff litlentab;
84 static uchar revtab[256];
86 static int uncblock(Input *in, History*);
87 static int fixedblock(Input *in, History*);
88 static int dynamicblock(Input *in, History*);
89 static int sregfill(Input *in, int n);
90 static int sregunget(Input *in);
91 static int decode(Input*, History*, Huff*, Huff*);
92 static int hufftab(Huff*, char*, int, int);
93 static int hdecsym(Input *in, Huff *h, int b);
101 /* byte reverse table */
105 revtab[i] |= 0x80 >> j;
107 for(i=257,base=3; i<Nlitlen; i++) {
108 litlenbase[i-257] = base;
109 base += 1<<litlenextra[i-257];
111 /* strange table entry in spec... */
112 litlenbase[285-257]--;
114 for(i=0,base=1; i<Noff; i++) {
116 base += 1<<offextra[i];
119 len = malloc(MaxLeaf);
123 /* static Litlen bit lengths */
126 for(i=144; i<256; i++)
128 for(i=256; i<280; i++)
130 for(i=280; i<Nlitlen; i++)
133 if(!hufftab(&litlentab, len, Nlitlen, MaxFlatBits))
134 return FlateInternal;
136 /* static Offset bit lengths */
137 for(i=0; i<Noff; i++)
140 if(!hufftab(&offtab, len, Noff, MaxFlatBits))
141 return FlateInternal;
148 inflate(void *wr, int (*w)(void*, void*, int), void *getr, int (*get)(void*))
154 his = malloc(sizeof(History));
168 if(!sregfill(&in, 3))
170 final = in.sreg & 0x1;
171 type = (in.sreg>>1) & 0x3;
176 in.error = FlateCorrupted;
180 if(!uncblock(&in, his))
185 if(!fixedblock(&in, his))
189 /* dynamic huffman */
190 if(!dynamicblock(&in, his))
196 if(his->cp != his->his && (*w)(wr, his->his, his->cp - his->his) != his->cp - his->his) {
197 in.error = FlateOutputFail;
205 if(in.error != FlateOk)
206 return FlateInternal;
211 if(in.error == FlateOk)
212 return FlateInternal;
217 uncblock(Input *in, History *his)
224 len = (*in->get)(in->getr);
225 len |= (*in->get)(in->getr)<<8;
226 nlen = (*in->get)(in->getr);
227 nlen |= (*in->get)(in->getr)<<8;
228 if(len != (~nlen&0xffff)) {
229 in->error = FlateCorrupted;
235 he = hs + HistorySize;
238 c = (*in->get)(in->getr);
244 if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
245 in->error = FlateOutputFail;
259 fixedblock(Input *in, History *his)
261 return decode(in, his, &litlentab, &offtab);
265 dynamicblock(Input *in, History *his)
267 Huff *lentab, *offtab;
269 int i, j, n, c, nlit, ndist, nclen, res, nb;
271 if(!sregfill(in, 14))
273 nlit = (in->sreg&0x1f) + 257;
274 ndist = ((in->sreg>>5) & 0x1f) + 1;
275 nclen = ((in->sreg>>10) & 0xf) + 4;
279 if(nlit > Nlitlen || ndist > Noff || nlit < 257) {
280 in->error = FlateCorrupted;
284 /* huff table header */
285 len = malloc(Nlitlen+Noff);
286 lentab = malloc(sizeof(Huff));
287 offtab = malloc(sizeof(Huff));
288 if(len == nil || lentab == nil || offtab == nil){
289 in->error = FlateNoMem;
292 for(i=0; i < Nclen; i++)
294 for(i=0; i<nclen; i++) {
297 len[clenorder[i]] = in->sreg & 0x7;
302 if(!hufftab(lentab, len, Nclen, ClenBits)){
303 in->error = FlateCorrupted;
309 nb = lentab->minbits;
311 if(in->nbits<nb && !sregfill(in, nb))
313 c = lentab->flat[in->sreg & lentab->flatmask];
318 c = hdecsym(in, lentab, c);
332 if(in->nbits<2 && !sregfill(in, 2))
334 j = (in->sreg&0x3)+3;
338 in->error = FlateCorrupted;
343 if(in->nbits<3 && !sregfill(in, 3))
345 j = (in->sreg&0x7)+3;
350 if(in->nbits<7 && !sregfill(in, 7))
352 j = (in->sreg&0x7f)+11;
357 in->error = FlateCorrupted;
362 in->error = FlateCorrupted;
373 if(!hufftab(lentab, len, nlit, LitlenBits)
374 || !hufftab(offtab, &len[nlit], ndist, OffBits)){
375 in->error = FlateCorrupted;
379 res = decode(in, his, lentab, offtab);
395 decode(Input *in, History *his, Huff *litlentab, Huff *offtab)
398 uchar *hs, *hp, *hq, *he;
403 he = hs + HistorySize;
407 nb = litlentab->minbits;
409 if(in->nbits<nb && !sregfill(in, nb))
411 c = litlentab->flat[in->sreg & litlentab->flatmask];
416 c = hdecsym(in, litlentab, c);
432 if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
433 in->error = FlateOutputFail;
445 in->error = FlateCorrupted;
451 if(in->nbits < nb && !sregfill(in, nb))
453 len = litlenbase[c] + (in->sreg & ((1<<nb)-1));
458 nb = offtab->minbits;
460 if(in->nbits<nb && !sregfill(in, nb))
462 c = offtab->flat[in->sreg & offtab->flatmask];
467 c = hdecsym(in, offtab, c);
479 in->error = FlateCorrupted;
484 if(in->nbits < nb && !sregfill(in, nb))
487 off = offbase[c] + (in->sreg & ((1<<nb)-1));
494 in->error = FlateCorrupted;
500 /* slow but correct */
509 if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
510 in->error = FlateOutputFail;
526 revcode(int c, int b)
528 /* shift encode up so it starts on bit 15 then reverse */
530 c = revtab[c>>8] | (revtab[c&0xff]<<8);
535 * construct the huffman decoding arrays and a fast lookup table.
536 * the fast lookup is a table indexed by the next flatbits bits,
537 * which returns the symbol matched and the number of bits consumed,
538 * or the minimum number of bits needed and 0xff if more than flatbits
541 * flatbits can be longer than the smallest huffman code,
542 * because shorter codes are assigned smaller lexical prefixes.
543 * this means assuming zeros for the next few bits will give a
544 * conservative answer, in the sense that it will either give the
545 * correct answer, or return the minimum number of bits which
546 * are needed for an answer.
549 hufftab(Huff *h, char *hb, int maxleaf, int flatbits)
551 ulong bitcount[MaxHuffBits];
552 ulong c, fc, ec, mincode, code, nc[MaxHuffBits];
553 int i, b, minbits, maxbits;
555 for(i = 0; i < MaxHuffBits; i++)
558 minbits = MaxHuffBits + 1;
559 for(i=0; i < maxleaf; i++){
576 h->maxbits = maxbits;
577 if(maxbits >= MaxHuffBits || minbits <= 0)
581 for(b = 0; b <= maxbits; b++){
586 code = mincode + bitcount[b];
589 h->maxcode[b] = code - 1;
590 h->last[b] += code - 1;
593 if(flatbits > maxbits)
595 h->flatmask = (1 << flatbits) - 1;
596 if(minbits > flatbits)
598 h->minbits = minbits;
601 for(i = 0; i < b; i++)
605 * initialize the flat table to include the minimum possible
606 * bit length for each code prefix
608 for(b = maxbits; b > flatbits; b--){
609 code = h->maxcode[b];
612 mincode = code + 1 - bitcount[b];
613 mincode >>= b - flatbits;
614 code >>= b - flatbits;
615 for(; mincode <= code; mincode++)
616 h->flat[revcode(mincode, flatbits)] = (b << 8) | 0xff;
619 h->maxleaf = maxleaf;
620 for(i = 0; i < maxleaf; i++){
627 ec = (c + 1) << (flatbits - b);
628 if(ec > (1<<flatbits))
629 return 0; /* this is actually an internal error */
630 for(fc = c << (flatbits - b); fc < ec; fc++)
631 h->flat[revcode(fc, flatbits)] = code;
644 hdecsym(Input *in, Huff *h, int nb)
648 if((nb & 0xff) == 0xff)
652 for(; nb <= h->maxbits; nb++){
653 if(in->nbits<nb && !sregfill(in, nb))
655 c = revtab[in->sreg&0xff]<<8;
656 c |= revtab[(in->sreg>>8)&0xff];
658 if(c <= h->maxcode[nb]){
667 in->error = FlateCorrupted;
672 sregfill(Input *in, int n)
676 while(n > in->nbits) {
677 c = (*in->get)(in->getr);
679 in->error = FlateInputFail;
682 in->sreg |= c<<in->nbits;
692 in->error = FlateInternal;
696 /* throw other bits on the floor */