blob: 287386d9dee990e0c296644e4aaa077b72c51e79 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath7f9de022003-11-06 03:17:23 +00002/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00003 * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00004 *
Rob Landley0edbad12006-04-17 22:49:30 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen4e573f42000-11-14 23:29:24 +00006 *
Glenn L McGrath7f9de022003-11-06 03:17:23 +00007 * Based on specification from
8 * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
Eric Andersen4e573f42000-11-14 23:29:24 +00009 *
Rob Landley0edbad12006-04-17 22:49:30 +000010 * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
11 * "end" line
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000012 */
13
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000014
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000016
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +000017static void read_stduu(FILE *src_stream, FILE *dst_stream)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000018{
Glenn L McGrath7f9de022003-11-06 03:17:23 +000019 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000020
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000021 while ((line = xmalloc_getline(src_stream)) != NULL) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000022 int length;
23 char *line_ptr = line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000024
Glenn L McGrath7f9de022003-11-06 03:17:23 +000025 if (strcmp(line, "end") == 0) {
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +000026 return;
Glenn L McGrath7f9de022003-11-06 03:17:23 +000027 }
28 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000029
Glenn L McGrath7f9de022003-11-06 03:17:23 +000030 if (length <= 0) {
31 /* Ignore the "`\n" line, why is it even in the encode file ? */
32 continue;
33 }
34 if (length > 60) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000035 bb_error_msg_and_die("line too long");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000036 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000037
Glenn L McGrath7f9de022003-11-06 03:17:23 +000038 line_ptr++;
Bernhard Reutner-Fischer42f67022006-09-21 20:40:56 +000039 /* Tolerate an overly long line to accomodate a possible exta '`' */
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000040 if (strlen(line_ptr) < (size_t)length) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000041 bb_error_msg_and_die("short file");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000042 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000043
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000044 while (length > 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000045 /* Merge four 6 bit chars to three 8 bit chars */
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000046 fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000047 line_ptr++;
48 length--;
49 if (length == 0) {
50 break;
51 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000052
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053 fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000054 line_ptr++;
55 length--;
56 if (length == 0) {
57 break;
58 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000059
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060 fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000061 line_ptr += 2;
62 length -= 2;
63 }
64 free(line);
65 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000066 bb_error_msg_and_die("short file");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000067}
68
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +000069static void read_base64(FILE *src_stream, FILE *dst_stream)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000070{
"Vladimir N. Oleynik"87be3162006-01-31 14:25:52 +000071 static const char base64_table[] =
Glenn L McGrath7f9de022003-11-06 03:17:23 +000072 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
Denis Vlasenko746204b2007-06-04 23:32:35 +000073 int term_count = 1;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000074
Glenn L McGrath7f9de022003-11-06 03:17:23 +000075 while (1) {
76 char translated[4];
77 int count = 0;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000078
Glenn L McGrath7f9de022003-11-06 03:17:23 +000079 while (count < 4) {
80 char *table_ptr;
Eric Andersen5e678872006-01-30 19:48:23 +000081 int ch;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000082
Glenn L McGrath7f9de022003-11-06 03:17:23 +000083 /* Get next _valid_ character */
84 do {
85 ch = fgetc(src_stream);
86 if (ch == EOF) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000087 bb_error_msg_and_die("short file");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000088 }
Denis Vlasenko746204b2007-06-04 23:32:35 +000089 table_ptr = strchr(base64_table, ch);
90 } while (table_ptr == NULL);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000091
Glenn L McGrath7f9de022003-11-06 03:17:23 +000092 /* Convert encoded charcter to decimal */
93 ch = table_ptr - base64_table;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000094
Glenn L McGrath7f9de022003-11-06 03:17:23 +000095 if (*table_ptr == '=') {
96 if (term_count == 0) {
Denis Vlasenko746204b2007-06-04 23:32:35 +000097 translated[count] = '\0';
Glenn L McGrath7f9de022003-11-06 03:17:23 +000098 break;
99 }
100 term_count++;
Denis Vlasenko746204b2007-06-04 23:32:35 +0000101 } else if (*table_ptr == '\n') {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000102 /* Check for terminating line */
103 if (term_count == 5) {
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000104 return;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000105 }
106 term_count = 1;
107 continue;
108 } else {
109 translated[count] = ch;
110 count++;
111 term_count = 0;
112 }
113 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000114
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000115 /* Merge 6 bit chars to 8 bit */
Denis Vlasenko746204b2007-06-04 23:32:35 +0000116 if (count > 1) {
117 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
118 }
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000119 if (count > 2) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000120 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000121 }
122 if (count > 3) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000123 fputc(translated[2] << 6 | translated[3], dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000124 }
125 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000126}
127
Denis Vlasenko06af2162007-02-03 17:28:39 +0000128int uudecode_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000129int uudecode_main(int argc, char **argv)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000130{
Denis Vlasenko746204b2007-06-04 23:32:35 +0000131 FILE *src_stream = stdin;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000132 char *outname = NULL;
133 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000134
Denis Vlasenko746204b2007-06-04 23:32:35 +0000135 opt_complementary = "?1"; /* 1 argument max */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000136 getopt32(argc, argv, "o:", &outname);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000137 argv += optind;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000138
Denis Vlasenko746204b2007-06-04 23:32:35 +0000139 if (argv[0])
140 src_stream = xfopen(argv[0], "r");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000141
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000142 /* Search for the start of the encoding */
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000143 while ((line = xmalloc_getline(src_stream)) != NULL) {
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000144 void (*decode_fn_ptr)(FILE * src, FILE * dst);
Rob Landley29d94b92006-09-23 19:56:21 +0000145 char *line_ptr;
146 FILE *dst_stream;
147 int mode;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000148
Rob Landley565bc332006-07-31 22:50:12 +0000149 if (strncmp(line, "begin-base64 ", 13) == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000150 line_ptr = line + 13;
151 decode_fn_ptr = read_base64;
152 } else if (strncmp(line, "begin ", 6) == 0) {
153 line_ptr = line + 6;
154 decode_fn_ptr = read_stduu;
Rob Landley29d94b92006-09-23 19:56:21 +0000155 } else {
156 free(line);
157 continue;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000158 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000159
Denis Vlasenko746204b2007-06-04 23:32:35 +0000160 /* begin line found. decode and exit */
Rob Landley29d94b92006-09-23 19:56:21 +0000161 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 McGrath7f9de022003-11-06 03:17:23 +0000166 }
Rob Landley29d94b92006-09-23 19:56:21 +0000167 outname++;
168 }
Denis Vlasenko746204b2007-06-04 23:32:35 +0000169 dst_stream = stdout;
170 if (NOT_LONE_DASH(outname)) {
Rob Landley29d94b92006-09-23 19:56:21 +0000171 dst_stream = xfopen(outname, "w");
172 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000173 }
174 free(line);
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000175 decode_fn_ptr(src_stream, dst_stream);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000176 /* fclose_if_not_stdin(src_stream); - redundant */
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000177 return EXIT_SUCCESS;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000178 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000179 bb_error_msg_and_die("no 'begin' line");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000180}
Denis Vlasenko746204b2007-06-04 23:32:35 +0000181
182/* Test script.
183Put this into an empty dir with busybox binary, an run.
184
185#!/bin/sh
186test -x busybox || { echo "No ./busybox?"; exit; }
187ln -sf busybox uudecode
188ln -sf busybox uuencode
189>A_null
190echo -n A >A
191echo -n AB >AB
192echo -n ABC >ABC
193echo -n ABCD >ABCD
194echo -n ABCDE >ABCDE
195echo -n ABCDEF >ABCDEF
196cat busybox >A_bbox
197for f in A*; do
198 echo uuencode $f
199 ./uuencode $f <$f >u_$f
200 ./uuencode -m $f <$f >m_$f
201done
202mkdir unpk_u unpk_m 2>/dev/null
203for f in u_*; do
204 ./uudecode <$f -o unpk_u/${f:2}
205 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
206 echo uudecode $f: $?
207done
208for f in m_*; do
209 ./uudecode <$f -o unpk_m/${f:2}
210 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
211 echo uudecode $f: $?
212done
213*/