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