uudecode.c revision 32780
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 "$Id: uudecode.c,v 1.10 1997/09/18 14:07:26 phk Exp $"; 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 65char *filename; 66int cflag, iflag, pflag, sflag; 67 68static void usage __P((void)); 69int decode __P((void)); 70int decode2 __P((int)); 71 72int 73main(argc, argv) 74 int argc; 75 char *argv[]; 76{ 77 int rval, ch; 78 79 while ((ch = getopt(argc, argv, "cips")) != -1) { 80 switch(ch) { 81 case 'c': 82 cflag = 1; /* multiple uudecode'd files */ 83 break; 84 case 'i': 85 iflag = 1; /* ask before override files */ 86 break; 87 case 'p': 88 pflag = 1; /* print output to stdout */ 89 break; 90 case 's': 91 sflag = 1; /* do not strip pathnames for output */ 92 break; 93 default: 94 usage(); 95 } 96 } 97 argc -= optind; 98 argv += optind; 99 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 118int 119decode () 120{ 121 int flag; 122 123 /* decode only one file per input stream */ 124 if (!cflag) 125 return(decode2(0)); 126 127 /* multiple uudecode'd files */ 128 for (flag = 0; ; flag++) 129 if (decode2(flag)) 130 return(1); 131 else if (feof(stdin)) 132 break; 133 134 return(0); 135} 136 137int 138decode2(flag) 139 int flag; 140{ 141 struct passwd *pw; 142 register int n; 143 register char ch, first, *p; 144 int mode, n1; 145 char buf[MAXPATHLEN]; 146 char buffn[MAXPATHLEN]; /* file name buffer */ 147 148 149 /* search for header line */ 150 do { 151 if (!fgets(buf, sizeof(buf), stdin)) { 152 if (flag) /* no error */ 153 return(0); 154 155 warnx("%s: no \"begin\" line", filename); 156 return(1); 157 } 158 } while (strncmp(buf, "begin ", 6) || 159 fnmatch("begin [0-7]* *", buf, 0)); 160 161 (void)sscanf(buf, "begin %o %s", &mode, buf); 162 163 if (!sflag && !pflag) { 164 strncpy(buffn, buf, sizeof(buffn)); 165 if (strrchr(buffn, '/') != NULL) 166 strncpy(buf, strrchr(buffn, '/') + 1, sizeof(buf)); 167 if (buf[0] == '\0') { 168 warnx("%s: illegal filename", buffn); 169 return(1); 170 } 171 } 172 173 /* handle ~user/file format */ 174 if (buf[0] == '~') { 175 if (!(p = index(buf, '/'))) { 176 warnx("%s: illegal ~user", filename); 177 return(1); 178 } 179 *p++ = '\0'; 180 if (!(pw = getpwnam(buf + 1))) { 181 warnx("%s: no user %s", filename, buf); 182 return(1); 183 } 184 n = strlen(pw->pw_dir); 185 n1 = strlen(p); 186 if (n + n1 + 2 > MAXPATHLEN) { 187 warnx("%s: path too long", filename); 188 return(1); 189 } 190 bcopy(p, buf + n + 1, n1 + 1); 191 bcopy(pw->pw_dir, buf, n); 192 buf[n] = '/'; 193 } 194 195 /* create output file, set mode */ 196 if (pflag) 197 ; /* print to stdout */ 198 199 else { 200 if (iflag && !access(buf, F_OK)) 201 (void)fprintf(stderr, "not overwritten: %s\n", buf); 202 if (!freopen(buf, "w", stdout) || 203 fchmod(fileno(stdout), mode&0666)) { 204 warn("%s: %s", buf, filename); 205 return(1); 206 } 207 } 208 strcpy(buffn, buf); /* store file name from header line */ 209 210 /* for each input line */ 211 for (;;) { 212 if (!fgets(p = buf, sizeof(buf), stdin)) { 213 warnx("%s: short file", filename); 214 return(1); 215 } 216#define DEC(c) (((c) - ' ') & 077) /* single character decode */ 217#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) 218/* #define IS_DEC(c) (1) */ 219 220#define OUT_OF_RANGE \ 221{ \ 222 warnx( \ 223"\n\tinput file: %s\n\tencoded file: %s\n\tcharacter out of range: [%d-%d]", \ 224 filename, buffn, 1 + ' ', 077 + ' ' + 1); \ 225 return(1); \ 226} 227 228 229 /* 230 * `n' is used to avoid writing out all the characters 231 * at the end of the file. 232 */ 233 if ((n = DEC(*p)) <= 0) 234 break; 235 for (++p; n > 0; p += 4, n -= 3) 236 if (n >= 3) { 237 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) && 238 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3)))) 239 OUT_OF_RANGE 240 241 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 242 putchar(ch); 243 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 244 putchar(ch); 245 ch = DEC(p[2]) << 6 | DEC(p[3]); 246 putchar(ch); 247 248 } 249 else { 250 if (n >= 1) { 251 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)))) 252 OUT_OF_RANGE 253 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 254 putchar(ch); 255 } 256 if (n >= 2) { 257 if (!(IS_DEC(*(p + 1)) && 258 IS_DEC(*(p + 2)))) 259 OUT_OF_RANGE 260 261 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 262 putchar(ch); 263 } 264 if (n >= 3) { 265 if (!(IS_DEC(*(p + 2)) && 266 IS_DEC(*(p + 3)))) 267 OUT_OF_RANGE 268 ch = DEC(p[2]) << 6 | DEC(p[3]); 269 putchar(ch); 270 } 271 } 272 } 273 if (fgets(buf, sizeof(buf), stdin) == NULL || 274 (strcmp(buf, "end") && strcmp(buf, "end\n") && 275 strcmp(buf, "end\r\n"))) { 276 warnx("%s: no \"end\" line", filename); 277 return(1); 278 } 279 return(0); 280} 281 282static void 283usage() 284{ 285 (void)fprintf(stderr, "usage: uudecode [-cips] [file ...]\n"); 286 exit(1); 287} 288