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 /*
24 * This file is 'mem-include-scan' clean. See test 1132.
25 */
26
27 #include "curl_setup.h"
28
29 #if defined(WIN32)
30
31 #include "curl_multibyte.h"
32
33 /*
34 * MultiByte conversions using Windows kernel32 library.
35 */
36
curlx_convert_UTF8_to_wchar(const char * str_utf8)37 wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8)
38 {
39 wchar_t *str_w = NULL;
40
41 if(str_utf8) {
42 int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
43 str_utf8, -1, NULL, 0);
44 if(str_w_len > 0) {
45 str_w = malloc(str_w_len * sizeof(wchar_t));
46 if(str_w) {
47 if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
48 str_w_len) == 0) {
49 free(str_w);
50 return NULL;
51 }
52 }
53 }
54 }
55
56 return str_w;
57 }
58
curlx_convert_wchar_to_UTF8(const wchar_t * str_w)59 char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w)
60 {
61 char *str_utf8 = NULL;
62
63 if(str_w) {
64 int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
65 NULL, 0, NULL, NULL);
66 if(bytes > 0) {
67 str_utf8 = malloc(bytes);
68 if(str_utf8) {
69 if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
70 NULL, NULL) == 0) {
71 free(str_utf8);
72 return NULL;
73 }
74 }
75 }
76 }
77
78 return str_utf8;
79 }
80
81 #endif /* WIN32 */
82
83 #if defined(USE_WIN32_LARGE_FILES) || defined(USE_WIN32_SMALL_FILES)
84
curlx_win32_fopen(const char * filename,const char * mode)85 FILE *curlx_win32_fopen(const char *filename, const char *mode)
86 {
87 #ifdef _UNICODE
88 FILE *result = NULL;
89 wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename);
90 wchar_t *mode_w = curlx_convert_UTF8_to_wchar(mode);
91 if(filename_w && mode_w)
92 result = _wfopen(filename_w, mode_w);
93 free(filename_w);
94 free(mode_w);
95 if(result)
96 return result;
97 #endif
98
99 return (fopen)(filename, mode);
100 }
101
curlx_win32_stat(const char * path,struct_stat * buffer)102 int curlx_win32_stat(const char *path, struct_stat *buffer)
103 {
104 int result = -1;
105 #ifdef _UNICODE
106 wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
107 #endif /* _UNICODE */
108
109 #if defined(USE_WIN32_SMALL_FILES)
110 #if defined(_UNICODE)
111 if(path_w)
112 result = _wstat(path_w, buffer);
113 else
114 #endif /* _UNICODE */
115 result = _stat(path, buffer);
116 #else /* USE_WIN32_SMALL_FILES */
117 #if defined(_UNICODE)
118 if(path_w)
119 result = _wstati64(path_w, buffer);
120 else
121 #endif /* _UNICODE */
122 result = _stati64(path, buffer);
123 #endif /* USE_WIN32_SMALL_FILES */
124
125 #ifdef _UNICODE
126 free(path_w);
127 #endif
128
129 return result;
130 }
131
curlx_win32_access(const char * path,int mode)132 int curlx_win32_access(const char *path, int mode)
133 {
134 int result = -1;
135 #ifdef _UNICODE
136 wchar_t *path_w = curlx_convert_UTF8_to_wchar(path);
137 #endif /* _UNICODE */
138
139 #if defined(_UNICODE)
140 if(path_w)
141 result = _waccess(path_w, mode);
142 else
143 #endif /* _UNICODE */
144 result = _access(path, mode);
145
146 #ifdef _UNICODE
147 free(path_w);
148 #endif
149
150 return result;
151 }
152
153 #endif /* USE_WIN32_LARGE_FILES || USE_WIN32_SMALL_FILES */
154