mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-20 10:13:17 +00:00
fix
This commit is contained in:
@@ -125,7 +125,7 @@ int __fastcall util_sha384file(char* filename, char* result)
|
||||
|
||||
if (filename == NULL) return -1;
|
||||
#ifdef WIN32
|
||||
fopen_s(&pFile, filename, "rbN");
|
||||
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rbN");
|
||||
#else
|
||||
pFile = fopen(filename, "rb");
|
||||
#endif
|
||||
@@ -264,7 +264,7 @@ size_t __fastcall util_writefile(char* filename, char* data, int datalen)
|
||||
size_t count = 0;
|
||||
|
||||
#ifdef WIN32
|
||||
fopen_s(&pFile, filename, "wbN");
|
||||
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"wbN");
|
||||
#else
|
||||
pFile = fopen(filename, "wb");
|
||||
#endif
|
||||
@@ -283,7 +283,7 @@ size_t __fastcall util_appendfile(char* filename, char* data, int datalen)
|
||||
size_t count = 0;
|
||||
|
||||
#ifdef WIN32
|
||||
fopen_s(&pFile, filename, "abN");
|
||||
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"abN");
|
||||
#else
|
||||
pFile = fopen(filename, "ab");
|
||||
#endif
|
||||
@@ -306,7 +306,7 @@ size_t __fastcall util_readfile(char* filename, char** data, size_t maxlen)
|
||||
if (filename == NULL) return 0;
|
||||
|
||||
#ifdef WIN32
|
||||
fopen_s(&pFile, filename, "rbN");
|
||||
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rbN");
|
||||
#else
|
||||
pFile = fopen(filename, "rb");
|
||||
#endif
|
||||
@@ -367,7 +367,11 @@ int __fastcall util_readfile2(char* filename, char** data)
|
||||
int __fastcall util_deletefile(char* filename)
|
||||
{
|
||||
if (filename == NULL) return 0;
|
||||
return remove(filename);
|
||||
#ifdef WIN32
|
||||
return(_wremove(ILibUTF8ToWide(filename, -1)));
|
||||
#else
|
||||
return(remove(filename));
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
@@ -407,8 +411,26 @@ BOOL util_CopyFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName, _I
|
||||
return (CopyFile2(lpExistingFileNameW, lpNewFileNameW, NULL) == S_OK);
|
||||
}
|
||||
#else
|
||||
BOOL util_MoveFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName) { return MoveFileA(lpExistingFileName, lpNewFileName); }
|
||||
BOOL util_CopyFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName, _In_ BOOL bFailIfExists) { return CopyFileA(lpExistingFileName, lpNewFileName, bFailIfExists); }
|
||||
BOOL util_MoveFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName)
|
||||
{
|
||||
WCHAR wExisting[4096];
|
||||
WCHAR wNew[4096];
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpExistingFileName, -1, (LPWSTR)wExisting, (int)sizeof(wExisting) / 2);
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpNewFileName, -1, (LPWSTR)wNew, (int)sizeof(wNew) / 2);
|
||||
|
||||
return MoveFileW(wExisting, wNew);
|
||||
}
|
||||
BOOL util_CopyFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName, _In_ BOOL bFailIfExists)
|
||||
{
|
||||
WCHAR wExisting[4096];
|
||||
WCHAR wNew[4096];
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpExistingFileName, -1, (LPWSTR)wExisting, (int)sizeof(wExisting) / 2);
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpNewFileName, -1, (LPWSTR)wNew, (int)sizeof(wNew) / 2);
|
||||
|
||||
return CopyFileW(wExisting, wNew, bFailIfExists);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -593,7 +615,7 @@ int __fastcall util_from_pem(char* filename, struct util_cert* cert)
|
||||
|
||||
if (filename == NULL) return -1;
|
||||
#ifdef WIN32
|
||||
fopen_s(&pFile, filename, "rbN");
|
||||
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rbN");
|
||||
#else
|
||||
pFile = fopen(filename, "rb");
|
||||
#endif
|
||||
|
||||
@@ -179,6 +179,22 @@ int ILibWhichPowerOfTwo(int number)
|
||||
return retVal;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
WCHAR ILibWideScratchPad[4096];
|
||||
char ILibUTF8ScratchPad[4096];
|
||||
char *ILibWideToUTF8(WCHAR* wstr, int len)
|
||||
{
|
||||
WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)wstr, len, (LPSTR)ILibUTF8ScratchPad, (int)sizeof(ILibUTF8ScratchPad), NULL, NULL);
|
||||
return((char*)ILibUTF8ScratchPad);
|
||||
}
|
||||
WCHAR* ILibUTF8ToWideEx(char* str, int len, WCHAR* buffer, int bufferCharacterSize)
|
||||
{
|
||||
if (buffer == NULL) { buffer = (WCHAR*)ILibWideScratchPad; bufferCharacterSize = (int)sizeof(ILibWideScratchPad) / 2; }
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)str, len, (LPWSTR)buffer, bufferCharacterSize);
|
||||
return(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
//
|
||||
// All of the following structures are meant to be private internal structures
|
||||
@@ -8062,7 +8078,7 @@ int ILibReadFileFromDiskEx(char **Target, char *FileName)
|
||||
FILE *SourceFile = NULL;
|
||||
|
||||
#ifdef WIN32
|
||||
fopen_s(&SourceFile, FileName, "rb");
|
||||
_wfopen_s(&SourceFile, ILibUTF8ToWide(FileName, -1), L"rb");
|
||||
#else
|
||||
SourceFile = fopen(FileName, "rb");
|
||||
#endif
|
||||
@@ -8111,7 +8127,7 @@ void ILibWriteStringToDiskEx(char *FileName, char *data, int dataLen)
|
||||
FILE *SourceFile = NULL;
|
||||
|
||||
#ifdef WIN32
|
||||
fopen_s(&SourceFile, FileName, "wb");
|
||||
_wfopen_s(&SourceFile, ILibUTF8ToWide(FileName, -1), L"wb");
|
||||
#else
|
||||
SourceFile = fopen(FileName, "wb");
|
||||
#endif
|
||||
@@ -8127,7 +8143,7 @@ void ILibAppendStringToDiskEx(char *FileName, char *data, int dataLen)
|
||||
FILE *SourceFile = NULL;
|
||||
|
||||
#ifdef WIN32
|
||||
fopen_s(&SourceFile, FileName, "ab");
|
||||
_wfopen_s(&SourceFile, ILibUTF8ToWide(FileName, -1), L"ab");
|
||||
#else
|
||||
SourceFile = fopen(FileName, "ab");
|
||||
#endif
|
||||
@@ -9647,7 +9663,7 @@ void ILibLinkedList_FileBacked_Reset(ILibLinkedList_FileBacked_Root *root)
|
||||
fclose(source);
|
||||
source = NULL;
|
||||
#ifdef WIN32
|
||||
fopen_s(&source, (char*)ptr[1], "wb+N");
|
||||
_wfopen_s(&source, ILibUTF8ToWide((char*)ptr[1], -1), L"wb+N");
|
||||
#else
|
||||
source = fopen((char*)ptr[1], "wb+");
|
||||
#endif
|
||||
@@ -9721,9 +9737,9 @@ ILibLinkedList_FileBacked_Root* ILibLinkedList_FileBacked_Create(char* path, uns
|
||||
ILibLinkedList_FileBacked_Root *retVal = NULL;
|
||||
|
||||
#ifdef WIN32
|
||||
if (fopen_s(&source, path, "rb+N") != 0)
|
||||
if (_wfopen_s(&source, ILibUTF8ToWide(path, -1), L"rb+N") != 0)
|
||||
{
|
||||
fopen_s(&source, path, "wb+N");
|
||||
_wfopen_s(&source, ILibUTF8ToWide(path, -1), L"wb+N");
|
||||
}
|
||||
#else
|
||||
if ((source = fopen(path, "rb+")) == NULL)
|
||||
|
||||
@@ -170,6 +170,21 @@ static inline void ignore_result(uintptr_t result) { (void)result; }
|
||||
#define PRINTERROR()
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
char *ILibWideToUTF8(WCHAR* wstr, int len);
|
||||
WCHAR* ILibUTF8ToWideEx(char* str, int len, WCHAR* buffer, int bufferCharacterSize);
|
||||
#define ILibUTF8ToWide(utf8string, len) ILibUTF8ToWideEx(utf8string, len, NULL, 0)
|
||||
#else
|
||||
#define ILibWideToUTF8(wstr, len) (wstr)
|
||||
#define ILibUTF8toWide(str, len) (str)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int ILibGetLocalTime(char *dest, int destLen);
|
||||
long ILibGetTimeStamp();
|
||||
|
||||
|
||||
@@ -797,7 +797,7 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
|
||||
if (needSetSid != 0) { spawnType ^= ILibProcessPipe_SpawnTypes_POSIX_DETACHED; }
|
||||
|
||||
#ifdef WIN32
|
||||
STARTUPINFOA info = { 0 };
|
||||
STARTUPINFOW info = { 0 };
|
||||
PROCESS_INFORMATION processInfo = { 0 };
|
||||
SECURITY_ATTRIBUTES saAttr;
|
||||
char* parms = NULL;
|
||||
@@ -806,7 +806,7 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
|
||||
int allocParms = 0;
|
||||
|
||||
ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));
|
||||
ZeroMemory(&info, sizeof(STARTUPINFOA));
|
||||
ZeroMemory(&info, sizeof(STARTUPINFOW));
|
||||
|
||||
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_SPECIFIED_USER && spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && (sessionId = WTSGetActiveConsoleSessionId()) == 0xFFFFFFFF) { return(NULL); } // No session attached to console, but requested to execute as logged in user
|
||||
@@ -817,7 +817,7 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
|
||||
if (DuplicateTokenEx(token, MAXIMUM_ALLOWED, 0, SecurityImpersonation, TokenPrimary, &userToken) == 0) { CloseHandle(token); ILIBMARKPOSITION(2); return(NULL); }
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_SPECIFIED_USER) { sessionId = (DWORD)(uint64_t)sid; }
|
||||
if (SetTokenInformation(userToken, (TOKEN_INFORMATION_CLASS)TokenSessionId, &sessionId, sizeof(sessionId)) == 0) { CloseHandle(token); CloseHandle(userToken); ILIBMARKPOSITION(2); return(NULL); }
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_WINLOGON) { info.lpDesktop = "Winsta0\\Winlogon"; }
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_WINLOGON) { info.lpDesktop = L"Winsta0\\Winlogon"; }
|
||||
}
|
||||
if (parameters != NULL && parameters[0] != NULL && parameters[1] == NULL)
|
||||
{
|
||||
@@ -861,8 +861,10 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
|
||||
retVal->parent = (ILibProcessPipe_Manager_Object*)pipeManager;
|
||||
retVal->chain = retVal->parent->ChainLink.ParentChain;
|
||||
#ifdef WIN32
|
||||
WCHAR tmp1[4096];
|
||||
WCHAR tmp2[4096];
|
||||
|
||||
info.cb = sizeof(STARTUPINFOA);
|
||||
info.cb = sizeof(STARTUPINFOW);
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
retVal->stdIn = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
@@ -880,8 +882,8 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
|
||||
info.dwFlags |= STARTF_USESTDHANDLES;
|
||||
}
|
||||
|
||||
if (((spawnType == ILibProcessPipe_SpawnTypes_DEFAULT || spawnType == ILibProcessPipe_SpawnTypes_DETACHED) && !CreateProcessA(target, parms, NULL, NULL, spawnType == ILibProcessPipe_SpawnTypes_DETACHED ? FALSE: TRUE, CREATE_NO_WINDOW | (needSetSid !=0? (DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)) ||
|
||||
(spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessAsUserA(userToken, target, parms, NULL, NULL, TRUE, CREATE_NO_WINDOW | (needSetSid != 0 ? (DETACHED_PROCESS| CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)))
|
||||
if (((spawnType == ILibProcessPipe_SpawnTypes_DEFAULT || spawnType == ILibProcessPipe_SpawnTypes_DETACHED) && !CreateProcessW(ILibUTF8ToWideEx(target, -1, tmp1, (int)sizeof(tmp1)/2), ILibUTF8ToWideEx(parms, -1, tmp2, (int)sizeof(tmp2)/2), NULL, NULL, spawnType == ILibProcessPipe_SpawnTypes_DETACHED ? FALSE: TRUE, CREATE_NO_WINDOW | (needSetSid !=0? (DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)) ||
|
||||
(spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessAsUserW(userToken, ILibUTF8ToWideEx(target, -1, tmp1, (int)sizeof(tmp1)/2), ILibUTF8ToWideEx(parms, -1, tmp2, (int)sizeof(tmp2)/2), NULL, NULL, TRUE, CREATE_NO_WINDOW | (needSetSid != 0 ? (DETACHED_PROCESS| CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)))
|
||||
{
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
|
||||
@@ -501,18 +501,18 @@ FILE* ILibSimpleDataStore_OpenFileEx2(char* filePath, int forceTruncateIfNonZero
|
||||
HANDLE h = NULL;
|
||||
if (forceTruncateIfNonZero != 0)
|
||||
{
|
||||
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
|
||||
{
|
||||
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
|
||||
{
|
||||
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
}
|
||||
}
|
||||
int fd = _open_osfhandle((intptr_t)h, _O_RDWR);
|
||||
@@ -522,7 +522,7 @@ FILE* ILibSimpleDataStore_OpenFileEx2(char* filePath, int forceTruncateIfNonZero
|
||||
}
|
||||
else
|
||||
{
|
||||
HANDLE h = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
HANDLE h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE) { return(NULL); }
|
||||
int fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
|
||||
if (fd == -1) { CloseHandle(h); return(NULL); }
|
||||
@@ -547,7 +547,7 @@ FILE* ILibSimpleDataStore_OpenFileEx2(char* filePath, int forceTruncateIfNonZero
|
||||
int ILibSimpleDataStore_Exists(char *filePath)
|
||||
{
|
||||
#ifdef WIN32
|
||||
return(_access(filePath, 0) == 0 ? 1 : 0);
|
||||
return(_waccess(ILibUTF8ToWide(filePath, -1), 0) == 0 ? 1 : 0);
|
||||
#else
|
||||
return(access(filePath, 0) == 0 ? 1 : 0);
|
||||
#endif
|
||||
@@ -858,8 +858,10 @@ __EXPORT_TYPE int ILibSimpleDataStore_Compact(ILibSimpleDataStore dataStore)
|
||||
|
||||
// Now we copy the temporary data store over the data store, making it the new valid version
|
||||
#ifdef WIN32
|
||||
if (CopyFileA(tmp, root->filePath, FALSE) == FALSE) { retVal = 1; }
|
||||
DeleteFile(tmp);
|
||||
WCHAR tmptmp[4096];
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)tmp, -1, (LPWSTR)tmptmp, (int)sizeof(tmptmp) / 2);
|
||||
if (CopyFileW(tmptmp, ILibUTF8ToWide(root->filePath, -1), FALSE) == FALSE) { retVal = 1; }
|
||||
DeleteFileW(tmptmp);
|
||||
#else
|
||||
if (rename(tmp, root->filePath) != 0) { retVal = 1; }
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user