"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Copyright 2003, Glenn McGrath |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Based on specification from |
| 8 | * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the |
| 11 | * "end" line |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 12 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | //config:config UUDECODE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 14 | //config: bool "uudecode (5.8 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | //config: default y |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 17 | //config: uudecode is used to decode a uuencoded file. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 18 | |
| 19 | //applet:IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_UUDECODE) += uudecode.o |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 22 | |
| 23 | //usage:#define uudecode_trivial_usage |
| 24 | //usage: "[-o OUTFILE] [INFILE]" |
| 25 | //usage:#define uudecode_full_usage "\n\n" |
| 26 | //usage: "Uudecode a file\n" |
Denys Vlasenko | 11e61d5 | 2012-03-05 14:23:26 +0100 | [diff] [blame] | 27 | //usage: "Finds OUTFILE in uuencoded source unless -o is given" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 28 | //usage: |
| 29 | //usage:#define uudecode_example_usage |
| 30 | //usage: "$ uudecode -o busybox busybox.uu\n" |
| 31 | //usage: "$ ls -l busybox\n" |
| 32 | //usage: "-rwxr-xr-x 1 ams ams 245264 Jun 7 21:35 busybox\n" |
| 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 36 | #if ENABLE_UUDECODE |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 37 | static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 38 | { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 39 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 40 | |
Denys Vlasenko | 2b48c38 | 2015-10-05 15:10:44 +0200 | [diff] [blame] | 41 | for (;;) { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 42 | int encoded_len, str_len; |
| 43 | char *line_ptr, *dst; |
Denys Vlasenko | 2b48c38 | 2015-10-05 15:10:44 +0200 | [diff] [blame] | 44 | size_t line_len; |
| 45 | |
| 46 | line_len = 64 * 1024; |
| 47 | line = xmalloc_fgets_str_len(src_stream, "\n", &line_len); |
| 48 | if (!line) |
| 49 | break; |
Denys Vlasenko | 7b6e8f3 | 2017-07-14 17:24:59 +0200 | [diff] [blame] | 50 | /* Handle both Unix and MSDOS text. |
| 51 | * Note: space should not be trimmed, some encoders use it instead of "`" |
| 52 | * for padding of last incomplete 4-char block. |
| 53 | */ |
Denys Vlasenko | 2b48c38 | 2015-10-05 15:10:44 +0200 | [diff] [blame] | 54 | str_len = line_len; |
Denys Vlasenko | 7b6e8f3 | 2017-07-14 17:24:59 +0200 | [diff] [blame] | 55 | while (--str_len >= 0 |
| 56 | && (line[str_len] == '\n' || line[str_len] == '\r') |
| 57 | ) { |
Denys Vlasenko | 2b48c38 | 2015-10-05 15:10:44 +0200 | [diff] [blame] | 58 | line[str_len] = '\0'; |
Denys Vlasenko | 7b6e8f3 | 2017-07-14 17:24:59 +0200 | [diff] [blame] | 59 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 61 | if (strcmp(line, "end") == 0) { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 62 | return; /* the only non-error exit */ |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 63 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 65 | line_ptr = line; |
| 66 | while (*line_ptr) { |
| 67 | *line_ptr = (*line_ptr - 0x20) & 0x3f; |
| 68 | line_ptr++; |
| 69 | } |
| 70 | str_len = line_ptr - line; |
| 71 | |
| 72 | encoded_len = line[0] * 4 / 3; |
| 73 | /* Check that line is not too short. (we tolerate |
Denys Vlasenko | 7b6e8f3 | 2017-07-14 17:24:59 +0200 | [diff] [blame] | 74 | * overly _long_ line to accommodate possible extra "`"). |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 75 | * Empty line case is also caught here. */ |
| 76 | if (str_len <= encoded_len) { |
| 77 | break; /* go to bb_error_msg_and_die("short file"); */ |
| 78 | } |
| 79 | if (encoded_len <= 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 80 | /* Ignore the "`\n" line, why is it even in the encode file ? */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 81 | free(line); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 82 | continue; |
| 83 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 84 | if (encoded_len > 60) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 85 | bb_simple_error_msg_and_die("line too long"); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 86 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 88 | dst = line; |
| 89 | line_ptr = line + 1; |
| 90 | do { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 91 | /* Merge four 6 bit chars to three 8 bit chars */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 92 | *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4; |
| 93 | encoded_len--; |
| 94 | if (encoded_len == 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 98 | *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2; |
| 99 | encoded_len--; |
| 100 | if (encoded_len == 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 101 | break; |
| 102 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 104 | *dst++ = line_ptr[2] << 6 | line_ptr[3]; |
| 105 | line_ptr += 4; |
| 106 | encoded_len -= 2; |
| 107 | } while (encoded_len > 0); |
| 108 | fwrite(line, 1, dst - line, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 109 | free(line); |
| 110 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 111 | bb_simple_error_msg_and_die("short file"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 114 | int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 115 | int uudecode_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 116 | { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 117 | FILE *src_stream; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 118 | char *outname = NULL; |
| 119 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 120 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 121 | getopt32(argv, "^" "o:" "\0" "?1"/* 1 arg max*/, &outname); |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 122 | argv += optind; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 123 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 124 | if (!argv[0]) |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 125 | *--argv = (char*)"-"; |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 126 | src_stream = xfopen_stdin(argv[0]); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 127 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 128 | /* Search for the start of the encoding */ |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 129 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 130 | void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 131 | char *line_ptr; |
| 132 | FILE *dst_stream; |
| 133 | int mode; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 134 | |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 135 | if (is_prefixed_with(line, "begin-base64 ")) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 136 | line_ptr = line + 13; |
| 137 | decode_fn_ptr = read_base64; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 138 | } else if (is_prefixed_with(line, "begin ")) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 139 | line_ptr = line + 6; |
| 140 | decode_fn_ptr = read_stduu; |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 141 | } else { |
| 142 | free(line); |
| 143 | continue; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 144 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 145 | |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 146 | /* begin line found. decode and exit */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 147 | mode = bb_strtou(line_ptr, NULL, 8); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 148 | if (outname == NULL) { |
| 149 | outname = strchr(line_ptr, ' '); |
Denys Vlasenko | 5f92043 | 2011-10-18 12:07:05 +0200 | [diff] [blame] | 150 | if (!outname) |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 151 | break; |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 152 | outname++; |
Denys Vlasenko | 2b48c38 | 2015-10-05 15:10:44 +0200 | [diff] [blame] | 153 | trim(outname); /* remove trailing space (and '\r' for DOS text) */ |
Denys Vlasenko | 5f92043 | 2011-10-18 12:07:05 +0200 | [diff] [blame] | 154 | if (!outname[0]) |
| 155 | break; |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 156 | } |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 157 | dst_stream = stdout; |
| 158 | if (NOT_LONE_DASH(outname)) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 159 | dst_stream = xfopen_for_write(outname); |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 160 | fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 161 | } |
| 162 | free(line); |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 163 | decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR); |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 164 | /* fclose_if_not_stdin(src_stream); - redundant */ |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 165 | return EXIT_SUCCESS; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 166 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 167 | bb_simple_error_msg_and_die("no 'begin' line"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 168 | } |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 169 | #endif |
| 170 | |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 171 | //config:config BASE32 |
| 172 | //config: bool "base32 (4.9 kb)" |
| 173 | //config: default y |
| 174 | //config: help |
| 175 | //config: Base32 encode and decode |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 176 | |
| 177 | //config:config BASE64 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 178 | //config: bool "base64 (4.9 kb)" |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 179 | //config: default y |
| 180 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 181 | //config: Base64 encode and decode |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 182 | |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 183 | //usage:#define base32_trivial_usage |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 184 | //usage: "[-d] [-w COL] [FILE]" |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 185 | //usage:#define base32_full_usage "\n\n" |
| 186 | //usage: "Base32 encode or decode FILE to standard output" |
| 187 | //usage: "\n -d Decode data" |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 188 | //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 189 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
| 190 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 191 | //usage:#define base64_trivial_usage |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 192 | //usage: "[-d] [-w COL] [FILE]" |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 193 | //usage:#define base64_full_usage "\n\n" |
| 194 | //usage: "Base64 encode or decode FILE to standard output" |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 195 | //usage: "\n -d Decode data" |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 196 | //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 197 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
| 198 | |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 199 | // APPLET_ODDNAME:name main location suid_type help |
| 200 | //applet:IF_BASE32(APPLET_ODDNAME(base32, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base32)) |
| 201 | //applet:IF_BASE64(APPLET_ODDNAME(base64, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base64)) |
| 202 | |
| 203 | //kbuild:lib-$(CONFIG_BASE64) += uudecode.o |
| 204 | //kbuild:lib-$(CONFIG_BASE32) += uudecode.o |
| 205 | |
| 206 | #if ENABLE_BASE32 || ENABLE_BASE64 |
| 207 | |
| 208 | # if ENABLE_BASE32 |
| 209 | static void bb_b32encode(char *p, const void *src, int length) |
| 210 | { |
| 211 | #define tbl bb_uuenc_tbl_base32 |
| 212 | const unsigned char *s = src; |
| 213 | |
| 214 | /* Transform 5x8 bits to 8x5 bits */ |
| 215 | while (length > 0) { |
| 216 | unsigned cur, next; |
| 217 | |
| 218 | length--; |
| 219 | cur = *s++; |
| 220 | *p++ = tbl[cur >> 3]; // xxxxx--- -------- -------- -------- -------- |
| 221 | cur &= 7; |
| 222 | |
| 223 | next = 0; |
| 224 | if (--length >= 0) |
| 225 | next = *s++; |
| 226 | *p++ = tbl[(cur << 2) + (next >> 6)]; // -----xxx xx------ -------- -------- -------- |
| 227 | cur = next & 0x3f; |
| 228 | |
| 229 | *p++ = tbl[cur >> 1]; // -------- --xxxxx- -------- -------- -------- |
| 230 | cur &= 1; |
| 231 | |
| 232 | next = 0; |
| 233 | if (--length >= 0) |
| 234 | next = *s++; |
| 235 | *p++ = tbl[(cur << 4) + (next >> 4)]; // -------- -------x xxxx---- -------- -------- |
| 236 | cur = next & 0xf; |
| 237 | |
| 238 | next = 0; |
| 239 | if (--length >= 0) |
| 240 | next = *s++; |
| 241 | *p++ = tbl[(cur << 1) + (next >> 7)]; // -------- -------- ----xxxx x------- -------- |
| 242 | cur = next & 0x7f; |
| 243 | |
| 244 | *p++ = tbl[cur >> 2]; // -------- -------- -------- -xxxxx-- -------- |
| 245 | cur &= 3; |
| 246 | |
| 247 | next = 0; |
| 248 | if (--length >= 0) |
| 249 | next = *s++; |
| 250 | *p++ = tbl[(cur << 3) + (next >> 5)]; // -------- -------- -------- ------xx xxx----- |
| 251 | cur = next & 0x1f; |
| 252 | |
| 253 | *p++ = tbl[cur]; // -------- -------- -------- -------- ---xxxxx |
| 254 | } |
| 255 | #undef tbl |
| 256 | /* Zero-terminate */ |
| 257 | *p = '\0'; |
| 258 | /* Pad as necessary */ |
| 259 | length = ((-length) * 3) >> 1; /* -4 => 6 pad chars, -3 => 4, -2 => 3, -1 => 1 */ |
| 260 | while (length--) { |
| 261 | *--p = '='; |
| 262 | } |
| 263 | } |
| 264 | # else |
| 265 | void bb_b32encode(char *p, const void *src, int length); /* undefined */ |
| 266 | # endif |
| 267 | |
| 268 | int baseNUM_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 269 | int baseNUM_main(int argc UNUSED_PARAM, char **argv) |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 270 | { |
| 271 | FILE *src_stream; |
| 272 | unsigned opts; |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 273 | unsigned col = 76; |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 274 | |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 275 | opts = getopt32(argv, "^" "dw:+" "\0" "?1"/* 1 arg max*/, &col); |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 276 | argv += optind; |
| 277 | |
| 278 | if (!argv[0]) |
| 279 | *--argv = (char*)"-"; |
| 280 | src_stream = xfopen_stdin(argv[0]); |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 281 | if (opts & 1) { |
Denys Vlasenko | 818a4aa | 2020-11-28 14:22:52 +0100 | [diff] [blame] | 282 | /* -d: decode */ |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 283 | int flags = (unsigned char)EOF; |
| 284 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) |
| 285 | flags = ((unsigned char)EOF) | BASE64_32; |
| 286 | read_base64(src_stream, stdout, flags); |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 287 | } else { |
| 288 | enum { |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 289 | SRC_BUF_SIZE = 3 * 5 * 32, /* this *MUST* be a multiple of 3 and 5 */ |
Denys Vlasenko | 818a4aa | 2020-11-28 14:22:52 +0100 | [diff] [blame] | 290 | DST_BUF_SIZE = 8 * ((SRC_BUF_SIZE + 4) / 5), /* max growth on encode (base32 case) */ |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 291 | }; |
Denys Vlasenko | cdab3c4 | 2020-11-27 15:39:23 +0100 | [diff] [blame] | 292 | /* Use one buffer for both input and output: |
Denys Vlasenko | 818a4aa | 2020-11-28 14:22:52 +0100 | [diff] [blame] | 293 | * encoding reads input "left-to-right", |
Denys Vlasenko | cdab3c4 | 2020-11-27 15:39:23 +0100 | [diff] [blame] | 294 | * it's safe to place source at the end of the buffer and |
Denys Vlasenko | 818a4aa | 2020-11-28 14:22:52 +0100 | [diff] [blame] | 295 | * overwrite it while encoding, just be careful to have a gap. |
Denys Vlasenko | cdab3c4 | 2020-11-27 15:39:23 +0100 | [diff] [blame] | 296 | */ |
| 297 | char dst_buf[((DST_BUF_SIZE + /*gap:*/ 16) /*round up to 16:*/ | 0xf) + 1]; |
| 298 | #define src_buf (dst_buf + sizeof(dst_buf) - SRC_BUF_SIZE) |
| 299 | int src_fd, rem; |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 300 | |
Denys Vlasenko | cdab3c4 | 2020-11-27 15:39:23 +0100 | [diff] [blame] | 301 | src_fd = fileno(src_stream); |
| 302 | rem = 0; |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 303 | while (1) { |
| 304 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 305 | if ((ssize_t)size < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 306 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 307 | if (size == 0) { |
| 308 | if (rem != 0) bb_putchar('\n'); |
| 309 | break; |
| 310 | } |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 311 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 312 | /* Encode the buffer we just read in */ |
Denys Vlasenko | 2090048 | 2020-11-25 22:47:00 +0100 | [diff] [blame] | 313 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) { |
| 314 | bb_b32encode(dst_buf, src_buf, size); |
| 315 | size = 8 * ((size + 4) / 5); |
| 316 | } else { |
| 317 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
| 318 | size = 4 * ((size + 2) / 3); |
| 319 | } |
| 320 | |
Denys Vlasenko | c8b3d9a | 2020-11-27 15:25:31 +0100 | [diff] [blame] | 321 | if (col == 0) { |
| 322 | fputs(dst_buf, stdout); |
| 323 | } else { |
| 324 | char *result = dst_buf; |
| 325 | if (rem == 0) |
| 326 | rem = col; |
| 327 | while (1) { |
| 328 | int out = size < rem ? size : rem; |
| 329 | rem -= out; |
| 330 | printf(rem != 0 ? "%.*s" : "%.*s\n", out, result); |
| 331 | if (rem != 0) |
| 332 | break; |
| 333 | size -= out; |
| 334 | if (size == 0) |
| 335 | break; |
| 336 | result += out; |
| 337 | rem = col; |
| 338 | } |
| 339 | } |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 340 | } |
Denys Vlasenko | cdab3c4 | 2020-11-27 15:39:23 +0100 | [diff] [blame] | 341 | #undef src_buf |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 345 | } |
| 346 | #endif |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 347 | |
| 348 | /* Test script. |
| 349 | Put this into an empty dir with busybox binary, an run. |
| 350 | |
| 351 | #!/bin/sh |
| 352 | test -x busybox || { echo "No ./busybox?"; exit; } |
| 353 | ln -sf busybox uudecode |
| 354 | ln -sf busybox uuencode |
| 355 | >A_null |
| 356 | echo -n A >A |
| 357 | echo -n AB >AB |
| 358 | echo -n ABC >ABC |
| 359 | echo -n ABCD >ABCD |
| 360 | echo -n ABCDE >ABCDE |
| 361 | echo -n ABCDEF >ABCDEF |
| 362 | cat busybox >A_bbox |
| 363 | for f in A*; do |
| 364 | echo uuencode $f |
| 365 | ./uuencode $f <$f >u_$f |
| 366 | ./uuencode -m $f <$f >m_$f |
| 367 | done |
| 368 | mkdir unpk_u unpk_m 2>/dev/null |
| 369 | for f in u_*; do |
| 370 | ./uudecode <$f -o unpk_u/${f:2} |
| 371 | diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1 |
| 372 | echo uudecode $f: $? |
| 373 | done |
| 374 | for f in m_*; do |
| 375 | ./uudecode <$f -o unpk_m/${f:2} |
| 376 | diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1 |
| 377 | echo uudecode $f: $? |
| 378 | done |
| 379 | */ |