idn_win32.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * 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. #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x600
  35. WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
  36. const WCHAR *lpUnicodeCharStr,
  37. int cchUnicodeChar,
  38. WCHAR *lpASCIICharStr,
  39. int cchASCIIChar);
  40. WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
  41. const WCHAR *lpASCIICharStr,
  42. int cchASCIIChar,
  43. WCHAR *lpUnicodeCharStr,
  44. int cchUnicodeChar);
  45. #endif
  46. #define IDN_MAX_LENGTH 255
  47. bool Curl_win32_idn_to_ascii(const char *in, char **out);
  48. bool Curl_win32_ascii_to_idn(const char *in, char **out);
  49. bool Curl_win32_idn_to_ascii(const char *in, char **out)
  50. {
  51. bool success = FALSE;
  52. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  53. if(in_w) {
  54. wchar_t punycode[IDN_MAX_LENGTH];
  55. int chars = IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH);
  56. curlx_unicodefree(in_w);
  57. if(chars) {
  58. char *mstr = curlx_convert_wchar_to_UTF8(punycode);
  59. if(mstr) {
  60. *out = strdup(mstr);
  61. curlx_unicodefree(mstr);
  62. if(*out)
  63. success = TRUE;
  64. }
  65. }
  66. }
  67. return success;
  68. }
  69. bool Curl_win32_ascii_to_idn(const char *in, char **out)
  70. {
  71. bool success = FALSE;
  72. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  73. if(in_w) {
  74. size_t in_len = wcslen(in_w) + 1;
  75. wchar_t unicode[IDN_MAX_LENGTH];
  76. int chars = IdnToUnicode(0, in_w, curlx_uztosi(in_len),
  77. unicode, IDN_MAX_LENGTH);
  78. curlx_unicodefree(in_w);
  79. if(chars) {
  80. char *mstr = curlx_convert_wchar_to_UTF8(unicode);
  81. if(mstr) {
  82. *out = strdup(mstr);
  83. curlx_unicodefree(mstr);
  84. if(*out)
  85. success = TRUE;
  86. }
  87. }
  88. }
  89. return success;
  90. }
  91. #endif /* USE_WIN32_IDN */