@@ -75,7 +75,8 @@ bool Curl_win32_idn_to_ascii(const char *in, char **out)
75
75
wchar_t * in_w = curlx_convert_UTF8_to_wchar (in );
76
76
if (in_w ) {
77
77
wchar_t punycode [IDN_MAX_LENGTH ];
78
- int chars = IdnToAscii (0 , in_w , -1 , punycode , IDN_MAX_LENGTH );
78
+ int chars = IdnToAscii (0 , in_w , (int )(wcslen (in_w ) + 1 ), punycode ,
79
+ IDN_MAX_LENGTH );
79
80
curlx_unicodefree (in_w );
80
81
if (chars ) {
81
82
char * mstr = curlx_convert_wchar_to_UTF8 (punycode );
@@ -91,6 +92,27 @@ bool Curl_win32_idn_to_ascii(const char *in, char **out)
91
92
return success ;
92
93
}
93
94
95
+ char * Curl_win32_ascii_to_idn (const char * in )
96
+ {
97
+ char * out = NULL ;
98
+
99
+ wchar_t * in_w = curlx_convert_UTF8_to_wchar (in );
100
+ if (in_w ) {
101
+ WCHAR idn [IDN_MAX_LENGTH ]; /* stores a UTF-16 string */
102
+ int chars = IdnToUnicode (0 , in_w , (int )(wcslen (in_w ) + 1 ), idn ,
103
+ IDN_MAX_LENGTH );
104
+ if (chars ) {
105
+ /* 'chars' is "the number of characters retrieved" */
106
+ char * mstr = curlx_convert_wchar_to_UTF8 (idn );
107
+ if (mstr ) {
108
+ out = strdup (mstr );
109
+ curlx_unicodefree (mstr );
110
+ }
111
+ }
112
+ }
113
+ return out ;
114
+ }
115
+
94
116
#endif /* USE_WIN32_IDN */
95
117
96
118
/*
@@ -144,6 +166,19 @@ static char *idn_decode(const char *input)
144
166
return decoded ;
145
167
}
146
168
169
+ static char * idn_encode (const char * puny )
170
+ {
171
+ char * enc = NULL ;
172
+ #ifdef USE_LIBIDN2
173
+ int rc = idn2_to_unicode_8z8z (puny , & enc , 0 );
174
+ if (rc != IDNA_SUCCESS )
175
+ return NULL ;
176
+ #elif defined(USE_WIN32_IDN )
177
+ enc = Curl_win32_ascii_to_idn (puny );
178
+ #endif
179
+ return enc ;
180
+ }
181
+
147
182
char * Curl_idn_decode (const char * input )
148
183
{
149
184
char * d = idn_decode (input );
@@ -157,6 +192,19 @@ char *Curl_idn_decode(const char *input)
157
192
return d ;
158
193
}
159
194
195
+ char * Curl_idn_encode (const char * puny )
196
+ {
197
+ char * d = idn_encode (puny );
198
+ #ifdef USE_LIBIDN2
199
+ if (d ) {
200
+ char * c = strdup (d );
201
+ idn2_free (d );
202
+ d = c ;
203
+ }
204
+ #endif
205
+ return d ;
206
+ }
207
+
160
208
/*
161
209
* Frees data allocated by idnconvert_hostname()
162
210
*/
0 commit comments