blob: 8d5af4a8afc6101456f6f2285e43366b6f95e88e [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 Andersened3ef502001-01-27 08:24:39 +000030#include <stdlib.h>
Eric Andersen2b6ab3c2000-06-13 06:54:53 +000031
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))
Matt Kraaidd19c692001-01-31 19:00:21 +0000145 error_msg("Read error");
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000146
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 */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000163 while ((opt = getopt (argc, argv, "m")) > 0) {
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000164 switch (opt) {
165 case 'm':
166 trans_ptr = uu_base64;
167 break;
168
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000169 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000170 show_usage();
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000171 }
172 }
173
174 switch (argc - optind) {
175 case 2:
176 /* Optional first argument is input file. */
177 if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000178 perror_msg("%s", argv[optind]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000179 return EXIT_FAILURE;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000180 }
181 mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
182 optind++;
183 break;
184
185 case 1:
186 mode = RW & ~umask (RW);
187 break;
188
189 case 0:
190 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000191 show_usage();
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000192 }
193
194 printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64",
195 mode, argv[optind]);
196 encode();
197 printf(trans_ptr == uu_std ? "end\n" : "====\n");
198 if (ferror (stdout)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000199 error_msg("Write error");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000200 return EXIT_FAILURE;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000201 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000202 return EXIT_SUCCESS;
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000203}
Eric Andersen4e573f42000-11-14 23:29:24 +0000204
205/* Copyright (c) 1983 Regents of the University of California.
206 * All rights reserved.
207 *
208 * Redistribution and use in source and binary forms, with or without
209 * modification, are permitted provided that the following conditions
210 * are met:
211 * 1. Redistributions of source code must retain the above copyright
212 * notice, this list of conditions and the following disclaimer.
213 * 2. Redistributions in binary form must reproduce the above copyright
214 * notice, this list of conditions and the following disclaimer in the
215 * documentation and/or other materials provided with the distribution.
216 *
217 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
218 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
219 *
220 * 4. Neither the name of the University nor the names of its contributors
221 * may be used to endorse or promote products derived from this software
222 * without specific prior written permission.
223 *
224 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
228 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
230 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
234 * SUCH DAMAGE.
235 */
236
237