Update version number and copyright year.
[exim.git] / src / src / auths / b64encode.c
CommitLineData
184e8823 1/* $Cambridge: exim/src/src/auths/b64encode.c,v 1.4 2007/01/08 10:50:19 ph10 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
184e8823 7/* Copyright (c) University of Cambridge 1995 - 2007 */
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,
18in base 64 as defined in RFC 2045 (MIME) and required by the SMTP AUTH
19extension (RFC 2554). The encoding algorithm is written out in a
20straightforward way. Turning it into some kind of compact loop is messy and
21would probably run more slowly.
22
23Arguments:
24 clear points to the clear text bytes
25 len the number of bytes to encode
26
27Returns: a pointer to the zero-terminated base 64 string, which
28 is in working store
29*/
30
31static uschar *enc64table =
32 US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33
34uschar *
35auth_b64encode(uschar *clear, int len)
36{
37uschar *code = store_get(4*((len+2)/3) + 1);
38uschar *p = code;
39
40while (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
73return code;
74}
75
76/* End of b64encode.c */