8000 minix-1/usr.bin/gzip/gzip.c at R3.3.0 · syte/minix-1 · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2102 lines (1871 loc) · 43.5 KB

gzip.c

File metadata and controls

2102 lines (1871 loc) · 43.5 KB
SUFFIX("-gz", ""),
SUFFIX("-z", ""),
SUFFIX("_z", ""),
SUFFIX(".taz", ".tar"),
SUFFIX(".tgz", ".tar"),
#ifndef NO_BZIP2_SUPPORT
SUFFIX(BZ2_SUFFIX, ""),
#endif
#ifndef NO_COMPRESS_SUPPORT
SUFFIX(Z_SUFFIX, ""),
#endif
#ifndef NO_XZ_SUPPORT
SUFFIX(XZ_SUFFIX, ""),
#endif
SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
#endif /* SMALL */
#undef SUFFIX
};
#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
#define SUFFIX_MAXLEN 30
static const char gzip_version[] = "NetBSD gzip 20101018";
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 */
#else
#define qflag 0
#define tflag 0
#endif
static int exit_value = 0; /* exit value */
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) || \
!defined(NO_XZ_SUPPORT)
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 *);
#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);
#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);
#endif
#ifndef NO_BZIP2_SUPPORT
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 *);
#endif
#ifndef NO_PACK_SUPPORT
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 *);
#endif
#ifdef SMALL
#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' },
#if 0
/*
* This is what else GNU gzip implements. --ascii isn't useful
8BD0
* on NetBSD, 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 },
};
#endif
int
main(int argc, char **argv)
{
const char *progname = getprogname();
#ifndef SMALL
char *gzip;
int len;
#endif
int ch;
/* XXX set up signals */
#ifndef SMALL
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;
#ifdef SMALL
#define OPT_LIST "123456789cdhlV"
#else
#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 */
#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;
#endif
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);
}
#ifndef SMALL
if (qflag == 0 && lflag && argc > 1)
print_list(-1, 0, "(totals)", 0);
#endif
exit(exit_value);
}
/* maybe print a warning */
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;
}
/* ... without an errno. */
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;
}
/* maybe print an error */
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 !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
!defined(NO_XZ_SUPPORT)
/* ... without an errno. */
void
maybe_errx(const char *fmt, ...)
{
va_list ap;
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;
/* 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;
*argc += nenvarg;
ac = *argv;
nargv = (char **)malloc((*argc + 1) * sizeof(char *));
if (nargv == NULL)
maybe_err("malloc");
/* stash this away */
*argv = nargv;
/* 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;
}
copy_done:
/* 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;
#ifdef SMALL
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;
}
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;
#else
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++;
#endif
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;
}
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;
}
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;
}
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;
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;
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 (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");
#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;
out:
if (inbufp != NULL)
free(inbufp);
if (outbufp != NULL)
free(outbufp);
if (gsizep)
*gsizep = out_tot;
return in_tot;
}
/*
* uncompress input to output then close the input. return the
* 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;
#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;
}
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;
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 (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;
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_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_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_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_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_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_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 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;
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 (
#ifndef SMALL
/* don't write anything with -t */
tflag == 0 &&
#endif
write(out, outbufp, wr) != wr) {
maybe_warn("error writing to output");
goto stop_and_fail;
}
out_tot += wr;
out_sub_tot += wr;
}
if (error == Z_STREAM_END) {
inflateEnd(&z);
state++;
}
z.next_out = (unsigned char *)outbufp;
z.avail_out = BUFLEN;
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;
}
}
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 < 4) {
if (!done_reading) {
needmore = 1;
continue;
}
maybe_warnx("truncated input");
goto stop_and_fail;
}
origlen = ((unsigned)z.next_in[0] & 0xff) |
0