| 1 | /* $Cambridge: exim/src/src/auths/xtextencode.c,v 1.4 2007/01/08 10:50:19 ph10 Exp $ */ |
| 2 | |
| 3 | /************************************************* |
| 4 | * Exim - an Internet mail transport agent * |
| 5 | *************************************************/ |
| 6 | |
| 7 | /* Copyright (c) University of Cambridge 1995 - 2007 */ |
| 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, |
| 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 = (uschar *)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); |
| 46 | |
| 47 | p = (uschar *)clear; |
| 48 | c = len; |
| 49 | while (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; |
| 60 | return code; |
| 61 | } |
| 62 | |
| 63 | /* End of xtextencode.c */ |