upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp16
1 files changed, 12 insertions, 4 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index d6ac0e1a4..d44160505 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -2,6 +2,7 @@
2 2
3#include <windows.h> 3#include <windows.h>
4#include <intrin.h> 4#include <intrin.h>
5#include <share.h>
5#include <tlhelp32.h> 6#include <tlhelp32.h>
6 7
7#include <algorithm> 8#include <algorithm>
@@ -10,7 +11,7 @@
10#include <cstdio> 11#include <cstdio>
11#include <cstdlib> 12#include <cstdlib>
12#include <cstring> 13#include <cstring>
13#include <fstream> 14#include <memory>
14#include <mutex> 15#include <mutex>
15#include <stdexcept> 16#include <stdexcept>
16#include <system_error> 17#include <system_error>
@@ -77,9 +78,16 @@ std::string path_utf8(const std::filesystem::path& path) {
77} 78}
78 79
79std::vector<uint8_t> read_file(const std::filesystem::path& p) { 80std::vector<uint8_t> read_file(const std::filesystem::path& p) {
80 std::ifstream f(p, std::ios::binary); 81 std::unique_ptr<std::FILE, decltype(&std::fclose)> file(
81 if (!f) throw std::runtime_error("cannot open " + path_utf8(p)); 82 _wfsopen(p.c_str(), L"rb", _SH_DENYNO), std::fclose);
82 return {std::istreambuf_iterator<char>(f), {}}; 83 if (!file) throw std::runtime_error("cannot open " + path_utf8(p));
84
85 std::vector<uint8_t> bytes;
86 std::array<uint8_t, 64 * 1024> block;
87 while (const auto count = std::fread(block.data(), 1, block.size(), file.get()))
88 bytes.insert(bytes.end(), block.data(), block.data() + count);
89 if (std::ferror(file.get())) throw std::runtime_error("cannot read " + path_utf8(p));
90 return bytes;
83} 91}
84 92
85template <class T> 93template <class T>