commit 95bac150ca7c9b70090d6bb58e47adef1a792e67
parent f50056b2e8f947f4f217025abefdc093493bf6b9
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:42:08 +0000
Scan file string fields through the guest FILE buffer
Diffstat:
2 files changed, 103 insertions(+), 1 deletion(-)
diff --git a/decompiled/runtime/guest_stdio.c b/decompiled/runtime/guest_stdio.c
@@ -5,6 +5,7 @@
#include "guest_heap.h"
#include "guest_format.h"
#include <errno.h>
+#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <strings.h>
@@ -486,6 +487,68 @@ static void crt_fgets_locked(void)
g_esp += 4;
}
+static void crt_fscanf_locked(void)
+{
+ struct stream *stream = find_stream(MEM32(g_esp + 4));
+ const char *format = (void *)ADDR(MEM32(g_esp + 8));
+ uint32_t argument = g_esp + 12, file = stream->file;
+ int assigned = 0, completed = 0;
+ while (*format) {
+ if (isspace((unsigned char)*format)) {
+ ++format;
+ int character;
+ do character = get_character(stream);
+ while (character >= 0 && isspace(character));
+ if (character >= 0) { --MEM32(file); ++MEM32(file + 4); }
+ continue;
+ }
+ if (*format != '%' || format[1] == '%') {
+ if (*format == '%') ++format;
+ int character = get_character(stream);
+ if (character != (unsigned char)*format++) {
+ if (character >= 0) { --MEM32(file); ++MEM32(file + 4); }
+ else if (!completed) assigned = -1;
+ break;
+ }
+ continue;
+ }
+
+ /* String fields consume translated bytes, leaving the delimiter unread. */
+ ++format;
+ int suppressed = *format == '*';
+ if (suppressed) ++format;
+ uint32_t width = 0;
+ while (isdigit((unsigned char)*format)) {
+ if (width > (INT32_MAX - (*format - '0')) / 10) goto unsupported_scan;
+ width = width * 10 + (*format++ - '0');
+ }
+ if (*format++ != 's') goto unsupported_scan;
+ if (!width) width = INT32_MAX;
+ uint32_t output = 0;
+ if (!suppressed) { output = MEM32(argument); argument += 4; }
+ int character;
+ do character = get_character(stream);
+ while (character >= 0 && isspace(character));
+ if (character < 0) { if (!completed) assigned = -1; break; }
+ uint32_t copied = 0;
+ for (;;) {
+ if (!suppressed) MEM8(output + copied) = character;
+ if (++copied == width) break;
+ character = get_character(stream);
+ if (character < 0) break;
+ if (isspace(character)) { --MEM32(file); ++MEM32(file + 4); break; }
+ }
+ if (!suppressed) { MEM8(output + copied) = 0; ++assigned; }
+ ++completed;
+ }
+ g_eax = assigned;
+ g_esp += 4;
+ return;
+unsupported_scan:
+ fprintf(stderr, "Unsupported guest fscanf format: %s\n", (char *)ADDR(MEM32(g_esp + 8)));
+ abort();
+}
+
static void crt_fread_locked(void)
{
uint32_t output = MEM32(g_esp + 4), size = MEM32(g_esp + 8), count = MEM32(g_esp + 12);
@@ -636,6 +699,7 @@ static void crt_fseek(void) { call_serialized(crt_fseek_locked); }
static void crt_fread(void) { call_serialized(crt_fread_locked); }
static void crt_fgetc(void) { call_serialized(crt_fgetc_locked); }
static void crt_fgets(void) { call_serialized(crt_fgets_locked); }
+static void crt_fscanf(void) { call_serialized(crt_fscanf_locked); }
static void crt_filbuf(void) { call_serialized(crt_filbuf_locked); }
recomp_func_t guest_stdio_lookup(const char *name)
@@ -650,6 +714,7 @@ recomp_func_t guest_stdio_lookup(const char *name)
{"ftell", crt_ftell}, {"fseek", crt_fseek}, {"fread", crt_fread},
{"fgetc", crt_fgetc}, {"getc", crt_fgetc}, {"_filbuf", crt_filbuf},
{"fgets", crt_fgets},
+ {"fscanf", crt_fscanf},
};
for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
if (!strcmp(name, functions[i].name)) return functions[i].function;
diff --git a/decompiled/tests/stdio_input.c b/decompiled/tests/stdio_input.c
@@ -306,6 +306,42 @@ static void text_reads_cross_buffer_boundaries(void)
assert(!call("fclose", 1, &file) && !remove("lines.bin"));
}
+static void scanned_strings_preserve_delimiters(void)
+{
+ const char input[] = " alpha\r\nbeta gamma%\0tail\x1aignored";
+ FILE *native = fopen("scan.txt", "wb");
+ assert(native && fwrite(input, 1, sizeof(input) - 1, native) == sizeof(input) - 1 && !fclose(native));
+ uint32_t file = open_file("scan.txt", "rt");
+ uint32_t set[] = {file, 0, 0, 2};
+ assert(!call("setvbuf", 4, set));
+ unsigned char storage[32]; memset(storage, 0xa5, sizeof(storage));
+ uint32_t bytes = address(storage, sizeof(storage));
+ uint32_t scan[] = {file, string("%3s"), bytes + 1, bytes + 16};
+
+ assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "alp", 4));
+ assert(call("fgetc", 1, &file) == 'h');
+ scan[1] = string("%s%s");
+ assert(call("fscanf", 4, scan) == 2);
+
+ assert(!memcmp(memory(bytes + 1), "a", 2) && !memcmp(memory(bytes + 16), "beta", 5));
+ assert(call("fgetc", 1, &file) == ' ');
+ scan[1] = string("%5s%%");
+ assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "gamma", 6));
+ scan[1] = string("%s");
+ assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "\0tail", 6));
+ assert(call("fscanf", 3, scan) == UINT32_MAX);
+ assert(*(unsigned char *)memory(bytes) == 0xa5 && *(unsigned char *)memory(bytes + 7) == 0xa5);
+ uint32_t seek[] = {file, 0, 0};
+ assert(!call("fseek", 3, seek));
+ scan[1] = string("x%s");
+ assert(!call("fscanf", 3, scan) && call("fgetc", 1, &file) == ' ');
+ scan[1] = string(" %*s %s");
+ assert(call("fscanf", 3, scan) == 1 && !memcmp(memory(bytes + 1), "beta", 5));
+ scan[1] = string("%*s%s");
+ assert(!call("fscanf", 3, scan));
+ assert(!call("fclose", 1, &file) && !remove("scan.txt"));
+}
+
int main(void)
{
#ifdef _WIN32
@@ -332,13 +368,14 @@ int main(void)
line_input_translation_and_boundaries();
line_input_capacity_and_binary_bytes();
text_reads_cross_buffer_boundaries();
+ scanned_strings_preserve_delimiters();
assert(!remove("input.bin") && !remove("update.bin") && !remove("text.txt") && !remove("write-only.bin"));
#ifdef _WIN32
assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
#else
assert(!chdir("/tmp") && !rmdir(directory));
#endif
- puts("stdio input: positions, binary reads, direct buffers, partial elements, EOF, update streams, text output, fgets/CRLF/control-Z and ABI passed");
+ puts("stdio input: positions, binary reads, direct buffers, partial elements, EOF, update streams, text output, fgets/CRLF/control-Z, fscanf strings and ABI passed");
return 0;
}