3 * Base64 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[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz"
43 static const signed char decodemap[] = {
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
47 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
48 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
50 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
51 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @base64_encode@ --- *
58 * Arguments: @base64_ctx *ctx@ = pointer to a context block
59 * @const void *p@ = pointer to a source buffer
60 * @size_t sz@ = size of the source buffer
61 * @dstr *d@ = pointer to destination string
65 * Use: Encodes a binary string in base64. To flush out the final
66 * few characters (if necessary), pass a null source pointer.
69 void base64_encode(base64_ctx *ctx,
70 const void *p, size_t sz,
74 unsigned long acc = ctx->acc;
75 unsigned qsz = ctx->qsz;
76 const unsigned char *src = p;
79 acc = (acc << 8) | *src++;
83 DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
84 DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
85 DPUTC(d, encodemap[(acc >> 6) & 0x3f]);
86 DPUTC(d, encodemap[(acc >> 0) & 0x3f]);
88 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
89 dstr_puts(d, ctx->indent);
100 unsigned long acc = ctx->acc;
101 unsigned qsz = ctx->qsz;
108 DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
109 DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
116 DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
117 DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
118 DPUTC(d, encodemap[(acc >> 6) & 0x3f]);
128 /* --- @base64_decode@ --- *
130 * Arguments: @base64_ctx *ctx@ = pointer to a context block
131 * @const void *p@ = pointer to a source buffer
132 * @size_t sz@ = size of the source buffer
133 * @dstr *d@ = pointer to destination string
137 * Use: Decodes a binary string in base64. To flush out the final
138 * few characters (if necessary), pass a null source pointer.
141 void base64_decode(base64_ctx *ctx,
142 const void *p, size_t sz,
146 unsigned long acc = ctx->acc;
147 unsigned qsz = ctx->qsz;
153 /* --- Get the next character and convert it --- */
156 if (ch >= 128 || ch < 0)
164 /* --- Bung it in the accumulator --- */
166 acc = (acc << 6) | ch;
169 /* --- Maybe write out a completed triplet --- */
172 DPUTC(d, (acc >> 16) & 0xff);
173 DPUTC(d, (acc >> 8) & 0xff);
174 DPUTC(d, (acc >> 0) & 0xff);
184 /* --- Notes about the tail-end bits --- *
186 * Ending Base64 decoding is messy. The reference I'm using to define
187 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
188 * do at the end. It explains that I'm meant to ignore spurious `='
189 * characters, and points out that I'm not guaranteed to see anything
190 * interesting at the end. I'll play safe here, and ignore all `='
191 * characters, relying on my client to work out when to stop feeding me
192 * data. I'll use the queue size to work out how many tail-end bytes
196 unsigned long acc = ctx->acc;
197 unsigned qsz = ctx->qsz;
199 /* --- Now fiddle with everything else --- *
201 * There's a bodge here for invalid encodings which have only one hextet
202 * in the final group. I'm not sure this is really worth having, but it
203 * might save some unexpected behaviour. (Not that you won't still get
204 * unexpected behaviour if the stream is completely empty, of course.)
208 acc <<= 6 * (4 - qsz);
213 DPUTC(d, (acc >> 16) & 0xff);
219 /* --- That seems to be good enough --- */
226 /* --- @base64_init@ --- *
228 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
232 * Use: Initializes a base64 context properly.
235 void base64_init(base64_ctx *ctx)
244 /*----- Test driver code --------------------------------------------------*/
248 int main(int argc, char *argv[])
250 unsigned char buf[BUFSIZ];
253 void (*proc)(base64_ctx *, const void *, size_t, dstr *);
258 if (argc > 1 && strcmp(argv[1], "-d") == 0)
259 proc = base64_decode;
261 proc = base64_encode;
268 sz = fread(buf, 1, sizeof(buf), stdin);
270 proc(&ctx, buf, sz, &d);
271 dstr_write(&d, stdout);
274 } while (sz == sizeof(buf));
276 proc(&ctx, 0, 0, &d);
277 dstr_write(&d, stdout);
279 if (proc == base64_encode)
287 /*----- That's all, folks -------------------------------------------------*/