Skip to content

Commit

Permalink
base64: windows: ensure stdout is binary-clean
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aklomp committed Jan 29, 2024
1 parent e7a0769 commit 5a0c585
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bin/base64.c
Expand Up @@ -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
Expand All @@ -24,6 +29,12 @@
#include <errno.h>
#include <limits.h>

// Include Windows-specific headers.
#ifdef WIN
# include <io.h>
# include <fcntl.h>
#endif

#include "../include/libbase64.h"

// Size of the buffer for the "raw" (not base64-encoded) data in bytes.
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5a0c585

Please sign in to comment.