Home | History | Annotate | Line # | Download | only in uudecode
uudecode.c revision 1.21
      1 /*	$NetBSD: uudecode.c,v 1.21 2006/09/24 15:32:48 elad 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\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #if 0
     41 static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
     42 #endif
     43 __RCSID("$NetBSD: uudecode.c,v 1.21 2006/09/24 15:32:48 elad 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 <netinet/in.h>
     55 #include <ctype.h>
     56 #include <err.h>
     57 #include <errno.h>
     58 #include <limits.h>
     59 #include <locale.h>
     60 #include <resolv.h>
     61 #include <pwd.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 static int decode(void);
     68 static void usage(void);
     69 static int checkend(const char *, const char *, const char *);
     70 static int base64_decode(void);
     71 int main(int, char *[]);
     72 
     73 int base64, pflag;
     74 char *filename;
     75 
     76 int
     77 main(int argc, char *argv[])
     78 {
     79 	int ch, rval;
     80 
     81 	setlocale(LC_ALL, "");
     82 	setprogname(argv[0]);
     83 
     84 	pflag = 0;
     85 	while ((ch = getopt(argc, argv, "mp")) != -1)
     86 		switch (ch) {
     87 		case 'm':
     88 			base64 = 1;
     89 			break;
     90 		case 'p':
     91 			pflag = 1;
     92 			break;
     93 		default:
     94 			usage();
     95 		}
     96 	argc -= optind;
     97 	argv += optind;
     98 
     99 	if (*argv) {
    100 		rval = 0;
    101 		do {
    102 			if (!freopen(filename = *argv, "r", stdin)) {
    103 				warn("%s", *argv);
    104 				rval = 1;
    105 				continue;
    106 			}
    107 			rval |= decode();
    108 		} while (*++argv);
    109 	} else {
    110 		filename = "stdin";
    111 		rval = decode();
    112 	}
    113 	exit(rval);
    114 }
    115 
    116 static int
    117 decode(void)
    118 {
    119 	struct passwd *pw;
    120 	int n;
    121 	char ch, *p;
    122 	int n1;
    123 	long mode;
    124 	char *fn;
    125 	char buf[MAXPATHLEN];
    126 
    127 	/* search for header line */
    128 	for (;;) {
    129 		if (!fgets(buf, sizeof(buf), stdin)) {
    130 			warnx("%s: no \"%s\" line", filename, base64 ?
    131 					"begin-base64" : "begin");
    132 			return(1);
    133 		}
    134 		p = buf;
    135 		if (strncmp(p, "begin-base64", 12) == 0) {
    136 			base64 = 1;
    137 			p += 13;
    138 			break;
    139 		} else if (strncmp(p, "begin", 5) == 0)  {
    140 			p += 6;
    141 			break;
    142 		} else
    143 			continue;
    144 	}
    145 
    146         /* must be followed by an octal mode and a space */
    147 	mode = strtol(p, &fn, 8);
    148 	if (fn == (p) || !isspace((unsigned char)*fn) || mode==LONG_MIN || mode==LONG_MAX)
    149 	{
    150 	        warnx("%s: invalid mode on \"%s\" line", filename,
    151 			base64 ? "begin-base64" : "begin");
    152 		return(1);
    153 	}
    154 	/* skip whitespace for file name */
    155 	while (*fn && isspace((unsigned char)*fn)) fn++;
    156 	if (*fn == 0) {
    157                 warnx("%s: no filename on \"%s\" line", filename,
    158 			base64 ? "begin-base64" : "begin");
    159 		return(1);
    160 	}
    161 	/* zap newline */
    162 	for (p = fn; *p && *p != '\n'; p++)
    163 	        ;
    164 	if (*p) *p = 0;
    165 
    166 	/* handle ~user/file format */
    167 	if (*fn == '~') {
    168 		if (!(p = strchr(fn, '/'))) {
    169 			warnx("%s: illegal ~user.", filename);
    170 			return(1);
    171 		}
    172 		*p++ = '\0';
    173 		if (!(pw = getpwnam(fn + 1))) {
    174 			warnx("%s: no user %s.", filename, buf);
    175 			return(1);
    176 		}
    177 		n = strlen(pw->pw_dir);
    178 		n1 = strlen(p);
    179 		if (n + n1 + 2 > MAXPATHLEN) {
    180 			warnx("%s: path too long.", filename);
    181 			return(1);
    182 		}
    183 		/* make space at beginning of buf by moving end of pathname */
    184 		memmove(buf + n + 1, p, n1 + 1);
    185 		memmove(buf, pw->pw_dir, n);
    186 		buf[n] = '/';
    187 		fn = buf;
    188 	}
    189 
    190 	/* create output file, set mode */
    191 	if (!pflag && (!freopen(fn, "w", stdout) ||
    192 	    fchmod(fileno(stdout), mode & 0666))) {
    193 		warn("%s: %s", fn, filename);
    194 		return(1);
    195 	}
    196 
    197 	if (base64)
    198 		return base64_decode();
    199 	else {
    200 		/* for each input line */
    201 		for (;;) {
    202 			if (!fgets(p = buf, sizeof(buf), stdin)) {
    203 				warnx("%s: short file.", filename);
    204 				return(1);
    205 			}
    206 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
    207 			/*
    208 		 	* `n' is used to avoid writing out all the characters
    209 		 	* at the end of the file.
    210 		 	*/
    211 			if ((n = DEC(*p)) <= 0)
    212 				break;
    213 			for (++p; n > 0; p += 4, n -= 3)
    214 				if (n >= 3) {
    215 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    216 					putchar(ch);
    217 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    218 					putchar(ch);
    219 					ch = DEC(p[2]) << 6 | DEC(p[3]);
    220 					putchar(ch);
    221 				}
    222 				else {
    223 					if (n >= 1) {
    224 						ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
    225 						putchar(ch);
    226 					}
    227 					if (n >= 2) {
    228 						ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
    229 						putchar(ch);
    230 					}
    231 					if (n >= 3) {
    232 						ch = DEC(p[2]) << 6 | DEC(p[3]);
    233 						putchar(ch);
    234 					}
    235 				}
    236 		}
    237 		if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
    238 			warnx("%s: no \"end\" line.", filename);
    239 			return(1);
    240 		}
    241 		return(0);
    242 	}
    243 }
    244 
    245 static int
    246 checkend(const char *ptr, const char *end, const char *msg)
    247 {
    248 	size_t n;
    249 
    250 	n = strlen(end);
    251 	if (strncmp(ptr, end, n) != 0 ||
    252 	    strspn(ptr + n, "\t\r\n") != strlen(ptr + n)) {
    253 		warnx("%s", msg);
    254 		return (1);
    255 	}
    256 	return (0);
    257 }
    258 
    259 static int
    260 base64_decode(void)
    261 {
    262 	int n;
    263 	char inbuf[MAXPATHLEN];
    264 	unsigned char outbuf[MAXPATHLEN * 4];
    265 
    266 	for (;;) {
    267 		if (!fgets(inbuf, sizeof(inbuf), stdin)) {
    268 			warnx("%s: short file.", filename);
    269 			return (1);
    270 		}
    271 		n = b64_pton(inbuf, outbuf, sizeof(outbuf));
    272 		if (n < 0)
    273 			break;
    274 		fwrite(outbuf, 1, n, stdout);
    275 	}
    276 	return (checkend(inbuf, "====",
    277 			"error decoding base64 input stream"));
    278 }
    279 
    280 static void
    281 usage()
    282 {
    283 	(void)fprintf(stderr, "usage: %s [-m | -p] [file ...]\n",
    284 		      getprogname());
    285 	exit(1);
    286 }
    287