113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci/* 2613498266Sopenharmony_ci * This file is 'mem-include-scan' clean, which means memdebug.h and 2713498266Sopenharmony_ci * curl_memory.h are purposely not included in this file. See test 1132. 2813498266Sopenharmony_ci * 2913498266Sopenharmony_ci * The functions in this file are curlx functions which are not tracked by the 3013498266Sopenharmony_ci * curl memory tracker memdebug. 3113498266Sopenharmony_ci */ 3213498266Sopenharmony_ci 3313498266Sopenharmony_ci#include "curl_setup.h" 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci#if defined(_WIN32) 3613498266Sopenharmony_ci 3713498266Sopenharmony_ci#include "curl_multibyte.h" 3813498266Sopenharmony_ci 3913498266Sopenharmony_ci/* 4013498266Sopenharmony_ci * MultiByte conversions using Windows kernel32 library. 4113498266Sopenharmony_ci */ 4213498266Sopenharmony_ci 4313498266Sopenharmony_ciwchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8) 4413498266Sopenharmony_ci{ 4513498266Sopenharmony_ci wchar_t *str_w = NULL; 4613498266Sopenharmony_ci 4713498266Sopenharmony_ci if(str_utf8) { 4813498266Sopenharmony_ci int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, 4913498266Sopenharmony_ci str_utf8, -1, NULL, 0); 5013498266Sopenharmony_ci if(str_w_len > 0) { 5113498266Sopenharmony_ci str_w = malloc(str_w_len * sizeof(wchar_t)); 5213498266Sopenharmony_ci if(str_w) { 5313498266Sopenharmony_ci if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w, 5413498266Sopenharmony_ci str_w_len) == 0) { 5513498266Sopenharmony_ci free(str_w); 5613498266Sopenharmony_ci return NULL; 5713498266Sopenharmony_ci } 5813498266Sopenharmony_ci } 5913498266Sopenharmony_ci } 6013498266Sopenharmony_ci } 6113498266Sopenharmony_ci 6213498266Sopenharmony_ci return str_w; 6313498266Sopenharmony_ci} 6413498266Sopenharmony_ci 6513498266Sopenharmony_cichar *curlx_convert_wchar_to_UTF8(const wchar_t *str_w) 6613498266Sopenharmony_ci{ 6713498266Sopenharmony_ci char *str_utf8 = NULL; 6813498266Sopenharmony_ci 6913498266Sopenharmony_ci if(str_w) { 7013498266Sopenharmony_ci int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, 7113498266Sopenharmony_ci NULL, 0, NULL, NULL); 7213498266Sopenharmony_ci if(bytes > 0) { 7313498266Sopenharmony_ci str_utf8 = malloc(bytes); 7413498266Sopenharmony_ci if(str_utf8) { 7513498266Sopenharmony_ci if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes, 7613498266Sopenharmony_ci NULL, NULL) == 0) { 7713498266Sopenharmony_ci free(str_utf8); 7813498266Sopenharmony_ci return NULL; 7913498266Sopenharmony_ci } 8013498266Sopenharmony_ci } 8113498266Sopenharmony_ci } 8213498266Sopenharmony_ci } 8313498266Sopenharmony_ci 8413498266Sopenharmony_ci return str_utf8; 8513498266Sopenharmony_ci} 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci#endif /* _WIN32 */ 8813498266Sopenharmony_ci 8913498266Sopenharmony_ci#if defined(USE_WIN32_LARGE_FILES) || defined(USE_WIN32_SMALL_FILES) 9013498266Sopenharmony_ci 9113498266Sopenharmony_ciint curlx_win32_open(const char *filename, int oflag, ...) 9213498266Sopenharmony_ci{ 9313498266Sopenharmony_ci int pmode = 0; 9413498266Sopenharmony_ci 9513498266Sopenharmony_ci#ifdef _UNICODE 9613498266Sopenharmony_ci int result = -1; 9713498266Sopenharmony_ci wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename); 9813498266Sopenharmony_ci#endif 9913498266Sopenharmony_ci 10013498266Sopenharmony_ci va_list param; 10113498266Sopenharmony_ci va_start(param, oflag); 10213498266Sopenharmony_ci if(oflag & O_CREAT) 10313498266Sopenharmony_ci pmode = va_arg(param, int); 10413498266Sopenharmony_ci va_end(param); 10513498266Sopenharmony_ci 10613498266Sopenharmony_ci#ifdef _UNICODE 10713498266Sopenharmony_ci if(filename_w) { 10813498266Sopenharmony_ci result = _wopen(filename_w, oflag, pmode); 10913498266Sopenharmony_ci curlx_unicodefree(filename_w); 11013498266Sopenharmony_ci } 11113498266Sopenharmony_ci else 11213498266Sopenharmony_ci errno = EINVAL; 11313498266Sopenharmony_ci return result; 11413498266Sopenharmony_ci#else 11513498266Sopenharmony_ci return (_open)(filename, oflag, pmode); 11613498266Sopenharmony_ci#endif 11713498266Sopenharmony_ci} 11813498266Sopenharmony_ci 11913498266Sopenharmony_ciFILE *curlx_win32_fopen(const char *filename, const char *mode) 12013498266Sopenharmony_ci{ 12113498266Sopenharmony_ci#ifdef _UNICODE 12213498266Sopenharmony_ci FILE *result = NULL; 12313498266Sopenharmony_ci wchar_t *filename_w = curlx_convert_UTF8_to_wchar(filename); 12413498266Sopenharmony_ci wchar_t *mode_w = curlx_convert_UTF8_to_wchar(mode); 12513498266Sopenharmony_ci if(filename_w && mode_w) 12613498266Sopenharmony_ci result = _wfopen(filename_w, mode_w); 12713498266Sopenharmony_ci else 12813498266Sopenharmony_ci errno = EINVAL; 12913498266Sopenharmony_ci curlx_unicodefree(filename_w); 13013498266Sopenharmony_ci curlx_unicodefree(mode_w); 13113498266Sopenharmony_ci return result; 13213498266Sopenharmony_ci#else 13313498266Sopenharmony_ci return (fopen)(filename, mode); 13413498266Sopenharmony_ci#endif 13513498266Sopenharmony_ci} 13613498266Sopenharmony_ci 13713498266Sopenharmony_ciint curlx_win32_stat(const char *path, struct_stat *buffer) 13813498266Sopenharmony_ci{ 13913498266Sopenharmony_ci#ifdef _UNICODE 14013498266Sopenharmony_ci int result = -1; 14113498266Sopenharmony_ci wchar_t *path_w = curlx_convert_UTF8_to_wchar(path); 14213498266Sopenharmony_ci if(path_w) { 14313498266Sopenharmony_ci#if defined(USE_WIN32_SMALL_FILES) 14413498266Sopenharmony_ci result = _wstat(path_w, buffer); 14513498266Sopenharmony_ci#else 14613498266Sopenharmony_ci result = _wstati64(path_w, buffer); 14713498266Sopenharmony_ci#endif 14813498266Sopenharmony_ci curlx_unicodefree(path_w); 14913498266Sopenharmony_ci } 15013498266Sopenharmony_ci else 15113498266Sopenharmony_ci errno = EINVAL; 15213498266Sopenharmony_ci return result; 15313498266Sopenharmony_ci#else 15413498266Sopenharmony_ci#if defined(USE_WIN32_SMALL_FILES) 15513498266Sopenharmony_ci return _stat(path, buffer); 15613498266Sopenharmony_ci#else 15713498266Sopenharmony_ci return _stati64(path, buffer); 15813498266Sopenharmony_ci#endif 15913498266Sopenharmony_ci#endif 16013498266Sopenharmony_ci} 16113498266Sopenharmony_ci 16213498266Sopenharmony_ciint curlx_win32_access(const char *path, int mode) 16313498266Sopenharmony_ci{ 16413498266Sopenharmony_ci#if defined(_UNICODE) 16513498266Sopenharmony_ci int result = -1; 16613498266Sopenharmony_ci wchar_t *path_w = curlx_convert_UTF8_to_wchar(path); 16713498266Sopenharmony_ci if(path_w) { 16813498266Sopenharmony_ci result = _waccess(path_w, mode); 16913498266Sopenharmony_ci curlx_unicodefree(path_w); 17013498266Sopenharmony_ci } 17113498266Sopenharmony_ci else 17213498266Sopenharmony_ci errno = EINVAL; 17313498266Sopenharmony_ci return result; 17413498266Sopenharmony_ci#else 17513498266Sopenharmony_ci return _access(path, mode); 17613498266Sopenharmony_ci#endif 17713498266Sopenharmony_ci} 17813498266Sopenharmony_ci 17913498266Sopenharmony_ci#endif /* USE_WIN32_LARGE_FILES || USE_WIN32_SMALL_FILES */ 180