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