1
0
mirror of synced 2024-09-23 19:18:24 +02:00

feat: Added ability to query provider information from pattern language

This commit is contained in:
WerWolv 2023-01-24 09:07:11 +01:00
parent 915106f360
commit cf9df6e36d
18 changed files with 122 additions and 15 deletions

@ -1 +1 @@
Subproject commit 59d8ceccb0b668e16a5d2b25c457514c9e8c1493
Subproject commit 40cd303e9213e544a3ff21562b16406140f254ec

View File

@ -64,7 +64,8 @@ namespace hex::prv {
[[nodiscard]] virtual std::optional<u32> getPageOfAddress(u64 address) const;
[[nodiscard]] virtual std::string getName() const = 0;
[[nodiscard]] virtual std::vector<std::pair<std::string, std::string>> getDataInformation() const = 0;
[[nodiscard]] virtual std::vector<std::pair<std::string, std::string>> getDataDescription() const = 0;
[[nodiscard]] virtual std::variant<std::string, i128> queryInformation(const std::string &category, const std::string &argument);
[[nodiscard]] virtual bool open() = 0;
virtual void close() = 0;

View File

@ -8,6 +8,8 @@
#include <map>
#include <optional>
#include <hex/helpers/magic.hpp>
namespace hex::prv {
u32 Provider::s_idCounter = 0;
@ -301,4 +303,16 @@ namespace hex::prv {
s_idCounter = id + 1;
}
[[nodiscard]] std::variant<std::string, i128> Provider::queryInformation(const std::string &category, const std::string &) {
if (category == "mime")
return magic::getMIMEType(this);
else if (category == "description")
return magic::getDescription(this);
else if (category == "provider_type")
return this->getTypeName();
else
return 0;
}
}

View File

@ -34,7 +34,7 @@ namespace hex::plugin::builtin {
void close() override;
[[nodiscard]] std::string getName() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override;
[[nodiscard]] bool hasLoadInterface() const override { return true; }
void drawLoadInterface() override;
@ -47,6 +47,7 @@ namespace hex::plugin::builtin {
}
[[nodiscard]] std::pair<Region, bool> getRegionValidity(u64 address) const override;
std::variant<std::string, i128> queryInformation(const std::string &category, const std::string &argument) override;
protected:
void reloadDrives();

View File

@ -47,7 +47,8 @@ namespace hex::plugin::builtin {
void saveAs(const std::fs::path &path) override;
[[nodiscard]] std::string getName() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override;
std::variant<std::string, i128> queryInformation(const std::string &category, const std::string &argument) override;
[[nodiscard]] bool hasFilePicker() const override { return true; }
[[nodiscard]] bool handleFilePicker() override;

View File

@ -32,7 +32,7 @@ namespace hex::plugin::builtin {
void saveAs(const std::fs::path &path) override;
[[nodiscard]] std::string getName() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override;
[[nodiscard]] bool open() override;
void close() override;
@ -50,6 +50,7 @@ namespace hex::plugin::builtin {
}
[[nodiscard]] std::pair<Region, bool> getRegionValidity(u64 address) const override;
std::variant<std::string, i128> queryInformation(const std::string &category, const std::string &argument) override;
protected:
hex::Socket m_socket;

View File

@ -27,7 +27,7 @@ namespace hex::plugin::builtin {
void close() override;
[[nodiscard]] std::string getName() const override;
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override { return { }; }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override { return { }; }
void loadSettings(const nlohmann::json &settings) override;
[[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override;

View File

@ -31,7 +31,7 @@ namespace hex::plugin::builtin {
void saveAs(const std::fs::path &path) override;
[[nodiscard]] std::string getName() const override { return LangEntry("hex.builtin.provider.mem_file.unsaved"); }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override { return { }; }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override { return { }; }
[[nodiscard]] std::string getTypeName() const override {
return "hex.builtin.provider.mem_file";

View File

@ -23,7 +23,7 @@ namespace hex::plugin::builtin {
[[nodiscard]] size_t getActualSize() const override { return 0x00; }
[[nodiscard]] std::string getName() const override { return "None"; }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override { return { }; }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override { return { }; }
void loadSettings(const nlohmann::json &settings) override { hex::unused(settings); }
[[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override { return settings; }

View File

@ -41,7 +41,7 @@ namespace hex::plugin::builtin {
[[nodiscard]] size_t getActualSize() const override { return this->m_size; }
[[nodiscard]] std::string getName() const override { return hex::format("{} View", this->m_provider->getName()); }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override { return this->m_provider->getDataInformation(); }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override { return this->m_provider->getDataDescription(); }
void loadSettings(const nlohmann::json &settings) override { hex::unused(settings); }
[[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override { return settings; }

View File

@ -1,5 +1,6 @@
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/net.hpp>
#include <pl/core/token.hpp>
@ -27,6 +28,29 @@ namespace hex::plugin::builtin {
});
}
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)
);
});
}
pl::api::Namespace nsHexDec = { "builtin", "hex", "dec" };
{
/* demangle(mangled_string) */

View File

@ -262,7 +262,7 @@ namespace hex::plugin::builtin {
return hex::toUTF8String(this->m_path);
}
std::vector<std::pair<std::string, std::string>> DiskProvider::getDataInformation() const {
std::vector<std::pair<std::string, std::string>> DiskProvider::getDataDescription() const {
return {
{ "hex.builtin.provider.disk.selected_disk"_lang, hex::toUTF8String(this->m_path) },
{ "hex.builtin.provider.disk.disk_size"_lang, hex::toByteString(this->m_diskSize) },
@ -363,4 +363,13 @@ namespace hex::plugin::builtin {
return { Region::Invalid(), false };
}
std::variant<std::string, i128> DiskProvider::queryInformation(const std::string &category, const std::string &argument) {
if (category == "file_path")
return hex::toUTF8String(this->m_path);
else if (category == "sector_size")
return this->m_sectorSize;
else
return Provider::queryInformation(category, argument);
}
}

View File

@ -158,7 +158,7 @@ namespace hex::plugin::builtin {
return hex::toUTF8String(this->m_path.filename());
}
std::vector<std::pair<std::string, std::string>> FileProvider::getDataInformation() const {
std::vector<std::pair<std::string, std::string>> FileProvider::getDataDescription() const {
std::vector<std::pair<std::string, std::string>> result;
result.emplace_back("hex.builtin.provider.file.path"_lang, hex::toUTF8String(this->m_path));
@ -173,6 +173,25 @@ namespace hex::plugin::builtin {
return result;
}
std::variant<std::string, i128> FileProvider::queryInformation(const std::string &category, const std::string &argument) {
if (category == "file_path")
return hex::toUTF8String(this->m_path);
else if (category == "file_name")
return hex::toUTF8String(this->m_path.filename());
else if (category == "file_extension")
return hex::toUTF8String(this->m_path.extension());
else if (category == "creation_time")
return this->m_fileStats.st_ctime;
else if (category == "access_time")
return this->m_fileStats.st_atime;
else if (category == "modification_time")
return this->m_fileStats.st_mtime;
else if (category == "permissions")
return this->m_fileStats.st_mode & 0777;
else
return Provider::queryInformation(category, argument);
}
bool FileProvider::handleFilePicker() {
return fs::openFileBrowser(fs::DialogMode::Open, {}, [this](const auto &path) {
this->setPath(path);

View File

@ -234,7 +234,7 @@ namespace hex::plugin::builtin {
return hex::format("hex.builtin.provider.gdb.name"_lang, address, port);
}
std::vector<std::pair<std::string, std::string>> GDBProvider::getDataInformation() const {
std::vector<std::pair<std::string, std::string>> GDBProvider::getDataDescription() const {
return {
{"hex.builtin.provider.gdb.server"_lang, hex::format("{}:{}", this->m_ipAddress, this->m_port)},
};
@ -331,4 +331,13 @@ namespace hex::plugin::builtin {
return { Region::Invalid(), false };
}
std::variant<std::string, i128> GDBProvider::queryInformation(const std::string &category, const std::string &argument) {
if (category == "ip")
return this->m_ipAddress;
else if (category == "port")
return this->m_port;
else
return Provider::queryInformation(category, argument);
}
}

View File

@ -226,7 +226,7 @@ namespace hex::plugin::builtin {
ImGui::TableNextRow();
for (auto &[name, value] : provider->getDataInformation()) {
for (auto &[name, value] : provider->getDataDescription()) {
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", name);
ImGui::TableNextColumn();

View File

@ -38,7 +38,7 @@ namespace hex::plugin::windows {
void saveAs(const std::fs::path &) override {}
[[nodiscard]] std::string getName() const override { return hex::format("hex.windows.provider.process_memory.name"_lang, this->m_selectedProcess != nullptr ? this->m_selectedProcess->name : ""); }
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override {
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override {
return {
{ "hex.windows.provider.process_memory.process_name"_lang, this->m_selectedProcess->name },
{ "hex.windows.provider.process_memory.process_id"_lang, std::to_string(this->m_selectedProcess->id) }
@ -61,6 +61,7 @@ namespace hex::plugin::windows {
}
[[nodiscard]] std::pair<Region, bool> getRegionValidity(u64) const override;
std::variant<std::string, i128> queryInformation(const std::string &category, const std::string &argument) override;
private:
void reloadProcessModules();

View File

@ -269,4 +269,31 @@ namespace hex::plugin::windows {
}
}
std::variant<std::string, i128> ProcessMemoryProvider::queryInformation(const std::string &category, const std::string &argument) {
auto findRegionByName = [this](const std::string &name) {
return std::find_if(this->m_memoryRegions.begin(), this->m_memoryRegions.end(),
[name](const auto &region) {
return region.name == name;
});
};
if (category == "region_address") {
if (auto iter = findRegionByName(argument); iter != this->m_memoryRegions.end())
return iter->region.getStartAddress();
else
return 0;
} else if (category == "region_size") {
if (auto iter = findRegionByName(argument); iter != this->m_memoryRegions.end())
return iter->region.getSize();
else
return 0;
} else if (category == "process_id") {
return this->m_selectedProcess->id;
} else if (category == "process_name") {
return this->m_selectedProcess->name;
} else
return Provider::queryInformation(category, argument);
}
}

View File

@ -28,7 +28,7 @@ namespace hex::test {
return "";
}
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataInformation() const override {
[[nodiscard]] std::vector<std::pair<std::string, std::string>> getDataDescription() const override {
return {};
}