3 * $Id: base64.c,v 1.5 2002/01/13 13:26:16 mdw Exp $
5 * Base64 encoding and decoding.
7 * (c) 1997 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2002/01/13 13:26:16 mdw
34 * Change names for internal tables.
36 * Revision 1.4 1999/10/15 21:08:46 mdw
37 * Change support for erroneous Base64 streams with length 1 mod 4.
39 * Revision 1.3 1999/05/21 22:14:30 mdw
40 * Take advantage of the new dynamic string macros.
42 * Revision 1.2 1999/05/18 21:45:27 mdw
43 * Allow Base64 encode and decode of arbitrary rubbish.
45 * Revision 1.1 1999/05/17 20:35:00 mdw
46 * Base64 encoding and decoding support.
50 /*----- Header files ------------------------------------------------------*/
59 /*----- Important tables --------------------------------------------------*/
61 static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
62 "abcdefghijklmnopqrstuvwxyz"
65 static const signed char decodemap[] = {
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
69 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
70 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
71 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
72 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
73 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @base64_encode@ --- *
80 * Arguments: @base64_ctx *ctx@ = pointer to a context block
81 * @const void *p@ = pointer to a source buffer
82 * @size_t sz@ = size of the source buffer
83 * @dstr *d@ = pointer to destination string
87 * Use: Encodes a binary string in base64. To flush out the final
88 * few characters (if necessary), pass a null source pointer.
91 void base64_encode(base64_ctx *ctx,
92 const void *p, size_t sz,
96 unsigned long acc = ctx->acc;
97 unsigned qsz = ctx->qsz;
98 const unsigned char *src = p;
101 acc = (acc << 8) | *src++;
105 DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
106 DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
107 DPUTC(d, encodemap[(acc >> 6) & 0x3f]);
108 DPUTC(d, encodemap[(acc >> 0) & 0x3f]);
110 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
111 dstr_puts(d, ctx->indent);
122 unsigned long acc = ctx->acc;
123 unsigned qsz = ctx->qsz;
130 DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
131 DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
138 DPUTC(d, encodemap[(acc >> 18) & 0x3f]);
139 DPUTC(d, encodemap[(acc >> 12) & 0x3f]);
140 DPUTC(d, encodemap[(acc >> 6) & 0x3f]);
150 /* --- @base64_decode@ --- *
152 * Arguments: @base64_ctx *ctx@ = pointer to a context block
153 * @const void *p@ = pointer to a source buffer
154 * @size_t sz@ = size of the source buffer
155 * @dstr *d@ = pointer to destination string
159 * Use: Decodes a binary string in base64. To flush out the final
160 * few characters (if necessary), pass a null source pointer.
163 void base64_decode(base64_ctx *ctx,
164 const void *p, size_t sz,
168 unsigned long acc = ctx->acc;
169 unsigned qsz = ctx->qsz;
175 /* --- Get the next character and convert it --- */
178 if (ch >= 128 || ch < 0)
186 /* --- Bung it in the accumulator --- */
188 acc = (acc << 6) | ch;
191 /* --- Maybe write out a completed triplet --- */
194 DPUTC(d, (acc >> 16) & 0xff);
195 DPUTC(d, (acc >> 8) & 0xff);
196 DPUTC(d, (acc >> 0) & 0xff);
206 /* --- Notes about the tail-end bits --- *
208 * Ending Base64 decoding is messy. The reference I'm using to define
209 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
210 * do at the end. It explains that I'm meant to ignore spurious `='
211 * characters, and points out that I'm not guaranteed to see anything
212 * interesting at the end. I'll play safe here, and ignore all `='
213 * characters, relying on my client to work out when to stop feeding me
214 * data. I'll use the queue size to work out how many tail-end bytes
218 unsigned long acc = ctx->acc;
219 unsigned qsz = ctx->qsz;
221 /* --- Now fiddle with everything else --- *
223 * There's a bodge here for invalid encodings which have only one hextet
224 * in the final group. I'm not sure this is really worth having, but it
225 * might save some unexpected behaviour. (Not that you won't still get
226 * unexpected behaviour if the stream is completely empty, of course.)
230 acc <<= 6 * (4 - qsz);
235 DPUTC(d, (acc >> 16) & 0xff);
241 /* --- That seems to be good enough --- */
248 /* --- @base64_init@ --- *
250 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
254 * Use: Initializes a base64 context properly.
257 void base64_init(base64_ctx *ctx)
266 /*----- Test driver code --------------------------------------------------*/
270 int main(int argc, char *argv[])
272 unsigned char buf[BUFSIZ];
275 void (*proc)(base64_ctx *, const unsigned char *, size_t, dstr *);
280 if (argc > 1 && strcmp(argv[1], "-d") == 0)
281 proc = base64_decode;
283 proc = base64_encode;
290 sz = fread(buf, 1, sizeof(buf), stdin);
292 proc(&ctx, buf, sz, &d);
293 dstr_write(&d, stdout);
296 } while (sz == sizeof(buf));
298 proc(&ctx, 0, 0, &d);
299 dstr_write(&d, stdout);
301 if (proc == base64_encode)
309 /*----- That's all, folks -------------------------------------------------*/