94 #define MAXCODES (MAXLCODES+MAXDCODES) 105 const unsigned char *
in;
132 while (s->
bitcnt < need) {
140 s->
bitbuf = (int)(val >> need);
144 return (
int)(val & ((1L << need) - 1));
177 if (s->
in[s->
incnt++] != (~len & 0xff) ||
178 s->
in[s->
incnt++] != ((~len >> 8) & 0xff))
243 code = first = index = 0;
244 for (len = 1; len <=
MAXBITS; len++) {
246 count = h->
count[len];
247 if (code - count < first)
276 code = first = index = 0;
284 if (code - count < first) {
348 for (len = 0; len <=
MAXBITS; len++)
350 for (symbol = 0; symbol < n; symbol++)
351 (h->
count[length[symbol]])++;
352 if (h->
count[0] == n)
357 for (len = 1; len <=
MAXBITS; len++) {
359 left -= h->
count[len];
366 for (len = 1; len <
MAXBITS; len++)
367 offs[len + 1] = offs[len] + h->
count[len];
373 for (symbol = 0; symbol < n; symbol++)
374 if (length[symbol] != 0)
375 h->
symbol[offs[length[symbol]]++] = symbol;
438 const struct huffman *distcode)
443 static const short lens[29] = {
444 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
445 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
446 static const short lext[29] = {
447 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
448 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
449 static const short dists[30] = {
450 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
451 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
452 8193, 12289, 16385, 24577};
453 static const short dext[30] = {
454 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
455 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
460 symbol =
decode(s, lencode);
472 else if (symbol > 256) {
477 len = lens[symbol] +
bits(s, lext[symbol]);
480 symbol =
decode(s, distcode);
483 dist = dists[symbol] +
bits(s, dext[symbol]);
484 #ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 495 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 506 }
while (symbol != 256);
538 static int virgin = 1;
541 static struct huffman lencode, distcode;
549 lencode.
count = lencnt;
551 distcode.
count = distcnt;
552 distcode.
symbol = distsym;
555 for (symbol = 0; symbol < 144; symbol++)
557 for (; symbol < 256; symbol++)
559 for (; symbol < 280; symbol++)
566 for (symbol = 0; symbol <
MAXDCODES; symbol++)
568 construct(&distcode, lengths, MAXDCODES);
575 return codes(s, &lencode, &distcode);
667 int nlen, ndist, ncode;
673 struct huffman lencode, distcode;
674 static const short order[19] =
675 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
678 lencode.
count = lencnt;
680 distcode.
count = distcnt;
681 distcode.
symbol = distsym;
684 nlen =
bits(s, 5) + 257;
685 ndist =
bits(s, 5) + 1;
686 ncode =
bits(s, 4) + 4;
691 for (index = 0; index < ncode; index++)
692 lengths[order[index]] =
bits(s, 3);
693 for (; index < 19; index++)
694 lengths[order[index]] = 0;
703 while (index < nlen + ndist) {
707 symbol =
decode(s, &lencode);
711 lengths[index++] =
symbol;
717 len = lengths[index - 1];
718 symbol = 3 +
bits(s, 2);
720 else if (symbol == 17)
721 symbol = 3 +
bits(s, 3);
723 symbol = 11 +
bits(s, 7);
724 if (index + symbol > nlen + ndist)
727 lengths[index++] = len;
732 if (lengths[256] == 0)
736 err =
construct(&lencode, lengths, nlen);
737 if (err && (err < 0 || nlen != lencode.
count[0] + lencode.
count[1]))
741 err =
construct(&distcode, lengths + nlen, ndist);
742 if (err && (err < 0 || ndist != distcode.
count[0] + distcode.
count[1]))
746 return codes(s, &lencode, &distcode);
794 unsigned long *destlen,
795 const unsigned char *source,
796 unsigned long *sourcelen)
809 s.
inlen = *sourcelen;
815 if (setjmp(s.
env) != 0)
837 *sourcelen = s.
incnt;
static int fixed(struct state *s)
int puff(unsigned char *dest, unsigned long *destlen, const unsigned char *source, unsigned long *sourcelen)
static int construct(struct huffman *h, const short *length, int n)
static int stored(struct state *s)
static int bits(struct state *s, int need)
static int decode(struct state *s, const struct huffman *h)
static int dynamic(struct state *s)
static int codes(struct state *s, const struct huffman *lencode, const struct huffman *distcode)
static big_t count(int syms, int len, int left)