Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 2 | /* |
| 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 29 | |
| 30 | #include "internal.h" |
Eric Andersen | 1e03add | 2000-07-06 09:56:35 +0000 | [diff] [blame] | 31 | #include <getopt.h> |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 32 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 33 | static const char gunzip_usage[] = |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 34 | "gunzip [OPTION]... FILE\n" |
| 35 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 36 | "\nUncompress FILE (or standard input if FILE is '-').\n\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | "Options:\n" |
| 38 | |
| 39 | "\t-c\tWrite output to standard output\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 40 | "\t-t\tTest compressed file integrity\n" |
| 41 | #endif |
| 42 | ; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 43 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 44 | |
| 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 Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 51 | #define BB_DECLARE_EXTERN |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 52 | #define bb_need_memory_exhausted |
| 53 | #define bb_need_name_too_long |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 54 | #include "messages.c" |
| 55 | |
| 56 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 57 | /* 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | static 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 86 | #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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 129 | |
| 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 Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 139 | # define memzero(s, n) memset ((void *)(s), 0, (n)) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 140 | #else |
| 141 | # include <strings.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | # define strchr index |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 143 | # define strrchr rindex |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | # define memcpy(d, s, n) bcopy((s), (d), (n)) |
| 145 | # define memcmp(s1, s2, n) bcmp((s1), (s2), (n)) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 146 | # 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | typedef unsigned char uch; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 156 | typedef unsigned short ush; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | typedef unsigned long ulg; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 158 | |
| 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | extern int method; /* compression method */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 168 | |
| 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 182 | # define INBUFSIZ 0x2000 /* input buffer size */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 183 | # else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | # define INBUFSIZ 0x8000 /* input buffer size */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 185 | # endif |
| 186 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | #define INBUF_EXTRA 64 /* required by unlzw() */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 188 | |
| 189 | #ifndef OUTBUFSIZ |
| 190 | # ifdef SMALL_MEM |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | # define OUTBUFSIZ 8192 /* output buffer size */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 192 | # else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 193 | # define OUTBUFSIZ 16384 /* output buffer size */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 194 | # endif |
| 195 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | #define OUTBUF_EXTRA 2048 /* required by unlzw() */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 197 | |
| 198 | #define SMALL_MEM |
| 199 | |
| 200 | #ifndef DIST_BUFSIZE |
| 201 | # ifdef SMALL_MEM |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 202 | # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 203 | # else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 205 | # 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 Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 215 | if (array == NULL) errorMsg(memory_exhausted); \ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 216 | } |
| 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | EXTERN(uch, inbuf); /* input buffer */ |
| 226 | EXTERN(uch, outbuf); /* output buffer */ |
| 227 | EXTERN(ush, d_buf); /* buffer for distances, see trees.c */ |
| 228 | EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 229 | #define tab_suffix window |
| 230 | #ifndef MAXSEG_64K |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 231 | # define tab_prefix prev /* hash link (see deflate.c) */ |
| 232 | # define head (prev+WSIZE) /* hash head (see deflate.c) */ |
| 233 | EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 234 | #else |
| 235 | # define tab_prefix0 prev |
| 236 | # define head tab_prefix1 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | EXTERN(ush, tab_prefix0); /* prefix for even codes */ |
| 238 | EXTERN(ush, tab_prefix1); /* prefix for odd codes */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 239 | #endif |
| 240 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | extern unsigned insize; /* valid bytes in inbuf */ |
| 242 | extern unsigned inptr; /* index of next byte to be processed in inbuf */ |
| 243 | extern unsigned outcnt; /* bytes in output buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 244 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 245 | extern long bytes_in; /* number of input bytes */ |
| 246 | extern long bytes_out; /* number of output bytes */ |
| 247 | extern long header_bytes; /* number of bytes in gzip header */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 248 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 249 | extern long ifile_size; /* input file size, -1 for devices (debug only) */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 250 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | typedef int file_t; /* Do not use stdio */ |
| 252 | |
| 253 | #define NO_FILE (-1) /* in memory compression */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 254 | |
| 255 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 256 | #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 257 | |
| 258 | /* gzip flag byte */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | #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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 266 | |
| 267 | #ifndef WSIZE |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 268 | # define WSIZE 0x8000 /* window size--must be a power of two, and */ |
| 269 | #endif /* at least 32K for zip's deflate method */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 270 | |
| 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 285 | extern int exit_code; /* program exit code */ |
| 286 | extern int verbose; /* be verbose (-v) */ |
| 287 | extern int level; /* compression level */ |
| 288 | extern int test; /* check .z file integrity */ |
| 289 | extern int save_orig_name; /* set if original name must be saved */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 290 | |
| 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 321 | #define seekable() 0 /* force sequential output */ |
| 322 | #define translate_eol 0 /* no option -a yet */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 323 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 324 | #define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 325 | |
| 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 Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 332 | # define Assert(cond,msg) {if(!(cond)) errorMsg(msg);} |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 333 | # 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 Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 351 | extern int unzip (int in, int out); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 352 | |
| 353 | /* in gzip.c */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 354 | RETSIGTYPE abort_gzip (void); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 355 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 356 | /* in deflate.c */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 357 | void lm_init (int pack_level, ush * flags); |
| 358 | ulg deflate (void); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 359 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 360 | /* in trees.c */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 361 | void ct_init (ush * attr, int *method); |
| 362 | int ct_tally (int dist, int lc); |
| 363 | ulg flush_block (char *buf, ulg stored_len, int eof); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 364 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 365 | /* in bits.c */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 366 | void bi_init (file_t zipfile); |
| 367 | void send_bits (int value, int length); |
| 368 | unsigned bi_reverse (unsigned value, int length); |
| 369 | void bi_windup (void); |
| 370 | void copy_block (char *buf, unsigned len, int header); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 371 | |
| 372 | /* in util.c: */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 373 | extern ulg updcrc (uch * s, unsigned n); |
| 374 | extern void clear_bufs (void); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 375 | static int fill_inbuf (int eof_ok); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 376 | extern void flush_outbuf (void); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 377 | static void flush_window (void); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 378 | extern void write_buf (int fd, void * buf, unsigned cnt); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 379 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 380 | #ifndef __linux__ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 381 | static char *basename (char *fname); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 382 | #endif /* not __linux__ */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 383 | void read_error_msg (void); |
| 384 | void write_error_msg (void); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 385 | |
| 386 | /* in inflate.c */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 387 | static int inflate (void); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 388 | |
| 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 404 | #define INIT_BITS 9 /* Initial number of bits per code */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 405 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 406 | #define LZW_MAGIC "\037\235" /* Magic header for lzw files, 1F 9D */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 407 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 408 | #define BIT_MASK 0x1f /* Mask for 'number of compression bits' */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 409 | /* 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | #define LZW_RESERVED 0x60 /* reserved bits */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 424 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 425 | #define CLEAR 256 /* flush the dictionary */ |
| 426 | #define FIRST (CLEAR+1) /* first free entry */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 427 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 428 | extern int maxbits; /* max bits per code for LZW */ |
| 429 | extern int block_mode; /* block compress mode -C compatible with 2.0 */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 430 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 431 | extern int lzw (int in, int out); |
| 432 | extern int unlzw (int in, int out); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 433 | |
| 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 452 | #include <time.h> |
| 453 | #include <fcntl.h> |
| 454 | #include <unistd.h> |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 455 | #include <stdlib.h> |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 456 | #if defined(DIRENT) |
| 457 | # include <dirent.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | typedef struct dirent dir_type; |
| 459 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 460 | # 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 466 | typedef struct direct dir_type; |
| 467 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 468 | # define DIR_OPT "SYSDIR" |
| 469 | # else |
| 470 | # ifdef SYSNDIR |
| 471 | # include <sys/ndir.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 472 | typedef struct direct dir_type; |
| 473 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 474 | # define DIR_OPT "SYSNDIR" |
| 475 | # else |
| 476 | # ifdef NDIR |
| 477 | # include <ndir.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 478 | typedef struct direct dir_type; |
| 479 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 480 | # 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 488 | #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 Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 494 | typedef RETSIGTYPE(*sig_type) (int); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 495 | |
| 496 | #ifndef O_BINARY |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 497 | # define O_BINARY 0 /* creation mode for open() */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 498 | #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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 517 | #define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 518 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 519 | #ifndef MAX_PATH_LEN /* max pathname length */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 520 | # ifdef BUFSIZ |
| 521 | # define MAX_PATH_LEN BUFSIZ |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 522 | # else |
| 523 | # define MAX_PATH_LEN 1024 |
| 524 | # endif |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 525 | #endif |
| 526 | |
| 527 | #ifndef SEEK_END |
| 528 | # define SEEK_END 2 |
| 529 | #endif |
| 530 | |
| 531 | #ifdef NO_OFF_T |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 532 | typedef long off_t; |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 533 | off_t lseek (int fd, off_t offset, int whence); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 534 | #endif |
| 535 | |
| 536 | |
| 537 | /* global buffers */ |
| 538 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 539 | DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA); |
| 540 | DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); |
| 541 | DECLARE(ush, d_buf, DIST_BUFSIZE); |
| 542 | DECLARE(uch, window, 2L * WSIZE); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 543 | #ifndef MAXSEG_64K |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 544 | DECLARE(ush, tab_prefix, 1L << BITS); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 545 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 546 | DECLARE(ush, tab_prefix0, 1L << (BITS - 1)); |
| 547 | DECLARE(ush, tab_prefix1, 1L << (BITS - 1)); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | /* local variables */ |
| 551 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 552 | int test_mode = 0; /* check file integrity option */ |
| 553 | int foreground; /* set if program run in foreground */ |
| 554 | int maxbits = BITS; /* max bits per code for LZW */ |
| 555 | int method = DEFLATED; /* compression method */ |
| 556 | int exit_code = OK; /* program exit code */ |
| 557 | int last_member; /* set for .zip and .Z files */ |
| 558 | int part_nb; /* number of parts in .gz file */ |
| 559 | long ifile_size; /* input file size, -1 for devices (debug only) */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 560 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 561 | long bytes_in; /* number of input bytes */ |
| 562 | long bytes_out; /* number of output bytes */ |
| 563 | long total_in = 0; /* input bytes for all files */ |
| 564 | long total_out = 0; /* output bytes for all files */ |
| 565 | struct stat istat; /* status for input file */ |
| 566 | int ifd; /* input file descriptor */ |
| 567 | int ofd; /* output file descriptor */ |
| 568 | unsigned insize; /* valid bytes in inbuf */ |
| 569 | unsigned inptr; /* index of next byte to be processed in inbuf */ |
| 570 | unsigned outcnt; /* bytes in output buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 571 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 572 | long header_bytes; /* number of bytes in gzip header */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 573 | |
| 574 | /* local functions */ |
| 575 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 576 | local int get_method (int in); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 577 | |
| 578 | #define strequ(s1, s2) (strcmp((s1),(s2)) == 0) |
| 579 | |
| 580 | /* ======================================================================== */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 581 | int gunzip_main(int argc, char **argv) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 582 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 583 | 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 594 | |
Matt Kraai | e58771e | 2000-07-12 15:38:49 +0000 | [diff] [blame] | 595 | if (strcmp(applet_name, "zcat") == 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 596 | to_stdout = 1; |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 597 | if (argc == 1) { |
| 598 | fromstdin = 1; |
| 599 | } |
| 600 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 601 | |
| 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 627 | #ifdef SIGTERM |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 628 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { |
| 629 | (void) signal(SIGTERM, (sig_type) abort_gzip); |
| 630 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 631 | #endif |
| 632 | #ifdef SIGHUP |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 633 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { |
| 634 | (void) signal(SIGHUP, (sig_type) abort_gzip); |
| 635 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 636 | #endif |
| 637 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 638 | file_count = argc - optind; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 639 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 640 | /* 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 645 | #ifndef MAXSEG_64K |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 646 | ALLOC(ush, tab_prefix, 1L << BITS); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 647 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 648 | ALLOC(ush, tab_prefix0, 1L << (BITS - 1)); |
| 649 | ALLOC(ush, tab_prefix1, 1L << (BITS - 1)); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 650 | #endif |
| 651 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 652 | if (fromstdin == 1) { |
| 653 | strcpy(ofname, "stdin"); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 654 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 655 | inFileNum = fileno(stdin); |
| 656 | ifile_size = -1L; /* convention for unknown size */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 657 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 658 | /* Open up the input file */ |
| 659 | if (*argv == '\0') |
| 660 | usage(gunzip_usage); |
| 661 | if (strlen(*argv) > MAX_PATH_LEN) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame^] | 662 | errorMsg(name_too_long); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 663 | exit(WARNING); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 664 | } |
| 665 | strcpy(ifname, *argv); |
| 666 | |
| 667 | /* Open input fille */ |
| 668 | inFileNum = open(ifname, O_RDONLY); |
| 669 | if (inFileNum < 0) { |
| 670 | perror(ifname); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 671 | exit(WARNING); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 672 | } |
| 673 | /* Get the time stamp on the input file. */ |
| 674 | result = stat(ifname, &statBuf); |
| 675 | if (result < 0) { |
| 676 | perror(ifname); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 677 | exit(WARNING); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 678 | } |
| 679 | ifile_size = statBuf.st_size; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 682 | 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) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame^] | 701 | errorMsg(name_too_long); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 702 | exit(WARNING); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | } |
| 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 Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 719 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 720 | outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW); |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 721 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 722 | outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL); |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 723 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 724 | if (outFileNum < 0) { |
| 725 | perror(ofname); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 726 | exit(WARNING); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 727 | } |
| 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 749 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 750 | return(exit_code); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 751 | } |
| 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 | */ |
| 765 | local int get_method(in) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 766 | int in; /* input file descriptor */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 767 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 768 | uch flags; /* compression flags */ |
| 769 | char magic[2]; /* magic header */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 770 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 771 | 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 Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 778 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 779 | if (memcmp(magic, GZIP_MAGIC, 2) == 0) { |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 780 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 781 | method = (int) get_byte(); |
| 782 | if (method != DEFLATED) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame^] | 783 | errorMsg("unknown method %d -- get newer version of gzip\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 784 | method); |
| 785 | exit_code = ERROR; |
| 786 | return -1; |
| 787 | } |
| 788 | flags = (uch) get_byte(); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 789 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 790 | (ulg) get_byte(); /* Ignore time stamp */ |
| 791 | (ulg) get_byte(); |
| 792 | (ulg) get_byte(); |
| 793 | (ulg) get_byte(); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 794 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 795 | (void) get_byte(); /* Ignore extra flags for the moment */ |
| 796 | (void) get_byte(); /* Ignore OS type for the moment */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 797 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 798 | if ((flags & EXTRA_FIELD) != 0) { |
| 799 | unsigned len = (unsigned) get_byte(); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 800 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 801 | len |= ((unsigned) get_byte()) << 8; |
| 802 | |
| 803 | while (len--) |
| 804 | (void) get_byte(); |
| 805 | } |
| 806 | |
| 807 | /* Discard original name if any */ |
| 808 | if ((flags & ORIG_NAME) != 0) { |
| 809 | while (get_char() != 0) /* null */ |
| 810 | ; |
| 811 | } |
| 812 | |
| 813 | /* Discard file comment if any */ |
| 814 | if ((flags & COMMENT) != 0) { |
| 815 | while (get_char() != 0) /* null */ |
| 816 | ; |
| 817 | } |
| 818 | if (part_nb == 1) { |
| 819 | header_bytes = inptr + 2 * sizeof(long); /* include crc and size */ |
| 820 | } |
| 821 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 824 | if (method >= 0) |
| 825 | return method; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 826 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 827 | if (part_nb == 1) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 828 | fprintf(stderr, "\nnot in gzip format\n"); |
| 829 | exit_code = ERROR; |
| 830 | return -1; |
| 831 | } else { |
| 832 | WARN((stderr, "\ndecompression OK, trailing garbage ignored\n")); |
| 833 | return -2; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 834 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 837 | /* ======================================================================== |
| 838 | * Signal and error handler. |
| 839 | */ |
| 840 | RETSIGTYPE abort_gzip() |
| 841 | { |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 842 | exit(ERROR); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 843 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 844 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 845 | /* unzip.c -- decompress files in gzip or pkzip format. |
| 846 | * Copyright (C) 1992-1993 Jean-loup Gailly |
| 847 | * This is free software; you can redistribute it and/or modify it under the |
| 848 | * terms of the GNU General Public License, see the file COPYING. |
| 849 | * |
| 850 | * The code in this file is derived from the file funzip.c written |
| 851 | * and put in the public domain by Mark Adler. |
| 852 | */ |
| 853 | |
| 854 | /* |
| 855 | This version can extract files in gzip or pkzip format. |
| 856 | For the latter, only the first entry is extracted, and it has to be |
| 857 | either deflated or stored. |
| 858 | */ |
| 859 | |
| 860 | /* #include "crypt.h" */ |
| 861 | |
| 862 | /* crypt.h (dummy version) -- do not perform encryption |
| 863 | * Hardly worth copyrighting :-) |
| 864 | */ |
| 865 | |
| 866 | #ifdef CRYPT |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 867 | # undef CRYPT /* dummy version */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 868 | #endif |
| 869 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 870 | #define RAND_HEAD_LEN 12 /* length of encryption random header */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 871 | |
| 872 | #define zencode |
| 873 | #define zdecode |
| 874 | |
| 875 | /* PKZIP header definitions */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 876 | #define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */ |
| 877 | #define LOCFLG 6 /* offset of bit flag */ |
| 878 | #define CRPFLG 1 /* bit for encrypted entry */ |
| 879 | #define EXTFLG 8 /* bit for extended local header */ |
| 880 | #define LOCHOW 8 /* offset of compression method */ |
| 881 | #define LOCTIM 10 /* file mod time (for decryption) */ |
| 882 | #define LOCCRC 14 /* offset of crc */ |
| 883 | #define LOCSIZ 18 /* offset of compressed size */ |
| 884 | #define LOCLEN 22 /* offset of uncompressed length */ |
| 885 | #define LOCFIL 26 /* offset of file name field length */ |
| 886 | #define LOCEXT 28 /* offset of extra field length */ |
| 887 | #define LOCHDR 30 /* size of local header, including sig */ |
| 888 | #define EXTHDR 16 /* size of extended local header, inc sig */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 889 | |
| 890 | |
| 891 | /* Globals */ |
| 892 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 893 | char *key; /* not used--needed to link crypt.c */ |
| 894 | int pkzip = 0; /* set for a pkzip file */ |
| 895 | int ext_header = 0; /* set if extended local header */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 896 | |
| 897 | /* =========================================================================== |
| 898 | * Unzip in to out. This routine works on both gzip and pkzip files. |
| 899 | * |
| 900 | * IN assertions: the buffer inbuf contains already the beginning of |
| 901 | * the compressed data, from offsets inptr to insize-1 included. |
| 902 | * The magic header has already been checked. The output buffer is cleared. |
| 903 | */ |
| 904 | int unzip(in, out) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 905 | int in, out; /* input and output file descriptors */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 906 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 907 | ulg orig_crc = 0; /* original crc */ |
| 908 | ulg orig_len = 0; /* original uncompressed length */ |
| 909 | int n; |
| 910 | uch buf[EXTHDR]; /* extended local header */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 911 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 912 | ifd = in; |
| 913 | ofd = out; |
| 914 | method = get_method(ifd); |
| 915 | if (method < 0) { |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 916 | exit(exit_code); /* error message already emitted */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 919 | updcrc(NULL, 0); /* initialize crc */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 920 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 921 | if (pkzip && !ext_header) { /* crc and length at the end otherwise */ |
| 922 | orig_crc = LG(inbuf + LOCCRC); |
| 923 | orig_len = LG(inbuf + LOCLEN); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 924 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 925 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 926 | /* Decompress */ |
| 927 | if (method == DEFLATED) { |
| 928 | |
| 929 | int res = inflate(); |
| 930 | |
| 931 | if (res == 3) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 932 | errorMsg(memory_exhausted); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 933 | } else if (res != 0) { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 934 | errorMsg("invalid compressed data--format violated"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | } else { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 938 | errorMsg("internal error, invalid method"); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 939 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 940 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 941 | /* Get the crc and original length */ |
| 942 | if (!pkzip) { |
| 943 | /* crc32 (see algorithm.doc) |
| 944 | * uncompressed input size modulo 2^32 |
| 945 | */ |
| 946 | for (n = 0; n < 8; n++) { |
| 947 | buf[n] = (uch) get_byte(); /* may cause an error if EOF */ |
| 948 | } |
| 949 | orig_crc = LG(buf); |
| 950 | orig_len = LG(buf + 4); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 951 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 952 | } else if (ext_header) { /* If extended header, check it */ |
| 953 | /* signature - 4bytes: 0x50 0x4b 0x07 0x08 |
| 954 | * CRC-32 value |
| 955 | * compressed size 4-bytes |
| 956 | * uncompressed size 4-bytes |
| 957 | */ |
| 958 | for (n = 0; n < EXTHDR; n++) { |
| 959 | buf[n] = (uch) get_byte(); /* may cause an error if EOF */ |
| 960 | } |
| 961 | orig_crc = LG(buf + 4); |
| 962 | orig_len = LG(buf + 12); |
| 963 | } |
| 964 | |
| 965 | /* Validate decompression */ |
| 966 | if (orig_crc != updcrc(outbuf, 0)) { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 967 | errorMsg("invalid compressed data--crc error"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 968 | } |
| 969 | if (orig_len != (ulg) bytes_out) { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 970 | errorMsg("invalid compressed data--length error"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* Check if there are more entries in a pkzip file */ |
| 974 | if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) { |
| 975 | WARN((stderr, "has more than one entry--rest ignored\n")); |
| 976 | } |
| 977 | ext_header = pkzip = 0; /* for next file */ |
| 978 | return OK; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 979 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 980 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 981 | /* util.c -- utility functions for gzip support |
| 982 | * Copyright (C) 1992-1993 Jean-loup Gailly |
| 983 | * This is free software; you can redistribute it and/or modify it under the |
| 984 | * terms of the GNU General Public License, see the file COPYING. |
| 985 | */ |
| 986 | |
| 987 | #include <ctype.h> |
| 988 | #include <errno.h> |
| 989 | #include <sys/types.h> |
| 990 | |
| 991 | #ifdef HAVE_UNISTD_H |
| 992 | # include <unistd.h> |
| 993 | #endif |
| 994 | #ifndef NO_FCNTL_H |
| 995 | # include <fcntl.h> |
| 996 | #endif |
| 997 | |
| 998 | #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H) |
| 999 | # include <stdlib.h> |
| 1000 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1001 | extern int errno; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1002 | #endif |
| 1003 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1004 | static const ulg crc_32_tab[]; /* crc table, defined below */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* =========================================================================== |
| 1007 | * Run a set of bytes through the crc shift register. If s is a NULL |
| 1008 | * pointer, then initialize the crc shift register contents instead. |
| 1009 | * Return the current crc in either case. |
| 1010 | */ |
| 1011 | ulg updcrc(s, n) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1012 | uch *s; /* pointer to bytes to pump through */ |
| 1013 | unsigned n; /* number of bytes in s[] */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1014 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1015 | register ulg c; /* temporary variable */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1016 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1017 | static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1018 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1019 | if (s == NULL) { |
| 1020 | c = 0xffffffffL; |
| 1021 | } else { |
| 1022 | c = crc; |
| 1023 | if (n) |
| 1024 | do { |
| 1025 | c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8); |
| 1026 | } while (--n); |
| 1027 | } |
| 1028 | crc = c; |
| 1029 | return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /* =========================================================================== |
| 1033 | * Clear input and output buffers |
| 1034 | */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1035 | void clear_bufs(void) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1036 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1037 | outcnt = 0; |
| 1038 | insize = inptr = 0; |
| 1039 | bytes_in = bytes_out = 0L; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* =========================================================================== |
| 1043 | * Fill the input buffer. This is called only when the buffer is empty. |
| 1044 | */ |
| 1045 | int fill_inbuf(eof_ok) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1046 | int eof_ok; /* set if EOF acceptable as a result */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1047 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1048 | int len; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1049 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1050 | /* Read as much as possible */ |
| 1051 | insize = 0; |
| 1052 | errno = 0; |
| 1053 | do { |
| 1054 | len = read(ifd, (char *) inbuf + insize, INBUFSIZ - insize); |
| 1055 | if (len == 0 || len == EOF) |
| 1056 | break; |
| 1057 | insize += len; |
| 1058 | } while (insize < INBUFSIZ); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1059 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1060 | if (insize == 0) { |
| 1061 | if (eof_ok) |
| 1062 | return EOF; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1063 | read_error_msg(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1064 | } |
| 1065 | bytes_in += (ulg) insize; |
| 1066 | inptr = 1; |
| 1067 | return inbuf[0]; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | /* =========================================================================== |
| 1071 | * Write the output buffer outbuf[0..outcnt-1] and update bytes_out. |
| 1072 | * (used for the compressed data only) |
| 1073 | */ |
| 1074 | void flush_outbuf() |
| 1075 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1076 | if (outcnt == 0) |
| 1077 | return; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1078 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1079 | if (!test_mode) |
| 1080 | write_buf(ofd, (char *) outbuf, outcnt); |
| 1081 | bytes_out += (ulg) outcnt; |
| 1082 | outcnt = 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* =========================================================================== |
| 1086 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. |
| 1087 | * (Used for the decompressed data only.) |
| 1088 | */ |
| 1089 | void flush_window() |
| 1090 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1091 | if (outcnt == 0) |
| 1092 | return; |
| 1093 | updcrc(window, outcnt); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1094 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1095 | if (!test_mode) |
| 1096 | write_buf(ofd, (char *) window, outcnt); |
| 1097 | bytes_out += (ulg) outcnt; |
| 1098 | outcnt = 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* =========================================================================== |
| 1102 | * Does the same as write(), but also handles partial pipe writes and checks |
| 1103 | * for error return. |
| 1104 | */ |
| 1105 | void write_buf(fd, buf, cnt) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1106 | int fd; |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 1107 | void * buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1108 | unsigned cnt; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1109 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1110 | unsigned n; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1111 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1112 | while ((n = write(fd, buf, cnt)) != cnt) { |
| 1113 | if (n == (unsigned) (-1)) { |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1114 | write_error_msg(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1115 | } |
| 1116 | cnt -= n; |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 1117 | buf = (void *) ((char *) buf + n); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1118 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | #if defined(NO_STRING_H) && !defined(STDC_HEADERS) |
| 1122 | |
| 1123 | /* Provide missing strspn and strcspn functions. */ |
| 1124 | |
| 1125 | # ifndef __STDC__ |
| 1126 | # define const |
| 1127 | # endif |
| 1128 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 1129 | int strspn (const char *s, const char *accept); |
| 1130 | int strcspn (const char *s, const char *reject); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1131 | |
| 1132 | /* ======================================================================== |
| 1133 | * Return the length of the maximum initial segment |
| 1134 | * of s which contains only characters in accept. |
| 1135 | */ |
| 1136 | int strspn(s, accept) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1137 | const char *s; |
| 1138 | const char *accept; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1139 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1140 | register const char *p; |
| 1141 | register const char *a; |
| 1142 | register int count = 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1143 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1144 | for (p = s; *p != '\0'; ++p) { |
| 1145 | for (a = accept; *a != '\0'; ++a) { |
| 1146 | if (*p == *a) |
| 1147 | break; |
| 1148 | } |
| 1149 | if (*a == '\0') |
| 1150 | return count; |
| 1151 | ++count; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1152 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1153 | return count; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* ======================================================================== |
| 1157 | * Return the length of the maximum inital segment of s |
| 1158 | * which contains no characters from reject. |
| 1159 | */ |
| 1160 | int strcspn(s, reject) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1161 | const char *s; |
| 1162 | const char *reject; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1163 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1164 | register int count = 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1165 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1166 | while (*s != '\0') { |
| 1167 | if (strchr(reject, *s++) != NULL) |
| 1168 | return count; |
| 1169 | ++count; |
| 1170 | } |
| 1171 | return count; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1174 | #endif /* NO_STRING_H */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1175 | |
| 1176 | |
| 1177 | /* ======================================================================== |
| 1178 | * Error handlers. |
| 1179 | */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1180 | void read_error_msg() |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1181 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1182 | fprintf(stderr, "\n"); |
| 1183 | if (errno != 0) { |
| 1184 | perror(""); |
| 1185 | } else { |
| 1186 | fprintf(stderr, "unexpected end of file\n"); |
| 1187 | } |
| 1188 | abort_gzip(); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1191 | void write_error_msg() |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1192 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1193 | fprintf(stderr, "\n"); |
| 1194 | perror(""); |
| 1195 | abort_gzip(); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | /* ======================================================================== |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1200 | * Table of CRC-32's of all single-byte values (made by makecrc.c) |
| 1201 | */ |
| 1202 | static const ulg crc_32_tab[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1203 | 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, |
| 1204 | 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, |
| 1205 | 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, |
| 1206 | 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, |
| 1207 | 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, |
| 1208 | 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, |
| 1209 | 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, |
| 1210 | 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, |
| 1211 | 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, |
| 1212 | 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, |
| 1213 | 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, |
| 1214 | 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, |
| 1215 | 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, |
| 1216 | 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, |
| 1217 | 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, |
| 1218 | 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, |
| 1219 | 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, |
| 1220 | 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, |
| 1221 | 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, |
| 1222 | 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, |
| 1223 | 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, |
| 1224 | 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, |
| 1225 | 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, |
| 1226 | 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, |
| 1227 | 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, |
| 1228 | 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, |
| 1229 | 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, |
| 1230 | 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, |
| 1231 | 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, |
| 1232 | 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, |
| 1233 | 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, |
| 1234 | 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, |
| 1235 | 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, |
| 1236 | 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, |
| 1237 | 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, |
| 1238 | 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, |
| 1239 | 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, |
| 1240 | 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, |
| 1241 | 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, |
| 1242 | 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, |
| 1243 | 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, |
| 1244 | 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, |
| 1245 | 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, |
| 1246 | 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, |
| 1247 | 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, |
| 1248 | 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, |
| 1249 | 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, |
| 1250 | 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, |
| 1251 | 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, |
| 1252 | 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, |
| 1253 | 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, |
| 1254 | 0x2d02ef8dL |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1255 | }; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1256 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1257 | /* inflate.c -- Not copyrighted 1992 by Mark Adler |
| 1258 | version c10p1, 10 January 1993 */ |
| 1259 | |
| 1260 | /* You can do whatever you like with this source file, though I would |
| 1261 | prefer that if you modify it and redistribute it that you include |
| 1262 | comments to that effect with your name and the date. Thank you. |
| 1263 | [The history has been moved to the file ChangeLog.] |
| 1264 | */ |
| 1265 | |
| 1266 | /* |
| 1267 | Inflate deflated (PKZIP's method 8 compressed) data. The compression |
| 1268 | method searches for as much of the current string of bytes (up to a |
| 1269 | length of 258) in the previous 32K bytes. If it doesn't find any |
| 1270 | matches (of at least length 3), it codes the next byte. Otherwise, it |
| 1271 | codes the length of the matched string and its distance backwards from |
| 1272 | the current position. There is a single Huffman code that codes both |
| 1273 | single bytes (called "literals") and match lengths. A second Huffman |
| 1274 | code codes the distance information, which follows a length code. Each |
| 1275 | length or distance code actually represents a base value and a number |
| 1276 | of "extra" (sometimes zero) bits to get to add to the base value. At |
| 1277 | the end of each deflated block is a special end-of-block (EOB) literal/ |
| 1278 | length code. The decoding process is basically: get a literal/length |
| 1279 | code; if EOB then done; if a literal, emit the decoded byte; if a |
| 1280 | length then get the distance and emit the referred-to bytes from the |
| 1281 | sliding window of previously emitted data. |
| 1282 | |
| 1283 | There are (currently) three kinds of inflate blocks: stored, fixed, and |
| 1284 | dynamic. The compressor deals with some chunk of data at a time, and |
| 1285 | decides which method to use on a chunk-by-chunk basis. A chunk might |
| 1286 | typically be 32K or 64K. If the chunk is uncompressible, then the |
| 1287 | "stored" method is used. In this case, the bytes are simply stored as |
| 1288 | is, eight bits per byte, with none of the above coding. The bytes are |
| 1289 | preceded by a count, since there is no longer an EOB code. |
| 1290 | |
| 1291 | If the data is compressible, then either the fixed or dynamic methods |
| 1292 | are used. In the dynamic method, the compressed data is preceded by |
| 1293 | an encoding of the literal/length and distance Huffman codes that are |
| 1294 | to be used to decode this block. The representation is itself Huffman |
| 1295 | coded, and so is preceded by a description of that code. These code |
| 1296 | descriptions take up a little space, and so for small blocks, there is |
| 1297 | a predefined set of codes, called the fixed codes. The fixed method is |
| 1298 | used if the block codes up smaller that way (usually for quite small |
| 1299 | chunks), otherwise the dynamic method is used. In the latter case, the |
| 1300 | codes are customized to the probabilities in the current block, and so |
| 1301 | can code it much better than the pre-determined fixed codes. |
| 1302 | |
| 1303 | The Huffman codes themselves are decoded using a mutli-level table |
| 1304 | lookup, in order to maximize the speed of decoding plus the speed of |
| 1305 | building the decoding tables. See the comments below that precede the |
| 1306 | lbits and dbits tuning parameters. |
| 1307 | */ |
| 1308 | |
| 1309 | |
| 1310 | /* |
| 1311 | Notes beyond the 1.93a appnote.txt: |
| 1312 | |
| 1313 | 1. Distance pointers never point before the beginning of the output |
| 1314 | stream. |
| 1315 | 2. Distance pointers can point back across blocks, up to 32k away. |
| 1316 | 3. There is an implied maximum of 7 bits for the bit length table and |
| 1317 | 15 bits for the actual data. |
| 1318 | 4. If only one code exists, then it is encoded using one bit. (Zero |
| 1319 | would be more efficient, but perhaps a little confusing.) If two |
| 1320 | codes exist, they are coded using one bit each (0 and 1). |
| 1321 | 5. There is no way of sending zero distance codes--a dummy must be |
| 1322 | sent if there are none. (History: a pre 2.0 version of PKZIP would |
| 1323 | store blocks with no distance codes, but this was discovered to be |
| 1324 | too harsh a criterion.) Valid only for 1.93a. 2.04c does allow |
| 1325 | zero distance codes, which is sent as one code of zero bits in |
| 1326 | length. |
| 1327 | 6. There are up to 286 literal/length codes. Code 256 represents the |
| 1328 | end-of-block. Note however that the static length tree defines |
| 1329 | 288 codes just to fill out the Huffman codes. Codes 286 and 287 |
| 1330 | cannot be used though, since there is no length base or extra bits |
| 1331 | defined for them. Similarly, there are up to 30 distance codes. |
| 1332 | However, static trees define 32 codes (all 5 bits) to fill out the |
| 1333 | Huffman codes, but the last two had better not show up in the data. |
| 1334 | 7. Unzip can check dynamic Huffman blocks for complete code sets. |
| 1335 | The exception is that a single code would not be complete (see #4). |
| 1336 | 8. The five bits following the block type is really the number of |
| 1337 | literal codes sent minus 257. |
| 1338 | 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits |
| 1339 | (1+6+6). Therefore, to output three times the length, you output |
| 1340 | three codes (1+1+1), whereas to output four times the same length, |
| 1341 | you only need two codes (1+3). Hmm. |
| 1342 | 10. In the tree reconstruction algorithm, Code = Code + Increment |
| 1343 | only if BitLength(i) is not zero. (Pretty obvious.) |
| 1344 | 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) |
| 1345 | 12. Note: length code 284 can represent 227-258, but length code 285 |
| 1346 | really is 258. The last length deserves its own, short code |
| 1347 | since it gets used a lot in very redundant files. The length |
| 1348 | 258 is special since 258 - 3 (the min match length) is 255. |
| 1349 | 13. The literal/length and distance code bit lengths are read as a |
| 1350 | single stream of lengths. It is possible (and advantageous) for |
| 1351 | a repeat code (16, 17, or 18) to go across the boundary between |
| 1352 | the two sets of lengths. |
| 1353 | */ |
| 1354 | |
| 1355 | #include <sys/types.h> |
| 1356 | |
| 1357 | #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H) |
| 1358 | # include <stdlib.h> |
| 1359 | #endif |
| 1360 | |
| 1361 | |
| 1362 | #define slide window |
| 1363 | |
| 1364 | /* Huffman code lookup table entry--this entry is four bytes for machines |
| 1365 | that have 16-bit pointers (e.g. PC's in the small or medium model). |
| 1366 | Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16 |
| 1367 | means that v is a literal, 16 < e < 32 means that v is a pointer to |
| 1368 | the next table, which codes e - 16 bits, and lastly e == 99 indicates |
| 1369 | an unused code. If a code with e == 99 is looked up, this implies an |
| 1370 | error in the data. */ |
| 1371 | struct huft { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1372 | uch e; /* number of extra bits or operation */ |
| 1373 | uch b; /* number of bits in this code or subcode */ |
| 1374 | union { |
| 1375 | ush n; /* literal, length base, or distance base */ |
| 1376 | struct huft *t; /* pointer to next level of table */ |
| 1377 | } v; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1378 | }; |
| 1379 | |
| 1380 | |
| 1381 | /* Function prototypes */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 1382 | int huft_build (unsigned *, unsigned, unsigned, ush *, ush *, |
| 1383 | struct huft **, int *); |
| 1384 | int huft_free (struct huft *); |
| 1385 | int inflate_codes (struct huft *, struct huft *, int, int); |
| 1386 | int inflate_stored (void); |
| 1387 | int inflate_fixed (void); |
| 1388 | int inflate_dynamic (void); |
| 1389 | int inflate_block (int *); |
| 1390 | int inflate (void); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1391 | |
| 1392 | |
| 1393 | /* The inflate algorithm uses a sliding 32K byte window on the uncompressed |
| 1394 | stream to find repeated byte strings. This is implemented here as a |
| 1395 | circular buffer. The index is updated simply by incrementing and then |
| 1396 | and'ing with 0x7fff (32K-1). */ |
| 1397 | /* It is left to other modules to supply the 32K area. It is assumed |
| 1398 | to be usable as if it were declared "uch slide[32768];" or as just |
| 1399 | "uch *slide;" and then malloc'ed in the latter case. The definition |
| 1400 | must be in unzip.h, included above. */ |
| 1401 | /* unsigned wp; current position in slide */ |
| 1402 | #define wp outcnt |
| 1403 | #define flush_output(w) (wp=(w),flush_window()) |
| 1404 | |
| 1405 | /* Tables for deflate from PKZIP's appnote.txt. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1406 | static unsigned border[] = { /* Order of the bit length code lengths */ |
| 1407 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
| 1408 | }; |
| 1409 | static ush cplens[] = { /* Copy lengths for literal codes 257..285 */ |
| 1410 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
| 1411 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
| 1412 | }; |
| 1413 | |
| 1414 | /* note: see note #13 above about the 258 in this list. */ |
| 1415 | static ush cplext[] = { /* Extra bits for literal codes 257..285 */ |
| 1416 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
| 1417 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 |
| 1418 | }; /* 99==invalid */ |
| 1419 | static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ |
| 1420 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
| 1421 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
| 1422 | 8193, 12289, 16385, 24577 |
| 1423 | }; |
| 1424 | static ush cpdext[] = { /* Extra bits for distance codes */ |
| 1425 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
| 1426 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
| 1427 | 12, 12, 13, 13 |
| 1428 | }; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1429 | |
| 1430 | |
| 1431 | |
| 1432 | /* Macros for inflate() bit peeking and grabbing. |
| 1433 | The usage is: |
| 1434 | |
| 1435 | NEEDBITS(j) |
| 1436 | x = b & mask_bits[j]; |
| 1437 | DUMPBITS(j) |
| 1438 | |
| 1439 | where NEEDBITS makes sure that b has at least j bits in it, and |
| 1440 | DUMPBITS removes the bits from b. The macros use the variable k |
| 1441 | for the number of bits in b. Normally, b and k are register |
| 1442 | variables for speed, and are initialized at the beginning of a |
| 1443 | routine that uses these macros from a global bit buffer and count. |
| 1444 | |
| 1445 | If we assume that EOB will be the longest code, then we will never |
| 1446 | ask for bits with NEEDBITS that are beyond the end of the stream. |
| 1447 | So, NEEDBITS should not read any more bytes than are needed to |
| 1448 | meet the request. Then no bytes need to be "returned" to the buffer |
| 1449 | at the end of the last block. |
| 1450 | |
| 1451 | However, this assumption is not true for fixed blocks--the EOB code |
| 1452 | is 7 bits, but the other literal/length codes can be 8 or 9 bits. |
| 1453 | (The EOB code is shorter than other codes because fixed blocks are |
| 1454 | generally short. So, while a block always has an EOB, many other |
| 1455 | literal/length codes have a significantly lower probability of |
| 1456 | showing up at all.) However, by making the first table have a |
| 1457 | lookup of seven bits, the EOB code will be found in that first |
| 1458 | lookup, and so will not require that too many bits be pulled from |
| 1459 | the stream. |
| 1460 | */ |
| 1461 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1462 | ulg bb; /* bit buffer */ |
| 1463 | unsigned bk; /* bits in bit buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1464 | |
| 1465 | ush mask_bits[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1466 | 0x0000, |
| 1467 | 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
| 1468 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1469 | }; |
| 1470 | |
| 1471 | #ifdef CRYPT |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1472 | uch cc; |
| 1473 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1474 | # define NEXTBYTE() (cc = get_byte(), zdecode(cc), cc) |
| 1475 | #else |
| 1476 | # define NEXTBYTE() (uch)get_byte() |
| 1477 | #endif |
| 1478 | #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}} |
| 1479 | #define DUMPBITS(n) {b>>=(n);k-=(n);} |
| 1480 | |
| 1481 | |
| 1482 | /* |
| 1483 | Huffman code decoding is performed using a multi-level table lookup. |
| 1484 | The fastest way to decode is to simply build a lookup table whose |
| 1485 | size is determined by the longest code. However, the time it takes |
| 1486 | to build this table can also be a factor if the data being decoded |
| 1487 | is not very long. The most common codes are necessarily the |
| 1488 | shortest codes, so those codes dominate the decoding time, and hence |
| 1489 | the speed. The idea is you can have a shorter table that decodes the |
| 1490 | shorter, more probable codes, and then point to subsidiary tables for |
| 1491 | the longer codes. The time it costs to decode the longer codes is |
| 1492 | then traded against the time it takes to make longer tables. |
| 1493 | |
| 1494 | This results of this trade are in the variables lbits and dbits |
| 1495 | below. lbits is the number of bits the first level table for literal/ |
| 1496 | length codes can decode in one step, and dbits is the same thing for |
| 1497 | the distance codes. Subsequent tables are also less than or equal to |
| 1498 | those sizes. These values may be adjusted either when all of the |
| 1499 | codes are shorter than that, in which case the longest code length in |
| 1500 | bits is used, or when the shortest code is *longer* than the requested |
| 1501 | table size, in which case the length of the shortest code in bits is |
| 1502 | used. |
| 1503 | |
| 1504 | There are two different values for the two tables, since they code a |
| 1505 | different number of possibilities each. The literal/length table |
| 1506 | codes 286 possible values, or in a flat code, a little over eight |
| 1507 | bits. The distance table codes 30 possible values, or a little less |
| 1508 | than five bits, flat. The optimum values for speed end up being |
| 1509 | about one bit more than those, so lbits is 8+1 and dbits is 5+1. |
| 1510 | The optimum values may differ though from machine to machine, and |
| 1511 | possibly even between compilers. Your mileage may vary. |
| 1512 | */ |
| 1513 | |
| 1514 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1515 | int lbits = 9; /* bits in base literal/length lookup table */ |
| 1516 | int dbits = 6; /* bits in base distance lookup table */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1517 | |
| 1518 | |
| 1519 | /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1520 | #define BMAX 16 /* maximum bit length of any code (16 for explode) */ |
| 1521 | #define N_MAX 288 /* maximum number of codes in any set */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1522 | |
| 1523 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1524 | unsigned hufts; /* track memory usage */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1525 | |
| 1526 | |
| 1527 | int huft_build(b, n, s, d, e, t, m) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1528 | unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ |
| 1529 | unsigned n; /* number of codes (assumed <= N_MAX) */ |
| 1530 | unsigned s; /* number of simple-valued codes (0..s-1) */ |
| 1531 | ush *d; /* list of base values for non-simple codes */ |
| 1532 | ush *e; /* list of extra bits for non-simple codes */ |
| 1533 | struct huft **t; /* result: starting table */ |
| 1534 | int *m; /* maximum lookup bits, returns actual */ |
| 1535 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1536 | /* Given a list of code lengths and a maximum table size, make a set of |
| 1537 | tables to decode that set of codes. Return zero on success, one if |
| 1538 | the given code set is incomplete (the tables are still built in this |
| 1539 | case), two if the input is invalid (all zero length codes or an |
| 1540 | oversubscribed set of lengths), and three if not enough memory. */ |
| 1541 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1542 | unsigned a; /* counter for codes of length k */ |
| 1543 | unsigned c[BMAX + 1]; /* bit length count table */ |
| 1544 | unsigned f; /* i repeats in table every f entries */ |
| 1545 | int g; /* maximum code length */ |
| 1546 | int h; /* table level */ |
| 1547 | register unsigned i; /* counter, current code */ |
| 1548 | register unsigned j; /* counter */ |
| 1549 | register int k; /* number of bits in current code */ |
| 1550 | int l; /* bits per table (returned in m) */ |
| 1551 | register unsigned *p; /* pointer into c[], b[], or v[] */ |
| 1552 | register struct huft *q; /* points to current table */ |
| 1553 | struct huft r; /* table entry for structure assignment */ |
| 1554 | struct huft *u[BMAX]; /* table stack */ |
| 1555 | unsigned v[N_MAX]; /* values in order of bit length */ |
| 1556 | register int w; /* bits before this table == (l * h) */ |
| 1557 | unsigned x[BMAX + 1]; /* bit offsets, then code stack */ |
| 1558 | unsigned *xp; /* pointer into x */ |
| 1559 | int y; /* number of dummy codes added */ |
| 1560 | unsigned z; /* number of entries in current table */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1561 | |
| 1562 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1563 | /* Generate counts for each bit length */ |
| 1564 | memzero(c, sizeof(c)); |
| 1565 | p = b; |
| 1566 | i = n; |
| 1567 | do { |
| 1568 | Tracecv(*p, |
| 1569 | (stderr, |
| 1570 | (n - i >= ' ' |
| 1571 | && n - i <= '~' ? "%c %d\n" : "0x%x %d\n"), n - i, *p)); |
| 1572 | c[*p]++; /* assume all entries <= BMAX */ |
| 1573 | p++; /* Can't combine with above line (Solaris bug) */ |
| 1574 | } while (--i); |
| 1575 | if (c[0] == n) { /* null input--all zero length codes */ |
| 1576 | *t = (struct huft *) NULL; |
| 1577 | *m = 0; |
| 1578 | return 0; |
| 1579 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1580 | |
| 1581 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1582 | /* Find minimum and maximum length, bound *m by those */ |
| 1583 | l = *m; |
| 1584 | for (j = 1; j <= BMAX; j++) |
| 1585 | if (c[j]) |
| 1586 | break; |
| 1587 | k = j; /* minimum code length */ |
| 1588 | if ((unsigned) l < j) |
| 1589 | l = j; |
| 1590 | for (i = BMAX; i; i--) |
| 1591 | if (c[i]) |
| 1592 | break; |
| 1593 | g = i; /* maximum code length */ |
| 1594 | if ((unsigned) l > i) |
| 1595 | l = i; |
| 1596 | *m = l; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1597 | |
| 1598 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1599 | /* Adjust last length count to fill out codes, if needed */ |
| 1600 | for (y = 1 << j; j < i; j++, y <<= 1) |
| 1601 | if ((y -= c[j]) < 0) |
| 1602 | return 2; /* bad input: more codes than bits */ |
| 1603 | if ((y -= c[i]) < 0) |
| 1604 | return 2; |
| 1605 | c[i] += y; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1606 | |
| 1607 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1608 | /* Generate starting offsets into the value table for each length */ |
| 1609 | x[1] = j = 0; |
| 1610 | p = c + 1; |
| 1611 | xp = x + 2; |
| 1612 | while (--i) { /* note that i == g from above */ |
| 1613 | *xp++ = (j += *p++); |
| 1614 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1615 | |
| 1616 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1617 | /* Make a table of values in order of bit lengths */ |
| 1618 | p = b; |
| 1619 | i = 0; |
| 1620 | do { |
| 1621 | if ((j = *p++) != 0) |
| 1622 | v[x[j]++] = i; |
| 1623 | } while (++i < n); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1624 | |
| 1625 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1626 | /* Generate the Huffman codes and for each, make the table entries */ |
| 1627 | x[0] = i = 0; /* first Huffman code is zero */ |
| 1628 | p = v; /* grab values in bit order */ |
| 1629 | h = -1; /* no tables yet--level -1 */ |
| 1630 | w = -l; /* bits decoded == (l * h) */ |
| 1631 | u[0] = (struct huft *) NULL; /* just to keep compilers happy */ |
| 1632 | q = (struct huft *) NULL; /* ditto */ |
| 1633 | z = 0; /* ditto */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1634 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1635 | /* go through the bit lengths (k already is bits in shortest code) */ |
| 1636 | for (; k <= g; k++) { |
| 1637 | a = c[k]; |
| 1638 | while (a--) { |
| 1639 | /* here i is the Huffman code of length k bits for value *p */ |
| 1640 | /* make tables up to required level */ |
| 1641 | while (k > w + l) { |
| 1642 | h++; |
| 1643 | w += l; /* previous table always l bits */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1644 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1645 | /* compute minimum size table less than or equal to l bits */ |
| 1646 | z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ |
| 1647 | if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */ |
| 1648 | f -= a + 1; /* deduct codes from patterns left */ |
| 1649 | xp = c + k; |
| 1650 | while (++j < z) { /* try smaller tables up to z bits */ |
| 1651 | if ((f <<= 1) <= *++xp) |
| 1652 | break; /* enough codes to use up j bits */ |
| 1653 | f -= *xp; /* else deduct codes from patterns */ |
| 1654 | } |
| 1655 | } |
| 1656 | z = 1 << j; /* table entries for j-bit table */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1657 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1658 | /* allocate and link in new table */ |
| 1659 | if ( |
| 1660 | (q = |
| 1661 | (struct huft *) malloc((z + 1) * |
| 1662 | sizeof(struct huft))) == |
| 1663 | (struct huft *) NULL) { |
| 1664 | if (h) |
| 1665 | huft_free(u[0]); |
| 1666 | return 3; /* not enough memory */ |
| 1667 | } |
| 1668 | hufts += z + 1; /* track memory usage */ |
| 1669 | *t = q + 1; /* link to list for huft_free() */ |
| 1670 | *(t = &(q->v.t)) = (struct huft *) NULL; |
| 1671 | u[h] = ++q; /* table starts after link */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1672 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1673 | /* connect to last table, if there is one */ |
| 1674 | if (h) { |
| 1675 | x[h] = i; /* save pattern for backing up */ |
| 1676 | r.b = (uch) l; /* bits to dump before this table */ |
| 1677 | r.e = (uch) (16 + j); /* bits in this table */ |
| 1678 | r.v.t = q; /* pointer to this table */ |
| 1679 | j = i >> (w - l); /* (get around Turbo C bug) */ |
| 1680 | u[h - 1][j] = r; /* connect to last table */ |
| 1681 | } |
| 1682 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1683 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1684 | /* set up table entry in r */ |
| 1685 | r.b = (uch) (k - w); |
| 1686 | if (p >= v + n) |
| 1687 | r.e = 99; /* out of values--invalid code */ |
| 1688 | else if (*p < s) { |
| 1689 | r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ |
| 1690 | r.v.n = (ush) (*p); /* simple code is just the value */ |
| 1691 | p++; /* one compiler does not like *p++ */ |
| 1692 | } else { |
| 1693 | r.e = (uch) e[*p - s]; /* non-simple--look up in lists */ |
| 1694 | r.v.n = d[*p++ - s]; |
| 1695 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1696 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1697 | /* fill code-like entries with r */ |
| 1698 | f = 1 << (k - w); |
| 1699 | for (j = i >> w; j < z; j += f) |
| 1700 | q[j] = r; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1701 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1702 | /* backwards increment the k-bit code i */ |
| 1703 | for (j = 1 << (k - 1); i & j; j >>= 1) |
| 1704 | i ^= j; |
| 1705 | i ^= j; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1706 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1707 | /* backup over finished tables */ |
| 1708 | while ((i & ((1 << w) - 1)) != x[h]) { |
| 1709 | h--; /* don't need to update q */ |
| 1710 | w -= l; |
| 1711 | } |
| 1712 | } |
| 1713 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1714 | |
| 1715 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1716 | /* Return true (1) if we were given an incomplete table */ |
| 1717 | return y != 0 && g != 1; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | |
| 1721 | |
| 1722 | int huft_free(t) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1723 | struct huft *t; /* table to free */ |
| 1724 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1725 | /* Free the malloc'ed tables built by huft_build(), which makes a linked |
| 1726 | list of the tables it made, with the links in a dummy first entry of |
| 1727 | each table. */ |
| 1728 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1729 | register struct huft *p, *q; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1730 | |
| 1731 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1732 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ |
| 1733 | p = t; |
| 1734 | while (p != (struct huft *) NULL) { |
| 1735 | q = (--p)->v.t; |
| 1736 | free((char *) p); |
| 1737 | p = q; |
| 1738 | } |
| 1739 | return 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | int inflate_codes(tl, td, bl, bd) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1744 | struct huft *tl, *td; /* literal/length and distance decoder tables */ |
| 1745 | int bl, bd; /* number of bits decoded by tl[] and td[] */ |
| 1746 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1747 | /* inflate (decompress) the codes in a deflated (compressed) block. |
| 1748 | Return an error code or zero if it all goes ok. */ |
| 1749 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1750 | register unsigned e; /* table entry flag/number of extra bits */ |
| 1751 | unsigned n, d; /* length and index for copy */ |
| 1752 | unsigned w; /* current window position */ |
| 1753 | struct huft *t; /* pointer to table entry */ |
| 1754 | unsigned ml, md; /* masks for bl and bd bits */ |
| 1755 | register ulg b; /* bit buffer */ |
| 1756 | register unsigned k; /* number of bits in bit buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1757 | |
| 1758 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1759 | /* make local copies of globals */ |
| 1760 | b = bb; /* initialize bit buffer */ |
| 1761 | k = bk; |
| 1762 | w = wp; /* initialize window position */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1763 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1764 | /* inflate the coded data */ |
| 1765 | ml = mask_bits[bl]; /* precompute masks for speed */ |
| 1766 | md = mask_bits[bd]; |
| 1767 | for (;;) { /* do until end of block */ |
| 1768 | NEEDBITS((unsigned) bl) |
| 1769 | if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) |
| 1770 | do { |
| 1771 | if (e == 99) |
| 1772 | return 1; |
| 1773 | DUMPBITS(t->b) |
| 1774 | e -= 16; |
| 1775 | NEEDBITS(e) |
| 1776 | } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) |
| 1777 | > 16); |
| 1778 | DUMPBITS(t->b) |
| 1779 | if (e == 16) { /* then it's a literal */ |
| 1780 | slide[w++] = (uch) t->v.n; |
| 1781 | Tracevv((stderr, "%c", slide[w - 1])); |
| 1782 | if (w == WSIZE) { |
| 1783 | flush_output(w); |
| 1784 | w = 0; |
| 1785 | } |
| 1786 | } else { /* it's an EOB or a length */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1787 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1788 | /* exit if end of block */ |
| 1789 | if (e == 15) |
| 1790 | break; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1791 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1792 | /* get length of block to copy */ |
| 1793 | NEEDBITS(e) |
| 1794 | n = t->v.n + ((unsigned) b & mask_bits[e]); |
| 1795 | DUMPBITS(e); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1796 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1797 | /* decode distance of block to copy */ |
| 1798 | NEEDBITS((unsigned) bd) |
| 1799 | if ((e = (t = td + ((unsigned) b & md))->e) > 16) |
| 1800 | do { |
| 1801 | if (e == 99) |
| 1802 | return 1; |
| 1803 | DUMPBITS(t->b) |
| 1804 | e -= 16; |
| 1805 | NEEDBITS(e) |
| 1806 | } |
| 1807 | while ( |
| 1808 | (e = |
| 1809 | (t = |
| 1810 | t->v.t + ((unsigned) b & mask_bits[e]))->e) > |
| 1811 | 16); |
| 1812 | DUMPBITS(t->b) |
| 1813 | NEEDBITS(e) |
| 1814 | d = w - t->v.n - ((unsigned) b & mask_bits[e]); |
| 1815 | DUMPBITS(e) |
| 1816 | Tracevv((stderr, "\\[%d,%d]", w - d, n)); |
| 1817 | |
| 1818 | /* do the copy */ |
| 1819 | do { |
| 1820 | n -= (e = |
| 1821 | (e = |
| 1822 | WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > |
| 1823 | n ? n : e); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1824 | #if !defined(NOMEMCPY) && !defined(DEBUG) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1825 | if (w - d >= e) { /* (this test assumes unsigned comparison) */ |
| 1826 | memcpy(slide + w, slide + d, e); |
| 1827 | w += e; |
| 1828 | d += e; |
| 1829 | } else /* do it slow to avoid memcpy() overlap */ |
| 1830 | #endif /* !NOMEMCPY */ |
| 1831 | do { |
| 1832 | slide[w++] = slide[d++]; |
| 1833 | Tracevv((stderr, "%c", slide[w - 1])); |
| 1834 | } while (--e); |
| 1835 | if (w == WSIZE) { |
| 1836 | flush_output(w); |
| 1837 | w = 0; |
| 1838 | } |
| 1839 | } while (n); |
| 1840 | } |
| 1841 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1842 | |
| 1843 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1844 | /* restore the globals from the locals */ |
| 1845 | wp = w; /* restore global window pointer */ |
| 1846 | bb = b; /* restore global bit buffer */ |
| 1847 | bk = k; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1848 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1849 | /* done */ |
| 1850 | return 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | int inflate_stored() |
| 1856 | /* "decompress" an inflated type 0 (stored) block. */ |
| 1857 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1858 | unsigned n; /* number of bytes in block */ |
| 1859 | unsigned w; /* current window position */ |
| 1860 | register ulg b; /* bit buffer */ |
| 1861 | register unsigned k; /* number of bits in bit buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1862 | |
| 1863 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1864 | /* make local copies of globals */ |
| 1865 | b = bb; /* initialize bit buffer */ |
| 1866 | k = bk; |
| 1867 | w = wp; /* initialize window position */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1868 | |
| 1869 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1870 | /* go to byte boundary */ |
| 1871 | n = k & 7; |
| 1872 | DUMPBITS(n); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1873 | |
| 1874 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1875 | /* get the length and its complement */ |
| 1876 | NEEDBITS(16) |
| 1877 | n = ((unsigned) b & 0xffff); |
| 1878 | DUMPBITS(16) |
| 1879 | NEEDBITS(16) |
| 1880 | if (n != (unsigned) ((~b) & 0xffff)) |
| 1881 | return 1; /* error in compressed data */ |
| 1882 | DUMPBITS(16) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1883 | |
| 1884 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1885 | /* read and output the compressed data */ |
| 1886 | while (n--) { |
| 1887 | NEEDBITS(8) |
| 1888 | slide[w++] = (uch) b; |
| 1889 | if (w == WSIZE) { |
| 1890 | flush_output(w); |
| 1891 | w = 0; |
| 1892 | } |
| 1893 | DUMPBITS(8) |
| 1894 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1895 | |
| 1896 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1897 | /* restore the globals from the locals */ |
| 1898 | wp = w; /* restore global window pointer */ |
| 1899 | bb = b; /* restore global bit buffer */ |
| 1900 | bk = k; |
| 1901 | return 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | int inflate_fixed() |
| 1907 | /* decompress an inflated type 1 (fixed Huffman codes) block. We should |
| 1908 | either replace this with a custom decoder, or at least precompute the |
| 1909 | Huffman tables. */ |
| 1910 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1911 | int i; /* temporary variable */ |
| 1912 | struct huft *tl; /* literal/length code table */ |
| 1913 | struct huft *td; /* distance code table */ |
| 1914 | int bl; /* lookup bits for tl */ |
| 1915 | int bd; /* lookup bits for td */ |
| 1916 | unsigned l[288]; /* length list for huft_build */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1917 | |
| 1918 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1919 | /* set up literal table */ |
| 1920 | for (i = 0; i < 144; i++) |
| 1921 | l[i] = 8; |
| 1922 | for (; i < 256; i++) |
| 1923 | l[i] = 9; |
| 1924 | for (; i < 280; i++) |
| 1925 | l[i] = 7; |
| 1926 | for (; i < 288; i++) /* make a complete, but wrong code set */ |
| 1927 | l[i] = 8; |
| 1928 | bl = 7; |
| 1929 | if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) |
| 1930 | return i; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1931 | |
| 1932 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1933 | /* set up distance table */ |
| 1934 | for (i = 0; i < 30; i++) /* make an incomplete code set */ |
| 1935 | l[i] = 5; |
| 1936 | bd = 5; |
| 1937 | if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { |
| 1938 | huft_free(tl); |
| 1939 | return i; |
| 1940 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1941 | |
| 1942 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1943 | /* decompress until an end-of-block code */ |
| 1944 | if (inflate_codes(tl, td, bl, bd)) |
| 1945 | return 1; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1946 | |
| 1947 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1948 | /* free the decoding tables, return */ |
| 1949 | huft_free(tl); |
| 1950 | huft_free(td); |
| 1951 | return 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | |
| 1955 | |
| 1956 | int inflate_dynamic() |
| 1957 | /* decompress an inflated type 2 (dynamic Huffman codes) block. */ |
| 1958 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1959 | int i; /* temporary variables */ |
| 1960 | unsigned j; |
| 1961 | unsigned l; /* last length */ |
| 1962 | unsigned m; /* mask for bit lengths table */ |
| 1963 | unsigned n; /* number of lengths to get */ |
| 1964 | struct huft *tl; /* literal/length code table */ |
| 1965 | struct huft *td; /* distance code table */ |
| 1966 | int bl; /* lookup bits for tl */ |
| 1967 | int bd; /* lookup bits for td */ |
| 1968 | unsigned nb; /* number of bit length codes */ |
| 1969 | unsigned nl; /* number of literal/length codes */ |
| 1970 | unsigned nd; /* number of distance codes */ |
| 1971 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1972 | #ifdef PKZIP_BUG_WORKAROUND |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1973 | unsigned ll[288 + 32]; /* literal/length and distance code lengths */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1974 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1975 | unsigned ll[286 + 30]; /* literal/length and distance code lengths */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1976 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1977 | register ulg b; /* bit buffer */ |
| 1978 | register unsigned k; /* number of bits in bit buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1979 | |
| 1980 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1981 | /* make local bit buffer */ |
| 1982 | b = bb; |
| 1983 | k = bk; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1984 | |
| 1985 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1986 | /* read in table lengths */ |
| 1987 | NEEDBITS(5) |
| 1988 | nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */ |
| 1989 | DUMPBITS(5) |
| 1990 | NEEDBITS(5) |
| 1991 | nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */ |
| 1992 | DUMPBITS(5) |
| 1993 | NEEDBITS(4) |
| 1994 | nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */ |
| 1995 | DUMPBITS(4) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1996 | #ifdef PKZIP_BUG_WORKAROUND |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1997 | if (nl > 288 || nd > 32) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 1998 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1999 | if (nl > 286 || nd > 30) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2000 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2001 | return 1; /* bad lengths */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2002 | |
| 2003 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2004 | /* read in bit-length-code lengths */ |
| 2005 | for (j = 0; j < nb; j++) { |
| 2006 | NEEDBITS(3) |
| 2007 | ll[border[j]] = (unsigned) b & 7; |
| 2008 | DUMPBITS(3) |
| 2009 | } |
| 2010 | for (; j < 19; j++) |
| 2011 | ll[border[j]] = 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2012 | |
| 2013 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2014 | /* build decoding table for trees--single level, 7 bit lookup */ |
| 2015 | bl = 7; |
| 2016 | if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) { |
| 2017 | if (i == 1) |
| 2018 | huft_free(tl); |
| 2019 | return i; /* incomplete code set */ |
| 2020 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2021 | |
| 2022 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2023 | /* read in literal and distance code lengths */ |
| 2024 | n = nl + nd; |
| 2025 | m = mask_bits[bl]; |
| 2026 | i = l = 0; |
| 2027 | while ((unsigned) i < n) { |
| 2028 | NEEDBITS((unsigned) bl) |
| 2029 | j = (td = tl + ((unsigned) b & m))->b; |
| 2030 | DUMPBITS(j) |
| 2031 | j = td->v.n; |
| 2032 | if (j < 16) /* length of code in bits (0..15) */ |
| 2033 | ll[i++] = l = j; /* save last length in l */ |
| 2034 | else if (j == 16) { /* repeat last length 3 to 6 times */ |
| 2035 | NEEDBITS(2) |
| 2036 | j = 3 + ((unsigned) b & 3); |
| 2037 | DUMPBITS(2) |
| 2038 | if ((unsigned) i + j > n) |
| 2039 | return 1; |
| 2040 | while (j--) |
| 2041 | ll[i++] = l; |
| 2042 | } else if (j == 17) { /* 3 to 10 zero length codes */ |
| 2043 | NEEDBITS(3) |
| 2044 | j = 3 + ((unsigned) b & 7); |
| 2045 | DUMPBITS(3) |
| 2046 | if ((unsigned) i + j > n) |
| 2047 | return 1; |
| 2048 | while (j--) |
| 2049 | ll[i++] = 0; |
| 2050 | l = 0; |
| 2051 | } else { /* j == 18: 11 to 138 zero length codes */ |
| 2052 | |
| 2053 | NEEDBITS(7) |
| 2054 | j = 11 + ((unsigned) b & 0x7f); |
| 2055 | DUMPBITS(7) |
| 2056 | if ((unsigned) i + j > n) |
| 2057 | return 1; |
| 2058 | while (j--) |
| 2059 | ll[i++] = 0; |
| 2060 | l = 0; |
| 2061 | } |
| 2062 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2063 | |
| 2064 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2065 | /* free decoding table for trees */ |
| 2066 | huft_free(tl); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2067 | |
| 2068 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2069 | /* restore the global bit buffer */ |
| 2070 | bb = b; |
| 2071 | bk = k; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2072 | |
| 2073 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2074 | /* build the decoding tables for literal/length and distance codes */ |
| 2075 | bl = lbits; |
| 2076 | if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { |
| 2077 | if (i == 1) { |
| 2078 | fprintf(stderr, " incomplete literal tree\n"); |
| 2079 | huft_free(tl); |
| 2080 | } |
| 2081 | return i; /* incomplete code set */ |
| 2082 | } |
| 2083 | bd = dbits; |
| 2084 | if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { |
| 2085 | if (i == 1) { |
| 2086 | fprintf(stderr, " incomplete distance tree\n"); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2087 | #ifdef PKZIP_BUG_WORKAROUND |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2088 | i = 0; |
| 2089 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2090 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2091 | huft_free(td); |
| 2092 | } |
| 2093 | huft_free(tl); |
| 2094 | return i; /* incomplete code set */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2095 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2096 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2097 | |
| 2098 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2099 | /* decompress until an end-of-block code */ |
| 2100 | if (inflate_codes(tl, td, bl, bd)) |
| 2101 | return 1; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2102 | |
| 2103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2104 | /* free the decoding tables, return */ |
| 2105 | huft_free(tl); |
| 2106 | huft_free(td); |
| 2107 | return 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | |
| 2111 | |
| 2112 | int inflate_block(e) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2113 | int *e; /* last block flag */ |
| 2114 | |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2115 | /* decompress an inflated block */ |
| 2116 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2117 | unsigned t; /* block type */ |
| 2118 | register ulg b; /* bit buffer */ |
| 2119 | register unsigned k; /* number of bits in bit buffer */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2120 | |
| 2121 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2122 | /* make local bit buffer */ |
| 2123 | b = bb; |
| 2124 | k = bk; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2125 | |
| 2126 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2127 | /* read in last block bit */ |
| 2128 | NEEDBITS(1) |
| 2129 | * e = (int) b & 1; |
| 2130 | DUMPBITS(1) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2131 | |
| 2132 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2133 | /* read in block type */ |
| 2134 | NEEDBITS(2) |
| 2135 | t = (unsigned) b & 3; |
| 2136 | DUMPBITS(2) |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2137 | |
| 2138 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2139 | /* restore the global bit buffer */ |
| 2140 | bb = b; |
| 2141 | bk = k; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2142 | |
| 2143 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2144 | /* inflate that block type */ |
| 2145 | if (t == 2) |
| 2146 | return inflate_dynamic(); |
| 2147 | if (t == 0) |
| 2148 | return inflate_stored(); |
| 2149 | if (t == 1) |
| 2150 | return inflate_fixed(); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2151 | |
| 2152 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2153 | /* bad block type */ |
| 2154 | return 2; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | |
| 2158 | |
| 2159 | int inflate() |
| 2160 | /* decompress an inflated entry */ |
| 2161 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2162 | int e; /* last block flag */ |
| 2163 | int r; /* result code */ |
| 2164 | unsigned h; /* maximum struct huft's malloc'ed */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2165 | |
| 2166 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2167 | /* initialize window, bit buffer */ |
| 2168 | wp = 0; |
| 2169 | bk = 0; |
| 2170 | bb = 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2171 | |
| 2172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2173 | /* decompress until the last block */ |
| 2174 | h = 0; |
| 2175 | do { |
| 2176 | hufts = 0; |
| 2177 | if ((r = inflate_block(&e)) != 0) |
| 2178 | return r; |
| 2179 | if (hufts > h) |
| 2180 | h = hufts; |
| 2181 | } while (!e); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2182 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2183 | /* Undo too much lookahead. The next read will be byte aligned so we |
| 2184 | * can discard unused bits in the last meaningful byte. |
| 2185 | */ |
| 2186 | while (bk >= 8) { |
| 2187 | bk -= 8; |
| 2188 | inptr--; |
| 2189 | } |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2190 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2191 | /* flush out slide */ |
| 2192 | flush_output(wp); |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2193 | |
| 2194 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2195 | /* return success */ |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2196 | #ifdef DEBUG |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 2197 | fprintf(stderr, "<%u> ", h); |
| 2198 | #endif /* DEBUG */ |
| 2199 | return 0; |
Eric Andersen | b052b47 | 1999-11-18 00:21:59 +0000 | [diff] [blame] | 2200 | } |