commit 244c008181729edd076f4f258b0dca4299c82b88
parent 058a91395876681d1578b12dc6e9076b2ea71b44
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:18:53 +0000
Implement guest line reading and Windows text input on Linux
Diffstat:
2 files changed, 171 insertions(+), 13 deletions(-)
diff --git a/decompiled/runtime/guest_stdio.c b/decompiled/runtime/guest_stdio.c
@@ -12,7 +12,7 @@
#include <unistd.h>
/* FILE pointers and buffers are guest addresses. Native descriptors stay here. */
-struct stream { uint32_t file; int descriptor, text; };
+struct stream { uint32_t file; int descriptor, text, text_eof, lookahead; };
static struct stream streams[512];
static unsigned stream_count;
@@ -33,6 +33,7 @@ static void initialize_streams(void)
streams[i].file = iob + i * 32;
streams[i].descriptor = i < 3 ? fcntl(i, F_DUPFD_CLOEXEC, 3) : -1;
streams[i].text = 1;
+ streams[i].lookahead = -1;
if (i < 3 && streams[i].descriptor < 0) { perror("standard stream duplication"); abort(); }
}
stream_count = 20;
@@ -161,6 +162,8 @@ static uint32_t open_stream(struct stream *stream, const char *name, const char
}
stream->descriptor = descriptor;
stream->text = text;
+ stream->text_eof = 0;
+ stream->lookahead = -1;
MEM32(stream->file) = MEM32(stream->file + 4) = MEM32(stream->file + 8) = 0;
MEM32(stream->file + 12) = flags;
MEM32(stream->file + 16) = number;
@@ -318,6 +321,7 @@ static int64_t stream_position(struct stream *stream)
if (stream->descriptor < 0) return file_error(EBADF);
off_t position = lseek(stream->descriptor, 0, SEEK_CUR);
if (position < 0) return file_error(errno);
+ if (stream->lookahead >= 0) --position;
uint32_t file = stream->file, flags = MEM32(file + 12);
if (flags & 1) {
if (stream->text && MEM32(file + 4)) {
@@ -360,6 +364,10 @@ static void crt_fseek_locked(void)
else if ((MEM32(file + 12) & 9) == 9 && !(MEM32(file + 12) & 0x400))
MEM32(file + 24) = 512;
g_eax = lseek(stream->descriptor, offset, origin) < 0 ? file_error(errno) : 0;
+ if (!g_eax) {
+ stream->text_eof = 0;
+ stream->lookahead = -1;
+ }
}
g_esp += 4;
}
@@ -373,10 +381,6 @@ static int fill_input(struct stream *stream)
return file_error(EBADF);
}
if (flags & 0x10) return -1;
- if (stream->text) {
- fprintf(stderr, "Guest text input translation is not implemented\n");
- abort();
- }
MEM32(file + 12) |= 1;
if (!MEM32(file + 8)) {
uint32_t buffer = guest_malloc(4096);
@@ -387,8 +391,45 @@ static int fill_input(struct stream *stream)
}
MEM32(file) = MEM32(file + 8);
ssize_t count;
- do count = read(stream->descriptor, (void *)ADDR(MEM32(file)), flags & 4 ? 1 : MEM32(file + 24));
- while (count < 0 && errno == EINTR);
+ unsigned char *buffer = (void *)ADDR(MEM32(file));
+ if (stream->text_eof) count = 0;
+ else if (stream->lookahead >= 0) {
+ buffer[0] = stream->lookahead;
+ stream->lookahead = -1;
+ count = 1;
+ } else {
+ do count = read(stream->descriptor, buffer, flags & 4 ? 1 : MEM32(file + 24));
+ while (count < 0 && errno == EINTR);
+ }
+
+ /* Text buffers expose translated bytes even to inline guest getc code. */
+ if (stream->text && count > 0) {
+ ssize_t translated = 0;
+ for (ssize_t i = 0; i < count; ++i) {
+ unsigned char character = buffer[i];
+ if (character == 0x1a) { stream->text_eof = 1; break; }
+ if (character == '\r') {
+ if (i + 1 < count) {
+ if (buffer[i + 1] == '\n') { character = '\n'; ++i; }
+ } else {
+ unsigned char next;
+ ssize_t got;
+ do got = read(stream->descriptor, &next, 1);
+ while (got < 0 && errno == EINTR);
+ if (got < 0) {
+ MEM32(file + 12) |= 0x20;
+ return file_error(errno);
+ }
+ if (got) {
+ if (next == '\n') character = '\n';
+ else stream->lookahead = next;
+ }
+ }
+ }
+ buffer[translated++] = character;
+ }
+ count = translated;
+ }
if (count <= 0) {
MEM32(file + 12) |= count < 0 ? 0x20 : 0x10;
return count < 0 ? file_error(errno) : -1;
@@ -405,15 +446,43 @@ static void crt_filbuf_locked(void)
g_esp += 4;
}
-static void crt_fgetc_locked(void)
+static int get_character(struct stream *stream)
{
- uint32_t file = MEM32(g_esp + 4);
- struct stream *stream = find_stream(file);
+ uint32_t file = stream->file;
if ((MEM32(file + 12) & 1) && (int32_t)MEM32(file + 4) > 0) {
--MEM32(file + 4);
- g_eax = MEM8(MEM32(file));
+ int character = MEM8(MEM32(file));
++MEM32(file);
- } else g_eax = fill_input(stream);
+ return character;
+ }
+ return fill_input(stream);
+}
+
+static void crt_fgetc_locked(void)
+{
+ g_eax = get_character(find_stream(MEM32(g_esp + 4)));
+ g_esp += 4;
+}
+
+static void crt_fgets_locked(void)
+{
+ uint32_t output = MEM32(g_esp + 4);
+ int32_t size = MEM32(g_esp + 8);
+ g_eax = 0;
+ if (size > 0) {
+ struct stream *stream = find_stream(MEM32(g_esp + 12));
+ int32_t copied = 0;
+ while (copied < size - 1) {
+ int character = get_character(stream);
+ if (character < 0) break;
+ MEM8(output + copied++) = character;
+ if (character == '\n') break;
+ }
+ if (copied || size == 1) {
+ MEM8(output + copied) = 0;
+ g_eax = output;
+ }
+ }
g_esp += 4;
}
@@ -566,6 +635,7 @@ static void crt_ftell(void) { call_serialized(crt_ftell_locked); }
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_filbuf(void) { call_serialized(crt_filbuf_locked); }
recomp_func_t guest_stdio_lookup(const char *name)
@@ -579,6 +649,7 @@ recomp_func_t guest_stdio_lookup(const char *name)
{"_chmod", crt_chmod}, {"_stat", crt_stat},
{"ftell", crt_ftell}, {"fseek", crt_fseek}, {"fread", crt_fread},
{"fgetc", crt_fgetc}, {"getc", crt_fgetc}, {"_filbuf", crt_filbuf},
+ {"fgets", crt_fgets},
};
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
@@ -222,6 +222,90 @@ static void input_buffers_and_invalid_operations(void)
assert(call("fclose", 1, &file) == 0);
}
+static void line_input_translation_and_boundaries(void)
+{
+ const char input[] = "ab\r\nc\rd\n\0x\r\nlast\r\x1aignored";
+ FILE *native = fopen("lines.bin", "wb");
+ assert(native && fwrite(input, 1, sizeof(input) - 1, native) == sizeof(input) - 1 && !fclose(native));
+ const char *expected[] = {"ab\n", "c\rd\n", "\0x\n", "last\r"};
+ unsigned lengths[] = {3, 4, 3, 5};
+
+ for (unsigned buffering = 0; buffering < 4; ++buffering) {
+ uint32_t file = open_file("lines.bin", "rt");
+ uint32_t set[] = {file, 0, buffering == 3 ? 4 : 0, buffering == 1 ? 2 : 4};
+ if (buffering) assert(!call("setvbuf", 4, set));
+ unsigned char storage[32];
+ memset(storage, 0xa5, sizeof(storage));
+ uint32_t bytes = address(storage, sizeof(storage));
+ uint32_t line[] = {bytes + 1, 30, file};
+ for (unsigned i = 0; i < 4; ++i) {
+ memset(memory(bytes), 0xa5, sizeof(storage));
+ uint32_t result = call("fgets", 3, line);
+
+ assert(result == bytes + 1);
+ assert(!memcmp(memory(bytes + 1), expected[i], lengths[i] + 1));
+ assert(*(unsigned char *)memory(bytes) == 0xa5);
+ assert(*(unsigned char *)memory(bytes + lengths[i] + 2) == 0xa5);
+ }
+ memset(memory(bytes), 0xa5, sizeof(storage));
+ assert(!call("fgets", 3, line));
+ assert(*(unsigned char *)memory(bytes + 1) == 0xa5);
+ assert(((uint32_t *)memory(file))[3] & 0x10);
+ uint32_t seek[] = {file, 0, 0};
+ assert(!call("fseek", 3, seek) && call("fgetc", 1, &file) == 'a');
+ assert(!call("fclose", 1, &file));
+ }
+ assert(!remove("lines.bin"));
+}
+
+static void line_input_capacity_and_binary_bytes(void)
+{
+ const char input[] = "ab\r\n\x1a\0z\r";
+ FILE *native = fopen("lines.bin", "wb");
+ assert(native && fwrite(input, 1, sizeof(input) - 1, native) == sizeof(input) - 1 && !fclose(native));
+ uint32_t file = open_file("lines.bin", "rb");
+ unsigned char storage[16]; memset(storage, 0xa5, sizeof(storage));
+ uint32_t bytes = address(storage, sizeof(storage));
+ uint32_t line[] = {bytes + 1, 0, file};
+
+ assert(!call("fgets", 3, line) && *(unsigned char *)memory(bytes + 1) == 0xa5);
+ line[1] = UINT32_MAX;
+ assert(!call("fgets", 3, line) && *(unsigned char *)memory(bytes + 1) == 0xa5);
+ line[1] = 1;
+ assert(call("fgets", 3, line) == bytes + 1 && !*(unsigned char *)memory(bytes + 1));
+ assert(!call("ftell", 1, &file));
+ line[1] = 3;
+ assert(call("fgets", 3, line) == bytes + 1 && !memcmp(memory(bytes + 1), "ab", 3));
+ line[1] = 14;
+ assert(call("fgets", 3, line) == bytes + 1 && !memcmp(memory(bytes + 1), "\r\n", 3));
+ uint32_t result = call("fgets", 3, line);
+
+ assert(result == bytes + 1 && !memcmp(memory(bytes + 1), "\x1a\0z\r", 5));
+ assert(*(unsigned char *)memory(bytes) == 0xa5 && *(unsigned char *)memory(bytes + 6) == 0xa5);
+ assert(!call("fgets", 3, line));
+ assert(!call("fclose", 1, &file) && !remove("lines.bin"));
+}
+
+static void text_reads_cross_buffer_boundaries(void)
+{
+ char input[4099]; memset(input, 'a', 4095);
+ memcpy(input + 4095, "\r\nb\r", 4);
+ FILE *native = fopen("lines.bin", "wb");
+ assert(native && fwrite(input, 1, sizeof(input), native) == sizeof(input) && !fclose(native));
+ uint32_t file = open_file("lines.bin", "rt");
+ unsigned char storage[4100]; memset(storage, 0xa5, sizeof(storage));
+ uint32_t bytes = address(storage, sizeof(storage));
+ uint32_t read[] = {bytes, 1, sizeof(storage), file};
+
+ uint32_t count = call("fread", 4, read);
+
+ assert(count == 4098 && !memcmp(memory(bytes), input, 4095));
+ assert(!memcmp(memory(bytes + 4095), "\nb\r", 3));
+ assert(*(unsigned char *)memory(bytes + 4098) == 0xa5);
+ assert(((uint32_t *)memory(file))[3] & 0x10);
+ assert(!call("fclose", 1, &file) && !remove("lines.bin"));
+}
+
int main(void)
{
#ifdef _WIN32
@@ -245,13 +329,16 @@ int main(void)
text_output_position_includes_crlf();
input_buffers_and_invalid_operations();
binary_reads_cross_buffer_boundaries();
+ line_input_translation_and_boundaries();
+ line_input_capacity_and_binary_bytes();
+ text_reads_cross_buffer_boundaries();
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 and ABI passed");
+ puts("stdio input: positions, binary reads, direct buffers, partial elements, EOF, update streams, text output, fgets/CRLF/control-Z and ABI passed");
return 0;
}