1 /* vi: set sw=4 ts=4: */
3 * Copyright 2003, Glenn McGrath
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 * Based on specification from
8 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
10 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
13 //config:config UUDECODE
14 //config: bool "uudecode (5.8 kb)"
17 //config: uudecode is used to decode a uuencoded file.
19 //applet:IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_UUDECODE) += uudecode.o
23 //usage:#define uudecode_trivial_usage
24 //usage: "[-o OUTFILE] [INFILE]"
25 //usage:#define uudecode_full_usage "\n\n"
26 //usage: "Uudecode a file\n"
27 //usage: "Finds OUTFILE in uuencoded source unless -o is given"
29 //usage:#define uudecode_example_usage
30 //usage: "$ uudecode -o busybox busybox.uu\n"
31 //usage: "$ ls -l busybox\n"
32 //usage: "-rwxr-xr-x 1 ams ams 245264 Jun 7 21:35 busybox\n"
37 static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM)
42 int encoded_len, str_len;
47 line = xmalloc_fgets_str_len(src_stream, "\n", &line_len);
50 /* Handle both Unix and MSDOS text.
51 * Note: space should not be trimmed, some encoders use it instead of "`"
52 * for padding of last incomplete 4-char block.
56 && (line[str_len] == '\n' || line[str_len] == '\r')
61 if (strcmp(line, "end") == 0) {
62 return; /* the only non-error exit */
67 *line_ptr = (*line_ptr - 0x20) & 0x3f;
70 str_len = line_ptr - line;
72 encoded_len = line[0] * 4 / 3;
73 /* Check that line is not too short. (we tolerate
74 * overly _long_ line to accommodate possible extra "`").
75 * Empty line case is also caught here. */
76 if (str_len <= encoded_len) {
77 break; /* go to bb_error_msg_and_die("short file"); */
79 if (encoded_len <= 0) {
80 /* Ignore the "`\n" line, why is it even in the encode file ? */
84 if (encoded_len > 60) {
85 bb_simple_error_msg_and_die("line too long");
91 /* Merge four 6 bit chars to three 8 bit chars */
92 *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
94 if (encoded_len == 0) {
98 *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
100 if (encoded_len == 0) {
104 *dst++ = line_ptr[2] << 6 | line_ptr[3];
107 } while (encoded_len > 0);
108 fwrite(line, 1, dst - line, dst_stream);
111 bb_simple_error_msg_and_die("short file");
116 int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
117 int uudecode_main(int argc UNUSED_PARAM, char **argv)
120 char *outname = NULL;
123 getopt32(argv, "^" "o:" "\0" "?1"/* 1 arg max*/, &outname);
127 *--argv = (char*)"-";
128 src_stream = xfopen_stdin(argv[0]);
130 /* Search for the start of the encoding */
131 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
132 void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
137 if (is_prefixed_with(line, "begin-base64 ")) {
138 line_ptr = line + 13;
139 decode_fn_ptr = read_base64;
140 } else if (is_prefixed_with(line, "begin ")) {
142 decode_fn_ptr = read_stduu;
148 /* begin line found. decode and exit */
149 mode = bb_strtou(line_ptr, NULL, 8);
150 if (outname == NULL) {
151 outname = strchr(line_ptr, ' ');
155 trim(outname); /* remove trailing space (and '\r' for DOS text) */
160 if (NOT_LONE_DASH(outname)) {
161 dst_stream = xfopen_for_write(outname);
162 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
165 decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
166 /* fclose_if_not_stdin(src_stream); - redundant */
169 bb_simple_error_msg_and_die("no 'begin' line");
173 //applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP))
175 //kbuild:lib-$(CONFIG_BASE64) += uudecode.o
177 //config:config BASE64
178 //config: bool "base64 (4.9 kb)"
181 //config: Base64 encode and decode
183 //usage:#define base64_trivial_usage
184 //usage: "[-d] [FILE]"
185 //usage:#define base64_full_usage "\n\n"
186 //usage: "Base64 encode or decode FILE to standard output"
187 //usage: "\n -d Decode data"
188 ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
189 ////usage: "\n -i When decoding, ignore non-alphabet characters"
192 int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
193 int base64_main(int argc UNUSED_PARAM, char **argv)
198 opts = getopt32(argv, "^" "d" "\0" "?1"/* 1 arg max*/);
202 *--argv = (char*)"-";
203 src_stream = xfopen_stdin(argv[0]);
205 read_base64(src_stream, stdout, /*flags:*/ (char)EOF);
208 SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */
209 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
211 char src_buf[SRC_BUF_SIZE];
212 char dst_buf[DST_BUF_SIZE + 1];
213 int src_fd = fileno(src_stream);
215 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
218 if ((ssize_t)size < 0)
219 bb_simple_perror_msg_and_die(bb_msg_read_error);
220 /* Encode the buffer we just read in */
221 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
222 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
228 fflush_stdout_and_exit(EXIT_SUCCESS);
233 Put this into an empty dir with busybox binary, an run.
236 test -x busybox || { echo "No ./busybox?"; exit; }
237 ln -sf busybox uudecode
238 ln -sf busybox uuencode
245 echo -n ABCDEF >ABCDEF
249 ./uuencode $f <$f >u_$f
250 ./uuencode -m $f <$f >m_$f
252 mkdir unpk_u unpk_m 2>/dev/null
254 ./uudecode <$f -o unpk_u/${f:2}
255 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
259 ./uudecode <$f -o unpk_m/${f:2}
260 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1