From f60f9f9fc912a22b222588f6211ed9ff09ccbece Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 29 Aug 2021 11:10:48 +0200 Subject: [PATCH] patterns: Fix endian settings not applying to char16 --- include/views/view_store.hpp | 25 ++++++++++++++++++ .../include/hex/lang/pattern_data.hpp | 15 ++++++----- plugins/libimhex/source/lang/evaluator.cpp | 1 + source/views/view_store.cpp | 26 +++++++++++++++++++ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 include/views/view_store.hpp create mode 100644 source/views/view_store.cpp diff --git a/include/views/view_store.hpp b/include/views/view_store.hpp new file mode 100644 index 000000000..c202c3ac9 --- /dev/null +++ b/include/views/view_store.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include +#include + +#include +#include + +namespace hex { + + namespace prv { class Provider; } + + class ViewTools : public View { + public: + ViewTools(); + ~ViewTools() override; + + void drawContent() override; + void drawMenu() override; + + }; + +} \ No newline at end of file diff --git a/plugins/libimhex/include/hex/lang/pattern_data.hpp b/plugins/libimhex/include/hex/lang/pattern_data.hpp index e89e3dffc..e3cac526e 100644 --- a/plugins/libimhex/include/hex/lang/pattern_data.hpp +++ b/plugins/libimhex/include/hex/lang/pattern_data.hpp @@ -21,9 +21,10 @@ namespace hex::lang { namespace { - std::string makeDisplayable(u8 *data, size_t size) { + template requires requires { sizeof(T) == 1; } + std::string makeDisplayable(T *data, size_t size) { std::string result; - for (u8* c = data; c < (data + size); c++) { + for (T* c = data; c < (data + size); c++) { if (iscntrl(*c) || *c > 0x7F) result += " "; else @@ -517,6 +518,7 @@ namespace hex::lang { void createEntry(prv::Provider* &provider) override { char16_t character; provider->read(this->getOffset(), &character, 2); + character = hex::changeEndianess(character, this->getEndian()); this->createDefaultEntry(hex::format("'{0}'", std::wstring_convert, char16_t>{}.to_bytes(character))); } @@ -536,9 +538,8 @@ namespace hex::lang { } void createEntry(prv::Provider* &provider) override { - std::vector buffer(this->getSize() + 1, 0x00); + std::string buffer(this->getSize(), 0x00); provider->read(this->getOffset(), buffer.data(), this->getSize()); - buffer[this->getSize()] = '\0'; this->createDefaultEntry(hex::format("\"{0}\"", makeDisplayable(buffer.data(), this->getSize()).c_str())); } @@ -558,9 +559,11 @@ namespace hex::lang { } void createEntry(prv::Provider* &provider) override { - std::u16string buffer(this->getSize() + 1, 0x00); + std::u16string buffer(this->getSize(), 0x00); provider->read(this->getOffset(), buffer.data(), this->getSize()); - buffer[this->getSize()] = '\0'; + + for (auto &c : buffer) + c = hex::changeEndianess(c, 2, this->getEndian()); auto utf8String = std::wstring_convert, char16_t>{}.to_bytes(buffer); diff --git a/plugins/libimhex/source/lang/evaluator.cpp b/plugins/libimhex/source/lang/evaluator.cpp index b78998c4e..ac98f77d7 100644 --- a/plugins/libimhex/source/lang/evaluator.cpp +++ b/plugins/libimhex/source/lang/evaluator.cpp @@ -1090,6 +1090,7 @@ namespace hex::lang { } pattern->setVariableName(node->getName().data()); + pattern->setEndian(this->getCurrentEndian()); return this->evaluateAttributes(node, pattern); } diff --git a/source/views/view_store.cpp b/source/views/view_store.cpp new file mode 100644 index 000000000..e62129b55 --- /dev/null +++ b/source/views/view_store.cpp @@ -0,0 +1,26 @@ +#include "views/view_tools.hpp" + +#include + +namespace hex { + + ViewTools::ViewTools() : View("hex.view.tools.name") { } + + ViewTools::~ViewTools() { } + + void ViewTools::drawContent() { + if (ImGui::Begin(View::toWindowName("hex.view.tools.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { + for (const auto& [name, function] : ContentRegistry::Tools::getEntries()) { + if (ImGui::CollapsingHeader(LangEntry(name))) { + function(); + } + } + } + ImGui::End(); + } + + void ViewTools::drawMenu() { + + } + +} \ No newline at end of file