Copyright updates:
[exim.git] / src / src / auths / xtextencode.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 #include "../exim.h"
10
11
12 /*************************************************
13 *          Encode byte-string in xtext           *
14 *************************************************/
15
16 /* This function encodes a string of bytes, containing any values whatsoever,
17 as "xtext", as defined in RFC 1891 and required by the SMTP AUTH extension (RFC
18 2554).
19
20 Arguments:
21   clear       points to the clear text bytes
22   len         the number of bytes to encode
23
24 Returns:      a pointer to the zero-terminated xtext string, which
25               is in working store
26 */
27
28 uschar *
29 auth_xtextencode(uschar *clear, int len)
30 {
31 uschar *code;
32 uschar *p = US clear;
33 uschar *pp;
34 int c = len;
35 int count = 1;
36 register int x;
37
38 /* We have to do a prepass to find out how many specials there are,
39 in order to get the right amount of store. */
40
41 while (c -- > 0)
42   count += ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')? 3 : 1;
43
44 pp = code = store_get(count, clear);
45
46 p = US clear;
47 c = len;
48 while (c-- > 0)
49   if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
50     pp += sprintf(CS pp, "+%.02x", x);   /* There's always room */
51   else
52     *pp++ = x;
53
54 *pp = 0;
55 return code;
56 }
57
58 /* End of xtextencode.c */