- merge -r15463:15564 from busybox_scratch branch through these changesets:
[platform/upstream/busybox.git] / coreutils / uudecode.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
4  *
5  *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  *
7  *  Based on specification from
8  *  http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
9  *
10  *  Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
11  *        "end" line
12  */
13
14
15 #include "busybox.h"
16
17 static int read_stduu(FILE *src_stream, FILE *dst_stream)
18 {
19         char *line;
20
21         while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
22                 int length;
23                 char *line_ptr = line;
24
25                 if (strcmp(line, "end") == 0) {
26                         return(EXIT_SUCCESS);
27                 }
28                 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
29
30                 if (length <= 0) {
31                         /* Ignore the "`\n" line, why is it even in the encode file ? */
32                         continue;
33                 }
34                 if (length > 60) {
35                         bb_error_msg_and_die("Line too long");
36                 }
37
38                 line_ptr++;
39                 /* Tolerate an overly long line to acomadate a possible exta '`' */
40                 if (strlen(line_ptr) < (size_t)length) {
41                         bb_error_msg_and_die("Short file");
42                 }
43
44                 while (length > 0) {
45                         /* Merge four 6 bit chars to three 8 bit chars */
46                         fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
47                         line_ptr++;
48                         length--;
49                         if (length == 0) {
50                                 break;
51                         }
52
53                         fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
54                         line_ptr++;
55                         length--;
56                         if (length == 0) {
57                                 break;
58                         }
59
60                         fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
61                         line_ptr += 2;
62                         length -= 2;
63                 }
64                 free(line);
65         }
66         bb_error_msg_and_die("Short file");
67 }
68
69 static int read_base64(FILE *src_stream, FILE *dst_stream)
70 {
71         static const char base64_table[] =
72                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
73         char term_count = 0;
74
75         while (1) {
76                 char translated[4];
77                 int count = 0;
78
79                 while (count < 4) {
80                         char *table_ptr;
81                         int ch;
82
83                         /* Get next _valid_ character */
84                         do {
85                                 ch = fgetc(src_stream);
86                                 if (ch == EOF) {
87                                         bb_error_msg_and_die("Short file");
88                                 }
89                         } while ((table_ptr = strchr(base64_table, ch)) == NULL);
90
91                         /* Convert encoded charcter to decimal */
92                         ch = table_ptr - base64_table;
93
94                         if (*table_ptr == '=') {
95                                 if (term_count == 0) {
96                                         translated[count] = 0;
97                                         break;
98                                 }
99                                 term_count++;
100                         }
101                         else if (*table_ptr == '\n') {
102                                 /* Check for terminating line */
103                                 if (term_count == 5) {
104                                         return(EXIT_SUCCESS);
105                                 }
106                                 term_count = 1;
107                                 continue;
108                         } else {
109                                 translated[count] = ch;
110                                 count++;
111                                 term_count = 0;
112                         }
113                 }
114
115                 /* Merge 6 bit chars to 8 bit */
116             fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
117                 if (count > 2) {
118                         fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
119                 }
120                 if (count > 3) {
121                         fputc(translated[2] << 6 | translated[3], dst_stream);
122                 }
123         }
124 }
125
126 int uudecode_main(int argc, char **argv)
127 {
128         int (*decode_fn_ptr) (FILE * src, FILE * dst);
129         FILE *src_stream;
130         char *outname = NULL;
131         char *line;
132
133         bb_getopt_ulflags(argc, argv, "o:", &outname);
134
135         if (optind == argc) {
136                 src_stream = stdin;
137         } else if (optind + 1 == argc) {
138                 src_stream = xfopen(argv[optind], "r");
139         } else {
140                 bb_show_usage();
141         }
142
143         /* Search for the start of the encoding */
144         while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
145                 char *line_ptr = NULL;
146
147                 if (strncmp(line, "begin-base64 ", 13) == 0) {
148                         line_ptr = line + 13;
149                         decode_fn_ptr = read_base64;
150                 } else if (strncmp(line, "begin ", 6) == 0) {
151                         line_ptr = line + 6;
152                         decode_fn_ptr = read_stduu;
153                 }
154
155                 if (line_ptr) {
156                         FILE *dst_stream;
157                         int mode;
158                         int ret;
159
160                         mode = strtoul(line_ptr, NULL, 8);
161                         if (outname == NULL) {
162                                 outname = strchr(line_ptr, ' ');
163                                 if ((outname == NULL) || (*outname == '\0')) {
164                                         break;
165                                 }
166                                 outname++;
167                         }
168                         if (strcmp(outname, "-") == 0) {
169                                 dst_stream = stdout;
170                         } else {
171                                 dst_stream = xfopen(outname, "w");
172                                 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
173                         }
174                         free(line);
175                         ret = decode_fn_ptr(src_stream, dst_stream);
176                         bb_fclose_nonstdin(src_stream);
177                         return(ret);
178                 }
179                 free(line);
180         }
181         bb_error_msg_and_die("No `begin' line");
182 }