MCPcopy Create free account
hub / github.com/DFIR-ORC/dfir-orc / MapFile

Function MapFile

src/OrcLib/Utils/MapFile.cpp:34–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32namespace Orc {
33
34Result<std::vector<uint8_t>> MapFile(const std::filesystem::path& path)
35{
36 auto file = CreateFileApi(
37 path.c_str(),
38 GENERIC_READ,
39 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
40 NULL,
41 OPEN_EXISTING,
42 FILE_ATTRIBUTE_NORMAL,
43 NULL);
44 if (!file)
45 {
46 Log::Debug("Failed CreateFile [{}]", file.error());
47 return file.error();
48 }
49
50 auto bytesToRead = GetFileSize(file->value());
51 if (bytesToRead.has_error())
52 {
53 Log::Debug("Failed GetFileSizeEx [{}]", bytesToRead.error());
54 return bytesToRead.error();
55 }
56
57 if (*bytesToRead > UINT32_MAX)
58 {
59 Log::Debug("File is too big");
60 return std::make_error_code(std::errc::no_buffer_space);
61 }
62
63 std::vector<uint8_t> buffer;
64
65 try
66 {
67 buffer.resize(*bytesToRead);
68 }
69 catch (const std::exception& e)
70 {
71 Log::Debug("Failed to resize buffer [{}]", e.what());
72 return std::make_error_code(std::errc::no_buffer_space);
73 }
74
75 DWORD bytesRead = 0;
76 if (!ReadFile(file->value(), buffer.data(), static_cast<DWORD>(buffer.size()), &bytesRead, NULL))
77 {
78 auto ec = LastWin32Error();
79 Log::Debug("Failed ReadFile [{}]", ec);
80 return ec;
81 }
82
83 return buffer;
84}
85
86} // namespace Orc

Callers

nothing calls this directly

Calls 10

CreateFileApiFunction · 0.85
c_strMethod · 0.80
whatMethod · 0.80
GetFileSizeFunction · 0.70
LastWin32ErrorFunction · 0.70
DebugFunction · 0.50
has_errorMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected