aorecomp

Anarchy Online classic in the browser

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


client/tests/strncmp.c (2479B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 static int (__cdecl *compare)(const char *, const char *, size_t);
      8 #else
      9 #include "guest_crt.h"
     10 #include "guest_memory.h"
     11 recomp_func_t recomp_lookup(uint32_t address) { abort(); }
     12 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     13 #endif
     14 
     15 static int call(const char *left, const char *right, unsigned count)
     16 {
     17 #ifdef _WIN32
     18     return compare(left, right, count);
     19 #else
     20     memcpy((void *)ADDR(0x50000100), left, 4);
     21     memcpy((void *)ADDR(0x50000200), right, 4);
     22     g_esp = 0x5000ff00;
     23     MEM32(g_esp + 4) = 0x50000100;
     24     MEM32(g_esp + 8) = 0x50000200;
     25     MEM32(g_esp + 12) = count;
     26     g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
     27     guest_crt_lookup("strncmp")();
     28     assert(g_esp == 0x5000ff04 && MEM32(g_esp + 8) == count);
     29     assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
     30     return (int32_t)g_eax;
     31 #endif
     32 }
     33 
     34 int main(void)
     35 {
     36 #ifdef _WIN32
     37     compare = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "strncmp");
     38     assert(compare);
     39 #else
     40     assert(guest_map("strncmp test", 0x50000000, 0x10000));
     41 #endif
     42     uint32_t digest = 2166136261u;
     43 
     44     for (unsigned count = 0; count <= 4; ++count)
     45         for (unsigned a = 0; a < 256; ++a)
     46             for (unsigned b = 0; b < 256; ++b) {
     47                 char left[] = {'A', (char)a, 'C', 0};
     48                 char right[] = {'A', (char)b, 'b', 0};
     49                 digest = (digest ^ (uint32_t)call(left, right, count)) * 16777619u;
     50             }
     51 
     52     assert(digest == 0x1e9c0ec5);
     53     assert(call("ABC", "ABD", 2) == 0);
     54     assert(call("ABC", "AbC", 3) == -32);
     55     assert(call("a\0Z", "a\0b", 4) == 0);
     56 #ifndef _WIN32
     57     MEM8(0x5000ffff) = 'A';
     58     MEM8(0x50000100) = 'A';
     59     g_esp = 0x5000ff00;
     60     MEM32(g_esp + 4) = 0x5000ffff;
     61     MEM32(g_esp + 8) = 0x50000100;
     62     MEM32(g_esp + 12) = 1;
     63     guest_crt_lookup("strncmp")();
     64     assert(g_eax == 0);
     65     assert(guest_map("strncmp second page", 0x50010000, 0x10000));
     66     MEM8(0x50010000) = 'B';
     67     MEM8(0x50000101) = 'C';
     68     g_esp = 0x5000ff00;
     69     MEM32(g_esp + 12) = 2;
     70     guest_crt_lookup("strncmp")();
     71     assert(g_eax == UINT32_MAX);
     72     g_esp = 0x5000ff00;
     73     MEM32(g_esp + 4) = MEM32(g_esp + 8) = MEM32(g_esp + 12) = 0;
     74     guest_crt_lookup("strncmp")();
     75     assert(g_eax == 0);
     76 #endif
     77     printf("strncmp: 327680 byte pairs, digest %08x, bounds, case, NUL and ABI passed\n", digest);
     78 }