"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 | |
"Vladimir N. Oleynik" | ba24820 | 2005-10-06 15:18:09 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 16 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 17 | static int 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) { |
| 26 | return(EXIT_SUCCESS); |
| 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) { |
| 35 | bb_error_msg_and_die("Line too long"); |
| 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) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 41 | bb_error_msg_and_die("Short file"); |
| 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 | } |
| 66 | bb_error_msg_and_die("Short file"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 69 | static int read_base64(FILE *src_stream, FILE *dst_stream) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 70 | { |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 71 | static const char base64_table[] = |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 72 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"; |
| 73 | char term_count = 0; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 74 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 75 | while (1) { |
| 76 | char translated[4]; |
| 77 | int count = 0; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 78 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 79 | while (count < 4) { |
| 80 | char *table_ptr; |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 81 | int ch; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 83 | /* Get next _valid_ character */ |
| 84 | do { |
| 85 | ch = fgetc(src_stream); |
| 86 | if (ch == EOF) { |
| 87 | bb_error_msg_and_die("Short file"); |
| 88 | } |
| 89 | } while ((table_ptr = strchr(base64_table, ch)) == NULL); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 90 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 91 | /* Convert encoded charcter to decimal */ |
| 92 | ch = table_ptr - base64_table; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 93 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 94 | if (*table_ptr == '=') { |
| 95 | if (term_count == 0) { |
| 96 | translated[count] = 0; |
| 97 | break; |
| 98 | } |
| 99 | term_count++; |
| 100 | } |
| 101 | else if (*table_ptr == '\n') { |
| 102 | /* Check for terminating line */ |
| 103 | if (term_count == 5) { |
| 104 | return(EXIT_SUCCESS); |
| 105 | } |
| 106 | term_count = 1; |
| 107 | continue; |
| 108 | } else { |
| 109 | translated[count] = ch; |
| 110 | count++; |
| 111 | term_count = 0; |
| 112 | } |
| 113 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 114 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 115 | /* Merge 6 bit chars to 8 bit */ |
| 116 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); |
| 117 | if (count > 2) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 118 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 119 | } |
| 120 | if (count > 3) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 121 | fputc(translated[2] << 6 | translated[3], dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 126 | int uudecode_main(int argc, char **argv) |
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 | FILE *src_stream; |
| 129 | char *outname = NULL; |
| 130 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 132 | getopt32(argc, argv, "o:", &outname); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 133 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 134 | if (optind == argc) { |
| 135 | src_stream = stdin; |
| 136 | } else if (optind + 1 == argc) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 137 | src_stream = xfopen(argv[optind], "r"); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 138 | } else { |
| 139 | bb_show_usage(); |
| 140 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 141 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 142 | /* Search for the start of the encoding */ |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 143 | while ((line = xmalloc_getline(src_stream)) != NULL) { |
Rob Landley | b97f07f | 2006-09-22 19:11:59 +0000 | [diff] [blame] | 144 | int (*decode_fn_ptr)(FILE * src, FILE * dst); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 145 | char *line_ptr; |
| 146 | FILE *dst_stream; |
| 147 | int mode; |
| 148 | int ret; |
| 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 | |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 161 | mode = strtoul(line_ptr, NULL, 8); |
| 162 | if (outname == NULL) { |
| 163 | outname = strchr(line_ptr, ' '); |
| 164 | if ((outname == NULL) || (*outname == '\0')) { |
| 165 | break; |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 166 | } |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 167 | outname++; |
| 168 | } |
| 169 | if (strcmp(outname, "-") == 0) { |
| 170 | dst_stream = stdout; |
| 171 | } else { |
| 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); |
Rob Landley | 29d94b9 | 2006-09-23 19:56:21 +0000 | [diff] [blame] | 176 | ret = decode_fn_ptr(src_stream, dst_stream); |
| 177 | bb_fclose_nonstdin(src_stream); |
| 178 | return(ret); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 179 | } |
| 180 | bb_error_msg_and_die("No `begin' line"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 181 | } |