1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
11 /*************************************************
12 * Encode byte-string in base 64 *
13 *************************************************/
15 /* This function encodes a string of bytes, containing any values whatsoever,
16 in base 64 as defined in RFC 2045 (MIME) and required by the SMTP AUTH
17 extension (RFC 2554). The encoding algorithm is written out in a
18 straightforward way. Turning it into some kind of compact loop is messy and
19 would probably run more slowly.
22 clear points to the clear text bytes
23 len the number of bytes to encode
25 Returns: a pointer to the zero-terminated base 64 string, which
29 static uschar *enc64table =
30 US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 auth_b64encode(uschar *clear, int len)
35 uschar *code = store_get(4*((len+2)/3) + 1);
43 *p++ = enc64table[(x >> 2) & 63];
47 *p++ = enc64table[(x << 4) & 63];
54 *p++ = enc64table[((x << 4) | ((y >> 4) & 15)) & 63];
58 *p++ = enc64table[(y << 2) & 63];
64 *p++ = enc64table[((y << 2) | ((x >> 6) & 3)) & 63];
66 *p++ = enc64table[x & 63];
74 /* End of b64encode.c */