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 */
29 string_domain_utf8_to_alabel(const uschar
* utf8
, uschar
** err
)
35 s
= US
stringprep_utf8_nfkc_normalize(CCS utf8
, -1);
36 if ( (rc
= idna_to_ascii_8z(CCS s
, CSS
&s1
, IDNA_USE_STD3_ASCII_RULES
))
40 if (err
) *err
= US
idna_strerror(rc
);
52 string_domain_alabel_to_utf8(const uschar
* alabel
, uschar
** err
)
57 if ( (rc
= idna_to_unicode_8z8z(CCS alabel
, CSS
&s1
, IDNA_USE_STD3_ASCII_RULES
))
60 if (err
) *err
= US
idna_strerror(rc
);
68 /**************************************************/
69 /* localpart conversions */
73 string_localpart_utf8_to_alabel(const uschar
* utf8
, uschar
** err
)
76 punycode_uint
* p
= (punycode_uint
*) stringprep_utf8_to_ucs4(CCS utf8
, -1, &ucs4_len
);
77 size_t p_len
= ucs4_len
*4; /* this multiplier is pure guesswork */
78 uschar
* res
= store_get(p_len
+5);
81 DEBUG(D_expand
) debug_printf("l_u2a: ulen %d plen %d\n", ucs4_len
, p_len
);
82 DEBUG(D_expand
) for (rc
= 0; rc
< ucs4_len
; rc
++) debug_printf("%08x ", p
[rc
]);
84 res
[0] = 'x'; res
[1] = 'n'; res
[2] = res
[3] = '-';
86 if ((rc
= punycode_encode(ucs4_len
, p
, NULL
, &p_len
, res
+4)) != PUNYCODE_SUCCESS
)
88 DEBUG(D_expand
) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc
));
90 if (err
) *err
= US
punycode_strerror(rc
);
93 DEBUG(D_expand
) debug_printf("l_u2a: plen %d\n", p_len
);
95 DEBUG(D_expand
) for (rc
= 0; rc
< p_len
; rc
++) debug_printf("%02x ", res
[rc
]);
96 DEBUG(D_expand
) debug_printf("\n");
104 string_localpart_alabel_to_utf8(const uschar
* alabel
, uschar
** err
)
106 size_t p_len
= strlen(alabel
);
112 if (alabel
[0] != 'x' || alabel
[1] != 'n' || alabel
[2] != '-' || alabel
[3] != '-')
114 if (err
) *err
= US
"bad alabel prefix";
118 DEBUG(D_expand
) debug_printf("l_a2u: plen %d\n", p_len
);
120 p
= (punycode_uint
*) store_get((p_len
+1) * sizeof(*p
));
122 if ((rc
= punycode_decode(p_len
, CCS alabel
+4, &p_len
, p
, NULL
)) != PUNYCODE_SUCCESS
)
124 if (err
) *err
= US
punycode_strerror(rc
);
127 DEBUG(D_expand
) debug_printf("l_a2u: dlen %d\n", p_len
);
129 s
= stringprep_ucs4_to_utf8(p
, p_len
, NULL
, &p_len
);
130 res
= string_copyn(s
, p_len
);
136 #endif /* whole file */