3 * $Id: base64.c,v 1.7 2004/04/08 01:36:11 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 /*----- Header files ------------------------------------------------------*/
39 /*----- Important tables --------------------------------------------------*/
41 static const char encodemap
[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "abcdefghijklmnopqrstuvwxyz"
45 static const signed char decodemap
[] = {
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
49 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
50 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
51 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
52 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
53 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @base64_encode@ --- *
60 * Arguments: @base64_ctx *ctx@ = pointer to a context block
61 * @const void *p@ = pointer to a source buffer
62 * @size_t sz@ = size of the source buffer
63 * @dstr *d@ = pointer to destination string
67 * Use: Encodes a binary string in base64. To flush out the final
68 * few characters (if necessary), pass a null source pointer.
71 void base64_encode(base64_ctx
*ctx
,
72 const void *p
, size_t sz
,
76 unsigned long acc
= ctx
->acc
;
77 unsigned qsz
= ctx
->qsz
;
78 const unsigned char *src
= p
;
81 acc
= (acc
<< 8) | *src
++;
85 DPUTC(d
, encodemap
[(acc
>> 18) & 0x3f]);
86 DPUTC(d
, encodemap
[(acc
>> 12) & 0x3f]);
87 DPUTC(d
, encodemap
[(acc
>> 6) & 0x3f]);
88 DPUTC(d
, encodemap
[(acc
>> 0) & 0x3f]);
90 if (ctx
->maxline
&& ctx
->lnlen
>= ctx
->maxline
) {
91 dstr_puts(d
, ctx
->indent
);
102 unsigned long acc
= ctx
->acc
;
103 unsigned qsz
= ctx
->qsz
;
110 DPUTC(d
, encodemap
[(acc
>> 18) & 0x3f]);
111 DPUTC(d
, encodemap
[(acc
>> 12) & 0x3f]);
118 DPUTC(d
, encodemap
[(acc
>> 18) & 0x3f]);
119 DPUTC(d
, encodemap
[(acc
>> 12) & 0x3f]);
120 DPUTC(d
, encodemap
[(acc
>> 6) & 0x3f]);
130 /* --- @base64_decode@ --- *
132 * Arguments: @base64_ctx *ctx@ = pointer to a context block
133 * @const void *p@ = pointer to a source buffer
134 * @size_t sz@ = size of the source buffer
135 * @dstr *d@ = pointer to destination string
139 * Use: Decodes a binary string in base64. To flush out the final
140 * few characters (if necessary), pass a null source pointer.
143 void base64_decode(base64_ctx
*ctx
,
144 const void *p
, size_t sz
,
148 unsigned long acc
= ctx
->acc
;
149 unsigned qsz
= ctx
->qsz
;
155 /* --- Get the next character and convert it --- */
158 if (ch
>= 128 || ch
< 0)
166 /* --- Bung it in the accumulator --- */
168 acc
= (acc
<< 6) | ch
;
171 /* --- Maybe write out a completed triplet --- */
174 DPUTC(d
, (acc
>> 16) & 0xff);
175 DPUTC(d
, (acc
>> 8) & 0xff);
176 DPUTC(d
, (acc
>> 0) & 0xff);
186 /* --- Notes about the tail-end bits --- *
188 * Ending Base64 decoding is messy. The reference I'm using to define
189 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
190 * do at the end. It explains that I'm meant to ignore spurious `='
191 * characters, and points out that I'm not guaranteed to see anything
192 * interesting at the end. I'll play safe here, and ignore all `='
193 * characters, relying on my client to work out when to stop feeding me
194 * data. I'll use the queue size to work out how many tail-end bytes
198 unsigned long acc
= ctx
->acc
;
199 unsigned qsz
= ctx
->qsz
;
201 /* --- Now fiddle with everything else --- *
203 * There's a bodge here for invalid encodings which have only one hextet
204 * in the final group. I'm not sure this is really worth having, but it
205 * might save some unexpected behaviour. (Not that you won't still get
206 * unexpected behaviour if the stream is completely empty, of course.)
210 acc
<<= 6 * (4 - qsz
);
215 DPUTC(d
, (acc
>> 16) & 0xff);
221 /* --- That seems to be good enough --- */
228 /* --- @base64_init@ --- *
230 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
234 * Use: Initializes a base64 context properly.
237 void base64_init(base64_ctx
*ctx
)
246 /*----- Test driver code --------------------------------------------------*/
250 int main(int argc
, char *argv
[])
252 unsigned char buf
[BUFSIZ
];
255 void (*proc
)(base64_ctx
*, const void *, size_t, dstr
*);
260 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
261 proc
= base64_decode
;
263 proc
= base64_encode
;
270 sz
= fread(buf
, 1, sizeof(buf
), stdin
);
272 proc(&ctx
, buf
, sz
, &d
);
273 dstr_write(&d
, stdout
);
276 } while (sz
== sizeof(buf
));
278 proc(&ctx
, 0, 0, &d
);
279 dstr_write(&d
, stdout
);
281 if (proc
== base64_encode
)
289 /*----- That's all, folks -------------------------------------------------*/