1/*-
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1983, 1993\n\
34	The Regents of the University of California.  All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)uuencode.c	8.2 (Berkeley) 4/2/94";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: stable/11/usr.bin/uuencode/uuencode.c 317062 2017-04-17 18:55:54Z araujo $");
43
44/*
45 * uuencode [input] output
46 *
47 * Encode a file so it can be mailed to a remote system.
48 */
49#include <sys/param.h>
50#include <sys/socket.h>
51#include <sys/stat.h>
52
53#include <netinet/in.h>
54
55#include <err.h>
56#include <libgen.h>
57#include <resolv.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63static void encode(void);
64static void base64_encode(void);
65static void usage(void);
66
67static FILE *output;
68static int mode;
69static char raw = 0;
70static char **av;
71
72int
73main(int argc, char *argv[])
74{
75	struct stat sb;
76	int base64;
77	int ch;
78	char *outfile;
79
80	base64 = 0;
81	outfile = NULL;
82
83	if (strcmp(basename(argv[0]), "b64encode") == 0)
84		base64 = 1;
85
86	while ((ch = getopt(argc, argv, "mo:r")) != -1) {
87		switch (ch) {
88		case 'm':
89			base64 = 1;
90			break;
91		case 'o':
92			outfile = optarg;
93			break;
94		case 'r':
95			raw = 1;
96			break;
97		case '?':
98		default:
99			usage();
100		}
101	}
102	argv += optind;
103	argc -= optind;
104
105	switch(argc) {
106	case 2:			/* optional first argument is input file */
107		if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
108			err(1, "%s", *argv);
109#define	RWX	(S_IRWXU|S_IRWXG|S_IRWXO)
110		mode = sb.st_mode & RWX;
111		++argv;
112		break;
113	case 1:
114#define	RW	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
115		mode = RW & ~umask(RW);
116		break;
117	case 0:
118	default:
119		usage();
120	}
121
122	av = argv;
123
124	if (outfile != NULL) {
125		output = fopen(outfile, "w+");
126		if (output == NULL)
127			err(1, "unable to open %s for output", outfile);
128	} else
129		output = stdout;
130	if (base64)
131		base64_encode();
132	else
133		encode();
134	if (ferror(output))
135		errx(1, "write error");
136	exit(0);
137}
138
139/* ENC is the basic 1 character encoding function to make a char printing */
140#define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
141
142/*
143 * Copy from in to out, encoding in base64 as you go along.
144 */
145static void
146base64_encode(void)
147{
148	/*
149	 * Output must fit into 80 columns, chunks come in 4, leave 1.
150	 */
151#define	GROUPS	((80 / 4) - 1)
152	unsigned char buf[3];
153	char buf2[sizeof(buf) * 2 + 1];
154	size_t n;
155	int rv, sequence;
156
157	sequence = 0;
158
159	if (!raw)
160		fprintf(output, "begin-base64 %o %s\n", mode, *av);
161	while ((n = fread(buf, 1, sizeof(buf), stdin))) {
162		++sequence;
163		rv = b64_ntop(buf, n, buf2, nitems(buf2));
164		if (rv == -1)
165			errx(1, "b64_ntop: error encoding base64");
166		fprintf(output, "%s%s", buf2, (sequence % GROUPS) ? "" : "\n");
167	}
168	if (sequence % GROUPS)
169		fprintf(output, "\n");
170	if (!raw)
171		fprintf(output, "====\n");
172}
173
174/*
175 * Copy from in to out, encoding as you go along.
176 */
177static void
178encode(void)
179{
180	register int ch, n;
181	register char *p;
182	char buf[80];
183
184	if (!raw)
185		(void)fprintf(output, "begin %o %s\n", mode, *av);
186	while ((n = fread(buf, 1, 45, stdin))) {
187		ch = ENC(n);
188		if (fputc(ch, output) == EOF)
189			break;
190		for (p = buf; n > 0; n -= 3, p += 3) {
191			/* Pad with nulls if not a multiple of 3. */
192			if (n < 3) {
193				p[2] = '\0';
194				if (n < 2)
195					p[1] = '\0';
196			}
197			ch = *p >> 2;
198			ch = ENC(ch);
199			if (fputc(ch, output) == EOF)
200				break;
201			ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
202			ch = ENC(ch);
203			if (fputc(ch, output) == EOF)
204				break;
205			ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
206			ch = ENC(ch);
207			if (fputc(ch, output) == EOF)
208				break;
209			ch = p[2] & 077;
210			ch = ENC(ch);
211			if (fputc(ch, output) == EOF)
212				break;
213		}
214		if (fputc('\n', output) == EOF)
215			break;
216	}
217	if (ferror(stdin))
218		errx(1, "read error");
219	if (!raw)
220		(void)fprintf(output, "%c\nend\n", ENC('\0'));
221}
222
223static void
224usage(void)
225{
226	(void)fprintf(stderr,
227"usage: uuencode [-m] [-o outfile] [infile] remotefile\n"
228"       b64encode [-o outfile] [infile] remotefile\n");
229	exit(1);
230}
231