-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathpattern_enum.hpp
More file actions
107 lines (80 loc) · 3.47 KB
/
pattern_enum.hpp
File metadata and controls
107 lines (80 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once
#include <pl/patterns/pattern.hpp>
namespace pl::ptrn {
class PatternEnum : public Pattern {
public:
struct EnumValue {
core::Token::Literal min, max;
[[nodiscard]] bool operator==(const EnumValue &other) const = default;
[[nodiscard]] bool operator!=(const EnumValue &other) const = default;
};
public:
PatternEnum(core::Evaluator *evaluator, u64 offset, size_t size, u32 line)
: Pattern(evaluator, offset, size, line) { }
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::shared_ptr<Pattern>(new PatternEnum(*this));
}
[[nodiscard]] core::Token::Literal getValue() const override {
u128 value = 0;
this->getEvaluator()->readData(this->getOffset(), &value, this->getSize(), this->getSection());
return transformValue(hlp::changeEndianess(value, this->getSize(), this->getEndian()));
}
[[nodiscard]] std::string getFormattedName() const override {
return "enum " + Pattern::getTypeName();
}
[[nodiscard]] std::string getTypeName() const override {
return Pattern::getTypeName();
}
void setEnumValues(const std::map<std::string, EnumValue> &enumValues) {
this->m_enumValues = enumValues;
}
const auto& getEnumValues() const {
return this->m_enumValues;
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherEnum = *static_cast<const PatternEnum *>(&other);
if (this->m_enumValues.size() != otherEnum.m_enumValues.size())
return false;
if (this->m_enumValues != otherEnum.m_enumValues)
return false;
return true;
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
std::string formatDisplayValue() override {
return fmt::format("{}", this->toString());
}
static std::string getEnumName(const std::string &typeName, u128 value, const std::map<std::string, EnumValue> &enumValues) {
std::string result = typeName + "::";
bool foundValue = false;
for (const auto &[name, values] : enumValues) {
const auto& [min, max] = values;
if (value >= min.toUnsigned() && value <= max.toUnsigned()) {
result += name;
foundValue = true;
break;
}
}
if (!foundValue)
result += "???";
return result;
}
[[nodiscard]] std::string toString() override {
u128 value = this->getValue().toUnsigned();
return Pattern::callUserFormatFunc(this->reference(), true).value_or(getEnumName(this->getTypeName(), value, m_enumValues));
}
std::vector<u8> getRawBytes() override {
std::vector<u8> result;
result.resize(this->getSize());
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
if (this->getEndian() != std::endian::native)
std::reverse(result.begin(), result.end());
return result;
}
private:
std::map<std::string, EnumValue> m_enumValues;
};
}