11 #include <sys/cdefs.h>
14 #include <sys/param.h>
15 #include <sys/inflate.h>
17 #include <sys/systm.h>
18 #include <sys/kernel.h>
20 #include <sys/malloc.h>
33 #define memzero(dest,len) bzero(dest,len)
37 #define FPRINTF printf
39 extern void putstr (
char *);
40 #define FPRINTF putstr
43 #define FLUSH(x,y) { \
44 int foo = (*x->gz_output)(x->gz_private,x->gz_slide,y); \
52 extern unsigned char *kzipmalloc (
int);
53 extern void kzipfree (
void*);
54 #define malloc(x, y, z) kzipmalloc((x))
55 #define free(x, y) kzipfree((x))
234 #define PKZIP_BUG_WORKAROUND
299 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
302 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
303 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
307 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
308 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99};
311 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
312 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
313 8193, 12289, 16385, 24577};
316 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
317 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
323 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
324 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
353 #define NEEDBITS(glbl,n) { \
355 int c=(*glbl->gz_input)(glbl->gz_private); \
363 #define DUMPBITS(n) {b>>=(n);k-=(n);}
426 unsigned c[
BMAX + 1];
436 register unsigned *p;
437 register struct huft *q;
442 unsigned x[
BMAX + 1];
448 el = n > 256 ? b[256] :
BMAX;
450 memzero((
char *) c,
sizeof(c));
452 for (i = 0; i <
BMAX+1; i++)
462 *t = (
struct huft *) NULL;
467 for (j = 1; j <=
BMAX; j++)
471 if ((
unsigned) *m < j)
473 for (i = BMAX; i; i--)
477 if ((
unsigned) *m > i)
481 for (y = 1 << j; j < i; j++, y <<= 1)
509 u[0] = (
struct huft *) NULL;
510 q = (
struct huft *) NULL;
514 for (; k <= g; k++) {
522 while (k > w + l[h]) {
529 z = (z = g - w) > (
unsigned) *m ? *m : z;
530 if ((f = 1 << (j = k - w)) > a + 1) {
538 if ((f <<= 1) <= *++xp)
545 if ((
unsigned) w + j > el && (unsigned) w < el)
553 if ((q = (
struct huft *)
malloc((z + 1) *
sizeof(
struct huft), M_GZIP, M_WAITOK)) ==
554 (
struct huft *) NULL) {
559 glbl->gz_hufts += z + 1;
562 *(t = &(q->
v.
t)) = (
struct huft *) NULL;
569 r.
b = (
uch) l[h - 1];
571 r.
e = (
uch) (16 + j);
573 j = (i & ((1 << w) - 1)) >> (w - l[h - 1]);
584 r.
e = (
uch) (*p < 256 ? 16 : 15);
589 r.
e = (
uch) e[*p - s];
596 for (j = i >> w; j < z; j += f)
600 for (j = 1 << (k - 1); i & j; j >>= 1)
605 while ((i & ((1 << w) - 1)) != x[h])
614 return y != 0 && g != 1;
625 register struct huft *p, *q;
629 while (p != (
struct huft *) NULL) {
642 struct
huft *tl, *td;
663 if ((e = (t = tl + ((
unsigned) b & ml))->e) > 16)
670 }
while ((e = (t = t->
v.
t + ((
unsigned) b &
mask[e]))->e) > 16);
673 glbl->gz_slide[w++] = (
uch) t->
v.
n;
685 n = t->
v.
n + ((unsigned) b &
mask[e]);
690 if ((e = (t = td + ((
unsigned) b & md))->e) > 16)
697 }
while ((e = (t = t->
v.
t + ((
unsigned) b &
mask[e]))->e) > 16);
700 d = w - t->
v.
n - ((unsigned) b &
mask[e]);
704 n -= (e = (e = GZ_WSIZE - ((d &= GZ_WSIZE - 1) > w ? d : w)) > n ? n : e);
708 memcpy(glbl->gz_slide + w, glbl->gz_slide + d, e);
715 glbl->gz_slide[w++] = glbl->gz_slide[d++];
755 n = ((unsigned) b & 0xffff);
758 if (n != (
unsigned) ((~b) & 0xffff))
764 glbl->gz_slide[w++] = (
uch) b;
787 if (glbl->gz_fixed_tl == (
struct huft *) NULL) {
789 static unsigned l[288];
792 for (i = 0; i < 144; i++)
801 glbl->gz_fixed_bl = 7;
803 &glbl->gz_fixed_tl, &glbl->gz_fixed_bl)) != 0) {
804 glbl->gz_fixed_tl = (
struct huft *) NULL;
808 for (i = 0; i < 30; i++)
811 glbl->gz_fixed_bd = 5;
813 &glbl->gz_fixed_td, &glbl->gz_fixed_bd)) > 1) {
815 glbl->gz_fixed_tl = (
struct huft *) NULL;
820 return inflate_codes(glbl, glbl->gz_fixed_tl, glbl->gz_fixed_td, glbl->gz_fixed_bl, glbl->gz_fixed_bd) != 0;
840 #ifdef PKZIP_BUG_WORKAROUND
841 unsigned ll[288 + 32];
844 unsigned ll[286 + 30];
856 nl = 257 + ((unsigned) b & 0x1f);
860 nd = 1 + ((unsigned) b & 0x1f);
863 nb = 4 + ((unsigned) b & 0xf);
865 #ifdef PKZIP_BUG_WORKAROUND
866 if (nl > 288 || nd > 32)
868 if (nl > 286 || nd > 30)
872 for (j = 0; j < nb; j++) {
874 ll[
border[j]] = (unsigned) b & 7;
882 if ((i =
huft_build(glbl, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
891 while ((
unsigned) i < n) {
893 j = (td = tl + ((unsigned) b & m))->b;
900 j = 3 + ((unsigned) b & 3);
902 if ((
unsigned) i + j > n)
906 }
else if (j == 17) {
908 j = 3 + ((unsigned) b & 7);
910 if ((
unsigned) i + j > n)
917 j = 11 + ((unsigned) b & 0x7f);
919 if ((
unsigned) i + j > n)
938 if (i == 1 && !
qflag) {
939 FPRINTF(
"(incomplete l-tree) ");
947 if (i == 1 && !
qflag) {
948 FPRINTF(
"(incomplete d-tree) ");
949 #ifdef PKZIP_BUG_WORKAROUND
989 t = (unsigned) b & 3;
1017 glbl->gz_fixed_tl = (
struct huft *) NULL;
1030 if (glbl->gz_hufts > h)
1035 FLUSH(glbl, glbl->gz_wp);
1050 if (!glbl->gz_slide)
1051 p = glbl->gz_slide =
malloc(GZ_WSIZE, M_GZIP, M_WAITOK);
1053 if (!glbl->gz_slide)
1061 if (glbl->gz_fixed_td != (
struct huft *) NULL) {
1063 glbl->gz_fixed_td = (
struct huft *) NULL;
1065 if (glbl->gz_fixed_tl != (
struct huft *) NULL) {
1067 glbl->gz_fixed_tl = (
struct huft *) NULL;
1070 if (p == glbl->gz_slide) {
1071 free(glbl->gz_slide, M_GZIP);
1072 glbl->gz_slide = NULL;
static int inflate_block(struct inflate *, int *)
static const unsigned border[]
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
#define NEEDBITS(glbl, n)
static int inflate_codes(struct inflate *, struct huft *, struct huft *, int, int)
int inflate(struct inflate *glbl)
static int huft_build(struct inflate *, unsigned *, unsigned, unsigned, const ush *, const ush *, struct huft **, int *)
static int inflate_stored(struct inflate *)
static MALLOC_DEFINE(M_GZIP,"gzip_trees","Gzip trees")
static const ush cpdext[]
static int xinflate(struct inflate *)
static const ush cplens[]
static int huft_free(struct inflate *, struct huft *)
void free(void *addr, struct malloc_type *mtp)
static int inflate_fixed(struct inflate *)
static const ush cpdist[]
static const ush cplext[]
#define memzero(dest, len)
static int inflate_dynamic(struct inflate *)