| 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 | * Decode byte-string in xtext * |
| 13 | *************************************************/ |
| 14 | |
| 15 | /* This function decodes a string in xtextformat as defined in RFC 1891 and |
| 16 | required by the SMTP AUTH extension (RFC 2554). We put the result in a piece of |
| 17 | store of equal length - it cannot be longer than this. Although in general the |
| 18 | result of decoding an xtext may be binary, in the context in which it is used |
| 19 | by Exim (for decoding the value of AUTH on a MAIL command), the result is |
| 20 | expected to be an addr-spec. We therefore add on a terminating zero, for |
| 21 | convenience. |
| 22 | |
| 23 | Arguments: |
| 24 | code points to the coded string, zero-terminated |
| 25 | ptr where to put the pointer to the result, which is in |
| 26 | dynamic store |
| 27 | |
| 28 | Returns: the number of bytes in the result, excluding the final zero; |
| 29 | -1 if the input is malformed |
| 30 | */ |
| 31 | |
| 32 | int |
| 33 | auth_xtextdecode(uschar *code, uschar **ptr) |
| 34 | { |
| 35 | register int x; |
| 36 | uschar *result = store_get(Ustrlen(code) + 1); |
| 37 | *ptr = result; |
| 38 | |
| 39 | while ((x = (*code++)) != 0) |
| 40 | { |
| 41 | if (x < 33 || x > 127 || x == '=') return -1; |
| 42 | if (x == '+') |
| 43 | { |
| 44 | register int y; |
| 45 | if (!isxdigit((x = (*code++)))) return -1; |
| 46 | y = ((isdigit(x))? x - '0' : (tolower(x) - 'a' + 10)) << 4; |
| 47 | if (!isxdigit((x = (*code++)))) return -1; |
| 48 | *result++ = y | ((isdigit(x))? x - '0' : (tolower(x) - 'a' + 10)); |
| 49 | } |
| 50 | else *result++ = x; |
| 51 | } |
| 52 | |
| 53 | *result = 0; |
| 54 | return result - *ptr; |
| 55 | } |
| 56 | |
| 57 | /* End of xtextdecode.c */ |