0af744c218634393c48710380ef28b98f23b5651
3 * $Id: base64.c,v 1.6 2003/05/16 00:22:58 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.6 2003/05/16 00:22:58 mdw
34 * Test base64 and hex encoding.
36 * Revision 1.5 2002/01/13 13:26:16 mdw
37 * Change names for internal tables.
39 * Revision 1.4 1999/10/15 21:08:46 mdw
40 * Change support for erroneous Base64 streams with length 1 mod 4.
42 * Revision 1.3 1999/05/21 22:14:30 mdw
43 * Take advantage of the new dynamic string macros.
45 * Revision 1.2 1999/05/18 21:45:27 mdw
46 * Allow Base64 encode and decode of arbitrary rubbish.
48 * Revision 1.1 1999/05/17 20:35:00 mdw
49 * Base64 encoding and decoding support.
53 /*----- Header files ------------------------------------------------------*/
62 /*----- Important tables --------------------------------------------------*/
64 static const char encodemap
[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65 "abcdefghijklmnopqrstuvwxyz"
68 static const signed char decodemap
[] = {
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 2x */
72 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */
73 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
74 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
75 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */
76 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */
79 /*----- Main code ---------------------------------------------------------*/
81 /* --- @base64_encode@ --- *
83 * Arguments: @base64_ctx *ctx@ = pointer to a context block
84 * @const void *p@ = pointer to a source buffer
85 * @size_t sz@ = size of the source buffer
86 * @dstr *d@ = pointer to destination string
90 * Use: Encodes a binary string in base64. To flush out the final
91 * few characters (if necessary), pass a null source pointer.
94 void base64_encode(base64_ctx
*ctx
,
95 const void *p
, size_t sz
,
99 unsigned long acc
= ctx
->acc
;
100 unsigned qsz
= ctx
->qsz
;
101 const unsigned char *src
= p
;
104 acc
= (acc
<< 8) | *src
++;
108 DPUTC(d
, encodemap
[(acc
>> 18) & 0x3f]);
109 DPUTC(d
, encodemap
[(acc
>> 12) & 0x3f]);
110 DPUTC(d
, encodemap
[(acc
>> 6) & 0x3f]);
111 DPUTC(d
, encodemap
[(acc
>> 0) & 0x3f]);
113 if (ctx
->maxline
&& ctx
->lnlen
>= ctx
->maxline
) {
114 dstr_puts(d
, ctx
->indent
);
125 unsigned long acc
= ctx
->acc
;
126 unsigned qsz
= ctx
->qsz
;
133 DPUTC(d
, encodemap
[(acc
>> 18) & 0x3f]);
134 DPUTC(d
, encodemap
[(acc
>> 12) & 0x3f]);
141 DPUTC(d
, encodemap
[(acc
>> 18) & 0x3f]);
142 DPUTC(d
, encodemap
[(acc
>> 12) & 0x3f]);
143 DPUTC(d
, encodemap
[(acc
>> 6) & 0x3f]);
153 /* --- @base64_decode@ --- *
155 * Arguments: @base64_ctx *ctx@ = pointer to a context block
156 * @const void *p@ = pointer to a source buffer
157 * @size_t sz@ = size of the source buffer
158 * @dstr *d@ = pointer to destination string
162 * Use: Decodes a binary string in base64. To flush out the final
163 * few characters (if necessary), pass a null source pointer.
166 void base64_decode(base64_ctx
*ctx
,
167 const void *p
, size_t sz
,
171 unsigned long acc
= ctx
->acc
;
172 unsigned qsz
= ctx
->qsz
;
178 /* --- Get the next character and convert it --- */
181 if (ch
>= 128 || ch
< 0)
189 /* --- Bung it in the accumulator --- */
191 acc
= (acc
<< 6) | ch
;
194 /* --- Maybe write out a completed triplet --- */
197 DPUTC(d
, (acc
>> 16) & 0xff);
198 DPUTC(d
, (acc
>> 8) & 0xff);
199 DPUTC(d
, (acc
>> 0) & 0xff);
209 /* --- Notes about the tail-end bits --- *
211 * Ending Base64 decoding is messy. The reference I'm using to define
212 * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to
213 * do at the end. It explains that I'm meant to ignore spurious `='
214 * characters, and points out that I'm not guaranteed to see anything
215 * interesting at the end. I'll play safe here, and ignore all `='
216 * characters, relying on my client to work out when to stop feeding me
217 * data. I'll use the queue size to work out how many tail-end bytes
221 unsigned long acc
= ctx
->acc
;
222 unsigned qsz
= ctx
->qsz
;
224 /* --- Now fiddle with everything else --- *
226 * There's a bodge here for invalid encodings which have only one hextet
227 * in the final group. I'm not sure this is really worth having, but it
228 * might save some unexpected behaviour. (Not that you won't still get
229 * unexpected behaviour if the stream is completely empty, of course.)
233 acc
<<= 6 * (4 - qsz
);
238 DPUTC(d
, (acc
>> 16) & 0xff);
244 /* --- That seems to be good enough --- */
251 /* --- @base64_init@ --- *
253 * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize
257 * Use: Initializes a base64 context properly.
260 void base64_init(base64_ctx
*ctx
)
269 /*----- Test driver code --------------------------------------------------*/
273 int main(int argc
, char *argv
[])
275 unsigned char buf
[BUFSIZ
];
278 void (*proc
)(base64_ctx
*, const void *, size_t, dstr
*);
283 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
284 proc
= base64_decode
;
286 proc
= base64_encode
;
293 sz
= fread(buf
, 1, sizeof(buf
), stdin
);
295 proc(&ctx
, buf
, sz
, &d
);
296 dstr_write(&d
, stdout
);
299 } while (sz
== sizeof(buf
));
301 proc(&ctx
, 0, 0, &d
);
302 dstr_write(&d
, stdout
);
304 if (proc
== base64_encode
)
312 /*----- That's all, folks -------------------------------------------------*/