[patch] refactor uuencode functions in uuencode.c and wget.c

Erik Hovland erik at hovland.org
Thu Jul 20 15:30:20 PDT 2006


Both wget.c and uuencode.c have functions to uuencode something. That
seems kind of redundant. So I took the uuencode function out of
uuencode.c, put it into libbb and switch both applets to use that
function instead.

This produces a small code savings if both applets are compiled in and
only slightly bloats if only one applet is chosen.

This patch should be identical to the one I submitted for bug 694:
http://bugs.busybox.net/view.php?id=694

E

-- 
Erik Hovland
mail: erik AT hovland DOT org
web: http://hovland.org/
PGP/GPG public key available on request
-------------- next part --------------
diff -Nurdp -x .svn -x busybox_old busybox-ro/coreutils/uuencode.c busybox-trunk/coreutils/uuencode.c
--- busybox-ro/coreutils/uuencode.c	2006-07-14 16:07:51.123861850 -0700
+++ busybox-trunk/coreutils/uuencode.c	2006-07-13 15:08:58.742615864 -0700
@@ -15,60 +15,6 @@
 #include <unistd.h>
 #include "busybox.h"
 
-/* Conversion table.  for base 64 */
-static const char tbl_base64[65] = {
-	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
-	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
-	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
-	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
-	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
-	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
-	'w', 'x', 'y', 'z', '0', '1', '2', '3',
-	'4', '5', '6', '7', '8', '9', '+', '/',
-	'=' /* termination character */
-};
-
-static const char tbl_std[65] = {
-	'`', '!', '"', '#', '$', '%', '&', '\'',
-	'(', ')', '*', '+', ',', '-', '.', '/',
-	'0', '1', '2', '3', '4', '5', '6', '7',
-	'8', '9', ':', ';', '<', '=', '>', '?',
-	'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
-	'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
-	'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
-	'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
-	'`' /* termination character */
-};
-
-/*
- * Encode the string S of length LENGTH to base64 format and place it
- * to STORE.  STORE will be 0-terminated, and must point to a writable
- * buffer of at least 1+BASE64_LENGTH(length) bytes.
- * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
- */
-static void uuencode (const unsigned char *s, char *store, const int length, const char *tbl)
-{
-	int i;
-	char *p = store;
-
-	/* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
-	for (i = 0; i < length; i += 3) {
-		*p++ = tbl[s[0] >> 2];
-		*p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
-		*p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
-		*p++ = tbl[s[2] & 0x3f];
-		s += 3;
-	}
-	/* Pad the result if necessary...  */
-	if (i == length + 1) {
-		*(p - 1) = tbl[64];
-	}
-	else if (i == length + 2) {
-		*(p - 1) = *(p - 2) = tbl[64];
-	}
-	/* ...and zero-terminate it.  */
-	*p = '\0';
-}
 
 #define SRC_BUF_SIZE	45  // This *MUST* be a multiple of 3
 #define DST_BUF_SIZE    4 * ((SRC_BUF_SIZE + 2) / 3)
@@ -85,9 +31,9 @@ int uuencode_main(int argc, char **argv)
 	RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1);
 	RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1);
 
-	tbl = tbl_std;
+	tbl = bb_uuenc_tbl_std;
 	if (bb_getopt_ulflags(argc, argv, "m") & 1) {
-		tbl = tbl_base64;
+		tbl = bb_uuenc_tbl_base64;
 	}
 
 	switch (argc - optind) {
@@ -106,7 +52,7 @@ int uuencode_main(int argc, char **argv)
 			bb_show_usage();
 	}
 
-	bb_printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]);
+	bb_printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, argv[argc - 1]);
 
 	while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
 		if (size != src_buf_size) {
@@ -116,17 +62,17 @@ int uuencode_main(int argc, char **argv)
 			memset(&src_buf[size], 0, src_buf_size - size);
 		}
 		/* Encode the buffer we just read in */
-		uuencode((unsigned char*)src_buf, dst_buf, size, tbl);
+		bb_uuencode((unsigned char*)src_buf, dst_buf, size, tbl);
 
 		putchar('\n');
-		if (tbl == tbl_std) {
+		if (tbl == bb_uuenc_tbl_std) {
 			putchar(tbl[size]);
 		}
 		if (fwrite(dst_buf, 1, write_size, stdout) != write_size) {
 			bb_perror_msg_and_die(bb_msg_write_error);
 		}
 	}
-	bb_printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n");
+	bb_printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
 
 	bb_xferror(src_stream, "source");	/* TODO - Fix this! */
 
diff -Nurdp -x .svn -x busybox_old busybox-ro/include/libbb.h busybox-trunk/include/libbb.h
--- busybox-ro/include/libbb.h	2006-07-20 15:07:06.574114598 -0700
+++ busybox-trunk/include/libbb.h	2006-07-20 14:07:45.806862857 -0700
@@ -483,6 +484,10 @@ extern unsigned char xread_char(int fd);
 extern void xlseek(int fd, off_t offset, int whence);
 extern void xwrite(int fd, void *buf, size_t count);
 
+extern const char bb_uuenc_tbl_base64[];
+extern const char bb_uuenc_tbl_std[];
+extern void bb_uuencode (const unsigned char *s, char *store, const int length, const char *tbl);
+
 #ifndef COMM_LEN
 #ifdef TASK_COMM_LEN
 #define COMM_LEN TASK_COMM_LEN
diff -Nurdp -x .svn -x busybox_old busybox-ro/libbb/Makefile.in busybox-trunk/libbb/Makefile.in
--- busybox-ro/libbb/Makefile.in	2006-07-14 16:07:55.612219166 -0700
+++ busybox-trunk/libbb/Makefile.in	2006-07-20 14:06:43.864724496 -0700
@@ -35,7 +35,7 @@ LIBBB-y:= \
 	getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \
 	perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \
 	warn_ignoring_args.c concat_subpath_file.c vfork_daemon_rexec.c \
-	bb_do_delay.c
+	bb_do_delay.c uuencode.c
 
 # conditionally compiled objects:
 LIBBB-$(CONFIG_FEATURE_MOUNT_LOOP)+= loop.c
diff -Nurdp -x .svn -x busybox_old busybox-ro/libbb/uuencode.c busybox-trunk/libbb/uuencode.c
--- busybox-ro/libbb/uuencode.c	1969-12-31 16:00:00.000000000 -0800
+++ busybox-trunk/libbb/uuencode.c	2006-07-14 15:59:34.842924160 -0700
@@ -0,0 +1,55 @@
+
+/* Conversion table.  for base 64 */
+const char bb_uuenc_tbl_base64[65] = {
+	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+	'w', 'x', 'y', 'z', '0', '1', '2', '3',
+	'4', '5', '6', '7', '8', '9', '+', '/',
+	'=' /* termination character */
+};
+
+const char bb_uuenc_tbl_std[65] = {
+	'`', '!', '"', '#', '$', '%', '&', '\'',
+	'(', ')', '*', '+', ',', '-', '.', '/',
+	'0', '1', '2', '3', '4', '5', '6', '7',
+	'8', '9', ':', ';', '<', '=', '>', '?',
+	'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+	'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+	'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
+	'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
+	'`' /* termination character */
+};
+
+/*
+ * Encode the string S of length LENGTH to base64 format and place it
+ * to STORE.  STORE will be 0-terminated, and must point to a writable
+ * buffer of at least 1+BASE64_LENGTH(length) bytes.
+ * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
+ */
+void bb_uuencode (const unsigned char *s, char *store, const int length, const char *tbl)
+{
+	int i;
+	char *p = store;
+
+	/* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
+	for (i = 0; i < length; i += 3) {
+		*p++ = tbl[s[0] >> 2];
+		*p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
+		*p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
+		*p++ = tbl[s[2] & 0x3f];
+		s += 3;
+	}
+	/* Pad the result if necessary...  */
+	if (i == length + 1) {
+		*(p - 1) = tbl[64];
+	}
+	else if (i == length + 2) {
+		*(p - 1) = *(p - 2) = tbl[64];
+	}
+	/* ...and zero-terminate it.  */
+	*p = '\0';
+}
diff -Nurdp -x .svn -x busybox_old busybox-ro/networking/wget.c busybox-trunk/networking/wget.c
--- busybox-ro/networking/wget.c	2006-07-14 16:07:36.976887550 -0700
+++ busybox-trunk/networking/wget.c	2006-07-13 15:37:23.873271689 -0700
@@ -7,6 +7,7 @@
  */
 
 #include "busybox.h"
+#include "libbb.h"
 #include <errno.h>
 #include <signal.h>
 #include <sys/ioctl.h>
@@ -101,29 +102,11 @@ static char *safe_fgets(char *s, int siz
 
 #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
 /*
- *  Base64-encode character string
- *  oops... isn't something similar in uuencode.c?
- *  XXX: It would be better to use already existing code
+ *  Base64-encode character string and return the string.
  */
 static char *base64enc(unsigned char *p, char *buf, int len) {
 
-	char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
-		    "0123456789+/";
-	char *s = buf;
-
-	while(*p) {
-		if (s >= buf+len-4)
-			bb_error_msg_and_die("buffer overflow");
-		*(s++) = al[(*p >> 2) & 0x3F];
-		*(s++) = al[((*p << 4) & 0x30) | ((*(p+1) >> 4) & 0x0F)];
-		*s = *(s+1) = '=';
-		*(s+2) = 0;
-		if (! *(++p)) break;
-		*(s++) = al[((*p << 2) & 0x3C) | ((*(p+1) >> 6) & 0x03)];
-		if (! *(++p)) break;
-		*(s++) = al[*(p++) & 0x3F];
-	}
-
+	bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
 	return buf;
 }
 #endif
diff -Nurdp -x .svn -x busybox_old busybox-ro/testsuite/uuencode.tests busybox-trunk/testsuite/uuencode.tests
--- busybox-ro/testsuite/uuencode.tests	1969-12-31 16:00:00.000000000 -0800
+++ busybox-trunk/testsuite/uuencode.tests	2006-07-13 15:05:23.049520844 -0700
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+# unit test for uuencode to test functionality.
+# Copyright 2006 by Erik Hovland <erik at hovland.org>
+# Licensed under GPL v2, see file LICENSE for details.
+
+# AUDIT: Unit tests for uuencode
+
+. testing.sh
+
+# testing "test name" "options" "expected result" "file input" "stdin"
+#   file input will be file called "input"
+#   test can create a file "actual" instead of writing to stdout
+
+# Test setup of standard input
+saved_umask=$(umask)
+umask 0
+testing "uuencode sets standard input mode correctly" \
+        "uuencode foo </dev/null | head -n 1 | grep -q 666 && echo yes" "yes\n" "" ""
+umask $saved_umask
+
+testing "uuencode correct encoding" "uuencode bb_uuenc_test.out" \
+"begin 644 bb_uuenc_test.out\nM5&AE(&9A<W0 at 9W)E>2!F;W@@:G5M<&5D(&]V97(@=&AE(&QA>GD at 8G)O=VX@\n%9&]G+ at H\`\n\`\nend\n" \
+        "" "The fast grey fox jumped over the lazy brown dog.\n"
+testing "uuencode correct base64 encoding" "uuencode -m bb_uuenc_test.out" \
+"begin-base64 644 bb_uuenc_test.out\nVGhlIGZhc3QgZ3JleSBmb3gganVtcGVkIG92ZXIgdGhlIGxhenkgYnJvd24g\nZG9nLgo=\n====\n" \
+        "" "The fast grey fox jumped over the lazy brown dog.\n"
+exit $FAILCOUNT


More information about the busybox mailing list