idn_win32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://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. /* The last #include file should be: */
  30. #include "memdebug.h"
  31. #ifdef WANT_IDN_PROTOTYPES
  32. # if defined(_SAL_VERSION)
  33. WINNORMALIZEAPI int WINAPI
  34. IdnToAscii(_In_ DWORD dwFlags,
  35. _In_reads_(cchUnicodeChar) LPCWSTR lpUnicodeCharStr,
  36. _In_ int cchUnicodeChar,
  37. _Out_writes_opt_(cchASCIIChar) LPWSTR lpASCIICharStr,
  38. _In_ int cchASCIIChar);
  39. WINNORMALIZEAPI int WINAPI
  40. IdnToUnicode(_In_ DWORD dwFlags,
  41. _In_reads_(cchASCIIChar) LPCWSTR lpASCIICharStr,
  42. _In_ int cchASCIIChar,
  43. _Out_writes_opt_(cchUnicodeChar) LPWSTR lpUnicodeCharStr,
  44. _In_ int cchUnicodeChar);
  45. # else
  46. WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
  47. const WCHAR *lpUnicodeCharStr,
  48. int cchUnicodeChar,
  49. WCHAR *lpASCIICharStr,
  50. int cchASCIIChar);
  51. WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
  52. const WCHAR *lpASCIICharStr,
  53. int cchASCIIChar,
  54. WCHAR *lpUnicodeCharStr,
  55. int cchUnicodeChar);
  56. # endif
  57. #endif
  58. #define IDN_MAX_LENGTH 255
  59. int curl_win32_idn_to_ascii(const char *in, char **out);
  60. int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8);
  61. int curl_win32_idn_to_ascii(const char *in, char **out)
  62. {
  63. wchar_t *in_w = Curl_convert_UTF8_to_wchar(in);
  64. if(in_w) {
  65. wchar_t punycode[IDN_MAX_LENGTH];
  66. if(IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH) == 0) {
  67. wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
  68. free(in_w);
  69. return 0;
  70. }
  71. free(in_w);
  72. *out = Curl_convert_wchar_to_UTF8(punycode);
  73. if(!*out)
  74. return 0;
  75. }
  76. return 1;
  77. }
  78. int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8)
  79. {
  80. (void)in_len; /* unused */
  81. if(in) {
  82. WCHAR unicode[IDN_MAX_LENGTH];
  83. if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) {
  84. wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
  85. return 0;
  86. }
  87. else {
  88. *out_utf8 = Curl_convert_wchar_to_UTF8(unicode);
  89. if(!*out_utf8)
  90. return 0;
  91. }
  92. }
  93. return 1;
  94. }
  95. #endif /* USE_WIN32_IDN */