1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
11 #ifdef EXPERIMENTAL_INTERNATIONAL
15 #include <stringprep.h>
18 string_is_utf8(const uschar * s)
21 while ((c = *s++)) if (c & 0x80) return TRUE;
25 /**************************************************/
26 /* Domain conversions */
27 /* the *err string pointer should be null before the call */
30 string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
36 s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
37 if ( (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
41 if (err) *err = US idna_strerror(rc);
53 string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
59 if ( (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
62 if (err) *err = US idna_strerror(rc);
70 /**************************************************/
71 /* localpart conversions */
72 /* the *err string pointer should be null before the call */
76 string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
84 if (!string_is_utf8(utf8)) return string_copy(utf8);
86 p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
87 p_len = ucs4_len*4; /* this multiplier is pure guesswork */
88 res = store_get(p_len+5);
90 res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
92 if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
94 DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
96 if (err) *err = US punycode_strerror(rc);
107 string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
109 size_t p_len = Ustrlen(alabel);
115 if (alabel[0] != 'x' || alabel[1] != 'n' || alabel[2] != '-' || alabel[3] != '-')
117 if (err) *err = US"bad alabel prefix";
122 p = (punycode_uint *) store_get((p_len+1) * sizeof(*p));
124 if ((rc = punycode_decode(p_len, CCS alabel+4, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
126 if (err) *err = US punycode_strerror(rc);
130 s = stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
131 res = string_copyn(s, p_len);
137 /**************************************************/
138 /* whole address conversion */
139 /* the *err string pointer should be null before the call */
142 string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
148 if (!*utf8) return string_copy(utf8);
150 DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
152 for (s = utf8; *s; s++)
155 l = string_copyn(utf8, s - utf8);
156 if ( (l = string_localpart_utf8_to_alabel(l, err), err && *err)
157 || (d = string_domain_utf8_to_alabel(++s, err), err && *err)
160 l = string_sprintf("%s@%s", l, d);
161 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
165 l = string_localpart_utf8_to_alabel(utf8, err);
166 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
172 /*************************************************
173 * Report the library versions. *
174 *************************************************/
176 /* See a description in tls-openssl.c for an explanation of why this exists.
178 Arguments: a FILE* to print the results to
183 utf8_version_report(FILE *f)
185 fprintf(f, "Library version: IDN: Compile: %s\n"
188 stringprep_check_version(NULL));
191 #endif /* whole file */