Deleted Added
full compact
uudecode.c (90720) uudecode.c (91661)
1/*-
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
43#endif
44static const char rcsid[] =
1/*-
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/usr.bin/uudecode/uudecode.c 90720 2002-02-16 09:18:33Z mike $";
45 "$FreeBSD: head/usr.bin/uudecode/uudecode.c 91661 2002-03-05 03:27:47Z jmallett $";
46#endif /* not lint */
47
48/*
49 * uudecode [file ...]
50 *
51 * create the specified file, decoding as you go.
52 * used with uuencode.
53 */
54#include <sys/param.h>
46#endif /* not lint */
47
48/*
49 * uudecode [file ...]
50 *
51 * create the specified file, decoding as you go.
52 * used with uuencode.
53 */
54#include <sys/param.h>
55#include <sys/socket.h>
55#include <sys/stat.h>
56
56#include <sys/stat.h>
57
58#include <netinet/in.h>
59
57#include <err.h>
60#include <err.h>
58#include <fnmatch.h>
59#include <pwd.h>
61#include <pwd.h>
62#include <resolv.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <unistd.h>
64
65const char *filename;
66char *outfile;
67int cflag, iflag, oflag, pflag, sflag;
68
69static void usage __P((void));
70int decode __P((void));
71int decode2 __P((int));
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67
68const char *filename;
69char *outfile;
70int cflag, iflag, oflag, pflag, sflag;
71
72static void usage __P((void));
73int decode __P((void));
74int decode2 __P((int));
75void base64_decode __P((const char *));
72
73int
74main(argc, argv)
75 int argc;
76 char *argv[];
77{
78 int rval, ch;
79
80 while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
81 switch(ch) {
82 case 'c':
83 if (oflag)
84 usage();
85 cflag = 1; /* multiple uudecode'd files */
86 break;
87 case 'i':
88 iflag = 1; /* ask before override files */
89 break;
90 case 'o':
91 if (cflag || pflag || sflag)
92 usage();
93 oflag = 1; /* output to the specified file */
94 sflag = 1; /* do not strip pathnames for output */
95 outfile = optarg; /* set the output filename */
96 break;
97 case 'p':
98 if (oflag)
99 usage();
100 pflag = 1; /* print output to stdout */
101 break;
102 case 's':
103 if (oflag)
104 usage();
105 sflag = 1; /* do not strip pathnames for output */
106 break;
107 default:
108 usage();
109 }
110 }
111 argc -= optind;
112 argv += optind;
113
114
115 if (*argv) {
116 rval = 0;
117 do {
118 if (!freopen(filename = *argv, "r", stdin)) {
119 warn("%s", *argv);
120 rval = 1;
121 continue;
122 }
123 rval |= decode();
124 } while (*++argv);
125 } else {
126 filename = "stdin";
127 rval = decode();
128 }
129 exit(rval);
130}
131
132int
133decode ()
134{
135 int flag;
136
137 /* decode only one file per input stream */
138 if (!cflag)
139 return(decode2(0));
140
141 /* multiple uudecode'd files */
142 for (flag = 0; ; flag++)
143 if (decode2(flag))
144 return(1);
145 else if (feof(stdin))
146 break;
147
148 return(0);
149}
150
151int
152decode2(flag)
153 int flag;
154{
155 struct passwd *pw;
156 register int n;
157 register char ch, *p;
76
77int
78main(argc, argv)
79 int argc;
80 char *argv[];
81{
82 int rval, ch;
83
84 while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
85 switch(ch) {
86 case 'c':
87 if (oflag)
88 usage();
89 cflag = 1; /* multiple uudecode'd files */
90 break;
91 case 'i':
92 iflag = 1; /* ask before override files */
93 break;
94 case 'o':
95 if (cflag || pflag || sflag)
96 usage();
97 oflag = 1; /* output to the specified file */
98 sflag = 1; /* do not strip pathnames for output */
99 outfile = optarg; /* set the output filename */
100 break;
101 case 'p':
102 if (oflag)
103 usage();
104 pflag = 1; /* print output to stdout */
105 break;
106 case 's':
107 if (oflag)
108 usage();
109 sflag = 1; /* do not strip pathnames for output */
110 break;
111 default:
112 usage();
113 }
114 }
115 argc -= optind;
116 argv += optind;
117
118
119 if (*argv) {
120 rval = 0;
121 do {
122 if (!freopen(filename = *argv, "r", stdin)) {
123 warn("%s", *argv);
124 rval = 1;
125 continue;
126 }
127 rval |= decode();
128 } while (*++argv);
129 } else {
130 filename = "stdin";
131 rval = decode();
132 }
133 exit(rval);
134}
135
136int
137decode ()
138{
139 int flag;
140
141 /* decode only one file per input stream */
142 if (!cflag)
143 return(decode2(0));
144
145 /* multiple uudecode'd files */
146 for (flag = 0; ; flag++)
147 if (decode2(flag))
148 return(1);
149 else if (feof(stdin))
150 break;
151
152 return(0);
153}
154
155int
156decode2(flag)
157 int flag;
158{
159 struct passwd *pw;
160 register int n;
161 register char ch, *p;
158 int ignore, mode, n1;
162 int base64, ignore, mode, n1;
159 char buf[MAXPATHLEN];
160 char buffn[MAXPATHLEN]; /* file name buffer */
161
163 char buf[MAXPATHLEN];
164 char buffn[MAXPATHLEN]; /* file name buffer */
165
162 ignore = 0;
166 base64 = ignore = 0;
163 /* search for header line */
164 do {
165 if (!fgets(buf, sizeof(buf), stdin)) {
166 if (flag) /* no error */
167 return(0);
168
169 warnx("%s: no \"begin\" line", filename);
170 return(1);
171 }
167 /* search for header line */
168 do {
169 if (!fgets(buf, sizeof(buf), stdin)) {
170 if (flag) /* no error */
171 return(0);
172
173 warnx("%s: no \"begin\" line", filename);
174 return(1);
175 }
172 } while (strncmp(buf, "begin ", 6) ||
173 fnmatch("begin [0-7]* *", buf, 0));
176 } while (strncmp(buf, "begin", 5) != 0);
174
177
178 if (strncmp(buf, "begin-base64", 12) == 0)
179 base64 = 1;
180
175 if (oflag) {
181 if (oflag) {
176 (void)sscanf(buf, "begin %o ", &mode);
182 if (base64)
183 (void)sscanf(buf, "begin-base64 %o ", &mode);
184 else
185 (void)sscanf(buf, "begin %o ", &mode);
177 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf)) {
178 warnx("%s: filename too long", outfile);
179 return (1);
180 }
186 if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf)) {
187 warnx("%s: filename too long", outfile);
188 return (1);
189 }
181 } else
182 (void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf);
190 } else {
191 if (base64)
192 (void)sscanf(buf, "begin-base64 %o %[^\n\r]", &mode, buf);
193 else
194 (void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf);
195 }
183
184 if (!sflag && !pflag) {
185 strncpy(buffn, buf, sizeof(buffn));
186 if (strrchr(buffn, '/') != NULL)
187 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
188 if (buf[0] == '\0') {
189 warnx("%s: illegal filename", buffn);
190 return(1);
191 }
192 }
193
194 /* handle ~user/file format */
195 if (buf[0] == '~') {
196 if (!(p = index(buf, '/'))) {
197 warnx("%s: illegal ~user", filename);
198 return(1);
199 }
200 *p++ = '\0';
201 if (!(pw = getpwnam(buf + 1))) {
202 warnx("%s: no user %s", filename, buf);
203 return(1);
204 }
205 n = strlen(pw->pw_dir);
206 n1 = strlen(p);
207 if (n + n1 + 2 > MAXPATHLEN) {
208 warnx("%s: path too long", filename);
209 return(1);
210 }
211 bcopy(p, buf + n + 1, n1 + 1);
212 bcopy(pw->pw_dir, buf, n);
213 buf[n] = '/';
214 }
215
216 /* create output file, set mode */
217 if (pflag)
218 ; /* print to stdout */
219
220 else {
221 if (iflag && !access(buf, F_OK)) {
222 (void)fprintf(stderr, "not overwritten: %s\n", buf);
223 ignore++;
224 } else if (!freopen(buf, "w", stdout) ||
225 fchmod(fileno(stdout), mode&0666)) {
226 warn("%s: %s", buf, filename);
227 return(1);
228 }
229 }
230 strcpy(buffn, buf); /* store file name from header line */
231
232 /* for each input line */
196
197 if (!sflag && !pflag) {
198 strncpy(buffn, buf, sizeof(buffn));
199 if (strrchr(buffn, '/') != NULL)
200 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
201 if (buf[0] == '\0') {
202 warnx("%s: illegal filename", buffn);
203 return(1);
204 }
205 }
206
207 /* handle ~user/file format */
208 if (buf[0] == '~') {
209 if (!(p = index(buf, '/'))) {
210 warnx("%s: illegal ~user", filename);
211 return(1);
212 }
213 *p++ = '\0';
214 if (!(pw = getpwnam(buf + 1))) {
215 warnx("%s: no user %s", filename, buf);
216 return(1);
217 }
218 n = strlen(pw->pw_dir);
219 n1 = strlen(p);
220 if (n + n1 + 2 > MAXPATHLEN) {
221 warnx("%s: path too long", filename);
222 return(1);
223 }
224 bcopy(p, buf + n + 1, n1 + 1);
225 bcopy(pw->pw_dir, buf, n);
226 buf[n] = '/';
227 }
228
229 /* create output file, set mode */
230 if (pflag)
231 ; /* print to stdout */
232
233 else {
234 if (iflag && !access(buf, F_OK)) {
235 (void)fprintf(stderr, "not overwritten: %s\n", buf);
236 ignore++;
237 } else if (!freopen(buf, "w", stdout) ||
238 fchmod(fileno(stdout), mode&0666)) {
239 warn("%s: %s", buf, filename);
240 return(1);
241 }
242 }
243 strcpy(buffn, buf); /* store file name from header line */
244
245 /* for each input line */
246next:
233 for (;;) {
234 if (!fgets(p = buf, sizeof(buf), stdin)) {
235 warnx("%s: short file", filename);
236 return(1);
237 }
247 for (;;) {
248 if (!fgets(p = buf, sizeof(buf), stdin)) {
249 warnx("%s: short file", filename);
250 return(1);
251 }
252 if (base64) {
253 if (strncmp(buf, "====", 4) == 0)
254 return (0);
255 base64_decode(buf);
256 goto next;
257 }
238#define DEC(c) (((c) - ' ') & 077) /* single character decode */
239#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
240/* #define IS_DEC(c) (1) */
241
242#define OUT_OF_RANGE \
243{ \
244 warnx( \
245"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
246 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
247 return(1); \
248}
249#define PUTCHAR(c) \
250if (!ignore) \
251 putchar(c)
252
253
254 /*
255 * `n' is used to avoid writing out all the characters
256 * at the end of the file.
257 */
258 if ((n = DEC(*p)) <= 0)
259 break;
260 for (++p; n > 0; p += 4, n -= 3)
261 if (n >= 3) {
262 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
263 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
264 OUT_OF_RANGE
265
266 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
267 PUTCHAR(ch);
268 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
269 PUTCHAR(ch);
270 ch = DEC(p[2]) << 6 | DEC(p[3]);
271 PUTCHAR(ch);
272
273 }
274 else {
275 if (n >= 1) {
276 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
277 OUT_OF_RANGE
278 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
279 PUTCHAR(ch);
280 }
281 if (n >= 2) {
282 if (!(IS_DEC(*(p + 1)) &&
283 IS_DEC(*(p + 2))))
284 OUT_OF_RANGE
285
286 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
287 PUTCHAR(ch);
288 }
289 if (n >= 3) {
290 if (!(IS_DEC(*(p + 2)) &&
291 IS_DEC(*(p + 3))))
292 OUT_OF_RANGE
293 ch = DEC(p[2]) << 6 | DEC(p[3]);
294 PUTCHAR(ch);
295 }
296 }
297 }
298 if (fgets(buf, sizeof(buf), stdin) == NULL ||
299 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
300 strcmp(buf, "end\r\n"))) {
301 warnx("%s: no \"end\" line", filename);
302 return(1);
303 }
304 return(0);
305}
306
258#define DEC(c) (((c) - ' ') & 077) /* single character decode */
259#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
260/* #define IS_DEC(c) (1) */
261
262#define OUT_OF_RANGE \
263{ \
264 warnx( \
265"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
266 filename, buffn, 1 + ' ', 077 + ' ' + 1); \
267 return(1); \
268}
269#define PUTCHAR(c) \
270if (!ignore) \
271 putchar(c)
272
273
274 /*
275 * `n' is used to avoid writing out all the characters
276 * at the end of the file.
277 */
278 if ((n = DEC(*p)) <= 0)
279 break;
280 for (++p; n > 0; p += 4, n -= 3)
281 if (n >= 3) {
282 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
283 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
284 OUT_OF_RANGE
285
286 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
287 PUTCHAR(ch);
288 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
289 PUTCHAR(ch);
290 ch = DEC(p[2]) << 6 | DEC(p[3]);
291 PUTCHAR(ch);
292
293 }
294 else {
295 if (n >= 1) {
296 if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
297 OUT_OF_RANGE
298 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
299 PUTCHAR(ch);
300 }
301 if (n >= 2) {
302 if (!(IS_DEC(*(p + 1)) &&
303 IS_DEC(*(p + 2))))
304 OUT_OF_RANGE
305
306 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
307 PUTCHAR(ch);
308 }
309 if (n >= 3) {
310 if (!(IS_DEC(*(p + 2)) &&
311 IS_DEC(*(p + 3))))
312 OUT_OF_RANGE
313 ch = DEC(p[2]) << 6 | DEC(p[3]);
314 PUTCHAR(ch);
315 }
316 }
317 }
318 if (fgets(buf, sizeof(buf), stdin) == NULL ||
319 (strcmp(buf, "end") && strcmp(buf, "end\n") &&
320 strcmp(buf, "end\r\n"))) {
321 warnx("%s: no \"end\" line", filename);
322 return(1);
323 }
324 return(0);
325}
326
327void
328base64_decode(stream)
329 const char *stream;
330{
331 unsigned char out[MAXPATHLEN * 4];
332 int rv;
333
334 rv = b64_pton(stream, out, (sizeof(out) / sizeof(out[0])));
335 if (rv == -1)
336 errx(1, "b64_pton: error decoding base64 input stream");
337 printf("%s", out);
338}
339
307static void
308usage()
309{
310 (void)fprintf(stderr, "usage: uudecode [-cips] [file ...]\n");
311 (void)fprintf(stderr, "usage: uudecode [-i] -o output_file [file]\n");
312 exit(1);
313}
340static void
341usage()
342{
343 (void)fprintf(stderr, "usage: uudecode [-cips] [file ...]\n");
344 (void)fprintf(stderr, "usage: uudecode [-i] -o output_file [file]\n");
345 exit(1);
346}