idn_win32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. * IDN conversions using Windows kernel32 and normaliz libraries.
  26. */
  27. #include "curl_setup.h"
  28. #ifdef USE_WIN32_IDN
  29. #include "curl_multibyte.h"
  30. #include "curl_memory.h"
  31. #include "warnless.h"
  32. /* The last #include file should be: */
  33. #include "memdebug.h"
  34. #ifdef WANT_IDN_PROTOTYPES
  35. # if defined(_SAL_VERSION)
  36. WINNORMALIZEAPI int WINAPI
  37. IdnToAscii(_In_ DWORD dwFlags,
  38. _In_reads_(cchUnicodeChar) LPCWSTR lpUnicodeCharStr,
  39. _In_ int cchUnicodeChar,
  40. _Out_writes_opt_(cchASCIIChar) LPWSTR lpASCIICharStr,
  41. _In_ int cchASCIIChar);
  42. WINNORMALIZEAPI int WINAPI
  43. IdnToUnicode(_In_ DWORD dwFlags,
  44. _In_reads_(cchASCIIChar) LPCWSTR lpASCIICharStr,
  45. _In_ int cchASCIIChar,
  46. _Out_writes_opt_(cchUnicodeChar) LPWSTR lpUnicodeCharStr,
  47. _In_ int cchUnicodeChar);
  48. # else
  49. WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
  50. const WCHAR *lpUnicodeCharStr,
  51. int cchUnicodeChar,
  52. WCHAR *lpASCIICharStr,
  53. int cchASCIIChar);
  54. WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
  55. const WCHAR *lpASCIICharStr,
  56. int cchASCIIChar,
  57. WCHAR *lpUnicodeCharStr,
  58. int cchUnicodeChar);
  59. # endif
  60. #endif
  61. #define IDN_MAX_LENGTH 255
  62. bool Curl_win32_idn_to_ascii(const char *in, char **out);
  63. bool Curl_win32_ascii_to_idn(const char *in, char **out);
  64. bool Curl_win32_idn_to_ascii(const char *in, char **out)
  65. {
  66. bool success = FALSE;
  67. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  68. if(in_w) {
  69. wchar_t punycode[IDN_MAX_LENGTH];
  70. int chars = IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH);
  71. curlx_unicodefree(in_w);
  72. if(chars) {
  73. char *mstr = curlx_convert_wchar_to_UTF8(punycode);
  74. if(mstr) {
  75. *out = strdup(mstr);
  76. curlx_unicodefree(mstr);
  77. if(*out)
  78. success = TRUE;
  79. }
  80. }
  81. }
  82. return success;
  83. }
  84. bool Curl_win32_ascii_to_idn(const char *in, char **out)
  85. {
  86. bool success = FALSE;
  87. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  88. if(in_w) {
  89. size_t in_len = wcslen(in_w) + 1;
  90. wchar_t unicode[IDN_MAX_LENGTH];
  91. int chars = IdnToUnicode(0, in_w, curlx_uztosi(in_len),
  92. unicode, IDN_MAX_LENGTH);
  93. curlx_unicodefree(in_w);
  94. if(chars) {
  95. char *mstr = curlx_convert_wchar_to_UTF8(unicode);
  96. if(mstr) {
  97. *out = strdup(mstr);
  98. curlx_unicodefree(mstr);
  99. if(*out)
  100. success = TRUE;
  101. }
  102. }
  103. }
  104. return success;
  105. }
  106. #endif /* USE_WIN32_IDN */