This commit is contained in:
rjkiv 2025-09-01 14:59:03 -07:00
parent f45dd85be3
commit 6f81e707ed
3 changed files with 93 additions and 2 deletions

View File

@ -1,4 +1,5 @@
#include "common.h"
#include <sys/random.h>
namespace advapi32 {
unsigned int WIN_FUNC RegOpenKeyExA(void *hKey, const char *lpSubKey, unsigned int ulOptions, void *samDesired, void **phkResult) {
@ -23,12 +24,25 @@ namespace advapi32 {
return false;
}
bool WIN_FUNC CryptGenRandom(void* hProv, unsigned int dwLen, char* pbBuffer){
DEBUG_LOG("STUB: CryptGenRandom(%p)\n", hProv);
if (!pbBuffer || dwLen == 0) return false;
ssize_t ret = getrandom(pbBuffer, dwLen, 0);
if (ret < 0 || (size_t)ret != dwLen) {
return false;
}
return true;
}
}
static void *resolveByName(const char *name) {
if (strcmp(name, "RegOpenKeyExA") == 0) return (void *) advapi32::RegOpenKeyExA;
if (strcmp(name, "CryptReleaseContext") == 0) return (void*) advapi32::CryptReleaseContext;
if (strcmp(name, "CryptAcquireContextW") == 0) return (void*) advapi32::CryptAcquireContextW;
if (strcmp(name, "CryptGenRandom") == 0) return (void*) advapi32::CryptGenRandom;
return nullptr;
}

View File

@ -17,6 +17,7 @@
#include <system_error>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/wait.h>
#include <spawn.h>
#include <vector>
@ -1709,6 +1710,30 @@ namespace kernel32 {
// return (void*)0x100003;
}
BOOL WIN_FUNC GetDiskFreeSpaceExW(const uint16_t* lpDirectoryName,
uint64_t* lpFreeBytesAvailableToCaller, uint64_t* lpTotalNumberOfBytes, uint64_t* lpTotalNumberOfFreeBytes){
if(!lpDirectoryName) return false;
std::string directoryName = wideStringToString(lpDirectoryName);
DEBUG_LOG("GetDiskFreeSpaceExW %s\n", directoryName.c_str());
struct statvfs buf;
if(statvfs(directoryName.c_str(), &buf) != 0){
return false;
}
if (lpFreeBytesAvailableToCaller)
*lpFreeBytesAvailableToCaller = (uint64_t)buf.f_bavail * buf.f_bsize;
if (lpTotalNumberOfBytes)
*lpTotalNumberOfBytes = (uint64_t)buf.f_blocks * buf.f_bsize;
if (lpTotalNumberOfFreeBytes)
*lpTotalNumberOfFreeBytes = (uint64_t)buf.f_bfree * buf.f_bsize;
DEBUG_LOG("\t-> available bytes %llu, total bytes %llu, total free bytes %llu\n",
*lpFreeBytesAvailableToCaller, *lpTotalNumberOfBytes, *lpTotalNumberOfFreeBytes);
return true;
}
void* WIN_FUNC LockResource(void* res) {
DEBUG_LOG("LockResource %p\n", res);
return (void*)0x100004;
@ -2622,6 +2647,7 @@ static void *resolveByName(const char *name) {
if (strcmp(name, "GetFileInformationByHandle") == 0) return (void *) kernel32::GetFileInformationByHandle;
if (strcmp(name, "GetTempFileNameA") == 0) return (void *) kernel32::GetTempFileNameA;
if (strcmp(name, "GetTempPathA") == 0) return (void *) kernel32::GetTempPathA;
if (strcmp(name, "GetDiskFreeSpaceExW") == 0) return (void*) kernel32::GetDiskFreeSpaceExW;
// sysinfoapi.h
if (strcmp(name, "GetSystemTime") == 0) return (void *) kernel32::GetSystemTime;

View File

@ -4,8 +4,10 @@
#include <cstdlib>
#include <cwchar>
#include <cwctype>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include "strutil.h"
typedef void (*_PVFV)();
@ -273,7 +275,7 @@ namespace msvcrt {
uint16_t* WIN_ENTRY _wcsdup(const uint16_t *strSource){
std::string src_str = wideStringToString(strSource);
DEBUG_LOG("_wcsdup: %s", src_str.c_str());
// DEBUG_LOG("_wcsdup: %s", src_str.c_str());
if(!strSource) return nullptr;
size_t strLen = wstrlen(strSource);
@ -285,10 +287,16 @@ namespace msvcrt {
}
std::string dst_str = wideStringToString(dup);
DEBUG_LOG(" --> %s\n", dst_str.c_str());
// DEBUG_LOG(" --> %s\n", dst_str.c_str());
return dup;
}
int WIN_ENTRY _waccess_s(const uint16_t* path, int mode){
std::string str = wideStringToString(path);
DEBUG_LOG("_waccess_s %s\n", str.c_str());
return access(str.c_str(), mode);
}
void* WIN_ENTRY memset(void *s, int c, size_t n){
return std::memset(s, c, n);
}
@ -424,6 +432,43 @@ namespace msvcrt {
return wstrtoul(strSource, endptr, base);
}
int WIN_ENTRY _dup2(int fd1, int fd2){
return dup2(fd1, fd2);
}
FILE* WIN_ENTRY _wfsopen(const uint16_t* filename, const uint16_t* mode, int shflag){
if (!filename || !mode) return nullptr;
std::string fname_str = wideStringToString(filename);
std::string mode_str = wideStringToString(mode);
DEBUG_LOG("_wfsopen file %s, mode %s\n", fname_str.c_str(), mode_str.c_str());
return fopen(fname_str.c_str(), mode_str.c_str());
}
int WIN_ENTRY fputws(const uint16_t* str, FILE* stream){
if(!str || !stream) return EOF;
std::string fname_str = wideStringToString(str);
DEBUG_LOG("fputws %s\n", fname_str.c_str());
if(fputs(fname_str.c_str(), stream) < 0) return EOF;
else return 0;
}
int WIN_ENTRY fclose(FILE* stream){
return ::fclose(stream);
}
int WIN_ENTRY _flushall(){
DEBUG_LOG("flushall\n");
int count = 0;
if (fflush(stdin) == 0) count++;
if (fflush(stdout) == 0) count++;
if (fflush(stderr) == 0) count++;
return count;
}
}
@ -466,6 +511,12 @@ static void *resolveByName(const char *name) {
if (strcmp(name, "wcschr") == 0) return (void*)msvcrt::wcschr;
if (strcmp(name, "getenv") == 0) return (void*)msvcrt::getenv;
if (strcmp(name, "_wgetenv_s") == 0) return (void*)msvcrt::_wgetenv_s;
if (strcmp(name, "_waccess_s") == 0) return (void*)msvcrt::_waccess_s;
if (strcmp(name, "_dup2") == 0) return (void*)msvcrt::_dup2;
if (strcmp(name, "_wfsopen") == 0) return (void*)msvcrt::_wfsopen;
if (strcmp(name, "fputws") == 0) return (void*)msvcrt::fputws;
if (strcmp(name, "fclose") == 0) return (void*)msvcrt::fclose;
if (strcmp(name, "_flushall") == 0) return (void*)msvcrt::_flushall;
return nullptr;
}