uudecode.c revision 89882
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 89882 2002-01-27 18:21:23Z 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			warnx("-p is deprecated, use `-o /dev/stdout' instead.");
101			pflag = 1; /* print output to stdout */
102			break;
103		case 's':
104			if (oflag)
105				usage();
106			sflag = 1; /* do not strip pathnames for output */
107			break;
108		default:
109			usage();
110		}
111	}
112        argc -= optind;
113        argv += optind;
114
115
116	if (*argv) {
117		rval = 0;
118		do {
119			if (!freopen(filename = *argv, "r", stdin)) {
120				warn("%s", *argv);
121				rval = 1;
122				continue;
123			}
124			rval |= decode();
125		} while (*++argv);
126	} else {
127		filename = "stdin";
128		rval = decode();
129	}
130	exit(rval);
131}
132
133int
134decode ()
135{
136	int flag;
137
138	/* decode only one file per input stream */
139	if (!cflag)
140		return(decode2(0));
141
142	/* multiple uudecode'd files */
143	for (flag = 0; ; flag++)
144		if (decode2(flag))
145			return(1);
146		else if (feof(stdin))
147			break;
148
149	return(0);
150}
151
152int
153decode2(flag)
154	int flag;
155{
156	struct passwd *pw;
157	register int n;
158	register char ch, *p;
159	int ignore, mode, n1;
160	char buf[MAXPATHLEN];
161	char buffn[MAXPATHLEN]; /* file name buffer */
162
163	ignore = 0;
164	/* search for header line */
165	do {
166		if (!fgets(buf, sizeof(buf), stdin)) {
167			if (flag) /* no error */
168				return(0);
169
170			warnx("%s: no \"begin\" line", filename);
171			return(1);
172		}
173	} while (strncmp(buf, "begin ", 6) ||
174		 fnmatch("begin [0-7]* *", buf, 0));
175
176	if (oflag) {
177		(void)sscanf(buf, "begin %o ", &mode);
178		if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf)) {
179			warnx("%s: filename too long", outfile);
180			return (1);
181		}
182	} else
183		(void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf);
184
185	if (!sflag && !pflag) {
186		strncpy(buffn, buf, sizeof(buffn));
187		if (strrchr(buffn, '/') != NULL)
188			strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf));
189		if (buf[0] == '\0') {
190			warnx("%s: illegal filename", buffn);
191			return(1);
192		}
193	}
194
195	/* handle ~user/file format */
196	if (buf[0] == '~') {
197		if (!(p = index(buf, '/'))) {
198			warnx("%s: illegal ~user", filename);
199			return(1);
200		}
201		*p++ = '\0';
202		if (!(pw = getpwnam(buf + 1))) {
203			warnx("%s: no user %s", filename, buf);
204			return(1);
205		}
206		n = strlen(pw->pw_dir);
207		n1 = strlen(p);
208		if (n + n1 + 2 > MAXPATHLEN) {
209			warnx("%s: path too long", filename);
210			return(1);
211		}
212		bcopy(p, buf + n + 1, n1 + 1);
213		bcopy(pw->pw_dir, buf, n);
214		buf[n] = '/';
215	}
216
217	/* create output file, set mode */
218	if (pflag)
219		; /* print to stdout */
220
221	else {
222		if (iflag && !access(buf, F_OK)) {
223			(void)fprintf(stderr, "not overwritten: %s\n", buf);
224			ignore++;
225		} else if (!freopen(buf, "w", stdout) ||
226		    fchmod(fileno(stdout), mode&0666)) {
227			warn("%s: %s", buf, filename);
228			return(1);
229		}
230	}
231	strcpy(buffn, buf); /* store file name from header line */
232
233	/* for each input line */
234	for (;;) {
235		if (!fgets(p = buf, sizeof(buf), stdin)) {
236			warnx("%s: short file", filename);
237			return(1);
238		}
239#define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
240#define IS_DEC(c) ( (((c) - ' ') >= 0) &&  (((c) - ' ') <= 077 + 1) )
241/* #define IS_DEC(c) (1) */
242
243#define OUT_OF_RANGE \
244{	\
245    warnx( \
246"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \
247 	filename, buffn, 1 + ' ', 077 + ' ' + 1); \
248        return(1); \
249}
250#define PUTCHAR(c) \
251if (!ignore) \
252	putchar(c)
253
254
255		/*
256		 * `n' is used to avoid writing out all the characters
257		 * at the end of the file.
258		 */
259		if ((n = DEC(*p)) <= 0)
260			break;
261		for (++p; n > 0; p += 4, n -= 3)
262			if (n >= 3) {
263				if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
264				     IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
265                                	OUT_OF_RANGE
266
267				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
268				PUTCHAR(ch);
269				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
270				PUTCHAR(ch);
271				ch = DEC(p[2]) << 6 | DEC(p[3]);
272				PUTCHAR(ch);
273
274			}
275			else {
276				if (n >= 1) {
277					if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
278	                                	OUT_OF_RANGE
279					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
280					PUTCHAR(ch);
281				}
282				if (n >= 2) {
283					if (!(IS_DEC(*(p + 1)) &&
284						IS_DEC(*(p + 2))))
285		                                OUT_OF_RANGE
286
287					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
288					PUTCHAR(ch);
289				}
290				if (n >= 3) {
291					if (!(IS_DEC(*(p + 2)) &&
292						IS_DEC(*(p + 3))))
293		                                OUT_OF_RANGE
294					ch = DEC(p[2]) << 6 | DEC(p[3]);
295					PUTCHAR(ch);
296				}
297			}
298	}
299	if (fgets(buf, sizeof(buf), stdin) == NULL ||
300	    (strcmp(buf, "end") && strcmp(buf, "end\n") &&
301	     strcmp(buf, "end\r\n"))) {
302		warnx("%s: no \"end\" line", filename);
303		return(1);
304	}
305	return(0);
306}
307
308static void
309usage()
310{
311	(void)fprintf(stderr, "usage: uudecode [-cips] [file ...]\n");
312	(void)fprintf(stderr, "usage: uudecode [-i] -o output_file [file]\n");
313	exit(1);
314}
315