1 /* $Cambridge: exim/src/src/auths/b64encode.c,v 1.3 2006/02/07 11:19:01 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2006 */
8 /* See the file NOTICE for conditions of use and distribution. */
13 /*************************************************
14 * Encode byte-string in base 64 *
15 *************************************************/
17 /* This function encodes a string of bytes, containing any values whatsoever,
18 in base 64 as defined in RFC 2045 (MIME) and required by the SMTP AUTH
19 extension (RFC 2554). The encoding algorithm is written out in a
20 straightforward way. Turning it into some kind of compact loop is messy and
21 would probably run more slowly.
24 clear points to the clear text bytes
25 len the number of bytes to encode
27 Returns: a pointer to the zero-terminated base 64 string, which
31 static uschar *enc64table =
32 US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
35 auth_b64encode(uschar *clear, int len)
37 uschar *code = store_get(4*((len+2)/3) + 1);
45 *p++ = enc64table[(x >> 2) & 63];
49 *p++ = enc64table[(x << 4) & 63];
56 *p++ = enc64table[((x << 4) | ((y >> 4) & 15)) & 63];
60 *p++ = enc64table[(y << 2) & 63];
66 *p++ = enc64table[((y << 2) | ((x >> 6) & 3)) & 63];
68 *p++ = enc64table[x & 63];
76 /* End of b64encode.c */