1
0
mirror of synced 2024-11-28 09:30:51 +01:00

patterns: Added bitfield_order pragma (#457)

This commit is contained in:
Lukas Cone 2022-02-28 00:07:04 +01:00 committed by GitHub
parent 0cc6d90e3d
commit 2847098020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -30,17 +30,17 @@ namespace hex::pl {
size_t bitOffset = 0;
std::vector<std::shared_ptr<Pattern>> fields;
bool isLeftToRight = false;
BitfieldOrder order = evaluator->getBitfieldOrder();
if (this->hasAttribute("left_to_right", false))
isLeftToRight = true;
order = BitfieldOrder::LeftToRight;
else if (this->hasAttribute("right_to_left", false))
isLeftToRight = false;
order = BitfieldOrder::RightToLeft;
std::vector<std::pair<std::string, ASTNode *>> entries;
for (const auto &[name, entry] : this->m_entries)
entries.push_back({ name, entry.get() });
if (isLeftToRight)
if (order == BitfieldOrder::LeftToRight)
std::reverse(entries.begin(), entries.end());
evaluator->pushScope(pattern.get(), fields);

View File

@ -34,6 +34,12 @@ namespace hex::pl {
Return
};
enum class BitfieldOrder
{
RightToLeft,
LeftToRight
};
class Pattern;
class PatternCreationLimiter;
class ASTNode;
@ -161,6 +167,14 @@ namespace hex::pl {
return this->m_loopLimit;
}
void setBitfieldOrder(BitfieldOrder order) {
this->m_bitfieldOrder = order;
}
[[nodiscard]] BitfieldOrder getBitfieldOrder() {
return this->m_bitfieldOrder;
}
u64 &dataOffset() { return this->m_currOffset; }
bool addCustomFunction(const std::string &name, u32 numParams, const ContentRegistry::PatternLanguage::Callback &function) {
@ -270,6 +284,7 @@ namespace hex::pl {
std::atomic<bool> m_dangerousFunctionCalled = false;
std::atomic<DangerousFunctionPermission> m_allowDangerousFunctions = DangerousFunctionPermission::Ask;
ControlFlowStatement m_currControlFlowStatement;
BitfieldOrder m_bitfieldOrder = BitfieldOrder::RightToLeft;
friend class PatternCreationLimiter;
};

View File

@ -83,6 +83,18 @@ namespace hex::pl {
ImHexApi::Provider::get()->setBaseAddress(baseAddress);
return true;
});
this->m_preprocessor->addPragmaHandler("bitfield_order", [this](const std::string &value) {
if (value == "left_to_right") {
this->m_evaluator->setBitfieldOrder(BitfieldOrder::LeftToRight);
return true;
} else if (value == "right_to_left") {
this->m_evaluator->setBitfieldOrder(BitfieldOrder::RightToLeft);
return true;
} else {
return false;
}
});
}
PatternLanguage::~PatternLanguage() {