summaryrefslogtreecommitdiff
path: root/usr/src/cmd/bnu/uudecode.c
blob: eb90dc6a77c75d72874980bc89d318e250725a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*	Copyright 2004 Sun Microsystems, Inc.  All rights reserved. */
/*	Use is subject to license terms. */

/*
 * uudecode [-o outfile | -p] [input]
 *
 * create the specified file, decoding as you go.
 * used with uuencode.
 */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <nl_types.h>
#include <langinfo.h>
#include <iconv.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <stdarg.h>

#define	BUFSIZE	90	/* must be a multiple of 3 */

#define	TABLE_SIZE	0x40

#define	isvalid(octet)	(octet <= 0x40)

/*
 * base64 decoding table
 */
/* BEGIN CSTYLED */
static char base64tab[] = {
	'\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
	'\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
	'\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
	'\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
	'\377', '\377', '\377', '\377', '\377', '\377', '\377', '\377',
	'\377', '\377', '\377',     62, '\377', '\377', '\377',     63,
	    52,     53,     54,     55,     56,     57,     58,     59,
	    60,     61, '\377', '\377', '\377', '\377', '\377', '\377',
	'\377',      0,      1,      2,      3,      4,      5,      6,
	     7,      8,      9,     10,     11,     12,     13,     14,
	    15,     16,     17,     18,     19,     20,     21,     22,
	    23,     24,     25, '\377', '\377', '\377', '\377', '\377',
	'\377',     26,     27,     28,     29,     30,     31,     32,
	    33,     34,     35,     36,     37,     38,     39,     40,
	    41,     42,     43,     44,     45,     46,     47,     48,
	    49,     50,     51, '\377', '\377', '\377', '\377', '\377'
};
/* END CSTYLED */

static char	decode_table[UCHAR_MAX + 1];

/* DEC is the basic 1 character decoding function (historical algorithm) */
#define	DEC(c)	decode_table[c]

/* true if the character is in the base64 encoding table */
#define	validbase64(c) (('A' <= (c) && (c) <= 'Z') || \
		('a' <= (c) && (c) <= 'z') || \
		('0' <= (c) && (c) <= '9') || \
		(c) == '+' || (c) == '/')

static void	decode(FILE *, FILE *, int);
static int	outdec(unsigned char *, unsigned char *, int);
static int	outdec64(unsigned char *, unsigned char *, int);

/* from usr/src/cmd/chmod/common.c */

void errmsg(int severity, int code, char *format, ...);

extern mode_t newmode(char *ms, mode_t new_mode, mode_t umsk,
    char *file, char *path);

static char	*prog;
static char	outfile[PATH_MAX];
static int	mode_err = 0;	/* mode conversion error flag */

int
main(int argc, char **argv)
{
	FILE *in, *out;
	int pipeout = 0;
	int oflag = 0;
	int i;
	mode_t mode, p_umask;
	char dest[PATH_MAX];
	char modebits[1024];
	char buf[LINE_MAX];
	int	c, errflag = 0;
	struct stat sbuf;
	int base64flag = 0;

	prog = argv[0];
	(void) signal(SIGPIPE, SIG_DFL);
	bzero(dest, sizeof (dest));
	outfile[0] = '\0';

	/* Set locale environment variables local definitions */
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
#endif
	(void) textdomain(TEXT_DOMAIN);
	p_umask = umask((mode_t)0);

	while ((c = getopt(argc, argv, "o:p")) != EOF) {
		switch (c) {
		case 'o':
			oflag++;
			(void) strncpy(outfile, optarg, sizeof (outfile));
			break;
		case 'p':
			pipeout++;
			break;
		default:
		case '?':
			errflag++;
			break;
		}
	}
	argc -= optind;
	argv = &argv[optind];

	/* optional input arg */
	if (argc > 0) {
		if ((in = fopen(*argv, "r")) == NULL) {
			perror(*argv);
			exit(1);
		}
		argv++; argc--;
	} else {
		in = stdin;
		errno = 0;
		if (fstat(fileno(in), &sbuf) < 0) {
			perror("stdin");
			exit(1);
		}
	}

	if ((argc > 0) || errflag || (oflag && pipeout)) {
		(void) fprintf(stderr,
		    gettext("Usage: %s [-o outfile | -p] [infile]\n"), prog);
		exit(2);
	}

	/* search for header line */
	for (;;) {
		if (fgets(buf, sizeof (buf), in) == NULL) {
			/* suppress message if we printed a mode error */
			if (mode_err == 0)
				(void) fprintf(stderr,
				    gettext("No begin line\n"));
			exit(3);
		}
		/*
		 * the check for begin-base64 obviously needs to come
		 * first, since both algorithms' begin strings start
		 * with 'begin'.  Also verify that there is a valid
		 * octal or symbolic file mode.
		 */
		if (strncmp(buf, "begin-base64", 12) == 0) {
			base64flag = 1;
			mode_err = 0;
			if ((sscanf(buf + 13, "%1023s %1023s",
			    modebits, dest) == 2) &&
			    ((sscanf(modebits, "%lo", &mode) == 1) ||
				((mode = newmode(modebits, 0, p_umask,
				    "", "")) != 0) && mode_err == 0))
				break;
		} else if (strncmp(buf, "begin", 5) == 0) {
			base64flag = 0;
			mode_err = 0;
			if ((sscanf(buf + 6, "%1023s %1023s",
			    modebits, dest) == 2) &&
			    ((sscanf(modebits, "%lo", &mode) == 1) ||
				((mode = newmode(modebits, 0, p_umask,
				    "", "")) != 0) && mode_err == 0))
				break;
		}
	}

	/*
	 * Now that we know the type of encoding used, we can
	 * initialize the decode table.
	 */
	if (base64flag == 0) {
		(void) memset(decode_table, 0xFF, sizeof (decode_table));
		for (i = 0; i <= TABLE_SIZE; i++)
			decode_table[(unsigned char)i + 0x20] =
			    (unsigned char)i & 0x3F;
	} else
		(void) memcpy(decode_table, base64tab, sizeof (base64tab));

	/*
	 * Filename specified on the command line with -o
	 * overrides filename in the encoded file.
	 */
	if (outfile[0] != '\0')
		(void) strncpy(dest, outfile, sizeof (dest));

	if (pipeout ||
	    (dest[0] == '-' && dest[1] == '\0' && outfile[0] == '\0')) {
		out = stdout;
		bzero(outfile, sizeof (outfile));
		bzero(dest, sizeof (dest));
	} else {
		/* handle ~user/file format */
		if (dest[0] == '~') {
			char *sl;
			struct passwd *user;
			char dnbuf[100];

			sl = strchr(dest, '/');
			if (sl == NULL) {
				(void) fprintf(stderr,
				    gettext("Illegal ~user\n"));
				exit(3);
			}
			*sl++ = 0;
			user = getpwnam(dest+1);
			if (user == NULL) {
				(void) fprintf(stderr,
				    gettext("No such user as %s\n"), dest);
				exit(4);
			}
			(void) strncpy(dnbuf, user->pw_dir, sizeof (dnbuf));
			(void) strlcat(dnbuf, "/", sizeof (dnbuf));
			(void) strlcat(dnbuf, sl, sizeof (dnbuf));
			(void) strncpy(dest, dnbuf, sizeof (dest));
		}
	}
	/* if not using stdout, create file */
	if (dest[0] != '\0') {
		if ((out = fopen(dest, "w")) == NULL) {
			perror(dest);
			exit(4);
		}
		(void) chmod(dest, mode & 0777);
	}

	decode(in, out, base64flag);

	if (fclose(out) == EOF) {
		perror(prog);
		exit(6);
	}

	return (0);
}

/*
 * copy from in to out, decoding as you go along.
 */

static void
decode(FILE *in, FILE *out, int base64)
{
	char	inbuf[120], *ibp, *iptr;
	unsigned char 	outbuf[BUFSIZE], *obp, *optr;
	int	n, octets, warned, endseen, numbase64chars;
	unsigned char chr[4], curchr, ch;
	longlong_t line;

	if (! base64) {	/* use historical algorithm */
		warned = 0;
		for (line = 1; ; line++) {
			/* for each input line */
			if (fgets(inbuf, sizeof (inbuf), in) == NULL) {
				(void) fprintf(stderr,
				    gettext("No end line\n"));
				exit(5);
			}

			/* Is line == 'end\n'? */
			if (strcmp(inbuf, "end\n") == 0) {
				break;
			}

			n = DEC(inbuf[0]);

			if (n < 0)
				continue;

			/*
			 * Decode data lines.
			 *
			 * Note that uuencode/uudecode uses only the portable
			 * character set for encoded data and the portable
			 * character set characters must be represented in
			 * a single byte.  We use this knowledge to reuse
			 * buffer space while decoding.
			 */
			octets = n;
			obp = (unsigned char *) &inbuf[0];
			ibp = &inbuf[1];
			while (octets > 0) {
				if ((ch = outdec((unsigned char *)obp,
				    (unsigned char *)ibp, octets))
				    != 0x20) {
					/* invalid characters where detected */
					if (!warned) {
						warned = 1;
						(void) fprintf(stderr,
						    gettext("Invalid character"
							" (0x%x) on line"
							" %lld\n"), ch, line);
					}
					break;
				}
				ibp += 4;
				obp += 3;
				octets -= 3;
			}
			/*
			 * Only write out uncorrupted lines
			 */
			if (octets <= 0) {
				(void) fwrite(inbuf, n, 1, out);
			}
		}
	} else {	/* use base64 algorithm */
		endseen = numbase64chars = 0;
		optr = outbuf;
		while ((fgets(inbuf, sizeof (inbuf), in)) != NULL) {
			/* process an input line */
			iptr = inbuf;
			while ((curchr = *(iptr++)) != '\0') {
				/* decode chars */
				if (curchr == '=') /* if end */
					endseen++;

				if (validbase64(curchr))
					chr[numbase64chars++] = curchr;
				/*
				 * if we've gathered 4 base64 octets
				 * we need to decode and output them
				 */
				if (numbase64chars == 4) {
					/*LINTED*/
					if (optr - outbuf > BUFSIZE - 3) {
						(void) fwrite(outbuf,
						    /*LINTED*/
						    (size_t)(optr - outbuf),
						    1, out);
						if (ferror(out)) {
							perror(prog);
							exit(6);
						}
						optr = outbuf;
					}
					octets = outdec64(optr, chr, 4);
					optr += octets;
					numbase64chars = 0;
				}
			}
			/*
			 * handle any remaining base64 octets at end
			 */
			if (endseen && numbase64chars > 0) {
				octets = outdec64(optr, chr, numbase64chars);
				optr += octets;
				numbase64chars = 0;
			}
		}
		/*
		 * if we have generated any additional output
		 * in the buffer, write it out
		 */
		if (optr != outbuf) {
			/*LINTED*/
			(void) fwrite(outbuf, (size_t)(optr - outbuf),
			    1, out);
			if (ferror(out)) {
				perror(prog);
				exit(6);
			}
		}

		if (endseen == 0) {
			(void) fprintf(stderr, gettext("No end line\n"));
			exit(5);
		}
	}
}

/*
 * historical algorithm
 *
 * output a group of 3 bytes (4 input characters).
 * the input chars are pointed to by p, they are to
 * be output to file f.  n is used to tell us not to
 * output all of them at the end of the file.
 */

static int
outdec(unsigned char *out, unsigned char *in, int n)
{
	unsigned char	b0 = DEC(*(in++));
	unsigned char	b1 = DEC(*(in++));
	unsigned char	b2 = DEC(*(in++));
	unsigned char	b3 = DEC(*in);

	if (!isvalid(b0)) {
		return (*(in-3));
	}
	if (!isvalid(b1)) {
		return (*(in-2));
	}

	*(out++) = (b0 << 2) | (b1 >> 4);

	if (n >= 2) {
		if (!isvalid(b2)) {
			return (*(in - 1));
		}

		*(out++) = (b1 << 4) | (b2 >> 2);

		if (n >= 3) {
			if (!isvalid(b3)) {
				return (*in);
			}
			*out = (b2 << 6) | b3;
		}
	}
	return (0x20); /* a know good value */
}

/*
 * base64 algorithm
 *
 * Takes a pointer to the current position in the output buffer,
 * a pointer to the (up to) 4 byte base64 input buffer and a
 * count of the number of valid input bytes.
 *
 * Return the number of bytes placed in the output buffer
 */
static int
outdec64(unsigned char *out, unsigned char *chr, int num)
{

	unsigned char char1, char2, char3, char4;
	unsigned char *outptr = out;
	int rc = 0;

	switch (num) {
	case 0:
	case 1: 	/* these are impossible */
	default:
		break;
	case 2:		/* 2 base64 bytes == 1 decoded byte */
		char1 = base64tab[chr[0]] & 0xFF;
		char2 = base64tab[chr[1]] & 0xFF;
		*(outptr++) = ((char1 << 2) & 0xFC) |
		    ((char2 >> 4) & 0x03);
		rc = 1;
		break;
	case 3:		/* 3 base64 bytes == 2 decoded bytes */
		char1 = base64tab[chr[0]] & 0xFF;
		char2 = base64tab[chr[1]] & 0xFF;
		char3 = base64tab[chr[2]] & 0xFF;
		*(outptr++) = ((char1 << 2) & 0xFC) |
		    ((char2 >> 4) & 0x03);
		*(outptr++) = ((char2 << 4) & 0xF0) |
		    ((char3 >> 2) & 0x0F);
		rc = 2;
		break;
	case 4:		/* 4 base64 bytes == 3 decoded bytes */
		char1 = base64tab[chr[0]] & 0xFF;
		char2 = base64tab[chr[1]] & 0xFF;
		char3 = base64tab[chr[2]] & 0xFF;
		char4 = base64tab[chr[3]] & 0xFF;
		*(outptr++) = ((char1 << 2) & 0xFC) |
		    ((char2 >> 4) & 0x03);
		*(outptr++) = ((char2 << 4) & 0xF0) |
		    ((char3 >> 2) & 0x0F);
		*(outptr++) = ((char3 << 6) & 0xC0) |
		    (char4 & 0x3F);
		rc = 3;
		break;
	}
	return (rc);
}

/*
 * error message routine called by newmode.
 *
 * The severity and code are ignored here.  If this routine gets
 * called, we set a global flag (which can be tested after return
 * from here) which tells us whether or not a valid mode has been
 * parsed or if we printed an error message.
 */

/*ARGSUSED*/
void
errmsg(int severity, int code, char *format, ...)
{
	va_list ap;

	va_start(ap, format);

	(void) fprintf(stderr, "uudecode: ");
	(void) fprintf(stderr, format, ap);

	va_end(ap);

	mode_err = 1;
}