From d8cc0bd2073e1383587c2875ea1d38e611fc13c0 Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Mon, 29 Jan 2024 00:22:55 +0100 Subject: [PATCH] base64: windows: disable auto-returns --- bin/base64.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/bin/base64.c b/bin/base64.c index 90a589e..1e2ea44 100644 --- a/bin/base64.c +++ b/bin/base64.c @@ -3,6 +3,11 @@ # define MINGW #endif +// Test for Windows. +#if defined(_WIN32) || defined(_WIN64) +# define WIN +#endif + // Decide if the writev(2) system call needs to be emulated as a series of // write(2) calls. At least MinGW does not support writev(2). #ifdef MINGW @@ -24,6 +29,11 @@ #include #include +// Include the Windows API. +#ifdef WIN +# include +#endif + #include "../include/libbase64.h" // Size of the buffer for the "raw" (not base64-encoded) data in bytes. @@ -303,7 +313,7 @@ write_wrapped (const struct config *config, char *buf, size_t len) } static bool -encode (const struct config *config, struct buffer *buf) +encode_inner (const struct config *config, struct buffer *buf) { size_t nread, nout; struct base64_state state; @@ -346,6 +356,47 @@ encode (const struct config *config, struct buffer *buf) return true; } +static bool +encode (const struct config *config, struct buffer *buf) +{ +#ifdef WIN + DWORD fdwMode; + + // Get the standard output handle. + HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + + // Check for errors. + if (hStdout == INVALID_HANDLE_VALUE) { + fprintf(stderr, "GetStdHandle: %d\n", GetLastError()); + return false; + } + + // Save the current output mode. + if (!GetConsoleMode(hStdout, &fdwMode)) { + fprintf(stderr, "GetConsoleMode: %d\n", GetLastError()); + return false; + } + + // Disable the mangling of newlines. + if (!SetConsoleMode(hStdout, fdwMode | DISABLE_NEWLINE_AUTO_RETURN)) { + fprintf(stderr, "SetConsoleMode: %d\n", GetLastError()); + return false; + } +#endif + + const bool ret = encode_inner(config, buf); + +#ifdef WIN + + // Restore the original console settings. + if (!SetConsoleMode(hStdout, fdwMode)) { + fprintf(stderr, "SetConsoleMode: %d\n", GetLastError()); + } +#endif + + return ret; +} + static inline size_t find_garbage (const char *p, const size_t avail) {