3 * $Id: base64.c,v 1.4 1999/10/15 21:08:46 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.4 1999/10/15 21:08:46 mdw
34 * Change support for erroneous Base64 streams with length 1 mod 4.
36 * Revision 1.3 1999/05/21 22:14:30 mdw
37 * Take advantage of the new dynamic string macros.
39 * Revision 1.2 1999/05/18 21:45:27 mdw
40 * Allow Base64 encode and decode of arbitrary rubbish.
42 * Revision 1.1 1999/05/17 20:35:00 mdw
43 * Base64 encoding and decoding support.
47 /*----- Header files ------------------------------------------------------*/
56 /*----- Important tables --------------------------------------------------*/
58 static const char base64_encodeMap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
59 "abcdefghijklmnopqrstuvwxyz"
62 static const signed char base64_decodeMap[] = {
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
66 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
67 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
68 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
69 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
70 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- @base64_encode@ --- *
77 * Arguments: @base64_ctx *ctx@ = pointer to a context block
78 * @const void *p@ = pointer to a source buffer
79 * @size_t sz@ = size of the source buffer
80 * @dstr *d@ = pointer to destination string
84 * Use: Encodes a binary string in base64. To flush out the final
85 * few characters (if necessary), pass a null source pointer.
88 void base64_encode(base64_ctx *ctx,
89 const void *p, size_t sz,
93 unsigned long acc = ctx->acc;
94 unsigned qsz = ctx->qsz;
95 const unsigned char *src = p;
98 acc = (acc << 8) | *src++;
102 DPUTC(d, base64_encodeMap[(acc >> 18) & 0x3f]);
103 DPUTC(d, base64_encodeMap[(acc >> 12) & 0x3f]);
104 DPUTC(d, base64_encodeMap[(acc >> 6) & 0x3f]);
105 DPUTC(d, base64_encodeMap[(acc >> 0) & 0x3f]);
107 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
108 dstr_puts(d, ctx->indent);
119 unsigned long acc = ctx->acc;
120 unsigned qsz = ctx->qsz;
127 DPUTC(d, base64_encodeMap[(acc >> 18) & 0x3f]);
128 DPUTC(d, base64_encodeMap[(acc >> 12) & 0x3f]);
135 DPUTC(d, base64_encodeMap[(acc >> 18) & 0x3f]);
136 DPUTC(d, base64_encodeMap[(acc >> 12) & 0x3f]);
137 DPUTC(d, base64_encodeMap[(acc >> 6) & 0x3f]);
147 /* --- @base64_decode@ --- *
149 * Arguments: @base64_ctx *ctx@ = pointer to a context block
150 * @const void *p@ = pointer to a source buffer
151 * @size_t sz@ = size of the source buffer
152 * @dstr *d@ = pointer to destination string
156 * Use: Decodes a binary string in base64. To flush out the final
157 * few characters (if necessary), pass a null source pointer.
160 void base64_decode(base64_ctx *ctx,
161 const void *p, size_t sz,
165 unsigned long acc = ctx->acc;
166 unsigned qsz = ctx->qsz;
172 /* --- Get the next character and convert it --- */
175 if (ch >= 128 || ch < 0)
178 ch = base64_decodeMap[ch];
183 /* --- Bung it in the accumulator --- */
185 acc = (acc << 6) | ch;
188 /* --- Maybe write out a completed triplet --- */
191 DPUTC(d, (acc >> 16) & 0xff);
192 DPUTC(d, (acc >> 8) & 0xff);
193 DPUTC(d, (acc >> 0) & 0xff);
203 /* --- Notes about the tail-end bits --- *
205 * Ending Base64 decoding is messy. The reference I'm using to define
206 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
207 * do at the end. It explains that I'm meant to ignore spurious `='
208 * characters, and points out that I'm not guaranteed to see anything
209 * interesting at the end. I'll play safe here, and ignore all `='
210 * characters, relying on my client to work out when to stop feeding me
211 * data. I'll use the queue size to work out how many tail-end bytes
215 unsigned long acc = ctx->acc;
216 unsigned qsz = ctx->qsz;
218 /* --- Now fiddle with everything else --- *
220 * There's a bodge here for invalid encodings which have only one hextet
221 * in the final group. I'm not sure this is really worth having, but it
222 * might save some unexpected behaviour. (Not that you won't still get
223 * unexpected behaviour if the stream is completely empty, of course.)
227 acc <<= 6 * (4 - qsz);
232 DPUTC(d, (acc >> 16) & 0xff);
238 /* --- That seems to be good enough --- */
245 /* --- @base64_init@ --- *
247 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
251 * Use: Initializes a base64 context properly.
254 void base64_init(base64_ctx *ctx)
263 /*----- Test driver code --------------------------------------------------*/
267 int main(int argc, char *argv[])
269 unsigned char buf[BUFSIZ];
272 void (*proc)(base64_ctx *, const unsigned char *, size_t, dstr *);
277 if (argc > 1 && strcmp(argv[1], "-d") == 0)
278 proc = base64_decode;
280 proc = base64_encode;
287 sz = fread(buf, 1, sizeof(buf), stdin);
289 proc(&ctx, buf, sz, &d);
290 dstr_write(&d, stdout);
293 } while (sz == sizeof(buf));
295 proc(&ctx, 0, 0, &d);
296 dstr_write(&d, stdout);
298 if (proc == base64_encode)
306 /*----- That's all, folks -------------------------------------------------*/