uudecode.c revision 24263
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 35char 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 41static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94"; 42#endif /* not lint */ 43 44/* 45 * uudecode [file ...] 46 * 47 * create the specified file, decoding as you go. 48 * used with uuencode. 49 */ 50#include <sys/param.h> 51#include <sys/stat.h> 52 53#include <fnmatch.h> 54#include <pwd.h> 55#include <stdio.h> 56#include <string.h> 57#include <stdlib.h> 58 59char *filename; 60int cflag, pflag; 61 62void usage __P((void)); 63int decode __P((void)); 64int decode2 __P((int)); 65 66extern int optind; 67 68int 69main(argc, argv) 70 int argc; 71 char *argv[]; 72{ 73 extern int errno; 74 int rval, ch; 75 76 while ((ch = getopt(argc, argv, "cp")) != EOF) { 77 switch(ch) { 78 case 'c': 79 cflag = 1; /* multiple uudecode'd files */ 80 break; 81 case 'p': 82 pflag = 1; /* print output to stdout */ 83 break; 84 default: 85 (void)usage(); 86 } 87 } 88 argc -= optind; 89 argv += optind; 90 91 92 if (*argv) { 93 rval = 0; 94 do { 95 if (!freopen(filename = *argv, "r", stdin)) { 96 (void)fprintf(stderr, "uudecode: %s: %s\n", 97 *argv, strerror(errno)); 98 rval = 1; 99 continue; 100 } 101 rval |= decode(); 102 } while (*++argv); 103 } else { 104 filename = "stdin"; 105 rval = decode(); 106 } 107 exit(rval); 108} 109 110int 111decode () 112{ 113 int flag; 114 115 /* decode only one file per input stream */ 116 if (!cflag) 117 return(decode2(0)); 118 119 /* multiple uudecode'd files */ 120 for (flag = 0; ; flag++) 121 if (decode2(flag)) 122 return(1); 123 else if (feof(stdin)) 124 break; 125 126 return(0); 127} 128 129int 130decode2(flag) 131 int flag; 132{ 133 extern int errno; 134 struct passwd *pw; 135 register int n; 136 register char ch, *p; 137 int mode, n1; 138 char buf[MAXPATHLEN]; 139 char buffn[MAXPATHLEN]; /* file name buffer */ 140 141 142 /* search for header line */ 143 do { 144 if (!fgets(buf, sizeof(buf), stdin)) { 145 if (flag) /* no error */ 146 return(0); 147 148 (void)fprintf(stderr, 149 "uudecode: %s: no \"begin\" line\n", filename); 150 return(1); 151 } 152 } while (strncmp(buf, "begin ", 6) || 153 fnmatch("begin [0-7]* *", buf, 0)); 154 155 (void)sscanf(buf, "begin %o %s", &mode, buf); 156 157 /* handle ~user/file format */ 158 if (buf[0] == '~') { 159 if (!(p = index(buf, '/'))) { 160 (void)fprintf(stderr, "uudecode: %s: illegal ~user.\n", 161 filename); 162 return(1); 163 } 164 *p++ = NULL; 165 if (!(pw = getpwnam(buf + 1))) { 166 (void)fprintf(stderr, "uudecode: %s: no user %s.\n", 167 filename, buf); 168 return(1); 169 } 170 n = strlen(pw->pw_dir); 171 n1 = strlen(p); 172 if (n + n1 + 2 > MAXPATHLEN) { 173 (void)fprintf(stderr, "uudecode: %s: path too long.\n", 174 filename); 175 return(1); 176 } 177 bcopy(p, buf + n + 1, n1 + 1); 178 bcopy(pw->pw_dir, buf, n); 179 buf[n] = '/'; 180 } 181 182 /* create output file, set mode */ 183 if (pflag) 184 ; /* print to stdout */ 185 186 else if (!freopen(buf, "w", stdout) || 187 fchmod(fileno(stdout), mode&0666)) { 188 (void)fprintf(stderr, "uudecode: %s: %s: %s\n", buf, 189 filename, strerror(errno)); 190 return(1); 191 } 192 strcpy(buffn, buf); /* store file name from header line */ 193 194 /* for each input line */ 195 for (;;) { 196 if (!fgets(p = buf, sizeof(buf), stdin)) { 197 (void)fprintf(stderr, "uudecode: %s: short file.\n", 198 filename); 199 return(1); 200 } 201#define DEC(c) (((c) - ' ') & 077) /* single character decode */ 202#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) 203/* #define IS_DEC(c) (1) */ 204 205#define OUT_OF_RANGE \ 206{ \ 207 (void)fprintf(stderr, \ 208 "uudecode:\n\tinput file: %s\n\tencoded file: %s\n\t%s: [%d-%d]\n", \ 209 filename, buffn, "character out of range", 1 + ' ', 077 + ' ' + 1); \ 210 return(1); \ 211} 212 213 214 /* 215 * `n' is used to avoid writing out all the characters 216 * at the end of the file. 217 */ 218 if ((n = DEC(*p)) <= 0) 219 break; 220 for (++p; n > 0; p += 4, n -= 3) 221 if (n >= 3) { 222 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) && 223 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3)))) 224 OUT_OF_RANGE 225 226 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 227 putchar(ch); 228 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 229 putchar(ch); 230 ch = DEC(p[2]) << 6 | DEC(p[3]); 231 putchar(ch); 232 233 } 234 else { 235 if (n >= 1) { 236 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)))) 237 OUT_OF_RANGE 238 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 239 putchar(ch); 240 } 241 if (n >= 2) { 242 if (!(IS_DEC(*(p + 1)) && 243 IS_DEC(*(p + 2)))) 244 OUT_OF_RANGE 245 246 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 247 putchar(ch); 248 } 249 if (n >= 3) { 250 if (!(IS_DEC(*(p + 2)) && 251 IS_DEC(*(p + 3)))) 252 OUT_OF_RANGE 253 ch = DEC(p[2]) << 6 | DEC(p[3]); 254 putchar(ch); 255 } 256 } 257 } 258 if (fgets(buf, sizeof(buf), stdin) == NULL || 259 (strcmp(buf, "end") && strcmp(buf, "end\n") && 260 strcmp(buf, "end\r\n"))) { 261 (void)fprintf(stderr, "uudecode: %s: no \"end\" line.\n", 262 filename); 263 return(1); 264 } 265 return(0); 266} 267 268void 269usage() 270{ 271 (void)fprintf(stderr, "usage: uudecode [-cp] [file ...]\n"); 272 exit(1); 273} 274