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