SPDX: license tags (mostly by guesswork)
[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 /* SPDX-License-Identifier: GPL-2.0-only */
9
10 #include "../exim.h"
11
12
13 /*************************************************
14 *          Encode byte-string in xtext           *
15 *************************************************/
16
17 /* This function encodes a string of bytes, containing any values whatsoever,
18 as "xtext", as defined in RFC 1891 and required by the SMTP AUTH extension (RFC
19 2554).
20
21 Arguments:
22   clear       points to the clear text bytes
23   len         the number of bytes to encode
24
25 Returns:      a pointer to the zero-terminated xtext string, which
26               is in working store
27 */
28
29 uschar *
30 auth_xtextencode(uschar *clear, int len)
31 {
32 uschar *code;
33 uschar *p = US clear;
34 uschar *pp;
35 int c = len;
36 int count = 1;
37 register int x;
38
39 /* We have to do a prepass to find out how many specials there are,
40 in order to get the right amount of store. */
41
42 while (c -- > 0)
43   count += ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')? 3 : 1;
44
45 pp = code = store_get(count, clear);
46
47 p = US clear;
48 c = len;
49 while (c-- > 0)
50   if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
51     pp += sprintf(CS pp, "+%.02x", x);   /* There's always room */
52   else
53     *pp++ = x;
54
55 *pp = 0;
56 return code;
57 }
58
59 /* End of xtextencode.c */