base64.c revision 1.1 1 /* $NetBSD: base64.c,v 1.1 2018/07/24 15:26:16 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.1 2018/07/24 15:26:16 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
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 errno = EINVAL;
70 return -1;
71 }
72 if (fputc(B64[out[i]], fout) == -1)
73 return -1;
74 if (++(*pos) == wrap) {
75 if (fputc('\n', fout) == -1)
76 return -1;
77 *pos = 0;
78 }
79 }
80 for (; i < 4; i++) {
81 if (fputc('=', fout) == -1)
82 return -1;
83 if (++(*pos) == wrap) {
84 if (fputc('\n', fout) == -1)
85 return -1;
86 *pos = 0;
87 }
88 }
89
90 return 0;
91 }
92
93 static void
94 encode(uint8_t out[4], uint8_t in[3])
95 {
96 out[0] = in[0] >> 2;
97 out[1] = (uint8_t)(((in[0] & 0x03) << 4) | (in[1] >> 4));
98 out[2] = (uint8_t)(((in[1] & 0x0f) << 2) | (in[2] >> 6));
99 out[3] = in[2] & 0x3f;
100 }
101
102 static int
103 b64_encode(FILE *fout, FILE *fin, size_t wrap)
104 {
105 uint8_t in[3];
106 uint8_t out[4];
107 size_t ilen;
108 size_t pos = 0;
109
110 while ((ilen = getinput(fin, in)) > 2) {
111 encode(out, in);
112 if (putoutput(fout, out, ilen, wrap, &pos) == -1)
113 return -1;
114 }
115
116 if (ilen != 0) {
117 encode(out, in);
118 if (putoutput(fout, out, ilen, wrap, &pos) == -1)
119 return -1;
120 }
121
122 if (pos && wrap) {
123 if (fputc('\n', fout) == -1)
124 return -1;
125 }
126 return 0;
127 }
128
129
130 static int
131 b64_decode(FILE *fout, FILE *fin, bool ignore)
132 {
133 int state, c;
134 uint8_t b, out;
135 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 -1;
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 -1;
161 out = (uint8_t)((b & 0xf) << 4);
162 break;
163 case 2:
164 out |= b >> 2;
165 if (fputc(out, fout) == -1)
166 return -1;
167 out = (uint8_t)((b & 0x3) << 6);
168 break;
169 case 3:
170 out |= b;
171 if (fputc(out, fout) == -1)
172 return -1;
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 -1;
186 case 2:
187 while ((c = getc(fin)) != -1) {
188 if (ignore && isspace(c))
189 continue;
190 break;
191 }
192 if (c != '=')
193 return -1;
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 -1;
203 return 0;
204 default:
205 abort();
206 }
207 }
208
209 if (c != -1 || state != 0)
210 return -1;
211
212 return 0;
213 }
214
215 static __dead 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
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(stdout, stdin, ignore) != 0;
230 else
231 e = b64_encode(stdout, stdin, wrap) != 0;
232
233 if (e != 0)
234 errx(EXIT_FAILURE, "%scoding failed", decode ? "De": "En");
235 }
236
237 int
238 main(int argc, char *argv[])
239 {
240 bool decode = false;
241 size_t wrap = 76;
242 bool ignore = false;
243 int c;
244
245 while ((c = getopt(argc, argv, "diw:")) != -1) {
246 switch (c) {
247 case 'd':
248 decode = true;
249 break;
250 case 'i':
251 ignore = true;
252 break;
253 case 'w':
254 wrap = (size_t)atoi(optarg);
255 break;
256 default:
257 usage();
258 }
259 }
260
261 if (optind == argc) {
262 doit(stdout, stdin, decode, ignore, wrap);
263 return EXIT_SUCCESS;
264 }
265
266 for (c = optind; c < argc; c++) {
267 FILE *fp = strcmp(argv[c], "-") == 0 ?
268 stdin : fopen(argv[c], "r");
269 if (fp == NULL)
270 err(EXIT_FAILURE, "Can't open `%s'", argv[c]);
271 doit(stdout, fp, decode, ignore, wrap);
272 if (fp != stdin)
273 fclose(fp);
274 fclose(fp);
275 }
276
277 return EXIT_SUCCESS;
278 }
279