1
0
mirror of synced 2024-12-11 23:46:00 +01:00
ImHex/source/views/view_strings.cpp

218 lines
9.5 KiB
C++
Raw Normal View History

2020-11-15 01:42:43 +01:00
#include "views/view_strings.hpp"
#include <hex/providers/provider.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
#include <llvm/Demangle/Demangle.h>
2021-02-22 10:16:58 +01:00
#include <imgui_imhex_extensions.h>
using namespace std::literals::string_literals;
2020-11-15 01:42:43 +01:00
namespace hex {
ViewStrings::ViewStrings() : View("hex.view.strings.name") {
EventManager::subscribe<EventDataChanged>(this, [this]() {
this->m_foundStrings.clear();
2020-11-15 01:42:43 +01:00
});
this->m_filter.reserve(0xFFFF);
std::memset(this->m_filter.data(), 0x00, this->m_filter.capacity());
2020-11-15 01:42:43 +01:00
}
ViewStrings::~ViewStrings() {
EventManager::unsubscribe<EventDataChanged>(this);
2020-11-15 01:42:43 +01:00
}
std::string readString(const FoundString &foundString) {
std::string string(foundString.size + 1, '\0');
ImHexApi::Provider::get()->read(foundString.offset, string.data(), foundString.size);
return string;
}
void ViewStrings::createStringContextMenu(const FoundString &foundString) {
if (ImGui::TableGetColumnFlags(2) == ImGuiTableColumnFlags_IsHovered && ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
ImGui::OpenPopup("StringContextMenu");
this->m_selectedString = readString(foundString);
}
if (ImGui::BeginPopup("StringContextMenu")) {
if (ImGui::MenuItem("hex.view.strings.copy"_lang)) {
ImGui::SetClipboardText(this->m_selectedString.c_str());
}
ImGui::Separator();
if (ImGui::MenuItem("hex.view.strings.demangle"_lang)) {
this->m_demangledName = llvm::demangle(this->m_selectedString);
if (!this->m_demangledName.empty())
View::doLater([]{ ImGui::OpenPopup("hex.view.strings.demangle.name"_lang); });
}
ImGui::EndPopup();
}
}
2021-02-22 10:16:58 +01:00
void ViewStrings::searchStrings() {
this->m_foundStrings.clear();
this->m_filterIndices.clear();
2021-02-22 10:16:58 +01:00
this->m_searching = true;
2021-02-22 10:16:58 +01:00
std::thread([this] {
auto provider = ImHexApi::Provider::get();
2020-11-15 01:42:43 +01:00
std::vector<u8> buffer(1024, 0x00);
u32 foundCharacters = 0;
for (u64 offset = 0; offset < provider->getSize(); offset += buffer.size()) {
size_t readSize = std::min(u64(buffer.size()), provider->getSize() - offset);
provider->readRelative(offset, buffer.data(), readSize);
2020-11-15 01:42:43 +01:00
for (u32 i = 0; i < readSize; i++) {
if (buffer[i] >= ' ' && buffer[i] <= '~' && offset < provider->getSize() - 1)
2020-11-15 01:42:43 +01:00
foundCharacters++;
else {
if (foundCharacters >= this->m_minimumLength) {
FoundString foundString = {
offset + i - foundCharacters + provider->getBaseAddress(),
foundCharacters
};
2020-11-15 01:42:43 +01:00
this->m_foundStrings.push_back(foundString);
this->m_filterIndices.push_back(this->m_foundStrings.size() - 1);
2020-11-15 01:42:43 +01:00
}
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 = ImHexApi::Provider::get();
2021-02-22 10:16:58 +01:00
if (ImGui::Begin(View::toWindowName("hex.view.strings.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
if (ImHexApi::Provider::isValid() && 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.capacity(), ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) {
auto &view = *static_cast<ViewStrings*>(data->UserData);
view.m_filter.resize(data->BufTextLen);
view.m_filterIndices.clear();
for (u64 i = 0; i < view.m_foundStrings.size(); i++) {
if (readString(view.m_foundStrings[i]).find(data->Buf) != std::string::npos)
view.m_filterIndices.push_back(i);
}
return 0;
}, this);
2021-02-22 10:16:58 +01:00
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 |
ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
ImGui::TableSetupScrollFreeze(0, 1);
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(),
[&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 readString(left) > readString(right);
2020-11-15 03:51:59 +01:00
else
return readString(left) < readString(right);
2020-11-15 03:51:59 +01:00
}
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;
clipper.Begin(this->m_filterIndices.size());
2020-11-20 11:56:37 +01:00
while (clipper.Step()) {
for (u64 i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
auto &foundString = this->m_foundStrings[this->m_filterIndices[i]];
auto string = readString(foundString);
2020-11-20 11:56:37 +01:00
2020-11-23 13:08:24 +01:00
ImGui::TableNextRow();
2020-11-20 11:56:37 +01:00
ImGui::TableNextColumn();
if (ImGui::Selectable(("##StringLine"s + std::to_string(i)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
EventManager::post<RequestSelectionChange>(Region { foundString.offset, foundString.size });
}
ImGui::PushID(i + 1);
createStringContextMenu(foundString);
ImGui::PopID();
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", string.c_str());
2020-11-20 11:56:37 +01:00
}
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();
if (ImGui::BeginPopup("hex.view.strings.demangle.title"_lang)) {
if (ImGui::BeginChild("##scrolling", ImVec2(500, 150))) {
2021-02-17 14:47:25 +01:00
ImGui::TextUnformatted("hex.view.strings.demangle.title"_lang);
ImGui::Separator();
ImGui::TextWrapped("%s", this->m_demangledName.c_str());
ImGui::NewLine();
if (ImGui::Button("hex.view.strings.demangle.copy"_lang))
ImGui::SetClipboardText(this->m_demangledName.c_str());
}
ImGui::EndChild();
ImGui::EndPopup();
}
2020-11-15 01:42:43 +01:00
}
void ViewStrings::drawMenu() {
2020-11-15 01:42:43 +01:00
}
}