[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Copyright (C) 2000 by Glenn McGrath
|
---|
| 4 | *
|
---|
| 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.
|
---|
| 7 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 9 | */
|
---|
| 10 |
|
---|
[3232] | 11 | //usage:#define uuencode_trivial_usage
|
---|
| 12 | //usage: "[-m] [FILE] STORED_FILENAME"
|
---|
| 13 | //usage:#define uuencode_full_usage "\n\n"
|
---|
| 14 | //usage: "Uuencode FILE (or stdin) to stdout\n"
|
---|
| 15 | //usage: "\n -m Use base64 encoding per RFC1521"
|
---|
| 16 | //usage:
|
---|
| 17 | //usage:#define uuencode_example_usage
|
---|
| 18 | //usage: "$ uuencode busybox busybox\n"
|
---|
| 19 | //usage: "begin 755 busybox\n"
|
---|
| 20 | //usage: "<encoded file snipped>\n"
|
---|
| 21 | //usage: "$ uudecode busybox busybox > busybox.uu\n"
|
---|
| 22 | //usage: "$\n"
|
---|
| 23 |
|
---|
[1765] | 24 | #include "libbb.h"
|
---|
[821] | 25 |
|
---|
[1765] | 26 | enum {
|
---|
[2725] | 27 | SRC_BUF_SIZE = 15*3, /* This *MUST* be a multiple of 3 */
|
---|
[1765] | 28 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
|
---|
[821] | 29 | };
|
---|
| 30 |
|
---|
[2725] | 31 | int uuencode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 32 | int uuencode_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 33 | {
|
---|
| 34 | struct stat stat_buf;
|
---|
[1765] | 35 | int src_fd = STDIN_FILENO;
|
---|
[821] | 36 | const char *tbl;
|
---|
| 37 | mode_t mode;
|
---|
[1765] | 38 | char src_buf[SRC_BUF_SIZE];
|
---|
| 39 | char dst_buf[DST_BUF_SIZE + 1];
|
---|
[821] | 40 |
|
---|
[1765] | 41 | tbl = bb_uuenc_tbl_std;
|
---|
| 42 | mode = 0666 & ~umask(0666);
|
---|
| 43 | opt_complementary = "-1:?2"; /* must have 1 or 2 args */
|
---|
| 44 | if (getopt32(argv, "m")) {
|
---|
| 45 | tbl = bb_uuenc_tbl_base64;
|
---|
[821] | 46 | }
|
---|
[1765] | 47 | argv += optind;
|
---|
[2725] | 48 | if (argv[1]) {
|
---|
| 49 | src_fd = xopen(argv[0], O_RDONLY);
|
---|
[1765] | 50 | fstat(src_fd, &stat_buf);
|
---|
| 51 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
|
---|
| 52 | argv++;
|
---|
| 53 | }
|
---|
[821] | 54 |
|
---|
[1765] | 55 | printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv);
|
---|
| 56 | while (1) {
|
---|
| 57 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
|
---|
| 58 | if (!size)
|
---|
[821] | 59 | break;
|
---|
[1765] | 60 | if ((ssize_t)size < 0)
|
---|
| 61 | bb_perror_msg_and_die(bb_msg_read_error);
|
---|
[821] | 62 | /* Encode the buffer we just read in */
|
---|
[1765] | 63 | bb_uuencode(dst_buf, src_buf, size, tbl);
|
---|
[2725] | 64 | bb_putchar('\n');
|
---|
[1765] | 65 | if (tbl == bb_uuenc_tbl_std) {
|
---|
[2725] | 66 | bb_putchar(tbl[size]);
|
---|
[821] | 67 | }
|
---|
[1765] | 68 | fflush(stdout);
|
---|
| 69 | xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
|
---|
[821] | 70 | }
|
---|
[1765] | 71 | printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n");
|
---|
[821] | 72 |
|
---|
[1765] | 73 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
[821] | 74 | }
|
---|