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