diff --git a/usr.bin/gzip/Makefile b/usr.bin/gzip/Makefile index b0a4bab17..c33dc3cb3 100644 --- a/usr.bin/gzip/Makefile +++ b/usr.bin/gzip/Makefile @@ -1,5 +1,3 @@ -# $NetBSD: Makefile,v 1.18 2013/11/13 11:12:24 pettai Exp $ - USE_FORT?= yes # data-driven bugs? PROG= gzip diff --git a/usr.bin/gzip/gzexe b/usr.bin/gzip/gzexe index f5416d8bd..d46a1a677 100755 --- a/usr.bin/gzip/gzexe +++ b/usr.bin/gzip/gzexe @@ -1,7 +1,5 @@ #!/bin/sh - # -# $NetBSD: gzexe,v 1.3 2004/05/01 08:22:41 wiz Exp $ -# # $OpenBSD: gzexe,v 1.3 2003/08/05 18:22:17 deraadt Exp $ # # Copyright (c) 2003 Otto Moerbeek diff --git a/usr.bin/gzip/gzexe.1 b/usr.bin/gzip/gzexe.1 index 765348740..a6b9fafd4 100644 --- a/usr.bin/gzip/gzexe.1 +++ b/usr.bin/gzip/gzexe.1 @@ -1,4 +1,3 @@ -.\" $NetBSD: gzexe.1,v 1.3 2003/12/28 12:49:41 wiz Exp $ .\" $OpenBSD: gzexe.1,v 1.1 2003/07/31 07:32:47 otto Exp $ .\" .\" Copyright (c) 2003 Otto Moerbeek diff --git a/usr.bin/gzip/gzip.1 b/usr.bin/gzip/gzip.1 index 28c21b495..6424b4771 100644 --- a/usr.bin/gzip/gzip.1 +++ b/usr.bin/gzip/gzip.1 @@ -1,4 +1,3 @@ -.\" $NetBSD: gzip.1,v 1.25 2015/04/06 21:41:17 wiz Exp $ .\" .\" Copyright (c) 1997, 2003, 2004 Matthew R. Green .\" All rights reserved. diff --git a/usr.bin/gzip/gzip.c b/usr.bin/gzip/gzip.c index c4f78e0d7..01a5088c3 100644 --- a/usr.bin/gzip/gzip.c +++ b/usr.bin/gzip/gzip.c @@ -1,4 +1,3 @@ -/* $NetBSD: gzip.c,v 1.108 2015/04/15 02:29:12 christos Exp $ */ /* * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green @@ -30,7 +29,6 @@ #ifndef lint __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\ Matthew R. Green. All rights reserved."); -__RCSID("$NetBSD: gzip.c,v 1.108 2015/04/15 02:29:12 christos Exp $"); #endif /* not lint */ /* @@ -50,20 +48,20 @@ __RCSID("$NetBSD: gzip.c,v 1.108 2015/04/15 02:29:12 christos Exp $"); #include #include -#include -#include -#include -#include -#include #include #include #include -#include #include +#include +#include #include #include -#include +#include +#include +#include #include +#include +#include #ifndef PRIdOFF #define PRIdOFF PRId64 @@ -71,235 +69,228 @@ __RCSID("$NetBSD: gzip.c,v 1.108 2015/04/15 02:29:12 christos Exp $"); /* what type of file are we dealing with */ enum filetype { - FT_GZIP, + FT_GZIP, #ifndef NO_BZIP2_SUPPORT - FT_BZIP2, + FT_BZIP2, #endif #ifndef NO_COMPRESS_SUPPORT - FT_Z, + FT_Z, #endif #ifndef NO_PACK_SUPPORT - FT_PACK, + FT_PACK, #endif #ifndef NO_XZ_SUPPORT - FT_XZ, + FT_XZ, #endif - FT_LAST, - FT_UNKNOWN + FT_LAST, + FT_UNKNOWN }; #ifndef NO_BZIP2_SUPPORT #include -#define BZ2_SUFFIX ".bz2" -#define BZIP2_MAGIC "\102\132\150" +#define BZ2_SUFFIX ".bz2" +#define BZIP2_MAGIC "\102\132\150" #endif #ifndef NO_COMPRESS_SUPPORT -#define Z_SUFFIX ".Z" -#define Z_MAGIC "\037\235" +#define Z_SUFFIX ".Z" +#define Z_MAGIC "\037\235" #endif #ifndef NO_PACK_SUPPORT -#define PACK_MAGIC "\037\036" +#define PACK_MAGIC "\037\036" #endif #ifndef NO_XZ_SUPPORT #include -#define XZ_SUFFIX ".xz" -#define XZ_MAGIC "\3757zXZ" +#define XZ_SUFFIX ".xz" +#define XZ_MAGIC "\3757zXZ" #endif -#define GZ_SUFFIX ".gz" +#define GZ_SUFFIX ".gz" -#define BUFLEN (64 * 1024) +#define BUFLEN (64 * 1024) -#define GZIP_MAGIC0 0x1F -#define GZIP_MAGIC1 0x8B -#define GZIP_OMAGIC1 0x9E +#define GZIP_MAGIC0 0x1F +#define GZIP_MAGIC1 0x8B +#define GZIP_OMAGIC1 0x9E -#define GZIP_TIMESTAMP (off_t)4 -#define GZIP_ORIGNAME (off_t)10 +#define GZIP_TIMESTAMP (off_t)4 +#define GZIP_ORIGNAME (off_t)10 -#define HEAD_CRC 0x02 -#define EXTRA_FIELD 0x04 -#define ORIG_NAME 0x08 -#define COMMENT 0x10 +#define HEAD_CRC 0x02 +#define EXTRA_FIELD 0x04 +#define ORIG_NAME 0x08 +#define COMMENT 0x10 -#define OS_CODE 3 /* Unix */ +#define OS_CODE 3 /* Unix */ typedef struct { - const char *zipped; - int ziplen; - const char *normal; /* for unzip - must not be longer than zipped */ + const char *zipped; + int ziplen; + const char *normal; /* for unzip - must not be longer than zipped */ } suffixes_t; static suffixes_t suffixes[] = { -#define SUFFIX(Z, N) {Z, sizeof Z - 1, N} - SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S .xxx */ +#define SUFFIX(Z, N) {Z, sizeof Z - 1, N} + SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S .xxx */ #ifndef SMALL - SUFFIX(GZ_SUFFIX, ""), - SUFFIX(".z", ""), - SUFFIX("-gz", ""), - SUFFIX("-z", ""), - SUFFIX("_z", ""), - SUFFIX(".taz", ".tar"), - SUFFIX(".tgz", ".tar"), + SUFFIX(GZ_SUFFIX, ""), SUFFIX(".z", ""), SUFFIX("-gz", ""), + SUFFIX("-z", ""), SUFFIX("_z", ""), SUFFIX(".taz", ".tar"), + SUFFIX(".tgz", ".tar"), #ifndef NO_BZIP2_SUPPORT - SUFFIX(BZ2_SUFFIX, ""), + SUFFIX(BZ2_SUFFIX, ""), #endif #ifndef NO_COMPRESS_SUPPORT - SUFFIX(Z_SUFFIX, ""), + SUFFIX(Z_SUFFIX, ""), #endif #ifndef NO_XZ_SUPPORT - SUFFIX(XZ_SUFFIX, ""), + SUFFIX(XZ_SUFFIX, ""), #endif - SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */ -#endif /* SMALL */ + SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */ +#endif /* SMALL */ #undef SUFFIX }; #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0]) -#define SUFFIX_MAXLEN 30 +#define SUFFIX_MAXLEN 30 -static const char gzip_version[] = "NetBSD gzip 20150113"; +static const char gzip_version[] = "gzip 20150113"; -static int cflag; /* stdout mode */ -static int dflag; /* decompress mode */ -static int lflag; /* list mode */ -static int numflag = 6; /* gzip -1..-9 value */ +static int cflag; /* stdout mode */ +static int dflag; /* decompress mode */ +static int lflag; /* list mode */ +static int numflag = 6; /* gzip -1..-9 value */ #ifndef SMALL -static int fflag; /* force mode */ -static int kflag; /* don't delete input files */ -static int nflag; /* don't save name/timestamp */ -static int Nflag; /* don't restore name/timestamp */ -static int qflag; /* quiet mode */ -static int rflag; /* recursive mode */ -static int tflag; /* test */ -static int vflag; /* verbose mode */ +static int fflag; /* force mode */ +static int kflag; /* don't delete input files */ +static int nflag; /* don't save name/timestamp */ +static int Nflag; /* don't restore name/timestamp */ +static int qflag; /* quiet mode */ +static int rflag; /* recursive mode */ +static int tflag; /* test */ +static int vflag; /* verbose mode */ #else -#define qflag 0 -#define tflag 0 +#define qflag 0 +#define tflag 0 #endif -static int exit_value = 0; /* exit value */ +static int exit_value = 0; /* exit value */ -static char *infile; /* name of file coming in */ +static char *infile; /* name of file coming in */ -static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead; -#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \ +static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead; +#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \ !defined(NO_XZ_SUPPORT) -static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead; +static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead; #endif -static void maybe_warn(const char *fmt, ...) __printflike(1, 2); -static void maybe_warnx(const char *fmt, ...) __printflike(1, 2); -static enum filetype file_gettype(u_char *); +static void maybe_warn(const char *fmt, ...) __printflike(1, 2); +static void maybe_warnx(const char *fmt, ...) __printflike(1, 2); +static enum filetype file_gettype(u_char *); #ifdef SMALL #define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz) #endif -static off_t gz_compress(int, int, off_t *, const char *, uint32_t); -static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *); -static off_t file_compress(char *, char *, size_t); -static off_t file_uncompress(char *, char *, size_t); -static void handle_pathname(char *); -static void handle_file(char *, struct stat *); -static void handle_stdin(void); -static void handle_stdout(void); -static void print_ratio(off_t, off_t, FILE *); -static void print_list(int fd, off_t, const char *, time_t); -__dead static void usage(void); -__dead static void display_version(void); -static const suffixes_t *check_suffix(char *, int); -static ssize_t read_retry(int, void *, size_t); +static off_t gz_compress(int, int, off_t *, const char *, uint32_t); +static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *); +static off_t file_compress(char *, char *, size_t); +static off_t file_uncompress(char *, char *, size_t); +static void handle_pathname(char *); +static void handle_file(char *, struct stat *); +static void handle_stdin(void); +static void handle_stdout(void); +static void print_ratio(off_t, off_t, FILE *); +static void print_list(int fd, off_t, const char *, time_t); +__dead static void usage(void); +__dead static void display_version(void); +static const suffixes_t *check_suffix(char *, int); +static ssize_t read_retry(int, void *, size_t); #ifdef SMALL #define unlink_input(f, sb) unlink(f) #else -static off_t cat_fd(unsigned char *, size_t, off_t *, int fd); -static void prepend_gzip(char *, int *, char ***); -static void handle_dir(char *); -static void print_verbage(const char *, const char *, off_t, off_t); -static void print_test(const char *, int); -static void copymodes(int fd, const struct stat *, const char *file); -static int check_outfile(const char *outfile); +static off_t cat_fd(unsigned char *, size_t, off_t *, int fd); +static void prepend_gzip(char *, int *, char ***); +static void handle_dir(char *); +static void print_verbage(const char *, const char *, off_t, off_t); +static void print_test(const char *, int); +static void copymodes(int fd, const struct stat *, const char *file); +static int check_outfile(const char *outfile); #endif #ifndef NO_BZIP2_SUPPORT -static off_t unbzip2(int, int, char *, size_t, off_t *); +static off_t unbzip2(int, int, char *, size_t, off_t *); #endif #ifndef NO_COMPRESS_SUPPORT -static FILE *zdopen(int); -static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *); +static FILE *zdopen(int); +static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *); #endif #ifndef NO_PACK_SUPPORT -static off_t unpack(int, int, char *, size_t, off_t *); +static off_t unpack(int, int, char *, size_t, off_t *); #endif #ifndef NO_XZ_SUPPORT -static off_t unxz(int, int, char *, size_t, off_t *); +static off_t unxz(int, int, char *, size_t, off_t *); #endif #ifdef SMALL -#define getopt_long(a,b,c,d,e) getopt(a,b,c) +#define getopt_long(a, b, c, d, e) getopt(a, b, c) #else static const struct option longopts[] = { - { "stdout", no_argument, 0, 'c' }, - { "to-stdout", no_argument, 0, 'c' }, - { "decompress", no_argument, 0, 'd' }, - { "uncompress", no_argument, 0, 'd' }, - { "force", no_argument, 0, 'f' }, - { "help", no_argument, 0, 'h' }, - { "keep", no_argument, 0, 'k' }, - { "list", no_argument, 0, 'l' }, - { "no-name", no_argument, 0, 'n' }, - { "name", no_argument, 0, 'N' }, - { "quiet", no_argument, 0, 'q' }, - { "recursive", no_argument, 0, 'r' }, - { "suffix", required_argument, 0, 'S' }, - { "test", no_argument, 0, 't' }, - { "verbose", no_argument, 0, 'v' }, - { "version", no_argument, 0, 'V' }, - { "fast", no_argument, 0, '1' }, - { "best", no_argument, 0, '9' }, + {"stdout", no_argument, 0, 'c'}, + {"to-stdout", no_argument, 0, 'c'}, + {"decompress", no_argument, 0, 'd'}, + {"uncompress", no_argument, 0, 'd'}, + {"force", no_argument, 0, 'f'}, + {"help", no_argument, 0, 'h'}, + {"keep", no_argument, 0, 'k'}, + {"list", no_argument, 0, 'l'}, + {"no-name", no_argument, 0, 'n'}, + {"name", no_argument, 0, 'N'}, + {"quiet", no_argument, 0, 'q'}, + {"recursive", no_argument, 0, 'r'}, + {"suffix", required_argument, 0, 'S'}, + {"test", no_argument, 0, 't'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"fast", no_argument, 0, '1'}, + {"best", no_argument, 0, '9'}, #if 0 /* * This is what else GNU gzip implements. --ascii isn't useful - * on NetBSD, and I don't care to have a --license. + * and I don't care to have a --license. */ { "ascii", no_argument, 0, 'a' }, { "license", no_argument, 0, 'L' }, #endif - { NULL, no_argument, 0, 0 }, + {NULL, no_argument, 0, 0}, }; #endif -int -main(int argc, char **argv) -{ - const char *progname = getprogname(); +int main(int argc, char **argv) { + const char *progname = getprogname(); #ifndef SMALL - char *gzip; - int len; + char *gzip; + int len; #endif - int ch; + int ch; - /* XXX set up signals */ + /* XXX set up signals */ #ifndef SMALL - if ((gzip = getenv("GZIP")) != NULL) - prepend_gzip(gzip, &argc, &argv); + if ((gzip = getenv("GZIP")) != NULL) + prepend_gzip(gzip, &argc, &argv); #endif - /* - * XXX - * handle being called `gunzip', `zcat' and `gzcat' - */ - if (strcmp(progname, "gunzip") == 0) - dflag = 1; - else if (strcmp(progname, "zcat") == 0 || - strcmp(progname, "gzcat") == 0) - dflag = cflag = 1; + /* + * XXX + * handle being called `gunzip', `zcat' and `gzcat' + */ + if (strcmp(progname, "gunzip") == 0) + dflag = 1; + else if (strcmp(progname, "zcat") == 0 || strcmp(progname, "gzcat") == 0) + dflag = cflag = 1; #ifdef SMALL #define OPT_LIST "123456789cdhlV" @@ -307,385 +298,373 @@ main(int argc, char **argv) #define OPT_LIST "123456789cdfhklNnqrS:tVv" #endif - while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) { - switch (ch) { - case '1': case '2': case '3': - case '4': case '5': case '6': - case '7': case '8': case '9': - numflag = ch - '0'; - break; - case 'c': - cflag = 1; - break; - case 'd': - dflag = 1; - break; - case 'l': - lflag = 1; - dflag = 1; - break; - case 'V': - display_version(); - /* NOTREACHED */ + while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) { + switch (ch) { + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + numflag = ch - '0'; + break; + case 'c': + cflag = 1; + break; + case 'd': + dflag = 1; + break; + case 'l': + lflag = 1; + dflag = 1; + break; + case 'V': + display_version(); + /* NOTREACHED */ #ifndef SMALL - case 'f': - fflag = 1; - break; - case 'k': - kflag = 1; - break; - case 'N': - nflag = 0; - Nflag = 1; - break; - case 'n': - nflag = 1; - Nflag = 0; - break; - case 'q': - qflag = 1; - break; - case 'r': - rflag = 1; - break; - case 'S': - len = strlen(optarg); - if (len != 0) { - if (len > SUFFIX_MAXLEN) - errx(1, "incorrect suffix: '%s'", optarg); - suffixes[0].zipped = optarg; - suffixes[0].ziplen = len; - } else { - suffixes[NUM_SUFFIXES - 1].zipped = ""; - suffixes[NUM_SUFFIXES - 1].ziplen = 0; - } - break; - case 't': - cflag = 1; - tflag = 1; - dflag = 1; - break; - case 'v': - vflag = 1; - break; + case 'f': + fflag = 1; + break; + case 'k': + kflag = 1; + break; + case 'N': + nflag = 0; + Nflag = 1; + break; + case 'n': + nflag = 1; + Nflag = 0; + break; + case 'q': + qflag = 1; + break; + case 'r': + rflag = 1; + break; + case 'S': + len = strlen(optarg); + if (len != 0) { + if (len > SUFFIX_MAXLEN) + errx(1, "incorrect suffix: '%s'", optarg); + suffixes[0].zipped = optarg; + suffixes[0].ziplen = len; + } else { + suffixes[NUM_SUFFIXES - 1].zipped = ""; + suffixes[NUM_SUFFIXES - 1].ziplen = 0; + } + break; + case 't': + cflag = 1; + tflag = 1; + dflag = 1; + break; + case 'v': + vflag = 1; + break; #endif - default: - usage(); - /* NOTREACHED */ - } - } - argv += optind; - argc -= optind; + default: + usage(); + /* NOTREACHED */ + } + } + argv += optind; + argc -= optind; - if (argc == 0) { - if (dflag) /* stdin mode */ - handle_stdin(); - else /* stdout mode */ - handle_stdout(); - } else { - do { - handle_pathname(argv[0]); - } while (*++argv); - } + if (argc == 0) { + if (dflag) /* stdin mode */ + handle_stdin(); + else /* stdout mode */ + handle_stdout(); + } else { + do { + handle_pathname(argv[0]); + } while (*++argv); + } #ifndef SMALL - if (qflag == 0 && lflag && argc > 1) - print_list(-1, 0, "(totals)", 0); + if (qflag == 0 && lflag && argc > 1) + print_list(-1, 0, "(totals)", 0); #endif - exit(exit_value); + exit(exit_value); } /* maybe print a warning */ -void -maybe_warn(const char *fmt, ...) -{ - va_list ap; +void maybe_warn(const char *fmt, ...) { + va_list ap; - if (qflag == 0) { - va_start(ap, fmt); - vwarn(fmt, ap); - va_end(ap); - } - if (exit_value == 0) - exit_value = 1; + if (qflag == 0) { + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); + } + if (exit_value == 0) + exit_value = 1; } /* ... without an errno. */ -void -maybe_warnx(const char *fmt, ...) -{ - va_list ap; +void maybe_warnx(const char *fmt, ...) { + va_list ap; - if (qflag == 0) { - va_start(ap, fmt); - vwarnx(fmt, ap); - va_end(ap); - } - if (exit_value == 0) - exit_value = 1; + if (qflag == 0) { + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); + } + if (exit_value == 0) + exit_value = 1; } /* maybe print an error */ -void -maybe_err(const char *fmt, ...) -{ - va_list ap; +void maybe_err(const char *fmt, ...) { + va_list ap; - if (qflag == 0) { - va_start(ap, fmt); - vwarn(fmt, ap); - va_end(ap); - } - exit(2); + if (qflag == 0) { + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); + } + exit(2); } -#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \ +#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \ !defined(NO_XZ_SUPPORT) /* ... without an errno. */ -void -maybe_errx(const char *fmt, ...) -{ - va_list ap; +void maybe_errx(const char *fmt, ...) { + va_list ap; - if (qflag == 0) { - va_start(ap, fmt); - vwarnx(fmt, ap); - va_end(ap); - } - exit(2); + if (qflag == 0) { + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); + } + exit(2); } #endif #ifndef SMALL /* split up $GZIP and prepend it to the argument list */ -static void -prepend_gzip(char *gzip, int *argc, char ***argv) -{ - char *s, **nargv, **ac; - int nenvarg = 0, i; +static void prepend_gzip(char *gzip, int *argc, char ***argv) { + char *s, **nargv, **ac; + int nenvarg = 0, i; - /* scan how many arguments there are */ - for (s = gzip;;) { - while (*s == ' ' || *s == '\t') - s++; - if (*s == 0) - goto count_done; - nenvarg++; - while (*s != ' ' && *s != '\t') - if (*s++ == 0) - goto count_done; - } + /* scan how many arguments there are */ + for (s = gzip;;) { + while (*s == ' ' || *s == '\t') + s++; + if (*s == 0) + goto count_done; + nenvarg++; + while (*s != ' ' && *s != '\t') + if (*s++ == 0) + goto count_done; + } count_done: - /* punt early */ - if (nenvarg == 0) - return; + /* punt early */ + if (nenvarg == 0) + return; - *argc += nenvarg; - ac = *argv; + *argc += nenvarg; + ac = *argv; - nargv = (char **)malloc((*argc + 1) * sizeof(char *)); - if (nargv == NULL) - maybe_err("malloc"); + nargv = (char **)malloc((*argc + 1) * sizeof(char *)); + if (nargv == NULL) + maybe_err("malloc"); - /* stash this away */ - *argv = nargv; + /* stash this away */ + *argv = nargv; - /* copy the program name first */ - i = 0; - nargv[i++] = *(ac++); + /* copy the program name first */ + i = 0; + nargv[i++] = *(ac++); - /* take a copy of $GZIP and add it to the array */ - s = strdup(gzip); - if (s == NULL) - maybe_err("strdup"); - for (;;) { - /* Skip whitespaces. */ - while (*s == ' ' || *s == '\t') - s++; - if (*s == 0) - goto copy_done; - nargv[i++] = s; - /* Find the end of this argument. */ - while (*s != ' ' && *s != '\t') - if (*s++ == 0) - /* Argument followed by NUL. */ - goto copy_done; - /* Terminate by overwriting ' ' or '\t' with NUL. */ - *s++ = 0; - } + /* take a copy of $GZIP and add it to the array */ + s = strdup(gzip); + if (s == NULL) + maybe_err("strdup"); + for (;;) { + /* Skip whitespaces. */ + while (*s == ' ' || *s == '\t') + s++; + if (*s == 0) + goto copy_done; + nargv[i++] = s; + /* Find the end of this argument. */ + while (*s != ' ' && *s != '\t') + if (*s++ == 0) + /* Argument followed by NUL. */ + goto copy_done; + /* Terminate by overwriting ' ' or '\t' with NUL. */ + *s++ = 0; + } copy_done: - /* copy the original arguments and a NULL */ - while (*ac) - nargv[i++] = *(ac++); - nargv[i] = NULL; + /* copy the original arguments and a NULL */ + while (*ac) + nargv[i++] = *(ac++); + nargv[i] = NULL; } #endif /* compress input to output. Return bytes read, -1 on error */ -static off_t -gz_compress(int in, int out, off_t *gsizep, const char *origname, uint32_t mtime) -{ - z_stream z; - char *outbufp, *inbufp; - off_t in_tot = 0, out_tot = 0; - ssize_t in_size; - int i, error; - uLong crc; +static off_t gz_compress(int in, int out, off_t *gsizep, const char *origname, + uint32_t mtime) { + z_stream z; + char *outbufp, *inbufp; + off_t in_tot = 0, out_tot = 0; + ssize_t in_size; + int i, error; + uLong crc; #ifdef SMALL - static char header[] = { GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, 0, - 0, 0, 0, 0, - 0, OS_CODE }; + static char header[] = {GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, 0, 0, + 0, 0, 0, 0, OS_CODE}; #endif - outbufp = malloc(BUFLEN); - inbufp = malloc(BUFLEN); - if (outbufp == NULL || inbufp == NULL) { - maybe_err("malloc failed"); - goto out; - } + outbufp = malloc(BUFLEN); + inbufp = malloc(BUFLEN); + if (outbufp == NULL || inbufp == NULL) { + maybe_err("malloc failed"); + goto out; + } - memset(&z, 0, sizeof z); - z.zalloc = Z_NULL; - z.zfree = Z_NULL; - z.opaque = 0; + memset(&z, 0, sizeof z); + z.zalloc = Z_NULL; + z.zfree = Z_NULL; + z.opaque = 0; #ifdef SMALL - memcpy(outbufp, header, sizeof header); - i = sizeof header; + memcpy(outbufp, header, sizeof header); + i = sizeof header; #else - if (nflag != 0) { - mtime = 0; - origname = ""; - } + if (nflag != 0) { + mtime = 0; + origname = ""; + } - i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s", - GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, - *origname ? ORIG_NAME : 0, - mtime & 0xff, - (mtime >> 8) & 0xff, - (mtime >> 16) & 0xff, - (mtime >> 24) & 0xff, - numflag == 1 ? 4 : numflag == 9 ? 2 : 0, - OS_CODE, origname); - if (i >= BUFLEN) - /* this need PATH_MAX > BUFLEN ... */ - maybe_err("snprintf"); - if (*origname) - i++; + i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s", GZIP_MAGIC0, + GZIP_MAGIC1, Z_DEFLATED, *origname ? ORIG_NAME : 0, mtime & 0xff, + (mtime >> 8) & 0xff, (mtime >> 16) & 0xff, (mtime >> 24) & 0xff, + numflag == 1 ? 4 + : numflag == 9 ? 2 + : 0, + OS_CODE, origname); + if (i >= BUFLEN) + /* this need PATH_MAX > BUFLEN ... */ + maybe_err("snprintf"); + if (*origname) + i++; #endif - z.next_out = (unsigned char *)outbufp + i; - z.avail_out = BUFLEN - i; + z.next_out = (unsigned char *)outbufp + i; + z.avail_out = BUFLEN - i; - error = deflateInit2(&z, numflag, Z_DEFLATED, - (-MAX_WBITS), 8, Z_DEFAULT_STRATEGY); - if (error != Z_OK) { - maybe_warnx("deflateInit2 failed"); - in_tot = -1; - goto out; - } + error = deflateInit2(&z, numflag, Z_DEFLATED, (-MAX_WBITS), 8, + Z_DEFAULT_STRATEGY); + if (error != Z_OK) { + maybe_warnx("deflateInit2 failed"); + in_tot = -1; + goto out; + } - crc = crc32(0L, Z_NULL, 0); - for (;;) { - if (z.avail_out == 0) { - if (write(out, outbufp, BUFLEN) != BUFLEN) { - maybe_warn("write"); - out_tot = -1; - goto out; - } + crc = crc32(0L, Z_NULL, 0); + for (;;) { + if (z.avail_out == 0) { + if (write(out, outbufp, BUFLEN) != BUFLEN) { + maybe_warn("write"); + out_tot = -1; + goto out; + } - out_tot += BUFLEN; - z.next_out = (unsigned char *)outbufp; - z.avail_out = BUFLEN; - } + out_tot += BUFLEN; + z.next_out = (unsigned char *)outbufp; + z.avail_out = BUFLEN; + } - if (z.avail_in == 0) { - in_size = read(in, inbufp, BUFLEN); - if (in_size < 0) { - maybe_warn("read"); - in_tot = -1; - goto out; - } - if (in_size == 0) - break; + if (z.avail_in == 0) { + in_size = read(in, inbufp, BUFLEN); + if (in_size < 0) { + maybe_warn("read"); + in_tot = -1; + goto out; + } + if (in_size == 0) + break; - crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size); - in_tot += in_size; - z.next_in = (unsigned char *)inbufp; - z.avail_in = in_size; - } + crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size); + in_tot += in_size; + z.next_in = (unsigned char *)inbufp; + z.avail_in = in_size; + } - error = deflate(&z, Z_NO_FLUSH); - if (error != Z_OK && error != Z_STREAM_END) { - maybe_warnx("deflate failed"); - in_tot = -1; - goto out; - } - } + error = deflate(&z, Z_NO_FLUSH); + if (error != Z_OK && error != Z_STREAM_END) { + maybe_warnx("deflate failed"); + in_tot = -1; + goto out; + } + } - /* clean up */ - for (;;) { - size_t len; - ssize_t w; + /* clean up */ + for (;;) { + size_t len; + ssize_t w; - error = deflate(&z, Z_FINISH); - if (error != Z_OK && error != Z_STREAM_END) { - maybe_warnx("deflate failed"); - in_tot = -1; - goto out; - } + error = deflate(&z, Z_FINISH); + if (error != Z_OK && error != Z_STREAM_END) { + maybe_warnx("deflate failed"); + in_tot = -1; + goto out; + } - len = (char *)z.next_out - outbufp; + len = (char *)z.next_out - outbufp; - w = write(out, outbufp, len); - if (w == -1 || (size_t)w != len) { - maybe_warn("write"); - out_tot = -1; - goto out; - } - out_tot += len; - z.next_out = (unsigned char *)outbufp; - z.avail_out = BUFLEN; + w = write(out, outbufp, len); + if (w == -1 || (size_t)w != len) { + maybe_warn("write"); + out_tot = -1; + goto out; + } + out_tot += len; + z.next_out = (unsigned char *)outbufp; + z.avail_out = BUFLEN; - if (error == Z_STREAM_END) - break; - } + if (error == Z_STREAM_END) + break; + } - if (deflateEnd(&z) != Z_OK) { - maybe_warnx("deflateEnd failed"); - in_tot = -1; - goto out; - } + if (deflateEnd(&z) != Z_OK) { + maybe_warnx("deflateEnd failed"); + in_tot = -1; + goto out; + } - i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c", - (int)crc & 0xff, - (int)(crc >> 8) & 0xff, - (int)(crc >> 16) & 0xff, - (int)(crc >> 24) & 0xff, - (int)in_tot & 0xff, - (int)(in_tot >> 8) & 0xff, - (int)(in_tot >> 16) & 0xff, - (int)(in_tot >> 24) & 0xff); - if (i != 8) - maybe_err("snprintf"); + i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c", (int)crc & 0xff, + (int)(crc >> 8) & 0xff, (int)(crc >> 16) & 0xff, + (int)(crc >> 24) & 0xff, (int)in_tot & 0xff, + (int)(in_tot >> 8) & 0xff, (int)(in_tot >> 16) & 0xff, + (int)(in_tot >> 24) & 0xff); + if (i != 8) + maybe_err("snprintf"); #if 0 if (in_tot > 0xffffffff) maybe_warn("input file size >= 4GB cannot be saved"); #endif - if (write(out, outbufp, i) != i) { - maybe_warn("write"); - in_tot = -1; - } else - out_tot += i; + if (write(out, outbufp, i) != i) { + maybe_warn("write"); + in_tot = -1; + } else + out_tot += i; out: - if (inbufp != NULL) - free(inbufp); - if (outbufp != NULL) - free(outbufp); - if (gsizep) - *gsizep = out_tot; - return in_tot; + if (inbufp != NULL) + free(inbufp); + if (outbufp != NULL) + free(outbufp); + if (gsizep) + *gsizep = out_tot; + return in_tot; } /* @@ -693,348 +672,345 @@ out: * uncompressed size written, and put the compressed sized read * into `*gsizep'. */ -static off_t -gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep, - const char *filename) -{ - z_stream z; - char *outbufp, *inbufp; - off_t out_tot = -1, in_tot = 0; - uint32_t out_sub_tot = 0; - enum { - GZSTATE_MAGIC0, - GZSTATE_MAGIC1, - GZSTATE_METHOD, - GZSTATE_FLAGS, - GZSTATE_SKIPPING, - GZSTATE_EXTRA, - GZSTATE_EXTRA2, - GZSTATE_EXTRA3, - GZSTATE_ORIGNAME, - GZSTATE_COMMENT, - GZSTATE_HEAD_CRC1, - GZSTATE_HEAD_CRC2, - GZSTATE_INIT, - GZSTATE_READ, - GZSTATE_CRC, - GZSTATE_LEN, - } state = GZSTATE_MAGIC0; - int flags = 0, skip_count = 0; - int error = Z_STREAM_ERROR, done_reading = 0; - uLong crc = 0; - ssize_t wr; - int needmore = 0; +static off_t gz_uncompress(int in, int out, char *pre, size_t prelen, + off_t *gsizep, const char *filename) { + z_stream z; + char *outbufp, *inbufp; + off_t out_tot = -1, in_tot = 0; + uint32_t out_sub_tot = 0; + enum { + GZSTATE_MAGIC0, + GZSTATE_MAGIC1, + GZSTATE_METHOD, + GZSTATE_FLAGS, + GZSTATE_SKIPPING, + GZSTATE_EXTRA, + GZSTATE_EXTRA2, + GZSTATE_EXTRA3, + GZSTATE_ORIGNAME, + GZSTATE_COMMENT, + GZSTATE_HEAD_CRC1, + GZSTATE_HEAD_CRC2, + GZSTATE_INIT, + GZSTATE_READ, + GZSTATE_CRC, + GZSTATE_LEN, + } state = GZSTATE_MAGIC0; + int flags = 0, skip_count = 0; + int error = Z_STREAM_ERROR, done_reading = 0; + uLong crc = 0; + ssize_t wr; + int needmore = 0; -#define ADVANCE() { z.next_in++; z.avail_in--; } +#define ADVANCE() \ + { \ + z.next_in++; \ + z.avail_in--; \ + } - if ((outbufp = malloc(BUFLEN)) == NULL) { - maybe_err("malloc failed"); - goto out2; - } - if ((inbufp = malloc(BUFLEN)) == NULL) { - maybe_err("malloc failed"); - goto out1; - } + if ((outbufp = malloc(BUFLEN)) == NULL) { + maybe_err("malloc failed"); + goto out2; + } + if ((inbufp = malloc(BUFLEN)) == NULL) { + maybe_err("malloc failed"); + goto out1; + } - memset(&z, 0, sizeof z); - z.avail_in = prelen; - z.next_in = (unsigned char *)pre; - z.avail_out = BUFLEN; - z.next_out = (unsigned char *)outbufp; - z.zalloc = NULL; - z.zfree = NULL; - z.opaque = 0; + memset(&z, 0, sizeof z); + z.avail_in = prelen; + z.next_in = (unsigned char *)pre; + z.avail_out = BUFLEN; + z.next_out = (unsigned char *)outbufp; + z.zalloc = NULL; + z.zfree = NULL; + z.opaque = 0; - in_tot = prelen; - out_tot = 0; + in_tot = prelen; + out_tot = 0; - for (;;) { - if ((z.avail_in == 0 || needmore) && done_reading == 0) { - ssize_t in_size; + for (;;) { + if ((z.avail_in == 0 || needmore) && done_reading == 0) { + ssize_t in_size; - if (z.avail_in > 0) { - memmove(inbufp, z.next_in, z.avail_in); - } - z.next_in = (unsigned char *)inbufp; - in_size = read(in, z.next_in + z.avail_in, - BUFLEN - z.avail_in); + if (z.avail_in > 0) { + memmove(inbufp, z.next_in, z.avail_in); + } + z.next_in = (unsigned char *)inbufp; + in_size = read(in, z.next_in + z.avail_in, BUFLEN - z.avail_in); - if (in_size == -1) { - maybe_warn("failed to read stdin"); - goto stop_and_fail; - } else if (in_size == 0) { - done_reading = 1; - } + if (in_size == -1) { + maybe_warn("failed to read stdin"); + goto stop_and_fail; + } else if (in_size == 0) { + done_reading = 1; + } - z.avail_in += in_size; - needmore = 0; + z.avail_in += in_size; + needmore = 0; - in_tot += in_size; - } - if (z.avail_in == 0) { - if (done_reading && state != GZSTATE_MAGIC0) { - maybe_warnx("%s: unexpected end of file", - filename); - goto stop_and_fail; - } - goto stop; - } - switch (state) { - case GZSTATE_MAGIC0: - if (*z.next_in != GZIP_MAGIC0) { - if (in_tot > 0) { - maybe_warnx("%s: trailing garbage " - "ignored", filename); - goto stop; - } - maybe_warnx("input not gziped (MAGIC0)"); - goto stop_and_fail; - } - ADVANCE(); - state++; - out_sub_tot = 0; - crc = crc32(0L, Z_NULL, 0); - break; + in_tot += in_size; + } + if (z.avail_in == 0) { + if (done_reading && state != GZSTATE_MAGIC0) { + maybe_warnx("%s: unexpected end of file", filename); + goto stop_and_fail; + } + goto stop; + } + switch (state) { + case GZSTATE_MAGIC0: + if (*z.next_in != GZIP_MAGIC0) { + if (in_tot > 0) { + maybe_warnx("%s: trailing garbage " + "ignored", + filename); + goto stop; + } + maybe_warnx("input not gziped (MAGIC0)"); + goto stop_and_fail; + } + ADVANCE(); + state++; + out_sub_tot = 0; + crc = crc32(0L, Z_NULL, 0); + break; - case GZSTATE_MAGIC1: - if (*z.next_in != GZIP_MAGIC1 && - *z.next_in != GZIP_OMAGIC1) { - maybe_warnx("input not gziped (MAGIC1)"); - goto stop_and_fail; - } - ADVANCE(); - state++; - break; + case GZSTATE_MAGIC1: + if (*z.next_in != GZIP_MAGIC1 && *z.next_in != GZIP_OMAGIC1) { + maybe_warnx("input not gziped (MAGIC1)"); + goto stop_and_fail; + } + ADVANCE(); + state++; + break; - case GZSTATE_METHOD: - if (*z.next_in != Z_DEFLATED) { - maybe_warnx("unknown compression method"); - goto stop_and_fail; - } - ADVANCE(); - state++; - break; + case GZSTATE_METHOD: + if (*z.next_in != Z_DEFLATED) { + maybe_warnx("unknown compression method"); + goto stop_and_fail; + } + ADVANCE(); + state++; + break; - case GZSTATE_FLAGS: - flags = *z.next_in; - ADVANCE(); - skip_count = 6; - state++; - break; + case GZSTATE_FLAGS: + flags = *z.next_in; + ADVANCE(); + skip_count = 6; + state++; + break; - case GZSTATE_SKIPPING: - if (skip_count > 0) { - skip_count--; - ADVANCE(); - } else - state++; - break; + case GZSTATE_SKIPPING: + if (skip_count > 0) { + skip_count--; + ADVANCE(); + } else + state++; + break; - case GZSTATE_EXTRA: - if ((flags & EXTRA_FIELD) == 0) { - state = GZSTATE_ORIGNAME; - break; - } - skip_count = *z.next_in; - ADVANCE(); - state++; - break; + case GZSTATE_EXTRA: + if ((flags & EXTRA_FIELD) == 0) { + state = GZSTATE_ORIGNAME; + break; + } + skip_count = *z.next_in; + ADVANCE(); + state++; + break; - case GZSTATE_EXTRA2: - skip_count |= ((*z.next_in) << 8); - ADVANCE(); - state++; - break; + case GZSTATE_EXTRA2: + skip_count |= ((*z.next_in) << 8); + ADVANCE(); + state++; + break; - case GZSTATE_EXTRA3: - if (skip_count > 0) { - skip_count--; - ADVANCE(); - } else - state++; - break; + case GZSTATE_EXTRA3: + if (skip_count > 0) { + skip_count--; + ADVANCE(); + } else + state++; + break; - case GZSTATE_ORIGNAME: - if ((flags & ORIG_NAME) == 0) { - state++; - break; - } - if (*z.next_in == 0) - state++; - ADVANCE(); - break; + case GZSTATE_ORIGNAME: + if ((flags & ORIG_NAME) == 0) { + state++; + break; + } + if (*z.next_in == 0) + state++; + ADVANCE(); + break; - case GZSTATE_COMMENT: - if ((flags & COMMENT) == 0) { - state++; - break; - } - if (*z.next_in == 0) - state++; - ADVANCE(); - break; + case GZSTATE_COMMENT: + if ((flags & COMMENT) == 0) { + state++; + break; + } + if (*z.next_in == 0) + state++; + ADVANCE(); + break; - case GZSTATE_HEAD_CRC1: - if (flags & HEAD_CRC) - skip_count = 2; - else - skip_count = 0; - state++; - break; + case GZSTATE_HEAD_CRC1: + if (flags & HEAD_CRC) + skip_count = 2; + else + skip_count = 0; + state++; + break; - case GZSTATE_HEAD_CRC2: - if (skip_count > 0) { - skip_count--; - ADVANCE(); - } else - state++; - break; + case GZSTATE_HEAD_CRC2: + if (skip_count > 0) { + skip_count--; + ADVANCE(); + } else + state++; + break; - case GZSTATE_INIT: - if (inflateInit2(&z, -MAX_WBITS) != Z_OK) { - maybe_warnx("failed to inflateInit"); - goto stop_and_fail; - } - state++; - break; + case GZSTATE_INIT: + if (inflateInit2(&z, -MAX_WBITS) != Z_OK) { + maybe_warnx("failed to inflateInit"); + goto stop_and_fail; + } + state++; + break; - case GZSTATE_READ: - error = inflate(&z, Z_FINISH); - switch (error) { - /* Z_BUF_ERROR goes with Z_FINISH... */ - case Z_BUF_ERROR: - if (z.avail_out > 0 && !done_reading) - continue; + case GZSTATE_READ: + error = inflate(&z, Z_FINISH); + switch (error) { + /* Z_BUF_ERROR goes with Z_FINISH... */ + case Z_BUF_ERROR: + if (z.avail_out > 0 && !done_reading) + continue; - case Z_STREAM_END: - case Z_OK: - break; + case Z_STREAM_END: + case Z_OK: + break; - case Z_NEED_DICT: - maybe_warnx("Z_NEED_DICT error"); - goto stop_and_fail; - case Z_DATA_ERROR: - maybe_warnx("data stream error"); - goto stop_and_fail; - case Z_STREAM_ERROR: - maybe_warnx("internal stream error"); - goto stop_and_fail; - case Z_MEM_ERROR: - maybe_warnx("memory allocation error"); - goto stop_and_fail; + case Z_NEED_DICT: + maybe_warnx("Z_NEED_DICT error"); + goto stop_and_fail; + case Z_DATA_ERROR: + maybe_warnx("data stream error"); + goto stop_and_fail; + case Z_STREAM_ERROR: + maybe_warnx("internal stream error"); + goto stop_and_fail; + case Z_MEM_ERROR: + maybe_warnx("memory allocation error"); + goto stop_and_fail; - default: - maybe_warn("unknown error from inflate(): %d", - error); - } - wr = BUFLEN - z.avail_out; + default: + maybe_warn("unknown error from inflate(): %d", error); + } + wr = BUFLEN - z.avail_out; - if (wr != 0) { - crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr); - if ( + if (wr != 0) { + crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr); + if ( #ifndef SMALL - /* don't write anything with -t */ - tflag == 0 && + /* don't write anything with -t */ + tflag == 0 && #endif - write(out, outbufp, wr) != wr) { - maybe_warn("error writing to output"); - goto stop_and_fail; - } + write(out, outbufp, wr) != wr) { + maybe_warn("error writing to output"); + goto stop_and_fail; + } - out_tot += wr; - out_sub_tot += wr; - } + out_tot += wr; + out_sub_tot += wr; + } - if (error == Z_STREAM_END) { - inflateEnd(&z); - state++; - } + if (error == Z_STREAM_END) { + inflateEnd(&z); + state++; + } - z.next_out = (unsigned char *)outbufp; - z.avail_out = BUFLEN; + z.next_out = (unsigned char *)outbufp; + z.avail_out = BUFLEN; - break; - case GZSTATE_CRC: - { - uLong origcrc; + break; + case GZSTATE_CRC: { + uLong origcrc; - if (z.avail_in < 4) { - if (!done_reading) { - needmore = 1; - continue; - } - maybe_warnx("truncated input"); - goto stop_and_fail; - } - origcrc = ((unsigned)z.next_in[0] & 0xff) | - ((unsigned)z.next_in[1] & 0xff) << 8 | - ((unsigned)z.next_in[2] & 0xff) << 16 | - ((unsigned)z.next_in[3] & 0xff) << 24; - if (origcrc != crc) { - maybe_warnx("invalid compressed" - " data--crc error"); - goto stop_and_fail; - } - } + if (z.avail_in < 4) { + if (!done_reading) { + needmore = 1; + continue; + } + maybe_warnx("truncated input"); + goto stop_and_fail; + } + origcrc = ((unsigned)z.next_in[0] & 0xff) | + ((unsigned)z.next_in[1] & 0xff) << 8 | + ((unsigned)z.next_in[2] & 0xff) << 16 | + ((unsigned)z.next_in[3] & 0xff) << 24; + if (origcrc != crc) { + maybe_warnx("invalid compressed" + " data--crc error"); + goto stop_and_fail; + } + } - z.avail_in -= 4; - z.next_in += 4; + z.avail_in -= 4; + z.next_in += 4; - if (!z.avail_in && done_reading) { - goto stop; - } - state++; - break; - case GZSTATE_LEN: - { - uLong origlen; + if (!z.avail_in && done_reading) { + goto stop; + } + state++; + break; + case GZSTATE_LEN: { + uLong origlen; - if (z.avail_in < 4) { - if (!done_reading) { - needmore = 1; - continue; - } - maybe_warnx("truncated input"); - goto stop_and_fail; - } - origlen = ((unsigned)z.next_in[0] & 0xff) | - ((unsigned)z.next_in[1] & 0xff) << 8 | - ((unsigned)z.next_in[2] & 0xff) << 16 | - ((unsigned)z.next_in[3] & 0xff) << 24; + if (z.avail_in < 4) { + if (!done_reading) { + needmore = 1; + continue; + } + maybe_warnx("truncated input"); + goto stop_and_fail; + } + origlen = ((unsigned)z.next_in[0] & 0xff) | + ((unsigned)z.next_in[1] & 0xff) << 8 | + ((unsigned)z.next_in[2] & 0xff) << 16 | + ((unsigned)z.next_in[3] & 0xff) << 24; - if (origlen != out_sub_tot) { - maybe_warnx("invalid compressed" - " data--length error"); - goto stop_and_fail; - } - } - - z.avail_in -= 4; - z.next_in += 4; + if (origlen != out_sub_tot) { + maybe_warnx("invalid compressed" + " data--length error"); + goto stop_and_fail; + } + } - if (error < 0) { - maybe_warnx("decompression error"); - goto stop_and_fail; - } - state = GZSTATE_MAGIC0; - break; - } - continue; -stop_and_fail: - out_tot = -1; -stop: - break; - } - if (state > GZSTATE_INIT) - inflateEnd(&z); + z.avail_in -= 4; + z.next_in += 4; - free(inbufp); + if (error < 0) { + maybe_warnx("decompression error"); + goto stop_and_fail; + } + state = GZSTATE_MAGIC0; + break; + } + continue; + stop_and_fail: + out_tot = -1; + stop: + break; + } + if (state > GZSTATE_INIT) + inflateEnd(&z); + + free(inbufp); out1: - free(outbufp); + free(outbufp); out2: - if (gsizep) - *gsizep = in_tot; - return (out_tot); + if (gsizep) + *gsizep = in_tot; + return (out_tot); } #ifndef SMALL @@ -1042,917 +1018,878 @@ out2: * set the owner, mode, flags & utimes using the given file descriptor. * file is only used in possible warning messages. */ -static void -copymodes(int fd, const struct stat *sbp, const char *file) -{ - struct timeval times[2]; - struct stat sb; +static void copymodes(int fd, const struct stat *sbp, const char *file) { + struct timeval times[2]; + struct stat sb; - /* - * If we have no info on the input, give this file some - * default values and return.. - */ - if (sbp == NULL) { - mode_t mask = umask(022); + /* + * If we have no info on the input, give this file some + * default values and return.. + */ + if (sbp == NULL) { + mode_t mask = umask(022); - (void)fchmod(fd, DEFFILEMODE & ~mask); - (void)umask(mask); - return; - } - sb = *sbp; + (void)fchmod(fd, DEFFILEMODE & ~mask); + (void)umask(mask); + return; + } + sb = *sbp; - /* if the chown fails, remove set-id bits as-per compress(1) */ - if (fchown(fd, sb.st_uid, sb.st_gid) < 0) { - if (errno != EPERM) - maybe_warn("couldn't fchown: %s", file); - sb.st_mode &= ~(S_ISUID|S_ISGID); - } + /* if the chown fails, remove set-id bits as-per compress(1) */ + if (fchown(fd, sb.st_uid, sb.st_gid) < 0) { + if (errno != EPERM) + maybe_warn("couldn't fchown: %s", file); + sb.st_mode &= ~(S_ISUID | S_ISGID); + } - /* we only allow set-id and the 9 normal permission bits */ - sb.st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; - if (fchmod(fd, sb.st_mode) < 0) - maybe_warn("couldn't fchmod: %s", file); + /* we only allow set-id and the 9 normal permission bits */ + sb.st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; + if (fchmod(fd, sb.st_mode) < 0) + maybe_warn("couldn't fchmod: %s", file); - /* only try flags if they exist already */ + /* only try flags if they exist already */ #if !defined(__minix) - if (sb.st_flags != 0 && fchflags(fd, sb.st_flags) < 0) - maybe_warn("couldn't fchflags: %s", file); + if (sb.st_flags != 0 && fchflags(fd, sb.st_flags) < 0) + maybe_warn("couldn't fchflags: %s", file); #endif /* !defined(__minix) */ - TIMESPEC_TO_TIMEVAL(×[0], &sb.st_atimespec); - TIMESPEC_TO_TIMEVAL(×[1], &sb.st_mtimespec); - if (futimes(fd, times) < 0) - maybe_warn("couldn't utimes: %s", file); + TIMESPEC_TO_TIMEVAL(×[0], &sb.st_atimespec); + TIMESPEC_TO_TIMEVAL(×[1], &sb.st_mtimespec); + if (futimes(fd, times) < 0) + maybe_warn("couldn't utimes: %s", file); } #endif /* what sort of file is this? */ -static enum filetype -file_gettype(u_char *buf) -{ +static enum filetype file_gettype(u_char *buf) { - if (buf[0] == GZIP_MAGIC0 && - (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1)) - return FT_GZIP; - else + if (buf[0] == GZIP_MAGIC0 && + (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1)) + return FT_GZIP; + else #ifndef NO_BZIP2_SUPPORT - if (memcmp(buf, BZIP2_MAGIC, 3) == 0 && - buf[3] >= '0' && buf[3] <= '9') - return FT_BZIP2; - else + if (memcmp(buf, BZIP2_MAGIC, 3) == 0 && buf[3] >= '0' && buf[3] <= '9') + return FT_BZIP2; + else #endif #ifndef NO_COMPRESS_SUPPORT - if (memcmp(buf, Z_MAGIC, 2) == 0) - return FT_Z; - else + if (memcmp(buf, Z_MAGIC, 2) == 0) + return FT_Z; + else #endif #ifndef NO_PACK_SUPPORT - if (memcmp(buf, PACK_MAGIC, 2) == 0) - return FT_PACK; - else + if (memcmp(buf, PACK_MAGIC, 2) == 0) + return FT_PACK; + else #endif #ifndef NO_XZ_SUPPORT - if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */ - return FT_XZ; - else + if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */ + return FT_XZ; + else #endif - return FT_UNKNOWN; + return FT_UNKNOWN; } #ifndef SMALL /* check the outfile is OK. */ -static int -check_outfile(const char *outfile) -{ - struct stat sb; - int ok = 1; +static int check_outfile(const char *outfile) { + struct stat sb; + int ok = 1; - if (lflag == 0 && stat(outfile, &sb) == 0) { - if (fflag) - unlink(outfile); - else if (isatty(STDIN_FILENO)) { - char ans[10] = { 'n', '\0' }; /* default */ + if (lflag == 0 && stat(outfile, &sb) == 0) { + if (fflag) + unlink(outfile); + else if (isatty(STDIN_FILENO)) { + char ans[10] = {'n', '\0'}; /* default */ - fprintf(stderr, "%s already exists -- do you wish to " - "overwrite (y or n)? " , outfile); - (void)fgets(ans, sizeof(ans) - 1, stdin); - if (ans[0] != 'y' && ans[0] != 'Y') { - fprintf(stderr, "\tnot overwriting\n"); - ok = 0; - } else - unlink(outfile); - } else { - maybe_warnx("%s already exists -- skipping", outfile); - ok = 0; - } - } - return ok; + fprintf(stderr, + "%s already exists -- do you wish to " + "overwrite (y or n)? ", + outfile); + (void)fgets(ans, sizeof(ans) - 1, stdin); + if (ans[0] != 'y' && ans[0] != 'Y') { + fprintf(stderr, "\tnot overwriting\n"); + ok = 0; + } else + unlink(outfile); + } else { + maybe_warnx("%s already exists -- skipping", outfile); + ok = 0; + } + } + return ok; } -static void -unlink_input(const char *file, const struct stat *sb) -{ - struct stat nsb; +static void unlink_input(const char *file, const struct stat *sb) { + struct stat nsb; - if (kflag) - return; - if (stat(file, &nsb) != 0) - /* Must be gone already */ - return; - if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino) - /* Definitely a different file */ - return; - unlink(file); + if (kflag) + return; + if (stat(file, &nsb) != 0) + /* Must be gone already */ + return; + if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino) + /* Definitely a different file */ + return; + unlink(file); } #endif -static const suffixes_t * -check_suffix(char *file, int xlate) -{ - const suffixes_t *s; - int len = strlen(file); - char *sp; +static const suffixes_t *check_suffix(char *file, int xlate) { + const suffixes_t *s; + int len = strlen(file); + char *sp; - for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) { - /* if it doesn't fit in "a.suf", don't bother */ - if (s->ziplen >= len) - continue; - sp = file + len - s->ziplen; - if (strcmp(s->zipped, sp) != 0) - continue; - if (xlate) - strcpy(sp, s->normal); - return s; - } - return NULL; + for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) { + /* if it doesn't fit in "a.suf", don't bother */ + if (s->ziplen >= len) + continue; + sp = file + len - s->ziplen; + if (strcmp(s->zipped, sp) != 0) + continue; + if (xlate) + strcpy(sp, s->normal); + return s; + } + return NULL; } /* * compress the given file: create a corresponding .gz file and remove the * original. */ -static off_t -file_compress(char *file, char *outfile, size_t outsize) -{ - int in; - int out; - off_t size, insize; +static off_t file_compress(char *file, char *outfile, size_t outsize) { + int in; + int out; + off_t size, insize; #ifndef SMALL - struct stat isb, osb; - const suffixes_t *suff; + struct stat isb, osb; + const suffixes_t *suff; #endif - in = open(file, O_RDONLY); - if (in == -1) { - maybe_warn("can't open %s", file); - return -1; - } + in = open(file, O_RDONLY); + if (in == -1) { + maybe_warn("can't open %s", file); + return -1; + } - if (cflag == 0) { + if (cflag == 0) { #ifndef SMALL - if (fstat(in, &isb) == 0) { - if (isb.st_nlink > 1 && fflag == 0) { - maybe_warnx("%s has %d other link%s -- " - "skipping", file, isb.st_nlink - 1, - isb.st_nlink == 1 ? "" : "s"); - close(in); - return -1; - } - } + if (fstat(in, &isb) == 0) { + if (isb.st_nlink > 1 && fflag == 0) { + maybe_warnx("%s has %d other link%s -- " + "skipping", + file, isb.st_nlink - 1, isb.st_nlink == 1 ? "" : "s"); + close(in); + return -1; + } + } - if (fflag == 0 && (suff = check_suffix(file, 0)) - && suff->zipped[0] != 0) { - maybe_warnx("%s already has %s suffix -- unchanged", - file, suff->zipped); - close(in); - return -1; - } + if (fflag == 0 && (suff = check_suffix(file, 0)) && suff->zipped[0] != 0) { + maybe_warnx("%s already has %s suffix -- unchanged", file, suff->zipped); + close(in); + return -1; + } #endif - /* Add (usually) .gz to filename */ - if ((size_t)snprintf(outfile, outsize, "%s%s", - file, suffixes[0].zipped) >= outsize) - memcpy(outfile + outsize - suffixes[0].ziplen - 1, - suffixes[0].zipped, suffixes[0].ziplen + 1); + /* Add (usually) .gz to filename */ + if ((size_t)snprintf(outfile, outsize, "%s%s", file, suffixes[0].zipped) >= + outsize) + memcpy(outfile + outsize - suffixes[0].ziplen - 1, suffixes[0].zipped, + suffixes[0].ziplen + 1); #ifndef SMALL - if (check_outfile(outfile) == 0) { - close(in); - return -1; - } + if (check_outfile(outfile) == 0) { + close(in); + return -1; + } #endif - } + } - if (cflag == 0) { - out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600); - if (out == -1) { - maybe_warn("could not create output: %s", outfile); - fclose(stdin); - return -1; - } - } else - out = STDOUT_FILENO; + if (cflag == 0) { + out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600); + if (out == -1) { + maybe_warn("could not create output: %s", outfile); + fclose(stdin); + return -1; + } + } else + out = STDOUT_FILENO; - insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime); + insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime); - (void)close(in); + (void)close(in); - /* - * If there was an error, insize will be -1. - * If we compressed to stdout, just return the size. - * Otherwise stat the file and check it is the correct size. - * We only blow away the file if we can stat the output and it - * has the expected size. - */ - if (cflag != 0) - return insize == -1 ? -1 : size; + /* + * If there was an error, insize will be -1. + * If we compressed to stdout, just return the size. + * Otherwise stat the file and check it is the correct size. + * We only blow away the file if we can stat the output and it + * has the expected size. + */ + if (cflag != 0) + return insize == -1 ? -1 : size; #ifndef SMALL - if (fstat(out, &osb) != 0) { - maybe_warn("couldn't stat: %s", outfile); - goto bad_outfile; - } + if (fstat(out, &osb) != 0) { + maybe_warn("couldn't stat: %s", outfile); + goto bad_outfile; + } - if (osb.st_size != size) { - maybe_warnx("output file: %s wrong size (%" PRIdOFF - " != %" PRIdOFF "), deleting", - outfile, osb.st_size, size); - goto bad_outfile; - } + if (osb.st_size != size) { + maybe_warnx("output file: %s wrong size (%" PRIdOFF " != %" PRIdOFF + "), deleting", + outfile, osb.st_size, size); + goto bad_outfile; + } - copymodes(out, &isb, outfile); + copymodes(out, &isb, outfile); #endif - if (close(out) == -1) - maybe_warn("couldn't close output"); + if (close(out) == -1) + maybe_warn("couldn't close output"); - /* output is good, ok to delete input */ - unlink_input(file, &isb); - return size; + /* output is good, ok to delete input */ + unlink_input(file, &isb); + return size; #ifndef SMALL - bad_outfile: - if (close(out) == -1) - maybe_warn("couldn't close output"); +bad_outfile: + if (close(out) == -1) + maybe_warn("couldn't close output"); - maybe_warnx("leaving original %s", file); - unlink(outfile); - return size; + maybe_warnx("leaving original %s", file); + unlink(outfile); + return size; #endif } /* uncompress the given file and remove the original */ -static off_t -file_uncompress(char *file, char *outfile, size_t outsize) -{ - struct stat isb, osb; - off_t size; - ssize_t rbytes; - unsigned char header1[4]; - enum filetype method; - int fd, ofd, zfd = -1; +static off_t file_uncompress(char *file, char *outfile, size_t outsize) { + struct stat isb, osb; + off_t size; + ssize_t rbytes; + unsigned char header1[4]; + enum filetype method; + int fd, ofd, zfd = -1; #ifndef SMALL - ssize_t rv; - time_t timestamp = 0; - char name[PATH_MAX + 1]; + ssize_t rv; + time_t timestamp = 0; + char name[PATH_MAX + 1]; #endif - /* gather the old name info */ + /* gather the old name info */ - fd = open(file, O_RDONLY); - if (fd < 0) { - maybe_warn("can't open %s", file); - goto lose; - } + fd = open(file, O_RDONLY); + if (fd < 0) { + maybe_warn("can't open %s", file); + goto lose; + } - strlcpy(outfile, file, outsize); - if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) { - maybe_warnx("%s: unknown suffix -- ignored", file); - goto lose; - } + strlcpy(outfile, file, outsize); + if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) { + maybe_warnx("%s: unknown suffix -- ignored", file); + goto lose; + } - rbytes = read(fd, header1, sizeof header1); - if (rbytes != sizeof header1) { - /* we don't want to fail here. */ + rbytes = read(fd, header1, sizeof header1); + if (rbytes != sizeof header1) { + /* we don't want to fail here. */ #ifndef SMALL - if (fflag) - goto lose; + if (fflag) + goto lose; #endif - if (rbytes == -1) - maybe_warn("can't read %s", file); - else - goto unexpected_EOF; - goto lose; - } + if (rbytes == -1) + maybe_warn("can't read %s", file); + else + goto unexpected_EOF; + goto lose; + } - method = file_gettype(header1); + method = file_gettype(header1); #ifndef SMALL - if (fflag == 0 && method == FT_UNKNOWN) { - maybe_warnx("%s: not in gzip format", file); - goto lose; - } + if (fflag == 0 && method == FT_UNKNOWN) { + maybe_warnx("%s: not in gzip format", file); + goto lose; + } #endif #ifndef SMALL - if (method == FT_GZIP && Nflag) { - unsigned char ts[4]; /* timestamp */ + if (method == FT_GZIP && Nflag) { + unsigned char ts[4]; /* timestamp */ - rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP); - if (rv >= 0 && rv < (ssize_t)(sizeof ts)) - goto unexpected_EOF; - if (rv == -1) { - if (!fflag) - maybe_warn("can't read %s", file); - goto lose; - } - timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0]; + rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP); + if (rv >= 0 && rv < (ssize_t)(sizeof ts)) + goto unexpected_EOF; + if (rv == -1) { + if (!fflag) + maybe_warn("can't read %s", file); + goto lose; + } + timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0]; - if (header1[3] & ORIG_NAME) { - rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME); - if (rbytes < 0) { - maybe_warn("can't read %s", file); - goto lose; - } - if (name[0] != '\0') { - char *dp, *nf; + if (header1[3] & ORIG_NAME) { + rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME); + if (rbytes < 0) { + maybe_warn("can't read %s", file); + goto lose; + } + if (name[0] != '\0') { + char *dp, *nf; - /* Make sure that name is NUL-terminated */ - name[rbytes] = '\0'; + /* Make sure that name is NUL-terminated */ + name[rbytes] = '\0'; - /* strip saved directory name */ - nf = strrchr(name, '/'); - if (nf == NULL) - nf = name; - else - nf++; + /* strip saved directory name */ + nf = strrchr(name, '/'); + if (nf == NULL) + nf = name; + else + nf++; - /* preserve original directory name */ - dp = strrchr(file, '/'); - if (dp == NULL) - dp = file; - else - dp++; - snprintf(outfile, outsize, "%.*s%.*s", - (int) (dp - file), - file, (int) rbytes, nf); - } - } - } + /* preserve original directory name */ + dp = strrchr(file, '/'); + if (dp == NULL) + dp = file; + else + dp++; + snprintf(outfile, outsize, "%.*s%.*s", (int)(dp - file), file, + (int)rbytes, nf); + } + } + } #endif - lseek(fd, 0, SEEK_SET); + lseek(fd, 0, SEEK_SET); - if (cflag == 0 || lflag) { - if (fstat(fd, &isb) != 0) - goto lose; + if (cflag == 0 || lflag) { + if (fstat(fd, &isb) != 0) + goto lose; #ifndef SMALL - if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) { - maybe_warnx("%s has %d other links -- skipping", - file, isb.st_nlink - 1); - goto lose; - } - if (nflag == 0 && timestamp) - isb.st_mtime = timestamp; - if (check_outfile(outfile) == 0) - goto lose; + if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) { + maybe_warnx("%s has %d other links -- skipping", file, isb.st_nlink - 1); + goto lose; + } + if (nflag == 0 && timestamp) + isb.st_mtime = timestamp; + if (check_outfile(outfile) == 0) + goto lose; #endif - } + } - if (cflag == 0 && lflag == 0) { - zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600); - if (zfd == STDOUT_FILENO) { - /* We won't close STDOUT_FILENO later... */ - zfd = dup(zfd); - close(STDOUT_FILENO); - } - if (zfd == -1) { - maybe_warn("can't open %s", outfile); - goto lose; - } - } else - zfd = STDOUT_FILENO; + if (cflag == 0 && lflag == 0) { + zfd = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600); + if (zfd == STDOUT_FILENO) { + /* We won't close STDOUT_FILENO later... */ + zfd = dup(zfd); + close(STDOUT_FILENO); + } + if (zfd == -1) { + maybe_warn("can't open %s", outfile); + goto lose; + } + } else + zfd = STDOUT_FILENO; - switch (method) { + switch (method) { #ifndef NO_BZIP2_SUPPORT - case FT_BZIP2: - /* XXX */ - if (lflag) { - maybe_warnx("no -l with bzip2 files"); - goto lose; - } + case FT_BZIP2: + /* XXX */ + if (lflag) { + maybe_warnx("no -l with bzip2 files"); + goto lose; + } - size = unbzip2(fd, zfd, NULL, 0, NULL); - break; + size = unbzip2(fd, zfd, NULL, 0, NULL); + break; #endif #ifndef NO_COMPRESS_SUPPORT - case FT_Z: { - FILE *in, *out; + case FT_Z: { + FILE *in, *out; - /* XXX */ - if (lflag) { - maybe_warnx("no -l with Lempel-Ziv files"); - goto lose; - } + /* XXX */ + if (lflag) { + maybe_warnx("no -l with Lempel-Ziv files"); + goto lose; + } - if ((in = zdopen(fd)) == NULL) { - maybe_warn("zdopen for read: %s", file); - goto lose; - } + if ((in = zdopen(fd)) == NULL) { + maybe_warn("zdopen for read: %s", file); + goto lose; + } - out = fdopen(dup(zfd), "w"); - if (out == NULL) { - maybe_warn("fdopen for write: %s", outfile); - fclose(in); - goto lose; - } + out = fdopen(dup(zfd), "w"); + if (out == NULL) { + maybe_warn("fdopen for write: %s", outfile); + fclose(in); + goto lose; + } - size = zuncompress(in, out, NULL, 0, NULL); - /* need to fclose() if ferror() is true... */ - if (ferror(in) | fclose(in)) { - maybe_warn("failed infile fclose"); - unlink(outfile); - (void)fclose(out); - } - if (fclose(out) != 0) { - maybe_warn("failed outfile fclose"); - unlink(outfile); - goto lose; - } - break; - } + size = zuncompress(in, out, NULL, 0, NULL); + /* need to fclose() if ferror() is true... */ + if (ferror(in) | fclose(in)) { + maybe_warn("failed infile fclose"); + unlink(outfile); + (void)fclose(out); + } + if (fclose(out) != 0) { + maybe_warn("failed outfile fclose"); + unlink(outfile); + goto lose; + } + break; + } #endif #ifndef NO_PACK_SUPPORT - case FT_PACK: - if (lflag) { - maybe_warnx("no -l with packed files"); - goto lose; - } + case FT_PACK: + if (lflag) { + maybe_warnx("no -l with packed files"); + goto lose; + } - size = unpack(fd, zfd, NULL, 0, NULL); - break; + size = unpack(fd, zfd, NULL, 0, NULL); + break; #endif #ifndef NO_XZ_SUPPORT - case FT_XZ: - if (lflag) { - maybe_warnx("no -l with xz files"); - goto lose; - } + case FT_XZ: + if (lflag) { + maybe_warnx("no -l with xz files"); + goto lose; + } - size = unxz(fd, zfd, NULL, 0, NULL); - break; + size = unxz(fd, zfd, NULL, 0, NULL); + break; #endif #ifndef SMALL - case FT_UNKNOWN: - if (lflag) { - maybe_warnx("no -l for unknown filetypes"); - goto lose; - } - size = cat_fd(NULL, 0, NULL, fd); - break; + case FT_UNKNOWN: + if (lflag) { + maybe_warnx("no -l for unknown filetypes"); + goto lose; + } + size = cat_fd(NULL, 0, NULL, fd); + break; #endif - default: - if (lflag) { - print_list(fd, isb.st_size, outfile, isb.st_mtime); - close(fd); - return -1; /* XXX */ - } + default: + if (lflag) { + print_list(fd, isb.st_size, outfile, isb.st_mtime); + close(fd); + return -1; /* XXX */ + } - size = gz_uncompress(fd, zfd, NULL, 0, NULL, file); - break; - } + size = gz_uncompress(fd, zfd, NULL, 0, NULL, file); + break; + } - if (close(fd) != 0) - maybe_warn("couldn't close input"); - if (zfd != STDOUT_FILENO && close(zfd) != 0) - maybe_warn("couldn't close output"); + if (close(fd) != 0) + maybe_warn("couldn't close input"); + if (zfd != STDOUT_FILENO && close(zfd) != 0) + maybe_warn("couldn't close output"); - if (size == -1) { - if (cflag == 0) - unlink(outfile); - maybe_warnx("%s: uncompress failed", file); - return -1; - } + if (size == -1) { + if (cflag == 0) + unlink(outfile); + maybe_warnx("%s: uncompress failed", file); + return -1; + } - /* if testing, or we uncompressed to stdout, this is all we need */ + /* if testing, or we uncompressed to stdout, this is all we need */ #ifndef SMALL - if (tflag) - return size; + if (tflag) + return size; #endif - /* if we are uncompressing to stdin, don't remove the file. */ - if (cflag) - return size; + /* if we are uncompressing to stdin, don't remove the file. */ + if (cflag) + return size; - /* - * if we create a file... - */ - /* - * if we can't stat the file don't remove the file. - */ + /* + * if we create a file... + */ + /* + * if we can't stat the file don't remove the file. + */ - ofd = open(outfile, O_RDWR, 0); - if (ofd == -1) { - maybe_warn("couldn't open (leaving original): %s", - outfile); - return -1; - } - if (fstat(ofd, &osb) != 0) { - maybe_warn("couldn't stat (leaving original): %s", - outfile); - close(ofd); - return -1; - } - if (osb.st_size != size) { - maybe_warnx("stat gave different size: %" PRIdOFF - " != %" PRIdOFF " (leaving original)", - size, osb.st_size); - close(ofd); - unlink(outfile); - return -1; - } - unlink_input(file, &isb); + ofd = open(outfile, O_RDWR, 0); + if (ofd == -1) { + maybe_warn("couldn't open (leaving original): %s", outfile); + return -1; + } + if (fstat(ofd, &osb) != 0) { + maybe_warn("couldn't stat (leaving original): %s", outfile); + close(ofd); + return -1; + } + if (osb.st_size != size) { + maybe_warnx("stat gave different size: %" PRIdOFF " != %" PRIdOFF + " (leaving original)", + size, osb.st_size); + close(ofd); + unlink(outfile); + return -1; + } + unlink_input(file, &isb); #ifndef SMALL - copymodes(ofd, &isb, outfile); + copymodes(ofd, &isb, outfile); #endif - close(ofd); - return size; + close(ofd); + return size; - unexpected_EOF: - maybe_warnx("%s: unexpected end of file", file); - lose: - if (fd != -1) - close(fd); - if (zfd != -1 && zfd != STDOUT_FILENO) - close(fd); - return -1; +unexpected_EOF: + maybe_warnx("%s: unexpected end of file", file); +lose: + if (fd != -1) + close(fd); + if (zfd != -1 && zfd != STDOUT_FILENO) + close(fd); + return -1; } #ifndef SMALL -static off_t -cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd) -{ - char buf[BUFLEN]; - off_t in_tot; - ssize_t w; +static off_t cat_fd(unsigned char *prepend, size_t count, off_t *gsizep, + int fd) { + char buf[BUFLEN]; + off_t in_tot; + ssize_t w; - in_tot = count; - w = write(STDOUT_FILENO, prepend, count); - if (w == -1 || (size_t)w != count) { - maybe_warn("write to stdout"); - return -1; - } - for (;;) { - ssize_t rv; + in_tot = count; + w = write(STDOUT_FILENO, prepend, count); + if (w == -1 || (size_t)w != count) { + maybe_warn("write to stdout"); + return -1; + } + for (;;) { + ssize_t rv; - rv = read(fd, buf, sizeof buf); - if (rv == 0) - break; - if (rv < 0) { - maybe_warn("read from fd %d", fd); - break; - } + rv = read(fd, buf, sizeof buf); + if (rv == 0) + break; + if (rv < 0) { + maybe_warn("read from fd %d", fd); + break; + } - if (write(STDOUT_FILENO, buf, rv) != rv) { - maybe_warn("write to stdout"); - break; - } - in_tot += rv; - } + if (write(STDOUT_FILENO, buf, rv) != rv) { + maybe_warn("write to stdout"); + break; + } + in_tot += rv; + } - if (gsizep) - *gsizep = in_tot; - return (in_tot); + if (gsizep) + *gsizep = in_tot; + return (in_tot); } #endif -static void -handle_stdin(void) -{ - unsigned char header1[4]; - off_t usize, gsize; - enum filetype method; - ssize_t bytes_read; +static void handle_stdin(void) { + unsigned char header1[4]; + off_t usize, gsize; + enum filetype method; + ssize_t bytes_read; #ifndef NO_COMPRESS_SUPPORT - FILE *in; + FILE *in; #endif #ifndef SMALL - if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) { - maybe_warnx("standard input is a terminal -- ignoring"); - return; - } + if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) { + maybe_warnx("standard input is a terminal -- ignoring"); + return; + } #endif - if (lflag) { - struct stat isb; + if (lflag) { + struct stat isb; - /* XXX could read the whole file, etc. */ - if (fstat(STDIN_FILENO, &isb) < 0) { - maybe_warn("fstat"); - return; - } - print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime); - return; - } + /* XXX could read the whole file, etc. */ + if (fstat(STDIN_FILENO, &isb) < 0) { + maybe_warn("fstat"); + return; + } + print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime); + return; + } - bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1); - if (bytes_read == -1) { - maybe_warn("can't read stdin"); - return; - } else if (bytes_read != sizeof(header1)) { - maybe_warnx("(stdin): unexpected end of file"); - return; - } + bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1); + if (bytes_read == -1) { + maybe_warn("can't read stdin"); + return; + } else if (bytes_read != sizeof(header1)) { + maybe_warnx("(stdin): unexpected end of file"); + return; + } - method = file_gettype(header1); - switch (method) { - default: + method = file_gettype(header1); + switch (method) { + default: #ifndef SMALL - if (fflag == 0) { - maybe_warnx("unknown compression format"); - return; - } - usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO); - break; + if (fflag == 0) { + maybe_warnx("unknown compression format"); + return; + } + usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO); + break; #endif - case FT_GZIP: - usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO, - (char *)header1, sizeof header1, &gsize, "(stdin)"); - break; + case FT_GZIP: + usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO, (char *)header1, + sizeof header1, &gsize, "(stdin)"); + break; #ifndef NO_BZIP2_SUPPORT - case FT_BZIP2: - usize = unbzip2(STDIN_FILENO, STDOUT_FILENO, - (char *)header1, sizeof header1, &gsize); - break; + case FT_BZIP2: + usize = unbzip2(STDIN_FILENO, STDOUT_FILENO, (char *)header1, + sizeof header1, &gsize); + break; #endif #ifndef NO_COMPRESS_SUPPORT - case FT_Z: - if ((in = zdopen(STDIN_FILENO)) == NULL) { - maybe_warnx("zopen of stdin"); - return; - } + case FT_Z: + if ((in = zdopen(STDIN_FILENO)) == NULL) { + maybe_warnx("zopen of stdin"); + return; + } - usize = zuncompress(in, stdout, (char *)header1, - sizeof header1, &gsize); - fclose(in); - break; + usize = zuncompress(in, stdout, (char *)header1, sizeof header1, &gsize); + fclose(in); + break; #endif #ifndef NO_PACK_SUPPORT - case FT_PACK: - usize = unpack(STDIN_FILENO, STDOUT_FILENO, - (char *)header1, sizeof header1, &gsize); - break; + case FT_PACK: + usize = unpack(STDIN_FILENO, STDOUT_FILENO, (char *)header1, sizeof header1, + &gsize); + break; #endif #ifndef NO_XZ_SUPPORT - case FT_XZ: - usize = unxz(STDIN_FILENO, STDOUT_FILENO, - (char *)header1, sizeof header1, &gsize); - break; + case FT_XZ: + usize = unxz(STDIN_FILENO, STDOUT_FILENO, (char *)header1, sizeof header1, + &gsize); + break; #endif - } + } #ifndef SMALL - if (vflag && !tflag && usize != -1 && gsize != -1) - print_verbage(NULL, NULL, usize, gsize); - if (vflag && tflag) - print_test("(stdin)", usize != -1); -#endif - + if (vflag && !tflag && usize != -1 && gsize != -1) + print_verbage(NULL, NULL, usize, gsize); + if (vflag && tflag) + print_test("(stdin)", usize != -1); +#endif } -static void -handle_stdout(void) -{ - off_t gsize, usize; - struct stat sb; - time_t systime; - uint32_t mtime; - int ret; +static void handle_stdout(void) { + off_t gsize, usize; + struct stat sb; + time_t systime; + uint32_t mtime; + int ret; #ifndef SMALL - if (fflag == 0 && isatty(STDOUT_FILENO)) { - maybe_warnx("standard output is a terminal -- ignoring"); - return; - } + if (fflag == 0 && isatty(STDOUT_FILENO)) { + maybe_warnx("standard output is a terminal -- ignoring"); + return; + } #endif - /* If stdin is a file use its mtime, otherwise use current time */ - ret = fstat(STDIN_FILENO, &sb); + /* If stdin is a file use its mtime, otherwise use current time */ + ret = fstat(STDIN_FILENO, &sb); #ifndef SMALL - if (ret < 0) { - maybe_warn("Can't stat stdin"); - return; - } + if (ret < 0) { + maybe_warn("Can't stat stdin"); + return; + } #endif - if (S_ISREG(sb.st_mode)) - mtime = (uint32_t)sb.st_mtime; - else { - systime = time(NULL); + if (S_ISREG(sb.st_mode)) + mtime = (uint32_t)sb.st_mtime; + else { + systime = time(NULL); #ifndef SMALL - if (systime == -1) { - maybe_warn("time"); - return; - } + if (systime == -1) { + maybe_warn("time"); + return; + } #endif - mtime = (uint32_t)systime; - } - - usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime); + mtime = (uint32_t)systime; + } + + usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime); #ifndef SMALL - if (vflag && !tflag && usize != -1 && gsize != -1) - print_verbage(NULL, NULL, usize, gsize); -#endif + if (vflag && !tflag && usize != -1 && gsize != -1) + print_verbage(NULL, NULL, usize, gsize); +#endif } /* do what is asked for, for the path name */ -static void -handle_pathname(char *path) -{ - char *opath = path, *s = NULL; - ssize_t len; - int slen; - struct stat sb; +static void handle_pathname(char *path) { + char *opath = path, *s = NULL; + ssize_t len; + int slen; + struct stat sb; - /* check for stdout/stdin */ - if (path[0] == '-' && path[1] == '\0') { - if (dflag) - handle_stdin(); - else - handle_stdout(); - return; - } + /* check for stdout/stdin */ + if (path[0] == '-' && path[1] == '\0') { + if (dflag) + handle_stdin(); + else + handle_stdout(); + return; + } retry: - if (stat(path, &sb) != 0) { - /* lets try .gz if we're decompressing */ - if (dflag && s == NULL && errno == ENOENT) { - len = strlen(path); - slen = suffixes[0].ziplen; - s = malloc(len + slen + 1); - if (s == NULL) - maybe_err("malloc"); - memcpy(s, path, len); - memcpy(s + len, suffixes[0].zipped, slen + 1); - path = s; - goto retry; - } - maybe_warn("can't stat: %s", opath); - goto out; - } + if (stat(path, &sb) != 0) { + /* lets try .gz if we're decompressing */ + if (dflag && s == NULL && errno == ENOENT) { + len = strlen(path); + slen = suffixes[0].ziplen; + s = malloc(len + slen + 1); + if (s == NULL) + maybe_err("malloc"); + memcpy(s, path, len); + memcpy(s + len, suffixes[0].zipped, slen + 1); + path = s; + goto retry; + } + maybe_warn("can't stat: %s", opath); + goto out; + } - if (S_ISDIR(sb.st_mode)) { + if (S_ISDIR(sb.st_mode)) { #ifndef SMALL - if (rflag) - handle_dir(path); - else + if (rflag) + handle_dir(path); + else #endif - maybe_warnx("%s is a directory", path); - goto out; - } + maybe_warnx("%s is a directory", path); + goto out; + } - if (S_ISREG(sb.st_mode)) - handle_file(path, &sb); - else - maybe_warnx("%s is not a regular file", path); + if (S_ISREG(sb.st_mode)) + handle_file(path, &sb); + else + maybe_warnx("%s is not a regular file", path); out: - if (s) - free(s); + if (s) + free(s); } /* compress/decompress a file */ -static void -handle_file(char *file, struct stat *sbp) -{ - off_t usize, gsize; - char outfile[PATH_MAX]; +static void handle_file(char *file, struct stat *sbp) { + off_t usize, gsize; + char outfile[PATH_MAX]; - infile = file; - if (dflag) { - usize = file_uncompress(file, outfile, sizeof(outfile)); + infile = file; + if (dflag) { + usize = file_uncompress(file, outfile, sizeof(outfile)); #ifndef SMALL - if (vflag && tflag) - print_test(file, usize != -1); + if (vflag && tflag) + print_test(file, usize != -1); #endif - if (usize == -1) - return; - gsize = sbp->st_size; - } else { - gsize = file_compress(file, outfile, sizeof(outfile)); - if (gsize == -1) - return; - usize = sbp->st_size; - } - + if (usize == -1) + return; + gsize = sbp->st_size; + } else { + gsize = file_compress(file, outfile, sizeof(outfile)); + if (gsize == -1) + return; + usize = sbp->st_size; + } #ifndef SMALL - if (vflag && !tflag) - print_verbage(file, (cflag) ? NULL : outfile, usize, gsize); + if (vflag && !tflag) + print_verbage(file, (cflag) ? NULL : outfile, usize, gsize); #endif } #ifndef SMALL /* this is used with -r to recursively descend directories */ -static void -handle_dir(char *dir) -{ - char *path_argv[2]; - FTS *fts; - FTSENT *entry; +static void handle_dir(char *dir) { + char *path_argv[2]; + FTS *fts; + FTSENT *entry; - path_argv[0] = dir; - path_argv[1] = 0; - fts = fts_open(path_argv, FTS_PHYSICAL, NULL); - if (fts == NULL) { - warn("couldn't fts_open %s", dir); - return; - } + path_argv[0] = dir; + path_argv[1] = 0; + fts = fts_open(path_argv, FTS_PHYSICAL, NULL); + if (fts == NULL) { + warn("couldn't fts_open %s", dir); + return; + } - while ((entry = fts_read(fts))) { - switch(entry->fts_info) { - case FTS_D: - case FTS_DP: - continue; + while ((entry = fts_read(fts))) { + switch (entry->fts_info) { + case FTS_D: + case FTS_DP: + continue; - case FTS_DNR: - case FTS_ERR: - case FTS_NS: - maybe_warn("%s", entry->fts_path); - continue; - case FTS_F: - handle_file(entry->fts_name, entry->fts_statp); - } - } - (void)fts_close(fts); + case FTS_DNR: + case FTS_ERR: + case FTS_NS: + maybe_warn("%s", entry->fts_path); + continue; + case FTS_F: + handle_file(entry->fts_name, entry->fts_statp); + } + } + (void)fts_close(fts); } #endif /* print a ratio - size reduction as a fraction of uncompressed size */ -static void -print_ratio(off_t in, off_t out, FILE *where) -{ - int percent10; /* 10 * percent */ - off_t diff; - char buff[8]; - int len; +static void print_ratio(off_t in, off_t out, FILE *where) { + int percent10; /* 10 * percent */ + off_t diff; + char buff[8]; + int len; - diff = in - out/2; - if (diff <= 0) - /* - * Output is more than double size of input! print -99.9% - * Quite possibly we've failed to get the original size. - */ - percent10 = -999; - else { - /* - * We only need 12 bits of result from the final division, - * so reduce the values until a 32bit division will suffice. - */ - while (in > 0x100000) { - diff >>= 1; - in >>= 1; - } - if (in != 0) - percent10 = ((u_int)diff * 2000) / (u_int)in - 1000; - else - percent10 = 0; - } + diff = in - out / 2; + if (diff <= 0) + /* + * Output is more than double size of input! print -99.9% + * Quite possibly we've failed to get the original size. + */ + percent10 = -999; + else { + /* + * We only need 12 bits of result from the final division, + * so reduce the values until a 32bit division will suffice. + */ + while (in > 0x100000) { + diff >>= 1; + in >>= 1; + } + if (in != 0) + percent10 = ((u_int)diff * 2000) / (u_int)in - 1000; + else + percent10 = 0; + } - len = snprintf(buff, sizeof buff, "%2.2d.", percent10); - /* Move the '.' to before the last digit */ - buff[len - 1] = buff[len - 2]; - buff[len - 2] = '.'; - fprintf(where, "%5s%%", buff); + len = snprintf(buff, sizeof buff, "%2.2d.", percent10); + /* Move the '.' to before the last digit */ + buff[len - 1] = buff[len - 2]; + buff[len - 2] = '.'; + fprintf(where, "%5s%%", buff); } #ifndef SMALL /* print compression statistics, and the new name (if there is one!) */ -static void -print_verbage(const char *file, const char *nfile, off_t usize, off_t gsize) -{ - if (file) - fprintf(stderr, "%s:%s ", file, - strlen(file) < 7 ? "\t\t" : "\t"); - print_ratio(usize, gsize, stderr); - if (nfile) - fprintf(stderr, " -- replaced with %s", nfile); - fprintf(stderr, "\n"); - fflush(stderr); +static void print_verbage(const char *file, const char *nfile, off_t usize, + off_t gsize) { + if (file) + fprintf(stderr, "%s:%s ", file, strlen(file) < 7 ? "\t\t" : "\t"); + print_ratio(usize, gsize, stderr); + if (nfile) + fprintf(stderr, " -- replaced with %s", nfile); + fprintf(stderr, "\n"); + fflush(stderr); } /* print test results */ -static void -print_test(const char *file, int ok) -{ +static void print_test(const char *file, int ok) { - if (exit_value == 0 && ok == 0) - exit_value = 1; - fprintf(stderr, "%s:%s %s\n", file, - strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK"); - fflush(stderr); + if (exit_value == 0 && ok == 0) + exit_value = 1; + fprintf(stderr, "%s:%s %s\n", file, strlen(file) < 7 ? "\t\t" : "\t", + ok ? "OK" : "NOT OK"); + fflush(stderr); } #endif @@ -1961,121 +1898,114 @@ print_test(const char *file, int ok) compressed uncompressed ratio uncompressed_name 354841 1679360 78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar */ -static void -print_list(int fd, off_t out, const char *outfile, time_t ts) -{ - static int first = 1; +static void print_list(int fd, off_t out, const char *outfile, time_t ts) { + static int first = 1; #ifndef SMALL - static off_t in_tot, out_tot; - uint32_t crc = 0; + static off_t in_tot, out_tot; + uint32_t crc = 0; #endif - off_t in = 0, rv; + off_t in = 0, rv; - if (first) { + if (first) { #ifndef SMALL - if (vflag) - printf("method crc date time "); + if (vflag) + printf("method crc date time "); #endif - if (qflag == 0) - printf(" compressed uncompressed " - "ratio uncompressed_name\n"); - } - first = 0; + if (qflag == 0) + printf(" compressed uncompressed " + "ratio uncompressed_name\n"); + } + first = 0; - /* print totals? */ + /* print totals? */ #ifndef SMALL - if (fd == -1) { - in = in_tot; - out = out_tot; - } else + if (fd == -1) { + in = in_tot; + out = out_tot; + } else #endif - { - /* read the last 4 bytes - this is the uncompressed size */ - rv = lseek(fd, (off_t)(-8), SEEK_END); - if (rv != -1) { - unsigned char buf[8]; - uint32_t usize; + { + /* read the last 4 bytes - this is the uncompressed size */ + rv = lseek(fd, (off_t)(-8), SEEK_END); + if (rv != -1) { + unsigned char buf[8]; + uint32_t usize; - rv = read(fd, (char *)buf, sizeof(buf)); - if (rv == -1) - maybe_warn("read of uncompressed size"); - else if (rv != sizeof(buf)) - maybe_warnx("read of uncompressed size"); + rv = read(fd, (char *)buf, sizeof(buf)); + if (rv == -1) + maybe_warn("read of uncompressed size"); + else if (rv != sizeof(buf)) + maybe_warnx("read of uncompressed size"); - else { - usize = buf[4] | buf[5] << 8 | - buf[6] << 16 | buf[7] << 24; - in = (off_t)usize; + else { + usize = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24; + in = (off_t)usize; #ifndef SMALL - crc = buf[0] | buf[1] << 8 | - buf[2] << 16 | buf[3] << 24; + crc = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24; #endif - } - } - } + } + } + } #ifndef SMALL - if (vflag && fd == -1) - printf(" "); - else if (vflag) { - char *date = ctime(&ts); + if (vflag && fd == -1) + printf(" "); + else if (vflag) { + char *date = ctime(&ts); - /* skip the day, 1/100th second, and year */ - date += 4; - date[12] = 0; - printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date); - } - in_tot += in; - out_tot += out; + /* skip the day, 1/100th second, and year */ + date += 4; + date[12] = 0; + printf("%5s %08x %11s ", "defla" /*XXX*/, crc, date); + } + in_tot += in; + out_tot += out; #endif - printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in); - print_ratio(in, out, stdout); - printf(" %s\n", outfile); + printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in); + print_ratio(in, out, stdout); + printf(" %s\n", outfile); } -/* display the usage of NetBSD gzip */ -static void -usage(void) -{ +/* display the usage of gzip */ +static void usage(void) { - fprintf(stderr, "%s\n", gzip_version); - fprintf(stderr, - "usage: %s [-" OPT_LIST "] [ [ ...]]\n" + fprintf(stderr, "%s\n", gzip_version); + fprintf(stderr, + "usage: %s [-" OPT_LIST "] [ [ ...]]\n" #ifndef SMALL - " -1 --fast fastest (worst) compression\n" - " -2 .. -8 set compression level\n" - " -9 --best best (slowest) compression\n" - " -c --stdout write to stdout, keep original files\n" - " --to-stdout\n" - " -d --decompress uncompress files\n" - " --uncompress\n" - " -f --force force overwriting & compress links\n" - " -h --help display this help\n" - " -k --keep don't delete input files during operation\n" - " -l --list list compressed file contents\n" - " -N --name save or restore original file name and time stamp\n" - " -n --no-name don't save original file name or time stamp\n" - " -q --quiet output no warnings\n" - " -r --recursive recursively compress files in directories\n" - " -S .suf use suffix .suf instead of .gz\n" - " --suffix .suf\n" - " -t --test test compressed file\n" - " -V --version display program version\n" - " -v --verbose print extra statistics\n", + " -1 --fast fastest (worst) compression\n" + " -2 .. -8 set compression level\n" + " -9 --best best (slowest) compression\n" + " -c --stdout write to stdout, keep original files\n" + " --to-stdout\n" + " -d --decompress uncompress files\n" + " --uncompress\n" + " -f --force force overwriting & compress links\n" + " -h --help display this help\n" + " -k --keep don't delete input files during operation\n" + " -l --list list compressed file contents\n" + " -N --name save or restore original file name and time " + "stamp\n" + " -n --no-name don't save original file name or time stamp\n" + " -q --quiet output no warnings\n" + " -r --recursive recursively compress files in directories\n" + " -S .suf use suffix .suf instead of .gz\n" + " --suffix .suf\n" + " -t --test test compressed file\n" + " -V --version display program version\n" + " -v --verbose print extra statistics\n", #else - , + , #endif - getprogname()); - exit(0); + getprogname()); + exit(0); } -/* display the version of NetBSD gzip */ -static void -display_version(void) -{ +/* display the version of gzip */ +static void display_version(void) { - fprintf(stderr, "%s\n", gzip_version); - exit(0); + fprintf(stderr, "%s\n", gzip_version); + exit(0); } #ifndef NO_BZIP2_SUPPORT @@ -2091,24 +2021,22 @@ display_version(void) #include "unxz.c" #endif -static ssize_t -read_retry(int fd, void *buf, size_t sz) -{ - char *cp = buf; - size_t left = MIN(sz, (size_t) SSIZE_MAX); +static ssize_t read_retry(int fd, void *buf, size_t sz) { + char *cp = buf; + size_t left = MIN(sz, (size_t)SSIZE_MAX); - while (left > 0) { - ssize_t ret; + while (left > 0) { + ssize_t ret; - ret = read(fd, cp, left); - if (ret == -1) { - return ret; - } else if (ret == 0) { - break; /* EOF */ - } - cp += ret; - left -= ret; - } + ret = read(fd, cp, left); + if (ret == -1) { + return ret; + } else if (ret == 0) { + break; /* EOF */ + } + cp += ret; + left -= ret; + } - return sz - left; + return sz - left; } diff --git a/usr.bin/gzip/unbzip2.c b/usr.bin/gzip/unbzip2.c index 1b87c3de0..6298d04ab 100644 --- a/usr.bin/gzip/unbzip2.c +++ b/usr.bin/gzip/unbzip2.c @@ -1,10 +1,9 @@ -/* $NetBSD: unbzip2.c,v 1.13 2009/12/05 03:23:37 mrg Exp $ */ /*- - * Copyright (c) 2006 The NetBSD Foundation, Inc. + * Copyright (c) 2006 The Foundation, Inc. * All rights reserved. * - * This code is derived from software contributed to The NetBSD Foundation + * This code is derived from software contributed to The Foundation * by Simon Burge. * * Redistribution and use in source and binary forms, with or without @@ -16,7 +15,7 @@ * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * THIS SOFTWARE IS PROVIDED BY THE FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS @@ -31,108 +30,107 @@ /* This file is #included by gzip.c */ -static off_t -unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in) -{ - int ret, end_of_file, cold = 0; - off_t bytes_out = 0; - bz_stream bzs; - static char *inbuf, *outbuf; +static off_t unbzip2(int in, int out, char *pre, size_t prelen, + off_t *bytes_in) { + int ret, end_of_file, cold = 0; + off_t bytes_out = 0; + bz_stream bzs; + static char *inbuf, *outbuf; - if (inbuf == NULL) - inbuf = malloc(BUFLEN); - if (outbuf == NULL) - outbuf = malloc(BUFLEN); - if (inbuf == NULL || outbuf == NULL) - maybe_err("malloc"); + if (inbuf == NULL) + inbuf = malloc(BUFLEN); + if (outbuf == NULL) + outbuf = malloc(BUFLEN); + if (inbuf == NULL || outbuf == NULL) + maybe_err("malloc"); - bzs.bzalloc = NULL; - bzs.bzfree = NULL; - bzs.opaque = NULL; + bzs.bzalloc = NULL; + bzs.bzfree = NULL; + bzs.opaque = NULL; - end_of_file = 0; - ret = BZ2_bzDecompressInit(&bzs, 0, 0); - if (ret != BZ_OK) - maybe_errx("bzip2 init"); + end_of_file = 0; + ret = BZ2_bzDecompressInit(&bzs, 0, 0); + if (ret != BZ_OK) + maybe_errx("bzip2 init"); - /* Prepend. */ - bzs.avail_in = prelen; - bzs.next_in = pre; + /* Prepend. */ + bzs.avail_in = prelen; + bzs.next_in = pre; - if (bytes_in) - *bytes_in = prelen; + if (bytes_in) + *bytes_in = prelen; - while (ret == BZ_OK) { - if (bzs.avail_in == 0 && !end_of_file) { - ssize_t n; + while (ret == BZ_OK) { + if (bzs.avail_in == 0 && !end_of_file) { + ssize_t n; - n = read(in, inbuf, BUFLEN); - if (n < 0) - maybe_err("read"); - if (n == 0) - end_of_file = 1; - bzs.next_in = inbuf; - bzs.avail_in = n; - if (bytes_in) - *bytes_in += n; - } + n = read(in, inbuf, BUFLEN); + if (n < 0) + maybe_err("read"); + if (n == 0) + end_of_file = 1; + bzs.next_in = inbuf; + bzs.avail_in = n; + if (bytes_in) + *bytes_in += n; + } - bzs.next_out = outbuf; - bzs.avail_out = BUFLEN; - ret = BZ2_bzDecompress(&bzs); + bzs.next_out = outbuf; + bzs.avail_out = BUFLEN; + ret = BZ2_bzDecompress(&bzs); - switch (ret) { - case BZ_STREAM_END: - case BZ_OK: - if (ret == BZ_OK && end_of_file) { - /* - * If we hit this after a stream end, consider - * it as the end of the whole file and don't - * bail out. - */ - if (cold == 1) - ret = BZ_STREAM_END; - else - maybe_errx("truncated file"); - } - cold = 0; - if (!tflag && bzs.avail_out != BUFLEN) { - ssize_t n; + switch (ret) { + case BZ_STREAM_END: + case BZ_OK: + if (ret == BZ_OK && end_of_file) { + /* + * If we hit this after a stream end, consider + * it as the end of the whole file and don't + * bail out. + */ + if (cold == 1) + ret = BZ_STREAM_END; + else + maybe_errx("truncated file"); + } + cold = 0; + if (!tflag && bzs.avail_out != BUFLEN) { + ssize_t n; - n = write(out, outbuf, BUFLEN - bzs.avail_out); - if (n < 0) - maybe_err("write"); - bytes_out += n; - } - if (ret == BZ_STREAM_END && !end_of_file) { - if (BZ2_bzDecompressEnd(&bzs) != BZ_OK || - BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK) - maybe_errx("bzip2 re-init"); - cold = 1; - ret = BZ_OK; - } - break; + n = write(out, outbuf, BUFLEN - bzs.avail_out); + if (n < 0) + maybe_err("write"); + bytes_out += n; + } + if (ret == BZ_STREAM_END && !end_of_file) { + if (BZ2_bzDecompressEnd(&bzs) != BZ_OK || + BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK) + maybe_errx("bzip2 re-init"); + cold = 1; + ret = BZ_OK; + } + break; - case BZ_DATA_ERROR: - maybe_warnx("bzip2 data integrity error"); - break; + case BZ_DATA_ERROR: + maybe_warnx("bzip2 data integrity error"); + break; - case BZ_DATA_ERROR_MAGIC: - maybe_warnx("bzip2 magic number error"); - break; + case BZ_DATA_ERROR_MAGIC: + maybe_warnx("bzip2 magic number error"); + break; - case BZ_MEM_ERROR: - maybe_warnx("bzip2 out of memory"); - break; - - default: - maybe_warnx("unknown bzip2 error: %d", ret); - break; - } - } + case BZ_MEM_ERROR: + maybe_warnx("bzip2 out of memory"); + break; - if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK) - return (-1); + default: + maybe_warnx("unknown bzip2 error: %d", ret); + break; + } + } - return (bytes_out); + if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK) + return (-1); + + return (bytes_out); } diff --git a/usr.bin/gzip/unpack.c b/usr.bin/gzip/unpack.c index 93dbadbc3..add1bd6ce 100644 --- a/usr.bin/gzip/unpack.c +++ b/usr.bin/gzip/unpack.c @@ -1,5 +1,5 @@ -/* $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij $ */ -/* $NetBSD: unpack.c,v 1.2 2010/11/06 21:42:32 mrg Exp $ */ +/* $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij + * $ */ /*- * Copyright (c) 2009 Xin LI @@ -56,8 +56,8 @@ * range [0..254], so all levels' symbol count would fit into 1 byte. */ -#define PACK_HEADER_LENGTH 7 -#define HTREE_MAXLEVEL 24 +#define PACK_HEADER_LENGTH 7 +#define HTREE_MAXLEVEL 24 /* * unpack descriptor @@ -69,22 +69,22 @@ * leaf nodes count. */ typedef struct { - int symbol_size; /* Size of the symbol table */ - int treelevels; /* Levels for the huffman tree */ + int symbol_size; /* Size of the symbol table */ + int treelevels; /* Levels for the huffman tree */ - int *symbolsin; /* Table of leaf symbols count in - each level */ - int *inodesin; /* Table of internal nodes count in - each level */ + int *symbolsin; /* Table of leaf symbols count in + each level */ + int *inodesin; /* Table of internal nodes count in + each level */ - char *symbol; /* The symbol table */ - char *symbol_eob; /* Pointer to the EOB symbol */ - char **tree; /* Decoding huffman tree (pointers to - first symbol of each tree level */ + char *symbol; /* The symbol table */ + char *symbol_eob; /* Pointer to the EOB symbol */ + char **tree; /* Decoding huffman tree (pointers to + first symbol of each tree level */ - off_t uncompressed_size; /* Uncompressed size */ - FILE *fpIn; /* Input stream */ - FILE *fpOut; /* Output stream */ + off_t uncompressed_size; /* Uncompressed size */ + FILE *fpIn; /* Input stream */ + FILE *fpOut; /* Output stream */ } unpack_descriptor_t; /* @@ -95,48 +95,43 @@ typedef struct { * We don't zero out pointers here because nobody else would ever * reference the memory block without scrubbing them. */ -static void -unpack_descriptor_fini(unpack_descriptor_t *unpackd) -{ +static void unpack_descriptor_fini(unpack_descriptor_t *unpackd) { - free(unpackd->symbolsin); - free(unpackd->inodesin); - free(unpackd->symbol); - free(unpackd->tree); + free(unpackd->symbolsin); + free(unpackd->inodesin); + free(unpackd->symbol); + free(unpackd->tree); - fclose(unpackd->fpIn); - fclose(unpackd->fpOut); + fclose(unpackd->fpIn); + fclose(unpackd->fpOut); } /* * Recursively fill the internal node count table */ -static void -unpackd_fill_inodesin(const unpack_descriptor_t *unpackd, int level) -{ +static void unpackd_fill_inodesin(const unpack_descriptor_t *unpackd, + int level) { - /* - * The internal nodes would be 1/2 of total internal nodes and - * leaf nodes in the next level. For the last level there - * would be no internal node by definition. - */ - if (level < unpackd->treelevels) { - unpackd_fill_inodesin(unpackd, level + 1); - unpackd->inodesin[level] = (unpackd->inodesin[level + 1] + - unpackd->symbolsin[level + 1]) / 2; - } else - unpackd->inodesin[level] = 0; + /* + * The internal nodes would be 1/2 of total internal nodes and + * leaf nodes in the next level. For the last level there + * would be no internal node by definition. + */ + if (level < unpackd->treelevels) { + unpackd_fill_inodesin(unpackd, level + 1); + unpackd->inodesin[level] = + (unpackd->inodesin[level + 1] + unpackd->symbolsin[level + 1]) / 2; + } else + unpackd->inodesin[level] = 0; } /* * Update counter for accepted bytes */ -static void -accepted_bytes(off_t *bytes_in, off_t newbytes) -{ +static void accepted_bytes(off_t *bytes_in, off_t newbytes) { - if (bytes_in != NULL) - (*bytes_in) += newbytes; + if (bytes_in != NULL) + (*bytes_in) += newbytes; } /* @@ -145,179 +140,170 @@ accepted_bytes(off_t *bytes_in, off_t newbytes) * * Return value is uncompressed size. */ -static void -unpack_parse_header(int in, int out, char *pre, size_t prelen, off_t *bytes_in, - unpack_descriptor_t *unpackd) -{ - unsigned char hdr[PACK_HEADER_LENGTH]; /* buffer for header */ - ssize_t bytesread; /* Bytes read from the file */ - int i, j, thisbyte; +static void unpack_parse_header(int in, int out, char *pre, size_t prelen, + off_t *bytes_in, unpack_descriptor_t *unpackd) { + unsigned char hdr[PACK_HEADER_LENGTH]; /* buffer for header */ + ssize_t bytesread; /* Bytes read from the file */ + int i, j, thisbyte; - /* Prepend the header buffer if we already read some data */ - if (prelen != 0) - memcpy(hdr, pre, prelen); + /* Prepend the header buffer if we already read some data */ + if (prelen != 0) + memcpy(hdr, pre, prelen); - /* Read in and fill the rest bytes of header */ - bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen); - if (bytesread < 0) - maybe_err("Error reading pack header"); + /* Read in and fill the rest bytes of header */ + bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen); + if (bytesread < 0) + maybe_err("Error reading pack header"); - accepted_bytes(bytes_in, PACK_HEADER_LENGTH); + accepted_bytes(bytes_in, PACK_HEADER_LENGTH); - /* Obtain uncompressed length (bytes 2,3,4,5)*/ - unpackd->uncompressed_size = 0; - for (i = 2; i <= 5; i++) { - unpackd->uncompressed_size <<= 8; - unpackd->uncompressed_size |= hdr[i]; - } + /* Obtain uncompressed length (bytes 2,3,4,5)*/ + unpackd->uncompressed_size = 0; + for (i = 2; i <= 5; i++) { + unpackd->uncompressed_size <<= 8; + unpackd->uncompressed_size |= hdr[i]; + } - /* Get the levels of the tree */ - unpackd->treelevels = hdr[6]; - if (unpackd->treelevels > HTREE_MAXLEVEL || unpackd->treelevels < 1) - maybe_errx("Huffman tree has insane levels"); + /* Get the levels of the tree */ + unpackd->treelevels = hdr[6]; + if (unpackd->treelevels > HTREE_MAXLEVEL || unpackd->treelevels < 1) + maybe_errx("Huffman tree has insane levels"); - /* Let libc take care for buffering from now on */ - if ((unpackd->fpIn = fdopen(in, "r")) == NULL) - maybe_err("Can not fdopen() input stream"); - if ((unpackd->fpOut = fdopen(out, "w")) == NULL) - maybe_err("Can not fdopen() output stream"); + /* Let libc take care for buffering from now on */ + if ((unpackd->fpIn = fdopen(in, "r")) == NULL) + maybe_err("Can not fdopen() input stream"); + if ((unpackd->fpOut = fdopen(out, "w")) == NULL) + maybe_err("Can not fdopen() output stream"); - /* Allocate for the tables of bounds and the tree itself */ - unpackd->inodesin = - calloc(unpackd->treelevels, sizeof(*(unpackd->inodesin))); - unpackd->symbolsin = - calloc(unpackd->treelevels, sizeof(*(unpackd->symbolsin))); - unpackd->tree = - calloc(unpackd->treelevels, (sizeof (*(unpackd->tree)))); - if (unpackd->inodesin == NULL || unpackd->symbolsin == NULL || - unpackd->tree == NULL) - maybe_err("calloc"); + /* Allocate for the tables of bounds and the tree itself */ + unpackd->inodesin = calloc(unpackd->treelevels, sizeof(*(unpackd->inodesin))); + unpackd->symbolsin = + calloc(unpackd->treelevels, sizeof(*(unpackd->symbolsin))); + unpackd->tree = calloc(unpackd->treelevels, (sizeof(*(unpackd->tree)))); + if (unpackd->inodesin == NULL || unpackd->symbolsin == NULL || + unpackd->tree == NULL) + maybe_err("calloc"); - /* We count from 0 so adjust to match array upper bound */ - unpackd->treelevels--; + /* We count from 0 so adjust to match array upper bound */ + unpackd->treelevels--; - /* Read the levels symbol count table and calculate total */ - unpackd->symbol_size = 1; /* EOB */ - for (i = 0; i <= unpackd->treelevels; i++) { - if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) - maybe_err("File appears to be truncated"); - unpackd->symbolsin[i] = (unsigned char)thisbyte; - unpackd->symbol_size += unpackd->symbolsin[i]; - } - accepted_bytes(bytes_in, unpackd->treelevels); - if (unpackd->symbol_size > 256) - maybe_errx("Bad symbol table"); + /* Read the levels symbol count table and calculate total */ + unpackd->symbol_size = 1; /* EOB */ + for (i = 0; i <= unpackd->treelevels; i++) { + if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) + maybe_err("File appears to be truncated"); + unpackd->symbolsin[i] = (unsigned char)thisbyte; + unpackd->symbol_size += unpackd->symbolsin[i]; + } + accepted_bytes(bytes_in, unpackd->treelevels); + if (unpackd->symbol_size > 256) + maybe_errx("Bad symbol table"); - /* Allocate for the symbol table, point symbol_eob at the beginning */ - unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size); - if (unpackd->symbol == NULL) - maybe_err("calloc"); + /* Allocate for the symbol table, point symbol_eob at the beginning */ + unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size); + if (unpackd->symbol == NULL) + maybe_err("calloc"); - /* - * Read in the symbol table, which contain [2, 256] symbols. - * In order to fit the count in one byte, pack(1) would offset - * it by reducing 2 from the actual number from the last level. - * - * We adjust the last level's symbol count by 1 here, because - * the EOB symbol is not being transmitted explicitly. Another - * adjustment would be done later afterward. - */ - unpackd->symbolsin[unpackd->treelevels]++; - for (i = 0; i <= unpackd->treelevels; i++) { - unpackd->tree[i] = unpackd->symbol_eob; - for (j = 0; j < unpackd->symbolsin[i]; j++) { - if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) - maybe_errx("Symbol table truncated"); - *unpackd->symbol_eob++ = (char)thisbyte; - } - accepted_bytes(bytes_in, unpackd->symbolsin[i]); - } + /* + * Read in the symbol table, which contain [2, 256] symbols. + * In order to fit the count in one byte, pack(1) would offset + * it by reducing 2 from the actual number from the last level. + * + * We adjust the last level's symbol count by 1 here, because + * the EOB symbol is not being transmitted explicitly. Another + * adjustment would be done later afterward. + */ + unpackd->symbolsin[unpackd->treelevels]++; + for (i = 0; i <= unpackd->treelevels; i++) { + unpackd->tree[i] = unpackd->symbol_eob; + for (j = 0; j < unpackd->symbolsin[i]; j++) { + if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) + maybe_errx("Symbol table truncated"); + *unpackd->symbol_eob++ = (char)thisbyte; + } + accepted_bytes(bytes_in, unpackd->symbolsin[i]); + } - /* Now, take account for the EOB symbol as well */ - unpackd->symbolsin[unpackd->treelevels]++; + /* Now, take account for the EOB symbol as well */ + unpackd->symbolsin[unpackd->treelevels]++; - /* - * The symbolsin table has been constructed now. - * Calculate the internal nodes count table based on it. - */ - unpackd_fill_inodesin(unpackd, 0); + /* + * The symbolsin table has been constructed now. + * Calculate the internal nodes count table based on it. + */ + unpackd_fill_inodesin(unpackd, 0); } /* * Decode huffman stream, based on the huffman tree. */ -static void -unpack_decode(const unpack_descriptor_t *unpackd, off_t *bytes_in) -{ - int thislevel, thiscode, thisbyte, inlevelindex; - int i; - off_t bytes_out = 0; - const char *thissymbol; /* The symbol pointer decoded from stream */ +static void unpack_decode(const unpack_descriptor_t *unpackd, off_t *bytes_in) { + int thislevel, thiscode, thisbyte, inlevelindex; + int i; + off_t bytes_out = 0; + const char *thissymbol; /* The symbol pointer decoded from stream */ - /* - * Decode huffman. Fetch every bytes from the file, get it - * into 'thiscode' bit-by-bit, then output the symbol we got - * when one has been found. - * - * Assumption: sizeof(int) > ((max tree levels + 1) / 8). - * bad things could happen if not. - */ - thislevel = 0; - thiscode = thisbyte = 0; + /* + * Decode huffman. Fetch every bytes from the file, get it + * into 'thiscode' bit-by-bit, then output the symbol we got + * when one has been found. + * + * Assumption: sizeof(int) > ((max tree levels + 1) / 8). + * bad things could happen if not. + */ + thislevel = 0; + thiscode = thisbyte = 0; - while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) { - accepted_bytes(bytes_in, 1); + while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) { + accepted_bytes(bytes_in, 1); - /* - * Split one bit from thisbyte, from highest to lowest, - * feed the bit into thiscode, until we got a symbol from - * the tree. - */ - for (i = 7; i >= 0; i--) { - thiscode = (thiscode << 1) | ((thisbyte >> i) & 1); + /* + * Split one bit from thisbyte, from highest to lowest, + * feed the bit into thiscode, until we got a symbol from + * the tree. + */ + for (i = 7; i >= 0; i--) { + thiscode = (thiscode << 1) | ((thisbyte >> i) & 1); - /* Did we got a symbol? (referencing leaf node) */ - if (thiscode >= unpackd->inodesin[thislevel]) { - inlevelindex = - thiscode - unpackd->inodesin[thislevel]; - if (inlevelindex > unpackd->symbolsin[thislevel]) - maybe_errx("File corrupt"); + /* Did we got a symbol? (referencing leaf node) */ + if (thiscode >= unpackd->inodesin[thislevel]) { + inlevelindex = thiscode - unpackd->inodesin[thislevel]; + if (inlevelindex > unpackd->symbolsin[thislevel]) + maybe_errx("File corrupt"); - thissymbol = - &(unpackd->tree[thislevel][inlevelindex]); - if ((thissymbol == unpackd->symbol_eob) && - (bytes_out == unpackd->uncompressed_size)) - goto finished; + thissymbol = &(unpackd->tree[thislevel][inlevelindex]); + if ((thissymbol == unpackd->symbol_eob) && + (bytes_out == unpackd->uncompressed_size)) + goto finished; - fputc((*thissymbol), unpackd->fpOut); - bytes_out++; + fputc((*thissymbol), unpackd->fpOut); + bytes_out++; - /* Prepare for next input */ - thislevel = 0; thiscode = 0; - } else { - thislevel++; - if (thislevel > unpackd->treelevels) - maybe_errx("File corrupt"); - } - } - } + /* Prepare for next input */ + thislevel = 0; + thiscode = 0; + } else { + thislevel++; + if (thislevel > unpackd->treelevels) + maybe_errx("File corrupt"); + } + } + } finished: - if (bytes_out != unpackd->uncompressed_size) - maybe_errx("Premature EOF"); + if (bytes_out != unpackd->uncompressed_size) + maybe_errx("Premature EOF"); } /* Handler for pack(1)'ed file */ -static off_t -unpack(int in, int out, char *pre, size_t prelen, off_t *bytes_in) -{ - unpack_descriptor_t unpackd; +static off_t unpack(int in, int out, char *pre, size_t prelen, + off_t *bytes_in) { + unpack_descriptor_t unpackd; - unpack_parse_header(dup(in), dup(out), pre, prelen, bytes_in, &unpackd); - unpack_decode(&unpackd, bytes_in); - unpack_descriptor_fini(&unpackd); + unpack_parse_header(dup(in), dup(out), pre, prelen, bytes_in, &unpackd); + unpack_decode(&unpackd, bytes_in); + unpack_descriptor_fini(&unpackd); - /* If we reached here, the unpack was successful */ - return (unpackd.uncompressed_size); + /* If we reached here, the unpack was successful */ + return (unpackd.uncompressed_size); } - diff --git a/usr.bin/gzip/unxz.c b/usr.bin/gzip/unxz.c index 282ef2434..8143248d4 100644 --- a/usr.bin/gzip/unxz.c +++ b/usr.bin/gzip/unxz.c @@ -1,10 +1,9 @@ -/* $NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $ */ /*- - * Copyright (c) 2011 The NetBSD Foundation, Inc. + * Copyright (c) 2011 The Foundation, Inc. * All rights reserved. * - * This code is derived from software contributed to The NetBSD Foundation + * This code is derived from software contributed to The Foundation * by Christos Zoulas. * * Redistribution and use in source and binary forms, with or without @@ -17,13 +16,13 @@ * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: - * This product includes software developed by the NetBSD + * This product includes software developed by the * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its + * 4. Neither the name of The Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * THIS SOFTWARE IS PROVIDED BY THE FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS @@ -36,125 +35,121 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include -__RCSID("$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $"); -#include #include +#include +#include #include #include -#include -static off_t -unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in) -{ - lzma_stream strm = LZMA_STREAM_INIT; - static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED; - lzma_ret ret; - lzma_action action = LZMA_RUN; - off_t bytes_out, bp; - uint8_t ibuf[BUFSIZ]; - uint8_t obuf[BUFSIZ]; +static off_t unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in) { + lzma_stream strm = LZMA_STREAM_INIT; + static const int flags = LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED; + lzma_ret ret; + lzma_action action = LZMA_RUN; + off_t bytes_out, bp; + uint8_t ibuf[BUFSIZ]; + uint8_t obuf[BUFSIZ]; - if (bytes_in == NULL) - bytes_in = &bp; + if (bytes_in == NULL) + bytes_in = &bp; - strm.next_in = ibuf; - memcpy(ibuf, pre, prelen); - strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen); - if (strm.avail_in == (size_t)-1) - maybe_err("read failed"); - strm.avail_in += prelen; - *bytes_in = strm.avail_in; + strm.next_in = ibuf; + memcpy(ibuf, pre, prelen); + strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen); + if (strm.avail_in == (size_t)-1) + maybe_err("read failed"); + strm.avail_in += prelen; + *bytes_in = strm.avail_in; - if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK) - maybe_errx("Can't initialize decoder (%d)", ret); + if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK) + maybe_errx("Can't initialize decoder (%d)", ret); - strm.next_out = NULL; - strm.avail_out = 0; - if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK) - maybe_errx("Can't read headers (%d)", ret); + strm.next_out = NULL; + strm.avail_out = 0; + if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK) + maybe_errx("Can't read headers (%d)", ret); - bytes_out = 0; - strm.next_out = obuf; - strm.avail_out = sizeof(obuf); + bytes_out = 0; + strm.next_out = obuf; + strm.avail_out = sizeof(obuf); - for (;;) { - if (strm.avail_in == 0) { - strm.next_in = ibuf; - strm.avail_in = read(i, ibuf, sizeof(ibuf)); - switch (strm.avail_in) { - case (size_t)-1: - maybe_err("read failed"); - /*NOTREACHED*/ - case 0: - action = LZMA_FINISH; - break; - default: - *bytes_in += strm.avail_in; - break; - } - } + for (;;) { + if (strm.avail_in == 0) { + strm.next_in = ibuf; + strm.avail_in = read(i, ibuf, sizeof(ibuf)); + switch (strm.avail_in) { + case (size_t)-1: + maybe_err("read failed"); + /*NOTREACHED*/ + case 0: + action = LZMA_FINISH; + break; + default: + *bytes_in += strm.avail_in; + break; + } + } - ret = lzma_code(&strm, action); + ret = lzma_code(&strm, action); - // Write and check write error before checking decoder error. - // This way as much data as possible gets written to output - // even if decoder detected an error. - if (strm.avail_out == 0 || ret != LZMA_OK) { - const size_t write_size = sizeof(obuf) - strm.avail_out; + // Write and check write error before checking decoder error. + // This way as much data as possible gets written to output + // even if decoder detected an error. + if (strm.avail_out == 0 || ret != LZMA_OK) { + const size_t write_size = sizeof(obuf) - strm.avail_out; - if (write(o, obuf, write_size) != (ssize_t)write_size) - maybe_err("write failed"); + if (write(o, obuf, write_size) != (ssize_t)write_size) + maybe_err("write failed"); - strm.next_out = obuf; - strm.avail_out = sizeof(obuf); - bytes_out += write_size; - } + strm.next_out = obuf; + strm.avail_out = sizeof(obuf); + bytes_out += write_size; + } - if (ret != LZMA_OK) { - if (ret == LZMA_STREAM_END) { - // Check that there's no trailing garbage. - if (strm.avail_in != 0 || read(i, ibuf, 1)) - ret = LZMA_DATA_ERROR; - else { - lzma_end(&strm); - return bytes_out; - } - } + if (ret != LZMA_OK) { + if (ret == LZMA_STREAM_END) { + // Check that there's no trailing garbage. + if (strm.avail_in != 0 || read(i, ibuf, 1)) + ret = LZMA_DATA_ERROR; + else { + lzma_end(&strm); + return bytes_out; + } + } - const char *msg; - switch (ret) { - case LZMA_MEM_ERROR: - msg = strerror(ENOMEM); - break; + const char *msg; + switch (ret) { + case LZMA_MEM_ERROR: + msg = strerror(ENOMEM); + break; - case LZMA_FORMAT_ERROR: - msg = "File format not recognized"; - break; + case LZMA_FORMAT_ERROR: + msg = "File format not recognized"; + break; - case LZMA_OPTIONS_ERROR: - // FIXME: Better message? - msg = "Unsupported compression options"; - break; + case LZMA_OPTIONS_ERROR: + // FIXME: Better message? + msg = "Unsupported compression options"; + break; - case LZMA_DATA_ERROR: - msg = "File is corrupt"; - break; + case LZMA_DATA_ERROR: + msg = "File is corrupt"; + break; - case LZMA_BUF_ERROR: - msg = "Unexpected end of input"; - break; + case LZMA_BUF_ERROR: + msg = "Unexpected end of input"; + break; - case LZMA_MEMLIMIT_ERROR: - msg = "Reached memory limit"; - break; + case LZMA_MEMLIMIT_ERROR: + msg = "Reached memory limit"; + break; - default: - maybe_errx("Unknown error (%d)", ret); - break; - } - maybe_errx("%s", msg); - - } - } + default: + maybe_errx("Unknown error (%d)", ret); + break; + } + maybe_errx("%s", msg); + } + } } diff --git a/usr.bin/gzip/zdiff b/usr.bin/gzip/zdiff index 6dfaf7fde..81ce0bdd9 100755 --- a/usr.bin/gzip/zdiff +++ b/usr.bin/gzip/zdiff @@ -1,11 +1,9 @@ #!/bin/sh - # -# $NetBSD: zdiff,v 1.5 2010/04/14 20:30:28 joerg Exp $ -# # $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $ # # Copyright (c) 2003 Todd C. Miller -# Copyright (c) 2010 Joerg Sonnenberger +# Copyright (c) 2010 Joerg Sonnenberger # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above diff --git a/usr.bin/gzip/zdiff.1 b/usr.bin/gzip/zdiff.1 index 8a448fc41..eb8da35b6 100644 --- a/usr.bin/gzip/zdiff.1 +++ b/usr.bin/gzip/zdiff.1 @@ -1,8 +1,7 @@ -.\" $NetBSD: zdiff.1,v 1.5 2010/04/14 19:52:05 wiz Exp $ .\" $OpenBSD: zdiff.1,v 1.2 2003/07/13 17:39:14 millert Exp $ .\" .\" Copyright (c) 2003 Todd C. Miller -.\" Copyright (c) 2010 Joerg Sonnenberger +.\" Copyright (c) 2010 Joerg Sonnenberger .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above diff --git a/usr.bin/gzip/zforce b/usr.bin/gzip/zforce index 34f1628ec..d991da030 100755 --- a/usr.bin/gzip/zforce +++ b/usr.bin/gzip/zforce @@ -1,7 +1,5 @@ #!/bin/sh - # -# $NetBSD: zforce,v 1.2 2003/12/28 12:43:43 wiz Exp $ -# # $OpenBSD: zforce,v 1.2 2003/08/05 18:22:17 deraadt Exp $ # # Copyright (c) 2003 Otto Moerbeek diff --git a/usr.bin/gzip/zforce.1 b/usr.bin/gzip/zforce.1 index a79af4006..81916e94c 100644 --- a/usr.bin/gzip/zforce.1 +++ b/usr.bin/gzip/zforce.1 @@ -1,4 +1,3 @@ -.\" $NetBSD: zforce.1,v 1.2 2003/12/28 12:43:43 wiz Exp $ .\" $OpenBSD: zforce.1,v 1.1 2003/07/29 11:50:09 otto Exp $ .\" .\" Copyright (c) 2003 Otto Moerbeek diff --git a/usr.bin/gzip/zgrep b/usr.bin/gzip/zgrep index 7da1afe32..1f232a16a 100755 --- a/usr.bin/gzip/zgrep +++ b/usr.bin/gzip/zgrep @@ -1,7 +1,5 @@ #!/bin/sh # -# $NetBSD: zgrep,v 1.9 2015/07/06 12:05:40 nakayama Exp $ -# # Copyright (c) 2003 Thomas Klausner. # # Redistribution and use in source and binary forms, with or without diff --git a/usr.bin/gzip/zgrep.1 b/usr.bin/gzip/zgrep.1 index 465de6029..a9ad028a0 100644 --- a/usr.bin/gzip/zgrep.1 +++ b/usr.bin/gzip/zgrep.1 @@ -1,4 +1,3 @@ -.\" $NetBSD: zgrep.1,v 1.3 2008/05/08 15:35:23 wiz Exp $ .\" .\" Copyright (c) 2003 Thomas Klausner. .\" @@ -91,4 +90,4 @@ In case of missing arguments or missing pattern, .Xr zcat 1 .Sh AUTHORS .An Thomas Klausner -.Aq wiz@NetBSD.org +.Aq wiz@example.org diff --git a/usr.bin/gzip/zmore b/usr.bin/gzip/zmore index b0100b94c..971071fb5 100755 --- a/usr.bin/gzip/zmore +++ b/usr.bin/gzip/zmore @@ -1,7 +1,5 @@ #!/bin/sh - # -# $NetBSD: zmore,v 1.5 2013/12/06 13:33:15 pettai Exp $ -# # $OpenBSD: zmore,v 1.6 2008/08/20 09:22:02 mpf Exp $ # # Copyright (c) 2003 Todd C. Miller diff --git a/usr.bin/gzip/zmore.1 b/usr.bin/gzip/zmore.1 index dbfc83386..a60435986 100644 --- a/usr.bin/gzip/zmore.1 +++ b/usr.bin/gzip/zmore.1 @@ -1,4 +1,3 @@ -.\" $NetBSD: zmore.1,v 1.4 2013/11/12 21:58:37 pettai Exp $ .\" $OpenBSD: zmore.1,v 1.10 2009/08/16 09:41:08 sobrado Exp $ .\" .\" Copyright (c) 2003 Todd C. Miller diff --git a/usr.bin/gzip/znew b/usr.bin/gzip/znew index 7a9a60a6e..88958e145 100755 --- a/usr.bin/gzip/znew +++ b/usr.bin/gzip/znew @@ -1,7 +1,5 @@ #!/bin/ksh - # -# $NetBSD: znew,v 1.3 2008/04/27 09:07:13 nakayama Exp $ -# # $OpenBSD: znew,v 1.2 2003/08/05 18:22:17 deraadt Exp $ # # Copyright (c) 2003 Otto Moerbeek diff --git a/usr.bin/gzip/znew.1 b/usr.bin/gzip/znew.1 index 31266e8bc..5e68543d9 100644 --- a/usr.bin/gzip/znew.1 +++ b/usr.bin/gzip/znew.1 @@ -1,4 +1,3 @@ -.\" $NetBSD: znew.1,v 1.2 2003/12/28 12:43:43 wiz Exp $ .\" $OpenBSD: znew.1,v 1.1 2003/08/02 20:52:50 otto Exp $ .\" .\" Copyright (c) 2003 Otto Moerbeek diff --git a/usr.bin/gzip/zuncompress.c b/usr.bin/gzip/zuncompress.c index 11b57ed76..efe8d7bc4 100644 --- a/usr.bin/gzip/zuncompress.c +++ b/usr.bin/gzip/zuncompress.c @@ -1,4 +1,3 @@ -/* $NetBSD: zuncompress.c,v 1.11 2011/08/16 13:55:02 joerg Exp $ */ /*- * Copyright (c) 1985, 1986, 1992, 1993 @@ -32,49 +31,47 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * from: NetBSD: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp + * from: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp */ /* This file is #included by gzip.c */ -static int zread(void *, char *, int); +static int zread(void *, char *, int); -#define tab_prefixof(i) (zs->zs_codetab[i]) -#define tab_suffixof(i) ((char_type *)(zs->zs_htab))[i] -#define de_stack ((char_type *)&tab_suffixof(1 << BITS)) +#define tab_prefixof(i) (zs->zs_codetab[i]) +#define tab_suffixof(i) ((char_type *)(zs->zs_htab))[i] +#define de_stack ((char_type *)&tab_suffixof(1 << BITS)) -#define BITS 16 /* Default bits. */ -#define HSIZE 69001 /* 95% occupancy */ /* XXX may not need HSIZE */ -#define BIT_MASK 0x1f /* Defines for third byte of header. */ -#define BLOCK_MASK 0x80 -#define CHECK_GAP 10000 /* Ratio check interval. */ -#define BUFSIZE (64 * 1024) +#define BITS 16 /* Default bits. */ +#define HSIZE 69001 /* 95% occupancy */ /* XXX may not need HSIZE */ +#define BIT_MASK 0x1f /* Defines for third byte of header. */ +#define BLOCK_MASK 0x80 +#define CHECK_GAP 10000 /* Ratio check interval. */ +#define BUFSIZE (64 * 1024) -/* +/* * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is * a fourth header byte (for expansion). - */ -#define INIT_BITS 9 /* Initial number of bits/code. */ + */ +#define INIT_BITS 9 /* Initial number of bits/code. */ /* * the next two codes should not be changed lightly, as they must not * lie within the contiguous general code space. */ -#define FIRST 257 /* First free entry. */ -#define CLEAR 256 /* Table clear output code. */ +#define FIRST 257 /* First free entry. */ +#define CLEAR 256 /* Table clear output code. */ +#define MAXCODE(n_bits) ((1 << (n_bits)) - 1) -#define MAXCODE(n_bits) ((1 << (n_bits)) - 1) +typedef long code_int; +typedef long count_int; +typedef u_char char_type; -typedef long code_int; -typedef long count_int; -typedef u_char char_type; +static char_type magic_header[] = {'\037', '\235'}; /* 1F 9D */ -static char_type magic_header[] = - {'\037', '\235'}; /* 1F 9D */ - -static char_type rmask[9] = - {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; +static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, + 0x1f, 0x3f, 0x7f, 0xff}; /* XXX zuncompress global */ off_t total_compressed_bytes; @@ -82,124 +79,116 @@ size_t compressed_prelen; char *compressed_pre; struct s_zstate { - FILE *zs_fp; /* File stream for I/O */ - char zs_mode; /* r or w */ - enum { - S_START, S_MIDDLE, S_EOF - } zs_state; /* State of computation */ - int zs_n_bits; /* Number of bits/code. */ - int zs_maxbits; /* User settable max # bits/code. */ - code_int zs_maxcode; /* Maximum code, given n_bits. */ - code_int zs_maxmaxcode; /* Should NEVER generate this code. */ - count_int zs_htab [HSIZE]; - u_short zs_codetab [HSIZE]; - code_int zs_hsize; /* For dynamic table sizing. */ - code_int zs_free_ent; /* First unused entry. */ - /* - * Block compression parameters -- after all codes are used up, - * and compression rate changes, start over. - */ - int zs_block_compress; - int zs_clear_flg; - long zs_ratio; - count_int zs_checkpoint; - int zs_offset; - long zs_in_count; /* Length of input. */ - long zs_bytes_out; /* Length of compressed output. */ - long zs_out_count; /* # of codes output (for debugging). */ - char_type zs_buf[BITS]; - union { - struct { - long zs_fcode; - code_int zs_ent; - code_int zs_hsize_reg; - int zs_hshift; - } w; /* Write parameters */ - struct { - char_type *zs_stackp; - int zs_finchar; - code_int zs_code, zs_oldcode, zs_incode; - int zs_roffset, zs_size; - char_type zs_gbuf[BITS]; - } r; /* Read parameters */ - } u; + FILE *zs_fp; /* File stream for I/O */ + char zs_mode; /* r or w */ + enum { S_START, S_MIDDLE, S_EOF } zs_state; /* State of computation */ + int zs_n_bits; /* Number of bits/code. */ + int zs_maxbits; /* User settable max # bits/code. */ + code_int zs_maxcode; /* Maximum code, given n_bits. */ + code_int zs_maxmaxcode; /* Should NEVER generate this code. */ + count_int zs_htab[HSIZE]; + u_short zs_codetab[HSIZE]; + code_int zs_hsize; /* For dynamic table sizing. */ + code_int zs_free_ent; /* First unused entry. */ + /* + * Block compression parameters -- after all codes are used up, + * and compression rate changes, start over. + */ + int zs_block_compress; + int zs_clear_flg; + long zs_ratio; + count_int zs_checkpoint; + int zs_offset; + long zs_in_count; /* Length of input. */ + long zs_bytes_out; /* Length of compressed output. */ + long zs_out_count; /* # of codes output (for debugging). */ + char_type zs_buf[BITS]; + union { + struct { + long zs_fcode; + code_int zs_ent; + code_int zs_hsize_reg; + int zs_hshift; + } w; /* Write parameters */ + struct { + char_type *zs_stackp; + int zs_finchar; + code_int zs_code, zs_oldcode, zs_incode; + int zs_roffset, zs_size; + char_type zs_gbuf[BITS]; + } r; /* Read parameters */ + } u; }; -static code_int getcode(struct s_zstate *zs); +static code_int getcode(struct s_zstate *zs); -static off_t -zuncompress(FILE *in, FILE *out, char *pre, size_t prelen, - off_t *compressed_bytes) -{ - off_t bin, bout = 0; - char *buf; +static off_t zuncompress(FILE *in, FILE *out, char *pre, size_t prelen, + off_t *compressed_bytes) { + off_t bin, bout = 0; + char *buf; - buf = malloc(BUFSIZE); - if (buf == NULL) - return -1; + buf = malloc(BUFSIZE); + if (buf == NULL) + return -1; - /* XXX */ - compressed_prelen = prelen; - if (prelen != 0) - compressed_pre = pre; - else - compressed_pre = NULL; + /* XXX */ + compressed_prelen = prelen; + if (prelen != 0) + compressed_pre = pre; + else + compressed_pre = NULL; - while ((bin = fread(buf, 1, sizeof(buf), in)) != 0) { - if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) { - free(buf); - return -1; - } - bout += bin; - } + while ((bin = fread(buf, 1, sizeof(buf), in)) != 0) { + if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) { + free(buf); + return -1; + } + bout += bin; + } - if (compressed_bytes) - *compressed_bytes = total_compressed_bytes; + if (compressed_bytes) + *compressed_bytes = total_compressed_bytes; - free(buf); - return bout; + free(buf); + return bout; } -static int -zclose(void *zs) -{ - free(zs); - /* We leave the caller to close the fd passed to zdopen() */ - return 0; +static int zclose(void *zs) { + free(zs); + /* We leave the caller to close the fd passed to zdopen() */ + return 0; } -FILE * -zdopen(int fd) -{ - struct s_zstate *zs; +FILE *zdopen(int fd) { + struct s_zstate *zs; - if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL) - return (NULL); + if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL) + return (NULL); - zs->zs_state = S_START; + zs->zs_state = S_START; - /* XXX we can get rid of some of these */ - zs->zs_hsize = HSIZE; /* For dynamic table sizing. */ - zs->zs_free_ent = 0; /* First unused entry. */ - zs->zs_block_compress = BLOCK_MASK; - zs->zs_clear_flg = 0; /* XXX we calloc()'d this structure why = 0? */ - zs->zs_ratio = 0; - zs->zs_checkpoint = CHECK_GAP; - zs->zs_in_count = 1; /* Length of input. */ - zs->zs_out_count = 0; /* # of codes output (for debugging). */ - zs->u.r.zs_roffset = 0; - zs->u.r.zs_size = 0; + /* XXX we can get rid of some of these */ + zs->zs_hsize = HSIZE; /* For dynamic table sizing. */ + zs->zs_free_ent = 0; /* First unused entry. */ + zs->zs_block_compress = BLOCK_MASK; + zs->zs_clear_flg = 0; /* XXX we calloc()'d this structure why = 0? */ + zs->zs_ratio = 0; + zs->zs_checkpoint = CHECK_GAP; + zs->zs_in_count = 1; /* Length of input. */ + zs->zs_out_count = 0; /* # of codes output (for debugging). */ + zs->u.r.zs_roffset = 0; + zs->u.r.zs_size = 0; - /* - * Layering compress on top of stdio in order to provide buffering, - * and ensure that reads and write work with the data specified. - */ - if ((zs->zs_fp = fdopen(fd, "r")) == NULL) { - free(zs); - return NULL; - } + /* + * Layering compress on top of stdio in order to provide buffering, + * and ensure that reads and write work with the data specified. + */ + if ((zs->zs_fp = fdopen(fd, "r")) == NULL) { + free(zs); + return NULL; + } - return funopen(zs, zread, NULL, NULL, zclose); + return funopen(zs, zread, NULL, NULL, zclose); } /* @@ -208,117 +197,115 @@ zdopen(int fd) * compressed file. The tables used herein are shared with those of the * compress() routine. See the definitions above. */ -static int -zread(void *cookie, char *rbp, int num) -{ - u_int count, i; - struct s_zstate *zs; - u_char *bp, header[3]; +static int zread(void *cookie, char *rbp, int num) { + u_int count, i; + struct s_zstate *zs; + u_char *bp, header[3]; - if (num == 0) - return (0); + if (num == 0) + return (0); - zs = cookie; - count = num; - bp = (u_char *)rbp; - switch (zs->zs_state) { - case S_START: - zs->zs_state = S_MIDDLE; - break; - case S_MIDDLE: - goto middle; - case S_EOF: - goto eof; - } + zs = cookie; + count = num; + bp = (u_char *)rbp; + switch (zs->zs_state) { + case S_START: + zs->zs_state = S_MIDDLE; + break; + case S_MIDDLE: + goto middle; + case S_EOF: + goto eof; + } - /* Check the magic number */ - for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--) - header[i] = *compressed_pre++; + /* Check the magic number */ + for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--) + header[i] = *compressed_pre++; - if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) != - sizeof(header) - i || - memcmp(header, magic_header, sizeof(magic_header)) != 0) { - errno = EFTYPE; - return (-1); - } - total_compressed_bytes = 0; - zs->zs_maxbits = header[2]; /* Set -b from file. */ - zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK; - zs->zs_maxbits &= BIT_MASK; - zs->zs_maxmaxcode = 1L << zs->zs_maxbits; - if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) { - errno = EFTYPE; - return (-1); - } - /* As above, initialize the first 256 entries in the table. */ - zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); - for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) { - tab_prefixof(zs->u.r.zs_code) = 0; - tab_suffixof(zs->u.r.zs_code) = (char_type) zs->u.r.zs_code; - } - zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256; + if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) != + sizeof(header) - i || + memcmp(header, magic_header, sizeof(magic_header)) != 0) { + errno = EFTYPE; + return (-1); + } + total_compressed_bytes = 0; + zs->zs_maxbits = header[2]; /* Set -b from file. */ + zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK; + zs->zs_maxbits &= BIT_MASK; + zs->zs_maxmaxcode = 1L << zs->zs_maxbits; + if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) { + errno = EFTYPE; + return (-1); + } + /* As above, initialize the first 256 entries in the table. */ + zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); + for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) { + tab_prefixof(zs->u.r.zs_code) = 0; + tab_suffixof(zs->u.r.zs_code) = (char_type)zs->u.r.zs_code; + } + zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256; - zs->u.r.zs_oldcode = -1; - zs->u.r.zs_stackp = de_stack; + zs->u.r.zs_oldcode = -1; + zs->u.r.zs_stackp = de_stack; - while ((zs->u.r.zs_code = getcode(zs)) > -1) { + while ((zs->u.r.zs_code = getcode(zs)) > -1) { - if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) { - for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; - zs->u.r.zs_code--) - tab_prefixof(zs->u.r.zs_code) = 0; - zs->zs_clear_flg = 1; - zs->zs_free_ent = FIRST; - zs->u.r.zs_oldcode = -1; - continue; - } - zs->u.r.zs_incode = zs->u.r.zs_code; + if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) { + for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) + tab_prefixof(zs->u.r.zs_code) = 0; + zs->zs_clear_flg = 1; + zs->zs_free_ent = FIRST; + zs->u.r.zs_oldcode = -1; + continue; + } + zs->u.r.zs_incode = zs->u.r.zs_code; - /* Special case for KwKwK string. */ - if (zs->u.r.zs_code >= zs->zs_free_ent) { - if (zs->u.r.zs_code > zs->zs_free_ent || - zs->u.r.zs_oldcode == -1) { - /* Bad stream. */ - errno = EINVAL; - return (-1); - } - *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar; - zs->u.r.zs_code = zs->u.r.zs_oldcode; - } - /* - * The above condition ensures that code < free_ent. - * The construction of tab_prefixof in turn guarantees that - * each iteration decreases code and therefore stack usage is - * bound by 1 << BITS - 256. - */ + /* Special case for KwKwK string. */ + if (zs->u.r.zs_code >= zs->zs_free_ent) { + if (zs->u.r.zs_code > zs->zs_free_ent || zs->u.r.zs_oldcode == -1) { + /* Bad stream. */ + errno = EINVAL; + return (-1); + } + *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar; + zs->u.r.zs_code = zs->u.r.zs_oldcode; + } + /* + * The above condition ensures that code < free_ent. + * The construction of tab_prefixof in turn guarantees that + * each iteration decreases code and therefore stack usage is + * bound by 1 << BITS - 256. + */ - /* Generate output characters in reverse order. */ - while (zs->u.r.zs_code >= 256) { - *zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code); - zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code); - } - *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code); + /* Generate output characters in reverse order. */ + while (zs->u.r.zs_code >= 256) { + *zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code); + zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code); + } + *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code); - /* And put them out in forward order. */ -middle: do { - if (count-- == 0) - return (num); - *bp++ = *--zs->u.r.zs_stackp; - } while (zs->u.r.zs_stackp > de_stack); + /* And put them out in forward order. */ + middle: + do { + if (count-- == 0) + return (num); + *bp++ = *--zs->u.r.zs_stackp; + } while (zs->u.r.zs_stackp > de_stack); - /* Generate the new entry. */ - if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode && - zs->u.r.zs_oldcode != -1) { - tab_prefixof(zs->u.r.zs_code) = (u_short) zs->u.r.zs_oldcode; - tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar; - zs->zs_free_ent = zs->u.r.zs_code + 1; - } + /* Generate the new entry. */ + if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode && + zs->u.r.zs_oldcode != -1) { + tab_prefixof(zs->u.r.zs_code) = (u_short)zs->u.r.zs_oldcode; + tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar; + zs->zs_free_ent = zs->u.r.zs_code + 1; + } - /* Remember previous code. */ - zs->u.r.zs_oldcode = zs->u.r.zs_incode; - } - zs->zs_state = S_EOF; -eof: return (num - count); + /* Remember previous code. */ + zs->u.r.zs_oldcode = zs->u.r.zs_incode; + } + zs->zs_state = S_EOF; +eof: + return (num - count); } /*- @@ -328,68 +315,68 @@ eof: return (num - count); * Outputs: * code or -1 is returned. */ -static code_int -getcode(struct s_zstate *zs) -{ - code_int gcode; - int r_off, bits, i; - char_type *bp; +static code_int getcode(struct s_zstate *zs) { + code_int gcode; + int r_off, bits, i; + char_type *bp; - bp = zs->u.r.zs_gbuf; - if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size || - zs->zs_free_ent > zs->zs_maxcode) { - /* - * If the next entry will be too big for the current gcode - * size, then we must increase the size. This implies reading - * a new buffer full, too. - */ - if (zs->zs_free_ent > zs->zs_maxcode) { - zs->zs_n_bits++; - if (zs->zs_n_bits == zs->zs_maxbits) /* Won't get any bigger now. */ - zs->zs_maxcode = zs->zs_maxmaxcode; - else - zs->zs_maxcode = MAXCODE(zs->zs_n_bits); - } - if (zs->zs_clear_flg > 0) { - zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); - zs->zs_clear_flg = 0; - } - /* XXX */ - for (i = 0; i < zs->zs_n_bits && compressed_prelen; i++, compressed_prelen--) - zs->u.r.zs_gbuf[i] = *compressed_pre++; - zs->u.r.zs_size = fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp); - zs->u.r.zs_size += i; - if (zs->u.r.zs_size <= 0) /* End of file. */ - return (-1); - zs->u.r.zs_roffset = 0; + bp = zs->u.r.zs_gbuf; + if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size || + zs->zs_free_ent > zs->zs_maxcode) { + /* + * If the next entry will be too big for the current gcode + * size, then we must increase the size. This implies reading + * a new buffer full, too. + */ + if (zs->zs_free_ent > zs->zs_maxcode) { + zs->zs_n_bits++; + if (zs->zs_n_bits == zs->zs_maxbits) /* Won't get any bigger now. */ + zs->zs_maxcode = zs->zs_maxmaxcode; + else + zs->zs_maxcode = MAXCODE(zs->zs_n_bits); + } + if (zs->zs_clear_flg > 0) { + zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); + zs->zs_clear_flg = 0; + } + /* XXX */ + for (i = 0; i < zs->zs_n_bits && compressed_prelen; + i++, compressed_prelen--) + zs->u.r.zs_gbuf[i] = *compressed_pre++; + zs->u.r.zs_size = + fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp); + zs->u.r.zs_size += i; + if (zs->u.r.zs_size <= 0) /* End of file. */ + return (-1); + zs->u.r.zs_roffset = 0; - total_compressed_bytes += zs->u.r.zs_size; + total_compressed_bytes += zs->u.r.zs_size; - /* Round size down to integral number of codes. */ - zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1); - } - r_off = zs->u.r.zs_roffset; - bits = zs->zs_n_bits; + /* Round size down to integral number of codes. */ + zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1); + } + r_off = zs->u.r.zs_roffset; + bits = zs->zs_n_bits; - /* Get to the first byte. */ - bp += (r_off >> 3); - r_off &= 7; + /* Get to the first byte. */ + bp += (r_off >> 3); + r_off &= 7; - /* Get first part (low order bits). */ - gcode = (*bp++ >> r_off); - bits -= (8 - r_off); - r_off = 8 - r_off; /* Now, roffset into gcode word. */ + /* Get first part (low order bits). */ + gcode = (*bp++ >> r_off); + bits -= (8 - r_off); + r_off = 8 - r_off; /* Now, roffset into gcode word. */ - /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ - if (bits >= 8) { - gcode |= *bp++ << r_off; - r_off += 8; - bits -= 8; - } + /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ + if (bits >= 8) { + gcode |= *bp++ << r_off; + r_off += 8; + bits -= 8; + } - /* High order bits. */ - gcode |= (*bp & rmask[bits]) << r_off; - zs->u.r.zs_roffset += zs->zs_n_bits; + /* High order bits. */ + gcode |= (*bp & rmask[bits]) << r_off; + zs->u.r.zs_roffset += zs->zs_n_bits; - return (gcode); + return (gcode); }