aorecomp

Anarchy Online classic in the browser

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


client/tests/stdio_input.c (21044B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #ifdef _WIN32
      7 #include <windows.h>
      8 static HMODULE crt;
      9 #else
     10 #include "guest_stdio.h"
     11 #include "guest_crt.h"
     12 #include "guest_memory.h"
     13 #include <unistd.h>
     14 recomp_func_t recomp_lookup(uint32_t address) { fprintf(stderr, "Unexpected stdio guest callback %08x\n", address); abort(); }
     15 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     16 #endif
     17 
     18 static void *memory(uint32_t address)
     19 {
     20 #ifdef _WIN32
     21     return (void *)(uintptr_t)address;
     22 #else
     23     return (void *)ADDR(address);
     24 #endif
     25 }
     26 
     27 static uint32_t address(const void *bytes, unsigned size)
     28 {
     29 #ifdef _WIN32
     30     return (uintptr_t)bytes;
     31 #else
     32     static uint32_t next = 0x50004000u;
     33     uint32_t result = next;
     34     memcpy(memory(result), bytes, size);
     35     next += (size + 3) & ~3u;
     36     assert(next < 0x5000e000u);
     37     return result;
     38 #endif
     39 }
     40 
     41 static uint32_t string(const char *text) { return address(text, strlen(text) + 1); }
     42 
     43 static uint32_t call(const char *name, unsigned count, const uint32_t *arguments)
     44 {
     45 #ifdef _WIN32
     46     uint32_t args[4] = {0};
     47     if (count) memcpy(args, arguments, count * 4);
     48     uint32_t (__cdecl *function)(uint32_t, uint32_t, uint32_t, uint32_t) = (void *)GetProcAddress(crt, name);
     49     assert(function);
     50     return function(args[0], args[1], args[2], args[3]);
     51 #else
     52     g_esp = 0x5000ff00u;
     53     if (count) memcpy(memory(g_esp + 4), arguments, count * 4);
     54     g_ebx = 0x12345678;
     55     g_esi = 0x23456789;
     56     g_edi = 0x3456789a;
     57     g_ebp = 0x456789ab;
     58     recomp_func_t function = guest_stdio_lookup(name);
     59     if (!function) function = guest_crt_lookup(name);
     60     assert(function);
     61     function();
     62     assert(g_esp == 0x5000ff04u);
     63     assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
     64     assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
     65     return g_eax;
     66 #endif
     67 }
     68 
     69 static uint32_t open_file(const char *name, const char *mode)
     70 {
     71     uint32_t args[] = {string(name), string(mode)};
     72     return call("fopen", 2, args);
     73 }
     74 
     75 static void check_contents(const char *path, const void *expected, unsigned size)
     76 {
     77     unsigned char bytes[256];
     78     FILE *file = fopen(path, "rb");
     79     assert(file);
     80     size_t count = fread(bytes, 1, sizeof(bytes), file);
     81     assert(!fclose(file));
     82     if (count != size || memcmp(bytes, expected, size)) {
     83         printf("File %s: %u bytes, expected %u\n", path, (unsigned)count, size);
     84         fflush(stdout);
     85     }
     86     assert(count == size && !memcmp(bytes, expected, size));
     87 }
     88 
     89 static void binary_position_and_direct_buffer_consumption(void)
     90 {
     91     uint32_t file = open_file("input.bin", "rb");
     92     uint32_t seek[] = {file, 0, 2};
     93     uint32_t *fields = memory(file);
     94 
     95     assert(call("ftell", 1, &file) == 0);
     96     assert(call("fseek", 3, seek) == 0);
     97     assert(call("ftell", 1, &file) == 6);
     98     seek[1] = -2; seek[2] = 1;
     99     assert(call("fseek", 3, seek) == 0);
    100     assert(call("fgetc", 1, &file) == 'e');
    101     assert(call("ftell", 1, &file) == 5);
    102     seek[1] = 1; seek[2] = 0;
    103     assert(call("fseek", 3, seek) == 0);
    104     assert(call("fgetc", 1, &file) == 'b');
    105     assert(fields[1] == 4 && *(unsigned char *)memory(fields[0]) == 'c');
    106     ++fields[0];
    107     --fields[1];
    108     uint32_t position = call("ftell", 1, &file);
    109     uint32_t next = call("getc", 1, &file);
    110 
    111     assert(position == 3 && next == 'd');
    112     assert(call("ftell", 1, &file) == 4);
    113     assert(call("fclose", 1, &file) == 0);
    114 }
    115 
    116 static void pushback_before_read_and_seek(void)
    117 {
    118     uint32_t file = open_file("input.bin", "rb");
    119     uint32_t put[] = {'X', file};
    120     uint32_t seek[] = {file, 0, 1};
    121 
    122     assert(call("ungetc", 2, put) == 'X');
    123     assert(call("fgetc", 1, &file) == 'X');
    124     assert(call("fgetc", 1, &file) == 'a');
    125     put[0] = 0x1ff;
    126     uint32_t pushed = call("ungetc", 2, put);
    127     uint32_t position = call("ftell", 1, &file);
    128     assert(call("fgetc", 1, &file) == 255);
    129     assert(call("ungetc", 2, put) == 255);
    130     assert(call("fseek", 3, seek) == 0);
    131 
    132     assert(pushed == 255 && position == 0);
    133     assert(call("fgetc", 1, &file) == 'a');
    134     assert(call("fclose", 1, &file) == 0);
    135     check_contents("input.bin", "abcdef", 6);
    136 }
    137 
    138 static void pushback_clears_eof(void)
    139 {
    140     uint32_t file = open_file("input.bin", "rb");
    141     uint32_t put[] = {'Z', file};
    142     while (call("fgetc", 1, &file) != UINT32_MAX) {}
    143     uint32_t *fields = memory(file);
    144     assert(fields[3] & 0x10);
    145 
    146     uint32_t result = call("ungetc", 2, put);
    147 
    148     assert(result == 'Z' && !(fields[3] & 0x10));
    149     assert(call("ftell", 1, &file) == 5);
    150     assert(call("fgetc", 1, &file) == 'Z');
    151     assert(call("ftell", 1, &file) == 6);
    152     assert(call("fgetc", 1, &file) == UINT32_MAX);
    153     assert(call("fclose", 1, &file) == 0);
    154 }
    155 
    156 static void pushback_failure_preserves_input(void)
    157 {
    158     uint32_t file = open_file("input.bin", "rb");
    159     uint32_t put[] = {'X', file};
    160     assert(call("fgetc", 1, &file) == 'a');
    161     assert(call("ungetc", 2, put) == 'X');
    162 
    163     put[0] = 'Y';
    164     uint32_t full = call("ungetc", 2, put);
    165     put[0] = UINT32_MAX;
    166     uint32_t eof = call("ungetc", 2, put);
    167 
    168     assert(full == UINT32_MAX && eof == UINT32_MAX);
    169     assert(call("fgetc", 1, &file) == 'X');
    170     assert(call("fgetc", 1, &file) == 'b');
    171     assert(call("fclose", 1, &file) == 0);
    172 }
    173 
    174 static void text_pushback_preserves_translation(void)
    175 {
    176     FILE *native = fopen("pushback.txt", "wb");
    177     assert(native && fwrite("a\r\nb", 1, 4, native) == 4 && !fclose(native));
    178     uint32_t file = open_file("pushback.txt", "rt");
    179     uint32_t put[] = {'\n', file};
    180     assert(call("fgetc", 1, &file) == 'a');
    181     assert(call("fgetc", 1, &file) == '\n');
    182 
    183     assert(call("ungetc", 2, put) == '\n');
    184     uint32_t position = call("ftell", 1, &file);
    185 
    186     assert(position == 1 && call("fgetc", 1, &file) == '\n');
    187     assert(call("ftell", 1, &file) == 3);
    188     assert(call("fgetc", 1, &file) == 'b');
    189     assert(call("fclose", 1, &file) == 0 && !remove("pushback.txt"));
    190 }
    191 
    192 static void partial_elements_and_eof_reset(void)
    193 {
    194     uint32_t file = open_file("input.bin", "rb");
    195     unsigned char storage[12]; memset(storage, 0xa5, sizeof(storage));
    196     uint32_t bytes = address(storage, sizeof(storage));
    197     uint32_t read[] = {bytes, 4, 2, file};
    198     uint32_t seek[] = {file, 0, 0};
    199     uint32_t *fields = memory(file);
    200 
    201     uint32_t count = call("fread", 4, read);
    202 
    203     assert(count == 1 && !memcmp(memory(bytes), "abcdef", 6));
    204     assert(((unsigned char *)memory(bytes))[6] == 0xa5 && (fields[3] & 0x10));
    205     assert(call("ftell", 1, &file) == 6 && call("fgetc", 1, &file) == UINT32_MAX);
    206     assert(call("fseek", 3, seek) == 0 && !(fields[3] & 0x10));
    207     assert(call("_filbuf", 1, &file) == 'a');
    208     assert(call("fflush", 1, &file) == 0 && call("ftell", 1, &file) == 6);
    209     assert(call("fgetc", 1, &file) == UINT32_MAX);
    210     assert(call("fclose", 1, &file) == 0);
    211 }
    212 
    213 static void update_stream_changes_direction_after_seek(void)
    214 {
    215     uint32_t file = open_file("update.bin", "w+b");
    216     uint32_t write[] = {string("abcdef"), 1, 6, file};
    217     uint32_t seek[] = {file, 2, 0};
    218     unsigned char storage[8] = {0};
    219     uint32_t bytes = address(storage, sizeof(storage));
    220     uint32_t read[] = {bytes, 1, 6, file};
    221     uint32_t put[] = {'Z', file};
    222 
    223     assert(call("fwrite", 4, write) == 6 && call("ftell", 1, &file) == 6);
    224     assert(call("fseek", 3, seek) == 0 && call("fgetc", 1, &file) == 'c');
    225     seek[1] = -1; seek[2] = 1;
    226     assert(call("fseek", 3, seek) == 0 && call("fputc", 2, put) == 'Z');
    227     seek[1] = 0; seek[2] = 0;
    228     assert(call("fseek", 3, seek) == 0 && call("fread", 4, read) == 6);
    229 
    230     assert(!memcmp(memory(bytes), "abZdef", 6));
    231     assert(call("fclose", 1, &file) == 0);
    232     check_contents("update.bin", "abZdef", 6);
    233 }
    234 
    235 static void text_output_position_includes_crlf(void)
    236 {
    237     uint32_t file = open_file("text.txt", "wt");
    238     uint32_t write[] = {string("a\nb"), 1, 3, file};
    239     uint32_t seek[] = {file, 1, 0};
    240     uint32_t put[] = {'X', file};
    241 
    242     assert(call("fwrite", 4, write) == 3);
    243     uint32_t position = call("ftell", 1, &file);
    244     assert(call("fseek", 3, seek) == 0 && call("fputc", 2, put) == 'X');
    245     assert(call("fclose", 1, &file) == 0);
    246 
    247     assert(position == 4);
    248     check_contents("text.txt", "aX\nb", 4);
    249 }
    250 
    251 static void binary_reads_cross_buffer_boundaries(void)
    252 {
    253     unsigned char expected[9000], storage[9002];
    254     for (unsigned i = 0; i < sizeof(expected); ++i) expected[i] = i % 256;
    255     FILE *native = fopen("large.bin", "wb");
    256     assert(native && fwrite(expected, 1, sizeof(expected), native) == sizeof(expected) && !fclose(native));
    257     memset(storage, 0xa5, sizeof(storage));
    258     uint32_t bytes = address(storage, sizeof(storage));
    259     uint32_t file = open_file("large.bin", "rb");
    260     uint32_t read[] = {bytes, 7, 1286, file};
    261 
    262     uint32_t count = call("fread", 4, read);
    263 
    264     assert(count == 1285 && !memcmp(memory(bytes), expected, sizeof(expected)));
    265     assert(((unsigned char *)memory(bytes))[9000] == 0xa5);
    266     assert(call("ftell", 1, &file) == 9000);
    267     assert(call("fclose", 1, &file) == 0 && !remove("large.bin"));
    268 }
    269 
    270 static void input_buffers_and_invalid_operations(void)
    271 {
    272     uint32_t file = open_file("input.bin", "rb");
    273     unsigned char storage[16] = {0};
    274     uint32_t buffer = address(storage, sizeof(storage));
    275     uint32_t set[] = {file, buffer, 0, 4};
    276     uint32_t seek[] = {file, -1, 0};
    277     uint32_t read[] = {buffer + 8, 0, 2, file};
    278 
    279     assert(call("setvbuf", 4, set) == 0);
    280     for (unsigned i = 0; i < 6; ++i) assert(call("fgetc", 1, &file) == 'a' + i);
    281     assert(call("fseek", 3, seek) == UINT32_MAX);
    282     assert(*(uint32_t *)memory(call("_errno", 0, NULL)) == 22);
    283     seek[1] = 0; seek[2] = 99;
    284     assert(call("fseek", 3, seek) == UINT32_MAX);
    285     assert(call("fread", 4, read) == 0);
    286 
    287     assert(call("ftell", 1, &file) == 6);
    288     assert(call("fclose", 1, &file) == 0);
    289     assert(call("ftell", 1, &file) == UINT32_MAX);
    290     assert(*(uint32_t *)memory(call("_errno", 0, NULL)) == 9);
    291     file = open_file("input.bin", "rb");
    292     set[0] = file; set[1] = 0; set[2] = 4; set[3] = 0;
    293     assert(call("setvbuf", 4, set) == 0 && call("fgetc", 1, &file) == 'a');
    294     assert(call("ftell", 1, &file) == 1 && call("fclose", 1, &file) == 0);
    295     file = open_file("write-only.bin", "wb");
    296     read[1] = read[2] = 1; read[3] = file;
    297     assert(call("fread", 4, read) == 0 && (((uint32_t *)memory(file))[3] & 0x20));
    298     assert(call("fclose", 1, &file) == 0);
    299 }
    300 
    301 static void line_input_translation_and_boundaries(void)
    302 {
    303     const char input[] = "ab\r\nc\rd\n\0x\r\nlast\r\x1aignored";
    304     FILE *native = fopen("lines.bin", "wb");
    305     assert(native && fwrite(input, 1, sizeof(input) - 1, native) == sizeof(input) - 1 && !fclose(native));
    306     const char *expected[] = {"ab\n", "c\rd\n", "\0x\n", "last\r"};
    307     unsigned lengths[] = {3, 4, 3, 5};
    308 
    309     for (unsigned buffering = 0; buffering < 4; ++buffering) {
    310         uint32_t file = open_file("lines.bin", "rt");
    311         uint32_t set[] = {file, 0, buffering == 3 ? 4 : 0, buffering == 1 ? 2 : 4};
    312         if (buffering) assert(!call("setvbuf", 4, set));
    313         unsigned char storage[32];
    314         memset(storage, 0xa5, sizeof(storage));
    315         uint32_t bytes = address(storage, sizeof(storage));
    316         uint32_t line[] = {bytes + 1, 30, file};
    317         for (unsigned i = 0; i < 4; ++i) {
    318             memset(memory(bytes), 0xa5, sizeof(storage));
    319             uint32_t result = call("fgets", 3, line);
    320 
    321             assert(result == bytes + 1);
    322             if (memcmp(memory(bytes + 1), expected[i], lengths[i] + 1)) {
    323                 fprintf(stderr, "Line mismatch: buffering=%u line=%u\n", buffering, i);
    324                 for (unsigned j = 0; j < 16; ++j) fprintf(stderr, "%02x ", *(unsigned char *)memory(bytes + 1 + j));
    325                 fputc('\n', stderr);
    326             }
    327             assert(!memcmp(memory(bytes + 1), expected[i], lengths[i] + 1));
    328             assert(*(unsigned char *)memory(bytes) == 0xa5);
    329             assert(*(unsigned char *)memory(bytes + lengths[i] + 2) == 0xa5);
    330         }
    331         memset(memory(bytes), 0xa5, sizeof(storage));
    332         assert(!call("fgets", 3, line));
    333         assert(*(unsigned char *)memory(bytes + 1) == 0xa5);
    334         assert(((uint32_t *)memory(file))[3] & 0x10);
    335         uint32_t seek[] = {file, 0, 0};
    336         assert(!call("fseek", 3, seek) && call("fgetc", 1, &file) == 'a');
    337         assert(!call("fclose", 1, &file));
    338     }
    339     assert(!remove("lines.bin"));
    340 }
    341 
    342 static void line_input_capacity_and_binary_bytes(void)
    343 {
    344     const char input[] = "ab\r\n\x1a\0z\r";
    345     FILE *native = fopen("lines.bin", "wb");
    346     assert(native && fwrite(input, 1, sizeof(input) - 1, native) == sizeof(input) - 1 && !fclose(native));
    347     uint32_t file = open_file("lines.bin", "rb");
    348     unsigned char storage[16]; memset(storage, 0xa5, sizeof(storage));
    349     uint32_t bytes = address(storage, sizeof(storage));
    350     uint32_t line[] = {bytes + 1, 0, file};
    351 
    352     assert(!call("fgets", 3, line) && *(unsigned char *)memory(bytes + 1) == 0xa5);
    353     line[1] = UINT32_MAX;
    354     assert(!call("fgets", 3, line) && *(unsigned char *)memory(bytes + 1) == 0xa5);
    355     line[1] = 1;
    356     assert(call("fgets", 3, line) == bytes + 1 && !*(unsigned char *)memory(bytes + 1));
    357     assert(!call("ftell", 1, &file));
    358     line[1] = 3;
    359     assert(call("fgets", 3, line) == bytes + 1 && !memcmp(memory(bytes + 1), "ab", 3));
    360     line[1] = 14;
    361     assert(call("fgets", 3, line) == bytes + 1 && !memcmp(memory(bytes + 1), "\r\n", 3));
    362     uint32_t result = call("fgets", 3, line);
    363 
    364     assert(result == bytes + 1 && !memcmp(memory(bytes + 1), "\x1a\0z\r", 5));
    365     assert(*(unsigned char *)memory(bytes) == 0xa5 && *(unsigned char *)memory(bytes + 6) == 0xa5);
    366     assert(!call("fgets", 3, line));
    367     assert(!call("fclose", 1, &file) && !remove("lines.bin"));
    368 }
    369 
    370 static void text_reads_cross_buffer_boundaries(void)
    371 {
    372     char input[4099]; memset(input, 'a', 4095);
    373     memcpy(input + 4095, "\r\nb\r", 4);
    374     FILE *native = fopen("lines.bin", "wb");
    375     assert(native && fwrite(input, 1, sizeof(input), native) == sizeof(input) && !fclose(native));
    376     uint32_t file = open_file("lines.bin", "rt");
    377     unsigned char storage[4100]; memset(storage, 0xa5, sizeof(storage));
    378     uint32_t bytes = address(storage, sizeof(storage));
    379     uint32_t read[] = {bytes, 1, sizeof(storage), file};
    380 
    381     uint32_t count = call("fread", 4, read);
    382 
    383     assert(count == 4098 && !memcmp(memory(bytes), input, 4095));
    384     assert(!memcmp(memory(bytes + 4095), "\nb\r", 3));
    385     assert(*(unsigned char *)memory(bytes + 4098) == 0xa5);
    386     assert(((uint32_t *)memory(file))[3] & 0x10);
    387     assert(!call("fclose", 1, &file) && !remove("lines.bin"));
    388 }
    389 
    390 static void scanned_strings_preserve_delimiters(void)
    391 {
    392     const char input[] = "  alpha\r\nbeta gamma%\0tail\x1aignored";
    393     FILE *native = fopen("scan.txt", "wb");
    394     assert(native && fwrite(input, 1, sizeof(input) - 1, native) == sizeof(input) - 1 && !fclose(native));
    395     uint32_t file = open_file("scan.txt", "rt");
    396     uint32_t set[] = {file, 0, 0, 2};
    397     assert(!call("setvbuf", 4, set));
    398     unsigned char storage[32]; memset(storage, 0xa5, sizeof(storage));
    399     uint32_t bytes = address(storage, sizeof(storage));
    400     uint32_t scan[] = {file, string("%3s"), bytes + 1, bytes + 16};
    401 
    402     assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "alp", 4));
    403     assert(call("fgetc", 1, &file) == 'h');
    404     scan[1] = string("%s%s");
    405     assert(call("fscanf", 4, scan) == 2);
    406 
    407     assert(!memcmp(memory(bytes + 1), "a", 2) && !memcmp(memory(bytes + 16), "beta", 5));
    408     assert(call("fgetc", 1, &file) == ' ');
    409     scan[1] = string("%5s%%");
    410     assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "gamma", 6));
    411     scan[1] = string("%s");
    412     assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "\0tail", 6));
    413     assert(call("fscanf", 3, scan) == UINT32_MAX);
    414     assert(*(unsigned char *)memory(bytes) == 0xa5 && *(unsigned char *)memory(bytes + 7) == 0xa5);
    415     uint32_t seek[] = {file, 0, 0};
    416     assert(!call("fseek", 3, seek));
    417     scan[1] = string("x%s");
    418     assert(!call("fscanf", 3, scan) && call("fgetc", 1, &file) == ' ');
    419     scan[1] = string(" %*s %s");
    420     assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "beta", 5));
    421     scan[1] = string("%*s%s");
    422     assert(!call("fscanf", 3, scan));
    423     assert(!call("fclose", 1, &file) && !remove("scan.txt"));
    424 }
    425 
    426 static void text_positions_match_legacy_buffer_rules(void)
    427 {
    428     const char *input[] = {"a\r\nb\r\nc", "a\nb\nc", "a\rb\r", "abc\x1axyz"};
    429     const int positions[][2][8] = {
    430         {{1, 3, 4, 6, 7, 7}, {2, 3, 4, 6, 7, 7}},
    431         {{-1, 1, 2, 4, 5, 5}, {1, 2, 3, 4, 5, 5}},
    432         {{1, 2, 3, 4, 4}, {1, 2, 3, 4, 4}},
    433         {{4, 5, 7, 7}, {1, 2, 4, 5, 6, 7, 7}}
    434     };
    435     for (unsigned i = 0; i < 4; ++i) for (unsigned buffering = 0; buffering < 3; ++buffering) {
    436         FILE *native = fopen("positions.txt", "wb");
    437         assert(native && fwrite(input[i], 1, strlen(input[i]), native) == strlen(input[i]) && !fclose(native));
    438         uint32_t file = open_file("positions.txt", "rt");
    439         uint32_t set[] = {file, 0, buffering == 2 ? 4 : 0, 2};
    440         if (buffering) assert(!call("setvbuf", 4, set));
    441         uint32_t storage[] = {0xa5a5a5a5, 0, 0, 0xa5a5a5a5};
    442         uint32_t bytes = address(storage, sizeof(storage));
    443         uint32_t getpos[] = {file, bytes + 4};
    444 
    445         for (unsigned j = 0; j < 8; ++j) {
    446             uint32_t character = call("fgetc", 1, &file);
    447             uint32_t result = call("fgetpos", 2, getpos);
    448             int64_t position;
    449             memcpy(&position, memory(bytes + 4), sizeof(position));
    450 
    451             int expected = positions[i][buffering != 0][j];
    452             assert(position == expected && result == (expected == -1 ? UINT32_MAX : 0));
    453             assert((int32_t)call("ftell", 1, &file) == expected);
    454             assert(*(uint32_t *)memory(bytes) == 0xa5a5a5a5 && *(uint32_t *)memory(bytes + 12) == 0xa5a5a5a5);
    455             if (character == UINT32_MAX) break;
    456         }
    457         assert(!call("fclose", 1, &file));
    458     }
    459     assert(!remove("positions.txt"));
    460 }
    461 
    462 static void saved_positions_restore_text_and_support_64_bits(void)
    463 {
    464     FILE *native = fopen("positions.txt", "wb");
    465     assert(native && fwrite("a\r\nb\r\nc", 1, 7, native) == 7 && !fclose(native));
    466     uint32_t file = open_file("positions.txt", "rt");
    467     uint32_t storage[4] = {0};
    468     uint32_t bytes = address(storage, sizeof(storage));
    469     uint32_t position[] = {file, bytes};
    470 
    471     assert(call("fgetc", 1, &file) == 'a' && !call("fgetpos", 2, position));
    472     while (call("fgetc", 1, &file) != UINT32_MAX) {}
    473     assert(!call("fsetpos", 2, position));
    474     uint32_t next = call("fgetc", 1, &file);
    475 
    476     assert(next == '\n' && call("fgetc", 1, &file) == 'b');
    477     assert(!call("fclose", 1, &file));
    478     file = open_file("positions.txt", "rb");
    479     position[0] = file;
    480     uint64_t large = 0x100000003ull;
    481     memcpy(memory(bytes), &large, sizeof(large));
    482     assert(!call("fsetpos", 2, position));
    483     position[1] = bytes + 8;
    484     assert(!call("fgetpos", 2, position) && !memcmp(memory(bytes + 8), &large, sizeof(large)));
    485     assert(!call("fclose", 1, &file) && !remove("positions.txt"));
    486 }
    487 
    488 int main(void)
    489 {
    490 #ifdef _WIN32
    491     crt = LoadLibraryA("msvcrt.dll");
    492     assert(crt);
    493     char temporary[MAX_PATH], directory[MAX_PATH];
    494     assert(GetTempPathA(sizeof(temporary), temporary));
    495     assert(GetTempFileNameA(temporary, "asi", 0, directory));
    496     assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL) && SetCurrentDirectoryA(directory));
    497 #else
    498     assert(guest_map("stdio input test", 0x50000000u, 0x10000u));
    499     g_fs_base = 0x50001000u;
    500     char directory[] = "/tmp/ao-input-test-XXXXXX";
    501     assert(mkdtemp(directory) && !chdir(directory));
    502 #endif
    503     FILE *file = fopen("input.bin", "wb");
    504     assert(file && fwrite("abcdef", 1, 6, file) == 6 && !fclose(file));
    505     binary_position_and_direct_buffer_consumption();
    506     pushback_before_read_and_seek();
    507     pushback_clears_eof();
    508     pushback_failure_preserves_input();
    509     text_pushback_preserves_translation();
    510     partial_elements_and_eof_reset();
    511     update_stream_changes_direction_after_seek();
    512     text_output_position_includes_crlf();
    513     input_buffers_and_invalid_operations();
    514     binary_reads_cross_buffer_boundaries();
    515     line_input_translation_and_boundaries();
    516     line_input_capacity_and_binary_bytes();
    517     text_reads_cross_buffer_boundaries();
    518     scanned_strings_preserve_delimiters();
    519     text_positions_match_legacy_buffer_rules();
    520     saved_positions_restore_text_and_support_64_bits();
    521     assert(!remove("input.bin") && !remove("update.bin") && !remove("text.txt") && !remove("write-only.bin"));
    522 #ifdef _WIN32
    523     assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
    524 #else
    525     assert(!chdir("/tmp") && !rmdir(directory));
    526 #endif
    527     puts("stdio input: 64-bit/text positions, binary reads, direct buffers, partial elements, EOF, update streams, text output, fgets/CRLF/control-Z, fscanf strings and ABI passed");
    528     return 0;
    529 }
    530