idn_win32.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 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. WINBASEAPI int WINAPI IdnToAscii(DWORD, const WCHAR *, int, WCHAR *, int);
  33. WINBASEAPI int WINAPI IdnToUnicode(DWORD, const WCHAR *, int, WCHAR *, int);
  34. #endif
  35. #define IDN_MAX_LENGTH 255
  36. int curl_win32_idn_to_ascii(const char *in, char **out);
  37. int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8);
  38. int curl_win32_idn_to_ascii(const char *in, char **out)
  39. {
  40. wchar_t *in_w = Curl_convert_UTF8_to_wchar(in);
  41. if(in_w) {
  42. wchar_t punycode[IDN_MAX_LENGTH];
  43. if(IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH) == 0) {
  44. wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
  45. free(in_w);
  46. return 0;
  47. }
  48. free(in_w);
  49. *out = Curl_convert_wchar_to_UTF8(punycode);
  50. if(!*out)
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8)
  56. {
  57. (void)in_len; /* unused */
  58. if(in) {
  59. WCHAR unicode[IDN_MAX_LENGTH];
  60. if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) {
  61. wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
  62. return 0;
  63. }
  64. else {
  65. *out_utf8 = Curl_convert_wchar_to_UTF8(unicode);
  66. if(!*out_utf8)
  67. return 0;
  68. }
  69. }
  70. return 1;
  71. }
  72. #endif /* USE_WIN32_IDN */