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) |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 16 | int uuencode_main(int argc, char **argv) |
| 17 | { |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 18 | const size_t src_buf_size = SRC_BUF_SIZE; |
| 19 | const size_t dst_buf_size = DST_BUF_SIZE; |
| 20 | size_t write_size = dst_buf_size; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 21 | struct stat stat_buf; |
| 22 | FILE *src_stream = stdin; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | const char *tbl; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 24 | size_t size; |
| 25 | mode_t mode; |
Eric Andersen | b077c9f | 2002-12-06 21:39:48 +0000 | [diff] [blame] | 26 | RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1); |
| 27 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame^] | 29 | tbl = bb_uuenc_tbl_std; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | if (bb_getopt_ulflags(argc, argv, "m") & 1) { |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame^] | 31 | tbl = bb_uuenc_tbl_base64; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | switch (argc - optind) { |
| 35 | case 2: |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 36 | src_stream = xfopen(argv[optind], "r"); |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 37 | xstat(argv[optind], &stat_buf); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 38 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 39 | if (src_stream == stdout) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | puts("NULL"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 41 | } |
| 42 | break; |
| 43 | case 1: |
Glenn L McGrath | b6071b6 | 2001-07-29 06:04:26 +0000 | [diff] [blame] | 44 | mode = 0666 & ~umask(0666); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 45 | break; |
| 46 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | bb_show_usage(); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame^] | 50 | bb_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] | 51 | |
| 52 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 53 | if (size != src_buf_size) { |
| 54 | /* write_size is always 60 until the last line */ |
| 55 | write_size=(4 * ((size + 2) / 3)); |
| 56 | /* pad with 0s so we can just encode extra bits */ |
| 57 | memset(&src_buf[size], 0, src_buf_size - size); |
| 58 | } |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 59 | /* Encode the buffer we just read in */ |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame^] | 60 | bb_uuencode((unsigned char*)src_buf, dst_buf, size, tbl); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 61 | |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 62 | putchar('\n'); |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame^] | 63 | if (tbl == bb_uuenc_tbl_std) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 64 | putchar(tbl[size]); |
| 65 | } |
| 66 | if (fwrite(dst_buf, 1, write_size, stdout) != write_size) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | bb_perror_msg_and_die(bb_msg_write_error); |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 68 | } |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 69 | } |
Denis Vlasenko | 21afc7d | 2006-09-03 15:49:40 +0000 | [diff] [blame^] | 70 | bb_printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 71 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 72 | xferror(src_stream, "source"); /* TODO - Fix this! */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | |
| 74 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 75 | } |