blob: 89e46a68620dda7786be64b4a3af7b5145222317 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen61677fe2000-04-13 01:18:56 +00002/*
3 * Gzip implementation for busybox
4 *
5 * Based on GNU gzip Copyright (C) 1992-1993 Jean-loup Gailly.
6 *
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
9 *
10 * Adjusted further by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
11 * to support files as well as stdin/stdout, and to generally behave itself wrt
12 * command line handling.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
Eric Andersenb052b471999-11-18 00:21:59 +000029
30#include "internal.h"
Eric Andersen1e03add2000-07-06 09:56:35 +000031#include <getopt.h>
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000032
Eric Andersenb052b471999-11-18 00:21:59 +000033static const char gunzip_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000034 "gunzip [OPTION]... FILE\n"
35#ifndef BB_FEATURE_TRIVIAL_HELP
36 "\nUncompress FILE (or standard input if FILE is '-').\n\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 "Options:\n"
38
39 "\t-c\tWrite output to standard output\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000040 "\t-t\tTest compressed file integrity\n"
41#endif
42 ;
Eric Andersenb052b471999-11-18 00:21:59 +000043
Erik Andersen61677fe2000-04-13 01:18:56 +000044
45 /* These defines are very important for BusyBox. Without these,
46 * huge chunks of ram are pre-allocated making the BusyBox bss
47 * size Freaking Huge(tm), which is a bad thing.*/
48#define SMALL_MEM
49#define DYN_ALLOC
50
Erik Andersen61677fe2000-04-13 01:18:56 +000051#define BB_DECLARE_EXTERN
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000052#define bb_need_memory_exhausted
53#define bb_need_name_too_long
Erik Andersen61677fe2000-04-13 01:18:56 +000054#include "messages.c"
55
56
Eric Andersenb052b471999-11-18 00:21:59 +000057/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
58 * Copyright (C) 1992-1993 Jean-loup Gailly
59 * The unzip code was written and put in the public domain by Mark Adler.
60 * Portions of the lzw code are derived from the public domain 'compress'
61 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
62 * Ken Turkowski, Dave Mack and Peter Jannesen.
63 *
64 * See the license_msg below and the file COPYING for the software license.
65 * See the file algorithm.doc for the compression algorithms and file formats.
66 */
67
68#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000069static char *license_msg[] = {
70 " Copyright (C) 1992-1993 Jean-loup Gailly",
71 " This program is free software; you can redistribute it and/or modify",
72 " it under the terms of the GNU General Public License as published by",
73 " the Free Software Foundation; either version 2, or (at your option)",
74 " any later version.",
75 "",
76 " This program is distributed in the hope that it will be useful,",
77 " but WITHOUT ANY WARRANTY; without even the implied warranty of",
78 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
79 " GNU General Public License for more details.",
80 "",
81 " You should have received a copy of the GNU General Public License",
82 " along with this program; if not, write to the Free Software",
83 " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
84 0
85};
Eric Andersenb052b471999-11-18 00:21:59 +000086#endif
87
88/* Compress files with zip algorithm and 'compress' interface.
89 * See usage() and help() functions below for all options.
90 * Outputs:
91 * file.gz: compressed file with same mode, owner, and utimes
92 * or stdout with -c option or if stdin used as input.
93 * If the output file name had to be truncated, the original name is kept
94 * in the compressed file.
95 * On MSDOS, file.tmp -> file.tmz. On VMS, file.tmp -> file.tmp-gz.
96 *
97 * Using gz on MSDOS would create too many file name conflicts. For
98 * example, foo.txt -> foo.tgz (.tgz must be reserved as shorthand for
99 * tar.gz). Similarly, foo.dir and foo.doc would both be mapped to foo.dgz.
100 * I also considered 12345678.txt -> 12345txt.gz but this truncates the name
101 * too heavily. There is no ideal solution given the MSDOS 8+3 limitation.
102 *
103 * For the meaning of all compilation flags, see comments in Makefile.in.
104 */
105
106#include <ctype.h>
107#include <sys/types.h>
108#include <signal.h>
109#include <sys/stat.h>
110#include <errno.h>
111
112/* #include "tailor.h" */
113
114/* tailor.h -- target dependent definitions
115 * Copyright (C) 1992-1993 Jean-loup Gailly.
116 * This is free software; you can redistribute it and/or modify it under the
117 * terms of the GNU General Public License, see the file COPYING.
118 */
119
120/* The target dependent definitions should be defined here only.
121 * The target dependent functions should be defined in tailor.c.
122 */
123
124#define RECORD_IO 0
125
126#define get_char() get_byte()
127#define put_char(c) put_byte(c)
128
Eric Andersenb052b471999-11-18 00:21:59 +0000129
130/* I don't like nested includes, but the string and io functions are used
131 * too often
132 */
133#include <stdio.h>
134#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
135# include <string.h>
136# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
137# include <memory.h>
138# endif
Erik Andersen61677fe2000-04-13 01:18:56 +0000139# define memzero(s, n) memset ((void *)(s), 0, (n))
Eric Andersenb052b471999-11-18 00:21:59 +0000140#else
141# include <strings.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142# define strchr index
Eric Andersenb052b471999-11-18 00:21:59 +0000143# define strrchr rindex
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144# define memcpy(d, s, n) bcopy((s), (d), (n))
145# define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
Eric Andersenb052b471999-11-18 00:21:59 +0000146# define memzero(s, n) bzero((s), (n))
147#endif
148
149#ifndef RETSIGTYPE
150# define RETSIGTYPE void
151#endif
152
153#define local static
154
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155typedef unsigned char uch;
Eric Andersenb052b471999-11-18 00:21:59 +0000156typedef unsigned short ush;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157typedef unsigned long ulg;
Eric Andersenb052b471999-11-18 00:21:59 +0000158
159/* Return codes from gzip */
160#define OK 0
161#define ERROR 1
162#define WARNING 2
163
164/* Compression methods (see algorithm.doc) */
165#define DEFLATED 8
166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167extern int method; /* compression method */
Eric Andersenb052b471999-11-18 00:21:59 +0000168
169/* To save memory for 16 bit systems, some arrays are overlaid between
170 * the various modules:
171 * deflate: prev+head window d_buf l_buf outbuf
172 * unlzw: tab_prefix tab_suffix stack inbuf outbuf
173 * inflate: window inbuf
174 * unpack: window inbuf prefix_len
175 * unlzh: left+right window c_table inbuf c_len
176 * For compression, input is done in window[]. For decompression, output
177 * is done in window except for unlzw.
178 */
179
180#ifndef INBUFSIZ
181# ifdef SMALL_MEM
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182# define INBUFSIZ 0x2000 /* input buffer size */
Eric Andersenb052b471999-11-18 00:21:59 +0000183# else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184# define INBUFSIZ 0x8000 /* input buffer size */
Eric Andersenb052b471999-11-18 00:21:59 +0000185# endif
186#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187#define INBUF_EXTRA 64 /* required by unlzw() */
Eric Andersenb052b471999-11-18 00:21:59 +0000188
189#ifndef OUTBUFSIZ
190# ifdef SMALL_MEM
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191# define OUTBUFSIZ 8192 /* output buffer size */
Eric Andersenb052b471999-11-18 00:21:59 +0000192# else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193# define OUTBUFSIZ 16384 /* output buffer size */
Eric Andersenb052b471999-11-18 00:21:59 +0000194# endif
195#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196#define OUTBUF_EXTRA 2048 /* required by unlzw() */
Eric Andersenb052b471999-11-18 00:21:59 +0000197
198#define SMALL_MEM
199
200#ifndef DIST_BUFSIZE
201# ifdef SMALL_MEM
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202# define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
Eric Andersenb052b471999-11-18 00:21:59 +0000203# else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204# define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
Eric Andersenb052b471999-11-18 00:21:59 +0000205# endif
206#endif
207
208/*#define DYN_ALLOC*/
209
210#ifdef DYN_ALLOC
211# define EXTERN(type, array) extern type * array
212# define DECLARE(type, array, size) type * array
213# define ALLOC(type, array, size) { \
214 array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
Matt Kraaibe84cd42000-07-12 17:02:35 +0000215 if (array == NULL) errorMsg(memory_exhausted); \
Eric Andersenb052b471999-11-18 00:21:59 +0000216 }
217# define FREE(array) {if (array != NULL) free(array), array=NULL;}
218#else
219# define EXTERN(type, array) extern type array[]
220# define DECLARE(type, array, size) type array[size]
221# define ALLOC(type, array, size)
222# define FREE(array)
223#endif
224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225EXTERN(uch, inbuf); /* input buffer */
226EXTERN(uch, outbuf); /* output buffer */
227EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
228EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
Eric Andersenb052b471999-11-18 00:21:59 +0000229#define tab_suffix window
230#ifndef MAXSEG_64K
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231# define tab_prefix prev /* hash link (see deflate.c) */
232# define head (prev+WSIZE) /* hash head (see deflate.c) */
233EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
Eric Andersenb052b471999-11-18 00:21:59 +0000234#else
235# define tab_prefix0 prev
236# define head tab_prefix1
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237EXTERN(ush, tab_prefix0); /* prefix for even codes */
238EXTERN(ush, tab_prefix1); /* prefix for odd codes */
Eric Andersenb052b471999-11-18 00:21:59 +0000239#endif
240
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241extern unsigned insize; /* valid bytes in inbuf */
242extern unsigned inptr; /* index of next byte to be processed in inbuf */
243extern unsigned outcnt; /* bytes in output buffer */
Eric Andersenb052b471999-11-18 00:21:59 +0000244
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245extern long bytes_in; /* number of input bytes */
246extern long bytes_out; /* number of output bytes */
247extern long header_bytes; /* number of bytes in gzip header */
Eric Andersenb052b471999-11-18 00:21:59 +0000248
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249extern long ifile_size; /* input file size, -1 for devices (debug only) */
Eric Andersenb052b471999-11-18 00:21:59 +0000250
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251typedef int file_t; /* Do not use stdio */
252
253#define NO_FILE (-1) /* in memory compression */
Eric Andersenb052b471999-11-18 00:21:59 +0000254
255
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256#define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
Eric Andersenb052b471999-11-18 00:21:59 +0000257
258/* gzip flag byte */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
260#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
261#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
262#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
263#define COMMENT 0x10 /* bit 4 set: file comment present */
264#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
265#define RESERVED 0xC0 /* bit 6,7: reserved */
Eric Andersenb052b471999-11-18 00:21:59 +0000266
267#ifndef WSIZE
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268# define WSIZE 0x8000 /* window size--must be a power of two, and */
269#endif /* at least 32K for zip's deflate method */
Eric Andersenb052b471999-11-18 00:21:59 +0000270
271#define MIN_MATCH 3
272#define MAX_MATCH 258
273/* The minimum and maximum match lengths */
274
275#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
276/* Minimum amount of lookahead, except at the end of the input file.
277 * See deflate.c for comments about the MIN_MATCH+1.
278 */
279
280#define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
281/* In order to simplify the code, particularly on 16 bit machines, match
282 * distances are limited to MAX_DIST instead of WSIZE.
283 */
284
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285extern int exit_code; /* program exit code */
286extern int verbose; /* be verbose (-v) */
287extern int level; /* compression level */
288extern int test; /* check .z file integrity */
289extern int save_orig_name; /* set if original name must be saved */
Eric Andersenb052b471999-11-18 00:21:59 +0000290
291#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
292#define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
293
294/* put_byte is used for the compressed output, put_ubyte for the
295 * uncompressed output. However unlzw() uses window for its
296 * suffix table instead of its output buffer, so it does not use put_ubyte
297 * (to be cleaned up).
298 */
299#define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
300 flush_outbuf();}
301#define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
302 flush_window();}
303
304/* Output a 16 bit value, lsb first */
305#define put_short(w) \
306{ if (outcnt < OUTBUFSIZ-2) { \
307 outbuf[outcnt++] = (uch) ((w) & 0xff); \
308 outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
309 } else { \
310 put_byte((uch)((w) & 0xff)); \
311 put_byte((uch)((ush)(w) >> 8)); \
312 } \
313}
314
315/* Output a 32 bit value to the bit stream, lsb first */
316#define put_long(n) { \
317 put_short((n) & 0xffff); \
318 put_short(((ulg)(n)) >> 16); \
319}
320
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321#define seekable() 0 /* force sequential output */
322#define translate_eol 0 /* no option -a yet */
Eric Andersenb052b471999-11-18 00:21:59 +0000323
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324#define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */
Eric Andersenb052b471999-11-18 00:21:59 +0000325
326/* Macros for getting two-byte and four-byte header values */
327#define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
328#define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
329
330/* Diagnostic functions */
331#ifdef DEBUG
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000332# define Assert(cond,msg) {if(!(cond)) errorMsg(msg);}
Eric Andersenb052b471999-11-18 00:21:59 +0000333# define Trace(x) fprintf x
334# define Tracev(x) {if (verbose) fprintf x ;}
335# define Tracevv(x) {if (verbose>1) fprintf x ;}
336# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
337# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
338#else
339# define Assert(cond,msg)
340# define Trace(x)
341# define Tracev(x)
342# define Tracevv(x)
343# define Tracec(c,x)
344# define Tracecv(c,x)
345#endif
346
347#define WARN(msg) {fprintf msg ; \
348 if (exit_code == OK) exit_code = WARNING;}
349
350 /* in unzip.c */
Erik Andersen61677fe2000-04-13 01:18:56 +0000351extern int unzip (int in, int out);
Eric Andersenb052b471999-11-18 00:21:59 +0000352
353 /* in gzip.c */
Erik Andersen61677fe2000-04-13 01:18:56 +0000354RETSIGTYPE abort_gzip (void);
Eric Andersenb052b471999-11-18 00:21:59 +0000355
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 /* in deflate.c */
Erik Andersen61677fe2000-04-13 01:18:56 +0000357void lm_init (int pack_level, ush * flags);
358ulg deflate (void);
Eric Andersenb052b471999-11-18 00:21:59 +0000359
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 /* in trees.c */
Erik Andersen61677fe2000-04-13 01:18:56 +0000361void ct_init (ush * attr, int *method);
362int ct_tally (int dist, int lc);
363ulg flush_block (char *buf, ulg stored_len, int eof);
Eric Andersenb052b471999-11-18 00:21:59 +0000364
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 /* in bits.c */
Erik Andersen61677fe2000-04-13 01:18:56 +0000366void bi_init (file_t zipfile);
367void send_bits (int value, int length);
368unsigned bi_reverse (unsigned value, int length);
369void bi_windup (void);
370void copy_block (char *buf, unsigned len, int header);
Eric Andersenb052b471999-11-18 00:21:59 +0000371
372 /* in util.c: */
Erik Andersen61677fe2000-04-13 01:18:56 +0000373extern ulg updcrc (uch * s, unsigned n);
374extern void clear_bufs (void);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000375static int fill_inbuf (int eof_ok);
Erik Andersen61677fe2000-04-13 01:18:56 +0000376extern void flush_outbuf (void);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000377static void flush_window (void);
Erik Andersen61677fe2000-04-13 01:18:56 +0000378extern void write_buf (int fd, void * buf, unsigned cnt);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379
Eric Andersenb052b471999-11-18 00:21:59 +0000380#ifndef __linux__
Erik Andersen330fd2b2000-05-19 05:35:19 +0000381static char *basename (char *fname);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382#endif /* not __linux__ */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000383void read_error_msg (void);
384void write_error_msg (void);
Eric Andersenb052b471999-11-18 00:21:59 +0000385
386 /* in inflate.c */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000387static int inflate (void);
Eric Andersenb052b471999-11-18 00:21:59 +0000388
389/* #include "lzw.h" */
390
391/* lzw.h -- define the lzw functions.
392 * Copyright (C) 1992-1993 Jean-loup Gailly.
393 * This is free software; you can redistribute it and/or modify it under the
394 * terms of the GNU General Public License, see the file COPYING.
395 */
396
397#if !defined(OF) && defined(lint)
398# include "gzip.h"
399#endif
400
401#ifndef BITS
402# define BITS 16
403#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404#define INIT_BITS 9 /* Initial number of bits per code */
Eric Andersenb052b471999-11-18 00:21:59 +0000405
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406#define LZW_MAGIC "\037\235" /* Magic header for lzw files, 1F 9D */
Eric Andersenb052b471999-11-18 00:21:59 +0000407
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408#define BIT_MASK 0x1f /* Mask for 'number of compression bits' */
Eric Andersenb052b471999-11-18 00:21:59 +0000409/* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free.
410 * It's a pity that old uncompress does not check bit 0x20. That makes
411 * extension of the format actually undesirable because old compress
412 * would just crash on the new format instead of giving a meaningful
413 * error message. It does check the number of bits, but it's more
414 * helpful to say "unsupported format, get a new version" than
415 * "can only handle 16 bits".
416 */
417
418#define BLOCK_MODE 0x80
419/* Block compression: if table is full and compression rate is dropping,
420 * clear the dictionary.
421 */
422
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423#define LZW_RESERVED 0x60 /* reserved bits */
Eric Andersenb052b471999-11-18 00:21:59 +0000424
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425#define CLEAR 256 /* flush the dictionary */
426#define FIRST (CLEAR+1) /* first free entry */
Eric Andersenb052b471999-11-18 00:21:59 +0000427
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428extern int maxbits; /* max bits per code for LZW */
429extern int block_mode; /* block compress mode -C compatible with 2.0 */
Eric Andersenb052b471999-11-18 00:21:59 +0000430
Erik Andersen61677fe2000-04-13 01:18:56 +0000431extern int lzw (int in, int out);
432extern int unlzw (int in, int out);
Eric Andersenb052b471999-11-18 00:21:59 +0000433
434
435/* #include "revision.h" */
436
437/* revision.h -- define the version number
438 * Copyright (C) 1992-1993 Jean-loup Gailly.
439 * This is free software; you can redistribute it and/or modify it under the
440 * terms of the GNU General Public License, see the file COPYING.
441 */
442
443#define VERSION "1.2.4"
444#define PATCHLEVEL 0
445#define REVDATE "18 Aug 93"
446
447/* This version does not support compression into old compress format: */
448#ifdef LZW
449# undef LZW
450#endif
451
Eric Andersenb052b471999-11-18 00:21:59 +0000452#include <time.h>
453#include <fcntl.h>
454#include <unistd.h>
Eric Andersenb052b471999-11-18 00:21:59 +0000455#include <stdlib.h>
Eric Andersenb052b471999-11-18 00:21:59 +0000456#if defined(DIRENT)
457# include <dirent.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458typedef struct dirent dir_type;
459
Eric Andersenb052b471999-11-18 00:21:59 +0000460# define NLENGTH(dirent) ((int)strlen((dirent)->d_name))
461# define DIR_OPT "DIRENT"
462#else
463# define NLENGTH(dirent) ((dirent)->d_namlen)
464# ifdef SYSDIR
465# include <sys/dir.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466typedef struct direct dir_type;
467
Eric Andersenb052b471999-11-18 00:21:59 +0000468# define DIR_OPT "SYSDIR"
469# else
470# ifdef SYSNDIR
471# include <sys/ndir.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472typedef struct direct dir_type;
473
Eric Andersenb052b471999-11-18 00:21:59 +0000474# define DIR_OPT "SYSNDIR"
475# else
476# ifdef NDIR
477# include <ndir.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478typedef struct direct dir_type;
479
Eric Andersenb052b471999-11-18 00:21:59 +0000480# define DIR_OPT "NDIR"
481# else
482# define NO_DIR
483# define DIR_OPT "NO_DIR"
484# endif
485# endif
486# endif
487#endif
Eric Andersenb052b471999-11-18 00:21:59 +0000488#if !defined(S_ISDIR) && defined(S_IFDIR)
489# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
490#endif
491#if !defined(S_ISREG) && defined(S_IFREG)
492# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
493#endif
Erik Andersen61677fe2000-04-13 01:18:56 +0000494typedef RETSIGTYPE(*sig_type) (int);
Eric Andersenb052b471999-11-18 00:21:59 +0000495
496#ifndef O_BINARY
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497# define O_BINARY 0 /* creation mode for open() */
Eric Andersenb052b471999-11-18 00:21:59 +0000498#endif
499
500#ifndef O_CREAT
501 /* Pure BSD system? */
502# include <sys/file.h>
503# ifndef O_CREAT
504# define O_CREAT FCREAT
505# endif
506# ifndef O_EXCL
507# define O_EXCL FEXCL
508# endif
509#endif
510
511#ifndef S_IRUSR
512# define S_IRUSR 0400
513#endif
514#ifndef S_IWUSR
515# define S_IWUSR 0200
516#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517#define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */
Eric Andersenb052b471999-11-18 00:21:59 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519#ifndef MAX_PATH_LEN /* max pathname length */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000520# ifdef BUFSIZ
521# define MAX_PATH_LEN BUFSIZ
Erik Andersenfac10d72000-02-07 05:29:42 +0000522# else
523# define MAX_PATH_LEN 1024
524# endif
Eric Andersenb052b471999-11-18 00:21:59 +0000525#endif
526
527#ifndef SEEK_END
528# define SEEK_END 2
529#endif
530
531#ifdef NO_OFF_T
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532typedef long off_t;
Erik Andersen61677fe2000-04-13 01:18:56 +0000533off_t lseek (int fd, off_t offset, int whence);
Eric Andersenb052b471999-11-18 00:21:59 +0000534#endif
535
536
537 /* global buffers */
538
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
540DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
541DECLARE(ush, d_buf, DIST_BUFSIZE);
542DECLARE(uch, window, 2L * WSIZE);
Eric Andersenb052b471999-11-18 00:21:59 +0000543#ifndef MAXSEG_64K
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544DECLARE(ush, tab_prefix, 1L << BITS);
Eric Andersenb052b471999-11-18 00:21:59 +0000545#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546DECLARE(ush, tab_prefix0, 1L << (BITS - 1));
547DECLARE(ush, tab_prefix1, 1L << (BITS - 1));
Eric Andersenb052b471999-11-18 00:21:59 +0000548#endif
549
550 /* local variables */
551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552int test_mode = 0; /* check file integrity option */
553int foreground; /* set if program run in foreground */
554int maxbits = BITS; /* max bits per code for LZW */
555int method = DEFLATED; /* compression method */
556int exit_code = OK; /* program exit code */
557int last_member; /* set for .zip and .Z files */
558int part_nb; /* number of parts in .gz file */
559long ifile_size; /* input file size, -1 for devices (debug only) */
Eric Andersenb052b471999-11-18 00:21:59 +0000560
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561long bytes_in; /* number of input bytes */
562long bytes_out; /* number of output bytes */
563long total_in = 0; /* input bytes for all files */
564long total_out = 0; /* output bytes for all files */
565struct stat istat; /* status for input file */
566int ifd; /* input file descriptor */
567int ofd; /* output file descriptor */
568unsigned insize; /* valid bytes in inbuf */
569unsigned inptr; /* index of next byte to be processed in inbuf */
570unsigned outcnt; /* bytes in output buffer */
Eric Andersenb052b471999-11-18 00:21:59 +0000571
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572long header_bytes; /* number of bytes in gzip header */
Eric Andersenb052b471999-11-18 00:21:59 +0000573
574/* local functions */
575
Erik Andersen61677fe2000-04-13 01:18:56 +0000576local int get_method (int in);
Eric Andersenb052b471999-11-18 00:21:59 +0000577
578#define strequ(s1, s2) (strcmp((s1),(s2)) == 0)
579
580/* ======================================================================== */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581int gunzip_main(int argc, char **argv)
Eric Andersenb052b471999-11-18 00:21:59 +0000582{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583 int file_count; /* number of files to precess */
584 int to_stdout = 0;
585 int fromstdin = 0;
586 int result;
587 int inFileNum;
588 int outFileNum;
589 int delInputFile = 0;
590 struct stat statBuf;
591 char *delFileName;
592 char ifname[MAX_PATH_LEN + 1]; /* input file name */
593 char ofname[MAX_PATH_LEN + 1]; /* output file name */
Eric Andersenb052b471999-11-18 00:21:59 +0000594
Matt Kraaie58771e2000-07-12 15:38:49 +0000595 if (strcmp(applet_name, "zcat") == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 to_stdout = 1;
Erik Andersen59b9e872000-05-10 05:05:45 +0000597 if (argc == 1) {
598 fromstdin = 1;
599 }
600 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601
602 /* Parse any options */
603 while (--argc > 0 && **(++argv) == '-') {
604 if (*((*argv) + 1) == '\0') {
605 fromstdin = 1;
606 to_stdout = 1;
607 }
608 while (*(++(*argv))) {
609 switch (**argv) {
610 case 'c':
611 to_stdout = 1;
612 break;
613 case 't':
614 test_mode = 1;
615 break;
616
617 default:
618 usage(gunzip_usage);
619 }
620 }
621 }
622
623 foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
624 if (foreground) {
625 (void) signal(SIGINT, (sig_type) abort_gzip);
626 }
Eric Andersenb052b471999-11-18 00:21:59 +0000627#ifdef SIGTERM
Erik Andersene49d5ec2000-02-08 19:58:47 +0000628 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
629 (void) signal(SIGTERM, (sig_type) abort_gzip);
630 }
Eric Andersenb052b471999-11-18 00:21:59 +0000631#endif
632#ifdef SIGHUP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
634 (void) signal(SIGHUP, (sig_type) abort_gzip);
635 }
Eric Andersenb052b471999-11-18 00:21:59 +0000636#endif
637
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638 file_count = argc - optind;
Eric Andersenb052b471999-11-18 00:21:59 +0000639
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640 /* Allocate all global buffers (for DYN_ALLOC option) */
641 ALLOC(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
642 ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
643 ALLOC(ush, d_buf, DIST_BUFSIZE);
644 ALLOC(uch, window, 2L * WSIZE);
Eric Andersenb052b471999-11-18 00:21:59 +0000645#ifndef MAXSEG_64K
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 ALLOC(ush, tab_prefix, 1L << BITS);
Eric Andersenb052b471999-11-18 00:21:59 +0000647#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 ALLOC(ush, tab_prefix0, 1L << (BITS - 1));
649 ALLOC(ush, tab_prefix1, 1L << (BITS - 1));
Eric Andersenb052b471999-11-18 00:21:59 +0000650#endif
651
Erik Andersene49d5ec2000-02-08 19:58:47 +0000652 if (fromstdin == 1) {
653 strcpy(ofname, "stdin");
Eric Andersenb052b471999-11-18 00:21:59 +0000654
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 inFileNum = fileno(stdin);
656 ifile_size = -1L; /* convention for unknown size */
Eric Andersenb052b471999-11-18 00:21:59 +0000657 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 /* Open up the input file */
659 if (*argv == '\0')
660 usage(gunzip_usage);
661 if (strlen(*argv) > MAX_PATH_LEN) {
662 fprintf(stderr, name_too_long, "gunzip");
Erik Andersen61677fe2000-04-13 01:18:56 +0000663 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 }
665 strcpy(ifname, *argv);
666
667 /* Open input fille */
668 inFileNum = open(ifname, O_RDONLY);
669 if (inFileNum < 0) {
670 perror(ifname);
Erik Andersen61677fe2000-04-13 01:18:56 +0000671 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 }
673 /* Get the time stamp on the input file. */
674 result = stat(ifname, &statBuf);
675 if (result < 0) {
676 perror(ifname);
Erik Andersen61677fe2000-04-13 01:18:56 +0000677 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 }
679 ifile_size = statBuf.st_size;
Eric Andersenb052b471999-11-18 00:21:59 +0000680 }
681
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 if (to_stdout == 1) {
683 /* And get to work */
684 strcpy(ofname, "stdout");
685 outFileNum = fileno(stdout);
686
687 clear_bufs(); /* clear input and output buffers */
688 part_nb = 0;
689
690 /* Actually do the compression/decompression. */
691 unzip(inFileNum, outFileNum);
692
693 } else if (test_mode) {
694 /* Actually do the compression/decompression. */
695 unzip(inFileNum, 2);
696 } else {
697 char *pos;
698
699 /* And get to work */
700 if (strlen(ifname) > MAX_PATH_LEN - 4) {
701 fprintf(stderr, name_too_long, "gunzip");
Erik Andersen61677fe2000-04-13 01:18:56 +0000702 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 }
704 strcpy(ofname, ifname);
705 pos = strstr(ofname, ".gz");
706 if (pos != NULL) {
707 *pos = '\0';
708 delInputFile = 1;
709 } else {
710 pos = strstr(ofname, ".tgz");
711 if (pos != NULL) {
712 *pos = '\0';
713 strcat(pos, ".tar");
714 delInputFile = 1;
715 }
716 }
717
718 /* Open output fille */
Erik Andersen4d1d0111999-12-17 18:44:15 +0000719#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000721#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000723#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 if (outFileNum < 0) {
725 perror(ofname);
Erik Andersen61677fe2000-04-13 01:18:56 +0000726 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 }
728 /* Set permissions on the file */
729 fchmod(outFileNum, statBuf.st_mode);
730
731 clear_bufs(); /* clear input and output buffers */
732 part_nb = 0;
733
734 /* Actually do the compression/decompression. */
735 result = unzip(inFileNum, outFileNum);
736
737 close(outFileNum);
738 close(inFileNum);
739 /* Delete the original file */
740 if (result == OK)
741 delFileName = ifname;
742 else
743 delFileName = ofname;
744
745 if (delInputFile == 1 && unlink(delFileName) < 0) {
746 perror(delFileName);
747 exit(FALSE);
748 }
Eric Andersenb052b471999-11-18 00:21:59 +0000749 }
Eric Andersenb6106152000-06-19 17:25:40 +0000750 return(exit_code);
Eric Andersenb052b471999-11-18 00:21:59 +0000751}
752
753
754/* ========================================================================
755 * Check the magic number of the input file and update ofname if an
756 * original name was given and to_stdout is not set.
757 * Return the compression method, -1 for error, -2 for warning.
758 * Set inptr to the offset of the next byte to be processed.
759 * Updates time_stamp if there is one and --no-time is not used.
760 * This function may be called repeatedly for an input file consisting
761 * of several contiguous gzip'ed members.
762 * IN assertions: there is at least one remaining compressed member.
763 * If the member is a zip file, it must be the only one.
764 */
765local int get_method(in)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766int in; /* input file descriptor */
Eric Andersenb052b471999-11-18 00:21:59 +0000767{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000768 uch flags; /* compression flags */
769 char magic[2]; /* magic header */
Eric Andersenb052b471999-11-18 00:21:59 +0000770
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 magic[0] = (char) get_byte();
772 magic[1] = (char) get_byte();
773 method = -1; /* unknown yet */
774 part_nb++; /* number of parts in gzip file */
775 header_bytes = 0;
776 last_member = RECORD_IO;
777 /* assume multiple members in gzip file except for record oriented I/O */
Eric Andersenb052b471999-11-18 00:21:59 +0000778
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
Eric Andersenb052b471999-11-18 00:21:59 +0000780
Erik Andersene49d5ec2000-02-08 19:58:47 +0000781 method = (int) get_byte();
782 if (method != DEFLATED) {
783 fprintf(stderr,
784 "unknown method %d -- get newer version of gzip\n",
785 method);
786 exit_code = ERROR;
787 return -1;
788 }
789 flags = (uch) get_byte();
Eric Andersenb052b471999-11-18 00:21:59 +0000790
Erik Andersene49d5ec2000-02-08 19:58:47 +0000791 (ulg) get_byte(); /* Ignore time stamp */
792 (ulg) get_byte();
793 (ulg) get_byte();
794 (ulg) get_byte();
Eric Andersenb052b471999-11-18 00:21:59 +0000795
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 (void) get_byte(); /* Ignore extra flags for the moment */
797 (void) get_byte(); /* Ignore OS type for the moment */
Eric Andersenb052b471999-11-18 00:21:59 +0000798
Erik Andersene49d5ec2000-02-08 19:58:47 +0000799 if ((flags & EXTRA_FIELD) != 0) {
800 unsigned len = (unsigned) get_byte();
Eric Andersenb052b471999-11-18 00:21:59 +0000801
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802 len |= ((unsigned) get_byte()) << 8;
803
804 while (len--)
805 (void) get_byte();
806 }
807
808 /* Discard original name if any */
809 if ((flags & ORIG_NAME) != 0) {
810 while (get_char() != 0) /* null */
811 ;
812 }
813
814 /* Discard file comment if any */
815 if ((flags & COMMENT) != 0) {
816 while (get_char() != 0) /* null */
817 ;
818 }
819 if (part_nb == 1) {
820 header_bytes = inptr + 2 * sizeof(long); /* include crc and size */
821 }
822
Eric Andersenb052b471999-11-18 00:21:59 +0000823 }
824
Erik Andersene49d5ec2000-02-08 19:58:47 +0000825 if (method >= 0)
826 return method;
Eric Andersenb052b471999-11-18 00:21:59 +0000827
Eric Andersenb052b471999-11-18 00:21:59 +0000828 if (part_nb == 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 fprintf(stderr, "\nnot in gzip format\n");
830 exit_code = ERROR;
831 return -1;
832 } else {
833 WARN((stderr, "\ndecompression OK, trailing garbage ignored\n"));
834 return -2;
Eric Andersenb052b471999-11-18 00:21:59 +0000835 }
Eric Andersenb052b471999-11-18 00:21:59 +0000836}
837
Eric Andersenb052b471999-11-18 00:21:59 +0000838/* ========================================================================
839 * Signal and error handler.
840 */
841RETSIGTYPE abort_gzip()
842{
Erik Andersen61677fe2000-04-13 01:18:56 +0000843 exit(ERROR);
Eric Andersenb052b471999-11-18 00:21:59 +0000844}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845
Eric Andersenb052b471999-11-18 00:21:59 +0000846/* unzip.c -- decompress files in gzip or pkzip format.
847 * Copyright (C) 1992-1993 Jean-loup Gailly
848 * This is free software; you can redistribute it and/or modify it under the
849 * terms of the GNU General Public License, see the file COPYING.
850 *
851 * The code in this file is derived from the file funzip.c written
852 * and put in the public domain by Mark Adler.
853 */
854
855/*
856 This version can extract files in gzip or pkzip format.
857 For the latter, only the first entry is extracted, and it has to be
858 either deflated or stored.
859 */
860
861/* #include "crypt.h" */
862
863/* crypt.h (dummy version) -- do not perform encryption
864 * Hardly worth copyrighting :-)
865 */
866
867#ifdef CRYPT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868# undef CRYPT /* dummy version */
Eric Andersenb052b471999-11-18 00:21:59 +0000869#endif
870
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871#define RAND_HEAD_LEN 12 /* length of encryption random header */
Eric Andersenb052b471999-11-18 00:21:59 +0000872
873#define zencode
874#define zdecode
875
876/* PKZIP header definitions */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877#define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
878#define LOCFLG 6 /* offset of bit flag */
879#define CRPFLG 1 /* bit for encrypted entry */
880#define EXTFLG 8 /* bit for extended local header */
881#define LOCHOW 8 /* offset of compression method */
882#define LOCTIM 10 /* file mod time (for decryption) */
883#define LOCCRC 14 /* offset of crc */
884#define LOCSIZ 18 /* offset of compressed size */
885#define LOCLEN 22 /* offset of uncompressed length */
886#define LOCFIL 26 /* offset of file name field length */
887#define LOCEXT 28 /* offset of extra field length */
888#define LOCHDR 30 /* size of local header, including sig */
889#define EXTHDR 16 /* size of extended local header, inc sig */
Eric Andersenb052b471999-11-18 00:21:59 +0000890
891
892/* Globals */
893
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894char *key; /* not used--needed to link crypt.c */
895int pkzip = 0; /* set for a pkzip file */
896int ext_header = 0; /* set if extended local header */
Eric Andersenb052b471999-11-18 00:21:59 +0000897
898/* ===========================================================================
899 * Unzip in to out. This routine works on both gzip and pkzip files.
900 *
901 * IN assertions: the buffer inbuf contains already the beginning of
902 * the compressed data, from offsets inptr to insize-1 included.
903 * The magic header has already been checked. The output buffer is cleared.
904 */
905int unzip(in, out)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906int in, out; /* input and output file descriptors */
Eric Andersenb052b471999-11-18 00:21:59 +0000907{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 ulg orig_crc = 0; /* original crc */
909 ulg orig_len = 0; /* original uncompressed length */
910 int n;
911 uch buf[EXTHDR]; /* extended local header */
Eric Andersenb052b471999-11-18 00:21:59 +0000912
Erik Andersene49d5ec2000-02-08 19:58:47 +0000913 ifd = in;
914 ofd = out;
915 method = get_method(ifd);
916 if (method < 0) {
Erik Andersen61677fe2000-04-13 01:18:56 +0000917 exit(exit_code); /* error message already emitted */
Eric Andersenb052b471999-11-18 00:21:59 +0000918 }
919
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 updcrc(NULL, 0); /* initialize crc */
Eric Andersenb052b471999-11-18 00:21:59 +0000921
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 if (pkzip && !ext_header) { /* crc and length at the end otherwise */
923 orig_crc = LG(inbuf + LOCCRC);
924 orig_len = LG(inbuf + LOCLEN);
Eric Andersenb052b471999-11-18 00:21:59 +0000925 }
Eric Andersenb052b471999-11-18 00:21:59 +0000926
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927 /* Decompress */
928 if (method == DEFLATED) {
929
930 int res = inflate();
931
932 if (res == 3) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000933 errorMsg(memory_exhausted);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 } else if (res != 0) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000935 errorMsg("invalid compressed data--format violated");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 }
937
938 } else {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000939 errorMsg("internal error, invalid method");
Eric Andersenb052b471999-11-18 00:21:59 +0000940 }
Eric Andersenb052b471999-11-18 00:21:59 +0000941
Erik Andersene49d5ec2000-02-08 19:58:47 +0000942 /* Get the crc and original length */
943 if (!pkzip) {
944 /* crc32 (see algorithm.doc)
945 * uncompressed input size modulo 2^32
946 */
947 for (n = 0; n < 8; n++) {
948 buf[n] = (uch) get_byte(); /* may cause an error if EOF */
949 }
950 orig_crc = LG(buf);
951 orig_len = LG(buf + 4);
Eric Andersenb052b471999-11-18 00:21:59 +0000952
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953 } else if (ext_header) { /* If extended header, check it */
954 /* signature - 4bytes: 0x50 0x4b 0x07 0x08
955 * CRC-32 value
956 * compressed size 4-bytes
957 * uncompressed size 4-bytes
958 */
959 for (n = 0; n < EXTHDR; n++) {
960 buf[n] = (uch) get_byte(); /* may cause an error if EOF */
961 }
962 orig_crc = LG(buf + 4);
963 orig_len = LG(buf + 12);
964 }
965
966 /* Validate decompression */
967 if (orig_crc != updcrc(outbuf, 0)) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000968 errorMsg("invalid compressed data--crc error");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 }
970 if (orig_len != (ulg) bytes_out) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000971 errorMsg("invalid compressed data--length error");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000972 }
973
974 /* Check if there are more entries in a pkzip file */
975 if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) {
976 WARN((stderr, "has more than one entry--rest ignored\n"));
977 }
978 ext_header = pkzip = 0; /* for next file */
979 return OK;
Eric Andersenb052b471999-11-18 00:21:59 +0000980}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000981
Eric Andersenb052b471999-11-18 00:21:59 +0000982/* util.c -- utility functions for gzip support
983 * Copyright (C) 1992-1993 Jean-loup Gailly
984 * This is free software; you can redistribute it and/or modify it under the
985 * terms of the GNU General Public License, see the file COPYING.
986 */
987
988#include <ctype.h>
989#include <errno.h>
990#include <sys/types.h>
991
992#ifdef HAVE_UNISTD_H
993# include <unistd.h>
994#endif
995#ifndef NO_FCNTL_H
996# include <fcntl.h>
997#endif
998
999#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
1000# include <stdlib.h>
1001#else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001002extern int errno;
Eric Andersenb052b471999-11-18 00:21:59 +00001003#endif
1004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005static const ulg crc_32_tab[]; /* crc table, defined below */
Eric Andersenb052b471999-11-18 00:21:59 +00001006
1007/* ===========================================================================
1008 * Run a set of bytes through the crc shift register. If s is a NULL
1009 * pointer, then initialize the crc shift register contents instead.
1010 * Return the current crc in either case.
1011 */
1012ulg updcrc(s, n)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001013uch *s; /* pointer to bytes to pump through */
1014unsigned n; /* number of bytes in s[] */
Eric Andersenb052b471999-11-18 00:21:59 +00001015{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001016 register ulg c; /* temporary variable */
Eric Andersenb052b471999-11-18 00:21:59 +00001017
Erik Andersene49d5ec2000-02-08 19:58:47 +00001018 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */
Eric Andersenb052b471999-11-18 00:21:59 +00001019
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020 if (s == NULL) {
1021 c = 0xffffffffL;
1022 } else {
1023 c = crc;
1024 if (n)
1025 do {
1026 c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8);
1027 } while (--n);
1028 }
1029 crc = c;
1030 return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */
Eric Andersenb052b471999-11-18 00:21:59 +00001031}
1032
1033/* ===========================================================================
1034 * Clear input and output buffers
1035 */
Erik Andersen330fd2b2000-05-19 05:35:19 +00001036void clear_bufs(void)
Eric Andersenb052b471999-11-18 00:21:59 +00001037{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 outcnt = 0;
1039 insize = inptr = 0;
1040 bytes_in = bytes_out = 0L;
Eric Andersenb052b471999-11-18 00:21:59 +00001041}
1042
1043/* ===========================================================================
1044 * Fill the input buffer. This is called only when the buffer is empty.
1045 */
1046int fill_inbuf(eof_ok)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047int eof_ok; /* set if EOF acceptable as a result */
Eric Andersenb052b471999-11-18 00:21:59 +00001048{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049 int len;
Eric Andersenb052b471999-11-18 00:21:59 +00001050
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051 /* Read as much as possible */
1052 insize = 0;
1053 errno = 0;
1054 do {
1055 len = read(ifd, (char *) inbuf + insize, INBUFSIZ - insize);
1056 if (len == 0 || len == EOF)
1057 break;
1058 insize += len;
1059 } while (insize < INBUFSIZ);
Eric Andersenb052b471999-11-18 00:21:59 +00001060
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 if (insize == 0) {
1062 if (eof_ok)
1063 return EOF;
Erik Andersen330fd2b2000-05-19 05:35:19 +00001064 read_error_msg();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001065 }
1066 bytes_in += (ulg) insize;
1067 inptr = 1;
1068 return inbuf[0];
Eric Andersenb052b471999-11-18 00:21:59 +00001069}
1070
1071/* ===========================================================================
1072 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
1073 * (used for the compressed data only)
1074 */
1075void flush_outbuf()
1076{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001077 if (outcnt == 0)
1078 return;
Eric Andersenb052b471999-11-18 00:21:59 +00001079
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 if (!test_mode)
1081 write_buf(ofd, (char *) outbuf, outcnt);
1082 bytes_out += (ulg) outcnt;
1083 outcnt = 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001084}
1085
1086/* ===========================================================================
1087 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
1088 * (Used for the decompressed data only.)
1089 */
1090void flush_window()
1091{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 if (outcnt == 0)
1093 return;
1094 updcrc(window, outcnt);
Eric Andersenb052b471999-11-18 00:21:59 +00001095
Erik Andersene49d5ec2000-02-08 19:58:47 +00001096 if (!test_mode)
1097 write_buf(ofd, (char *) window, outcnt);
1098 bytes_out += (ulg) outcnt;
1099 outcnt = 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001100}
1101
1102/* ===========================================================================
1103 * Does the same as write(), but also handles partial pipe writes and checks
1104 * for error return.
1105 */
1106void write_buf(fd, buf, cnt)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107int fd;
Erik Andersen61677fe2000-04-13 01:18:56 +00001108void * buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001109unsigned cnt;
Eric Andersenb052b471999-11-18 00:21:59 +00001110{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001111 unsigned n;
Eric Andersenb052b471999-11-18 00:21:59 +00001112
Erik Andersene49d5ec2000-02-08 19:58:47 +00001113 while ((n = write(fd, buf, cnt)) != cnt) {
1114 if (n == (unsigned) (-1)) {
Erik Andersen330fd2b2000-05-19 05:35:19 +00001115 write_error_msg();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 }
1117 cnt -= n;
Erik Andersen61677fe2000-04-13 01:18:56 +00001118 buf = (void *) ((char *) buf + n);
Eric Andersenb052b471999-11-18 00:21:59 +00001119 }
Eric Andersenb052b471999-11-18 00:21:59 +00001120}
1121
1122#if defined(NO_STRING_H) && !defined(STDC_HEADERS)
1123
1124/* Provide missing strspn and strcspn functions. */
1125
1126# ifndef __STDC__
1127# define const
1128# endif
1129
Erik Andersen61677fe2000-04-13 01:18:56 +00001130int strspn (const char *s, const char *accept);
1131int strcspn (const char *s, const char *reject);
Eric Andersenb052b471999-11-18 00:21:59 +00001132
1133/* ========================================================================
1134 * Return the length of the maximum initial segment
1135 * of s which contains only characters in accept.
1136 */
1137int strspn(s, accept)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001138const char *s;
1139const char *accept;
Eric Andersenb052b471999-11-18 00:21:59 +00001140{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001141 register const char *p;
1142 register const char *a;
1143 register int count = 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001144
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 for (p = s; *p != '\0'; ++p) {
1146 for (a = accept; *a != '\0'; ++a) {
1147 if (*p == *a)
1148 break;
1149 }
1150 if (*a == '\0')
1151 return count;
1152 ++count;
Eric Andersenb052b471999-11-18 00:21:59 +00001153 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 return count;
Eric Andersenb052b471999-11-18 00:21:59 +00001155}
1156
1157/* ========================================================================
1158 * Return the length of the maximum inital segment of s
1159 * which contains no characters from reject.
1160 */
1161int strcspn(s, reject)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001162const char *s;
1163const char *reject;
Eric Andersenb052b471999-11-18 00:21:59 +00001164{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001165 register int count = 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001166
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 while (*s != '\0') {
1168 if (strchr(reject, *s++) != NULL)
1169 return count;
1170 ++count;
1171 }
1172 return count;
Eric Andersenb052b471999-11-18 00:21:59 +00001173}
1174
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175#endif /* NO_STRING_H */
Eric Andersenb052b471999-11-18 00:21:59 +00001176
1177
1178/* ========================================================================
1179 * Error handlers.
1180 */
Erik Andersen330fd2b2000-05-19 05:35:19 +00001181void read_error_msg()
Eric Andersenb052b471999-11-18 00:21:59 +00001182{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 fprintf(stderr, "\n");
1184 if (errno != 0) {
1185 perror("");
1186 } else {
1187 fprintf(stderr, "unexpected end of file\n");
1188 }
1189 abort_gzip();
Eric Andersenb052b471999-11-18 00:21:59 +00001190}
1191
Erik Andersen330fd2b2000-05-19 05:35:19 +00001192void write_error_msg()
Eric Andersenb052b471999-11-18 00:21:59 +00001193{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001194 fprintf(stderr, "\n");
1195 perror("");
1196 abort_gzip();
Eric Andersenb052b471999-11-18 00:21:59 +00001197}
1198
1199
1200/* ========================================================================
Eric Andersenb052b471999-11-18 00:21:59 +00001201 * Table of CRC-32's of all single-byte values (made by makecrc.c)
1202 */
1203static const ulg crc_32_tab[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
1205 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
1206 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
1207 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
1208 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
1209 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
1210 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
1211 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
1212 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
1213 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
1214 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
1215 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
1216 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
1217 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
1218 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
1219 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
1220 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
1221 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
1222 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
1223 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
1224 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
1225 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
1226 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
1227 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
1228 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
1229 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
1230 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
1231 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
1232 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
1233 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
1234 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
1235 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
1236 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
1237 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
1238 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
1239 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
1240 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
1241 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
1242 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
1243 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
1244 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
1245 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
1246 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
1247 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
1248 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
1249 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
1250 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
1251 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
1252 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
1253 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
1254 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
1255 0x2d02ef8dL
Eric Andersenb052b471999-11-18 00:21:59 +00001256};
Erik Andersene49d5ec2000-02-08 19:58:47 +00001257
Eric Andersenb052b471999-11-18 00:21:59 +00001258/* inflate.c -- Not copyrighted 1992 by Mark Adler
1259 version c10p1, 10 January 1993 */
1260
1261/* You can do whatever you like with this source file, though I would
1262 prefer that if you modify it and redistribute it that you include
1263 comments to that effect with your name and the date. Thank you.
1264 [The history has been moved to the file ChangeLog.]
1265 */
1266
1267/*
1268 Inflate deflated (PKZIP's method 8 compressed) data. The compression
1269 method searches for as much of the current string of bytes (up to a
1270 length of 258) in the previous 32K bytes. If it doesn't find any
1271 matches (of at least length 3), it codes the next byte. Otherwise, it
1272 codes the length of the matched string and its distance backwards from
1273 the current position. There is a single Huffman code that codes both
1274 single bytes (called "literals") and match lengths. A second Huffman
1275 code codes the distance information, which follows a length code. Each
1276 length or distance code actually represents a base value and a number
1277 of "extra" (sometimes zero) bits to get to add to the base value. At
1278 the end of each deflated block is a special end-of-block (EOB) literal/
1279 length code. The decoding process is basically: get a literal/length
1280 code; if EOB then done; if a literal, emit the decoded byte; if a
1281 length then get the distance and emit the referred-to bytes from the
1282 sliding window of previously emitted data.
1283
1284 There are (currently) three kinds of inflate blocks: stored, fixed, and
1285 dynamic. The compressor deals with some chunk of data at a time, and
1286 decides which method to use on a chunk-by-chunk basis. A chunk might
1287 typically be 32K or 64K. If the chunk is uncompressible, then the
1288 "stored" method is used. In this case, the bytes are simply stored as
1289 is, eight bits per byte, with none of the above coding. The bytes are
1290 preceded by a count, since there is no longer an EOB code.
1291
1292 If the data is compressible, then either the fixed or dynamic methods
1293 are used. In the dynamic method, the compressed data is preceded by
1294 an encoding of the literal/length and distance Huffman codes that are
1295 to be used to decode this block. The representation is itself Huffman
1296 coded, and so is preceded by a description of that code. These code
1297 descriptions take up a little space, and so for small blocks, there is
1298 a predefined set of codes, called the fixed codes. The fixed method is
1299 used if the block codes up smaller that way (usually for quite small
1300 chunks), otherwise the dynamic method is used. In the latter case, the
1301 codes are customized to the probabilities in the current block, and so
1302 can code it much better than the pre-determined fixed codes.
1303
1304 The Huffman codes themselves are decoded using a mutli-level table
1305 lookup, in order to maximize the speed of decoding plus the speed of
1306 building the decoding tables. See the comments below that precede the
1307 lbits and dbits tuning parameters.
1308 */
1309
1310
1311/*
1312 Notes beyond the 1.93a appnote.txt:
1313
1314 1. Distance pointers never point before the beginning of the output
1315 stream.
1316 2. Distance pointers can point back across blocks, up to 32k away.
1317 3. There is an implied maximum of 7 bits for the bit length table and
1318 15 bits for the actual data.
1319 4. If only one code exists, then it is encoded using one bit. (Zero
1320 would be more efficient, but perhaps a little confusing.) If two
1321 codes exist, they are coded using one bit each (0 and 1).
1322 5. There is no way of sending zero distance codes--a dummy must be
1323 sent if there are none. (History: a pre 2.0 version of PKZIP would
1324 store blocks with no distance codes, but this was discovered to be
1325 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
1326 zero distance codes, which is sent as one code of zero bits in
1327 length.
1328 6. There are up to 286 literal/length codes. Code 256 represents the
1329 end-of-block. Note however that the static length tree defines
1330 288 codes just to fill out the Huffman codes. Codes 286 and 287
1331 cannot be used though, since there is no length base or extra bits
1332 defined for them. Similarly, there are up to 30 distance codes.
1333 However, static trees define 32 codes (all 5 bits) to fill out the
1334 Huffman codes, but the last two had better not show up in the data.
1335 7. Unzip can check dynamic Huffman blocks for complete code sets.
1336 The exception is that a single code would not be complete (see #4).
1337 8. The five bits following the block type is really the number of
1338 literal codes sent minus 257.
1339 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
1340 (1+6+6). Therefore, to output three times the length, you output
1341 three codes (1+1+1), whereas to output four times the same length,
1342 you only need two codes (1+3). Hmm.
1343 10. In the tree reconstruction algorithm, Code = Code + Increment
1344 only if BitLength(i) is not zero. (Pretty obvious.)
1345 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
1346 12. Note: length code 284 can represent 227-258, but length code 285
1347 really is 258. The last length deserves its own, short code
1348 since it gets used a lot in very redundant files. The length
1349 258 is special since 258 - 3 (the min match length) is 255.
1350 13. The literal/length and distance code bit lengths are read as a
1351 single stream of lengths. It is possible (and advantageous) for
1352 a repeat code (16, 17, or 18) to go across the boundary between
1353 the two sets of lengths.
1354 */
1355
1356#include <sys/types.h>
1357
1358#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
1359# include <stdlib.h>
1360#endif
1361
1362
1363#define slide window
1364
1365/* Huffman code lookup table entry--this entry is four bytes for machines
1366 that have 16-bit pointers (e.g. PC's in the small or medium model).
1367 Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
1368 means that v is a literal, 16 < e < 32 means that v is a pointer to
1369 the next table, which codes e - 16 bits, and lastly e == 99 indicates
1370 an unused code. If a code with e == 99 is looked up, this implies an
1371 error in the data. */
1372struct huft {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001373 uch e; /* number of extra bits or operation */
1374 uch b; /* number of bits in this code or subcode */
1375 union {
1376 ush n; /* literal, length base, or distance base */
1377 struct huft *t; /* pointer to next level of table */
1378 } v;
Eric Andersenb052b471999-11-18 00:21:59 +00001379};
1380
1381
1382/* Function prototypes */
Erik Andersen61677fe2000-04-13 01:18:56 +00001383int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
1384 struct huft **, int *);
1385int huft_free (struct huft *);
1386int inflate_codes (struct huft *, struct huft *, int, int);
1387int inflate_stored (void);
1388int inflate_fixed (void);
1389int inflate_dynamic (void);
1390int inflate_block (int *);
1391int inflate (void);
Eric Andersenb052b471999-11-18 00:21:59 +00001392
1393
1394/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
1395 stream to find repeated byte strings. This is implemented here as a
1396 circular buffer. The index is updated simply by incrementing and then
1397 and'ing with 0x7fff (32K-1). */
1398/* It is left to other modules to supply the 32K area. It is assumed
1399 to be usable as if it were declared "uch slide[32768];" or as just
1400 "uch *slide;" and then malloc'ed in the latter case. The definition
1401 must be in unzip.h, included above. */
1402/* unsigned wp; current position in slide */
1403#define wp outcnt
1404#define flush_output(w) (wp=(w),flush_window())
1405
1406/* Tables for deflate from PKZIP's appnote.txt. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001407static unsigned border[] = { /* Order of the bit length code lengths */
1408 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
1409};
1410static ush cplens[] = { /* Copy lengths for literal codes 257..285 */
1411 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1412 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
1413};
1414
1415 /* note: see note #13 above about the 258 in this list. */
1416static ush cplext[] = { /* Extra bits for literal codes 257..285 */
1417 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1418 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
1419}; /* 99==invalid */
1420static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
1421 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1422 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1423 8193, 12289, 16385, 24577
1424};
1425static ush cpdext[] = { /* Extra bits for distance codes */
1426 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1427 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1428 12, 12, 13, 13
1429};
Eric Andersenb052b471999-11-18 00:21:59 +00001430
1431
1432
1433/* Macros for inflate() bit peeking and grabbing.
1434 The usage is:
1435
1436 NEEDBITS(j)
1437 x = b & mask_bits[j];
1438 DUMPBITS(j)
1439
1440 where NEEDBITS makes sure that b has at least j bits in it, and
1441 DUMPBITS removes the bits from b. The macros use the variable k
1442 for the number of bits in b. Normally, b and k are register
1443 variables for speed, and are initialized at the beginning of a
1444 routine that uses these macros from a global bit buffer and count.
1445
1446 If we assume that EOB will be the longest code, then we will never
1447 ask for bits with NEEDBITS that are beyond the end of the stream.
1448 So, NEEDBITS should not read any more bytes than are needed to
1449 meet the request. Then no bytes need to be "returned" to the buffer
1450 at the end of the last block.
1451
1452 However, this assumption is not true for fixed blocks--the EOB code
1453 is 7 bits, but the other literal/length codes can be 8 or 9 bits.
1454 (The EOB code is shorter than other codes because fixed blocks are
1455 generally short. So, while a block always has an EOB, many other
1456 literal/length codes have a significantly lower probability of
1457 showing up at all.) However, by making the first table have a
1458 lookup of seven bits, the EOB code will be found in that first
1459 lookup, and so will not require that too many bits be pulled from
1460 the stream.
1461 */
1462
Erik Andersene49d5ec2000-02-08 19:58:47 +00001463ulg bb; /* bit buffer */
1464unsigned bk; /* bits in bit buffer */
Eric Andersenb052b471999-11-18 00:21:59 +00001465
1466ush mask_bits[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001467 0x0000,
1468 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
1469 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
Eric Andersenb052b471999-11-18 00:21:59 +00001470};
1471
1472#ifdef CRYPT
Erik Andersene49d5ec2000-02-08 19:58:47 +00001473uch cc;
1474
Eric Andersenb052b471999-11-18 00:21:59 +00001475# define NEXTBYTE() (cc = get_byte(), zdecode(cc), cc)
1476#else
1477# define NEXTBYTE() (uch)get_byte()
1478#endif
1479#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
1480#define DUMPBITS(n) {b>>=(n);k-=(n);}
1481
1482
1483/*
1484 Huffman code decoding is performed using a multi-level table lookup.
1485 The fastest way to decode is to simply build a lookup table whose
1486 size is determined by the longest code. However, the time it takes
1487 to build this table can also be a factor if the data being decoded
1488 is not very long. The most common codes are necessarily the
1489 shortest codes, so those codes dominate the decoding time, and hence
1490 the speed. The idea is you can have a shorter table that decodes the
1491 shorter, more probable codes, and then point to subsidiary tables for
1492 the longer codes. The time it costs to decode the longer codes is
1493 then traded against the time it takes to make longer tables.
1494
1495 This results of this trade are in the variables lbits and dbits
1496 below. lbits is the number of bits the first level table for literal/
1497 length codes can decode in one step, and dbits is the same thing for
1498 the distance codes. Subsequent tables are also less than or equal to
1499 those sizes. These values may be adjusted either when all of the
1500 codes are shorter than that, in which case the longest code length in
1501 bits is used, or when the shortest code is *longer* than the requested
1502 table size, in which case the length of the shortest code in bits is
1503 used.
1504
1505 There are two different values for the two tables, since they code a
1506 different number of possibilities each. The literal/length table
1507 codes 286 possible values, or in a flat code, a little over eight
1508 bits. The distance table codes 30 possible values, or a little less
1509 than five bits, flat. The optimum values for speed end up being
1510 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1511 The optimum values may differ though from machine to machine, and
1512 possibly even between compilers. Your mileage may vary.
1513 */
1514
1515
Erik Andersene49d5ec2000-02-08 19:58:47 +00001516int lbits = 9; /* bits in base literal/length lookup table */
1517int dbits = 6; /* bits in base distance lookup table */
Eric Andersenb052b471999-11-18 00:21:59 +00001518
1519
1520/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001521#define BMAX 16 /* maximum bit length of any code (16 for explode) */
1522#define N_MAX 288 /* maximum number of codes in any set */
Eric Andersenb052b471999-11-18 00:21:59 +00001523
1524
Erik Andersene49d5ec2000-02-08 19:58:47 +00001525unsigned hufts; /* track memory usage */
Eric Andersenb052b471999-11-18 00:21:59 +00001526
1527
1528int huft_build(b, n, s, d, e, t, m)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001529unsigned *b; /* code lengths in bits (all assumed <= BMAX) */
1530unsigned n; /* number of codes (assumed <= N_MAX) */
1531unsigned s; /* number of simple-valued codes (0..s-1) */
1532ush *d; /* list of base values for non-simple codes */
1533ush *e; /* list of extra bits for non-simple codes */
1534struct huft **t; /* result: starting table */
1535int *m; /* maximum lookup bits, returns actual */
1536
Eric Andersenb052b471999-11-18 00:21:59 +00001537/* Given a list of code lengths and a maximum table size, make a set of
1538 tables to decode that set of codes. Return zero on success, one if
1539 the given code set is incomplete (the tables are still built in this
1540 case), two if the input is invalid (all zero length codes or an
1541 oversubscribed set of lengths), and three if not enough memory. */
1542{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001543 unsigned a; /* counter for codes of length k */
1544 unsigned c[BMAX + 1]; /* bit length count table */
1545 unsigned f; /* i repeats in table every f entries */
1546 int g; /* maximum code length */
1547 int h; /* table level */
1548 register unsigned i; /* counter, current code */
1549 register unsigned j; /* counter */
1550 register int k; /* number of bits in current code */
1551 int l; /* bits per table (returned in m) */
1552 register unsigned *p; /* pointer into c[], b[], or v[] */
1553 register struct huft *q; /* points to current table */
1554 struct huft r; /* table entry for structure assignment */
1555 struct huft *u[BMAX]; /* table stack */
1556 unsigned v[N_MAX]; /* values in order of bit length */
1557 register int w; /* bits before this table == (l * h) */
1558 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
1559 unsigned *xp; /* pointer into x */
1560 int y; /* number of dummy codes added */
1561 unsigned z; /* number of entries in current table */
Eric Andersenb052b471999-11-18 00:21:59 +00001562
1563
Erik Andersene49d5ec2000-02-08 19:58:47 +00001564 /* Generate counts for each bit length */
1565 memzero(c, sizeof(c));
1566 p = b;
1567 i = n;
1568 do {
1569 Tracecv(*p,
1570 (stderr,
1571 (n - i >= ' '
1572 && n - i <= '~' ? "%c %d\n" : "0x%x %d\n"), n - i, *p));
1573 c[*p]++; /* assume all entries <= BMAX */
1574 p++; /* Can't combine with above line (Solaris bug) */
1575 } while (--i);
1576 if (c[0] == n) { /* null input--all zero length codes */
1577 *t = (struct huft *) NULL;
1578 *m = 0;
1579 return 0;
1580 }
Eric Andersenb052b471999-11-18 00:21:59 +00001581
1582
Erik Andersene49d5ec2000-02-08 19:58:47 +00001583 /* Find minimum and maximum length, bound *m by those */
1584 l = *m;
1585 for (j = 1; j <= BMAX; j++)
1586 if (c[j])
1587 break;
1588 k = j; /* minimum code length */
1589 if ((unsigned) l < j)
1590 l = j;
1591 for (i = BMAX; i; i--)
1592 if (c[i])
1593 break;
1594 g = i; /* maximum code length */
1595 if ((unsigned) l > i)
1596 l = i;
1597 *m = l;
Eric Andersenb052b471999-11-18 00:21:59 +00001598
1599
Erik Andersene49d5ec2000-02-08 19:58:47 +00001600 /* Adjust last length count to fill out codes, if needed */
1601 for (y = 1 << j; j < i; j++, y <<= 1)
1602 if ((y -= c[j]) < 0)
1603 return 2; /* bad input: more codes than bits */
1604 if ((y -= c[i]) < 0)
1605 return 2;
1606 c[i] += y;
Eric Andersenb052b471999-11-18 00:21:59 +00001607
1608
Erik Andersene49d5ec2000-02-08 19:58:47 +00001609 /* Generate starting offsets into the value table for each length */
1610 x[1] = j = 0;
1611 p = c + 1;
1612 xp = x + 2;
1613 while (--i) { /* note that i == g from above */
1614 *xp++ = (j += *p++);
1615 }
Eric Andersenb052b471999-11-18 00:21:59 +00001616
1617
Erik Andersene49d5ec2000-02-08 19:58:47 +00001618 /* Make a table of values in order of bit lengths */
1619 p = b;
1620 i = 0;
1621 do {
1622 if ((j = *p++) != 0)
1623 v[x[j]++] = i;
1624 } while (++i < n);
Eric Andersenb052b471999-11-18 00:21:59 +00001625
1626
Erik Andersene49d5ec2000-02-08 19:58:47 +00001627 /* Generate the Huffman codes and for each, make the table entries */
1628 x[0] = i = 0; /* first Huffman code is zero */
1629 p = v; /* grab values in bit order */
1630 h = -1; /* no tables yet--level -1 */
1631 w = -l; /* bits decoded == (l * h) */
1632 u[0] = (struct huft *) NULL; /* just to keep compilers happy */
1633 q = (struct huft *) NULL; /* ditto */
1634 z = 0; /* ditto */
Eric Andersenb052b471999-11-18 00:21:59 +00001635
Erik Andersene49d5ec2000-02-08 19:58:47 +00001636 /* go through the bit lengths (k already is bits in shortest code) */
1637 for (; k <= g; k++) {
1638 a = c[k];
1639 while (a--) {
1640 /* here i is the Huffman code of length k bits for value *p */
1641 /* make tables up to required level */
1642 while (k > w + l) {
1643 h++;
1644 w += l; /* previous table always l bits */
Eric Andersenb052b471999-11-18 00:21:59 +00001645
Erik Andersene49d5ec2000-02-08 19:58:47 +00001646 /* compute minimum size table less than or equal to l bits */
1647 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
1648 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */
1649 f -= a + 1; /* deduct codes from patterns left */
1650 xp = c + k;
1651 while (++j < z) { /* try smaller tables up to z bits */
1652 if ((f <<= 1) <= *++xp)
1653 break; /* enough codes to use up j bits */
1654 f -= *xp; /* else deduct codes from patterns */
1655 }
1656 }
1657 z = 1 << j; /* table entries for j-bit table */
Eric Andersenb052b471999-11-18 00:21:59 +00001658
Erik Andersene49d5ec2000-02-08 19:58:47 +00001659 /* allocate and link in new table */
1660 if (
1661 (q =
1662 (struct huft *) malloc((z + 1) *
1663 sizeof(struct huft))) ==
1664 (struct huft *) NULL) {
1665 if (h)
1666 huft_free(u[0]);
1667 return 3; /* not enough memory */
1668 }
1669 hufts += z + 1; /* track memory usage */
1670 *t = q + 1; /* link to list for huft_free() */
1671 *(t = &(q->v.t)) = (struct huft *) NULL;
1672 u[h] = ++q; /* table starts after link */
Eric Andersenb052b471999-11-18 00:21:59 +00001673
Erik Andersene49d5ec2000-02-08 19:58:47 +00001674 /* connect to last table, if there is one */
1675 if (h) {
1676 x[h] = i; /* save pattern for backing up */
1677 r.b = (uch) l; /* bits to dump before this table */
1678 r.e = (uch) (16 + j); /* bits in this table */
1679 r.v.t = q; /* pointer to this table */
1680 j = i >> (w - l); /* (get around Turbo C bug) */
1681 u[h - 1][j] = r; /* connect to last table */
1682 }
1683 }
Eric Andersenb052b471999-11-18 00:21:59 +00001684
Erik Andersene49d5ec2000-02-08 19:58:47 +00001685 /* set up table entry in r */
1686 r.b = (uch) (k - w);
1687 if (p >= v + n)
1688 r.e = 99; /* out of values--invalid code */
1689 else if (*p < s) {
1690 r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
1691 r.v.n = (ush) (*p); /* simple code is just the value */
1692 p++; /* one compiler does not like *p++ */
1693 } else {
1694 r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
1695 r.v.n = d[*p++ - s];
1696 }
Eric Andersenb052b471999-11-18 00:21:59 +00001697
Erik Andersene49d5ec2000-02-08 19:58:47 +00001698 /* fill code-like entries with r */
1699 f = 1 << (k - w);
1700 for (j = i >> w; j < z; j += f)
1701 q[j] = r;
Eric Andersenb052b471999-11-18 00:21:59 +00001702
Erik Andersene49d5ec2000-02-08 19:58:47 +00001703 /* backwards increment the k-bit code i */
1704 for (j = 1 << (k - 1); i & j; j >>= 1)
1705 i ^= j;
1706 i ^= j;
Eric Andersenb052b471999-11-18 00:21:59 +00001707
Erik Andersene49d5ec2000-02-08 19:58:47 +00001708 /* backup over finished tables */
1709 while ((i & ((1 << w) - 1)) != x[h]) {
1710 h--; /* don't need to update q */
1711 w -= l;
1712 }
1713 }
1714 }
Eric Andersenb052b471999-11-18 00:21:59 +00001715
1716
Erik Andersene49d5ec2000-02-08 19:58:47 +00001717 /* Return true (1) if we were given an incomplete table */
1718 return y != 0 && g != 1;
Eric Andersenb052b471999-11-18 00:21:59 +00001719}
1720
1721
1722
1723int huft_free(t)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001724struct huft *t; /* table to free */
1725
Eric Andersenb052b471999-11-18 00:21:59 +00001726/* Free the malloc'ed tables built by huft_build(), which makes a linked
1727 list of the tables it made, with the links in a dummy first entry of
1728 each table. */
1729{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001730 register struct huft *p, *q;
Eric Andersenb052b471999-11-18 00:21:59 +00001731
1732
Erik Andersene49d5ec2000-02-08 19:58:47 +00001733 /* Go through linked list, freeing from the malloced (t[-1]) address. */
1734 p = t;
1735 while (p != (struct huft *) NULL) {
1736 q = (--p)->v.t;
1737 free((char *) p);
1738 p = q;
1739 }
1740 return 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001741}
1742
1743
1744int inflate_codes(tl, td, bl, bd)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001745struct huft *tl, *td; /* literal/length and distance decoder tables */
1746int bl, bd; /* number of bits decoded by tl[] and td[] */
1747
Eric Andersenb052b471999-11-18 00:21:59 +00001748/* inflate (decompress) the codes in a deflated (compressed) block.
1749 Return an error code or zero if it all goes ok. */
1750{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001751 register unsigned e; /* table entry flag/number of extra bits */
1752 unsigned n, d; /* length and index for copy */
1753 unsigned w; /* current window position */
1754 struct huft *t; /* pointer to table entry */
1755 unsigned ml, md; /* masks for bl and bd bits */
1756 register ulg b; /* bit buffer */
1757 register unsigned k; /* number of bits in bit buffer */
Eric Andersenb052b471999-11-18 00:21:59 +00001758
1759
Erik Andersene49d5ec2000-02-08 19:58:47 +00001760 /* make local copies of globals */
1761 b = bb; /* initialize bit buffer */
1762 k = bk;
1763 w = wp; /* initialize window position */
Eric Andersenb052b471999-11-18 00:21:59 +00001764
Erik Andersene49d5ec2000-02-08 19:58:47 +00001765 /* inflate the coded data */
1766 ml = mask_bits[bl]; /* precompute masks for speed */
1767 md = mask_bits[bd];
1768 for (;;) { /* do until end of block */
1769 NEEDBITS((unsigned) bl)
1770 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
1771 do {
1772 if (e == 99)
1773 return 1;
1774 DUMPBITS(t->b)
1775 e -= 16;
1776 NEEDBITS(e)
1777 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
1778 > 16);
1779 DUMPBITS(t->b)
1780 if (e == 16) { /* then it's a literal */
1781 slide[w++] = (uch) t->v.n;
1782 Tracevv((stderr, "%c", slide[w - 1]));
1783 if (w == WSIZE) {
1784 flush_output(w);
1785 w = 0;
1786 }
1787 } else { /* it's an EOB or a length */
Eric Andersenb052b471999-11-18 00:21:59 +00001788
Erik Andersene49d5ec2000-02-08 19:58:47 +00001789 /* exit if end of block */
1790 if (e == 15)
1791 break;
Eric Andersenb052b471999-11-18 00:21:59 +00001792
Erik Andersene49d5ec2000-02-08 19:58:47 +00001793 /* get length of block to copy */
1794 NEEDBITS(e)
1795 n = t->v.n + ((unsigned) b & mask_bits[e]);
1796 DUMPBITS(e);
Eric Andersenb052b471999-11-18 00:21:59 +00001797
Erik Andersene49d5ec2000-02-08 19:58:47 +00001798 /* decode distance of block to copy */
1799 NEEDBITS((unsigned) bd)
1800 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
1801 do {
1802 if (e == 99)
1803 return 1;
1804 DUMPBITS(t->b)
1805 e -= 16;
1806 NEEDBITS(e)
1807 }
1808 while (
1809 (e =
1810 (t =
1811 t->v.t + ((unsigned) b & mask_bits[e]))->e) >
1812 16);
1813 DUMPBITS(t->b)
1814 NEEDBITS(e)
1815 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
1816 DUMPBITS(e)
1817 Tracevv((stderr, "\\[%d,%d]", w - d, n));
1818
1819 /* do the copy */
1820 do {
1821 n -= (e =
1822 (e =
1823 WSIZE - ((d &= WSIZE - 1) > w ? d : w)) >
1824 n ? n : e);
Eric Andersenb052b471999-11-18 00:21:59 +00001825#if !defined(NOMEMCPY) && !defined(DEBUG)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001826 if (w - d >= e) { /* (this test assumes unsigned comparison) */
1827 memcpy(slide + w, slide + d, e);
1828 w += e;
1829 d += e;
1830 } else /* do it slow to avoid memcpy() overlap */
1831#endif /* !NOMEMCPY */
1832 do {
1833 slide[w++] = slide[d++];
1834 Tracevv((stderr, "%c", slide[w - 1]));
1835 } while (--e);
1836 if (w == WSIZE) {
1837 flush_output(w);
1838 w = 0;
1839 }
1840 } while (n);
1841 }
1842 }
Eric Andersenb052b471999-11-18 00:21:59 +00001843
1844
Erik Andersene49d5ec2000-02-08 19:58:47 +00001845 /* restore the globals from the locals */
1846 wp = w; /* restore global window pointer */
1847 bb = b; /* restore global bit buffer */
1848 bk = k;
Eric Andersenb052b471999-11-18 00:21:59 +00001849
Erik Andersene49d5ec2000-02-08 19:58:47 +00001850 /* done */
1851 return 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001852}
1853
1854
1855
1856int inflate_stored()
1857/* "decompress" an inflated type 0 (stored) block. */
1858{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001859 unsigned n; /* number of bytes in block */
1860 unsigned w; /* current window position */
1861 register ulg b; /* bit buffer */
1862 register unsigned k; /* number of bits in bit buffer */
Eric Andersenb052b471999-11-18 00:21:59 +00001863
1864
Erik Andersene49d5ec2000-02-08 19:58:47 +00001865 /* make local copies of globals */
1866 b = bb; /* initialize bit buffer */
1867 k = bk;
1868 w = wp; /* initialize window position */
Eric Andersenb052b471999-11-18 00:21:59 +00001869
1870
Erik Andersene49d5ec2000-02-08 19:58:47 +00001871 /* go to byte boundary */
1872 n = k & 7;
1873 DUMPBITS(n);
Eric Andersenb052b471999-11-18 00:21:59 +00001874
1875
Erik Andersene49d5ec2000-02-08 19:58:47 +00001876 /* get the length and its complement */
1877 NEEDBITS(16)
1878 n = ((unsigned) b & 0xffff);
1879 DUMPBITS(16)
1880 NEEDBITS(16)
1881 if (n != (unsigned) ((~b) & 0xffff))
1882 return 1; /* error in compressed data */
1883 DUMPBITS(16)
Eric Andersenb052b471999-11-18 00:21:59 +00001884
1885
Erik Andersene49d5ec2000-02-08 19:58:47 +00001886 /* read and output the compressed data */
1887 while (n--) {
1888 NEEDBITS(8)
1889 slide[w++] = (uch) b;
1890 if (w == WSIZE) {
1891 flush_output(w);
1892 w = 0;
1893 }
1894 DUMPBITS(8)
1895 }
Eric Andersenb052b471999-11-18 00:21:59 +00001896
1897
Erik Andersene49d5ec2000-02-08 19:58:47 +00001898 /* restore the globals from the locals */
1899 wp = w; /* restore global window pointer */
1900 bb = b; /* restore global bit buffer */
1901 bk = k;
1902 return 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001903}
1904
1905
1906
1907int inflate_fixed()
1908/* decompress an inflated type 1 (fixed Huffman codes) block. We should
1909 either replace this with a custom decoder, or at least precompute the
1910 Huffman tables. */
1911{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001912 int i; /* temporary variable */
1913 struct huft *tl; /* literal/length code table */
1914 struct huft *td; /* distance code table */
1915 int bl; /* lookup bits for tl */
1916 int bd; /* lookup bits for td */
1917 unsigned l[288]; /* length list for huft_build */
Eric Andersenb052b471999-11-18 00:21:59 +00001918
1919
Erik Andersene49d5ec2000-02-08 19:58:47 +00001920 /* set up literal table */
1921 for (i = 0; i < 144; i++)
1922 l[i] = 8;
1923 for (; i < 256; i++)
1924 l[i] = 9;
1925 for (; i < 280; i++)
1926 l[i] = 7;
1927 for (; i < 288; i++) /* make a complete, but wrong code set */
1928 l[i] = 8;
1929 bl = 7;
1930 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
1931 return i;
Eric Andersenb052b471999-11-18 00:21:59 +00001932
1933
Erik Andersene49d5ec2000-02-08 19:58:47 +00001934 /* set up distance table */
1935 for (i = 0; i < 30; i++) /* make an incomplete code set */
1936 l[i] = 5;
1937 bd = 5;
1938 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
1939 huft_free(tl);
1940 return i;
1941 }
Eric Andersenb052b471999-11-18 00:21:59 +00001942
1943
Erik Andersene49d5ec2000-02-08 19:58:47 +00001944 /* decompress until an end-of-block code */
1945 if (inflate_codes(tl, td, bl, bd))
1946 return 1;
Eric Andersenb052b471999-11-18 00:21:59 +00001947
1948
Erik Andersene49d5ec2000-02-08 19:58:47 +00001949 /* free the decoding tables, return */
1950 huft_free(tl);
1951 huft_free(td);
1952 return 0;
Eric Andersenb052b471999-11-18 00:21:59 +00001953}
1954
1955
1956
1957int inflate_dynamic()
1958/* decompress an inflated type 2 (dynamic Huffman codes) block. */
1959{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001960 int i; /* temporary variables */
1961 unsigned j;
1962 unsigned l; /* last length */
1963 unsigned m; /* mask for bit lengths table */
1964 unsigned n; /* number of lengths to get */
1965 struct huft *tl; /* literal/length code table */
1966 struct huft *td; /* distance code table */
1967 int bl; /* lookup bits for tl */
1968 int bd; /* lookup bits for td */
1969 unsigned nb; /* number of bit length codes */
1970 unsigned nl; /* number of literal/length codes */
1971 unsigned nd; /* number of distance codes */
1972
Eric Andersenb052b471999-11-18 00:21:59 +00001973#ifdef PKZIP_BUG_WORKAROUND
Erik Andersene49d5ec2000-02-08 19:58:47 +00001974 unsigned ll[288 + 32]; /* literal/length and distance code lengths */
Eric Andersenb052b471999-11-18 00:21:59 +00001975#else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001976 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
Eric Andersenb052b471999-11-18 00:21:59 +00001977#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001978 register ulg b; /* bit buffer */
1979 register unsigned k; /* number of bits in bit buffer */
Eric Andersenb052b471999-11-18 00:21:59 +00001980
1981
Erik Andersene49d5ec2000-02-08 19:58:47 +00001982 /* make local bit buffer */
1983 b = bb;
1984 k = bk;
Eric Andersenb052b471999-11-18 00:21:59 +00001985
1986
Erik Andersene49d5ec2000-02-08 19:58:47 +00001987 /* read in table lengths */
1988 NEEDBITS(5)
1989 nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
1990 DUMPBITS(5)
1991 NEEDBITS(5)
1992 nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
1993 DUMPBITS(5)
1994 NEEDBITS(4)
1995 nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
1996 DUMPBITS(4)
Eric Andersenb052b471999-11-18 00:21:59 +00001997#ifdef PKZIP_BUG_WORKAROUND
Erik Andersene49d5ec2000-02-08 19:58:47 +00001998 if (nl > 288 || nd > 32)
Eric Andersenb052b471999-11-18 00:21:59 +00001999#else
Erik Andersene49d5ec2000-02-08 19:58:47 +00002000 if (nl > 286 || nd > 30)
Eric Andersenb052b471999-11-18 00:21:59 +00002001#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00002002 return 1; /* bad lengths */
Eric Andersenb052b471999-11-18 00:21:59 +00002003
2004
Erik Andersene49d5ec2000-02-08 19:58:47 +00002005 /* read in bit-length-code lengths */
2006 for (j = 0; j < nb; j++) {
2007 NEEDBITS(3)
2008 ll[border[j]] = (unsigned) b & 7;
2009 DUMPBITS(3)
2010 }
2011 for (; j < 19; j++)
2012 ll[border[j]] = 0;
Eric Andersenb052b471999-11-18 00:21:59 +00002013
2014
Erik Andersene49d5ec2000-02-08 19:58:47 +00002015 /* build decoding table for trees--single level, 7 bit lookup */
2016 bl = 7;
2017 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
2018 if (i == 1)
2019 huft_free(tl);
2020 return i; /* incomplete code set */
2021 }
Eric Andersenb052b471999-11-18 00:21:59 +00002022
2023
Erik Andersene49d5ec2000-02-08 19:58:47 +00002024 /* read in literal and distance code lengths */
2025 n = nl + nd;
2026 m = mask_bits[bl];
2027 i = l = 0;
2028 while ((unsigned) i < n) {
2029 NEEDBITS((unsigned) bl)
2030 j = (td = tl + ((unsigned) b & m))->b;
2031 DUMPBITS(j)
2032 j = td->v.n;
2033 if (j < 16) /* length of code in bits (0..15) */
2034 ll[i++] = l = j; /* save last length in l */
2035 else if (j == 16) { /* repeat last length 3 to 6 times */
2036 NEEDBITS(2)
2037 j = 3 + ((unsigned) b & 3);
2038 DUMPBITS(2)
2039 if ((unsigned) i + j > n)
2040 return 1;
2041 while (j--)
2042 ll[i++] = l;
2043 } else if (j == 17) { /* 3 to 10 zero length codes */
2044 NEEDBITS(3)
2045 j = 3 + ((unsigned) b & 7);
2046 DUMPBITS(3)
2047 if ((unsigned) i + j > n)
2048 return 1;
2049 while (j--)
2050 ll[i++] = 0;
2051 l = 0;
2052 } else { /* j == 18: 11 to 138 zero length codes */
2053
2054 NEEDBITS(7)
2055 j = 11 + ((unsigned) b & 0x7f);
2056 DUMPBITS(7)
2057 if ((unsigned) i + j > n)
2058 return 1;
2059 while (j--)
2060 ll[i++] = 0;
2061 l = 0;
2062 }
2063 }
Eric Andersenb052b471999-11-18 00:21:59 +00002064
2065
Erik Andersene49d5ec2000-02-08 19:58:47 +00002066 /* free decoding table for trees */
2067 huft_free(tl);
Eric Andersenb052b471999-11-18 00:21:59 +00002068
2069
Erik Andersene49d5ec2000-02-08 19:58:47 +00002070 /* restore the global bit buffer */
2071 bb = b;
2072 bk = k;
Eric Andersenb052b471999-11-18 00:21:59 +00002073
2074
Erik Andersene49d5ec2000-02-08 19:58:47 +00002075 /* build the decoding tables for literal/length and distance codes */
2076 bl = lbits;
2077 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
2078 if (i == 1) {
2079 fprintf(stderr, " incomplete literal tree\n");
2080 huft_free(tl);
2081 }
2082 return i; /* incomplete code set */
2083 }
2084 bd = dbits;
2085 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
2086 if (i == 1) {
2087 fprintf(stderr, " incomplete distance tree\n");
Eric Andersenb052b471999-11-18 00:21:59 +00002088#ifdef PKZIP_BUG_WORKAROUND
Erik Andersene49d5ec2000-02-08 19:58:47 +00002089 i = 0;
2090 }
Eric Andersenb052b471999-11-18 00:21:59 +00002091#else
Erik Andersene49d5ec2000-02-08 19:58:47 +00002092 huft_free(td);
2093 }
2094 huft_free(tl);
2095 return i; /* incomplete code set */
Eric Andersenb052b471999-11-18 00:21:59 +00002096#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00002097 }
Eric Andersenb052b471999-11-18 00:21:59 +00002098
2099
Erik Andersene49d5ec2000-02-08 19:58:47 +00002100 /* decompress until an end-of-block code */
2101 if (inflate_codes(tl, td, bl, bd))
2102 return 1;
Eric Andersenb052b471999-11-18 00:21:59 +00002103
2104
Erik Andersene49d5ec2000-02-08 19:58:47 +00002105 /* free the decoding tables, return */
2106 huft_free(tl);
2107 huft_free(td);
2108 return 0;
Eric Andersenb052b471999-11-18 00:21:59 +00002109}
2110
2111
2112
2113int inflate_block(e)
Erik Andersene49d5ec2000-02-08 19:58:47 +00002114int *e; /* last block flag */
2115
Eric Andersenb052b471999-11-18 00:21:59 +00002116/* decompress an inflated block */
2117{
Erik Andersene49d5ec2000-02-08 19:58:47 +00002118 unsigned t; /* block type */
2119 register ulg b; /* bit buffer */
2120 register unsigned k; /* number of bits in bit buffer */
Eric Andersenb052b471999-11-18 00:21:59 +00002121
2122
Erik Andersene49d5ec2000-02-08 19:58:47 +00002123 /* make local bit buffer */
2124 b = bb;
2125 k = bk;
Eric Andersenb052b471999-11-18 00:21:59 +00002126
2127
Erik Andersene49d5ec2000-02-08 19:58:47 +00002128 /* read in last block bit */
2129 NEEDBITS(1)
2130 * e = (int) b & 1;
2131 DUMPBITS(1)
Eric Andersenb052b471999-11-18 00:21:59 +00002132
2133
Erik Andersene49d5ec2000-02-08 19:58:47 +00002134 /* read in block type */
2135 NEEDBITS(2)
2136 t = (unsigned) b & 3;
2137 DUMPBITS(2)
Eric Andersenb052b471999-11-18 00:21:59 +00002138
2139
Erik Andersene49d5ec2000-02-08 19:58:47 +00002140 /* restore the global bit buffer */
2141 bb = b;
2142 bk = k;
Eric Andersenb052b471999-11-18 00:21:59 +00002143
2144
Erik Andersene49d5ec2000-02-08 19:58:47 +00002145 /* inflate that block type */
2146 if (t == 2)
2147 return inflate_dynamic();
2148 if (t == 0)
2149 return inflate_stored();
2150 if (t == 1)
2151 return inflate_fixed();
Eric Andersenb052b471999-11-18 00:21:59 +00002152
2153
Erik Andersene49d5ec2000-02-08 19:58:47 +00002154 /* bad block type */
2155 return 2;
Eric Andersenb052b471999-11-18 00:21:59 +00002156}
2157
2158
2159
2160int inflate()
2161/* decompress an inflated entry */
2162{
Erik Andersene49d5ec2000-02-08 19:58:47 +00002163 int e; /* last block flag */
2164 int r; /* result code */
2165 unsigned h; /* maximum struct huft's malloc'ed */
Eric Andersenb052b471999-11-18 00:21:59 +00002166
2167
Erik Andersene49d5ec2000-02-08 19:58:47 +00002168 /* initialize window, bit buffer */
2169 wp = 0;
2170 bk = 0;
2171 bb = 0;
Eric Andersenb052b471999-11-18 00:21:59 +00002172
2173
Erik Andersene49d5ec2000-02-08 19:58:47 +00002174 /* decompress until the last block */
2175 h = 0;
2176 do {
2177 hufts = 0;
2178 if ((r = inflate_block(&e)) != 0)
2179 return r;
2180 if (hufts > h)
2181 h = hufts;
2182 } while (!e);
Eric Andersenb052b471999-11-18 00:21:59 +00002183
Erik Andersene49d5ec2000-02-08 19:58:47 +00002184 /* Undo too much lookahead. The next read will be byte aligned so we
2185 * can discard unused bits in the last meaningful byte.
2186 */
2187 while (bk >= 8) {
2188 bk -= 8;
2189 inptr--;
2190 }
Eric Andersenb052b471999-11-18 00:21:59 +00002191
Erik Andersene49d5ec2000-02-08 19:58:47 +00002192 /* flush out slide */
2193 flush_output(wp);
Eric Andersenb052b471999-11-18 00:21:59 +00002194
2195
Erik Andersene49d5ec2000-02-08 19:58:47 +00002196 /* return success */
Eric Andersenb052b471999-11-18 00:21:59 +00002197#ifdef DEBUG
Erik Andersene49d5ec2000-02-08 19:58:47 +00002198 fprintf(stderr, "<%u> ", h);
2199#endif /* DEBUG */
2200 return 0;
Eric Andersenb052b471999-11-18 00:21:59 +00002201}