commit 407f4559907b5c5c32d929ee54f72425f4b300be
parent 9a98fa1ec142195caec93d6d11b85776ada44c93
Author: anon <anon@example.com>
Date: Fri, 16 Oct 2026 05:33:48 +0000
Implement Miles file loading with Windows differential tests
Diffstat:
2 files changed, 82 insertions(+), 0 deletions(-)
diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c
@@ -295,6 +295,48 @@ static void open_stream(void)
if (pthread_mutex_unlock(&audio_mutex)) abort();
}
+static void file_read(void)
+{
+ uint32_t filename = MEM32(g_esp + 4), destination = MEM32(g_esp + 8);
+ g_esp += 12;
+ g_eax = 0;
+ char *path = strdup((const char *)ADDR(filename));
+ if (!path) abort();
+ for (char *p = path; *p; ++p) if (*p == '\\') *p = '/';
+ FILE *file = fopen(path, "rb");
+ free(path);
+ if (!file) return;
+
+ /* Miles allocates four extra bytes; -1 requests a leading file length. */
+ if (fseek(file, 0, SEEK_END)) { fclose(file); return; }
+ long size = ftell(file);
+ if (size < 0 || size > UINT32_MAX - 4 || fseek(file, 0, SEEK_SET)) {
+ fclose(file);
+ return;
+ }
+ int allocated = !destination || destination == UINT32_MAX;
+ uint32_t result = allocated ? guest_malloc(size + 4) : destination;
+ if (!result) { fclose(file); return; }
+ uint32_t data = result;
+ if (destination == UINT32_MAX) {
+ MEM32(data) = size;
+ data += 4;
+ }
+ size_t count = fread((void *)ADDR(data), 1, size, file);
+ fclose(file);
+ if (count != (size_t)size) {
+ if (allocated) guest_free(result);
+ return;
+ }
+ g_eax = result;
+}
+
+static void mem_free_lock(void)
+{
+ guest_free(MEM32(g_esp + 4));
+ g_esp += 8;
+}
+
enum stream_operation { STREAM_STATUS, STREAM_VOLUME, STREAM_START, STREAM_PAUSE, STREAM_CLOSE, STREAM_POSITION };
static void stream_control(enum stream_operation operation, unsigned arguments)
@@ -358,6 +400,8 @@ recomp_func_t linux_audio_lookup(const char *dll, const char *name)
{"_AIL_shutdown@0", miles_shutdown},
{"_AIL_digital_latency@4", digital_latency},
{"_AIL_open_stream@12", open_stream},
+ {"_AIL_file_read@8", file_read},
+ {"_AIL_mem_free_lock@4", mem_free_lock},
{"_AIL_start_stream@4", start_stream},
{"_AIL_pause_stream@8", pause_stream},
{"_AIL_close_stream@4", close_stream},
diff --git a/decompiled/tests/audio.c b/decompiled/tests/audio.c
@@ -75,6 +75,43 @@ static uint32_t call(const char *name, unsigned count, const uint32_t *args)
#endif
}
+static void read_files(uint32_t scratch)
+{
+ const unsigned sizes[] = {0, 7, 32769};
+ for (unsigned i = 0; i < 3; ++i) {
+ FILE *file = fopen("miles-file-test.tmp", "wb");
+ assert(file);
+ for (unsigned j = 0; j < sizes[i]; ++j) assert(fputc(j & 255, file) != EOF);
+ assert(!fclose(file));
+ strcpy(POINTER(scratch), ".\\miles-file-test.tmp");
+ for (unsigned mode = 0; mode < (i == 2 ? 2u : 3u); ++mode) {
+ uint32_t args[] = {scratch, mode == 0 ? 0 : mode == 1 ? UINT32_MAX : scratch + 128};
+ memset(POINTER(scratch + 124), 0xa5, 32);
+
+ uint32_t result = call("_AIL_file_read@8", 2, args);
+
+ assert(result);
+ unsigned offset = mode == 1 ? 4 : 0;
+ if (mode == 1) assert(*(uint32_t *)POINTER(result) == sizes[i]);
+ for (unsigned j = 0; j < sizes[i]; ++j)
+ assert(((unsigned char *)POINTER(result))[offset + j] == (j & 255));
+ if (mode == 2) {
+ assert(result == scratch + 128);
+ assert(*(uint32_t *)POINTER(scratch + 124) == 0xa5a5a5a5);
+ assert(((unsigned char *)POINTER(result))[sizes[i]] == 0xa5);
+ } else call("_AIL_mem_free_lock@4", 1, &result);
+ }
+ assert(!remove("miles-file-test.tmp"));
+ }
+
+ uint32_t args[] = {scratch, 0};
+
+ uint32_t result = call("_AIL_file_read@8", 2, args);
+
+ assert(!result);
+ puts("audio files: allocated, length-prefixed, caller buffer, empty, multi-chunk and missing passed");
+}
+
static void redist_paths(uint32_t input)
{
const char *paths[] = {NULL, "", ".", "C:", "C:\\ao", "C:\\ao\\", "audio/"};
@@ -234,6 +271,7 @@ int main(int argc, char **argv)
assert(g_eax > 0 && g_eax == (unsigned)SDL_GetNumAudioDevices(0));
assert(g_esp == 0x5000ff04);
#endif
+ read_files(address);
redist_paths(address);
assert(call("_AIL_startup@0", 0, NULL) == 1);
assert(call("_AIL_startup@0", 0, NULL) == 0);