Imported Upstream version 4.84
[hcoop/debian/exim4.git] / src / auths / xtextencode.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
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,
16 as "xtext", as defined in RFC 1891 and required by the SMTP AUTH extension (RFC
17 2554).
18
19 Arguments:
20 clear points to the clear text bytes
21 len the number of bytes to encode
22
23 Returns: a pointer to the zero-terminated xtext string, which
24 is in working store
25 */
26
27 uschar *
28 auth_xtextencode(uschar *clear, int len)
29 {
30 uschar *code;
31 uschar *p = (uschar *)clear;
32 uschar *pp;
33 int c = len;
34 int count = 1;
35 register int x;
36
37 /* We have to do a prepass to find out how many specials there are,
38 in order to get the right amount of store. */
39
40 while (c -- > 0)
41 count += ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')? 3 : 1;
42
43 pp = code = store_get(count);
44
45 p = (uschar *)clear;
46 c = len;
47 while (c-- > 0)
48 {
49 if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
50 {
51 sprintf(CS pp, "+%.02x", x); /* There's always room */
52 pp += 3;
53 }
54 else *pp++ = x;
55 }
56
57 *pp = 0;
58 return code;
59 }
60
61 /* End of xtextencode.c */