xref: /netbsd/usr.bin/base64/base64.c (revision bb505439)
1 /*	$NetBSD: base64.c,v 1.5 2021/08/27 17:53:13 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: base64.c,v 1.5 2021/08/27 17:53:13 christos Exp $");
34 
35 #include <ctype.h>
36 #include <errno.h>
37 #include <err.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 static const char B64[] =
46     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
47 
48 static size_t
getinput(FILE * fin,uint8_t in[3])49 getinput(FILE *fin, uint8_t in[3])
50 {
51 	size_t res;
52 	int c;
53 
54 	for (res = 0; res < 3 && (c = getc(fin)) != EOF; res++)
55 		in[res] = (uint8_t)c;
56 	for (size_t i = res; i < 3; i++)
57 		in[i] = 0;
58 
59 	return res;
60 }
61 
62 static int
putoutput(FILE * fout,uint8_t out[4],size_t len,size_t wrap,size_t * pos)63 putoutput(FILE *fout, uint8_t out[4], size_t len, size_t wrap, size_t *pos)
64 {
65 	size_t i;
66 
67 	for (i = 0; i < len + 1; i++) {
68 		if (out[i] >= 64) {
69 			return EINVAL;
70 		}
71 		if (fputc(B64[out[i]], fout) == -1)
72 			return errno;
73 		if (++(*pos) == wrap) {
74 			if (fputc('\n', fout) == -1)
75 				return errno;
76 			*pos = 0;
77 		}
78 	}
79 	for (; i < 4; i++) {
80 		if (fputc('=', fout) == -1)
81 			return errno;
82 		if (++(*pos) == wrap) {
83 			if (fputc('\n', fout) == -1)
84 				return errno;
85 			*pos = 0;
86 		}
87 	}
88 
89 	return 0;
90 }
91 
92 static void
encode(uint8_t out[4],uint8_t in[3])93 encode(uint8_t out[4], uint8_t in[3])
94 {
95 	out[0] = in[0] >> 2;
96 	out[1] = (uint8_t)(((in[0] & 0x03) << 4) | (in[1] >> 4));
97 	out[2] = (uint8_t)(((in[1] & 0x0f) << 2) | (in[2] >> 6));
98 	out[3] = in[2] & 0x3f;
99 }
100 
101 static int
b64_encode(FILE * fout,FILE * fin,size_t wrap)102 b64_encode(FILE *fout, FILE *fin, size_t wrap)
103 {
104 	uint8_t in[3];
105 	uint8_t out[4];
106 	size_t ilen;
107 	size_t pos = 0;
108 	int e;
109 
110 	while ((ilen = getinput(fin, in)) > 2) {
111 		encode(out, in);
112 		if ((e = putoutput(fout, out, ilen, wrap, &pos)) != 0)
113 			return e;
114 	}
115 
116 	if (ilen != 0) {
117 		encode(out, in);
118 		if ((e = putoutput(fout, out, ilen, wrap, &pos)) != 0)
119 			return e;
120 	}
121 
122 	if (pos && wrap) {
123 		if (fputc('\n', fout) == -1)
124 			return errno;
125 	}
126 	return 0;
127 }
128 
129 
130 static int
b64_decode(FILE * fout,FILE * fin,bool ignore)131 b64_decode(FILE *fout, FILE *fin, bool ignore)
132 {
133 	int state, c;
134 	uint8_t b, out;
135 	const char *pos;
136 
137 	state = 0;
138 	out = 0;
139 
140 	while ((c = getc(fin)) != -1) {
141 		if (ignore && isspace(c))
142 			continue;
143 
144 		if (c == '=')
145 			break;
146 
147 		pos = strchr(B64, c);
148 		if (pos == NULL)
149 			return EFTYPE;
150 
151 		b = (uint8_t)(pos - B64);
152 
153 		switch (state) {
154 		case 0:
155 			out = (uint8_t)(b << 2);
156 			break;
157 		case 1:
158 			out |= b >> 4;
159 			if (fputc(out, fout) == -1)
160 				return errno;
161 			out = (uint8_t)((b & 0xf) << 4);
162 			break;
163 		case 2:
164 			out |= b >> 2;
165 			if (fputc(out, fout) == -1)
166 				return errno;
167 			out = (uint8_t)((b & 0x3) << 6);
168 			break;
169 		case 3:
170 			out |= b;
171 			if (fputc(out, fout) == -1)
172 				return errno;
173 			out = 0;
174 			break;
175 		default:
176 			abort();
177 		}
178 		state = (state + 1) & 3;
179 	}
180 
181 	if (c == '=') {
182 		switch (state) {
183 		case 0:
184 		case 1:
185 			return EFTYPE;
186 		case 2:
187 			while ((c = getc(fin)) != -1) {
188 				if (ignore && isspace(c))
189 					continue;
190 				break;
191 			}
192 			if (c != '=')
193 				return EFTYPE;
194 			/*FALLTHROUGH*/
195 		case 3:
196 			while ((c = getc(fin)) != -1) {
197 				if (ignore && isspace(c))
198 					continue;
199 				break;
200 			}
201 			if (c != -1)
202 				return EFTYPE;
203 			return 0;
204 		default:
205 			abort();
206 		}
207 	}
208 
209 	if (c != -1 || state != 0)
210 		return EFTYPE;
211 
212 	return 0;
213 }
214 
215 static __dead void
usage(void)216 usage(void)
217 {
218 	fprintf(stderr, "Usage: %s [-di] [-w <wrap>] [<file>]...\n",
219 	    getprogname());
220 	exit(EXIT_FAILURE);
221 }
222 
223 static void
doit(FILE * fout,FILE * fin,bool decode,bool ignore,size_t wrap)224 doit(FILE *fout, FILE *fin, bool decode, bool ignore, size_t wrap)
225 {
226 	int e;
227 
228 	if (decode)
229 		e = b64_decode(fout, fin, ignore);
230 	else
231 		e = b64_encode(fout, fin, wrap);
232 
233 	if (e == 0)
234 		return;
235 	errc(EXIT_FAILURE, e, "%scoding failed", decode ? "De": "En");
236 }
237 
238 int
main(int argc,char * argv[])239 main(int argc, char *argv[])
240 {
241 	bool decode = false;
242 	size_t wrap = 76;
243 	bool ignore = true;
244 	int c;
245 
246 	while ((c = getopt(argc, argv, "b:Ddiw:")) != -1) {
247 		switch (c) {
248 		case 'D':
249 			decode = ignore = true;
250 			break;
251 		case 'd':
252 			decode = true;
253 			break;
254 		case 'i':
255 			ignore = true;
256 			break;
257 		case 'b':
258 		case 'w':
259 			wrap = (size_t)atoi(optarg);
260 			break;
261 		default:
262 			usage();
263 		}
264 	}
265 
266 	if (optind == argc) {
267 		doit(stdout, stdin, decode, ignore, wrap);
268 		return EXIT_SUCCESS;
269 	}
270 
271 	for (c = optind; c < argc; c++) {
272 		FILE *fp = strcmp(argv[c], "-") == 0 ?
273 		    stdin : fopen(argv[c], "r");
274 		if (fp == NULL)
275 			err(EXIT_FAILURE, "Can't open `%s'", argv[c]);
276 		doit(stdout, fp, decode, ignore, wrap);
277 		if (fp != stdin)
278 			fclose(fp);
279 		fclose(fp);
280 	}
281 
282 	return EXIT_SUCCESS;
283 }
284