3 * Base32 encoding and decoding.
5 * (c) 1997 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- Important tables --------------------------------------------------*/
39 static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" };
41 static 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 */
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @base32_encode@ --- *
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
63 * Use: Encodes a binary string in base32. To flush out the final
64 * few characters (if necessary), pass a null source pointer.
67 void base32_encode(base32_ctx *ctx,
68 const void *p, size_t sz,
72 unsigned long accl = ctx->accl, acch = ctx->acch;
73 unsigned qsz = ctx->qsz;
74 const unsigned char *src = p;
77 acch = (acch << 8) | ((accl >> 24) & 0xff);
78 accl = (accl << 8) | *src++;
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]);
91 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
92 dstr_puts(d, ctx->indent);
100 ctx->acch = acch; ctx->accl = accl;
103 unsigned long accl = ctx->accl, acch = ctx->acch;
104 unsigned qsz = ctx->qsz;
107 unsigned pad = 5 - qsz;
109 acch = accl << (pad * 8 - 32);
112 acch = (acch << (8 * pad)) | ((accl & 0xffffffff) >> (32 - 8 * pad));
113 accl = accl << (8 * pad);
118 DPUTC(d, encodemap[(acch >> 3) & 0x1f]);
119 acch = (acch << 5) | ((accl >> 27) & 0x1f);
130 ctx->acch = ctx->accl = 0;
134 /* --- @base32_decode@ --- *
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
143 * Use: Decodes a binary string in base32. To flush out the final
144 * few characters (if necessary), pass a null source pointer.
147 void base32_decode(base32_ctx *ctx,
148 const void *p, size_t sz,
152 unsigned long accl = ctx->accl, acch = ctx->acch;
153 unsigned qsz = ctx->qsz;
159 /* --- Get the next character and convert it --- */
162 if (ch >= 128 || ch < 0)
170 /* --- Bung it in the accumulator --- */
172 acch = (acch << 5) | ((accl >> 27) & 0x1f);
173 accl = (accl << 5) | ch;
176 /* --- Maybe write out a completed triplet --- */
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);
189 ctx->acch = acch; ctx->accl = accl;
193 /* --- Notes about the tail-end bits --- *
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.
199 unsigned long acch = ctx->acch, accl = ctx->accl;
200 unsigned qsz = ctx->qsz;
202 /* --- Now fiddle with everything else --- *
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
212 unsigned pad = 8 - qsz;
214 acch = accl << (5 * pad - 32);
217 acch = (acch << (5 * pad)) | ((accl & 0xffffffff) >> (32 - 5 * pad));
218 accl = accl << (5 * pad);
223 DPUTC(d, acch & 0xff);
230 /* --- That seems to be good enough --- */
233 ctx->acch = ctx->accl = 0;
237 /* --- @base32_init@ --- *
239 * Arguments: @base32_ctx *ctx@ = pointer to context block to initialize
243 * Use: Initializes a base32 context properly.
246 void base32_init(base32_ctx *ctx)
248 ctx->accl = ctx->acch = 0;
255 /*----- Test driver code --------------------------------------------------*/
259 int main(int argc, char *argv[])
261 unsigned char buf[BUFSIZ];
264 void (*proc)(base32_ctx *, const void *, size_t, dstr *);
269 if (argc > 1 && strcmp(argv[1], "-d") == 0)
270 proc = base32_decode;
272 proc = base32_encode;
279 sz = fread(buf, 1, sizeof(buf), stdin);
281 proc(&ctx, buf, sz, &d);
282 dstr_write(&d, stdout);
285 } while (sz == sizeof(buf));
287 proc(&ctx, 0, 0, &d);
288 dstr_write(&d, stdout);
290 if (proc == base32_encode)
298 /*----- That's all, folks -------------------------------------------------*/