Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 04 Jul 2001 19:08:15 +0900
From:      "Akinori MUSHA" <knu@iDaemons.org>
To:        obrien@FreeBSD.org
Cc:        audit@FreeBSD.org
Subject:   a patch to add bunzip2'ing functionality to gzip(1)
Message-ID:  <8666d9dn1s.wl@archon.local.idaemons.org>

next in thread | raw e-mail | index | archive | help
Hi,

Here is a patch to add bunzip2'ing functionality to our gzip(1), which
I obtained from NetBSD.  Now that bzip2 is in the base, why don't we
make gzip(1) grok how to handle .bz2 files? :)

-- 
                     /
                    /__  __            Akinori.org / MUSHA.org
                   / )  )  ) )  /     FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ /  ( (__(  @ iDaemons.org / and.or.jp

"Freeze this moment a little bit longer, make each impression
  a little bit stronger..  Experience slips away -- Time stand still"

M share/mk/bsd.libnames.mk
M gnu/usr.bin/gzip/Makefile
M gnu/usr.bin/gzip/gzip.1
M gnu/usr.bin/gzip/gzip.c
M gnu/usr.bin/gzip/gzip.h
A gnu/usr.bin/gzip/unbzip2.c

Index: share/mk/bsd.libnames.mk
===================================================================
RCS file: /home/ncvs/src/share/mk/bsd.libnames.mk,v
retrieving revision 1.43
diff -u -r1.43 bsd.libnames.mk
--- share/mk/bsd.libnames.mk	2001/06/07 08:31:30	1.43
+++ share/mk/bsd.libnames.mk	2001/07/04 09:55:42
@@ -10,6 +10,7 @@
 LIBALIAS?=	${DESTDIR}${LIBDIR}/libalias.a
 LIBASN1?=	${DESTDIR}${LIBDIR}/libasn1.a	# XXX in secure dist, not base
 LIBATM?=	${DESTDIR}${LIBDIR}/libatm.a
+LIBBZ2?=	${DESTDIR}${LIBDIR}/libbz2.a
 LIBC?=		${DESTDIR}${LIBDIR}/libc.a
 LIBC_PIC?=	${DESTDIR}${LIBDIR}/libc_pic.a
 LIBC_R?=	${DESTDIR}${LIBDIR}/libc_r.a
Index: gnu/usr.bin/gzip/Makefile
===================================================================
RCS file: /home/ncvs/src/gnu/usr.bin/gzip/Makefile,v
retrieving revision 1.23
diff -u -r1.23 Makefile
--- gnu/usr.bin/gzip/Makefile	2001/04/07 11:21:17	1.23
+++ gnu/usr.bin/gzip/Makefile	2001/07/04 09:38:56
@@ -3,7 +3,7 @@
 
 PROG=	gzip
 SRCS=	gzip.c zip.c deflate.c trees.c bits.c unzip.c inflate.c util.c \
-	crypt.c lzw.c unlzw.c unlzh.c unpack.c getopt.c
+	crypt.c lzw.c unbzip2.c unlzw.c unlzh.c unpack.c getopt.c
 CFLAGS+=-DSTDC_HEADERS=1 -DHAVE_UNISTD_H=1 -DDIRENT=1
 .if ${MACHINE_ARCH} == "i386"
 SRCS+=	match.S
@@ -13,6 +13,9 @@
 MAN=	gzexe.1 gzip.1 zdiff.1 zforce.1 zmore.1 znew.1
 
 NOSHARED?=yes
+
+LDADD+=	-lbz2
+DPADD+=	${LIBBZ2}
 
 LINKS+= ${BINDIR}/gzip ${BINDIR}/gunzip
 LINKS+= ${BINDIR}/gzip ${BINDIR}/gzcat
Index: gnu/usr.bin/gzip/gzip.1
===================================================================
RCS file: /home/ncvs/src/gnu/usr.bin/gzip/gzip.1,v
retrieving revision 1.7
diff -u -r1.7 gzip.1
--- gnu/usr.bin/gzip/gzip.1	1999/09/20 09:15:15	1.7
+++ gnu/usr.bin/gzip/gzip.1	2001/07/04 09:42:11
@@ -98,11 +98,11 @@
 .PP
 .I gunzip
 can currently decompress files created by
-.I gzip, zip, compress, compress -H
+.I gzip, bzip2, zip, compress, compress -H
 or
 .I pack.
 The detection of the input format is automatic.  When using
-the first two formats,
+the first three formats,
 .I gunzip
 checks a 32 bit CRC. For
 .I pack, gunzip
@@ -369,7 +369,8 @@
 On Vax/VMS, the name of the environment variable is GZIP_OPT, to
 avoid a conflict with the symbol set for invocation of the program.
 .SH "SEE ALSO"
-znew(1), zcmp(1), zmore(1), zforce(1), gzexe(1), compress(1)
+znew(1), zcmp(1), zmore(1), zforce(1), gzexe(1), zip(1), unzip(1),
+bzip2(1), compress(1), pack(1)
 .SH "DIAGNOSTICS"
 Exit status is normally 0;
 if an error occurs, exit status is 1. If a warning occurs, exit status is 2.
Index: gnu/usr.bin/gzip/gzip.c
===================================================================
RCS file: /home/ncvs/src/gnu/usr.bin/gzip/gzip.c,v
retrieving revision 1.10
diff -u -r1.10 gzip.c
--- gnu/usr.bin/gzip/gzip.c	1999/08/27 23:35:50	1.10
+++ gnu/usr.bin/gzip/gzip.c	2001/07/04 09:44:43
@@ -957,7 +957,7 @@
     int nlen, slen;
     char suffix[MAX_SUFFIX+3]; /* last chars of name, forced to lower case */
     static char *known_suffixes[] =
-       {z_suffix, ".gz", ".z", ".taz", ".tgz", "-gz", "-z", "_z",
+       {z_suffix, ".gz", ".z", ".taz", ".tgz", "-gz", "-z", "_z", ".bz2",
 #ifdef MAX_EXT_CHARS
           "z",
 #endif
@@ -1291,6 +1291,14 @@
 	/* check_zipfile may get ofname from the local header */
 	last_member = 1;
 
+    } else if (memcmp(magic, BZIP2_MAGIC, 2) == 0 && inptr == 2
+	    && memcmp((char*)inbuf, BZIP2_MAGIC, 3) == 0
+	    && (inbuf[3] >= '0' && inbuf[3] <= '9')) {
+	inptr = 0;
+	work = unbzip2;
+	method = BZIP2ED;
+	last_member = 1;
+
     } else if (memcmp(magic, PACK_MAGIC, 2) == 0) {
 	work = unpack;
 	method = PACKED;
@@ -1340,7 +1348,8 @@
         "compr",  /* 1 */
         "pack ",  /* 2 */
         "lzh  ",  /* 3 */
-        "", "", "", "", /* 4 to 7 reserved */
+        "", "", "", /* 4 to 6 reserved */
+        "bzip2",
         "defla"}; /* 8 */
     char *date;
 
Index: gnu/usr.bin/gzip/gzip.h
===================================================================
RCS file: /home/ncvs/src/gnu/usr.bin/gzip/gzip.h,v
retrieving revision 1.3
diff -u -r1.3 gzip.h
--- gnu/usr.bin/gzip/gzip.h	1995/05/30 05:00:35	1.3
+++ gnu/usr.bin/gzip/gzip.h	2001/07/04 09:46:26
@@ -55,7 +55,8 @@
 #define COMPRESSED  1
 #define PACKED      2
 #define LZHED       3
-/* methods 4 to 7 reserved */
+/* methods 4 to 6 reserved */
+#define BZIP2ED     7
 #define DEFLATED    8
 #define MAX_METHODS 9
 extern int method;         /* compression method */
@@ -157,6 +158,7 @@
 #define	OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
 #define	LZH_MAGIC      "\037\240" /* Magic header for SCO LZH Compress files*/
 #define PKZIP_MAGIC    "\120\113\003\004" /* Magic header for pkzip files */
+#define BZIP2_MAGIC    "\102\132\150" /* Magic header for bzip2 files */
 
 /* gzip flag byte */
 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
@@ -271,6 +273,9 @@
 
 	/* in unlzh.c */
 extern int unlzh      OF((int in, int out));
+
+	/* in unbzip2.c */
+extern int unbzip2    OF((int in, int out));
 
 	/* in gzip.c */
 RETSIGTYPE abort_gzip OF((void));
Index: gnu/usr.bin/gzip/unbzip2.c
===================================================================
RCS file: unbzip2.c
diff -N unbzip2.c
--- /dev/null	Wed Jul  4 18:55:00 2001
+++ unbzip2.c	Wed Jul  4 18:35:50 2001
@@ -0,0 +1,84 @@
+/* unbzip2.c -- decompress files in bzip2 format.
+ */
+
+#ifdef RCSID
+/* static char rcsid[] = "$Id: unbzip2.c,v 1.4 2001/06/03 13:43:01 simonb Exp $"; */
+static char rcsid[] = "$FreeBSD$";
+#endif
+
+#define BZ_NO_STDIO
+#include <bzlib.h>
+#include <stddef.h>
+
+#include "gzip.h"
+
+
+/* ============================================================================
+ * Bunzip2 in to out.
+ */
+int unbzip2(in, out) 
+    int in, out;    /* input and output file descriptors */
+{
+	int		n, ret, end_of_file;
+	bz_stream	bzs;
+
+	bzs.bzalloc = NULL;
+	bzs.bzfree = NULL;
+	bzs.opaque = NULL;
+
+	end_of_file = 0;
+	if (BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
+		return(ERROR);
+
+	/* Use up the remainder of "inbuf" that's been read in already */
+	bzs.next_in = inbuf;
+	bzs.avail_in = insize;
+
+	while (1) {
+		if (bzs.avail_in == 0 && !end_of_file) {
+			n = read(in, inbuf, INBUFSIZ);
+			if (n < 0)
+				read_error();
+			if (n == 0)
+				end_of_file = 1;
+			bzs.next_in = inbuf;
+			bzs.avail_in = n;
+		}
+
+		bzs.next_out = outbuf;
+		bzs.avail_out = OUTBUFSIZ;
+		ret = BZ2_bzDecompress(&bzs);
+
+		if (ret == BZ_STREAM_END) {
+			if (!test) {
+				n = write(out, outbuf, OUTBUFSIZ - bzs.avail_out);
+				if (n < 0)
+					write_error();
+				bytes_out += n;
+			}
+			break;
+		}
+		else if (ret == BZ_OK) {
+			if (end_of_file)
+				read_error();
+			if (!test)
+				n = write(out, outbuf, OUTBUFSIZ - bzs.avail_out);
+			bytes_out += n;
+		}
+		else {
+			switch (ret) {
+			  case BZ_DATA_ERROR:
+				error("bzip2 data integrity error");
+			  case BZ_DATA_ERROR_MAGIC:
+				error("bzip2 magic number error");
+			  case BZ_MEM_ERROR:
+				error("bzip2 out of memory");
+			}
+		}
+	}
+
+	if (BZ2_bzDecompressEnd(&bzs) != BZ_OK)
+		return(ERROR);
+
+	return(OK);
+}

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8666d9dn1s.wl>