1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include "libbb.h"
35
36#if ENABLE_UUDECODE
37static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM)
38{
39 char *line;
40
41 for (;;) {
42 int encoded_len, str_len;
43 char *line_ptr, *dst;
44 size_t line_len;
45
46 line_len = 64 * 1024;
47 line = xmalloc_fgets_str_len(src_stream, "\n", &line_len);
48 if (!line)
49 break;
50
51
52
53
54 str_len = line_len;
55 while (--str_len >= 0
56 && (line[str_len] == '\n' || line[str_len] == '\r')
57 ) {
58 line[str_len] = '\0';
59 }
60
61 if (strcmp(line, "end") == 0) {
62 return;
63 }
64
65 line_ptr = line;
66 while (*line_ptr) {
67 *line_ptr = (*line_ptr - 0x20) & 0x3f;
68 line_ptr++;
69 }
70 str_len = line_ptr - line;
71
72 encoded_len = line[0] * 4 / 3;
73
74
75
76 if (str_len <= encoded_len) {
77 break;
78 }
79 if (encoded_len <= 0) {
80
81 free(line);
82 continue;
83 }
84 if (encoded_len > 60) {
85 bb_error_msg_and_die("line too long");
86 }
87
88 dst = line;
89 line_ptr = line + 1;
90 do {
91
92 *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
93 encoded_len--;
94 if (encoded_len == 0) {
95 break;
96 }
97
98 *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
99 encoded_len--;
100 if (encoded_len == 0) {
101 break;
102 }
103
104 *dst++ = line_ptr[2] << 6 | line_ptr[3];
105 line_ptr += 4;
106 encoded_len -= 2;
107 } while (encoded_len > 0);
108 fwrite(line, 1, dst - line, dst_stream);
109 free(line);
110 }
111 bb_error_msg_and_die("short file");
112}
113#endif
114
115#if ENABLE_UUDECODE
116int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
117int uudecode_main(int argc UNUSED_PARAM, char **argv)
118{
119 FILE *src_stream;
120 char *outname = NULL;
121 char *line;
122
123 getopt32(argv, "^" "o:" "\0" "?1", &outname);
124 argv += optind;
125
126 if (!argv[0])
127 *--argv = (char*)"-";
128 src_stream = xfopen_stdin(argv[0]);
129
130
131 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
132 void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
133 char *line_ptr;
134 FILE *dst_stream;
135 int mode;
136
137 if (is_prefixed_with(line, "begin-base64 ")) {
138 line_ptr = line + 13;
139 decode_fn_ptr = read_base64;
140 } else if (is_prefixed_with(line, "begin ")) {
141 line_ptr = line + 6;
142 decode_fn_ptr = read_stduu;
143 } else {
144 free(line);
145 continue;
146 }
147
148
149 mode = bb_strtou(line_ptr, NULL, 8);
150 if (outname == NULL) {
151 outname = strchr(line_ptr, ' ');
152 if (!outname)
153 break;
154 outname++;
155 trim(outname);
156 if (!outname[0])
157 break;
158 }
159 dst_stream = stdout;
160 if (NOT_LONE_DASH(outname)) {
161 dst_stream = xfopen_for_write(outname);
162 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
163 }
164 free(line);
165 decode_fn_ptr(src_stream, dst_stream, BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
166
167 return EXIT_SUCCESS;
168 }
169 bb_error_msg_and_die("no 'begin' line");
170}
171#endif
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191#if ENABLE_BASE64
192int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
193int base64_main(int argc UNUSED_PARAM, char **argv)
194{
195 FILE *src_stream;
196 unsigned opts;
197
198 opts = getopt32(argv, "^" "d" "\0" "?1");
199 argv += optind;
200
201 if (!argv[0])
202 *--argv = (char*)"-";
203 src_stream = xfopen_stdin(argv[0]);
204 if (opts) {
205 read_base64(src_stream, stdout, (char)EOF);
206 } else {
207 enum {
208 SRC_BUF_SIZE = 76/4*3,
209 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
210 };
211 char src_buf[SRC_BUF_SIZE];
212 char dst_buf[DST_BUF_SIZE + 1];
213 int src_fd = fileno(src_stream);
214 while (1) {
215 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
216 if (!size)
217 break;
218 if ((ssize_t)size < 0)
219 bb_perror_msg_and_die(bb_msg_read_error);
220
221 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
222 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
223 bb_putchar('\n');
224 fflush(stdout);
225 }
226 }
227
228 fflush_stdout_and_exit(EXIT_SUCCESS);
229}
230#endif
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264