idn_win32.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, 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. ***************************************************************************/
  22. /*
  23. * IDN conversions using Windows kernel32 and normaliz libraries.
  24. */
  25. #include "curl_setup.h"
  26. #ifdef USE_WIN32_IDN
  27. #include "curl_multibyte.h"
  28. #include "curl_memory.h"
  29. #include "warnless.h"
  30. /* The last #include file should be: */
  31. #include "memdebug.h"
  32. #ifdef WANT_IDN_PROTOTYPES
  33. # if defined(_SAL_VERSION)
  34. WINNORMALIZEAPI int WINAPI
  35. IdnToAscii(_In_ DWORD dwFlags,
  36. _In_reads_(cchUnicodeChar) LPCWSTR lpUnicodeCharStr,
  37. _In_ int cchUnicodeChar,
  38. _Out_writes_opt_(cchASCIIChar) LPWSTR lpASCIICharStr,
  39. _In_ int cchASCIIChar);
  40. WINNORMALIZEAPI int WINAPI
  41. IdnToUnicode(_In_ DWORD dwFlags,
  42. _In_reads_(cchASCIIChar) LPCWSTR lpASCIICharStr,
  43. _In_ int cchASCIIChar,
  44. _Out_writes_opt_(cchUnicodeChar) LPWSTR lpUnicodeCharStr,
  45. _In_ int cchUnicodeChar);
  46. # else
  47. WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
  48. const WCHAR *lpUnicodeCharStr,
  49. int cchUnicodeChar,
  50. WCHAR *lpASCIICharStr,
  51. int cchASCIIChar);
  52. WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
  53. const WCHAR *lpASCIICharStr,
  54. int cchASCIIChar,
  55. WCHAR *lpUnicodeCharStr,
  56. int cchUnicodeChar);
  57. # endif
  58. #endif
  59. #define IDN_MAX_LENGTH 255
  60. bool curl_win32_idn_to_ascii(const char *in, char **out);
  61. bool curl_win32_ascii_to_idn(const char *in, char **out);
  62. bool curl_win32_idn_to_ascii(const char *in, char **out)
  63. {
  64. bool success = FALSE;
  65. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  66. if(in_w) {
  67. wchar_t punycode[IDN_MAX_LENGTH];
  68. int chars = IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH);
  69. curlx_unicodefree(in_w);
  70. if(chars) {
  71. char *mstr = curlx_convert_wchar_to_UTF8(punycode);
  72. if(mstr) {
  73. *out = strdup(mstr);
  74. curlx_unicodefree(mstr);
  75. if(*out)
  76. success = TRUE;
  77. }
  78. }
  79. }
  80. return success;
  81. }
  82. bool curl_win32_ascii_to_idn(const char *in, char **out)
  83. {
  84. bool success = FALSE;
  85. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  86. if(in_w) {
  87. size_t in_len = wcslen(in_w) + 1;
  88. wchar_t unicode[IDN_MAX_LENGTH];
  89. int chars = IdnToUnicode(0, in_w, curlx_uztosi(in_len),
  90. unicode, IDN_MAX_LENGTH);
  91. curlx_unicodefree(in_w);
  92. if(chars) {
  93. char *mstr = curlx_convert_wchar_to_UTF8(unicode);
  94. if(mstr) {
  95. *out = strdup(mstr);
  96. curlx_unicodefree(mstr);
  97. if(*out)
  98. success = TRUE;
  99. }
  100. }
  101. }
  102. return success;
  103. }
  104. #endif /* USE_WIN32_IDN */