curl_multibyte.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #if defined(USE_WIN32_IDN) || ((defined(USE_WINDOWS_SSPI) || \
  25. defined(USE_WIN32_LDAP)) && defined(UNICODE))
  26. /*
  27. * MultiByte conversions using Windows kernel32 library.
  28. */
  29. #include "curl_multibyte.h"
  30. #include "curl_memory.h"
  31. /* The last #include file should be: */
  32. #include "memdebug.h"
  33. wchar_t *Curl_convert_UTF8_to_wchar(const char *str_utf8)
  34. {
  35. wchar_t *str_w = NULL;
  36. if(str_utf8) {
  37. int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  38. str_utf8, -1, NULL, 0);
  39. if(str_w_len > 0) {
  40. str_w = malloc(str_w_len * sizeof(wchar_t));
  41. if(str_w) {
  42. if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
  43. str_w_len) == 0) {
  44. free(str_w);
  45. return NULL;
  46. }
  47. }
  48. }
  49. }
  50. return str_w;
  51. }
  52. char *Curl_convert_wchar_to_UTF8(const wchar_t *str_w)
  53. {
  54. char *str_utf8 = NULL;
  55. if(str_w) {
  56. int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
  57. NULL, 0, NULL, NULL);
  58. if(bytes > 0) {
  59. str_utf8 = malloc(bytes);
  60. if(str_utf8) {
  61. if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
  62. NULL, NULL) == 0) {
  63. free(str_utf8);
  64. return NULL;
  65. }
  66. }
  67. }
  68. }
  69. return str_utf8;
  70. }
  71. #endif /* USE_WIN32_IDN || ((USE_WINDOWS_SSPI || USE_WIN32_LDAP) && UNICODE) */