Files
tdkpin/original/reconstructed/tests/test_window_messages.c
T
ddidderr 8b99e9607c feat(reconstruction): complete binary-backed C recovery
Replace the partial mechanics transcriptions with a separate, readable C11
reconstruction of the complete Win16 image while preserving the original raw
Ghidra export as immutable evidence. Cover all ordinary and overlapping entry
points, Borland runtime behavior, Win16 imports, segmented data, callbacks,
resources, indirect control flow, physics, rendering, persistence, and
startup/shutdown lifecycles.

Add deterministic extraction and audit tooling plus address-linked ledgers for
functions, imports, DGROUP ranges and objects, relocations, resources, and
callbacks. The final gate records zero raw, partial, restored, unknown,
blocked, or unclassified required units. Keep the semantic-fidelity boundary
explicit: the portable C is not claimed to reproduce a byte-identical Borland
NE build.

Add strict focused harnesses for every reconstructed C unit, exact resource
round-trip checks, and a 16-bit Borland Real48 reference probe. No Rust source
or Cargo metadata is changed in this phase.

Test Plan:
- `bash original/tools/test_reconstructed_c.sh` -- passed
- `bash original/tools/probe_real48_reference.sh` -- passed bit-for-bit
- `python3 original/tools/audit_reconstruction.py --require-complete` -- passed
- `git diff --cached --check` -- passed
- `git diff HEAD -- '*.rs' Cargo.toml Cargo.lock` -- empty
2026-08-23 16:41:17 +02:00

366 lines
13 KiB
C

#include "../tdkpin_window_messages.h"
#include <assert.h>
Win16FarPtr g_object_windows_application;
HINSTANCE16 g_win16_previous_instance;
HINSTANCE16 g_win16_instance;
uint16_t g_win16_show_command;
Win16FarPtr g_win16_message_box;
Win16FarPtr g_object_windows_dispatch_proc;
uint16_t g_object_windows_thunk_code_selector;
Win16FarPtr g_object_windows_free_thunk;
Win16FarPtr g_object_windows_message_hook;
Win16FarPtr g_object_windows_creating_object;
static bool g_bool_result;
static Win16FarPtr g_bool_proc;
static Win16FarPtr g_disposed;
static Win16FarPtr g_event_proc;
static Win16FarPtr g_event_object;
static unsigned g_event_calls;
static uint16_t g_quit_code;
static uint16_t g_removed_property_offsets[2];
static unsigned g_remove_calls;
static int32_t g_window_style;
static HWND16 g_focus_window;
static HWND16 g_is_child_parent;
static HWND16 g_is_child_child;
static BOOL16 g_is_child_result;
static Win16FarPtr g_void_proc;
static Win16FarPtr g_original_proc;
static HWND16 g_original_window;
static UINT16 g_original_message;
static WPARAM16 g_original_wparam;
static LPARAM16 g_original_lparam;
static BOOL16 g_is_window_result;
static BOOL16 g_is_iconic_result;
static HWND16 g_set_focus_window;
bool win16_call_object_bool_method(
Win16FarPtr procedure, Win16FarPtr object)
{
(void)object;
g_bool_proc = procedure;
return g_bool_result;
}
void borland_dispose_object(Win16FarPtr object)
{
g_disposed = object;
}
void win16_call_object_message_handler(
Win16FarPtr procedure,
Win16FarPtr object,
ObjectWindowsMessage *message)
{
(void)message;
g_event_proc = procedure;
g_event_object = object;
g_event_calls++;
}
void PostQuitMessage16(INT16 exit_code)
{
g_quit_code = (uint16_t)exit_code;
}
Win16Handle RemoveProp16(HWND16 window, Win16FarPtr name)
{
assert(window == 0x77 && g_remove_calls < 2);
g_removed_property_offsets[g_remove_calls++] = win16_far_offset(name);
return 0;
}
int32_t GetWindowLong16(HWND16 window, INT16 index)
{
assert(window == 0x77 && index == -16);
return g_window_style;
}
HWND16 GetFocus16(void)
{
return g_focus_window;
}
BOOL16 IsChild16(HWND16 parent, HWND16 child)
{
g_is_child_parent = parent;
g_is_child_child = child;
return g_is_child_result;
}
BOOL16 IsWindow16(HWND16 window)
{
assert(window != 0);
return g_is_window_result;
}
BOOL16 IsIconic16(HWND16 window)
{
assert(window == 0x77);
return g_is_iconic_result;
}
HWND16 SetFocus16(HWND16 window)
{
g_set_focus_window = window;
return 0;
}
void win16_call_object_method(
Win16FarPtr procedure, Win16FarPtr object)
{
assert(object != 0);
g_void_proc = procedure;
}
LRESULT16 CallWindowProc16(
Win16FarPtr procedure,
HWND16 window,
UINT16 message,
WPARAM16 wparam,
LPARAM16 lparam)
{
g_original_proc = procedure;
g_original_window = window;
g_original_message = message;
g_original_wparam = wparam;
g_original_lparam = lparam;
return (LRESULT16)0x12345678u;
}
static void write_far(uint16_t offset, Win16FarPtr pointer)
{
win16_write_u16(
win16_dgroup_pointer(offset), 0, win16_far_offset(pointer));
win16_write_u16(
win16_dgroup_pointer((uint16_t)(offset + 2)),
0,
win16_far_selector(pointer));
}
int main(void)
{
static uint8_t dgroup[0x1000];
static uint8_t application_bytes[64];
static uint8_t main_bytes[80];
static uint8_t other_bytes[80];
win16_reset_segment_bindings();
win16_bind_segment(0x5000, dgroup, sizeof(dgroup), true);
win16_set_dgroup_selector(0x5000);
win16_bind_segment(
0x7000, application_bytes, sizeof(application_bytes), true);
win16_bind_segment(0x7100, main_bytes, sizeof(main_bytes), true);
win16_bind_segment(0x7200, other_bytes, sizeof(other_bytes), true);
g_object_windows_application = win16_make_far_pointer(0x7000, 2);
Win16FarPtr main_window = win16_make_far_pointer(0x7100, 2);
Win16FarPtr other_window = win16_make_far_pointer(0x7200, 2);
uint16_t app_vmt = 0x0100;
uint16_t window_vmt = 0x0200;
win16_write_u16(g_object_windows_application, 0, app_vmt);
win16_write_u16(main_window, 0, window_vmt);
win16_write_u16(other_window, 0, window_vmt);
win16_write_u16(main_window, 4, 0x77);
win16_write_u16(other_window, 4, 0x77);
win16_write_u16(other_window, 2, 9);
win16_write_u8(win16_far_add_offset(other_window, 0x16), 1);
win16_write_u16(
g_object_windows_application, 8, win16_far_offset(main_window));
win16_write_u16(
g_object_windows_application, 10, win16_far_selector(main_window));
Win16FarPtr app_can_close = win16_make_far_pointer(0x6000, 0x1000);
Win16FarPtr window_can_close = win16_make_far_pointer(0x6000, 0x2000);
Win16FarPtr default_handler = win16_make_far_pointer(0x6100, 0x3000);
Win16FarPtr message_0014 = win16_make_far_pointer(0x6100, 0x4000);
Win16FarPtr scroll_handler = win16_make_far_pointer(0x6100, 0x5000);
Win16FarPtr slot_38 = win16_make_far_pointer(0x6100, 0x6000);
write_far(app_vmt + 0x44, app_can_close);
write_far(window_vmt + 0x3c, window_can_close);
write_far(window_vmt + 0x0c, default_handler);
write_far(window_vmt + 0x10, message_0014);
write_far(window_vmt + 0x48, scroll_handler);
write_far(window_vmt + 0x38, slot_38);
assert(object_windows_has_flags(other_window, 1));
assert(!object_windows_has_flags(other_window, 3));
assert(object_windows_zero_word_default(other_window) == 0);
object_windows_set_flag_4(other_window);
assert(object_windows_has_flags(other_window, 5));
object_windows_clear_flag_4(other_window);
assert(object_windows_has_flags(other_window, 1));
object_windows_set_flag_1(other_window);
assert(object_windows_has_flags(other_window, 1));
object_windows_update_flags(other_window, false, 1);
assert(!object_windows_has_flags(other_window, 1));
object_windows_set_flag_1(other_window);
ObjectWindowsMessage message = {
.window = 0x77,
.message = 0,
.wparam = 1,
.lparam = 0,
.result = 0,
};
g_bool_result = false;
g_disposed = 0;
object_windows_wm_close(main_window, &message);
assert(g_bool_proc == app_can_close && g_disposed == 0);
g_bool_result = true;
object_windows_wm_close(other_window, &message);
assert(g_bool_proc == window_can_close && g_disposed == other_window);
g_event_calls = 0;
g_quit_code = 0;
object_windows_wm_destroy(main_window, &message);
assert(g_quit_code == 0x77);
assert(g_event_calls == 1 && g_event_proc == default_handler);
g_quit_code = 0;
object_windows_wm_destroy(other_window, &message);
assert(g_quit_code == 0);
g_remove_calls = 0;
object_windows_wm_nc_destroy(other_window, &message);
assert(g_remove_calls == 2);
assert(g_removed_property_offsets[0] == 0x067a);
assert(g_removed_property_offsets[1] == 0x067e);
assert(win16_read_u16(win16_far_add_offset(other_window, 4)) == 0);
win16_write_u16(other_window, 4, 0x77);
message.wparam = 1;
object_windows_wm_activate(other_window, &message);
assert(win16_read_far_pointer(g_object_windows_application, 0x0e) ==
other_window);
message.wparam = 0;
object_windows_wm_activate(other_window, &message);
assert(win16_read_far_pointer(g_object_windows_application, 0x0e) ==
other_window);
win16_write_u8(win16_far_add_offset(other_window, 0x16), 0);
message.wparam = 1;
object_windows_wm_activate(other_window, &message);
assert(win16_read_far_pointer(g_object_windows_application, 0x0e) == 0);
g_bool_result = true;
object_windows_wm_query_end_session(main_window, &message);
assert(message.result == 1 && g_bool_proc == app_can_close);
g_bool_result = false;
object_windows_wm_query_end_session(other_window, &message);
assert(message.result == 0 && g_bool_proc == window_can_close);
g_bool_result = false;
g_event_calls = 0;
object_windows_message_0014(main_window, &message);
assert(g_bool_proc == app_can_close && g_event_calls == 0);
object_windows_message_0014(other_window, &message);
assert(g_event_calls == 1 && g_event_proc == message_0014);
assert(g_event_object == other_window);
g_window_style = 0;
object_windows_wm_hscroll(other_window, &message);
assert(g_event_proc == scroll_handler);
object_windows_wm_vscroll(other_window, &message);
assert(g_event_proc == scroll_handler);
g_window_style = (int32_t)0x00100000u;
object_windows_wm_hscroll(other_window, &message);
assert(g_event_proc == default_handler);
g_window_style = (int32_t)0x00200000u;
object_windows_wm_vscroll(other_window, &message);
assert(g_event_proc == default_handler);
assert(object_windows_turbo_window_class_name(other_window) ==
win16_dgroup_pointer(0x068c));
g_event_calls = 0;
object_windows_forward_default(other_window, &message);
assert(g_event_calls == 1 && g_event_proc == default_handler);
win16_write_u8(win16_far_add_offset(other_window, 0x16), 0);
win16_write_u16(other_window, 4, 0);
object_windows_set_flag_4_if_windowed(0xbeef, other_window);
assert(!object_windows_has_flags(other_window, 4));
win16_write_u16(other_window, 4, 0x77);
object_windows_set_flag_4_if_windowed(0xbeef, other_window);
assert(object_windows_has_flags(other_window, 4));
win16_write_u16(other_window, 0x35, 0xbeef);
assert(object_windows_get_field_35(other_window) == 0xbeef);
g_focus_window = 0;
win16_write_u16(other_window, 0x3f, 0);
object_windows_remember_child_focus(other_window);
assert(win16_read_u16(win16_far_add_offset(other_window, 0x3f)) == 0);
g_focus_window = 0x99;
g_is_child_result = 0;
object_windows_remember_child_focus(other_window);
assert(g_is_child_parent == 0x77 && g_is_child_child == 0x99);
assert(win16_read_u16(win16_far_add_offset(other_window, 0x3f)) == 0);
g_is_child_result = 1;
object_windows_remember_child_focus(other_window);
assert(win16_read_u16(win16_far_add_offset(other_window, 0x3f)) == 0x99);
g_void_proc = 0;
g_event_calls = 0;
object_windows_call_slot_38_then_default(other_window, &message);
assert(g_void_proc == slot_38 && g_event_calls == 1);
g_focus_window = 0x88;
g_is_child_result = 1;
g_event_calls = 0;
object_windows_remember_focus_then_default(other_window, &message);
assert(win16_read_u16(win16_far_add_offset(other_window, 0x3f)) == 0x88);
assert(g_event_calls == 1 && g_event_proc == default_handler);
Win16FarPtr original_proc = win16_make_far_pointer(0x6200, 0x7000);
win16_write_u16(other_window, 0x37, win16_far_offset(original_proc));
win16_write_u16(other_window, 0x39, win16_far_selector(original_proc));
message.message = 0x123;
message.wparam = 0x4567;
message.lparam = (LPARAM16)0x89abcdefu;
object_windows_call_original_window_proc(other_window, &message);
assert(g_original_proc == original_proc && g_original_window == 0x77);
assert(g_original_message == 0x123 && g_original_wparam == 0x4567);
assert(g_original_lparam == (LPARAM16)0x89abcdefu);
assert(message.result == (LRESULT16)0x12345678u);
g_void_proc = 0;
object_windows_call_slot_38(other_window, &message);
assert(g_void_proc == slot_38);
message.result = (LRESULT16)0x76543210u;
object_windows_zero_message_result(other_window, &message);
assert(message.result == 0);
win16_write_u16(other_window, 0x3f, 0x99);
g_is_window_result = 0;
g_set_focus_window = 0;
object_windows_restore_child_focus(other_window);
assert(g_set_focus_window == 0);
g_is_window_result = 1;
g_is_iconic_result = 1;
object_windows_restore_child_focus(other_window);
assert(g_set_focus_window == 0);
g_is_iconic_result = 0;
object_windows_restore_child_focus(other_window);
assert(g_set_focus_window == 0x99);
win16_write_u8(win16_far_add_offset(other_window, 0x16), 1);
message.wparam = 0;
g_focus_window = 0x88;
g_is_child_result = 1;
object_windows_wm_activate_with_focus(other_window, &message);
assert(win16_read_u16(win16_far_add_offset(other_window, 0x3f)) == 0x88);
message.wparam = 1;
g_set_focus_window = 0;
object_windows_wm_activate_with_focus(other_window, &message);
assert(g_set_focus_window == 0x88);
message.wparam = 0xf020;
g_focus_window = 0x66;
object_windows_wm_syscommand_focus(other_window, &message);
assert(win16_read_u16(win16_far_add_offset(other_window, 0x3f)) == 0x66);
message.wparam = 0xf120;
g_set_focus_window = 0;
object_windows_wm_syscommand_focus(other_window, &message);
assert(g_set_focus_window == 0x66);
message.wparam = 0x1234;
g_set_focus_window = 0;
object_windows_wm_syscommand_focus(other_window, &message);
assert(g_set_focus_window == 0);
return 0;
}