wideStringToString no longer cuts off wide chars

This commit is contained in:
rjkiv 2025-09-02 12:04:40 -07:00
parent 2650eaa946
commit e92f6e911d
3 changed files with 26 additions and 11 deletions

View File

@ -25,7 +25,7 @@ namespace advapi32 {
return false;
}
bool WIN_FUNC CryptGenRandom(void* hProv, unsigned int dwLen, char* pbBuffer){
bool WIN_FUNC CryptGenRandom(void* hProv, unsigned int dwLen, unsigned char* pbBuffer){
DEBUG_LOG("STUB: CryptGenRandom(%p)\n", hProv);
if (!pbBuffer || dwLen == 0) return false;

View File

@ -369,15 +369,12 @@ namespace msvcrt {
std::string src_str = wideStringToString(src);
DEBUG_LOG("wcscpy_s %s\n", src_str.c_str());
if (!dest || !src || dest_size == 0) {
return -1;
return 22;
}
size_t src_len = 0;
while (src[src_len] != 0) src_len++;
if (src_len + 1 > dest_size) {
if (wstrlen(src) + 1 > dest_size) {
dest[0] = 0;
return -1;
return 34;
}
wstrcpy(dest, src);

View File

@ -2,6 +2,9 @@
#include "strings.h"
#include <cstdint>
#include <vector>
#include <string>
#include <locale>
#include <codecvt>
size_t wstrlen(const uint16_t *str) {
size_t len = 0;
@ -108,14 +111,29 @@ size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n) {
}
std::string wideStringToString(const uint16_t *src, int len = -1) {
if(!src) return std::string();
if (len < 0) {
len = src ? wstrlen(src) : 0;
}
std::string res(len, '\0');
for (int i = 0; i < len; i++) {
res[i] = src[i] & 0xFF;
std::u16string u16str;
for(const uint16_t* p = src; *p != 0; p++){
u16str.push_back(*p);
}
return res;
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(reinterpret_cast<const char16_t*>(u16str.data()));
// the old implementation
// std::string res(len, '\0');
// for (int i = 0; i < len; i++) {
// if(src[i] > 255){
// DEBUG_LOG("Encountered wide char with value 0x%X!\n", src[i]);
// assert(src[i] <= 255);
// }
// res[i] = src[i] & 0xFF;
// }
// return res;
}
std::vector<uint16_t> stringToWideString(const char *src) {