blob: 0c4311f2464274a31ad06440291f4e071327f393 [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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000014
Denys Vlasenkoee062642010-08-31 14:09:22 +020015#if ENABLE_UUDECODE
Denys Vlasenko9fe98f72010-09-16 17:51:13 +020016static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000017{
Glenn L McGrath7f9de022003-11-06 03:17:23 +000018 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000019
Denis Vlasenko8ee649a2008-03-26 20:04:27 +000020 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000021 int encoded_len, str_len;
22 char *line_ptr, *dst;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000023
Glenn L McGrath7f9de022003-11-06 03:17:23 +000024 if (strcmp(line, "end") == 0) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000025 return; /* the only non-error exit */
Glenn L McGrath7f9de022003-11-06 03:17:23 +000026 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000027
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000028 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) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000043 /* Ignore the "`\n" line, why is it even in the encode file ? */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000044 free(line);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000045 continue;
46 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000047 if (encoded_len > 60) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000048 bb_error_msg_and_die("line too long");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000049 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000051 dst = line;
52 line_ptr = line + 1;
53 do {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000054 /* Merge four 6 bit chars to three 8 bit chars */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000055 *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
56 encoded_len--;
57 if (encoded_len == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000058 break;
59 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000060
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000061 *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
62 encoded_len--;
63 if (encoded_len == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000064 break;
65 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000066
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000067 *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);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000072 free(line);
73 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000074 bb_error_msg_and_die("short file");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000075}
Denys Vlasenkoee062642010-08-31 14:09:22 +020076#endif
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000077
Denys Vlasenkoee062642010-08-31 14:09:22 +020078#if ENABLE_UUDECODE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000079int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000080int uudecode_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000081{
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000082 FILE *src_stream;
Glenn L McGrath7f9de022003-11-06 03:17:23 +000083 char *outname = NULL;
84 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000085
Denis Vlasenko746204b2007-06-04 23:32:35 +000086 opt_complementary = "?1"; /* 1 argument max */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000087 getopt32(argv, "o:", &outname);
Denis Vlasenko746204b2007-06-04 23:32:35 +000088 argv += optind;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000089
Denys Vlasenkoee062642010-08-31 14:09:22 +020090 if (!argv[0])
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000091 *--argv = (char*)"-";
Denys Vlasenkoee062642010-08-31 14:09:22 +020092 src_stream = xfopen_stdin(argv[0]);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000093
Glenn L McGrath7f9de022003-11-06 03:17:23 +000094 /* Search for the start of the encoding */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +000095 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
Denys Vlasenko9fe98f72010-09-16 17:51:13 +020096 void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
Rob Landley29d94b92006-09-23 19:56:21 +000097 char *line_ptr;
98 FILE *dst_stream;
99 int mode;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000100
Rob Landley565bc332006-07-31 22:50:12 +0000101 if (strncmp(line, "begin-base64 ", 13) == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000102 line_ptr = line + 13;
103 decode_fn_ptr = read_base64;
104 } else if (strncmp(line, "begin ", 6) == 0) {
105 line_ptr = line + 6;
106 decode_fn_ptr = read_stduu;
Rob Landley29d94b92006-09-23 19:56:21 +0000107 } else {
108 free(line);
109 continue;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000110 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000111
Denis Vlasenko746204b2007-06-04 23:32:35 +0000112 /* begin line found. decode and exit */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000113 mode = bb_strtou(line_ptr, NULL, 8);
Rob Landley29d94b92006-09-23 19:56:21 +0000114 if (outname == NULL) {
115 outname = strchr(line_ptr, ' ');
116 if ((outname == NULL) || (*outname == '\0')) {
117 break;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000118 }
Rob Landley29d94b92006-09-23 19:56:21 +0000119 outname++;
120 }
Denis Vlasenko746204b2007-06-04 23:32:35 +0000121 dst_stream = stdout;
122 if (NOT_LONE_DASH(outname)) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000123 dst_stream = xfopen_for_write(outname);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000124 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000125 }
126 free(line);
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200127 decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000128 /* fclose_if_not_stdin(src_stream); - redundant */
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000129 return EXIT_SUCCESS;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000130 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000131 bb_error_msg_and_die("no 'begin' line");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000132}
Denys Vlasenkoee062642010-08-31 14:09:22 +0200133#endif
134
135//applet:IF_BASE64(APPLET(base64, _BB_DIR_BIN, _BB_SUID_DROP))
136
137//kbuild:lib-$(CONFIG_BASE64) += uudecode.o
138
139//config:config BASE64
140//config: bool "base64"
141//config: default y
142//config: help
143//config: Base64 encode and decode
144
145//usage:#define base64_trivial_usage
146//usage: "[-d] [FILE]"
147//usage:#define base64_full_usage "\n\n"
148//usage: "Base64 encode or decode FILE to standard output"
149//usage: "\nOptions:"
150//usage: "\n -d Decode data"
151////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
152////usage: "\n -i When decoding, ignore non-alphabet characters"
153
154#if ENABLE_BASE64
155int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
156int base64_main(int argc UNUSED_PARAM, char **argv)
157{
158 FILE *src_stream;
159 unsigned opts;
160
161 opt_complementary = "?1"; /* 1 argument max */
162 opts = getopt32(argv, "d");
163 argv += optind;
164
165 if (!argv[0])
166 *--argv = (char*)"-";
167 src_stream = xfopen_stdin(argv[0]);
168 if (opts) {
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200169 read_base64(src_stream, stdout, /*flags:*/ (char)EOF);
Denys Vlasenkoee062642010-08-31 14:09:22 +0200170 } else {
171 enum {
172 SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */
173 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
174 };
175 char src_buf[SRC_BUF_SIZE];
176 char dst_buf[DST_BUF_SIZE + 1];
177 int src_fd = fileno(src_stream);
178 while (1) {
179 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
180 if (!size)
181 break;
182 if ((ssize_t)size < 0)
183 bb_perror_msg_and_die(bb_msg_read_error);
184 /* Encode the buffer we just read in */
185 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
186 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
187 bb_putchar('\n');
188 fflush(stdout);
189 }
190 }
191
192 fflush_stdout_and_exit(EXIT_SUCCESS);
193}
194#endif
Denis Vlasenko746204b2007-06-04 23:32:35 +0000195
196/* Test script.
197Put this into an empty dir with busybox binary, an run.
198
199#!/bin/sh
200test -x busybox || { echo "No ./busybox?"; exit; }
201ln -sf busybox uudecode
202ln -sf busybox uuencode
203>A_null
204echo -n A >A
205echo -n AB >AB
206echo -n ABC >ABC
207echo -n ABCD >ABCD
208echo -n ABCDE >ABCDE
209echo -n ABCDEF >ABCDEF
210cat busybox >A_bbox
211for f in A*; do
212 echo uuencode $f
213 ./uuencode $f <$f >u_$f
214 ./uuencode -m $f <$f >m_$f
215done
216mkdir unpk_u unpk_m 2>/dev/null
217for f in u_*; do
218 ./uudecode <$f -o unpk_u/${f:2}
219 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
220 echo uudecode $f: $?
221done
222for f in m_*; do
223 ./uudecode <$f -o unpk_m/${f:2}
224 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
225 echo uudecode $f: $?
226done
227*/