aorecomp

Anarchy Online classic in the browser

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


client/runtime/guest_crt.c (39082B)


      1 #define _GNU_SOURCE
      2 #include "guest_crt.h"
      3 #include "guest_heap.h"
      4 #include "guest_memory.h"
      5 #include <ctype.h>
      6 #include <time.h>
      7 
      8 static _Thread_local uint32_t random_state = 1;
      9 static uint32_t user_matherr;
     10 static uint32_t *exit_callbacks;
     11 static size_t exit_callback_count;
     12 static int error_mode;
     13 static uint32_t abort_handler;
     14 static uint32_t new_handler;
     15 static int new_mode;
     16 
     17 uint32_t guest_crt_errno_address(void)
     18 {
     19     /* Private CRT storage inside the host-owned 64 KiB guest thread block. */
     20     return g_fs_base + 0x2000;
     21 }
     22 
     23 static void crt_errno(void)
     24 {
     25     g_eax = guest_crt_errno_address();
     26     g_esp += 4;
     27 }
     28 
     29 struct sort_context { uint32_t base; const unsigned char *memory; recomp_func_t compare; };
     30 
     31 static int compare_guest_elements(const void *left, const void *right, void *context)
     32 {
     33     struct sort_context *sort = context;
     34     uint32_t stack = g_esp, caller = g_cur_func;
     35     PUSH32(g_esp, sort->base + ((const unsigned char *)right - sort->memory));
     36     PUSH32(g_esp, sort->base + ((const unsigned char *)left - sort->memory));
     37     PUSH32(g_esp, RECOMP_RETADDR);
     38     sort->compare();
     39     if (g_esp != stack - 8) {
     40         fprintf(stderr, "Guest qsort comparator returned with an unbalanced stack\n");
     41         abort();
     42     }
     43     g_esp = stack;
     44     g_cur_func = caller;
     45     return (int32_t)g_eax;
     46 }
     47 
     48 static void crt_qsort(void)
     49 {
     50     uint32_t base = MEM32(g_esp + 4), count = MEM32(g_esp + 8);
     51     uint32_t width = MEM32(g_esp + 12), comparator = MEM32(g_esp + 16);
     52     if (count > 1 && width) {
     53         recomp_func_t compare = recomp_lookup(comparator);
     54         if (!compare) compare = recomp_lookup_import(comparator);
     55         if (!compare) { fprintf(stderr, "Missing qsort comparator %08x\n", comparator); abort(); }
     56         struct sort_context context = {base, (const unsigned char *)ADDR(base), compare};
     57         qsort_r((void *)context.memory, count, width, compare_guest_elements, &context);
     58     }
     59     g_esp += 4;
     60 }
     61 
     62 static void crt_set_error_mode(void)
     63 {
     64     int mode = (int32_t)MEM32(g_esp + 4);
     65     g_eax = error_mode;
     66     if (mode >= 0 && mode <= 2) error_mode = mode;
     67     else if (mode != 3) {
     68         MEM32(guest_crt_errno_address()) = 22; /* MSVCRT EINVAL */
     69         g_eax = UINT32_MAX;
     70     }
     71     g_esp += 4;
     72 }
     73 
     74 static void require_abort_signal(void)
     75 {
     76     if (MEM32(g_esp + 4) != 22) {
     77         fprintf(stderr, "Unsupported guest CRT signal %u\n", MEM32(g_esp + 4));
     78         abort();
     79     }
     80 }
     81 
     82 static void crt_signal(void)
     83 {
     84     require_abort_signal();
     85     g_eax = abort_handler;
     86     abort_handler = MEM32(g_esp + 8);
     87     g_esp += 4;
     88 }
     89 
     90 static void deliver_abort_signal(void)
     91 {
     92     if (!abort_handler) abort();
     93     if (abort_handler == 1) return; /* SIG_IGN */
     94     uint32_t handler = abort_handler, caller = g_cur_func, stack = g_esp;
     95     abort_handler = 0; /* MSVCRT resets the handler before synchronous delivery. */
     96     recomp_func_t function = recomp_lookup(handler);
     97     if (!function) function = recomp_lookup_import(handler);
     98     PUSH32(g_esp, 22);
     99     PUSH32(g_esp, RECOMP_RETADDR);
    100     function();
    101     if (g_esp != stack - 4) {
    102         fprintf(stderr, "Guest SIGABRT handler returned with an unbalanced stack\n");
    103         abort();
    104     }
    105     g_esp = stack;
    106     g_cur_func = caller;
    107 }
    108 
    109 static void crt_raise(void)
    110 {
    111     require_abort_signal();
    112     deliver_abort_signal();
    113     g_eax = 0;
    114     g_esp += 4;
    115 }
    116 
    117 static void crt_abort(void)
    118 {
    119     deliver_abort_signal();
    120     abort(); /* abort terminates even when the handler returns or ignores SIGABRT. */
    121 }
    122 
    123 uint32_t guest_crt_data(const char *name)
    124 {
    125     /* Initial C-locale state uses guest storage, including interlocked counters. */
    126     static const struct { const char *name; uint32_t offset; } symbols[] = {
    127         {"_adjust_fdiv", 0}, {"__unguarded_readlc_active", 4}, {"__setlc_active", 8},
    128         {"__lc_handle", 16}, {"__lc_codepage", 40}, {"__lc_collate_cp", 44},
    129         {"__mb_cur_max", 48}, {"_fmode", 52}, {"_commode", 56},
    130         {"_acmdln", 60}, {"__argc", 64}, {"__argv", 68},
    131         {"_environ", 72}, {"__initenv", 76}, {"_pgmptr", 80},
    132         {"__app_type", 84}, {"_pctype", 88},
    133         {"_iob", 0x1000},
    134     };
    135     unsigned index;
    136     for (index = 0; index < sizeof(symbols) / sizeof(symbols[0]); ++index)
    137         if (!strcmp(name, symbols[index].name)) break;
    138     if (index == sizeof(symbols) / sizeof(symbols[0])) return 0;
    139     static int initialized;
    140     if (!initialized) {
    141         if (!guest_map("CRT data", 0x08500000u, 0x10000u)) abort();
    142         MEM32(0x08500030u) = 1;
    143         MEM8(0x08500100u) = 'C';
    144         /* VC6 lconv has ten guest string pointers followed by eight CHAR_MAX fields. */
    145         MEM8(0x08500104u) = '.';
    146         for (unsigned i = 0; i < 10; ++i)
    147             MEM32(0x08500120u + i * 4) = i ? 0x08500106u : 0x08500104u;
    148         memset((void *)ADDR(0x08500148u), 127, 8);
    149         /* _pctype points past the EOF entry to 256 unsigned-short C-locale masks. */
    150         MEM32(0x08500058u) = 0x08500202u;
    151         for (unsigned c = 0; c < 128; ++c) {
    152             unsigned mask = c < 32 || c == 127 ? 0x20 : 0;
    153             if ((c >= 9 && c <= 13) || c == ' ') mask |= 8;
    154             if (c == ' ') mask |= 0x40;
    155             if (c >= 'A' && c <= 'Z') mask |= 1;
    156             if (c >= 'a' && c <= 'z') mask |= 2;
    157             if (c >= '0' && c <= '9') mask |= 4;
    158             if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) mask |= 0x80;
    159             if (c >= 33 && c <= 126 && !(mask & 7)) mask |= 0x10;
    160             MEM16(0x08500202u + c * 2) = mask;
    161         }
    162         /* MSVCRT _iobuf is eight DWORDs; host FILE objects never enter this array. */
    163         MEM32(0x08501000u) = 0x08502000u;
    164         MEM32(0x08501008u) = 0x08502000u;
    165         MEM32(0x0850100cu) = 0x101; /* Initial MSVCRT stdin flags. */
    166         MEM32(0x08501018u) = 4096;
    167         for (uint32_t index = 1; index < 3; ++index) {
    168             MEM32(0x08501000u + index * 32 + 12) = 2; /* _IOWRT */
    169             MEM32(0x08501000u + index * 32 + 16) = index;
    170         }
    171         initialized = 1;
    172     }
    173     return 0x08500000u + symbols[index].offset;
    174 }
    175 
    176 static uint32_t copy_string_vector(int count, char **strings)
    177 {
    178     uint64_t size = ((uint64_t)count + 1) * 4;
    179     for (int i = 0; i < count; ++i) size += strlen(strings[i]) + 1;
    180     uint32_t result = size <= UINT32_MAX ? guest_malloc((uint32_t)size) : 0;
    181     if (!result) { fprintf(stderr, "Guest startup allocation failed\n"); abort(); }
    182     uint32_t text = result + (count + 1) * 4;
    183     for (int i = 0; i < count; ++i) {
    184         size_t length = strlen(strings[i]) + 1;
    185         MEM32(result + i * 4) = text;
    186         memcpy((void *)ADDR(text), strings[i], length);
    187         text += (uint32_t)length;
    188     }
    189     MEM32(result + count * 4) = 0;
    190     return result;
    191 }
    192 
    193 void guest_crt_startup(int argc, char **argv, char **environment)
    194 {
    195     /* Keep both the pointer arrays and their strings in guest-owned memory. */
    196     int count = 0;
    197     while (environment[count]) ++count;
    198     MEM32(guest_crt_data("__argc")) = argc;
    199     MEM32(guest_crt_data("__argv")) = copy_string_vector(argc, argv);
    200     MEM32(guest_crt_data("_environ")) = copy_string_vector(count, environment);
    201     MEM32(guest_crt_data("__initenv")) = MEM32(guest_crt_data("_environ"));
    202     MEM32(guest_crt_data("_pgmptr")) = MEM32(MEM32(guest_crt_data("__argv")));
    203 
    204     /* Preserve simple login tokens; quote only arguments that need escaping. */
    205     uint64_t capacity = 1;
    206     for (int i = 0; i < argc; ++i) capacity += strlen(argv[i]) * 2 + 3;
    207     uint32_t command = capacity <= UINT32_MAX ? guest_malloc((uint32_t)capacity) : 0;
    208     if (!command) { fprintf(stderr, "Guest command line allocation failed\n"); abort(); }
    209     char *output = (char *)ADDR(command);
    210     for (int i = 0; i < argc; ++i) {
    211         if (i) *output++ = ' ';
    212         if (*argv[i] && !strpbrk(argv[i], " \t\"")) {
    213             size_t length = strlen(argv[i]);
    214             memcpy(output, argv[i], length);
    215             output += length;
    216             continue;
    217         }
    218         *output++ = '"';
    219         const char *input = argv[i];
    220         while (*input) {
    221             unsigned slashes = 0;
    222             while (*input == '\\') { ++slashes; ++input; }
    223             unsigned escaped = (*input == '"' || !*input) ? slashes * 2 : slashes;
    224             while (escaped--) *output++ = '\\';
    225             if (*input == '"') *output++ = '\\';
    226             if (*input) *output++ = *input++;
    227         }
    228         *output++ = '"';
    229     }
    230     *output = 0;
    231     MEM32(guest_crt_data("_acmdln")) = command;
    232 }
    233 
    234 static void crt_set_app_type(void)
    235 {
    236     MEM32(guest_crt_data("__app_type")) = MEM32(g_esp + 4);
    237     g_esp += 4;
    238 }
    239 
    240 static void crt_p_fmode(void)
    241 {
    242     g_eax = guest_crt_data("_fmode");
    243     g_esp += 4;
    244 }
    245 
    246 static void crt_p_commode(void)
    247 {
    248     g_eax = guest_crt_data("_commode");
    249     g_esp += 4;
    250 }
    251 
    252 static void crt_setusermatherr(void)
    253 {
    254     user_matherr = MEM32(g_esp + 4);
    255     g_esp += 4;
    256 }
    257 
    258 static void crt_getmainargs(void)
    259 {
    260     /* AO disables wildcard expansion and the malloc new-handler mode. */
    261     if (MEM32(g_esp + 16) || MEM32(MEM32(g_esp + 20))) {
    262         fprintf(stderr, "Guest CRT wildcard expansion/new-handler mode is not implemented\n");
    263         abort();
    264     }
    265     MEM32(MEM32(g_esp + 4)) = MEM32(guest_crt_data("__argc"));
    266     MEM32(MEM32(g_esp + 8)) = MEM32(guest_crt_data("__argv"));
    267     MEM32(MEM32(g_esp + 12)) = MEM32(guest_crt_data("_environ"));
    268     g_eax = 0;
    269     g_esp += 4;
    270 }
    271 
    272 static void crt_controlfp(void)
    273 {
    274     /* MSVCRT's public mask differs from the x87 word used by generated C. */
    275     uint32_t state = ((g_fpu_cw & 1) << 4) | ((g_fpu_cw & 2) << 18) |
    276                      ((g_fpu_cw & 4) << 1) | ((g_fpu_cw & 8) >> 1) |
    277                      ((g_fpu_cw & 16) >> 3) | ((g_fpu_cw & 32) >> 5) |
    278                      ((g_fpu_cw & 0x0c00) >> 2) | ((g_fpu_cw & 0x1000) << 6);
    279     if ((g_fpu_cw & 0x0300) == 0x0200) state |= 0x10000; /* 53 bits */
    280     if (!(g_fpu_cw & 0x0300)) state |= 0x20000; /* 24 bits */
    281     uint32_t mask = MEM32(g_esp + 8) & 0x7031f; /* _controlfp preserves denormal masking. */
    282     uint32_t requested = (state & ~mask) | (MEM32(g_esp + 4) & mask);
    283     /* AO's CRT selects the existing 53-bit, nearest, masked-exception mode.
    284      * Other arithmetic modes need support in generated operations first. */
    285     if (requested != state || g_fpu_cw != 0x027f) {
    286         fprintf(stderr, "Unsupported guest floating-point mode %08x (x87=%04x)\n",
    287                 requested, g_fpu_cw);
    288         abort();
    289     }
    290     g_eax = state;
    291     g_esp += 4;
    292 }
    293 
    294 static void crt_initterm(void)
    295 {
    296     uint32_t begin = MEM32(g_esp + 4), end = MEM32(g_esp + 8);
    297     uint32_t caller = g_cur_func;
    298     for (uint32_t slot = begin; slot < end; slot += 4) {
    299         uint32_t entry = MEM32(slot);
    300         if (!entry) continue;
    301         recomp_func_t function = recomp_lookup(entry);
    302         if (!function) function = recomp_lookup_import(entry);
    303         if (!function) { fprintf(stderr, "Missing CRT initializer %08x\n", entry); abort(); }
    304         PUSH32(g_esp, RECOMP_RETADDR);
    305         function();
    306     }
    307     g_cur_func = caller;
    308     g_esp += 4;
    309 }
    310 
    311 static int call_new_handler(uint32_t size)
    312 {
    313     if (!new_handler) return 0;
    314     uint32_t caller = g_cur_func, stack = g_esp;
    315     recomp_func_t function = recomp_lookup(new_handler);
    316     if (!function) function = recomp_lookup_import(new_handler);
    317     PUSH32(g_esp, size);
    318     PUSH32(g_esp, RECOMP_RETADDR);
    319     function();
    320     if (g_esp != stack - 4) {
    321         fprintf(stderr, "Guest new handler returned with an unbalanced stack\n");
    322         abort();
    323     }
    324     g_esp = stack;
    325     g_cur_func = caller;
    326     return g_eax != 0;
    327 }
    328 
    329 static void crt_set_new_handler(void)
    330 {
    331     g_eax = new_handler;
    332     new_handler = MEM32(g_esp + 4);
    333     g_esp += 4;
    334 }
    335 
    336 static void crt_set_new_mode(void)
    337 {
    338     uint32_t mode = MEM32(g_esp + 4);
    339     g_eax = new_mode;
    340     if (mode <= 1) new_mode = mode;
    341     else { MEM32(guest_crt_errno_address()) = 22; g_eax = UINT32_MAX; }
    342     g_esp += 4;
    343 }
    344 
    345 static uint32_t allocate(uint32_t size, uint32_t old)
    346 {
    347     uint32_t result = 0;
    348     /* MSVCRT calls the handler once for oversized malloc/realloc, in either mode. */
    349     if (size > 0xffffffe0u) call_new_handler(size);
    350     else {
    351         do {
    352             result = old ? guest_realloc(old, size) : guest_malloc(size);
    353         } while (!result && new_mode && call_new_handler(size));
    354     }
    355     if (!result) MEM32(guest_crt_errno_address()) = 12;
    356     return result;
    357 }
    358 
    359 /* Every CRT adapter consumes the guest return slot; cdecl callers pop arguments. */
    360 static void crt_malloc(void)
    361 {
    362     g_eax = allocate(MEM32(g_esp + 4), 0);
    363     g_esp += 4;
    364 }
    365 
    366 static void crt_srand(void)
    367 {
    368     random_state = MEM32(g_esp + 4);
    369     g_esp += 4;
    370 }
    371 
    372 static void crt_rand(void)
    373 {
    374     random_state = random_state * 214013u + 2531011u;
    375     g_eax = (random_state >> 16) & 0x7fffu;
    376     g_esp += 4;
    377 }
    378 
    379 static void crt_free(void)
    380 {
    381     guest_free(MEM32(g_esp + 4));
    382     g_esp += 4;
    383 }
    384 
    385 static void crt_realloc(void)
    386 {
    387     uint32_t old = MEM32(g_esp + 4), size = MEM32(g_esp + 8);
    388     if (old && !size) { guest_free(old); g_eax = 0; }
    389     else g_eax = allocate(size, old);
    390     g_esp += 4;
    391 }
    392 
    393 static void crt_calloc(void)
    394 {
    395     uint64_t size = (uint64_t)MEM32(g_esp + 4) * MEM32(g_esp + 8);
    396     g_eax = 0;
    397     if (size > 0xffffffe0u) MEM32(guest_crt_errno_address()) = 12;
    398     else g_eax = allocate((uint32_t)size, 0);
    399     if (g_eax) memset((void *)ADDR(g_eax), 0, (size_t)size);
    400     g_esp += 4;
    401 }
    402 
    403 static void crt_dllonexit(void)
    404 {
    405     uint32_t function = MEM32(g_esp + 4);
    406     uint32_t begin_slot = MEM32(g_esp + 8), end_slot = MEM32(g_esp + 12);
    407     uint32_t begin = MEM32(begin_slot), size = MEM32(end_slot) - begin;
    408     uint32_t replacement = size <= UINT32_MAX - 4 ? guest_realloc(begin, size + 4) : 0;
    409     g_eax = 0;
    410     if (replacement) {
    411         MEM32(replacement + size) = function;
    412         MEM32(begin_slot) = replacement;
    413         MEM32(end_slot) = replacement + size + 4;
    414         g_eax = function;
    415     }
    416     g_esp += 4;
    417 }
    418 
    419 static void crt_onexit(void)
    420 {
    421     uint32_t callback = MEM32(g_esp + 4);
    422     uint32_t *replacement = callback ? realloc(exit_callbacks,
    423         (exit_callback_count + 1) * sizeof(*exit_callbacks)) : NULL;
    424     g_eax = 0;
    425     if (replacement) {
    426         exit_callbacks = replacement;
    427         exit_callbacks[exit_callback_count++] = callback;
    428         g_eax = callback;
    429     }
    430     g_esp += 4;
    431 }
    432 
    433 static void crt_cexit(void)
    434 {
    435     uint32_t caller = g_cur_func;
    436     while (exit_callback_count) {
    437         uint32_t entry = exit_callbacks[--exit_callback_count];
    438         recomp_func_t function = recomp_lookup(entry);
    439         if (!function) function = recomp_lookup_import(entry);
    440         if (!function) { fprintf(stderr, "Missing CRT exit callback %08x\n", entry); abort(); }
    441         PUSH32(g_esp, RECOMP_RETADDR);
    442         function();
    443     }
    444     free(exit_callbacks);
    445     exit_callbacks = NULL;
    446     fflush(NULL);
    447     g_cur_func = caller;
    448     g_esp += 4;
    449 }
    450 
    451 static void crt_new(void)
    452 {
    453     uint32_t size = MEM32(g_esp + 4), result;
    454     do { result = allocate(size, 0); } while (!result && call_new_handler(size));
    455     g_eax = result;
    456     g_esp += 4;
    457 }
    458 
    459 static void crt_ftol(void)
    460 {
    461     /* Match runtime/x87crt.c:native32_shim_ftol, including exact integer shadows. */
    462     int64_t value = fp_st0_to_i64(g_st[0], g_st_i64[0], 0x0c7f);
    463     fp_pop_impl(g_st, &g_fp_top);
    464     g_eax = (uint32_t)value;
    465     g_edx = (uint32_t)((uint64_t)value >> 32);
    466     g_esp += 4;
    467 }
    468 
    469 static void crt_floor(void)
    470 {
    471     fp_push_impl(g_st, &g_fp_top, floor(MEMD(g_esp + 4)));
    472     g_esp += 4;
    473 }
    474 
    475 static void crt_finite(void)
    476 {
    477     /* Inspect the exponent without evaluating a signaling NaN on the host FPU. */
    478     g_eax = (MEM32(g_esp + 8) & 0x7ff00000u) != 0x7ff00000u;
    479     g_esp += 4;
    480 }
    481 
    482 static void crt_ceil(void)
    483 {
    484     fp_push_impl(g_st, &g_fp_top, ceil(MEMD(g_esp + 4)));
    485     g_esp += 4;
    486 }
    487 
    488 static void crt_ciacos(void)
    489 {
    490     double input = fp_pop_impl(g_st, &g_fp_top);
    491     double value = acos(input);
    492     if (!isfinite(input) || fabs(input) > 1) MEM32(guest_crt_errno_address()) = 33;
    493     fp_push_impl(g_st, &g_fp_top, value);
    494     g_esp += 4;
    495 }
    496 
    497 static void crt_cipow(void)
    498 {
    499     double exponent = fp_pop_impl(g_st, &g_fp_top);
    500     double base = fp_pop_impl(g_st, &g_fp_top);
    501     double value = pow(base, exponent);
    502     /* MSVCRT treats NaN operands and (+/-1)^(-infinity) as domain errors. */
    503     if (isnan(base) || isnan(exponent) || (fabs(base) == 1 && exponent == -INFINITY)) value = -NAN;
    504     if (isnan(value)) MEM32(guest_crt_errno_address()) = 33;
    505     else if (isinf(value) && isfinite(base) && isfinite(exponent)) MEM32(guest_crt_errno_address()) = 34;
    506     fp_push_impl(g_st, &g_fp_top, value);
    507     g_esp += 4;
    508 }
    509 
    510 static void crt_cifmod(void)
    511 {
    512     double divisor = fp_pop_impl(g_st, &g_fp_top);
    513     double dividend = fp_pop_impl(g_st, &g_fp_top);
    514     double value = fmod(dividend, divisor);
    515     if (!dividend && divisor && isfinite(divisor)) value = 0;
    516     /* Legacy MSVCRT treats every non-finite operand as a domain error. */
    517     if (isinf(divisor) && !isnan(dividend)) value = -NAN;
    518     if (!isfinite(dividend) || !isfinite(divisor) || !divisor)
    519         MEM32(guest_crt_errno_address()) = 33;
    520     fp_push_impl(g_st, &g_fp_top, value);
    521     g_esp += 4;
    522 }
    523 
    524 static void crt_atof(void)
    525 {
    526     const char *input = (const char *)ADDR(MEM32(g_esp + 4));
    527     while (*input == ' ' || (*input >= '\t' && *input <= '\r')) ++input;
    528     const char *end = input, *exponent = NULL;
    529     if (*end == '+' || *end == '-') ++end;
    530     while (*end >= '0' && *end <= '9') ++end;
    531     if (*end == '.') {
    532         ++end;
    533         while (*end >= '0' && *end <= '9') ++end;
    534     }
    535     if (*end == 'e' || *end == 'E' || *end == 'd' || *end == 'D') {
    536         exponent = end++;
    537         if (*end == '+' || *end == '-') ++end;
    538         while (*end >= '0' && *end <= '9') ++end;
    539     }
    540     /* MSVCRT accepts decimal/D exponents, rejects hex/inf/nan, and preserves errno. */
    541     size_t length = end - input;
    542     char *decimal = malloc(length + 1);
    543     if (!decimal) abort();
    544     memcpy(decimal, input, length);
    545     decimal[length] = 0;
    546     if (exponent) decimal[exponent - input] = 'e';
    547     double value = strtod(decimal, NULL);
    548     free(decimal);
    549     fp_push_impl(g_st, &g_fp_top, value);
    550     g_esp += 4;
    551 }
    552 
    553 static void crt_isspace(void)
    554 {
    555     uint32_t value = MEM32(g_esp + 4);
    556     g_eax = value == ' ' || (value >= '\t' && value <= '\r') ? 8 : 0;
    557     g_esp += 4;
    558 }
    559 
    560 static void crt_itoa(void)
    561 {
    562     uint32_t value = MEM32(g_esp + 4), output = MEM32(g_esp + 8), radix = MEM32(g_esp + 12);
    563     if (radix < 2 || radix > 36) { fprintf(stderr, "Invalid _itoa radix %u\n", radix); abort(); }
    564     g_eax = output;
    565     if (radix == 10 && (int32_t)value < 0) {
    566         MEM8(output++) = '-';
    567         value = 0u - value;
    568     }
    569     char digits[32];
    570     unsigned count = 0;
    571     do {
    572         digits[count++] = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
    573         value /= radix;
    574     } while (value);
    575     while (count) MEM8(output++) = digits[--count];
    576     MEM8(output) = 0;
    577     g_esp += 4;
    578 }
    579 
    580 static void crt_eh_prolog(void)
    581 {
    582     /* Match runtime/native32.c:native32_shim_EH_prolog's guest stack layout. */
    583     PUSH32(g_esp, UINT32_MAX);
    584     PUSH32(g_esp, g_eax);
    585     g_eax = MEM32(g_fs_base);
    586     PUSH32(g_esp, g_eax);
    587     g_eax = MEM32(g_esp + 12);
    588     MEM32(g_fs_base) = g_esp;
    589     MEM32(g_esp + 12) = g_ebp;
    590     g_ebp = g_esp + 12;
    591     MEM32(g_esp - 4) = g_eax;
    592 }
    593 
    594 static void crt_time(void)
    595 {
    596     time_t now = time(NULL);
    597     g_eax = now >= 0 && now <= INT32_MAX ? (uint32_t)now : UINT32_MAX;
    598     uint32_t output = MEM32(g_esp + 4);
    599     if (output) MEM32(output) = g_eax;
    600     g_esp += 4;
    601 }
    602 
    603 static void crt_gmtime(void)
    604 {
    605     uint32_t input = MEM32(g_esp + 4);
    606     g_esp += 4;
    607     g_eax = 0;
    608     if (!input || (int32_t)MEM32(input) < -43200) {
    609         MEM32(guest_crt_errno_address()) = 22;
    610         return;
    611     }
    612 
    613     /* Legacy MSVCRT accepts twelve hours before the epoch without normalizing. */
    614     int32_t seconds = MEM32(input);
    615     time_t timestamp = seconds < 0 ? 0 : seconds;
    616     struct tm converted;
    617     gmtime_r(&timestamp, &converted);
    618     if (seconds < 0) {
    619         converted.tm_sec = seconds % 60;
    620         converted.tm_min = seconds / 60 % 60;
    621         converted.tm_hour = seconds / 3600;
    622     }
    623     int32_t fields[] = {converted.tm_sec, converted.tm_min, converted.tm_hour,
    624                        converted.tm_mday, converted.tm_mon, converted.tm_year,
    625                        converted.tm_wday, converted.tm_yday, converted.tm_isdst};
    626     g_eax = g_fs_base + 0x2040;
    627     memcpy((void *)ADDR(g_eax), fields, sizeof(fields));
    628 }
    629 
    630 static void crt_strdate(void)
    631 {
    632     time_t now = time(NULL);
    633     struct tm converted;
    634     localtime_r(&now, &converted);
    635     g_eax = MEM32(g_esp + 4);
    636     strftime((char *)ADDR(g_eax), 9, "%m/%d/%y", &converted);
    637     g_esp += 4;
    638 }
    639 
    640 static void crt_asctime(void)
    641 {
    642     uint32_t input = MEM32(g_esp + 4);
    643     g_esp += 4;
    644     g_eax = 0;
    645     int32_t fields[9];
    646     if (input) memcpy(fields, (void *)ADDR(input), sizeof(fields));
    647     if (!input || fields[0] < 0 || fields[0] > 59 || fields[1] < 0 || fields[1] > 59 ||
    648         fields[2] < 0 || fields[2] > 23 || fields[3] < 1 || fields[3] > 31 ||
    649         fields[4] < 0 || fields[4] > 11 || fields[5] < 0 || fields[5] > 8099 ||
    650         fields[6] < 0 || fields[6] > 6) {
    651         MEM32(guest_crt_errno_address()) = 22;
    652         return;
    653     }
    654 
    655     /* MSVCRT pads the day with zero and returns a per-thread 26-byte buffer. */
    656     g_eax = g_fs_base + 0x2080;
    657     snprintf((char *)ADDR(g_eax), 26, "%.3s %.3s %02d %02d:%02d:%02d %04d\n",
    658              "SunMonTueWedThuFriSat" + fields[6] * 3,
    659              "JanFebMarAprMayJunJulAugSepOctNovDec" + fields[4] * 3,
    660              fields[3], fields[2], fields[1], fields[0], fields[5] + 1900);
    661 }
    662 
    663 static void crt_strcpy(void)
    664 {
    665     g_eax = MEM32(g_esp + 4);
    666     strcpy((char *)ADDR(g_eax), (const char *)ADDR(MEM32(g_esp + 8)));
    667     g_esp += 4;
    668 }
    669 
    670 static void crt_strncpy(void)
    671 {
    672     uint32_t dst = MEM32(g_esp + 4);
    673     strncpy((char *)ADDR(dst), (const char *)ADDR(MEM32(g_esp + 8)), MEM32(g_esp + 12));
    674     g_eax = dst;
    675     g_esp += 4;
    676 }
    677 
    678 static void crt_strchr(void)
    679 {
    680     uint32_t start = MEM32(g_esp + 4);
    681     const char *text = (const char *)ADDR(start);
    682     const char *found = strchr(text, (int)MEM32(g_esp + 8));
    683     g_eax = found ? start + (uint32_t)(found - text) : 0;
    684     g_esp += 4;
    685 }
    686 
    687 static void crt_strcspn(void)
    688 {
    689     g_eax = (uint32_t)strcspn((const char *)ADDR(MEM32(g_esp + 4)),
    690                              (const char *)ADDR(MEM32(g_esp + 8)));
    691     g_esp += 4;
    692 }
    693 
    694 static void crt_strstr(void)
    695 {
    696     uint32_t start = MEM32(g_esp + 4);
    697     const char *text = (const char *)ADDR(start);
    698     const char *found = strstr(text, (const char *)ADDR(MEM32(g_esp + 8)));
    699     g_eax = found ? start + (uint32_t)(found - text) : 0;
    700     g_esp += 4;
    701 }
    702 
    703 static void crt_strlen(void)
    704 {
    705     g_eax = (uint32_t)strlen((const char *)ADDR(MEM32(g_esp + 4)));
    706     g_esp += 4;
    707 }
    708 
    709 static void crt_strrchr(void)
    710 {
    711     uint32_t start = MEM32(g_esp + 4);
    712     const char *text = (const char *)ADDR(start);
    713     const char *found = strrchr(text, (int)MEM32(g_esp + 8));
    714     g_eax = found ? start + (uint32_t)(found - text) : 0;
    715     g_esp += 4;
    716 }
    717 
    718 static void crt_getenv(void)
    719 {
    720     uint32_t name = MEM32(g_esp + 4);
    721     g_esp += 4;
    722     g_eax = 0;
    723     if (!name) { MEM32(guest_crt_errno_address()) = 22; return; }
    724     const unsigned char *query = (const unsigned char *)ADDR(name);
    725     size_t length = strlen((const char *)query);
    726     uint32_t environment = MEM32(guest_crt_data("_environ"));
    727     for (uint32_t i = 0; MEM32(environment + i * 4); ++i) {
    728         uint32_t entry = MEM32(environment + i * 4);
    729         const unsigned char *value = (const unsigned char *)ADDR(entry);
    730         size_t matched = 0;
    731         while (matched < length) {
    732             unsigned a = query[matched], b = value[matched];
    733             if (a >= 'A' && a <= 'Z') a += 'a' - 'A';
    734             if (b >= 'A' && b <= 'Z') b += 'a' - 'A';
    735             if (a != b) break;
    736             ++matched;
    737         }
    738         if (matched == length && value[matched] == '=') {
    739             g_eax = entry + length + 1;
    740             return;
    741         }
    742     }
    743 }
    744 
    745 static void crt_tolower(void)
    746 {
    747     g_eax = MEM32(g_esp + 4);
    748     if (g_eax >= 'A' && g_eax <= 'Z') g_eax += 'a' - 'A';
    749     g_esp += 4;
    750 }
    751 
    752 static void crt_toupper(void)
    753 {
    754     g_eax = MEM32(g_esp + 4);
    755     if (g_eax >= 'a' && g_eax <= 'z') g_eax -= 'a' - 'A';
    756     g_esp += 4;
    757 }
    758 
    759 static void crt_isupper(void)
    760 {
    761     uint32_t value = MEM32(g_esp + 4);
    762     g_eax = value >= 'A' && value <= 'Z' ? 1 : 0;
    763     g_esp += 4;
    764 }
    765 
    766 static void crt_islower(void)
    767 {
    768     uint32_t value = MEM32(g_esp + 4);
    769     g_eax = value >= 'a' && value <= 'z' ? 2 : 0;
    770     g_esp += 4;
    771 }
    772 
    773 static void crt_stricmp(void)
    774 {
    775     const unsigned char *left = (const unsigned char *)ADDR(MEM32(g_esp + 4));
    776     const unsigned char *right = (const unsigned char *)ADDR(MEM32(g_esp + 8));
    777     unsigned a, b;
    778     /* The guest CRT currently supports the initial C locale only. */
    779     do {
    780         a = *left++; b = *right++;
    781         if (a >= 'A' && a <= 'Z') a += 'a' - 'A';
    782         if (b >= 'A' && b <= 'Z') b += 'a' - 'A';
    783     } while (a && a == b);
    784     g_eax = (uint32_t)((a > b) - (a < b));
    785     g_esp += 4;
    786 }
    787 
    788 static void crt_strnicmp(void)
    789 {
    790     uint32_t left = MEM32(g_esp + 4), right = MEM32(g_esp + 8);
    791     uint32_t count = MEM32(g_esp + 12);
    792     g_eax = 0;
    793     /* Resolve each guest byte so a bounded comparison can cross guest pages. */
    794     for (uint32_t i = 0; i < count; ++i) {
    795         unsigned a = MEM8(left + i), b = MEM8(right + i);
    796         if (a >= 'A' && a <= 'Z') a += 'a' - 'A';
    797         if (b >= 'A' && b <= 'Z') b += 'a' - 'A';
    798         if (a != b) { g_eax = (uint32_t)((a > b) - (a < b)); break; }
    799         if (!a) break;
    800     }
    801     g_esp += 4;
    802 }
    803 
    804 static void crt_strcmp(void)
    805 {
    806     g_eax = (uint32_t)strcmp((const char *)ADDR(MEM32(g_esp + 4)),
    807                              (const char *)ADDR(MEM32(g_esp + 8)));
    808     g_esp += 4;
    809 }
    810 
    811 static void crt_strncmp(void)
    812 {
    813     uint32_t left = MEM32(g_esp + 4), right = MEM32(g_esp + 8), count = MEM32(g_esp + 12);
    814     g_eax = 0;
    815     for (uint32_t i = 0; i < count; ++i) {
    816         unsigned a = MEM8(left + i), b = MEM8(right + i);
    817         if (a != b) { g_eax = a - b; break; }
    818         if (!a) break;
    819     }
    820     g_esp += 4;
    821 }
    822 
    823 static void crt_memcmp(void)
    824 {
    825     g_eax = (uint32_t)memcmp((const void *)ADDR(MEM32(g_esp + 4)),
    826                              (const void *)ADDR(MEM32(g_esp + 8)), MEM32(g_esp + 12));
    827     g_esp += 4;
    828 }
    829 
    830 static void crt_memchr(void)
    831 {
    832     uint32_t start = MEM32(g_esp + 4);
    833     const char *bytes = (const char *)ADDR(start);
    834     const char *found = memchr(bytes, (int)MEM32(g_esp + 8), MEM32(g_esp + 12));
    835     g_eax = found ? start + (uint32_t)(found - bytes) : 0;
    836     g_esp += 4;
    837 }
    838 
    839 static void crt_memcpy(void)
    840 {
    841     g_eax = MEM32(g_esp + 4);
    842     memcpy((void *)ADDR(g_eax), (const void *)ADDR(MEM32(g_esp + 8)), MEM32(g_esp + 12));
    843     g_esp += 4;
    844 }
    845 
    846 static void crt_memmove(void)
    847 {
    848     g_eax = MEM32(g_esp + 4);
    849     memmove((void *)ADDR(g_eax), (const void *)ADDR(MEM32(g_esp + 8)), MEM32(g_esp + 12));
    850     g_esp += 4;
    851 }
    852 
    853 static void crt_memset(void)
    854 {
    855     g_eax = MEM32(g_esp + 4);
    856     memset((void *)ADDR(g_eax), MEM32(g_esp + 8), MEM32(g_esp + 12));
    857     g_esp += 4;
    858 }
    859 
    860 static void crt_sscanf(void)
    861 {
    862     const char *input = (const char *)ADDR(MEM32(g_esp + 4));
    863     const char *start = input;
    864     const char *format = (const char *)ADDR(MEM32(g_esp + 8));
    865     char *conversion = malloc(strlen(format) + 4);
    866     if (!conversion) { perror("scanf format allocation"); abort(); }
    867     uint32_t argument = g_esp + 12;
    868     int assigned = 0, completed = 0;
    869     while (*format) {
    870         if (isspace((unsigned char)*format)) {
    871             ++format;
    872             while (isspace((unsigned char)*input)) ++input;
    873             continue;
    874         }
    875         if (*format != '%' || format[1] == '%') {
    876             if (*format == '%') ++format;
    877             if (*input != *format) {
    878                 if (!*input && !completed) assigned = -1;
    879                 break;
    880             }
    881             ++input;
    882             ++format;
    883             continue;
    884         }
    885 
    886         /* Parse one conversion; translate Windows long/pointer widths explicitly. */
    887         ++format;
    888         int suppressed = *format == '*';
    889         if (suppressed) ++format;
    890         char *output = conversion;
    891         *output++ = '%';
    892         if (suppressed) *output++ = '*';
    893         uint32_t width = 0;
    894         while (isdigit((unsigned char)*format)) {
    895             if (width > (INT32_MAX - (*format - '0')) / 10) goto unsupported;
    896             width = width * 10 + (*format - '0');
    897             *output++ = *format++;
    898         }
    899         int length = 0;
    900         if (*format == 'h') {
    901             ++format;
    902             length = 2;
    903             if (*format == 'h') { ++format; length = 1; }
    904         } else if (*format == 'l') {
    905             ++format;
    906             length = 4;
    907             if (*format == 'l') { ++format; length = 8; }
    908         } else if (*format == 'L') {
    909             ++format;
    910             length = 8;
    911         } else if (!strncmp(format, "I64", 3)) {
    912             format += 3;
    913             length = 8;
    914         } else if (!strncmp(format, "I32", 3)) {
    915             format += 3;
    916             length = 4;
    917         }
    918         char type = *format;
    919         if (!type) goto unsupported;
    920         ++format;
    921         uint32_t destination = 0;
    922         if (!suppressed) { destination = MEM32(argument); argument += 4; }
    923         if (type == 'n') {
    924             uint32_t consumed = (uint32_t)(input - start);
    925             if (!suppressed) {
    926                 if (length == 1) MEM8(destination) = consumed;
    927                 else if (length == 2) MEM16(destination) = consumed;
    928                 else if (length == 8) MEM64(destination) = consumed;
    929                 else MEM32(destination) = consumed;
    930             }
    931             ++completed;
    932             continue;
    933         }
    934         if (type == 'c' && !length) {
    935             /* MSVCRT accepts a short character field at EOF; glibc can reject it. */
    936             uint32_t consumed = 0;
    937             while (consumed < (width ? width : 1) && input[consumed]) ++consumed;
    938             if (!consumed) {
    939                 if (!completed) assigned = -1;
    940                 break;
    941             }
    942             if (!suppressed) { memcpy((void *)ADDR(destination), input, consumed); ++assigned; }
    943             input += consumed;
    944             ++completed;
    945             continue;
    946         }
    947         if (strchr("diouxX", type)) {
    948             if (length == 1) { *output++ = 'h'; *output++ = 'h'; }
    949             if (length == 2) *output++ = 'h';
    950             if (length == 8) { *output++ = 'l'; *output++ = 'l'; }
    951         } else if (strchr("feEgG", type)) {
    952             if (length && length != 4 && length != 8) goto unsupported;
    953             if (length) *output++ = 'l'; /* Windows long double is also 64-bit. */
    954         } else if (type == 'p') {
    955             if (length) goto unsupported;
    956             type = 'x'; /* Guest pointers are 32-bit hexadecimal integers. */
    957         } else if (strchr("sc[", type)) {
    958             if (length) goto unsupported; /* Wide strings need UTF-16 conversion. */
    959         } else goto unsupported;
    960         *output++ = type;
    961         if (type == '[') {
    962             if (*format == '^') *output++ = *format++;
    963             if (*format == ']') *output++ = *format++;
    964             while (*format && *format != ']') *output++ = *format++;
    965             if (!*format) goto unsupported;
    966             *output++ = *format++;
    967         }
    968         strcpy(output, "%n");
    969 
    970         /* Native scanf only receives translated pointers with compatible sizes. */
    971         int consumed = -1;
    972         int result = suppressed ? sscanf(input, conversion, &consumed) :
    973                      sscanf(input, conversion, (void *)ADDR(destination), &consumed);
    974         if (consumed < 0) {
    975             if (result == EOF && !completed) assigned = -1;
    976             break;
    977         }
    978         input += consumed;
    979         ++completed;
    980         if (!suppressed) ++assigned;
    981     }
    982     free(conversion);
    983     g_eax = assigned;
    984     g_esp += 4;
    985     return;
    986 
    987 unsupported:
    988     fprintf(stderr, "Unsupported guest sscanf format: %s\n", (char *)ADDR(MEM32(g_esp + 8)));
    989     abort();
    990 }
    991 
    992 static void crt_atoi(void)
    993 {
    994     const unsigned char *input = (const unsigned char *)ADDR(MEM32(g_esp + 4));
    995     MEM32(guest_crt_errno_address()) = 0;
    996     while (*input == ' ' || (*input >= '\t' && *input <= '\r')) ++input;
    997     int negative = *input == '-';
    998     if (*input == '+' || *input == '-') ++input;
    999     uint32_t value = 0;
   1000     /* Legacy MSVCRT wraps at 32 bits, including arbitrarily long inputs. */
   1001     while (*input >= '0' && *input <= '9') value = value * 10u + (*input++ - '0');
   1002     g_eax = negative ? 0u - value : value;
   1003     g_esp += 4;
   1004 }
   1005 
   1006 static void crt_setlocale(void)
   1007 {
   1008     int32_t category = MEM32(g_esp + 4);
   1009     uint32_t locale = MEM32(g_esp + 8);
   1010     g_esp += 4;
   1011     g_eax = 0;
   1012     if (category < 0 || category > 5) {
   1013         MEM32(guest_crt_errno_address()) = 22;
   1014         return;
   1015     }
   1016     if (locale && strcmp((char *)ADDR(locale), "C")) {
   1017         fprintf(stderr, "Unsupported guest CRT locale: %s\n", (char *)ADDR(locale));
   1018         abort();
   1019     }
   1020     /* Every supported category retains the initial C locale in guest storage. */
   1021     guest_crt_data("__lc_codepage");
   1022     g_eax = 0x08500100u;
   1023 }
   1024 
   1025 static void crt_localeconv(void)
   1026 {
   1027     guest_crt_data("__lc_codepage");
   1028     g_eax = 0x08500120u;
   1029     g_esp += 4;
   1030 }
   1031 
   1032 static void crt_getdays(void)
   1033 {
   1034     /* The C locale returns a caller-owned list, starting with Sunday. */
   1035     const char names[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:Wednesday"
   1036                          ":Thu:Thursday:Fri:Friday:Sat:Saturday";
   1037     g_eax = guest_malloc(sizeof(names));
   1038     if (g_eax) memcpy((void *)ADDR(g_eax), names, sizeof(names));
   1039     else MEM32(guest_crt_errno_address()) = 12;
   1040     g_esp += 4;
   1041 }
   1042 
   1043 static void crt_getmonths(void)
   1044 {
   1045     /* The C locale returns a caller-owned list, starting with January. */
   1046     const char names[] = ":Jan:January:Feb:February:Mar:March:Apr:April:May:May:Jun:June"
   1047                          ":Jul:July:Aug:August:Sep:September:Oct:October:Nov:November:Dec:December";
   1048     g_eax = guest_malloc(sizeof(names));
   1049     if (g_eax) memcpy((void *)ADDR(g_eax), names, sizeof(names));
   1050     else MEM32(guest_crt_errno_address()) = 12;
   1051     g_esp += 4;
   1052 }
   1053 
   1054 static void crt_gettnames(void)
   1055 {
   1056     const char *names[] = {
   1057         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
   1058         "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
   1059         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
   1060         "January", "February", "March", "April", "May", "June", "July",
   1061         "August", "September", "October", "November", "December",
   1062         "AM", "PM", "MM/dd/yy", "dddd, MMMM dd, yyyy", "HH:mm:ss",
   1063     };
   1064     /* VC6 time data: 43 guest pointers, LCID, calendar type and reference count. */
   1065     uint32_t size = 184;
   1066     for (unsigned i = 0; i < 43; ++i) size += strlen(names[i]) + 1;
   1067     g_eax = guest_malloc(size);
   1068     g_esp += 4;
   1069     if (!g_eax) {
   1070         MEM32(guest_crt_errno_address()) = 12;
   1071         return;
   1072     }
   1073 
   1074     /* Strings belong to the same allocation so the caller can free the table. */
   1075     uint32_t output = g_eax + 184;
   1076     for (unsigned i = 0; i < 43; ++i) {
   1077         uint32_t length = strlen(names[i]) + 1;
   1078         MEM32(g_eax + i * 4) = output;
   1079         memcpy((void *)ADDR(output), names[i], length);
   1080         output += length;
   1081     }
   1082     MEM32(g_eax + 172) = 1033;
   1083     MEM32(g_eax + 176) = 1;
   1084     MEM32(g_eax + 180) = 1;
   1085 }
   1086 
   1087 static void crt_splitpath(void)
   1088 {
   1089     const char *path = (const char *)ADDR(MEM32(g_esp + 4));
   1090     const char *directory = path + (*path && path[1] == ':' ? 2 : 0);
   1091     const char *filename = directory, *extension = NULL, *end = directory;
   1092     /* Separators discard dots in directory names; only the final dot is an extension. */
   1093     for (; *end; ++end) {
   1094         if (*end == '/' || *end == '\\') { filename = end + 1; extension = NULL; }
   1095         else if (*end == '.') extension = end;
   1096     }
   1097     if (!extension) extension = end;
   1098     const char *parts[] = {path, directory, filename, extension, end};
   1099     for (unsigned i = 0; i < 4; ++i) {
   1100         uint32_t output = MEM32(g_esp + 8 + i * 4);
   1101         if (!output) continue;
   1102         size_t length = parts[i + 1] - parts[i];
   1103         memcpy((void *)ADDR(output), parts[i], length);
   1104         MEM8(output + length) = 0;
   1105     }
   1106     g_esp += 4;
   1107 }
   1108 
   1109 recomp_func_t guest_crt_lookup(const char *name)
   1110 {
   1111     static const struct { const char *name; recomp_func_t function; } functions[] = {
   1112         {"_errno", crt_errno},
   1113         {"?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z", crt_set_new_handler},
   1114         {"?_set_new_mode@@YAHH@Z", crt_set_new_mode},
   1115         {"_set_error_mode", crt_set_error_mode},
   1116         {"signal", crt_signal}, {"raise", crt_raise}, {"abort", crt_abort},
   1117         {"__set_app_type", crt_set_app_type}, {"__p__fmode", crt_p_fmode},
   1118         {"__p__commode", crt_p_commode}, {"__setusermatherr", crt_setusermatherr},
   1119         {"__getmainargs", crt_getmainargs},
   1120         {"getenv", crt_getenv},
   1121         {"_controlfp", crt_controlfp},
   1122         {"malloc", crt_malloc}, {"free", crt_free}, {"realloc", crt_realloc},
   1123         {"srand", crt_srand}, {"rand", crt_rand},
   1124         {"qsort", crt_qsort},
   1125         {"calloc", crt_calloc}, {"strncpy", crt_strncpy}, {"strchr", crt_strchr},
   1126         {"strcpy", crt_strcpy}, {"strcspn", crt_strcspn},
   1127         {"strstr", crt_strstr},
   1128         {"time", crt_time},
   1129         {"gmtime", crt_gmtime}, {"asctime", crt_asctime}, {"_strdate", crt_strdate},
   1130         {"memcpy", crt_memcpy}, {"memmove", crt_memmove}, {"memset", crt_memset},
   1131         {"_initterm", crt_initterm},
   1132         {"__dllonexit", crt_dllonexit},
   1133         {"_onexit", crt_onexit}, {"_cexit", crt_cexit},
   1134         {"??2@YAPAXI@Z", crt_new}, {"??3@YAXPAX@Z", crt_free},
   1135         {"_ftol", crt_ftol},
   1136         {"floor", crt_floor},
   1137         {"ceil", crt_ceil},
   1138         {"_finite", crt_finite},
   1139         {"_CIfmod", crt_cifmod},
   1140         {"_CIacos", crt_ciacos},
   1141         {"_CIpow", crt_cipow},
   1142         {"atof", crt_atof},
   1143         {"isspace", crt_isspace},
   1144         {"_itoa", crt_itoa},
   1145         {"_EH_prolog", crt_eh_prolog},
   1146         {"strlen", crt_strlen}, {"strcmp", crt_strcmp},
   1147         {"strncmp", crt_strncmp},
   1148         {"_stricmp", crt_stricmp},
   1149         {"_strnicmp", crt_strnicmp},
   1150         {"tolower", crt_tolower}, {"toupper", crt_toupper},
   1151         {"isupper", crt_isupper}, {"islower", crt_islower},
   1152         {"strrchr", crt_strrchr}, {"_mbsrchr", crt_strrchr}, /* Initial single-byte locale. */
   1153         {"memcmp", crt_memcmp}, {"memchr", crt_memchr},
   1154         {"sscanf", crt_sscanf},
   1155         {"atoi", crt_atoi}, {"atol", crt_atoi}, /* Both return 32 bits on Windows. */
   1156         {"setlocale", crt_setlocale},
   1157         {"localeconv", crt_localeconv},
   1158         {"_Getdays", crt_getdays}, {"_Getmonths", crt_getmonths},
   1159         {"_Gettnames", crt_gettnames},
   1160         {"_splitpath", crt_splitpath},
   1161     };
   1162     for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
   1163         if (!strcmp(name, functions[i].name)) return functions[i].function;
   1164     return NULL;
   1165 }