4 * This is a collection of several routines from gzip-1.0.3
7 * Ported to 386bsd by Serge Vakulenko
9 * $DragonFly: src/sys/i386/boot/kzipboot/Attic/misc.c,v 1.2 2003/08/07 21:17:20 dillon Exp $
18 extern const char input_data[];
19 extern const int input_len;
29 void putstr (char *c);
30 void *memcpy (void *to, const void *from, unsigned len);
31 int memcmp (const void *arg1, const void *arg2, unsigned len);
33 ulong crc; /* shift register contents */
34 ulong crc_32_tab[256]; /* crc table, defined below */
37 * Run a set of bytes through the crc shift register. If s is a NULL
38 * pointer, then initialize the crc shift register contents instead.
39 * Return the current crc in either case.
42 uchar *s; /* pointer to bytes to pump through */
43 unsigned n; /* number of bytes in s[] */
46 crc = crc_32_tab[(uchar)crc ^ (*s++)] ^ (crc >> 8);
50 * Clear input and output buffers
59 * Fill the input buffer. This is called only when the buffer is empty
60 * and at least one byte is really needed.
66 /* Read as much as possible */
69 len = INBUFSIZ - insize;
70 if (len > input_len - input_ptr + 1)
71 len = input_len-input_ptr+1;
75 inbuf[insize+i] = input_data[input_ptr+i];
78 } while (insize < INBUFSIZ);
80 error("unable to fill buffer");
85 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
86 * (Used for the decompressed data only.)
90 if (outcnt == 0) return;
91 updcrc(window, outcnt);
93 memcpy(&output_data[output_ptr], (char *)window, outcnt);
100 * Code to compute the CRC-32 table. Borrowed from
101 * gzip-1.0.3/makecrc.c.
102 * Not copyrighted 1990 Mark Adler
106 ulong c; /* crc shift register */
107 ulong e; /* polynomial exclusive-or pattern */
108 int i; /* counter for all possible eight bit values */
109 int k; /* byte being shifted into crc apparatus */
111 /* terms of polynomial defining this crc (except x^32): */
112 static const uchar poly[] = { 0,1,2,4,5,7,8,10,11,12,16,22,23,26, };
114 /* Make exclusive-or pattern from polynomial */
116 for (i = 0; i < sizeof(poly)/sizeof(*poly); i++)
117 e |= 1L << (31 - poly[i]);
121 for (i = 1; i < 256; i++) {
123 for (k = i | 256; k != 1; k >>= 1) {
124 c = c & 1 ? (c >> 1) ^ e : c >> 1;
133 * Check the magic number of the input file and update ofname if an
134 * original name was given and to_stdout is not set.
135 * Set inptr to the offset of the next byte to be processed.
137 static void get_method()
140 char magic[2]; /* magic header */
142 magic[0] = get_byte();
143 magic[1] = get_byte();
145 method = -1; /* unknown yet */
146 extended = pkzip = 0;
147 /* assume multiple members in gzip file except for record oriented I/O */
149 if (memcmp(magic, GZIP_MAGIC, 2) == 0
150 || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {
153 if (flags & ENCRYPTED)
154 error("Input is encrypted");
155 if (flags & CONTINUATION)
156 error("Multi part input");
157 if (flags & RESERVED)
158 error("Input has invalid flags");
160 (void) get_byte(); /* Get timestamp */
165 (void) get_byte(); /* Ignore extra flags for the moment */
166 (void) get_byte(); /* Ignore OS type for the moment */
168 if (flags & EXTRA_FIELD) {
169 unsigned len = get_byte();
170 len |= get_byte() << 8;
175 /* Discard file comment if any */
180 } else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2
181 && memcmp(inbuf, PKZIP_MAGIC, 4) == 0) {
183 * To simplify the code, we support a zip file when alone only.
184 * We are thus guaranteed that the entire local header fits in inbuf.
189 } else if (memcmp(magic, PACK_MAGIC, 2) == 0)
190 error("packed input");
191 else if (memcmp(magic, LZW_MAGIC, 2) == 0)
192 error("compressed input");
194 error("Corrupted input");
198 decompress_kernel (void *dest)