From ee06264a29c81a2d309c9919222d61ff92aa7b7c Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 31 Aug 2010 14:09:22 +0200 Subject: [PATCH] base64: new applet function old new delta base64_main - 217 +217 packed_usage 27181 27229 +48 read_base64 348 373 +25 applet_names 2299 2306 +7 bbconfig_config_bz2 4942 4948 +6 applet_main 1352 1356 +4 applet_nameofs 676 678 +2 applet_install_loc 169 170 +1 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 7/0 up/down: 310/0) Total: 310 bytes Signed-off-by: Denys Vlasenko --- coreutils/uudecode.c | 78 +++++++++++++++++++++++++++++++++++++++++--- coreutils/uuencode.c | 4 +-- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 06211b8ef..207fb0b8d 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c @@ -10,9 +10,9 @@ * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the * "end" line */ - #include "libbb.h" +#if ENABLE_UUDECODE static void read_stduu(FILE *src_stream, FILE *dst_stream) { char *line; @@ -73,13 +73,14 @@ static void read_stduu(FILE *src_stream, FILE *dst_stream) } bb_error_msg_and_die("short file"); } +#endif static void read_base64(FILE *src_stream, FILE *dst_stream) { int term_count = 1; while (1) { - char translated[4]; + unsigned char translated[4]; int count = 0; while (count < 4) { @@ -93,6 +94,12 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) do { ch = fgetc(src_stream); if (ch == EOF) { + if (ENABLE_BASE64 + && (!ENABLE_UUDECODE || applet_name[0] == 'b') + && count == 0 + ) { + return; + } bb_error_msg_and_die("short file"); } table_ptr = strchr(bb_uuenc_tbl_base64, ch); @@ -134,6 +141,7 @@ static void read_base64(FILE *src_stream, FILE *dst_stream) } } +#if ENABLE_UUDECODE int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int uudecode_main(int argc UNUSED_PARAM, char **argv) { @@ -145,9 +153,9 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) getopt32(argv, "o:", &outname); argv += optind; - if (!*argv) + if (!argv[0]) *--argv = (char*)"-"; - src_stream = xfopen_stdin(*argv); + src_stream = xfopen_stdin(argv[0]); /* Search for the start of the encoding */ while ((line = xmalloc_fgetline(src_stream)) != NULL) { @@ -188,6 +196,68 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) } bb_error_msg_and_die("no 'begin' line"); } +#endif + +//applet:IF_BASE64(APPLET(base64, _BB_DIR_BIN, _BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_BASE64) += uudecode.o + +//config:config BASE64 +//config: bool "base64" +//config: default y +//config: help +//config: Base64 encode and decode + +//usage:#define base64_trivial_usage +//usage: "[-d] [FILE]" +//usage:#define base64_full_usage "\n\n" +//usage: "Base64 encode or decode FILE to standard output" +//usage: "\nOptions:" +//usage: "\n -d Decode data" +////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" +////usage: "\n -i When decoding, ignore non-alphabet characters" + +#if ENABLE_BASE64 +int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int base64_main(int argc UNUSED_PARAM, char **argv) +{ + FILE *src_stream; + unsigned opts; + + opt_complementary = "?1"; /* 1 argument max */ + opts = getopt32(argv, "d"); + argv += optind; + + if (!argv[0]) + *--argv = (char*)"-"; + src_stream = xfopen_stdin(argv[0]); + if (opts) { + read_base64(src_stream, stdout); + } else { + enum { + SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */ + DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), + }; + char src_buf[SRC_BUF_SIZE]; + char dst_buf[DST_BUF_SIZE + 1]; + int src_fd = fileno(src_stream); + while (1) { + size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); + if (!size) + break; + if ((ssize_t)size < 0) + bb_perror_msg_and_die(bb_msg_read_error); + /* Encode the buffer we just read in */ + bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); + xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); + bb_putchar('\n'); + fflush(stdout); + } + } + + fflush_stdout_and_exit(EXIT_SUCCESS); +} +#endif /* Test script. Put this into an empty dir with busybox binary, an run. diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c index 490f8d1bd..fe9e8c664 100644 --- a/coreutils/uuencode.c +++ b/coreutils/uuencode.c @@ -11,7 +11,7 @@ #include "libbb.h" enum { - SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */ + SRC_BUF_SIZE = 15*3, /* This *MUST* be a multiple of 3 */ DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), }; @@ -33,7 +33,7 @@ int uuencode_main(int argc UNUSED_PARAM, char **argv) } argv += optind; if (argv[1]) { - src_fd = xopen(*argv, O_RDONLY); + src_fd = xopen(argv[0], O_RDONLY); fstat(src_fd, &stat_buf); mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); argv++; -- 2.25.1