2022-07-29 13:59:57 +02:00
|
|
|
#include "content/views/view_find.hpp"
|
|
|
|
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
#include <hex/providers/buffered_reader.hpp>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2022-09-19 10:34:57 +02:00
|
|
|
#include <charconv>
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
#include <llvm/Demangle/Demangle.h>
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
ViewFind::ViewFind() : View("hex.builtin.view.find.name") {
|
|
|
|
const static auto HighlightColor = [] { return (ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarPurple) & 0x00FFFFFF) | 0x70000000; };
|
|
|
|
|
2022-09-28 15:01:43 +02:00
|
|
|
ImHexApi::HexEditor::addBackgroundHighlightingProvider([this](u64 address, const u8* data, size_t size, bool) -> std::optional<color_t> {
|
2022-07-29 13:59:57 +02:00
|
|
|
hex::unused(data, size);
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
if (this->m_searchTask.isRunning())
|
|
|
|
return { };
|
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
|
|
|
|
if (!this->m_occurrenceTree[provider].findOverlapping(address, address).empty())
|
2022-07-29 13:59:57 +02:00
|
|
|
return HighlightColor();
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
|
|
|
|
|
|
|
ImHexApi::HexEditor::addTooltipProvider([this](u64 address, const u8* data, size_t size) {
|
|
|
|
hex::unused(data, size);
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
if (this->m_searchTask.isRunning())
|
|
|
|
return;
|
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
|
|
|
|
auto occurrences = this->m_occurrenceTree[provider].findOverlapping(address, address);
|
|
|
|
if (occurrences.empty())
|
2022-07-29 13:59:57 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
ImGui::BeginTooltip();
|
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
for (const auto &occurrence : occurrences) {
|
|
|
|
ImGui::PushID(&occurrence);
|
|
|
|
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
{
|
|
|
|
const auto value = this->decodeValue(ImHexApi::Provider::get(), occurrence.value);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
ImGui::ColorButton("##color", ImColor(HighlightColor()));
|
|
|
|
ImGui::SameLine(0, 10);
|
2022-09-19 10:34:57 +02:00
|
|
|
ImGui::TextFormatted("{} ", value);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
if (ImGui::GetIO().KeyShift) {
|
|
|
|
ImGui::Indent();
|
|
|
|
if (ImGui::BeginTable("##extra_info", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-08-06 12:57:47 +02:00
|
|
|
ImGui::TextFormatted("{}: ", "hex.builtin.common.region"_lang.get());
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TableNextColumn();
|
2022-08-06 12:57:47 +02:00
|
|
|
ImGui::TextFormatted("[ 0x{:08X} - 0x{:08X} ]", occurrence.value.region.getStartAddress(), occurrence.value.region.getEndAddress());
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
auto demangledValue = llvm::demangle(value);
|
|
|
|
|
|
|
|
if (value != demangledValue) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted("{}: ", "hex.builtin.view.find.demangled"_lang.get());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted("{}", demangledValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_TableRowBg, HighlightColor());
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_TableRowBgAlt, HighlightColor());
|
|
|
|
ImGui::EndTable();
|
|
|
|
ImGui::PopStyleColor(2);
|
|
|
|
}
|
|
|
|
ImGui::PopID();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-03 10:19:34 +02:00
|
|
|
|
|
|
|
std::vector<ViewFind::BinaryPattern> ViewFind::parseBinaryPatternString(std::string string) {
|
|
|
|
std::vector<BinaryPattern> result;
|
|
|
|
|
|
|
|
if (string.length() < 2)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
bool inString = false;
|
|
|
|
while (string.length() > 0) {
|
|
|
|
BinaryPattern pattern = { 0, 0 };
|
|
|
|
if (string.starts_with("\"")) {
|
|
|
|
inString = !inString;
|
|
|
|
string = string.substr(1);
|
|
|
|
continue;
|
|
|
|
} else if (inString) {
|
|
|
|
pattern = { 0xFF, u8(string.front()) };
|
|
|
|
string = string.substr(1);
|
|
|
|
} else if (string.starts_with("??")) {
|
|
|
|
pattern = { 0x00, 0x00 };
|
|
|
|
string = string.substr(2);
|
|
|
|
} else if ((std::isxdigit(string.front()) || string.front() == '?') && string.length() >= 2) {
|
|
|
|
const auto hex = string.substr(0, 2);
|
|
|
|
|
|
|
|
for (const auto &c : hex) {
|
|
|
|
pattern.mask <<= 4;
|
|
|
|
pattern.value <<= 4;
|
|
|
|
|
|
|
|
if (std::isxdigit(c)) {
|
|
|
|
pattern.mask |= 0x0F;
|
|
|
|
|
|
|
|
if (auto hexValue = hex::hexCharToValue(c); hexValue.has_value())
|
|
|
|
pattern.value |= hexValue.value();
|
|
|
|
else
|
|
|
|
return { };
|
|
|
|
} else if (c != '?') {
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string = string.substr(2);
|
|
|
|
} else if (std::isspace(string.front())) {
|
|
|
|
string = string.substr(1);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inString)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
template<typename Type, typename StorageType>
|
|
|
|
static std::tuple<bool, std::variant<u64, i64, float, double>, size_t> parseNumericValue(const std::string &string) {
|
|
|
|
static_assert(sizeof(StorageType) >= sizeof(Type));
|
|
|
|
|
2023-01-21 18:44:12 +01:00
|
|
|
StorageType value;
|
|
|
|
|
|
|
|
std::size_t processed = 0;
|
|
|
|
try {
|
|
|
|
if constexpr (std::floating_point<Type>)
|
|
|
|
value = std::stod(string, &processed);
|
|
|
|
else if constexpr (std::signed_integral<Type>)
|
|
|
|
value = std::stoll(string, &processed, 0);
|
|
|
|
else
|
|
|
|
value = std::stoull(string, &processed, 0);
|
|
|
|
} catch (std::exception &) {
|
|
|
|
return { false, { }, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (processed != string.size())
|
2022-09-19 10:34:57 +02:00
|
|
|
return { false, { }, 0 };
|
|
|
|
|
2022-12-18 14:17:57 +01:00
|
|
|
if (value < std::numeric_limits<Type>::lowest() || value > std::numeric_limits<Type>::max())
|
2022-09-19 10:34:57 +02:00
|
|
|
return { false, { }, 0 };
|
|
|
|
|
|
|
|
return { true, value, sizeof(Type) };
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<bool, std::variant<u64, i64, float, double>, size_t> ViewFind::parseNumericValueInput(const std::string &input, SearchSettings::Value::Type type) {
|
|
|
|
switch (type) {
|
|
|
|
using enum SearchSettings::Value::Type;
|
|
|
|
|
|
|
|
case U8: return parseNumericValue<u8, u64>(input);
|
|
|
|
case U16: return parseNumericValue<u16, u64>(input);
|
|
|
|
case U32: return parseNumericValue<u32, u64>(input);
|
|
|
|
case U64: return parseNumericValue<u64, u64>(input);
|
|
|
|
case I8: return parseNumericValue<i8, i64>(input);
|
|
|
|
case I16: return parseNumericValue<i16, i64>(input);
|
|
|
|
case I32: return parseNumericValue<i32, i64>(input);
|
|
|
|
case I64: return parseNumericValue<i64, i64>(input);
|
|
|
|
case F32: return parseNumericValue<float, float>(input);
|
|
|
|
case F64: return parseNumericValue<double, double>(input);
|
|
|
|
default: return { false, { }, 0 };
|
|
|
|
}
|
2022-10-02 17:30:26 +02:00
|
|
|
}
|
2022-08-03 10:19:34 +02:00
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
template<typename T>
|
|
|
|
static std::string formatBytes(const std::vector<u8> &bytes) {
|
|
|
|
if (bytes.size() > sizeof(T))
|
|
|
|
return { };
|
|
|
|
|
|
|
|
T value = 0x00;
|
|
|
|
std::memcpy(&value, bytes.data(), bytes.size());
|
|
|
|
|
|
|
|
if (std::signed_integral<T>)
|
|
|
|
hex::signExtend(bytes.size() * 8, value);
|
|
|
|
|
|
|
|
return hex::format("{}", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ViewFind::Occurrence> ViewFind::searchStrings(Task &task, prv::Provider *provider, hex::Region searchRegion, const SearchSettings::Strings &settings) {
|
2023-03-17 08:16:13 +01:00
|
|
|
using enum SearchSettings::StringType;
|
2022-08-03 11:38:36 +02:00
|
|
|
|
|
|
|
std::vector<Occurrence> results;
|
|
|
|
|
|
|
|
if (settings.type == ASCII_UTF16BE || settings.type == ASCII_UTF16LE) {
|
|
|
|
auto newSettings = settings;
|
|
|
|
|
|
|
|
newSettings.type = ASCII;
|
|
|
|
auto asciiResults = searchStrings(task, provider, searchRegion, newSettings);
|
|
|
|
std::copy(asciiResults.begin(), asciiResults.end(), std::back_inserter(results));
|
|
|
|
|
|
|
|
if (settings.type == ASCII_UTF16BE) {
|
|
|
|
newSettings.type = UTF16BE;
|
|
|
|
auto utf16Results = searchStrings(task, provider, searchRegion, newSettings);
|
|
|
|
std::copy(utf16Results.begin(), utf16Results.end(), std::back_inserter(results));
|
|
|
|
} else if (settings.type == ASCII_UTF16LE) {
|
|
|
|
newSettings.type = UTF16LE;
|
|
|
|
auto utf16Results = searchStrings(task, provider, searchRegion, newSettings);
|
|
|
|
std::copy(utf16Results.begin(), utf16Results.end(), std::back_inserter(results));
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
auto reader = prv::ProviderReader(provider);
|
2022-07-29 13:59:57 +02:00
|
|
|
reader.seek(searchRegion.getStartAddress());
|
|
|
|
reader.setEndAddress(searchRegion.getEndAddress());
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
const auto [decodeType, endian] = [&] -> std::pair<Occurrence::DecodeType, std::endian> {
|
2022-08-03 11:38:36 +02:00
|
|
|
if (settings.type == ASCII)
|
2022-09-19 10:34:57 +02:00
|
|
|
return { Occurrence::DecodeType::ASCII, std::endian::native };
|
2023-03-17 08:16:13 +01:00
|
|
|
else if (settings.type == SearchSettings::StringType::UTF16BE)
|
2022-09-19 10:34:57 +02:00
|
|
|
return { Occurrence::DecodeType::UTF16, std::endian::big };
|
2023-03-17 08:16:13 +01:00
|
|
|
else if (settings.type == SearchSettings::StringType::UTF16LE)
|
2022-09-19 10:34:57 +02:00
|
|
|
return { Occurrence::DecodeType::UTF16, std::endian::little };
|
2022-08-03 11:38:36 +02:00
|
|
|
else
|
2022-09-19 10:34:57 +02:00
|
|
|
return { Occurrence::DecodeType::Binary, std::endian::native };
|
2022-08-03 11:38:36 +02:00
|
|
|
}();
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
size_t countedCharacters = 0;
|
|
|
|
u64 startAddress = reader.begin().getAddress();
|
2023-03-11 14:39:50 +01:00
|
|
|
u64 endAddress = reader.end().getAddress();
|
2023-03-21 15:37:49 +01:00
|
|
|
|
|
|
|
u64 progress = 0;
|
2022-07-29 13:59:57 +02:00
|
|
|
for (u8 byte : reader) {
|
|
|
|
bool validChar =
|
2023-03-17 08:16:13 +01:00
|
|
|
(settings.lowerCaseLetters && std::islower(byte)) ||
|
|
|
|
(settings.upperCaseLetters && std::isupper(byte)) ||
|
|
|
|
(settings.numbers && std::isdigit(byte)) ||
|
|
|
|
(settings.spaces && std::isspace(byte) && byte != '\r' && byte != '\n') ||
|
|
|
|
(settings.underscores && byte == '_') ||
|
|
|
|
(settings.symbols && std::ispunct(byte) && !std::isspace(byte)) ||
|
|
|
|
(settings.lineFeeds && (byte == '\r' || byte == '\n'));
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-03 11:38:36 +02:00
|
|
|
if (settings.type == UTF16LE) {
|
2022-07-29 13:59:57 +02:00
|
|
|
// Check if second byte of UTF-16 encoded string is 0x00
|
|
|
|
if (countedCharacters % 2 == 1)
|
2023-03-31 22:20:00 +02:00
|
|
|
validChar = byte == 0x00;
|
2022-08-03 11:38:36 +02:00
|
|
|
} else if (settings.type == UTF16BE) {
|
2022-07-29 13:59:57 +02:00
|
|
|
// Check if first byte of UTF-16 encoded string is 0x00
|
|
|
|
if (countedCharacters % 2 == 0)
|
2023-03-31 22:20:00 +02:00
|
|
|
validChar = byte == 0x00;
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:37:49 +01:00
|
|
|
task.update(progress);
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
if (validChar)
|
|
|
|
countedCharacters++;
|
2023-03-11 14:39:50 +01:00
|
|
|
if (!validChar || startAddress + countedCharacters == endAddress) {
|
2022-07-29 13:59:57 +02:00
|
|
|
if (countedCharacters >= size_t(settings.minLength)) {
|
|
|
|
if (!(settings.nullTermination && byte != 0x00)) {
|
2022-09-19 10:34:57 +02:00
|
|
|
results.push_back(Occurrence { Region { startAddress, countedCharacters }, decodeType, endian });
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startAddress += countedCharacters + 1;
|
|
|
|
countedCharacters = 0;
|
2023-03-21 15:37:49 +01:00
|
|
|
progress = startAddress - searchRegion.getStartAddress();
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
std::vector<ViewFind::Occurrence> ViewFind::searchSequence(Task &task, prv::Provider *provider, hex::Region searchRegion, const SearchSettings::Sequence &settings) {
|
2022-08-03 11:38:36 +02:00
|
|
|
std::vector<Occurrence> results;
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
auto reader = prv::ProviderReader(provider);
|
2022-07-29 13:59:57 +02:00
|
|
|
reader.seek(searchRegion.getStartAddress());
|
|
|
|
reader.setEndAddress(searchRegion.getEndAddress());
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
auto bytes = hex::decodeByteString(settings.sequence);
|
|
|
|
|
|
|
|
if (bytes.empty())
|
2022-09-13 14:05:48 +02:00
|
|
|
return { };
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
auto occurrence = reader.begin();
|
2023-03-21 15:37:49 +01:00
|
|
|
u64 progress = 0;
|
2022-07-29 13:59:57 +02:00
|
|
|
while (true) {
|
2023-03-21 15:37:49 +01:00
|
|
|
task.update(progress);
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
occurrence = std::search(reader.begin(), reader.end(), std::boyer_moore_horspool_searcher(bytes.begin(), bytes.end()));
|
2022-07-29 13:59:57 +02:00
|
|
|
if (occurrence == reader.end())
|
|
|
|
break;
|
|
|
|
|
2022-07-29 18:49:43 +02:00
|
|
|
auto address = occurrence.getAddress();
|
2022-08-06 12:57:47 +02:00
|
|
|
reader.seek(address + 1);
|
2022-09-19 10:34:57 +02:00
|
|
|
results.push_back(Occurrence{ Region { address, bytes.size() }, Occurrence::DecodeType::Binary, std::endian::native });
|
2023-03-21 15:37:49 +01:00
|
|
|
progress = address - searchRegion.getStartAddress();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
std::vector<ViewFind::Occurrence> ViewFind::searchRegex(Task &task, prv::Provider *provider, hex::Region searchRegion, const SearchSettings::Regex &settings) {
|
2022-08-03 11:38:36 +02:00
|
|
|
auto stringOccurrences = searchStrings(task, provider, searchRegion, SearchSettings::Strings {
|
2023-03-17 08:16:13 +01:00
|
|
|
.minLength = settings.minLength,
|
|
|
|
.nullTermination = settings.nullTermination,
|
|
|
|
.type = settings.type,
|
|
|
|
.lowerCaseLetters = true,
|
|
|
|
.upperCaseLetters = true,
|
|
|
|
.numbers = true,
|
|
|
|
.underscores = true,
|
|
|
|
.symbols = true,
|
|
|
|
.spaces = true,
|
|
|
|
.lineFeeds = true
|
2022-07-29 13:59:57 +02:00
|
|
|
});
|
|
|
|
|
2022-08-03 11:38:36 +02:00
|
|
|
std::vector<Occurrence> result;
|
2022-07-29 13:59:57 +02:00
|
|
|
std::regex regex(settings.pattern);
|
2022-08-03 11:38:36 +02:00
|
|
|
for (const auto &occurrence : stringOccurrences) {
|
|
|
|
std::string string(occurrence.region.getSize(), '\x00');
|
|
|
|
provider->read(occurrence.region.getStartAddress(), string.data(), occurrence.region.getSize());
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2023-03-21 15:37:49 +01:00
|
|
|
task.update();
|
|
|
|
|
2022-09-13 14:06:08 +02:00
|
|
|
if (settings.fullMatch) {
|
|
|
|
if (std::regex_match(string, regex))
|
|
|
|
result.push_back(occurrence);
|
|
|
|
} else {
|
|
|
|
if (std::regex_search(string, regex))
|
|
|
|
result.push_back(occurrence);
|
|
|
|
}
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
std::vector<ViewFind::Occurrence> ViewFind::searchBinaryPattern(Task &task, prv::Provider *provider, hex::Region searchRegion, const SearchSettings::BinaryPattern &settings) {
|
2022-08-03 11:38:36 +02:00
|
|
|
std::vector<Occurrence> results;
|
2022-08-03 10:19:34 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
auto reader = prv::ProviderReader(provider);
|
2022-08-03 10:19:34 +02:00
|
|
|
reader.seek(searchRegion.getStartAddress());
|
|
|
|
reader.setEndAddress(searchRegion.getEndAddress());
|
|
|
|
|
|
|
|
u32 matchedBytes = 0;
|
|
|
|
const size_t patternSize = settings.pattern.size();
|
2022-08-06 12:57:47 +02:00
|
|
|
|
2023-03-21 15:37:49 +01:00
|
|
|
u64 progress = 0;
|
2022-08-06 12:57:47 +02:00
|
|
|
for (auto it = reader.begin(); it != reader.end(); ++it) {
|
|
|
|
auto byte = *it;
|
|
|
|
|
2023-03-21 15:37:49 +01:00
|
|
|
task.update(progress);
|
2022-08-03 10:19:34 +02:00
|
|
|
if ((byte & settings.pattern[matchedBytes].mask) == settings.pattern[matchedBytes].value) {
|
|
|
|
matchedBytes++;
|
|
|
|
if (matchedBytes == settings.pattern.size()) {
|
2022-08-06 12:57:47 +02:00
|
|
|
auto occurrenceAddress = it.getAddress() - (patternSize - 1);
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
results.push_back(Occurrence { Region { occurrenceAddress, patternSize }, Occurrence::DecodeType::Binary, std::endian::native });
|
2023-03-21 15:37:49 +01:00
|
|
|
progress = occurrenceAddress;
|
2022-08-06 12:57:47 +02:00
|
|
|
it.setAddress(occurrenceAddress);
|
2022-08-03 10:19:34 +02:00
|
|
|
matchedBytes = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-06 13:16:53 +02:00
|
|
|
if (matchedBytes > 0)
|
|
|
|
it -= matchedBytes;
|
2022-08-03 10:19:34 +02:00
|
|
|
matchedBytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
std::vector<ViewFind::Occurrence> ViewFind::searchValue(Task &task, prv::Provider *provider, Region searchRegion, const SearchSettings::Value &settings) {
|
|
|
|
std::vector<Occurrence> results;
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
auto reader = prv::ProviderReader(provider);
|
2022-09-19 10:34:57 +02:00
|
|
|
reader.seek(searchRegion.getStartAddress());
|
|
|
|
reader.setEndAddress(searchRegion.getEndAddress());
|
|
|
|
|
|
|
|
const auto [validMin, min, sizeMin] = parseNumericValueInput(settings.inputMin, settings.type);
|
|
|
|
const auto [validMax, max, sizeMax] = parseNumericValueInput(settings.inputMax, settings.type);
|
|
|
|
|
|
|
|
if (!validMin || !validMax || sizeMin != sizeMax)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
const auto size = sizeMin;
|
|
|
|
|
|
|
|
u64 bytes = 0x00;
|
|
|
|
u64 address = searchRegion.getStartAddress();
|
|
|
|
size_t validBytes = 0;
|
|
|
|
for (u8 byte : reader) {
|
|
|
|
bytes <<= 8;
|
|
|
|
bytes |= byte;
|
|
|
|
|
|
|
|
if (validBytes == size) {
|
|
|
|
bytes &= hex::bitmask(size * 8);
|
|
|
|
|
2023-03-21 15:37:49 +01:00
|
|
|
task.update(address);
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
auto result = std::visit([&](auto tag) {
|
|
|
|
using T = std::remove_cvref_t<std::decay_t<decltype(tag)>>;
|
|
|
|
|
|
|
|
auto minValue = std::get<T>(min);
|
|
|
|
auto maxValue = std::get<T>(max);
|
|
|
|
|
|
|
|
T value = 0;
|
|
|
|
std::memcpy(&value, &bytes, size);
|
|
|
|
value = hex::changeEndianess(value, size, std::endian::big);
|
|
|
|
value = hex::changeEndianess(value, size, settings.endian);
|
|
|
|
|
|
|
|
return value >= minValue && value <= maxValue;
|
|
|
|
}, min);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
Occurrence::DecodeType decodeType = [&]{
|
|
|
|
switch (settings.type) {
|
|
|
|
using enum SearchSettings::Value::Type;
|
|
|
|
using enum Occurrence::DecodeType;
|
|
|
|
|
2023-01-04 14:03:09 +01:00
|
|
|
case U8:
|
|
|
|
case U16:
|
|
|
|
case U32:
|
|
|
|
case U64:
|
|
|
|
return Unsigned;
|
|
|
|
case I8:
|
|
|
|
case I16:
|
|
|
|
case I32:
|
|
|
|
case I64:
|
|
|
|
return Signed;
|
|
|
|
case F32:
|
|
|
|
return Float;
|
|
|
|
case F64:
|
|
|
|
return Double;
|
|
|
|
default:
|
|
|
|
return Binary;
|
2022-09-19 10:34:57 +02:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
|
|
results.push_back(Occurrence { Region { address - (size - 1), size }, decodeType, settings.endian });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
validBytes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
address++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
void ViewFind::runSearch() {
|
|
|
|
Region searchRegion = [this]{
|
2022-08-07 12:20:40 +02:00
|
|
|
if (this->m_searchSettings.range == ui::SelectedRegion::EntireData || !ImHexApi::HexEditor::isSelectionValid()) {
|
2022-07-29 13:59:57 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
return Region { provider->getBaseAddress(), provider->getActualSize() };
|
|
|
|
} else {
|
2022-11-08 21:43:22 +01:00
|
|
|
return ImHexApi::HexEditor::getSelection()->getRegion();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_searchTask = TaskManager::createTask("hex.builtin.view.find.searching", searchRegion.getSize(), [this, settings = this->m_searchSettings, searchRegion](auto &task) {
|
2022-07-29 13:59:57 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
|
|
|
|
switch (settings.mode) {
|
|
|
|
using enum SearchSettings::Mode;
|
|
|
|
case Strings:
|
2022-08-03 11:38:36 +02:00
|
|
|
this->m_foundOccurrences[provider] = searchStrings(task, provider, searchRegion, settings.strings);
|
2022-07-29 13:59:57 +02:00
|
|
|
break;
|
|
|
|
case Sequence:
|
2022-08-03 11:38:36 +02:00
|
|
|
this->m_foundOccurrences[provider] = searchSequence(task, provider, searchRegion, settings.bytes);
|
2022-07-29 13:59:57 +02:00
|
|
|
break;
|
|
|
|
case Regex:
|
2022-08-03 11:38:36 +02:00
|
|
|
this->m_foundOccurrences[provider] = searchRegex(task, provider, searchRegion, settings.regex);
|
2022-07-29 13:59:57 +02:00
|
|
|
break;
|
2022-08-03 10:19:34 +02:00
|
|
|
case BinaryPattern:
|
2022-08-03 11:38:36 +02:00
|
|
|
this->m_foundOccurrences[provider] = searchBinaryPattern(task, provider, searchRegion, settings.binaryPattern);
|
2022-08-03 10:19:34 +02:00
|
|
|
break;
|
2022-09-19 10:34:57 +02:00
|
|
|
case Value:
|
|
|
|
this->m_foundOccurrences[provider] = searchValue(task, provider, searchRegion, settings.value);
|
|
|
|
break;
|
2022-08-17 16:15:36 +02:00
|
|
|
}
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-06 12:57:47 +02:00
|
|
|
this->m_sortedOccurrences[provider] = this->m_foundOccurrences[provider];
|
|
|
|
|
|
|
|
OccurrenceTree::interval_vector intervals;
|
|
|
|
for (const auto &occurrence : this->m_foundOccurrences[provider])
|
|
|
|
intervals.push_back(OccurrenceTree::interval(occurrence.region.getStartAddress(), occurrence.region.getEndAddress(), occurrence));
|
|
|
|
this->m_occurrenceTree[provider] = std::move(intervals);
|
2022-08-17 16:15:36 +02:00
|
|
|
});
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 11:38:36 +02:00
|
|
|
std::string ViewFind::decodeValue(prv::Provider *provider, Occurrence occurrence) const {
|
|
|
|
std::vector<u8> bytes(std::min<size_t>(occurrence.region.getSize(), 128));
|
|
|
|
provider->read(occurrence.region.getStartAddress(), bytes.data(), bytes.size());
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
switch (this->m_decodeSettings.mode) {
|
|
|
|
using enum SearchSettings::Mode;
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
case Value:
|
2022-07-29 13:59:57 +02:00
|
|
|
case Strings:
|
|
|
|
{
|
2022-08-03 11:38:36 +02:00
|
|
|
switch (occurrence.decodeType) {
|
|
|
|
using enum Occurrence::DecodeType;
|
|
|
|
case Binary:
|
2022-07-29 13:59:57 +02:00
|
|
|
case ASCII:
|
|
|
|
result = hex::encodeByteString(bytes);
|
|
|
|
break;
|
2022-09-19 10:34:57 +02:00
|
|
|
case UTF16:
|
2023-03-31 22:20:00 +02:00
|
|
|
for (size_t i = occurrence.endian == std::endian::little ? 0 : 1; i < bytes.size(); i += 2)
|
2022-07-29 13:59:57 +02:00
|
|
|
result += hex::encodeByteString({ bytes[i] });
|
|
|
|
break;
|
2022-09-19 10:34:57 +02:00
|
|
|
case Unsigned:
|
|
|
|
result += formatBytes<u64>(bytes);
|
|
|
|
break;
|
|
|
|
case Signed:
|
|
|
|
result += formatBytes<i64>(bytes);
|
|
|
|
break;
|
|
|
|
case Float:
|
|
|
|
result += formatBytes<float>(bytes);
|
|
|
|
break;
|
|
|
|
case Double:
|
|
|
|
result += formatBytes<double>(bytes);
|
2022-07-29 13:59:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Sequence:
|
|
|
|
case Regex:
|
2022-08-03 10:19:34 +02:00
|
|
|
case BinaryPattern:
|
2022-07-29 13:59:57 +02:00
|
|
|
result = hex::encodeByteString(bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawContextMenu(const std::string &value) {
|
|
|
|
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right) && ImGui::IsItemHovered()) {
|
|
|
|
ImGui::OpenPopup("FindContextMenu");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("FindContextMenu")) {
|
2022-07-29 14:05:30 +02:00
|
|
|
if (ImGui::MenuItem("hex.builtin.view.find.context.copy"_lang))
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::SetClipboardText(value.c_str());
|
|
|
|
if (ImGui::MenuItem("hex.builtin.view.find.context.copy_demangle"_lang))
|
|
|
|
ImGui::SetClipboardText(llvm::demangle(value).c_str());
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewFind::drawContent() {
|
|
|
|
if (ImGui::Begin(View::toWindowName("hex.builtin.view.find.name").c_str(), &this->getWindowOpenState())) {
|
2022-07-29 18:49:43 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
ImGui::BeginDisabled(this->m_searchTask.isRunning());
|
2022-07-29 13:59:57 +02:00
|
|
|
{
|
2022-08-10 10:28:40 +02:00
|
|
|
ui::regionSelectionPicker(&this->m_searchSettings.range, true, true);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
if (ImGui::BeginTabBar("SearchMethods")) {
|
2023-03-17 08:16:13 +01:00
|
|
|
const std::array<std::string, 5> StringTypes = {
|
|
|
|
"hex.builtin.common.encoding.ascii"_lang,
|
|
|
|
"hex.builtin.common.encoding.utf16le"_lang,
|
|
|
|
"hex.builtin.common.encoding.utf16be"_lang,
|
|
|
|
hex::format("{} + {}", "hex.builtin.common.encoding.ascii"_lang, "hex.builtin.common.encoding.utf16le"_lang),
|
|
|
|
hex::format("{} + {}", "hex.builtin.common.encoding.ascii"_lang, "hex.builtin.common.encoding.utf16be"_lang)
|
|
|
|
};
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
auto &mode = this->m_searchSettings.mode;
|
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.strings"_lang)) {
|
|
|
|
auto &settings = this->m_searchSettings.strings;
|
|
|
|
mode = SearchSettings::Mode::Strings;
|
|
|
|
|
|
|
|
ImGui::InputInt("hex.builtin.view.find.strings.min_length"_lang, &settings.minLength, 1, 1);
|
|
|
|
if (settings.minLength < 1)
|
|
|
|
settings.minLength = 1;
|
|
|
|
|
|
|
|
if (ImGui::BeginCombo("hex.builtin.common.type"_lang, StringTypes[std::to_underlying(settings.type)].c_str())) {
|
|
|
|
for (size_t i = 0; i < StringTypes.size(); i++) {
|
2023-03-17 08:16:13 +01:00
|
|
|
auto type = static_cast<SearchSettings::StringType>(i);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
if (ImGui::Selectable(StringTypes[i].c_str(), type == settings.type))
|
|
|
|
settings.type = type;
|
|
|
|
}
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::CollapsingHeader("hex.builtin.view.find.strings.match_settings"_lang)) {
|
|
|
|
ImGui::Checkbox("hex.builtin.view.find.strings.null_term"_lang, &settings.nullTermination);
|
|
|
|
|
|
|
|
ImGui::Header("hex.builtin.view.find.strings.chars"_lang);
|
2023-03-17 08:16:13 +01:00
|
|
|
ImGui::Checkbox(hex::format("{} [a-z]", "hex.builtin.view.find.strings.lower_case"_lang.get()).c_str(), &settings.lowerCaseLetters);
|
|
|
|
ImGui::Checkbox(hex::format("{} [A-Z]", "hex.builtin.view.find.strings.upper_case"_lang.get()).c_str(), &settings.upperCaseLetters);
|
|
|
|
ImGui::Checkbox(hex::format("{} [0-9]", "hex.builtin.view.find.strings.numbers"_lang.get()).c_str(), &settings.numbers);
|
|
|
|
ImGui::Checkbox(hex::format("{} [_]", "hex.builtin.view.find.strings.underscores"_lang.get()).c_str(), &settings.underscores);
|
|
|
|
ImGui::Checkbox(hex::format("{} [!\"#$%...]", "hex.builtin.view.find.strings.symbols"_lang.get()).c_str(), &settings.symbols);
|
|
|
|
ImGui::Checkbox(hex::format("{} [ \\f\\t\\v]", "hex.builtin.view.find.strings.spaces"_lang.get()).c_str(), &settings.spaces);
|
|
|
|
ImGui::Checkbox(hex::format("{} [\\r\\n]", "hex.builtin.view.find.strings.line_feeds"_lang.get()).c_str(), &settings.lineFeeds);
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->m_settingsValid = true;
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.sequences"_lang)) {
|
|
|
|
auto &settings = this->m_searchSettings.bytes;
|
|
|
|
|
|
|
|
mode = SearchSettings::Mode::Sequence;
|
|
|
|
|
2022-09-19 11:29:51 +02:00
|
|
|
ImGui::InputTextIcon("hex.builtin.common.value"_lang, ICON_VS_SYMBOL_KEY, settings.sequence);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2022-09-13 14:05:48 +02:00
|
|
|
this->m_settingsValid = !settings.sequence.empty() && !hex::decodeByteString(settings.sequence).empty();
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.regex"_lang)) {
|
|
|
|
auto &settings = this->m_searchSettings.regex;
|
|
|
|
|
|
|
|
mode = SearchSettings::Mode::Regex;
|
|
|
|
|
2023-03-17 08:16:13 +01:00
|
|
|
ImGui::InputInt("hex.builtin.view.find.strings.min_length"_lang, &settings.minLength, 1, 1);
|
|
|
|
if (settings.minLength < 1)
|
|
|
|
settings.minLength = 1;
|
|
|
|
|
|
|
|
if (ImGui::BeginCombo("hex.builtin.common.type"_lang, StringTypes[std::to_underlying(settings.type)].c_str())) {
|
|
|
|
for (size_t i = 0; i < StringTypes.size(); i++) {
|
|
|
|
auto type = static_cast<SearchSettings::StringType>(i);
|
|
|
|
|
|
|
|
if (ImGui::Selectable(StringTypes[i].c_str(), type == settings.type))
|
|
|
|
settings.type = type;
|
|
|
|
}
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Checkbox("hex.builtin.view.find.strings.null_term"_lang, &settings.nullTermination);
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
2022-09-19 11:29:51 +02:00
|
|
|
ImGui::InputTextIcon("hex.builtin.view.find.regex.pattern"_lang, ICON_VS_REGEX, settings.pattern);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
std::regex regex(settings.pattern);
|
|
|
|
this->m_settingsValid = true;
|
|
|
|
} catch (std::regex_error &e) {
|
|
|
|
this->m_settingsValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.pattern.empty())
|
|
|
|
this->m_settingsValid = false;
|
|
|
|
|
2022-09-13 14:06:08 +02:00
|
|
|
ImGui::Checkbox("hex.builtin.view.find.regex.full_match"_lang, &settings.fullMatch);
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
2022-08-03 10:19:34 +02:00
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.binary_pattern"_lang)) {
|
|
|
|
auto &settings = this->m_searchSettings.binaryPattern;
|
|
|
|
|
|
|
|
mode = SearchSettings::Mode::BinaryPattern;
|
|
|
|
|
2022-09-19 11:29:51 +02:00
|
|
|
ImGui::InputTextIcon("hex.builtin.view.find.binary_pattern"_lang, ICON_VS_SYMBOL_NAMESPACE, settings.input);
|
2022-08-03 10:19:34 +02:00
|
|
|
|
|
|
|
settings.pattern = parseBinaryPatternString(settings.input);
|
|
|
|
this->m_settingsValid = !settings.pattern.empty();
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
2022-09-19 10:34:57 +02:00
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.value"_lang)) {
|
|
|
|
auto &settings = this->m_searchSettings.value;
|
|
|
|
|
|
|
|
mode = SearchSettings::Mode::Value;
|
|
|
|
|
|
|
|
bool edited = false;
|
|
|
|
|
2022-09-19 11:29:51 +02:00
|
|
|
if (ImGui::InputTextIcon("hex.builtin.view.find.value.min"_lang, ICON_VS_SYMBOL_NUMERIC, settings.inputMin)) edited = true;
|
|
|
|
if (ImGui::InputTextIcon("hex.builtin.view.find.value.max"_lang, ICON_VS_SYMBOL_NUMERIC, settings.inputMax)) edited = true;
|
2022-09-19 10:34:57 +02:00
|
|
|
|
|
|
|
const std::array<std::string, 10> InputTypes = {
|
|
|
|
"hex.builtin.common.type.u8"_lang,
|
|
|
|
"hex.builtin.common.type.u16"_lang,
|
|
|
|
"hex.builtin.common.type.u32"_lang,
|
|
|
|
"hex.builtin.common.type.u64"_lang,
|
|
|
|
"hex.builtin.common.type.i8"_lang,
|
|
|
|
"hex.builtin.common.type.i16"_lang,
|
|
|
|
"hex.builtin.common.type.i32"_lang,
|
|
|
|
"hex.builtin.common.type.i64"_lang,
|
|
|
|
"hex.builtin.common.type.f32"_lang,
|
|
|
|
"hex.builtin.common.type.f64"_lang
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ImGui::BeginCombo("hex.builtin.common.type"_lang, InputTypes[std::to_underlying(settings.type)].c_str())) {
|
|
|
|
for (size_t i = 0; i < InputTypes.size(); i++) {
|
|
|
|
auto type = static_cast<SearchSettings::Value::Type>(i);
|
|
|
|
|
|
|
|
if (ImGui::Selectable(InputTypes[i].c_str(), type == settings.type)) {
|
|
|
|
settings.type = type;
|
|
|
|
edited = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int selection = [&] {
|
|
|
|
switch (settings.endian) {
|
|
|
|
default:
|
|
|
|
case std::endian::little: return 0;
|
|
|
|
case std::endian::big: return 1;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
std::array options = { "hex.builtin.common.little"_lang, "hex.builtin.common.big"_lang };
|
|
|
|
if (ImGui::SliderInt("hex.builtin.common.endian"_lang, &selection, 0, options.size() - 1, options[selection], ImGuiSliderFlags_NoInput)) {
|
|
|
|
edited = true;
|
|
|
|
switch (selection) {
|
|
|
|
default:
|
|
|
|
case 0: settings.endian = std::endian::little; break;
|
|
|
|
case 1: settings.endian = std::endian::big; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edited) {
|
|
|
|
auto [minValid, min, minSize] = parseNumericValueInput(settings.inputMin, settings.type);
|
2023-01-21 18:44:12 +01:00
|
|
|
auto [maxValid, max, maxSize] = parseNumericValueInput(settings.inputMax, settings.type);
|
2022-09-19 10:34:57 +02:00
|
|
|
|
|
|
|
this->m_settingsValid = minValid && maxValid && minSize == maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
ImGui::BeginDisabled(!this->m_settingsValid);
|
|
|
|
{
|
|
|
|
if (ImGui::Button("hex.builtin.view.find.search"_lang)) {
|
|
|
|
this->runSearch();
|
|
|
|
|
|
|
|
this->m_decodeSettings = this->m_searchSettings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
2022-07-29 18:49:43 +02:00
|
|
|
ImGui::SameLine();
|
2022-08-03 11:38:36 +02:00
|
|
|
ImGui::TextFormatted("hex.builtin.view.find.search.entries"_lang, this->m_foundOccurrences[provider].size());
|
2022-09-13 14:22:18 +02:00
|
|
|
|
|
|
|
ImGui::BeginDisabled(this->m_foundOccurrences[provider].empty());
|
|
|
|
{
|
|
|
|
if (ImGui::Button("hex.builtin.view.find.search.reset"_lang)) {
|
|
|
|
this->m_foundOccurrences[provider].clear();
|
|
|
|
this->m_sortedOccurrences[provider].clear();
|
|
|
|
this->m_occurrenceTree[provider].clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
2022-08-03 11:38:36 +02:00
|
|
|
auto &currOccurrences = this->m_sortedOccurrences[provider];
|
2022-07-29 18:49:43 +02:00
|
|
|
|
2022-08-03 23:32:34 +02:00
|
|
|
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
|
2023-03-17 11:32:08 +01:00
|
|
|
auto prevFilterLength = this->m_currFilter[provider].length();
|
2022-09-03 23:15:30 +02:00
|
|
|
if (ImGui::InputTextWithHint("##filter", "hex.builtin.common.filter"_lang, this->m_currFilter[provider])) {
|
2023-03-17 11:32:08 +01:00
|
|
|
if (prevFilterLength > this->m_currFilter[provider].length())
|
|
|
|
this->m_sortedOccurrences[provider] = this->m_foundOccurrences[provider];
|
|
|
|
|
|
|
|
if (this->m_filterTask.isRunning())
|
|
|
|
this->m_filterTask.interrupt();
|
|
|
|
|
|
|
|
if (!this->m_currFilter[provider].empty()) {
|
|
|
|
this->m_filterTask = TaskManager::createTask("Filtering", 0, [this, provider, &currOccurrences](Task &task) {
|
|
|
|
currOccurrences.erase(std::remove_if(currOccurrences.begin(), currOccurrences.end(), [this, provider, &task](const auto ®ion) {
|
|
|
|
task.update();
|
|
|
|
return !hex::containsIgnoreCase(this->decodeValue(provider, region), this->m_currFilter[provider]);
|
|
|
|
}), currOccurrences.end());
|
|
|
|
});
|
|
|
|
}
|
2022-07-29 18:49:43 +02:00
|
|
|
}
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
if (ImGui::BeginTable("##entries", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.offset"_lang, 0, -1, ImGui::GetID("offset"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.size"_lang, 0, -1, ImGui::GetID("size"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.value"_lang, 0, -1, ImGui::GetID("value"));
|
|
|
|
|
|
|
|
auto sortSpecs = ImGui::TableGetSortSpecs();
|
|
|
|
|
|
|
|
if (sortSpecs->SpecsDirty) {
|
2022-08-03 11:38:36 +02:00
|
|
|
std::sort(currOccurrences.begin(), currOccurrences.end(), [this, &sortSpecs, provider](Occurrence &left, Occurrence &right) -> bool {
|
2022-07-29 13:59:57 +02:00
|
|
|
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("offset")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
2022-08-03 11:38:36 +02:00
|
|
|
return left.region.getStartAddress() > right.region.getStartAddress();
|
2022-07-29 13:59:57 +02:00
|
|
|
else
|
2022-08-03 11:38:36 +02:00
|
|
|
return left.region.getStartAddress() < right.region.getStartAddress();
|
2022-07-29 13:59:57 +02:00
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("size")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
2022-08-03 11:38:36 +02:00
|
|
|
return left.region.getSize() > right.region.getSize();
|
2022-07-29 13:59:57 +02:00
|
|
|
else
|
2022-08-03 11:38:36 +02:00
|
|
|
return left.region.getSize() < right.region.getSize();
|
2022-07-29 13:59:57 +02:00
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("value")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return this->decodeValue(provider, left) > this->decodeValue(provider, right);
|
|
|
|
else
|
|
|
|
return this->decodeValue(provider, left) < this->decodeValue(provider, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
sortSpecs->SpecsDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
|
|
|
ImGuiListClipper clipper;
|
2022-08-03 11:38:36 +02:00
|
|
|
clipper.Begin(currOccurrences.size(), ImGui::GetTextLineHeightWithSpacing());
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
while (clipper.Step()) {
|
2022-08-03 11:38:36 +02:00
|
|
|
for (size_t i = clipper.DisplayStart; i < std::min<size_t>(clipper.DisplayEnd, currOccurrences.size()); i++) {
|
|
|
|
auto &foundItem = currOccurrences[i];
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2022-08-03 11:38:36 +02:00
|
|
|
ImGui::TextFormatted("0x{:08X}", foundItem.region.getStartAddress());
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TableNextColumn();
|
2022-08-03 11:38:36 +02:00
|
|
|
ImGui::TextFormatted("{}", hex::toByteString(foundItem.region.getSize()));
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
ImGui::PushID(i);
|
|
|
|
|
|
|
|
auto value = this->decodeValue(provider, foundItem);
|
|
|
|
ImGui::TextFormatted("{}", value);
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Selectable("##line", false, ImGuiSelectableFlags_SpanAllColumns))
|
2022-08-03 11:38:36 +02:00
|
|
|
ImHexApi::HexEditor::setSelection(foundItem.region.getStartAddress(), foundItem.region.getSize());
|
2022-07-29 13:59:57 +02:00
|
|
|
drawContextMenu(value);
|
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clipper.End();
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|