aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 568389f2022e866e434db0e051fd1667dc5bc505
parent 26399f4ba67f25110cbdf194b743211cfab9855e
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 19:31:12 +0000

Decode DEFLATE streams with local C cursors

Diffstat:
Msrc/Makefile.linux | 8++++++++
Msrc/Makefile.wasm | 4++++
Msrc/game/bbr.dll/lifted-0044.c | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tests/inflate_fast.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 237 insertions(+), 0 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -259,6 +259,8 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/loading-fade ./build/linux/jpeg-idct ./build/linux/jpeg-idct-paged + ./build/linux/inflate-fast + ./build/linux/inflate-fast-paged ./build/linux/png-filter ./build/linux/png-filter-paged ./build/linux/archive-string @@ -617,3 +619,9 @@ build/linux/jpeg-idct build/linux/jpeg-idct-paged: tests/jpeg_idct.c game/bbr.dl $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/jpeg-idct build/linux/jpeg-idct-paged + +build/linux/inflate-fast build/linux/inflate-fast-paged: tests/inflate_fast.c game/bbr.dll/lifted-0044.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/inflate-fast build/linux/inflate-fast-paged diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -179,3 +179,7 @@ build/wasm/png-filter.js: tests/png_filter.c game/bbr.dll/lifted-0047.c runtime/ build/wasm/jpeg-idct.js: tests/jpeg_idct.c game/bbr.dll/lifted-0042.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + +build/wasm/inflate-fast.js: tests/inflate_fast.c game/bbr.dll/lifted-0044.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ diff --git a/src/game/bbr.dll/lifted-0044.c b/src/game/bbr.dll/lifted-0044.c @@ -122960,6 +122960,146 @@ void sub_250C1F70(void) { uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS RECOMP_ENTER(0x250C1F70u); + /* Keep the decoder cursors local while processing Huffman codes. The + * cooperative scheduler retains the instruction path and scratch frame. */ + if (!recomp_yield_hook) { + uint32_t literal_mask = MEM32(0x251b7ff8 + MEM32(esp + 4) * 4); + uint32_t distance_mask = MEM32(0x251b7ff8 + MEM32(esp + 8) * 4); + uint32_t literals = MEM32(esp + 12), distances = MEM32(esp + 16); + uint32_t state = MEM32(esp + 20), stream = MEM32(esp + 24); + uint32_t bits = MEM32(state + 0x1c), hold = MEM32(state + 0x20); + uint32_t input = MEM32(stream), available = MEM32(stream + 4); + uint32_t output = MEM32(state + 0x34), read = MEM32(state + 0x30); + uint32_t room = output < read ? read - output - 1 : MEM32(state + 0x2c) - output; + uint32_t edges = 0, result = 0; + + for (;;) { + /* The fast-loop caller guarantees ten input bytes and room for + * a maximum-length match. Refill before each root-table lookup. */ + while (bits < 20) { + hold |= (uint32_t)MEM8(input++) << (bits & 31); + bits += 8; + --available; + ++edges; + } + uint32_t entry = literals + (hold & literal_mask) * 8; + uint32_t operation = MEM8(entry); + for (;;) { + uint32_t drop = MEM8(entry + 1); + hold >>= drop & 31; + bits -= drop; + if (!operation) { + MEM8(output++) = MEM8(entry + 4); + --room; + break; + } + if ((operation & 64) && !(operation & 16)) { + result = operation & 32 ? 1 : 0xfffffffdu; + if (result != 1) MEM32(stream + 0x18) = 0x251b6de0; + goto inflate_fast_done; + } + if (!(operation & 16)) { + entry += ((hold & MEM32(0x251b7ff8 + operation * 4)) + MEM32(entry + 4)) * 8; + operation = MEM8(entry); + if (operation) ++edges; + continue; + } + + /* Decode a match length, then its distance. Table offsets + * are relative to the current entry, not the table root. */ + uint32_t extra = operation & 15; + uint32_t length = MEM32(entry + 4) + (hold & MEM32(0x251b7ff8 + extra * 4)); + hold >>= extra; + bits -= extra; + while (bits < 15) { + hold |= (uint32_t)MEM8(input++) << (bits & 31); + bits += 8; + --available; + ++edges; + } + entry = distances + (hold & distance_mask) * 8; + for (;;) { + operation = MEM8(entry); + drop = MEM8(entry + 1); + hold >>= drop & 31; + bits -= drop; + if (operation & 16) break; + if ((operation & 64) && !(operation & 16)) { + MEM32(stream + 0x18) = 0x251b6dc8; + result = 0xfffffffdu; + goto inflate_fast_done; + } + entry += ((hold & MEM32(0x251b7ff8 + operation * 4)) + MEM32(entry + 4)) * 8; + ++edges; + } + extra = operation & 15; + while (bits < extra) { + hold |= (uint32_t)MEM8(input++) << (bits & 31); + bits += 8; + --available; + ++edges; + } + uint32_t distance = MEM32(entry + 4) + (hold & MEM32(0x251b7ff8 + extra * 4)); + hold >>= extra; + bits -= extra; + room -= length; + + /* Copy forward so overlapping matches repeat their newly + * produced bytes. A history match may cross the window end. */ + uint32_t window = MEM32(state + 0x28); + uint32_t source; + if (distance <= output - window) { + source = output - distance; + MEM8(output++) = MEM8(source++); + MEM8(output++) = MEM8(source++); + length -= 2; + } else { + uint32_t tail = distance - (output - window); + source = MEM32(state + 0x2c) - tail; + if (length > tail) { + length -= tail; + edges += tail; + do { MEM8(output++) = MEM8(source++); } while (--tail); + source = window; + } + } + edges += length; + do { MEM8(output++) = MEM8(source++); } while (--length); + break; + } + if (room < 258 || available < 10) break; + ++edges; + } + +inflate_fast_done: + /* Return complete unused bytes to the caller's streaming decoder. + * Keep the high accumulator bits, as the original routine does. */ + uint32_t consumed = MEM32(stream + 4) - available; + uint32_t unused = bits >> 3; + if (unused > consumed) unused = consumed; + input -= unused; + available += unused; + MEM32(state + 0x1c) = bits - unused * 8; + MEM32(state + 0x20) = hold; + consumed = input - MEM32(stream); + uint32_t total = MEM32(stream + 8); + MEM32(stream) = input; + MEM32(stream + 4) = available; + MEM32(stream + 8) = total + consumed; + MEM32(state + 0x34) = output; + g_backedges += edges; + eax = result; + ecx = consumed; + edx = output; + _flag_k = result ? FK_ADD : FK_TEST; + _flag_a = result ? total : 0; + _flag_b = result ? consumed : 0; + _cf = result && total + consumed < total; + RECOMP_FLAGS_OUT(); + esp += 4; + RECOMP_REGS_OUT(); + return; + } L_250C1F70: PUSH32(esp, ebp); /* 0x250C1F70: push ebp */ diff --git a/src/tests/inflate_fast.c b/src/tests/inflate_fast.c @@ -0,0 +1,85 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_250C1F70(void); +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +static unsigned yields; +static void observe_yield(void) { ++yields; } + +int main(void) +{ + assert(guest_map("inflate fixture", 0x50000000, 0x50000)); + assert(guest_map("inflate masks", 0x251b0000, 0x10000)); + for (unsigned i = 0; i <= 16; ++i) MEM32(0x251b7ff8 + i * 4) = (1u << i) - 1; + const struct { + unsigned literal_operation, length, distance_operation, distance; + unsigned initial_bits, output_bytes, final_bits, result, message, edges; + } cases[] = { + {0, 65, 80, 1, 0, 1, 7, 0, 0, 3}, + {96, 0, 80, 1, 0, 0, 7, 1, 0, 3}, + {64, 0, 80, 1, 0, 0, 7, 0xfffffffd, 0x251b6de0, 3}, + {80, 258, 80, 1, 0, 258, 6, 0, 0, 259}, + {80, 5, 80, 3, 0, 5, 6, 0, 0, 8}, + {0, 65, 80, 1, 31, 1, 30, 0, 0, 0}, + {80, 3, 64, 1, 0, 0, 6, 0xfffffffd, 0x251b6dc8, 3}, + }; + for (unsigned hook = 0; hook < 2; ++hook) { + for (unsigned trial = 0; trial < sizeof(cases) / sizeof(cases[0]); ++trial) { + memset((void *)ADDR(0x50000000), 0, 0x50000); + MEM8(0x50001000) = cases[trial].literal_operation; + MEM8(0x50001001) = 1; + MEM32(0x50001004) = cases[trial].length; + MEM8(0x50002000) = cases[trial].distance_operation; + MEM8(0x50002001) = 1; + MEM32(0x50002004) = cases[trial].distance; + MEM32(0x5000401c) = cases[trial].initial_bits; + MEM32(0x50004028) = 0x5000fffd; + MEM32(0x5000402c) = 0x50017ffd; + MEM32(0x50004030) = 0x50010101; + MEM32(0x50004034) = 0x5000fffe; + MEM32(0x50004100) = 0x5002fff7; + MEM32(0x50004104) = 10; + MEM32(0x50004108) = 0xffffffff; + memset((void *)ADDR(0x5000fffe), 0xa5, 259); + MEM8(0x5000fffd) = 'A'; + MEM8(0x50017ffb) = 'X'; + MEM8(0x50017ffc) = 'Y'; + g_esp = 0x5004f008; + g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456; + MEM32(g_esp + 4) = 1; MEM32(g_esp + 8) = 1; + MEM32(g_esp + 12) = 0x50001000; MEM32(g_esp + 16) = 0x50002000; + MEM32(g_esp + 20) = 0x50004000; MEM32(g_esp + 24) = 0x50004100; + recomp_yield_hook = hook ? observe_yield : NULL; + g_backedges = RECOMP_YIELD_EVERY - 1; + yields = 0; + + sub_250C1F70(); + + unsigned consumed = cases[trial].initial_bits ? 0 : 1; + assert(MEM32(0x5000401c) == cases[trial].final_bits); + assert(MEM32(0x50004020) == 0); + assert(MEM32(0x50004034) == 0x5000fffe + cases[trial].output_bytes); + assert(MEM32(0x50004100) == 0x5002fff7 + consumed); + assert(MEM32(0x50004104) == 10 - consumed); + assert(MEM32(0x50004108) == 0xffffffff + consumed); + assert(MEM32(0x50004118) == cases[trial].message); + for (unsigned i = 0; i < cases[trial].output_bytes; ++i) + assert(MEM8(0x5000fffe + i) == (trial == 4 ? "XYAXY"[i] : 'A')); + assert(MEM8(0x5000fffd) == 'A'); + assert(MEM8(0x5000fffe + cases[trial].output_bytes) == 0xa5); + assert(g_esp == 0x5004f00c && g_ebp == 123); + assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); + assert(g_eax == cases[trial].result && g_ecx == consumed); + assert(g_edx == 0x5000fffe + cases[trial].output_bytes); + assert(g_flag_k == (cases[trial].result ? FK_ADD : FK_TEST)); + assert(g_flag_a == (cases[trial].result ? 0xffffffff : 0)); + assert(g_flag_b == (cases[trial].result ? consumed : 0)); + assert(g_flag_cf == !!cases[trial].result); + unsigned did_yield = hook && cases[trial].edges; + assert(g_backedges == RECOMP_YIELD_EVERY - 1 + cases[trial].edges - did_yield * RECOMP_YIELD_EVERY); + assert(yields == did_yield); + } + } +}