diff options
| author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-09-03 15:49:40 +0000 |
|---|---|---|
| committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-09-03 15:49:40 +0000 |
| commit | f9b31bf3bcd38c95a6575265e2597fbf1da04b16 (patch) | |
| tree | 5cf8056965bf44d78ef9937fe1f529fa2915e772 | |
| parent | 5f698a69678ee6ac7e499f6482d3fbc745ac4ef4 (diff) | |
| download | busybox-w32-f9b31bf3bcd38c95a6575265e2597fbf1da04b16.tar.gz busybox-w32-f9b31bf3bcd38c95a6575265e2597fbf1da04b16.tar.bz2 busybox-w32-f9b31bf3bcd38c95a6575265e2597fbf1da04b16.zip | |
uuencode: common implementation for wget and uuencode (closing bug 694)
git-svn-id: svn://busybox.net/trunk/busybox@16043 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | coreutils/uuencode.c | 66 | ||||
| -rw-r--r-- | include/libbb.h | 4 | ||||
| -rw-r--r-- | libbb/Makefile.in | 2 | ||||
| -rw-r--r-- | libbb/uuencode.c | 63 | ||||
| -rw-r--r-- | networking/wget.c | 22 | ||||
| -rwxr-xr-x | testsuite/uuencode.tests | 28 | ||||
| -rw-r--r-- | testsuite/uuencode/uuencode-sets-standard-input-mode-correctly | 4 |
7 files changed, 105 insertions, 84 deletions
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c index 1449d9aeb..1a8882fc7 100644 --- a/coreutils/uuencode.c +++ b/coreutils/uuencode.c | |||
| @@ -10,60 +10,6 @@ | |||
| 10 | 10 | ||
| 11 | #include "busybox.h" | 11 | #include "busybox.h" |
| 12 | 12 | ||
| 13 | /* Conversion table. for base 64 */ | ||
| 14 | static const char tbl_base64[65] = { | ||
| 15 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | ||
| 16 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | ||
| 17 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | ||
| 18 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | ||
| 19 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | ||
| 20 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | ||
| 21 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', | ||
| 22 | '4', '5', '6', '7', '8', '9', '+', '/', | ||
| 23 | '=' /* termination character */ | ||
| 24 | }; | ||
| 25 | |||
| 26 | static const char tbl_std[65] = { | ||
| 27 | '`', '!', '"', '#', '$', '%', '&', '\'', | ||
| 28 | '(', ')', '*', '+', ',', '-', '.', '/', | ||
| 29 | '0', '1', '2', '3', '4', '5', '6', '7', | ||
| 30 | '8', '9', ':', ';', '<', '=', '>', '?', | ||
| 31 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
| 32 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | ||
| 33 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | ||
| 34 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', | ||
| 35 | '`' /* termination character */ | ||
| 36 | }; | ||
| 37 | |||
| 38 | /* | ||
| 39 | * Encode the string S of length LENGTH to base64 format and place it | ||
| 40 | * to STORE. STORE will be 0-terminated, and must point to a writable | ||
| 41 | * buffer of at least 1+BASE64_LENGTH(length) bytes. | ||
| 42 | * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) | ||
| 43 | */ | ||
| 44 | static void uuencode (const unsigned char *s, char *store, const int length, const char *tbl) | ||
| 45 | { | ||
| 46 | int i; | ||
| 47 | char *p = store; | ||
| 48 | |||
| 49 | /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ | ||
| 50 | for (i = 0; i < length; i += 3) { | ||
| 51 | *p++ = tbl[s[0] >> 2]; | ||
| 52 | *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; | ||
| 53 | *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; | ||
| 54 | *p++ = tbl[s[2] & 0x3f]; | ||
| 55 | s += 3; | ||
| 56 | } | ||
| 57 | /* Pad the result if necessary... */ | ||
| 58 | if (i == length + 1) { | ||
| 59 | *(p - 1) = tbl[64]; | ||
| 60 | } | ||
| 61 | else if (i == length + 2) { | ||
| 62 | *(p - 1) = *(p - 2) = tbl[64]; | ||
| 63 | } | ||
| 64 | /* ...and zero-terminate it. */ | ||
| 65 | *p = '\0'; | ||
| 66 | } | ||
| 67 | 13 | ||
| 68 | #define SRC_BUF_SIZE 45 // This *MUST* be a multiple of 3 | 14 | #define SRC_BUF_SIZE 45 // This *MUST* be a multiple of 3 |
| 69 | #define DST_BUF_SIZE 4 * ((SRC_BUF_SIZE + 2) / 3) | 15 | #define DST_BUF_SIZE 4 * ((SRC_BUF_SIZE + 2) / 3) |
| @@ -80,9 +26,9 @@ int uuencode_main(int argc, char **argv) | |||
| 80 | RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1); | 26 | RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1); |
| 81 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); | 27 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); |
| 82 | 28 | ||
| 83 | tbl = tbl_std; | 29 | tbl = bb_uuenc_tbl_std; |
| 84 | if (bb_getopt_ulflags(argc, argv, "m") & 1) { | 30 | if (bb_getopt_ulflags(argc, argv, "m") & 1) { |
| 85 | tbl = tbl_base64; | 31 | tbl = bb_uuenc_tbl_base64; |
| 86 | } | 32 | } |
| 87 | 33 | ||
| 88 | switch (argc - optind) { | 34 | switch (argc - optind) { |
| @@ -101,7 +47,7 @@ int uuencode_main(int argc, char **argv) | |||
| 101 | bb_show_usage(); | 47 | bb_show_usage(); |
| 102 | } | 48 | } |
| 103 | 49 | ||
| 104 | bb_printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]); | 50 | bb_printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, argv[argc - 1]); |
| 105 | 51 | ||
| 106 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { | 52 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { |
| 107 | if (size != src_buf_size) { | 53 | if (size != src_buf_size) { |
| @@ -111,17 +57,17 @@ int uuencode_main(int argc, char **argv) | |||
| 111 | memset(&src_buf[size], 0, src_buf_size - size); | 57 | memset(&src_buf[size], 0, src_buf_size - size); |
| 112 | } | 58 | } |
| 113 | /* Encode the buffer we just read in */ | 59 | /* Encode the buffer we just read in */ |
| 114 | uuencode((unsigned char*)src_buf, dst_buf, size, tbl); | 60 | bb_uuencode((unsigned char*)src_buf, dst_buf, size, tbl); |
| 115 | 61 | ||
| 116 | putchar('\n'); | 62 | putchar('\n'); |
| 117 | if (tbl == tbl_std) { | 63 | if (tbl == bb_uuenc_tbl_std) { |
| 118 | putchar(tbl[size]); | 64 | putchar(tbl[size]); |
| 119 | } | 65 | } |
| 120 | if (fwrite(dst_buf, 1, write_size, stdout) != write_size) { | 66 | if (fwrite(dst_buf, 1, write_size, stdout) != write_size) { |
| 121 | bb_perror_msg_and_die(bb_msg_write_error); | 67 | bb_perror_msg_and_die(bb_msg_write_error); |
| 122 | } | 68 | } |
| 123 | } | 69 | } |
| 124 | bb_printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n"); | 70 | bb_printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n"); |
| 125 | 71 | ||
| 126 | xferror(src_stream, "source"); /* TODO - Fix this! */ | 72 | xferror(src_stream, "source"); /* TODO - Fix this! */ |
| 127 | 73 | ||
diff --git a/include/libbb.h b/include/libbb.h index b94586165..404ff2e7a 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -498,6 +498,10 @@ extern unsigned char xread_char(int fd); | |||
| 498 | extern void xlseek(int fd, off_t offset, int whence); | 498 | extern void xlseek(int fd, off_t offset, int whence); |
| 499 | extern void xwrite(int fd, void *buf, size_t count); | 499 | extern void xwrite(int fd, void *buf, size_t count); |
| 500 | 500 | ||
| 501 | extern const char bb_uuenc_tbl_base64[]; | ||
| 502 | extern const char bb_uuenc_tbl_std[]; | ||
| 503 | extern void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl); | ||
| 504 | |||
| 501 | #ifndef COMM_LEN | 505 | #ifndef COMM_LEN |
| 502 | #ifdef TASK_COMM_LEN | 506 | #ifdef TASK_COMM_LEN |
| 503 | #define COMM_LEN TASK_COMM_LEN | 507 | #define COMM_LEN TASK_COMM_LEN |
diff --git a/libbb/Makefile.in b/libbb/Makefile.in index f94c100a9..0f2328072 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in | |||
| @@ -48,6 +48,8 @@ LIBBB-$(CONFIG_SU)+= correct_password.c | |||
| 48 | LIBBB-$(CONFIG_LOGIN)+= correct_password.c | 48 | LIBBB-$(CONFIG_LOGIN)+= correct_password.c |
| 49 | LIBBB-$(CONFIG_DF)+= find_mount_point.c | 49 | LIBBB-$(CONFIG_DF)+= find_mount_point.c |
| 50 | LIBBB-$(CONFIG_EJECT)+= find_mount_point.c | 50 | LIBBB-$(CONFIG_EJECT)+= find_mount_point.c |
| 51 | LIBBB-$(CONFIG_UUENCODE)+= uuencode.c | ||
| 52 | LIBBB-$(CONFIG_WGET)+= uuencode.c | ||
| 51 | 53 | ||
| 52 | # We shouldn't build xregcomp.c if we don't need it - this ensures we don't | 54 | # We shouldn't build xregcomp.c if we don't need it - this ensures we don't |
| 53 | # require regex.h to be in the include dir even if we don't need it thereby | 55 | # 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 000000000..38401205b --- /dev/null +++ b/libbb/uuencode.c | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * Copyright 2006 Rob Landley <rob@landley.net> | ||
| 4 | * | ||
| 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "libbb.h" | ||
| 9 | |||
| 10 | /* Conversion table. for base 64 */ | ||
| 11 | const char bb_uuenc_tbl_base64[65] = { | ||
| 12 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | ||
| 13 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | ||
| 14 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | ||
| 15 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | ||
| 16 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | ||
| 17 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | ||
| 18 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', | ||
| 19 | '4', '5', '6', '7', '8', '9', '+', '/', | ||
| 20 | '=' /* termination character */ | ||
| 21 | }; | ||
| 22 | |||
| 23 | const char bb_uuenc_tbl_std[65] = { | ||
| 24 | '`', '!', '"', '#', '$', '%', '&', '\'', | ||
| 25 | '(', ')', '*', '+', ',', '-', '.', '/', | ||
| 26 | '0', '1', '2', '3', '4', '5', '6', '7', | ||
| 27 | '8', '9', ':', ';', '<', '=', '>', '?', | ||
| 28 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
| 29 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | ||
| 30 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | ||
| 31 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', | ||
| 32 | '`' /* termination character */ | ||
| 33 | }; | ||
| 34 | |||
| 35 | /* | ||
| 36 | * Encode the string S of length LENGTH to base64 format and place it | ||
| 37 | * to STORE. STORE will be 0-terminated, and must point to a writable | ||
| 38 | * buffer of at least 1+BASE64_LENGTH(length) bytes. | ||
| 39 | * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) | ||
| 40 | */ | ||
| 41 | void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl) | ||
| 42 | { | ||
| 43 | int i; | ||
| 44 | char *p = store; | ||
| 45 | |||
| 46 | /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ | ||
| 47 | for (i = 0; i < length; i += 3) { | ||
| 48 | *p++ = tbl[s[0] >> 2]; | ||
| 49 | *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; | ||
| 50 | *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; | ||
| 51 | *p++ = tbl[s[2] & 0x3f]; | ||
| 52 | s += 3; | ||
| 53 | } | ||
| 54 | /* Pad the result if necessary... */ | ||
| 55 | if (i == length + 1) { | ||
| 56 | *(p - 1) = tbl[64]; | ||
| 57 | } | ||
| 58 | else if (i == length + 2) { | ||
| 59 | *(p - 1) = *(p - 2) = tbl[64]; | ||
| 60 | } | ||
| 61 | /* ...and zero-terminate it. */ | ||
| 62 | *p = '\0'; | ||
| 63 | } | ||
diff --git a/networking/wget.c b/networking/wget.c index 755515262..c3b9fc039 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
| @@ -98,29 +98,11 @@ static char *safe_fgets(char *s, int size, FILE *stream) | |||
| 98 | 98 | ||
| 99 | #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION | 99 | #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION |
| 100 | /* | 100 | /* |
| 101 | * Base64-encode character string | 101 | * Base64-encode character string and return the string. |
| 102 | * oops... isn't something similar in uuencode.c? | ||
| 103 | * XXX: It would be better to use already existing code | ||
| 104 | */ | 102 | */ |
| 105 | static char *base64enc(unsigned char *p, char *buf, int len) { | 103 | static char *base64enc(unsigned char *p, char *buf, int len) { |
| 106 | 104 | ||
| 107 | char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | 105 | bb_uuencode(p, buf, len, bb_uuenc_tbl_base64); |
| 108 | "0123456789+/"; | ||
| 109 | char *s = buf; | ||
| 110 | |||
| 111 | while(*p) { | ||
| 112 | if (s >= buf+len-4) | ||
| 113 | bb_error_msg_and_die("buffer overflow"); | ||
| 114 | *(s++) = al[(*p >> 2) & 0x3F]; | ||
| 115 | *(s++) = al[((*p << 4) & 0x30) | ((*(p+1) >> 4) & 0x0F)]; | ||
| 116 | *s = *(s+1) = '='; | ||
| 117 | *(s+2) = 0; | ||
| 118 | if (! *(++p)) break; | ||
| 119 | *(s++) = al[((*p << 2) & 0x3C) | ((*(p+1) >> 6) & 0x03)]; | ||
| 120 | if (! *(++p)) break; | ||
| 121 | *(s++) = al[*(p++) & 0x3F]; | ||
| 122 | } | ||
| 123 | |||
| 124 | return buf; | 106 | return buf; |
| 125 | } | 107 | } |
| 126 | #endif | 108 | #endif |
diff --git a/testsuite/uuencode.tests b/testsuite/uuencode.tests new file mode 100755 index 000000000..cb658db3b --- /dev/null +++ b/testsuite/uuencode.tests | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | # unit test for uuencode to test functionality. | ||
| 4 | # Copyright 2006 by Erik Hovland <erik@hovland.org> | ||
| 5 | # Licensed under GPL v2, see file LICENSE for details. | ||
| 6 | |||
| 7 | # AUDIT: Unit tests for uuencode | ||
| 8 | |||
| 9 | . testing.sh | ||
| 10 | |||
| 11 | # testing "test name" "options" "expected result" "file input" "stdin" | ||
| 12 | # file input will be file called "input" | ||
| 13 | # test can create a file "actual" instead of writing to stdout | ||
| 14 | |||
| 15 | # Test setup of standard input | ||
| 16 | saved_umask=$(umask) | ||
| 17 | umask 0 | ||
| 18 | testing "uuencode sets standard input mode correctly" \ | ||
| 19 | "uuencode foo </dev/null | head -n 1 | grep -q 666 && echo yes" "yes\n" "" "" | ||
| 20 | umask $saved_umask | ||
| 21 | |||
| 22 | testing "uuencode correct encoding" "uuencode bb_uuenc_test.out" \ | ||
| 23 | "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" \ | ||
| 24 | "" "The fast grey fox jumped over the lazy brown dog.\n" | ||
| 25 | testing "uuencode correct base64 encoding" "uuencode -m bb_uuenc_test.out" \ | ||
| 26 | "begin-base64 644 bb_uuenc_test.out\nVGhlIGZhc3QgZ3JleSBmb3gganVtcGVkIG92ZXIgdGhlIGxhenkgYnJvd24g\nZG9nLgo=\n====\n" \ | ||
| 27 | "" "The fast grey fox jumped over the lazy brown dog.\n" | ||
| 28 | 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 1a48a6656..000000000 --- a/testsuite/uuencode/uuencode-sets-standard-input-mode-correctly +++ /dev/null | |||
| @@ -1,4 +0,0 @@ | |||
| 1 | saved_umask=$(umask) | ||
| 2 | umask 0 | ||
| 3 | busybox uuencode foo </dev/null | head -n 1 | grep -q 666 | ||
| 4 | umask $saved_umask | ||
