| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
| 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 = US 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 = US clear; |
| 46 | c = len; |
| 47 | while (c-- > 0) |
| 48 | if ((x = *p++) < 33 || x > 127 || x == '+' || x == '=') |
| 49 | pp += sprintf(CS pp, "+%.02x", x); /* There's always room */ |
| 50 | else |
| 51 | *pp++ = x; |
| 52 | |
| 53 | *pp = 0; |
| 54 | return code; |
| 55 | } |
| 56 | |
| 57 | /* End of xtextencode.c */ |