2 * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
4 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 * Based on specification from
7 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
9 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
16 #include <getopt.h> /* optind */
21 static int read_stduu(FILE *src_stream, FILE *dst_stream)
25 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
27 char *line_ptr = line;
29 if (strcmp(line, "end") == 0) {
32 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
35 /* Ignore the "`\n" line, why is it even in the encode file ? */
39 bb_error_msg_and_die("Line too long");
43 /* Tolerate an overly long line to acomadate a possible exta '`' */
44 if (strlen(line_ptr) < (size_t)length) {
45 bb_error_msg_and_die("Short file");
49 /* Merge four 6 bit chars to three 8 bit chars */
50 fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
57 fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
64 fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
70 bb_error_msg_and_die("Short file");
73 static int read_base64(FILE *src_stream, FILE *dst_stream)
75 static const char base64_table[] =
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
87 /* Get next _valid_ character */
89 ch = fgetc(src_stream);
91 bb_error_msg_and_die("Short file");
93 } while ((table_ptr = strchr(base64_table, ch)) == NULL);
95 /* Convert encoded charcter to decimal */
96 ch = table_ptr - base64_table;
98 if (*table_ptr == '=') {
99 if (term_count == 0) {
100 translated[count] = 0;
105 else if (*table_ptr == '\n') {
106 /* Check for terminating line */
107 if (term_count == 5) {
108 return(EXIT_SUCCESS);
113 translated[count] = ch;
119 /* Merge 6 bit chars to 8 bit */
120 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
122 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
125 fputc(translated[2] << 6 | translated[3], dst_stream);
130 int uudecode_main(int argc, char **argv)
132 int (*decode_fn_ptr) (FILE * src, FILE * dst);
134 char *outname = NULL;
138 opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
140 if (optind == argc) {
142 } else if (optind + 1 == argc) {
143 src_stream = bb_xfopen(argv[optind], "r");
148 /* Search for the start of the encoding */
149 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
150 char *line_ptr = NULL;
154 } else if (strncmp(line, "begin-base64 ", 13) == 0) {
155 line_ptr = line + 13;
156 decode_fn_ptr = read_base64;
157 } else if (strncmp(line, "begin ", 6) == 0) {
159 decode_fn_ptr = read_stduu;
167 mode = strtoul(line_ptr, NULL, 8);
168 if (outname == NULL) {
169 outname = strchr(line_ptr, ' ');
170 if ((outname == NULL) || (*outname == '\0')) {
175 if (strcmp(outname, "-") == 0) {
178 dst_stream = bb_xfopen(outname, "w");
179 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
182 ret = decode_fn_ptr(src_stream, dst_stream);
183 bb_fclose_nonstdin(src_stream);
188 bb_error_msg_and_die("No `begin' line");