Skip to content

Commit 30826b3

Browse files
committed
OrcLib: Utils: add MapFile
1 parent 039e321 commit 30826b3

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/OrcLib/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,8 @@ set(SRC_UTILITIES
831831
"Utils/Guard/Winsock.cpp"
832832
"Utils/Locker.h"
833833
"Utils/MakeArray.h"
834+
"Utils/MapFile.h"
835+
"Utils/MapFile.cpp"
834836
"Utils/MetaPtr.h"
835837
"Utils/Result.h"
836838
"Utils/Round.h"

src/OrcLib/Utils/MapFile.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
//
4+
// Copyright © 2024 ANSSI. All Rights Reserved.
5+
//
6+
// Author(s): fabienfl (ANSSI)
7+
//
8+
9+
#include "Utils/MapFile.h"
10+
11+
#include "Utils/Result.h"
12+
#include "Utils/Guard.h"
13+
14+
using namespace Orc;
15+
16+
namespace {
17+
18+
Result<uint64_t> GetFileSize(HANDLE hFile)
19+
{
20+
LARGE_INTEGER fileSize;
21+
if (!GetFileSizeEx(hFile, &fileSize))
22+
{
23+
return LastWin32Error();
24+
}
25+
26+
return fileSize.QuadPart;
27+
}
28+
29+
} // namespace
30+
31+
namespace Orc {
32+
33+
Result<std::vector<uint8_t>> MapFile(const std::filesystem::path& path)
34+
{
35+
Guard::FileHandle file = CreateFileW(
36+
path.c_str(),
37+
GENERIC_READ,
38+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
39+
NULL,
40+
OPEN_EXISTING,
41+
FILE_ATTRIBUTE_NORMAL,
42+
NULL);
43+
if (!file.IsValid())
44+
{
45+
auto ec = LastWin32Error();
46+
Log::Debug("Failed CreateFile [{}]", ec);
47+
return ec;
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

src/OrcLib/Utils/MapFile.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
//
4+
// Copyright © 2024 ANSSI. All Rights Reserved.
5+
//
6+
// Author(s): fabienfl (ANSSI)
7+
//
8+
9+
#pragma once
10+
11+
#include <filesystem>
12+
#include <vector>
13+
14+
#include "Utils/Result.h"
15+
16+
namespace Orc {
17+
18+
Result<std::vector<uint8_t>> MapFile(const std::filesystem::path& path);
19+
20+
} // namespace Orc

0 commit comments

Comments
 (0)