idn_win32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. free(in_w);
  70. if(chars) {
  71. *out = curlx_convert_wchar_to_UTF8(punycode);
  72. if(*out)
  73. success = TRUE;
  74. }
  75. }
  76. return success;
  77. }
  78. bool curl_win32_ascii_to_idn(const char *in, char **out)
  79. {
  80. bool success = FALSE;
  81. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  82. if(in_w) {
  83. size_t in_len = wcslen(in_w) + 1;
  84. wchar_t unicode[IDN_MAX_LENGTH];
  85. int chars = IdnToUnicode(0, in_w, curlx_uztosi(in_len),
  86. unicode, IDN_MAX_LENGTH);
  87. free(in_w);
  88. if(chars) {
  89. *out = curlx_convert_wchar_to_UTF8(unicode);
  90. if(*out)
  91. success = TRUE;
  92. }
  93. }
  94. return success;
  95. }
  96. #endif /* USE_WIN32_IDN */