"""Development-only reference decoder: runs Boxsim's own machine code. This module emulates `ExpandA2F` (VA 0xa11880) out of an unmodified Boxsim.exe under Unicorn, with faked Delphi TStream objects. It is exact by construction and exists solely as the oracle that the pure Python decoder in `boxsim.a2f` is differentially tested against. It is NOT part of the shipped tool: it needs `unicorn`, `pefile` and an 8 MB proprietary Windows binary. Point BOXSIM_EXE at a Boxsim.exe to use it. python -m boxsim.cli decode --oracle file.bpj """ from __future__ import annotations import os import struct MAGIC = b"MyIni2CK" ENTRY = 0xA11880 # ExpandA2F(self, instream, outstream) ERR_FUNC = 0x97E584 # Boxsim's error reporter RAISE_FUNC = 0x80ACC4 # Delphi RaiseExcept OBJ_BASE = 0x20000000 # decoder object: ~11 MB of zero-init tables OBJ_SIZE = 0x01000000 FAKE_BASE = 0x40000000 # fake stream objects, vtables, trap stubs STACK_BASE = 0x60000000 STACK_SIZE = 0x100000 GDT_BASE = 0x50000000 FS_SEG = 0x50002000 # Delphi SEH chain scratch space SENTINEL = 0x7F00DEAD IN_OBJ, OUT_OBJ = FAKE_BASE, FAKE_BASE + 0x100 IN_VT, OUT_VT = FAKE_BASE + 0x1000, FAKE_BASE + 0x2000 STUB = FAKE_BASE + 0x3000 S_IN_SIZE, S_IN_READ, S_IN_SEEK, S_OUT_WRITE, S_OUT_SIZE, S_OUT_SEEK = range(6) STUB_SPAN = 256 def default_exe() -> str: return os.environ.get( "BOXSIM_EXE", os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "boxsim_dl", "BoxSim 2.1 DE (Stand 12.2025)", "Boxsim.exe", ), ) def _gdt_entry(base: int, limit: int, access: int, gran: int) -> bytes: e = limit & 0xFFFF e |= (base & 0xFFFFFF) << 16 e |= (access & 0xFF) << 40 e |= ((limit >> 16) & 0xF) << 48 e |= (gran & 0xF) << 52 e |= ((base >> 24) & 0xFF) << 56 return struct.pack(" bytes: """Decompress a descrambled MyIni2CK stream using Boxsim's own decoder.""" import pefile from unicorn import UC_ARCH_X86, UC_HOOK_CODE, UC_MODE_32, Uc from unicorn.x86_const import ( UC_X86_REG_CS, UC_X86_REG_DS, UC_X86_REG_EAX, UC_X86_REG_ECX, UC_X86_REG_EDX, UC_X86_REG_ES, UC_X86_REG_ESP, UC_X86_REG_FS, UC_X86_REG_GDTR, UC_X86_REG_GS, UC_X86_REG_SS, ) exe = exe or default_exe() if not os.path.exists(exe): raise FileNotFoundError( f"oracle needs Boxsim.exe; set BOXSIM_EXE (looked at {exe!r})" ) pe = pefile.PE(exe) base = pe.OPTIONAL_HEADER.ImageBase img = pe.get_memory_mapped_image(ImageBase=base) uc = Uc(UC_ARCH_X86, UC_MODE_32) uc.mem_map(base, (len(img) + 0xFFF) & ~0xFFF) uc.mem_write(base, img) for addr, size in ( (OBJ_BASE, OBJ_SIZE), (FAKE_BASE, 0x10000), (STACK_BASE, STACK_SIZE), (GDT_BASE, 0x4000), ): uc.mem_map(addr, size) uc.mem_write(IN_OBJ, struct.pack("