commit 5f783c8f329cccfa6fd2688d06aace5b657cf5aa
parent b9a3171ba16670455695307be01179bce91f619f
Author: anon <anon@example.com>
Date: Fri, 16 Oct 2026 07:13:26 +0000
Support UTF-8 keyboard input and verify Linux menus and movement
Diffstat:
4 files changed, 148 insertions(+), 0 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -214,6 +214,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/data-record
./build/linux/qsort
./build/linux/finite
+ ./build/linux/wide-utf8
build/linux/surfaces: tests/surfaces.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/linux_window.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h
mkdir -p $(@D)
@@ -358,3 +359,8 @@ build/linux/finite: tests/finite.c runtime/guest_crt.c runtime/guest_heap.c runt
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
test: build/linux/finite
+
+build/linux/wide-utf8: tests/wide_utf8.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_platform.h runtime/guest_memory.h
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -o $@
+
+test: build/linux/wide-utf8
diff --git a/decompiled/runtime/linux_platform.c b/decompiled/runtime/linux_platform.c
@@ -325,6 +325,59 @@ static void create_directory(void)
g_esp += 12;
}
+static void wide_char_to_multi_byte(void)
+{
+ uint32_t code_page = MEM32(g_esp + 4), flags = MEM32(g_esp + 8);
+ uint32_t input = MEM32(g_esp + 12), output = MEM32(g_esp + 20);
+ int32_t count = MEM32(g_esp + 16), capacity = MEM32(g_esp + 24);
+ uint32_t default_char = MEM32(g_esp + 28), used_default = MEM32(g_esp + 32);
+ g_esp += 36;
+ g_eax = 0;
+ if (code_page != 65001) {
+ fprintf(stderr, "Unsupported WideCharToMultiByte code page %u\n", code_page); exit(3);
+ }
+ if (flags & ~0x80u) { MEM32(g_fs_base + 0x34) = 1004; return; }
+ if (!input || !count || capacity < 0 || (capacity && !output) ||
+ input == output || default_char || used_default) {
+ MEM32(g_fs_base + 0x34) = 87; return;
+ }
+
+ /* Negative counts include the terminator; positive counts consume exact UTF-16 units. */
+ uint32_t written = 0;
+ int invalid = 0;
+ for (uint32_t i = 0; count < 0 || i < (uint32_t)count; ++i) {
+ uint32_t character = MEM16(input + i * 2);
+ if (character >= 0xd800 && character <= 0xdbff && (count < 0 || i + 1 < (uint32_t)count)) {
+ uint32_t low = MEM16(input + (i + 1) * 2);
+ if (low >= 0xdc00 && low <= 0xdfff) {
+ character = 0x10000 + ((character - 0xd800) << 10) + low - 0xdc00;
+ ++i;
+ }
+ }
+ if (character >= 0xd800 && character <= 0xdfff) {
+ invalid = 1;
+ character = 0xfffd;
+ }
+ unsigned length = character < 0x80 ? 1 : character < 0x800 ? 2 : character < 0x10000 ? 3 : 4;
+ if (written > INT32_MAX - length || (capacity && written + length > (uint32_t)capacity)) {
+ MEM32(g_fs_base + 0x34) = 122; return;
+ }
+ if (capacity) {
+ uint32_t value = character;
+ for (unsigned j = length - 1; j; --j) {
+ MEM8(output + written + j) = 0x80 | (value & 0x3f);
+ value >>= 6;
+ }
+ MEM8(output + written) = length == 1 ? value : (length == 2 ? 0xc0 : length == 3 ? 0xe0 : 0xf0) | value;
+ }
+ written += length;
+ if (count < 0 && !character) break;
+ }
+ /* Windows retains replacement output even when strict validation fails. */
+ if (invalid && (flags & 0x80)) MEM32(g_fs_base + 0x34) = 1113;
+ else g_eax = written;
+}
+
static void set_handle_count(void)
{
/* Win32 keeps this DOS compatibility call as a no-op. */
@@ -376,6 +429,7 @@ recomp_func_t linux_platform_lookup(const char *dll, const char *name)
{"DeleteFileA", delete_file},
{"CreateDirectoryA", create_directory},
{"SetHandleCount", set_handle_count},
+ {"WideCharToMultiByte", wide_char_to_multi_byte},
{"GlobalMemoryStatus", global_memory_status},
{"InterlockedExchange", interlocked_exchange},
{"InterlockedIncrement", interlocked_increment},
diff --git a/decompiled/tests/wide_utf8.c b/decompiled/tests/wide_utf8.c
@@ -0,0 +1,68 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include "linux_platform.h"
+#include "guest_memory.h"
+#endif
+
+int main(void)
+{
+ const uint16_t inputs[][16] = {{'i'}, {0}, {0x7f,0x80,0x7ff,0x800,0xd7ff,0xe000,0xffff},
+ {0x20ac}, {0xd83d,0xde00}, {0xdbff,0xdfff}, {0xd800}, {0xdc00},
+ {'a',0xd800,'b'}, {0xdc00,0xd800}, {0xd800,0xd800,0xdc00}, {'a',0,'b'}};
+ const int counts[] = {1,1,7,1,2,2,1,1,3,2,3,3};
+#ifndef _WIN32
+ assert(guest_map("UTF conversion", 0x50000000, 0x30000));
+ g_fs_base = 0x50000000;
+#endif
+ uint32_t digest = 2166136261u;
+ for (unsigned sample = 0; sample < 12; ++sample)
+ for (unsigned terminated = 0; terminated < 3; ++terminated)
+ for (unsigned strict = 0; strict < 2; ++strict)
+ for (int capacity = 0; capacity <= 24; ++capacity) {
+ unsigned char output[32];
+ memset(output, 0xa5, sizeof(output));
+ int count = terminated ? -(int)terminated : counts[sample];
+ uint32_t flags = strict ? 0x80 : 0;
+#ifdef _WIN32
+ SetLastError(777);
+#else
+ memcpy((void *)ADDR(0x5001fff9), inputs[sample], 32);
+ memcpy((void *)ADDR(0x5000fff9), output, 32);
+ MEM32(g_fs_base + 0x34) = 777;
+ g_esp = 0x50000300;
+ const uint32_t args[] = {RECOMP_RETADDR, 65001, flags, 0x5001fff9, count, 0x5000fff9, capacity, 0, 0};
+ memcpy((void *)ADDR(g_esp), args, sizeof(args));
+ g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
+#endif
+
+#ifdef _WIN32
+ int result = WideCharToMultiByte(65001, flags, (const WCHAR *)inputs[sample], count,
+ (char *)output, capacity, NULL, NULL);
+ uint32_t error = GetLastError();
+#else
+ linux_platform_lookup("kernel32.dll", "WideCharToMultiByte")();
+ int result = g_eax;
+ uint32_t error = MEM32(g_fs_base + 0x34);
+ memcpy(output, (void *)ADDR(0x5000fff9), 32);
+#endif
+
+ if (result) assert(error == 777);
+ else assert(error == 122 || error == 1113);
+ for (unsigned i = capacity; i < sizeof(output); ++i) assert(output[i] == 0xa5);
+ if (!sample && capacity) assert(result == (terminated ? 2 : 1) || capacity == 1);
+ if (!sample && capacity >= 2) assert(output[0] == 'i' && output[1] == (terminated ? 0 : 0xa5));
+#ifndef _WIN32
+ assert(g_esp == 0x50000324 && g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
+ assert(!memcmp((void *)ADDR(0x5001fff9), inputs[sample], 32));
+#endif
+ digest = (digest ^ result) * 16777619u;
+ digest = (digest ^ error) * 16777619u;
+ for (unsigned i = 0; i < sizeof(output); ++i) digest = (digest ^ output[i]) * 16777619u;
+ }
+ printf("WideCharToMultiByte UTF-8: 1800 length/capacity/surrogate cases, bytes, errors, canaries and ABI; digest %08x\n", digest);
+}
diff --git a/tools/reconstruction/test_portable_wide_utf8.py b/tools/reconstruction/test_portable_wide_utf8.py
@@ -0,0 +1,20 @@
+"""Compare UTF-16 to UTF-8 conversion with Windows."""
+import pathlib
+import subprocess
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-wide-utf8"
+work.mkdir(parents=True, exist_ok=True)
+reference = work / "reference.exe"
+subprocess.run([
+ str(root / "tools/rust/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"),
+ "-O2", "tests/wide_utf8.c", "-o", str(reference),
+], cwd=decompiled, check=True)
+expected = subprocess.check_output([str(reference)], text=True, timeout=30)
+linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:]
+wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"]
+subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/wide-utf8"], check=True)
+actual = subprocess.check_output(wsl + ["./build/linux/wide-utf8"], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux: " + actual.strip())