aorecomp

Anarchy Online classic in the browser

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


client/runtime/guest_msvcp.c (8352B)


      1 #include "guest_msvcp.h"
      2 #include "guest_heap.h"
      3 #include "guest_memory.h"
      4 
      5 #define STRING_TYPE "?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@"
      6 
      7 /* VC6 MSVCP60 char string layout and refcounts: docs/vc6-string-abi.md. */
      8 static void string_error(const char *reason)
      9 {
     10     fprintf(stderr, "Unsupported MSVCP60 string exception: %s\n", reason);
     11     abort();
     12 }
     13 
     14 static void tidy(uint32_t object, int built)
     15 {
     16     uint32_t data = MEM32(object + 4);
     17     if (built && data) {
     18         if (MEM8(data - 1) == 0 || MEM8(data - 1) == 255) guest_free(data - 1);
     19         else --MEM8(data - 1);
     20     }
     21     MEM32(object + 4) = MEM32(object + 8) = MEM32(object + 12) = 0;
     22 }
     23 
     24 static void eos(uint32_t object, uint32_t size)
     25 {
     26     MEM32(object + 8) = size;
     27     MEM8(MEM32(object + 4) + size) = 0;
     28 }
     29 
     30 static void copy(uint32_t object, uint32_t size)
     31 {
     32     uint32_t capacity = size | 31u;
     33     if (capacity > 0xfffffffdu) capacity = size;
     34     if (capacity > 0x7ffffffdu) string_error("allocation exceeds signed VC6 allocation size");
     35     uint32_t data = guest_malloc(capacity + 2);
     36     if (!data) string_error("allocation failure");
     37     uint32_t length = MEM32(object + 8);
     38     if (length > capacity) length = capacity;
     39     if (length) memcpy((void *)ADDR(data + 1), (void *)ADDR(MEM32(object + 4)), length);
     40     tidy(object, 1);
     41     MEM32(object + 4) = data + 1;
     42     MEM32(object + 12) = capacity;
     43     MEM8(data) = 0;
     44     eos(object, length);
     45 }
     46 
     47 static int grow(uint32_t object, uint32_t size, int trim)
     48 {
     49     if (size > 0xfffffffdu) string_error("length error");
     50     uint32_t data = MEM32(object + 4);
     51     if (data && MEM8(data - 1) != 0 && MEM8(data - 1) != 255) {
     52         if (size) { copy(object, size); return 1; }
     53         --MEM8(data - 1);
     54         tidy(object, 0);
     55         return 0;
     56     }
     57     if (!size) {
     58         if (trim) tidy(object, 1);
     59         else if (data) eos(object, 0);
     60         return 0;
     61     }
     62     if (trim && (MEM32(object + 12) > 31 || MEM32(object + 12) < size)) {
     63         tidy(object, 1);
     64         copy(object, size);
     65     } else if (MEM32(object + 12) < size) copy(object, size);
     66     return 1;
     67 }
     68 
     69 static void assign_bytes(uint32_t object, uint32_t text, uint32_t size)
     70 {
     71     if (grow(object, size, 1)) {
     72         memcpy((void *)ADDR(MEM32(object + 4)), (void *)ADDR(text), size);
     73         eos(object, size);
     74     }
     75 }
     76 
     77 void guest_msvcp_assign_host(uint32_t object, const char *text)
     78 {
     79     uint32_t size = strlen(text);
     80     if (grow(object, size, 1)) {
     81         memcpy((void *)ADDR(MEM32(object + 4)), text, size);
     82         eos(object, size);
     83     }
     84 }
     85 
     86 static void erase(uint32_t object, uint32_t offset, uint32_t count)
     87 {
     88     if (offset > MEM32(object + 8)) string_error("out of range");
     89     uint32_t data = MEM32(object + 4);
     90     /* _Split in this DLL deliberately obtains the shared buffer length with strlen. */
     91     if (data && MEM8(data - 1) != 0 && MEM8(data - 1) != 255) {
     92         tidy(object, 1);
     93         assign_bytes(object, data, (uint32_t)strlen((char *)ADDR(data)));
     94     }
     95     uint32_t remaining = MEM32(object + 8) - offset;
     96     if (count > remaining) count = remaining;
     97     if (count) {
     98         data = MEM32(object + 4);
     99         memmove((void *)ADDR(data + offset), (void *)ADDR(data + offset + count), remaining - count);
    100         uint32_t length = MEM32(object + 8) - count;
    101         if (grow(object, length, 0)) eos(object, length);
    102     }
    103 }
    104 
    105 static void assign_string(uint32_t object, uint32_t source, uint32_t offset, uint32_t count)
    106 {
    107     if (offset > MEM32(source + 8)) string_error("out of range");
    108     uint32_t size = MEM32(source + 8) - offset;
    109     if (count < size) size = count;
    110     if (object == source) {
    111         erase(object, offset + size, UINT32_MAX);
    112         erase(object, 0, offset);
    113     } else if (size && size == MEM32(source + 8) && MEM8(MEM32(source + 4) - 1) < 254) {
    114         tidy(object, 1);
    115         MEM32(object + 4) = MEM32(source + 4);
    116         MEM32(object + 8) = MEM32(source + 8);
    117         MEM32(object + 12) = MEM32(source + 12);
    118         ++MEM8(MEM32(object + 4) - 1);
    119     } else if (grow(object, size, 1)) {
    120         memcpy((void *)ADDR(MEM32(object + 4)), (void *)ADDR(MEM32(source + 4) + offset), size);
    121         eos(object, size);
    122     }
    123 }
    124 
    125 static void freeze(uint32_t object)
    126 {
    127     uint32_t data = MEM32(object + 4);
    128     if (data && MEM8(data - 1) != 0 && MEM8(data - 1) != 255)
    129         grow(object, MEM32(object + 8), 0);
    130     data = MEM32(object + 4);
    131     if (data) MEM8(data - 1) = 255;
    132 }
    133 
    134 uint32_t guest_msvcp_data(const char *name)
    135 {
    136     uint32_t address;
    137     if (!strcmp(name, "?npos@" STRING_TYPE "2IB")) address = 0x08600000u;
    138     else if (!strcmp(name, "?_C@?1??_Nullstr@" STRING_TYPE "CAPBDXZ@4DB")) address = 0x08600008u;
    139     else return 0;
    140     static int initialized;
    141     if (!initialized) {
    142         if (!guest_map("MSVCP string data", 0x08600000u, 0x10000u)) abort();
    143         MEM32(0x08600000u) = UINT32_MAX;
    144         initialized = 1;
    145     }
    146     return address;
    147 }
    148 
    149 /* Methods use ECX for this and pop their own arguments and return slot. */
    150 static void string_ctor(void)
    151 {
    152     MEM8(g_ecx) = MEM8(MEM32(g_esp + 4));
    153     tidy(g_ecx, 0);
    154     g_eax = g_ecx;
    155     g_esp += 8;
    156 }
    157 
    158 static void string_copy_ctor(void)
    159 {
    160     uint32_t source = MEM32(g_esp + 4);
    161     MEM8(g_ecx) = MEM8(source);
    162     tidy(g_ecx, 0);
    163     assign_string(g_ecx, source, 0, UINT32_MAX);
    164     g_eax = g_ecx;
    165     g_esp += 8;
    166 }
    167 
    168 static void string_dtor(void) { tidy(g_ecx, 1); g_esp += 4; }
    169 static void string_tidy(void) { tidy(g_ecx, MEM8(g_esp + 4)); g_esp += 8; }
    170 static void string_copy(void) { copy(g_ecx, MEM32(g_esp + 4)); g_esp += 8; }
    171 static void string_freeze(void) { freeze(g_ecx); g_esp += 4; }
    172 static void string_eos(void) { eos(g_ecx, MEM32(g_esp + 4)); g_esp += 8; }
    173 static void string_max_size(void) { g_eax = 0xfffffffdu; g_esp += 4; }
    174 static void string_size(void) { g_eax = MEM32(g_ecx + 8); g_esp += 4; }
    175 static void string_capacity(void) { g_eax = MEM32(g_ecx + 12); g_esp += 4; }
    176 static void string_xran(void) { string_error("out of range"); }
    177 static void string_xlen(void) { string_error("length error"); }
    178 
    179 static void string_grow(void)
    180 {
    181     SET_LO8(g_eax, grow(g_ecx, MEM32(g_esp + 4), MEM8(g_esp + 8)));
    182     g_esp += 12;
    183 }
    184 
    185 static void string_erase(void)
    186 {
    187     erase(g_ecx, MEM32(g_esp + 4), MEM32(g_esp + 8));
    188     g_eax = g_ecx;
    189     g_esp += 12;
    190 }
    191 
    192 static void string_assign_bytes(void)
    193 {
    194     assign_bytes(g_ecx, MEM32(g_esp + 4), MEM32(g_esp + 8));
    195     g_eax = g_ecx;
    196     g_esp += 12;
    197 }
    198 
    199 static void string_assign_string(void)
    200 {
    201     assign_string(g_ecx, MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12));
    202     g_eax = g_ecx;
    203     g_esp += 16;
    204 }
    205 
    206 static void string_less(void)
    207 {
    208     uint32_t left = MEM32(g_esp + 4), right = MEM32(g_esp + 8);
    209     uint32_t size = MEM32(left + 8) < MEM32(right + 8) ? MEM32(left + 8) : MEM32(right + 8);
    210     int result = size ? memcmp((void *)ADDR(MEM32(left + 4)), (void *)ADDR(MEM32(right + 4)), size) : 0;
    211     SET_LO8(g_eax, result < 0 || (!result && MEM32(left + 8) < MEM32(right + 8)));
    212     g_esp += 4;
    213 }
    214 
    215 recomp_func_t guest_msvcp_lookup(const char *name)
    216 {
    217     static const struct { const char *name; recomp_func_t function; } functions[] = {
    218         {"??0" STRING_TYPE "QAE@ABV?$allocator@D@1@@Z", string_ctor},
    219         {"??0" STRING_TYPE "QAE@ABV01@@Z", string_copy_ctor},
    220         {"??1" STRING_TYPE "QAE@XZ", string_dtor},
    221         {"?erase@" STRING_TYPE "QAEAAV12@II@Z", string_erase},
    222         {"?assign@" STRING_TYPE "QAEAAV12@PBDI@Z", string_assign_bytes},
    223         {"?assign@" STRING_TYPE "QAEAAV12@ABV12@II@Z", string_assign_string},
    224         {"?_Grow@" STRING_TYPE "AAE_NI_N@Z", string_grow},
    225         {"?_Copy@" STRING_TYPE "AAEXI@Z", string_copy},
    226         {"?_Eos@" STRING_TYPE "AAEXI@Z", string_eos},
    227         {"?_Freeze@" STRING_TYPE "AAEXXZ", string_freeze},
    228         {"?_Tidy@" STRING_TYPE "AAEX_N@Z", string_tidy},
    229         {"?max_size@" STRING_TYPE "QBEIXZ", string_max_size},
    230         {"?size@" STRING_TYPE "QBEIXZ", string_size},
    231         {"?capacity@" STRING_TYPE "QBEIXZ", string_capacity},
    232         {"??Mstd@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@0@Z", string_less},
    233         {"?_Xran@std@@YAXXZ", string_xran}, {"?_Xlen@std@@YAXXZ", string_xlen},
    234     };
    235     for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
    236         if (!strcmp(name, functions[i].name)) return functions[i].function;
    237     return NULL;
    238 }