diff options
Diffstat (limited to 'gunzip.c')
-rw-r--r-- | gunzip.c | 2520 |
1 files changed, 1267 insertions, 1253 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* zcat : stripped version based on gzip sources | 2 | /* zcat : stripped version based on gzip sources |
2 | Sven Rudolph <sr1@inf.tu-dresden.de> | 3 | Sven Rudolph <sr1@inf.tu-dresden.de> |
3 | */ | 4 | */ |
@@ -8,11 +9,12 @@ | |||
8 | #include "messages.c" | 9 | #include "messages.c" |
9 | 10 | ||
10 | static const char gunzip_usage[] = | 11 | static const char gunzip_usage[] = |
11 | "gunzip [OPTION]... FILE\n\n" | 12 | "gunzip [OPTION]... FILE\n\n" |
12 | "Uncompress FILE (or standard input if FILE is '-').\n\n" | 13 | "Uncompress FILE (or standard input if FILE is '-').\n\n" |
13 | "Options:\n" | 14 | "Options:\n" |
14 | "\t-c\tWrite output to standard output\n" | 15 | |
15 | "\t-t\tTest compressed file integrity\n"; | 16 | "\t-c\tWrite output to standard output\n" |
17 | "\t-t\tTest compressed file integrity\n"; | ||
16 | 18 | ||
17 | /* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface | 19 | /* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface |
18 | * Copyright (C) 1992-1993 Jean-loup Gailly | 20 | * Copyright (C) 1992-1993 Jean-loup Gailly |
@@ -26,22 +28,23 @@ static const char gunzip_usage[] = | |||
26 | */ | 28 | */ |
27 | 29 | ||
28 | #if 0 | 30 | #if 0 |
29 | static char *license_msg[] = { | 31 | static char *license_msg[] = { |
30 | " Copyright (C) 1992-1993 Jean-loup Gailly", | 32 | " Copyright (C) 1992-1993 Jean-loup Gailly", |
31 | " This program is free software; you can redistribute it and/or modify", | 33 | " This program is free software; you can redistribute it and/or modify", |
32 | " it under the terms of the GNU General Public License as published by", | 34 | " it under the terms of the GNU General Public License as published by", |
33 | " the Free Software Foundation; either version 2, or (at your option)", | 35 | " the Free Software Foundation; either version 2, or (at your option)", |
34 | " any later version.", | 36 | " any later version.", |
35 | "", | 37 | "", |
36 | " This program is distributed in the hope that it will be useful,", | 38 | " This program is distributed in the hope that it will be useful,", |
37 | " but WITHOUT ANY WARRANTY; without even the implied warranty of", | 39 | " but WITHOUT ANY WARRANTY; without even the implied warranty of", |
38 | " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the", | 40 | " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the", |
39 | " GNU General Public License for more details.", | 41 | " GNU General Public License for more details.", |
40 | "", | 42 | "", |
41 | " You should have received a copy of the GNU General Public License", | 43 | " You should have received a copy of the GNU General Public License", |
42 | " along with this program; if not, write to the Free Software", | 44 | " along with this program; if not, write to the Free Software", |
43 | " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.", | 45 | " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.", |
44 | 0}; | 46 | 0 |
47 | }; | ||
45 | #endif | 48 | #endif |
46 | 49 | ||
47 | /* Compress files with zip algorithm and 'compress' interface. | 50 | /* Compress files with zip algorithm and 'compress' interface. |
@@ -67,7 +70,7 @@ static char *license_msg[] = { | |||
67 | #include <signal.h> | 70 | #include <signal.h> |
68 | #include <sys/stat.h> | 71 | #include <sys/stat.h> |
69 | #include <errno.h> | 72 | #include <errno.h> |
70 | #include <sys/param.h> /* for PATH_MAX */ | 73 | #include <sys/param.h> /* for PATH_MAX */ |
71 | 74 | ||
72 | /* #include "tailor.h" */ | 75 | /* #include "tailor.h" */ |
73 | 76 | ||
@@ -101,9 +104,9 @@ static char *license_msg[] = { | |||
101 | #endif | 104 | #endif |
102 | 105 | ||
103 | #ifdef __STDC__ | 106 | #ifdef __STDC__ |
104 | typedef void *voidp; | 107 | typedef void *voidp; |
105 | #else | 108 | #else |
106 | typedef char *voidp; | 109 | typedef char *voidp; |
107 | #endif | 110 | #endif |
108 | 111 | ||
109 | /* I don't like nested includes, but the string and io functions are used | 112 | /* I don't like nested includes, but the string and io functions are used |
@@ -118,10 +121,10 @@ static char *license_msg[] = { | |||
118 | # define memzero(s, n) memset ((voidp)(s), 0, (n)) | 121 | # define memzero(s, n) memset ((voidp)(s), 0, (n)) |
119 | #else | 122 | #else |
120 | # include <strings.h> | 123 | # include <strings.h> |
121 | # define strchr index | 124 | # define strchr index |
122 | # define strrchr rindex | 125 | # define strrchr rindex |
123 | # define memcpy(d, s, n) bcopy((s), (d), (n)) | 126 | # define memcpy(d, s, n) bcopy((s), (d), (n)) |
124 | # define memcmp(s1, s2, n) bcmp((s1), (s2), (n)) | 127 | # define memcmp(s1, s2, n) bcmp((s1), (s2), (n)) |
125 | # define memzero(s, n) bzero((s), (n)) | 128 | # define memzero(s, n) bzero((s), (n)) |
126 | #endif | 129 | #endif |
127 | 130 | ||
@@ -131,9 +134,9 @@ static char *license_msg[] = { | |||
131 | 134 | ||
132 | #define local static | 135 | #define local static |
133 | 136 | ||
134 | typedef unsigned char uch; | 137 | typedef unsigned char uch; |
135 | typedef unsigned short ush; | 138 | typedef unsigned short ush; |
136 | typedef unsigned long ulg; | 139 | typedef unsigned long ulg; |
137 | 140 | ||
138 | /* Return codes from gzip */ | 141 | /* Return codes from gzip */ |
139 | #define OK 0 | 142 | #define OK 0 |
@@ -143,7 +146,7 @@ typedef unsigned long ulg; | |||
143 | /* Compression methods (see algorithm.doc) */ | 146 | /* Compression methods (see algorithm.doc) */ |
144 | #define DEFLATED 8 | 147 | #define DEFLATED 8 |
145 | 148 | ||
146 | extern int method; /* compression method */ | 149 | extern int method; /* compression method */ |
147 | 150 | ||
148 | /* To save memory for 16 bit systems, some arrays are overlaid between | 151 | /* To save memory for 16 bit systems, some arrays are overlaid between |
149 | * the various modules: | 152 | * the various modules: |
@@ -158,29 +161,29 @@ extern int method; /* compression method */ | |||
158 | 161 | ||
159 | #ifndef INBUFSIZ | 162 | #ifndef INBUFSIZ |
160 | # ifdef SMALL_MEM | 163 | # ifdef SMALL_MEM |
161 | # define INBUFSIZ 0x2000 /* input buffer size */ | 164 | # define INBUFSIZ 0x2000 /* input buffer size */ |
162 | # else | 165 | # else |
163 | # define INBUFSIZ 0x8000 /* input buffer size */ | 166 | # define INBUFSIZ 0x8000 /* input buffer size */ |
164 | # endif | 167 | # endif |
165 | #endif | 168 | #endif |
166 | #define INBUF_EXTRA 64 /* required by unlzw() */ | 169 | #define INBUF_EXTRA 64 /* required by unlzw() */ |
167 | 170 | ||
168 | #ifndef OUTBUFSIZ | 171 | #ifndef OUTBUFSIZ |
169 | # ifdef SMALL_MEM | 172 | # ifdef SMALL_MEM |
170 | # define OUTBUFSIZ 8192 /* output buffer size */ | 173 | # define OUTBUFSIZ 8192 /* output buffer size */ |
171 | # else | 174 | # else |
172 | # define OUTBUFSIZ 16384 /* output buffer size */ | 175 | # define OUTBUFSIZ 16384 /* output buffer size */ |
173 | # endif | 176 | # endif |
174 | #endif | 177 | #endif |
175 | #define OUTBUF_EXTRA 2048 /* required by unlzw() */ | 178 | #define OUTBUF_EXTRA 2048 /* required by unlzw() */ |
176 | 179 | ||
177 | #define SMALL_MEM | 180 | #define SMALL_MEM |
178 | 181 | ||
179 | #ifndef DIST_BUFSIZE | 182 | #ifndef DIST_BUFSIZE |
180 | # ifdef SMALL_MEM | 183 | # ifdef SMALL_MEM |
181 | # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */ | 184 | # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */ |
182 | # else | 185 | # else |
183 | # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */ | 186 | # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */ |
184 | # endif | 187 | # endif |
185 | #endif | 188 | #endif |
186 | 189 | ||
@@ -201,50 +204,51 @@ extern int method; /* compression method */ | |||
201 | # define FREE(array) | 204 | # define FREE(array) |
202 | #endif | 205 | #endif |
203 | 206 | ||
204 | EXTERN(uch, inbuf); /* input buffer */ | 207 | EXTERN(uch, inbuf); /* input buffer */ |
205 | EXTERN(uch, outbuf); /* output buffer */ | 208 | EXTERN(uch, outbuf); /* output buffer */ |
206 | EXTERN(ush, d_buf); /* buffer for distances, see trees.c */ | 209 | EXTERN(ush, d_buf); /* buffer for distances, see trees.c */ |
207 | EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */ | 210 | EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */ |
208 | #define tab_suffix window | 211 | #define tab_suffix window |
209 | #ifndef MAXSEG_64K | 212 | #ifndef MAXSEG_64K |
210 | # define tab_prefix prev /* hash link (see deflate.c) */ | 213 | # define tab_prefix prev /* hash link (see deflate.c) */ |
211 | # define head (prev+WSIZE) /* hash head (see deflate.c) */ | 214 | # define head (prev+WSIZE) /* hash head (see deflate.c) */ |
212 | EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */ | 215 | EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */ |
213 | #else | 216 | #else |
214 | # define tab_prefix0 prev | 217 | # define tab_prefix0 prev |
215 | # define head tab_prefix1 | 218 | # define head tab_prefix1 |
216 | EXTERN(ush, tab_prefix0); /* prefix for even codes */ | 219 | EXTERN(ush, tab_prefix0); /* prefix for even codes */ |
217 | EXTERN(ush, tab_prefix1); /* prefix for odd codes */ | 220 | EXTERN(ush, tab_prefix1); /* prefix for odd codes */ |
218 | #endif | 221 | #endif |
219 | 222 | ||
220 | extern unsigned insize; /* valid bytes in inbuf */ | 223 | extern unsigned insize; /* valid bytes in inbuf */ |
221 | extern unsigned inptr; /* index of next byte to be processed in inbuf */ | 224 | extern unsigned inptr; /* index of next byte to be processed in inbuf */ |
222 | extern unsigned outcnt; /* bytes in output buffer */ | 225 | extern unsigned outcnt; /* bytes in output buffer */ |
226 | |||
227 | extern long bytes_in; /* number of input bytes */ | ||
228 | extern long bytes_out; /* number of output bytes */ | ||
229 | extern long header_bytes; /* number of bytes in gzip header */ | ||
223 | 230 | ||
224 | extern long bytes_in; /* number of input bytes */ | 231 | extern long ifile_size; /* input file size, -1 for devices (debug only) */ |
225 | extern long bytes_out; /* number of output bytes */ | ||
226 | extern long header_bytes;/* number of bytes in gzip header */ | ||
227 | 232 | ||
228 | extern long ifile_size; /* input file size, -1 for devices (debug only) */ | 233 | typedef int file_t; /* Do not use stdio */ |
229 | 234 | ||
230 | typedef int file_t; /* Do not use stdio */ | 235 | #define NO_FILE (-1) /* in memory compression */ |
231 | #define NO_FILE (-1) /* in memory compression */ | ||
232 | 236 | ||
233 | 237 | ||
234 | #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */ | 238 | #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */ |
235 | 239 | ||
236 | /* gzip flag byte */ | 240 | /* gzip flag byte */ |
237 | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ | 241 | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ |
238 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ | 242 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ |
239 | #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ | 243 | #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ |
240 | #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ | 244 | #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ |
241 | #define COMMENT 0x10 /* bit 4 set: file comment present */ | 245 | #define COMMENT 0x10 /* bit 4 set: file comment present */ |
242 | #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ | 246 | #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ |
243 | #define RESERVED 0xC0 /* bit 6,7: reserved */ | 247 | #define RESERVED 0xC0 /* bit 6,7: reserved */ |
244 | 248 | ||
245 | #ifndef WSIZE | 249 | #ifndef WSIZE |
246 | # define WSIZE 0x8000 /* window size--must be a power of two, and */ | 250 | # define WSIZE 0x8000 /* window size--must be a power of two, and */ |
247 | #endif /* at least 32K for zip's deflate method */ | 251 | #endif /* at least 32K for zip's deflate method */ |
248 | 252 | ||
249 | #define MIN_MATCH 3 | 253 | #define MIN_MATCH 3 |
250 | #define MAX_MATCH 258 | 254 | #define MAX_MATCH 258 |
@@ -260,11 +264,11 @@ typedef int file_t; /* Do not use stdio */ | |||
260 | * distances are limited to MAX_DIST instead of WSIZE. | 264 | * distances are limited to MAX_DIST instead of WSIZE. |
261 | */ | 265 | */ |
262 | 266 | ||
263 | extern int exit_code; /* program exit code */ | 267 | extern int exit_code; /* program exit code */ |
264 | extern int verbose; /* be verbose (-v) */ | 268 | extern int verbose; /* be verbose (-v) */ |
265 | extern int level; /* compression level */ | 269 | extern int level; /* compression level */ |
266 | extern int test; /* check .z file integrity */ | 270 | extern int test; /* check .z file integrity */ |
267 | extern int save_orig_name; /* set if original name must be saved */ | 271 | extern int save_orig_name; /* set if original name must be saved */ |
268 | 272 | ||
269 | #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0)) | 273 | #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0)) |
270 | #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1)) | 274 | #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1)) |
@@ -296,10 +300,10 @@ extern int save_orig_name; /* set if original name must be saved */ | |||
296 | put_short(((ulg)(n)) >> 16); \ | 300 | put_short(((ulg)(n)) >> 16); \ |
297 | } | 301 | } |
298 | 302 | ||
299 | #define seekable() 0 /* force sequential output */ | 303 | #define seekable() 0 /* force sequential output */ |
300 | #define translate_eol 0 /* no option -a yet */ | 304 | #define translate_eol 0 /* no option -a yet */ |
301 | 305 | ||
302 | #define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */ | 306 | #define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */ |
303 | 307 | ||
304 | /* Macros for getting two-byte and four-byte header values */ | 308 | /* Macros for getting two-byte and four-byte header values */ |
305 | #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)) | 309 | #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)) |
@@ -329,43 +333,44 @@ extern int save_orig_name; /* set if original name must be saved */ | |||
329 | 333 | ||
330 | 334 | ||
331 | /* in unzip.c */ | 335 | /* in unzip.c */ |
332 | extern int unzip OF((int in, int out)); | 336 | extern int unzip OF((int in, int out)); |
333 | 337 | ||
334 | /* in gzip.c */ | 338 | /* in gzip.c */ |
335 | RETSIGTYPE abort_gzip OF((void)); | 339 | RETSIGTYPE abort_gzip OF((void)); |
336 | 340 | ||
337 | /* in deflate.c */ | 341 | /* in deflate.c */ |
338 | void lm_init OF((int pack_level, ush *flags)); | 342 | void lm_init OF((int pack_level, ush * flags)); |
339 | ulg deflate OF((void)); | 343 | ulg deflate OF((void)); |
340 | 344 | ||
341 | /* in trees.c */ | 345 | /* in trees.c */ |
342 | void ct_init OF((ush *attr, int *method)); | 346 | void ct_init OF((ush * attr, int *method)); |
343 | int ct_tally OF((int dist, int lc)); | 347 | int ct_tally OF((int dist, int lc)); |
344 | ulg flush_block OF((char *buf, ulg stored_len, int eof)); | 348 | ulg flush_block OF((char *buf, ulg stored_len, int eof)); |
345 | 349 | ||
346 | /* in bits.c */ | 350 | /* in bits.c */ |
347 | void bi_init OF((file_t zipfile)); | 351 | void bi_init OF((file_t zipfile)); |
348 | void send_bits OF((int value, int length)); | 352 | void send_bits OF((int value, int length)); |
349 | unsigned bi_reverse OF((unsigned value, int length)); | 353 | unsigned bi_reverse OF((unsigned value, int length)); |
350 | void bi_windup OF((void)); | 354 | void bi_windup OF((void)); |
351 | void copy_block OF((char *buf, unsigned len, int header)); | 355 | void copy_block OF((char *buf, unsigned len, int header)); |
352 | extern int (*read_buf) OF((char *buf, unsigned size)); | 356 | extern int (*read_buf) OF((char *buf, unsigned size)); |
353 | 357 | ||
354 | /* in util.c: */ | 358 | /* in util.c: */ |
355 | extern int copy OF((int in, int out)); | 359 | extern int copy OF((int in, int out)); |
356 | extern ulg updcrc OF((uch *s, unsigned n)); | 360 | extern ulg updcrc OF((uch * s, unsigned n)); |
357 | extern void clear_bufs OF((void)); | 361 | extern void clear_bufs OF((void)); |
358 | extern int fill_inbuf OF((int eof_ok)); | 362 | extern int fill_inbuf OF((int eof_ok)); |
359 | extern void flush_outbuf OF((void)); | 363 | extern void flush_outbuf OF((void)); |
360 | extern void flush_window OF((void)); | 364 | extern void flush_window OF((void)); |
361 | extern void write_buf OF((int fd, voidp buf, unsigned cnt)); | 365 | extern void write_buf OF((int fd, voidp buf, unsigned cnt)); |
366 | |||
362 | #ifndef __linux__ | 367 | #ifndef __linux__ |
363 | extern char *basename OF((char *fname)); | 368 | extern char *basename OF((char *fname)); |
364 | #endif /* not __linux__ */ | 369 | #endif /* not __linux__ */ |
365 | extern void error OF((char *m)); | 370 | extern void error OF((char *m)); |
366 | extern void warn OF((char *a, char *b)); | 371 | extern void warn OF((char *a, char *b)); |
367 | extern void read_error OF((void)); | 372 | extern void read_error OF((void)); |
368 | extern void write_error OF((void)); | 373 | extern void write_error OF((void)); |
369 | 374 | ||
370 | /* in inflate.c */ | 375 | /* in inflate.c */ |
371 | extern int inflate OF((void)); | 376 | extern int inflate OF((void)); |
@@ -385,11 +390,11 @@ extern int inflate OF((void)); | |||
385 | #ifndef BITS | 390 | #ifndef BITS |
386 | # define BITS 16 | 391 | # define BITS 16 |
387 | #endif | 392 | #endif |
388 | #define INIT_BITS 9 /* Initial number of bits per code */ | 393 | #define INIT_BITS 9 /* Initial number of bits per code */ |
389 | 394 | ||
390 | #define LZW_MAGIC "\037\235" /* Magic header for lzw files, 1F 9D */ | 395 | #define LZW_MAGIC "\037\235" /* Magic header for lzw files, 1F 9D */ |
391 | 396 | ||
392 | #define BIT_MASK 0x1f /* Mask for 'number of compression bits' */ | 397 | #define BIT_MASK 0x1f /* Mask for 'number of compression bits' */ |
393 | /* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free. | 398 | /* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free. |
394 | * It's a pity that old uncompress does not check bit 0x20. That makes | 399 | * It's a pity that old uncompress does not check bit 0x20. That makes |
395 | * extension of the format actually undesirable because old compress | 400 | * extension of the format actually undesirable because old compress |
@@ -404,16 +409,16 @@ extern int inflate OF((void)); | |||
404 | * clear the dictionary. | 409 | * clear the dictionary. |
405 | */ | 410 | */ |
406 | 411 | ||
407 | #define LZW_RESERVED 0x60 /* reserved bits */ | 412 | #define LZW_RESERVED 0x60 /* reserved bits */ |
408 | 413 | ||
409 | #define CLEAR 256 /* flush the dictionary */ | 414 | #define CLEAR 256 /* flush the dictionary */ |
410 | #define FIRST (CLEAR+1) /* first free entry */ | 415 | #define FIRST (CLEAR+1) /* first free entry */ |
411 | 416 | ||
412 | extern int maxbits; /* max bits per code for LZW */ | 417 | extern int maxbits; /* max bits per code for LZW */ |
413 | extern int block_mode; /* block compress mode -C compatible with 2.0 */ | 418 | extern int block_mode; /* block compress mode -C compatible with 2.0 */ |
414 | 419 | ||
415 | extern int lzw OF((int in, int out)); | 420 | extern int lzw OF((int in, int out)); |
416 | extern int unlzw OF((int in, int out)); | 421 | extern int unlzw OF((int in, int out)); |
417 | 422 | ||
418 | 423 | ||
419 | /* #include "revision.h" */ | 424 | /* #include "revision.h" */ |
@@ -458,14 +463,12 @@ extern int unlzw OF((int in, int out)); | |||
458 | #ifdef __cplusplus | 463 | #ifdef __cplusplus |
459 | extern "C" { | 464 | extern "C" { |
460 | #endif | 465 | #endif |
461 | |||
462 | /* For communication from `getopt' to the caller. | 466 | /* For communication from `getopt' to the caller. |
463 | When `getopt' finds an option that takes an argument, | 467 | When `getopt' finds an option that takes an argument, |
464 | the argument value is returned here. | 468 | the argument value is returned here. |
465 | Also, when `ordering' is RETURN_IN_ORDER, | 469 | Also, when `ordering' is RETURN_IN_ORDER, |
466 | each non-option ARGV-element is returned here. */ | 470 | each non-option ARGV-element is returned here. */ |
467 | 471 | extern char *optarg; | |
468 | extern char *optarg; | ||
469 | 472 | ||
470 | /* Index in ARGV of the next element to be scanned. | 473 | /* Index in ARGV of the next element to be scanned. |
471 | This is used for communication to and from the caller | 474 | This is used for communication to and from the caller |
@@ -479,16 +482,16 @@ extern char *optarg; | |||
479 | Otherwise, `optind' communicates from one call to the next | 482 | Otherwise, `optind' communicates from one call to the next |
480 | how much of ARGV has been scanned so far. */ | 483 | how much of ARGV has been scanned so far. */ |
481 | 484 | ||
482 | extern int optind; | 485 | extern int optind; |
483 | 486 | ||
484 | /* Callers store zero here to inhibit the error message `getopt' prints | 487 | /* Callers store zero here to inhibit the error message `getopt' prints |
485 | for unrecognized options. */ | 488 | for unrecognized options. */ |
486 | 489 | ||
487 | extern int opterr; | 490 | extern int opterr; |
488 | 491 | ||
489 | /* Set to an option character which was unrecognized. */ | 492 | /* Set to an option character which was unrecognized. */ |
490 | 493 | ||
491 | extern int optopt; | 494 | extern int optopt; |
492 | 495 | ||
493 | /* Describe the long-named options requested by the application. | 496 | /* Describe the long-named options requested by the application. |
494 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector | 497 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector |
@@ -511,19 +514,18 @@ extern int optopt; | |||
511 | one). For long options that have a zero `flag' field, `getopt' | 514 | one). For long options that have a zero `flag' field, `getopt' |
512 | returns the contents of the `val' field. */ | 515 | returns the contents of the `val' field. */ |
513 | 516 | ||
514 | struct option | 517 | struct option { |
515 | { | ||
516 | #if __STDC__ | 518 | #if __STDC__ |
517 | const char *name; | 519 | const char *name; |
518 | #else | 520 | #else |
519 | char *name; | 521 | char *name; |
520 | #endif | 522 | #endif |
521 | /* has_arg can't be an enum because some compilers complain about | 523 | /* has_arg can't be an enum because some compilers complain about |
522 | type mismatches in all the code that assumes it is an int. */ | 524 | type mismatches in all the code that assumes it is an int. */ |
523 | int has_arg; | 525 | int has_arg; |
524 | int *flag; | 526 | int *flag; |
525 | int val; | 527 | int val; |
526 | }; | 528 | }; |
527 | 529 | ||
528 | /* Names for the values of the `has_arg' field of `struct option'. */ | 530 | /* Names for the values of the `has_arg' field of `struct option'. */ |
529 | 531 | ||
@@ -536,60 +538,61 @@ struct option | |||
536 | /* Many other libraries have conflicting prototypes for getopt, with | 538 | /* Many other libraries have conflicting prototypes for getopt, with |
537 | differences in the consts, in stdlib.h. To avoid compilation | 539 | differences in the consts, in stdlib.h. To avoid compilation |
538 | errors, only prototype getopt for the GNU C library. */ | 540 | errors, only prototype getopt for the GNU C library. */ |
539 | extern int getopt (int argc, char *const *argv, const char *shortopts); | 541 | extern int getopt(int argc, char *const *argv, const char *shortopts); |
540 | #endif /* not __GNU_LIBRARY__ */ | 542 | #endif /* not __GNU_LIBRARY__ */ |
541 | extern int getopt_long (int argc, char *const *argv, const char *shortopts, | 543 | extern int getopt_long(int argc, char *const *argv, |
542 | const struct option *longopts, int *longind); | 544 | const char *shortopts, |
543 | extern int getopt_long_only (int argc, char *const *argv, | 545 | const struct option *longopts, int *longind); |
544 | const char *shortopts, | 546 | extern int getopt_long_only(int argc, char *const *argv, |
545 | const struct option *longopts, int *longind); | 547 | const char *shortopts, |
548 | const struct option *longopts, | ||
549 | int *longind); | ||
546 | 550 | ||
547 | /* Internal only. Users should not call this directly. */ | 551 | /* Internal only. Users should not call this directly. */ |
548 | extern int _getopt_internal (int argc, char *const *argv, | 552 | extern int _getopt_internal(int argc, char *const *argv, |
549 | const char *shortopts, | 553 | const char *shortopts, |
550 | const struct option *longopts, int *longind, | 554 | const struct option *longopts, |
551 | int long_only); | 555 | int *longind, int long_only); |
552 | #else /* not __STDC__ */ | 556 | #else /* not __STDC__ */ |
553 | extern int getopt (); | 557 | extern int getopt(); |
554 | extern int getopt_long (); | 558 | extern int getopt_long(); |
555 | extern int getopt_long_only (); | 559 | extern int getopt_long_only(); |
556 | 560 | ||
557 | extern int _getopt_internal (); | 561 | extern int _getopt_internal(); |
558 | #endif /* not __STDC__ */ | 562 | #endif /* not __STDC__ */ |
559 | 563 | ||
560 | #ifdef __cplusplus | 564 | #ifdef __cplusplus |
561 | } | 565 | } |
562 | #endif | 566 | #endif |
563 | 567 | #endif /* _GETOPT_H */ | |
564 | #endif /* _GETOPT_H */ | ||
565 | |||
566 | |||
567 | #include <time.h> | 568 | #include <time.h> |
568 | #include <fcntl.h> | 569 | #include <fcntl.h> |
569 | #include <unistd.h> | 570 | #include <unistd.h> |
570 | |||
571 | #include <stdlib.h> | 571 | #include <stdlib.h> |
572 | |||
573 | #if defined(DIRENT) | 572 | #if defined(DIRENT) |
574 | # include <dirent.h> | 573 | # include <dirent.h> |
575 | typedef struct dirent dir_type; | 574 | typedef struct dirent dir_type; |
575 | |||
576 | # define NLENGTH(dirent) ((int)strlen((dirent)->d_name)) | 576 | # define NLENGTH(dirent) ((int)strlen((dirent)->d_name)) |
577 | # define DIR_OPT "DIRENT" | 577 | # define DIR_OPT "DIRENT" |
578 | #else | 578 | #else |
579 | # define NLENGTH(dirent) ((dirent)->d_namlen) | 579 | # define NLENGTH(dirent) ((dirent)->d_namlen) |
580 | # ifdef SYSDIR | 580 | # ifdef SYSDIR |
581 | # include <sys/dir.h> | 581 | # include <sys/dir.h> |
582 | typedef struct direct dir_type; | 582 | typedef struct direct dir_type; |
583 | |||
583 | # define DIR_OPT "SYSDIR" | 584 | # define DIR_OPT "SYSDIR" |
584 | # else | 585 | # else |
585 | # ifdef SYSNDIR | 586 | # ifdef SYSNDIR |
586 | # include <sys/ndir.h> | 587 | # include <sys/ndir.h> |
587 | typedef struct direct dir_type; | 588 | typedef struct direct dir_type; |
589 | |||
588 | # define DIR_OPT "SYSNDIR" | 590 | # define DIR_OPT "SYSNDIR" |
589 | # else | 591 | # else |
590 | # ifdef NDIR | 592 | # ifdef NDIR |
591 | # include <ndir.h> | 593 | # include <ndir.h> |
592 | typedef struct direct dir_type; | 594 | typedef struct direct dir_type; |
595 | |||
593 | # define DIR_OPT "NDIR" | 596 | # define DIR_OPT "NDIR" |
594 | # else | 597 | # else |
595 | # define NO_DIR | 598 | # define NO_DIR |
@@ -598,18 +601,16 @@ extern int _getopt_internal (); | |||
598 | # endif | 601 | # endif |
599 | # endif | 602 | # endif |
600 | #endif | 603 | #endif |
601 | |||
602 | #if !defined(S_ISDIR) && defined(S_IFDIR) | 604 | #if !defined(S_ISDIR) && defined(S_IFDIR) |
603 | # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | 605 | # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
604 | #endif | 606 | #endif |
605 | #if !defined(S_ISREG) && defined(S_IFREG) | 607 | #if !defined(S_ISREG) && defined(S_IFREG) |
606 | # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) | 608 | # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
607 | #endif | 609 | #endif |
608 | 610 | typedef RETSIGTYPE(*sig_type) OF((int)); | |
609 | typedef RETSIGTYPE (*sig_type) OF((int)); | ||
610 | 611 | ||
611 | #ifndef O_BINARY | 612 | #ifndef O_BINARY |
612 | # define O_BINARY 0 /* creation mode for open() */ | 613 | # define O_BINARY 0 /* creation mode for open() */ |
613 | #endif | 614 | #endif |
614 | 615 | ||
615 | #ifndef O_CREAT | 616 | #ifndef O_CREAT |
@@ -629,9 +630,9 @@ typedef RETSIGTYPE (*sig_type) OF((int)); | |||
629 | #ifndef S_IWUSR | 630 | #ifndef S_IWUSR |
630 | # define S_IWUSR 0200 | 631 | # define S_IWUSR 0200 |
631 | #endif | 632 | #endif |
632 | #define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */ | 633 | #define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */ |
633 | 634 | ||
634 | #ifndef MAX_PATH_LEN /* max pathname length */ | 635 | #ifndef MAX_PATH_LEN /* max pathname length */ |
635 | # ifdef PATH_MAX | 636 | # ifdef PATH_MAX |
636 | # define MAX_PATH_LEN PATH_MAX | 637 | # define MAX_PATH_LEN PATH_MAX |
637 | # else | 638 | # else |
@@ -644,224 +645,224 @@ typedef RETSIGTYPE (*sig_type) OF((int)); | |||
644 | #endif | 645 | #endif |
645 | 646 | ||
646 | #ifdef NO_OFF_T | 647 | #ifdef NO_OFF_T |
647 | typedef long off_t; | 648 | typedef long off_t; |
648 | off_t lseek OF((int fd, off_t offset, int whence)); | 649 | off_t lseek OF((int fd, off_t offset, int whence)); |
649 | #endif | 650 | #endif |
650 | 651 | ||
651 | 652 | ||
652 | /* global buffers */ | 653 | /* global buffers */ |
653 | 654 | ||
654 | DECLARE(uch, inbuf, INBUFSIZ +INBUF_EXTRA); | 655 | DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA); |
655 | DECLARE(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA); | 656 | DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); |
656 | DECLARE(ush, d_buf, DIST_BUFSIZE); | 657 | DECLARE(ush, d_buf, DIST_BUFSIZE); |
657 | DECLARE(uch, window, 2L*WSIZE); | 658 | DECLARE(uch, window, 2L * WSIZE); |
658 | #ifndef MAXSEG_64K | 659 | #ifndef MAXSEG_64K |
659 | DECLARE(ush, tab_prefix, 1L<<BITS); | 660 | DECLARE(ush, tab_prefix, 1L << BITS); |
660 | #else | 661 | #else |
661 | DECLARE(ush, tab_prefix0, 1L<<(BITS-1)); | 662 | DECLARE(ush, tab_prefix0, 1L << (BITS - 1)); |
662 | DECLARE(ush, tab_prefix1, 1L<<(BITS-1)); | 663 | DECLARE(ush, tab_prefix1, 1L << (BITS - 1)); |
663 | #endif | 664 | #endif |
664 | 665 | ||
665 | /* local variables */ | 666 | /* local variables */ |
666 | 667 | ||
667 | int test_mode = 0; /* check file integrity option */ | 668 | int test_mode = 0; /* check file integrity option */ |
668 | int foreground; /* set if program run in foreground */ | 669 | int foreground; /* set if program run in foreground */ |
669 | int maxbits = BITS; /* max bits per code for LZW */ | 670 | int maxbits = BITS; /* max bits per code for LZW */ |
670 | int method = DEFLATED;/* compression method */ | 671 | int method = DEFLATED; /* compression method */ |
671 | int exit_code = OK; /* program exit code */ | 672 | int exit_code = OK; /* program exit code */ |
672 | int last_member; /* set for .zip and .Z files */ | 673 | int last_member; /* set for .zip and .Z files */ |
673 | int part_nb; /* number of parts in .gz file */ | 674 | int part_nb; /* number of parts in .gz file */ |
674 | long ifile_size; /* input file size, -1 for devices (debug only) */ | 675 | long ifile_size; /* input file size, -1 for devices (debug only) */ |
675 | 676 | ||
676 | long bytes_in; /* number of input bytes */ | 677 | long bytes_in; /* number of input bytes */ |
677 | long bytes_out; /* number of output bytes */ | 678 | long bytes_out; /* number of output bytes */ |
678 | long total_in = 0; /* input bytes for all files */ | 679 | long total_in = 0; /* input bytes for all files */ |
679 | long total_out = 0; /* output bytes for all files */ | 680 | long total_out = 0; /* output bytes for all files */ |
680 | struct stat istat; /* status for input file */ | 681 | struct stat istat; /* status for input file */ |
681 | int ifd; /* input file descriptor */ | 682 | int ifd; /* input file descriptor */ |
682 | int ofd; /* output file descriptor */ | 683 | int ofd; /* output file descriptor */ |
683 | unsigned insize; /* valid bytes in inbuf */ | 684 | unsigned insize; /* valid bytes in inbuf */ |
684 | unsigned inptr; /* index of next byte to be processed in inbuf */ | 685 | unsigned inptr; /* index of next byte to be processed in inbuf */ |
685 | unsigned outcnt; /* bytes in output buffer */ | 686 | unsigned outcnt; /* bytes in output buffer */ |
686 | 687 | ||
687 | long header_bytes; /* number of bytes in gzip header */ | 688 | long header_bytes; /* number of bytes in gzip header */ |
688 | 689 | ||
689 | /* local functions */ | 690 | /* local functions */ |
690 | 691 | ||
691 | local int get_method OF((int in)); | 692 | local int get_method OF((int in)); |
692 | 693 | ||
693 | #define strequ(s1, s2) (strcmp((s1),(s2)) == 0) | 694 | #define strequ(s1, s2) (strcmp((s1),(s2)) == 0) |
694 | 695 | ||
695 | /* ======================================================================== */ | 696 | /* ======================================================================== */ |
696 | int gunzip_main (int argc, char** argv) | 697 | int gunzip_main(int argc, char **argv) |
697 | { | 698 | { |
698 | int file_count; /* number of files to precess */ | 699 | int file_count; /* number of files to precess */ |
699 | int to_stdout = 0; | 700 | int to_stdout = 0; |
700 | int fromstdin = 0; | 701 | int fromstdin = 0; |
701 | int result; | 702 | int result; |
702 | int inFileNum; | 703 | int inFileNum; |
703 | int outFileNum; | 704 | int outFileNum; |
704 | int delInputFile=0; | 705 | int delInputFile = 0; |
705 | struct stat statBuf; | 706 | struct stat statBuf; |
706 | char* delFileName; | 707 | char *delFileName; |
707 | char ifname[MAX_PATH_LEN + 1]; /* input file name */ | 708 | char ifname[MAX_PATH_LEN + 1]; /* input file name */ |
708 | char ofname[MAX_PATH_LEN + 1]; /* output file name */ | 709 | char ofname[MAX_PATH_LEN + 1]; /* output file name */ |
709 | 710 | ||
710 | if (argc==1) | 711 | if (argc == 1) |
711 | usage(gunzip_usage); | 712 | usage(gunzip_usage); |
712 | 713 | ||
713 | if (strcmp(*argv, "zcat")==0) | 714 | if (strcmp(*argv, "zcat") == 0) |
714 | to_stdout = 1; | ||
715 | |||
716 | /* Parse any options */ | ||
717 | while (--argc > 0 && **(++argv) == '-') { | ||
718 | if (*((*argv)+1) == '\0') { | ||
719 | fromstdin = 1; | ||
720 | to_stdout = 1; | ||
721 | } | ||
722 | while (*(++(*argv))) { | ||
723 | switch (**argv) { | ||
724 | case 'c': | ||
725 | to_stdout = 1; | 715 | to_stdout = 1; |
726 | break; | ||
727 | case 't': | ||
728 | test_mode = 1; | ||
729 | break; | ||
730 | 716 | ||
731 | default: | 717 | /* Parse any options */ |
732 | usage(gunzip_usage); | 718 | while (--argc > 0 && **(++argv) == '-') { |
733 | } | 719 | if (*((*argv) + 1) == '\0') { |
720 | fromstdin = 1; | ||
721 | to_stdout = 1; | ||
722 | } | ||
723 | while (*(++(*argv))) { | ||
724 | switch (**argv) { | ||
725 | case 'c': | ||
726 | to_stdout = 1; | ||
727 | break; | ||
728 | case 't': | ||
729 | test_mode = 1; | ||
730 | break; | ||
731 | |||
732 | default: | ||
733 | usage(gunzip_usage); | ||
734 | } | ||
735 | } | ||
734 | } | 736 | } |
735 | } | ||
736 | 737 | ||
737 | foreground = signal(SIGINT, SIG_IGN) != SIG_IGN; | 738 | foreground = signal(SIGINT, SIG_IGN) != SIG_IGN; |
738 | if (foreground) { | 739 | if (foreground) { |
739 | (void) signal (SIGINT, (sig_type)abort_gzip); | 740 | (void) signal(SIGINT, (sig_type) abort_gzip); |
740 | } | 741 | } |
741 | #ifdef SIGTERM | 742 | #ifdef SIGTERM |
742 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { | 743 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { |
743 | (void) signal(SIGTERM, (sig_type)abort_gzip); | 744 | (void) signal(SIGTERM, (sig_type) abort_gzip); |
744 | } | 745 | } |
745 | #endif | 746 | #endif |
746 | #ifdef SIGHUP | 747 | #ifdef SIGHUP |
747 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { | 748 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { |
748 | (void) signal(SIGHUP, (sig_type)abort_gzip); | 749 | (void) signal(SIGHUP, (sig_type) abort_gzip); |
749 | } | 750 | } |
750 | #endif | 751 | #endif |
751 | 752 | ||
752 | file_count = argc - optind; | 753 | file_count = argc - optind; |
753 | 754 | ||
754 | /* Allocate all global buffers (for DYN_ALLOC option) */ | 755 | /* Allocate all global buffers (for DYN_ALLOC option) */ |
755 | ALLOC(uch, inbuf, INBUFSIZ +INBUF_EXTRA); | 756 | ALLOC(uch, inbuf, INBUFSIZ + INBUF_EXTRA); |
756 | ALLOC(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA); | 757 | ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); |
757 | ALLOC(ush, d_buf, DIST_BUFSIZE); | 758 | ALLOC(ush, d_buf, DIST_BUFSIZE); |
758 | ALLOC(uch, window, 2L*WSIZE); | 759 | ALLOC(uch, window, 2L * WSIZE); |
759 | #ifndef MAXSEG_64K | 760 | #ifndef MAXSEG_64K |
760 | ALLOC(ush, tab_prefix, 1L<<BITS); | 761 | ALLOC(ush, tab_prefix, 1L << BITS); |
761 | #else | 762 | #else |
762 | ALLOC(ush, tab_prefix0, 1L<<(BITS-1)); | 763 | ALLOC(ush, tab_prefix0, 1L << (BITS - 1)); |
763 | ALLOC(ush, tab_prefix1, 1L<<(BITS-1)); | 764 | ALLOC(ush, tab_prefix1, 1L << (BITS - 1)); |
764 | #endif | 765 | #endif |
765 | 766 | ||
766 | if (fromstdin==1) { | 767 | if (fromstdin == 1) { |
767 | strcpy(ofname, "stdin"); | 768 | strcpy(ofname, "stdin"); |
768 | |||
769 | inFileNum=fileno(stdin); | ||
770 | ifile_size = -1L; /* convention for unknown size */ | ||
771 | } else { | ||
772 | /* Open up the input file */ | ||
773 | if (*argv=='\0') | ||
774 | usage(gunzip_usage); | ||
775 | if (strlen(*argv) > MAX_PATH_LEN) { | ||
776 | fprintf(stderr, name_too_long, "gunzip"); | ||
777 | do_exit(WARNING); | ||
778 | } | ||
779 | strcpy(ifname, *argv); | ||
780 | 769 | ||
781 | /* Open input fille */ | 770 | inFileNum = fileno(stdin); |
782 | inFileNum=open( ifname, O_RDONLY); | 771 | ifile_size = -1L; /* convention for unknown size */ |
783 | if (inFileNum < 0) { | ||
784 | perror(ifname); | ||
785 | do_exit(WARNING); | ||
786 | } | ||
787 | /* Get the time stamp on the input file. */ | ||
788 | result = stat(ifname, &statBuf); | ||
789 | if (result < 0) { | ||
790 | perror(ifname); | ||
791 | do_exit(WARNING); | ||
792 | } | ||
793 | ifile_size = statBuf.st_size; | ||
794 | } | ||
795 | |||
796 | if (to_stdout==1) { | ||
797 | /* And get to work */ | ||
798 | strcpy(ofname, "stdout"); | ||
799 | outFileNum=fileno(stdout); | ||
800 | |||
801 | clear_bufs(); /* clear input and output buffers */ | ||
802 | part_nb = 0; | ||
803 | |||
804 | /* Actually do the compression/decompression. */ | ||
805 | unzip(inFileNum, outFileNum); | ||
806 | |||
807 | } else if (test_mode) { | ||
808 | /* Actually do the compression/decompression. */ | ||
809 | unzip(inFileNum, 2); | ||
810 | } else { | ||
811 | char* pos; | ||
812 | |||
813 | /* And get to work */ | ||
814 | if (strlen(ifname) > MAX_PATH_LEN - 4) { | ||
815 | fprintf(stderr, name_too_long, "gunzip"); | ||
816 | do_exit(WARNING); | ||
817 | } | ||
818 | strcpy(ofname, ifname); | ||
819 | pos=strstr(ofname, ".gz"); | ||
820 | if (pos != NULL) { | ||
821 | *pos='\0'; | ||
822 | delInputFile=1; | ||
823 | } else { | 772 | } else { |
824 | pos=strstr(ofname, ".tgz"); | 773 | /* Open up the input file */ |
825 | if (pos != NULL) { | 774 | if (*argv == '\0') |
826 | *pos='\0'; | 775 | usage(gunzip_usage); |
827 | strcat( pos, ".tar"); | 776 | if (strlen(*argv) > MAX_PATH_LEN) { |
828 | delInputFile=1; | 777 | fprintf(stderr, name_too_long, "gunzip"); |
829 | } | 778 | do_exit(WARNING); |
779 | } | ||
780 | strcpy(ifname, *argv); | ||
781 | |||
782 | /* Open input fille */ | ||
783 | inFileNum = open(ifname, O_RDONLY); | ||
784 | if (inFileNum < 0) { | ||
785 | perror(ifname); | ||
786 | do_exit(WARNING); | ||
787 | } | ||
788 | /* Get the time stamp on the input file. */ | ||
789 | result = stat(ifname, &statBuf); | ||
790 | if (result < 0) { | ||
791 | perror(ifname); | ||
792 | do_exit(WARNING); | ||
793 | } | ||
794 | ifile_size = statBuf.st_size; | ||
830 | } | 795 | } |
831 | 796 | ||
832 | /* Open output fille */ | 797 | if (to_stdout == 1) { |
798 | /* And get to work */ | ||
799 | strcpy(ofname, "stdout"); | ||
800 | outFileNum = fileno(stdout); | ||
801 | |||
802 | clear_bufs(); /* clear input and output buffers */ | ||
803 | part_nb = 0; | ||
804 | |||
805 | /* Actually do the compression/decompression. */ | ||
806 | unzip(inFileNum, outFileNum); | ||
807 | |||
808 | } else if (test_mode) { | ||
809 | /* Actually do the compression/decompression. */ | ||
810 | unzip(inFileNum, 2); | ||
811 | } else { | ||
812 | char *pos; | ||
813 | |||
814 | /* And get to work */ | ||
815 | if (strlen(ifname) > MAX_PATH_LEN - 4) { | ||
816 | fprintf(stderr, name_too_long, "gunzip"); | ||
817 | do_exit(WARNING); | ||
818 | } | ||
819 | strcpy(ofname, ifname); | ||
820 | pos = strstr(ofname, ".gz"); | ||
821 | if (pos != NULL) { | ||
822 | *pos = '\0'; | ||
823 | delInputFile = 1; | ||
824 | } else { | ||
825 | pos = strstr(ofname, ".tgz"); | ||
826 | if (pos != NULL) { | ||
827 | *pos = '\0'; | ||
828 | strcat(pos, ".tar"); | ||
829 | delInputFile = 1; | ||
830 | } | ||
831 | } | ||
832 | |||
833 | /* Open output fille */ | ||
833 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) | 834 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
834 | outFileNum=open( ofname, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW); | 835 | outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW); |
835 | #else | 836 | #else |
836 | outFileNum=open( ofname, O_RDWR|O_CREAT|O_EXCL); | 837 | outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL); |
837 | #endif | 838 | #endif |
838 | if (outFileNum < 0) { | 839 | if (outFileNum < 0) { |
839 | perror(ofname); | 840 | perror(ofname); |
840 | do_exit(WARNING); | 841 | do_exit(WARNING); |
841 | } | 842 | } |
842 | /* Set permissions on the file */ | 843 | /* Set permissions on the file */ |
843 | fchmod(outFileNum, statBuf.st_mode); | 844 | fchmod(outFileNum, statBuf.st_mode); |
844 | 845 | ||
845 | clear_bufs(); /* clear input and output buffers */ | 846 | clear_bufs(); /* clear input and output buffers */ |
846 | part_nb = 0; | 847 | part_nb = 0; |
847 | 848 | ||
848 | /* Actually do the compression/decompression. */ | 849 | /* Actually do the compression/decompression. */ |
849 | result=unzip(inFileNum, outFileNum); | 850 | result = unzip(inFileNum, outFileNum); |
850 | 851 | ||
851 | close( outFileNum); | 852 | close(outFileNum); |
852 | close( inFileNum); | 853 | close(inFileNum); |
853 | /* Delete the original file */ | 854 | /* Delete the original file */ |
854 | if (result == OK) | 855 | if (result == OK) |
855 | delFileName=ifname; | 856 | delFileName = ifname; |
856 | else | 857 | else |
857 | delFileName=ofname; | 858 | delFileName = ofname; |
858 | 859 | ||
859 | if (delInputFile == 1 && unlink (delFileName) < 0) { | 860 | if (delInputFile == 1 && unlink(delFileName) < 0) { |
860 | perror (delFileName); | 861 | perror(delFileName); |
861 | exit( FALSE); | 862 | exit(FALSE); |
863 | } | ||
862 | } | 864 | } |
863 | } | 865 | do_exit(exit_code); |
864 | do_exit(exit_code); | ||
865 | } | 866 | } |
866 | 867 | ||
867 | 868 | ||
@@ -877,71 +878,76 @@ int gunzip_main (int argc, char** argv) | |||
877 | * If the member is a zip file, it must be the only one. | 878 | * If the member is a zip file, it must be the only one. |
878 | */ | 879 | */ |
879 | local int get_method(in) | 880 | local int get_method(in) |
880 | int in; /* input file descriptor */ | 881 | int in; /* input file descriptor */ |
881 | { | 882 | { |
882 | uch flags; /* compression flags */ | 883 | uch flags; /* compression flags */ |
883 | char magic[2]; /* magic header */ | 884 | char magic[2]; /* magic header */ |
884 | 885 | ||
885 | magic[0] = (char)get_byte(); | 886 | magic[0] = (char) get_byte(); |
886 | magic[1] = (char)get_byte(); | 887 | magic[1] = (char) get_byte(); |
887 | method = -1; /* unknown yet */ | 888 | method = -1; /* unknown yet */ |
888 | part_nb++; /* number of parts in gzip file */ | 889 | part_nb++; /* number of parts in gzip file */ |
889 | header_bytes = 0; | 890 | header_bytes = 0; |
890 | last_member = RECORD_IO; | 891 | last_member = RECORD_IO; |
891 | /* assume multiple members in gzip file except for record oriented I/O */ | 892 | /* assume multiple members in gzip file except for record oriented I/O */ |
892 | 893 | ||
893 | if (memcmp(magic, GZIP_MAGIC, 2) == 0) { | 894 | if (memcmp(magic, GZIP_MAGIC, 2) == 0) { |
894 | 895 | ||
895 | method = (int)get_byte(); | 896 | method = (int) get_byte(); |
896 | if (method != DEFLATED) { | 897 | if (method != DEFLATED) { |
897 | fprintf(stderr, | 898 | fprintf(stderr, |
898 | "unknown method %d -- get newer version of gzip\n", | 899 | "unknown method %d -- get newer version of gzip\n", |
899 | method); | 900 | method); |
900 | exit_code = ERROR; | 901 | exit_code = ERROR; |
901 | return -1; | 902 | return -1; |
902 | } | 903 | } |
903 | flags = (uch)get_byte(); | 904 | flags = (uch) get_byte(); |
904 | 905 | ||
905 | (ulg)get_byte(); /* Ignore time stamp */ | 906 | (ulg) get_byte(); /* Ignore time stamp */ |
906 | (ulg)get_byte(); | 907 | (ulg) get_byte(); |
907 | (ulg)get_byte(); | 908 | (ulg) get_byte(); |
908 | (ulg)get_byte(); | 909 | (ulg) get_byte(); |
910 | |||
911 | (void) get_byte(); /* Ignore extra flags for the moment */ | ||
912 | (void) get_byte(); /* Ignore OS type for the moment */ | ||
913 | |||
914 | if ((flags & EXTRA_FIELD) != 0) { | ||
915 | unsigned len = (unsigned) get_byte(); | ||
916 | |||
917 | len |= ((unsigned) get_byte()) << 8; | ||
918 | |||
919 | while (len--) | ||
920 | (void) get_byte(); | ||
921 | } | ||
922 | |||
923 | /* Discard original name if any */ | ||
924 | if ((flags & ORIG_NAME) != 0) { | ||
925 | while (get_char() != 0) /* null */ | ||
926 | ; | ||
927 | } | ||
928 | |||
929 | /* Discard file comment if any */ | ||
930 | if ((flags & COMMENT) != 0) { | ||
931 | while (get_char() != 0) /* null */ | ||
932 | ; | ||
933 | } | ||
934 | if (part_nb == 1) { | ||
935 | header_bytes = inptr + 2 * sizeof(long); /* include crc and size */ | ||
936 | } | ||
909 | 937 | ||
910 | (void)get_byte(); /* Ignore extra flags for the moment */ | ||
911 | (void)get_byte(); /* Ignore OS type for the moment */ | ||
912 | |||
913 | if ((flags & EXTRA_FIELD) != 0) { | ||
914 | unsigned len = (unsigned)get_byte(); | ||
915 | len |= ((unsigned)get_byte())<<8; | ||
916 | |||
917 | while (len--) (void)get_byte(); | ||
918 | } | 938 | } |
919 | 939 | ||
920 | /* Discard original name if any */ | 940 | if (method >= 0) |
921 | if ((flags & ORIG_NAME) != 0) { | 941 | return method; |
922 | while (get_char() != 0) /* null */ ; | ||
923 | } | ||
924 | 942 | ||
925 | /* Discard file comment if any */ | ||
926 | if ((flags & COMMENT) != 0) { | ||
927 | while (get_char() != 0) /* null */ ; | ||
928 | } | ||
929 | if (part_nb == 1) { | 943 | if (part_nb == 1) { |
930 | header_bytes = inptr + 2*sizeof(long); /* include crc and size */ | 944 | fprintf(stderr, "\nnot in gzip format\n"); |
945 | exit_code = ERROR; | ||
946 | return -1; | ||
947 | } else { | ||
948 | WARN((stderr, "\ndecompression OK, trailing garbage ignored\n")); | ||
949 | return -2; | ||
931 | } | 950 | } |
932 | |||
933 | } | ||
934 | |||
935 | if (method >= 0) return method; | ||
936 | |||
937 | if (part_nb == 1) { | ||
938 | fprintf(stderr, "\nnot in gzip format\n"); | ||
939 | exit_code = ERROR; | ||
940 | return -1; | ||
941 | } else { | ||
942 | WARN((stderr, "\ndecompression OK, trailing garbage ignored\n")); | ||
943 | return -2; | ||
944 | } | ||
945 | } | 951 | } |
946 | 952 | ||
947 | /* ======================================================================== | 953 | /* ======================================================================== |
@@ -949,8 +955,9 @@ local int get_method(in) | |||
949 | */ | 955 | */ |
950 | RETSIGTYPE abort_gzip() | 956 | RETSIGTYPE abort_gzip() |
951 | { | 957 | { |
952 | do_exit(ERROR); | 958 | do_exit(ERROR); |
953 | } | 959 | } |
960 | |||
954 | /* unzip.c -- decompress files in gzip or pkzip format. | 961 | /* unzip.c -- decompress files in gzip or pkzip format. |
955 | * Copyright (C) 1992-1993 Jean-loup Gailly | 962 | * Copyright (C) 1992-1993 Jean-loup Gailly |
956 | * This is free software; you can redistribute it and/or modify it under the | 963 | * This is free software; you can redistribute it and/or modify it under the |
@@ -973,35 +980,35 @@ RETSIGTYPE abort_gzip() | |||
973 | */ | 980 | */ |
974 | 981 | ||
975 | #ifdef CRYPT | 982 | #ifdef CRYPT |
976 | # undef CRYPT /* dummy version */ | 983 | # undef CRYPT /* dummy version */ |
977 | #endif | 984 | #endif |
978 | 985 | ||
979 | #define RAND_HEAD_LEN 12 /* length of encryption random header */ | 986 | #define RAND_HEAD_LEN 12 /* length of encryption random header */ |
980 | 987 | ||
981 | #define zencode | 988 | #define zencode |
982 | #define zdecode | 989 | #define zdecode |
983 | 990 | ||
984 | /* PKZIP header definitions */ | 991 | /* PKZIP header definitions */ |
985 | #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */ | 992 | #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */ |
986 | #define LOCFLG 6 /* offset of bit flag */ | 993 | #define LOCFLG 6 /* offset of bit flag */ |
987 | #define CRPFLG 1 /* bit for encrypted entry */ | 994 | #define CRPFLG 1 /* bit for encrypted entry */ |
988 | #define EXTFLG 8 /* bit for extended local header */ | 995 | #define EXTFLG 8 /* bit for extended local header */ |
989 | #define LOCHOW 8 /* offset of compression method */ | 996 | #define LOCHOW 8 /* offset of compression method */ |
990 | #define LOCTIM 10 /* file mod time (for decryption) */ | 997 | #define LOCTIM 10 /* file mod time (for decryption) */ |
991 | #define LOCCRC 14 /* offset of crc */ | 998 | #define LOCCRC 14 /* offset of crc */ |
992 | #define LOCSIZ 18 /* offset of compressed size */ | 999 | #define LOCSIZ 18 /* offset of compressed size */ |
993 | #define LOCLEN 22 /* offset of uncompressed length */ | 1000 | #define LOCLEN 22 /* offset of uncompressed length */ |
994 | #define LOCFIL 26 /* offset of file name field length */ | 1001 | #define LOCFIL 26 /* offset of file name field length */ |
995 | #define LOCEXT 28 /* offset of extra field length */ | 1002 | #define LOCEXT 28 /* offset of extra field length */ |
996 | #define LOCHDR 30 /* size of local header, including sig */ | 1003 | #define LOCHDR 30 /* size of local header, including sig */ |
997 | #define EXTHDR 16 /* size of extended local header, inc sig */ | 1004 | #define EXTHDR 16 /* size of extended local header, inc sig */ |
998 | 1005 | ||
999 | 1006 | ||
1000 | /* Globals */ | 1007 | /* Globals */ |
1001 | 1008 | ||
1002 | char *key; /* not used--needed to link crypt.c */ | 1009 | char *key; /* not used--needed to link crypt.c */ |
1003 | int pkzip = 0; /* set for a pkzip file */ | 1010 | int pkzip = 0; /* set for a pkzip file */ |
1004 | int ext_header = 0; /* set if extended local header */ | 1011 | int ext_header = 0; /* set if extended local header */ |
1005 | 1012 | ||
1006 | /* =========================================================================== | 1013 | /* =========================================================================== |
1007 | * Unzip in to out. This routine works on both gzip and pkzip files. | 1014 | * Unzip in to out. This routine works on both gzip and pkzip files. |
@@ -1011,81 +1018,82 @@ int ext_header = 0; /* set if extended local header */ | |||
1011 | * The magic header has already been checked. The output buffer is cleared. | 1018 | * The magic header has already been checked. The output buffer is cleared. |
1012 | */ | 1019 | */ |
1013 | int unzip(in, out) | 1020 | int unzip(in, out) |
1014 | int in, out; /* input and output file descriptors */ | 1021 | int in, out; /* input and output file descriptors */ |
1015 | { | 1022 | { |
1016 | ulg orig_crc = 0; /* original crc */ | 1023 | ulg orig_crc = 0; /* original crc */ |
1017 | ulg orig_len = 0; /* original uncompressed length */ | 1024 | ulg orig_len = 0; /* original uncompressed length */ |
1018 | int n; | 1025 | int n; |
1019 | uch buf[EXTHDR]; /* extended local header */ | 1026 | uch buf[EXTHDR]; /* extended local header */ |
1020 | 1027 | ||
1021 | ifd = in; | 1028 | ifd = in; |
1022 | ofd = out; | 1029 | ofd = out; |
1023 | method = get_method(ifd); | 1030 | method = get_method(ifd); |
1024 | if (method < 0) { | 1031 | if (method < 0) { |
1025 | do_exit(exit_code); /* error message already emitted */ | 1032 | do_exit(exit_code); /* error message already emitted */ |
1026 | } | ||
1027 | |||
1028 | updcrc(NULL, 0); /* initialize crc */ | ||
1029 | |||
1030 | if (pkzip && !ext_header) { /* crc and length at the end otherwise */ | ||
1031 | orig_crc = LG(inbuf + LOCCRC); | ||
1032 | orig_len = LG(inbuf + LOCLEN); | ||
1033 | } | ||
1034 | |||
1035 | /* Decompress */ | ||
1036 | if (method == DEFLATED) { | ||
1037 | |||
1038 | int res = inflate(); | ||
1039 | |||
1040 | if (res == 3) { | ||
1041 | error("out of memory"); | ||
1042 | } else if (res != 0) { | ||
1043 | error("invalid compressed data--format violated"); | ||
1044 | } | 1033 | } |
1045 | 1034 | ||
1046 | } else { | 1035 | updcrc(NULL, 0); /* initialize crc */ |
1047 | error("internal error, invalid method"); | 1036 | |
1048 | } | 1037 | if (pkzip && !ext_header) { /* crc and length at the end otherwise */ |
1049 | 1038 | orig_crc = LG(inbuf + LOCCRC); | |
1050 | /* Get the crc and original length */ | 1039 | orig_len = LG(inbuf + LOCLEN); |
1051 | if (!pkzip) { | ||
1052 | /* crc32 (see algorithm.doc) | ||
1053 | * uncompressed input size modulo 2^32 | ||
1054 | */ | ||
1055 | for (n = 0; n < 8; n++) { | ||
1056 | buf[n] = (uch)get_byte(); /* may cause an error if EOF */ | ||
1057 | } | 1040 | } |
1058 | orig_crc = LG(buf); | 1041 | |
1059 | orig_len = LG(buf+4); | 1042 | /* Decompress */ |
1060 | 1043 | if (method == DEFLATED) { | |
1061 | } else if (ext_header) { /* If extended header, check it */ | 1044 | |
1062 | /* signature - 4bytes: 0x50 0x4b 0x07 0x08 | 1045 | int res = inflate(); |
1063 | * CRC-32 value | 1046 | |
1064 | * compressed size 4-bytes | 1047 | if (res == 3) { |
1065 | * uncompressed size 4-bytes | 1048 | error("out of memory"); |
1066 | */ | 1049 | } else if (res != 0) { |
1067 | for (n = 0; n < EXTHDR; n++) { | 1050 | error("invalid compressed data--format violated"); |
1068 | buf[n] = (uch)get_byte(); /* may cause an error if EOF */ | 1051 | } |
1052 | |||
1053 | } else { | ||
1054 | error("internal error, invalid method"); | ||
1055 | } | ||
1056 | |||
1057 | /* Get the crc and original length */ | ||
1058 | if (!pkzip) { | ||
1059 | /* crc32 (see algorithm.doc) | ||
1060 | * uncompressed input size modulo 2^32 | ||
1061 | */ | ||
1062 | for (n = 0; n < 8; n++) { | ||
1063 | buf[n] = (uch) get_byte(); /* may cause an error if EOF */ | ||
1064 | } | ||
1065 | orig_crc = LG(buf); | ||
1066 | orig_len = LG(buf + 4); | ||
1067 | |||
1068 | } else if (ext_header) { /* If extended header, check it */ | ||
1069 | /* signature - 4bytes: 0x50 0x4b 0x07 0x08 | ||
1070 | * CRC-32 value | ||
1071 | * compressed size 4-bytes | ||
1072 | * uncompressed size 4-bytes | ||
1073 | */ | ||
1074 | for (n = 0; n < EXTHDR; n++) { | ||
1075 | buf[n] = (uch) get_byte(); /* may cause an error if EOF */ | ||
1076 | } | ||
1077 | orig_crc = LG(buf + 4); | ||
1078 | orig_len = LG(buf + 12); | ||
1079 | } | ||
1080 | |||
1081 | /* Validate decompression */ | ||
1082 | if (orig_crc != updcrc(outbuf, 0)) { | ||
1083 | error("invalid compressed data--crc error"); | ||
1069 | } | 1084 | } |
1070 | orig_crc = LG(buf+4); | 1085 | if (orig_len != (ulg) bytes_out) { |
1071 | orig_len = LG(buf+12); | 1086 | error("invalid compressed data--length error"); |
1072 | } | 1087 | } |
1073 | 1088 | ||
1074 | /* Validate decompression */ | 1089 | /* Check if there are more entries in a pkzip file */ |
1075 | if (orig_crc != updcrc(outbuf, 0)) { | 1090 | if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) { |
1076 | error("invalid compressed data--crc error"); | 1091 | WARN((stderr, "has more than one entry--rest ignored\n")); |
1077 | } | 1092 | } |
1078 | if (orig_len != (ulg)bytes_out) { | 1093 | ext_header = pkzip = 0; /* for next file */ |
1079 | error("invalid compressed data--length error"); | 1094 | return OK; |
1080 | } | ||
1081 | |||
1082 | /* Check if there are more entries in a pkzip file */ | ||
1083 | if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) { | ||
1084 | WARN((stderr,"has more than one entry--rest ignored\n")); | ||
1085 | } | ||
1086 | ext_header = pkzip = 0; /* for next file */ | ||
1087 | return OK; | ||
1088 | } | 1095 | } |
1096 | |||
1089 | /* util.c -- utility functions for gzip support | 1097 | /* util.c -- utility functions for gzip support |
1090 | * Copyright (C) 1992-1993 Jean-loup Gailly | 1098 | * Copyright (C) 1992-1993 Jean-loup Gailly |
1091 | * This is free software; you can redistribute it and/or modify it under the | 1099 | * This is free software; you can redistribute it and/or modify it under the |
@@ -1106,10 +1114,10 @@ int unzip(in, out) | |||
1106 | #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H) | 1114 | #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H) |
1107 | # include <stdlib.h> | 1115 | # include <stdlib.h> |
1108 | #else | 1116 | #else |
1109 | extern int errno; | 1117 | extern int errno; |
1110 | #endif | 1118 | #endif |
1111 | 1119 | ||
1112 | static const ulg crc_32_tab[]; /* crc table, defined below */ | 1120 | static const ulg crc_32_tab[]; /* crc table, defined below */ |
1113 | 1121 | ||
1114 | /* =========================================================================== | 1122 | /* =========================================================================== |
1115 | * Run a set of bytes through the crc shift register. If s is a NULL | 1123 | * Run a set of bytes through the crc shift register. If s is a NULL |
@@ -1117,23 +1125,24 @@ static const ulg crc_32_tab[]; /* crc table, defined below */ | |||
1117 | * Return the current crc in either case. | 1125 | * Return the current crc in either case. |
1118 | */ | 1126 | */ |
1119 | ulg updcrc(s, n) | 1127 | ulg updcrc(s, n) |
1120 | uch *s; /* pointer to bytes to pump through */ | 1128 | uch *s; /* pointer to bytes to pump through */ |
1121 | unsigned n; /* number of bytes in s[] */ | 1129 | unsigned n; /* number of bytes in s[] */ |
1122 | { | 1130 | { |
1123 | register ulg c; /* temporary variable */ | 1131 | register ulg c; /* temporary variable */ |
1124 | 1132 | ||
1125 | static ulg crc = (ulg)0xffffffffL; /* shift register contents */ | 1133 | static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ |
1126 | 1134 | ||
1127 | if (s == NULL) { | 1135 | if (s == NULL) { |
1128 | c = 0xffffffffL; | 1136 | c = 0xffffffffL; |
1129 | } else { | 1137 | } else { |
1130 | c = crc; | 1138 | c = crc; |
1131 | if (n) do { | 1139 | if (n) |
1132 | c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8); | 1140 | do { |
1133 | } while (--n); | 1141 | c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8); |
1134 | } | 1142 | } while (--n); |
1135 | crc = c; | 1143 | } |
1136 | return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ | 1144 | crc = c; |
1145 | return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ | ||
1137 | } | 1146 | } |
1138 | 1147 | ||
1139 | /* =========================================================================== | 1148 | /* =========================================================================== |
@@ -1141,35 +1150,37 @@ ulg updcrc(s, n) | |||
1141 | */ | 1150 | */ |
1142 | void clear_bufs() | 1151 | void clear_bufs() |
1143 | { | 1152 | { |
1144 | outcnt = 0; | 1153 | outcnt = 0; |
1145 | insize = inptr = 0; | 1154 | insize = inptr = 0; |
1146 | bytes_in = bytes_out = 0L; | 1155 | bytes_in = bytes_out = 0L; |
1147 | } | 1156 | } |
1148 | 1157 | ||
1149 | /* =========================================================================== | 1158 | /* =========================================================================== |
1150 | * Fill the input buffer. This is called only when the buffer is empty. | 1159 | * Fill the input buffer. This is called only when the buffer is empty. |
1151 | */ | 1160 | */ |
1152 | int fill_inbuf(eof_ok) | 1161 | int fill_inbuf(eof_ok) |
1153 | int eof_ok; /* set if EOF acceptable as a result */ | 1162 | int eof_ok; /* set if EOF acceptable as a result */ |
1154 | { | 1163 | { |
1155 | int len; | 1164 | int len; |
1156 | 1165 | ||
1157 | /* Read as much as possible */ | 1166 | /* Read as much as possible */ |
1158 | insize = 0; | 1167 | insize = 0; |
1159 | errno = 0; | 1168 | errno = 0; |
1160 | do { | 1169 | do { |
1161 | len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize); | 1170 | len = read(ifd, (char *) inbuf + insize, INBUFSIZ - insize); |
1162 | if (len == 0 || len == EOF) break; | 1171 | if (len == 0 || len == EOF) |
1163 | insize += len; | 1172 | break; |
1164 | } while (insize < INBUFSIZ); | 1173 | insize += len; |
1165 | 1174 | } while (insize < INBUFSIZ); | |
1166 | if (insize == 0) { | 1175 | |
1167 | if (eof_ok) return EOF; | 1176 | if (insize == 0) { |
1168 | read_error(); | 1177 | if (eof_ok) |
1169 | } | 1178 | return EOF; |
1170 | bytes_in += (ulg)insize; | 1179 | read_error(); |
1171 | inptr = 1; | 1180 | } |
1172 | return inbuf[0]; | 1181 | bytes_in += (ulg) insize; |
1182 | inptr = 1; | ||
1183 | return inbuf[0]; | ||
1173 | } | 1184 | } |
1174 | 1185 | ||
1175 | /* =========================================================================== | 1186 | /* =========================================================================== |
@@ -1178,12 +1189,13 @@ int fill_inbuf(eof_ok) | |||
1178 | */ | 1189 | */ |
1179 | void flush_outbuf() | 1190 | void flush_outbuf() |
1180 | { | 1191 | { |
1181 | if (outcnt == 0) return; | 1192 | if (outcnt == 0) |
1193 | return; | ||
1182 | 1194 | ||
1183 | if (!test_mode) | 1195 | if (!test_mode) |
1184 | write_buf(ofd, (char *)outbuf, outcnt); | 1196 | write_buf(ofd, (char *) outbuf, outcnt); |
1185 | bytes_out += (ulg)outcnt; | 1197 | bytes_out += (ulg) outcnt; |
1186 | outcnt = 0; | 1198 | outcnt = 0; |
1187 | } | 1199 | } |
1188 | 1200 | ||
1189 | /* =========================================================================== | 1201 | /* =========================================================================== |
@@ -1192,13 +1204,14 @@ void flush_outbuf() | |||
1192 | */ | 1204 | */ |
1193 | void flush_window() | 1205 | void flush_window() |
1194 | { | 1206 | { |
1195 | if (outcnt == 0) return; | 1207 | if (outcnt == 0) |
1196 | updcrc(window, outcnt); | 1208 | return; |
1197 | 1209 | updcrc(window, outcnt); | |
1198 | if (!test_mode) | 1210 | |
1199 | write_buf(ofd, (char *)window, outcnt); | 1211 | if (!test_mode) |
1200 | bytes_out += (ulg)outcnt; | 1212 | write_buf(ofd, (char *) window, outcnt); |
1201 | outcnt = 0; | 1213 | bytes_out += (ulg) outcnt; |
1214 | outcnt = 0; | ||
1202 | } | 1215 | } |
1203 | 1216 | ||
1204 | /* =========================================================================== | 1217 | /* =========================================================================== |
@@ -1206,19 +1219,19 @@ void flush_window() | |||
1206 | * for error return. | 1219 | * for error return. |
1207 | */ | 1220 | */ |
1208 | void write_buf(fd, buf, cnt) | 1221 | void write_buf(fd, buf, cnt) |
1209 | int fd; | 1222 | int fd; |
1210 | voidp buf; | 1223 | voidp buf; |
1211 | unsigned cnt; | 1224 | unsigned cnt; |
1212 | { | 1225 | { |
1213 | unsigned n; | 1226 | unsigned n; |
1214 | 1227 | ||
1215 | while ((n = write(fd, buf, cnt)) != cnt) { | 1228 | while ((n = write(fd, buf, cnt)) != cnt) { |
1216 | if (n == (unsigned)(-1)) { | 1229 | if (n == (unsigned) (-1)) { |
1217 | write_error(); | 1230 | write_error(); |
1231 | } | ||
1232 | cnt -= n; | ||
1233 | buf = (voidp) ((char *) buf + n); | ||
1218 | } | 1234 | } |
1219 | cnt -= n; | ||
1220 | buf = (voidp)((char*)buf+n); | ||
1221 | } | ||
1222 | } | 1235 | } |
1223 | 1236 | ||
1224 | #if defined(NO_STRING_H) && !defined(STDC_HEADERS) | 1237 | #if defined(NO_STRING_H) && !defined(STDC_HEADERS) |
@@ -1229,7 +1242,7 @@ void write_buf(fd, buf, cnt) | |||
1229 | # define const | 1242 | # define const |
1230 | # endif | 1243 | # endif |
1231 | 1244 | ||
1232 | int strspn OF((const char *s, const char *accept)); | 1245 | int strspn OF((const char *s, const char *accept)); |
1233 | int strcspn OF((const char *s, const char *reject)); | 1246 | int strcspn OF((const char *s, const char *reject)); |
1234 | 1247 | ||
1235 | /* ======================================================================== | 1248 | /* ======================================================================== |
@@ -1237,21 +1250,23 @@ int strcspn OF((const char *s, const char *reject)); | |||
1237 | * of s which contains only characters in accept. | 1250 | * of s which contains only characters in accept. |
1238 | */ | 1251 | */ |
1239 | int strspn(s, accept) | 1252 | int strspn(s, accept) |
1240 | const char *s; | 1253 | const char *s; |
1241 | const char *accept; | 1254 | const char *accept; |
1242 | { | 1255 | { |
1243 | register const char *p; | 1256 | register const char *p; |
1244 | register const char *a; | 1257 | register const char *a; |
1245 | register int count = 0; | 1258 | register int count = 0; |
1246 | 1259 | ||
1247 | for (p = s; *p != '\0'; ++p) { | 1260 | for (p = s; *p != '\0'; ++p) { |
1248 | for (a = accept; *a != '\0'; ++a) { | 1261 | for (a = accept; *a != '\0'; ++a) { |
1249 | if (*p == *a) break; | 1262 | if (*p == *a) |
1263 | break; | ||
1264 | } | ||
1265 | if (*a == '\0') | ||
1266 | return count; | ||
1267 | ++count; | ||
1250 | } | 1268 | } |
1251 | if (*a == '\0') return count; | 1269 | return count; |
1252 | ++count; | ||
1253 | } | ||
1254 | return count; | ||
1255 | } | 1270 | } |
1256 | 1271 | ||
1257 | /* ======================================================================== | 1272 | /* ======================================================================== |
@@ -1259,46 +1274,47 @@ int strspn(s, accept) | |||
1259 | * which contains no characters from reject. | 1274 | * which contains no characters from reject. |
1260 | */ | 1275 | */ |
1261 | int strcspn(s, reject) | 1276 | int strcspn(s, reject) |
1262 | const char *s; | 1277 | const char *s; |
1263 | const char *reject; | 1278 | const char *reject; |
1264 | { | 1279 | { |
1265 | register int count = 0; | 1280 | register int count = 0; |
1266 | 1281 | ||
1267 | while (*s != '\0') { | 1282 | while (*s != '\0') { |
1268 | if (strchr(reject, *s++) != NULL) return count; | 1283 | if (strchr(reject, *s++) != NULL) |
1269 | ++count; | 1284 | return count; |
1270 | } | 1285 | ++count; |
1271 | return count; | 1286 | } |
1287 | return count; | ||
1272 | } | 1288 | } |
1273 | 1289 | ||
1274 | #endif /* NO_STRING_H */ | 1290 | #endif /* NO_STRING_H */ |
1275 | 1291 | ||
1276 | 1292 | ||
1277 | /* ======================================================================== | 1293 | /* ======================================================================== |
1278 | * Error handlers. | 1294 | * Error handlers. |
1279 | */ | 1295 | */ |
1280 | void warn(a, b) | 1296 | void warn(a, b) |
1281 | char *a, *b; /* message strings juxtaposed in output */ | 1297 | char *a, *b; /* message strings juxtaposed in output */ |
1282 | { | 1298 | { |
1283 | WARN((stderr, "warning: %s%s\n", a, b)); | 1299 | WARN((stderr, "warning: %s%s\n", a, b)); |
1284 | } | 1300 | } |
1285 | 1301 | ||
1286 | void read_error() | 1302 | void read_error() |
1287 | { | 1303 | { |
1288 | fprintf(stderr, "\n"); | 1304 | fprintf(stderr, "\n"); |
1289 | if (errno != 0) { | 1305 | if (errno != 0) { |
1290 | perror(""); | 1306 | perror(""); |
1291 | } else { | 1307 | } else { |
1292 | fprintf(stderr, "unexpected end of file\n"); | 1308 | fprintf(stderr, "unexpected end of file\n"); |
1293 | } | 1309 | } |
1294 | abort_gzip(); | 1310 | abort_gzip(); |
1295 | } | 1311 | } |
1296 | 1312 | ||
1297 | void write_error() | 1313 | void write_error() |
1298 | { | 1314 | { |
1299 | fprintf(stderr, "\n"); | 1315 | fprintf(stderr, "\n"); |
1300 | perror(""); | 1316 | perror(""); |
1301 | abort_gzip(); | 1317 | abort_gzip(); |
1302 | } | 1318 | } |
1303 | 1319 | ||
1304 | 1320 | ||
@@ -1306,59 +1322,60 @@ void write_error() | |||
1306 | * Table of CRC-32's of all single-byte values (made by makecrc.c) | 1322 | * Table of CRC-32's of all single-byte values (made by makecrc.c) |
1307 | */ | 1323 | */ |
1308 | static const ulg crc_32_tab[] = { | 1324 | static const ulg crc_32_tab[] = { |
1309 | 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, | 1325 | 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, |
1310 | 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, | 1326 | 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, |
1311 | 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, | 1327 | 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, |
1312 | 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, | 1328 | 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, |
1313 | 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, | 1329 | 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, |
1314 | 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, | 1330 | 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, |
1315 | 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, | 1331 | 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, |
1316 | 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, | 1332 | 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, |
1317 | 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, | 1333 | 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, |
1318 | 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, | 1334 | 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, |
1319 | 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, | 1335 | 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, |
1320 | 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, | 1336 | 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, |
1321 | 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, | 1337 | 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, |
1322 | 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, | 1338 | 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, |
1323 | 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, | 1339 | 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, |
1324 | 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, | 1340 | 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, |
1325 | 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, | 1341 | 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, |
1326 | 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, | 1342 | 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, |
1327 | 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, | 1343 | 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, |
1328 | 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, | 1344 | 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, |
1329 | 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, | 1345 | 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, |
1330 | 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, | 1346 | 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, |
1331 | 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, | 1347 | 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, |
1332 | 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, | 1348 | 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, |
1333 | 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, | 1349 | 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, |
1334 | 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, | 1350 | 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, |
1335 | 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, | 1351 | 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, |
1336 | 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, | 1352 | 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, |
1337 | 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, | 1353 | 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, |
1338 | 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, | 1354 | 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, |
1339 | 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, | 1355 | 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, |
1340 | 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, | 1356 | 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, |
1341 | 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, | 1357 | 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, |
1342 | 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, | 1358 | 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, |
1343 | 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, | 1359 | 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, |
1344 | 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, | 1360 | 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, |
1345 | 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, | 1361 | 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, |
1346 | 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, | 1362 | 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, |
1347 | 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, | 1363 | 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, |
1348 | 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, | 1364 | 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, |
1349 | 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, | 1365 | 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, |
1350 | 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, | 1366 | 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, |
1351 | 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, | 1367 | 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, |
1352 | 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, | 1368 | 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, |
1353 | 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, | 1369 | 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, |
1354 | 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, | 1370 | 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, |
1355 | 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, | 1371 | 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, |
1356 | 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, | 1372 | 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, |
1357 | 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, | 1373 | 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, |
1358 | 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, | 1374 | 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, |
1359 | 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, | 1375 | 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, |
1360 | 0x2d02ef8dL | 1376 | 0x2d02ef8dL |
1361 | }; | 1377 | }; |
1378 | |||
1362 | /* inflate.c -- Not copyrighted 1992 by Mark Adler | 1379 | /* inflate.c -- Not copyrighted 1992 by Mark Adler |
1363 | version c10p1, 10 January 1993 */ | 1380 | version c10p1, 10 January 1993 */ |
1364 | 1381 | ||
@@ -1474,18 +1491,18 @@ static const ulg crc_32_tab[] = { | |||
1474 | an unused code. If a code with e == 99 is looked up, this implies an | 1491 | an unused code. If a code with e == 99 is looked up, this implies an |
1475 | error in the data. */ | 1492 | error in the data. */ |
1476 | struct huft { | 1493 | struct huft { |
1477 | uch e; /* number of extra bits or operation */ | 1494 | uch e; /* number of extra bits or operation */ |
1478 | uch b; /* number of bits in this code or subcode */ | 1495 | uch b; /* number of bits in this code or subcode */ |
1479 | union { | 1496 | union { |
1480 | ush n; /* literal, length base, or distance base */ | 1497 | ush n; /* literal, length base, or distance base */ |
1481 | struct huft *t; /* pointer to next level of table */ | 1498 | struct huft *t; /* pointer to next level of table */ |
1482 | } v; | 1499 | } v; |
1483 | }; | 1500 | }; |
1484 | 1501 | ||
1485 | 1502 | ||
1486 | /* Function prototypes */ | 1503 | /* Function prototypes */ |
1487 | int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *, | 1504 | int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *, |
1488 | struct huft **, int *)); | 1505 | struct huft **, int *)); |
1489 | int huft_free OF((struct huft *)); | 1506 | int huft_free OF((struct huft *)); |
1490 | int inflate_codes OF((struct huft *, struct huft *, int, int)); | 1507 | int inflate_codes OF((struct huft *, struct huft *, int, int)); |
1491 | int inflate_stored OF((void)); | 1508 | int inflate_stored OF((void)); |
@@ -1508,23 +1525,29 @@ int inflate OF((void)); | |||
1508 | #define flush_output(w) (wp=(w),flush_window()) | 1525 | #define flush_output(w) (wp=(w),flush_window()) |
1509 | 1526 | ||
1510 | /* Tables for deflate from PKZIP's appnote.txt. */ | 1527 | /* Tables for deflate from PKZIP's appnote.txt. */ |
1511 | static unsigned border[] = { /* Order of the bit length code lengths */ | 1528 | static unsigned border[] = { /* Order of the bit length code lengths */ |
1512 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 1529 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
1513 | static ush cplens[] = { /* Copy lengths for literal codes 257..285 */ | 1530 | }; |
1514 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | 1531 | static ush cplens[] = { /* Copy lengths for literal codes 257..285 */ |
1515 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | 1532 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
1516 | /* note: see note #13 above about the 258 in this list. */ | 1533 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
1517 | static ush cplext[] = { /* Extra bits for literal codes 257..285 */ | 1534 | }; |
1518 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, | 1535 | |
1519 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ | 1536 | /* note: see note #13 above about the 258 in this list. */ |
1520 | static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ | 1537 | static ush cplext[] = { /* Extra bits for literal codes 257..285 */ |
1521 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | 1538 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
1522 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | 1539 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 |
1523 | 8193, 12289, 16385, 24577}; | 1540 | }; /* 99==invalid */ |
1524 | static ush cpdext[] = { /* Extra bits for distance codes */ | 1541 | static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ |
1525 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | 1542 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
1526 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | 1543 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
1527 | 12, 12, 13, 13}; | 1544 | 8193, 12289, 16385, 24577 |
1545 | }; | ||
1546 | static ush cpdext[] = { /* Extra bits for distance codes */ | ||
1547 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | ||
1548 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | ||
1549 | 12, 12, 13, 13 | ||
1550 | }; | ||
1528 | 1551 | ||
1529 | 1552 | ||
1530 | 1553 | ||
@@ -1558,17 +1581,18 @@ static ush cpdext[] = { /* Extra bits for distance codes */ | |||
1558 | the stream. | 1581 | the stream. |
1559 | */ | 1582 | */ |
1560 | 1583 | ||
1561 | ulg bb; /* bit buffer */ | 1584 | ulg bb; /* bit buffer */ |
1562 | unsigned bk; /* bits in bit buffer */ | 1585 | unsigned bk; /* bits in bit buffer */ |
1563 | 1586 | ||
1564 | ush mask_bits[] = { | 1587 | ush mask_bits[] = { |
1565 | 0x0000, | 1588 | 0x0000, |
1566 | 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, | 1589 | 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
1567 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff | 1590 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
1568 | }; | 1591 | }; |
1569 | 1592 | ||
1570 | #ifdef CRYPT | 1593 | #ifdef CRYPT |
1571 | uch cc; | 1594 | uch cc; |
1595 | |||
1572 | # define NEXTBYTE() (cc = get_byte(), zdecode(cc), cc) | 1596 | # define NEXTBYTE() (cc = get_byte(), zdecode(cc), cc) |
1573 | #else | 1597 | #else |
1574 | # define NEXTBYTE() (uch)get_byte() | 1598 | # define NEXTBYTE() (uch)get_byte() |
@@ -1610,342 +1634,342 @@ ush mask_bits[] = { | |||
1610 | */ | 1634 | */ |
1611 | 1635 | ||
1612 | 1636 | ||
1613 | int lbits = 9; /* bits in base literal/length lookup table */ | 1637 | int lbits = 9; /* bits in base literal/length lookup table */ |
1614 | int dbits = 6; /* bits in base distance lookup table */ | 1638 | int dbits = 6; /* bits in base distance lookup table */ |
1615 | 1639 | ||
1616 | 1640 | ||
1617 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ | 1641 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ |
1618 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ | 1642 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ |
1619 | #define N_MAX 288 /* maximum number of codes in any set */ | 1643 | #define N_MAX 288 /* maximum number of codes in any set */ |
1620 | 1644 | ||
1621 | 1645 | ||
1622 | unsigned hufts; /* track memory usage */ | 1646 | unsigned hufts; /* track memory usage */ |
1623 | 1647 | ||
1624 | 1648 | ||
1625 | int huft_build(b, n, s, d, e, t, m) | 1649 | int huft_build(b, n, s, d, e, t, m) |
1626 | unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ | 1650 | unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ |
1627 | unsigned n; /* number of codes (assumed <= N_MAX) */ | 1651 | unsigned n; /* number of codes (assumed <= N_MAX) */ |
1628 | unsigned s; /* number of simple-valued codes (0..s-1) */ | 1652 | unsigned s; /* number of simple-valued codes (0..s-1) */ |
1629 | ush *d; /* list of base values for non-simple codes */ | 1653 | ush *d; /* list of base values for non-simple codes */ |
1630 | ush *e; /* list of extra bits for non-simple codes */ | 1654 | ush *e; /* list of extra bits for non-simple codes */ |
1631 | struct huft **t; /* result: starting table */ | 1655 | struct huft **t; /* result: starting table */ |
1632 | int *m; /* maximum lookup bits, returns actual */ | 1656 | int *m; /* maximum lookup bits, returns actual */ |
1657 | |||
1633 | /* Given a list of code lengths and a maximum table size, make a set of | 1658 | /* Given a list of code lengths and a maximum table size, make a set of |
1634 | tables to decode that set of codes. Return zero on success, one if | 1659 | tables to decode that set of codes. Return zero on success, one if |
1635 | the given code set is incomplete (the tables are still built in this | 1660 | the given code set is incomplete (the tables are still built in this |
1636 | case), two if the input is invalid (all zero length codes or an | 1661 | case), two if the input is invalid (all zero length codes or an |
1637 | oversubscribed set of lengths), and three if not enough memory. */ | 1662 | oversubscribed set of lengths), and three if not enough memory. */ |
1638 | { | 1663 | { |
1639 | unsigned a; /* counter for codes of length k */ | 1664 | unsigned a; /* counter for codes of length k */ |
1640 | unsigned c[BMAX+1]; /* bit length count table */ | 1665 | unsigned c[BMAX + 1]; /* bit length count table */ |
1641 | unsigned f; /* i repeats in table every f entries */ | 1666 | unsigned f; /* i repeats in table every f entries */ |
1642 | int g; /* maximum code length */ | 1667 | int g; /* maximum code length */ |
1643 | int h; /* table level */ | 1668 | int h; /* table level */ |
1644 | register unsigned i; /* counter, current code */ | 1669 | register unsigned i; /* counter, current code */ |
1645 | register unsigned j; /* counter */ | 1670 | register unsigned j; /* counter */ |
1646 | register int k; /* number of bits in current code */ | 1671 | register int k; /* number of bits in current code */ |
1647 | int l; /* bits per table (returned in m) */ | 1672 | int l; /* bits per table (returned in m) */ |
1648 | register unsigned *p; /* pointer into c[], b[], or v[] */ | 1673 | register unsigned *p; /* pointer into c[], b[], or v[] */ |
1649 | register struct huft *q; /* points to current table */ | 1674 | register struct huft *q; /* points to current table */ |
1650 | struct huft r; /* table entry for structure assignment */ | 1675 | struct huft r; /* table entry for structure assignment */ |
1651 | struct huft *u[BMAX]; /* table stack */ | 1676 | struct huft *u[BMAX]; /* table stack */ |
1652 | unsigned v[N_MAX]; /* values in order of bit length */ | 1677 | unsigned v[N_MAX]; /* values in order of bit length */ |
1653 | register int w; /* bits before this table == (l * h) */ | 1678 | register int w; /* bits before this table == (l * h) */ |
1654 | unsigned x[BMAX+1]; /* bit offsets, then code stack */ | 1679 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ |
1655 | unsigned *xp; /* pointer into x */ | 1680 | unsigned *xp; /* pointer into x */ |
1656 | int y; /* number of dummy codes added */ | 1681 | int y; /* number of dummy codes added */ |
1657 | unsigned z; /* number of entries in current table */ | 1682 | unsigned z; /* number of entries in current table */ |
1658 | 1683 | ||
1659 | 1684 | ||
1660 | /* Generate counts for each bit length */ | 1685 | /* Generate counts for each bit length */ |
1661 | memzero(c, sizeof(c)); | 1686 | memzero(c, sizeof(c)); |
1662 | p = b; i = n; | 1687 | p = b; |
1663 | do { | 1688 | i = n; |
1664 | Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"), | 1689 | do { |
1665 | n-i, *p)); | 1690 | Tracecv(*p, |
1666 | c[*p]++; /* assume all entries <= BMAX */ | 1691 | (stderr, |
1667 | p++; /* Can't combine with above line (Solaris bug) */ | 1692 | (n - i >= ' ' |
1668 | } while (--i); | 1693 | && n - i <= '~' ? "%c %d\n" : "0x%x %d\n"), n - i, *p)); |
1669 | if (c[0] == n) /* null input--all zero length codes */ | 1694 | c[*p]++; /* assume all entries <= BMAX */ |
1670 | { | 1695 | p++; /* Can't combine with above line (Solaris bug) */ |
1671 | *t = (struct huft *)NULL; | 1696 | } while (--i); |
1672 | *m = 0; | 1697 | if (c[0] == n) { /* null input--all zero length codes */ |
1673 | return 0; | 1698 | *t = (struct huft *) NULL; |
1674 | } | 1699 | *m = 0; |
1675 | 1700 | return 0; | |
1676 | 1701 | } | |
1677 | /* Find minimum and maximum length, bound *m by those */ | 1702 | |
1678 | l = *m; | 1703 | |
1679 | for (j = 1; j <= BMAX; j++) | 1704 | /* Find minimum and maximum length, bound *m by those */ |
1680 | if (c[j]) | 1705 | l = *m; |
1681 | break; | 1706 | for (j = 1; j <= BMAX; j++) |
1682 | k = j; /* minimum code length */ | 1707 | if (c[j]) |
1683 | if ((unsigned)l < j) | 1708 | break; |
1684 | l = j; | 1709 | k = j; /* minimum code length */ |
1685 | for (i = BMAX; i; i--) | 1710 | if ((unsigned) l < j) |
1686 | if (c[i]) | 1711 | l = j; |
1687 | break; | 1712 | for (i = BMAX; i; i--) |
1688 | g = i; /* maximum code length */ | 1713 | if (c[i]) |
1689 | if ((unsigned)l > i) | 1714 | break; |
1690 | l = i; | 1715 | g = i; /* maximum code length */ |
1691 | *m = l; | 1716 | if ((unsigned) l > i) |
1692 | 1717 | l = i; | |
1693 | 1718 | *m = l; | |
1694 | /* Adjust last length count to fill out codes, if needed */ | 1719 | |
1695 | for (y = 1 << j; j < i; j++, y <<= 1) | 1720 | |
1696 | if ((y -= c[j]) < 0) | 1721 | /* Adjust last length count to fill out codes, if needed */ |
1697 | return 2; /* bad input: more codes than bits */ | 1722 | for (y = 1 << j; j < i; j++, y <<= 1) |
1698 | if ((y -= c[i]) < 0) | 1723 | if ((y -= c[j]) < 0) |
1699 | return 2; | 1724 | return 2; /* bad input: more codes than bits */ |
1700 | c[i] += y; | 1725 | if ((y -= c[i]) < 0) |
1701 | 1726 | return 2; | |
1702 | 1727 | c[i] += y; | |
1703 | /* Generate starting offsets into the value table for each length */ | 1728 | |
1704 | x[1] = j = 0; | 1729 | |
1705 | p = c + 1; xp = x + 2; | 1730 | /* Generate starting offsets into the value table for each length */ |
1706 | while (--i) { /* note that i == g from above */ | 1731 | x[1] = j = 0; |
1707 | *xp++ = (j += *p++); | 1732 | p = c + 1; |
1708 | } | 1733 | xp = x + 2; |
1709 | 1734 | while (--i) { /* note that i == g from above */ | |
1710 | 1735 | *xp++ = (j += *p++); | |
1711 | /* Make a table of values in order of bit lengths */ | 1736 | } |
1712 | p = b; i = 0; | 1737 | |
1713 | do { | 1738 | |
1714 | if ((j = *p++) != 0) | 1739 | /* Make a table of values in order of bit lengths */ |
1715 | v[x[j]++] = i; | 1740 | p = b; |
1716 | } while (++i < n); | 1741 | i = 0; |
1717 | 1742 | do { | |
1718 | 1743 | if ((j = *p++) != 0) | |
1719 | /* Generate the Huffman codes and for each, make the table entries */ | 1744 | v[x[j]++] = i; |
1720 | x[0] = i = 0; /* first Huffman code is zero */ | 1745 | } while (++i < n); |
1721 | p = v; /* grab values in bit order */ | 1746 | |
1722 | h = -1; /* no tables yet--level -1 */ | 1747 | |
1723 | w = -l; /* bits decoded == (l * h) */ | 1748 | /* Generate the Huffman codes and for each, make the table entries */ |
1724 | u[0] = (struct huft *)NULL; /* just to keep compilers happy */ | 1749 | x[0] = i = 0; /* first Huffman code is zero */ |
1725 | q = (struct huft *)NULL; /* ditto */ | 1750 | p = v; /* grab values in bit order */ |
1726 | z = 0; /* ditto */ | 1751 | h = -1; /* no tables yet--level -1 */ |
1727 | 1752 | w = -l; /* bits decoded == (l * h) */ | |
1728 | /* go through the bit lengths (k already is bits in shortest code) */ | 1753 | u[0] = (struct huft *) NULL; /* just to keep compilers happy */ |
1729 | for (; k <= g; k++) | 1754 | q = (struct huft *) NULL; /* ditto */ |
1730 | { | 1755 | z = 0; /* ditto */ |
1731 | a = c[k]; | 1756 | |
1732 | while (a--) | 1757 | /* go through the bit lengths (k already is bits in shortest code) */ |
1733 | { | 1758 | for (; k <= g; k++) { |
1734 | /* here i is the Huffman code of length k bits for value *p */ | 1759 | a = c[k]; |
1735 | /* make tables up to required level */ | 1760 | while (a--) { |
1736 | while (k > w + l) | 1761 | /* here i is the Huffman code of length k bits for value *p */ |
1737 | { | 1762 | /* make tables up to required level */ |
1738 | h++; | 1763 | while (k > w + l) { |
1739 | w += l; /* previous table always l bits */ | 1764 | h++; |
1740 | 1765 | w += l; /* previous table always l bits */ | |
1741 | /* compute minimum size table less than or equal to l bits */ | 1766 | |
1742 | z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */ | 1767 | /* compute minimum size table less than or equal to l bits */ |
1743 | if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ | 1768 | z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ |
1744 | { /* too few codes for k-w bit table */ | 1769 | if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */ |
1745 | f -= a + 1; /* deduct codes from patterns left */ | 1770 | f -= a + 1; /* deduct codes from patterns left */ |
1746 | xp = c + k; | 1771 | xp = c + k; |
1747 | while (++j < z) /* try smaller tables up to z bits */ | 1772 | while (++j < z) { /* try smaller tables up to z bits */ |
1748 | { | 1773 | if ((f <<= 1) <= *++xp) |
1749 | if ((f <<= 1) <= *++xp) | 1774 | break; /* enough codes to use up j bits */ |
1750 | break; /* enough codes to use up j bits */ | 1775 | f -= *xp; /* else deduct codes from patterns */ |
1751 | f -= *xp; /* else deduct codes from patterns */ | 1776 | } |
1752 | } | 1777 | } |
1753 | } | 1778 | z = 1 << j; /* table entries for j-bit table */ |
1754 | z = 1 << j; /* table entries for j-bit table */ | 1779 | |
1755 | 1780 | /* allocate and link in new table */ | |
1756 | /* allocate and link in new table */ | 1781 | if ( |
1757 | if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == | 1782 | (q = |
1758 | (struct huft *)NULL) | 1783 | (struct huft *) malloc((z + 1) * |
1759 | { | 1784 | sizeof(struct huft))) == |
1760 | if (h) | 1785 | (struct huft *) NULL) { |
1761 | huft_free(u[0]); | 1786 | if (h) |
1762 | return 3; /* not enough memory */ | 1787 | huft_free(u[0]); |
1763 | } | 1788 | return 3; /* not enough memory */ |
1764 | hufts += z + 1; /* track memory usage */ | 1789 | } |
1765 | *t = q + 1; /* link to list for huft_free() */ | 1790 | hufts += z + 1; /* track memory usage */ |
1766 | *(t = &(q->v.t)) = (struct huft *)NULL; | 1791 | *t = q + 1; /* link to list for huft_free() */ |
1767 | u[h] = ++q; /* table starts after link */ | 1792 | *(t = &(q->v.t)) = (struct huft *) NULL; |
1768 | 1793 | u[h] = ++q; /* table starts after link */ | |
1769 | /* connect to last table, if there is one */ | 1794 | |
1770 | if (h) | 1795 | /* connect to last table, if there is one */ |
1771 | { | 1796 | if (h) { |
1772 | x[h] = i; /* save pattern for backing up */ | 1797 | x[h] = i; /* save pattern for backing up */ |
1773 | r.b = (uch)l; /* bits to dump before this table */ | 1798 | r.b = (uch) l; /* bits to dump before this table */ |
1774 | r.e = (uch)(16 + j); /* bits in this table */ | 1799 | r.e = (uch) (16 + j); /* bits in this table */ |
1775 | r.v.t = q; /* pointer to this table */ | 1800 | r.v.t = q; /* pointer to this table */ |
1776 | j = i >> (w - l); /* (get around Turbo C bug) */ | 1801 | j = i >> (w - l); /* (get around Turbo C bug) */ |
1777 | u[h-1][j] = r; /* connect to last table */ | 1802 | u[h - 1][j] = r; /* connect to last table */ |
1778 | } | 1803 | } |
1779 | } | 1804 | } |
1780 | 1805 | ||
1781 | /* set up table entry in r */ | 1806 | /* set up table entry in r */ |
1782 | r.b = (uch)(k - w); | 1807 | r.b = (uch) (k - w); |
1783 | if (p >= v + n) | 1808 | if (p >= v + n) |
1784 | r.e = 99; /* out of values--invalid code */ | 1809 | r.e = 99; /* out of values--invalid code */ |
1785 | else if (*p < s) | 1810 | else if (*p < s) { |
1786 | { | 1811 | r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ |
1787 | r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ | 1812 | r.v.n = (ush) (*p); /* simple code is just the value */ |
1788 | r.v.n = (ush)(*p); /* simple code is just the value */ | 1813 | p++; /* one compiler does not like *p++ */ |
1789 | p++; /* one compiler does not like *p++ */ | 1814 | } else { |
1790 | } | 1815 | r.e = (uch) e[*p - s]; /* non-simple--look up in lists */ |
1791 | else | 1816 | r.v.n = d[*p++ - s]; |
1792 | { | 1817 | } |
1793 | r.e = (uch)e[*p - s]; /* non-simple--look up in lists */ | 1818 | |
1794 | r.v.n = d[*p++ - s]; | 1819 | /* fill code-like entries with r */ |
1795 | } | 1820 | f = 1 << (k - w); |
1796 | 1821 | for (j = i >> w; j < z; j += f) | |
1797 | /* fill code-like entries with r */ | 1822 | q[j] = r; |
1798 | f = 1 << (k - w); | 1823 | |
1799 | for (j = i >> w; j < z; j += f) | 1824 | /* backwards increment the k-bit code i */ |
1800 | q[j] = r; | 1825 | for (j = 1 << (k - 1); i & j; j >>= 1) |
1801 | 1826 | i ^= j; | |
1802 | /* backwards increment the k-bit code i */ | 1827 | i ^= j; |
1803 | for (j = 1 << (k - 1); i & j; j >>= 1) | 1828 | |
1804 | i ^= j; | 1829 | /* backup over finished tables */ |
1805 | i ^= j; | 1830 | while ((i & ((1 << w) - 1)) != x[h]) { |
1806 | 1831 | h--; /* don't need to update q */ | |
1807 | /* backup over finished tables */ | 1832 | w -= l; |
1808 | while ((i & ((1 << w) - 1)) != x[h]) | 1833 | } |
1809 | { | 1834 | } |
1810 | h--; /* don't need to update q */ | 1835 | } |
1811 | w -= l; | 1836 | |
1812 | } | 1837 | |
1813 | } | 1838 | /* Return true (1) if we were given an incomplete table */ |
1814 | } | 1839 | return y != 0 && g != 1; |
1815 | |||
1816 | |||
1817 | /* Return true (1) if we were given an incomplete table */ | ||
1818 | return y != 0 && g != 1; | ||
1819 | } | 1840 | } |
1820 | 1841 | ||
1821 | 1842 | ||
1822 | 1843 | ||
1823 | int huft_free(t) | 1844 | int huft_free(t) |
1824 | struct huft *t; /* table to free */ | 1845 | struct huft *t; /* table to free */ |
1846 | |||
1825 | /* Free the malloc'ed tables built by huft_build(), which makes a linked | 1847 | /* Free the malloc'ed tables built by huft_build(), which makes a linked |
1826 | list of the tables it made, with the links in a dummy first entry of | 1848 | list of the tables it made, with the links in a dummy first entry of |
1827 | each table. */ | 1849 | each table. */ |
1828 | { | 1850 | { |
1829 | register struct huft *p, *q; | 1851 | register struct huft *p, *q; |
1830 | 1852 | ||
1831 | 1853 | ||
1832 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ | 1854 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ |
1833 | p = t; | 1855 | p = t; |
1834 | while (p != (struct huft *)NULL) | 1856 | while (p != (struct huft *) NULL) { |
1835 | { | 1857 | q = (--p)->v.t; |
1836 | q = (--p)->v.t; | 1858 | free((char *) p); |
1837 | free((char*)p); | 1859 | p = q; |
1838 | p = q; | 1860 | } |
1839 | } | 1861 | return 0; |
1840 | return 0; | ||
1841 | } | 1862 | } |
1842 | 1863 | ||
1843 | 1864 | ||
1844 | int inflate_codes(tl, td, bl, bd) | 1865 | int inflate_codes(tl, td, bl, bd) |
1845 | struct huft *tl, *td; /* literal/length and distance decoder tables */ | 1866 | struct huft *tl, *td; /* literal/length and distance decoder tables */ |
1846 | int bl, bd; /* number of bits decoded by tl[] and td[] */ | 1867 | int bl, bd; /* number of bits decoded by tl[] and td[] */ |
1868 | |||
1847 | /* inflate (decompress) the codes in a deflated (compressed) block. | 1869 | /* inflate (decompress) the codes in a deflated (compressed) block. |
1848 | Return an error code or zero if it all goes ok. */ | 1870 | Return an error code or zero if it all goes ok. */ |
1849 | { | 1871 | { |
1850 | register unsigned e; /* table entry flag/number of extra bits */ | 1872 | register unsigned e; /* table entry flag/number of extra bits */ |
1851 | unsigned n, d; /* length and index for copy */ | 1873 | unsigned n, d; /* length and index for copy */ |
1852 | unsigned w; /* current window position */ | 1874 | unsigned w; /* current window position */ |
1853 | struct huft *t; /* pointer to table entry */ | 1875 | struct huft *t; /* pointer to table entry */ |
1854 | unsigned ml, md; /* masks for bl and bd bits */ | 1876 | unsigned ml, md; /* masks for bl and bd bits */ |
1855 | register ulg b; /* bit buffer */ | 1877 | register ulg b; /* bit buffer */ |
1856 | register unsigned k; /* number of bits in bit buffer */ | 1878 | register unsigned k; /* number of bits in bit buffer */ |
1857 | 1879 | ||
1858 | 1880 | ||
1859 | /* make local copies of globals */ | 1881 | /* make local copies of globals */ |
1860 | b = bb; /* initialize bit buffer */ | 1882 | b = bb; /* initialize bit buffer */ |
1861 | k = bk; | 1883 | k = bk; |
1862 | w = wp; /* initialize window position */ | 1884 | w = wp; /* initialize window position */ |
1863 | 1885 | ||
1864 | /* inflate the coded data */ | 1886 | /* inflate the coded data */ |
1865 | ml = mask_bits[bl]; /* precompute masks for speed */ | 1887 | ml = mask_bits[bl]; /* precompute masks for speed */ |
1866 | md = mask_bits[bd]; | 1888 | md = mask_bits[bd]; |
1867 | for (;;) /* do until end of block */ | 1889 | for (;;) { /* do until end of block */ |
1868 | { | 1890 | NEEDBITS((unsigned) bl) |
1869 | NEEDBITS((unsigned)bl) | 1891 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
1870 | if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) | 1892 | do { |
1871 | do { | 1893 | if (e == 99) |
1872 | if (e == 99) | 1894 | return 1; |
1873 | return 1; | 1895 | DUMPBITS(t->b) |
1874 | DUMPBITS(t->b) | 1896 | e -= 16; |
1875 | e -= 16; | 1897 | NEEDBITS(e) |
1876 | NEEDBITS(e) | 1898 | } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) |
1877 | } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); | 1899 | > 16); |
1878 | DUMPBITS(t->b) | 1900 | DUMPBITS(t->b) |
1879 | if (e == 16) /* then it's a literal */ | 1901 | if (e == 16) { /* then it's a literal */ |
1880 | { | 1902 | slide[w++] = (uch) t->v.n; |
1881 | slide[w++] = (uch)t->v.n; | 1903 | Tracevv((stderr, "%c", slide[w - 1])); |
1882 | Tracevv((stderr, "%c", slide[w-1])); | 1904 | if (w == WSIZE) { |
1883 | if (w == WSIZE) | 1905 | flush_output(w); |
1884 | { | 1906 | w = 0; |
1885 | flush_output(w); | 1907 | } |
1886 | w = 0; | 1908 | } else { /* it's an EOB or a length */ |
1887 | } | 1909 | |
1888 | } | 1910 | /* exit if end of block */ |
1889 | else /* it's an EOB or a length */ | 1911 | if (e == 15) |
1890 | { | 1912 | break; |
1891 | /* exit if end of block */ | 1913 | |
1892 | if (e == 15) | 1914 | /* get length of block to copy */ |
1893 | break; | 1915 | NEEDBITS(e) |
1894 | 1916 | n = t->v.n + ((unsigned) b & mask_bits[e]); | |
1895 | /* get length of block to copy */ | 1917 | DUMPBITS(e); |
1896 | NEEDBITS(e) | 1918 | |
1897 | n = t->v.n + ((unsigned)b & mask_bits[e]); | 1919 | /* decode distance of block to copy */ |
1898 | DUMPBITS(e); | 1920 | NEEDBITS((unsigned) bd) |
1899 | 1921 | if ((e = (t = td + ((unsigned) b & md))->e) > 16) | |
1900 | /* decode distance of block to copy */ | 1922 | do { |
1901 | NEEDBITS((unsigned)bd) | 1923 | if (e == 99) |
1902 | if ((e = (t = td + ((unsigned)b & md))->e) > 16) | 1924 | return 1; |
1903 | do { | 1925 | DUMPBITS(t->b) |
1904 | if (e == 99) | 1926 | e -= 16; |
1905 | return 1; | 1927 | NEEDBITS(e) |
1906 | DUMPBITS(t->b) | 1928 | } |
1907 | e -= 16; | 1929 | while ( |
1908 | NEEDBITS(e) | 1930 | (e = |
1909 | } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); | 1931 | (t = |
1910 | DUMPBITS(t->b) | 1932 | t->v.t + ((unsigned) b & mask_bits[e]))->e) > |
1911 | NEEDBITS(e) | 1933 | 16); |
1912 | d = w - t->v.n - ((unsigned)b & mask_bits[e]); | 1934 | DUMPBITS(t->b) |
1913 | DUMPBITS(e) | 1935 | NEEDBITS(e) |
1914 | Tracevv((stderr,"\\[%d,%d]", w-d, n)); | 1936 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
1915 | 1937 | DUMPBITS(e) | |
1916 | /* do the copy */ | 1938 | Tracevv((stderr, "\\[%d,%d]", w - d, n)); |
1917 | do { | 1939 | |
1918 | n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); | 1940 | /* do the copy */ |
1941 | do { | ||
1942 | n -= (e = | ||
1943 | (e = | ||
1944 | WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > | ||
1945 | n ? n : e); | ||
1919 | #if !defined(NOMEMCPY) && !defined(DEBUG) | 1946 | #if !defined(NOMEMCPY) && !defined(DEBUG) |
1920 | if (w - d >= e) /* (this test assumes unsigned comparison) */ | 1947 | if (w - d >= e) { /* (this test assumes unsigned comparison) */ |
1921 | { | 1948 | memcpy(slide + w, slide + d, e); |
1922 | memcpy(slide + w, slide + d, e); | 1949 | w += e; |
1923 | w += e; | 1950 | d += e; |
1924 | d += e; | 1951 | } else /* do it slow to avoid memcpy() overlap */ |
1925 | } | 1952 | #endif /* !NOMEMCPY */ |
1926 | else /* do it slow to avoid memcpy() overlap */ | 1953 | do { |
1927 | #endif /* !NOMEMCPY */ | 1954 | slide[w++] = slide[d++]; |
1928 | do { | 1955 | Tracevv((stderr, "%c", slide[w - 1])); |
1929 | slide[w++] = slide[d++]; | 1956 | } while (--e); |
1930 | Tracevv((stderr, "%c", slide[w-1])); | 1957 | if (w == WSIZE) { |
1931 | } while (--e); | 1958 | flush_output(w); |
1932 | if (w == WSIZE) | 1959 | w = 0; |
1933 | { | 1960 | } |
1934 | flush_output(w); | 1961 | } while (n); |
1935 | w = 0; | 1962 | } |
1936 | } | 1963 | } |
1937 | } while (n); | 1964 | |
1938 | } | 1965 | |
1939 | } | 1966 | /* restore the globals from the locals */ |
1940 | 1967 | wp = w; /* restore global window pointer */ | |
1941 | 1968 | bb = b; /* restore global bit buffer */ | |
1942 | /* restore the globals from the locals */ | 1969 | bk = k; |
1943 | wp = w; /* restore global window pointer */ | 1970 | |
1944 | bb = b; /* restore global bit buffer */ | 1971 | /* done */ |
1945 | bk = k; | 1972 | return 0; |
1946 | |||
1947 | /* done */ | ||
1948 | return 0; | ||
1949 | } | 1973 | } |
1950 | 1974 | ||
1951 | 1975 | ||
@@ -1953,52 +1977,50 @@ int bl, bd; /* number of bits decoded by tl[] and td[] */ | |||
1953 | int inflate_stored() | 1977 | int inflate_stored() |
1954 | /* "decompress" an inflated type 0 (stored) block. */ | 1978 | /* "decompress" an inflated type 0 (stored) block. */ |
1955 | { | 1979 | { |
1956 | unsigned n; /* number of bytes in block */ | 1980 | unsigned n; /* number of bytes in block */ |
1957 | unsigned w; /* current window position */ | 1981 | unsigned w; /* current window position */ |
1958 | register ulg b; /* bit buffer */ | 1982 | register ulg b; /* bit buffer */ |
1959 | register unsigned k; /* number of bits in bit buffer */ | 1983 | register unsigned k; /* number of bits in bit buffer */ |
1960 | 1984 | ||
1961 | 1985 | ||
1962 | /* make local copies of globals */ | 1986 | /* make local copies of globals */ |
1963 | b = bb; /* initialize bit buffer */ | 1987 | b = bb; /* initialize bit buffer */ |
1964 | k = bk; | 1988 | k = bk; |
1965 | w = wp; /* initialize window position */ | 1989 | w = wp; /* initialize window position */ |
1966 | 1990 | ||
1967 | 1991 | ||
1968 | /* go to byte boundary */ | 1992 | /* go to byte boundary */ |
1969 | n = k & 7; | 1993 | n = k & 7; |
1970 | DUMPBITS(n); | 1994 | DUMPBITS(n); |
1971 | 1995 | ||
1972 | 1996 | ||
1973 | /* get the length and its complement */ | 1997 | /* get the length and its complement */ |
1974 | NEEDBITS(16) | 1998 | NEEDBITS(16) |
1975 | n = ((unsigned)b & 0xffff); | 1999 | n = ((unsigned) b & 0xffff); |
1976 | DUMPBITS(16) | 2000 | DUMPBITS(16) |
1977 | NEEDBITS(16) | 2001 | NEEDBITS(16) |
1978 | if (n != (unsigned)((~b) & 0xffff)) | 2002 | if (n != (unsigned) ((~b) & 0xffff)) |
1979 | return 1; /* error in compressed data */ | 2003 | return 1; /* error in compressed data */ |
1980 | DUMPBITS(16) | 2004 | DUMPBITS(16) |
1981 | 2005 | ||
1982 | 2006 | ||
1983 | /* read and output the compressed data */ | 2007 | /* read and output the compressed data */ |
1984 | while (n--) | 2008 | while (n--) { |
1985 | { | 2009 | NEEDBITS(8) |
1986 | NEEDBITS(8) | 2010 | slide[w++] = (uch) b; |
1987 | slide[w++] = (uch)b; | 2011 | if (w == WSIZE) { |
1988 | if (w == WSIZE) | 2012 | flush_output(w); |
1989 | { | 2013 | w = 0; |
1990 | flush_output(w); | 2014 | } |
1991 | w = 0; | 2015 | DUMPBITS(8) |
1992 | } | 2016 | } |
1993 | DUMPBITS(8) | 2017 | |
1994 | } | 2018 | |
1995 | 2019 | /* restore the globals from the locals */ | |
1996 | 2020 | wp = w; /* restore global window pointer */ | |
1997 | /* restore the globals from the locals */ | 2021 | bb = b; /* restore global bit buffer */ |
1998 | wp = w; /* restore global window pointer */ | 2022 | bk = k; |
1999 | bb = b; /* restore global bit buffer */ | 2023 | return 0; |
2000 | bk = k; | ||
2001 | return 0; | ||
2002 | } | 2024 | } |
2003 | 2025 | ||
2004 | 2026 | ||
@@ -2008,48 +2030,47 @@ int inflate_fixed() | |||
2008 | either replace this with a custom decoder, or at least precompute the | 2030 | either replace this with a custom decoder, or at least precompute the |
2009 | Huffman tables. */ | 2031 | Huffman tables. */ |
2010 | { | 2032 | { |
2011 | int i; /* temporary variable */ | 2033 | int i; /* temporary variable */ |
2012 | struct huft *tl; /* literal/length code table */ | 2034 | struct huft *tl; /* literal/length code table */ |
2013 | struct huft *td; /* distance code table */ | 2035 | struct huft *td; /* distance code table */ |
2014 | int bl; /* lookup bits for tl */ | 2036 | int bl; /* lookup bits for tl */ |
2015 | int bd; /* lookup bits for td */ | 2037 | int bd; /* lookup bits for td */ |
2016 | unsigned l[288]; /* length list for huft_build */ | 2038 | unsigned l[288]; /* length list for huft_build */ |
2017 | 2039 | ||
2018 | 2040 | ||
2019 | /* set up literal table */ | 2041 | /* set up literal table */ |
2020 | for (i = 0; i < 144; i++) | 2042 | for (i = 0; i < 144; i++) |
2021 | l[i] = 8; | 2043 | l[i] = 8; |
2022 | for (; i < 256; i++) | 2044 | for (; i < 256; i++) |
2023 | l[i] = 9; | 2045 | l[i] = 9; |
2024 | for (; i < 280; i++) | 2046 | for (; i < 280; i++) |
2025 | l[i] = 7; | 2047 | l[i] = 7; |
2026 | for (; i < 288; i++) /* make a complete, but wrong code set */ | 2048 | for (; i < 288; i++) /* make a complete, but wrong code set */ |
2027 | l[i] = 8; | 2049 | l[i] = 8; |
2028 | bl = 7; | 2050 | bl = 7; |
2029 | if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) | 2051 | if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) |
2030 | return i; | 2052 | return i; |
2031 | 2053 | ||
2032 | 2054 | ||
2033 | /* set up distance table */ | 2055 | /* set up distance table */ |
2034 | for (i = 0; i < 30; i++) /* make an incomplete code set */ | 2056 | for (i = 0; i < 30; i++) /* make an incomplete code set */ |
2035 | l[i] = 5; | 2057 | l[i] = 5; |
2036 | bd = 5; | 2058 | bd = 5; |
2037 | if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) | 2059 | if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { |
2038 | { | 2060 | huft_free(tl); |
2039 | huft_free(tl); | 2061 | return i; |
2040 | return i; | 2062 | } |
2041 | } | 2063 | |
2042 | 2064 | ||
2043 | 2065 | /* decompress until an end-of-block code */ | |
2044 | /* decompress until an end-of-block code */ | 2066 | if (inflate_codes(tl, td, bl, bd)) |
2045 | if (inflate_codes(tl, td, bl, bd)) | 2067 | return 1; |
2046 | return 1; | 2068 | |
2047 | 2069 | ||
2048 | 2070 | /* free the decoding tables, return */ | |
2049 | /* free the decoding tables, return */ | 2071 | huft_free(tl); |
2050 | huft_free(tl); | 2072 | huft_free(td); |
2051 | huft_free(td); | 2073 | return 0; |
2052 | return 0; | ||
2053 | } | 2074 | } |
2054 | 2075 | ||
2055 | 2076 | ||
@@ -2057,209 +2078,202 @@ int inflate_fixed() | |||
2057 | int inflate_dynamic() | 2078 | int inflate_dynamic() |
2058 | /* decompress an inflated type 2 (dynamic Huffman codes) block. */ | 2079 | /* decompress an inflated type 2 (dynamic Huffman codes) block. */ |
2059 | { | 2080 | { |
2060 | int i; /* temporary variables */ | 2081 | int i; /* temporary variables */ |
2061 | unsigned j; | 2082 | unsigned j; |
2062 | unsigned l; /* last length */ | 2083 | unsigned l; /* last length */ |
2063 | unsigned m; /* mask for bit lengths table */ | 2084 | unsigned m; /* mask for bit lengths table */ |
2064 | unsigned n; /* number of lengths to get */ | 2085 | unsigned n; /* number of lengths to get */ |
2065 | struct huft *tl; /* literal/length code table */ | 2086 | struct huft *tl; /* literal/length code table */ |
2066 | struct huft *td; /* distance code table */ | 2087 | struct huft *td; /* distance code table */ |
2067 | int bl; /* lookup bits for tl */ | 2088 | int bl; /* lookup bits for tl */ |
2068 | int bd; /* lookup bits for td */ | 2089 | int bd; /* lookup bits for td */ |
2069 | unsigned nb; /* number of bit length codes */ | 2090 | unsigned nb; /* number of bit length codes */ |
2070 | unsigned nl; /* number of literal/length codes */ | 2091 | unsigned nl; /* number of literal/length codes */ |
2071 | unsigned nd; /* number of distance codes */ | 2092 | unsigned nd; /* number of distance codes */ |
2093 | |||
2072 | #ifdef PKZIP_BUG_WORKAROUND | 2094 | #ifdef PKZIP_BUG_WORKAROUND |
2073 | unsigned ll[288+32]; /* literal/length and distance code lengths */ | 2095 | unsigned ll[288 + 32]; /* literal/length and distance code lengths */ |
2074 | #else | 2096 | #else |
2075 | unsigned ll[286+30]; /* literal/length and distance code lengths */ | 2097 | unsigned ll[286 + 30]; /* literal/length and distance code lengths */ |
2076 | #endif | 2098 | #endif |
2077 | register ulg b; /* bit buffer */ | 2099 | register ulg b; /* bit buffer */ |
2078 | register unsigned k; /* number of bits in bit buffer */ | 2100 | register unsigned k; /* number of bits in bit buffer */ |
2079 | 2101 | ||
2080 | 2102 | ||
2081 | /* make local bit buffer */ | 2103 | /* make local bit buffer */ |
2082 | b = bb; | 2104 | b = bb; |
2083 | k = bk; | 2105 | k = bk; |
2084 | 2106 | ||
2085 | 2107 | ||
2086 | /* read in table lengths */ | 2108 | /* read in table lengths */ |
2087 | NEEDBITS(5) | 2109 | NEEDBITS(5) |
2088 | nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ | 2110 | nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */ |
2089 | DUMPBITS(5) | 2111 | DUMPBITS(5) |
2090 | NEEDBITS(5) | 2112 | NEEDBITS(5) |
2091 | nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ | 2113 | nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */ |
2092 | DUMPBITS(5) | 2114 | DUMPBITS(5) |
2093 | NEEDBITS(4) | 2115 | NEEDBITS(4) |
2094 | nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ | 2116 | nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */ |
2095 | DUMPBITS(4) | 2117 | DUMPBITS(4) |
2096 | #ifdef PKZIP_BUG_WORKAROUND | 2118 | #ifdef PKZIP_BUG_WORKAROUND |
2097 | if (nl > 288 || nd > 32) | 2119 | if (nl > 288 || nd > 32) |
2098 | #else | 2120 | #else |
2099 | if (nl > 286 || nd > 30) | 2121 | if (nl > 286 || nd > 30) |
2100 | #endif | 2122 | #endif |
2101 | return 1; /* bad lengths */ | 2123 | return 1; /* bad lengths */ |
2102 | 2124 | ||
2103 | 2125 | ||
2104 | /* read in bit-length-code lengths */ | 2126 | /* read in bit-length-code lengths */ |
2105 | for (j = 0; j < nb; j++) | 2127 | for (j = 0; j < nb; j++) { |
2106 | { | 2128 | NEEDBITS(3) |
2107 | NEEDBITS(3) | 2129 | ll[border[j]] = (unsigned) b & 7; |
2108 | ll[border[j]] = (unsigned)b & 7; | 2130 | DUMPBITS(3) |
2109 | DUMPBITS(3) | 2131 | } |
2110 | } | 2132 | for (; j < 19; j++) |
2111 | for (; j < 19; j++) | 2133 | ll[border[j]] = 0; |
2112 | ll[border[j]] = 0; | 2134 | |
2113 | 2135 | ||
2114 | 2136 | /* build decoding table for trees--single level, 7 bit lookup */ | |
2115 | /* build decoding table for trees--single level, 7 bit lookup */ | 2137 | bl = 7; |
2116 | bl = 7; | 2138 | if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) { |
2117 | if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) | 2139 | if (i == 1) |
2118 | { | 2140 | huft_free(tl); |
2119 | if (i == 1) | 2141 | return i; /* incomplete code set */ |
2120 | huft_free(tl); | 2142 | } |
2121 | return i; /* incomplete code set */ | 2143 | |
2122 | } | 2144 | |
2123 | 2145 | /* read in literal and distance code lengths */ | |
2124 | 2146 | n = nl + nd; | |
2125 | /* read in literal and distance code lengths */ | 2147 | m = mask_bits[bl]; |
2126 | n = nl + nd; | 2148 | i = l = 0; |
2127 | m = mask_bits[bl]; | 2149 | while ((unsigned) i < n) { |
2128 | i = l = 0; | 2150 | NEEDBITS((unsigned) bl) |
2129 | while ((unsigned)i < n) | 2151 | j = (td = tl + ((unsigned) b & m))->b; |
2130 | { | 2152 | DUMPBITS(j) |
2131 | NEEDBITS((unsigned)bl) | 2153 | j = td->v.n; |
2132 | j = (td = tl + ((unsigned)b & m))->b; | 2154 | if (j < 16) /* length of code in bits (0..15) */ |
2133 | DUMPBITS(j) | 2155 | ll[i++] = l = j; /* save last length in l */ |
2134 | j = td->v.n; | 2156 | else if (j == 16) { /* repeat last length 3 to 6 times */ |
2135 | if (j < 16) /* length of code in bits (0..15) */ | 2157 | NEEDBITS(2) |
2136 | ll[i++] = l = j; /* save last length in l */ | 2158 | j = 3 + ((unsigned) b & 3); |
2137 | else if (j == 16) /* repeat last length 3 to 6 times */ | 2159 | DUMPBITS(2) |
2138 | { | 2160 | if ((unsigned) i + j > n) |
2139 | NEEDBITS(2) | 2161 | return 1; |
2140 | j = 3 + ((unsigned)b & 3); | 2162 | while (j--) |
2141 | DUMPBITS(2) | 2163 | ll[i++] = l; |
2142 | if ((unsigned)i + j > n) | 2164 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
2143 | return 1; | 2165 | NEEDBITS(3) |
2144 | while (j--) | 2166 | j = 3 + ((unsigned) b & 7); |
2145 | ll[i++] = l; | 2167 | DUMPBITS(3) |
2146 | } | 2168 | if ((unsigned) i + j > n) |
2147 | else if (j == 17) /* 3 to 10 zero length codes */ | 2169 | return 1; |
2148 | { | 2170 | while (j--) |
2149 | NEEDBITS(3) | 2171 | ll[i++] = 0; |
2150 | j = 3 + ((unsigned)b & 7); | 2172 | l = 0; |
2151 | DUMPBITS(3) | 2173 | } else { /* j == 18: 11 to 138 zero length codes */ |
2152 | if ((unsigned)i + j > n) | 2174 | |
2153 | return 1; | 2175 | NEEDBITS(7) |
2154 | while (j--) | 2176 | j = 11 + ((unsigned) b & 0x7f); |
2155 | ll[i++] = 0; | 2177 | DUMPBITS(7) |
2156 | l = 0; | 2178 | if ((unsigned) i + j > n) |
2157 | } | 2179 | return 1; |
2158 | else /* j == 18: 11 to 138 zero length codes */ | 2180 | while (j--) |
2159 | { | 2181 | ll[i++] = 0; |
2160 | NEEDBITS(7) | 2182 | l = 0; |
2161 | j = 11 + ((unsigned)b & 0x7f); | 2183 | } |
2162 | DUMPBITS(7) | 2184 | } |
2163 | if ((unsigned)i + j > n) | 2185 | |
2164 | return 1; | 2186 | |
2165 | while (j--) | 2187 | /* free decoding table for trees */ |
2166 | ll[i++] = 0; | 2188 | huft_free(tl); |
2167 | l = 0; | 2189 | |
2168 | } | 2190 | |
2169 | } | 2191 | /* restore the global bit buffer */ |
2170 | 2192 | bb = b; | |
2171 | 2193 | bk = k; | |
2172 | /* free decoding table for trees */ | 2194 | |
2173 | huft_free(tl); | 2195 | |
2174 | 2196 | /* build the decoding tables for literal/length and distance codes */ | |
2175 | 2197 | bl = lbits; | |
2176 | /* restore the global bit buffer */ | 2198 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { |
2177 | bb = b; | 2199 | if (i == 1) { |
2178 | bk = k; | 2200 | fprintf(stderr, " incomplete literal tree\n"); |
2179 | 2201 | huft_free(tl); | |
2180 | 2202 | } | |
2181 | /* build the decoding tables for literal/length and distance codes */ | 2203 | return i; /* incomplete code set */ |
2182 | bl = lbits; | 2204 | } |
2183 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) | 2205 | bd = dbits; |
2184 | { | 2206 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { |
2185 | if (i == 1) { | 2207 | if (i == 1) { |
2186 | fprintf(stderr, " incomplete literal tree\n"); | 2208 | fprintf(stderr, " incomplete distance tree\n"); |
2187 | huft_free(tl); | ||
2188 | } | ||
2189 | return i; /* incomplete code set */ | ||
2190 | } | ||
2191 | bd = dbits; | ||
2192 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) | ||
2193 | { | ||
2194 | if (i == 1) { | ||
2195 | fprintf(stderr, " incomplete distance tree\n"); | ||
2196 | #ifdef PKZIP_BUG_WORKAROUND | 2209 | #ifdef PKZIP_BUG_WORKAROUND |
2197 | i = 0; | 2210 | i = 0; |
2198 | } | 2211 | } |
2199 | #else | 2212 | #else |
2200 | huft_free(td); | 2213 | huft_free(td); |
2201 | } | 2214 | } |
2202 | huft_free(tl); | 2215 | huft_free(tl); |
2203 | return i; /* incomplete code set */ | 2216 | return i; /* incomplete code set */ |
2204 | #endif | 2217 | #endif |
2205 | } | 2218 | } |
2206 | 2219 | ||
2207 | 2220 | ||
2208 | /* decompress until an end-of-block code */ | 2221 | /* decompress until an end-of-block code */ |
2209 | if (inflate_codes(tl, td, bl, bd)) | 2222 | if (inflate_codes(tl, td, bl, bd)) |
2210 | return 1; | 2223 | return 1; |
2211 | 2224 | ||
2212 | 2225 | ||
2213 | /* free the decoding tables, return */ | 2226 | /* free the decoding tables, return */ |
2214 | huft_free(tl); | 2227 | huft_free(tl); |
2215 | huft_free(td); | 2228 | huft_free(td); |
2216 | return 0; | 2229 | return 0; |
2217 | } | 2230 | } |
2218 | 2231 | ||
2219 | 2232 | ||
2220 | 2233 | ||
2221 | int inflate_block(e) | 2234 | int inflate_block(e) |
2222 | int *e; /* last block flag */ | 2235 | int *e; /* last block flag */ |
2236 | |||
2223 | /* decompress an inflated block */ | 2237 | /* decompress an inflated block */ |
2224 | { | 2238 | { |
2225 | unsigned t; /* block type */ | 2239 | unsigned t; /* block type */ |
2226 | register ulg b; /* bit buffer */ | 2240 | register ulg b; /* bit buffer */ |
2227 | register unsigned k; /* number of bits in bit buffer */ | 2241 | register unsigned k; /* number of bits in bit buffer */ |
2228 | 2242 | ||
2229 | 2243 | ||
2230 | /* make local bit buffer */ | 2244 | /* make local bit buffer */ |
2231 | b = bb; | 2245 | b = bb; |
2232 | k = bk; | 2246 | k = bk; |
2233 | 2247 | ||
2234 | 2248 | ||
2235 | /* read in last block bit */ | 2249 | /* read in last block bit */ |
2236 | NEEDBITS(1) | 2250 | NEEDBITS(1) |
2237 | *e = (int)b & 1; | 2251 | * e = (int) b & 1; |
2238 | DUMPBITS(1) | 2252 | DUMPBITS(1) |
2239 | 2253 | ||
2240 | 2254 | ||
2241 | /* read in block type */ | 2255 | /* read in block type */ |
2242 | NEEDBITS(2) | 2256 | NEEDBITS(2) |
2243 | t = (unsigned)b & 3; | 2257 | t = (unsigned) b & 3; |
2244 | DUMPBITS(2) | 2258 | DUMPBITS(2) |
2245 | 2259 | ||
2246 | 2260 | ||
2247 | /* restore the global bit buffer */ | 2261 | /* restore the global bit buffer */ |
2248 | bb = b; | 2262 | bb = b; |
2249 | bk = k; | 2263 | bk = k; |
2250 | 2264 | ||
2251 | 2265 | ||
2252 | /* inflate that block type */ | 2266 | /* inflate that block type */ |
2253 | if (t == 2) | 2267 | if (t == 2) |
2254 | return inflate_dynamic(); | 2268 | return inflate_dynamic(); |
2255 | if (t == 0) | 2269 | if (t == 0) |
2256 | return inflate_stored(); | 2270 | return inflate_stored(); |
2257 | if (t == 1) | 2271 | if (t == 1) |
2258 | return inflate_fixed(); | 2272 | return inflate_fixed(); |
2259 | 2273 | ||
2260 | 2274 | ||
2261 | /* bad block type */ | 2275 | /* bad block type */ |
2262 | return 2; | 2276 | return 2; |
2263 | } | 2277 | } |
2264 | 2278 | ||
2265 | 2279 | ||
@@ -2267,42 +2281,42 @@ int *e; /* last block flag */ | |||
2267 | int inflate() | 2281 | int inflate() |
2268 | /* decompress an inflated entry */ | 2282 | /* decompress an inflated entry */ |
2269 | { | 2283 | { |
2270 | int e; /* last block flag */ | 2284 | int e; /* last block flag */ |
2271 | int r; /* result code */ | 2285 | int r; /* result code */ |
2272 | unsigned h; /* maximum struct huft's malloc'ed */ | 2286 | unsigned h; /* maximum struct huft's malloc'ed */ |
2273 | 2287 | ||
2274 | 2288 | ||
2275 | /* initialize window, bit buffer */ | 2289 | /* initialize window, bit buffer */ |
2276 | wp = 0; | 2290 | wp = 0; |
2277 | bk = 0; | 2291 | bk = 0; |
2278 | bb = 0; | 2292 | bb = 0; |
2279 | 2293 | ||
2280 | 2294 | ||
2281 | /* decompress until the last block */ | 2295 | /* decompress until the last block */ |
2282 | h = 0; | 2296 | h = 0; |
2283 | do { | 2297 | do { |
2284 | hufts = 0; | 2298 | hufts = 0; |
2285 | if ((r = inflate_block(&e)) != 0) | 2299 | if ((r = inflate_block(&e)) != 0) |
2286 | return r; | 2300 | return r; |
2287 | if (hufts > h) | 2301 | if (hufts > h) |
2288 | h = hufts; | 2302 | h = hufts; |
2289 | } while (!e); | 2303 | } while (!e); |
2290 | 2304 | ||
2291 | /* Undo too much lookahead. The next read will be byte aligned so we | 2305 | /* Undo too much lookahead. The next read will be byte aligned so we |
2292 | * can discard unused bits in the last meaningful byte. | 2306 | * can discard unused bits in the last meaningful byte. |
2293 | */ | 2307 | */ |
2294 | while (bk >= 8) { | 2308 | while (bk >= 8) { |
2295 | bk -= 8; | 2309 | bk -= 8; |
2296 | inptr--; | 2310 | inptr--; |
2297 | } | 2311 | } |
2298 | 2312 | ||
2299 | /* flush out slide */ | 2313 | /* flush out slide */ |
2300 | flush_output(wp); | 2314 | flush_output(wp); |
2301 | 2315 | ||
2302 | 2316 | ||
2303 | /* return success */ | 2317 | /* return success */ |
2304 | #ifdef DEBUG | 2318 | #ifdef DEBUG |
2305 | fprintf(stderr, "<%u> ", h); | 2319 | fprintf(stderr, "<%u> ", h); |
2306 | #endif /* DEBUG */ | 2320 | #endif /* DEBUG */ |
2307 | return 0; | 2321 | return 0; |
2308 | } | 2322 | } |