curl_multibyte.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  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. }
  92. else
  93. errno = EINVAL;
  94. return result;
  95. #else
  96. return (_open)(filename, oflag, pmode);
  97. #endif
  98. }
  99. FILE *curlx_win32_fopen(const char *filename, const char *mode)
  100. {
  101. #ifdef _UNICODE
  102. FILE *result = NULL;
  103. wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
  104. wchar_t *mode_w = curlx_convert_UTF8_to_wchar(mode);
  105. if(filename_w && mode_w)
  106. result = _wfopen(filename_w, mode_w);
  107. else
  108. errno = EINVAL;
  109. free(filename_w);
  110. free(mode_w);
  111. return result;
  112. #else
  113. return (fopen)(filename, mode);
  114. #endif
  115. }
  116. int curlx_win32_stat(const char *path, struct_stat *buffer)
  117. {
  118. #ifdef _UNICODE
  119. int result = -1;
  120. wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
  121. if(path_w) {
  122. #if defined(USE_WIN32_SMALL_FILES)
  123. result = _wstat(path_w, buffer);
  124. #else
  125. result = _wstati64(path_w, buffer);
  126. #endif
  127. free(path_w);
  128. }
  129. else
  130. errno = EINVAL;
  131. return result;
  132. #else
  133. #if defined(USE_WIN32_SMALL_FILES)
  134. return _stat(path, buffer);
  135. #else
  136. return _stati64(path, buffer);
  137. #endif
  138. #endif
  139. }
  140. int curlx_win32_access(const char *path, int mode)
  141. {
  142. #if defined(_UNICODE)
  143. int result = -1;
  144. wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
  145. if(path_w) {
  146. result = _waccess(path_w, mode);
  147. free(path_w);
  148. }
  149. else
  150. errno = EINVAL;
  151. return result;
  152. #else
  153. return _access(path, mode);
  154. #endif
  155. }
  156. #endif /* USE_WIN32_LARGE_FILES || USE_WIN32_SMALL_FILES */