blob: bf03252ac1a226ba0e3679f98f8ba6afa56029a2 [file] [log] [blame]
Glenn L McGrath7f9de022003-11-06 03:17:23 +00001/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00002 * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00003 *
Rob Landley0edbad12006-04-17 22:49:30 +00004 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen4e573f42000-11-14 23:29:24 +00005 *
Glenn L McGrath7f9de022003-11-06 03:17:23 +00006 * Based on specification from
7 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
Eric Andersen4e573f42000-11-14 23:29:24 +00008 *
Rob Landley0edbad12006-04-17 22:49:30 +00009 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
10 * "end" line
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000011 */
12
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000013
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000014#include <stdio.h>
15#include <errno.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000016#include <getopt.h> /* optind */
Eric Andersened3ef502001-01-27 08:24:39 +000017#include <string.h>
18#include <stdlib.h>
"Vladimir N. Oleynik"ba248202005-10-06 15:18:09 +000019#include "busybox.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000020
Glenn L McGrath7f9de022003-11-06 03:17:23 +000021static int read_stduu(FILE *src_stream, FILE *dst_stream)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000022{
Glenn L McGrath7f9de022003-11-06 03:17:23 +000023 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000024
Glenn L McGrath7f9de022003-11-06 03:17:23 +000025 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
26 int length;
27 char *line_ptr = line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000028
Glenn L McGrath7f9de022003-11-06 03:17:23 +000029 if (strcmp(line, "end") == 0) {
30 return(EXIT_SUCCESS);
31 }
32 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000033
Glenn L McGrath7f9de022003-11-06 03:17:23 +000034 if (length <= 0) {
35 /* Ignore the "`\n" line, why is it even in the encode file ? */
36 continue;
37 }
38 if (length > 60) {
39 bb_error_msg_and_die("Line too long");
40 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000041
Glenn L McGrath7f9de022003-11-06 03:17:23 +000042 line_ptr++;
43 /* Tolerate an overly long line to acomadate a possible exta '`' */
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000044 if (strlen(line_ptr) < (size_t)length) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000045 bb_error_msg_and_die("Short file");
46 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000047
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000048 while (length > 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000049 /* Merge four 6 bit chars to three 8 bit chars */
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000050 fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000051 line_ptr++;
52 length--;
53 if (length == 0) {
54 break;
55 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000056
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057 fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000058 line_ptr++;
59 length--;
60 if (length == 0) {
61 break;
62 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000063
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000064 fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000065 line_ptr += 2;
66 length -= 2;
67 }
68 free(line);
69 }
70 bb_error_msg_and_die("Short file");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000071}
72
Glenn L McGrath7f9de022003-11-06 03:17:23 +000073static int read_base64(FILE *src_stream, FILE *dst_stream)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000074{
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000075 static const char base64_table[] =
Glenn L McGrath7f9de022003-11-06 03:17:23 +000076 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
77 char term_count = 0;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000078
Glenn L McGrath7f9de022003-11-06 03:17:23 +000079 while (1) {
80 char translated[4];
81 int count = 0;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000082
Glenn L McGrath7f9de022003-11-06 03:17:23 +000083 while (count < 4) {
84 char *table_ptr;
Eric Andersen5e678872006-01-30 19:48:23 +000085 int ch;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000086
Glenn L McGrath7f9de022003-11-06 03:17:23 +000087 /* Get next _valid_ character */
88 do {
89 ch = fgetc(src_stream);
90 if (ch == EOF) {
91 bb_error_msg_and_die("Short file");
92 }
93 } while ((table_ptr = strchr(base64_table, ch)) == NULL);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000094
Glenn L McGrath7f9de022003-11-06 03:17:23 +000095 /* Convert encoded charcter to decimal */
96 ch = table_ptr - base64_table;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000097
Glenn L McGrath7f9de022003-11-06 03:17:23 +000098 if (*table_ptr == '=') {
99 if (term_count == 0) {
100 translated[count] = 0;
101 break;
102 }
103 term_count++;
104 }
105 else if (*table_ptr == '\n') {
106 /* Check for terminating line */
107 if (term_count == 5) {
108 return(EXIT_SUCCESS);
109 }
110 term_count = 1;
111 continue;
112 } else {
113 translated[count] = ch;
114 count++;
115 term_count = 0;
116 }
117 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000118
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000119 /* Merge 6 bit chars to 8 bit */
120 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
121 if (count > 2) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000123 }
124 if (count > 3) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000125 fputc(translated[2] << 6 | translated[3], dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000126 }
127 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000128}
129
Rob Landleydfba7412006-03-06 20:47:33 +0000130int uudecode_main(int argc, char **argv)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000131{
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000132 int (*decode_fn_ptr) (FILE * src, FILE * dst);
133 FILE *src_stream;
134 char *outname = NULL;
135 char *line;
136 int opt;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000137
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000138 opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000139
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000140 if (optind == argc) {
141 src_stream = stdin;
142 } else if (optind + 1 == argc) {
143 src_stream = bb_xfopen(argv[optind], "r");
144 } else {
145 bb_show_usage();
146 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000147
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000148 /* Search for the start of the encoding */
149 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
150 char *line_ptr = NULL;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000151
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000152 if (line == NULL) {
153 break;
154 } else if (strncmp(line, "begin-base64 ", 13) == 0) {
155 line_ptr = line + 13;
156 decode_fn_ptr = read_base64;
157 } else if (strncmp(line, "begin ", 6) == 0) {
158 line_ptr = line + 6;
159 decode_fn_ptr = read_stduu;
160 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000161
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000162 if (line_ptr) {
163 FILE *dst_stream;
164 int mode;
165 int ret;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000166
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000167 mode = strtoul(line_ptr, NULL, 8);
168 if (outname == NULL) {
169 outname = strchr(line_ptr, ' ');
170 if ((outname == NULL) || (*outname == '\0')) {
171 break;
172 }
173 outname++;
174 }
175 if (strcmp(outname, "-") == 0) {
176 dst_stream = stdout;
177 } else {
178 dst_stream = bb_xfopen(outname, "w");
179 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
180 }
181 free(line);
182 ret = decode_fn_ptr(src_stream, dst_stream);
183 bb_fclose_nonstdin(src_stream);
184 return(ret);
185 }
186 free(line);
187 }
188 bb_error_msg_and_die("No `begin' line");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000189}