3 * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation; either version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Library General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Based on specification from
19 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
21 * Bugs: the spec doesnt mention anything about "`\n`\n" prior to the "end" line
27 #include <getopt.h> /* optind */
32 static int read_stduu(FILE *src_stream, FILE *dst_stream)
36 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
38 char *line_ptr = line;
40 if (strcmp(line, "end") == 0) {
43 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
46 /* Ignore the "`\n" line, why is it even in the encode file ? */
50 bb_error_msg_and_die("Line too long");
54 /* Tolerate an overly long line to acomadate a possible exta '`' */
55 if (strlen(line_ptr) < (size_t)length) {
56 bb_error_msg_and_die("Short file");
60 /* Merge four 6 bit chars to three 8 bit chars */
61 fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
68 fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
75 fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
81 bb_error_msg_and_die("Short file");
84 static int read_base64(FILE *src_stream, FILE *dst_stream)
86 static const char base64_table[] =
87 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
98 /* Get next _valid_ character */
100 ch = fgetc(src_stream);
102 bb_error_msg_and_die("Short file");
104 } while ((table_ptr = strchr(base64_table, ch)) == NULL);
106 /* Convert encoded charcter to decimal */
107 ch = table_ptr - base64_table;
109 if (*table_ptr == '=') {
110 if (term_count == 0) {
111 translated[count] = 0;
116 else if (*table_ptr == '\n') {
117 /* Check for terminating line */
118 if (term_count == 5) {
119 return(EXIT_SUCCESS);
124 translated[count] = ch;
130 /* Merge 6 bit chars to 8 bit */
131 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
133 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
136 fputc(translated[2] << 6 | translated[3], dst_stream);
141 int uudecode_main(int argc, char **argv)
143 int (*decode_fn_ptr) (FILE * src, FILE * dst);
145 char *outname = NULL;
149 opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
151 if (optind == argc) {
153 } else if (optind + 1 == argc) {
154 src_stream = bb_xfopen(argv[optind], "r");
159 /* Search for the start of the encoding */
160 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
161 char *line_ptr = NULL;
165 } else if (strncmp(line, "begin-base64 ", 13) == 0) {
166 line_ptr = line + 13;
167 decode_fn_ptr = read_base64;
168 } else if (strncmp(line, "begin ", 6) == 0) {
170 decode_fn_ptr = read_stduu;
178 mode = strtoul(line_ptr, NULL, 8);
179 if (outname == NULL) {
180 outname = strchr(line_ptr, ' ');
181 if ((outname == NULL) || (*outname == '\0')) {
186 if (strcmp(outname, "-") == 0) {
189 dst_stream = bb_xfopen(outname, "w");
190 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
193 ret = decode_fn_ptr(src_stream, dst_stream);
194 bb_fclose_nonstdin(src_stream);
199 bb_error_msg_and_die("No `begin' line");