Skip to content

Commit cdd37d6

Browse files
committed
OrcLib: add ResurrectRecordMode
1 parent 3f061d1 commit cdd37d6

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

src/OrcLib/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ set(SRC_DISK_FILESYSTEM_NTFS_MFT
149149
"MFTUtils.h"
150150
"MFTWalker.cpp"
151151
"MFTWalker.h"
152+
"ResurrectRecordsMode.h"
153+
"ResurrectRecordsMode.cpp"
152154
)
153155

154156
source_group(Disk\\FileSystem\\NTFS\\MFT FILES ${SRC_DISK_FILESYSTEM_NTFS_MFT})

src/OrcLib/ResurrectRecordsMode.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
//
4+
// Copyright © 2023 ANSSI. All Rights Reserved.
5+
//
6+
// Author(s): fabienfl (ANSSI)
7+
//
8+
9+
#include "ResurrectRecordsMode.h"
10+
11+
#include <boost/algorithm/string.hpp>
12+
13+
#include "Log/Log.h"
14+
15+
namespace {
16+
17+
using namespace std::string_view_literals;
18+
19+
constexpr auto kNo = "No"sv;
20+
constexpr auto kYes = "Yes"sv;
21+
constexpr auto kResident = "Resident"sv;
22+
23+
constexpr auto kNoW = L"No"sv;
24+
constexpr auto kYesW = L"Yes"sv;
25+
constexpr auto kResidentW = L"Resident"sv;
26+
27+
using ResurrectRecordsModeEntry = std::tuple<Orc::ResurrectRecordsMode, std::string_view, std::wstring_view>;
28+
29+
constexpr std::array kModes = {
30+
ResurrectRecordsModeEntry {
31+
Orc::ResurrectRecordsMode::kNo,
32+
kNo,
33+
kNoW,
34+
},
35+
ResurrectRecordsModeEntry {
36+
Orc::ResurrectRecordsMode::kYes,
37+
kYes,
38+
kYesW,
39+
},
40+
ResurrectRecordsModeEntry {Orc::ResurrectRecordsMode::kResident, kResident, kResidentW}};
41+
42+
Orc::Result<ResurrectRecordsModeEntry> Get(Orc::ResurrectRecordsMode mode)
43+
{
44+
using namespace Orc;
45+
46+
if (mode >= ResurrectRecordsMode::kCount)
47+
{
48+
Log::Debug(
49+
"Failed 'ResurrectRecordsMode' conversion (value: {})",
50+
static_cast<std::underlying_type_t<ResurrectRecordsMode>>(mode));
51+
52+
return std::errc::no_such_file_or_directory;
53+
}
54+
55+
const auto& entry = kModes[static_cast<size_t>(mode)];
56+
if (std::get<0>(entry) != mode)
57+
{
58+
return std::errc::not_supported;
59+
}
60+
61+
return entry;
62+
}
63+
64+
} // namespace
65+
66+
namespace Orc {
67+
68+
Result<std::string_view> ToString(ResurrectRecordsMode mode)
69+
{
70+
auto rv = Get(mode);
71+
if (!rv)
72+
{
73+
return rv.error();
74+
}
75+
76+
return std::get<1>(rv.value());
77+
}
78+
79+
Result<std::wstring_view> ToWString(ResurrectRecordsMode mode)
80+
{
81+
auto rv = Get(mode);
82+
if (!rv)
83+
{
84+
return rv.error();
85+
}
86+
87+
return std::get<2>(rv.value());
88+
}
89+
90+
Result<ResurrectRecordsMode> ToResurrectRecordsMode(std::string_view mode)
91+
{
92+
auto it = std::find_if(std::cbegin(kModes), std::cend(kModes), [&mode](const auto& item) {
93+
return boost::iequals(std::get<1>(item), mode);
94+
});
95+
96+
if (it != std::cend(kModes))
97+
{
98+
return std::get<0>(*it);
99+
}
100+
101+
return std::errc::no_such_file_or_directory;
102+
}
103+
104+
Result<ResurrectRecordsMode> ToResurrectRecordsMode(std::wstring_view mode)
105+
{
106+
auto it = std::find_if(std::cbegin(kModes), std::cend(kModes), [&mode](const auto& item) {
107+
return boost::iequals(std::get<2>(item), mode);
108+
});
109+
110+
if (it != std::cend(kModes))
111+
{
112+
return std::get<0>(*it);
113+
}
114+
115+
return std::errc::no_such_file_or_directory;
116+
}
117+
} // namespace Orc

src/OrcLib/ResurrectRecordsMode.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
//
4+
// Copyright © 2023 ANSSI. All Rights Reserved.
5+
//
6+
// Author(s): fabienfl (ANSSI)
7+
//
8+
9+
#pragma once
10+
11+
#include <string>
12+
#include "Utils/Result.h"
13+
14+
namespace Orc {
15+
16+
enum class ResurrectRecordsMode : std::uint32_t
17+
{
18+
kNo = 0,
19+
kYes,
20+
kResident,
21+
kCount
22+
};
23+
24+
Result<std::string_view> ToString(ResurrectRecordsMode mode);
25+
Result<std::wstring_view> ToWString(ResurrectRecordsMode mode);
26+
27+
Result<ResurrectRecordsMode> ToResurrectRecordsMode(std::string_view mode);
28+
Result<ResurrectRecordsMode> ToResurrectRecordsMode(std::wstring_view mode);
29+
30+
} // namespace Orc

0 commit comments

Comments
 (0)