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
16 static void FAST_FUNC
read_stduu(FILE *src_stream
, FILE *dst_stream
, int flags UNUSED_PARAM
)
20 while ((line
= xmalloc_fgetline(src_stream
)) != NULL
) {
21 int encoded_len
, str_len
;
24 if (strcmp(line
, "end") == 0) {
25 return; /* the only non-error exit */
30 *line_ptr
= (*line_ptr
- 0x20) & 0x3f;
33 str_len
= line_ptr
- line
;
35 encoded_len
= line
[0] * 4 / 3;
36 /* Check that line is not too short. (we tolerate
37 * overly _long_ line to accomodate possible extra '`').
38 * Empty line case is also caught here. */
39 if (str_len
<= encoded_len
) {
40 break; /* go to bb_error_msg_and_die("short file"); */
42 if (encoded_len
<= 0) {
43 /* Ignore the "`\n" line, why is it even in the encode file ? */
47 if (encoded_len
> 60) {
48 bb_error_msg_and_die("line too long");
54 /* Merge four 6 bit chars to three 8 bit chars */
55 *dst
++ = line_ptr
[0] << 2 | line_ptr
[1] >> 4;
57 if (encoded_len
== 0) {
61 *dst
++ = line_ptr
[1] << 4 | line_ptr
[2] >> 2;
63 if (encoded_len
== 0) {
67 *dst
++ = line_ptr
[2] << 6 | line_ptr
[3];
70 } while (encoded_len
> 0);
71 fwrite(line
, 1, dst
- line
, dst_stream
);
74 bb_error_msg_and_die("short file");
79 int uudecode_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
80 int uudecode_main(int argc UNUSED_PARAM
, char **argv
)
86 opt_complementary
= "?1"; /* 1 argument max */
87 getopt32(argv
, "o:", &outname
);
92 src_stream
= xfopen_stdin(argv
[0]);
94 /* Search for the start of the encoding */
95 while ((line
= xmalloc_fgetline(src_stream
)) != NULL
) {
96 void FAST_FUNC (*decode_fn_ptr
)(FILE *src
, FILE *dst
, int flags
);
101 if (strncmp(line
, "begin-base64 ", 13) == 0) {
102 line_ptr
= line
+ 13;
103 decode_fn_ptr
= read_base64
;
104 } else if (strncmp(line
, "begin ", 6) == 0) {
106 decode_fn_ptr
= read_stduu
;
112 /* begin line found. decode and exit */
113 mode
= bb_strtou(line_ptr
, NULL
, 8);
114 if (outname
== NULL
) {
115 outname
= strchr(line_ptr
, ' ');
116 if ((outname
== NULL
) || (*outname
== '\0')) {
122 if (NOT_LONE_DASH(outname
)) {
123 dst_stream
= xfopen_for_write(outname
);
124 fchmod(fileno(dst_stream
), mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
));
127 decode_fn_ptr(src_stream
, dst_stream
, /*flags:*/ BASE64_FLAG_UU_STOP
+ BASE64_FLAG_NO_STOP_CHAR
);
128 /* fclose_if_not_stdin(src_stream); - redundant */
131 bb_error_msg_and_die("no 'begin' line");
135 //applet:IF_BASE64(APPLET(base64, _BB_DIR_BIN, _BB_SUID_DROP))
137 //kbuild:lib-$(CONFIG_BASE64) += uudecode.o
139 //config:config BASE64
140 //config: bool "base64"
143 //config: Base64 encode and decode
145 //usage:#define base64_trivial_usage
146 //usage: "[-d] [FILE]"
147 //usage:#define base64_full_usage "\n\n"
148 //usage: "Base64 encode or decode FILE to standard output"
149 //usage: "\nOptions:"
150 //usage: "\n -d Decode data"
151 ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
152 ////usage: "\n -i When decoding, ignore non-alphabet characters"
155 int base64_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
156 int base64_main(int argc UNUSED_PARAM
, char **argv
)
161 opt_complementary
= "?1"; /* 1 argument max */
162 opts
= getopt32(argv
, "d");
166 *--argv
= (char*)"-";
167 src_stream
= xfopen_stdin(argv
[0]);
169 read_base64(src_stream
, stdout
, /*flags:*/ (char)EOF
);
172 SRC_BUF_SIZE
= 76/4*3, /* This *MUST* be a multiple of 3 */
173 DST_BUF_SIZE
= 4 * ((SRC_BUF_SIZE
+ 2) / 3),
175 char src_buf
[SRC_BUF_SIZE
];
176 char dst_buf
[DST_BUF_SIZE
+ 1];
177 int src_fd
= fileno(src_stream
);
179 size_t size
= full_read(src_fd
, src_buf
, SRC_BUF_SIZE
);
182 if ((ssize_t
)size
< 0)
183 bb_perror_msg_and_die(bb_msg_read_error
);
184 /* Encode the buffer we just read in */
185 bb_uuencode(dst_buf
, src_buf
, size
, bb_uuenc_tbl_base64
);
186 xwrite(STDOUT_FILENO
, dst_buf
, 4 * ((size
+ 2) / 3));
192 fflush_stdout_and_exit(EXIT_SUCCESS
);
197 Put this into an empty dir with busybox binary, an run.
200 test -x busybox || { echo "No ./busybox?"; exit; }
201 ln -sf busybox uudecode
202 ln -sf busybox uuencode
209 echo -n ABCDEF >ABCDEF
213 ./uuencode $f <$f >u_$f
214 ./uuencode -m $f <$f >m_$f
216 mkdir unpk_u unpk_m 2>/dev/null
218 ./uudecode <$f -o unpk_u/${f:2}
219 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
223 ./uudecode <$f -o unpk_m/${f:2}
224 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1