uudecode.c revision 90720
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 $";
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/stat.h>
56
57#include <err.h>
58#include <fnmatch.h>
59#include <pwd.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));
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;
158	int ignore, mode, n1;
159	char buf[MAXPATHLEN];
160	char buffn[MAXPATHLEN]; /* file name buffer */
161
162	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		}
172	} while (strncmp(buf, "begin ", 6) ||
173		 fnmatch("begin [0-7]* *", buf, 0));
174
175	if (oflag) {
176		(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		}
181	} else
182		(void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf);
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 */
233	for (;;) {
234		if (!fgets(p = buf, sizeof(buf), stdin)) {
235			warnx("%s: short file", filename);
236			return(1);
237		}
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
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}
314