Files
neoLegacy-mac/tests/support/zlib_test_support.c
2026-07-19 00:18:58 -05:00

169 lines
6.8 KiB
C

/*
* Host harness for the reconstructed zlib translation unit.
*
* zlib is Iggy's DEFLATE / inflate decompressor -- a streaming fork of the
* public-domain stb_image zlib decoder (the recovered file-static helpers
* parse_zlib_header / zbuild_huffman / zhuffman_decode_slow / zreceive /
* compute_huffman_codes / parse_uncompressed_block_header are stb_image's
* inflate internals). It exports EXACTLY EIGHT entry points on all four
* platforms (durango / orbis / ps3 / vita):
*
* iggy_zlib_decoder(stbm, data, len, srcfile, tag) -- build a decoder over a
* fixed input buffer [data, data+len); validates the 2-byte zlib header,
* then iggy_stbm_alloc's a (window_size + 0xdd0)-byte decoder.
* iggy_zlib_decoder_callback(stbm, read_fn, state) -- same, but pulls input
* through a user byte-refill callback instead of a fixed buffer.
* iggy_zlib_decoder_free(decoder) -- flush the read callback once, then
* iggy_stbm_free(decoder->stbm_ctx, decoder).
* iggy_zlib_init_defaults(void) -- fill the file-static fixed-Huffman code
* length tables (default_length[288], default_distance[32]).
* iggy_zlib_refill(decoder) -- the block state machine: read BFINAL+BTYPE,
* dispatch stored / fixed-Huffman / dynamic-Huffman, refill the window.
* iggy_zlib_skip(decoder, n) -- advance the output cursor n bytes.
* iggy_zlib_current_offset(decoder) -- bytes consumed so far.
* iggy_zlib_is_at_end(decoder) -- end-of-stream flag.
*
* zlib OWNS NO externally named data global. On durango the only external
* non-function symbol nm surfaces is the ' R ' rodata COMDAT
* ??_C@..zlib.c -- the embedded __FILE__ source path string, a read-only
* artifact, NOT owned exported data (see the cross test's EXPECTED_GLOBALS ==
* set()). The real "owned data" is the DEFLATE spec tables (length_base /
* length_extra / dist_base / dist_extra, the 19-entry code-length dezigzag) and
* the fixed-Huffman default_length / default_distance -- all FILE-STATIC (nm
* lowercase d/b/r), local on every platform, recovered byte-for-byte by the
* cross test rather than via ctypes in_dll.
*
* DURANGO 64-bit decoder-struct field map (byte offsets, recovered from
* raw/durango):
* 0x00 output read cursor (absolute ptr into the window)
* 0x08 output end (absolute ptr into the window)
* 0x18 input refill callback fn ptr (defaults to file-static refill_zeros)
* 0x20 callback state
* 0x28 input cur ptr 0x30 input end ptr
* 0x38 stbm context (passed to iggy_stbm_free)
* 0x48 window size = 1 << ((CMF >> 4) + 8) 0x4c window mask (size - 1)
* 0x50 self/window pointer = decoder + 0xdd0
* 0x60 is_at_end flag (u32) 0x68 consumed-bytes accumulator
*
* The stubs below are the genuine nm ' U ' imports (iggy_stbm_alloc /
* iggy_stbm_free -- iggy_allocation, satisfied) so the durango dylib resolves
* fully; libc (memset) binds to the host. The eight module exports are DEFINED
* BY THE MODULE, never here. iggy_stbm_alloc returns a real, generously sized
* backing buffer so the decoder can be built and driven; both calls record
* their arguments.
*
* DEFERRED (flagged structural, NOT exercised): the Huffman-decode + LZ77
* back-reference core -- the fixed-Huffman (BTYPE=01) and dynamic-Huffman
* (BTYPE=10 / compute_huffman_codes) inflate loops, zbuild_huffman table
* construction, zhuffman_decode_slow, and the length/distance extra-bit
* back-reference copy. The runtime test pins the tractable, deterministic
* slices: header validation, the window-size formula, the alloc/free contract,
* a STORED (uncompressed, BTYPE=00) block round trip, and stream-position
* tracking.
*/
#include <stdint.h>
#include <stddef.h>
/* ---- Backing store for the modeled allocator. ---- */
#define ZLIB_TEST_BACKING 0x20000
static uint8_t zlib_backing[ZLIB_TEST_BACKING];
/* ---- Controllable input for the callback (streaming) variant. ---- */
#define ZLIB_TEST_INPUT 256
static uint8_t zlib_input[ZLIB_TEST_INPUT];
static size_t zlib_input_len;
/*
* The module's byte-refill callback contract (recovered from parse_zlib_header
* / get16bits_slow in raw/durango): invoked as (*fn)(cur, &end, state) when the
* input window is empty; it returns the new cur pointer and writes the new end
* through the second argument. This model hands back the whole controllable
* buffer -- enough to serve the shallow (header) path the callback-variant test
* pins.
*/
static void *zlib_reader(void *cur, void **pend, void *state) {
(void)cur;
(void)state;
*pend = zlib_input + zlib_input_len;
return zlib_input;
}
void zlib_test_set_input(const uint8_t *data, size_t n) {
if (n > ZLIB_TEST_INPUT) {
n = ZLIB_TEST_INPUT;
}
for (size_t i = 0; i < n; ++i) {
zlib_input[i] = data[i];
}
zlib_input_len = n;
}
void *zlib_test_reader_fn(void) { return (void *)zlib_reader; }
/* ---- Recording counters / captures. ---- */
static size_t alloc_count;
static size_t free_count;
static void *alloc_last_ctx;
static size_t alloc_last_size;
static int alloc_last_tag;
static void *alloc_last_srcfile;
static void *free_last_ctx;
static void *free_last_ptr;
void zlib_test_reset(void) {
alloc_count = 0;
free_count = 0;
alloc_last_ctx = 0;
alloc_last_size = 0;
alloc_last_tag = 0;
alloc_last_srcfile = 0;
free_last_ctx = 0;
free_last_ptr = 0;
zlib_input_len = 0;
for (size_t i = 0; i < ZLIB_TEST_INPUT; ++i) {
zlib_input[i] = 0;
}
for (size_t i = 0; i < ZLIB_TEST_BACKING; ++i) {
zlib_backing[i] = 0;
}
}
/* ---- Count / capture accessors. ---- */
size_t zlib_test_alloc_count(void) { return alloc_count; }
size_t zlib_test_free_count(void) { return free_count; }
void *zlib_test_alloc_last_ctx(void) { return alloc_last_ctx; }
size_t zlib_test_alloc_last_size(void) { return alloc_last_size; }
int zlib_test_alloc_last_tag(void) { return alloc_last_tag; }
void *zlib_test_alloc_last_srcfile(void) { return alloc_last_srcfile; }
void *zlib_test_free_last_ctx(void) { return free_last_ctx; }
void *zlib_test_free_last_ptr(void) { return free_last_ptr; }
/* ==================================================================== *
* Genuine nm ' U ' imports (iggy_allocation, satisfied) -- recording
* stubs so the durango dylib resolves fully. iggy_stbm_alloc returns the
* shared backing buffer; the decoder writes its window there.
* ==================================================================== */
void *iggy_stbm_alloc(void *stbm, size_t size, int tag,
const char *source_file, int line) {
(void)line;
++alloc_count;
alloc_last_ctx = stbm;
alloc_last_size = size;
alloc_last_tag = tag;
alloc_last_srcfile = (void *)source_file;
if (size > ZLIB_TEST_BACKING) {
return 0;
}
return zlib_backing;
}
void iggy_stbm_free(void *stbm, void *allocation) {
++free_count;
free_last_ctx = stbm;
free_last_ptr = allocation;
}