Deleted Added
full compact
1/* $NetBSD: gzip.c,v 1.99 2011/03/23 12:59:44 tsutsui Exp $ */
1/* $NetBSD: gzip.c,v 1.105 2011/08/30 23:06:00 joerg Exp $ */
2
3/*-
4 * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 16 unchanged lines hidden (view full) ---

26 * SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31#ifndef lint
32__COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\
33 Matthew R. Green. All rights reserved.");
34__FBSDID("$FreeBSD: head/usr.bin/gzip/gzip.c 222287 2011-05-25 18:04:11Z delphij $");
34__FBSDID("$FreeBSD: head/usr.bin/gzip/gzip.c 226184 2011-10-10 06:37:32Z delphij $");
35#endif /* not lint */
36
37/*
38 * gzip.c -- GPL free gzip using zlib.
39 *
40 * RFC 1950 covers the zlib format
41 * RFC 1951 covers the deflate format
42 * RFC 1952 covers the gzip format

--- 29 unchanged lines hidden (view full) ---

72 FT_BZIP2,
73#endif
74#ifndef NO_COMPRESS_SUPPORT
75 FT_Z,
76#endif
77#ifndef NO_PACK_SUPPORT
78 FT_PACK,
79#endif
80#ifndef NO_XZ_SUPPORT
81 FT_XZ,
82#endif
83 FT_LAST,
84 FT_UNKNOWN
85};
86
87#ifndef NO_BZIP2_SUPPORT
88#include <bzlib.h>
89
90#define BZ2_SUFFIX ".bz2"

--- 4 unchanged lines hidden (view full) ---

95#define Z_SUFFIX ".Z"
96#define Z_MAGIC "\037\235"
97#endif
98
99#ifndef NO_PACK_SUPPORT
100#define PACK_MAGIC "\037\036"
101#endif
102
103#ifndef NO_XZ_SUPPORT
104#include <lzma.h>
105#define XZ_SUFFIX ".xz"
106#define XZ_MAGIC "\3757zXZ"
107#endif
108
109#define GZ_SUFFIX ".gz"
110
111#define BUFLEN (64 * 1024)
112
113#define GZIP_MAGIC0 0x1F
114#define GZIP_MAGIC1 0x8B
115#define GZIP_OMAGIC1 0x9E
116

--- 26 unchanged lines hidden (view full) ---

143#ifndef NO_BZIP2_SUPPORT
144 SUFFIX(BZ2_SUFFIX, ""),
145 SUFFIX(".tbz", ".tar"),
146 SUFFIX(".tbz2", ".tar"),
147#endif
148#ifndef NO_COMPRESS_SUPPORT
149 SUFFIX(Z_SUFFIX, ""),
150#endif
151#ifndef NO_XZ_SUPPORT
152 SUFFIX(XZ_SUFFIX, ""),
153#endif
154 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
155#endif /* SMALL */
156#undef SUFFIX
157};
158#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
159#define SUFFIX_MAXLEN 30
160
149static const char gzip_version[] = "FreeBSD gzip 20110523";
161static const char gzip_version[] = "FreeBSD gzip 20111009";
162
163#ifndef SMALL
164static const char gzip_copyright[] = \
165" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
166" All rights reserved.\n"
167"\n"
168" Redistribution and use in source and binary forms, with or without\n"
169" modification, are permitted provided that the following conditions\n"

--- 36 unchanged lines hidden (view full) ---

206#define qflag 0
207#define tflag 0
208#endif
209
210static int exit_value = 0; /* exit value */
211
212static char *infile; /* name of file coming in */
213
202static void maybe_err(const char *fmt, ...) __dead2
203 __attribute__((__format__(__printf__, 1, 2)));
204#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
205static void maybe_errx(const char *fmt, ...) __dead2
206 __attribute__((__format__(__printf__, 1, 2)));
214static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead2;
215#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
216 !defined(NO_XZ_SUPPORT)
217static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead2;
218#endif
208static void maybe_warn(const char *fmt, ...)
209 __attribute__((__format__(__printf__, 1, 2)));
210static void maybe_warnx(const char *fmt, ...)
211 __attribute__((__format__(__printf__, 1, 2)));
219static void maybe_warn(const char *fmt, ...) __printflike(1, 2);
220static void maybe_warnx(const char *fmt, ...) __printflike(1, 2);
221static enum filetype file_gettype(u_char *);
222#ifdef SMALL
223#define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
224#endif
225static off_t gz_compress(int, int, off_t *, const char *, uint32_t);
226static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *);
227static off_t file_compress(char *, char *, size_t);
228static off_t file_uncompress(char *, char *, size_t);
229static void handle_pathname(char *);
230static void handle_file(char *, struct stat *);
231static void handle_stdin(void);
232static void handle_stdout(void);
233static void print_ratio(off_t, off_t, FILE *);
234static void print_list(int fd, off_t, const char *, time_t);
226static void usage(void);
227static void display_version(void);
235static void usage(void) __dead2;
236static void display_version(void) __dead2;
237#ifndef SMALL
238static void display_license(void);
239static void sigint_handler(int);
240#endif
241static const suffixes_t *check_suffix(char *, int);
242static ssize_t read_retry(int, void *, size_t);
243
244#ifdef SMALL

--- 16 unchanged lines hidden (view full) ---

261static FILE *zdopen(int);
262static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *);
263#endif
264
265#ifndef NO_PACK_SUPPORT
266static off_t unpack(int, int, char *, size_t, off_t *);
267#endif
268
260int main(int, char **p);
269#ifndef NO_XZ_SUPPORT
270static off_t unxz(int, int, char *, size_t, off_t *);
271#endif
272
273#ifdef SMALL
274#define getopt_long(a,b,c,d,e) getopt(a,b,c)
275#else
276static const struct option longopts[] = {
277 { "stdout", no_argument, 0, 'c' },
278 { "to-stdout", no_argument, 0, 'c' },
279 { "decompress", no_argument, 0, 'd' },

--- 182 unchanged lines hidden (view full) ---

462 if (qflag == 0) {
463 va_start(ap, fmt);
464 vwarn(fmt, ap);
465 va_end(ap);
466 }
467 exit(2);
468}
469
459#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
470#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
471 !defined(NO_XZ_SUPPORT)
472/* ... without an errno. */
473void
474maybe_errx(const char *fmt, ...)
475{
476 va_list ap;
477
478 if (qflag == 0) {
479 va_start(ap, fmt);

--- 649 unchanged lines hidden (view full) ---

1129 return FT_Z;
1130 else
1131#endif
1132#ifndef NO_PACK_SUPPORT
1133 if (memcmp(buf, PACK_MAGIC, 2) == 0)
1134 return FT_PACK;
1135 else
1136#endif
1137#ifndef NO_XZ_SUPPORT
1138 if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */
1139 return FT_XZ;
1140 else
1141#endif
1142 return FT_UNKNOWN;
1143}
1144
1145#ifndef SMALL
1146/* check the outfile is OK. */
1147static int
1148check_outfile(const char *outfile)
1149{

--- 231 unchanged lines hidden (view full) ---

1381 if (rbytes == -1)
1382 maybe_warn("can't read %s", file);
1383 else
1384 goto unexpected_EOF;
1385 goto lose;
1386 }
1387
1388 method = file_gettype(header1);
1372
1389#ifndef SMALL
1390 if (fflag == 0 && method == FT_UNKNOWN) {
1391 maybe_warnx("%s: not in gzip format", file);
1392 goto lose;
1393 }
1394
1395#endif
1396

--- 61 unchanged lines hidden (view full) ---

1458 goto lose;
1459 }
1460#ifndef SMALL
1461 remove_file = outfile;
1462#endif
1463 } else
1464 zfd = STDOUT_FILENO;
1465
1466 switch (method) {
1467#ifndef NO_BZIP2_SUPPORT
1451 if (method == FT_BZIP2) {
1452
1468 case FT_BZIP2:
1469 /* XXX */
1470 if (lflag) {
1471 maybe_warnx("no -l with bzip2 files");
1472 goto lose;
1473 }
1474
1475 size = unbzip2(fd, zfd, NULL, 0, NULL);
1460 } else
1476 break;
1477#endif
1478
1479#ifndef NO_COMPRESS_SUPPORT
1464 if (method == FT_Z) {
1480 case FT_Z: {
1481 FILE *in, *out;
1482
1483 /* XXX */
1484 if (lflag) {
1485 maybe_warnx("no -l with Lempel-Ziv files");
1486 goto lose;
1487 }
1488

--- 16 unchanged lines hidden (view full) ---

1505 unlink(outfile);
1506 (void)fclose(out);
1507 }
1508 if (fclose(out) != 0) {
1509 maybe_warn("failed outfile fclose");
1510 unlink(outfile);
1511 goto lose;
1512 }
1497 } else
1513 break;
1514 }
1515#endif
1516
1517#ifndef NO_PACK_SUPPORT
1501 if (method == FT_PACK) {
1518 case FT_PACK:
1519 if (lflag) {
1520 maybe_warnx("no -l with packed files");
1521 goto lose;
1522 }
1523
1524 size = unpack(fd, zfd, NULL, 0, NULL);
1508 } else
1525 break;
1526#endif
1527
1528#ifndef NO_XZ_SUPPORT
1529 case FT_XZ:
1530 if (lflag) {
1531 maybe_warnx("no -l with xz files");
1532 goto lose;
1533 }
1534
1535 size = unxz(fd, zfd, NULL, 0, NULL);
1536 break;
1537#endif
1538
1539#ifndef SMALL
1512 if (method == FT_UNKNOWN) {
1540 case FT_UNKNOWN:
1541 if (lflag) {
1542 maybe_warnx("no -l for unknown filetypes");
1543 goto lose;
1544 }
1545 size = cat_fd(NULL, 0, NULL, fd);
1518 } else
1546 break;
1547#endif
1520 {
1548 default:
1549 if (lflag) {
1550 print_list(fd, isb.st_size, outfile, isb.st_mtime);
1551 close(fd);
1552 return -1; /* XXX */
1553 }
1554
1555 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1556 break;
1557 }
1558
1559 if (close(fd) != 0)
1560 maybe_warn("couldn't close input");
1561 if (zfd != STDOUT_FILENO && close(zfd) != 0)
1562 maybe_warn("couldn't close output");
1563
1564 if (size == -1) {

--- 156 unchanged lines hidden (view full) ---

1721#endif
1722#ifndef NO_COMPRESS_SUPPORT
1723 case FT_Z:
1724 if ((in = zdopen(STDIN_FILENO)) == NULL) {
1725 maybe_warnx("zopen of stdin");
1726 return;
1727 }
1728
1700 usize = zuncompress(in, stdout, (char *)header1, sizeof header1, &gsize);
1729 usize = zuncompress(in, stdout, (char *)header1,
1730 sizeof header1, &gsize);
1731 fclose(in);
1732 break;
1733#endif
1734#ifndef NO_PACK_SUPPORT
1735 case FT_PACK:
1736 usize = unpack(STDIN_FILENO, STDOUT_FILENO,
1737 (char *)header1, sizeof header1, &gsize);
1738 break;
1739#endif
1740#ifndef NO_XZ_SUPPORT
1741 case FT_XZ:
1742 usize = unxz(STDIN_FILENO, STDOUT_FILENO,
1743 (char *)header1, sizeof header1, &gsize);
1744 break;
1745#endif
1746 }
1747
1748#ifndef SMALL
1749 if (vflag && !tflag && usize != -1 && gsize != -1)
1750 print_verbage(NULL, NULL, usize, gsize);
1751 if (vflag && tflag)
1752 print_test("(stdin)", usize != -1);
1753#endif

--- 351 unchanged lines hidden (view full) ---

2105}
2106
2107#ifndef SMALL
2108/* display the license information of FreeBSD gzip */
2109static void
2110display_license(void)
2111{
2112
2077 fprintf(stderr, "%s (based on NetBSD gzip 20091011)\n", gzip_version);
2113 fprintf(stderr, "%s (based on NetBSD gzip 20111009)\n", gzip_version);
2114 fprintf(stderr, "%s\n", gzip_copyright);
2115 exit(0);
2116}
2117#endif
2118
2119/* display the version of NetBSD gzip */
2120static void
2121display_version(void)

--- 7 unchanged lines hidden (view full) ---

2129#include "unbzip2.c"
2130#endif
2131#ifndef NO_COMPRESS_SUPPORT
2132#include "zuncompress.c"
2133#endif
2134#ifndef NO_PACK_SUPPORT
2135#include "unpack.c"
2136#endif
2137#ifndef NO_XZ_SUPPORT
2138#include "unxz.c"
2139#endif
2140
2141static ssize_t
2142read_retry(int fd, void *buf, size_t sz)
2143{
2144 char *cp = buf;
2145 size_t left = MIN(sz, (size_t) SSIZE_MAX);
2146
2147 while (left > 0) {

--- 14 unchanged lines hidden ---