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