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 read_stduu(FILE *src_stream, FILE *dst_stream)
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");
77 static void read_base64(FILE *src_stream, FILE *dst_stream)
89 /* Get next _valid_ character.
90 * global vector bb_uuenc_tbl_base64[] contains this string:
91 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
94 ch = fgetc(src_stream);
96 bb_error_msg_and_die("short file");
98 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
99 } while (table_ptr == NULL);
101 /* Convert encoded character to decimal */
102 ch = table_ptr - bb_uuenc_tbl_base64;
104 if (*table_ptr == '=') {
105 if (term_count == 0) {
106 translated[count] = '\0';
110 } else if (*table_ptr == '\n') {
111 /* Check for terminating line */
112 if (term_count == 5) {
118 translated[count] = ch;
124 /* Merge 6 bit chars to 8 bit */
126 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
129 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
132 fputc(translated[2] << 6 | translated[3], dst_stream);
137 int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
138 int uudecode_main(int argc UNUSED_PARAM, char **argv)
141 char *outname = NULL;
144 opt_complementary = "?1"; /* 1 argument max */
145 getopt32(argv, "o:", &outname);
149 *--argv = (char*)"-";
150 src_stream = xfopen_stdin(*argv);
152 /* Search for the start of the encoding */
153 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
154 void (*decode_fn_ptr)(FILE *src, FILE *dst);
159 if (strncmp(line, "begin-base64 ", 13) == 0) {
160 line_ptr = line + 13;
161 decode_fn_ptr = read_base64;
162 } else if (strncmp(line, "begin ", 6) == 0) {
164 decode_fn_ptr = read_stduu;
170 /* begin line found. decode and exit */
171 mode = bb_strtou(line_ptr, NULL, 8);
172 if (outname == NULL) {
173 outname = strchr(line_ptr, ' ');
174 if ((outname == NULL) || (*outname == '\0')) {
180 if (NOT_LONE_DASH(outname)) {
181 dst_stream = xfopen_for_write(outname);
182 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
185 decode_fn_ptr(src_stream, dst_stream);
186 /* fclose_if_not_stdin(src_stream); - redundant */
189 bb_error_msg_and_die("no 'begin' line");
193 Put this into an empty dir with busybox binary, an run.
196 test -x busybox || { echo "No ./busybox?"; exit; }
197 ln -sf busybox uudecode
198 ln -sf busybox uuencode
205 echo -n ABCDEF >ABCDEF
209 ./uuencode $f <$f >u_$f
210 ./uuencode -m $f <$f >m_$f
212 mkdir unpk_u unpk_m 2>/dev/null
214 ./uudecode <$f -o unpk_u/${f:2}
215 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
219 ./uudecode <$f -o unpk_m/${f:2}
220 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1