Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame^] | 3 | * Copyright (C) 2000 by Glenn McGrath |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame^] | 5 | * 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 Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 10 | //config:config UUENCODE |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "uuencode (4.6 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: uuencode is used to uuencode a file. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | |
| 16 | //applet:IF_UUENCODE(APPLET(uuencode, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_UUENCODE) += uuencode.o |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 19 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 20 | //usage:#define uuencode_trivial_usage |
Denys Vlasenko | fa737ce | 2012-03-05 14:22:33 +0100 | [diff] [blame] | 21 | //usage: "[-m] [FILE] STORED_FILENAME" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 22 | //usage:#define uuencode_full_usage "\n\n" |
Denys Vlasenko | fa737ce | 2012-03-05 14:22:33 +0100 | [diff] [blame] | 23 | //usage: "Uuencode FILE (or stdin) to stdout\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 24 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 33 | #include "libbb.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 35 | enum { |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 36 | SRC_BUF_SIZE = 15*3, /* This *MUST* be a multiple of 3 */ |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 37 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 38 | }; |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 40 | int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 41 | int uuencode_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 42 | { |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 43 | struct stat stat_buf; |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 44 | int src_fd = STDIN_FILENO; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | const char *tbl; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 46 | mode_t mode; |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 47 | char src_buf[SRC_BUF_SIZE]; |
| 48 | char dst_buf[DST_BUF_SIZE + 1]; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 50 | tbl = bb_uuenc_tbl_std; |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 51 | mode = 0666 & ~umask(0666); |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 52 | if (getopt32(argv, "^" "m" "\0" "-1:?2"/*must have 1 or 2 args*/)) { |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 53 | tbl = bb_uuenc_tbl_base64; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 54 | } |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 55 | argv += optind; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 56 | if (argv[1]) { |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 57 | src_fd = xopen(argv[0], O_RDONLY); |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 58 | fstat(src_fd, &stat_buf); |
| 59 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 60 | argv++; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 63 | printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv); |
| 64 | while (1) { |
| 65 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); |
| 66 | if (!size) |
| 67 | break; |
| 68 | if ((ssize_t)size < 0) |
| 69 | bb_perror_msg_and_die(bb_msg_read_error); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 70 | /* Encode the buffer we just read in */ |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 71 | bb_uuencode(dst_buf, src_buf, size, tbl); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 72 | bb_putchar('\n'); |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 73 | if (tbl == bb_uuenc_tbl_std) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 74 | bb_putchar(tbl[size]); |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 75 | } |
Denis Vlasenko | e8240f1 | 2007-06-26 15:59:37 +0000 | [diff] [blame] | 76 | fflush(stdout); |
| 77 | xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 78 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 79 | printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 81 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 82 | } |