curl_multibyte.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. /*
  23. * This file is 'mem-include-scan' clean. See test 1132.
  24. */
  25. #include "curl_setup.h"
  26. #if defined(WIN32)
  27. #include "curl_multibyte.h"
  28. /*
  29. * MultiByte conversions using Windows kernel32 library.
  30. */
  31. wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8)
  32. {
  33. wchar_t *str_w = NULL;
  34. if(str_utf8) {
  35. int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  36. str_utf8, -1, NULL, 0);
  37. if(str_w_len > 0) {
  38. str_w = malloc(str_w_len * sizeof(wchar_t));
  39. if(str_w) {
  40. if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
  41. str_w_len) == 0) {
  42. free(str_w);
  43. return NULL;
  44. }
  45. }
  46. }
  47. }
  48. return str_w;
  49. }
  50. char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w)
  51. {
  52. char *str_utf8 = NULL;
  53. if(str_w) {
  54. int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
  55. NULL, 0, NULL, NULL);
  56. if(bytes > 0) {
  57. str_utf8 = malloc(bytes);
  58. if(str_utf8) {
  59. if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
  60. NULL, NULL) == 0) {
  61. free(str_utf8);
  62. return NULL;
  63. }
  64. }
  65. }
  66. }
  67. return str_utf8;
  68. }
  69. #endif /* WIN32 */
  70. #if defined(USE_WIN32_LARGE_FILES) || defined(USE_WIN32_SMALL_FILES)
  71. FILE *curlx_win32_fopen(const char *filename, const char *mode)
  72. {
  73. #ifdef _UNICODE
  74. FILE *result = NULL;
  75. wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
  76. wchar_t *mode_w = curlx_convert_UTF8_to_wchar(mode);
  77. if(filename_w && mode_w)
  78. result = _wfopen(filename_w, mode_w);
  79. free(filename_w);
  80. free(mode_w);
  81. if(result)
  82. return result;
  83. #endif
  84. return (fopen)(filename, mode);
  85. }
  86. int curlx_win32_stat(const char *path, struct_stat *buffer)
  87. {
  88. int result = -1;
  89. #ifdef _UNICODE
  90. wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
  91. #endif /* _UNICODE */
  92. #if defined(USE_WIN32_SMALL_FILES)
  93. #if defined(_UNICODE)
  94. if(path_w)
  95. result = _wstat(path_w, buffer);
  96. else
  97. #endif /* _UNICODE */
  98. result = _stat(path, buffer);
  99. #else /* USE_WIN32_SMALL_FILES */
  100. #if defined(_UNICODE)
  101. if(path_w)
  102. result = _wstati64(path_w, buffer);
  103. else
  104. #endif /* _UNICODE */
  105. result = _stati64(path, buffer);
  106. #endif /* USE_WIN32_SMALL_FILES */
  107. #ifdef _UNICODE
  108. free(path_w);
  109. #endif
  110. return result;
  111. }
  112. #endif /* USE_WIN32_LARGE_FILES || USE_WIN32_SMALL_FILES */