aboutsummaryrefslogtreecommitdiff
path: root/coreutils/uudecode.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/uudecode.c')
-rw-r--r--coreutils/uudecode.c91
1 files changed, 80 insertions, 11 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c
index 0298a4bdb..207fb0b8d 100644
--- a/coreutils/uudecode.c
+++ b/coreutils/uudecode.c
@@ -1,19 +1,18 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Copyright 2003, Glenn McGrath 3 * Copyright 2003, Glenn McGrath
4 * 4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 * 6 *
7 * Based on specification from 7 * Based on specification from
8 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html 8 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
9 * 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
15#include "libbb.h" 13#include "libbb.h"
16 14
15#if ENABLE_UUDECODE
17static void read_stduu(FILE *src_stream, FILE *dst_stream) 16static void read_stduu(FILE *src_stream, FILE *dst_stream)
18{ 17{
19 char *line; 18 char *line;
@@ -74,13 +73,14 @@ static void read_stduu(FILE *src_stream, FILE *dst_stream)
74 } 73 }
75 bb_error_msg_and_die("short file"); 74 bb_error_msg_and_die("short file");
76} 75}
76#endif
77 77
78static void read_base64(FILE *src_stream, FILE *dst_stream) 78static void read_base64(FILE *src_stream, FILE *dst_stream)
79{ 79{
80 int term_count = 1; 80 int term_count = 1;
81 81
82 while (1) { 82 while (1) {
83 char translated[4]; 83 unsigned char translated[4];
84 int count = 0; 84 int count = 0;
85 85
86 while (count < 4) { 86 while (count < 4) {
@@ -94,6 +94,12 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
94 do { 94 do {
95 ch = fgetc(src_stream); 95 ch = fgetc(src_stream);
96 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 }
97 bb_error_msg_and_die("short file"); 103 bb_error_msg_and_die("short file");
98 } 104 }
99 table_ptr = strchr(bb_uuenc_tbl_base64, ch); 105 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
@@ -135,6 +141,7 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
135 } 141 }
136} 142}
137 143
144#if ENABLE_UUDECODE
138int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 145int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
139int uudecode_main(int argc UNUSED_PARAM, char **argv) 146int uudecode_main(int argc UNUSED_PARAM, char **argv)
140{ 147{
@@ -146,9 +153,9 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
146 getopt32(argv, "o:", &outname); 153 getopt32(argv, "o:", &outname);
147 argv += optind; 154 argv += optind;
148 155
149 if (!*argv) 156 if (!argv[0])
150 *--argv = (char*)"-"; 157 *--argv = (char*)"-";
151 src_stream = xfopen_stdin(*argv); 158 src_stream = xfopen_stdin(argv[0]);
152 159
153 /* Search for the start of the encoding */ 160 /* Search for the start of the encoding */
154 while ((line = xmalloc_fgetline(src_stream)) != NULL) { 161 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
@@ -189,6 +196,68 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
189 } 196 }
190 bb_error_msg_and_die("no 'begin' line"); 197 bb_error_msg_and_die("no 'begin' line");
191} 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
192 261
193/* Test script. 262/* Test script.
194Put this into an empty dir with busybox binary, an run. 263Put this into an empty dir with busybox binary, an run.