fsck_minix.c lost fat.
[oweals/busybox.git] / gunzip.c
1 /* vi: set sw=4 ts=4: */
2 /* zcat : stripped version based on gzip sources
3    Sven Rudolph <sr1@inf.tu-dresden.de>
4    */
5
6 #include "internal.h"
7 #define bb_need_name_too_long
8 #define BB_DECLARE_EXTERN
9 #include "messages.c"
10
11 static const char gunzip_usage[] =
12         "gunzip [OPTION]... FILE\n\n"
13         "Uncompress FILE (or standard input if FILE is '-').\n\n"
14         "Options:\n"
15
16         "\t-c\tWrite output to standard output\n"
17         "\t-t\tTest compressed file integrity\n";
18
19 /* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
20  * Copyright (C) 1992-1993 Jean-loup Gailly
21  * The unzip code was written and put in the public domain by Mark Adler.
22  * Portions of the lzw code are derived from the public domain 'compress'
23  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
24  * Ken Turkowski, Dave Mack and Peter Jannesen.
25  *
26  * See the license_msg below and the file COPYING for the software license.
27  * See the file algorithm.doc for the compression algorithms and file formats.
28  */
29
30 #if 0
31 static char *license_msg[] = {
32         "   Copyright (C) 1992-1993 Jean-loup Gailly",
33         "   This program is free software; you can redistribute it and/or modify",
34         "   it under the terms of the GNU General Public License as published by",
35         "   the Free Software Foundation; either version 2, or (at your option)",
36         "   any later version.",
37         "",
38         "   This program is distributed in the hope that it will be useful,",
39         "   but WITHOUT ANY WARRANTY; without even the implied warranty of",
40         "   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the",
41         "   GNU General Public License for more details.",
42         "",
43         "   You should have received a copy of the GNU General Public License",
44         "   along with this program; if not, write to the Free Software",
45         "   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
46         0
47 };
48 #endif
49
50 /* Compress files with zip algorithm and 'compress' interface.
51  * See usage() and help() functions below for all options.
52  * Outputs:
53  *        file.gz:   compressed file with same mode, owner, and utimes
54  *     or stdout with -c option or if stdin used as input.
55  * If the output file name had to be truncated, the original name is kept
56  * in the compressed file.
57  * On MSDOS, file.tmp -> file.tmz. On VMS, file.tmp -> file.tmp-gz.
58  *
59  * Using gz on MSDOS would create too many file name conflicts. For
60  * example, foo.txt -> foo.tgz (.tgz must be reserved as shorthand for
61  * tar.gz). Similarly, foo.dir and foo.doc would both be mapped to foo.dgz.
62  * I also considered 12345678.txt -> 12345txt.gz but this truncates the name
63  * too heavily. There is no ideal solution given the MSDOS 8+3 limitation. 
64  *
65  * For the meaning of all compilation flags, see comments in Makefile.in.
66  */
67
68 #include <ctype.h>
69 #include <sys/types.h>
70 #include <signal.h>
71 #include <sys/stat.h>
72 #include <errno.h>
73 #include <sys/param.h>                  /* for PATH_MAX */
74
75 /* #include "tailor.h" */
76
77 /* tailor.h -- target dependent definitions
78  * Copyright (C) 1992-1993 Jean-loup Gailly.
79  * This is free software; you can redistribute it and/or modify it under the
80  * terms of the GNU General Public License, see the file COPYING.
81  */
82
83 /* The target dependent definitions should be defined here only.
84  * The target dependent functions should be defined in tailor.c.
85  */
86
87 #define RECORD_IO 0
88
89 #define get_char() get_byte()
90 #define put_char(c) put_byte(c)
91
92 /* #include "gzip.h" */
93
94 /* gzip.h -- common declarations for all gzip modules
95  * Copyright (C) 1992-1993 Jean-loup Gailly.
96  * This is free software; you can redistribute it and/or modify it under the
97  * terms of the GNU General Public License, see the file COPYING.
98  */
99
100 #if defined(__STDC__) || defined(PROTO)
101 #  define OF(args)  args
102 #else
103 #  define OF(args)  ()
104 #endif
105
106 #ifdef __STDC__
107 typedef void *voidp;
108 #else
109 typedef char *voidp;
110 #endif
111
112 /* I don't like nested includes, but the string and io functions are used
113  * too often
114  */
115 #include <stdio.h>
116 #if !defined(NO_STRING_H) || defined(STDC_HEADERS)
117 #  include <string.h>
118 #  if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
119 #    include <memory.h>
120 #  endif
121 #  define memzero(s, n)     memset ((voidp)(s), 0, (n))
122 #else
123 #  include <strings.h>
124 #  define strchr            index
125 #  define strrchr           rindex
126 #  define memcpy(d, s, n)   bcopy((s), (d), (n))
127 #  define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
128 #  define memzero(s, n)     bzero((s), (n))
129 #endif
130
131 #ifndef RETSIGTYPE
132 #  define RETSIGTYPE void
133 #endif
134
135 #define local static
136
137 typedef unsigned char uch;
138 typedef unsigned short ush;
139 typedef unsigned long ulg;
140
141 /* Return codes from gzip */
142 #define OK      0
143 #define ERROR   1
144 #define WARNING 2
145
146 /* Compression methods (see algorithm.doc) */
147 #define DEFLATED    8
148
149 extern int method;                              /* compression method */
150
151 /* To save memory for 16 bit systems, some arrays are overlaid between
152  * the various modules:
153  * deflate:  prev+head   window      d_buf  l_buf  outbuf
154  * unlzw:    tab_prefix  tab_suffix  stack  inbuf  outbuf
155  * inflate:              window             inbuf
156  * unpack:               window             inbuf  prefix_len
157  * unlzh:    left+right  window      c_table inbuf c_len
158  * For compression, input is done in window[]. For decompression, output
159  * is done in window except for unlzw.
160  */
161
162 #ifndef INBUFSIZ
163 #  ifdef SMALL_MEM
164 #    define INBUFSIZ  0x2000    /* input buffer size */
165 #  else
166 #    define INBUFSIZ  0x8000    /* input buffer size */
167 #  endif
168 #endif
169 #define INBUF_EXTRA  64                 /* required by unlzw() */
170
171 #ifndef OUTBUFSIZ
172 #  ifdef SMALL_MEM
173 #    define OUTBUFSIZ   8192    /* output buffer size */
174 #  else
175 #    define OUTBUFSIZ  16384    /* output buffer size */
176 #  endif
177 #endif
178 #define OUTBUF_EXTRA 2048               /* required by unlzw() */
179
180 #define SMALL_MEM
181
182 #ifndef DIST_BUFSIZE
183 #  ifdef SMALL_MEM
184 #    define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
185 #  else
186 #    define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
187 #  endif
188 #endif
189
190 /*#define DYN_ALLOC*/
191
192 #ifdef DYN_ALLOC
193 #  define EXTERN(type, array)  extern type * array
194 #  define DECLARE(type, array, size)  type * array
195 #  define ALLOC(type, array, size) { \
196       array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
197       if (array == NULL) error("insufficient memory"); \
198    }
199 #  define FREE(array) {if (array != NULL) free(array), array=NULL;}
200 #else
201 #  define EXTERN(type, array)  extern type array[]
202 #  define DECLARE(type, array, size)  type array[size]
203 #  define ALLOC(type, array, size)
204 #  define FREE(array)
205 #endif
206
207 EXTERN(uch, inbuf);                             /* input buffer */
208 EXTERN(uch, outbuf);                    /* output buffer */
209 EXTERN(ush, d_buf);                             /* buffer for distances, see trees.c */
210 EXTERN(uch, window);                    /* Sliding window and suffix table (unlzw) */
211 #define tab_suffix window
212 #ifndef MAXSEG_64K
213 #  define tab_prefix prev               /* hash link (see deflate.c) */
214 #  define head (prev+WSIZE)             /* hash head (see deflate.c) */
215 EXTERN(ush, tab_prefix);                /* prefix code (see unlzw.c) */
216 #else
217 #  define tab_prefix0 prev
218 #  define head tab_prefix1
219 EXTERN(ush, tab_prefix0);               /* prefix for even codes */
220 EXTERN(ush, tab_prefix1);               /* prefix for odd  codes */
221 #endif
222
223 extern unsigned insize;                 /* valid bytes in inbuf */
224 extern unsigned inptr;                  /* index of next byte to be processed in inbuf */
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 */
230
231 extern long ifile_size;                 /* input file size, -1 for devices (debug only) */
232
233 typedef int file_t;                             /* Do not use stdio */
234
235 #define NO_FILE  (-1)                   /* in memory compression */
236
237
238 #define GZIP_MAGIC     "\037\213"       /* Magic header for gzip files, 1F 8B */
239
240 /* gzip flag byte */
241 #define ASCII_FLAG   0x01               /* bit 0 set: file probably ascii text */
242 #define CONTINUATION 0x02               /* bit 1 set: continuation of multi-part gzip file */
243 #define EXTRA_FIELD  0x04               /* bit 2 set: extra field present */
244 #define ORIG_NAME    0x08               /* bit 3 set: original file name present */
245 #define COMMENT      0x10               /* bit 4 set: file comment present */
246 #define ENCRYPTED    0x20               /* bit 5 set: file is encrypted */
247 #define RESERVED     0xC0               /* bit 6,7:   reserved */
248
249 #ifndef WSIZE
250 #  define WSIZE 0x8000                  /* window size--must be a power of two, and */
251 #endif                                                  /*  at least 32K for zip's deflate method */
252
253 #define MIN_MATCH  3
254 #define MAX_MATCH  258
255 /* The minimum and maximum match lengths */
256
257 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
258 /* Minimum amount of lookahead, except at the end of the input file.
259  * See deflate.c for comments about the MIN_MATCH+1.
260  */
261
262 #define MAX_DIST  (WSIZE-MIN_LOOKAHEAD)
263 /* In order to simplify the code, particularly on 16 bit machines, match
264  * distances are limited to MAX_DIST instead of WSIZE.
265  */
266
267 extern int exit_code;                   /* program exit code */
268 extern int verbose;                             /* be verbose (-v) */
269 extern int level;                               /* compression level */
270 extern int test;                                /* check .z file integrity */
271 extern int save_orig_name;              /* set if original name must be saved */
272
273 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
274 #define try_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
275
276 /* put_byte is used for the compressed output, put_ubyte for the
277  * uncompressed output. However unlzw() uses window for its
278  * suffix table instead of its output buffer, so it does not use put_ubyte
279  * (to be cleaned up).
280  */
281 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
282    flush_outbuf();}
283 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
284    flush_window();}
285
286 /* Output a 16 bit value, lsb first */
287 #define put_short(w) \
288 { if (outcnt < OUTBUFSIZ-2) { \
289     outbuf[outcnt++] = (uch) ((w) & 0xff); \
290     outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
291   } else { \
292     put_byte((uch)((w) & 0xff)); \
293     put_byte((uch)((ush)(w) >> 8)); \
294   } \
295 }
296
297 /* Output a 32 bit value to the bit stream, lsb first */
298 #define put_long(n) { \
299     put_short((n) & 0xffff); \
300     put_short(((ulg)(n)) >> 16); \
301 }
302
303 #define seekable()    0                 /* force sequential output */
304 #define translate_eol 0                 /* no option -a yet */
305
306 #define tolow(c)  (isupper(c) ? (c)-'A'+'a' : (c))      /* force to lower case */
307
308 /* Macros for getting two-byte and four-byte header values */
309 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
310 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
311
312 /* Diagnostic functions */
313 #ifdef DEBUG
314 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
315 #  define Trace(x) fprintf x
316 #  define Tracev(x) {if (verbose) fprintf x ;}
317 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
318 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
319 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
320 #else
321 #  define Assert(cond,msg)
322 #  define Trace(x)
323 #  define Tracev(x)
324 #  define Tracevv(x)
325 #  define Tracec(c,x)
326 #  define Tracecv(c,x)
327 #endif
328
329 #define WARN(msg) {fprintf msg ; \
330                    if (exit_code == OK) exit_code = WARNING;}
331
332 #define do_exit(c) exit(c)
333
334
335         /* in unzip.c */
336 extern int unzip OF((int in, int out));
337
338         /* in gzip.c */
339 RETSIGTYPE abort_gzip OF((void));
340
341                 /* in deflate.c */
342 void lm_init OF((int pack_level, ush * flags));
343 ulg deflate OF((void));
344
345                 /* in trees.c */
346 void ct_init OF((ush * attr, int *method));
347 int ct_tally OF((int dist, int lc));
348 ulg flush_block OF((char *buf, ulg stored_len, int eof));
349
350                 /* in bits.c */
351 void bi_init OF((file_t zipfile));
352 void send_bits OF((int value, int length));
353 unsigned bi_reverse OF((unsigned value, int length));
354 void bi_windup OF((void));
355 void copy_block OF((char *buf, unsigned len, int header));
356 extern int (*read_buf) OF((char *buf, unsigned size));
357
358         /* in util.c: */
359 extern int copy OF((int in, int out));
360 extern ulg updcrc OF((uch * s, unsigned n));
361 extern void clear_bufs OF((void));
362 extern int fill_inbuf OF((int eof_ok));
363 extern void flush_outbuf OF((void));
364 extern void flush_window OF((void));
365 extern void write_buf OF((int fd, voidp buf, unsigned cnt));
366
367 #ifndef __linux__
368 extern char *basename OF((char *fname));
369 #endif                                                  /* not __linux__ */
370 extern void error OF((char *m));
371 extern void warn OF((char *a, char *b));
372 extern void read_error OF((void));
373 extern void write_error OF((void));
374
375         /* in inflate.c */
376 extern int inflate OF((void));
377
378 /* #include "lzw.h" */
379
380 /* lzw.h -- define the lzw functions.
381  * Copyright (C) 1992-1993 Jean-loup Gailly.
382  * This is free software; you can redistribute it and/or modify it under the
383  * terms of the GNU General Public License, see the file COPYING.
384  */
385
386 #if !defined(OF) && defined(lint)
387 #  include "gzip.h"
388 #endif
389
390 #ifndef BITS
391 #  define BITS 16
392 #endif
393 #define INIT_BITS 9                             /* Initial number of bits per code */
394
395 #define LZW_MAGIC  "\037\235"   /* Magic header for lzw files, 1F 9D */
396
397 #define BIT_MASK    0x1f                /* Mask for 'number of compression bits' */
398 /* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free.
399  * It's a pity that old uncompress does not check bit 0x20. That makes
400  * extension of the format actually undesirable because old compress
401  * would just crash on the new format instead of giving a meaningful
402  * error message. It does check the number of bits, but it's more
403  * helpful to say "unsupported format, get a new version" than
404  * "can only handle 16 bits".
405  */
406
407 #define BLOCK_MODE  0x80
408 /* Block compression: if table is full and compression rate is dropping,
409  * clear the dictionary.
410  */
411
412 #define LZW_RESERVED 0x60               /* reserved bits */
413
414 #define CLEAR  256                              /* flush the dictionary */
415 #define FIRST  (CLEAR+1)                /* first free entry */
416
417 extern int maxbits;                             /* max bits per code for LZW */
418 extern int block_mode;                  /* block compress mode -C compatible with 2.0 */
419
420 extern int lzw OF((int in, int out));
421 extern int unlzw OF((int in, int out));
422
423
424 /* #include "revision.h" */
425
426 /* revision.h -- define the version number
427  * Copyright (C) 1992-1993 Jean-loup Gailly.
428  * This is free software; you can redistribute it and/or modify it under the
429  * terms of the GNU General Public License, see the file COPYING.
430  */
431
432 #define VERSION "1.2.4"
433 #define PATCHLEVEL 0
434 #define REVDATE "18 Aug 93"
435
436 /* This version does not support compression into old compress format: */
437 #ifdef LZW
438 #  undef LZW
439 #endif
440
441 /* #include "getopt.h" */
442
443 /* Declarations for getopt.
444    Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
445
446    This program is free software; you can redistribute it and/or modify it
447    under the terms of the GNU General Public License as published by the
448    Free Software Foundation; either version 2, or (at your option) any
449    later version.
450
451    This program is distributed in the hope that it will be useful,
452    but WITHOUT ANY WARRANTY; without even the implied warranty of
453    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
454    GNU General Public License for more details.
455
456    You should have received a copy of the GNU General Public License
457    along with this program; if not, write to the Free Software
458    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
459
460 #ifndef _GETOPT_H
461 #define _GETOPT_H 1
462
463 #ifdef  __cplusplus
464 extern "C" {
465 #endif
466 /* For communication from `getopt' to the caller.
467    When `getopt' finds an option that takes an argument,
468    the argument value is returned here.
469    Also, when `ordering' is RETURN_IN_ORDER,
470    each non-option ARGV-element is returned here.  */
471                 extern char *optarg;
472
473 /* Index in ARGV of the next element to be scanned.
474    This is used for communication to and from the caller
475    and for communication between successive calls to `getopt'.
476
477    On entry to `getopt', zero means this is the first call; initialize.
478
479    When `getopt' returns EOF, this is the index of the first of the
480    non-option elements that the caller should itself scan.
481
482    Otherwise, `optind' communicates from one call to the next
483    how much of ARGV has been scanned so far.  */
484
485         extern int optind;
486
487 /* Callers store zero here to inhibit the error message `getopt' prints
488    for unrecognized options.  */
489
490         extern int opterr;
491
492 /* Set to an option character which was unrecognized.  */
493
494         extern int optopt;
495
496 /* Describe the long-named options requested by the application.
497    The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
498    of `struct option' terminated by an element containing a name which is
499    zero.
500
501    The field `has_arg' is:
502    no_argument          (or 0) if the option does not take an argument,
503    required_argument    (or 1) if the option requires an argument,
504    optional_argument    (or 2) if the option takes an optional argument.
505
506    If the field `flag' is not NULL, it points to a variable that is set
507    to the value given in the field `val' when the option is found, but
508    left unchanged if the option is not found.
509
510    To have a long-named option do something other than set an `int' to
511    a compiled-in constant, such as set a value from `optarg', set the
512    option's `flag' field to zero and its `val' field to a nonzero
513    value (the equivalent single-letter option character, if there is
514    one).  For long options that have a zero `flag' field, `getopt'
515    returns the contents of the `val' field.  */
516
517         struct option {
518 #if     __STDC__
519                 const char *name;
520 #else
521                 char *name;
522 #endif
523                 /* has_arg can't be an enum because some compilers complain about
524                    type mismatches in all the code that assumes it is an int.  */
525                 int has_arg;
526                 int *flag;
527                 int val;
528         };
529
530 /* Names for the values of the `has_arg' field of `struct option'.  */
531
532 #define no_argument             0
533 #define required_argument       1
534 #define optional_argument       2
535
536 #if __STDC__ || defined(PROTO)
537 #if defined(__GNU_LIBRARY__)
538 /* Many other libraries have conflicting prototypes for getopt, with
539    differences in the consts, in stdlib.h.  To avoid compilation
540    errors, only prototype getopt for the GNU C library.  */
541         extern int getopt(int argc, char *const *argv, const char *shortopts);
542 #endif                                                  /* not __GNU_LIBRARY__ */
543         extern int getopt_long(int argc, char *const *argv,
544                                                    const char *shortopts,
545                                                    const struct option *longopts, int *longind);
546         extern int getopt_long_only(int argc, char *const *argv,
547                                                                 const char *shortopts,
548                                                                 const struct option *longopts,
549                                                                 int *longind);
550
551 /* Internal only.  Users should not call this directly.  */
552         extern int _getopt_internal(int argc, char *const *argv,
553                                                                 const char *shortopts,
554                                                                 const struct option *longopts,
555                                                                 int *longind, int long_only);
556 #else                                                   /* not __STDC__ */
557         extern int getopt();
558         extern int getopt_long();
559         extern int getopt_long_only();
560
561         extern int _getopt_internal();
562 #endif                                                  /* not __STDC__ */
563
564 #ifdef  __cplusplus
565 }
566 #endif
567 #endif                                                  /* _GETOPT_H */
568 #include <time.h>
569 #include <fcntl.h>
570 #include <unistd.h>
571 #include <stdlib.h>
572 #if defined(DIRENT)
573 #  include <dirent.h>
574 typedef struct dirent dir_type;
575
576 #  define NLENGTH(dirent) ((int)strlen((dirent)->d_name))
577 #  define DIR_OPT "DIRENT"
578 #else
579 #  define NLENGTH(dirent) ((dirent)->d_namlen)
580 #  ifdef SYSDIR
581 #    include <sys/dir.h>
582 typedef struct direct dir_type;
583
584 #    define DIR_OPT "SYSDIR"
585 #  else
586 #    ifdef SYSNDIR
587 #      include <sys/ndir.h>
588 typedef struct direct dir_type;
589
590 #      define DIR_OPT "SYSNDIR"
591 #    else
592 #      ifdef NDIR
593 #        include <ndir.h>
594 typedef struct direct dir_type;
595
596 #        define DIR_OPT "NDIR"
597 #      else
598 #        define NO_DIR
599 #        define DIR_OPT "NO_DIR"
600 #      endif
601 #    endif
602 #  endif
603 #endif
604 #if !defined(S_ISDIR) && defined(S_IFDIR)
605 #  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
606 #endif
607 #if !defined(S_ISREG) && defined(S_IFREG)
608 #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
609 #endif
610 typedef RETSIGTYPE(*sig_type) OF((int));
611
612 #ifndef O_BINARY
613 #  define  O_BINARY  0                  /* creation mode for open() */
614 #endif
615
616 #ifndef O_CREAT
617    /* Pure BSD system? */
618 #  include <sys/file.h>
619 #  ifndef O_CREAT
620 #    define O_CREAT FCREAT
621 #  endif
622 #  ifndef O_EXCL
623 #    define O_EXCL FEXCL
624 #  endif
625 #endif
626
627 #ifndef S_IRUSR
628 #  define S_IRUSR 0400
629 #endif
630 #ifndef S_IWUSR
631 #  define S_IWUSR 0200
632 #endif
633 #define RW_USER (S_IRUSR | S_IWUSR)     /* creation mode for open() */
634
635 #ifndef MAX_PATH_LEN                    /* max pathname length */
636 #  ifdef PATH_MAX
637 #    define MAX_PATH_LEN   PATH_MAX
638 #  else
639 #    define MAX_PATH_LEN   1024
640 #  endif
641 #endif
642
643 #ifndef SEEK_END
644 #  define SEEK_END 2
645 #endif
646
647 #ifdef NO_OFF_T
648 typedef long off_t;
649 off_t lseek OF((int fd, off_t offset, int whence));
650 #endif
651
652
653                 /* global buffers */
654
655 DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
656 DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
657 DECLARE(ush, d_buf, DIST_BUFSIZE);
658 DECLARE(uch, window, 2L * WSIZE);
659 #ifndef MAXSEG_64K
660 DECLARE(ush, tab_prefix, 1L << BITS);
661 #else
662 DECLARE(ush, tab_prefix0, 1L << (BITS - 1));
663 DECLARE(ush, tab_prefix1, 1L << (BITS - 1));
664 #endif
665
666                 /* local variables */
667
668 int test_mode = 0;                              /* check file integrity option */
669 int foreground;                                 /* set if program run in foreground */
670 int maxbits = BITS;                             /* max bits per code for LZW */
671 int method = DEFLATED;                  /* compression method */
672 int exit_code = OK;                             /* program exit code */
673 int last_member;                                /* set for .zip and .Z files */
674 int part_nb;                                    /* number of parts in .gz file */
675 long ifile_size;                                /* input file size, -1 for devices (debug only) */
676
677 long bytes_in;                                  /* number of input bytes */
678 long bytes_out;                                 /* number of output bytes */
679 long total_in = 0;                              /* input bytes for all files */
680 long total_out = 0;                             /* output bytes for all files */
681 struct stat istat;                              /* status for input file */
682 int ifd;                                                /* input file descriptor */
683 int ofd;                                                /* output file descriptor */
684 unsigned insize;                                /* valid bytes in inbuf */
685 unsigned inptr;                                 /* index of next byte to be processed in inbuf */
686 unsigned outcnt;                                /* bytes in output buffer */
687
688 long header_bytes;                              /* number of bytes in gzip header */
689
690 /* local functions */
691
692 local int get_method OF((int in));
693
694 #define strequ(s1, s2) (strcmp((s1),(s2)) == 0)
695
696 /* ======================================================================== */
697 int gunzip_main(int argc, char **argv)
698 {
699         int file_count;                         /* number of files to precess */
700         int to_stdout = 0;
701         int fromstdin = 0;
702         int result;
703         int inFileNum;
704         int outFileNum;
705         int delInputFile = 0;
706         struct stat statBuf;
707         char *delFileName;
708         char ifname[MAX_PATH_LEN + 1];  /* input file name */
709         char ofname[MAX_PATH_LEN + 1];  /* output file name */
710
711         if (argc == 1)
712                 usage(gunzip_usage);
713
714         if (strcmp(*argv, "zcat") == 0)
715                 to_stdout = 1;
716
717         /* Parse any options */
718         while (--argc > 0 && **(++argv) == '-') {
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                 }
736         }
737
738         foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
739         if (foreground) {
740                 (void) signal(SIGINT, (sig_type) abort_gzip);
741         }
742 #ifdef SIGTERM
743         if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
744                 (void) signal(SIGTERM, (sig_type) abort_gzip);
745         }
746 #endif
747 #ifdef SIGHUP
748         if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
749                 (void) signal(SIGHUP, (sig_type) abort_gzip);
750         }
751 #endif
752
753         file_count = argc - optind;
754
755         /* Allocate all global buffers (for DYN_ALLOC option) */
756         ALLOC(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
757         ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
758         ALLOC(ush, d_buf, DIST_BUFSIZE);
759         ALLOC(uch, window, 2L * WSIZE);
760 #ifndef MAXSEG_64K
761         ALLOC(ush, tab_prefix, 1L << BITS);
762 #else
763         ALLOC(ush, tab_prefix0, 1L << (BITS - 1));
764         ALLOC(ush, tab_prefix1, 1L << (BITS - 1));
765 #endif
766
767         if (fromstdin == 1) {
768                 strcpy(ofname, "stdin");
769
770                 inFileNum = fileno(stdin);
771                 ifile_size = -1L;               /* convention for unknown size */
772         } else {
773                 /* Open up the input file */
774                 if (*argv == '\0')
775                         usage(gunzip_usage);
776                 if (strlen(*argv) > MAX_PATH_LEN) {
777                         fprintf(stderr, name_too_long, "gunzip");
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;
795         }
796
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 */
834 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
835                 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
836 #else
837                 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
838 #endif
839                 if (outFileNum < 0) {
840                         perror(ofname);
841                         do_exit(WARNING);
842                 }
843                 /* Set permissions on the file */
844                 fchmod(outFileNum, statBuf.st_mode);
845
846                 clear_bufs();                   /* clear input and output buffers */
847                 part_nb = 0;
848
849                 /* Actually do the compression/decompression. */
850                 result = unzip(inFileNum, outFileNum);
851
852                 close(outFileNum);
853                 close(inFileNum);
854                 /* Delete the original file */
855                 if (result == OK)
856                         delFileName = ifname;
857                 else
858                         delFileName = ofname;
859
860                 if (delInputFile == 1 && unlink(delFileName) < 0) {
861                         perror(delFileName);
862                         exit(FALSE);
863                 }
864         }
865         do_exit(exit_code);
866 }
867
868
869 /* ========================================================================
870  * Check the magic number of the input file and update ofname if an
871  * original name was given and to_stdout is not set.
872  * Return the compression method, -1 for error, -2 for warning.
873  * Set inptr to the offset of the next byte to be processed.
874  * Updates time_stamp if there is one and --no-time is not used.
875  * This function may be called repeatedly for an input file consisting
876  * of several contiguous gzip'ed members.
877  * IN assertions: there is at least one remaining compressed member.
878  *   If the member is a zip file, it must be the only one.
879  */
880 local int get_method(in)
881 int in;                                                 /* input file descriptor */
882 {
883         uch flags;                                      /* compression flags */
884         char magic[2];                          /* magic header */
885
886         magic[0] = (char) get_byte();
887         magic[1] = (char) get_byte();
888         method = -1;                            /* unknown yet */
889         part_nb++;                                      /* number of parts in gzip file */
890         header_bytes = 0;
891         last_member = RECORD_IO;
892         /* assume multiple members in gzip file except for record oriented I/O */
893
894         if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
895
896                 method = (int) get_byte();
897                 if (method != DEFLATED) {
898                         fprintf(stderr,
899                                         "unknown method %d -- get newer version of gzip\n",
900                                         method);
901                         exit_code = ERROR;
902                         return -1;
903                 }
904                 flags = (uch) get_byte();
905
906                 (ulg) get_byte();               /* Ignore time stamp */
907                 (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                 }
937
938         }
939
940         if (method >= 0)
941                 return method;
942
943         if (part_nb == 1) {
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;
950         }
951 }
952
953 /* ========================================================================
954  * Signal and error handler.
955  */
956 RETSIGTYPE abort_gzip()
957 {
958         do_exit(ERROR);
959 }
960
961 /* unzip.c -- decompress files in gzip or pkzip format.
962  * Copyright (C) 1992-1993 Jean-loup Gailly
963  * This is free software; you can redistribute it and/or modify it under the
964  * terms of the GNU General Public License, see the file COPYING.
965  *
966  * The code in this file is derived from the file funzip.c written
967  * and put in the public domain by Mark Adler.
968  */
969
970 /*
971    This version can extract files in gzip or pkzip format.
972    For the latter, only the first entry is extracted, and it has to be
973    either deflated or stored.
974  */
975
976 /* #include "crypt.h" */
977
978 /* crypt.h (dummy version) -- do not perform encryption
979  * Hardly worth copyrighting :-)
980  */
981
982 #ifdef CRYPT
983 #  undef CRYPT                                  /* dummy version */
984 #endif
985
986 #define RAND_HEAD_LEN  12               /* length of encryption random header */
987
988 #define zencode
989 #define zdecode
990
991 /* PKZIP header definitions */
992 #define LOCSIG 0x04034b50L              /* four-byte lead-in (lsb first) */
993 #define LOCFLG 6                                /* offset of bit flag */
994 #define  CRPFLG 1                               /*  bit for encrypted entry */
995 #define  EXTFLG 8                               /*  bit for extended local header */
996 #define LOCHOW 8                                /* offset of compression method */
997 #define LOCTIM 10                               /* file mod time (for decryption) */
998 #define LOCCRC 14                               /* offset of crc */
999 #define LOCSIZ 18                               /* offset of compressed size */
1000 #define LOCLEN 22                               /* offset of uncompressed length */
1001 #define LOCFIL 26                               /* offset of file name field length */
1002 #define LOCEXT 28                               /* offset of extra field length */
1003 #define LOCHDR 30                               /* size of local header, including sig */
1004 #define EXTHDR 16                               /* size of extended local header, inc sig */
1005
1006
1007 /* Globals */
1008
1009 char *key;                                              /* not used--needed to link crypt.c */
1010 int pkzip = 0;                                  /* set for a pkzip file */
1011 int ext_header = 0;                             /* set if extended local header */
1012
1013 /* ===========================================================================
1014  * Unzip in to out.  This routine works on both gzip and pkzip files.
1015  *
1016  * IN assertions: the buffer inbuf contains already the beginning of
1017  *   the compressed data, from offsets inptr to insize-1 included.
1018  *   The magic header has already been checked. The output buffer is cleared.
1019  */
1020 int unzip(in, out)
1021 int in, out;                                    /* input and output file descriptors */
1022 {
1023         ulg orig_crc = 0;                       /* original crc */
1024         ulg orig_len = 0;                       /* original uncompressed length */
1025         int n;
1026         uch buf[EXTHDR];                        /* extended local header */
1027
1028         ifd = in;
1029         ofd = out;
1030         method = get_method(ifd);
1031         if (method < 0) {
1032                 do_exit(exit_code);             /* error message already emitted */
1033         }
1034
1035         updcrc(NULL, 0);                        /* initialize crc */
1036
1037         if (pkzip && !ext_header) {     /* crc and length at the end otherwise */
1038                 orig_crc = LG(inbuf + LOCCRC);
1039                 orig_len = LG(inbuf + LOCLEN);
1040         }
1041
1042         /* Decompress */
1043         if (method == DEFLATED) {
1044
1045                 int res = inflate();
1046
1047                 if (res == 3) {
1048                         error("out of memory");
1049                 } else if (res != 0) {
1050                         error("invalid compressed data--format violated");
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");
1084         }
1085         if (orig_len != (ulg) bytes_out) {
1086                 error("invalid compressed data--length error");
1087         }
1088
1089         /* Check if there are more entries in a pkzip file */
1090         if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) {
1091                 WARN((stderr, "has more than one entry--rest ignored\n"));
1092         }
1093         ext_header = pkzip = 0;         /* for next file */
1094         return OK;
1095 }
1096
1097 /* util.c -- utility functions for gzip support
1098  * Copyright (C) 1992-1993 Jean-loup Gailly
1099  * This is free software; you can redistribute it and/or modify it under the
1100  * terms of the GNU General Public License, see the file COPYING.
1101  */
1102
1103 #include <ctype.h>
1104 #include <errno.h>
1105 #include <sys/types.h>
1106
1107 #ifdef HAVE_UNISTD_H
1108 #  include <unistd.h>
1109 #endif
1110 #ifndef NO_FCNTL_H
1111 #  include <fcntl.h>
1112 #endif
1113
1114 #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
1115 #  include <stdlib.h>
1116 #else
1117 extern int errno;
1118 #endif
1119
1120 static const ulg crc_32_tab[];  /* crc table, defined below */
1121
1122 /* ===========================================================================
1123  * Run a set of bytes through the crc shift register.  If s is a NULL
1124  * pointer, then initialize the crc shift register contents instead.
1125  * Return the current crc in either case.
1126  */
1127 ulg updcrc(s, n)
1128 uch *s;                                                 /* pointer to bytes to pump through */
1129 unsigned n;                                             /* number of bytes in s[] */
1130 {
1131         register ulg c;                         /* temporary variable */
1132
1133         static ulg crc = (ulg) 0xffffffffL;     /* shift register contents */
1134
1135         if (s == NULL) {
1136                 c = 0xffffffffL;
1137         } else {
1138                 c = crc;
1139                 if (n)
1140                         do {
1141                                 c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8);
1142                         } while (--n);
1143         }
1144         crc = c;
1145         return c ^ 0xffffffffL;         /* (instead of ~c for 64-bit machines) */
1146 }
1147
1148 /* ===========================================================================
1149  * Clear input and output buffers
1150  */
1151 void clear_bufs()
1152 {
1153         outcnt = 0;
1154         insize = inptr = 0;
1155         bytes_in = bytes_out = 0L;
1156 }
1157
1158 /* ===========================================================================
1159  * Fill the input buffer. This is called only when the buffer is empty.
1160  */
1161 int fill_inbuf(eof_ok)
1162 int eof_ok;                                             /* set if EOF acceptable as a result */
1163 {
1164         int len;
1165
1166         /* Read as much as possible */
1167         insize = 0;
1168         errno = 0;
1169         do {
1170                 len = read(ifd, (char *) inbuf + insize, INBUFSIZ - insize);
1171                 if (len == 0 || len == EOF)
1172                         break;
1173                 insize += len;
1174         } while (insize < INBUFSIZ);
1175
1176         if (insize == 0) {
1177                 if (eof_ok)
1178                         return EOF;
1179                 read_error();
1180         }
1181         bytes_in += (ulg) insize;
1182         inptr = 1;
1183         return inbuf[0];
1184 }
1185
1186 /* ===========================================================================
1187  * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
1188  * (used for the compressed data only)
1189  */
1190 void flush_outbuf()
1191 {
1192         if (outcnt == 0)
1193                 return;
1194
1195         if (!test_mode)
1196                 write_buf(ofd, (char *) outbuf, outcnt);
1197         bytes_out += (ulg) outcnt;
1198         outcnt = 0;
1199 }
1200
1201 /* ===========================================================================
1202  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
1203  * (Used for the decompressed data only.)
1204  */
1205 void flush_window()
1206 {
1207         if (outcnt == 0)
1208                 return;
1209         updcrc(window, outcnt);
1210
1211         if (!test_mode)
1212                 write_buf(ofd, (char *) window, outcnt);
1213         bytes_out += (ulg) outcnt;
1214         outcnt = 0;
1215 }
1216
1217 /* ===========================================================================
1218  * Does the same as write(), but also handles partial pipe writes and checks
1219  * for error return.
1220  */
1221 void write_buf(fd, buf, cnt)
1222 int fd;
1223 voidp buf;
1224 unsigned cnt;
1225 {
1226         unsigned n;
1227
1228         while ((n = write(fd, buf, cnt)) != cnt) {
1229                 if (n == (unsigned) (-1)) {
1230                         write_error();
1231                 }
1232                 cnt -= n;
1233                 buf = (voidp) ((char *) buf + n);
1234         }
1235 }
1236
1237 #if defined(NO_STRING_H) && !defined(STDC_HEADERS)
1238
1239 /* Provide missing strspn and strcspn functions. */
1240
1241 #  ifndef __STDC__
1242 #    define const
1243 #  endif
1244
1245 int strspn OF((const char *s, const char *accept));
1246 int strcspn OF((const char *s, const char *reject));
1247
1248 /* ========================================================================
1249  * Return the length of the maximum initial segment
1250  * of s which contains only characters in accept.
1251  */
1252 int strspn(s, accept)
1253 const char *s;
1254 const char *accept;
1255 {
1256         register const char *p;
1257         register const char *a;
1258         register int count = 0;
1259
1260         for (p = s; *p != '\0'; ++p) {
1261                 for (a = accept; *a != '\0'; ++a) {
1262                         if (*p == *a)
1263                                 break;
1264                 }
1265                 if (*a == '\0')
1266                         return count;
1267                 ++count;
1268         }
1269         return count;
1270 }
1271
1272 /* ========================================================================
1273  * Return the length of the maximum inital segment of s
1274  * which contains no characters from reject.
1275  */
1276 int strcspn(s, reject)
1277 const char *s;
1278 const char *reject;
1279 {
1280         register int count = 0;
1281
1282         while (*s != '\0') {
1283                 if (strchr(reject, *s++) != NULL)
1284                         return count;
1285                 ++count;
1286         }
1287         return count;
1288 }
1289
1290 #endif                                                  /* NO_STRING_H */
1291
1292
1293 /* ========================================================================
1294  * Error handlers.
1295  */
1296 void warn(a, b)
1297 char *a, *b;                                    /* message strings juxtaposed in output */
1298 {
1299         WARN((stderr, "warning: %s%s\n", a, b));
1300 }
1301
1302 void read_error()
1303 {
1304         fprintf(stderr, "\n");
1305         if (errno != 0) {
1306                 perror("");
1307         } else {
1308                 fprintf(stderr, "unexpected end of file\n");
1309         }
1310         abort_gzip();
1311 }
1312
1313 void write_error()
1314 {
1315         fprintf(stderr, "\n");
1316         perror("");
1317         abort_gzip();
1318 }
1319
1320
1321 /* ========================================================================
1322  * Table of CRC-32's of all single-byte values (made by makecrc.c)
1323  */
1324 static const ulg crc_32_tab[] = {
1325         0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
1326         0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
1327         0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
1328         0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
1329         0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
1330         0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
1331         0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
1332         0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
1333         0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
1334         0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
1335         0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
1336         0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
1337         0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
1338         0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
1339         0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
1340         0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
1341         0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
1342         0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
1343         0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
1344         0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
1345         0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
1346         0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
1347         0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
1348         0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
1349         0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
1350         0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
1351         0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
1352         0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
1353         0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
1354         0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
1355         0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
1356         0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
1357         0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
1358         0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
1359         0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
1360         0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
1361         0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
1362         0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
1363         0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
1364         0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
1365         0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
1366         0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
1367         0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
1368         0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
1369         0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
1370         0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
1371         0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
1372         0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
1373         0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
1374         0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
1375         0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
1376         0x2d02ef8dL
1377 };
1378
1379 /* inflate.c -- Not copyrighted 1992 by Mark Adler
1380    version c10p1, 10 January 1993 */
1381
1382 /* You can do whatever you like with this source file, though I would
1383    prefer that if you modify it and redistribute it that you include
1384    comments to that effect with your name and the date.  Thank you.
1385    [The history has been moved to the file ChangeLog.]
1386  */
1387
1388 /*
1389    Inflate deflated (PKZIP's method 8 compressed) data.  The compression
1390    method searches for as much of the current string of bytes (up to a
1391    length of 258) in the previous 32K bytes.  If it doesn't find any
1392    matches (of at least length 3), it codes the next byte.  Otherwise, it
1393    codes the length of the matched string and its distance backwards from
1394    the current position.  There is a single Huffman code that codes both
1395    single bytes (called "literals") and match lengths.  A second Huffman
1396    code codes the distance information, which follows a length code.  Each
1397    length or distance code actually represents a base value and a number
1398    of "extra" (sometimes zero) bits to get to add to the base value.  At
1399    the end of each deflated block is a special end-of-block (EOB) literal/
1400    length code.  The decoding process is basically: get a literal/length
1401    code; if EOB then done; if a literal, emit the decoded byte; if a
1402    length then get the distance and emit the referred-to bytes from the
1403    sliding window of previously emitted data.
1404
1405    There are (currently) three kinds of inflate blocks: stored, fixed, and
1406    dynamic.  The compressor deals with some chunk of data at a time, and
1407    decides which method to use on a chunk-by-chunk basis.  A chunk might
1408    typically be 32K or 64K.  If the chunk is uncompressible, then the
1409    "stored" method is used.  In this case, the bytes are simply stored as
1410    is, eight bits per byte, with none of the above coding.  The bytes are
1411    preceded by a count, since there is no longer an EOB code.
1412
1413    If the data is compressible, then either the fixed or dynamic methods
1414    are used.  In the dynamic method, the compressed data is preceded by
1415    an encoding of the literal/length and distance Huffman codes that are
1416    to be used to decode this block.  The representation is itself Huffman
1417    coded, and so is preceded by a description of that code.  These code
1418    descriptions take up a little space, and so for small blocks, there is
1419    a predefined set of codes, called the fixed codes.  The fixed method is
1420    used if the block codes up smaller that way (usually for quite small
1421    chunks), otherwise the dynamic method is used.  In the latter case, the
1422    codes are customized to the probabilities in the current block, and so
1423    can code it much better than the pre-determined fixed codes.
1424  
1425    The Huffman codes themselves are decoded using a mutli-level table
1426    lookup, in order to maximize the speed of decoding plus the speed of
1427    building the decoding tables.  See the comments below that precede the
1428    lbits and dbits tuning parameters.
1429  */
1430
1431
1432 /*
1433    Notes beyond the 1.93a appnote.txt:
1434
1435    1. Distance pointers never point before the beginning of the output
1436       stream.
1437    2. Distance pointers can point back across blocks, up to 32k away.
1438    3. There is an implied maximum of 7 bits for the bit length table and
1439       15 bits for the actual data.
1440    4. If only one code exists, then it is encoded using one bit.  (Zero
1441       would be more efficient, but perhaps a little confusing.)  If two
1442       codes exist, they are coded using one bit each (0 and 1).
1443    5. There is no way of sending zero distance codes--a dummy must be
1444       sent if there are none.  (History: a pre 2.0 version of PKZIP would
1445       store blocks with no distance codes, but this was discovered to be
1446       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
1447       zero distance codes, which is sent as one code of zero bits in
1448       length.
1449    6. There are up to 286 literal/length codes.  Code 256 represents the
1450       end-of-block.  Note however that the static length tree defines
1451       288 codes just to fill out the Huffman codes.  Codes 286 and 287
1452       cannot be used though, since there is no length base or extra bits
1453       defined for them.  Similarly, there are up to 30 distance codes.
1454       However, static trees define 32 codes (all 5 bits) to fill out the
1455       Huffman codes, but the last two had better not show up in the data.
1456    7. Unzip can check dynamic Huffman blocks for complete code sets.
1457       The exception is that a single code would not be complete (see #4).
1458    8. The five bits following the block type is really the number of
1459       literal codes sent minus 257.
1460    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
1461       (1+6+6).  Therefore, to output three times the length, you output
1462       three codes (1+1+1), whereas to output four times the same length,
1463       you only need two codes (1+3).  Hmm.
1464   10. In the tree reconstruction algorithm, Code = Code + Increment
1465       only if BitLength(i) is not zero.  (Pretty obvious.)
1466   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
1467   12. Note: length code 284 can represent 227-258, but length code 285
1468       really is 258.  The last length deserves its own, short code
1469       since it gets used a lot in very redundant files.  The length
1470       258 is special since 258 - 3 (the min match length) is 255.
1471   13. The literal/length and distance code bit lengths are read as a
1472       single stream of lengths.  It is possible (and advantageous) for
1473       a repeat code (16, 17, or 18) to go across the boundary between
1474       the two sets of lengths.
1475  */
1476
1477 #include <sys/types.h>
1478
1479 #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
1480 #  include <stdlib.h>
1481 #endif
1482
1483
1484 #define slide window
1485
1486 /* Huffman code lookup table entry--this entry is four bytes for machines
1487    that have 16-bit pointers (e.g. PC's in the small or medium model).
1488    Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
1489    means that v is a literal, 16 < e < 32 means that v is a pointer to
1490    the next table, which codes e - 16 bits, and lastly e == 99 indicates
1491    an unused code.  If a code with e == 99 is looked up, this implies an
1492    error in the data. */
1493 struct huft {
1494         uch e;                                          /* number of extra bits or operation */
1495         uch b;                                          /* number of bits in this code or subcode */
1496         union {
1497                 ush n;                                  /* literal, length base, or distance base */
1498                 struct huft *t;                 /* pointer to next level of table */
1499         } v;
1500 };
1501
1502
1503 /* Function prototypes */
1504 int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
1505                                    struct huft **, int *));
1506 int huft_free OF((struct huft *));
1507 int inflate_codes OF((struct huft *, struct huft *, int, int));
1508 int inflate_stored OF((void));
1509 int inflate_fixed OF((void));
1510 int inflate_dynamic OF((void));
1511 int inflate_block OF((int *));
1512 int inflate OF((void));
1513
1514
1515 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
1516    stream to find repeated byte strings.  This is implemented here as a
1517    circular buffer.  The index is updated simply by incrementing and then
1518    and'ing with 0x7fff (32K-1). */
1519 /* It is left to other modules to supply the 32K area.  It is assumed
1520    to be usable as if it were declared "uch slide[32768];" or as just
1521    "uch *slide;" and then malloc'ed in the latter case.  The definition
1522    must be in unzip.h, included above. */
1523 /* unsigned wp;             current position in slide */
1524 #define wp outcnt
1525 #define flush_output(w) (wp=(w),flush_window())
1526
1527 /* Tables for deflate from PKZIP's appnote.txt. */
1528 static unsigned border[] = {    /* Order of the bit length code lengths */
1529         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
1530 };
1531 static ush cplens[] = {                 /* Copy lengths for literal codes 257..285 */
1532         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1533         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
1534 };
1535
1536                 /* note: see note #13 above about the 258 in this list. */
1537 static ush cplext[] = {                 /* Extra bits for literal codes 257..285 */
1538         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1539         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
1540 };                                                              /* 99==invalid */
1541 static ush cpdist[] = {                 /* Copy offsets for distance codes 0..29 */
1542         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1543         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
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 };
1551
1552
1553
1554 /* Macros for inflate() bit peeking and grabbing.
1555    The usage is:
1556    
1557         NEEDBITS(j)
1558         x = b & mask_bits[j];
1559         DUMPBITS(j)
1560
1561    where NEEDBITS makes sure that b has at least j bits in it, and
1562    DUMPBITS removes the bits from b.  The macros use the variable k
1563    for the number of bits in b.  Normally, b and k are register
1564    variables for speed, and are initialized at the beginning of a
1565    routine that uses these macros from a global bit buffer and count.
1566
1567    If we assume that EOB will be the longest code, then we will never
1568    ask for bits with NEEDBITS that are beyond the end of the stream.
1569    So, NEEDBITS should not read any more bytes than are needed to
1570    meet the request.  Then no bytes need to be "returned" to the buffer
1571    at the end of the last block.
1572
1573    However, this assumption is not true for fixed blocks--the EOB code
1574    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
1575    (The EOB code is shorter than other codes because fixed blocks are
1576    generally short.  So, while a block always has an EOB, many other
1577    literal/length codes have a significantly lower probability of
1578    showing up at all.)  However, by making the first table have a
1579    lookup of seven bits, the EOB code will be found in that first
1580    lookup, and so will not require that too many bits be pulled from
1581    the stream.
1582  */
1583
1584 ulg bb;                                                 /* bit buffer */
1585 unsigned bk;                                    /* bits in bit buffer */
1586
1587 ush mask_bits[] = {
1588         0x0000,
1589         0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
1590         0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
1591 };
1592
1593 #ifdef CRYPT
1594 uch cc;
1595
1596 #  define NEXTBYTE() (cc = get_byte(), zdecode(cc), cc)
1597 #else
1598 #  define NEXTBYTE()  (uch)get_byte()
1599 #endif
1600 #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
1601 #define DUMPBITS(n) {b>>=(n);k-=(n);}
1602
1603
1604 /*
1605    Huffman code decoding is performed using a multi-level table lookup.
1606    The fastest way to decode is to simply build a lookup table whose
1607    size is determined by the longest code.  However, the time it takes
1608    to build this table can also be a factor if the data being decoded
1609    is not very long.  The most common codes are necessarily the
1610    shortest codes, so those codes dominate the decoding time, and hence
1611    the speed.  The idea is you can have a shorter table that decodes the
1612    shorter, more probable codes, and then point to subsidiary tables for
1613    the longer codes.  The time it costs to decode the longer codes is
1614    then traded against the time it takes to make longer tables.
1615
1616    This results of this trade are in the variables lbits and dbits
1617    below.  lbits is the number of bits the first level table for literal/
1618    length codes can decode in one step, and dbits is the same thing for
1619    the distance codes.  Subsequent tables are also less than or equal to
1620    those sizes.  These values may be adjusted either when all of the
1621    codes are shorter than that, in which case the longest code length in
1622    bits is used, or when the shortest code is *longer* than the requested
1623    table size, in which case the length of the shortest code in bits is
1624    used.
1625
1626    There are two different values for the two tables, since they code a
1627    different number of possibilities each.  The literal/length table
1628    codes 286 possible values, or in a flat code, a little over eight
1629    bits.  The distance table codes 30 possible values, or a little less
1630    than five bits, flat.  The optimum values for speed end up being
1631    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1632    The optimum values may differ though from machine to machine, and
1633    possibly even between compilers.  Your mileage may vary.
1634  */
1635
1636
1637 int lbits = 9;                                  /* bits in base literal/length lookup table */
1638 int dbits = 6;                                  /* bits in base distance lookup table */
1639
1640
1641 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
1642 #define BMAX 16                                 /* maximum bit length of any code (16 for explode) */
1643 #define N_MAX 288                               /* maximum number of codes in any set */
1644
1645
1646 unsigned hufts;                                 /* track memory usage */
1647
1648
1649 int huft_build(b, n, s, d, e, t, m)
1650 unsigned *b;                                    /* code lengths in bits (all assumed <= BMAX) */
1651 unsigned n;                                             /* number of codes (assumed <= N_MAX) */
1652 unsigned s;                                             /* number of simple-valued codes (0..s-1) */
1653 ush *d;                                                 /* list of base values for non-simple codes */
1654 ush *e;                                                 /* list of extra bits for non-simple codes */
1655 struct huft **t;                                /* result: starting table */
1656 int *m;                                                 /* maximum lookup bits, returns actual */
1657
1658 /* Given a list of code lengths and a maximum table size, make a set of
1659    tables to decode that set of codes.  Return zero on success, one if
1660    the given code set is incomplete (the tables are still built in this
1661    case), two if the input is invalid (all zero length codes or an
1662    oversubscribed set of lengths), and three if not enough memory. */
1663 {
1664         unsigned a;                                     /* counter for codes of length k */
1665         unsigned c[BMAX + 1];           /* bit length count table */
1666         unsigned f;                                     /* i repeats in table every f entries */
1667         int g;                                          /* maximum code length */
1668         int h;                                          /* table level */
1669         register unsigned i;            /* counter, current code */
1670         register unsigned j;            /* counter */
1671         register int k;                         /* number of bits in current code */
1672         int l;                                          /* bits per table (returned in m) */
1673         register unsigned *p;           /* pointer into c[], b[], or v[] */
1674         register struct huft *q;        /* points to current table */
1675         struct huft r;                          /* table entry for structure assignment */
1676         struct huft *u[BMAX];           /* table stack */
1677         unsigned v[N_MAX];                      /* values in order of bit length */
1678         register int w;                         /* bits before this table == (l * h) */
1679         unsigned x[BMAX + 1];           /* bit offsets, then code stack */
1680         unsigned *xp;                           /* pointer into x */
1681         int y;                                          /* number of dummy codes added */
1682         unsigned z;                                     /* number of entries in current table */
1683
1684
1685         /* Generate counts for each bit length */
1686         memzero(c, sizeof(c));
1687         p = b;
1688         i = n;
1689         do {
1690                 Tracecv(*p,
1691                                 (stderr,
1692                                  (n - i >= ' '
1693                                   && n - i <= '~' ? "%c %d\n" : "0x%x %d\n"), n - i, *p));
1694                 c[*p]++;                                /* assume all entries <= BMAX */
1695                 p++;                                    /* Can't combine with above line (Solaris bug) */
1696         } while (--i);
1697         if (c[0] == n) {                        /* null input--all zero length codes */
1698                 *t = (struct huft *) NULL;
1699                 *m = 0;
1700                 return 0;
1701         }
1702
1703
1704         /* Find minimum and maximum length, bound *m by those */
1705         l = *m;
1706         for (j = 1; j <= BMAX; j++)
1707                 if (c[j])
1708                         break;
1709         k = j;                                          /* minimum code length */
1710         if ((unsigned) l < j)
1711                 l = j;
1712         for (i = BMAX; i; i--)
1713                 if (c[i])
1714                         break;
1715         g = i;                                          /* maximum code length */
1716         if ((unsigned) l > i)
1717                 l = i;
1718         *m = l;
1719
1720
1721         /* Adjust last length count to fill out codes, if needed */
1722         for (y = 1 << j; j < i; j++, y <<= 1)
1723                 if ((y -= c[j]) < 0)
1724                         return 2;                       /* bad input: more codes than bits */
1725         if ((y -= c[i]) < 0)
1726                 return 2;
1727         c[i] += y;
1728
1729
1730         /* Generate starting offsets into the value table for each length */
1731         x[1] = j = 0;
1732         p = c + 1;
1733         xp = x + 2;
1734         while (--i) {                           /* note that i == g from above */
1735                 *xp++ = (j += *p++);
1736         }
1737
1738
1739         /* Make a table of values in order of bit lengths */
1740         p = b;
1741         i = 0;
1742         do {
1743                 if ((j = *p++) != 0)
1744                         v[x[j]++] = i;
1745         } while (++i < n);
1746
1747
1748         /* Generate the Huffman codes and for each, make the table entries */
1749         x[0] = i = 0;                           /* first Huffman code is zero */
1750         p = v;                                          /* grab values in bit order */
1751         h = -1;                                         /* no tables yet--level -1 */
1752         w = -l;                                         /* bits decoded == (l * h) */
1753         u[0] = (struct huft *) NULL;    /* just to keep compilers happy */
1754         q = (struct huft *) NULL;       /* ditto */
1755         z = 0;                                          /* ditto */
1756
1757         /* go through the bit lengths (k already is bits in shortest code) */
1758         for (; k <= g; k++) {
1759                 a = c[k];
1760                 while (a--) {
1761                         /* here i is the Huffman code of length k bits for value *p */
1762                         /* make tables up to required level */
1763                         while (k > w + l) {
1764                                 h++;
1765                                 w += l;                 /* previous table always l bits */
1766
1767                                 /* compute minimum size table less than or equal to l bits */
1768                                 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
1769                                 if ((f = 1 << (j = k - w)) > a + 1) {   /* try a k-w bit table *//* too few codes for k-w bit table */
1770                                         f -= a + 1;     /* deduct codes from patterns left */
1771                                         xp = c + k;
1772                                         while (++j < z) {       /* try smaller tables up to z bits */
1773                                                 if ((f <<= 1) <= *++xp)
1774                                                         break;  /* enough codes to use up j bits */
1775                                                 f -= *xp;       /* else deduct codes from patterns */
1776                                         }
1777                                 }
1778                                 z = 1 << j;             /* table entries for j-bit table */
1779
1780                                 /* allocate and link in new table */
1781                                 if (
1782                                         (q =
1783                                          (struct huft *) malloc((z + 1) *
1784                                                                                         sizeof(struct huft))) ==
1785                                         (struct huft *) NULL) {
1786                                         if (h)
1787                                                 huft_free(u[0]);
1788                                         return 3;       /* not enough memory */
1789                                 }
1790                                 hufts += z + 1; /* track memory usage */
1791                                 *t = q + 1;             /* link to list for huft_free() */
1792                                 *(t = &(q->v.t)) = (struct huft *) NULL;
1793                                 u[h] = ++q;             /* table starts after link */
1794
1795                                 /* connect to last table, if there is one */
1796                                 if (h) {
1797                                         x[h] = i;       /* save pattern for backing up */
1798                                         r.b = (uch) l;  /* bits to dump before this table */
1799                                         r.e = (uch) (16 + j);   /* bits in this table */
1800                                         r.v.t = q;      /* pointer to this table */
1801                                         j = i >> (w - l);       /* (get around Turbo C bug) */
1802                                         u[h - 1][j] = r;        /* connect to last table */
1803                                 }
1804                         }
1805
1806                         /* set up table entry in r */
1807                         r.b = (uch) (k - w);
1808                         if (p >= v + n)
1809                                 r.e = 99;               /* out of values--invalid code */
1810                         else if (*p < s) {
1811                                 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 */
1813                                 p++;                    /* one compiler does not like *p++ */
1814                         } else {
1815                                 r.e = (uch) e[*p - s];  /* non-simple--look up in lists */
1816                                 r.v.n = d[*p++ - s];
1817                         }
1818
1819                         /* fill code-like entries with r */
1820                         f = 1 << (k - w);
1821                         for (j = i >> w; j < z; j += f)
1822                                 q[j] = r;
1823
1824                         /* backwards increment the k-bit code i */
1825                         for (j = 1 << (k - 1); i & j; j >>= 1)
1826                                 i ^= j;
1827                         i ^= j;
1828
1829                         /* backup over finished tables */
1830                         while ((i & ((1 << w) - 1)) != x[h]) {
1831                                 h--;                    /* don't need to update q */
1832                                 w -= l;
1833                         }
1834                 }
1835         }
1836
1837
1838         /* Return true (1) if we were given an incomplete table */
1839         return y != 0 && g != 1;
1840 }
1841
1842
1843
1844 int huft_free(t)
1845 struct huft *t;                                 /* table to free */
1846
1847 /* Free the malloc'ed tables built by huft_build(), which makes a linked
1848    list of the tables it made, with the links in a dummy first entry of
1849    each table. */
1850 {
1851         register struct huft *p, *q;
1852
1853
1854         /* Go through linked list, freeing from the malloced (t[-1]) address. */
1855         p = t;
1856         while (p != (struct huft *) NULL) {
1857                 q = (--p)->v.t;
1858                 free((char *) p);
1859                 p = q;
1860         }
1861         return 0;
1862 }
1863
1864
1865 int inflate_codes(tl, td, bl, bd)
1866 struct huft *tl, *td;                   /* literal/length and distance decoder tables */
1867 int bl, bd;                                             /* number of bits decoded by tl[] and td[] */
1868
1869 /* inflate (decompress) the codes in a deflated (compressed) block.
1870    Return an error code or zero if it all goes ok. */
1871 {
1872         register unsigned e;            /* table entry flag/number of extra bits */
1873         unsigned n, d;                          /* length and index for copy */
1874         unsigned w;                                     /* current window position */
1875         struct huft *t;                         /* pointer to table entry */
1876         unsigned ml, md;                        /* masks for bl and bd bits */
1877         register ulg b;                         /* bit buffer */
1878         register unsigned k;            /* number of bits in bit buffer */
1879
1880
1881         /* make local copies of globals */
1882         b = bb;                                         /* initialize bit buffer */
1883         k = bk;
1884         w = wp;                                         /* initialize window position */
1885
1886         /* inflate the coded data */
1887         ml = mask_bits[bl];                     /* precompute masks for speed */
1888         md = mask_bits[bd];
1889         for (;;) {                                      /* do until end of block */
1890                 NEEDBITS((unsigned) bl)
1891                         if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
1892                         do {
1893                                 if (e == 99)
1894                                         return 1;
1895                                 DUMPBITS(t->b)
1896                                         e -= 16;
1897                                 NEEDBITS(e)
1898                         } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
1899                                          > 16);
1900                 DUMPBITS(t->b)
1901                         if (e == 16) {          /* then it's a literal */
1902                         slide[w++] = (uch) t->v.n;
1903                         Tracevv((stderr, "%c", slide[w - 1]));
1904                         if (w == WSIZE) {
1905                                 flush_output(w);
1906                                 w = 0;
1907                         }
1908                 } else {                                /* it's an EOB or a length */
1909
1910                         /* exit if end of block */
1911                         if (e == 15)
1912                                 break;
1913
1914                         /* get length of block to copy */
1915                         NEEDBITS(e)
1916                                 n = t->v.n + ((unsigned) b & mask_bits[e]);
1917                         DUMPBITS(e);
1918
1919                         /* decode distance of block to copy */
1920                         NEEDBITS((unsigned) bd)
1921                                 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
1922                                 do {
1923                                         if (e == 99)
1924                                                 return 1;
1925                                         DUMPBITS(t->b)
1926                                                 e -= 16;
1927                                         NEEDBITS(e)
1928                                 }
1929                                         while (
1930                                                    (e =
1931                                                         (t =
1932                                                          t->v.t + ((unsigned) b & mask_bits[e]))->e) >
1933                                                    16);
1934                         DUMPBITS(t->b)
1935                                 NEEDBITS(e)
1936                                 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
1937                         DUMPBITS(e)
1938                                 Tracevv((stderr, "\\[%d,%d]", w - d, n));
1939
1940                         /* do the copy */
1941                         do {
1942                                 n -= (e =
1943                                           (e =
1944                                            WSIZE - ((d &= WSIZE - 1) > w ? d : w)) >
1945                                           n ? n : e);
1946 #if !defined(NOMEMCPY) && !defined(DEBUG)
1947                                 if (w - d >= e) {       /* (this test assumes unsigned comparison) */
1948                                         memcpy(slide + w, slide + d, e);
1949                                         w += e;
1950                                         d += e;
1951                                 } else                  /* do it slow to avoid memcpy() overlap */
1952 #endif                                                  /* !NOMEMCPY */
1953                                         do {
1954                                                 slide[w++] = slide[d++];
1955                                                 Tracevv((stderr, "%c", slide[w - 1]));
1956                                         } while (--e);
1957                                 if (w == WSIZE) {
1958                                         flush_output(w);
1959                                         w = 0;
1960                                 }
1961                         } while (n);
1962                 }
1963         }
1964
1965
1966         /* restore the globals from the locals */
1967         wp = w;                                         /* restore global window pointer */
1968         bb = b;                                         /* restore global bit buffer */
1969         bk = k;
1970
1971         /* done */
1972         return 0;
1973 }
1974
1975
1976
1977 int inflate_stored()
1978 /* "decompress" an inflated type 0 (stored) block. */
1979 {
1980         unsigned n;                                     /* number of bytes in block */
1981         unsigned w;                                     /* current window position */
1982         register ulg b;                         /* bit buffer */
1983         register unsigned k;            /* number of bits in bit buffer */
1984
1985
1986         /* make local copies of globals */
1987         b = bb;                                         /* initialize bit buffer */
1988         k = bk;
1989         w = wp;                                         /* initialize window position */
1990
1991
1992         /* go to byte boundary */
1993         n = k & 7;
1994         DUMPBITS(n);
1995
1996
1997         /* get the length and its complement */
1998         NEEDBITS(16)
1999                 n = ((unsigned) b & 0xffff);
2000         DUMPBITS(16)
2001                 NEEDBITS(16)
2002                 if (n != (unsigned) ((~b) & 0xffff))
2003                 return 1;                               /* error in compressed data */
2004         DUMPBITS(16)
2005
2006
2007                 /* read and output the compressed data */
2008                 while (n--) {
2009                 NEEDBITS(8)
2010                         slide[w++] = (uch) b;
2011                 if (w == WSIZE) {
2012                         flush_output(w);
2013                         w = 0;
2014                 }
2015                 DUMPBITS(8)
2016         }
2017
2018
2019         /* restore the globals from the locals */
2020         wp = w;                                         /* restore global window pointer */
2021         bb = b;                                         /* restore global bit buffer */
2022         bk = k;
2023         return 0;
2024 }
2025
2026
2027
2028 int inflate_fixed()
2029 /* decompress an inflated type 1 (fixed Huffman codes) block.  We should
2030    either replace this with a custom decoder, or at least precompute the
2031    Huffman tables. */
2032 {
2033         int i;                                          /* temporary variable */
2034         struct huft *tl;                        /* literal/length code table */
2035         struct huft *td;                        /* distance code table */
2036         int bl;                                         /* lookup bits for tl */
2037         int bd;                                         /* lookup bits for td */
2038         unsigned l[288];                        /* length list for huft_build */
2039
2040
2041         /* set up literal table */
2042         for (i = 0; i < 144; i++)
2043                 l[i] = 8;
2044         for (; i < 256; i++)
2045                 l[i] = 9;
2046         for (; i < 280; i++)
2047                 l[i] = 7;
2048         for (; i < 288; i++)            /* make a complete, but wrong code set */
2049                 l[i] = 8;
2050         bl = 7;
2051         if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
2052                 return i;
2053
2054
2055         /* set up distance table */
2056         for (i = 0; i < 30; i++)        /* make an incomplete code set */
2057                 l[i] = 5;
2058         bd = 5;
2059         if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
2060                 huft_free(tl);
2061                 return i;
2062         }
2063
2064
2065         /* decompress until an end-of-block code */
2066         if (inflate_codes(tl, td, bl, bd))
2067                 return 1;
2068
2069
2070         /* free the decoding tables, return */
2071         huft_free(tl);
2072         huft_free(td);
2073         return 0;
2074 }
2075
2076
2077
2078 int inflate_dynamic()
2079 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
2080 {
2081         int i;                                          /* temporary variables */
2082         unsigned j;
2083         unsigned l;                                     /* last length */
2084         unsigned m;                                     /* mask for bit lengths table */
2085         unsigned n;                                     /* number of lengths to get */
2086         struct huft *tl;                        /* literal/length code table */
2087         struct huft *td;                        /* distance code table */
2088         int bl;                                         /* lookup bits for tl */
2089         int bd;                                         /* lookup bits for td */
2090         unsigned nb;                            /* number of bit length codes */
2091         unsigned nl;                            /* number of literal/length codes */
2092         unsigned nd;                            /* number of distance codes */
2093
2094 #ifdef PKZIP_BUG_WORKAROUND
2095         unsigned ll[288 + 32];          /* literal/length and distance code lengths */
2096 #else
2097         unsigned ll[286 + 30];          /* literal/length and distance code lengths */
2098 #endif
2099         register ulg b;                         /* bit buffer */
2100         register unsigned k;            /* number of bits in bit buffer */
2101
2102
2103         /* make local bit buffer */
2104         b = bb;
2105         k = bk;
2106
2107
2108         /* read in table lengths */
2109         NEEDBITS(5)
2110                 nl = 257 + ((unsigned) b & 0x1f);       /* number of literal/length codes */
2111         DUMPBITS(5)
2112                 NEEDBITS(5)
2113                 nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
2114         DUMPBITS(5)
2115                 NEEDBITS(4)
2116                 nb = 4 + ((unsigned) b & 0xf);  /* number of bit length codes */
2117         DUMPBITS(4)
2118 #ifdef PKZIP_BUG_WORKAROUND
2119                 if (nl > 288 || nd > 32)
2120 #else
2121                 if (nl > 286 || nd > 30)
2122 #endif
2123                 return 1;                               /* bad lengths */
2124
2125
2126         /* read in bit-length-code lengths */
2127         for (j = 0; j < nb; j++) {
2128                 NEEDBITS(3)
2129                         ll[border[j]] = (unsigned) b & 7;
2130                 DUMPBITS(3)
2131         }
2132         for (; j < 19; j++)
2133                 ll[border[j]] = 0;
2134
2135
2136         /* build decoding table for trees--single level, 7 bit lookup */
2137         bl = 7;
2138         if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
2139                 if (i == 1)
2140                         huft_free(tl);
2141                 return i;                               /* incomplete code set */
2142         }
2143
2144
2145         /* read in literal and distance code lengths */
2146         n = nl + nd;
2147         m = mask_bits[bl];
2148         i = l = 0;
2149         while ((unsigned) i < n) {
2150                 NEEDBITS((unsigned) bl)
2151                         j = (td = tl + ((unsigned) b & m))->b;
2152                 DUMPBITS(j)
2153                         j = td->v.n;
2154                 if (j < 16)                             /* length of code in bits (0..15) */
2155                         ll[i++] = l = j;        /* save last length in l */
2156                 else if (j == 16) {             /* repeat last length 3 to 6 times */
2157                         NEEDBITS(2)
2158                                 j = 3 + ((unsigned) b & 3);
2159                         DUMPBITS(2)
2160                                 if ((unsigned) i + j > n)
2161                                 return 1;
2162                         while (j--)
2163                                 ll[i++] = l;
2164                 } else if (j == 17) {   /* 3 to 10 zero length codes */
2165                         NEEDBITS(3)
2166                                 j = 3 + ((unsigned) b & 7);
2167                         DUMPBITS(3)
2168                                 if ((unsigned) i + j > n)
2169                                 return 1;
2170                         while (j--)
2171                                 ll[i++] = 0;
2172                         l = 0;
2173                 } else {                                /* j == 18: 11 to 138 zero length codes */
2174
2175                         NEEDBITS(7)
2176                                 j = 11 + ((unsigned) b & 0x7f);
2177                         DUMPBITS(7)
2178                                 if ((unsigned) i + j > n)
2179                                 return 1;
2180                         while (j--)
2181                                 ll[i++] = 0;
2182                         l = 0;
2183                 }
2184         }
2185
2186
2187         /* free decoding table for trees */
2188         huft_free(tl);
2189
2190
2191         /* restore the global bit buffer */
2192         bb = b;
2193         bk = k;
2194
2195
2196         /* build the decoding tables for literal/length and distance codes */
2197         bl = lbits;
2198         if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
2199                 if (i == 1) {
2200                         fprintf(stderr, " incomplete literal tree\n");
2201                         huft_free(tl);
2202                 }
2203                 return i;                               /* incomplete code set */
2204         }
2205         bd = dbits;
2206         if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
2207                 if (i == 1) {
2208                         fprintf(stderr, " incomplete distance tree\n");
2209 #ifdef PKZIP_BUG_WORKAROUND
2210                         i = 0;
2211                 }
2212 #else
2213                         huft_free(td);
2214                 }
2215                 huft_free(tl);
2216                 return i;                               /* incomplete code set */
2217 #endif
2218         }
2219
2220
2221         /* decompress until an end-of-block code */
2222         if (inflate_codes(tl, td, bl, bd))
2223                 return 1;
2224
2225
2226         /* free the decoding tables, return */
2227         huft_free(tl);
2228         huft_free(td);
2229         return 0;
2230 }
2231
2232
2233
2234 int inflate_block(e)
2235 int *e;                                                 /* last block flag */
2236
2237 /* decompress an inflated block */
2238 {
2239         unsigned t;                                     /* block type */
2240         register ulg b;                         /* bit buffer */
2241         register unsigned k;            /* number of bits in bit buffer */
2242
2243
2244         /* make local bit buffer */
2245         b = bb;
2246         k = bk;
2247
2248
2249         /* read in last block bit */
2250         NEEDBITS(1)
2251                 * e = (int) b & 1;
2252         DUMPBITS(1)
2253
2254
2255                 /* read in block type */
2256                 NEEDBITS(2)
2257                 t = (unsigned) b & 3;
2258         DUMPBITS(2)
2259
2260
2261                 /* restore the global bit buffer */
2262                 bb = b;
2263         bk = k;
2264
2265
2266         /* inflate that block type */
2267         if (t == 2)
2268                 return inflate_dynamic();
2269         if (t == 0)
2270                 return inflate_stored();
2271         if (t == 1)
2272                 return inflate_fixed();
2273
2274
2275         /* bad block type */
2276         return 2;
2277 }
2278
2279
2280
2281 int inflate()
2282 /* decompress an inflated entry */
2283 {
2284         int e;                                          /* last block flag */
2285         int r;                                          /* result code */
2286         unsigned h;                                     /* maximum struct huft's malloc'ed */
2287
2288
2289         /* initialize window, bit buffer */
2290         wp = 0;
2291         bk = 0;
2292         bb = 0;
2293
2294
2295         /* decompress until the last block */
2296         h = 0;
2297         do {
2298                 hufts = 0;
2299                 if ((r = inflate_block(&e)) != 0)
2300                         return r;
2301                 if (hufts > h)
2302                         h = hufts;
2303         } while (!e);
2304
2305         /* Undo too much lookahead. The next read will be byte aligned so we
2306          * can discard unused bits in the last meaningful byte.
2307          */
2308         while (bk >= 8) {
2309                 bk -= 8;
2310                 inptr--;
2311         }
2312
2313         /* flush out slide */
2314         flush_output(wp);
2315
2316
2317         /* return success */
2318 #ifdef DEBUG
2319         fprintf(stderr, "<%u> ", h);
2320 #endif                                                  /* DEBUG */
2321         return 0;
2322 }