1
0
mirror of synced 2024-11-27 17:10:51 +01:00
ImHex/plugins/builtin/source/content/pl_builtin_functions.cpp

76 lines
2.9 KiB
C++
Raw Normal View History

#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
2021-09-25 16:24:08 +02:00
#include <hex/helpers/net.hpp>
2021-08-29 22:15:18 +02:00
#include <pl/core/token.hpp>
#include <pl/core/log_console.hpp>
#include <pl/core/evaluator.hpp>
#include <pl/patterns/pattern.hpp>
2022-09-30 12:49:49 +02:00
#include <llvm/Demangle/Demangle.h>
namespace hex::plugin::builtin {
void registerPatternLanguageFunctions() {
using namespace pl::core;
using FunctionParameterCount = pl::api::FunctionParameterCount;
pl::api::Namespace nsHexCore = { "builtin", "hex", "core" };
{
/* get_selection() */
2022-09-30 12:49:49 +02:00
ContentRegistry::PatternLanguage::addFunction(nsHexCore, "get_selection", FunctionParameterCount::none(), [](Evaluator *, auto) -> std::optional<Token::Literal> {
if (!ImHexApi::HexEditor::isSelectionValid())
return std::numeric_limits<u128>::max();
auto selection = ImHexApi::HexEditor::getSelection();
return u128(u128(selection->getStartAddress()) << 64 | u128(selection->getSize()));
});
}
pl::api::Namespace nsHexPrv = { "builtin", "hex", "prv" };
{
/* get_information() */
ContentRegistry::PatternLanguage::addFunction(nsHexCore, "get_information", FunctionParameterCount::between(1, 2), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
std::string category = params[0].toString(false);
std::string argument = params.size() == 2 ? params[1].toString(false) : "";
if (!ImHexApi::Provider::isValid())
return u128(0);
auto provider = ImHexApi::Provider::get();
if (!provider->isAvailable())
return u128(0);
return std::visit(
[](auto &&value) -> Token::Literal {
return value;
},
provider->queryInformation(category, argument)
);
});
}
2022-09-30 12:49:49 +02:00
pl::api::Namespace nsHexDec = { "builtin", "hex", "dec" };
{
/* demangle(mangled_string) */
ContentRegistry::PatternLanguage::addFunction(nsHexDec, "demangle", FunctionParameterCount::exactly(1), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
2023-01-23 13:53:29 +01:00
const auto mangledString = params[0].toString(false);
2022-09-30 12:49:49 +02:00
return llvm::demangle(mangledString);
});
}
pl::api::Namespace nsHexHttp = { "builtin", "hex", "http" };
2021-09-25 16:24:08 +02:00
{
/* get(url) */
ContentRegistry::PatternLanguage::addDangerousFunction(nsHexHttp, "get", FunctionParameterCount::exactly(1), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
2023-01-23 13:53:29 +01:00
const auto url = params[0].toString(false);
2021-09-25 16:24:08 +02:00
hex::Net net;
return net.getString(url).get().body;
});
2021-09-25 16:24:08 +02:00
}
}
}