aorecomp

Anarchy Online classic in the browser

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


client/tests/splitpath.c (3566B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 #else
      8 #include "guest_crt.h"
      9 #include "guest_memory.h"
     10 recomp_func_t recomp_lookup(uint32_t address) { abort(); }
     11 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     12 #endif
     13 
     14 int main(void)
     15 {
     16     static const char *cases[][5] = {
     17         {"", "", "", "", ""},
     18         {"song.wav", "", "", "song", ".wav"},
     19         {"music", "", "", "music", ""},
     20         {"C:\\AO\\audio\\song.wav", "C:", "\\AO\\audio\\", "song", ".wav"},
     21         {"c:song.wav", "c:", "", "song", ".wav"},
     22         {"C:", "C:", "", "", ""},
     23         {"1:file", "1:", "", "file", ""},
     24         {"a/b\\c.wav", "", "a/b\\", "c", ".wav"},
     25         {"a.b/c", "", "a.b/", "c", ""},
     26         {"/one/two/", "", "/one/two/", "", ""},
     27         {"\\\\host\\share\\file.ext", "", "\\\\host\\share\\", "file", ".ext"},
     28         {"file.tar.gz", "", "", "file.tar", ".gz"},
     29         {".hidden", "", "", "", ".hidden"},
     30         {".", "", "", "", "."},
     31         {"..", "", "", ".", "."},
     32         {"file.", "", "", "file", "."},
     33         {".././file", "", ".././", "file", ""},
     34         {"file:stream", "", "", "file:stream", ""},
     35     };
     36 #ifdef _WIN32
     37     HMODULE library = LoadLibraryA("msvcrt.dll");
     38     void (__cdecl *split)(const char *, char *, char *, char *, char *) =
     39         (void *)GetProcAddress(library, "_splitpath");
     40     int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno");
     41     assert(split && get_errno);
     42 #else
     43     assert(guest_map("splitpath test", 0x50000000, 0x20000));
     44     g_fs_base = 0x50000000;
     45 #endif
     46     for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
     47         for (unsigned mask = 0; mask < 16; ++mask) {
     48 #ifdef _WIN32
     49             char path[256], output[4][260];
     50             *get_errno() = 777;
     51 #else
     52             char *path = (char *)ADDR(0x5000fff0);
     53             char (*output)[260] = (void *)ADDR(0x50000100);
     54             MEM32(guest_crt_errno_address()) = 777;
     55             g_esp = 0x5000fe00;
     56             MEM32(g_esp) = RECOMP_RETADDR;
     57             MEM32(g_esp + 4) = 0x5000fff0;
     58             g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
     59 #endif
     60             strcpy(path, cases[i][0]);
     61             memset(output, 0xa5, 4 * 260);
     62 
     63 #ifdef _WIN32
     64             split(path, mask & 1 ? output[0] + 1 : NULL, mask & 2 ? output[1] + 1 : NULL,
     65                   mask & 4 ? output[2] + 1 : NULL, mask & 8 ? output[3] + 1 : NULL);
     66 #else
     67             for (unsigned part = 0; part < 4; ++part)
     68                 MEM32(g_esp + 8 + part * 4) = mask & (1u << part) ? 0x50000101 + part * 260 : 0;
     69             guest_crt_lookup("_splitpath")();
     70 #endif
     71 
     72             assert(!strcmp(path, cases[i][0]));
     73             for (unsigned part = 0; part < 4; ++part) {
     74                 size_t written = 0;
     75                 if (mask & (1u << part)) {
     76                     assert(!strcmp(output[part] + 1, cases[i][part + 1]));
     77                     written = strlen(cases[i][part + 1]) + 1;
     78                 }
     79                 assert((unsigned char)output[part][0] == 0xa5);
     80                 for (unsigned byte = written + 1; byte < 260; ++byte)
     81                     assert((unsigned char)output[part][byte] == 0xa5);
     82             }
     83 #ifdef _WIN32
     84             assert(*get_errno() == 777);
     85 #else
     86             assert(MEM32(guest_crt_errno_address()) == 777 && g_esp == 0x5000fe04);
     87             assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
     88 #endif
     89         }
     90     }
     91     puts("splitpath: 18 paths, all 16 output masks, canaries, errno and guest ABI passed");
     92 }