curl_multibyte.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  22. /*
  23. * This file is 'mem-include-scan' clean, which means memdebug.h and
  24. * curl_memory.h are purposely not included in this file. See test 1132.
  25. *
  26. * The functions in this file are curlx functions which are not tracked by the
  27. * curl memory tracker memdebug.
  28. */
  29. #include "curl_setup.h"
  30. #if defined(WIN32)
  31. #include "curl_multibyte.h"
  32. /*
  33. * MultiByte conversions using Windows kernel32 library.
  34. */
  35. wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8)
  36. {
  37. wchar_t *str_w = NULL;
  38. if(str_utf8) {
  39. int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  40. str_utf8, -1, NULL, 0);
  41. if(str_w_len > 0) {
  42. str_w = malloc(str_w_len * sizeof(wchar_t));
  43. if(str_w) {
  44. if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
  45. str_w_len) == 0) {
  46. free(str_w);
  47. return NULL;
  48. }
  49. }
  50. }
  51. }
  52. return str_w;
  53. }
  54. char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w)
  55. {
  56. char *str_utf8 = NULL;
  57. if(str_w) {
  58. int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
  59. NULL, 0, NULL, NULL);
  60. if(bytes > 0) {
  61. str_utf8 = malloc(bytes);
  62. if(str_utf8) {
  63. if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
  64. NULL, NULL) == 0) {
  65. free(str_utf8);
  66. return NULL;
  67. }
  68. }
  69. }
  70. }
  71. return str_utf8;
  72. }
  73. #endif /* WIN32 */
  74. #if defined(USE_WIN32_LARGE_FILES) || defined(USE_WIN32_SMALL_FILES)
  75. int curlx_win32_open(const char *filename, int oflag, ...)
  76. {
  77. int pmode = 0;
  78. #ifdef _UNICODE
  79. int result = -1;
  80. wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
  81. #endif
  82. va_list param;
  83. va_start(param, oflag);
  84. if(oflag & O_CREAT)
  85. pmode = va_arg(param, int);
  86. va_end(param);
  87. #ifdef _UNICODE
  88. if(filename_w)
  89. result = _wopen(filename_w, oflag, pmode);
  90. free(filename_w);
  91. if(result != -1)
  92. return result;
  93. #endif
  94. return (_open)(filename, oflag, pmode);
  95. }
  96. FILE *curlx_win32_fopen(const char *filename, const char *mode)
  97. {
  98. #ifdef _UNICODE
  99. FILE *result = NULL;
  100. wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
  101. wchar_t *mode_w = curlx_convert_UTF8_to_wchar(mode);
  102. if(filename_w && mode_w)
  103. result = _wfopen(filename_w, mode_w);
  104. free(filename_w);
  105. free(mode_w);
  106. if(result)
  107. return result;
  108. #endif
  109. return (fopen)(filename, mode);
  110. }
  111. int curlx_win32_stat(const char *path, struct_stat *buffer)
  112. {
  113. int result = -1;
  114. #ifdef _UNICODE
  115. wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
  116. if(path_w) {
  117. #if defined(USE_WIN32_SMALL_FILES)
  118. result = _wstat(path_w, buffer);
  119. #else
  120. result = _wstati64(path_w, buffer);
  121. #endif
  122. free(path_w);
  123. if(result != -1)
  124. return result;
  125. }
  126. #endif /* _UNICODE */
  127. #if defined(USE_WIN32_SMALL_FILES)
  128. result = _stat(path, buffer);
  129. #else
  130. result = _stati64(path, buffer);
  131. #endif
  132. return result;
  133. }
  134. int curlx_win32_access(const char *path, int mode)
  135. {
  136. #if defined(_UNICODE)
  137. wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
  138. if(path_w) {
  139. int result = _waccess(path_w, mode);
  140. free(path_w);
  141. if(result != -1)
  142. return result;
  143. }
  144. #endif /* _UNICODE */
  145. return _access(path, mode);
  146. }
  147. #endif /* USE_WIN32_LARGE_FILES || USE_WIN32_SMALL_FILES */