Files
neoLegacy-mac/tests/support/swf_decode_test_support.c

146 lines
7.2 KiB
C

/*
* Host harness for the reconstructed swf_decode translation unit.
*
* swf_decode is Iggy's SWF/.iggy tag decoder + library / export bookkeeping:
* the whole-file entry (iggy_decode_swf_from_memory), the late image decoder
* dispatch (iggy_decode_image), the library table accessors (iggy_add_library /
* iggy_get_library / iggy_get_library_id / iggy_get_libnum / iggy_get_export /
* iggy_free_library_id) and the per-tag PlaceObject size classifier
* (iggy_get_placeobject_size). It exports EXACTLY 9 entry points on all four
* platforms (durango / orbis / ps3 / vita).
*
* Unlike a leaf decoder, swf_decode OWNS four externally named data globals --
* iggy_libraries (the 0x20-slot library pointer array; 0x100 bytes @ 64-bit /
* 0x80 @ 32-bit), iggy_load_imports, iggy_load_processing_num_libraries and
* last_abc. iggy_libraries is the one the runtime test drives: iggy_add_library
* writes iggy_libraries[lib->id] and iggy_get_library reads it back, so the two
* round-trip through that shared module-owned array with no direct .in_dll poke.
* (The library-id bitmask iggy_library_id_allocation that get_libnum /
* free_library_id toggle is a FILE-STATIC 'b' local, not one of the four exported
* globals -- each fresh per-test dylib load starts it at zeroed BSS.)
*
* OBJECT MODEL (durango 64-bit -- the runtime test builds ONLY the durango dylib
* so the 64-bit layout is authoritative; raw durango line cites inline):
* iggy_add_library(lib): iggy_libraries[*(char*)(lib+0x234)] = lib;
* return *(char*)(lib+0x234). (raw:5744-5745)
* iggy_get_library(i): return iggy_libraries[i]. (raw:5918)
* iggy_get_library_id(name): scan iggy_libraries[0..0x20); for each non-null
* lib with *(lib+0x210)!=0, rrwcscmp its name vs
* `name`; return index on 0, else -1. (raw:5932-5944)
* iggy_get_export(lib,name): count @ lib+0x21c, table @ lib+0x220, stride
* 0x18: entry[i].name @ +8 compared, entry[i].value
* @ +0 returned; 0 on miss. (raw:5866-5878)
* iggy_get_libnum(): first clear bit of iggy_library_id_allocation,
* CAS-set it, return the index (0xffffffff full).
* (raw:5892-5908)
* iggy_free_library_id(id): CAS-clear bit `id`. (raw:5848-5851)
* iggy_get_placeobject_size(p): branch classifier on the PlaceObject flag bits
* at p+9 / p+0xa / p+0x30 -> {0x40,0x48,0x50,0x58,
* 0x60}. Pure, no imports. (raw:5957-5971)
* iggy_decode_image(ctx,tag,..): dispatch on *(char*)(tag+0x25): 0x06/0x15/0x23
* -> late_definebitsjpeg; 0x14/0x24 ->
* late_definebitslossless; any other -> no-op.
* (raw:5757-5779)
* iggy_decode_swf_from_memory(ctx,comp,data,len,...): comp=='C' opens an
* iggy_zlib_decoder; a NULL decoder ->
* iggy_send_error(ctx,0x3e9,0) and return 0
* (the compressed-header reject path); otherwise
* swf_decode_contents drives the tag loop.
* (raw:5806-5835)
*
* The pinned durango tests reach only four imports: rrwcscmp (name compares),
* rrAtomicCAS32 (the id bitmask), iggy_send_error (the reject path) and
* iggy_zlib_decoder (returned NULL to exercise that reject). Every other import
* (the ~30 iggy_* / rr* surface -- jpeg, zlib, font, image_storage, arena, ...) is
* referenced only inside the deep, un-exercised bodies (swf_decode_contents,
* late_definebits*, read_abc_file) and is left to -Wl,-undefined,dynamic_lookup:
* never called at runtime, never bound. Only the four reached symbols are
* provided below, as recording / controllable stubs so the durango dylib links and
* the real accessor bodies run.
*/
#include <stdint.h>
#include <string.h>
/* ---- Recording counters. ---- */
static size_t wcscmp_count;
static size_t cas_count;
static size_t send_error_count;
static size_t zlib_decoder_count;
static int32_t last_error_code;
/* Controllable iggy_zlib_decoder return (default NULL -> exercises the
* compressed-header reject path in iggy_decode_swf_from_memory). */
static void *zlib_decoder_result;
void swf_decode_test_reset(void) {
wcscmp_count = 0;
cas_count = 0;
send_error_count = 0;
zlib_decoder_count = 0;
last_error_code = 0;
zlib_decoder_result = 0;
}
/* ---- Count / last-value accessors. ---- */
size_t swf_decode_test_wcscmp_count(void) { return wcscmp_count; }
size_t swf_decode_test_cas_count(void) { return cas_count; }
size_t swf_decode_test_send_error_count(void) { return send_error_count; }
size_t swf_decode_test_zlib_decoder_count(void) { return zlib_decoder_count; }
int32_t swf_decode_test_last_error_code(void) { return last_error_code; }
void swf_decode_test_set_zlib_decoder_result(void *result) {
zlib_decoder_result = result;
}
/* ---- Imports (recording / controllable stubs). ---- */
/* RAD wide-string compare (iggy runtime). Faithful-enough model: 0 iff the two
* name pointers are identical. The runtime test arranges the "hit" operands to be
* the SAME buffer (a real wcscmp of one string against itself is also 0) and the
* "miss" operands to be distinct buffers with distinct content (a real wcscmp is
* non-zero), so the equality model and any faithful wide-compare agree on every
* case the test exercises. Symmetric, so robust to argument order. */
int rrwcscmp(const void *a, const void *b) {
++wcscmp_count;
return a == b ? 0 : 1;
}
/* RAD 32-bit atomic compare-and-swap (iggy runtime). get_libnum / free_library_id
* each read iggy_library_id_allocation, compute the exact new bitmask themselves,
* and retry `while (result == 0)`; single-threaded there is no contention, so the
* faithful observable behaviour is "store newval, report success". The middle
* argument is the compiler's spilled expected-value scratch (a Ghidra artifact of
* the CAS lowering) and is opaque here. */
int rrAtomicCAS32(volatile int32_t *dest, void *expected_scratch, int32_t newval) {
(void)expected_scratch;
++cas_count;
*dest = newval;
return 1;
}
/* iggy error sink (iggy_global). Records the last code so the compressed-header
* reject path can assert iggy_send_error(ctx, 0x3e9, 0). */
void iggy_send_error(void *ctx, int32_t code, int32_t arg) {
(void)ctx;
(void)arg;
++send_error_count;
last_error_code = code;
}
/* iggy_zlib zlib-decoder factory. Controllable return -- default NULL drives the
* iggy_decode_swf_from_memory compressed-header reject path; the deep success path
* (swf_decode_contents) is not exercised by the pinned tests. */
void *iggy_zlib_decoder(void *arena, void *data, void *end, const char *file,
int line) {
(void)arena;
(void)data;
(void)end;
(void)file;
(void)line;
++zlib_decoder_count;
return zlib_decoder_result;
}