1 HEADER #include <stdlib.h>
3 static char b64_encchr(unsigned char c) {
4 if (c<26) return 'A'+c; c-=26;
5 if (c<26) return 'a'+c; c-=26;
6 if (c<10) return '0'+c; c-=10;
11 // Encodes `len` bytes from `src` into `dst`. `dst` must be large enough to
12 // hold the base64-encoded form of the input.
13 PUBLIC_FN void base64_encode(char *dst, unsigned char *src, size_t len) {
15 if (len == 0) { *dst = '\0'; return; }
16 unsigned char a=src[0], b=0, c=0;
18 int eqsigns = (len<2)?(2-len):0;
19 if (len) b=src[0], len--, src++;
20 if (len) c=src[0], len--, src++;
22 unsigned char k = a>>2;
23 unsigned char l = ((a&3)<<4) |(b>>4);
24 unsigned char m = ((b&15)<<2)|(c>>6);
25 unsigned char n = c&63;
27 *(dst++) = b64_encchr(k);
28 *(dst++) = b64_encchr(l);
29 *(dst++) = (eqsigns==2)?'=':b64_encchr(m);
30 *(dst++) = eqsigns?'=':b64_encchr(n);