summaryrefslogtreecommitdiffstats
path: root/src/auths/xtextencode.c
blob: 30ff8f11ddb039401f34d74c1e2ccfea0e1210ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*************************************************
*     Exim - an Internet mail transport agent    *
*************************************************/

/* Copyright (c) University of Cambridge 1995 - 2018 */
/* See the file NOTICE for conditions of use and distribution. */

#include "../exim.h"


/*************************************************
*          Encode byte-string in xtext           *
*************************************************/

/* This function encodes a string of bytes, containing any values whatsoever,
as "xtext", as defined in RFC 1891 and required by the SMTP AUTH extension (RFC
2554).

Arguments:
  clear       points to the clear text bytes
  len         the number of bytes to encode

Returns:      a pointer to the zero-terminated xtext string, which
              is in working store
*/

uschar *
auth_xtextencode(uschar *clear, int len)
{
uschar *code;
uschar *p = US clear;
uschar *pp;
int c = len;
int count = 1;
register int x;

/* We have to do a prepass to find out how many specials there are,
in order to get the right amount of store. */

while (c -- > 0)
  count += ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')? 3 : 1;

pp = code = store_get(count, is_tainted(clear));

p = US clear;
c = len;
while (c-- > 0)
  if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
    pp += sprintf(CS pp, "+%.02x", x);   /* There's always room */
  else
    *pp++ = x;

*pp = 0;
return code;
}

/* End of xtextencode.c */