gzip.c (222287) | gzip.c (226184) |
---|---|
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."); | 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 | 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 |
|
80 FT_LAST, 81 FT_UNKNOWN 82}; 83 84#ifndef NO_BZIP2_SUPPORT 85#include <bzlib.h> 86 87#define BZ2_SUFFIX ".bz2" --- 4 unchanged lines hidden (view full) --- 92#define Z_SUFFIX ".Z" 93#define Z_MAGIC "\037\235" 94#endif 95 96#ifndef NO_PACK_SUPPORT 97#define PACK_MAGIC "\037\036" 98#endif 99 | 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 |
|
100#define GZ_SUFFIX ".gz" 101 102#define BUFLEN (64 * 1024) 103 104#define GZIP_MAGIC0 0x1F 105#define GZIP_MAGIC1 0x8B 106#define GZIP_OMAGIC1 0x9E 107 --- 26 unchanged lines hidden (view full) --- 134#ifndef NO_BZIP2_SUPPORT 135 SUFFIX(BZ2_SUFFIX, ""), 136 SUFFIX(".tbz", ".tar"), 137 SUFFIX(".tbz2", ".tar"), 138#endif 139#ifndef NO_COMPRESS_SUPPORT 140 SUFFIX(Z_SUFFIX, ""), 141#endif | 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 |
|
142 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */ 143#endif /* SMALL */ 144#undef SUFFIX 145}; 146#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0]) 147#define SUFFIX_MAXLEN 30 148 | 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"; |
150 151#ifndef SMALL 152static const char gzip_copyright[] = \ 153" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n" 154" All rights reserved.\n" 155"\n" 156" Redistribution and use in source and binary forms, with or without\n" 157" modification, are permitted provided that the following conditions\n" --- 36 unchanged lines hidden (view full) --- 194#define qflag 0 195#define tflag 0 196#endif 197 198static int exit_value = 0; /* exit value */ 199 200static char *infile; /* name of file coming in */ 201 | 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; |
207#endif | 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); |
212static enum filetype file_gettype(u_char *); 213#ifdef SMALL 214#define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz) 215#endif 216static off_t gz_compress(int, int, off_t *, const char *, uint32_t); 217static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *); 218static off_t file_compress(char *, char *, size_t); 219static off_t file_uncompress(char *, char *, size_t); 220static void handle_pathname(char *); 221static void handle_file(char *, struct stat *); 222static void handle_stdin(void); 223static void handle_stdout(void); 224static void print_ratio(off_t, off_t, FILE *); 225static void print_list(int fd, off_t, const char *, time_t); | 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; |
228#ifndef SMALL 229static void display_license(void); 230static void sigint_handler(int); 231#endif 232static const suffixes_t *check_suffix(char *, int); 233static ssize_t read_retry(int, void *, size_t); 234 235#ifdef SMALL --- 16 unchanged lines hidden (view full) --- 252static FILE *zdopen(int); 253static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *); 254#endif 255 256#ifndef NO_PACK_SUPPORT 257static off_t unpack(int, int, char *, size_t, off_t *); 258#endif 259 | 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 |
261 262#ifdef SMALL 263#define getopt_long(a,b,c,d,e) getopt(a,b,c) 264#else 265static const struct option longopts[] = { 266 { "stdout", no_argument, 0, 'c' }, 267 { "to-stdout", no_argument, 0, 'c' }, 268 { "decompress", no_argument, 0, 'd' }, --- 182 unchanged lines hidden (view full) --- 451 if (qflag == 0) { 452 va_start(ap, fmt); 453 vwarn(fmt, ap); 454 va_end(ap); 455 } 456 exit(2); 457} 458 | 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) |
460/* ... without an errno. */ 461void 462maybe_errx(const char *fmt, ...) 463{ 464 va_list ap; 465 466 if (qflag == 0) { 467 va_start(ap, fmt); --- 649 unchanged lines hidden (view full) --- 1117 return FT_Z; 1118 else 1119#endif 1120#ifndef NO_PACK_SUPPORT 1121 if (memcmp(buf, PACK_MAGIC, 2) == 0) 1122 return FT_PACK; 1123 else 1124#endif | 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 |
|
1125 return FT_UNKNOWN; 1126} 1127 1128#ifndef SMALL 1129/* check the outfile is OK. */ 1130static int 1131check_outfile(const char *outfile) 1132{ --- 231 unchanged lines hidden (view full) --- 1364 if (rbytes == -1) 1365 maybe_warn("can't read %s", file); 1366 else 1367 goto unexpected_EOF; 1368 goto lose; 1369 } 1370 1371 method = file_gettype(header1); | 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 | |
1373#ifndef SMALL 1374 if (fflag == 0 && method == FT_UNKNOWN) { 1375 maybe_warnx("%s: not in gzip format", file); 1376 goto lose; 1377 } 1378 1379#endif 1380 --- 61 unchanged lines hidden (view full) --- 1442 goto lose; 1443 } 1444#ifndef SMALL 1445 remove_file = outfile; 1446#endif 1447 } else 1448 zfd = STDOUT_FILENO; 1449 | 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) { |
|
1450#ifndef NO_BZIP2_SUPPORT | 1467#ifndef NO_BZIP2_SUPPORT |
1451 if (method == FT_BZIP2) { 1452 | 1468 case FT_BZIP2: |
1453 /* XXX */ 1454 if (lflag) { 1455 maybe_warnx("no -l with bzip2 files"); 1456 goto lose; 1457 } 1458 1459 size = unbzip2(fd, zfd, NULL, 0, NULL); | 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; |
1461#endif 1462 1463#ifndef NO_COMPRESS_SUPPORT | 1477#endif 1478 1479#ifndef NO_COMPRESS_SUPPORT |
1464 if (method == FT_Z) { | 1480 case FT_Z: { |
1465 FILE *in, *out; 1466 1467 /* XXX */ 1468 if (lflag) { 1469 maybe_warnx("no -l with Lempel-Ziv files"); 1470 goto lose; 1471 } 1472 --- 16 unchanged lines hidden (view full) --- 1489 unlink(outfile); 1490 (void)fclose(out); 1491 } 1492 if (fclose(out) != 0) { 1493 maybe_warn("failed outfile fclose"); 1494 unlink(outfile); 1495 goto lose; 1496 } | 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 } |
1498#endif 1499 1500#ifndef NO_PACK_SUPPORT | 1515#endif 1516 1517#ifndef NO_PACK_SUPPORT |
1501 if (method == FT_PACK) { | 1518 case FT_PACK: |
1502 if (lflag) { 1503 maybe_warnx("no -l with packed files"); 1504 goto lose; 1505 } 1506 1507 size = unpack(fd, zfd, NULL, 0, NULL); | 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; |
1509#endif 1510 | 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 |
|
1511#ifndef SMALL | 1539#ifndef SMALL |
1512 if (method == FT_UNKNOWN) { | 1540 case FT_UNKNOWN: |
1513 if (lflag) { 1514 maybe_warnx("no -l for unknown filetypes"); 1515 goto lose; 1516 } 1517 size = cat_fd(NULL, 0, NULL, fd); | 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; |
1519#endif | 1547#endif |
1520 { | 1548 default: |
1521 if (lflag) { 1522 print_list(fd, isb.st_size, outfile, isb.st_mtime); 1523 close(fd); 1524 return -1; /* XXX */ 1525 } 1526 1527 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file); | 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; |
|
1528 } 1529 1530 if (close(fd) != 0) 1531 maybe_warn("couldn't close input"); 1532 if (zfd != STDOUT_FILENO && close(zfd) != 0) 1533 maybe_warn("couldn't close output"); 1534 1535 if (size == -1) { --- 156 unchanged lines hidden (view full) --- 1692#endif 1693#ifndef NO_COMPRESS_SUPPORT 1694 case FT_Z: 1695 if ((in = zdopen(STDIN_FILENO)) == NULL) { 1696 maybe_warnx("zopen of stdin"); 1697 return; 1698 } 1699 | 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); |
1701 fclose(in); 1702 break; 1703#endif 1704#ifndef NO_PACK_SUPPORT 1705 case FT_PACK: 1706 usize = unpack(STDIN_FILENO, STDOUT_FILENO, 1707 (char *)header1, sizeof header1, &gsize); 1708 break; 1709#endif | 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 |
|
1710 } 1711 1712#ifndef SMALL 1713 if (vflag && !tflag && usize != -1 && gsize != -1) 1714 print_verbage(NULL, NULL, usize, gsize); 1715 if (vflag && tflag) 1716 print_test("(stdin)", usize != -1); 1717#endif --- 351 unchanged lines hidden (view full) --- 2069} 2070 2071#ifndef SMALL 2072/* display the license information of FreeBSD gzip */ 2073static void 2074display_license(void) 2075{ 2076 | 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); |
2078 fprintf(stderr, "%s\n", gzip_copyright); 2079 exit(0); 2080} 2081#endif 2082 2083/* display the version of NetBSD gzip */ 2084static void 2085display_version(void) --- 7 unchanged lines hidden (view full) --- 2093#include "unbzip2.c" 2094#endif 2095#ifndef NO_COMPRESS_SUPPORT 2096#include "zuncompress.c" 2097#endif 2098#ifndef NO_PACK_SUPPORT 2099#include "unpack.c" 2100#endif | 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 |
|
2101 2102static ssize_t 2103read_retry(int fd, void *buf, size_t sz) 2104{ 2105 char *cp = buf; 2106 size_t left = MIN(sz, (size_t) SSIZE_MAX); 2107 2108 while (left > 0) { --- 14 unchanged lines hidden --- | 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 --- |