[BusyBox-cvs] busybox/archival/libunarchive open_transformer.c, NONE, 1.1 Makefile.in, 1.17, 1.18 archive_xread.c, 1.3, 1.4 data_align.c, 1.2, 1.3 decompress_bunzip2.c, 1.10, 1.11 filter_accept_list_reassign.c, 1.3, 1.4 get_header_tar_bz2.c, 1.5, 1.6 get_header_tar_gz.c, 1.7, 1.8 init_handle.c, 1.3, 1.4 unzip.c, 1.31, 1.32

Glenn McGrath bug1 at busybox.net
Sat Nov 15 23:19:07 UTC 2003


Update of /var/cvs/busybox/archival/libunarchive
In directory winder:/tmp/cvs-serv4579/archival/libunarchive

Modified Files:
	Makefile.in archive_xread.c data_align.c decompress_bunzip2.c 
	filter_accept_list_reassign.c get_header_tar_bz2.c 
	get_header_tar_gz.c init_handle.c unzip.c 
Added Files:
	open_transformer.c 
Log Message:
Move from read_gz to the pipe()+fork() method.
open_transformer(), common code for pipe+fork.
Function pointer for read() no longer needed.
Allow inflate to be initialised with a specified buffer size to avoid 
over-reading.
Reset static variables in inflate_get_next_window to fix a bug where 
only the first file in a .zip would be be extracted.


--- NEW FILE: open_transformer.c ---
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <unistd.h>

#include "libbb.h"

/* transformer(), more than meets the eye */
extern int open_transformer(int src_fd, int (*transformer)(int src_fd, int dst_fd))
{
	int fd_pipe[2];
	int pid;

	if (pipe(fd_pipe) != 0) {
		bb_perror_msg_and_die("Can't create pipe");
	}

	pid = fork();
	if (pid == -1) {
		bb_perror_msg_and_die("Fork failed");
	}

	if (pid == 0) {
		/* child process */
	    close(fd_pipe[0]); /* We don't wan't to read from the pipe */
	    transformer(src_fd, fd_pipe[1]);
	    close(fd_pipe[1]); /* Send EOF */
	    exit(0);
	    /* notreached */
	}
	/* parent process */
	close(fd_pipe[1]); /* Don't want to write down the pipe */
	close(src_fd);

	return(fd_pipe[0]);
}

Index: get_header_tar_gz.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/get_header_tar_gz.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- get_header_tar_gz.c	7 Nov 2003 21:22:09 -0000	1.7
+++ get_header_tar_gz.c	15 Nov 2003 23:19:02 -0000	1.8
@@ -23,6 +23,9 @@
 {
 	unsigned char magic[2];
 
+	/* Cant lseek over pipe's */
+	archive_handle->seek = seek_by_char;
+
 	archive_xread_all(archive_handle, &magic, 2);
 	if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
 		bb_error_msg_and_die("Invalid gzip magic");
@@ -30,20 +33,10 @@
 
 	check_header_gzip(archive_handle->src_fd);
 
-	GZ_gzReadOpen(archive_handle->src_fd, 0, 0);
-
-	archive_handle->read = read_gz;
-	archive_handle->seek = seek_by_char;
-
+	archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
 	archive_handle->offset = 0;
 	while (get_header_tar(archive_handle) == EXIT_SUCCESS);
 
-	/* Cleanup */
-	GZ_gzReadClose();
-
-	check_trailer_gzip(archive_handle->src_fd);
-	
-	/* Can only do one tar.gz per archive */
+	/* Can only do one file at a time */
 	return(EXIT_FAILURE);
 }
-

Index: Makefile.in
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/Makefile.in,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- Makefile.in	14 Nov 2003 09:22:24 -0000	1.17
+++ Makefile.in	15 Nov 2003 23:19:02 -0000	1.18
@@ -49,6 +49,7 @@
 \
 	data_align.o \
 	find_list_entry.o \
+	open_transformer.o \
 	init_handle.o
 
 GUNZIP_FILES:= check_header_gzip.o unzip.o

Index: init_handle.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/init_handle.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- init_handle.c	5 Jul 2003 23:10:26 -0000	1.3
+++ init_handle.c	15 Nov 2003 23:19:02 -0000	1.4
@@ -30,7 +30,6 @@
 	archive_handle->action_header = header_skip;
 	archive_handle->action_data = data_skip;
 	archive_handle->filter = filter_accept_all;
-	archive_handle->read = bb_full_read;
 	archive_handle->seek = seek_by_jump;
 
 	return(archive_handle);

Index: data_align.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/data_align.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- data_align.c	3 Nov 2002 14:05:07 -0000	1.2
+++ data_align.c	15 Nov 2003 23:19:02 -0000	1.3
@@ -27,7 +27,6 @@
 	const unsigned short skip_amount = (boundary - (archive_handle->offset % boundary)) % boundary;
 
 	archive_handle->seek(archive_handle, skip_amount);
-
 	archive_handle->offset += skip_amount;
 
 	return;

Index: unzip.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/unzip.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- unzip.c	14 Jul 2003 21:20:41 -0000	1.31
+++ unzip.c	15 Nov 2003 23:19:02 -0000	1.32
@@ -103,7 +103,7 @@
 static unsigned char gunzip_bk;	/* bits in bit buffer */
 
 /* These control the size of the bytebuffer */
-#define BYTEBUFFER_MAX 0x8000
+static unsigned int bytebuffer_max = 0x8000;
 static unsigned char *bytebuffer = NULL;
 static unsigned int bytebuffer_offset = 0;
 static unsigned int bytebuffer_size = 0;
@@ -144,21 +144,16 @@
 	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
 };
 
-static void fill_bytebuffer(void)
-{
-	if (bytebuffer_offset >= bytebuffer_size) {
-		/* Leave the first 4 bytes empty so we can always unwind the bitbuffer 
-		 * to the front of the bytebuffer, leave 4 bytes free at end of tail
-		 * so we can easily top up buffer in check_trailer_gzip() */
-		bytebuffer_size = 4 + bb_xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8);
-		bytebuffer_offset = 4;
-	}
-}
-
 static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
 {
 	while (*current < required) {
-		fill_bytebuffer();
+		if (bytebuffer_offset >= bytebuffer_size) {
+			/* Leave the first 4 bytes empty so we can always unwind the bitbuffer 
+			 * to the front of the bytebuffer, leave 4 bytes free at end of tail
+			 * so we can easily top up buffer in check_trailer_gzip() */
+			bytebuffer_size = 4 + bb_xread(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8);
+			bytebuffer_offset = 4;
+		}
 		bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
 		bytebuffer_offset++;
 		*current += 8;
@@ -861,9 +856,9 @@
 
 static int inflate_get_next_window(void)
 {
-	static int needAnotherBlock = 1;
 	static int method = -1; // Method == -1 for stored, -2 for codes
 	static int e = 0;
+	static int needAnotherBlock = 1;
 	
 	gunzip_outbuf_count = 0;
 
@@ -873,6 +868,8 @@
 		if (needAnotherBlock) {
 			if(e) {
 				calculate_gunzip_crc();
+				e = 0;
+				needAnotherBlock = 1;
 				return 0;
 			} // Last block
 			method = inflate_block(&e);
@@ -895,54 +892,25 @@
 	/* Doesnt get here */
 }
 
-/*
- * User functions
- *
- * read_gz, GZ_gzReadOpen, GZ_gzReadClose, inflate
- */
-
-extern ssize_t read_gz(int fd, void *buf, size_t count)
+/* Initialise bytebuffer, be carefull not to overfill the buffer */
+extern void inflate_init(unsigned int bufsize)
 {
-	static int morebytes = 0, finished = 0;
-	
-	if (morebytes) {
-		int bytesRead = morebytes > count ? count : morebytes;
-		memcpy(buf, gunzip_window + (gunzip_outbuf_count - morebytes), bytesRead);
-		morebytes -= bytesRead;
-		return bytesRead;
-	} else if (finished) {
-		return 0;
-	} else if (count >= 0x8000) { // We can decompress direcly to the buffer, 32k at a time
-		// Could decompress to larger buffer, but it must be a power of 2, and calculating that is probably more expensive than the benefit
-		unsigned char *old_gunzip_window = gunzip_window; // Save old window
-		gunzip_window = buf;
-		if (inflate_get_next_window() == 0) finished = 1;
-		gunzip_window = old_gunzip_window; // Restore old window
-		return gunzip_outbuf_count;
-	} else { // Oh well, need to split up the gunzip_window
-		int bytesRead;
-		if (inflate_get_next_window() == 0) finished = 1;
-		morebytes = gunzip_outbuf_count;
-		bytesRead = morebytes > count ? count : morebytes;
-		memcpy(buf, gunzip_window, bytesRead);
-		morebytes -= bytesRead;
-		return bytesRead;
-	}
-	
+	/* Set the bytebuffer size, default is same as gunzip_wsize */
+	bytebuffer_max = bufsize + 8;
+	bytebuffer_offset = 4;
+	bytebuffer_size = 0;
 }
 
-extern void GZ_gzReadOpen(int fd, void *unused, int nUnused)
+extern int inflate_unzip(int in, int out)
 {
+	ssize_t nwrote;
 	typedef void (*sig_type) (int);
 
 	/* Allocate all global buffers (for DYN_ALLOC option) */
 	gunzip_window = xmalloc(gunzip_wsize);
 	gunzip_outbuf_count = 0;
 	gunzip_bytes_out = 0;
-	gunzip_src_fd = fd;
-
-	/* Input buffer */
-	bytebuffer = xmalloc(BYTEBUFFER_MAX);
+	gunzip_src_fd = in;
 
 	/* initialize gunzip_window, bit buffer */
 	gunzip_bk = 0;
@@ -950,10 +918,20 @@
 
 	/* Create the crc table */
 	make_gunzip_crc_table();
-}
 
-extern void GZ_gzReadClose(void)
-{
+	/* Allocate space for buffer */
+	bytebuffer = xmalloc(bytebuffer_max);	
+
+	while(1) {
+		int ret = inflate_get_next_window();
+		nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
+		if (nwrote == -1) {
+			bb_perror_msg("write");
+			return -1;
+		}
+		if (ret == 0) break;
+	}
+
 	/* Cleanup */
 	free(gunzip_window);
 	free(gunzip_crc_table);
@@ -967,57 +945,20 @@
 		gunzip_bb >>= 8;
 		gunzip_bk -= 8;
 	}
-}
-
-/*extern int inflate(int in, int out) // Useful for testing read_gz
-{
-	char buf[8192];
-	ssize_t nread, nwrote;
-
-	GZ_gzReadOpen(in, 0, 0);
-	while(1) { // Robbed from bb_copyfd.c
-		nread = read_gz(in, buf, sizeof(buf));
-		if (nread == 0) break; // no data to write
-		else if (nread == -1) {
-			bb_perror_msg("read");
-			return -1;
-		}
-		nwrote = bb_full_write(out, buf, nread);
-		if (nwrote == -1) {
-			bb_perror_msg("write");
-			return -1;
-		}
-	}
-	GZ_gzReadClose();
-	return 0;
-}*/
-
-extern int inflate(int in, int out)
-{
-	ssize_t nwrote;
-	GZ_gzReadOpen(in, 0, 0);
-	while(1) {
-		int ret = inflate_get_next_window();
-		nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
-		if (nwrote == -1) {
-			bb_perror_msg("write");
-			return -1;
-		}
-		if (ret == 0) break;
-	}
-	GZ_gzReadClose();
 	return 0;
 }
 
-extern void check_trailer_gzip(int src_fd)
+extern int inflate_gunzip(int in, int out)
 {
 	unsigned int stored_crc = 0;
 	unsigned char count;
 
+	inflate_unzip(in, out);
+
 	/* top up the input buffer with the rest of the trailer */
 	count = bytebuffer_size - bytebuffer_offset;
 	if (count < 8) {
-		bb_xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count);
+		bb_xread_all(in, &bytebuffer[bytebuffer_size], 8 - count);
 		bytebuffer_size += 8 - count;
 	}
 	for (count = 0; count != 4; count++) {
@@ -1027,14 +968,15 @@
 
 	/* Validate decompression - crc */
 	if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
-		bb_error_msg_and_die("crc error");
+		bb_error_msg("crc error");
 	}
 
 	/* Validate decompression - size */
 	if (gunzip_bytes_out !=
 		(bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
 		(bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
-		bb_error_msg_and_die("Incorrect length, but crc is correct");
+		bb_error_msg("Incorrect length");
 	}
-
+	
+	return 0;
 }

Index: archive_xread.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/archive_xread.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- archive_xread.c	5 Jul 2003 23:10:26 -0000	1.3
+++ archive_xread.c	15 Nov 2003 23:19:02 -0000	1.4
@@ -24,7 +24,7 @@
 {
 	ssize_t size;
 
-	size = archive_handle->read(archive_handle->src_fd, buf, count);
+	size = bb_full_read(archive_handle->src_fd, buf, count);
 	if (size < 0) {
 		bb_perror_msg_and_die("Read error");
 	}

Index: decompress_bunzip2.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/decompress_bunzip2.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- decompress_bunzip2.c	5 Nov 2003 04:55:58 -0000	1.10
+++ decompress_bunzip2.c	15 Nov 2003 23:19:02 -0000	1.11
@@ -424,7 +424,7 @@
    are ignored, data is written to out_fd and return is RETVAL_OK or error.
 */
 
-extern int read_bunzip(bunzip_data *bd, char *outbuf, int len)
+static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
 {
 	const unsigned int *dbuf;
 	int pos,current,previous,gotcount;
@@ -511,7 +511,7 @@
 /* Allocate the structure, read file header.  If in_fd==-1, inbuf must contain
    a complete bunzip file (len bytes long).  If in_fd!=-1, inbuf and len are
    ignored, and data is read from file handle into temporary buffer. */
-extern int start_bunzip(bunzip_data **bdp, int in_fd, char *inbuf, int len)
+static int start_bunzip(bunzip_data **bdp, int in_fd, char *inbuf, int len)
 {
 	bunzip_data *bd;
 	unsigned int i,j,c;

Index: get_header_tar_bz2.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/get_header_tar_bz2.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- get_header_tar_bz2.c	14 Nov 2003 09:21:27 -0000	1.5
+++ get_header_tar_bz2.c	15 Nov 2003 23:19:02 -0000	1.6
@@ -26,53 +26,13 @@
 
 extern char get_header_tar_bz2(archive_handle_t *archive_handle)
 {
-	int fd_pipe[2];
-	int pid;
-
 	/* Cant lseek over pipe's */
-	archive_handle->read = safe_read;
 	archive_handle->seek = seek_by_char;
 
-	if (pipe(fd_pipe) != 0) {
-		bb_error_msg_and_die("Can't create pipe");
-	}
-
-	pid = fork();
-	if (pid == -1) {
-		bb_error_msg_and_die("Fork failed\n");
-	}
-
-	if (pid == 0) {
-		/* child process */
-		int status;
-
-	    close(fd_pipe[0]); /* We don't wan't to read from the pipe */
-		uncompressStream(archive_handle->src_fd, fd_pipe[1]);
-	    close(fd_pipe[1]); /* Send EOF */
-	    exit(status);
-	    /* notreached */
-	}
-	/* parent process */
-	close(fd_pipe[1]); /* Don't want to write down the pipe */
-	close(archive_handle->src_fd);
-
-	archive_handle->src_fd = fd_pipe[0];
-
+	archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompressStream);
 	archive_handle->offset = 0;
 	while (get_header_tar(archive_handle) == EXIT_SUCCESS);
 
-	close(fd_pipe[0]);
-#if 0
-	if (kill(pid, SIGTERM) == -1) {
-		bb_error_msg_and_die("Couldnt kill gunzip process");
-	}
-#endif
-	
-	/* I dont think this is needed */
-	if (waitpid(pid, NULL, 0) == -1) {
-		bb_error_msg("Couldnt wait ?");
-	}
-
 	/* Can only do one file at a time */
 	return(EXIT_FAILURE);
 }

Index: filter_accept_list_reassign.c
===================================================================
RCS file: /var/cvs/busybox/archival/libunarchive/filter_accept_list_reassign.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- filter_accept_list_reassign.c	7 Nov 2003 21:31:58 -0000	1.3
+++ filter_accept_list_reassign.c	15 Nov 2003 23:19:02 -0000	1.4
@@ -40,14 +40,12 @@
 		/* Modify the subarchive handler based on the extension */
 #ifdef CONFIG_FEATURE_DEB_TAR_GZ
 		if (strcmp(name_ptr, ".gz") == 0) {
-			archive_handle->sub_archive->read = read;
 			archive_handle->action_data_subarchive = get_header_tar_gz;
 			return(EXIT_SUCCESS);
 		}
 #endif
 #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
 		if (strcmp(name_ptr, ".bz2") == 0) {
-			archive_handle->sub_archive->read = read;
 			archive_handle->action_data_subarchive = get_header_tar_bz2;
 			return(EXIT_SUCCESS);
 		}




More information about the busybox-cvs mailing list