diff --git a/plugins/builtin/include/content/views/view_disassembler.hpp b/plugins/builtin/include/content/views/view_disassembler.hpp index eb506555b..58e5c04bf 100644 --- a/plugins/builtin/include/content/views/view_disassembler.hpp +++ b/plugins/builtin/include/content/views/view_disassembler.hpp @@ -31,7 +31,7 @@ namespace hex::plugin::builtin { TaskHolder m_disassemblerTask; u64 m_baseAddress = 0; - ui::SelectedRegion m_range = ui::SelectedRegion::EntireData; + ui::RegionType m_range = ui::RegionType::EntireData; Region m_codeRegion = { 0, 0 }; Architecture m_architecture = Architecture::ARM; diff --git a/plugins/builtin/include/content/views/view_find.hpp b/plugins/builtin/include/content/views/view_find.hpp index 761d626e2..d2fc95e16 100644 --- a/plugins/builtin/include/content/views/view_find.hpp +++ b/plugins/builtin/include/content/views/view_find.hpp @@ -33,7 +33,8 @@ namespace hex::plugin::builtin { }; struct SearchSettings { - ui::SelectedRegion range = ui::SelectedRegion::EntireData; + ui::RegionType range = ui::RegionType::EntireData; + Region region = { 0, 0 }; enum class Mode : int { Strings, diff --git a/plugins/builtin/include/content/views/view_information.hpp b/plugins/builtin/include/content/views/view_information.hpp index 55340f4bd..cbaa2f43d 100644 --- a/plugins/builtin/include/content/views/view_information.hpp +++ b/plugins/builtin/include/content/views/view_information.hpp @@ -4,6 +4,7 @@ #include #include "content/helpers/diagrams.hpp" +#include #include #include @@ -34,6 +35,7 @@ namespace hex::plugin::builtin { TaskHolder m_analyzerTask; + Region m_analysisRegion = { 0, 0 }; Region m_analyzedRegion = { 0, 0 }; std::string m_dataDescription; @@ -47,10 +49,8 @@ namespace hex::plugin::builtin { void analyze(); - // User controlled input (referenced by ImgGui) int m_inputChunkSize = 0; - u64 m_inputStartAddress = 0; - u64 m_inputEndAddress = 0; + ui::RegionType m_selectionType = ui::RegionType::EntireData; }; } diff --git a/plugins/builtin/include/ui/widgets.hpp b/plugins/builtin/include/ui/widgets.hpp index 7fdf77c96..a356c0d86 100644 --- a/plugins/builtin/include/ui/widgets.hpp +++ b/plugins/builtin/include/ui/widgets.hpp @@ -9,20 +9,46 @@ namespace hex::prv { class Provider; } namespace hex::plugin::builtin::ui { - enum class SelectedRegion : int { + enum class RegionType : int { EntireData, - Selection + Selection, + Region }; - inline void regionSelectionPicker(SelectedRegion *region, bool showHeader = true, bool firstEntry = false) { + inline void regionSelectionPicker(Region *region, prv::Provider *provider, RegionType *type, bool showHeader = true, bool firstEntry = false) { if (showHeader) ImGui::Header("hex.builtin.common.range"_lang, firstEntry); - if (ImGui::RadioButton("hex.builtin.common.range.entire_data"_lang, *region == SelectedRegion::EntireData)) - *region = SelectedRegion::EntireData; - if (ImGui::RadioButton("hex.builtin.common.range.selection"_lang, *region == SelectedRegion::Selection)) - *region = SelectedRegion::Selection; + if (ImGui::RadioButton("hex.builtin.common.range.entire_data"_lang, *type == RegionType::EntireData)) { + *region = { provider->getBaseAddress(), provider->getActualSize() }; + *type = RegionType::EntireData; + } + if (ImGui::RadioButton("hex.builtin.common.range.selection"_lang, *type == RegionType::Selection)) { + *region = ImHexApi::HexEditor::getSelection().value_or(ImHexApi::HexEditor::ProviderRegion { { 0, 1 }, provider }); + *type = RegionType::Selection; + } + if (ImGui::RadioButton("hex.builtin.common.region"_lang, *type == RegionType::Region)) { + *type = RegionType::Region; + } + if (*type == RegionType::Region) { + ImGui::SameLine(); + + const auto width = ImGui::GetContentRegionAvail().x / 2 - ImGui::CalcTextSize(" - ").x / 2 - ImGui::GetStyle().FramePadding.x * 4; + u64 start = region->getStartAddress(), end = region->getEndAddress(); + + ImGui::PushItemWidth(width); + ImGui::InputHexadecimal("##start", &start); + ImGui::PopItemWidth(); + ImGui::SameLine(); + ImGui::TextUnformatted(" - "); + ImGui::SameLine(); + ImGui::PushItemWidth(width); + ImGui::InputHexadecimal("##end", &end); + ImGui::PopItemWidth(); + + *region = { start, (end - start) + 1 }; + } } } \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_disassembler.cpp b/plugins/builtin/source/content/views/view_disassembler.cpp index 2501e6c2c..3f1805ae2 100644 --- a/plugins/builtin/source/content/views/view_disassembler.cpp +++ b/plugins/builtin/source/content/views/view_disassembler.cpp @@ -92,21 +92,8 @@ namespace hex::plugin::builtin { ImGui::InputHexadecimal("hex.builtin.view.disassembler.base"_lang, &this->m_baseAddress, ImGuiInputTextFlags_CharsHexadecimal); - ui::regionSelectionPicker(&this->m_range); - switch (this->m_range) { - case ui::SelectedRegion::Selection: { - auto region = ImHexApi::HexEditor::getSelection(); - if (region.has_value()) { - this->m_codeRegion = region.value(); - } - } - break; - case ui::SelectedRegion::EntireData: { - auto base = provider->getBaseAddress(); - this->m_codeRegion = { base, base + provider->getActualSize() - 1 }; - } - break; - } + ui::regionSelectionPicker(&this->m_codeRegion, provider, &this->m_range); + if (ImGui::IsItemEdited()) { // Force execution of Region Selection Event diff --git a/plugins/builtin/source/content/views/view_find.cpp b/plugins/builtin/source/content/views/view_find.cpp index 280c5f4b9..d4866851c 100644 --- a/plugins/builtin/source/content/views/view_find.cpp +++ b/plugins/builtin/source/content/views/view_find.cpp @@ -486,14 +486,7 @@ namespace hex::plugin::builtin { } void ViewFind::runSearch() { - Region searchRegion = [this]{ - if (this->m_searchSettings.range == ui::SelectedRegion::EntireData || !ImHexApi::HexEditor::isSelectionValid()) { - auto provider = ImHexApi::Provider::get(); - return Region { provider->getBaseAddress(), provider->getActualSize() }; - } else { - return ImHexApi::HexEditor::getSelection()->getRegion(); - } - }(); + Region searchRegion = this->m_searchSettings.region; this->m_searchTask = TaskManager::createTask("hex.builtin.view.find.searching", searchRegion.getSize(), [this, settings = this->m_searchSettings, searchRegion](auto &task) { auto provider = ImHexApi::Provider::get(); @@ -596,7 +589,7 @@ namespace hex::plugin::builtin { ImGui::BeginDisabled(this->m_searchTask.isRunning()); { - ui::regionSelectionPicker(&this->m_searchSettings.range, true, true); + ui::regionSelectionPicker(&this->m_searchSettings.region, provider, &this->m_searchSettings.range, true, true); ImGui::NewLine(); diff --git a/plugins/builtin/source/content/views/view_information.cpp b/plugins/builtin/source/content/views/view_information.cpp index d80887479..0c0dd3ce7 100644 --- a/plugins/builtin/source/content/views/view_information.cpp +++ b/plugins/builtin/source/content/views/view_information.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -69,21 +68,14 @@ namespace hex::plugin::builtin { auto provider = ImHexApi::Provider::get(); if ((this->m_inputChunkSize <= 0) - || (this->m_inputStartAddress >= this->m_inputEndAddress) - || ((size_t) this->m_inputEndAddress > provider->getActualSize())) { + || (this->m_analyzedRegion.getStartAddress() >= this->m_analyzedRegion.getEndAddress()) + || (this->m_analyzedRegion.getEndAddress() > provider->getActualSize())) { // Invalid parameters, set default one this->m_inputChunkSize = 256; - this->m_inputStartAddress = 0; - this->m_inputEndAddress = provider->getActualSize(); + this->m_analyzedRegion = { provider->getBaseAddress(), provider->getActualSize() }; } - task.setMaxValue(this->m_inputEndAddress - this->m_inputStartAddress); - - // Modify the analyzed region - this->m_analyzedRegion = { - provider->getBaseAddress() + this->m_inputStartAddress, - size_t(this->m_inputEndAddress - this->m_inputStartAddress) - }; + task.setMaxValue(this->m_analyzedRegion.getSize()); { magic::compile(); @@ -102,17 +94,18 @@ namespace hex::plugin::builtin { // Setup / start each analysis this->m_byteDistribution.reset(); - this->m_digram.reset(this->m_inputEndAddress - this->m_inputStartAddress); - this->m_layeredDistribution.reset(this->m_inputEndAddress - this->m_inputStartAddress); - this->m_byteTypesDistribution.reset(this->m_inputStartAddress, this->m_inputEndAddress, - provider->getBaseAddress(), provider->getActualSize()); - this->m_chunkBasedEntropy.reset(this->m_inputChunkSize, this->m_inputStartAddress, this->m_inputEndAddress, + this->m_digram.reset(this->m_analysisRegion.getSize()); + this->m_layeredDistribution.reset(this->m_analysisRegion.getSize()); + this->m_byteTypesDistribution.reset(this->m_analysisRegion.getStartAddress(), this->m_analysisRegion.getEndAddress(), provider->getBaseAddress(), provider->getActualSize()); + this->m_chunkBasedEntropy.reset(this->m_inputChunkSize, this->m_analysisRegion.getStartAddress(), this->m_analysisRegion.getEndAddress(), provider->getBaseAddress(), provider->getActualSize()); // Create a handle to the file auto reader = prv::ProviderReader(provider); - reader.seek(provider->getBaseAddress() + this->m_inputStartAddress); - reader.setEndAddress(provider->getBaseAddress() + this->m_inputEndAddress); + reader.seek(this->m_analysisRegion.getStartAddress()); + reader.setEndAddress(this->m_analysisRegion.getEndAddress()); + + this->m_analyzedRegion = this->m_analysisRegion; u64 count = 0; @@ -128,7 +121,7 @@ namespace hex::plugin::builtin { task.update(count); } - this->m_averageEntropy = this->m_chunkBasedEntropy.calculateEntropy(this->m_byteDistribution.get(), this->m_inputEndAddress - this->m_inputStartAddress); + this->m_averageEntropy = this->m_chunkBasedEntropy.calculateEntropy(this->m_byteDistribution.get(), this->m_analyzedRegion.getSize()); this->m_highestBlockEntropy = this->m_chunkBasedEntropy.getHighestEntropyBlockValue(); this->m_highestBlockEntropyAddress = this->m_chunkBasedEntropy.getHighestEntropyBlockAddress(); this->m_lowestBlockEntropy = this->m_chunkBasedEntropy.getLowestEntropyBlockValue(); @@ -150,11 +143,7 @@ namespace hex::plugin::builtin { { ImGui::Header("hex.builtin.common.settings"_lang, true); - const u64 min = 0; - const u64 max = provider->getActualSize(); - ImGui::SliderScalar("hex.builtin.common.begin"_lang, ImGuiDataType_U64, &this->m_inputStartAddress, &min, &max, "0x%02llX", ImGuiSliderFlags_AlwaysClamp); - ImGui::SliderScalar("hex.builtin.common.end"_lang, ImGuiDataType_U64, &this->m_inputEndAddress, &min, &max, "0x%02llX", ImGuiSliderFlags_AlwaysClamp); - + ui::regionSelectionPicker(&this->m_analysisRegion, provider, &this->m_selectionType, false); ImGui::NewLine(); ImGui::InputInt("hex.builtin.view.information.block_size"_lang, &this->m_inputChunkSize, ImGuiInputTextFlags_CharsDecimal);