Files

76 lines
2.0 KiB
C++
Raw Permalink Normal View History

2022-06-28 23:08:23 +02:00
#include "common.h"
2025-09-26 01:51:25 -06:00
#include "strutil.h"
2022-06-28 23:08:23 +02:00
namespace user32 {
2025-09-26 01:51:25 -06:00
constexpr uint32_t RT_STRING_ID = 6;
2022-06-29 13:19:45 +02:00
2022-06-28 23:08:23 +02:00
int WIN_FUNC LoadStringA(void* hInstance, unsigned int uID, char* lpBuffer, int cchBufferMax) {
2025-09-26 01:51:25 -06:00
DEBUG_LOG("LoadStringA %p %u %d\n", hInstance, uID, cchBufferMax);
if (!lpBuffer || cchBufferMax <= 0) {
return 0;
}
wibo::Executable *mod = wibo::executableFromModule((HMODULE) hInstance);
2023-10-01 23:56:35 -04:00
if (!mod) {
return 0;
}
2025-09-26 01:51:25 -06:00
wibo::ResourceIdentifier type = wibo::ResourceIdentifier::fromID(RT_STRING_ID);
wibo::ResourceIdentifier table = wibo::ResourceIdentifier::fromID((uID >> 4) + 1);
wibo::ResourceLocation loc;
if (!mod->findResource(type, table, std::nullopt, loc)) {
2022-06-29 13:19:45 +02:00
return 0;
}
2025-09-26 01:51:25 -06:00
const uint16_t *cursor = reinterpret_cast<const uint16_t *>(loc.data);
const uint16_t *end = cursor + (loc.size / sizeof(uint16_t));
unsigned int entryIndex = uID & 0x0Fu;
for (unsigned int i = 0; i < entryIndex; ++i) {
if (cursor >= end) {
return 0;
}
uint16_t length = *cursor++;
if (cursor + length > end) {
return 0;
}
cursor += length;
2022-06-29 13:19:45 +02:00
}
2025-09-26 01:51:25 -06:00
if (cursor >= end) {
return 0;
}
uint16_t length = *cursor++;
if (cursor + length > end) {
return 0;
}
int copyLength = length;
if (copyLength > cchBufferMax - 1) {
copyLength = cchBufferMax - 1;
}
for (int i = 0; i < copyLength; ++i) {
lpBuffer[i] = static_cast<char>(cursor[i] & 0xFF);
}
lpBuffer[copyLength] = 0;
DEBUG_LOG("LoadStringA -> %.*s\n", copyLength, lpBuffer);
return copyLength;
2022-06-28 23:08:23 +02:00
}
2022-07-27 12:28:29 +02:00
int WIN_FUNC MessageBoxA(void *hwnd, const char *lpText, const char *lpCaption, unsigned int uType) {
printf("MESSAGE BOX: [%s] %s\n", lpCaption, lpText);
fflush(stdout);
return 1;
}
2022-06-28 23:08:23 +02:00
}
static void *resolveByName(const char *name) {
2022-06-28 23:08:23 +02:00
if (strcmp(name, "LoadStringA") == 0) return (void *) user32::LoadStringA;
2022-07-27 12:28:29 +02:00
if (strcmp(name, "MessageBoxA") == 0) return (void *) user32::MessageBoxA;
return nullptr;
2022-06-28 23:08:23 +02:00
}
wibo::Module lib_user32 = {
(const char *[]){
"user32",
"user32.dll",
nullptr,
},
resolveByName,
nullptr,
};