Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Copyright (C) 2000 by Glenn McGrath |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [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 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 9 | */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 10 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 11 | #include "busybox.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 12 | |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 13 | |
Eric Andersen | b077c9f | 2002-12-06 21:39:48 +0000 | [diff] [blame] | 14 | #define SRC_BUF_SIZE 45 // This *MUST* be a multiple of 3 |
| 15 | #define DST_BUF_SIZE 4 * ((SRC_BUF_SIZE + 2) / 3) |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 16 | int uuencode_main(int argc, char **argv); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 17 | int uuencode_main(int argc, char **argv) |
| 18 | { |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 19 | const size_t src_buf_size = SRC_BUF_SIZE; |
| 20 | const size_t dst_buf_size = DST_BUF_SIZE; |
| 21 | size_t write_size = dst_buf_size; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 22 | struct stat stat_buf; |
| 23 | FILE *src_stream = stdin; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | const char *tbl; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 25 | size_t size; |
| 26 | mode_t mode; |
Eric Andersen | b077c9f | 2002-12-06 21:39:48 +0000 | [diff] [blame] | 27 | RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1); |
| 28 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 30 | tbl = bb_uuenc_tbl_std; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 31 | if (getopt32(argc, argv, "m") & 1) { |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 32 | tbl = bb_uuenc_tbl_base64; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | switch (argc - optind) { |
| 36 | case 2: |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 37 | src_stream = xfopen(argv[optind], "r"); |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 38 | xstat(argv[optind], &stat_buf); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 39 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 40 | if (src_stream == stdout) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | puts("NULL"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 42 | } |
| 43 | break; |
| 44 | case 1: |
Glenn L McGrath | b6071b6 | 2001-07-29 06:04:26 +0000 | [diff] [blame] | 45 | mode = 0666 & ~umask(0666); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 46 | break; |
| 47 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | bb_show_usage(); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 51 | printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, argv[argc - 1]); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 52 | |
| 53 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 54 | if (size != src_buf_size) { |
| 55 | /* write_size is always 60 until the last line */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 56 | write_size = (4 * ((size + 2) / 3)); |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 57 | /* pad with 0s so we can just encode extra bits */ |
| 58 | memset(&src_buf[size], 0, src_buf_size - size); |
| 59 | } |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 60 | /* Encode the buffer we just read in */ |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 61 | bb_uuencode((unsigned char*)src_buf, dst_buf, size, tbl); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 62 | |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 63 | putchar('\n'); |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame] | 64 | if (tbl == bb_uuenc_tbl_std) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 65 | putchar(tbl[size]); |
| 66 | } |
| 67 | if (fwrite(dst_buf, 1, write_size, stdout) != write_size) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | bb_perror_msg_and_die(bb_msg_write_error); |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 69 | } |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 71 | printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 73 | die_if_ferror(src_stream, "source"); /* TODO - Fix this! */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 75 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 76 | } |