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];
53 /* litlen code words 257-285 extra bits */
54 static int litlenextra[Nlitlen-257] =
57 /* 260 */ 0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
58 /* 270 */ 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
59 /* 280 */ 4, 5, 5, 5, 5, 0, 0, 0
62 static int litlenbase[Nlitlen-257];
64 /* offset code word extra bits */
65 static int offextra[Noff] =
67 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
68 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
69 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
72 static int offbase[Noff];
74 /* order code lengths */
75 static int clenorder[Nclen] =
77 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
80 /* for static huffman tables */
81 static Huff litlentab;
83 static uchar revtab[256];
85 static int uncblock(Input *in, History*);
86 static int fixedblock(Input *in, History*);
87 static int dynamicblock(Input *in, History*);
88 static int sregfill(Input *in, int n);
89 static int sregunget(Input *in);
90 static int decode(Input*, History*, Huff*, Huff*);
91 static int hufftab(Huff*, char*, int, int);
92 static int hdecsym(Input *in, Huff *h, int b);
100 /* byte reverse table */
104 revtab[i] |= 0x80 >> j;
106 for(i=257,base=3; i<Nlitlen; i++) {
107 litlenbase[i-257] = base;
108 base += 1<<litlenextra[i-257];
110 /* strange table entry in spec... */
111 litlenbase[285-257]--;
113 for(i=0,base=1; i<Noff; i++) {
115 base += 1<<offextra[i];
118 len = malloc(MaxLeaf);
122 /* static Litlen bit lengths */
125 for(i=144; i<256; i++)
127 for(i=256; i<280; i++)
129 for(i=280; i<Nlitlen; i++)
132 if(!hufftab(&litlentab, len, Nlitlen, MaxFlatBits))
133 return FlateInternal;
135 /* static Offset bit lengths */
136 for(i=0; i<Noff; i++)
139 if(!hufftab(&offtab, len, Noff, MaxFlatBits))
140 return FlateInternal;
147 inflate(void *wr, int (*w)(void*, void*, int), void *getr, int (*get)(void*))
153 his = malloc(sizeof(History));
167 if(!sregfill(&in, 3))
169 final = in.sreg & 0x1;
170 type = (in.sreg>>1) & 0x3;
175 in.error = FlateCorrupted;
179 if(!uncblock(&in, his))
184 if(!fixedblock(&in, his))
188 /* dynamic huffman */
189 if(!dynamicblock(&in, his))
195 if(his->cp != his->his && (*w)(wr, his->his, his->cp - his->his) != his->cp - his->his) {
196 in.error = FlateOutputFail;
204 if(in.error != FlateOk)
205 return FlateInternal;
210 if(in.error == FlateOk)
211 return FlateInternal;
216 uncblock(Input *in, History *his)
223 len = (*in->get)(in->getr);
224 len |= (*in->get)(in->getr)<<8;
225 nlen = (*in->get)(in->getr);
226 nlen |= (*in->get)(in->getr)<<8;
227 if(len != (~nlen&0xffff)) {
228 in->error = FlateCorrupted;
234 he = hs + HistorySize;
237 c = (*in->get)(in->getr);
243 if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
244 in->error = FlateOutputFail;
258 fixedblock(Input *in, History *his)
260 return decode(in, his, &litlentab, &offtab);
264 dynamicblock(Input *in, History *his)
266 Huff *lentab, *offtab;
268 int i, j, n, c, nlit, ndist, nclen, res, nb;
270 if(!sregfill(in, 14))
272 nlit = (in->sreg&0x1f) + 257;
273 ndist = ((in->sreg>>5) & 0x1f) + 1;
274 nclen = ((in->sreg>>10) & 0xf) + 4;
278 if(nlit > Nlitlen || ndist > Noff || nlit < 257) {
279 in->error = FlateCorrupted;
283 /* huff table header */
284 len = malloc(Nlitlen+Noff);
285 lentab = malloc(sizeof(Huff));
286 offtab = malloc(sizeof(Huff));
287 if(len == nil || lentab == nil || offtab == nil){
288 in->error = FlateNoMem;
291 for(i=0; i < Nclen; i++)
293 for(i=0; i<nclen; i++) {
296 len[clenorder[i]] = in->sreg & 0x7;
301 if(!hufftab(lentab, len, Nclen, ClenBits)){
302 in->error = FlateCorrupted;
308 nb = lentab->minbits;
310 if(in->nbits<nb && !sregfill(in, nb))
312 c = lentab->flat[in->sreg & lentab->flatmask];
317 c = hdecsym(in, lentab, c);
331 if(in->nbits<2 && !sregfill(in, 2))
333 j = (in->sreg&0x3)+3;
337 in->error = FlateCorrupted;
342 if(in->nbits<3 && !sregfill(in, 3))
344 j = (in->sreg&0x7)+3;
349 if(in->nbits<7 && !sregfill(in, 7))
351 j = (in->sreg&0x7f)+11;
356 in->error = FlateCorrupted;
361 in->error = FlateCorrupted;
372 if(!hufftab(lentab, len, nlit, LitlenBits)
373 || !hufftab(offtab, &len[nlit], ndist, OffBits)){
374 in->error = FlateCorrupted;
378 res = decode(in, his, lentab, offtab);
394 decode(Input *in, History *his, Huff *litlentab, Huff *offtab)
397 uchar *hs, *hp, *hq, *he;
402 he = hs + HistorySize;
406 nb = litlentab->minbits;
408 if(in->nbits<nb && !sregfill(in, nb))
410 c = litlentab->flat[in->sreg & litlentab->flatmask];
415 c = hdecsym(in, litlentab, c);
431 if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
432 in->error = FlateOutputFail;
444 in->error = FlateCorrupted;
450 if(in->nbits < nb && !sregfill(in, nb))
452 len = litlenbase[c] + (in->sreg & ((1<<nb)-1));
457 nb = offtab->minbits;
459 if(in->nbits<nb && !sregfill(in, nb))
461 c = offtab->flat[in->sreg & offtab->flatmask];
466 c = hdecsym(in, offtab, c);
478 in->error = FlateCorrupted;
483 if(in->nbits < nb && !sregfill(in, nb))
486 off = offbase[c] + (in->sreg & ((1<<nb)-1));
493 in->error = FlateCorrupted;
499 /* slow but correct */
508 if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
509 in->error = FlateOutputFail;
525 revcode(int c, int b)
527 /* shift encode up so it starts on bit 15 then reverse */
529 c = revtab[c>>8] | (revtab[c&0xff]<<8);
534 * construct the huffman decoding arrays and a fast lookup table.
535 * the fast lookup is a table indexed by the next flatbits bits,
536 * which returns the symbol matched and the number of bits consumed,
537 * or the minimum number of bits needed and 0xff if more than flatbits
540 * flatbits can be longer than the smallest huffman code,
541 * because shorter codes are assigned smaller lexical prefixes.
542 * this means assuming zeros for the next few bits will give a
543 * conservative answer, in the sense that it will either give the
544 * correct answer, or return the minimum number of bits which
545 * are needed for an answer.
548 hufftab(Huff *h, char *hb, int maxleaf, int flatbits)
550 ulong bitcount[MaxHuffBits];
551 ulong c, fc, ec, mincode, code, nc[MaxHuffBits];
552 int i, b, minbits, maxbits;
554 for(i = 0; i < MaxHuffBits; i++)
557 minbits = MaxHuffBits + 1;
558 for(i=0; i < maxleaf; i++){
569 h->maxbits = maxbits;
578 for(b = 0; b <= maxbits; b++){
583 code = mincode + bitcount[b];
586 h->maxcode[b] = code - 1;
587 h->last[b] += code - 1;
590 if(flatbits > maxbits)
592 h->flatmask = (1 << flatbits) - 1;
593 if(minbits > flatbits)
595 h->minbits = minbits;
598 for(i = 0; i < b; i++)
602 * initialize the flat table to include the minimum possible
603 * bit length for each code prefix
605 for(b = maxbits; b > flatbits; b--){
606 code = h->maxcode[b];
609 mincode = code + 1 - bitcount[b];
610 mincode >>= b - flatbits;
611 code >>= b - flatbits;
612 for(; mincode <= code; mincode++)
613 h->flat[revcode(mincode, flatbits)] = (b << 8) | 0xff;
616 for(i = 0; i < maxleaf; i++){
623 ec = (c + 1) << (flatbits - b);
624 if(ec > (1<<flatbits))
625 return 0; /* this is actually an internal error */
626 for(fc = c << (flatbits - b); fc < ec; fc++)
627 h->flat[revcode(fc, flatbits)] = code;
640 hdecsym(Input *in, Huff *h, int nb)
644 if((nb & 0xff) == 0xff)
648 for(; nb <= h->maxbits; nb++){
649 if(in->nbits<nb && !sregfill(in, nb))
651 c = revtab[in->sreg&0xff]<<8;
652 c |= revtab[(in->sreg>>8)&0xff];
654 if(c <= h->maxcode[nb]){
657 return h->decode[h->last[nb] - c];
660 in->error = FlateCorrupted;
665 sregfill(Input *in, int n)
669 while(n > in->nbits) {
670 c = (*in->get)(in->getr);
672 in->error = FlateInputFail;
675 in->sreg |= c<<in->nbits;
685 in->error = FlateInternal;
689 /* throw other bits on the floor */