blob: ddce2548b87b7b77dd546ef602e46c7b365e3e1d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath7f9de022003-11-06 03:17:23 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Copyright 2003, Glenn McGrath
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen4e573f42000-11-14 23:29:24 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Based on specification from
8 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
Eric Andersen4e573f42000-11-14 23:29:24 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
11 * "end" line
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000012 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013//config:config UUDECODE
14//config: bool "uudecode"
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
Pere Orga34425382011-03-31 14:43:25 +020022
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"
Denys Vlasenko11e61d52012-03-05 14:23:26 +010027//usage: "Finds OUTFILE in uuencoded source unless -o is given"
Pere Orga34425382011-03-31 14:43:25 +020028//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000035
Denys Vlasenkoee062642010-08-31 14:09:22 +020036#if ENABLE_UUDECODE
Denys Vlasenko9fe98f72010-09-16 17:51:13 +020037static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000038{
Glenn L McGrath7f9de022003-11-06 03:17:23 +000039 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000040
Denys Vlasenko2b48c382015-10-05 15:10:44 +020041 for (;;) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000042 int encoded_len, str_len;
43 char *line_ptr, *dst;
Denys Vlasenko2b48c382015-10-05 15:10:44 +020044 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, and stray trailing spaces */
51 str_len = line_len;
52 while (--str_len >= 0 && isspace(line[str_len]))
53 line[str_len] = '\0';
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000054
Glenn L McGrath7f9de022003-11-06 03:17:23 +000055 if (strcmp(line, "end") == 0) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000056 return; /* the only non-error exit */
Glenn L McGrath7f9de022003-11-06 03:17:23 +000057 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000058
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000059 line_ptr = line;
60 while (*line_ptr) {
61 *line_ptr = (*line_ptr - 0x20) & 0x3f;
62 line_ptr++;
63 }
64 str_len = line_ptr - line;
65
66 encoded_len = line[0] * 4 / 3;
67 /* Check that line is not too short. (we tolerate
Maninder Singh97c64912015-05-25 13:46:36 +020068 * overly _long_ line to accommodate possible extra '`').
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000069 * Empty line case is also caught here. */
70 if (str_len <= encoded_len) {
71 break; /* go to bb_error_msg_and_die("short file"); */
72 }
73 if (encoded_len <= 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000074 /* Ignore the "`\n" line, why is it even in the encode file ? */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000075 free(line);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000076 continue;
77 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000078 if (encoded_len > 60) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000079 bb_error_msg_and_die("line too long");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000080 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000081
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000082 dst = line;
83 line_ptr = line + 1;
84 do {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000085 /* Merge four 6 bit chars to three 8 bit chars */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000086 *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
87 encoded_len--;
88 if (encoded_len == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000089 break;
90 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000091
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000092 *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
93 encoded_len--;
94 if (encoded_len == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000095 break;
96 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000097
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000098 *dst++ = line_ptr[2] << 6 | line_ptr[3];
99 line_ptr += 4;
100 encoded_len -= 2;
101 } while (encoded_len > 0);
102 fwrite(line, 1, dst - line, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000103 free(line);
104 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000105 bb_error_msg_and_die("short file");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000106}
Denys Vlasenkoee062642010-08-31 14:09:22 +0200107#endif
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000108
Denys Vlasenkoee062642010-08-31 14:09:22 +0200109#if ENABLE_UUDECODE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000110int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000111int uudecode_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000112{
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000113 FILE *src_stream;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000114 char *outname = NULL;
115 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000116
Denis Vlasenko746204b2007-06-04 23:32:35 +0000117 opt_complementary = "?1"; /* 1 argument max */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000118 getopt32(argv, "o:", &outname);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000119 argv += optind;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000120
Denys Vlasenkoee062642010-08-31 14:09:22 +0200121 if (!argv[0])
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000122 *--argv = (char*)"-";
Denys Vlasenkoee062642010-08-31 14:09:22 +0200123 src_stream = xfopen_stdin(argv[0]);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000124
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000125 /* Search for the start of the encoding */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000126 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200127 void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
Rob Landley29d94b92006-09-23 19:56:21 +0000128 char *line_ptr;
129 FILE *dst_stream;
130 int mode;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000131
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100132 if (is_prefixed_with(line, "begin-base64 ")) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000133 line_ptr = line + 13;
134 decode_fn_ptr = read_base64;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100135 } else if (is_prefixed_with(line, "begin ")) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000136 line_ptr = line + 6;
137 decode_fn_ptr = read_stduu;
Rob Landley29d94b92006-09-23 19:56:21 +0000138 } else {
139 free(line);
140 continue;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000141 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000142
Denis Vlasenko746204b2007-06-04 23:32:35 +0000143 /* begin line found. decode and exit */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000144 mode = bb_strtou(line_ptr, NULL, 8);
Rob Landley29d94b92006-09-23 19:56:21 +0000145 if (outname == NULL) {
146 outname = strchr(line_ptr, ' ');
Denys Vlasenko5f920432011-10-18 12:07:05 +0200147 if (!outname)
Rob Landley29d94b92006-09-23 19:56:21 +0000148 break;
Rob Landley29d94b92006-09-23 19:56:21 +0000149 outname++;
Denys Vlasenko2b48c382015-10-05 15:10:44 +0200150 trim(outname); /* remove trailing space (and '\r' for DOS text) */
Denys Vlasenko5f920432011-10-18 12:07:05 +0200151 if (!outname[0])
152 break;
Rob Landley29d94b92006-09-23 19:56:21 +0000153 }
Denis Vlasenko746204b2007-06-04 23:32:35 +0000154 dst_stream = stdout;
155 if (NOT_LONE_DASH(outname)) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000156 dst_stream = xfopen_for_write(outname);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000157 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000158 }
159 free(line);
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200160 decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000161 /* fclose_if_not_stdin(src_stream); - redundant */
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000162 return EXIT_SUCCESS;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000163 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000164 bb_error_msg_and_die("no 'begin' line");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000165}
Denys Vlasenkoee062642010-08-31 14:09:22 +0200166#endif
167
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100168//applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenkoee062642010-08-31 14:09:22 +0200169
170//kbuild:lib-$(CONFIG_BASE64) += uudecode.o
171
172//config:config BASE64
173//config: bool "base64"
174//config: default y
175//config: help
176//config: Base64 encode and decode
177
178//usage:#define base64_trivial_usage
179//usage: "[-d] [FILE]"
180//usage:#define base64_full_usage "\n\n"
181//usage: "Base64 encode or decode FILE to standard output"
Denys Vlasenkoee062642010-08-31 14:09:22 +0200182//usage: "\n -d Decode data"
183////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
184////usage: "\n -i When decoding, ignore non-alphabet characters"
185
186#if ENABLE_BASE64
187int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
188int base64_main(int argc UNUSED_PARAM, char **argv)
189{
190 FILE *src_stream;
191 unsigned opts;
192
193 opt_complementary = "?1"; /* 1 argument max */
194 opts = getopt32(argv, "d");
195 argv += optind;
196
197 if (!argv[0])
198 *--argv = (char*)"-";
199 src_stream = xfopen_stdin(argv[0]);
200 if (opts) {
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200201 read_base64(src_stream, stdout, /*flags:*/ (char)EOF);
Denys Vlasenkoee062642010-08-31 14:09:22 +0200202 } else {
203 enum {
204 SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */
205 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
206 };
207 char src_buf[SRC_BUF_SIZE];
208 char dst_buf[DST_BUF_SIZE + 1];
209 int src_fd = fileno(src_stream);
210 while (1) {
211 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
212 if (!size)
213 break;
214 if ((ssize_t)size < 0)
215 bb_perror_msg_and_die(bb_msg_read_error);
216 /* Encode the buffer we just read in */
217 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
218 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
219 bb_putchar('\n');
220 fflush(stdout);
221 }
222 }
223
224 fflush_stdout_and_exit(EXIT_SUCCESS);
225}
226#endif
Denis Vlasenko746204b2007-06-04 23:32:35 +0000227
228/* Test script.
229Put this into an empty dir with busybox binary, an run.
230
231#!/bin/sh
232test -x busybox || { echo "No ./busybox?"; exit; }
233ln -sf busybox uudecode
234ln -sf busybox uuencode
235>A_null
236echo -n A >A
237echo -n AB >AB
238echo -n ABC >ABC
239echo -n ABCD >ABCD
240echo -n ABCDE >ABCDE
241echo -n ABCDEF >ABCDEF
242cat busybox >A_bbox
243for f in A*; do
244 echo uuencode $f
245 ./uuencode $f <$f >u_$f
246 ./uuencode -m $f <$f >m_$f
247done
248mkdir unpk_u unpk_m 2>/dev/null
249for f in u_*; do
250 ./uudecode <$f -o unpk_u/${f:2}
251 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
252 echo uudecode $f: $?
253done
254for f in m_*; do
255 ./uudecode <$f -o unpk_m/${f:2}
256 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
257 echo uudecode $f: $?
258done
259*/