svn commit: r226184 - head/usr.bin/gzip

Xin LI delphij at FreeBSD.org
Mon Oct 10 06:37:32 UTC 2011


Author: delphij
Date: Mon Oct 10 06:37:32 2011
New Revision: 226184
URL: http://svn.freebsd.org/changeset/base/226184

Log:
  Incorporate recent changes from NetBSD.  Most notable change is the addition
  of support of decompressing xz files.
  
  Obtained from:	NetBSD

Added:
  head/usr.bin/gzip/unxz.c   (contents, props changed)
Modified:
  head/usr.bin/gzip/Makefile
  head/usr.bin/gzip/gzip.1
  head/usr.bin/gzip/gzip.c

Modified: head/usr.bin/gzip/Makefile
==============================================================================
--- head/usr.bin/gzip/Makefile	Mon Oct 10 06:13:27 2011	(r226183)
+++ head/usr.bin/gzip/Makefile	Mon Oct 10 06:37:32 2011	(r226184)
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.13 2009/04/14 22:15:20 lukem Exp $
+#	$NetBSD: Makefile,v 1.16 2011/06/21 13:25:45 joerg Exp $
 # $FreeBSD$
 
 .include <bsd.own.mk>
@@ -6,8 +6,8 @@
 PROG=		gzip
 MAN=		gzip.1 gzexe.1 zdiff.1 zforce.1 zmore.1 znew.1
 
-DPADD=		${LIBZ}
-LDADD=		-lz
+DPADD=		${LIBZ} ${LIBLZMA}
+LDADD=		-lz -llzma
 
 .if ${MK_BZIP2_SUPPORT} != "no"
 DPADD+=		${LIBBZ2}

Modified: head/usr.bin/gzip/gzip.1
==============================================================================
--- head/usr.bin/gzip/gzip.1	Mon Oct 10 06:13:27 2011	(r226183)
+++ head/usr.bin/gzip/gzip.1	Mon Oct 10 06:37:32 2011	(r226184)
@@ -1,4 +1,4 @@
-.\"	$NetBSD: gzip.1,v 1.20 2009/04/01 08:15:37 mrg Exp $
+.\"	$NetBSD: gzip.1,v 1.21 2011/06/19 02:22:36 christos Exp $
 .\"
 .\" Copyright (c) 1997, 2003, 2004 Matthew R. Green
 .\" All rights reserved.
@@ -25,7 +25,7 @@
 .\" SUCH DAMAGE.
 .\"
 .\" $FreeBSD$
-.Dd May 23, 2011
+.Dd October 9, 2011
 .Dt GZIP 1
 .Os
 .Sh NAME
@@ -185,6 +185,7 @@ Options on the command line will overrid
 .Sh SEE ALSO
 .Xr bzip2 1 ,
 .Xr compress 1 ,
+.Xr xz 1 ,
 .Xr fts 3 ,
 .Xr zlib 3
 .Sh HISTORY

Modified: head/usr.bin/gzip/gzip.c
==============================================================================
--- head/usr.bin/gzip/gzip.c	Mon Oct 10 06:13:27 2011	(r226183)
+++ head/usr.bin/gzip/gzip.c	Mon Oct 10 06:37:32 2011	(r226184)
@@ -1,4 +1,4 @@
-/*	$NetBSD: gzip.c,v 1.99 2011/03/23 12:59:44 tsutsui Exp $	*/
+/*	$NetBSD: gzip.c,v 1.105 2011/08/30 23:06:00 joerg Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
@@ -77,6 +77,9 @@ enum filetype {
 #ifndef NO_PACK_SUPPORT
 	FT_PACK,
 #endif
+#ifndef NO_XZ_SUPPORT
+	FT_XZ,
+#endif
 	FT_LAST,
 	FT_UNKNOWN
 };
@@ -97,6 +100,12 @@ enum filetype {
 #define PACK_MAGIC	"\037\036"
 #endif
 
+#ifndef NO_XZ_SUPPORT
+#include <lzma.h>
+#define XZ_SUFFIX	".xz"
+#define XZ_MAGIC	"\3757zXZ"
+#endif
+
 #define GZ_SUFFIX	".gz"
 
 #define BUFLEN		(64 * 1024)
@@ -139,6 +148,9 @@ static suffixes_t suffixes[] = {
 #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
@@ -146,7 +158,7 @@ static suffixes_t suffixes[] = {
 #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
 #define SUFFIX_MAXLEN	30
 
-static	const char	gzip_version[] = "FreeBSD gzip 20110523";
+static	const char	gzip_version[] = "FreeBSD gzip 20111009";
 
 #ifndef SMALL
 static	const char	gzip_copyright[] = \
@@ -199,16 +211,13 @@ static	int	exit_value = 0;		/* exit valu
 
 static	char	*infile;		/* name of file coming in */
 
-static	void	maybe_err(const char *fmt, ...) __dead2
-    __attribute__((__format__(__printf__, 1, 2)));
-#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
-static	void	maybe_errx(const char *fmt, ...) __dead2
-    __attribute__((__format__(__printf__, 1, 2)));
-#endif
-static	void	maybe_warn(const char *fmt, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
-static	void	maybe_warnx(const char *fmt, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
+static	void	maybe_err(const char *fmt, ...) __printflike(1, 2) __dead2;
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) ||	\
+    !defined(NO_XZ_SUPPORT)
+static	void	maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead2;
+#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)
@@ -223,8 +232,8 @@ 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);
-static	void	usage(void);
-static	void	display_version(void);
+static	void	usage(void) __dead2;
+static	void	display_version(void) __dead2;
 #ifndef SMALL
 static	void	display_license(void);
 static	void	sigint_handler(int);
@@ -257,7 +266,9 @@ static	off_t	zuncompress(FILE *, FILE *,
 static	off_t	unpack(int, int, char *, size_t, off_t *);
 #endif
 
-int main(int, char **p);
+#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)
@@ -456,7 +467,8 @@ maybe_err(const char *fmt, ...)
 	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, ...)
@@ -1122,6 +1134,11 @@ file_gettype(u_char *buf)
 		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
+#endif
 		return FT_UNKNOWN;
 }
 
@@ -1369,7 +1386,6 @@ file_uncompress(char *file, char *outfil
 	}
 
 	method = file_gettype(header1);
-
 #ifndef SMALL
 	if (fflag == 0 && method == FT_UNKNOWN) {
 		maybe_warnx("%s: not in gzip format", file);
@@ -1447,9 +1463,9 @@ file_uncompress(char *file, char *outfil
 	} else
 		zfd = STDOUT_FILENO;
 
+	switch (method) {
 #ifndef NO_BZIP2_SUPPORT
-	if (method == FT_BZIP2) {
-
+	case FT_BZIP2:
 		/* XXX */
 		if (lflag) {
 			maybe_warnx("no -l with bzip2 files");
@@ -1457,11 +1473,11 @@ file_uncompress(char *file, char *outfil
 		}
 
 		size = unbzip2(fd, zfd, NULL, 0, NULL);
-	} else
+		break;
 #endif
 
 #ifndef NO_COMPRESS_SUPPORT
-	if (method == FT_Z) {
+	case FT_Z: {
 		FILE *in, *out;
 
 		/* XXX */
@@ -1494,30 +1510,42 @@ file_uncompress(char *file, char *outfil
 			unlink(outfile);
 			goto lose;
 		}
-	} else
+		break;
+	}
 #endif
 
 #ifndef NO_PACK_SUPPORT
-	if (method == FT_PACK) {
+	case FT_PACK:
 		if (lflag) {
 			maybe_warnx("no -l with packed files");
 			goto lose;
 		}
 
 		size = unpack(fd, zfd, NULL, 0, NULL);
-	} else
+		break;
+#endif
+
+#ifndef NO_XZ_SUPPORT
+	case FT_XZ:
+		if (lflag) {
+			maybe_warnx("no -l with xz files");
+			goto lose;
+		}
+
+		size = unxz(fd, zfd, NULL, 0, NULL);
+		break;
 #endif
 
 #ifndef SMALL
-	if (method == FT_UNKNOWN) {
+	case FT_UNKNOWN:
 		if (lflag) {
 			maybe_warnx("no -l for unknown filetypes");
 			goto lose;
 		}
 		size = cat_fd(NULL, 0, NULL, fd);
-	} else
+		break;
 #endif
-	{
+	default:
 		if (lflag) {
 			print_list(fd, isb.st_size, outfile, isb.st_mtime);
 			close(fd);
@@ -1525,6 +1553,7 @@ file_uncompress(char *file, char *outfil
 		}
 
 		size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
+		break;
 	}
 
 	if (close(fd) != 0)
@@ -1697,7 +1726,8 @@ handle_stdin(void)
 			return;
 		}
 
-		usize = zuncompress(in, stdout, (char *)header1, sizeof header1, &gsize);
+		usize = zuncompress(in, stdout, (char *)header1,
+		    sizeof header1, &gsize);
 		fclose(in);
 		break;
 #endif
@@ -1707,6 +1737,12 @@ handle_stdin(void)
 			       (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;
+#endif
 	}
 
 #ifndef SMALL
@@ -2074,7 +2110,7 @@ static void
 display_license(void)
 {
 
-	fprintf(stderr, "%s (based on NetBSD gzip 20091011)\n", gzip_version);
+	fprintf(stderr, "%s (based on NetBSD gzip 20111009)\n", gzip_version);
 	fprintf(stderr, "%s\n", gzip_copyright);
 	exit(0);
 }
@@ -2098,6 +2134,9 @@ display_version(void)
 #ifndef NO_PACK_SUPPORT
 #include "unpack.c"
 #endif
+#ifndef NO_XZ_SUPPORT
+#include "unxz.c"
+#endif
 
 static ssize_t
 read_retry(int fd, void *buf, size_t sz)

Added: head/usr.bin/gzip/unxz.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.bin/gzip/unxz.c	Mon Oct 10 06:37:32 2011	(r226184)
@@ -0,0 +1,160 @@
+/*	$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    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
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD 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
+ * ``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
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <stdarg.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <lzma.h>
+
+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;
+
+	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);
+
+	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);
+
+	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);
+
+		// 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");
+
+			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;
+				}
+			}
+
+			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_OPTIONS_ERROR:
+				// FIXME: Better message?
+				msg = "Unsupported compression options";
+				break;
+
+			case LZMA_DATA_ERROR:
+				msg = "File is corrupt";
+				break;
+
+			case LZMA_BUF_ERROR:
+				msg = "Unexpected end of input";
+				break;
+
+			case LZMA_MEMLIMIT_ERROR:
+				msg = "Reached memory limit";
+				break;
+
+			default:
+				maybe_errx("Unknown error (%d)", ret);
+				break;
+			}
+			maybe_errx("%s", msg);
+
+		}
+	}
+}


More information about the svn-src-head mailing list