3 * $Id: base64.c,v 1.1 1999/05/17 20:35:00 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.1 1999/05/17 20:35:00 mdw
34 * Base64 encoding and decoding support.
38 /*----- Header files ------------------------------------------------------*/
47 /*----- Important tables --------------------------------------------------*/
49 static const char base64_encodeMap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
50 "abcdefghijklmnopqrstuvwxyz"
53 static const signed char base64_decodeMap[] = {
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
57 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
58 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
59 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
60 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
61 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @base64_encode@ --- *
68 * Arguments: @base64_ctx *ctx@ = pointer to a context block
69 * @const unsigned char *src@ = pointer to a source buffer
70 * @size_t sz@ = size of the source buffer
71 * @dstr *d@ = pointer to destination string
75 * Use: Encodes a binary string in base64. To flush out the final
76 * few characters (if necessary), pass a null source pointer.
79 void base64_encode(base64_ctx *ctx,
80 const unsigned char *src, size_t sz,
84 unsigned long acc = ctx->acc;
85 unsigned qsz = ctx->qsz;
88 acc = (acc << 8) | *src++;
92 dstr_putc(d, base64_encodeMap[(acc >> 18) & 0x3f]);
93 dstr_putc(d, base64_encodeMap[(acc >> 12) & 0x3f]);
94 dstr_putc(d, base64_encodeMap[(acc >> 6) & 0x3f]);
95 dstr_putc(d, base64_encodeMap[(acc >> 0) & 0x3f]);
97 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
98 dstr_puts(d, ctx->indent);
109 unsigned long acc = ctx->acc;
110 unsigned qsz = ctx->qsz;
117 dstr_putc(d, base64_encodeMap[(acc >> 18) & 0x3f]);
118 dstr_putc(d, base64_encodeMap[(acc >> 12) & 0x3f]);
125 dstr_putc(d, base64_encodeMap[(acc >> 18) & 0x3f]);
126 dstr_putc(d, base64_encodeMap[(acc >> 12) & 0x3f]);
127 dstr_putc(d, base64_encodeMap[(acc >> 6) & 0x3f]);
137 /* --- @base64_decode@ --- *
139 * Arguments: @base64_ctx *ctx@ = pointer to a context block
140 * @const unsigned char *src@ = pointer to a source buffer
141 * @size_t sz@ = size of the source buffer
142 * @dstr *d@ = pointer to destination string
146 * Use: Decodes a binary string in base64. To flush out the final
147 * few characters (if necessary), pass a null source pointer.
150 void base64_decode(base64_ctx *ctx,
151 const unsigned char *src, size_t sz,
155 unsigned long acc = ctx->acc;
156 unsigned qsz = ctx->qsz;
161 /* --- Get the next character and convert it --- */
164 if (ch >= 128 || ch < 0)
167 ch = base64_decodeMap[ch];
172 /* --- Bung it in the accumulator --- */
174 acc = (acc << 6) | ch;
177 /* --- Maybe write out a completed triplet --- */
180 dstr_putc(d, (acc >> 16) & 0xff);
181 dstr_putc(d, (acc >> 8) & 0xff);
182 dstr_putc(d, (acc >> 0) & 0xff);
192 /* --- Notes about the tail-end bits --- *
194 * Ending Base64 decoding is messy. The reference I'm using to define
195 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
196 * do at the end. It explains that I'm meant to ignore spurious `='
197 * characters, and points out that I'm not guaranteed to see anything
198 * interesting at the end. I'll play safe here, and ignore all `='
199 * characters, relying on my client to work out when to stop feeding me
200 * data. I'll use the queue size to work out how many tail-end bytes
204 unsigned long acc = ctx->acc;
205 unsigned qsz = ctx->qsz;
207 /* --- Now fiddle with everything else --- */
209 acc <<= 6 * (4 - qsz);
212 dstr_putc(d, (acc >> 16) & 0xff);
217 /* --- That seems to be good enough --- */
224 /* --- @base64_init@ --- *
226 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
230 * Use: Initializes a base64 context properly.
233 void base64_init(base64_ctx *ctx)
242 /*----- Test driver code --------------------------------------------------*/
246 int main(int argc, char *argv[])
248 unsigned char buf[BUFSIZ];
251 void (*proc)(base64_ctx *, const unsigned char *, size_t, dstr *);
257 if (argc > 1 && strcmp(argv[1], "-d") == 0)
258 proc = base64_decode;
260 proc = base64_encode;
267 sz = fread(buf, 1, sizeof(buf), stdin);
269 proc(&ctx, buf, sz, &d);
270 dstr_write(&d, stdout);
273 } while (sz == sizeof(buf));
275 proc(&ctx, 0, 0, &d);
276 dstr_write(&d, stdout);
278 if (proc == base64_encode)
286 /*----- That's all, folks -------------------------------------------------*/