Windows: ensure we use binary IO
There's no point in behaving differently on Linux compared to Windows,
so make sure we disable any behind-the-scenes IO character translation.
#include <assert.h>
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
#include <sys/mman.h>
+#else
+#include <io.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
@@ -419,6 +422,13 @@ ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr)
assert(addr);
assert(fd >= 0);
+#if _WIN32
+ if (_setmode(fd, _O_BINARY) == -1) {
+ LOGERR(ctx, LY_ESYS, "Failed to switch the file descriptor to binary mode.", strerror(errno));
+ return LY_ESYS;
+ }
+#endif
+
if (fstat(fd, &sb) == -1) {
LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno));
return LY_ESYS;
