tar: postpone creation of symlinks with "suspicious" targets. Closes 8411
[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 //config:config UUDECODE
14 //config:       bool "uudecode (5.9 kb)"
15 //config:       default y
16 //config:       help
17 //config:       uudecode is used to decode a uuencoded file.
18
19 //applet:IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP))
20
21 //kbuild:lib-$(CONFIG_UUDECODE) += uudecode.o
22
23 //usage:#define uudecode_trivial_usage
24 //usage:       "[-o OUTFILE] [INFILE]"
25 //usage:#define uudecode_full_usage "\n\n"
26 //usage:       "Uudecode a file\n"
27 //usage:       "Finds OUTFILE in uuencoded source unless -o is given"
28 //usage:
29 //usage:#define uudecode_example_usage
30 //usage:       "$ uudecode -o busybox busybox.uu\n"
31 //usage:       "$ ls -l busybox\n"
32 //usage:       "-rwxr-xr-x   1 ams      ams        245264 Jun  7 21:35 busybox\n"
33
34 #include "libbb.h"
35
36 #if ENABLE_UUDECODE
37 static 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                 /* Handle both Unix and MSDOS text.
51                  * Note: space should not be trimmed, some encoders use it instead of "`"
52                  * for padding of last incomplete 4-char block.
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; /* the only non-error exit */
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                 /* Check that line is not too short. (we tolerate
74                  * overly _long_ line to accommodate possible extra "`").
75                  * Empty line case is also caught here. */
76                 if (str_len <= encoded_len) {
77                         break; /* go to bb_error_msg_and_die("short file"); */
78                 }
79                 if (encoded_len <= 0) {
80                         /* Ignore the "`\n" line, why is it even in the encode file ? */
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                         /* Merge four 6 bit chars to three 8 bit chars */
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
116 int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
117 int uudecode_main(int argc UNUSED_PARAM, char **argv)
118 {
119         FILE *src_stream;
120         char *outname = NULL;
121         char *line;
122
123         opt_complementary = "?1"; /* 1 argument max */
124         getopt32(argv, "o:", &outname);
125         argv += optind;
126
127         if (!argv[0])
128                 *--argv = (char*)"-";
129         src_stream = xfopen_stdin(argv[0]);
130
131         /* Search for the start of the encoding */
132         while ((line = xmalloc_fgetline(src_stream)) != NULL) {
133                 void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
134                 char *line_ptr;
135                 FILE *dst_stream;
136                 int mode;
137
138                 if (is_prefixed_with(line, "begin-base64 ")) {
139                         line_ptr = line + 13;
140                         decode_fn_ptr = read_base64;
141                 } else if (is_prefixed_with(line, "begin ")) {
142                         line_ptr = line + 6;
143                         decode_fn_ptr = read_stduu;
144                 } else {
145                         free(line);
146                         continue;
147                 }
148
149                 /* begin line found. decode and exit */
150                 mode = bb_strtou(line_ptr, NULL, 8);
151                 if (outname == NULL) {
152                         outname = strchr(line_ptr, ' ');
153                         if (!outname)
154                                 break;
155                         outname++;
156                         trim(outname); /* remove trailing space (and '\r' for DOS text) */
157                         if (!outname[0])
158                                 break;
159                 }
160                 dst_stream = stdout;
161                 if (NOT_LONE_DASH(outname)) {
162                         dst_stream = xfopen_for_write(outname);
163                         fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
164                 }
165                 free(line);
166                 decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
167                 /* fclose_if_not_stdin(src_stream); - redundant */
168                 return EXIT_SUCCESS;
169         }
170         bb_error_msg_and_die("no 'begin' line");
171 }
172 #endif
173
174 //applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP))
175
176 //kbuild:lib-$(CONFIG_BASE64) += uudecode.o
177
178 //config:config BASE64
179 //config:       bool "base64 (5 kb)"
180 //config:       default y
181 //config:       help
182 //config:       Base64 encode and decode
183
184 //usage:#define base64_trivial_usage
185 //usage:        "[-d] [FILE]"
186 //usage:#define base64_full_usage "\n\n"
187 //usage:       "Base64 encode or decode FILE to standard output"
188 //usage:     "\n        -d      Decode data"
189 ////usage:     "\n      -w COL  Wrap lines at COL (default 76, 0 disables)"
190 ////usage:     "\n      -i      When decoding, ignore non-alphabet characters"
191
192 #if ENABLE_BASE64
193 int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
194 int base64_main(int argc UNUSED_PARAM, char **argv)
195 {
196         FILE *src_stream;
197         unsigned opts;
198
199         opt_complementary = "?1"; /* 1 argument max */
200         opts = getopt32(argv, "d");
201         argv += optind;
202
203         if (!argv[0])
204                 *--argv = (char*)"-";
205         src_stream = xfopen_stdin(argv[0]);
206         if (opts) {
207                 read_base64(src_stream, stdout, /*flags:*/ (char)EOF);
208         } else {
209                 enum {
210                         SRC_BUF_SIZE = 76/4*3,  /* This *MUST* be a multiple of 3 */
211                         DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
212                 };
213                 char src_buf[SRC_BUF_SIZE];
214                 char dst_buf[DST_BUF_SIZE + 1];
215                 int src_fd = fileno(src_stream);
216                 while (1) {
217                         size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
218                         if (!size)
219                                 break;
220                         if ((ssize_t)size < 0)
221                                 bb_perror_msg_and_die(bb_msg_read_error);
222                         /* Encode the buffer we just read in */
223                         bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
224                         xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
225                         bb_putchar('\n');
226                         fflush(stdout);
227                 }
228         }
229
230         fflush_stdout_and_exit(EXIT_SUCCESS);
231 }
232 #endif
233
234 /* Test script.
235 Put this into an empty dir with busybox binary, an run.
236
237 #!/bin/sh
238 test -x busybox || { echo "No ./busybox?"; exit; }
239 ln -sf busybox uudecode
240 ln -sf busybox uuencode
241 >A_null
242 echo -n A >A
243 echo -n AB >AB
244 echo -n ABC >ABC
245 echo -n ABCD >ABCD
246 echo -n ABCDE >ABCDE
247 echo -n ABCDEF >ABCDEF
248 cat busybox >A_bbox
249 for f in A*; do
250     echo uuencode $f
251     ./uuencode    $f <$f >u_$f
252     ./uuencode -m $f <$f >m_$f
253 done
254 mkdir unpk_u unpk_m 2>/dev/null
255 for f in u_*; do
256     ./uudecode <$f -o unpk_u/${f:2}
257     diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
258     echo uudecode $f: $?
259 done
260 for f in m_*; do
261     ./uudecode <$f -o unpk_m/${f:2}
262     diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
263     echo uudecode $f: $?
264 done
265 */