base64.c revision 1.6 1 /* $NetBSD: base64.c,v 1.6 2023/08/11 02:43:59 rillig 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.6 2023/08/11 02:43:59 rillig 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
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
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 if (fputc(B64[out[i]], fout) == -1)
71 return errno;
72 if (++(*pos) == wrap) {
73 if (fputc('\n', fout) == -1)
74 return errno;
75 *pos = 0;
76 }
77 }
78 for (; i < 4; i++) {
79 if (fputc('=', fout) == -1)
80 return errno;
81 if (++(*pos) == wrap) {
82 if (fputc('\n', fout) == -1)
83 return errno;
84 *pos = 0;
85 }
86 }
87
88 return 0;
89 }
90
91 static void
92 encode(uint8_t out[4], uint8_t in[3])
93 {
94 out[0] = in[0] >> 2;
95 out[1] = (uint8_t)(((in[0] & 0x03) << 4) | (in[1] >> 4));
96 out[2] = (uint8_t)(((in[1] & 0x0f) << 2) | (in[2] >> 6));
97 out[3] = in[2] & 0x3f;
98 }
99
100 static int
101 b64_encode(FILE *fout, FILE *fin, size_t wrap)
102 {
103 uint8_t in[3];
104 uint8_t out[4];
105 size_t ilen;
106 size_t pos = 0;
107 int e;
108
109 while ((ilen = getinput(fin, in)) > 2) {
110 encode(out, in);
111 if ((e = putoutput(fout, out, ilen, wrap, &pos)) != 0)
112 return e;
113 }
114
115 if (ilen != 0) {
116 encode(out, in);
117 if ((e = putoutput(fout, out, ilen, wrap, &pos)) != 0)
118 return e;
119 }
120
121 if (pos != 0 && wrap != 0) {
122 if (fputc('\n', fout) == -1)
123 return errno;
124 }
125 return 0;
126 }
127
128 static int
129 b64_decode(FILE *fout, FILE *fin, bool ignore)
130 {
131 int state, c;
132 uint8_t b, out;
133 const char *pos;
134
135 state = 0;
136 out = 0;
137
138 while ((c = getc(fin)) != -1) {
139 if (ignore && isspace(c))
140 continue;
141
142 if (c == '=')
143 break;
144
145 pos = strchr(B64, c);
146 if (pos == NULL)
147 return EFTYPE;
148
149 b = (uint8_t)(pos - B64);
150
151 switch (state) {
152 case 0:
153 out = (uint8_t)(b << 2);
154 break;
155 case 1:
156 out |= b >> 4;
157 if (fputc(out, fout) == -1)
158 return errno;
159 out = (uint8_t)((b & 0xf) << 4);
160 break;
161 case 2:
162 out |= b >> 2;
163 if (fputc(out, fout) == -1)
164 return errno;
165 out = (uint8_t)((b & 0x3) << 6);
166 break;
167 case 3:
168 out |= b;
169 if (fputc(out, fout) == -1)
170 return errno;
171 out = 0;
172 break;
173 default:
174 abort();
175 }
176 state = (state + 1) & 3;
177 }
178
179 if (c == '=') {
180 switch (state) {
181 case 0:
182 case 1:
183 return EFTYPE;
184 case 2:
185 while ((c = getc(fin)) != -1) {
186 if (ignore && isspace(c))
187 continue;
188 break;
189 }
190 if (c != '=')
191 return EFTYPE;
192 /*FALLTHROUGH*/
193 case 3:
194 while ((c = getc(fin)) != -1) {
195 if (ignore && isspace(c))
196 continue;
197 break;
198 }
199 if (c != -1)
200 return EFTYPE;
201 return 0;
202 default:
203 abort();
204 }
205 }
206
207 if (c != -1 || state != 0)
208 return EFTYPE;
209
210 return 0;
211 }
212
213 static __dead void
214 usage(void)
215 {
216 fprintf(stderr, "Usage: %s [-di] [-w <wrap>] [<file>]...\n",
217 getprogname());
218 exit(EXIT_FAILURE);
219 }
220
221 static void
222 doit(FILE *fout, FILE *fin, bool decode, bool ignore, size_t wrap)
223 {
224 int e;
225
226 if (decode)
227 e = b64_decode(fout, fin, ignore);
228 else
229 e = b64_encode(fout, fin, wrap);
230
231 if (e == 0)
232 return;
233 errc(EXIT_FAILURE, e, "%scoding failed", decode ? "De": "En");
234 }
235
236 int
237 main(int argc, char *argv[])
238 {
239 bool decode = false;
240 size_t wrap = 76;
241 bool ignore = true;
242 int c;
243
244 while ((c = getopt(argc, argv, "b:Ddiw:")) != -1) {
245 switch (c) {
246 case 'D':
247 decode = ignore = true;
248 break;
249 case 'd':
250 decode = true;
251 break;
252 case 'i':
253 ignore = true;
254 break;
255 case 'b':
256 case 'w':
257 wrap = (size_t)atoi(optarg);
258 break;
259 default:
260 usage();
261 }
262 }
263
264 if (optind == argc) {
265 doit(stdout, stdin, decode, ignore, wrap);
266 return EXIT_SUCCESS;
267 }
268
269 for (c = optind; c < argc; c++) {
270 FILE *fp = strcmp(argv[c], "-") == 0 ?
271 stdin : fopen(argv[c], "r");
272 if (fp == NULL)
273 err(EXIT_FAILURE, "Can't open `%s'", argv[c]);
274 doit(stdout, fp, decode, ignore, wrap);
275 if (fp != stdin)
276 fclose(fp);
277 fclose(fp);
278 }
279
280 return EXIT_SUCCESS;
281 }
282