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