diff options
author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-09-25 02:47:48 +0000 |
---|---|---|
committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-09-25 02:47:48 +0000 |
commit | d4a48a11220f6a86ee2702c01c986d0a1a0a7bbc (patch) | |
tree | f38c7ef4317eea28c6abdb0adbbb286fe041711e /libbb | |
parent | d2871e072e3f545a35e07d715f3cf02cf96c011d (diff) | |
download | busybox-w32-d4a48a11220f6a86ee2702c01c986d0a1a0a7bbc.tar.gz busybox-w32-d4a48a11220f6a86ee2702c01c986d0a1a0a7bbc.tar.bz2 busybox-w32-d4a48a11220f6a86ee2702c01c986d0a1a0a7bbc.zip |
New common unarchive code.
git-svn-id: svn://busybox.net/trunk/busybox@5589 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Makefile.in | 30 | ||||
-rw-r--r-- | libbb/unzip.c | 747 | ||||
-rw-r--r-- | libbb/xfuncs.c | 56 |
3 files changed, 363 insertions, 470 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in index e7ca9aa55..449d23c68 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in | |||
@@ -28,20 +28,22 @@ LIBBB_SRC:= \ | |||
28 | copy_file_chunk.c dump.c libc5.c device_open.c error_msg.c \ | 28 | copy_file_chunk.c dump.c libc5.c device_open.c error_msg.c \ |
29 | error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \ | 29 | error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \ |
30 | find_root_device.c full_read.c full_write.c get_console.c \ | 30 | find_root_device.c full_read.c full_write.c get_console.c \ |
31 | get_last_path_component.c get_line_from_file.c gz_open.c human_readable.c \ | 31 | get_last_path_component.c get_line_from_file.c \ |
32 | isdirectory.c kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c \ | 32 | human_readable.c isdirectory.c kernel_version.c loop.c \ |
33 | mtab_file.c my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c \ | 33 | mode_string.c module_syscalls.c mtab.c mtab_file.c my_getgrnam.c \ |
34 | my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ | 34 | my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \ |
35 | print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \ | 35 | parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ |
36 | safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \ | 36 | print_file.c process_escape_sequence.c read_package_field.c \ |
37 | trim.c unzip.c uncompress.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c \ | 37 | recursive_action.c safe_read.c safe_strncpy.c syscalls.c \ |
38 | xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ | 38 | syslog_msg_with_name.c time_string.c trim.c unzip.c uncompress.c \ |
39 | copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ | 39 | vdprintf.c verror_msg.c vperror_msg.c wfopen.c xgetcwd.c xreadlink.c \ |
40 | dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \ | 40 | xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c \ |
41 | simplify_path.c inet_common.c inode_hash.c obscure.c pwd2spwd.c xfuncs.c \ | 41 | vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ |
42 | correct_password.c change_identity.c setup_environment.c run_shell.c \ | 42 | dirname.c make_directory.c create_icmp_socket.c u_signal_names.c \ |
43 | pw_encrypt.c restricted_shell.c xgethostbyname2.c create_icmp6_socket.c \ | 43 | arith.c simplify_path.c inet_common.c inode_hash.c obscure.c \ |
44 | xconnect.c bb_asprintf.c | 44 | pwd2spwd.c xfuncs.c correct_password.c change_identity.c \ |
45 | setup_environment.c run_shell.c pw_encrypt.c restricted_shell.c \ | ||
46 | xgethostbyname2.c create_icmp6_socket.c xconnect.c bb_asprintf.c | ||
45 | 47 | ||
46 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) | 48 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) |
47 | 49 | ||
diff --git a/libbb/unzip.c b/libbb/unzip.c index d84067068..20bf88f55 100644 --- a/libbb/unzip.c +++ b/libbb/unzip.c | |||
@@ -65,114 +65,126 @@ static char *license_msg[] = { | |||
65 | #include <signal.h> | 65 | #include <signal.h> |
66 | #include <stdlib.h> | 66 | #include <stdlib.h> |
67 | #include <string.h> | 67 | #include <string.h> |
68 | #include <unistd.h> | ||
69 | #include <fcntl.h> | ||
68 | #include "config.h" | 70 | #include "config.h" |
69 | #include "libbb.h" | 71 | #include "busybox.h" |
72 | #include "unarchive.h" | ||
70 | 73 | ||
71 | #ifdef CONFIG_FEATURE_UNCOMPRESS | 74 | typedef struct huft_s { |
72 | int uncompress(FILE * in, FILE * out); | 75 | unsigned char e; /* number of extra bits or operation */ |
73 | #endif | 76 | unsigned char b; /* number of bits in this code or subcode */ |
74 | 77 | union { | |
75 | static FILE *in_file, *out_file; | 78 | unsigned short n; /* literal, length base, or distance base */ |
79 | struct huft_s *t; /* pointer to next level of table */ | ||
80 | } v; | ||
81 | } huft_t; | ||
76 | 82 | ||
77 | /* these are freed by gz_close */ | 83 | static int gunzip_src_fd; |
78 | static unsigned char *window; | 84 | static int gunzip_dst_fd; |
79 | static unsigned long *crc_table; | 85 | unsigned int gunzip_bytes_out; /* number of output bytes */ |
86 | static unsigned int gunzip_outbuf_count; /* bytes in output buffer */ | ||
80 | 87 | ||
81 | static unsigned long crc; /* shift register contents */ | 88 | /* This is used to sanify any unused bits from the bitbuffer |
89 | * so they arent skipped when reading trailers (trailing headers) */ | ||
90 | unsigned char gunzip_in_buffer_count; | ||
91 | unsigned char *gunzip_in_buffer; | ||
82 | 92 | ||
83 | /* Return codes from gzip */ | 93 | /* gunzip_window size--must be a power of two, and |
84 | #define ERROR 1 | 94 | * at least 32K for zip's deflate method */ |
95 | static const int gunzip_wsize = 0x8000; | ||
85 | 96 | ||
86 | /* | 97 | static unsigned char *gunzip_window; |
87 | * window size--must be a power of two, and | 98 | static unsigned int *gunzip_crc_table; |
88 | * at least 32K for zip's deflate method | 99 | unsigned int gunzip_crc; |
89 | */ | ||
90 | #define WSIZE 0x8000 | ||
91 | 100 | ||
92 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ | 101 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ |
93 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ | 102 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ |
94 | #define N_MAX 288 /* maximum number of codes in any set */ | 103 | #define N_MAX 288 /* maximum number of codes in any set */ |
95 | 104 | ||
96 | static long bytes_out; /* number of output bytes */ | 105 | static unsigned int gunzip_hufts; /* track memory usage */ |
97 | static unsigned long outcnt; /* bytes in output buffer */ | 106 | static unsigned int gunzip_bb; /* bit buffer */ |
98 | 107 | static unsigned char gunzip_bk; /* bits in bit buffer */ | |
99 | static unsigned hufts; /* track memory usage */ | ||
100 | static unsigned long bb; /* bit buffer */ | ||
101 | static unsigned bk; /* bits in bit buffer */ | ||
102 | |||
103 | typedef struct huft_s { | ||
104 | unsigned char e; /* number of extra bits or operation */ | ||
105 | unsigned char b; /* number of bits in this code or subcode */ | ||
106 | union { | ||
107 | unsigned short n; /* literal, length base, or distance base */ | ||
108 | struct huft_s *t; /* pointer to next level of table */ | ||
109 | } v; | ||
110 | } huft_t; | ||
111 | 108 | ||
112 | static const unsigned short mask_bits[] = { | 109 | static const unsigned short mask_bits[] = { |
113 | 0x0000, | 110 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
114 | 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, | ||
115 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff | 111 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
116 | }; | 112 | }; |
117 | 113 | ||
118 | /* static int error_number = 0; */ | 114 | /* Copy lengths for literal codes 257..285 */ |
119 | /* ======================================================================== | 115 | static const unsigned short cplens[] = { |
120 | * Signal and error handler. | 116 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, |
121 | */ | 117 | 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
118 | }; | ||
119 | |||
120 | /* note: see note #13 above about the 258 in this list. */ | ||
121 | /* Extra bits for literal codes 257..285 */ | ||
122 | static const unsigned char cplext[] = { | ||
123 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, | ||
124 | 5, 5, 5, 0, 99, 99 | ||
125 | }; /* 99==invalid */ | ||
126 | |||
127 | /* Copy offsets for distance codes 0..29 */ | ||
128 | static const unsigned short cpdist[] = { | ||
129 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, | ||
130 | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 | ||
131 | }; | ||
132 | |||
133 | /* Extra bits for distance codes */ | ||
134 | static const unsigned char cpdext[] = { | ||
135 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, | ||
136 | 11, 11, 12, 12, 13, 13 | ||
137 | }; | ||
138 | |||
139 | /* Tables for deflate from PKZIP's appnote.txt. */ | ||
140 | /* Order of the bit length code lengths */ | ||
141 | static const unsigned char border[] = { | ||
142 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | ||
143 | }; | ||
144 | |||
145 | static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required) | ||
146 | { | ||
147 | while (*current < required) { | ||
148 | bitbuffer |= ((unsigned int) xread_char(gunzip_src_fd)) << *current; | ||
149 | *current += 8; | ||
150 | } | ||
151 | |||
152 | return(bitbuffer); | ||
153 | } | ||
122 | 154 | ||
123 | static void abort_gzip(void) | 155 | static void abort_gzip(void) |
124 | { | 156 | { |
125 | error_msg("gzip aborted\n"); | 157 | error_msg("gzip aborted\n"); |
126 | exit(ERROR); | 158 | exit(-1); |
127 | } | 159 | } |
128 | 160 | ||
129 | static void make_crc_table(void) | 161 | static void make_gunzip_crc_table(void) |
130 | { | 162 | { |
131 | const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */ | 163 | const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */ |
132 | unsigned short i; /* counter for all possible eight bit values */ | 164 | unsigned short i; /* counter for all possible eight bit values */ |
133 | 165 | ||
134 | /* initial shift register value */ | 166 | /* initial shift register value */ |
135 | crc = 0xffffffffL; | 167 | gunzip_crc = 0xffffffffL; |
136 | crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long)); | 168 | gunzip_crc_table = (unsigned int *) malloc(256 * sizeof(unsigned int)); |
137 | 169 | ||
138 | /* Compute and print table of CRC's, five per line */ | 170 | /* Compute and print table of CRC's, five per line */ |
139 | for (i = 0; i < 256; i++) { | 171 | for (i = 0; i < 256; i++) { |
140 | unsigned long table_entry; /* crc shift register */ | 172 | unsigned int table_entry; /* crc shift register */ |
141 | char k; /* byte being shifted into crc apparatus */ | 173 | unsigned char k; /* byte being shifted into crc apparatus */ |
142 | 174 | ||
143 | table_entry = i; | 175 | table_entry = i; |
144 | /* The idea to initialize the register with the byte instead of | 176 | /* The idea to initialize the register with the byte instead of |
145 | * zero was stolen from Haruhiko Okumura's ar002 | 177 | * zero was stolen from Haruhiko Okumura's ar002 |
146 | */ | 178 | */ |
147 | for (k = 8; k; k--) { | 179 | for (k = 8; k; k--) { |
148 | table_entry = | 180 | if (table_entry & 1) { |
149 | table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> | 181 | table_entry = (table_entry >> 1) ^ poly; |
150 | 1; | 182 | } else { |
151 | } | 183 | table_entry >>= 1; |
152 | crc_table[i] = table_entry; | ||
153 | } | 184 | } |
154 | } | ||
155 | |||
156 | /* =========================================================================== | ||
157 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. | ||
158 | * (Used for the decompressed data only.) | ||
159 | */ | ||
160 | static void flush_window(void) | ||
161 | { | ||
162 | int n; | ||
163 | |||
164 | if (outcnt == 0) | ||
165 | return; | ||
166 | |||
167 | for (n = 0; n < outcnt; n++) { | ||
168 | crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8); | ||
169 | } | 185 | } |
170 | 186 | gunzip_crc_table[i] = table_entry; | |
171 | if (fwrite(window, 1, outcnt, out_file) != outcnt) { | ||
172 | error_msg_and_die("Couldnt write"); | ||
173 | } | 187 | } |
174 | bytes_out += (unsigned long) outcnt; | ||
175 | outcnt = 0; | ||
176 | } | 188 | } |
177 | 189 | ||
178 | /* | 190 | /* |
@@ -183,7 +195,8 @@ static void flush_window(void) | |||
183 | */ | 195 | */ |
184 | static int huft_free(huft_t * t) | 196 | static int huft_free(huft_t * t) |
185 | { | 197 | { |
186 | huft_t *p, *q; | 198 | huft_t *p; |
199 | huft_t *q; | ||
187 | 200 | ||
188 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ | 201 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ |
189 | p = t; | 202 | p = t; |
@@ -195,8 +208,6 @@ static int huft_free(huft_t * t) | |||
195 | return 0; | 208 | return 0; |
196 | } | 209 | } |
197 | 210 | ||
198 | typedef unsigned char extra_bits_t; | ||
199 | |||
200 | /* Given a list of code lengths and a maximum table size, make a set of | 211 | /* Given a list of code lengths and a maximum table size, make a set of |
201 | * tables to decode that set of codes. Return zero on success, one if | 212 | * tables to decode that set of codes. Return zero on success, one if |
202 | * the given code set is incomplete (the tables are still built in this | 213 | * the given code set is incomplete (the tables are still built in this |
@@ -213,7 +224,7 @@ typedef unsigned char extra_bits_t; | |||
213 | */ | 224 | */ |
214 | static int huft_build(unsigned int *b, const unsigned int n, | 225 | static int huft_build(unsigned int *b, const unsigned int n, |
215 | const unsigned int s, const unsigned short *d, | 226 | const unsigned int s, const unsigned short *d, |
216 | const extra_bits_t * e, huft_t ** t, int *m) | 227 | const unsigned char *e, huft_t ** t, int *m) |
217 | { | 228 | { |
218 | unsigned a; /* counter for codes of length k */ | 229 | unsigned a; /* counter for codes of length k */ |
219 | unsigned c[BMAX + 1]; /* bit length count table */ | 230 | unsigned c[BMAX + 1]; /* bit length count table */ |
@@ -251,26 +262,35 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
251 | 262 | ||
252 | /* Find minimum and maximum length, bound *m by those */ | 263 | /* Find minimum and maximum length, bound *m by those */ |
253 | l = *m; | 264 | l = *m; |
254 | for (j = 1; j <= BMAX; j++) | 265 | for (j = 1; j <= BMAX; j++) { |
255 | if (c[j]) | 266 | if (c[j]) { |
256 | break; | 267 | break; |
268 | } | ||
269 | } | ||
257 | k = j; /* minimum code length */ | 270 | k = j; /* minimum code length */ |
258 | if ((unsigned) l < j) | 271 | if ((unsigned) l < j) { |
259 | l = j; | 272 | l = j; |
260 | for (i = BMAX; i; i--) | 273 | } |
261 | if (c[i]) | 274 | for (i = BMAX; i; i--) { |
275 | if (c[i]) { | ||
262 | break; | 276 | break; |
277 | } | ||
278 | } | ||
263 | g = i; /* maximum code length */ | 279 | g = i; /* maximum code length */ |
264 | if ((unsigned) l > i) | 280 | if ((unsigned) l > i) { |
265 | l = i; | 281 | l = i; |
282 | } | ||
266 | *m = l; | 283 | *m = l; |
267 | 284 | ||
268 | /* Adjust last length count to fill out codes, if needed */ | 285 | /* Adjust last length count to fill out codes, if needed */ |
269 | for (y = 1 << j; j < i; j++, y <<= 1) | 286 | for (y = 1 << j; j < i; j++, y <<= 1) { |
270 | if ((y -= c[j]) < 0) | 287 | if ((y -= c[j]) < 0) { |
271 | return 2; /* bad input: more codes than bits */ | 288 | return 2; /* bad input: more codes than bits */ |
272 | if ((y -= c[i]) < 0) | 289 | } |
290 | } | ||
291 | if ((y -= c[i]) < 0) { | ||
273 | return 2; | 292 | return 2; |
293 | } | ||
274 | c[i] += y; | 294 | c[i] += y; |
275 | 295 | ||
276 | /* Generate starting offsets into the value table for each length */ | 296 | /* Generate starting offsets into the value table for each length */ |
@@ -285,8 +305,9 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
285 | p = b; | 305 | p = b; |
286 | i = 0; | 306 | i = 0; |
287 | do { | 307 | do { |
288 | if ((j = *p++) != 0) | 308 | if ((j = *p++) != 0) { |
289 | v[x[j]++] = i; | 309 | v[x[j]++] = i; |
310 | } | ||
290 | } while (++i < n); | 311 | } while (++i < n); |
291 | 312 | ||
292 | /* Generate the Huffman codes and for each, make the table entries */ | 313 | /* Generate the Huffman codes and for each, make the table entries */ |
@@ -314,8 +335,9 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
314 | f -= a + 1; /* deduct codes from patterns left */ | 335 | f -= a + 1; /* deduct codes from patterns left */ |
315 | xp = c + k; | 336 | xp = c + k; |
316 | while (++j < z) { /* try smaller tables up to z bits */ | 337 | while (++j < z) { /* try smaller tables up to z bits */ |
317 | if ((f <<= 1) <= *++xp) | 338 | if ((f <<= 1) <= *++xp) { |
318 | break; /* enough codes to use up j bits */ | 339 | break; /* enough codes to use up j bits */ |
340 | } | ||
319 | f -= *xp; /* else deduct codes from patterns */ | 341 | f -= *xp; /* else deduct codes from patterns */ |
320 | } | 342 | } |
321 | } | 343 | } |
@@ -324,7 +346,7 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
324 | /* allocate and link in new table */ | 346 | /* allocate and link in new table */ |
325 | q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); | 347 | q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); |
326 | 348 | ||
327 | hufts += z + 1; /* track memory usage */ | 349 | gunzip_hufts += z + 1; /* track memory usage */ |
328 | *t = q + 1; /* link to list for huft_free() */ | 350 | *t = q + 1; /* link to list for huft_free() */ |
329 | *(t = &(q->v.t)) = NULL; | 351 | *(t = &(q->v.t)) = NULL; |
330 | u[h] = ++q; /* table starts after link */ | 352 | u[h] = ++q; /* table starts after link */ |
@@ -342,9 +364,9 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
342 | 364 | ||
343 | /* set up table entry in r */ | 365 | /* set up table entry in r */ |
344 | r.b = (unsigned char) (k - w); | 366 | r.b = (unsigned char) (k - w); |
345 | if (p >= v + n) | 367 | if (p >= v + n) { |
346 | r.e = 99; /* out of values--invalid code */ | 368 | r.e = 99; /* out of values--invalid code */ |
347 | else if (*p < s) { | 369 | } else if (*p < s) { |
348 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ | 370 | r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ |
349 | r.v.n = (unsigned short) (*p); /* simple code is just the value */ | 371 | r.v.n = (unsigned short) (*p); /* simple code is just the value */ |
350 | p++; /* one compiler does not like *p++ */ | 372 | p++; /* one compiler does not like *p++ */ |
@@ -355,12 +377,14 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
355 | 377 | ||
356 | /* fill code-like entries with r */ | 378 | /* fill code-like entries with r */ |
357 | f = 1 << (k - w); | 379 | f = 1 << (k - w); |
358 | for (j = i >> w; j < z; j += f) | 380 | for (j = i >> w; j < z; j += f) { |
359 | q[j] = r; | 381 | q[j] = r; |
382 | } | ||
360 | 383 | ||
361 | /* backwards increment the k-bit code i */ | 384 | /* backwards increment the k-bit code i */ |
362 | for (j = 1 << (k - 1); i & j; j >>= 1) | 385 | for (j = 1 << (k - 1); i & j; j >>= 1) { |
363 | i ^= j; | 386 | i ^= j; |
387 | } | ||
364 | i ^= j; | 388 | i ^= j; |
365 | 389 | ||
366 | /* backup over finished tables */ | 390 | /* backup over finished tables */ |
@@ -374,6 +398,25 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
374 | return y != 0 && g != 1; | 398 | return y != 0 && g != 1; |
375 | } | 399 | } |
376 | 400 | ||
401 | /* =========================================================================== | ||
402 | * Write the output gunzip_window gunzip_window[0..gunzip_outbuf_count-1] and update crc and gunzip_bytes_out. | ||
403 | * (Used for the decompressed data only.) | ||
404 | */ | ||
405 | static void flush_gunzip_window(void) | ||
406 | { | ||
407 | int n; | ||
408 | |||
409 | for (n = 0; n < gunzip_outbuf_count; n++) { | ||
410 | gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8); | ||
411 | } | ||
412 | |||
413 | if (write(gunzip_dst_fd, gunzip_window, gunzip_outbuf_count) != gunzip_outbuf_count) { | ||
414 | error_msg_and_die("Couldnt write"); | ||
415 | } | ||
416 | gunzip_bytes_out += gunzip_outbuf_count; | ||
417 | gunzip_outbuf_count = 0; | ||
418 | } | ||
419 | |||
377 | /* | 420 | /* |
378 | * inflate (decompress) the codes in a deflated (compressed) block. | 421 | * inflate (decompress) the codes in a deflated (compressed) block. |
379 | * Return an error code or zero if it all goes ok. | 422 | * Return an error code or zero if it all goes ok. |
@@ -381,32 +424,26 @@ static int huft_build(unsigned int *b, const unsigned int n, | |||
381 | * tl, td: literal/length and distance decoder tables | 424 | * tl, td: literal/length and distance decoder tables |
382 | * bl, bd: number of bits decoded by tl[] and td[] | 425 | * bl, bd: number of bits decoded by tl[] and td[] |
383 | */ | 426 | */ |
384 | static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | 427 | static int inflate_codes(huft_t * tl, huft_t * td, const unsigned int bl, const unsigned int bd) |
385 | { | 428 | { |
386 | register unsigned long e; /* table entry flag/number of extra bits */ | 429 | unsigned int e; /* table entry flag/number of extra bits */ |
387 | unsigned long n, d; /* length and index for copy */ | 430 | unsigned int n, d; /* length and index for copy */ |
388 | unsigned long w; /* current window position */ | 431 | unsigned int w; /* current gunzip_window position */ |
389 | huft_t *t; /* pointer to table entry */ | 432 | huft_t *t; /* pointer to table entry */ |
390 | unsigned ml, md; /* masks for bl and bd bits */ | 433 | unsigned int ml, md; /* masks for bl and bd bits */ |
391 | register unsigned long b; /* bit buffer */ | 434 | unsigned int b; /* bit buffer */ |
392 | register unsigned k; /* number of bits in bit buffer */ | 435 | unsigned int k; /* number of bits in bit buffer */ |
393 | register int input_char; | ||
394 | 436 | ||
395 | /* make local copies of globals */ | 437 | /* make local copies of globals */ |
396 | b = bb; /* initialize bit buffer */ | 438 | b = gunzip_bb; /* initialize bit buffer */ |
397 | k = bk; | 439 | k = gunzip_bk; |
398 | w = outcnt; /* initialize window position */ | 440 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ |
399 | 441 | ||
400 | /* inflate the coded data */ | 442 | /* inflate the coded data */ |
401 | ml = mask_bits[bl]; /* precompute masks for speed */ | 443 | ml = mask_bits[bl]; /* precompute masks for speed */ |
402 | md = mask_bits[bd]; | 444 | md = mask_bits[bd]; |
403 | for (;;) { /* do until end of block */ | 445 | while (1) { /* do until end of block */ |
404 | while (k < (unsigned) bl) { | 446 | b = fill_bitbuffer(b, &k, bl); |
405 | input_char = fgetc(in_file); | ||
406 | if (input_char == EOF) return 1; | ||
407 | b |= ((unsigned long)input_char) << k; | ||
408 | k += 8; | ||
409 | } | ||
410 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) | 447 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
411 | do { | 448 | do { |
412 | if (e == 99) { | 449 | if (e == 99) { |
@@ -415,20 +452,16 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
415 | b >>= t->b; | 452 | b >>= t->b; |
416 | k -= t->b; | 453 | k -= t->b; |
417 | e -= 16; | 454 | e -= 16; |
418 | while (k < e) { | 455 | b = fill_bitbuffer(b, &k, e); |
419 | input_char = fgetc(in_file); | ||
420 | if (input_char == EOF) return 1; | ||
421 | b |= ((unsigned long)input_char) << k; | ||
422 | k += 8; | ||
423 | } | ||
424 | } while ((e = | 456 | } while ((e = |
425 | (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); | 457 | (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); |
426 | b >>= t->b; | 458 | b >>= t->b; |
427 | k -= t->b; | 459 | k -= t->b; |
428 | if (e == 16) { /* then it's a literal */ | 460 | if (e == 16) { /* then it's a literal */ |
429 | window[w++] = (unsigned char) t->v.n; | 461 | gunzip_window[w++] = (unsigned char) t->v.n; |
430 | if (w == WSIZE) { | 462 | if (w == gunzip_wsize) { |
431 | outcnt = (w), flush_window(); | 463 | gunzip_outbuf_count = (w); |
464 | flush_gunzip_window(); | ||
432 | w = 0; | 465 | w = 0; |
433 | } | 466 | } |
434 | } else { /* it's an EOB or a length */ | 467 | } else { /* it's an EOB or a length */ |
@@ -439,24 +472,13 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
439 | } | 472 | } |
440 | 473 | ||
441 | /* get length of block to copy */ | 474 | /* get length of block to copy */ |
442 | while (k < e) { | 475 | b = fill_bitbuffer(b, &k, e); |
443 | input_char = fgetc(in_file); | ||
444 | if (input_char == EOF) return 1; | ||
445 | b |= ((unsigned long)input_char) << k; | ||
446 | k += 8; | ||
447 | } | ||
448 | n = t->v.n + ((unsigned) b & mask_bits[e]); | 476 | n = t->v.n + ((unsigned) b & mask_bits[e]); |
449 | b >>= e; | 477 | b >>= e; |
450 | k -= e; | 478 | k -= e; |
451 | 479 | ||
452 | /* decode distance of block to copy */ | 480 | /* decode distance of block to copy */ |
453 | while (k < (unsigned) bd) { | 481 | b = fill_bitbuffer(b, &k, bd); |
454 | input_char = fgetc(in_file); | ||
455 | if (input_char == EOF) return 1; | ||
456 | b |= ((unsigned long)input_char) << k; | ||
457 | k += 8; | ||
458 | } | ||
459 | |||
460 | if ((e = (t = td + ((unsigned) b & md))->e) > 16) | 482 | if ((e = (t = td + ((unsigned) b & md))->e) > 16) |
461 | do { | 483 | do { |
462 | if (e == 99) | 484 | if (e == 99) |
@@ -464,23 +486,13 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
464 | b >>= t->b; | 486 | b >>= t->b; |
465 | k -= t->b; | 487 | k -= t->b; |
466 | e -= 16; | 488 | e -= 16; |
467 | while (k < e) { | 489 | b = fill_bitbuffer(b, &k, e); |
468 | input_char = fgetc(in_file); | ||
469 | if (input_char == EOF) return 1; | ||
470 | b |= ((unsigned long)input_char) << k; | ||
471 | k += 8; | ||
472 | } | ||
473 | } while ((e = | 490 | } while ((e = |
474 | (t = | 491 | (t = |
475 | t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); | 492 | t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); |
476 | b >>= t->b; | 493 | b >>= t->b; |
477 | k -= t->b; | 494 | k -= t->b; |
478 | while (k < e) { | 495 | b = fill_bitbuffer(b, &k, e); |
479 | input_char = fgetc(in_file); | ||
480 | if (input_char == EOF) return 1; | ||
481 | b |= ((unsigned long)input_char) << k; | ||
482 | k += 8; | ||
483 | } | ||
484 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); | 496 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
485 | b >>= e; | 497 | b >>= e; |
486 | k -= e; | 498 | k -= e; |
@@ -489,60 +501,38 @@ static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) | |||
489 | do { | 501 | do { |
490 | n -= (e = | 502 | n -= (e = |
491 | (e = | 503 | (e = |
492 | WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); | 504 | gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e); |
493 | #if !defined(NOMEMCPY) && !defined(DEBUG) | 505 | /* copy to new buffer to prevent possible overwrite */ |
494 | if (w - d >= e) { /* (this test assumes unsigned comparison) */ | 506 | if (w - d >= e) { /* (this test assumes unsigned comparison) */ |
495 | memcpy(window + w, window + d, e); | 507 | memcpy(gunzip_window + w, gunzip_window + d, e); |
496 | w += e; | 508 | w += e; |
497 | d += e; | 509 | d += e; |
498 | } else /* do it slow to avoid memcpy() overlap */ | 510 | } else { |
499 | #endif /* !NOMEMCPY */ | 511 | /* do it slow to avoid memcpy() overlap */ |
512 | /* !NOMEMCPY */ | ||
500 | do { | 513 | do { |
501 | window[w++] = window[d++]; | 514 | gunzip_window[w++] = gunzip_window[d++]; |
502 | } while (--e); | 515 | } while (--e); |
503 | if (w == WSIZE) { | 516 | } |
504 | outcnt = (w), flush_window(); | 517 | if (w == gunzip_wsize) { |
518 | gunzip_outbuf_count = (w); | ||
519 | flush_gunzip_window(); | ||
505 | w = 0; | 520 | w = 0; |
506 | } | 521 | } |
522 | |||
507 | } while (n); | 523 | } while (n); |
508 | } | 524 | } |
509 | } | 525 | } |
510 | 526 | ||
511 | /* restore the globals from the locals */ | 527 | /* restore the globals from the locals */ |
512 | outcnt = w; /* restore global window pointer */ | 528 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ |
513 | bb = b; /* restore global bit buffer */ | 529 | gunzip_bb = b; /* restore global bit buffer */ |
514 | bk = k; | 530 | gunzip_bk = k; |
515 | 531 | ||
516 | /* done */ | 532 | /* done */ |
517 | return 0; | 533 | return 0; |
518 | } | 534 | } |
519 | 535 | ||
520 | static const unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ | ||
521 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | ||
522 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 | ||
523 | }; | ||
524 | |||
525 | /* note: see note #13 above about the 258 in this list. */ | ||
526 | static const extra_bits_t cplext[] = { /* Extra bits for literal codes 257..285 */ | ||
527 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, | ||
528 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 | ||
529 | }; /* 99==invalid */ | ||
530 | static const unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ | ||
531 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | ||
532 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | ||
533 | 8193, 12289, 16385, 24577 | ||
534 | }; | ||
535 | static const extra_bits_t cpdext[] = { /* Extra bits for distance codes */ | ||
536 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | ||
537 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | ||
538 | 12, 12, 13, 13 | ||
539 | }; | ||
540 | |||
541 | /* Tables for deflate from PKZIP's appnote.txt. */ | ||
542 | static const extra_bits_t border[] = { /* Order of the bit length code lengths */ | ||
543 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | ||
544 | }; | ||
545 | |||
546 | /* | 536 | /* |
547 | * decompress an inflated block | 537 | * decompress an inflated block |
548 | * e: last block flag | 538 | * e: last block flag |
@@ -552,53 +542,43 @@ static const extra_bits_t border[] = { /* Order of the bit length code lengths * | |||
552 | static int inflate_block(int *e) | 542 | static int inflate_block(int *e) |
553 | { | 543 | { |
554 | unsigned t; /* block type */ | 544 | unsigned t; /* block type */ |
555 | register unsigned long b; /* bit buffer */ | 545 | register unsigned int b; /* bit buffer */ |
556 | register unsigned k; /* number of bits in bit buffer */ | 546 | unsigned int k; /* number of bits in bit buffer */ |
557 | int input_char; | ||
558 | 547 | ||
559 | /* make local bit buffer */ | 548 | /* make local bit buffer */ |
560 | b = bb; | 549 | |
561 | k = bk; | 550 | b = gunzip_bb; |
551 | k = gunzip_bk; | ||
562 | 552 | ||
563 | /* read in last block bit */ | 553 | /* read in last block bit */ |
564 | while (k < 1) { | 554 | b = fill_bitbuffer(b, &k, 1); |
565 | input_char = fgetc(in_file); | ||
566 | if (input_char == EOF) return 1; | ||
567 | b |= ((unsigned long)input_char) << k; | ||
568 | k += 8; | ||
569 | } | ||
570 | *e = (int) b & 1; | 555 | *e = (int) b & 1; |
571 | b >>= 1; | 556 | b >>= 1; |
572 | k -= 1; | 557 | k -= 1; |
573 | 558 | ||
574 | /* read in block type */ | 559 | /* read in block type */ |
575 | while (k < 2) { | 560 | b = fill_bitbuffer(b, &k, 2); |
576 | input_char = fgetc(in_file); | ||
577 | if (input_char == EOF) return 1; | ||
578 | b |= ((unsigned long)input_char) << k; | ||
579 | k += 8; | ||
580 | } | ||
581 | t = (unsigned) b & 3; | 561 | t = (unsigned) b & 3; |
582 | b >>= 2; | 562 | b >>= 2; |
583 | k -= 2; | 563 | k -= 2; |
584 | 564 | ||
585 | /* restore the global bit buffer */ | 565 | /* restore the global bit buffer */ |
586 | bb = b; | 566 | gunzip_bb = b; |
587 | bk = k; | 567 | gunzip_bk = k; |
588 | 568 | ||
589 | /* inflate that block type */ | 569 | /* inflate that block type */ |
590 | switch (t) { | 570 | switch (t) { |
591 | case 0: /* Inflate stored */ | 571 | case 0: /* Inflate stored */ |
592 | { | 572 | { |
593 | unsigned long n; /* number of bytes in block */ | 573 | unsigned int n; /* number of bytes in block */ |
594 | unsigned long w; /* current window position */ | 574 | unsigned int w; /* current gunzip_window position */ |
595 | register unsigned long b_stored; /* bit buffer */ | 575 | unsigned int b_stored; /* bit buffer */ |
596 | register unsigned long k_stored; /* number of bits in bit buffer */ | 576 | unsigned int k_stored; /* number of bits in bit buffer */ |
597 | 577 | ||
598 | /* make local copies of globals */ | 578 | /* make local copies of globals */ |
599 | b_stored = bb; /* initialize bit buffer */ | 579 | b_stored = gunzip_bb; /* initialize bit buffer */ |
600 | k_stored = bk; | 580 | k_stored = gunzip_bk; |
601 | w = outcnt; /* initialize window position */ | 581 | w = gunzip_outbuf_count; /* initialize gunzip_window position */ |
602 | 582 | ||
603 | /* go to byte boundary */ | 583 | /* go to byte boundary */ |
604 | n = k_stored & 7; | 584 | n = k_stored & 7; |
@@ -606,21 +586,12 @@ static int inflate_block(int *e) | |||
606 | k_stored -= n; | 586 | k_stored -= n; |
607 | 587 | ||
608 | /* get the length and its complement */ | 588 | /* get the length and its complement */ |
609 | while (k_stored < 16) { | 589 | b_stored = fill_bitbuffer(b_stored, &k_stored, 16); |
610 | input_char = fgetc(in_file); | ||
611 | if (input_char == EOF) return 1; | ||
612 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
613 | k_stored += 8; | ||
614 | } | ||
615 | n = ((unsigned) b_stored & 0xffff); | 590 | n = ((unsigned) b_stored & 0xffff); |
616 | b_stored >>= 16; | 591 | b_stored >>= 16; |
617 | k_stored -= 16; | 592 | k_stored -= 16; |
618 | while (k_stored < 16) { | 593 | |
619 | input_char = fgetc(in_file); | 594 | b_stored = fill_bitbuffer(b_stored, &k_stored, 16); |
620 | if (input_char == EOF) return 1; | ||
621 | b_stored |= ((unsigned long)input_char) << k_stored; | ||
622 | k_stored += 8; | ||
623 | } | ||
624 | if (n != (unsigned) ((~b_stored) & 0xffff)) { | 595 | if (n != (unsigned) ((~b_stored) & 0xffff)) { |
625 | return 1; /* error in compressed data */ | 596 | return 1; /* error in compressed data */ |
626 | } | 597 | } |
@@ -629,15 +600,11 @@ static int inflate_block(int *e) | |||
629 | 600 | ||
630 | /* read and output the compressed data */ | 601 | /* read and output the compressed data */ |
631 | while (n--) { | 602 | while (n--) { |
632 | while (k_stored < 8) { | 603 | b_stored = fill_bitbuffer(b_stored, &k_stored, 8); |
633 | input_char = fgetc(in_file); | 604 | gunzip_window[w++] = (unsigned char) b_stored; |
634 | if (input_char == EOF) return 1; | 605 | if (w == (unsigned int) gunzip_wsize) { |
635 | b_stored |= ((unsigned long)input_char) << k_stored; | 606 | gunzip_outbuf_count = (w); |
636 | k_stored += 8; | 607 | flush_gunzip_window(); |
637 | } | ||
638 | window[w++] = (unsigned char) b_stored; | ||
639 | if (w == (unsigned long) WSIZE) { | ||
640 | outcnt = (w), flush_window(); | ||
641 | w = 0; | 608 | w = 0; |
642 | } | 609 | } |
643 | b_stored >>= 8; | 610 | b_stored >>= 8; |
@@ -645,9 +612,9 @@ static int inflate_block(int *e) | |||
645 | } | 612 | } |
646 | 613 | ||
647 | /* restore the globals from the locals */ | 614 | /* restore the globals from the locals */ |
648 | outcnt = w; /* restore global window pointer */ | 615 | gunzip_outbuf_count = w; /* restore global gunzip_window pointer */ |
649 | bb = b_stored; /* restore global bit buffer */ | 616 | gunzip_bb = b_stored; /* restore global bit buffer */ |
650 | bk = k_stored; | 617 | gunzip_bk = k_stored; |
651 | return 0; | 618 | return 0; |
652 | } | 619 | } |
653 | case 1: /* Inflate fixed | 620 | case 1: /* Inflate fixed |
@@ -659,8 +626,8 @@ static int inflate_block(int *e) | |||
659 | int i; /* temporary variable */ | 626 | int i; /* temporary variable */ |
660 | huft_t *tl; /* literal/length code table */ | 627 | huft_t *tl; /* literal/length code table */ |
661 | huft_t *td; /* distance code table */ | 628 | huft_t *td; /* distance code table */ |
662 | int bl; /* lookup bits for tl */ | 629 | unsigned int bl; /* lookup bits for tl */ |
663 | int bd; /* lookup bits for td */ | 630 | unsigned int bd; /* lookup bits for td */ |
664 | unsigned int l[288]; /* length list for huft_build */ | 631 | unsigned int l[288]; /* length list for huft_build */ |
665 | 632 | ||
666 | /* set up literal table */ | 633 | /* set up literal table */ |
@@ -692,8 +659,9 @@ static int inflate_block(int *e) | |||
692 | } | 659 | } |
693 | 660 | ||
694 | /* decompress until an end-of-block code */ | 661 | /* decompress until an end-of-block code */ |
695 | if (inflate_codes(tl, td, bl, bd)) | 662 | if (inflate_codes(tl, td, bl, bd)) { |
696 | return 1; | 663 | return 1; |
664 | } | ||
697 | 665 | ||
698 | /* free the decoding tables, return */ | 666 | /* free the decoding tables, return */ |
699 | huft_free(tl); | 667 | huft_free(tl); |
@@ -705,53 +673,41 @@ static int inflate_block(int *e) | |||
705 | const int dbits = 6; /* bits in base distance lookup table */ | 673 | const int dbits = 6; /* bits in base distance lookup table */ |
706 | const int lbits = 9; /* bits in base literal/length lookup table */ | 674 | const int lbits = 9; /* bits in base literal/length lookup table */ |
707 | 675 | ||
708 | int i; /* temporary variables */ | ||
709 | unsigned j; | ||
710 | unsigned l; /* last length */ | ||
711 | unsigned m; /* mask for bit lengths table */ | ||
712 | unsigned n; /* number of lengths to get */ | ||
713 | huft_t *tl; /* literal/length code table */ | 676 | huft_t *tl; /* literal/length code table */ |
714 | huft_t *td; /* distance code table */ | 677 | huft_t *td; /* distance code table */ |
715 | int bl; /* lookup bits for tl */ | 678 | unsigned int i; /* temporary variables */ |
716 | int bd; /* lookup bits for td */ | 679 | unsigned int j; |
717 | unsigned nb; /* number of bit length codes */ | 680 | unsigned int l; /* last length */ |
718 | unsigned nl; /* number of literal/length codes */ | 681 | unsigned int m; /* mask for bit lengths table */ |
719 | unsigned nd; /* number of distance codes */ | 682 | unsigned int n; /* number of lengths to get */ |
720 | 683 | unsigned int bl; /* lookup bits for tl */ | |
721 | unsigned ll[286 + 30]; /* literal/length and distance code lengths */ | 684 | unsigned int bd; /* lookup bits for td */ |
722 | register unsigned long b_dynamic; /* bit buffer */ | 685 | unsigned int nb; /* number of bit length codes */ |
723 | register unsigned k_dynamic; /* number of bits in bit buffer */ | 686 | unsigned int nl; /* number of literal/length codes */ |
687 | unsigned int nd; /* number of distance codes */ | ||
688 | |||
689 | unsigned int ll[286 + 30]; /* literal/length and distance code lengths */ | ||
690 | unsigned int b_dynamic; /* bit buffer */ | ||
691 | unsigned int k_dynamic; /* number of bits in bit buffer */ | ||
724 | 692 | ||
725 | /* make local bit buffer */ | 693 | /* make local bit buffer */ |
726 | b_dynamic = bb; | 694 | b_dynamic = gunzip_bb; |
727 | k_dynamic = bk; | 695 | k_dynamic = gunzip_bk; |
728 | 696 | ||
729 | /* read in table lengths */ | 697 | /* read in table lengths */ |
730 | while (k_dynamic < 5) { | 698 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5); |
731 | input_char = fgetc(in_file); | 699 | nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */ |
732 | if (input_char == EOF) return 1; | 700 | |
733 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
734 | k_dynamic += 8; | ||
735 | } | ||
736 | nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ | ||
737 | b_dynamic >>= 5; | 701 | b_dynamic >>= 5; |
738 | k_dynamic -= 5; | 702 | k_dynamic -= 5; |
739 | while (k_dynamic < 5) { | 703 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5); |
740 | input_char = fgetc(in_file); | 704 | nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */ |
741 | if (input_char == EOF) return 1; | 705 | |
742 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
743 | k_dynamic += 8; | ||
744 | } | ||
745 | nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ | ||
746 | b_dynamic >>= 5; | 706 | b_dynamic >>= 5; |
747 | k_dynamic -= 5; | 707 | k_dynamic -= 5; |
748 | while (k_dynamic < 4) { | 708 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4); |
749 | input_char = fgetc(in_file); | 709 | nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */ |
750 | if (input_char == EOF) return 1; | 710 | |
751 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
752 | k_dynamic += 8; | ||
753 | } | ||
754 | nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ | ||
755 | b_dynamic >>= 4; | 711 | b_dynamic >>= 4; |
756 | k_dynamic -= 4; | 712 | k_dynamic -= 4; |
757 | if (nl > 286 || nd > 30) { | 713 | if (nl > 286 || nd > 30) { |
@@ -760,13 +716,8 @@ static int inflate_block(int *e) | |||
760 | 716 | ||
761 | /* read in bit-length-code lengths */ | 717 | /* read in bit-length-code lengths */ |
762 | for (j = 0; j < nb; j++) { | 718 | for (j = 0; j < nb; j++) { |
763 | while (k_dynamic < 3) { | 719 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3); |
764 | input_char = fgetc(in_file); | 720 | ll[border[j]] = (unsigned int) b_dynamic & 7; |
765 | if (input_char == EOF) return 1; | ||
766 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
767 | k_dynamic += 8; | ||
768 | } | ||
769 | ll[border[j]] = (unsigned) b_dynamic & 7; | ||
770 | b_dynamic >>= 3; | 721 | b_dynamic >>= 3; |
771 | k_dynamic -= 3; | 722 | k_dynamic -= 3; |
772 | } | 723 | } |
@@ -776,7 +727,8 @@ static int inflate_block(int *e) | |||
776 | 727 | ||
777 | /* build decoding table for trees--single level, 7 bit lookup */ | 728 | /* build decoding table for trees--single level, 7 bit lookup */ |
778 | bl = 7; | 729 | bl = 7; |
779 | if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) { | 730 | i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl); |
731 | if (i != 0) { | ||
780 | if (i == 1) { | 732 | if (i == 1) { |
781 | huft_free(tl); | 733 | huft_free(tl); |
782 | } | 734 | } |
@@ -787,46 +739,31 @@ static int inflate_block(int *e) | |||
787 | n = nl + nd; | 739 | n = nl + nd; |
788 | m = mask_bits[bl]; | 740 | m = mask_bits[bl]; |
789 | i = l = 0; | 741 | i = l = 0; |
790 | while ((unsigned) i < n) { | 742 | while ((unsigned int) i < n) { |
791 | while (k_dynamic < (unsigned) bl) { | 743 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl); |
792 | input_char = fgetc(in_file); | 744 | j = (td = tl + ((unsigned int) b_dynamic & m))->b; |
793 | if (input_char == EOF) return 1; | ||
794 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
795 | k_dynamic += 8; | ||
796 | } | ||
797 | j = (td = tl + ((unsigned) b_dynamic & m))->b; | ||
798 | b_dynamic >>= j; | 745 | b_dynamic >>= j; |
799 | k_dynamic -= j; | 746 | k_dynamic -= j; |
800 | j = td->v.n; | 747 | j = td->v.n; |
801 | if (j < 16) { /* length of code in bits (0..15) */ | 748 | if (j < 16) { /* length of code in bits (0..15) */ |
802 | ll[i++] = l = j; /* save last length in l */ | 749 | ll[i++] = l = j; /* save last length in l */ |
803 | } else if (j == 16) { /* repeat last length 3 to 6 times */ | 750 | } else if (j == 16) { /* repeat last length 3 to 6 times */ |
804 | while (k_dynamic < 2) { | 751 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2); |
805 | input_char = fgetc(in_file); | 752 | j = 3 + ((unsigned int) b_dynamic & 3); |
806 | if (input_char == EOF) return 1; | ||
807 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
808 | k_dynamic += 8; | ||
809 | } | ||
810 | j = 3 + ((unsigned) b_dynamic & 3); | ||
811 | b_dynamic >>= 2; | 753 | b_dynamic >>= 2; |
812 | k_dynamic -= 2; | 754 | k_dynamic -= 2; |
813 | if ((unsigned) i + j > n) { | 755 | if ((unsigned int) i + j > n) { |
814 | return 1; | 756 | return 1; |
815 | } | 757 | } |
816 | while (j--) { | 758 | while (j--) { |
817 | ll[i++] = l; | 759 | ll[i++] = l; |
818 | } | 760 | } |
819 | } else if (j == 17) { /* 3 to 10 zero length codes */ | 761 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
820 | while (k_dynamic < 3) { | 762 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3); |
821 | input_char = fgetc(in_file); | 763 | j = 3 + ((unsigned int) b_dynamic & 7); |
822 | if (input_char == EOF) return 1; | ||
823 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
824 | k_dynamic += 8; | ||
825 | } | ||
826 | j = 3 + ((unsigned) b_dynamic & 7); | ||
827 | b_dynamic >>= 3; | 764 | b_dynamic >>= 3; |
828 | k_dynamic -= 3; | 765 | k_dynamic -= 3; |
829 | if ((unsigned) i + j > n) { | 766 | if ((unsigned int) i + j > n) { |
830 | return 1; | 767 | return 1; |
831 | } | 768 | } |
832 | while (j--) { | 769 | while (j--) { |
@@ -834,16 +771,11 @@ static int inflate_block(int *e) | |||
834 | } | 771 | } |
835 | l = 0; | 772 | l = 0; |
836 | } else { /* j == 18: 11 to 138 zero length codes */ | 773 | } else { /* j == 18: 11 to 138 zero length codes */ |
837 | while (k_dynamic < 7) { | 774 | b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7); |
838 | input_char = fgetc(in_file); | 775 | j = 11 + ((unsigned int) b_dynamic & 0x7f); |
839 | if (input_char == EOF) return 1; | ||
840 | b_dynamic |= ((unsigned long)input_char) << k_dynamic; | ||
841 | k_dynamic += 8; | ||
842 | } | ||
843 | j = 11 + ((unsigned) b_dynamic & 0x7f); | ||
844 | b_dynamic >>= 7; | 776 | b_dynamic >>= 7; |
845 | k_dynamic -= 7; | 777 | k_dynamic -= 7; |
846 | if ((unsigned) i + j > n) { | 778 | if ((unsigned int) i + j > n) { |
847 | return 1; | 779 | return 1; |
848 | } | 780 | } |
849 | while (j--) { | 781 | while (j--) { |
@@ -857,22 +789,24 @@ static int inflate_block(int *e) | |||
857 | huft_free(tl); | 789 | huft_free(tl); |
858 | 790 | ||
859 | /* restore the global bit buffer */ | 791 | /* restore the global bit buffer */ |
860 | bb = b_dynamic; | 792 | gunzip_bb = b_dynamic; |
861 | bk = k_dynamic; | 793 | gunzip_bk = k_dynamic; |
862 | 794 | ||
863 | /* build the decoding tables for literal/length and distance codes */ | 795 | /* build the decoding tables for literal/length and distance codes */ |
864 | bl = lbits; | 796 | bl = lbits; |
797 | |||
865 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { | 798 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { |
866 | if (i == 1) { | 799 | if (i == 1) { |
867 | error_msg("Incomplete literal tree"); | 800 | error_msg_and_die("Incomplete literal tree"); |
868 | huft_free(tl); | 801 | huft_free(tl); |
869 | } | 802 | } |
870 | return i; /* incomplete code set */ | 803 | return i; /* incomplete code set */ |
871 | } | 804 | } |
805 | |||
872 | bd = dbits; | 806 | bd = dbits; |
873 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { | 807 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { |
874 | if (i == 1) { | 808 | if (i == 1) { |
875 | error_msg("incomplete distance tree"); | 809 | error_msg_and_die("incomplete distance tree"); |
876 | huft_free(td); | 810 | huft_free(td); |
877 | } | 811 | } |
878 | huft_free(tl); | 812 | huft_free(tl); |
@@ -880,8 +814,9 @@ static int inflate_block(int *e) | |||
880 | } | 814 | } |
881 | 815 | ||
882 | /* decompress until an end-of-block code */ | 816 | /* decompress until an end-of-block code */ |
883 | if (inflate_codes(tl, td, bl, bd)) | 817 | if (inflate_codes(tl, td, bl, bd)) { |
884 | return 1; | 818 | return 1; |
819 | } | ||
885 | 820 | ||
886 | /* free the decoding tables, return */ | 821 | /* free the decoding tables, return */ |
887 | huft_free(tl); | 822 | huft_free(tl); |
@@ -890,6 +825,7 @@ static int inflate_block(int *e) | |||
890 | } | 825 | } |
891 | default: | 826 | default: |
892 | /* bad block type */ | 827 | /* bad block type */ |
828 | error_msg("bad block type %d\n", t); | ||
893 | return 2; | 829 | return 2; |
894 | } | 830 | } |
895 | } | 831 | } |
@@ -897,72 +833,24 @@ static int inflate_block(int *e) | |||
897 | /* | 833 | /* |
898 | * decompress an inflated entry | 834 | * decompress an inflated entry |
899 | * | 835 | * |
900 | * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr | 836 | * GLOBAL VARIABLES: gunzip_outbuf_count, bk, gunzip_bb, hufts, inptr |
901 | */ | 837 | */ |
902 | extern int inflate(FILE * in, FILE * out) | 838 | extern int inflate(int in, int out) |
903 | { | 839 | { |
840 | typedef void (*sig_type) (int); | ||
904 | int e; /* last block flag */ | 841 | int e; /* last block flag */ |
905 | int r; /* result code */ | 842 | int r; /* result code */ |
906 | unsigned h = 0; /* maximum struct huft's malloc'ed */ | 843 | unsigned h = 0; /* maximum struct huft's malloc'ed */ |
907 | 844 | ||
908 | /* initialize window, bit buffer */ | ||
909 | outcnt = 0; | ||
910 | bk = 0; | ||
911 | bb = 0; | ||
912 | |||
913 | in_file = in; | ||
914 | out_file = out; | ||
915 | |||
916 | /* Allocate all global buffers (for DYN_ALLOC option) */ | 845 | /* Allocate all global buffers (for DYN_ALLOC option) */ |
917 | window = xmalloc((size_t) (((2L * WSIZE) + 1L) * sizeof(unsigned char))); | 846 | gunzip_window = xmalloc(0x8000); |
918 | bytes_out = 0L; | 847 | gunzip_outbuf_count = 0; |
848 | gunzip_bytes_out = 0; | ||
919 | 849 | ||
920 | /* Create the crc table */ | 850 | gunzip_src_fd = in; |
921 | make_crc_table(); | 851 | gunzip_dst_fd = out; |
922 | 852 | ||
923 | /* decompress until the last block */ | 853 | gunzip_in_buffer = malloc(8); |
924 | do { | ||
925 | hufts = 0; | ||
926 | if ((r = inflate_block(&e)) != 0) { | ||
927 | return r; | ||
928 | } | ||
929 | if (hufts > h) { | ||
930 | h = hufts; | ||
931 | } | ||
932 | } while (!e); | ||
933 | |||
934 | /* Undo too much lookahead. The next read will be byte aligned so we | ||
935 | * can discard unused bits in the last meaningful byte. | ||
936 | */ | ||
937 | while (bk >= 8) { | ||
938 | bk -= 8; | ||
939 | ungetc((bb << bk), in_file); | ||
940 | } | ||
941 | |||
942 | /* flush out window */ | ||
943 | flush_window(); | ||
944 | free(window); | ||
945 | free(crc_table); | ||
946 | |||
947 | /* return success */ | ||
948 | return 0; | ||
949 | } | ||
950 | |||
951 | /* =========================================================================== | ||
952 | * Unzip in to out. This routine works on gzip files only. | ||
953 | * | ||
954 | * IN assertions: the buffer inbuf contains already the beginning of | ||
955 | * the compressed data, from offsets inptr to insize-1 included. | ||
956 | * The magic header has already been checked. The output buffer is cleared. | ||
957 | * in, out: input and output file descriptors | ||
958 | */ | ||
959 | extern int unzip(FILE * l_in_file, FILE * l_out_file) | ||
960 | { | ||
961 | unsigned char buf[8]; /* extended local header */ | ||
962 | unsigned char flags; /* compression flags */ | ||
963 | typedef void (*sig_type) (int); | ||
964 | unsigned short i; | ||
965 | unsigned char magic[2]; | ||
966 | 854 | ||
967 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) { | 855 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) { |
968 | (void) signal(SIGINT, (sig_type) abort_gzip); | 856 | (void) signal(SIGINT, (sig_type) abort_gzip); |
@@ -973,97 +861,44 @@ extern int unzip(FILE * l_in_file, FILE * l_out_file) | |||
973 | } | 861 | } |
974 | #endif | 862 | #endif |
975 | 863 | ||
976 | magic[0] = fgetc(l_in_file); | 864 | /* initialize gunzip_window, bit buffer */ |
977 | magic[1] = fgetc(l_in_file); | 865 | gunzip_bk = 0; |
978 | 866 | gunzip_bb = 0; | |
979 | #ifdef CONFIG_FEATURE_UNCOMPRESS | ||
980 | /* Magic header for compress files, 1F 9d = \037\235 */ | ||
981 | if ((magic[0] == 0x1F) && (magic[1] == 0x9d)) { | ||
982 | return uncompress(l_in_file, l_out_file); | ||
983 | } | ||
984 | #endif | ||
985 | |||
986 | /* Magic header for gzip files, 1F 8B = \037\213 */ | ||
987 | if ((magic[0] != 0x1F) || (magic[1] != 0x8b)) { | ||
988 | error_msg("Invalid gzip magic"); | ||
989 | return EXIT_FAILURE; | ||
990 | } | ||
991 | |||
992 | /* Check the compression method */ | ||
993 | if (fgetc(l_in_file) != 8) /* also catches EOF */ { | ||
994 | error_msg("Unknown compression method"); | ||
995 | return (-1); | ||
996 | } | ||
997 | |||
998 | flags = (unsigned char) fgetc(l_in_file); | ||
999 | |||
1000 | /* Ignore time stamp(4), extra flags(1), OS type(1) */ | ||
1001 | for (i = 0; i < 6; i++) { | ||
1002 | fgetc(l_in_file); | ||
1003 | } | ||
1004 | 867 | ||
1005 | if (flags & 0x04) { | 868 | /* Create the crc table */ |
1006 | /* bit 2 set: extra field present */ | 869 | make_gunzip_crc_table(); |
1007 | const unsigned short extra = | ||
1008 | fgetc(l_in_file) + (fgetc(l_in_file) << 8); | ||
1009 | if (feof(in_file)) return 1; | ||
1010 | for (i = 0; i < extra; i++) { | ||
1011 | fgetc(l_in_file); | ||
1012 | } | ||
1013 | } | ||
1014 | |||
1015 | /* Discard original name if any */ | ||
1016 | if (flags & 0x08) { | ||
1017 | /* bit 3 set: original file name present */ | ||
1018 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ | ||
1019 | } | ||
1020 | 870 | ||
1021 | /* Discard file comment if any */ | 871 | /* decompress until the last block */ |
1022 | if (flags & 0x10) { | 872 | do { |
1023 | /* bit 4 set: file comment present */ | 873 | gunzip_hufts = 0; |
1024 | while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */ | 874 | r = inflate_block(&e); |
875 | if (r != 0) { | ||
876 | error_msg_and_die("inflate error %d", r); | ||
877 | return r; | ||
1025 | } | 878 | } |
1026 | 879 | if (gunzip_hufts > h) { | |
1027 | /* Decompress */ | 880 | h = gunzip_hufts; |
1028 | if (inflate(l_in_file, l_out_file) != 0) { | ||
1029 | error_msg("invalid compressed data--format violated"); | ||
1030 | } | 881 | } |
882 | } while (!e); | ||
1031 | 883 | ||
1032 | /* Get the crc and original length | 884 | /* write any buffered uncompressed data */ |
1033 | * crc32 (see algorithm.doc) | 885 | flush_gunzip_window(); |
1034 | * uncompressed input size modulo 2^32 | 886 | free(gunzip_window); |
1035 | */ | 887 | |
1036 | fread(buf, 1, 8, l_in_file); | 888 | /* Cleanup */ |
1037 | 889 | free(gunzip_crc_table); | |
1038 | /* Validate decompression - crc */ | 890 | |
1039 | if ((unsigned int) ((buf[0] | (buf[1] << 8)) | | 891 | /* Store unused bytes in a global buffer so calling applets can access it */ |
1040 | ((buf[2] | (buf[3] << 8)) << 16)) != | 892 | gunzip_in_buffer_count = 0; |
1041 | (crc ^ 0xffffffffL)) { | 893 | if (gunzip_bk >= 8) { |
1042 | error_msg("invalid compressed data--crc error"); | 894 | /* Undo too much lookahead. The next read will be byte aligned |
1043 | } | 895 | * so we can discard unused bits in the last meaningful byte. */ |
1044 | /* Validate decompression - size */ | 896 | gunzip_in_buffer[gunzip_in_buffer_count] = gunzip_bb & 0xff; |
1045 | if (((buf[4] | (buf[5] << 8)) | ((buf[6] | (buf[7] << 8)) << 16)) != | 897 | gunzip_in_buffer_count++; |
1046 | (unsigned long) bytes_out) { | 898 | gunzip_bb >>= 8; |
1047 | error_msg("invalid compressed data--length error"); | 899 | gunzip_bk -= 8; |
1048 | } | 900 | } |
1049 | 901 | ||
902 | /* return success */ | ||
1050 | return 0; | 903 | return 0; |
1051 | } | 904 | } |
1052 | |||
1053 | /* | ||
1054 | * This needs access to global variables window and crc_table, so its not in its own file. | ||
1055 | */ | ||
1056 | extern void gz_close(int gunzip_pid) | ||
1057 | { | ||
1058 | if (kill(gunzip_pid, SIGTERM) == -1) { | ||
1059 | error_msg_and_die | ||
1060 | ("*** Couldnt kill old gunzip process *** aborting"); | ||
1061 | } | ||
1062 | |||
1063 | if (waitpid(gunzip_pid, NULL, 0) == -1) { | ||
1064 | printf("Couldnt wait ?"); | ||
1065 | } | ||
1066 | |||
1067 | free(window); | ||
1068 | free(crc_table); | ||
1069 | } | ||
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 869c04a4c..2249e263a 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -19,10 +19,13 @@ | |||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | */ | 20 | */ |
21 | 21 | ||
22 | #include <sys/types.h> | ||
23 | #include <sys/stat.h> | ||
22 | #include <stdio.h> | 24 | #include <stdio.h> |
23 | #include <string.h> | 25 | #include <string.h> |
24 | #include <stdlib.h> | 26 | #include <stdlib.h> |
25 | #include <unistd.h> | 27 | #include <unistd.h> |
28 | #include <fcntl.h> | ||
26 | #include "libbb.h" | 29 | #include "libbb.h" |
27 | 30 | ||
28 | 31 | ||
@@ -85,6 +88,59 @@ FILE *xfopen(const char *path, const char *mode) | |||
85 | return fp; | 88 | return fp; |
86 | } | 89 | } |
87 | 90 | ||
91 | extern int xopen(const char *pathname, int flags) | ||
92 | { | ||
93 | int ret; | ||
94 | |||
95 | ret = open(pathname, flags); | ||
96 | if (ret == -1) { | ||
97 | perror_msg_and_die("%s", pathname); | ||
98 | } | ||
99 | return ret; | ||
100 | } | ||
101 | |||
102 | extern ssize_t xread(int fd, void *buf, size_t count) | ||
103 | { | ||
104 | ssize_t size; | ||
105 | |||
106 | size = read(fd, buf, count); | ||
107 | if (size == -1) { | ||
108 | perror_msg_and_die("Read error"); | ||
109 | } | ||
110 | return(size); | ||
111 | } | ||
112 | |||
113 | extern void xread_all(int fd, void *buf, size_t count) | ||
114 | { | ||
115 | ssize_t size; | ||
116 | |||
117 | size = xread(fd, buf, count); | ||
118 | if (size != count) { | ||
119 | error_msg_and_die("Short read"); | ||
120 | } | ||
121 | return; | ||
122 | } | ||
123 | |||
124 | extern ssize_t xread_all_eof(int fd, void *buf, size_t count) | ||
125 | { | ||
126 | ssize_t size; | ||
127 | |||
128 | size = xread(fd, buf, count); | ||
129 | if ((size != 0) && (size != count)) { | ||
130 | error_msg_and_die("Short read"); | ||
131 | } | ||
132 | return(size); | ||
133 | } | ||
134 | |||
135 | extern unsigned char xread_char(int fd) | ||
136 | { | ||
137 | char tmp; | ||
138 | |||
139 | xread_all(fd, &tmp, 1); | ||
140 | |||
141 | return(tmp); | ||
142 | } | ||
143 | |||
88 | /* Stupid gcc always includes its own builtin strlen()... */ | 144 | /* Stupid gcc always includes its own builtin strlen()... */ |
89 | #undef strlen | 145 | #undef strlen |
90 | size_t xstrlen(const char *string) | 146 | size_t xstrlen(const char *string) |