1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. */ 26*7c478bd9Sstevel@tonic-gate /* Use is subject to license terms. */ 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * uudecode [-o outfile | -p] [input] 32*7c478bd9Sstevel@tonic-gate * 33*7c478bd9Sstevel@tonic-gate * create the specified file, decoding as you go. 34*7c478bd9Sstevel@tonic-gate * used with uuencode. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate #include <unistd.h> 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <pwd.h> 40*7c478bd9Sstevel@tonic-gate #include <string.h> 41*7c478bd9Sstevel@tonic-gate #include <strings.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 44*7c478bd9Sstevel@tonic-gate #include <locale.h> 45*7c478bd9Sstevel@tonic-gate #include <nl_types.h> 46*7c478bd9Sstevel@tonic-gate #include <langinfo.h> 47*7c478bd9Sstevel@tonic-gate #include <iconv.h> 48*7c478bd9Sstevel@tonic-gate #include <limits.h> 49*7c478bd9Sstevel@tonic-gate #include <errno.h> 50*7c478bd9Sstevel@tonic-gate #include <ctype.h> 51*7c478bd9Sstevel@tonic-gate #include <signal.h> 52*7c478bd9Sstevel@tonic-gate #include <stdarg.h> 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate #define BUFSIZE 90 /* must be a multiple of 3 */ 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate #define TABLE_SIZE 0x40 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate #define isvalid(octet) (octet <= 0x40) 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate /* 61*7c478bd9Sstevel@tonic-gate * base64 decoding table 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */ 64*7c478bd9Sstevel@tonic-gate static char base64tab[] = { 65*7c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377', 66*7c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377', 67*7c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377', 68*7c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377', 69*7c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377', 70*7c478bd9Sstevel@tonic-gate '\377', '\377', '\377', 62, '\377', '\377', '\377', 63, 71*7c478bd9Sstevel@tonic-gate 52, 53, 54, 55, 56, 57, 58, 59, 72*7c478bd9Sstevel@tonic-gate 60, 61, '\377', '\377', '\377', '\377', '\377', '\377', 73*7c478bd9Sstevel@tonic-gate '\377', 0, 1, 2, 3, 4, 5, 6, 74*7c478bd9Sstevel@tonic-gate 7, 8, 9, 10, 11, 12, 13, 14, 75*7c478bd9Sstevel@tonic-gate 15, 16, 17, 18, 19, 20, 21, 22, 76*7c478bd9Sstevel@tonic-gate 23, 24, 25, '\377', '\377', '\377', '\377', '\377', 77*7c478bd9Sstevel@tonic-gate '\377', 26, 27, 28, 29, 30, 31, 32, 78*7c478bd9Sstevel@tonic-gate 33, 34, 35, 36, 37, 38, 39, 40, 79*7c478bd9Sstevel@tonic-gate 41, 42, 43, 44, 45, 46, 47, 48, 80*7c478bd9Sstevel@tonic-gate 49, 50, 51, '\377', '\377', '\377', '\377', '\377' 81*7c478bd9Sstevel@tonic-gate }; 82*7c478bd9Sstevel@tonic-gate /* END CSTYLED */ 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate static char decode_table[UCHAR_MAX + 1]; 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate /* DEC is the basic 1 character decoding function (historical algorithm) */ 87*7c478bd9Sstevel@tonic-gate #define DEC(c) decode_table[c] 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate /* true if the character is in the base64 encoding table */ 90*7c478bd9Sstevel@tonic-gate #define validbase64(c) (('A' <= (c) && (c) <= 'Z') || \ 91*7c478bd9Sstevel@tonic-gate ('a' <= (c) && (c) <= 'z') || \ 92*7c478bd9Sstevel@tonic-gate ('0' <= (c) && (c) <= '9') || \ 93*7c478bd9Sstevel@tonic-gate (c) == '+' || (c) == '/') 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate static void decode(FILE *, FILE *, int); 96*7c478bd9Sstevel@tonic-gate static int outdec(unsigned char *, unsigned char *, int); 97*7c478bd9Sstevel@tonic-gate static int outdec64(unsigned char *, unsigned char *, int); 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate /* from usr/src/cmd/chmod/common.c */ 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate void errmsg(int severity, int code, char *format, ...); 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate extern mode_t newmode(char *ms, mode_t new_mode, mode_t umsk, 104*7c478bd9Sstevel@tonic-gate char *file, char *path); 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate static char *prog; 107*7c478bd9Sstevel@tonic-gate static char outfile[PATH_MAX]; 108*7c478bd9Sstevel@tonic-gate static int mode_err = 0; /* mode conversion error flag */ 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate int 111*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 112*7c478bd9Sstevel@tonic-gate { 113*7c478bd9Sstevel@tonic-gate FILE *in, *out; 114*7c478bd9Sstevel@tonic-gate int pipeout = 0; 115*7c478bd9Sstevel@tonic-gate int oflag = 0; 116*7c478bd9Sstevel@tonic-gate int i; 117*7c478bd9Sstevel@tonic-gate mode_t mode, p_umask; 118*7c478bd9Sstevel@tonic-gate char dest[PATH_MAX]; 119*7c478bd9Sstevel@tonic-gate char modebits[1024]; 120*7c478bd9Sstevel@tonic-gate char buf[LINE_MAX]; 121*7c478bd9Sstevel@tonic-gate int c, errflag = 0; 122*7c478bd9Sstevel@tonic-gate struct stat sbuf; 123*7c478bd9Sstevel@tonic-gate int base64flag = 0; 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate prog = argv[0]; 126*7c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_DFL); 127*7c478bd9Sstevel@tonic-gate bzero(dest, sizeof (dest)); 128*7c478bd9Sstevel@tonic-gate outfile[0] = '\0'; 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate /* Set locale environment variables local definitions */ 131*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 132*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 133*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 134*7c478bd9Sstevel@tonic-gate #endif 135*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 136*7c478bd9Sstevel@tonic-gate p_umask = umask((mode_t)0); 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:p")) != EOF) { 139*7c478bd9Sstevel@tonic-gate switch (c) { 140*7c478bd9Sstevel@tonic-gate case 'o': 141*7c478bd9Sstevel@tonic-gate oflag++; 142*7c478bd9Sstevel@tonic-gate (void) strncpy(outfile, optarg, sizeof (outfile)); 143*7c478bd9Sstevel@tonic-gate break; 144*7c478bd9Sstevel@tonic-gate case 'p': 145*7c478bd9Sstevel@tonic-gate pipeout++; 146*7c478bd9Sstevel@tonic-gate break; 147*7c478bd9Sstevel@tonic-gate default: 148*7c478bd9Sstevel@tonic-gate case '?': 149*7c478bd9Sstevel@tonic-gate errflag++; 150*7c478bd9Sstevel@tonic-gate break; 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate argc -= optind; 154*7c478bd9Sstevel@tonic-gate argv = &argv[optind]; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate /* optional input arg */ 157*7c478bd9Sstevel@tonic-gate if (argc > 0) { 158*7c478bd9Sstevel@tonic-gate if ((in = fopen(*argv, "r")) == NULL) { 159*7c478bd9Sstevel@tonic-gate perror(*argv); 160*7c478bd9Sstevel@tonic-gate exit(1); 161*7c478bd9Sstevel@tonic-gate } 162*7c478bd9Sstevel@tonic-gate argv++; argc--; 163*7c478bd9Sstevel@tonic-gate } else { 164*7c478bd9Sstevel@tonic-gate in = stdin; 165*7c478bd9Sstevel@tonic-gate errno = 0; 166*7c478bd9Sstevel@tonic-gate if (fstat(fileno(in), &sbuf) < 0) { 167*7c478bd9Sstevel@tonic-gate perror("stdin"); 168*7c478bd9Sstevel@tonic-gate exit(1); 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate if ((argc > 0) || errflag || (oflag && pipeout)) { 173*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 174*7c478bd9Sstevel@tonic-gate gettext("Usage: %s [-o outfile | -p] [infile]\n"), prog); 175*7c478bd9Sstevel@tonic-gate exit(2); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate /* search for header line */ 179*7c478bd9Sstevel@tonic-gate for (;;) { 180*7c478bd9Sstevel@tonic-gate if (fgets(buf, sizeof (buf), in) == NULL) { 181*7c478bd9Sstevel@tonic-gate /* suppress message if we printed a mode error */ 182*7c478bd9Sstevel@tonic-gate if (mode_err == 0) 183*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 184*7c478bd9Sstevel@tonic-gate gettext("No begin line\n")); 185*7c478bd9Sstevel@tonic-gate exit(3); 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate /* 188*7c478bd9Sstevel@tonic-gate * the check for begin-base64 obviously needs to come 189*7c478bd9Sstevel@tonic-gate * first, since both algorithms' begin strings start 190*7c478bd9Sstevel@tonic-gate * with 'begin'. Also verify that there is a valid 191*7c478bd9Sstevel@tonic-gate * octal or symbolic file mode. 192*7c478bd9Sstevel@tonic-gate */ 193*7c478bd9Sstevel@tonic-gate if (strncmp(buf, "begin-base64", 12) == 0) { 194*7c478bd9Sstevel@tonic-gate base64flag = 1; 195*7c478bd9Sstevel@tonic-gate mode_err = 0; 196*7c478bd9Sstevel@tonic-gate if ((sscanf(buf + 13, "%1023s %1023s", 197*7c478bd9Sstevel@tonic-gate modebits, dest) == 2) && 198*7c478bd9Sstevel@tonic-gate ((sscanf(modebits, "%lo", &mode) == 1) || 199*7c478bd9Sstevel@tonic-gate ((mode = newmode(modebits, 0, p_umask, 200*7c478bd9Sstevel@tonic-gate "", "")) != 0) && mode_err == 0)) 201*7c478bd9Sstevel@tonic-gate break; 202*7c478bd9Sstevel@tonic-gate } else if (strncmp(buf, "begin", 5) == 0) { 203*7c478bd9Sstevel@tonic-gate base64flag = 0; 204*7c478bd9Sstevel@tonic-gate mode_err = 0; 205*7c478bd9Sstevel@tonic-gate if ((sscanf(buf + 6, "%1023s %1023s", 206*7c478bd9Sstevel@tonic-gate modebits, dest) == 2) && 207*7c478bd9Sstevel@tonic-gate ((sscanf(modebits, "%lo", &mode) == 1) || 208*7c478bd9Sstevel@tonic-gate ((mode = newmode(modebits, 0, p_umask, 209*7c478bd9Sstevel@tonic-gate "", "")) != 0) && mode_err == 0)) 210*7c478bd9Sstevel@tonic-gate break; 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate /* 215*7c478bd9Sstevel@tonic-gate * Now that we know the type of encoding used, we can 216*7c478bd9Sstevel@tonic-gate * initialize the decode table. 217*7c478bd9Sstevel@tonic-gate */ 218*7c478bd9Sstevel@tonic-gate if (base64flag == 0) { 219*7c478bd9Sstevel@tonic-gate (void) memset(decode_table, 0xFF, sizeof (decode_table)); 220*7c478bd9Sstevel@tonic-gate for (i = 0; i <= TABLE_SIZE; i++) 221*7c478bd9Sstevel@tonic-gate decode_table[(unsigned char)i + 0x20] = 222*7c478bd9Sstevel@tonic-gate (unsigned char)i & 0x3F; 223*7c478bd9Sstevel@tonic-gate } else 224*7c478bd9Sstevel@tonic-gate (void) memcpy(decode_table, base64tab, sizeof (base64tab)); 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate /* 227*7c478bd9Sstevel@tonic-gate * Filename specified on the command line with -o 228*7c478bd9Sstevel@tonic-gate * overrides filename in the encoded file. 229*7c478bd9Sstevel@tonic-gate */ 230*7c478bd9Sstevel@tonic-gate if (outfile[0] != '\0') 231*7c478bd9Sstevel@tonic-gate (void) strncpy(dest, outfile, sizeof (dest)); 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate if (pipeout || 234*7c478bd9Sstevel@tonic-gate (dest[0] == '-' && dest[1] == '\0' && outfile[0] == '\0')) { 235*7c478bd9Sstevel@tonic-gate out = stdout; 236*7c478bd9Sstevel@tonic-gate bzero(outfile, sizeof (outfile)); 237*7c478bd9Sstevel@tonic-gate bzero(dest, sizeof (dest)); 238*7c478bd9Sstevel@tonic-gate } else { 239*7c478bd9Sstevel@tonic-gate /* handle ~user/file format */ 240*7c478bd9Sstevel@tonic-gate if (dest[0] == '~') { 241*7c478bd9Sstevel@tonic-gate char *sl; 242*7c478bd9Sstevel@tonic-gate struct passwd *user; 243*7c478bd9Sstevel@tonic-gate char dnbuf[100]; 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate sl = strchr(dest, '/'); 246*7c478bd9Sstevel@tonic-gate if (sl == NULL) { 247*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 248*7c478bd9Sstevel@tonic-gate gettext("Illegal ~user\n")); 249*7c478bd9Sstevel@tonic-gate exit(3); 250*7c478bd9Sstevel@tonic-gate } 251*7c478bd9Sstevel@tonic-gate *sl++ = 0; 252*7c478bd9Sstevel@tonic-gate user = getpwnam(dest+1); 253*7c478bd9Sstevel@tonic-gate if (user == NULL) { 254*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 255*7c478bd9Sstevel@tonic-gate gettext("No such user as %s\n"), dest); 256*7c478bd9Sstevel@tonic-gate exit(4); 257*7c478bd9Sstevel@tonic-gate } 258*7c478bd9Sstevel@tonic-gate (void) strncpy(dnbuf, user->pw_dir, sizeof (dnbuf)); 259*7c478bd9Sstevel@tonic-gate (void) strlcat(dnbuf, "/", sizeof (dnbuf)); 260*7c478bd9Sstevel@tonic-gate (void) strlcat(dnbuf, sl, sizeof (dnbuf)); 261*7c478bd9Sstevel@tonic-gate (void) strncpy(dest, dnbuf, sizeof (dest)); 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate /* if not using stdout, create file */ 265*7c478bd9Sstevel@tonic-gate if (dest[0] != '\0') { 266*7c478bd9Sstevel@tonic-gate if ((out = fopen(dest, "w")) == NULL) { 267*7c478bd9Sstevel@tonic-gate perror(dest); 268*7c478bd9Sstevel@tonic-gate exit(4); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate (void) chmod(dest, mode & 0777); 271*7c478bd9Sstevel@tonic-gate } 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate decode(in, out, base64flag); 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate if (fclose(out) == EOF) { 276*7c478bd9Sstevel@tonic-gate perror(prog); 277*7c478bd9Sstevel@tonic-gate exit(6); 278*7c478bd9Sstevel@tonic-gate } 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate return (0); 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate /* 284*7c478bd9Sstevel@tonic-gate * copy from in to out, decoding as you go along. 285*7c478bd9Sstevel@tonic-gate */ 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate static void 288*7c478bd9Sstevel@tonic-gate decode(FILE *in, FILE *out, int base64) 289*7c478bd9Sstevel@tonic-gate { 290*7c478bd9Sstevel@tonic-gate char inbuf[120], *ibp, *iptr; 291*7c478bd9Sstevel@tonic-gate unsigned char outbuf[BUFSIZE], *obp, *optr; 292*7c478bd9Sstevel@tonic-gate int n, octets, warned, endseen, numbase64chars; 293*7c478bd9Sstevel@tonic-gate unsigned char chr[4], curchr, ch; 294*7c478bd9Sstevel@tonic-gate longlong_t line; 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate if (! base64) { /* use historical algorithm */ 297*7c478bd9Sstevel@tonic-gate warned = 0; 298*7c478bd9Sstevel@tonic-gate for (line = 1; ; line++) { 299*7c478bd9Sstevel@tonic-gate /* for each input line */ 300*7c478bd9Sstevel@tonic-gate if (fgets(inbuf, sizeof (inbuf), in) == NULL) { 301*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 302*7c478bd9Sstevel@tonic-gate gettext("No end line\n")); 303*7c478bd9Sstevel@tonic-gate exit(5); 304*7c478bd9Sstevel@tonic-gate } 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate /* Is line == 'end\n'? */ 307*7c478bd9Sstevel@tonic-gate if (strcmp(inbuf, "end\n") == 0) { 308*7c478bd9Sstevel@tonic-gate break; 309*7c478bd9Sstevel@tonic-gate } 310*7c478bd9Sstevel@tonic-gate 311*7c478bd9Sstevel@tonic-gate n = DEC(inbuf[0]); 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate if (n < 0) 314*7c478bd9Sstevel@tonic-gate continue; 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate /* 317*7c478bd9Sstevel@tonic-gate * Decode data lines. 318*7c478bd9Sstevel@tonic-gate * 319*7c478bd9Sstevel@tonic-gate * Note that uuencode/uudecode uses only the portable 320*7c478bd9Sstevel@tonic-gate * character set for encoded data and the portable 321*7c478bd9Sstevel@tonic-gate * character set characters must be represented in 322*7c478bd9Sstevel@tonic-gate * a single byte. We use this knowledge to reuse 323*7c478bd9Sstevel@tonic-gate * buffer space while decoding. 324*7c478bd9Sstevel@tonic-gate */ 325*7c478bd9Sstevel@tonic-gate octets = n; 326*7c478bd9Sstevel@tonic-gate obp = (unsigned char *) &inbuf[0]; 327*7c478bd9Sstevel@tonic-gate ibp = &inbuf[1]; 328*7c478bd9Sstevel@tonic-gate while (octets > 0) { 329*7c478bd9Sstevel@tonic-gate if ((ch = outdec((unsigned char *)obp, 330*7c478bd9Sstevel@tonic-gate (unsigned char *)ibp, octets)) 331*7c478bd9Sstevel@tonic-gate != 0x20) { 332*7c478bd9Sstevel@tonic-gate /* invalid characters where detected */ 333*7c478bd9Sstevel@tonic-gate if (!warned) { 334*7c478bd9Sstevel@tonic-gate warned = 1; 335*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 336*7c478bd9Sstevel@tonic-gate gettext("Invalid character" 337*7c478bd9Sstevel@tonic-gate " (0x%x) on line" 338*7c478bd9Sstevel@tonic-gate " %lld\n"), ch, line); 339*7c478bd9Sstevel@tonic-gate } 340*7c478bd9Sstevel@tonic-gate break; 341*7c478bd9Sstevel@tonic-gate } 342*7c478bd9Sstevel@tonic-gate ibp += 4; 343*7c478bd9Sstevel@tonic-gate obp += 3; 344*7c478bd9Sstevel@tonic-gate octets -= 3; 345*7c478bd9Sstevel@tonic-gate } 346*7c478bd9Sstevel@tonic-gate /* 347*7c478bd9Sstevel@tonic-gate * Only write out uncorrupted lines 348*7c478bd9Sstevel@tonic-gate */ 349*7c478bd9Sstevel@tonic-gate if (octets <= 0) { 350*7c478bd9Sstevel@tonic-gate (void) fwrite(inbuf, n, 1, out); 351*7c478bd9Sstevel@tonic-gate } 352*7c478bd9Sstevel@tonic-gate } 353*7c478bd9Sstevel@tonic-gate } else { /* use base64 algorithm */ 354*7c478bd9Sstevel@tonic-gate endseen = numbase64chars = 0; 355*7c478bd9Sstevel@tonic-gate optr = outbuf; 356*7c478bd9Sstevel@tonic-gate while ((fgets(inbuf, sizeof (inbuf), in)) != NULL) { 357*7c478bd9Sstevel@tonic-gate /* process an input line */ 358*7c478bd9Sstevel@tonic-gate iptr = inbuf; 359*7c478bd9Sstevel@tonic-gate while ((curchr = *(iptr++)) != NULL) { 360*7c478bd9Sstevel@tonic-gate /* decode chars */ 361*7c478bd9Sstevel@tonic-gate if (curchr == '=') /* if end */ 362*7c478bd9Sstevel@tonic-gate endseen++; 363*7c478bd9Sstevel@tonic-gate 364*7c478bd9Sstevel@tonic-gate if (validbase64(curchr)) 365*7c478bd9Sstevel@tonic-gate chr[numbase64chars++] = curchr; 366*7c478bd9Sstevel@tonic-gate /* 367*7c478bd9Sstevel@tonic-gate * if we've gathered 4 base64 octets 368*7c478bd9Sstevel@tonic-gate * we need to decode and output them 369*7c478bd9Sstevel@tonic-gate */ 370*7c478bd9Sstevel@tonic-gate if (numbase64chars == 4) { 371*7c478bd9Sstevel@tonic-gate /*LINTED*/ 372*7c478bd9Sstevel@tonic-gate if (optr - outbuf > BUFSIZE - 3) { 373*7c478bd9Sstevel@tonic-gate (void) fwrite(outbuf, 374*7c478bd9Sstevel@tonic-gate /*LINTED*/ 375*7c478bd9Sstevel@tonic-gate (size_t)(optr - outbuf), 376*7c478bd9Sstevel@tonic-gate 1, out); 377*7c478bd9Sstevel@tonic-gate if (ferror(out)) { 378*7c478bd9Sstevel@tonic-gate perror(prog); 379*7c478bd9Sstevel@tonic-gate exit(6); 380*7c478bd9Sstevel@tonic-gate } 381*7c478bd9Sstevel@tonic-gate optr = outbuf; 382*7c478bd9Sstevel@tonic-gate } 383*7c478bd9Sstevel@tonic-gate octets = outdec64(optr, chr, 4); 384*7c478bd9Sstevel@tonic-gate optr += octets; 385*7c478bd9Sstevel@tonic-gate numbase64chars = 0; 386*7c478bd9Sstevel@tonic-gate } 387*7c478bd9Sstevel@tonic-gate } 388*7c478bd9Sstevel@tonic-gate /* 389*7c478bd9Sstevel@tonic-gate * handle any remaining base64 octets at end 390*7c478bd9Sstevel@tonic-gate */ 391*7c478bd9Sstevel@tonic-gate if (endseen && numbase64chars > 0) { 392*7c478bd9Sstevel@tonic-gate octets = outdec64(optr, chr, numbase64chars); 393*7c478bd9Sstevel@tonic-gate optr += octets; 394*7c478bd9Sstevel@tonic-gate numbase64chars = 0; 395*7c478bd9Sstevel@tonic-gate } 396*7c478bd9Sstevel@tonic-gate } 397*7c478bd9Sstevel@tonic-gate /* 398*7c478bd9Sstevel@tonic-gate * if we have generated any additional output 399*7c478bd9Sstevel@tonic-gate * in the buffer, write it out 400*7c478bd9Sstevel@tonic-gate */ 401*7c478bd9Sstevel@tonic-gate if (optr != outbuf) { 402*7c478bd9Sstevel@tonic-gate /*LINTED*/ 403*7c478bd9Sstevel@tonic-gate (void) fwrite(outbuf, (size_t)(optr - outbuf), 404*7c478bd9Sstevel@tonic-gate 1, out); 405*7c478bd9Sstevel@tonic-gate if (ferror(out)) { 406*7c478bd9Sstevel@tonic-gate perror(prog); 407*7c478bd9Sstevel@tonic-gate exit(6); 408*7c478bd9Sstevel@tonic-gate } 409*7c478bd9Sstevel@tonic-gate } 410*7c478bd9Sstevel@tonic-gate 411*7c478bd9Sstevel@tonic-gate if (endseen == 0) { 412*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No end line\n")); 413*7c478bd9Sstevel@tonic-gate exit(5); 414*7c478bd9Sstevel@tonic-gate } 415*7c478bd9Sstevel@tonic-gate } 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate 418*7c478bd9Sstevel@tonic-gate /* 419*7c478bd9Sstevel@tonic-gate * historical algorithm 420*7c478bd9Sstevel@tonic-gate * 421*7c478bd9Sstevel@tonic-gate * output a group of 3 bytes (4 input characters). 422*7c478bd9Sstevel@tonic-gate * the input chars are pointed to by p, they are to 423*7c478bd9Sstevel@tonic-gate * be output to file f. n is used to tell us not to 424*7c478bd9Sstevel@tonic-gate * output all of them at the end of the file. 425*7c478bd9Sstevel@tonic-gate */ 426*7c478bd9Sstevel@tonic-gate 427*7c478bd9Sstevel@tonic-gate static int 428*7c478bd9Sstevel@tonic-gate outdec(unsigned char *out, unsigned char *in, int n) 429*7c478bd9Sstevel@tonic-gate { 430*7c478bd9Sstevel@tonic-gate unsigned char b0 = DEC(*(in++)); 431*7c478bd9Sstevel@tonic-gate unsigned char b1 = DEC(*(in++)); 432*7c478bd9Sstevel@tonic-gate unsigned char b2 = DEC(*(in++)); 433*7c478bd9Sstevel@tonic-gate unsigned char b3 = DEC(*in); 434*7c478bd9Sstevel@tonic-gate 435*7c478bd9Sstevel@tonic-gate if (!isvalid(b0)) { 436*7c478bd9Sstevel@tonic-gate return (*(in-3)); 437*7c478bd9Sstevel@tonic-gate } 438*7c478bd9Sstevel@tonic-gate if (!isvalid(b1)) { 439*7c478bd9Sstevel@tonic-gate return (*(in-2)); 440*7c478bd9Sstevel@tonic-gate } 441*7c478bd9Sstevel@tonic-gate 442*7c478bd9Sstevel@tonic-gate *(out++) = (b0 << 2) | (b1 >> 4); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate if (n >= 2) { 445*7c478bd9Sstevel@tonic-gate if (!isvalid(b2)) { 446*7c478bd9Sstevel@tonic-gate return (*(in - 1)); 447*7c478bd9Sstevel@tonic-gate } 448*7c478bd9Sstevel@tonic-gate 449*7c478bd9Sstevel@tonic-gate *(out++) = (b1 << 4) | (b2 >> 2); 450*7c478bd9Sstevel@tonic-gate 451*7c478bd9Sstevel@tonic-gate if (n >= 3) { 452*7c478bd9Sstevel@tonic-gate if (!isvalid(b3)) { 453*7c478bd9Sstevel@tonic-gate return (*in); 454*7c478bd9Sstevel@tonic-gate } 455*7c478bd9Sstevel@tonic-gate *out = (b2 << 6) | b3; 456*7c478bd9Sstevel@tonic-gate } 457*7c478bd9Sstevel@tonic-gate } 458*7c478bd9Sstevel@tonic-gate return (0x20); /* a know good value */ 459*7c478bd9Sstevel@tonic-gate } 460*7c478bd9Sstevel@tonic-gate 461*7c478bd9Sstevel@tonic-gate /* 462*7c478bd9Sstevel@tonic-gate * base64 algorithm 463*7c478bd9Sstevel@tonic-gate * 464*7c478bd9Sstevel@tonic-gate * Takes a pointer to the current position in the output buffer, 465*7c478bd9Sstevel@tonic-gate * a pointer to the (up to) 4 byte base64 input buffer and a 466*7c478bd9Sstevel@tonic-gate * count of the number of valid input bytes. 467*7c478bd9Sstevel@tonic-gate * 468*7c478bd9Sstevel@tonic-gate * Return the number of bytes placed in the output buffer 469*7c478bd9Sstevel@tonic-gate */ 470*7c478bd9Sstevel@tonic-gate static int 471*7c478bd9Sstevel@tonic-gate outdec64(unsigned char *out, unsigned char *chr, int num) 472*7c478bd9Sstevel@tonic-gate { 473*7c478bd9Sstevel@tonic-gate 474*7c478bd9Sstevel@tonic-gate unsigned char char1, char2, char3, char4; 475*7c478bd9Sstevel@tonic-gate unsigned char *outptr = out; 476*7c478bd9Sstevel@tonic-gate int rc = 0; 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate switch (num) { 479*7c478bd9Sstevel@tonic-gate case 0: 480*7c478bd9Sstevel@tonic-gate case 1: /* these are impossible */ 481*7c478bd9Sstevel@tonic-gate default: 482*7c478bd9Sstevel@tonic-gate break; 483*7c478bd9Sstevel@tonic-gate case 2: /* 2 base64 bytes == 1 decoded byte */ 484*7c478bd9Sstevel@tonic-gate char1 = base64tab[chr[0]] & 0xFF; 485*7c478bd9Sstevel@tonic-gate char2 = base64tab[chr[1]] & 0xFF; 486*7c478bd9Sstevel@tonic-gate *(outptr++) = ((char1 << 2) & 0xFC) | 487*7c478bd9Sstevel@tonic-gate ((char2 >> 4) & 0x03); 488*7c478bd9Sstevel@tonic-gate rc = 1; 489*7c478bd9Sstevel@tonic-gate break; 490*7c478bd9Sstevel@tonic-gate case 3: /* 3 base64 bytes == 2 decoded bytes */ 491*7c478bd9Sstevel@tonic-gate char1 = base64tab[chr[0]] & 0xFF; 492*7c478bd9Sstevel@tonic-gate char2 = base64tab[chr[1]] & 0xFF; 493*7c478bd9Sstevel@tonic-gate char3 = base64tab[chr[2]] & 0xFF; 494*7c478bd9Sstevel@tonic-gate *(outptr++) = ((char1 << 2) & 0xFC) | 495*7c478bd9Sstevel@tonic-gate ((char2 >> 4) & 0x03); 496*7c478bd9Sstevel@tonic-gate *(outptr++) = ((char2 << 4) & 0xF0) | 497*7c478bd9Sstevel@tonic-gate ((char3 >> 2) & 0x0F); 498*7c478bd9Sstevel@tonic-gate rc = 2; 499*7c478bd9Sstevel@tonic-gate break; 500*7c478bd9Sstevel@tonic-gate case 4: /* 4 base64 bytes == 3 decoded bytes */ 501*7c478bd9Sstevel@tonic-gate char1 = base64tab[chr[0]] & 0xFF; 502*7c478bd9Sstevel@tonic-gate char2 = base64tab[chr[1]] & 0xFF; 503*7c478bd9Sstevel@tonic-gate char3 = base64tab[chr[2]] & 0xFF; 504*7c478bd9Sstevel@tonic-gate char4 = base64tab[chr[3]] & 0xFF; 505*7c478bd9Sstevel@tonic-gate *(outptr++) = ((char1 << 2) & 0xFC) | 506*7c478bd9Sstevel@tonic-gate ((char2 >> 4) & 0x03); 507*7c478bd9Sstevel@tonic-gate *(outptr++) = ((char2 << 4) & 0xF0) | 508*7c478bd9Sstevel@tonic-gate ((char3 >> 2) & 0x0F); 509*7c478bd9Sstevel@tonic-gate *(outptr++) = ((char3 << 6) & 0xC0) | 510*7c478bd9Sstevel@tonic-gate (char4 & 0x3F); 511*7c478bd9Sstevel@tonic-gate rc = 3; 512*7c478bd9Sstevel@tonic-gate break; 513*7c478bd9Sstevel@tonic-gate } 514*7c478bd9Sstevel@tonic-gate return (rc); 515*7c478bd9Sstevel@tonic-gate } 516*7c478bd9Sstevel@tonic-gate 517*7c478bd9Sstevel@tonic-gate /* 518*7c478bd9Sstevel@tonic-gate * error message routine called by newmode. 519*7c478bd9Sstevel@tonic-gate * 520*7c478bd9Sstevel@tonic-gate * The severity and code are ignored here. If this routine gets 521*7c478bd9Sstevel@tonic-gate * called, we set a global flag (which can be tested after return 522*7c478bd9Sstevel@tonic-gate * from here) which tells us whether or not a valid mode has been 523*7c478bd9Sstevel@tonic-gate * parsed or if we printed an error message. 524*7c478bd9Sstevel@tonic-gate */ 525*7c478bd9Sstevel@tonic-gate 526*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 527*7c478bd9Sstevel@tonic-gate void 528*7c478bd9Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...) 529*7c478bd9Sstevel@tonic-gate { 530*7c478bd9Sstevel@tonic-gate va_list ap; 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate va_start(ap, format); 533*7c478bd9Sstevel@tonic-gate 534*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "uudecode: "); 535*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, format, ap); 536*7c478bd9Sstevel@tonic-gate 537*7c478bd9Sstevel@tonic-gate va_end(ap); 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate mode_err = 1; 540*7c478bd9Sstevel@tonic-gate } 541