gzip.c (34e314e794c323387e9423037c7715fdbe27915f) gzip.c (5c4b64e66b7f93035d3655b6c11b3ddcc18cd8f0)
1/* $NetBSD: gzip.c,v 1.113 2018/06/12 00:42:17 kamil Exp $ */
1/* $NetBSD: gzip.c,v 1.116 2018/10/27 11:39:12 skrll Exp $ */
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 *
6 * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
7 * Matthew R. Green
8 * All rights reserved.
9 *

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

79 FT_Z,
80#endif
81#ifndef NO_PACK_SUPPORT
82 FT_PACK,
83#endif
84#ifndef NO_XZ_SUPPORT
85 FT_XZ,
86#endif
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 *
6 * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
7 * Matthew R. Green
8 * All rights reserved.
9 *

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

79 FT_Z,
80#endif
81#ifndef NO_PACK_SUPPORT
82 FT_PACK,
83#endif
84#ifndef NO_XZ_SUPPORT
85 FT_XZ,
86#endif
87#ifndef NO_LZ_SUPPORT
88 FT_LZ,
89#endif
87 FT_LAST,
88 FT_UNKNOWN
89};
90
91#ifndef NO_BZIP2_SUPPORT
92#include <bzlib.h>
93
94#define BZ2_SUFFIX ".bz2"

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

105#endif
106
107#ifndef NO_XZ_SUPPORT
108#include <lzma.h>
109#define XZ_SUFFIX ".xz"
110#define XZ_MAGIC "\3757zXZ"
111#endif
112
90 FT_LAST,
91 FT_UNKNOWN
92};
93
94#ifndef NO_BZIP2_SUPPORT
95#include <bzlib.h>
96
97#define BZ2_SUFFIX ".bz2"

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

108#endif
109
110#ifndef NO_XZ_SUPPORT
111#include <lzma.h>
112#define XZ_SUFFIX ".xz"
113#define XZ_MAGIC "\3757zXZ"
114#endif
115
116#ifndef NO_LZ_SUPPORT
117#define LZ_SUFFIX ".lz"
118#define LZ_MAGIC "LZIP"
119#endif
120
113#define GZ_SUFFIX ".gz"
114
115#define BUFLEN (64 * 1024)
116
117#define GZIP_MAGIC0 0x1F
118#define GZIP_MAGIC1 0x8B
119#define GZIP_OMAGIC1 0x9E
120

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

150 SUFFIX(".tbz2", ".tar"),
151#endif
152#ifndef NO_COMPRESS_SUPPORT
153 SUFFIX(Z_SUFFIX, ""),
154#endif
155#ifndef NO_XZ_SUPPORT
156 SUFFIX(XZ_SUFFIX, ""),
157#endif
121#define GZ_SUFFIX ".gz"
122
123#define BUFLEN (64 * 1024)
124
125#define GZIP_MAGIC0 0x1F
126#define GZIP_MAGIC1 0x8B
127#define GZIP_OMAGIC1 0x9E
128

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

158 SUFFIX(".tbz2", ".tar"),
159#endif
160#ifndef NO_COMPRESS_SUPPORT
161 SUFFIX(Z_SUFFIX, ""),
162#endif
163#ifndef NO_XZ_SUPPORT
164 SUFFIX(XZ_SUFFIX, ""),
165#endif
166#ifndef NO_LZ_SUPPORT
167 SUFFIX(LZ_SUFFIX, ""),
168#endif
158 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
159#endif /* SMALL */
160#undef SUFFIX
161};
162#define NUM_SUFFIXES (nitems(suffixes))
163#define SUFFIX_MAXLEN 30
164
169 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
170#endif /* SMALL */
171#undef SUFFIX
172};
173#define NUM_SUFFIXES (nitems(suffixes))
174#define SUFFIX_MAXLEN 30
175
165static const char gzip_version[] = "FreeBSD gzip 20171121";
176static const char gzip_version[] = "FreeBSD gzip 20190107";
166
167#ifndef SMALL
168static const char gzip_copyright[] = \
169" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
170" All rights reserved.\n"
171"\n"
172" Redistribution and use in source and binary forms, with or without\n"
173" modification, are permitted provided that the following conditions\n"

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

241static void usage(void) __dead2;
242static void display_version(void) __dead2;
243#ifndef SMALL
244static void display_license(void);
245#endif
246static const suffixes_t *check_suffix(char *, int);
247static ssize_t read_retry(int, void *, size_t);
248static ssize_t write_retry(int, const void *, size_t);
177
178#ifndef SMALL
179static const char gzip_copyright[] = \
180" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
181" All rights reserved.\n"
182"\n"
183" Redistribution and use in source and binary forms, with or without\n"
184" modification, are permitted provided that the following conditions\n"

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

252static void usage(void) __dead2;
253static void display_version(void) __dead2;
254#ifndef SMALL
255static void display_license(void);
256#endif
257static const suffixes_t *check_suffix(char *, int);
258static ssize_t read_retry(int, void *, size_t);
259static ssize_t write_retry(int, const void *, size_t);
260static void print_list_out(off_t, off_t, const char*);
249
250#ifdef SMALL
251#define infile_set(f,t) infile_set(f)
252#endif
253static void infile_set(const char *newinfile, off_t total);
254
255#ifdef SMALL
256#define unlink_input(f, sb) unlink(f)

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

284#endif
285
286#ifndef NO_PACK_SUPPORT
287static off_t unpack(int, int, char *, size_t, off_t *);
288#endif
289
290#ifndef NO_XZ_SUPPORT
291static off_t unxz(int, int, char *, size_t, off_t *);
261
262#ifdef SMALL
263#define infile_set(f,t) infile_set(f)
264#endif
265static void infile_set(const char *newinfile, off_t total);
266
267#ifdef SMALL
268#define unlink_input(f, sb) unlink(f)

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

296#endif
297
298#ifndef NO_PACK_SUPPORT
299static off_t unpack(int, int, char *, size_t, off_t *);
300#endif
301
302#ifndef NO_XZ_SUPPORT
303static off_t unxz(int, int, char *, size_t, off_t *);
304static off_t unxz_len(int);
292#endif
293
305#endif
306
307#ifndef NO_LZ_SUPPORT
308static off_t unlz(int, int, char *, size_t, off_t *);
309#endif
310
294#ifdef SMALL
295#define getopt_long(a,b,c,d,e) getopt(a,b,c)
296#else
297static const struct option longopts[] = {
298 { "stdout", no_argument, 0, 'c' },
299 { "to-stdout", no_argument, 0, 'c' },
300 { "decompress", no_argument, 0, 'd' },
301 { "uncompress", no_argument, 0, 'd' },

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

1154 return FT_PACK;
1155 else
1156#endif
1157#ifndef NO_XZ_SUPPORT
1158 if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */
1159 return FT_XZ;
1160 else
1161#endif
311#ifdef SMALL
312#define getopt_long(a,b,c,d,e) getopt(a,b,c)
313#else
314static const struct option longopts[] = {
315 { "stdout", no_argument, 0, 'c' },
316 { "to-stdout", no_argument, 0, 'c' },
317 { "decompress", no_argument, 0, 'd' },
318 { "uncompress", no_argument, 0, 'd' },

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

1171 return FT_PACK;
1172 else
1173#endif
1174#ifndef NO_XZ_SUPPORT
1175 if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */
1176 return FT_XZ;
1177 else
1178#endif
1179#ifndef NO_LZ_SUPPORT
1180 if (memcmp(buf, LZ_MAGIC, 4) == 0)
1181 return FT_LZ;
1182 else
1183#endif
1162 return FT_UNKNOWN;
1163}
1164
1165#ifndef SMALL
1166/* check the outfile is OK. */
1167static int
1168check_outfile(const char *outfile)
1169{

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

1627
1628 size = unpack(fd, zfd, NULL, 0, NULL);
1629 break;
1630#endif
1631
1632#ifndef NO_XZ_SUPPORT
1633 case FT_XZ:
1634 if (lflag) {
1184 return FT_UNKNOWN;
1185}
1186
1187#ifndef SMALL
1188/* check the outfile is OK. */
1189static int
1190check_outfile(const char *outfile)
1191{

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

1649
1650 size = unpack(fd, zfd, NULL, 0, NULL);
1651 break;
1652#endif
1653
1654#ifndef NO_XZ_SUPPORT
1655 case FT_XZ:
1656 if (lflag) {
1635 maybe_warnx("no -l with xz files");
1636 goto lose;
1657 size = unxz_len(fd);
1658 print_list_out(in_size, size, file);
1659 return -1;
1637 }
1660 }
1638
1639 size = unxz(fd, zfd, NULL, 0, NULL);
1640 break;
1641#endif
1642
1661 size = unxz(fd, zfd, NULL, 0, NULL);
1662 break;
1663#endif
1664
1665#ifndef NO_LZ_SUPPORT
1666 case FT_LZ:
1667 if (lflag) {
1668 maybe_warnx("no -l with lzip files");
1669 goto lose;
1670 }
1671 size = unlz(fd, zfd, NULL, 0, NULL);
1672 break;
1673#endif
1643#ifndef SMALL
1644 case FT_UNKNOWN:
1645 if (lflag) {
1646 maybe_warnx("no -l for unknown filetypes");
1647 goto lose;
1648 }
1649 size = cat_fd(NULL, 0, NULL, fd);
1650 break;

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

1867 break;
1868#endif
1869#ifndef NO_XZ_SUPPORT
1870 case FT_XZ:
1871 usize = unxz(STDIN_FILENO, STDOUT_FILENO,
1872 (char *)header1, sizeof header1, &gsize);
1873 break;
1874#endif
1674#ifndef SMALL
1675 case FT_UNKNOWN:
1676 if (lflag) {
1677 maybe_warnx("no -l for unknown filetypes");
1678 goto lose;
1679 }
1680 size = cat_fd(NULL, 0, NULL, fd);
1681 break;

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

1898 break;
1899#endif
1900#ifndef NO_XZ_SUPPORT
1901 case FT_XZ:
1902 usize = unxz(STDIN_FILENO, STDOUT_FILENO,
1903 (char *)header1, sizeof header1, &gsize);
1904 break;
1905#endif
1906#ifndef NO_LZ_SUPPORT
1907 case FT_LZ:
1908 usize = unlz(STDIN_FILENO, STDOUT_FILENO,
1909 (char *)header1, sizeof header1, &gsize);
1910 break;
1911#endif
1875 }
1876
1877#ifndef SMALL
1878 if (vflag && !tflag && usize != -1 && gsize != -1)
1879 print_verbage(NULL, NULL, usize, gsize);
1880 if (vflag && tflag)
1881 print_test("(stdin)", usize != -1);
1882#else

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

2192 date[12] = 0;
2193 printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
2194 }
2195 in_tot += in;
2196 out_tot += out;
2197#else
2198 (void)&ts; /* XXX */
2199#endif
1912 }
1913
1914#ifndef SMALL
1915 if (vflag && !tflag && usize != -1 && gsize != -1)
1916 print_verbage(NULL, NULL, usize, gsize);
1917 if (vflag && tflag)
1918 print_test("(stdin)", usize != -1);
1919#else

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

2229 date[12] = 0;
2230 printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
2231 }
2232 in_tot += in;
2233 out_tot += out;
2234#else
2235 (void)&ts; /* XXX */
2236#endif
2237 print_list_out(out, in, outfile);
2238}
2239
2240static void
2241print_list_out(off_t out, off_t in, const char *outfile)
2242{
2200 printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
2201 print_ratio(in, out, stdout);
2202 printf(" %s\n", outfile);
2203}
2204
2205/* display the usage of NetBSD gzip */
2206static void
2207usage(void)

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

2266#include "zuncompress.c"
2267#endif
2268#ifndef NO_PACK_SUPPORT
2269#include "unpack.c"
2270#endif
2271#ifndef NO_XZ_SUPPORT
2272#include "unxz.c"
2273#endif
2243 printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
2244 print_ratio(in, out, stdout);
2245 printf(" %s\n", outfile);
2246}
2247
2248/* display the usage of NetBSD gzip */
2249static void
2250usage(void)

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

2309#include "zuncompress.c"
2310#endif
2311#ifndef NO_PACK_SUPPORT
2312#include "unpack.c"
2313#endif
2314#ifndef NO_XZ_SUPPORT
2315#include "unxz.c"
2316#endif
2317#ifndef NO_LZ_SUPPORT
2318#include "unlz.c"
2319#endif
2274
2275static ssize_t
2276read_retry(int fd, void *buf, size_t sz)
2277{
2278 char *cp = buf;
2279 size_t left = MIN(sz, (size_t) SSIZE_MAX);
2280
2281 while (left > 0) {

--- 36 unchanged lines hidden ---
2320
2321static ssize_t
2322read_retry(int fd, void *buf, size_t sz)
2323{
2324 char *cp = buf;
2325 size_t left = MIN(sz, (size_t) SSIZE_MAX);
2326
2327 while (left > 0) {

--- 36 unchanged lines hidden ---