xref: /openbsd-src/usr.bin/uudecode/uudecode.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: uudecode.c,v 1.12 2003/07/10 00:06:51 david Exp $	*/
2 /*	$NetBSD: uudecode.c,v 1.6 1994/11/17 07:40:43 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
40 #endif
41 static char rcsid[] = "$OpenBSD: uudecode.c,v 1.12 2003/07/10 00:06:51 david Exp $";
42 #endif /* not lint */
43 
44 /*
45  * uudecode [-p] [file ...]
46  *
47  * create the specified file, decoding as you go.
48  * used with uuencode.
49  *
50  * Write to stdout if '-p' is specified.  Use this option if you care about
51  * security at all.
52  */
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <locale.h>
57 #include <errno.h>
58 #include <sys/param.h>
59 #include <sys/stat.h>
60 
61 #include <pwd.h>
62 #include <unistd.h>
63 
64 static int decode(int);
65 static void usage(void);
66 char *filename;
67 
68 int
69 main(int argc, char *argv[])
70 {
71 	int rval;
72 	int ch;
73 	int tostdout = 0;
74 
75 	setlocale(LC_ALL, "");
76 
77 	while ((ch = getopt(argc, argv, "p")) != -1)
78 		switch((char)ch) {
79 		case 'p':
80 			tostdout++;
81 			break;
82 		case '?':
83 		default:
84 			usage();
85 		}
86 	argc -= optind;
87 	argv += optind;
88 
89 	if (*argv) {
90 		rval = 0;
91 		do {
92 			if (!freopen(filename = *argv, "r", stdin)) {
93 				(void)fprintf(stderr, "uudecode: %s: %s\n",
94 				    *argv, strerror(errno));
95 				rval = 1;
96 				continue;
97 			}
98 			rval |= decode(tostdout);
99 		} while (*++argv);
100 	} else {
101 		filename = "stdin";
102 		rval = decode(tostdout);
103 	}
104 	exit(rval);
105 }
106 
107 static int
108 decode(int tostdout)
109 {
110 	struct passwd *pw;
111 	int n;
112 	char ch, *p;
113 	int mode, n1;
114 	char buf[MAXPATHLEN];
115 
116 	/* search for header line */
117 	do {
118 		if (!fgets(buf, sizeof(buf), stdin)) {
119 			(void)fprintf(stderr,
120 			    "uudecode: %s: no \"begin\" line\n", filename);
121 			return(1);
122 		}
123 	} while (strncmp(buf, "begin ", 6));
124 	(void)sscanf(buf, "begin %o %1023[^\n\r]", &mode, buf);
125 
126 	/* handle ~user/file format */
127 	if (buf[0] == '~') {
128 		if (!(p = strchr(buf, '/'))) {
129 			(void)fprintf(stderr, "uudecode: %s: illegal ~user.\n",
130 			    filename);
131 			return(1);
132 		}
133 		*p++ = NULL;
134 		if (!(pw = getpwnam(buf + 1))) {
135 			(void)fprintf(stderr, "uudecode: %s: no user %s.\n",
136 			    filename, buf);
137 			return(1);
138 		}
139 		n = strlen(pw->pw_dir);
140 		n1 = strlen(p);
141 		if (n + n1 + 2 > MAXPATHLEN) {
142 			(void)fprintf(stderr, "uudecode: %s: path too long.\n",
143 			    filename);
144 			return(1);
145 		}
146 		bcopy(p, buf + n + 1, n1 + 1);
147 		bcopy(pw->pw_dir, buf, n);
148 		buf[n] = '/';
149 	}
150 
151 	if (!tostdout) {
152 		/* create output file, set mode */
153 		if (!freopen(buf, "w", stdout) ||
154 		    fchmod(fileno(stdout), mode&0666)) {
155 			(void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf,
156 			    filename, strerror(errno));
157 			return(1);
158 		}
159 	}
160 
161 	/* for each input line */
162 	for (;;) {
163 		if (!fgets(p = buf, sizeof(buf), stdin)) {
164 			(void)fprintf(stderr, "uudecode: %s: short file.\n",
165 			    filename);
166 			return(1);
167 		}
168 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
169 		/*
170 		 * `n' is used to avoid writing out all the characters
171 		 * at the end of the file.
172 		 */
173 		if ((n = DEC(*p)) <= 0)
174 			break;
175 		for (++p; n > 0; p += 4, n -= 3)
176 			if (n >= 3) {
177 				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
178 				putchar(ch);
179 				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
180 				putchar(ch);
181 				ch = DEC(p[2]) << 6 | DEC(p[3]);
182 				putchar(ch);
183 			}
184 			else {
185 				if (n >= 1) {
186 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
187 					putchar(ch);
188 				}
189 				if (n >= 2) {
190 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
191 					putchar(ch);
192 				}
193 				if (n >= 3) {
194 					ch = DEC(p[2]) << 6 | DEC(p[3]);
195 					putchar(ch);
196 				}
197 			}
198 	}
199 	if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
200 		(void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n",
201 		    filename);
202 		return(1);
203 	}
204 	return(0);
205 }
206 
207 static void
208 usage(void)
209 {
210 	(void)fprintf(stderr, "usage: uudecode [-p] [file ...]\n");
211 	exit(1);
212 }
213