"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 | /* |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 3 | * Copyright 2003, Glenn McGrath <bug1@iinet.net.au> |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 0edbad1 | 2006-04-17 22:49:30 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [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 | * |
Rob Landley | 0edbad1 | 2006-04-17 22:49:30 +0000 | [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 | */ |
| 13 | |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 16 | |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 17 | static void read_stduu(FILE *src_stream, FILE *dst_stream) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 18 | { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 19 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 21 | while ((line = xmalloc_getline(src_stream)) != NULL) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 22 | int length; |
| 23 | char *line_ptr = line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 24 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 25 | if (strcmp(line, "end") == 0) { |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 26 | return; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 27 | } |
| 28 | length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3; |
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 | if (length <= 0) { |
| 31 | /* Ignore the "`\n" line, why is it even in the encode file ? */ |
| 32 | continue; |
| 33 | } |
| 34 | if (length > 60) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 35 | bb_error_msg_and_die("line too long"); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 36 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 37 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 38 | line_ptr++; |
Bernhard Reutner-Fischer | 42f6702 | 2006-09-21 20:40:56 +0000 | [diff] [blame] | 39 | /* Tolerate an overly long line to accomodate a possible exta '`' */ |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 40 | if (strlen(line_ptr) < (size_t)length) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 41 | bb_error_msg_and_die("short file"); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 42 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 43 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 44 | while (length > 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 45 | /* Merge four 6 bit chars to three 8 bit chars */ |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 46 | fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 47 | line_ptr++; |
| 48 | length--; |
| 49 | if (length == 0) { |
| 50 | break; |
| 51 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 52 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 53 | fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 54 | line_ptr++; |
| 55 | length--; |
| 56 | if (length == 0) { |
| 57 | break; |
| 58 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 59 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 60 | fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 61 | line_ptr += 2; |
| 62 | length -= 2; |
| 63 | } |
| 64 | free(line); |
| 65 | } |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 66 | bb_error_msg_and_die("short file"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 69 | static void read_base64(FILE *src_stream, FILE *dst_stream) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 70 | { |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 71 | int term_count = 1; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 73 | while (1) { |
| 74 | char translated[4]; |
| 75 | int count = 0; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 76 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 77 | while (count < 4) { |
| 78 | char *table_ptr; |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 79 | int ch; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | aa198dd | 2007-06-12 07:24:11 +0000 | [diff] [blame] | 81 | /* Get next _valid_ character. |
| 82 | * global vector bb_uuenc_tbl_base64[] contains this string: |
| 83 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n" |
| 84 | */ |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 85 | do { |
| 86 | ch = fgetc(src_stream); |
| 87 | if (ch == EOF) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 88 | bb_error_msg_and_die("short file"); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 89 | } |
Denis Vlasenko | aa198dd | 2007-06-12 07:24:11 +0000 | [diff] [blame] | 90 | table_ptr = strchr(bb_uuenc_tbl_base64, ch); |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 91 | } while (table_ptr == NULL); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | aa198dd | 2007-06-12 07:24:11 +0000 | [diff] [blame] | 93 | /* Convert encoded character to decimal */ |
| 94 | ch = table_ptr - bb_uuenc_tbl_base64; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 95 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 96 | if (*table_ptr == '=') { |
| 97 | if (term_count == 0) { |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 98 | translated[count] = '\0'; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 99 | break; |
| 100 | } |
| 101 | term_count++; |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 102 | } else if (*table_ptr == '\n') { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 103 | /* Check for terminating line */ |
| 104 | if (term_count == 5) { |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 105 | return; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 106 | } |
| 107 | term_count = 1; |
| 108 | continue; |
| 109 | } else { |
| 110 | translated[count] = ch; |
| 111 | count++; |
| 112 | term_count = 0; |
| 113 | } |
| 114 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 115 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 116 | /* Merge 6 bit chars to 8 bit */ |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 117 | if (count > 1) { |
| 118 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); |
| 119 | } |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 120 | if (count > 2) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 121 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 122 | } |
| 123 | if (count > 3) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 124 | fputc(translated[2] << 6 | translated[3], dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 129 | int uudecode_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 130 | int uudecode_main(int argc, char **argv) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 131 | { |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 132 | FILE *src_stream = stdin; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 133 | char *outname = NULL; |
| 134 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 136 | opt_complementary = "?1"; /* 1 argument max */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 137 | getopt32(argc, argv, "o:", &outname); |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 138 | argv += optind; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 140 | if (argv[0]) |
| 141 | src_stream = xfopen(argv[0], "r"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 142 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 143 | /* Search for the start of the encoding */ |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 144 | while ((line = xmalloc_getline(src_stream)) != NULL) { |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 145 | void (*decode_fn_ptr)(FILE * src, FILE * dst); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 146 | char *line_ptr; |
| 147 | FILE *dst_stream; |
| 148 | int mode; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 149 | |
Rob Landley | 565bc33 | 2006-07-31 22:50:12 +0000 | [diff] [blame] | 150 | if (strncmp(line, "begin-base64 ", 13) == 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 151 | line_ptr = line + 13; |
| 152 | decode_fn_ptr = read_base64; |
| 153 | } else if (strncmp(line, "begin ", 6) == 0) { |
| 154 | line_ptr = line + 6; |
| 155 | decode_fn_ptr = read_stduu; |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 156 | } else { |
| 157 | free(line); |
| 158 | continue; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 159 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 160 | |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 161 | /* begin line found. decode and exit */ |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 162 | mode = strtoul(line_ptr, NULL, 8); |
| 163 | if (outname == NULL) { |
| 164 | outname = strchr(line_ptr, ' '); |
| 165 | if ((outname == NULL) || (*outname == '\0')) { |
| 166 | break; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 167 | } |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 168 | outname++; |
| 169 | } |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 170 | dst_stream = stdout; |
| 171 | if (NOT_LONE_DASH(outname)) { |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 172 | dst_stream = xfopen(outname, "w"); |
| 173 | chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 174 | } |
| 175 | free(line); |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 176 | decode_fn_ptr(src_stream, dst_stream); |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 177 | /* fclose_if_not_stdin(src_stream); - redundant */ |
Bernhard Reutner-Fischer | ca5b352 | 2007-01-20 21:29:32 +0000 | [diff] [blame] | 178 | return EXIT_SUCCESS; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 179 | } |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 180 | bb_error_msg_and_die("no 'begin' line"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 181 | } |
Denis Vlasenko | 746204b | 2007-06-04 23:32:35 +0000 | [diff] [blame] | 182 | |
| 183 | /* Test script. |
| 184 | Put this into an empty dir with busybox binary, an run. |
| 185 | |
| 186 | #!/bin/sh |
| 187 | test -x busybox || { echo "No ./busybox?"; exit; } |
| 188 | ln -sf busybox uudecode |
| 189 | ln -sf busybox uuencode |
| 190 | >A_null |
| 191 | echo -n A >A |
| 192 | echo -n AB >AB |
| 193 | echo -n ABC >ABC |
| 194 | echo -n ABCD >ABCD |
| 195 | echo -n ABCDE >ABCDE |
| 196 | echo -n ABCDEF >ABCDEF |
| 197 | cat busybox >A_bbox |
| 198 | for f in A*; do |
| 199 | echo uuencode $f |
| 200 | ./uuencode $f <$f >u_$f |
| 201 | ./uuencode -m $f <$f >m_$f |
| 202 | done |
| 203 | mkdir unpk_u unpk_m 2>/dev/null |
| 204 | for f in u_*; do |
| 205 | ./uudecode <$f -o unpk_u/${f:2} |
| 206 | diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1 |
| 207 | echo uudecode $f: $? |
| 208 | done |
| 209 | for f in m_*; do |
| 210 | ./uudecode <$f -o unpk_m/${f:2} |
| 211 | diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1 |
| 212 | echo uudecode $f: $? |
| 213 | done |
| 214 | */ |