From e6bed0aa2eb5b94d19724cc5ee14da6436d892a3 Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Mon, 29 Jan 2024 00:22:55 +0100 Subject: [PATCH] base64: windows: ensure stdout is binary-clean Ensure that stdout is binary-clean and does not silently change newlines to CRLFs when they occur at the end of a write buffer. Resolves #137. --- bin/base64.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/bin/base64.c b/bin/base64.c index 90a589e..77508c8 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,12 @@ #include #include +// Include Windows-specific headers. +#ifdef WIN +# include +# include +#endif + #include "../include/libbase64.h" // Size of the buffer for the "raw" (not base64-encoded) data in bytes. @@ -638,6 +649,15 @@ main (int argc, char **argv) return 1; } +#ifdef WIN + + // On Windows platforms, ensure that stdout is binary-clean, and + // newlines at the end of the line are not silently converted to CRLFs. + // This seems to be the portable way to do it. freopen() and + // SetConsoleMode() occasionally result in permission errors. + _setmode(1, _O_BINARY); +#endif + // Encode or decode the input based on the user's choice. const bool ret = config.decode ? decode(&config, &buf)