06211b8ef1888c9cc387201f5efe3900d5d11f08
[oweals/busybox.git] / coreutils / uudecode.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright 2003, Glenn McGrath
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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 #include "libbb.h"
15
16 static void read_stduu(FILE *src_stream, FILE *dst_stream)
17 {
18         char *line;
19
20         while ((line = xmalloc_fgetline(src_stream)) != NULL) {
21                 int encoded_len, str_len;
22                 char *line_ptr, *dst;
23
24                 if (strcmp(line, "end") == 0) {
25                         return; /* the only non-error exit */
26                 }
27
28                 line_ptr = line;
29                 while (*line_ptr) {
30                         *line_ptr = (*line_ptr - 0x20) & 0x3f;
31                         line_ptr++;
32                 }
33                 str_len = line_ptr - line;
34
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"); */
41                 }
42                 if (encoded_len <= 0) {
43                         /* Ignore the "`\n" line, why is it even in the encode file ? */
44                         free(line);
45                         continue;
46                 }
47                 if (encoded_len > 60) {
48                         bb_error_msg_and_die("line too long");
49                 }
50
51                 dst = line;
52                 line_ptr = line + 1;
53                 do {
54                         /* Merge four 6 bit chars to three 8 bit chars */
55                         *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
56                         encoded_len--;
57                         if (encoded_len == 0) {
58                                 break;
59                         }
60
61                         *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
62                         encoded_len--;
63                         if (encoded_len == 0) {
64                                 break;
65                         }
66
67                         *dst++ = line_ptr[2] << 6 | line_ptr[3];
68                         line_ptr += 4;
69                         encoded_len -= 2;
70                 } while (encoded_len > 0);
71                 fwrite(line, 1, dst - line, dst_stream);
72                 free(line);
73         }
74         bb_error_msg_and_die("short file");
75 }
76
77 static void read_base64(FILE *src_stream, FILE *dst_stream)
78 {
79         int term_count = 1;
80
81         while (1) {
82                 char translated[4];
83                 int count = 0;
84
85                 while (count < 4) {
86                         char *table_ptr;
87                         int ch;
88
89                         /* Get next _valid_ character.
90                          * global vector bb_uuenc_tbl_base64[] contains this string:
91                          * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
92                          */
93                         do {
94                                 ch = fgetc(src_stream);
95                                 if (ch == EOF) {
96                                         bb_error_msg_and_die("short file");
97                                 }
98                                 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
99                         } while (table_ptr == NULL);
100
101                         /* Convert encoded character to decimal */
102                         ch = table_ptr - bb_uuenc_tbl_base64;
103
104                         if (*table_ptr == '=') {
105                                 if (term_count == 0) {
106                                         translated[count] = '\0';
107                                         break;
108                                 }
109                                 term_count++;
110                         } else if (*table_ptr == '\n') {
111                                 /* Check for terminating line */
112                                 if (term_count == 5) {
113                                         return;
114                                 }
115                                 term_count = 1;
116                                 continue;
117                         } else {
118                                 translated[count] = ch;
119                                 count++;
120                                 term_count = 0;
121                         }
122                 }
123
124                 /* Merge 6 bit chars to 8 bit */
125                 if (count > 1) {
126                         fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
127                 }
128                 if (count > 2) {
129                         fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
130                 }
131                 if (count > 3) {
132                         fputc(translated[2] << 6 | translated[3], dst_stream);
133                 }
134         }
135 }
136
137 int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
138 int uudecode_main(int argc UNUSED_PARAM, char **argv)
139 {
140         FILE *src_stream;
141         char *outname = NULL;
142         char *line;
143
144         opt_complementary = "?1"; /* 1 argument max */
145         getopt32(argv, "o:", &outname);
146         argv += optind;
147
148         if (!*argv)
149                 *--argv = (char*)"-";
150         src_stream = xfopen_stdin(*argv);
151
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);
155                 char *line_ptr;
156                 FILE *dst_stream;
157                 int mode;
158
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) {
163                         line_ptr = line + 6;
164                         decode_fn_ptr = read_stduu;
165                 } else {
166                         free(line);
167                         continue;
168                 }
169
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')) {
175                                 break;
176                         }
177                         outname++;
178                 }
179                 dst_stream = stdout;
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));
183                 }
184                 free(line);
185                 decode_fn_ptr(src_stream, dst_stream);
186                 /* fclose_if_not_stdin(src_stream); - redundant */
187                 return EXIT_SUCCESS;
188         }
189         bb_error_msg_and_die("no 'begin' line");
190 }
191
192 /* Test script.
193 Put this into an empty dir with busybox binary, an run.
194
195 #!/bin/sh
196 test -x busybox || { echo "No ./busybox?"; exit; }
197 ln -sf busybox uudecode
198 ln -sf busybox uuencode
199 >A_null
200 echo -n A >A
201 echo -n AB >AB
202 echo -n ABC >ABC
203 echo -n ABCD >ABCD
204 echo -n ABCDE >ABCDE
205 echo -n ABCDEF >ABCDEF
206 cat busybox >A_bbox
207 for f in A*; do
208     echo uuencode $f
209     ./uuencode    $f <$f >u_$f
210     ./uuencode -m $f <$f >m_$f
211 done
212 mkdir unpk_u unpk_m 2>/dev/null
213 for f in u_*; do
214     ./uudecode <$f -o unpk_u/${f:2}
215     diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
216     echo uudecode $f: $?
217 done
218 for f in m_*; do
219     ./uudecode <$f -o unpk_m/${f:2}
220     diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
221     echo uudecode $f: $?
222 done
223 */