blob: 97801aa097ec8b400e029ab2478bd18a619038b0 [file] [log] [blame]
Eric Andersen2b6ab3c2000-06-13 06:54:53 +00001/* uuencode.c -- uuencode utility.
2 * Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3 *
4 * This product is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This product is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this product; see the file COPYING. If not, write to
16 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20/* Copyright (c) 1983 Regents of the University of California.
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the University of
34 * California, Berkeley and its contributors.
35 * 4. Neither the name of the University nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 */
51
52/* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */
53/* Hacked to work with BusyBox by Alfred M. Szmidt */
54
55#include "internal.h"
56
57#include <stdio.h>
58#include <errno.h>
59#include <pwd.h>
60
61#define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
62
63static void encode __P ((void));
64
65/* Pointer to the translation table we currently use. */
66const char *trans_ptr;
67
68/* The two currently defined translation tables. The first is the
69 standard uuencoding, the second is base64 encoding. */
70const char uu_std[64] = {
71 '`', '!', '"', '#', '$', '%', '&', '\'',
72 '(', ')', '*', '+', ',', '-', '.', '/',
73 '0', '1', '2', '3', '4', '5', '6', '7',
74 '8', '9', ':', ';', '<', '=', '>', '?',
75 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
76 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
77 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
78 'X', 'Y', 'Z', '[', '\\', ']', '^', '_'
79};
80
81const char uu_base64[64] = {
82 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
83 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
84 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
85 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
86 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
87 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
88 'w', 'x', 'y', 'z', '0', '1', '2', '3',
89 '4', '5', '6', '7', '8', '9', '+', '/'
90};
91
92/* ENC is the basic 1 character encoding function to make a char printing. */
93#define ENC(Char) (trans_ptr[(Char) & 077])
94
95/* Copy from IN to OUT, encoding as you go along. */
96static void encode()
97{
98 register int ch, n;
99 char *p = NULL;
100 char buf[80];
101
102 while (1) {
103 n = 0;
104 do {
105 register int m = fread (buf, 1, 45 - n, stdin);
106 if (m == 0)
107 break;
108 n += m;
109 }
110 while (n < 45);
111
112 if (n == 0)
113 break;
114
115 if (trans_ptr == uu_std)
116 if (putchar (ENC (n)) == EOF)
117 break;
118 for (p = buf; n > 2; n -= 3, p += 3) {
119 ch = *p >> 2;
120 ch = ENC (ch);
121 if (putchar (ch) == EOF)
122 break;
123 ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
124 ch = ENC (ch);
125 if (putchar (ch) == EOF)
126 break;
127 ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
128 ch = ENC (ch);
129 if (putchar (ch) == EOF)
130 break;
131 ch = p[2] & 077;
132 ch = ENC (ch);
133 if (putchar (ch) == EOF)
134 break;
135 }
136
137 if (n != 0)
138 break;
139
140 if (putchar ('\n') == EOF)
141 break;
142 }
143
144 while (n != 0) {
145 char c1 = *p;
146 char c2 = n == 1 ? 0 : p[1];
147
148 ch = c1 >> 2;
149 ch = ENC (ch);
150 if (putchar (ch) == EOF)
151 break;
152
153 ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017);
154 ch = ENC (ch);
155 if (putchar (ch) == EOF)
156 break;
157
158 if (n == 1)
159 ch = trans_ptr == uu_std ? ENC ('\0') : '=';
160 else {
161 ch = (c2 << 2) & 074;
162 ch = ENC (ch);
163 }
164 if (putchar (ch) == EOF)
165 break;
166 ch = trans_ptr == uu_std ? ENC ('\0') : '=';
167 if (putchar (ch) == EOF)
168 break;
169 putchar ('\n');
170 break;
171 }
172
173 if (ferror (stdin))
174 errorMsg("Read error\n");
175
176 if (trans_ptr == uu_std) {
177 putchar (ENC ('\0'));
178 putchar ('\n');
179 }
180}
181
182static const char uuencode_usage[] =
183 "uuencode [OPTION] [INFILE] REMOTEFILE\n"
184#ifndef BB_FEATURE_TRIVIAL_HELP
185 "\nUuencode a file.\n\n"
186 "Options:\n"
187 "\t-m\tuse base64 encoding as of RFC1521\n"
188#endif
189;
190
191int uuencode_main (int argc,
192 char **argv)
193{
194 int opt;
195 struct stat sb;
196 int mode;
197
198 trans_ptr = uu_std; /* Standard encoding is old uu format */
199
200 /* Parse any options */
201 while ((opt = getopt (argc, argv, "m")) != EOF) {
202 switch (opt) {
203 case 'm':
204 trans_ptr = uu_base64;
205 break;
206
207 case 0:
208 break;
209
210 default:
211 usage(uuencode_usage);
212 }
213 }
214
215 switch (argc - optind) {
216 case 2:
217 /* Optional first argument is input file. */
218 if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) {
219 errorMsg("uuencode: %s: %s\n", argv[optind], strerror(errno));
220 exit FALSE;
221 }
222 mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
223 optind++;
224 break;
225
226 case 1:
227 mode = RW & ~umask (RW);
228 break;
229
230 case 0:
231 default:
232 usage(uuencode_usage);
233 }
234
235 printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64",
236 mode, argv[optind]);
237 encode();
238 printf(trans_ptr == uu_std ? "end\n" : "====\n");
239 if (ferror (stdout)) {
240 errorMsg("Write error\n");
241 exit FALSE;
242 }
Eric Andersenb6106152000-06-19 17:25:40 +0000243 return( TRUE);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000244}