3 * $Id: base64.c,v 1.2 1999/05/18 21:45:27 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.2 1999/05/18 21:45:27 mdw
34 * Allow Base64 encode and decode of arbitrary rubbish.
36 * Revision 1.1 1999/05/17 20:35:00 mdw
37 * Base64 encoding and decoding support.
41 /*----- Header files ------------------------------------------------------*/
50 /*----- Important tables --------------------------------------------------*/
52 static const char base64_encodeMap
[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53 "abcdefghijklmnopqrstuvwxyz"
56 static const signed char base64_decodeMap
[] = {
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
60 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
61 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
62 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
63 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
64 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @base64_encode@ --- *
71 * Arguments: @base64_ctx *ctx@ = pointer to a context block
72 * @const void *p@ = pointer to a source buffer
73 * @size_t sz@ = size of the source buffer
74 * @dstr *d@ = pointer to destination string
78 * Use: Encodes a binary string in base64. To flush out the final
79 * few characters (if necessary), pass a null source pointer.
82 void base64_encode(base64_ctx
*ctx
,
83 const void *p
, size_t sz
,
87 unsigned long acc
= ctx
->acc
;
88 unsigned qsz
= ctx
->qsz
;
89 const unsigned char *src
= p
;
92 acc
= (acc
<< 8) | *src
++;
96 DPUTC(d
, base64_encodeMap
[(acc
>> 18) & 0x3f]);
97 DPUTC(d
, base64_encodeMap
[(acc
>> 12) & 0x3f]);
98 DPUTC(d
, base64_encodeMap
[(acc
>> 6) & 0x3f]);
99 DPUTC(d
, base64_encodeMap
[(acc
>> 0) & 0x3f]);
101 if (ctx
->maxline
&& ctx
->lnlen
>= ctx
->maxline
) {
102 dstr_puts(d
, ctx
->indent
);
113 unsigned long acc
= ctx
->acc
;
114 unsigned qsz
= ctx
->qsz
;
121 DPUTC(d
, base64_encodeMap
[(acc
>> 18) & 0x3f]);
122 DPUTC(d
, base64_encodeMap
[(acc
>> 12) & 0x3f]);
129 DPUTC(d
, base64_encodeMap
[(acc
>> 18) & 0x3f]);
130 DPUTC(d
, base64_encodeMap
[(acc
>> 12) & 0x3f]);
131 DPUTC(d
, base64_encodeMap
[(acc
>> 6) & 0x3f]);
141 /* --- @base64_decode@ --- *
143 * Arguments: @base64_ctx *ctx@ = pointer to a context block
144 * @const void *p@ = pointer to a source buffer
145 * @size_t sz@ = size of the source buffer
146 * @dstr *d@ = pointer to destination string
150 * Use: Decodes a binary string in base64. To flush out the final
151 * few characters (if necessary), pass a null source pointer.
154 void base64_decode(base64_ctx
*ctx
,
155 const void *p
, size_t sz
,
159 unsigned long acc
= ctx
->acc
;
160 unsigned qsz
= ctx
->qsz
;
166 /* --- Get the next character and convert it --- */
169 if (ch
>= 128 || ch
< 0)
172 ch
= base64_decodeMap
[ch
];
177 /* --- Bung it in the accumulator --- */
179 acc
= (acc
<< 6) | ch
;
182 /* --- Maybe write out a completed triplet --- */
185 DPUTC(d
, (acc
>> 16) & 0xff);
186 DPUTC(d
, (acc
>> 8) & 0xff);
187 DPUTC(d
, (acc
>> 0) & 0xff);
197 /* --- Notes about the tail-end bits --- *
199 * Ending Base64 decoding is messy. The reference I'm using to define
200 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
201 * do at the end. It explains that I'm meant to ignore spurious `='
202 * characters, and points out that I'm not guaranteed to see anything
203 * interesting at the end. I'll play safe here, and ignore all `='
204 * characters, relying on my client to work out when to stop feeding me
205 * data. I'll use the queue size to work out how many tail-end bytes
209 unsigned long acc
= ctx
->acc
;
210 unsigned qsz
= ctx
->qsz
;
212 /* --- Now fiddle with everything else --- */
214 acc
<<= 6 * (4 - qsz
);
217 DPUTC(d
, (acc
>> 16) & 0xff);
222 /* --- That seems to be good enough --- */
229 /* --- @base64_init@ --- *
231 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
235 * Use: Initializes a base64 context properly.
238 void base64_init(base64_ctx
*ctx
)
247 /*----- Test driver code --------------------------------------------------*/
251 int main(int argc
, char *argv
[])
253 unsigned char buf
[BUFSIZ
];
256 void (*proc
)(base64_ctx
*, const unsigned char *, size_t, dstr
*);
262 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
263 proc
= base64_decode
;
265 proc
= base64_encode
;
272 sz
= fread(buf
, 1, sizeof(buf
), stdin
);
274 proc(&ctx
, buf
, sz
, &d
);
275 dstr_write(&d
, stdout
);
278 } while (sz
== sizeof(buf
));
280 proc(&ctx
, 0, 0, &d
);
281 dstr_write(&d
, stdout
);
283 if (proc
== base64_encode
)
291 /*----- That's all, folks -------------------------------------------------*/