xref: /minix3/usr.bin/uudecode/uudecode.c (revision 2fde3a4846bf5ff2e91c001c8718329d949fe938)
1*2fde3a48SSky Liu /*	$NetBSD: uudecode.c,v 1.28 2013/01/28 19:50:30 apb Exp $	*/
2*2fde3a48SSky Liu 
3*2fde3a48SSky Liu /*-
4*2fde3a48SSky Liu  * Copyright (c) 1983, 1993
5*2fde3a48SSky Liu  *	The Regents of the University of California.  All rights reserved.
6*2fde3a48SSky Liu  *
7*2fde3a48SSky Liu  * Redistribution and use in source and binary forms, with or without
8*2fde3a48SSky Liu  * modification, are permitted provided that the following conditions
9*2fde3a48SSky Liu  * are met:
10*2fde3a48SSky Liu  * 1. Redistributions of source code must retain the above copyright
11*2fde3a48SSky Liu  *    notice, this list of conditions and the following disclaimer.
12*2fde3a48SSky Liu  * 2. Redistributions in binary form must reproduce the above copyright
13*2fde3a48SSky Liu  *    notice, this list of conditions and the following disclaimer in the
14*2fde3a48SSky Liu  *    documentation and/or other materials provided with the distribution.
15*2fde3a48SSky Liu  * 3. Neither the name of the University nor the names of its contributors
16*2fde3a48SSky Liu  *    may be used to endorse or promote products derived from this software
17*2fde3a48SSky Liu  *    without specific prior written permission.
18*2fde3a48SSky Liu  *
19*2fde3a48SSky Liu  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*2fde3a48SSky Liu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*2fde3a48SSky Liu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*2fde3a48SSky Liu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*2fde3a48SSky Liu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*2fde3a48SSky Liu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*2fde3a48SSky Liu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*2fde3a48SSky Liu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*2fde3a48SSky Liu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*2fde3a48SSky Liu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*2fde3a48SSky Liu  * SUCH DAMAGE.
30*2fde3a48SSky Liu  */
31*2fde3a48SSky Liu 
32*2fde3a48SSky Liu #if HAVE_NBTOOL_CONFIG_H
33*2fde3a48SSky Liu #include "nbtool_config.h"
34*2fde3a48SSky Liu #endif
35*2fde3a48SSky Liu 
36*2fde3a48SSky Liu #include <sys/cdefs.h>
37*2fde3a48SSky Liu #if !defined(lint)
38*2fde3a48SSky Liu __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
39*2fde3a48SSky Liu  The Regents of the University of California.  All rights reserved.");
40*2fde3a48SSky Liu #if 0
41*2fde3a48SSky Liu static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
42*2fde3a48SSky Liu #endif
43*2fde3a48SSky Liu __RCSID("$NetBSD: uudecode.c,v 1.28 2013/01/28 19:50:30 apb Exp $");
44*2fde3a48SSky Liu #endif /* not lint */
45*2fde3a48SSky Liu 
46*2fde3a48SSky Liu /*
47*2fde3a48SSky Liu  * uudecode [file ...]
48*2fde3a48SSky Liu  *
49*2fde3a48SSky Liu  * create the specified file, decoding as you go.
50*2fde3a48SSky Liu  * used with uuencode.
51*2fde3a48SSky Liu  */
52*2fde3a48SSky Liu #include <sys/param.h>
53*2fde3a48SSky Liu #include <sys/stat.h>
54*2fde3a48SSky Liu #include <ctype.h>
55*2fde3a48SSky Liu #include <err.h>
56*2fde3a48SSky Liu #include <errno.h>
57*2fde3a48SSky Liu #include <limits.h>
58*2fde3a48SSky Liu #include <locale.h>
59*2fde3a48SSky Liu #include <pwd.h>
60*2fde3a48SSky Liu #include <stdio.h>
61*2fde3a48SSky Liu #include <stdlib.h>
62*2fde3a48SSky Liu #include <string.h>
63*2fde3a48SSky Liu #include <unistd.h>
64*2fde3a48SSky Liu 
65*2fde3a48SSky Liu #ifndef NO_BASE64
66*2fde3a48SSky Liu #include <netinet/in.h>
67*2fde3a48SSky Liu #include <resolv.h>
68*2fde3a48SSky Liu #endif
69*2fde3a48SSky Liu 
70*2fde3a48SSky Liu static int decode(char *);
71*2fde3a48SSky Liu __dead static void usage(void);
72*2fde3a48SSky Liu static int checkend(const char *, const char *, const char *);
73*2fde3a48SSky Liu static int base64_decode(void);
74*2fde3a48SSky Liu 
75*2fde3a48SSky Liu static int base64;
76*2fde3a48SSky Liu static const char *inputname;
77*2fde3a48SSky Liu 
78*2fde3a48SSky Liu int
main(int argc,char * argv[])79*2fde3a48SSky Liu main(int argc, char *argv[])
80*2fde3a48SSky Liu {
81*2fde3a48SSky Liu 	int ch, rval;
82*2fde3a48SSky Liu 	char *outputname = NULL;
83*2fde3a48SSky Liu 
84*2fde3a48SSky Liu 	setlocale(LC_ALL, "");
85*2fde3a48SSky Liu 	setprogname(argv[0]);
86*2fde3a48SSky Liu 
87*2fde3a48SSky Liu 	while ((ch = getopt(argc, argv, "mo:p")) != -1)
88*2fde3a48SSky Liu 		switch (ch) {
89*2fde3a48SSky Liu 		case 'm':
90*2fde3a48SSky Liu 			base64 = 1;
91*2fde3a48SSky Liu 			break;
92*2fde3a48SSky Liu 		case 'o':
93*2fde3a48SSky Liu 			outputname = optarg;
94*2fde3a48SSky Liu 			break;
95*2fde3a48SSky Liu 		case 'p':
96*2fde3a48SSky Liu 			outputname = __UNCONST("/dev/stdout");
97*2fde3a48SSky Liu 			break;
98*2fde3a48SSky Liu 		default:
99*2fde3a48SSky Liu 			usage();
100*2fde3a48SSky Liu 		}
101*2fde3a48SSky Liu 	argc -= optind;
102*2fde3a48SSky Liu 	argv += optind;
103*2fde3a48SSky Liu 
104*2fde3a48SSky Liu 	if (*argv) {
105*2fde3a48SSky Liu 		rval = 0;
106*2fde3a48SSky Liu 		do {
107*2fde3a48SSky Liu 			if (!freopen(inputname = *argv, "r", stdin)) {
108*2fde3a48SSky Liu 				warn("%s", *argv);
109*2fde3a48SSky Liu 				rval = 1;
110*2fde3a48SSky Liu 				continue;
111*2fde3a48SSky Liu 			}
112*2fde3a48SSky Liu 			rval |= decode(outputname);
113*2fde3a48SSky Liu 		} while (*++argv);
114*2fde3a48SSky Liu 	} else {
115*2fde3a48SSky Liu 		inputname = "stdin";
116*2fde3a48SSky Liu 		rval = decode(outputname);
117*2fde3a48SSky Liu 	}
118*2fde3a48SSky Liu 	exit(rval);
119*2fde3a48SSky Liu }
120*2fde3a48SSky Liu 
121*2fde3a48SSky Liu /*
122*2fde3a48SSky Liu  * Decode one file from stdin.  If outputname is not NULL
123*2fde3a48SSky Liu  * then it overrides the file name embedded in the input data.
124*2fde3a48SSky Liu  */
125*2fde3a48SSky Liu static int
decode(char * outputname)126*2fde3a48SSky Liu decode(char *outputname)
127*2fde3a48SSky Liu {
128*2fde3a48SSky Liu 	struct passwd *pw;
129*2fde3a48SSky Liu 	int n;
130*2fde3a48SSky Liu 	char ch, *p;
131*2fde3a48SSky Liu 	int n1;
132*2fde3a48SSky Liu 	long mode;
133*2fde3a48SSky Liu 	char *fn;
134*2fde3a48SSky Liu 	char buf[MAXPATHLEN];
135*2fde3a48SSky Liu 
136*2fde3a48SSky Liu 	/* search for header line */
137*2fde3a48SSky Liu 	for (;;) {
138*2fde3a48SSky Liu 		if (!fgets(buf, sizeof(buf), stdin)) {
139*2fde3a48SSky Liu 			warnx("%s: no \"%s\" line", inputname, base64 ?
140*2fde3a48SSky Liu 					"begin-base64" : "begin");
141*2fde3a48SSky Liu 			return(1);
142*2fde3a48SSky Liu 		}
143*2fde3a48SSky Liu 		p = buf;
144*2fde3a48SSky Liu 		if (strncmp(p, "begin-base64", 12) == 0) {
145*2fde3a48SSky Liu 			base64 = 1;
146*2fde3a48SSky Liu 			p += 13;
147*2fde3a48SSky Liu 			break;
148*2fde3a48SSky Liu 		} else if (strncmp(p, "begin", 5) == 0)  {
149*2fde3a48SSky Liu 			p += 6;
150*2fde3a48SSky Liu 			break;
151*2fde3a48SSky Liu 		} else
152*2fde3a48SSky Liu 			continue;
153*2fde3a48SSky Liu 	}
154*2fde3a48SSky Liu 
155*2fde3a48SSky Liu         /* must be followed by an octal mode and a space */
156*2fde3a48SSky Liu 	mode = strtol(p, &fn, 8);
157*2fde3a48SSky Liu 	if (fn == (p) || !isspace((unsigned char)*fn) || mode==LONG_MIN || mode==LONG_MAX)
158*2fde3a48SSky Liu 	{
159*2fde3a48SSky Liu 	        warnx("%s: invalid mode on \"%s\" line", inputname,
160*2fde3a48SSky Liu 			base64 ? "begin-base64" : "begin");
161*2fde3a48SSky Liu 		return(1);
162*2fde3a48SSky Liu 	}
163*2fde3a48SSky Liu 	/* skip whitespace for file name */
164*2fde3a48SSky Liu 	while (*fn && isspace((unsigned char)*fn)) fn++;
165*2fde3a48SSky Liu 	if (*fn == 0) {
166*2fde3a48SSky Liu                 warnx("%s: no filename on \"%s\" line", inputname,
167*2fde3a48SSky Liu 			base64 ? "begin-base64" : "begin");
168*2fde3a48SSky Liu 		return(1);
169*2fde3a48SSky Liu 	}
170*2fde3a48SSky Liu 	/* zap newline */
171*2fde3a48SSky Liu 	for (p = fn; *p && *p != '\n'; p++)
172*2fde3a48SSky Liu 	        ;
173*2fde3a48SSky Liu 	if (*p) *p = 0;
174*2fde3a48SSky Liu 
175*2fde3a48SSky Liu 	/* outputname overrides fn */
176*2fde3a48SSky Liu 	if (outputname)
177*2fde3a48SSky Liu 		fn = outputname;
178*2fde3a48SSky Liu 
179*2fde3a48SSky Liu 	/* handle ~user/file format */
180*2fde3a48SSky Liu 	if (*fn == '~') {
181*2fde3a48SSky Liu 		if (!(p = strchr(fn, '/'))) {
182*2fde3a48SSky Liu 			warnx("%s: illegal ~user.", inputname);
183*2fde3a48SSky Liu 			return(1);
184*2fde3a48SSky Liu 		}
185*2fde3a48SSky Liu 		*p++ = '\0';
186*2fde3a48SSky Liu 		if (!(pw = getpwnam(fn + 1))) {
187*2fde3a48SSky Liu 			warnx("%s: no user %s.", inputname, buf);
188*2fde3a48SSky Liu 			return(1);
189*2fde3a48SSky Liu 		}
190*2fde3a48SSky Liu 		n = strlen(pw->pw_dir);
191*2fde3a48SSky Liu 		n1 = strlen(p);
192*2fde3a48SSky Liu 		if (n + n1 + 2 > MAXPATHLEN) {
193*2fde3a48SSky Liu 			warnx("%s: path too long.", inputname);
194*2fde3a48SSky Liu 			return(1);
195*2fde3a48SSky Liu 		}
196*2fde3a48SSky Liu 		/* make space at beginning of buf by moving end of pathname */
197*2fde3a48SSky Liu 		memmove(buf + n + 1, p, n1 + 1);
198*2fde3a48SSky Liu 		memmove(buf, pw->pw_dir, n);
199*2fde3a48SSky Liu 		buf[n] = '/';
200*2fde3a48SSky Liu 		fn = buf;
201*2fde3a48SSky Liu 	}
202*2fde3a48SSky Liu 
203*2fde3a48SSky Liu 	if (strcmp(fn, "/dev/stdout") == 0 || strcmp(fn, "-") == 0) {
204*2fde3a48SSky Liu 		/*
205*2fde3a48SSky Liu 		 * POSIX.1-2008 says that both "-" and "/dev/stdout"
206*2fde3a48SSky Liu 		 * refer to standard output when they appear in the file
207*2fde3a48SSky Liu 		 * header, but only "/dev/stdout" refers to standard
208*2fde3a48SSky Liu 		 * output when it appears as the argument to the "-o"
209*2fde3a48SSky Liu 		 * command line option.
210*2fde3a48SSky Liu 		 *
211*2fde3a48SSky Liu 		 * We handle both special names, regardless of whether
212*2fde3a48SSky Liu 		 * they came from the "-o" option or from the header of
213*2fde3a48SSky Liu 		 * the input stream.
214*2fde3a48SSky Liu 		 */
215*2fde3a48SSky Liu 	} else {
216*2fde3a48SSky Liu 		/*
217*2fde3a48SSky Liu 		 * Create output file, and set its mode.  POSIX.1-2008
218*2fde3a48SSky Liu 		 * requires the mode to be used exactly, ignoring the
219*2fde3a48SSky Liu 		 * umask and anything else, but we mask it with 0666.
220*2fde3a48SSky Liu 		 */
221*2fde3a48SSky Liu 		if (freopen(fn, "w", stdout) == NULL ||
222*2fde3a48SSky Liu 		    fchmod(fileno(stdout), mode & 0666) != 0) {
223*2fde3a48SSky Liu 			warn("%s: %s", fn, inputname);
224*2fde3a48SSky Liu 			return(1);
225*2fde3a48SSky Liu 		}
226*2fde3a48SSky Liu 	}
227*2fde3a48SSky Liu 
228*2fde3a48SSky Liu 	if (base64)
229*2fde3a48SSky Liu 		return base64_decode();
230*2fde3a48SSky Liu 	else {
231*2fde3a48SSky Liu 		/* for each input line */
232*2fde3a48SSky Liu 		for (;;) {
233*2fde3a48SSky Liu 			if (!fgets(p = buf, sizeof(buf), stdin)) {
234*2fde3a48SSky Liu 				warnx("%s: short file.", inputname);
235*2fde3a48SSky Liu 				return(1);
236*2fde3a48SSky Liu 			}
237*2fde3a48SSky Liu #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
238*2fde3a48SSky Liu 			/*
239*2fde3a48SSky Liu 		 	* `n' is used to avoid writing out all the characters
240*2fde3a48SSky Liu 		 	* at the end of the file.
241*2fde3a48SSky Liu 		 	*/
242*2fde3a48SSky Liu 			if ((n = DEC(*p)) <= 0)
243*2fde3a48SSky Liu 				break;
244*2fde3a48SSky Liu 			for (++p; n > 0; p += 4, n -= 3)
245*2fde3a48SSky Liu 				if (n >= 3) {
246*2fde3a48SSky Liu 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
247*2fde3a48SSky Liu 					putchar(ch);
248*2fde3a48SSky Liu 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
249*2fde3a48SSky Liu 					putchar(ch);
250*2fde3a48SSky Liu 					ch = DEC(p[2]) << 6 | DEC(p[3]);
251*2fde3a48SSky Liu 					putchar(ch);
252*2fde3a48SSky Liu 				}
253*2fde3a48SSky Liu 				else {
254*2fde3a48SSky Liu 					if (n >= 1) {
255*2fde3a48SSky Liu 						ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
256*2fde3a48SSky Liu 						putchar(ch);
257*2fde3a48SSky Liu 					}
258*2fde3a48SSky Liu 					if (n >= 2) {
259*2fde3a48SSky Liu 						ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
260*2fde3a48SSky Liu 						putchar(ch);
261*2fde3a48SSky Liu 					}
262*2fde3a48SSky Liu 					if (n >= 3) {
263*2fde3a48SSky Liu 						ch = DEC(p[2]) << 6 | DEC(p[3]);
264*2fde3a48SSky Liu 						putchar(ch);
265*2fde3a48SSky Liu 					}
266*2fde3a48SSky Liu 				}
267*2fde3a48SSky Liu 		}
268*2fde3a48SSky Liu 		if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
269*2fde3a48SSky Liu 			warnx("%s: no \"end\" line.", inputname);
270*2fde3a48SSky Liu 			return(1);
271*2fde3a48SSky Liu 		}
272*2fde3a48SSky Liu 		return(0);
273*2fde3a48SSky Liu 	}
274*2fde3a48SSky Liu }
275*2fde3a48SSky Liu 
276*2fde3a48SSky Liu static int
checkend(const char * ptr,const char * end,const char * msg)277*2fde3a48SSky Liu checkend(const char *ptr, const char *end, const char *msg)
278*2fde3a48SSky Liu {
279*2fde3a48SSky Liu 	size_t n;
280*2fde3a48SSky Liu 
281*2fde3a48SSky Liu 	n = strlen(end);
282*2fde3a48SSky Liu 	if (strncmp(ptr, end, n) != 0 ||
283*2fde3a48SSky Liu 	    strspn(ptr + n, "\t\r\n") != strlen(ptr + n)) {
284*2fde3a48SSky Liu 		warnx("%s", msg);
285*2fde3a48SSky Liu 		return (1);
286*2fde3a48SSky Liu 	}
287*2fde3a48SSky Liu 	return (0);
288*2fde3a48SSky Liu }
289*2fde3a48SSky Liu 
290*2fde3a48SSky Liu static int
base64_decode(void)291*2fde3a48SSky Liu base64_decode(void)
292*2fde3a48SSky Liu {
293*2fde3a48SSky Liu 	int n;
294*2fde3a48SSky Liu 	char inbuf[MAXPATHLEN];
295*2fde3a48SSky Liu 	unsigned char outbuf[MAXPATHLEN * 4];
296*2fde3a48SSky Liu 
297*2fde3a48SSky Liu 	for (;;) {
298*2fde3a48SSky Liu 		if (!fgets(inbuf, sizeof(inbuf), stdin)) {
299*2fde3a48SSky Liu 			warnx("%s: short file.", inputname);
300*2fde3a48SSky Liu 			return (1);
301*2fde3a48SSky Liu 		}
302*2fde3a48SSky Liu #ifdef NO_BASE64
303*2fde3a48SSky Liu 		warnx("%s: base64 decoding is not supported", inputname);
304*2fde3a48SSky Liu 		return (1);
305*2fde3a48SSky Liu #else
306*2fde3a48SSky Liu 		n = b64_pton(inbuf, outbuf, sizeof(outbuf));
307*2fde3a48SSky Liu #endif
308*2fde3a48SSky Liu 		if (n < 0)
309*2fde3a48SSky Liu 			break;
310*2fde3a48SSky Liu 		fwrite(outbuf, 1, n, stdout);
311*2fde3a48SSky Liu 	}
312*2fde3a48SSky Liu 	return (checkend(inbuf, "====",
313*2fde3a48SSky Liu 			"error decoding base64 input stream"));
314*2fde3a48SSky Liu }
315*2fde3a48SSky Liu 
316*2fde3a48SSky Liu static void
usage(void)317*2fde3a48SSky Liu usage(void)
318*2fde3a48SSky Liu {
319*2fde3a48SSky Liu 	(void)fprintf(stderr,
320*2fde3a48SSky Liu 		      "usage: %s [-m] [-p | -o outfile] [encoded-file ...]\n",
321*2fde3a48SSky Liu 		      getprogname());
322*2fde3a48SSky Liu 	exit(1);
323*2fde3a48SSky Liu }
324