chiark / gitweb /
Infrastructure: Split the files into subdirectories.
[mLib] / codec / base32.c
CommitLineData
1c4fa429 1/* -*-c-*-
1c4fa429 2 *
3 * Base32 encoding and decoding.
4 *
5 * (c) 1997 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
1c4fa429 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
1c4fa429 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
d4efbcd9 21 *
1c4fa429 22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "base32.h"
35#include "dstr.h"
36
37/*----- Important tables --------------------------------------------------*/
38
39static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" };
40
41static const signed char decodemap[] = {
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
45 -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, /* 3x */
46 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
47 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
48 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 6x */
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 7x */
d4efbcd9 50};
1c4fa429 51
52/*----- Main code ---------------------------------------------------------*/
53
54/* --- @base32_encode@ --- *
55 *
56 * Arguments: @base32_ctx *ctx@ = pointer to a context block
57 * @const void *p@ = pointer to a source buffer
58 * @size_t sz@ = size of the source buffer
59 * @dstr *d@ = pointer to destination string
60 *
61 * Returns: ---
62 *
63 * Use: Encodes a binary string in base32. To flush out the final
64 * few characters (if necessary), pass a null source pointer.
65 */
66
67void base32_encode(base32_ctx *ctx,
68 const void *p, size_t sz,
69 dstr *d)
70{
71 if (p) {
72 unsigned long accl = ctx->accl, acch = ctx->acch;
73 unsigned qsz = ctx->qsz;
74 const unsigned char *src = p;
75
76 while (sz) {
77 acch = (acch << 8) | ((accl >> 24) & 0xff);
78 accl = (accl << 8) | *src++;
79 qsz++;
80 sz--;
81 if (qsz == 5) {
82 DPUTC(d, encodemap[(acch >> 3) & 0x1f]);
83 DPUTC(d, encodemap[((acch << 2) & 0x1c) | ((accl >> 30) & 0x03)]);
84 DPUTC(d, encodemap[(accl >> 25) & 0x1f]);
85 DPUTC(d, encodemap[(accl >> 20) & 0x1f]);
86 DPUTC(d, encodemap[(accl >> 15) & 0x1f]);
87 DPUTC(d, encodemap[(accl >> 10) & 0x1f]);
88 DPUTC(d, encodemap[(accl >> 5) & 0x1f]);
89 DPUTC(d, encodemap[(accl >> 0) & 0x1f]);
90 ctx->lnlen += 8;
91 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
92 dstr_puts(d, ctx->indent);
93 ctx->lnlen = 0;
94 }
95 qsz = 0;
96 accl = acch = 0;
97 }
98 }
99
100 ctx->acch = acch; ctx->accl = accl;
101 ctx->qsz = qsz;
102 } else {
103 unsigned long accl = ctx->accl, acch = ctx->acch;
104 unsigned qsz = ctx->qsz;
105
106 if (qsz) {
107 unsigned pad = 5 - qsz;
108 if (pad > 3) {
109 acch = accl << (pad * 8 - 32);
110 accl = 0;
111 } else {
112 acch = (acch << (8 * pad)) | ((accl & 0xffffffff) >> (32 - 8 * pad));
113 accl = accl << (8 * pad);
114 }
115 qsz = 40;
116 pad *= 8;
117 while (qsz > pad) {
118 DPUTC(d, encodemap[(acch >> 3) & 0x1f]);
119 acch = (acch << 5) | ((accl >> 27) & 0x1f);
120 accl = (accl << 5);
121 qsz -= 5;
122 }
123 while (qsz) {
124 DPUTC(d, '=');
125 qsz -= 5;
126 }
127 ctx->lnlen += 8;
128 }
129 ctx->qsz = 0;
130 ctx->acch = ctx->accl = 0;
131 }
132}
133
134/* --- @base32_decode@ --- *
135 *
136 * Arguments: @base32_ctx *ctx@ = pointer to a context block
137 * @const void *p@ = pointer to a source buffer
138 * @size_t sz@ = size of the source buffer
139 * @dstr *d@ = pointer to destination string
140 *
141 * Returns: ---
142 *
143 * Use: Decodes a binary string in base32. To flush out the final
144 * few characters (if necessary), pass a null source pointer.
145 */
146
147void base32_decode(base32_ctx *ctx,
148 const void *p, size_t sz,
149 dstr *d)
150{
151 if (p) {
152 unsigned long accl = ctx->accl, acch = ctx->acch;
153 unsigned qsz = ctx->qsz;
154 const char *src = p;
155 int ch;
156
157 while (sz) {
158
159 /* --- Get the next character and convert it --- */
160
161 ch = *src++;
162 if (ch >= 128 || ch < 0)
163 ch = -1;
164 else
165 ch = decodemap[ch];
166 sz--;
167 if (ch == -1)
168 continue;
169
170 /* --- Bung it in the accumulator --- */
171
172 acch = (acch << 5) | ((accl >> 27) & 0x1f);
173 accl = (accl << 5) | ch;
174 qsz++;
175
176 /* --- Maybe write out a completed triplet --- */
177
178 if (qsz == 8) {
179 DPUTC(d, (acch >> 0) & 0xff);
180 DPUTC(d, (accl >> 24) & 0xff);
181 DPUTC(d, (accl >> 16) & 0xff);
182 DPUTC(d, (accl >> 8) & 0xff);
183 DPUTC(d, (accl >> 0) & 0xff);
184 acch = accl = 0;
185 qsz = 0;
186 }
187 }
188
189 ctx->acch = acch; ctx->accl = accl;
190 ctx->qsz = qsz;
191 } else {
192
193 /* --- Notes about the tail-end bits --- *
194 *
195 * I'll use the queue size to work out how many tail-end bytes I ought to
196 * write. This isn't strictly right, but it's easier.
197 */
198
199 unsigned long acch = ctx->acch, accl = ctx->accl;
200 unsigned qsz = ctx->qsz;
201
202 /* --- Now fiddle with everything else --- *
203 *
204 * There's a bodge here for invalid encodings which have a funny number
205 * of quintets in the final group. I'm not sure this is really worth
206 * having, but it might save some unexpected behaviour. (Not that you
207 * won't still get unexpected behaviour if the stream is completely
208 * empty, of course.)
209 */
210
211 if (qsz) {
212 unsigned pad = 8 - qsz;
213 if (pad > 6) {
214 acch = accl << (5 * pad - 32);
215 accl = 0;
216 } else {
217 acch = (acch << (5 * pad)) | ((accl & 0xffffffff) >> (32 - 5 * pad));
218 accl = accl << (5 * pad);
219 }
220
221 qsz *= 5;
222 while (qsz >= 8) {
223 DPUTC(d, acch & 0xff);
224 acch = accl >> 24;
225 accl <<= 8;
226 qsz -= 8;
227 }
228 }
229
230 /* --- That seems to be good enough --- */
231
232 ctx->qsz = 0;
233 ctx->acch = ctx->accl = 0;
234 }
235}
236
237/* --- @base32_init@ --- *
238 *
239 * Arguments: @base32_ctx *ctx@ = pointer to context block to initialize
240 *
241 * Returns: ---
242 *
243 * Use: Initializes a base32 context properly.
244 */
245
246void base32_init(base32_ctx *ctx)
247{
248 ctx->accl = ctx->acch = 0;
249 ctx->qsz = 0;
250 ctx->lnlen = 0;
251 ctx->indent = "\n";
252 ctx->maxline = 72;
253}
254
255/*----- Test driver code --------------------------------------------------*/
256
257#ifdef TEST_RIG
258
259int main(int argc, char *argv[])
260{
261 unsigned char buf[BUFSIZ];
262 dstr d = DSTR_INIT;
263 base32_ctx ctx;
264 void (*proc)(base32_ctx *, const void *, size_t, dstr *);
265 size_t sz;
266
267 base32_init(&ctx);
268
269 if (argc > 1 && strcmp(argv[1], "-d") == 0)
270 proc = base32_decode;
271 else {
272 proc = base32_encode;
273 putchar('\t');
274 ctx.indent = "\n\t";
275 ctx.maxline = 32;
276 }
277
278 do {
279 sz = fread(buf, 1, sizeof(buf), stdin);
280 if (sz) {
281 proc(&ctx, buf, sz, &d);
282 dstr_write(&d, stdout);
283 dstr_destroy(&d);
284 }
285 } while (sz == sizeof(buf));
286
287 proc(&ctx, 0, 0, &d);
288 dstr_write(&d, stdout);
289
290 if (proc == base32_encode)
291 putchar('\n');
292
293 return (0);
294}
295
296#endif
297
298/*----- That's all, folks -------------------------------------------------*/