client/tests/audio_decode.c (4339B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #ifdef _WIN32 7 #define WIN32_LEAN_AND_MEAN 8 #include <windows.h> 9 static HMODULE miles; 10 #define POINTER(p) ((void *)(uintptr_t)(p)) 11 static uint32_t allocate(unsigned size) { return (uint32_t)(uintptr_t)malloc(size); } 12 static void release(uint32_t data) { free(POINTER(data)); } 13 #else 14 #include "linux_audio.h" 15 #include "guest_heap.h" 16 #include "guest_memory.h" 17 #define POINTER(p) ((void *)ADDR(p)) 18 static uint32_t allocate(unsigned size) { return guest_malloc(size); } 19 static void release(uint32_t data) { guest_free(data); } 20 #endif 21 22 static uint32_t call(const char *name, unsigned count, const uint32_t *args) 23 { 24 #ifdef _WIN32 25 FARPROC function = GetProcAddress(miles, name); 26 assert(function); 27 switch (count) { 28 case 0: return ((uint32_t (__stdcall *)(void))function)(); 29 case 1: return ((uint32_t (__stdcall *)(uint32_t))function)(args[0]); 30 case 6: return ((uint32_t (__stdcall *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))function) 31 (args[0], args[1], args[2], args[3], args[4], args[5]); 32 default: abort(); 33 } 34 #else 35 g_esp = 0x5000ff00; 36 g_ebp = 0x11223344; g_ebx = 0x22334455; 37 g_esi = 0x33445566; g_edi = 0x44556677; 38 MEM32(g_esp) = RECOMP_RETADDR; 39 for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i]; 40 recomp_func_t function = linux_audio_lookup("mss32.dll", name); 41 assert(function); 42 function(); 43 assert(g_esp == 0x5000ff04 + count * 4); 44 assert(g_ebp == 0x11223344 && g_ebx == 0x22334455); 45 assert(g_esi == 0x33445566 && g_edi == 0x44556677); 46 return g_eax; 47 #endif 48 } 49 50 int main(int argc, char **argv) 51 { 52 #ifdef _WIN32 53 assert(argc == 5); 54 miles = LoadLibraryA(argv[3]); 55 assert(miles); 56 uint32_t directory = (uint32_t)(uintptr_t)argv[4]; 57 call("_AIL_set_redist_directory@4", 1, &directory); 58 #else 59 assert(argc == 3); 60 assert(guest_map("audio decode test", 0x50000000, 0x10000)); 61 #endif 62 assert(call("_AIL_startup@0", 0, NULL)); 63 FILE *file = fopen(argv[1], "rb"); 64 assert(file && !fseek(file, 0, SEEK_END)); 65 long size = ftell(file); 66 assert(size > 0 && size < INT32_MAX && !fseek(file, 0, SEEK_SET)); 67 uint32_t input = allocate(size), scratch = allocate(128); 68 assert(input && scratch); 69 assert(fread(POINTER(input), 1, size, file) == (size_t)size); 70 assert(!fclose(file)); 71 72 strcpy(POINTER(scratch), "fixture.MP3"); 73 uint32_t args[] = {input, size, scratch, scratch + 64, scratch + 68, 0}; 74 uint32_t *outputs = POINTER(scratch + 64); 75 outputs[-1] = outputs[2] = 0xa5a5a5a5; 76 outputs[0] = outputs[1] = 0; 77 78 assert(call("_AIL_decompress_ASI@24", 6, args)); 79 80 assert(outputs[-1] == 0xa5a5a5a5 && outputs[2] == 0xa5a5a5a5); 81 uint32_t wave = outputs[0], length = outputs[1]; 82 assert(wave && length > 44); 83 unsigned char *bytes = POINTER(wave); 84 assert(!memcmp(bytes, "RIFF", 4) && !memcmp(bytes + 8, "WAVEfmt ", 8)); 85 assert(*(uint32_t *)(bytes + 4) == length - 8); 86 assert(*(uint32_t *)(bytes + 16) == 16 && *(uint16_t *)(bytes + 20) == 1); 87 assert(*(uint16_t *)(bytes + 34) == 16 && !memcmp(bytes + 36, "data", 4)); 88 assert(*(uint32_t *)(bytes + 40) == length - 44); 89 file = fopen(argv[2], "wb"); 90 assert(file && fwrite(bytes, 1, length, file) == length && !fclose(file)); 91 92 /* The game's actual call leaves the output-size argument null. */ 93 args[4] = 0; 94 outputs[0] = 0; 95 assert(call("_AIL_decompress_ASI@24", 6, args)); 96 assert(outputs[0] && !memcmp(POINTER(outputs[0]), bytes, length)); 97 call("_AIL_mem_free_lock@4", 1, outputs); 98 call("_AIL_mem_free_lock@4", 1, &wave); 99 100 strcpy(POINTER(scratch), "fixture.wav"); 101 args[4] = scratch + 68; 102 outputs[0] = 0x11223344; outputs[1] = 0x55667788; 103 104 uint32_t rejected = call("_AIL_decompress_ASI@24", 6, args); 105 106 assert(!rejected && outputs[0] == 0x11223344 && outputs[1] == 0x55667788); 107 strcpy(POINTER(scratch), "fixture.mp3"); 108 args[1] = 0; 109 assert(!call("_AIL_decompress_ASI@24", 6, args)); 110 assert(outputs[0] == 0x11223344 && outputs[1] == 0x55667788); 111 release(input); 112 release(scratch); 113 call("_AIL_shutdown@0", 0, NULL); 114 puts("audio decode: PCM16 WAV, optional size, buffer ownership and rejected inputs passed"); 115 }