3 * $Id: base64.c,v 1.3 1999/05/21 22:14:30 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.3 1999/05/21 22:14:30 mdw
34 * Take advantage of the new dynamic string macros.
36 * Revision 1.2 1999/05/18 21:45:27 mdw
37 * Allow Base64 encode and decode of arbitrary rubbish.
39 * Revision 1.1 1999/05/17 20:35:00 mdw
40 * Base64 encoding and decoding support.
44 /*----- Header files ------------------------------------------------------*/
53 /*----- Important tables --------------------------------------------------*/
55 static const char base64_encodeMap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
56 "abcdefghijklmnopqrstuvwxyz"
59 static const signed char base64_decodeMap[] = {
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
63 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
64 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
65 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
66 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
67 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
70 /*----- Main code ---------------------------------------------------------*/
72 /* --- @base64_encode@ --- *
74 * Arguments: @base64_ctx *ctx@ = pointer to a context block
75 * @const void *p@ = pointer to a source buffer
76 * @size_t sz@ = size of the source buffer
77 * @dstr *d@ = pointer to destination string
81 * Use: Encodes a binary string in base64. To flush out the final
82 * few characters (if necessary), pass a null source pointer.
85 void base64_encode(base64_ctx *ctx,
86 const void *p, size_t sz,
90 unsigned long acc = ctx->acc;
91 unsigned qsz = ctx->qsz;
92 const unsigned char *src = p;
95 acc = (acc << 8) | *src++;
99 DPUTC(d, base64_encodeMap[(acc >> 18) & 0x3f]);
100 DPUTC(d, base64_encodeMap[(acc >> 12) & 0x3f]);
101 DPUTC(d, base64_encodeMap[(acc >> 6) & 0x3f]);
102 DPUTC(d, base64_encodeMap[(acc >> 0) & 0x3f]);
104 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
105 dstr_puts(d, ctx->indent);
116 unsigned long acc = ctx->acc;
117 unsigned qsz = ctx->qsz;
124 DPUTC(d, base64_encodeMap[(acc >> 18) & 0x3f]);
125 DPUTC(d, base64_encodeMap[(acc >> 12) & 0x3f]);
132 DPUTC(d, base64_encodeMap[(acc >> 18) & 0x3f]);
133 DPUTC(d, base64_encodeMap[(acc >> 12) & 0x3f]);
134 DPUTC(d, base64_encodeMap[(acc >> 6) & 0x3f]);
144 /* --- @base64_decode@ --- *
146 * Arguments: @base64_ctx *ctx@ = pointer to a context block
147 * @const void *p@ = pointer to a source buffer
148 * @size_t sz@ = size of the source buffer
149 * @dstr *d@ = pointer to destination string
153 * Use: Decodes a binary string in base64. To flush out the final
154 * few characters (if necessary), pass a null source pointer.
157 void base64_decode(base64_ctx *ctx,
158 const void *p, size_t sz,
162 unsigned long acc = ctx->acc;
163 unsigned qsz = ctx->qsz;
169 /* --- Get the next character and convert it --- */
172 if (ch >= 128 || ch < 0)
175 ch = base64_decodeMap[ch];
180 /* --- Bung it in the accumulator --- */
182 acc = (acc << 6) | ch;
185 /* --- Maybe write out a completed triplet --- */
188 DPUTC(d, (acc >> 16) & 0xff);
189 DPUTC(d, (acc >> 8) & 0xff);
190 DPUTC(d, (acc >> 0) & 0xff);
200 /* --- Notes about the tail-end bits --- *
202 * Ending Base64 decoding is messy. The reference I'm using to define
203 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
204 * do at the end. It explains that I'm meant to ignore spurious `='
205 * characters, and points out that I'm not guaranteed to see anything
206 * interesting at the end. I'll play safe here, and ignore all `='
207 * characters, relying on my client to work out when to stop feeding me
208 * data. I'll use the queue size to work out how many tail-end bytes
212 unsigned long acc = ctx->acc;
213 unsigned qsz = ctx->qsz;
215 /* --- Now fiddle with everything else --- */
217 acc <<= 6 * (4 - qsz);
220 DPUTC(d, (acc >> 16) & 0xff);
225 /* --- That seems to be good enough --- */
232 /* --- @base64_init@ --- *
234 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
238 * Use: Initializes a base64 context properly.
241 void base64_init(base64_ctx *ctx)
250 /*----- Test driver code --------------------------------------------------*/
254 int main(int argc, char *argv[])
256 unsigned char buf[BUFSIZ];
259 void (*proc)(base64_ctx *, const unsigned char *, size_t, dstr *);
264 if (argc > 1 && strcmp(argv[1], "-d") == 0)
265 proc = base64_decode;
267 proc = base64_encode;
274 sz = fread(buf, 1, sizeof(buf), stdin);
276 proc(&ctx, buf, sz, &d);
277 dstr_write(&d, stdout);
280 } while (sz == sizeof(buf));
282 proc(&ctx, 0, 0, &d);
283 dstr_write(&d, stdout);
285 if (proc == base64_encode)
293 /*----- That's all, folks -------------------------------------------------*/