105 #include <linux/compiler.h>
106 #ifdef NO_INFLATE_MALLOC
107 #include <linux/slab.h>
111 static char rcsid[] =
"#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
116 #if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
117 # include <sys/types.h>
150 const ush *,
const ush *,
struct huft **,
int *));
170 #define flush_output(w) (wp=(w),flush_window())
173 static const unsigned border[] = {
174 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
175 static const ush cplens[] = {
176 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
177 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
179 static const ush cplext[] = {
180 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
181 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99};
182 static const ush cpdist[] = {
183 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
184 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
185 8193, 12289, 16385, 24577};
186 static const ush cpdext[] = {
187 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
188 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
228 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
229 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
232 #define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
233 #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
234 #define DUMPBITS(n) {b>>=(n);k-=(n);}
236 #ifndef NO_INFLATE_MALLOC
241 static unsigned long malloc_ptr;
242 static int malloc_count;
249 error(
"Malloc error");
253 malloc_ptr = (malloc_ptr + 3) & ~3;
255 p = (
void *)malloc_ptr;
259 error(
"Out of memory");
265 static void free(
void *where)
272 #define malloc(a) kmalloc(a, GFP_KERNEL)
273 #define free(a) kfree(a)
344 register unsigned *
p;
345 register struct huft *
q;
363 stk =
malloc(
sizeof(*stk));
373 memzero(stk->c,
sizeof(stk->c));
376 Tracecv(*p, (stderr, (n-i >=
' ' && n-i <=
'~' ?
"%c %d\n" :
"0x%x %d\n"),
393 for (j = 1; j <=
BMAX; j++)
399 for (i =
BMAX;
i; i--)
410 for (y = 1 << j; j <
i; j++, y <<= 1)
411 if ((y -= c[j]) < 0) {
415 if ((y -= c[i]) < 0) {
425 p = c + 1; xp = x + 2;
469 z = (z = g -
w) > (
unsigned)l ? l : z;
470 if ((f = 1 << (j = k - w)) > a + 1)
478 if ((f <<= 1) <= *++xp)
522 r.
e = (
uch)(*p < 256 ? 16 : 15);
528 r.
e = (
uch)e[*p - s];
535 for (j = i >> w; j < z; j +=
f)
539 for (j = 1 << (k - 1); i &
j; j >>= 1)
544 while ((i & ((1 << w) - 1)) != x[
h])
557 ret = y != 0 && g != 1;
573 register struct huft *
p, *
q;
617 if ((e = (t = tl + ((
unsigned)b & ml))->e) > 16)
624 }
while ((e = (t = t->
v.
t + ((
unsigned)b & mask_bits[e]))->e) > 16);
644 n = t->
v.
n + ((unsigned)b & mask_bits[e]);
649 if ((e = (t = td + ((
unsigned)b & md))->e) > 16)
656 }
while ((e = (t = t->
v.
t + ((
unsigned)b & mask_bits[e]))->e) > 16);
659 d = w - t->
v.
n - ((unsigned)b & mask_bits[e]);
661 Tracevv((stderr,
"\\[%d,%d]", w-d, n));
665 n -= (e = (e =
WSIZE - ((d &=
WSIZE-1) > w ? d : w)) > n ? n : e);
666 #if !defined(NOMEMCPY) && !defined(DEBUG)
726 n = ((unsigned)b & 0xffff);
729 if (n != (
unsigned)((~b) & 0xffff))
778 l =
malloc(
sizeof(*l) * 288);
783 for (i = 0; i < 144; i++)
792 if ((i =
huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
798 for (i = 0; i < 30; i++)
801 if ((i =
huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
850 #ifdef PKZIP_BUG_WORKAROUND
851 ll =
malloc(
sizeof(*ll) * (288+32));
853 ll =
malloc(
sizeof(*ll) * (286+30));
866 nl = 257 + ((unsigned)b & 0x1f);
869 nd = 1 + ((unsigned)b & 0x1f);
872 nb = 4 + ((unsigned)b & 0xf);
874 #ifdef PKZIP_BUG_WORKAROUND
875 if (nl > 288 || nd > 32)
877 if (nl > 286 || nd > 30)
887 for (j = 0; j < nb; j++)
890 ll[border[
j]] = (unsigned)b & 7;
914 while ((
unsigned)i < n)
917 j = (td = tl + ((unsigned)b & m))->b;
925 j = 3 + ((unsigned)b & 3);
927 if ((
unsigned)i + j > n) {
937 j = 3 + ((unsigned)b & 7);
939 if ((
unsigned)i + j > n) {
950 j = 11 + ((unsigned)b & 0x7f);
952 if ((
unsigned)i + j > n) {
977 if ((i =
huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
981 error(
"incomplete literal tree");
989 if ((i =
huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
993 error(
"incomplete distance tree");
994 #ifdef PKZIP_BUG_WORKAROUND
1040 register unsigned k;
1057 t = (unsigned)b & 3;
1101 #ifdef ARCH_HAS_DECOMP_WDOG
1136 static ulg crc_32_tab[256];
1138 #define CRC_VALUE (crc ^ 0xffffffffUL)
1156 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
1160 for (i = 0; i <
sizeof(
p)/
sizeof(
int); i++)
1161 e |= 1
L << (31 - p[i]);
1165 for (i = 1; i < 256; i++)
1168 for (k = i | 256; k != 1; k >>= 1)
1170 c = c & 1 ? (c >> 1) ^ e : c >> 1;
1178 crc = (
ulg)0xffffffffUL;
1182 #define ASCII_FLAG 0x01
1183 #define CONTINUATION 0x02
1184 #define EXTRA_FIELD 0x04
1185 #define ORIG_NAME 0x08
1186 #define COMMENT 0x10
1187 #define ENCRYPTED 0x20
1188 #define RESERVED 0xC0
1196 unsigned char magic[2];
1206 if (magic[0] != 037 ||
1207 ((magic[1] != 0213) && (magic[1] != 0236))) {
1208 error(
"bad gzip magic numbers");
1214 error(
"internal error, invalid method");
1220 error(
"Input is encrypted");
1224 error(
"Multi part input");
1228 error(
"Input has invalid flags");
1262 error(
"invalid compressed format (err=1)");
1265 error(
"invalid compressed format (err=2)");
1268 error(
"out of memory");
1271 error(
"out of input data");
1274 error(
"invalid compressed format (other)");
1298 if (orig_len != bytes_out) {
1299 error(
"length error");
1305 error(
"out of input data");