blob: 8d15adbf6d87b58d9303cfd642401303c91d506f [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.
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000018 *
Eric Andersen4e573f42000-11-14 23:29:24 +000019 * Original copyright notice is retained at the end of this file.
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000020 */
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000021/* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */
22/* Hacked to work with BusyBox by Alfred M. Szmidt */
23
Eric Andersen4e573f42000-11-14 23:29:24 +000024
Eric Andersen3570a342000-09-25 21:45:58 +000025#include "busybox.h"
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000026
27#include <stdio.h>
28#include <errno.h>
Eric Andersen999bf722000-07-09 06:59:58 +000029#include <getopt.h>
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000030#include <pwd.h>
31
32#define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
33
34static void encode __P ((void));
35
36/* Pointer to the translation table we currently use. */
37const char *trans_ptr;
38
39/* The two currently defined translation tables. The first is the
40 standard uuencoding, the second is base64 encoding. */
41const char uu_std[64] = {
42 '`', '!', '"', '#', '$', '%', '&', '\'',
43 '(', ')', '*', '+', ',', '-', '.', '/',
44 '0', '1', '2', '3', '4', '5', '6', '7',
45 '8', '9', ':', ';', '<', '=', '>', '?',
46 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
47 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
48 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
49 'X', 'Y', 'Z', '[', '\\', ']', '^', '_'
50};
51
52const char uu_base64[64] = {
53 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
54 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
55 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
56 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
57 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
58 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
59 'w', 'x', 'y', 'z', '0', '1', '2', '3',
60 '4', '5', '6', '7', '8', '9', '+', '/'
61};
62
63/* ENC is the basic 1 character encoding function to make a char printing. */
64#define ENC(Char) (trans_ptr[(Char) & 077])
65
66/* Copy from IN to OUT, encoding as you go along. */
67static void encode()
68{
69 register int ch, n;
70 char *p = NULL;
71 char buf[80];
72
73 while (1) {
74 n = 0;
75 do {
76 register int m = fread (buf, 1, 45 - n, stdin);
77 if (m == 0)
78 break;
79 n += m;
80 }
81 while (n < 45);
82
83 if (n == 0)
84 break;
85
86 if (trans_ptr == uu_std)
87 if (putchar (ENC (n)) == EOF)
88 break;
89 for (p = buf; n > 2; n -= 3, p += 3) {
90 ch = *p >> 2;
91 ch = ENC (ch);
92 if (putchar (ch) == EOF)
93 break;
94 ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
95 ch = ENC (ch);
96 if (putchar (ch) == EOF)
97 break;
98 ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
99 ch = ENC (ch);
100 if (putchar (ch) == EOF)
101 break;
102 ch = p[2] & 077;
103 ch = ENC (ch);
104 if (putchar (ch) == EOF)
105 break;
106 }
107
108 if (n != 0)
109 break;
110
111 if (putchar ('\n') == EOF)
112 break;
113 }
114
115 while (n != 0) {
116 char c1 = *p;
117 char c2 = n == 1 ? 0 : p[1];
118
119 ch = c1 >> 2;
120 ch = ENC (ch);
121 if (putchar (ch) == EOF)
122 break;
123
124 ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017);
125 ch = ENC (ch);
126 if (putchar (ch) == EOF)
127 break;
128
129 if (n == 1)
130 ch = trans_ptr == uu_std ? ENC ('\0') : '=';
131 else {
132 ch = (c2 << 2) & 074;
133 ch = ENC (ch);
134 }
135 if (putchar (ch) == EOF)
136 break;
137 ch = trans_ptr == uu_std ? ENC ('\0') : '=';
138 if (putchar (ch) == EOF)
139 break;
140 putchar ('\n');
141 break;
142 }
143
144 if (ferror (stdin))
145 errorMsg("Read error\n");
146
147 if (trans_ptr == uu_std) {
148 putchar (ENC ('\0'));
149 putchar ('\n');
150 }
151}
152
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000153int uuencode_main (int argc,
154 char **argv)
155{
156 int opt;
157 struct stat sb;
158 int mode;
159
160 trans_ptr = uu_std; /* Standard encoding is old uu format */
161
162 /* Parse any options */
163 while ((opt = getopt (argc, argv, "m")) != EOF) {
164 switch (opt) {
165 case 'm':
166 trans_ptr = uu_base64;
167 break;
168
169 case 0:
170 break;
171
172 default:
173 usage(uuencode_usage);
174 }
175 }
176
177 switch (argc - optind) {
178 case 2:
179 /* Optional first argument is input file. */
180 if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000181 errorMsg("%s: %s\n", argv[optind], strerror(errno));
Matt Kraai3e856ce2000-12-01 02:55:13 +0000182 return EXIT_FAILURE;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000183 }
184 mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
185 optind++;
186 break;
187
188 case 1:
189 mode = RW & ~umask (RW);
190 break;
191
192 case 0:
193 default:
194 usage(uuencode_usage);
195 }
196
197 printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64",
198 mode, argv[optind]);
199 encode();
200 printf(trans_ptr == uu_std ? "end\n" : "====\n");
201 if (ferror (stdout)) {
202 errorMsg("Write error\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000203 return EXIT_FAILURE;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000204 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000205 return EXIT_SUCCESS;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000206}
Eric Andersen4e573f42000-11-14 23:29:24 +0000207
208/* Copyright (c) 1983 Regents of the University of California.
209 * All rights reserved.
210 *
211 * Redistribution and use in source and binary forms, with or without
212 * modification, are permitted provided that the following conditions
213 * are met:
214 * 1. Redistributions of source code must retain the above copyright
215 * notice, this list of conditions and the following disclaimer.
216 * 2. Redistributions in binary form must reproduce the above copyright
217 * notice, this list of conditions and the following disclaimer in the
218 * documentation and/or other materials provided with the distribution.
219 *
220 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
221 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
222 *
223 * 4. Neither the name of the University nor the names of its contributors
224 * may be used to endorse or promote products derived from this software
225 * without specific prior written permission.
226 *
227 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
230 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
235 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
237 * SUCH DAMAGE.
238 */
239
240