Home | History | Annotate | Line # | Download | only in uudecode
uudecode.c revision 1.27
      1 /*	$NetBSD: uudecode.c,v 1.27 2013/01/28 16:06:42 apb 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.27 2013/01/28 16:06:42 apb 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(char *);
     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;
     76 static const char *inputname;
     77 
     78 int
     79 main(int argc, char *argv[])
     80 {
     81 	int ch, rval;
     82 	char *outputname = NULL;
     83 
     84 	setlocale(LC_ALL, "");
     85 	setprogname(argv[0]);
     86 
     87 	while ((ch = getopt(argc, argv, "mo:p")) != -1)
     88 		switch (ch) {
     89 		case 'm':
     90 			base64 = 1;
     91 			break;
     92 		case 'o':
     93 			outputname = optarg;
     94 			break;
     95 		case 'p':
     96 			outputname = __UNCONST("/dev/stdout");
     97 			break;
     98 		default:
     99 			usage();
    100 		}
    101 	argc -= optind;
    102 	argv += optind;
    103 
    104 	if (*argv) {
    105 		rval = 0;
    106 		do {
    107 			if (!freopen(inputname = *argv, "r", stdin)) {
    108 				warn("%s", *argv);
    109 				rval = 1;
    110 				continue;
    111 			}
    112 			rval |= decode(outputname);
    113 		} while (*++argv);
    114 	} else {
    115 		inputname = "stdin";
    116 		rval = decode(outputname);
    117 	}
    118 	exit(rval);
    119 }
    120 
    121 /*
    122  * Decode one file from stdin.  If outputname is not NULL
    123  * then it overrides the file name embedded in the input data.
    124  */
    125 static int
    126 decode(char *outputname)
    127 {
    128 	struct passwd *pw;
    129 	int n;
    130 	char ch, *p;
    131 	int n1;
    132 	long mode;
    133 	char *fn;
    134 	char buf[MAXPATHLEN];
    135 
    136 	/* search for header line */
    137 	for (;;) {
    138 		if (!fgets(buf, sizeof(buf), stdin)) {
    139 			warnx("%s: no \"%s\" line", inputname, base64 ?
    140 					"begin-base64" : "begin");
    141 			return(1);
    142 		}
    143 		p = buf;
    144 		if (strncmp(p, "begin-base64", 12) == 0) {
    145 			base64 = 1;
    146 			p += 13;
    147 			break;
    148 		} else if (strncmp(p, "begin", 5) == 0)  {
    149 			p += 6;
    150 			break;
    151 		} else
    152 			continue;
    153 	}
    154 
    155         /* must be followed by an octal mode and a space */
    156 	mode = strtol(p, &fn, 8);
    157 	if (fn == (p) || !isspace((unsigned char)*fn) || mode==LONG_MIN || mode==LONG_MAX)
    158 	{
    159 	        warnx("%s: invalid mode on \"%s\" line", inputname,
    160 			base64 ? "begin-base64" : "begin");
    161 		return(1);
    162 	}
    163 	/* skip whitespace for file name */
    164 	while (*fn && isspace((unsigned char)*fn)) fn++;
    165 	if (*fn == 0) {
    166                 warnx("%s: no filename on \"%s\" line", inputname,
    167 			base64 ? "begin-base64" : "begin");
    168 		return(1);
    169 	}
    170 	/* zap newline */
    171 	for (p = fn; *p && *p != '\n'; p++)
    172 	        ;
    173 	if (*p) *p = 0;
    174 
    175 	/* outputname overrides fn */
    176 	if (outputname)
    177 		fn = outputname;
    178 
    179 	/* handle ~user/file format */
    180 	if (*fn == '~') {
    181 		if (!(p = strchr(fn, '/'))) {
    182 			warnx("%s: illegal ~user.", inputname);
    183 			return(1);
    184 		}
    185 		*p++ = '\0';
    186 		if (!(pw = getpwnam(fn + 1))) {
    187 			warnx("%s: no user %s.", inputname, buf);
    188 			return(1);
    189 		}
    190 		n = strlen(pw->pw_dir);
    191 		n1 = strlen(p);
    192 		if (n + n1 + 2 > MAXPATHLEN) {
    193 			warnx("%s: path too long.", inputname);
    194 			return(1);
    195 		}
    196 		/* make space at beginning of buf by moving end of pathname */
    197 		memmove(buf + n + 1, p, n1 + 1);
    198 		memmove(buf, pw->pw_dir, n);
    199 		buf[n] = '/';
    200 		fn = buf;
    201 	}
    202 
    203 	if (strcmp(fn, "/dev/stdout") == 0) {
    204 		/* use stdout */
    205 	} else {
    206 		/* create output file, set mode */
    207 		if (freopen(fn, "w", stdout) == NULL ||
    208 		    fchmod(fileno(stdout), mode & 0666) != 0) {
    209 			warn("%s: %s", fn, inputname);
    210 			return(1);
    211 		}
    212 	}
    213 
    214 	if (base64)
    215 		return base64_decode();
    216 	else {
    217 		/* for each input line */
    218 		for (;;) {
    219 			if (!fgets(p = buf, sizeof(buf), stdin)) {
    220 				warnx("%s: short file.", inputname);
    221 				return(1);
    222 			}
    223 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
    224 			/*
    225 		 	* `n' is used to avoid writing out all the characters
    226 		 	* at the end of the file.
    227 		 	*/
    228 			if ((n = DEC(*p)) <= 0)
    229 				break;
    230 			for (++p; n > 0; p += 4, n -= 3)
    231 				if (n >= 3) {
    232 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    233 					putchar(ch);
    234 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    235 					putchar(ch);
    236 					ch = DEC(p[2]) << 6 | DEC(p[3]);
    237 					putchar(ch);
    238 				}
    239 				else {
    240 					if (n >= 1) {
    241 						ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    242 						putchar(ch);
    243 					}
    244 					if (n >= 2) {
    245 						ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    246 						putchar(ch);
    247 					}
    248 					if (n >= 3) {
    249 						ch = DEC(p[2]) << 6 | DEC(p[3]);
    250 						putchar(ch);
    251 					}
    252 				}
    253 		}
    254 		if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
    255 			warnx("%s: no \"end\" line.", inputname);
    256 			return(1);
    257 		}
    258 		return(0);
    259 	}
    260 }
    261 
    262 static int
    263 checkend(const char *ptr, const char *end, const char *msg)
    264 {
    265 	size_t n;
    266 
    267 	n = strlen(end);
    268 	if (strncmp(ptr, end, n) != 0 ||
    269 	    strspn(ptr + n, "\t\r\n") != strlen(ptr + n)) {
    270 		warnx("%s", msg);
    271 		return (1);
    272 	}
    273 	return (0);
    274 }
    275 
    276 static int
    277 base64_decode(void)
    278 {
    279 	int n;
    280 	char inbuf[MAXPATHLEN];
    281 	unsigned char outbuf[MAXPATHLEN * 4];
    282 
    283 	for (;;) {
    284 		if (!fgets(inbuf, sizeof(inbuf), stdin)) {
    285 			warnx("%s: short file.", inputname);
    286 			return (1);
    287 		}
    288 #ifdef NO_BASE64
    289 		warnx("%s: base64 decoding is not supported", inputname);
    290 		return (1);
    291 #else
    292 		n = b64_pton(inbuf, outbuf, sizeof(outbuf));
    293 #endif
    294 		if (n < 0)
    295 			break;
    296 		fwrite(outbuf, 1, n, stdout);
    297 	}
    298 	return (checkend(inbuf, "====",
    299 			"error decoding base64 input stream"));
    300 }
    301 
    302 static void
    303 usage(void)
    304 {
    305 	(void)fprintf(stderr,
    306 		      "usage: %s [-m] [-p | -o outfile] [encoded-file ...]\n",
    307 		      getprogname());
    308 	exit(1);
    309 }
    310