aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/uudecode.c78
-rw-r--r--coreutils/uuencode.c4
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 @@
10 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the 10 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
11 * "end" line 11 * "end" line
12 */ 12 */
13
14#include "libbb.h" 13#include "libbb.h"
15 14
15#if ENABLE_UUDECODE
16static void read_stduu(FILE *src_stream, FILE *dst_stream) 16static void read_stduu(FILE *src_stream, FILE *dst_stream)
17{ 17{
18 char *line; 18 char *line;
@@ -73,13 +73,14 @@ static void read_stduu(FILE *src_stream, FILE *dst_stream)
73 } 73 }
74 bb_error_msg_and_die("short file"); 74 bb_error_msg_and_die("short file");
75} 75}
76#endif
76 77
77static void read_base64(FILE *src_stream, FILE *dst_stream) 78static void read_base64(FILE *src_stream, FILE *dst_stream)
78{ 79{
79 int term_count = 1; 80 int term_count = 1;
80 81
81 while (1) { 82 while (1) {
82 char translated[4]; 83 unsigned char translated[4];
83 int count = 0; 84 int count = 0;
84 85
85 while (count < 4) { 86 while (count < 4) {
@@ -93,6 +94,12 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
93 do { 94 do {
94 ch = fgetc(src_stream); 95 ch = fgetc(src_stream);
95 if (ch == EOF) { 96 if (ch == EOF) {
97 if (ENABLE_BASE64
98 && (!ENABLE_UUDECODE || applet_name[0] == 'b')
99 && count == 0
100 ) {
101 return;
102 }
96 bb_error_msg_and_die("short file"); 103 bb_error_msg_and_die("short file");
97 } 104 }
98 table_ptr = strchr(bb_uuenc_tbl_base64, ch); 105 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
@@ -134,6 +141,7 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
134 } 141 }
135} 142}
136 143
144#if ENABLE_UUDECODE
137int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 145int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
138int uudecode_main(int argc UNUSED_PARAM, char **argv) 146int uudecode_main(int argc UNUSED_PARAM, char **argv)
139{ 147{
@@ -145,9 +153,9 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
145 getopt32(argv, "o:", &outname); 153 getopt32(argv, "o:", &outname);
146 argv += optind; 154 argv += optind;
147 155
148 if (!*argv) 156 if (!argv[0])
149 *--argv = (char*)"-"; 157 *--argv = (char*)"-";
150 src_stream = xfopen_stdin(*argv); 158 src_stream = xfopen_stdin(argv[0]);
151 159
152 /* Search for the start of the encoding */ 160 /* Search for the start of the encoding */
153 while ((line = xmalloc_fgetline(src_stream)) != NULL) { 161 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
@@ -188,6 +196,68 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
188 } 196 }
189 bb_error_msg_and_die("no 'begin' line"); 197 bb_error_msg_and_die("no 'begin' line");
190} 198}
199#endif
200
201//applet:IF_BASE64(APPLET(base64, _BB_DIR_BIN, _BB_SUID_DROP))
202
203//kbuild:lib-$(CONFIG_BASE64) += uudecode.o
204
205//config:config BASE64
206//config: bool "base64"
207//config: default y
208//config: help
209//config: Base64 encode and decode
210
211//usage:#define base64_trivial_usage
212//usage: "[-d] [FILE]"
213//usage:#define base64_full_usage "\n\n"
214//usage: "Base64 encode or decode FILE to standard output"
215//usage: "\nOptions:"
216//usage: "\n -d Decode data"
217////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
218////usage: "\n -i When decoding, ignore non-alphabet characters"
219
220#if ENABLE_BASE64
221int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
222int base64_main(int argc UNUSED_PARAM, char **argv)
223{
224 FILE *src_stream;
225 unsigned opts;
226
227 opt_complementary = "?1"; /* 1 argument max */
228 opts = getopt32(argv, "d");
229 argv += optind;
230
231 if (!argv[0])
232 *--argv = (char*)"-";
233 src_stream = xfopen_stdin(argv[0]);
234 if (opts) {
235 read_base64(src_stream, stdout);
236 } else {
237 enum {
238 SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */
239 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
240 };
241 char src_buf[SRC_BUF_SIZE];
242 char dst_buf[DST_BUF_SIZE + 1];
243 int src_fd = fileno(src_stream);
244 while (1) {
245 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
246 if (!size)
247 break;
248 if ((ssize_t)size < 0)
249 bb_perror_msg_and_die(bb_msg_read_error);
250 /* Encode the buffer we just read in */
251 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
252 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
253 bb_putchar('\n');
254 fflush(stdout);
255 }
256 }
257
258 fflush_stdout_and_exit(EXIT_SUCCESS);
259}
260#endif
191 261
192/* Test script. 262/* Test script.
193Put this into an empty dir with busybox binary, an run. 263Put 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 @@
11#include "libbb.h" 11#include "libbb.h"
12 12
13enum { 13enum {
14 SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */ 14 SRC_BUF_SIZE = 15*3, /* This *MUST* be a multiple of 3 */
15 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), 15 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
16}; 16};
17 17
@@ -33,7 +33,7 @@ int uuencode_main(int argc UNUSED_PARAM, char **argv)
33 } 33 }
34 argv += optind; 34 argv += optind;
35 if (argv[1]) { 35 if (argv[1]) {
36 src_fd = xopen(*argv, O_RDONLY); 36 src_fd = xopen(argv[0], O_RDONLY);
37 fstat(src_fd, &stat_buf); 37 fstat(src_fd, &stat_buf);
38 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 38 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
39 argv++; 39 argv++;