4 * This is a collection of several routines from gzip-1.0.3
7 * Ported to 386bsd by Serge Vakulenko
16 extern const char input_data[];
17 extern const int input_len;
27 void putstr (char *c);
28 void *memcpy (void *to, const void *from, unsigned len);
29 int memcmp (const void *arg1, const void *arg2, unsigned len);
31 ulong crc; /* shift register contents */
32 ulong crc_32_tab[256]; /* crc table, defined below */
35 * Run a set of bytes through the crc shift register. If s is a NULL
36 * pointer, then initialize the crc shift register contents instead.
37 * Return the current crc in either case.
40 uchar *s; /* pointer to bytes to pump through */
41 unsigned n; /* number of bytes in s[] */
44 crc = crc_32_tab[(uchar)crc ^ (*s++)] ^ (crc >> 8);
48 * Clear input and output buffers
57 * Fill the input buffer. This is called only when the buffer is empty
58 * and at least one byte is really needed.
64 /* Read as much as possible */
67 len = INBUFSIZ - insize;
68 if (len > input_len - input_ptr + 1)
69 len = input_len-input_ptr+1;
73 inbuf[insize+i] = input_data[input_ptr+i];
76 } while (insize < INBUFSIZ);
78 error("unable to fill buffer");
83 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
84 * (Used for the decompressed data only.)
88 if (outcnt == 0) return;
89 updcrc(window, outcnt);
91 memcpy(&output_data[output_ptr], (char *)window, outcnt);
98 * Code to compute the CRC-32 table. Borrowed from
99 * gzip-1.0.3/makecrc.c.
100 * Not copyrighted 1990 Mark Adler
104 ulong c; /* crc shift register */
105 ulong e; /* polynomial exclusive-or pattern */
106 int i; /* counter for all possible eight bit values */
107 int k; /* byte being shifted into crc apparatus */
109 /* terms of polynomial defining this crc (except x^32): */
110 static const uchar poly[] = { 0,1,2,4,5,7,8,10,11,12,16,22,23,26, };
112 /* Make exclusive-or pattern from polynomial */
114 for (i = 0; i < sizeof(poly)/sizeof(*poly); i++)
115 e |= 1L << (31 - poly[i]);
119 for (i = 1; i < 256; i++) {
121 for (k = i | 256; k != 1; k >>= 1) {
122 c = c & 1 ? (c >> 1) ^ e : c >> 1;
131 * Check the magic number of the input file and update ofname if an
132 * original name was given and to_stdout is not set.
133 * Set inptr to the offset of the next byte to be processed.
135 static void get_method()
138 char magic[2]; /* magic header */
140 magic[0] = get_byte();
141 magic[1] = get_byte();
143 method = -1; /* unknown yet */
144 extended = pkzip = 0;
145 /* assume multiple members in gzip file except for record oriented I/O */
147 if (memcmp(magic, GZIP_MAGIC, 2) == 0
148 || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {
151 if (flags & ENCRYPTED)
152 error("Input is encrypted");
153 if (flags & CONTINUATION)
154 error("Multi part input");
155 if (flags & RESERVED)
156 error("Input has invalid flags");
158 (void) get_byte(); /* Get timestamp */
163 (void) get_byte(); /* Ignore extra flags for the moment */
164 (void) get_byte(); /* Ignore OS type for the moment */
166 if (flags & EXTRA_FIELD) {
167 unsigned len = get_byte();
168 len |= get_byte() << 8;
173 /* Discard file comment if any */
178 } else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2
179 && memcmp(inbuf, PKZIP_MAGIC, 4) == 0) {
181 * To simplify the code, we support a zip file when alone only.
182 * We are thus guaranteed that the entire local header fits in inbuf.
187 } else if (memcmp(magic, PACK_MAGIC, 2) == 0)
188 error("packed input");
189 else if (memcmp(magic, LZW_MAGIC, 2) == 0)
190 error("compressed input");
192 error("Corrupted input");
196 decompress_kernel (void *dest)