idn_win32: Better error checking

.. also fix a conversion bug in the unused function
curl_win32_ascii_to_idn().

And remove wprintfs on error (Jay).

Bug: https://github.com/curl/curl/pull/637
This commit is contained in:
Michael Kaufmann 2016-02-05 21:15:43 +01:00 committed by Jay Satiro
parent c71b6a2795
commit 9e7fcd4291
1 changed files with 21 additions and 25 deletions

View File

@ -65,44 +65,40 @@ WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
#define IDN_MAX_LENGTH 255 #define IDN_MAX_LENGTH 255
int curl_win32_idn_to_ascii(const char *in, char **out); int curl_win32_idn_to_ascii(const char *in, char **out);
int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8); int curl_win32_ascii_to_idn(const char *in, char **out);
int curl_win32_idn_to_ascii(const char *in, char **out) int curl_win32_idn_to_ascii(const char *in, char **out)
{ {
int ret = 0;
wchar_t *in_w = Curl_convert_UTF8_to_wchar(in); wchar_t *in_w = Curl_convert_UTF8_to_wchar(in);
if(in_w) { if(in_w) {
wchar_t punycode[IDN_MAX_LENGTH]; wchar_t punycode[IDN_MAX_LENGTH];
if(IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH) == 0) { int chars = IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH);
wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
free(in_w);
return 0;
}
free(in_w); free(in_w);
if(chars) {
*out = Curl_convert_wchar_to_UTF8(punycode); *out = Curl_convert_wchar_to_UTF8(punycode);
if(!*out) if(*out)
return 0; ret = 1; /* success */
}
} }
return 1; return ret;
} }
int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8) int curl_win32_ascii_to_idn(const char *in, char **out)
{ {
(void)in_len; /* unused */ int ret = 0;
if(in) { wchar_t *in_w = Curl_convert_UTF8_to_wchar(in);
WCHAR unicode[IDN_MAX_LENGTH]; if(in_w) {
wchar_t unicode[IDN_MAX_LENGTH];
if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) { int chars = IdnToUnicode(0, in_w, wcslen(in_w)+1, unicode, IDN_MAX_LENGTH);
wprintf(L"ERROR %d converting to Punycode\n", GetLastError()); free(in_w);
return 0; if(chars) {
} *out = Curl_convert_wchar_to_UTF8(unicode);
else { if(*out)
*out_utf8 = Curl_convert_wchar_to_UTF8(unicode); ret = 1; /* success */
if(!*out_utf8)
return 0;
} }
} }
return 1; return ret;
} }
#endif /* USE_WIN32_IDN */ #endif /* USE_WIN32_IDN */