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 | |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 13 | /* Conversion table. for base 64 */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 14 | static const char tbl_base64[65] = { |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 15 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 16 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 17 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 18 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 19 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 20 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 21 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 22 | '4', '5', '6', '7', '8', '9', '+', '/', |
| 23 | '=' /* termination character */ |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | static const char tbl_std[65] = { |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 27 | '`', '!', '"', '#', '$', '%', '&', '\'', |
| 28 | '(', ')', '*', '+', ',', '-', '.', '/', |
| 29 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 30 | '8', '9', ':', ';', '<', '=', '>', '?', |
| 31 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
| 32 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 33 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 34 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 35 | '`' /* termination character */ |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 38 | /* |
| 39 | * Encode the string S of length LENGTH to base64 format and place it |
| 40 | * to STORE. STORE will be 0-terminated, and must point to a writable |
| 41 | * buffer of at least 1+BASE64_LENGTH(length) bytes. |
| 42 | * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 43 | */ |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 44 | static void uuencode (const unsigned char *s, char *store, const int length, const char *tbl) |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 45 | { |
| 46 | int i; |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 47 | char *p = store; |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 48 | |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 49 | /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ |
| 50 | for (i = 0; i < length; i += 3) { |
| 51 | *p++ = tbl[s[0] >> 2]; |
| 52 | *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; |
| 53 | *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; |
| 54 | *p++ = tbl[s[2] & 0x3f]; |
| 55 | s += 3; |
| 56 | } |
| 57 | /* Pad the result if necessary... */ |
| 58 | if (i == length + 1) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 59 | *(p - 1) = tbl[64]; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 60 | } |
| 61 | else if (i == length + 2) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 62 | *(p - 1) = *(p - 2) = tbl[64]; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 63 | } |
| 64 | /* ...and zero-terminate it. */ |
| 65 | *p = '\0'; |
| 66 | } |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 67 | |
Eric Andersen | b077c9f | 2002-12-06 21:39:48 +0000 | [diff] [blame] | 68 | #define SRC_BUF_SIZE 45 // This *MUST* be a multiple of 3 |
| 69 | #define DST_BUF_SIZE 4 * ((SRC_BUF_SIZE + 2) / 3) |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 70 | int uuencode_main(int argc, char **argv) |
| 71 | { |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 72 | const size_t src_buf_size = SRC_BUF_SIZE; |
| 73 | const size_t dst_buf_size = DST_BUF_SIZE; |
| 74 | size_t write_size = dst_buf_size; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 75 | struct stat stat_buf; |
| 76 | FILE *src_stream = stdin; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | const char *tbl; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 78 | size_t size; |
| 79 | mode_t mode; |
Eric Andersen | b077c9f | 2002-12-06 21:39:48 +0000 | [diff] [blame] | 80 | RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1); |
| 81 | RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 82 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | tbl = tbl_std; |
| 84 | if (bb_getopt_ulflags(argc, argv, "m") & 1) { |
| 85 | tbl = tbl_base64; |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | switch (argc - optind) { |
| 89 | case 2: |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 90 | src_stream = xfopen(argv[optind], "r"); |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 91 | xstat(argv[optind], &stat_buf); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 92 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 93 | if (src_stream == stdout) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | puts("NULL"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 95 | } |
| 96 | break; |
| 97 | case 1: |
Glenn L McGrath | b6071b6 | 2001-07-29 06:04:26 +0000 | [diff] [blame] | 98 | mode = 0666 & ~umask(0666); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 99 | break; |
| 100 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | bb_show_usage(); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | bb_printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 105 | |
| 106 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 107 | if (size != src_buf_size) { |
| 108 | /* write_size is always 60 until the last line */ |
| 109 | write_size=(4 * ((size + 2) / 3)); |
| 110 | /* pad with 0s so we can just encode extra bits */ |
| 111 | memset(&src_buf[size], 0, src_buf_size - size); |
| 112 | } |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 113 | /* Encode the buffer we just read in */ |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 114 | uuencode((unsigned char*)src_buf, dst_buf, size, tbl); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 115 | |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 116 | putchar('\n'); |
| 117 | if (tbl == tbl_std) { |
| 118 | putchar(tbl[size]); |
| 119 | } |
| 120 | if (fwrite(dst_buf, 1, write_size, stdout) != write_size) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | bb_perror_msg_and_die(bb_msg_write_error); |
Tim Riker | 95bf6da | 2002-05-01 05:57:16 +0000 | [diff] [blame] | 122 | } |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 123 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 124 | bb_printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n"); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 125 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 126 | xferror(src_stream, "source"); /* TODO - Fix this! */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | |
| 128 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
Glenn L McGrath | 089deca | 2001-07-28 21:06:13 +0000 | [diff] [blame] | 129 | } |