2
* This file is part of libESMTP, a library for submission of RFC 2822
3
* formatted electronic mail messages using the SMTP protocol described
6
* Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
/* Routines to encode and decode base64 text.
35
/* RFC 2045 section 6.8 */
37
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38
"abcdefghijklmnopqrstuvwxyz"
42
static const char index_64[128] =
44
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
47
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
48
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
49
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
50
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
51
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
54
/* Decode srclen bytes of base64 data contained in src and put the result
55
in dst. Since the destination buffer may contain arbitrary binary
56
data and it is not necessarily a string, there is no \0 byte at the
57
end of the decoded data. */
59
b64_decode (void *dst, int dstlen, const char *src, int srclen)
61
const unsigned char *p, *q;
65
assert (dst != NULL && dstlen > 0 && src != NULL);
68
srclen = strlen (src);
70
/* Remove leading and trailing white space */
71
for (p = (const unsigned char *) src;
72
srclen > 0 && isspace (*p);
75
for (q = p + srclen - 1; q >= p && isspace (*q); q--, srclen--)
78
/* Length MUST be a multiple of 4 */
82
/* Destination buffer length must be sufficient */
83
if (srclen / 4 * 3 + 1 > dstlen)
90
if (*p >= 128 || (c1 = index_64[*p++]) == -1)
92
if (*p >= 128 || (c2 = index_64[*p++]) == -1)
94
*t++ = (c1 << 2) | ((c2 & 0x30) >> 4);
96
if (p[0] == '=' && p[1] == '=')
98
if (*p >= 128 || (c1 = index_64[*p++]) == -1)
100
*t++ = ((c2 & 0x0f) << 4) | ((c1 & 0x3c) >> 2);
104
if (*p >= 128 || (c2 = index_64[*p++]) == -1)
106
*t++ = ((c1 & 0x03) << 6) | c2;
109
return t - (unsigned char *) dst;
112
/* Return a pointer to a base 64 encoded string. The input data is
113
arbitrary binary data, the output is a \0 terminated string.
114
src and dst may not share the same buffer. */
116
b64_encode (char *dst, int dstlen, const void *src, int srclen)
119
const unsigned char *from;
120
unsigned char c1, c2;
123
assert (dst != NULL && dstlen > 0 && src != NULL && srclen >= 0);
125
dst_needed = (srclen + 2) / 3;
127
if (dstlen < dst_needed + 1)
133
c1 = *from++; srclen--;
134
*to++ = base64[c1 >> 2];
135
c1 = (c1 & 0x03) << 4;
143
c2 = *from++; srclen--;
144
c1 |= (c2 >> 4) & 0x0f;
146
c1 = (c2 & 0x0f) << 2;
153
c2 = *from++; srclen--;
154
c1 |= (c2 >> 6) & 0x03;
156
*to++ = base64[c2 & 0x3f];