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