Another wish.
[exim.git] / src / src / auths / xtextencode.c
CommitLineData
c988f1f4 1/* $Cambridge: exim/src/src/auths/xtextencode.c,v 1.2 2005/01/04 10:00:43 ph10 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
c988f1f4 7/* Copyright (c) University of Cambridge 1995 - 2005 */
0756eb3c
PH
8/* See the file NOTICE for conditions of use and distribution. */
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,
18as "xtext", as defined in RFC 1891 and required by the SMTP AUTH extension (RFC
192554).
20
21Arguments:
22 clear points to the clear text bytes
23 len the number of bytes to encode
24
25Returns: a pointer to the zero-terminated xtext string, which
26 is in working store
27*/
28
29uschar *
30auth_xtextencode(uschar *clear, int len)
31{
32uschar *code;
33uschar *p = (uschar *)clear;
34uschar *pp;
35int c = len;
36int count = 1;
37register int x;
38
39/* We have to do a prepass to find out how many specials there are,
40in order to get the right amount of store. */
41
42while (c -- > 0)
43 count += ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')? 3 : 1;
44
45pp = code = store_get(count);
46
47p = (uschar *)clear;
48c = len;
49while (c-- > 0)
50 {
51 if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=')
52 {
53 sprintf(CS pp, "+%.02x", x); /* There's always room */
54 pp += 3;
55 }
56 else *pp++ = x;
57 }
58
59*pp = 0;
60return code;
61}
62
63/* End of xtextencode.c */