"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 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 13 | |
| 14 | //usage:#define uudecode_trivial_usage |
| 15 | //usage: "[-o OUTFILE] [INFILE]" |
| 16 | //usage:#define uudecode_full_usage "\n\n" |
| 17 | //usage: "Uudecode a file\n" |
| 18 | //usage: "Finds outfile name in uuencoded source unless -o is given" |
| 19 | //usage: |
| 20 | //usage:#define uudecode_example_usage |
| 21 | //usage: "$ uudecode -o busybox busybox.uu\n" |
| 22 | //usage: "$ ls -l busybox\n" |
| 23 | //usage: "-rwxr-xr-x 1 ams ams 245264 Jun 7 21:35 busybox\n" |
| 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 26 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 27 | #if ENABLE_UUDECODE |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 28 | 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] | 29 | { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 30 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 32 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 33 | int encoded_len, str_len; |
| 34 | char *line_ptr, *dst; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 35 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 36 | if (strcmp(line, "end") == 0) { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 37 | return; /* the only non-error exit */ |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 38 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 40 | line_ptr = line; |
| 41 | while (*line_ptr) { |
| 42 | *line_ptr = (*line_ptr - 0x20) & 0x3f; |
| 43 | line_ptr++; |
| 44 | } |
| 45 | str_len = line_ptr - line; |
| 46 | |
| 47 | encoded_len = line[0] * 4 / 3; |
| 48 | /* Check that line is not too short. (we tolerate |
| 49 | * overly _long_ line to accomodate possible extra '`'). |
| 50 | * Empty line case is also caught here. */ |
| 51 | if (str_len <= encoded_len) { |
| 52 | break; /* go to bb_error_msg_and_die("short file"); */ |
| 53 | } |
| 54 | if (encoded_len <= 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 55 | /* Ignore the "`\n" line, why is it even in the encode file ? */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 56 | free(line); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 57 | continue; |
| 58 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 59 | if (encoded_len > 60) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 60 | bb_error_msg_and_die("line too long"); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 61 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 63 | dst = line; |
| 64 | line_ptr = line + 1; |
| 65 | do { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 66 | /* Merge four 6 bit chars to three 8 bit chars */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 67 | *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4; |
| 68 | encoded_len--; |
| 69 | if (encoded_len == 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 70 | break; |
| 71 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 73 | *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2; |
| 74 | encoded_len--; |
| 75 | if (encoded_len == 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 76 | break; |
| 77 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 79 | *dst++ = line_ptr[2] << 6 | line_ptr[3]; |
| 80 | line_ptr += 4; |
| 81 | encoded_len -= 2; |
| 82 | } while (encoded_len > 0); |
| 83 | fwrite(line, 1, dst - line, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 84 | free(line); |
| 85 | } |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 86 | bb_error_msg_and_die("short file"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 87 | } |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 88 | #endif |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 89 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 90 | #if ENABLE_UUDECODE |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 91 | int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 92 | int uudecode_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 93 | { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 94 | FILE *src_stream; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 95 | char *outname = NULL; |
| 96 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 98 | opt_complementary = "?1"; /* 1 argument max */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 99 | getopt32(argv, "o:", &outname); |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 100 | argv += optind; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 101 | |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 102 | if (!argv[0]) |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 103 | *--argv = (char*)"-"; |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 104 | src_stream = xfopen_stdin(argv[0]); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 105 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 106 | /* Search for the start of the encoding */ |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 107 | while ((line = xmalloc_fgetline(src_stream)) != NULL) { |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 108 | void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 109 | char *line_ptr; |
| 110 | FILE *dst_stream; |
| 111 | int mode; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 112 | |
Rob Landley | 565bc33 | 2006-07-31 22:50:12 +0000 | [diff] [blame] | 113 | if (strncmp(line, "begin-base64 ", 13) == 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 114 | line_ptr = line + 13; |
| 115 | decode_fn_ptr = read_base64; |
| 116 | } else if (strncmp(line, "begin ", 6) == 0) { |
| 117 | line_ptr = line + 6; |
| 118 | decode_fn_ptr = read_stduu; |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 119 | } else { |
| 120 | free(line); |
| 121 | continue; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 122 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 124 | /* begin line found. decode and exit */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 125 | mode = bb_strtou(line_ptr, NULL, 8); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 126 | if (outname == NULL) { |
| 127 | outname = strchr(line_ptr, ' '); |
| 128 | if ((outname == NULL) || (*outname == '\0')) { |
| 129 | break; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 130 | } |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 131 | outname++; |
| 132 | } |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 133 | dst_stream = stdout; |
| 134 | if (NOT_LONE_DASH(outname)) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 135 | dst_stream = xfopen_for_write(outname); |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 136 | fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 137 | } |
| 138 | free(line); |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 139 | 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] | 140 | /* fclose_if_not_stdin(src_stream); - redundant */ |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 141 | return EXIT_SUCCESS; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 142 | } |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 143 | bb_error_msg_and_die("no 'begin' line"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 144 | } |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 145 | #endif |
| 146 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 147 | //applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 148 | |
| 149 | //kbuild:lib-$(CONFIG_BASE64) += uudecode.o |
| 150 | |
| 151 | //config:config BASE64 |
| 152 | //config: bool "base64" |
| 153 | //config: default y |
| 154 | //config: help |
| 155 | //config: Base64 encode and decode |
| 156 | |
| 157 | //usage:#define base64_trivial_usage |
| 158 | //usage: "[-d] [FILE]" |
| 159 | //usage:#define base64_full_usage "\n\n" |
| 160 | //usage: "Base64 encode or decode FILE to standard output" |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 161 | //usage: "\n -d Decode data" |
| 162 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
| 163 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
| 164 | |
| 165 | #if ENABLE_BASE64 |
| 166 | int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 167 | int base64_main(int argc UNUSED_PARAM, char **argv) |
| 168 | { |
| 169 | FILE *src_stream; |
| 170 | unsigned opts; |
| 171 | |
| 172 | opt_complementary = "?1"; /* 1 argument max */ |
| 173 | opts = getopt32(argv, "d"); |
| 174 | argv += optind; |
| 175 | |
| 176 | if (!argv[0]) |
| 177 | *--argv = (char*)"-"; |
| 178 | src_stream = xfopen_stdin(argv[0]); |
| 179 | if (opts) { |
Denys Vlasenko | 9fe98f7 | 2010-09-16 17:51:13 +0200 | [diff] [blame] | 180 | read_base64(src_stream, stdout, /*flags:*/ (char)EOF); |
Denys Vlasenko | ee06264 | 2010-08-31 14:09:22 +0200 | [diff] [blame] | 181 | } else { |
| 182 | enum { |
| 183 | SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */ |
| 184 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 185 | }; |
| 186 | char src_buf[SRC_BUF_SIZE]; |
| 187 | char dst_buf[DST_BUF_SIZE + 1]; |
| 188 | int src_fd = fileno(src_stream); |
| 189 | while (1) { |
| 190 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); |
| 191 | if (!size) |
| 192 | break; |
| 193 | if ((ssize_t)size < 0) |
| 194 | bb_perror_msg_and_die(bb_msg_read_error); |
| 195 | /* Encode the buffer we just read in */ |
| 196 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
| 197 | xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); |
| 198 | bb_putchar('\n'); |
| 199 | fflush(stdout); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 204 | } |
| 205 | #endif |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 206 | |
| 207 | /* Test script. |
| 208 | Put this into an empty dir with busybox binary, an run. |
| 209 | |
| 210 | #!/bin/sh |
| 211 | test -x busybox || { echo "No ./busybox?"; exit; } |
| 212 | ln -sf busybox uudecode |
| 213 | ln -sf busybox uuencode |
| 214 | >A_null |
| 215 | echo -n A >A |
| 216 | echo -n AB >AB |
| 217 | echo -n ABC >ABC |
| 218 | echo -n ABCD >ABCD |
| 219 | echo -n ABCDE >ABCDE |
| 220 | echo -n ABCDEF >ABCDEF |
| 221 | cat busybox >A_bbox |
| 222 | for f in A*; do |
| 223 | echo uuencode $f |
| 224 | ./uuencode $f <$f >u_$f |
| 225 | ./uuencode -m $f <$f >m_$f |
| 226 | done |
| 227 | mkdir unpk_u unpk_m 2>/dev/null |
| 228 | for f in u_*; do |
| 229 | ./uudecode <$f -o unpk_u/${f:2} |
| 230 | diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1 |
| 231 | echo uudecode $f: $? |
| 232 | done |
| 233 | for f in m_*; do |
| 234 | ./uudecode <$f -o unpk_m/${f:2} |
| 235 | diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1 |
| 236 | echo uudecode $f: $? |
| 237 | done |
| 238 | */ |