curl_multibyte.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * This file is 'mem-include-scan' clean, which means memdebug.h and
  26. * curl_memory.h are purposely not included in this file. See test 1132.
  27. *
  28. * The functions in this file are curlx functions which are not tracked by the
  29. * curl memory tracker memdebug.
  30. */
  31. #include "curl_setup.h"
  32. #if defined(_WIN32)
  33. #include "curl_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 */
  76. #if defined(USE_WIN32_LARGE_FILES) || defined(USE_WIN32_SMALL_FILES)
  77. int curlx_win32_open(const char *filename, int oflag, ...)
  78. {
  79. int pmode = 0;
  80. #ifdef _UNICODE
  81. int result = -1;
  82. wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
  83. #endif
  84. va_list param;
  85. va_start(param, oflag);
  86. if(oflag & O_CREAT)
  87. pmode = va_arg(param, int);
  88. va_end(param);
  89. #ifdef _UNICODE
  90. if(filename_w) {
  91. result = _wopen(filename_w, oflag, pmode);
  92. curlx_unicodefree(filename_w);
  93. }
  94. else
  95. errno = EINVAL;
  96. return result;
  97. #else
  98. return (_open)(filename, oflag, pmode);
  99. #endif
  100. }
  101. FILE *curlx_win32_fopen(const char *filename, const char *mode)
  102. {
  103. #ifdef _UNICODE
  104. FILE *result = NULL;
  105. wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
  106. wchar_t *mode_w = curlx_convert_UTF8_to_wchar(mode);
  107. if(filename_w && mode_w)
  108. result = _wfopen(filename_w, mode_w);
  109. else
  110. errno = EINVAL;
  111. curlx_unicodefree(filename_w);
  112. curlx_unicodefree(mode_w);
  113. return result;
  114. #else
  115. return (fopen)(filename, mode);
  116. #endif
  117. }
  118. int curlx_win32_stat(const char *path, struct_stat *buffer)
  119. {
  120. #ifdef _UNICODE
  121. int result = -1;
  122. wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
  123. if(path_w) {
  124. #if defined(USE_WIN32_SMALL_FILES)
  125. result = _wstat(path_w, buffer);
  126. #else
  127. result = _wstati64(path_w, buffer);
  128. #endif
  129. curlx_unicodefree(path_w);
  130. }
  131. else
  132. errno = EINVAL;
  133. return result;
  134. #else
  135. #if defined(USE_WIN32_SMALL_FILES)
  136. return _stat(path, buffer);
  137. #else
  138. return _stati64(path, buffer);
  139. #endif
  140. #endif
  141. }
  142. #endif /* USE_WIN32_LARGE_FILES || USE_WIN32_SMALL_FILES */