changeset 54:60a7fb8ddeb4

Add a quick and dirt bzcat (stdin to stdout only for the moment) to test out the bunzip2 library.
author Rob Landley <rob@landley.net>
date Tue, 16 Jan 2007 13:26:02 -0500
parents 41d55b5d49fd
children 0bb7c679499a
files lib/bunzip.c toys/bzcat.c toys/toylist.h
diffstat 3 files changed, 26 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/lib/bunzip.c	Sun Jan 14 20:20:06 2007 -0500
+++ b/lib/bunzip.c	Tue Jan 16 13:26:02 2007 -0500
@@ -3,10 +3,11 @@
 
    Copyright 2003, 2006 by Rob Landley (rob@landley.net).
 
-   Based on a close reading (but not the actual code) of bzip2 decompression
-   code by Julian R Seward (jseward@acm.org), which also acknowledges
-   contributions by Mike Burrows, David Wheeler, Peter Fenwick, Alistair
-   Moffat, Radford Neal, Ian H. Witten, Robert Sedgewick, and Jon L. Bentley.
+   Based on a close reading (but not the actual code) of the original bzip2
+   decompression code by Julian R Seward (jseward@acm.org), which also
+   acknowledges contributions by Mike Burrows, David Wheeler, Peter Fenwick,
+   Alistair Moffat, Radford Neal, Ian H. Witten, Robert Sedgewick, and
+   Jon L. Bentley.
 */
 
 #include "toys.h"
@@ -117,7 +118,7 @@
 }
 
 // Decompress a block of text to into intermediate buffer
-extern int read_bunzip_data(bunzip_data *bd)
+int read_bunzip_data(bunzip_data *bd)
 {
 	struct group_data *hufGroup;
 	int dbufCount, nextSym, dbufSize, origPtr, groupCount, *base, *limit,
@@ -195,7 +196,7 @@
 		t = get_bits(bd, 5);
 		for (i = 0; i < symCount; i++) {
 			for(;;) {
-				if (t < 1 || t > MAX_HUFCODE_BITS) return RETVAL_DATA_ERROR;
+				if (MAX_HUFCODE_BITS < (unsigned)t-1) return RETVAL_DATA_ERROR;
 				if(!get_bits(bd, 1)) break;
 				if(!get_bits(bd, 1)) t++;
 				else t--;
@@ -397,7 +398,7 @@
 }
 
 // Flush output buffer to disk
-extern void flush_bunzip_outbuf(bunzip_data *bd, int out_fd)
+void flush_bunzip_outbuf(bunzip_data *bd, int out_fd)
 {
 	if (bd->outbufPos) {
 		if (write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos)
@@ -409,7 +410,7 @@
 // Undo burrows-wheeler transform on intermediate buffer to produce output.
 // If !len, write up to len bytes of data to buf.  Otherwise write to out_fd.
 // Returns len ? bytes written : RETVAL_OK.  Notice all errors negative #'s.
-extern int write_bunzip_data(bunzip_data *bd, int out_fd, char *outbuf, int len)
+int write_bunzip_data(bunzip_data *bd, int out_fd, char *outbuf, int len)
 {
 	unsigned int *dbuf = bd->dbuf;
 	int count, pos, current, run, copies, outbyte, previous, gotcount = 0;
@@ -503,7 +504,7 @@
 
 // Allocate the structure, read file header.  If !len, src_fd contains
 // filehandle to read from.  Else inbuf contains data.
-extern int start_bunzip(bunzip_data **bdp, int src_fd, char *inbuf, int len)
+int start_bunzip(bunzip_data **bdp, int src_fd, char *inbuf, int len)
 {
 	bunzip_data *bd;
 	unsigned int i, j, c;
@@ -553,7 +554,7 @@
 
 // Example usage: decompress src_fd to dst_fd.  (Stops at end of bzip data,
 // not end of file.)
-extern char *uncompressStream(int src_fd, int dst_fd)
+char *bunzipStream(int src_fd, int dst_fd)
 {
 	bunzip_data *bd;
 	int i;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toys/bzcat.c	Tue Jan 16 13:26:02 2007 -0500
@@ -0,0 +1,14 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * bzcat.c - decompress stdin to stdout using bunzip2.
+ */
+
+#include "toys.h"
+
+int bzcat_main(void)
+{
+	char *error = bunzipStream(0, 1);
+
+	if (error) error_exit(error);
+	return 0;
+}
--- a/toys/toylist.h	Sun Jan 14 20:20:06 2007 -0500
+++ b/toys/toylist.h	Tue Jan 16 13:26:02 2007 -0500
@@ -67,6 +67,7 @@
 
 // The rest of these are alphabetical, for binary search.
 
+USE_BZCAT(NEWTOY(bzcat, "", TOYFLAG_USR|TOYFLAG_BIN))
 USE_CATV(NEWTOY(catv, "vte", TOYFLAG_NOFORK|TOYFLAG_USR|TOYFLAG_BIN))
 USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK))
 USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN))