Commit | Line | Data |
---|---|---|
c988f1f4 | 1 | /* $Cambridge: exim/src/src/auths/b64encode.c,v 1.2 2005/01/04 10:00:43 ph10 Exp $ */ |
0756eb3c PH |
2 | |
3 | /************************************************* | |
4 | * Exim - an Internet mail transport agent * | |
5 | *************************************************/ | |
6 | ||
c988f1f4 | 7 | /* Copyright (c) University of Cambridge 1995 - 2005 */ |
0756eb3c PH |
8 | /* See the file NOTICE for conditions of use and distribution. */ |
9 | ||
10 | #include "../exim.h" | |
11 | ||
12 | ||
13 | /************************************************* | |
14 | * Encode byte-string in base 64 * | |
15 | *************************************************/ | |
16 | ||
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. | |
22 | ||
23 | Arguments: | |
24 | clear points to the clear text bytes | |
25 | len the number of bytes to encode | |
26 | ||
27 | Returns: a pointer to the zero-terminated base 64 string, which | |
28 | is in working store | |
29 | */ | |
30 | ||
31 | static uschar *enc64table = | |
32 | US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
33 | ||
34 | uschar * | |
35 | auth_b64encode(uschar *clear, int len) | |
36 | { | |
37 | uschar *code = store_get(4*((len+2)/3) + 1); | |
38 | uschar *p = code; | |
39 | ||
40 | while (len-- >0) | |
41 | { | |
42 | register int x, y; | |
43 | ||
44 | x = *clear++; | |
45 | *p++ = enc64table[(x >> 2) & 63]; | |
46 | ||
47 | if (len-- <= 0) | |
48 | { | |
49 | *p++ = enc64table[(x << 4) & 63]; | |
50 | *p++ = '='; | |
51 | *p++ = '='; | |
52 | break; | |
53 | } | |
54 | ||
55 | y = *clear++; | |
56 | *p++ = enc64table[((x << 4) | ((y >> 4) & 15)) & 63]; | |
57 | ||
58 | if (len-- <= 0) | |
59 | { | |
60 | *p++ = enc64table[(y << 2) & 63]; | |
61 | *p++ = '='; | |
62 | break; | |
63 | } | |
64 | ||
65 | x = *clear++; | |
66 | *p++ = enc64table[((y << 2) | ((x >> 6) & 3)) & 63]; | |
67 | ||
68 | *p++ = enc64table[x & 63]; | |
69 | } | |
70 | ||
71 | *p = 0; | |
72 | ||
73 | return code; | |
74 | } | |
75 | ||
76 | /* End of b64encode.c */ |