17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. */
267c478bd9Sstevel@tonic-gate /* Use is subject to license terms. */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * uudecode [-o outfile | -p] [input]
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * create the specified file, decoding as you go.
327c478bd9Sstevel@tonic-gate * used with uuencode.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <pwd.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate #include <nl_types.h>
447c478bd9Sstevel@tonic-gate #include <langinfo.h>
457c478bd9Sstevel@tonic-gate #include <iconv.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <signal.h>
507c478bd9Sstevel@tonic-gate #include <stdarg.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #define BUFSIZE 90 /* must be a multiple of 3 */
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #define TABLE_SIZE 0x40
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate #define isvalid(octet) (octet <= 0x40)
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * base64 decoding table
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
627c478bd9Sstevel@tonic-gate static char base64tab[] = {
637c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
647c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
657c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
667c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
677c478bd9Sstevel@tonic-gate '\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
687c478bd9Sstevel@tonic-gate '\377', '\377', '\377', 62, '\377', '\377', '\377', 63,
697c478bd9Sstevel@tonic-gate 52, 53, 54, 55, 56, 57, 58, 59,
707c478bd9Sstevel@tonic-gate 60, 61, '\377', '\377', '\377', '\377', '\377', '\377',
717c478bd9Sstevel@tonic-gate '\377', 0, 1, 2, 3, 4, 5, 6,
727c478bd9Sstevel@tonic-gate 7, 8, 9, 10, 11, 12, 13, 14,
737c478bd9Sstevel@tonic-gate 15, 16, 17, 18, 19, 20, 21, 22,
747c478bd9Sstevel@tonic-gate 23, 24, 25, '\377', '\377', '\377', '\377', '\377',
757c478bd9Sstevel@tonic-gate '\377', 26, 27, 28, 29, 30, 31, 32,
767c478bd9Sstevel@tonic-gate 33, 34, 35, 36, 37, 38, 39, 40,
777c478bd9Sstevel@tonic-gate 41, 42, 43, 44, 45, 46, 47, 48,
787c478bd9Sstevel@tonic-gate 49, 50, 51, '\377', '\377', '\377', '\377', '\377'
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate /* END CSTYLED */
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate static char decode_table[UCHAR_MAX + 1];
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /* DEC is the basic 1 character decoding function (historical algorithm) */
857c478bd9Sstevel@tonic-gate #define DEC(c) decode_table[c]
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /* true if the character is in the base64 encoding table */
887c478bd9Sstevel@tonic-gate #define validbase64(c) (('A' <= (c) && (c) <= 'Z') || \
897c478bd9Sstevel@tonic-gate ('a' <= (c) && (c) <= 'z') || \
907c478bd9Sstevel@tonic-gate ('0' <= (c) && (c) <= '9') || \
917c478bd9Sstevel@tonic-gate (c) == '+' || (c) == '/')
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate static void decode(FILE *, FILE *, int);
947c478bd9Sstevel@tonic-gate static int outdec(unsigned char *, unsigned char *, int);
957c478bd9Sstevel@tonic-gate static int outdec64(unsigned char *, unsigned char *, int);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /* from usr/src/cmd/chmod/common.c */
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate void errmsg(int severity, int code, char *format, ...);
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate extern mode_t newmode(char *ms, mode_t new_mode, mode_t umsk,
1027c478bd9Sstevel@tonic-gate char *file, char *path);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate static char *prog;
1057c478bd9Sstevel@tonic-gate static char outfile[PATH_MAX];
1067c478bd9Sstevel@tonic-gate static int mode_err = 0; /* mode conversion error flag */
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1097c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate FILE *in, *out;
1127c478bd9Sstevel@tonic-gate int pipeout = 0;
1137c478bd9Sstevel@tonic-gate int oflag = 0;
1147c478bd9Sstevel@tonic-gate int i;
1157c478bd9Sstevel@tonic-gate mode_t mode, p_umask;
1167c478bd9Sstevel@tonic-gate char dest[PATH_MAX];
1177c478bd9Sstevel@tonic-gate char modebits[1024];
1187c478bd9Sstevel@tonic-gate char buf[LINE_MAX];
1197c478bd9Sstevel@tonic-gate int c, errflag = 0;
1207c478bd9Sstevel@tonic-gate struct stat sbuf;
1217c478bd9Sstevel@tonic-gate int base64flag = 0;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate prog = argv[0];
1247c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_DFL);
1257c478bd9Sstevel@tonic-gate bzero(dest, sizeof (dest));
1267c478bd9Sstevel@tonic-gate outfile[0] = '\0';
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /* Set locale environment variables local definitions */
1297c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1307c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1317c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1347c478bd9Sstevel@tonic-gate p_umask = umask((mode_t)0);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:p")) != EOF) {
1377c478bd9Sstevel@tonic-gate switch (c) {
1387c478bd9Sstevel@tonic-gate case 'o':
1397c478bd9Sstevel@tonic-gate oflag++;
1407c478bd9Sstevel@tonic-gate (void) strncpy(outfile, optarg, sizeof (outfile));
1417c478bd9Sstevel@tonic-gate break;
1427c478bd9Sstevel@tonic-gate case 'p':
1437c478bd9Sstevel@tonic-gate pipeout++;
1447c478bd9Sstevel@tonic-gate break;
1457c478bd9Sstevel@tonic-gate default:
1467c478bd9Sstevel@tonic-gate case '?':
1477c478bd9Sstevel@tonic-gate errflag++;
1487c478bd9Sstevel@tonic-gate break;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate argc -= optind;
1527c478bd9Sstevel@tonic-gate argv = &argv[optind];
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /* optional input arg */
1557c478bd9Sstevel@tonic-gate if (argc > 0) {
1567c478bd9Sstevel@tonic-gate if ((in = fopen(*argv, "r")) == NULL) {
1577c478bd9Sstevel@tonic-gate perror(*argv);
1587c478bd9Sstevel@tonic-gate exit(1);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate argv++; argc--;
1617c478bd9Sstevel@tonic-gate } else {
1627c478bd9Sstevel@tonic-gate in = stdin;
1637c478bd9Sstevel@tonic-gate errno = 0;
1647c478bd9Sstevel@tonic-gate if (fstat(fileno(in), &sbuf) < 0) {
1657c478bd9Sstevel@tonic-gate perror("stdin");
1667c478bd9Sstevel@tonic-gate exit(1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if ((argc > 0) || errflag || (oflag && pipeout)) {
1717c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1727c478bd9Sstevel@tonic-gate gettext("Usage: %s [-o outfile | -p] [infile]\n"), prog);
1737c478bd9Sstevel@tonic-gate exit(2);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /* search for header line */
1777c478bd9Sstevel@tonic-gate for (;;) {
1787c478bd9Sstevel@tonic-gate if (fgets(buf, sizeof (buf), in) == NULL) {
1797c478bd9Sstevel@tonic-gate /* suppress message if we printed a mode error */
1807c478bd9Sstevel@tonic-gate if (mode_err == 0)
1817c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1827c478bd9Sstevel@tonic-gate gettext("No begin line\n"));
1837c478bd9Sstevel@tonic-gate exit(3);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * the check for begin-base64 obviously needs to come
1877c478bd9Sstevel@tonic-gate * first, since both algorithms' begin strings start
1887c478bd9Sstevel@tonic-gate * with 'begin'. Also verify that there is a valid
1897c478bd9Sstevel@tonic-gate * octal or symbolic file mode.
1907c478bd9Sstevel@tonic-gate */
1917c478bd9Sstevel@tonic-gate if (strncmp(buf, "begin-base64", 12) == 0) {
1927c478bd9Sstevel@tonic-gate base64flag = 1;
1937c478bd9Sstevel@tonic-gate mode_err = 0;
1947c478bd9Sstevel@tonic-gate if ((sscanf(buf + 13, "%1023s %1023s",
1957c478bd9Sstevel@tonic-gate modebits, dest) == 2) &&
1967c478bd9Sstevel@tonic-gate ((sscanf(modebits, "%lo", &mode) == 1) ||
1977c478bd9Sstevel@tonic-gate ((mode = newmode(modebits, 0, p_umask,
1987c478bd9Sstevel@tonic-gate "", "")) != 0) && mode_err == 0))
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate } else if (strncmp(buf, "begin", 5) == 0) {
2017c478bd9Sstevel@tonic-gate base64flag = 0;
2027c478bd9Sstevel@tonic-gate mode_err = 0;
2037c478bd9Sstevel@tonic-gate if ((sscanf(buf + 6, "%1023s %1023s",
2047c478bd9Sstevel@tonic-gate modebits, dest) == 2) &&
2057c478bd9Sstevel@tonic-gate ((sscanf(modebits, "%lo", &mode) == 1) ||
2067c478bd9Sstevel@tonic-gate ((mode = newmode(modebits, 0, p_umask,
2077c478bd9Sstevel@tonic-gate "", "")) != 0) && mode_err == 0))
2087c478bd9Sstevel@tonic-gate break;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * Now that we know the type of encoding used, we can
2147c478bd9Sstevel@tonic-gate * initialize the decode table.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate if (base64flag == 0) {
2177c478bd9Sstevel@tonic-gate (void) memset(decode_table, 0xFF, sizeof (decode_table));
2187c478bd9Sstevel@tonic-gate for (i = 0; i <= TABLE_SIZE; i++)
2197c478bd9Sstevel@tonic-gate decode_table[(unsigned char)i + 0x20] =
2207c478bd9Sstevel@tonic-gate (unsigned char)i & 0x3F;
2217c478bd9Sstevel@tonic-gate } else
2227c478bd9Sstevel@tonic-gate (void) memcpy(decode_table, base64tab, sizeof (base64tab));
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * Filename specified on the command line with -o
2267c478bd9Sstevel@tonic-gate * overrides filename in the encoded file.
2277c478bd9Sstevel@tonic-gate */
2287c478bd9Sstevel@tonic-gate if (outfile[0] != '\0')
2297c478bd9Sstevel@tonic-gate (void) strncpy(dest, outfile, sizeof (dest));
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (pipeout ||
2327c478bd9Sstevel@tonic-gate (dest[0] == '-' && dest[1] == '\0' && outfile[0] == '\0')) {
2337c478bd9Sstevel@tonic-gate out = stdout;
2347c478bd9Sstevel@tonic-gate bzero(outfile, sizeof (outfile));
2357c478bd9Sstevel@tonic-gate bzero(dest, sizeof (dest));
2367c478bd9Sstevel@tonic-gate } else {
2377c478bd9Sstevel@tonic-gate /* handle ~user/file format */
2387c478bd9Sstevel@tonic-gate if (dest[0] == '~') {
2397c478bd9Sstevel@tonic-gate char *sl;
2407c478bd9Sstevel@tonic-gate struct passwd *user;
2417c478bd9Sstevel@tonic-gate char dnbuf[100];
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate sl = strchr(dest, '/');
2447c478bd9Sstevel@tonic-gate if (sl == NULL) {
2457c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2467c478bd9Sstevel@tonic-gate gettext("Illegal ~user\n"));
2477c478bd9Sstevel@tonic-gate exit(3);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate *sl++ = 0;
2507c478bd9Sstevel@tonic-gate user = getpwnam(dest+1);
2517c478bd9Sstevel@tonic-gate if (user == NULL) {
2527c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2537c478bd9Sstevel@tonic-gate gettext("No such user as %s\n"), dest);
2547c478bd9Sstevel@tonic-gate exit(4);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate (void) strncpy(dnbuf, user->pw_dir, sizeof (dnbuf));
2577c478bd9Sstevel@tonic-gate (void) strlcat(dnbuf, "/", sizeof (dnbuf));
2587c478bd9Sstevel@tonic-gate (void) strlcat(dnbuf, sl, sizeof (dnbuf));
2597c478bd9Sstevel@tonic-gate (void) strncpy(dest, dnbuf, sizeof (dest));
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate /* if not using stdout, create file */
2637c478bd9Sstevel@tonic-gate if (dest[0] != '\0') {
2647c478bd9Sstevel@tonic-gate if ((out = fopen(dest, "w")) == NULL) {
2657c478bd9Sstevel@tonic-gate perror(dest);
2667c478bd9Sstevel@tonic-gate exit(4);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate (void) chmod(dest, mode & 0777);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate decode(in, out, base64flag);
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate if (fclose(out) == EOF) {
2747c478bd9Sstevel@tonic-gate perror(prog);
2757c478bd9Sstevel@tonic-gate exit(6);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate return (0);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * copy from in to out, decoding as you go along.
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate static void
decode(FILE * in,FILE * out,int base64)2867c478bd9Sstevel@tonic-gate decode(FILE *in, FILE *out, int base64)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate char inbuf[120], *ibp, *iptr;
2897c478bd9Sstevel@tonic-gate unsigned char outbuf[BUFSIZE], *obp, *optr;
2907c478bd9Sstevel@tonic-gate int n, octets, warned, endseen, numbase64chars;
2917c478bd9Sstevel@tonic-gate unsigned char chr[4], curchr, ch;
2927c478bd9Sstevel@tonic-gate longlong_t line;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate if (! base64) { /* use historical algorithm */
2957c478bd9Sstevel@tonic-gate warned = 0;
2967c478bd9Sstevel@tonic-gate for (line = 1; ; line++) {
2977c478bd9Sstevel@tonic-gate /* for each input line */
2987c478bd9Sstevel@tonic-gate if (fgets(inbuf, sizeof (inbuf), in) == NULL) {
2997c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3007c478bd9Sstevel@tonic-gate gettext("No end line\n"));
3017c478bd9Sstevel@tonic-gate exit(5);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate /* Is line == 'end\n'? */
3057c478bd9Sstevel@tonic-gate if (strcmp(inbuf, "end\n") == 0) {
3067c478bd9Sstevel@tonic-gate break;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate n = DEC(inbuf[0]);
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate if (n < 0)
3127c478bd9Sstevel@tonic-gate continue;
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate * Decode data lines.
3167c478bd9Sstevel@tonic-gate *
3177c478bd9Sstevel@tonic-gate * Note that uuencode/uudecode uses only the portable
3187c478bd9Sstevel@tonic-gate * character set for encoded data and the portable
3197c478bd9Sstevel@tonic-gate * character set characters must be represented in
3207c478bd9Sstevel@tonic-gate * a single byte. We use this knowledge to reuse
3217c478bd9Sstevel@tonic-gate * buffer space while decoding.
3227c478bd9Sstevel@tonic-gate */
3237c478bd9Sstevel@tonic-gate octets = n;
3247c478bd9Sstevel@tonic-gate obp = (unsigned char *) &inbuf[0];
3257c478bd9Sstevel@tonic-gate ibp = &inbuf[1];
3267c478bd9Sstevel@tonic-gate while (octets > 0) {
3277c478bd9Sstevel@tonic-gate if ((ch = outdec((unsigned char *)obp,
3287c478bd9Sstevel@tonic-gate (unsigned char *)ibp, octets))
3297c478bd9Sstevel@tonic-gate != 0x20) {
3307c478bd9Sstevel@tonic-gate /* invalid characters where detected */
3317c478bd9Sstevel@tonic-gate if (!warned) {
3327c478bd9Sstevel@tonic-gate warned = 1;
3337c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3347c478bd9Sstevel@tonic-gate gettext("Invalid character"
3357c478bd9Sstevel@tonic-gate " (0x%x) on line"
3367c478bd9Sstevel@tonic-gate " %lld\n"), ch, line);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate break;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate ibp += 4;
3417c478bd9Sstevel@tonic-gate obp += 3;
3427c478bd9Sstevel@tonic-gate octets -= 3;
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * Only write out uncorrupted lines
3467c478bd9Sstevel@tonic-gate */
3477c478bd9Sstevel@tonic-gate if (octets <= 0) {
3487c478bd9Sstevel@tonic-gate (void) fwrite(inbuf, n, 1, out);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate } else { /* use base64 algorithm */
3527c478bd9Sstevel@tonic-gate endseen = numbase64chars = 0;
3537c478bd9Sstevel@tonic-gate optr = outbuf;
3547c478bd9Sstevel@tonic-gate while ((fgets(inbuf, sizeof (inbuf), in)) != NULL) {
3557c478bd9Sstevel@tonic-gate /* process an input line */
3567c478bd9Sstevel@tonic-gate iptr = inbuf;
357*09f57adeSToomas Soome while ((curchr = *(iptr++)) != '\0') {
3587c478bd9Sstevel@tonic-gate /* decode chars */
3597c478bd9Sstevel@tonic-gate if (curchr == '=') /* if end */
3607c478bd9Sstevel@tonic-gate endseen++;
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate if (validbase64(curchr))
3637c478bd9Sstevel@tonic-gate chr[numbase64chars++] = curchr;
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * if we've gathered 4 base64 octets
3667c478bd9Sstevel@tonic-gate * we need to decode and output them
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate if (numbase64chars == 4) {
3697c478bd9Sstevel@tonic-gate /*LINTED*/
3707c478bd9Sstevel@tonic-gate if (optr - outbuf > BUFSIZE - 3) {
3717c478bd9Sstevel@tonic-gate (void) fwrite(outbuf,
3727c478bd9Sstevel@tonic-gate /*LINTED*/
3737c478bd9Sstevel@tonic-gate (size_t)(optr - outbuf),
3747c478bd9Sstevel@tonic-gate 1, out);
3757c478bd9Sstevel@tonic-gate if (ferror(out)) {
3767c478bd9Sstevel@tonic-gate perror(prog);
3777c478bd9Sstevel@tonic-gate exit(6);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate optr = outbuf;
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate octets = outdec64(optr, chr, 4);
3827c478bd9Sstevel@tonic-gate optr += octets;
3837c478bd9Sstevel@tonic-gate numbase64chars = 0;
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate * handle any remaining base64 octets at end
3887c478bd9Sstevel@tonic-gate */
3897c478bd9Sstevel@tonic-gate if (endseen && numbase64chars > 0) {
3907c478bd9Sstevel@tonic-gate octets = outdec64(optr, chr, numbase64chars);
3917c478bd9Sstevel@tonic-gate optr += octets;
3927c478bd9Sstevel@tonic-gate numbase64chars = 0;
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate * if we have generated any additional output
3977c478bd9Sstevel@tonic-gate * in the buffer, write it out
3987c478bd9Sstevel@tonic-gate */
3997c478bd9Sstevel@tonic-gate if (optr != outbuf) {
4007c478bd9Sstevel@tonic-gate /*LINTED*/
4017c478bd9Sstevel@tonic-gate (void) fwrite(outbuf, (size_t)(optr - outbuf),
4027c478bd9Sstevel@tonic-gate 1, out);
4037c478bd9Sstevel@tonic-gate if (ferror(out)) {
4047c478bd9Sstevel@tonic-gate perror(prog);
4057c478bd9Sstevel@tonic-gate exit(6);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate if (endseen == 0) {
4107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No end line\n"));
4117c478bd9Sstevel@tonic-gate exit(5);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate * historical algorithm
4187c478bd9Sstevel@tonic-gate *
4197c478bd9Sstevel@tonic-gate * output a group of 3 bytes (4 input characters).
4207c478bd9Sstevel@tonic-gate * the input chars are pointed to by p, they are to
4217c478bd9Sstevel@tonic-gate * be output to file f. n is used to tell us not to
4227c478bd9Sstevel@tonic-gate * output all of them at the end of the file.
4237c478bd9Sstevel@tonic-gate */
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate static int
outdec(unsigned char * out,unsigned char * in,int n)4267c478bd9Sstevel@tonic-gate outdec(unsigned char *out, unsigned char *in, int n)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate unsigned char b0 = DEC(*(in++));
4297c478bd9Sstevel@tonic-gate unsigned char b1 = DEC(*(in++));
4307c478bd9Sstevel@tonic-gate unsigned char b2 = DEC(*(in++));
4317c478bd9Sstevel@tonic-gate unsigned char b3 = DEC(*in);
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate if (!isvalid(b0)) {
4347c478bd9Sstevel@tonic-gate return (*(in-3));
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate if (!isvalid(b1)) {
4377c478bd9Sstevel@tonic-gate return (*(in-2));
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate *(out++) = (b0 << 2) | (b1 >> 4);
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate if (n >= 2) {
4437c478bd9Sstevel@tonic-gate if (!isvalid(b2)) {
4447c478bd9Sstevel@tonic-gate return (*(in - 1));
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate *(out++) = (b1 << 4) | (b2 >> 2);
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate if (n >= 3) {
4507c478bd9Sstevel@tonic-gate if (!isvalid(b3)) {
4517c478bd9Sstevel@tonic-gate return (*in);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate *out = (b2 << 6) | b3;
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate return (0x20); /* a know good value */
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate * base64 algorithm
4617c478bd9Sstevel@tonic-gate *
4627c478bd9Sstevel@tonic-gate * Takes a pointer to the current position in the output buffer,
4637c478bd9Sstevel@tonic-gate * a pointer to the (up to) 4 byte base64 input buffer and a
4647c478bd9Sstevel@tonic-gate * count of the number of valid input bytes.
4657c478bd9Sstevel@tonic-gate *
4667c478bd9Sstevel@tonic-gate * Return the number of bytes placed in the output buffer
4677c478bd9Sstevel@tonic-gate */
4687c478bd9Sstevel@tonic-gate static int
outdec64(unsigned char * out,unsigned char * chr,int num)4697c478bd9Sstevel@tonic-gate outdec64(unsigned char *out, unsigned char *chr, int num)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate unsigned char char1, char2, char3, char4;
4737c478bd9Sstevel@tonic-gate unsigned char *outptr = out;
4747c478bd9Sstevel@tonic-gate int rc = 0;
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate switch (num) {
4777c478bd9Sstevel@tonic-gate case 0:
4787c478bd9Sstevel@tonic-gate case 1: /* these are impossible */
4797c478bd9Sstevel@tonic-gate default:
4807c478bd9Sstevel@tonic-gate break;
4817c478bd9Sstevel@tonic-gate case 2: /* 2 base64 bytes == 1 decoded byte */
4827c478bd9Sstevel@tonic-gate char1 = base64tab[chr[0]] & 0xFF;
4837c478bd9Sstevel@tonic-gate char2 = base64tab[chr[1]] & 0xFF;
4847c478bd9Sstevel@tonic-gate *(outptr++) = ((char1 << 2) & 0xFC) |
4857c478bd9Sstevel@tonic-gate ((char2 >> 4) & 0x03);
4867c478bd9Sstevel@tonic-gate rc = 1;
4877c478bd9Sstevel@tonic-gate break;
4887c478bd9Sstevel@tonic-gate case 3: /* 3 base64 bytes == 2 decoded bytes */
4897c478bd9Sstevel@tonic-gate char1 = base64tab[chr[0]] & 0xFF;
4907c478bd9Sstevel@tonic-gate char2 = base64tab[chr[1]] & 0xFF;
4917c478bd9Sstevel@tonic-gate char3 = base64tab[chr[2]] & 0xFF;
4927c478bd9Sstevel@tonic-gate *(outptr++) = ((char1 << 2) & 0xFC) |
4937c478bd9Sstevel@tonic-gate ((char2 >> 4) & 0x03);
4947c478bd9Sstevel@tonic-gate *(outptr++) = ((char2 << 4) & 0xF0) |
4957c478bd9Sstevel@tonic-gate ((char3 >> 2) & 0x0F);
4967c478bd9Sstevel@tonic-gate rc = 2;
4977c478bd9Sstevel@tonic-gate break;
4987c478bd9Sstevel@tonic-gate case 4: /* 4 base64 bytes == 3 decoded bytes */
4997c478bd9Sstevel@tonic-gate char1 = base64tab[chr[0]] & 0xFF;
5007c478bd9Sstevel@tonic-gate char2 = base64tab[chr[1]] & 0xFF;
5017c478bd9Sstevel@tonic-gate char3 = base64tab[chr[2]] & 0xFF;
5027c478bd9Sstevel@tonic-gate char4 = base64tab[chr[3]] & 0xFF;
5037c478bd9Sstevel@tonic-gate *(outptr++) = ((char1 << 2) & 0xFC) |
5047c478bd9Sstevel@tonic-gate ((char2 >> 4) & 0x03);
5057c478bd9Sstevel@tonic-gate *(outptr++) = ((char2 << 4) & 0xF0) |
5067c478bd9Sstevel@tonic-gate ((char3 >> 2) & 0x0F);
5077c478bd9Sstevel@tonic-gate *(outptr++) = ((char3 << 6) & 0xC0) |
5087c478bd9Sstevel@tonic-gate (char4 & 0x3F);
5097c478bd9Sstevel@tonic-gate rc = 3;
5107c478bd9Sstevel@tonic-gate break;
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate return (rc);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate * error message routine called by newmode.
5177c478bd9Sstevel@tonic-gate *
5187c478bd9Sstevel@tonic-gate * The severity and code are ignored here. If this routine gets
5197c478bd9Sstevel@tonic-gate * called, we set a global flag (which can be tested after return
5207c478bd9Sstevel@tonic-gate * from here) which tells us whether or not a valid mode has been
5217c478bd9Sstevel@tonic-gate * parsed or if we printed an error message.
5227c478bd9Sstevel@tonic-gate */
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5257c478bd9Sstevel@tonic-gate void
errmsg(int severity,int code,char * format,...)5267c478bd9Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate va_list ap;
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate va_start(ap, format);
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "uudecode: ");
5337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, format, ap);
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate va_end(ap);
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate mode_err = 1;
5387c478bd9Sstevel@tonic-gate }
539