Files
neoLegacy-mac/tests/iggy_file_reader_test_support.py
2026-07-13 05:10:58 -05:00

233 lines
7.4 KiB
Python

import ctypes
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "iggy-decompiled/src/reconstructed/iggy_file_reader.c"
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
VARIANTS = {
"durango": ("IGGY_RECONSTRUCT_FILE_READER_DURANGO", "little", 0x40, 1),
"orbis": ("IGGY_RECONSTRUCT_FILE_READER_ORBIS", "little", 0x40, 1),
"ps3": ("IGGY_RECONSTRUCT_FILE_READER_PS3", "big", 0x20, 2),
"vita": ("IGGY_RECONSTRUCT_FILE_READER_VITA", "little", 0x20, 1),
}
HARNESS = r"""
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "iggy_file_reader.h"
int arena_calls;
int stbm_calls;
int stbm_free_calls;
size_t arena_size;
size_t stbm_size;
int stbm_tag;
int stbm_line;
unsigned char arena_storage[4096];
unsigned char stbm_storage[4096];
void reader_harness_reset(void) {
arena_calls = 0;
stbm_calls = 0;
stbm_free_calls = 0;
arena_size = 0;
stbm_size = 0;
stbm_tag = 0;
stbm_line = 0;
memset(arena_storage, 0xa5, sizeof(arena_storage));
memset(stbm_storage, 0xa5, sizeof(stbm_storage));
}
void *iggy_arena_malloc_lowlevel(IggyArena *arena, intptr_t size) {
(void)arena;
arena_calls++;
arena_size = (size_t)size;
return arena_storage;
}
void *iggy_stbm_alloc(void *stbm, size_t size, int tag,
const char *source_file, int line) {
(void)stbm;
(void)source_file;
stbm_calls++;
stbm_size = size;
stbm_tag = tag;
stbm_line = line;
return stbm_storage;
}
void iggy_stbm_free(void *stbm, void *allocation) {
(void)stbm;
(void)allocation;
stbm_free_calls++;
}
"""
class IggyConvertedSection(ctypes.Structure):
_fields_ = [
("data", ctypes.c_void_p),
("byte_size", ctypes.c_uint32),
("section_id", ctypes.c_uint8),
("structure_def_index", ctypes.c_uint8),
("flags", ctypes.c_uint8),
("processed", ctypes.c_uint8),
]
class ReconstructedIggyFileReaderTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temporary_directory = tempfile.TemporaryDirectory()
temporary_path = Path(cls.temporary_directory.name)
harness_path = temporary_path / "iggy_file_reader_harness.c"
harness_path.write_text(HARNESS, encoding="utf-8")
cls.libraries = {}
for platform, (define, _, _, _) in VARIANTS.items():
library_path = temporary_path / f"libiggy_file_reader_{platform}.dylib"
result = subprocess.run(
[
"clang",
"-dynamiclib",
"-std=c11",
"-O2",
"-fno-builtin",
"-Wall",
"-Wextra",
"-Werror",
f"-D{define}=1",
"-I",
str(RECONSTRUCTED),
str(SOURCE),
str(harness_path),
"-o",
str(library_path),
],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise AssertionError(result.stdout + result.stderr)
library = ctypes.CDLL(str(library_path))
cls.configure_library(library)
cls.libraries[platform] = library
@classmethod
def tearDownClass(cls):
cls.temporary_directory.cleanup()
@staticmethod
def configure_library(library):
library.iggy_convert_file.argtypes = [
ctypes.c_void_p,
ctypes.c_int32,
ctypes.c_uint64,
ctypes.c_uint64,
ctypes.POINTER(IggyConvertedSection),
ctypes.c_int32,
ctypes.POINTER(ctypes.c_int32),
ctypes.POINTER(ctypes.c_uint8),
ctypes.POINTER(ctypes.c_void_p),
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_int32,
ctypes.POINTER(ctypes.c_int32),
ctypes.POINTER(ctypes.c_uint8),
ctypes.c_int32,
]
library.iggy_convert_file.restype = ctypes.c_int32
library.reader_harness_reset.argtypes = []
library.reader_harness_reset.restype = None
@staticmethod
def build_file(platform, sections=(), *, endian_marker=None,
pointer_bits=None, bitfield=None, compatibility=0):
_, target_endian, target_pointer_bits, target_bitfield = VARIANTS[platform]
header = bytearray(32 + 16 * len(sections))
header[0:8] = bytes.fromhex("49670aed00090000")
header[8] = 1 if target_endian == "little" else 2
header[9] = target_pointer_bits
header[10] = target_bitfield
header[11] = compatibility
if endian_marker is not None:
header[8] = endian_marker
if pointer_bits is not None:
header[9] = pointer_bits
if bitfield is not None:
header[10] = bitfield
header[28:32] = len(sections).to_bytes(4, target_endian)
payload_offset = len(header)
payloads = bytearray()
for index, section in enumerate(sections):
payload = bytes(section["payload"])
descriptor = 32 + index * 16
header[descriptor] = section["id"]
header[descriptor + 1] = section.get("processed", 0)
header[descriptor + 2] = section.get("definition", 0)
header[descriptor + 3] = section.get("flags", 0)
header[descriptor + 4:descriptor + 8] = len(payload).to_bytes(
4, target_endian
)
header[descriptor + 12:descriptor + 16] = payload_offset.to_bytes(
4, target_endian
)
payloads.extend(payload)
payload_offset += len(payload)
return header + payloads
@staticmethod
def context():
context_bytes = (ctypes.c_uint8 * 4)(0x11, 0x22, 0x33, 0x44)
context_ptrs = (ctypes.c_void_p * 4)(0x11111111, 0x22222222,
0x33333333, 0x44444444)
return context_bytes, context_ptrs
def convert(self, platform, file_bytes, wanted=0, stbm=0, capacity=8,
allow_alias=0, out_count_sentinel=0x12345678):
library = self.libraries[platform]
library.reader_harness_reset()
file_storage = (ctypes.c_uint8 * len(file_bytes)).from_buffer_copy(file_bytes)
output = (IggyConvertedSection * max(capacity, 1))()
output_count = ctypes.c_int32(out_count_sentinel)
did_alias = ctypes.c_int32(0x55555555)
scratch = (ctypes.c_uint8 * 65536)(*([0xCC] * 65536))
context_bytes, context_ptrs = self.context()
status = library.iggy_convert_file(
file_storage,
len(file_bytes),
wanted,
stbm,
output,
capacity,
ctypes.byref(output_count),
context_bytes,
context_ptrs,
ctypes.c_void_p(0x1234),
ctypes.c_void_p(0x5678),
allow_alias,
ctypes.byref(did_alias),
scratch,
len(scratch),
)
return {
"library": library,
"file": file_storage,
"output": output,
"count": output_count.value,
"alias": did_alias.value,
"scratch": scratch,
"status": status,
}
@staticmethod
def harness_value(library, name, ctype=ctypes.c_int):
return ctype.in_dll(library, name).value