2023-11-18 14:34:33 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2023-03-23 11:23:07 +01:00
|
|
|
#include <hex/helpers/http_requests.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
#include <pl/core/token.hpp>
|
|
|
|
#include <pl/core/evaluator.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-09-30 12:49:49 +02:00
|
|
|
#include <llvm/Demangle/Demangle.h>
|
2024-01-11 20:11:22 +01:00
|
|
|
#include <pl/patterns/pattern.hpp>
|
2023-03-23 11:23:07 +01:00
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
void registerPatternLanguageFunctions() {
|
2022-08-06 21:38:09 +02:00
|
|
|
using namespace pl::core;
|
2022-04-17 16:57:30 +02:00
|
|
|
using FunctionParameterCount = pl::api::FunctionParameterCount;
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-09-15 09:34:59 +02:00
|
|
|
{
|
2023-11-10 20:47:08 +01:00
|
|
|
const pl::api::Namespace nsHexCore = { "builtin", "hex", "core" };
|
|
|
|
|
2022-09-15 09:34:59 +02:00
|
|
|
/* get_selection() */
|
2022-09-30 12:49:49 +02:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsHexCore, "get_selection", FunctionParameterCount::none(), [](Evaluator *, auto) -> std::optional<Token::Literal> {
|
2022-09-15 09:34:59 +02:00
|
|
|
if (!ImHexApi::HexEditor::isSelectionValid())
|
|
|
|
return std::numeric_limits<u128>::max();
|
|
|
|
|
|
|
|
auto selection = ImHexApi::HexEditor::getSelection();
|
|
|
|
|
|
|
|
return u128(u128(selection->getStartAddress()) << 64 | u128(selection->getSize()));
|
|
|
|
});
|
2024-01-11 20:11:22 +01:00
|
|
|
|
|
|
|
/* add_virtual_file(path, pattern) */
|
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsHexCore, "add_virtual_file", FunctionParameterCount::exactly(2), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto path = params[0].toString(false);
|
|
|
|
auto pattern = params[1].toPattern();
|
|
|
|
|
|
|
|
Region region = Region::Invalid();
|
|
|
|
if (pattern->getSection() == pl::ptrn::Pattern::MainSectionId)
|
|
|
|
region = Region(pattern->getOffset(), pattern->getSize());
|
|
|
|
|
|
|
|
ImHexApi::HexEditor::addVirtualFile(path, pattern->getBytes(), region);
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
2022-09-15 09:34:59 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
{
|
2023-11-10 20:47:08 +01:00
|
|
|
const pl::api::Namespace nsHexPrv = { "builtin", "hex", "prv" };
|
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
/* get_information() */
|
2023-03-28 10:27:49 +02:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsHexPrv, "get_information", FunctionParameterCount::between(1, 2), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
|
2023-01-24 09:07:11 +01:00
|
|
|
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
|
|
|
{
|
2023-11-10 20:47:08 +01:00
|
|
|
const pl::api::Namespace nsHexDec = { "builtin", "hex", "dec" };
|
|
|
|
|
2022-09-30 12:49:49 +02:00
|
|
|
/* 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:24:08 +02:00
|
|
|
{
|
2023-11-10 20:47:08 +01:00
|
|
|
const pl::api::Namespace nsHexHttp = { "builtin", "hex", "http" };
|
|
|
|
|
2021-09-25 16:24:08 +02:00
|
|
|
/* get(url) */
|
2022-09-15 09:34:59 +02:00
|
|
|
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
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
hex::HttpRequest request("GET", url);
|
|
|
|
return request.execute().get().getData();
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-09-25 16:24:08 +02:00
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
2021-09-21 21:29:18 +02:00
|
|
|
}
|