blob: 8b18b7a7317ccdb20f18d94dee1bf48b43580605 [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/*
Denis Vlasenko0beaff82007-09-21 13:16:32 +00003 * Copyright 2003, Glenn McGrath
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 Vlasenko8ee649a2008-03-26 20:04:27 +000021 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000022 int encoded_len, str_len;
23 char *line_ptr, *dst;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000024
Glenn L McGrath7f9de022003-11-06 03:17:23 +000025 if (strcmp(line, "end") == 0) {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000026 return; /* the only non-error exit */
Glenn L McGrath7f9de022003-11-06 03:17:23 +000027 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000028
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000029 line_ptr = line;
30 while (*line_ptr) {
31 *line_ptr = (*line_ptr - 0x20) & 0x3f;
32 line_ptr++;
33 }
34 str_len = line_ptr - line;
35
36 encoded_len = line[0] * 4 / 3;
37 /* Check that line is not too short. (we tolerate
38 * overly _long_ line to accomodate possible extra '`').
39 * Empty line case is also caught here. */
40 if (str_len <= encoded_len) {
41 break; /* go to bb_error_msg_and_die("short file"); */
42 }
43 if (encoded_len <= 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000044 /* Ignore the "`\n" line, why is it even in the encode file ? */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000045 free(line);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000046 continue;
47 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000048 if (encoded_len > 60) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000049 bb_error_msg_and_die("line too long");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000050 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000051
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000052 dst = line;
53 line_ptr = line + 1;
54 do {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000055 /* Merge four 6 bit chars to three 8 bit chars */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000056 *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
57 encoded_len--;
58 if (encoded_len == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000059 break;
60 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000061
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000062 *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
63 encoded_len--;
64 if (encoded_len == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +000065 break;
66 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000067
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000068 *dst++ = line_ptr[2] << 6 | line_ptr[3];
69 line_ptr += 4;
70 encoded_len -= 2;
71 } while (encoded_len > 0);
72 fwrite(line, 1, dst - line, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +000073 free(line);
74 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000075 bb_error_msg_and_die("short file");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000076}
77
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +000078static void read_base64(FILE *src_stream, FILE *dst_stream)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000079{
Denis Vlasenko746204b2007-06-04 23:32:35 +000080 int term_count = 1;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000081
Glenn L McGrath7f9de022003-11-06 03:17:23 +000082 while (1) {
83 char translated[4];
84 int count = 0;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000085
Glenn L McGrath7f9de022003-11-06 03:17:23 +000086 while (count < 4) {
87 char *table_ptr;
Eric Andersen5e678872006-01-30 19:48:23 +000088 int ch;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000089
Denis Vlasenkoaa198dd2007-06-12 07:24:11 +000090 /* Get next _valid_ character.
91 * global vector bb_uuenc_tbl_base64[] contains this string:
92 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
93 */
Glenn L McGrath7f9de022003-11-06 03:17:23 +000094 do {
95 ch = fgetc(src_stream);
96 if (ch == EOF) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000097 bb_error_msg_and_die("short file");
Glenn L McGrath7f9de022003-11-06 03:17:23 +000098 }
Denis Vlasenkoaa198dd2007-06-12 07:24:11 +000099 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000100 } while (table_ptr == NULL);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000101
Denis Vlasenkoaa198dd2007-06-12 07:24:11 +0000102 /* Convert encoded character to decimal */
103 ch = table_ptr - bb_uuenc_tbl_base64;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000104
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000105 if (*table_ptr == '=') {
106 if (term_count == 0) {
Denis Vlasenko746204b2007-06-04 23:32:35 +0000107 translated[count] = '\0';
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000108 break;
109 }
110 term_count++;
Denis Vlasenko746204b2007-06-04 23:32:35 +0000111 } else if (*table_ptr == '\n') {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000112 /* Check for terminating line */
113 if (term_count == 5) {
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000114 return;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000115 }
116 term_count = 1;
117 continue;
118 } else {
119 translated[count] = ch;
120 count++;
121 term_count = 0;
122 }
123 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000124
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000125 /* Merge 6 bit chars to 8 bit */
Denis Vlasenko746204b2007-06-04 23:32:35 +0000126 if (count > 1) {
127 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
128 }
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000129 if (count > 2) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000130 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000131 }
132 if (count > 3) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000133 fputc(translated[2] << 6 | translated[3], dst_stream);
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000134 }
135 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000136}
137
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000138int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +0000139int uudecode_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000140{
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000141 FILE *src_stream;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000142 char *outname = NULL;
143 char *line;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000144
Denis Vlasenko746204b2007-06-04 23:32:35 +0000145 opt_complementary = "?1"; /* 1 argument max */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000146 getopt32(argv, "o:", &outname);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000147 argv += optind;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000148
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000149 if (!*argv)
150 *--argv = (char*)"-";
151 src_stream = xfopen_stdin(*argv);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000152
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000153 /* Search for the start of the encoding */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000154 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000155 void (*decode_fn_ptr)(FILE *src, FILE *dst);
Rob Landley29d94b92006-09-23 19:56:21 +0000156 char *line_ptr;
157 FILE *dst_stream;
158 int mode;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000159
Rob Landley565bc332006-07-31 22:50:12 +0000160 if (strncmp(line, "begin-base64 ", 13) == 0) {
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000161 line_ptr = line + 13;
162 decode_fn_ptr = read_base64;
163 } else if (strncmp(line, "begin ", 6) == 0) {
164 line_ptr = line + 6;
165 decode_fn_ptr = read_stduu;
Rob Landley29d94b92006-09-23 19:56:21 +0000166 } else {
167 free(line);
168 continue;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000169 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000170
Denis Vlasenko746204b2007-06-04 23:32:35 +0000171 /* begin line found. decode and exit */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000172 mode = bb_strtou(line_ptr, NULL, 8);
Rob Landley29d94b92006-09-23 19:56:21 +0000173 if (outname == NULL) {
174 outname = strchr(line_ptr, ' ');
175 if ((outname == NULL) || (*outname == '\0')) {
176 break;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000177 }
Rob Landley29d94b92006-09-23 19:56:21 +0000178 outname++;
179 }
Denis Vlasenko746204b2007-06-04 23:32:35 +0000180 dst_stream = stdout;
181 if (NOT_LONE_DASH(outname)) {
Rob Landley29d94b92006-09-23 19:56:21 +0000182 dst_stream = xfopen(outname, "w");
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000183 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000184 }
185 free(line);
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000186 decode_fn_ptr(src_stream, dst_stream);
Denis Vlasenko746204b2007-06-04 23:32:35 +0000187 /* fclose_if_not_stdin(src_stream); - redundant */
Bernhard Reutner-Fischerca5b3522007-01-20 21:29:32 +0000188 return EXIT_SUCCESS;
Glenn L McGrath7f9de022003-11-06 03:17:23 +0000189 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000190 bb_error_msg_and_die("no 'begin' line");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000191}
Denis Vlasenko746204b2007-06-04 23:32:35 +0000192
193/* Test script.
194Put this into an empty dir with busybox binary, an run.
195
196#!/bin/sh
197test -x busybox || { echo "No ./busybox?"; exit; }
198ln -sf busybox uudecode
199ln -sf busybox uuencode
200>A_null
201echo -n A >A
202echo -n AB >AB
203echo -n ABC >ABC
204echo -n ABCD >ABCD
205echo -n ABCDE >ABCDE
206echo -n ABCDEF >ABCDEF
207cat busybox >A_bbox
208for f in A*; do
209 echo uuencode $f
210 ./uuencode $f <$f >u_$f
211 ./uuencode -m $f <$f >m_$f
212done
213mkdir unpk_u unpk_m 2>/dev/null
214for f in u_*; do
215 ./uudecode <$f -o unpk_u/${f:2}
216 diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
217 echo uudecode $f: $?
218done
219for f in m_*; do
220 ./uudecode <$f -o unpk_m/${f:2}
221 diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
222 echo uudecode $f: $?
223done
224*/