1 /* uuencode.c -- uuencode utility.
2 * Copyright (C) 1994, 1995 Free Software Foundation, Inc.
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)
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.
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
19 * Original copyright notice is retained at the end of this file.
21 /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */
22 /* Hacked to work with BusyBox by Alfred M. Szmidt */
32 #define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
34 static void encode __P ((void));
36 /* Pointer to the translation table we currently use. */
37 const char *trans_ptr;
39 /* The two currently defined translation tables. The first is the
40 standard uuencoding, the second is base64 encoding. */
41 const 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', '[', '\\', ']', '^', '_'
52 const 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', '+', '/'
63 /* ENC is the basic 1 character encoding function to make a char printing. */
64 #define ENC(Char) (trans_ptr[(Char) & 077])
66 /* Copy from IN to OUT, encoding as you go along. */
76 register int m = fread (buf, 1, 45 - n, stdin);
86 if (trans_ptr == uu_std)
87 if (putchar (ENC (n)) == EOF)
89 for (p = buf; n > 2; n -= 3, p += 3) {
92 if (putchar (ch) == EOF)
94 ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
96 if (putchar (ch) == EOF)
98 ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
100 if (putchar (ch) == EOF)
104 if (putchar (ch) == EOF)
111 if (putchar ('\n') == EOF)
117 char c2 = n == 1 ? 0 : p[1];
121 if (putchar (ch) == EOF)
124 ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017);
126 if (putchar (ch) == EOF)
130 ch = trans_ptr == uu_std ? ENC ('\0') : '=';
132 ch = (c2 << 2) & 074;
135 if (putchar (ch) == EOF)
137 ch = trans_ptr == uu_std ? ENC ('\0') : '=';
138 if (putchar (ch) == EOF)
145 error_msg("Read error\n");
147 if (trans_ptr == uu_std) {
148 putchar (ENC ('\0'));
153 int uuencode_main (int argc,
160 trans_ptr = uu_std; /* Standard encoding is old uu format */
162 /* Parse any options */
163 while ((opt = getopt (argc, argv, "m")) != EOF) {
166 trans_ptr = uu_base64;
173 usage(uuencode_usage);
177 switch (argc - optind) {
179 /* Optional first argument is input file. */
180 if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) {
181 error_msg("%s: %s\n", argv[optind], strerror(errno));
184 mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
189 mode = RW & ~umask (RW);
194 usage(uuencode_usage);
197 printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64",
200 printf(trans_ptr == uu_std ? "end\n" : "====\n");
201 if (ferror (stdout)) {
202 error_msg("Write error\n");
208 /* Copyright (c) 1983 Regents of the University of California.
209 * All rights reserved.
211 * Redistribution and use in source and binary forms, with or without
212 * modification, are permitted provided that the following conditions
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.
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>
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.
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