1 /* vi: set sw=4 ts=4: */
3 * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
17 #include <getopt.h> /* optind */
22 static int read_stduu(FILE *src_stream, FILE *dst_stream)
26 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
28 char *line_ptr = line;
30 if (strcmp(line, "end") == 0) {
33 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
36 /* Ignore the "`\n" line, why is it even in the encode file ? */
40 bb_error_msg_and_die("Line too long");
44 /* Tolerate an overly long line to acomadate a possible exta '`' */
45 if (strlen(line_ptr) < (size_t)length) {
46 bb_error_msg_and_die("Short file");
50 /* Merge four 6 bit chars to three 8 bit chars */
51 fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
58 fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
65 fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
71 bb_error_msg_and_die("Short file");
74 static int read_base64(FILE *src_stream, FILE *dst_stream)
76 static const char base64_table[] =
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
88 /* Get next _valid_ character */
90 ch = fgetc(src_stream);
92 bb_error_msg_and_die("Short file");
94 } while ((table_ptr = strchr(base64_table, ch)) == NULL);
96 /* Convert encoded charcter to decimal */
97 ch = table_ptr - base64_table;
99 if (*table_ptr == '=') {
100 if (term_count == 0) {
101 translated[count] = 0;
106 else if (*table_ptr == '\n') {
107 /* Check for terminating line */
108 if (term_count == 5) {
109 return(EXIT_SUCCESS);
114 translated[count] = ch;
120 /* Merge 6 bit chars to 8 bit */
121 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
123 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
126 fputc(translated[2] << 6 | translated[3], dst_stream);
131 int uudecode_main(int argc, char **argv)
133 int (*decode_fn_ptr) (FILE * src, FILE * dst);
135 char *outname = NULL;
139 opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
141 if (optind == argc) {
143 } else if (optind + 1 == argc) {
144 src_stream = bb_xfopen(argv[optind], "r");
149 /* Search for the start of the encoding */
150 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
151 char *line_ptr = NULL;
153 if (strncmp(line, "begin-base64 ", 13) == 0) {
154 line_ptr = line + 13;
155 decode_fn_ptr = read_base64;
156 } else if (strncmp(line, "begin ", 6) == 0) {
158 decode_fn_ptr = read_stduu;
166 mode = strtoul(line_ptr, NULL, 8);
167 if (outname == NULL) {
168 outname = strchr(line_ptr, ' ');
169 if ((outname == NULL) || (*outname == '\0')) {
174 if (strcmp(outname, "-") == 0) {
177 dst_stream = bb_xfopen(outname, "w");
178 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
181 ret = decode_fn_ptr(src_stream, dst_stream);
182 bb_fclose_nonstdin(src_stream);
187 bb_error_msg_and_die("No `begin' line");