uuencode: common implementation for wget and uuencode (closing bug 694)
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c
index 1449d9a..1a8882f 100644
--- a/coreutils/uuencode.c
+++ b/coreutils/uuencode.c
@@ -10,60 +10,6 @@
#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)
@@ -80,9 +26,9 @@
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) {
@@ -101,7 +47,7 @@
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) {
@@ -111,17 +57,17 @@
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");
xferror(src_stream, "source"); /* TODO - Fix this! */
diff --git a/include/libbb.h b/include/libbb.h
index b945861..404ff2e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -498,6 +498,10 @@
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 --git a/libbb/Makefile.in b/libbb/Makefile.in
index f94c100..0f23280 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -48,6 +48,8 @@
LIBBB-$(CONFIG_LOGIN)+= correct_password.c
LIBBB-$(CONFIG_DF)+= find_mount_point.c
LIBBB-$(CONFIG_EJECT)+= find_mount_point.c
+LIBBB-$(CONFIG_UUENCODE)+= uuencode.c
+LIBBB-$(CONFIG_WGET)+= uuencode.c
# We shouldn't build xregcomp.c if we don't need it - this ensures we don't
# require regex.h to be in the include dir even if we don't need it thereby
diff --git a/libbb/uuencode.c b/libbb/uuencode.c
new file mode 100644
index 0000000..3840120
--- /dev/null
+++ b/libbb/uuencode.c
@@ -0,0 +1,63 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Copyright 2006 Rob Landley <rob@landley.net>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+
+/* 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 --git a/networking/wget.c b/networking/wget.c
index 7555152..c3b9fc0 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -98,29 +98,11 @@
#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 --git a/testsuite/uuencode.tests b/testsuite/uuencode.tests
new file mode 100755
index 0000000..cb658db
--- /dev/null
+++ b/testsuite/uuencode.tests
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+# unit test for uuencode to test functionality.
+# Copyright 2006 by Erik Hovland <erik@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@9W)E>2!F;W@@:G5M<&5D(&]V97(@=&AE(&QA>GD@8G)O=VX@\n%9&]G+@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
diff --git a/testsuite/uuencode/uuencode-sets-standard-input-mode-correctly b/testsuite/uuencode/uuencode-sets-standard-input-mode-correctly
deleted file mode 100644
index 1a48a66..0000000
--- a/testsuite/uuencode/uuencode-sets-standard-input-mode-correctly
+++ /dev/null
@@ -1,4 +0,0 @@
-saved_umask=$(umask)
-umask 0
-busybox uuencode foo </dev/null | head -n 1 | grep -q 666
-umask $saved_umask