tls
[exim.git] / src / src / utf8.c
CommitLineData
0d7911ea
JH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) Jeremy Harris 2015 */
6/* See the file NOTICE for conditions of use and distribution. */
7
8
9#include "exim.h"
10
11#ifdef EXPERIMENTAL_INTERNATIONAL
12
13#include <idna.h>
14#include <punycode.h>
15#include <stringprep.h>
16
17BOOL
18string_is_utf8(const uschar * s)
19{
20uschar c;
21while ((c = *s++)) if (c & 0x80) return TRUE;
22return FALSE;
23}
24
25/**************************************************/
26/* Domain conversions */
27
28uschar *
29string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
30{
31uschar * s1;
32uschar * s;
33int rc;
34
35s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
37bf366e 36if ( (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
0d7911ea
JH
37 != IDNA_SUCCESS)
38 {
39 free(s);
40 if (err) *err = US idna_strerror(rc);
41 return NULL;
42 }
43free(s);
44s = string_copy(s1);
45free(s1);
46return s;
47}
48
49
50
51uschar *
52string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
53{
54uschar * s1;
55uschar * s;
56int rc;
57if ( (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
58 != IDNA_SUCCESS)
59 {
60 if (err) *err = US idna_strerror(rc);
61 return NULL;
62 }
63s = string_copy(s1);
64free(s1);
65return s;
66}
67
68/**************************************************/
69/* localpart conversions */
70
71
72uschar *
73string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
74{
75size_t ucs4_len;
76punycode_uint * p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
77size_t p_len = ucs4_len*4; /* this multiplier is pure guesswork */
78uschar * res = store_get(p_len+5);
79int rc;
80
81res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
82
83if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, res+4)) != PUNYCODE_SUCCESS)
84 {
4e08fd50 85 DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
0d7911ea
JH
86 free(p);
87 if (err) *err = US punycode_strerror(rc);
88 return NULL;
89 }
4e08fd50 90p_len += 4;
0d7911ea
JH
91free(p);
92res[p_len] = '\0';
93return res;
94}
95
96
97uschar *
98string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
99{
100size_t p_len = strlen(alabel);
101punycode_uint * p;
4e08fd50
JH
102uschar * s;
103uschar * res;
0d7911ea
JH
104int rc;
105
106if (alabel[0] != 'x' || alabel[1] != 'n' || alabel[2] != '-' || alabel[3] != '-')
107 {
108 if (err) *err = US"bad alabel prefix";
109 return NULL;
110 }
0d7911ea 111
9d4319df 112p_len -= 4;
0d7911ea
JH
113p = (punycode_uint *) store_get((p_len+1) * sizeof(*p));
114
115if ((rc = punycode_decode(p_len, CCS alabel+4, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
116 {
117 if (err) *err = US punycode_strerror(rc);
118 return NULL;
119 }
4e08fd50
JH
120
121s = stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
122res = string_copyn(s, p_len);
123free(s);
124return res;
0d7911ea
JH
125}
126
127
b04be5e7
JH
128/*************************************************
129* Report the library versions. *
130*************************************************/
131
132/* See a description in tls-openssl.c for an explanation of why this exists.
133
134Arguments: a FILE* to print the results to
135Returns: nothing
136*/
137
138void
139utf8_version_report(FILE *f)
140{
141fprintf(f, "Library version: IDN: Compile: %s\n"
142 " Runtime: %s\n",
143 STRINGPREP_VERSION,
144 stringprep_check_version(NULL));
145}
146
0d7911ea
JH
147#endif /* whole file */
148
149/* vi: aw ai sw=2
150*/
151/* End of utf8.c */