2020-11-15 01:42:43 +01:00
|
|
|
#include "views/view_strings.hpp"
|
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2020-11-15 01:42:43 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
2021-02-22 10:16:58 +01:00
|
|
|
#include <thread>
|
2020-11-15 01:42:43 +01:00
|
|
|
|
2020-11-24 18:12:08 +01:00
|
|
|
#include <llvm/Demangle/Demangle.h>
|
2021-02-22 10:16:58 +01:00
|
|
|
#include <imgui_imhex_extensions.h>
|
2020-11-24 18:12:08 +01:00
|
|
|
|
2020-11-23 13:10:14 +01:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2020-11-15 01:42:43 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2021-02-21 13:49:03 +01:00
|
|
|
ViewStrings::ViewStrings() : View("hex.view.strings.name"_lang) {
|
2021-01-21 10:53:12 +01:00
|
|
|
View::subscribeEvent(Events::DataChanged, [this](auto){
|
2020-11-22 19:43:35 +01:00
|
|
|
this->m_foundStrings.clear();
|
2020-11-15 01:42:43 +01:00
|
|
|
});
|
|
|
|
|
2021-02-17 14:47:25 +01:00
|
|
|
this->m_filter.resize(0xFFFF, 0x00);
|
2020-11-15 01:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewStrings::~ViewStrings() {
|
|
|
|
View::unsubscribeEvent(Events::DataChanged);
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:59:49 +01:00
|
|
|
|
2020-11-24 18:12:08 +01:00
|
|
|
void ViewStrings::createStringContextMenu(const FoundString &foundString) {
|
2021-01-21 23:09:43 +01:00
|
|
|
if (ImGui::TableGetColumnFlags(2) == ImGuiTableColumnFlags_IsHovered && ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
|
2020-11-24 02:59:49 +01:00
|
|
|
ImGui::OpenPopup("StringContextMenu");
|
2020-11-24 18:12:08 +01:00
|
|
|
this->m_selectedString = foundString.string;
|
|
|
|
}
|
2020-11-24 02:59:49 +01:00
|
|
|
if (ImGui::BeginPopup("StringContextMenu")) {
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::MenuItem("hex.view.strings.copy"_lang)) {
|
2020-11-24 18:12:08 +01:00
|
|
|
ImGui::SetClipboardText(this->m_selectedString.c_str());
|
|
|
|
}
|
2020-11-24 02:59:49 +01:00
|
|
|
ImGui::Separator();
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::MenuItem("hex.view.strings.demangle"_lang)) {
|
2020-11-24 18:12:08 +01:00
|
|
|
this->m_demangledName = llvm::demangle(this->m_selectedString);
|
|
|
|
if (!this->m_demangledName.empty())
|
2021-02-21 13:49:03 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("hex.view.strings.demangle.name"_lang); });
|
2020-11-24 18:12:08 +01:00
|
|
|
}
|
2020-11-24 02:59:49 +01:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:16:58 +01:00
|
|
|
void ViewStrings::searchStrings() {
|
|
|
|
this->m_foundStrings.clear();
|
|
|
|
this->m_searching = true;
|
2020-11-24 02:59:49 +01:00
|
|
|
|
2021-02-22 10:16:58 +01:00
|
|
|
std::thread([this] {
|
|
|
|
auto provider = SharedData::currentProvider;
|
2020-11-15 01:42:43 +01:00
|
|
|
|
|
|
|
std::vector<u8> buffer(1024, 0x00);
|
|
|
|
u32 foundCharacters = 0;
|
2020-12-27 15:39:06 +01:00
|
|
|
|
|
|
|
for (u64 offset = 0; offset < provider->getSize(); offset += buffer.size()) {
|
|
|
|
size_t readSize = std::min(u64(buffer.size()), provider->getSize() - offset);
|
|
|
|
provider->read(offset, buffer.data(), readSize);
|
2020-11-15 01:42:43 +01:00
|
|
|
|
2020-11-23 16:19:31 +01:00
|
|
|
for (u32 i = 0; i < readSize; i++) {
|
2020-11-15 01:42:43 +01:00
|
|
|
if (buffer[i] >= 0x20 && buffer[i] <= 0x7E)
|
|
|
|
foundCharacters++;
|
|
|
|
else {
|
|
|
|
if (foundCharacters >= this->m_minimumLength) {
|
|
|
|
FoundString foundString;
|
|
|
|
|
|
|
|
foundString.offset = offset + i - foundCharacters;
|
|
|
|
foundString.size = foundCharacters;
|
|
|
|
foundString.string.reserve(foundCharacters);
|
|
|
|
foundString.string.resize(foundCharacters);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(foundString.offset, foundString.string.data(), foundCharacters);
|
2020-11-15 01:42:43 +01:00
|
|
|
|
|
|
|
this->m_foundStrings.push_back(foundString);
|
|
|
|
}
|
|
|
|
|
|
|
|
foundCharacters = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:16:58 +01:00
|
|
|
this->m_searching = false;
|
|
|
|
}).detach();
|
2020-11-15 01:42:43 +01:00
|
|
|
|
2021-02-22 10:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStrings::drawContent() {
|
|
|
|
auto provider = SharedData::currentProvider;
|
|
|
|
|
|
|
|
|
|
|
|
if (ImGui::Begin("hex.view.strings.name"_lang, &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2020-12-27 15:39:06 +01:00
|
|
|
if (provider != nullptr && provider->isReadable()) {
|
2021-02-22 10:16:58 +01:00
|
|
|
ImGui::Disabled([this]{
|
|
|
|
if (ImGui::InputInt("hex.view.strings.min_length"_lang, &this->m_minimumLength, 1, 0))
|
|
|
|
this->m_foundStrings.clear();
|
|
|
|
|
|
|
|
ImGui::InputText("hex.view.strings.filter"_lang, this->m_filter.data(), this->m_filter.size());
|
|
|
|
if (ImGui::Button("hex.view.strings.extract"_lang))
|
|
|
|
this->searchStrings();
|
|
|
|
}, this->m_searching);
|
|
|
|
|
|
|
|
if (this->m_searching) {
|
|
|
|
ImGui::SameLine();
|
2021-02-22 13:06:53 +01:00
|
|
|
ImGui::TextSpinner("hex.view.strings.searching"_lang);
|
2021-02-22 10:16:58 +01:00
|
|
|
}
|
2020-11-15 03:51:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
if (ImGui::BeginTable("##strings", 3,
|
|
|
|
ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable |
|
2020-11-23 15:22:26 +01:00
|
|
|
ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TableSetupColumn("hex.view.strings.offset"_lang, 0, -1, ImGui::GetID("offset"));
|
|
|
|
ImGui::TableSetupColumn("hex.view.strings.size"_lang, 0, -1, ImGui::GetID("size"));
|
|
|
|
ImGui::TableSetupColumn("hex.view.strings.string"_lang, 0, -1, ImGui::GetID("string"));
|
2020-11-15 03:51:59 +01:00
|
|
|
|
|
|
|
auto sortSpecs = ImGui::TableGetSortSpecs();
|
|
|
|
|
|
|
|
if (sortSpecs->SpecsDirty) {
|
|
|
|
std::sort(this->m_foundStrings.begin(), this->m_foundStrings.end(),
|
2020-11-15 16:06:10 +01:00
|
|
|
[&sortSpecs](FoundString &left, FoundString &right) -> bool {
|
2021-02-17 14:47:25 +01:00
|
|
|
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("offset")) {
|
2020-11-15 03:51:59 +01:00
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left.offset > right.offset;
|
|
|
|
else
|
|
|
|
return left.offset < right.offset;
|
2021-02-17 14:47:25 +01:00
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("size")) {
|
2020-11-15 03:51:59 +01:00
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left.size > right.size;
|
|
|
|
else
|
|
|
|
return left.size < right.size;
|
2021-02-17 14:47:25 +01:00
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("string")) {
|
2020-11-15 03:51:59 +01:00
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left.string > right.string;
|
|
|
|
else
|
|
|
|
return left.string < right.string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
sortSpecs->SpecsDirty = false;
|
|
|
|
}
|
2020-11-15 01:42:43 +01:00
|
|
|
|
2020-11-15 03:51:59 +01:00
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
|
|
|
ImGuiListClipper clipper;
|
2020-11-20 11:56:37 +01:00
|
|
|
clipper.Begin(this->m_foundStrings.size());
|
|
|
|
|
|
|
|
while (clipper.Step()) {
|
|
|
|
for (u64 i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
|
|
|
auto &foundString = this->m_foundStrings[i];
|
|
|
|
|
2021-02-17 14:47:25 +01:00
|
|
|
if (strlen(this->m_filter.data()) != 0 &&
|
|
|
|
foundString.string.find(this->m_filter.data()) == std::string::npos)
|
2020-11-20 11:56:37 +01:00
|
|
|
continue;
|
|
|
|
|
2020-11-23 13:08:24 +01:00
|
|
|
ImGui::TableNextRow();
|
2020-11-20 11:56:37 +01:00
|
|
|
ImGui::TableNextColumn();
|
2020-11-23 13:10:14 +01:00
|
|
|
if (ImGui::Selectable(("##StringLine"s + std::to_string(i)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
|
|
|
|
Region selectRegion = { foundString.offset, foundString.size };
|
2021-01-21 10:53:12 +01:00
|
|
|
View::postEvent(Events::SelectionChangeRequest, selectRegion);
|
2020-11-23 13:10:14 +01:00
|
|
|
}
|
2020-11-24 18:12:08 +01:00
|
|
|
ImGui::PushID(i + 1);
|
|
|
|
createStringContextMenu(foundString);
|
|
|
|
ImGui::PopID();
|
2020-11-23 13:10:14 +01:00
|
|
|
ImGui::SameLine();
|
2020-11-20 11:56:37 +01:00
|
|
|
ImGui::Text("0x%08lx : 0x%08lx", foundString.offset, foundString.offset + foundString.size);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::Text("0x%04lx", foundString.size);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::Text("%s", foundString.string.c_str());
|
|
|
|
}
|
2020-11-15 03:51:59 +01:00
|
|
|
}
|
|
|
|
clipper.End();
|
2020-11-15 01:42:43 +01:00
|
|
|
|
2020-11-15 03:51:59 +01:00
|
|
|
ImGui::EndTable();
|
2020-11-15 01:42:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
2020-11-24 18:12:08 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::BeginPopup("hex.view.strings.demangle.title"_lang)) {
|
2020-11-24 18:12:08 +01:00
|
|
|
if (ImGui::BeginChild("##scrolling", ImVec2(500, 150))) {
|
2021-02-17 14:47:25 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.strings.demangle.title"_lang);
|
2020-11-24 18:12:08 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::TextWrapped("%s", this->m_demangledName.c_str());
|
|
|
|
ImGui::EndChild();
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::Button("hex.view.strings.demangle.copy"_lang))
|
2020-11-24 18:12:08 +01:00
|
|
|
ImGui::SetClipboardText(this->m_demangledName.c_str());
|
|
|
|
}
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2020-11-15 01:42:43 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewStrings::drawMenu() {
|
2020-11-23 23:57:19 +01:00
|
|
|
|
2020-11-15 01:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|