svn commit: trunk/busybox/archival: bz

vda at busybox.net vda at busybox.net
Wed Oct 17 10:14:11 UTC 2007


Author: vda
Date: 2007-10-17 03:14:11 -0700 (Wed, 17 Oct 2007)
New Revision: 20271

Log:
bzip2: more of code shrink

compressStream                                       503     473     -30
BZ2_bzCompress                                        78       -     -78
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/1 up/down: 0/-108)           Total: -108 bytes
   text    data     bss     dec     hex filename
 676300    2538   12104  690942   a8afe busybox_old
 676192    2538   12104  690834   a8a92 busybox_unstripped



Modified:
   trunk/busybox/archival/bz/bzlib.c
   trunk/busybox/archival/bzip2.c


Changeset:
Modified: trunk/busybox/archival/bz/bzlib.c
===================================================================
--- trunk/busybox/archival/bz/bzlib.c	2007-10-17 09:59:01 UTC (rev 20270)
+++ trunk/busybox/archival/bz/bzlib.c	2007-10-17 10:14:11 UTC (rev 20271)
@@ -76,11 +76,9 @@
 
 
 static
-Bool isempty_RL(EState* s)
+int isempty_RL(EState* s)
 {
-	if (s->state_in_ch < 256 && s->state_in_len > 0)
-		return False;
-	return True;
+	return (s->state_in_ch >= 256 || s->state_in_len <= 0);
 }
 
 
@@ -333,7 +331,7 @@
 			}
 
 #ifdef FLUSH_IS_UNUSED
-case_BZ_M_FLUSHING:
+ case_BZ_M_FLUSHING:
 		case BZ_M_FLUSHING:
 			/*if (s->avail_in_expect != s->strm->avail_in)
 				return BZ_SEQUENCE_ERROR;*/

Modified: trunk/busybox/archival/bzip2.c
===================================================================
--- trunk/busybox/archival/bzip2.c	2007-10-17 09:59:01 UTC (rev 20270)
+++ trunk/busybox/archival/bzip2.c	2007-10-17 10:14:11 UTC (rev 20271)
@@ -51,79 +51,55 @@
 	IOBUF_SIZE = 8 * 1024
 };
 
+static uint8_t level;
+
+/* NB: compressStream() has to return -1 on errors, not die.
+ * bbunpack() will correctly clean up in this case
+ * (delete incomplete .bz2 file)
+ */
+
 /* Returns:
- * <0 on write errors (examine errno),
- * >0 on short writes (errno == 0)
- * 0  no error (entire input consumed, gimme more)
- * on "impossible" errors (internal bzip2 compressor bug) dies
+ * -1 on errors
+ * total written bytes so far otherwise
  */
 static
-ssize_t bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
+USE_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
 {
 	int n, n2, ret;
 
 	strm->avail_in = rlen;
-	strm->next_in  = rbuf;
+	strm->next_in = rbuf;
 	while (1) {
 		strm->avail_out = IOBUF_SIZE;
 		strm->next_out = wbuf;
 
-		ret = BZ2_bzCompress(strm, BZ_RUN);
-		if (ret != BZ_RUN_OK)
+		ret = BZ2_bzCompress(strm, rlen ? BZ_RUN : BZ_FINISH);
+		if (ret != BZ_RUN_OK /* BZ_RUNning */
+		 && ret != BZ_FINISH_OK /* BZ_FINISHing, but not done yet */
+		 && ret != BZ_STREAM_END /* BZ_FINISHed */
+		) {
 			bb_error_msg_and_die("internal error %d", ret);
-
-		n = IOBUF_SIZE - strm->avail_out;
-		if (n) {
-			/* short reads must have errno == 0 */
-			errno = 0;
-			n2 = full_write(STDOUT_FILENO, wbuf, n);
-			if (n2 != n)
-				return n2 ? n2 : 1;
 		}
 
-		if (strm->avail_in == 0)
-			return 0;
-	}
-}
-
-
-/*---------------------------------------------------*/
-static
-USE_DESKTOP(long long) int bz_write_tail(bz_stream *strm, void *wbuf)
-{
-	int n, n2, ret;
-	USE_DESKTOP(long long) int total;
-
-	total = -1;
-	while (1) {
-		strm->avail_out = IOBUF_SIZE;
-		strm->next_out = wbuf;
-
-		ret = BZ2_bzCompress(strm, BZ_FINISH);
-		if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
-			bb_error_msg_and_die("internal error %d", ret);
-
 		n = IOBUF_SIZE - strm->avail_out;
 		if (n) {
 			n2 = full_write(STDOUT_FILENO, wbuf, n);
-			if (n2 != n)
-				goto err;
+			if (n2 != n) {
+				if (n2 >= 0)
+					errno = 0; /* prevent bogus error message */
+				bb_perror_msg(n2 >= 0 ? "short write" : "write error");
+				return -1;
+			}
 		}
 
 		if (ret == BZ_STREAM_END)
 			break;
+		if (rlen && strm->avail_in == 0)
+			break;
 	}
-
-	total = 0 USE_DESKTOP( + strm->total_out );
- err:
-#if ENABLE_FEATURE_CLEAN_UP
-	BZ2_bzCompressEnd(strm);
-#endif
-	return total;
+	return 0 USE_DESKTOP( + strm->total_out );
 }
 
-static uint8_t level;
-
 static
 USE_DESKTOP(long long) int compressStream(void)
 {
@@ -136,26 +112,26 @@
 #define wbuf (iobuf + IOBUF_SIZE)
 
 	iobuf = xmalloc(2 * IOBUF_SIZE);
-
 	BZ2_bzCompressInit(strm, level);
 
 	while (1) {
 		count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
-		if (count < 0)
+		if (count < 0) {
 			bb_perror_msg("read error");
-		if (count <= 0)
+			total = -1;
 			break;
-		count = bz_write(strm, rbuf, count, wbuf);
-		if (count) {
-			bb_perror_msg(count < 0 ? "write error" : "short write");
-			break;
 		}
+		/* if count == 0, bz_write finalizes compression */
+		total = bz_write(strm, rbuf, count, wbuf);
+		if (count == 0 || total < 0)
+			break;
 	}
 
-	total = bz_write_tail(strm, wbuf);
+#if ENABLE_FEATURE_CLEAN_UP
+	BZ2_bzCompressEnd(strm);
 	free(iobuf);
-	/* we had no error _only_ if count == 0 */
-	return count == 0 ? total : -1;
+#endif
+	return total;
 }
 
 static




More information about the busybox-cvs mailing list