Skip to content

Commit

Permalink
base64: windows: disable auto-returns
Browse files Browse the repository at this point in the history
  • Loading branch information
aklomp committed Jan 28, 2024
1 parent e7a0769 commit d8cc0bd
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion 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,11 @@
#include <errno.h>
#include <limits.h>

// Include the Windows API.
#ifdef WIN
# include <windows.h>
#endif

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

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

0 comments on commit d8cc0bd

Please sign in to comment.