"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 | |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <errno.h> |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 17 | #include <getopt.h> /* optind */ |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
"Vladimir N. Oleynik" | ba24820 | 2005-10-06 15:18:09 +0000 | [diff] [blame] | 20 | #include "busybox.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 21 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 22 | static int read_stduu(FILE *src_stream, FILE *dst_stream) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 23 | { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 24 | char *line; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 25 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 26 | while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) { |
| 27 | int length; |
| 28 | char *line_ptr = line; |
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 (strcmp(line, "end") == 0) { |
| 31 | return(EXIT_SUCCESS); |
| 32 | } |
| 33 | length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 34 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 35 | if (length <= 0) { |
| 36 | /* Ignore the "`\n" line, why is it even in the encode file ? */ |
| 37 | continue; |
| 38 | } |
| 39 | if (length > 60) { |
| 40 | bb_error_msg_and_die("Line too long"); |
| 41 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 42 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 43 | line_ptr++; |
| 44 | /* Tolerate an overly long line to acomadate a possible exta '`' */ |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 45 | if (strlen(line_ptr) < (size_t)length) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 46 | bb_error_msg_and_die("Short file"); |
| 47 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 48 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 49 | while (length > 0) { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 50 | /* Merge four 6 bit chars to three 8 bit chars */ |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 51 | 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] | 52 | line_ptr++; |
| 53 | length--; |
| 54 | if (length == 0) { |
| 55 | break; |
| 56 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 57 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 58 | 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] | 59 | line_ptr++; |
| 60 | length--; |
| 61 | if (length == 0) { |
| 62 | break; |
| 63 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 64 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 65 | 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] | 66 | line_ptr += 2; |
| 67 | length -= 2; |
| 68 | } |
| 69 | free(line); |
| 70 | } |
| 71 | bb_error_msg_and_die("Short file"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 74 | static int read_base64(FILE *src_stream, FILE *dst_stream) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 75 | { |
"Vladimir N. Oleynik" | 87be316 | 2006-01-31 14:25:52 +0000 | [diff] [blame] | 76 | static const char base64_table[] = |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 77 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"; |
| 78 | char term_count = 0; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 79 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 80 | while (1) { |
| 81 | char translated[4]; |
| 82 | int count = 0; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 83 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 84 | while (count < 4) { |
| 85 | char *table_ptr; |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 86 | int ch; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 87 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 88 | /* Get next _valid_ character */ |
| 89 | do { |
| 90 | ch = fgetc(src_stream); |
| 91 | if (ch == EOF) { |
| 92 | bb_error_msg_and_die("Short file"); |
| 93 | } |
| 94 | } while ((table_ptr = strchr(base64_table, ch)) == NULL); |
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 | /* Convert encoded charcter to decimal */ |
| 97 | ch = table_ptr - base64_table; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 98 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 99 | if (*table_ptr == '=') { |
| 100 | if (term_count == 0) { |
| 101 | translated[count] = 0; |
| 102 | break; |
| 103 | } |
| 104 | term_count++; |
| 105 | } |
| 106 | else if (*table_ptr == '\n') { |
| 107 | /* Check for terminating line */ |
| 108 | if (term_count == 5) { |
| 109 | return(EXIT_SUCCESS); |
| 110 | } |
| 111 | term_count = 1; |
| 112 | continue; |
| 113 | } else { |
| 114 | translated[count] = ch; |
| 115 | count++; |
| 116 | term_count = 0; |
| 117 | } |
| 118 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 119 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 120 | /* Merge 6 bit chars to 8 bit */ |
| 121 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); |
| 122 | if (count > 2) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 123 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 124 | } |
| 125 | if (count > 3) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 126 | fputc(translated[2] << 6 | translated[3], dst_stream); |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 131 | int uudecode_main(int argc, char **argv) |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 132 | { |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 133 | int (*decode_fn_ptr) (FILE * src, FILE * dst); |
| 134 | FILE *src_stream; |
| 135 | char *outname = NULL; |
| 136 | char *line; |
| 137 | int opt; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 138 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 139 | opt = bb_getopt_ulflags(argc, argv, "o:", &outname); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 140 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 141 | if (optind == argc) { |
| 142 | src_stream = stdin; |
| 143 | } else if (optind + 1 == argc) { |
| 144 | src_stream = bb_xfopen(argv[optind], "r"); |
| 145 | } else { |
| 146 | bb_show_usage(); |
| 147 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 148 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 149 | /* Search for the start of the encoding */ |
| 150 | while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) { |
| 151 | char *line_ptr = NULL; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 152 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 153 | if (line == NULL) { |
| 154 | break; |
| 155 | } else if (strncmp(line, "begin-base64 ", 13) == 0) { |
| 156 | line_ptr = line + 13; |
| 157 | decode_fn_ptr = read_base64; |
| 158 | } else if (strncmp(line, "begin ", 6) == 0) { |
| 159 | line_ptr = line + 6; |
| 160 | decode_fn_ptr = read_stduu; |
| 161 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 162 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 163 | if (line_ptr) { |
| 164 | FILE *dst_stream; |
| 165 | int mode; |
| 166 | int ret; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 167 | |
Glenn L McGrath | 7f9de02 | 2003-11-06 03:17:23 +0000 | [diff] [blame] | 168 | mode = strtoul(line_ptr, NULL, 8); |
| 169 | if (outname == NULL) { |
| 170 | outname = strchr(line_ptr, ' '); |
| 171 | if ((outname == NULL) || (*outname == '\0')) { |
| 172 | break; |
| 173 | } |
| 174 | outname++; |
| 175 | } |
| 176 | if (strcmp(outname, "-") == 0) { |
| 177 | dst_stream = stdout; |
| 178 | } else { |
| 179 | dst_stream = bb_xfopen(outname, "w"); |
| 180 | chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
| 181 | } |
| 182 | free(line); |
| 183 | ret = decode_fn_ptr(src_stream, dst_stream); |
| 184 | bb_fclose_nonstdin(src_stream); |
| 185 | return(ret); |
| 186 | } |
| 187 | free(line); |
| 188 | } |
| 189 | bb_error_msg_and_die("No `begin' line"); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 190 | } |