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