diff --git a/bin/base64.c b/bin/base64.c index 90a589e..f411369 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. @@ -308,6 +319,15 @@ encode (const struct config *config, struct buffer *buf) size_t nread, nout; struct base64_state state; +#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 + // Initialize the encoder's state structure. base64_stream_encode_init(&state, 0);