blob: 7164f838a232cf0539a45c778efaf132566f4635 [file] [log] [blame]
Glenn L McGrath089deca2001-07-28 21:06:13 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2000 by Glenn McGrath
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00004 *
Glenn L McGrath089deca2001-07-28 21:06:13 +00005 * based on the function base64_encode from http.c in wget v1.6
6 * Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00009 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010010//config:config UUENCODE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "uuencode (4.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: uuencode is used to uuencode a file.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015
16//applet:IF_UUENCODE(APPLET(uuencode, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_UUENCODE) += uuencode.o
Rob Landleyd921b2e2006-08-03 15:41:12 +000019
Pere Orga34425382011-03-31 14:43:25 +020020//usage:#define uuencode_trivial_usage
Denys Vlasenkofa737ce2012-03-05 14:22:33 +010021//usage: "[-m] [FILE] STORED_FILENAME"
Pere Orga34425382011-03-31 14:43:25 +020022//usage:#define uuencode_full_usage "\n\n"
Denys Vlasenkofa737ce2012-03-05 14:22:33 +010023//usage: "Uuencode FILE (or stdin) to stdout\n"
Pere Orga34425382011-03-31 14:43:25 +020024//usage: "\n -m Use base64 encoding per RFC1521"
25//usage:
26//usage:#define uuencode_example_usage
27//usage: "$ uuencode busybox busybox\n"
28//usage: "begin 755 busybox\n"
29//usage: "<encoded file snipped>\n"
30//usage: "$ uudecode busybox busybox > busybox.uu\n"
31//usage: "$\n"
32
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000034
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000035enum {
Denys Vlasenkoee062642010-08-31 14:09:22 +020036 SRC_BUF_SIZE = 15*3, /* This *MUST* be a multiple of 3 */
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000037 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
38};
Eric Andersen4e573f42000-11-14 23:29:24 +000039
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000040int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010041int uuencode_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath089deca2001-07-28 21:06:13 +000042{
Glenn L McGrath089deca2001-07-28 21:06:13 +000043 struct stat stat_buf;
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000044 int src_fd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 const char *tbl;
Glenn L McGrath089deca2001-07-28 21:06:13 +000046 mode_t mode;
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000047 char src_buf[SRC_BUF_SIZE];
48 char dst_buf[DST_BUF_SIZE + 1];
Glenn L McGrath089deca2001-07-28 21:06:13 +000049
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000050 tbl = bb_uuenc_tbl_std;
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000051 mode = 0666 & ~umask(0666);
52 opt_complementary = "-1:?2"; /* must have 1 or 2 args */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000053 if (getopt32(argv, "m")) {
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000054 tbl = bb_uuenc_tbl_base64;
Glenn L McGrath089deca2001-07-28 21:06:13 +000055 }
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000056 argv += optind;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010057 if (argv[1]) {
Denys Vlasenkoee062642010-08-31 14:09:22 +020058 src_fd = xopen(argv[0], O_RDONLY);
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000059 fstat(src_fd, &stat_buf);
60 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
61 argv++;
Glenn L McGrath089deca2001-07-28 21:06:13 +000062 }
63
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000064 printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv);
65 while (1) {
66 size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
67 if (!size)
68 break;
69 if ((ssize_t)size < 0)
70 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath089deca2001-07-28 21:06:13 +000071 /* Encode the buffer we just read in */
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000072 bb_uuencode(dst_buf, src_buf, size, tbl);
Denis Vlasenko4daad902007-09-27 10:20:47 +000073 bb_putchar('\n');
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000074 if (tbl == bb_uuenc_tbl_std) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000075 bb_putchar(tbl[size]);
Tim Riker95bf6da2002-05-01 05:57:16 +000076 }
Denis Vlasenkoe8240f12007-06-26 15:59:37 +000077 fflush(stdout);
78 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
Glenn L McGrath089deca2001-07-28 21:06:13 +000079 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000080 printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
Glenn L McGrath089deca2001-07-28 21:06:13 +000081
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000082 fflush_stdout_and_exit(EXIT_SUCCESS);
Glenn L McGrath089deca2001-07-28 21:06:13 +000083}