MCPcopy Create free account
hub / github.com/NetSPI/BOF-PE / LoadFile

Function LoadFile

loader/main.cpp:52–73  ·  view source on GitHub ↗

https://stackoverflow.com/questions/51352863/what-is-the-idiomatic-c17-standard-approach-to-reading-binary-files

Source from the content-addressed store, hash-verified

50
51//https://stackoverflow.com/questions/51352863/what-is-the-idiomatic-c17-standard-approach-to-reading-binary-files
52std::vector<std::byte> LoadFile(std::string const& filepath)
53{
54 std::ifstream ifs(filepath, std::ios::binary|std::ios::ate);
55
56 if(!ifs)
57 throw std::runtime_error(filepath + ": " + std::strerror(errno));
58
59 auto end = ifs.tellg();
60 ifs.seekg(0, std::ios::beg);
61
62 auto size = std::size_t(end - ifs.tellg());
63
64 if(size == 0) // avoid undefined behavior
65 return {};
66
67 std::vector<std::byte> buffer(size);
68
69 if(!ifs.read((char*)buffer.data(), buffer.size()))
70 throw std::runtime_error(filepath + ": " + std::strerror(errno));
71
72 return buffer;
73}
74
75
76int GetPackedArguments(int argc, const char* argv[], const char* bof_args_def, std::string& result);

Callers 1

mainFunction · 0.85

Calls 3

sizeMethod · 0.80
readMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected