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