multibyte.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 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. * This file is 'mem-include-scan' clean, which means its memory allocations
  26. * are not tracked by the curl memory tracker memdebug, so they must not use
  27. * `CURLDEBUG` macro replacements in memdebug.h for free, malloc, etc. To avoid
  28. * these macro replacements, wrap the names in parentheses to call the original
  29. * versions: `ptr = (malloc)(123)`, `(free)(ptr)`, etc.
  30. */
  31. #include "../curl_setup.h"
  32. #ifdef _WIN32
  33. #include "multibyte.h"
  34. /*
  35. * MultiByte conversions using Windows kernel32 library.
  36. */
  37. wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8)
  38. {
  39. wchar_t *str_w = NULL;
  40. if(str_utf8) {
  41. int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  42. str_utf8, -1, NULL, 0);
  43. if(str_w_len > 0) {
  44. str_w = (malloc)(str_w_len * sizeof(wchar_t));
  45. if(str_w) {
  46. if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
  47. str_w_len) == 0) {
  48. (free)(str_w);
  49. return NULL;
  50. }
  51. }
  52. }
  53. }
  54. return str_w;
  55. }
  56. char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w)
  57. {
  58. char *str_utf8 = NULL;
  59. if(str_w) {
  60. int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
  61. NULL, 0, NULL, NULL);
  62. if(bytes > 0) {
  63. str_utf8 = (malloc)(bytes);
  64. if(str_utf8) {
  65. if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
  66. NULL, NULL) == 0) {
  67. (free)(str_utf8);
  68. return NULL;
  69. }
  70. }
  71. }
  72. }
  73. return str_utf8;
  74. }
  75. #endif /* _WIN32 */