2022-07-29 13:59:57 +02:00
|
|
|
#include "content/views/view_find.hpp"
|
|
|
|
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
#include <hex/api/achievement_manager.hpp>
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
#include <hex/providers/buffered_reader.hpp>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <llvm/Demangle/Demangle.h>
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
ViewFind::ViewFind() : View("hex.builtin.view.find.name") {
|
2023-11-16 22:24:06 +01:00
|
|
|
const static auto HighlightColor = [] { return (ImGuiExt::GetCustomColorU32(ImGuiCustomCol_FindHighlight) & 0x00FFFFFF) | 0x70000000; };
|
2022-07-29 13:59:57 +02:00
|
|
|
|
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 { };
|
|
|
|
|
2023-05-22 00:01:58 +02:00
|
|
|
if (!this->m_occurrenceTree->overlapping({ address, address + size }).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;
|
|
|
|
|
2023-05-22 00:01:58 +02:00
|
|
|
auto occurrences = this->m_occurrenceTree->overlapping({ address, address + size });
|
2023-05-11 09:27:23 +02:00
|
|
|
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
|
|
|
{
|
2023-05-11 09:27:23 +02:00
|
|
|
auto region = occurrence.value.region;
|
|
|
|
const auto value = this->decodeValue(ImHexApi::Provider::get(), occurrence.value, 256);
|
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);
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::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();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}: ", "hex.builtin.common.region"_lang);
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("[ 0x{:08X} - 0x{:08X} ]", region.getStartAddress(), 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();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}: ", "hex.builtin.view.find.demangled"_lang);
|
2022-08-06 12:57:47 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}", demangledValue);
|
2022-08-06 12:57:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2023-05-28 11:50:10 +02:00
|
|
|
|
|
|
|
ShortcutManager::addShortcut(this, CTRLCMD + Keys::A, [this] {
|
|
|
|
if (this->m_filterTask.isRunning())
|
|
|
|
return;
|
|
|
|
if (this->m_searchTask.isRunning())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto &occurrence : *this->m_sortedOccurrences)
|
|
|
|
occurrence.selected = true;
|
|
|
|
});
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
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>)
|
2023-05-28 14:30:33 +02:00
|
|
|
value = hex::signExtend(bytes.size() * 8, value);
|
2022-09-19 10:34:57 +02:00
|
|
|
|
|
|
|
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)) {
|
2023-05-28 11:50:10 +02:00
|
|
|
if (!settings.nullTermination || byte == 0x00) {
|
|
|
|
results.push_back(Occurrence { Region { startAddress, countedCharacters }, decodeType, endian, false });
|
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);
|
2023-05-28 11:50:10 +02:00
|
|
|
results.push_back(Occurrence{ Region { address, bytes.size() }, Occurrence::DecodeType::Binary, std::endian::native, false });
|
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());
|
|
|
|
|
2023-05-22 00:01:58 +02:00
|
|
|
const size_t patternSize = settings.pattern.getSize();
|
2022-08-06 12:57:47 +02:00
|
|
|
|
2023-04-09 15:28:31 +02:00
|
|
|
if (settings.alignment == 1) {
|
2023-04-08 20:59:33 +02:00
|
|
|
u32 matchedBytes = 0;
|
|
|
|
for (auto it = reader.begin(); it < reader.end(); it += 1) {
|
|
|
|
auto byte = *it;
|
2022-08-06 12:57:47 +02:00
|
|
|
|
2023-04-08 20:59:33 +02:00
|
|
|
task.update(it.getAddress());
|
2023-05-22 00:01:58 +02:00
|
|
|
if (settings.pattern.matchesByte(byte, matchedBytes)) {
|
2023-04-08 20:59:33 +02:00
|
|
|
matchedBytes++;
|
2023-05-22 00:01:58 +02:00
|
|
|
if (matchedBytes == settings.pattern.getSize()) {
|
2023-04-08 20:59:33 +02:00
|
|
|
auto occurrenceAddress = it.getAddress() - (patternSize - 1);
|
|
|
|
|
2023-05-28 11:50:10 +02:00
|
|
|
results.push_back(Occurrence { Region { occurrenceAddress, patternSize }, Occurrence::DecodeType::Binary, std::endian::native, false });
|
2023-04-08 20:59:33 +02:00
|
|
|
it.setAddress(occurrenceAddress);
|
|
|
|
matchedBytes = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (matchedBytes > 0)
|
|
|
|
it -= matchedBytes;
|
2022-08-03 10:19:34 +02:00
|
|
|
matchedBytes = 0;
|
|
|
|
}
|
2023-04-08 20:59:33 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::vector<u8> data(patternSize);
|
2023-04-09 15:28:31 +02:00
|
|
|
for (u64 address = searchRegion.getStartAddress(); address < searchRegion.getEndAddress(); address += settings.alignment) {
|
2023-04-08 20:59:33 +02:00
|
|
|
reader.read(address, data.data(), data.size());
|
|
|
|
|
|
|
|
task.update(address);
|
|
|
|
|
|
|
|
bool match = true;
|
|
|
|
for (u32 i = 0; i < patternSize; i++) {
|
2023-10-22 20:59:32 +02:00
|
|
|
if (!settings.pattern.matchesByte(data[i], i)) {
|
2023-04-08 20:59:33 +02:00
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match)
|
2023-05-28 11:50:10 +02:00
|
|
|
results.push_back(Occurrence { Region { address, patternSize }, Occurrence::DecodeType::Binary, std::endian::native, false });
|
2022-08-03 10:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2023-04-08 20:59:33 +02:00
|
|
|
auto inputMin = settings.inputMin;
|
|
|
|
auto inputMax = settings.inputMax;
|
|
|
|
|
|
|
|
if (inputMax.empty())
|
|
|
|
inputMax = inputMin;
|
|
|
|
|
|
|
|
const auto [validMin, min, sizeMin] = parseNumericValueInput(inputMin, settings.type);
|
|
|
|
const auto [validMax, max, sizeMax] = parseNumericValueInput(inputMax, settings.type);
|
2022-09-19 10:34:57 +02:00
|
|
|
|
|
|
|
if (!validMin || !validMax || sizeMin != sizeMax)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
const auto size = sizeMin;
|
|
|
|
|
2023-04-08 20:59:33 +02:00
|
|
|
const auto advance = settings.aligned ? size : 1;
|
|
|
|
|
|
|
|
for (u64 address = searchRegion.getStartAddress(); address < searchRegion.getEndAddress(); address += advance) {
|
|
|
|
task.update(address);
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
auto result = std::visit([&]<typename T>(T) {
|
|
|
|
using DecayedType = std::remove_cvref_t<std::decay_t<T>>;
|
2023-04-08 20:59:33 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
auto minValue = std::get<DecayedType>(min);
|
|
|
|
auto maxValue = std::get<DecayedType>(max);
|
2023-04-08 20:59:33 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
DecayedType value = 0;
|
2023-04-08 20:59:33 +02:00
|
|
|
reader.read(address, reinterpret_cast<u8*>(&value), size);
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-28 11:50:10 +02:00
|
|
|
results.push_back(Occurrence { Region { address, size }, decodeType, settings.endian, false });
|
2022-09-19 10:34:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
void ViewFind::runSearch() {
|
2023-04-10 14:08:21 +02:00
|
|
|
Region searchRegion = this->m_searchSettings.region;
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2023-08-06 21:33:15 +02:00
|
|
|
if (this->m_searchSettings.mode == SearchSettings::Mode::Strings)
|
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.find", "hex.builtin.achievement.find.find_strings.name");
|
|
|
|
else if (this->m_searchSettings.mode == SearchSettings::Mode::Sequence)
|
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.find", "hex.builtin.achievement.find.find_specific_string.name");
|
|
|
|
else if (this->m_searchSettings.mode == SearchSettings::Mode::Value) {
|
|
|
|
if (this->m_searchSettings.value.inputMin == "250" && this->m_searchSettings.value.inputMax == "1000")
|
2023-09-12 22:24:12 +02:00
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.find", "hex.builtin.achievement.find.find_numeric.name");
|
2023-08-06 21:33:15 +02:00
|
|
|
}
|
|
|
|
|
2023-08-09 20:01:57 +02:00
|
|
|
this->m_occurrenceTree->clear();
|
2023-09-12 22:23:47 +02:00
|
|
|
EventManager::post<EventHighlightingChanged>();
|
2023-08-06 21:33:15 +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:
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_foundOccurrences.get(provider) = searchStrings(task, provider, searchRegion, settings.strings);
|
2022-07-29 13:59:57 +02:00
|
|
|
break;
|
|
|
|
case Sequence:
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_foundOccurrences.get(provider) = searchSequence(task, provider, searchRegion, settings.bytes);
|
2022-07-29 13:59:57 +02:00
|
|
|
break;
|
|
|
|
case Regex:
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_foundOccurrences.get(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:
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_foundOccurrences.get(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:
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_foundOccurrences.get(provider) = searchValue(task, provider, searchRegion, settings.value);
|
2022-09-19 10:34:57 +02:00
|
|
|
break;
|
2022-08-17 16:15:36 +02:00
|
|
|
}
|
2022-07-29 13:59:57 +02:00
|
|
|
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_sortedOccurrences.get(provider) = this->m_foundOccurrences.get(provider);
|
2022-08-06 12:57:47 +02:00
|
|
|
|
2023-05-07 23:27:43 +02:00
|
|
|
for (const auto &occurrence : this->m_foundOccurrences.get(provider))
|
2023-05-11 09:27:23 +02:00
|
|
|
this->m_occurrenceTree->insert({ occurrence.region.getStartAddress(), occurrence.region.getEndAddress() }, occurrence);
|
2023-09-12 22:23:47 +02:00
|
|
|
|
|
|
|
TaskManager::doLater([] {
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
});
|
2022-08-17 16:15:36 +02:00
|
|
|
});
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::string ViewFind::decodeValue(prv::Provider *provider, const Occurrence &occurrence, size_t maxBytes) const {
|
2023-04-01 11:04:07 +02:00
|
|
|
std::vector<u8> bytes(std::min<size_t>(occurrence.region.getSize(), maxBytes));
|
2022-08-03 11:38:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-01 11:04:07 +02:00
|
|
|
if (occurrence.region.getSize() > maxBytes)
|
|
|
|
result += "...";
|
|
|
|
|
2022-07-29 13:59:57 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 11:50:10 +02:00
|
|
|
void ViewFind::drawContextMenu(Occurrence &target, const std::string &value) {
|
2022-07-29 13:59:57 +02:00
|
|
|
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right) && ImGui::IsItemHovered()) {
|
|
|
|
ImGui::OpenPopup("FindContextMenu");
|
2023-05-28 11:50:10 +02:00
|
|
|
target.selected = true;
|
|
|
|
this->m_replaceBuffer.clear();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2023-05-28 11:50:10 +02:00
|
|
|
if (ImGui::BeginMenu("hex.builtin.view.find.context.replace"_lang)) {
|
|
|
|
if (ImGui::BeginTabBar("##replace_tabs")) {
|
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.context.replace.hex"_lang)) {
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InputTextIcon("##replace_input", ICON_VS_SYMBOL_NAMESPACE, this->m_replaceBuffer);
|
2023-05-28 11:50:10 +02:00
|
|
|
|
|
|
|
ImGui::BeginDisabled(this->m_replaceBuffer.empty());
|
|
|
|
if (ImGui::Button("hex.builtin.view.find.context.replace"_lang)) {
|
2023-05-28 14:30:33 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
auto bytes = parseHexString(this->m_replaceBuffer);
|
|
|
|
|
2023-05-28 11:50:10 +02:00
|
|
|
for (const auto &occurrence : *this->m_sortedOccurrences) {
|
|
|
|
if (occurrence.selected) {
|
|
|
|
size_t size = std::min<size_t>(occurrence.region.size, bytes.size());
|
|
|
|
provider->write(occurrence.region.getStartAddress(), bytes.data(), size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginTabItem("hex.builtin.view.find.context.replace.ascii"_lang)) {
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InputTextIcon("##replace_input", ICON_VS_SYMBOL_KEY, this->m_replaceBuffer);
|
2023-05-28 11:50:10 +02:00
|
|
|
|
|
|
|
ImGui::BeginDisabled(this->m_replaceBuffer.empty());
|
|
|
|
if (ImGui::Button("hex.builtin.view.find.context.replace"_lang)) {
|
2023-05-28 14:30:33 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
auto bytes = decodeByteString(this->m_replaceBuffer);
|
|
|
|
|
2023-05-28 11:50:10 +02:00
|
|
|
for (const auto &occurrence : *this->m_sortedOccurrences) {
|
|
|
|
if (occurrence.selected) {
|
2023-05-28 14:30:33 +02:00
|
|
|
size_t size = std::min<size_t>(occurrence.region.size, bytes.size());
|
|
|
|
provider->write(occurrence.region.getStartAddress(), bytes.data(), size);
|
2023-05-28 11:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2023-04-10 14:08:21 +02:00
|
|
|
ui::regionSelectionPicker(&this->m_searchSettings.region, provider, &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);
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::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;
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::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();
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::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;
|
2023-11-10 20:47:08 +01:00
|
|
|
} catch (const std::regex_error &) {
|
2022-07-29 13:59:57 +02:00
|
|
|
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;
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InputTextIcon("hex.builtin.view.find.binary_pattern"_lang, ICON_VS_SYMBOL_NAMESPACE, settings.input);
|
2023-04-09 15:28:31 +02:00
|
|
|
|
|
|
|
constexpr static u32 min = 1, max = 0x1000;
|
|
|
|
ImGui::SliderScalar("hex.builtin.view.find.binary_pattern.alignment"_lang, ImGuiDataType_U32, &settings.alignment, &min, &max);
|
2022-08-03 10:19:34 +02:00
|
|
|
|
2023-05-22 00:01:58 +02:00
|
|
|
settings.pattern = hex::BinaryPattern(settings.input);
|
|
|
|
this->m_settingsValid = settings.pattern.isValid() && settings.alignment > 0;
|
2022-08-03 10:19:34 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-04-08 20:59:33 +02:00
|
|
|
if (settings.range) {
|
2023-11-16 22:24:06 +01:00
|
|
|
if (ImGuiExt::InputTextIcon("hex.builtin.view.find.value.min"_lang, ICON_VS_SYMBOL_NUMERIC, settings.inputMin)) edited = true;
|
|
|
|
if (ImGuiExt::InputTextIcon("hex.builtin.view.find.value.max"_lang, ICON_VS_SYMBOL_NUMERIC, settings.inputMax)) edited = true;
|
2023-04-08 20:59:33 +02:00
|
|
|
} else {
|
2023-11-16 22:24:06 +01:00
|
|
|
if (ImGuiExt::InputTextIcon("hex.builtin.common.value"_lang, ICON_VS_SYMBOL_NUMERIC, settings.inputMin)) {
|
2023-04-08 20:59:33 +02:00
|
|
|
edited = true;
|
|
|
|
settings.inputMax = settings.inputMin;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::BeginDisabled();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InputTextIcon("##placeholder_value", ICON_VS_SYMBOL_NUMERIC, settings.inputMax);
|
2023-04-08 20:59:33 +02:00
|
|
|
ImGui::EndDisabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Checkbox("hex.builtin.view.find.value.range"_lang, &settings.range);
|
|
|
|
ImGui::NewLine();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 15:28:31 +02:00
|
|
|
ImGui::Checkbox("hex.builtin.view.find.value.aligned"_lang, &settings.aligned);
|
2023-04-08 20:59:33 +02:00
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-13 17:51:16 +02:00
|
|
|
if (settings.inputMin.empty())
|
|
|
|
this->m_settingsValid = false;
|
|
|
|
|
2022-09-19 10:34:57 +02:00
|
|
|
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();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("hex.builtin.view.find.search.entries"_lang, this->m_foundOccurrences->size());
|
2022-09-13 14:22:18 +02:00
|
|
|
|
2023-05-07 23:27:43 +02:00
|
|
|
ImGui::BeginDisabled(this->m_foundOccurrences->empty());
|
2022-09-13 14:22:18 +02:00
|
|
|
{
|
|
|
|
if (ImGui::Button("hex.builtin.view.find.search.reset"_lang)) {
|
2023-05-07 23:27:43 +02:00
|
|
|
this->m_foundOccurrences->clear();
|
|
|
|
this->m_sortedOccurrences->clear();
|
2023-09-12 22:23:47 +02:00
|
|
|
this->m_occurrenceTree->clear();
|
|
|
|
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
2022-09-13 14:22:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
2022-07-29 13:59:57 +02:00
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
2023-05-07 23:27:43 +02:00
|
|
|
auto &currOccurrences = *this->m_sortedOccurrences;
|
2022-07-29 18:49:43 +02:00
|
|
|
|
2023-06-04 22:52:36 +02:00
|
|
|
ImGui::PushItemWidth(-1);
|
2023-05-07 23:27:43 +02:00
|
|
|
auto prevFilterLength = this->m_currFilter->length();
|
2023-11-16 22:24:06 +01:00
|
|
|
if (ImGuiExt::InputTextIcon("##filter", ICON_VS_FILTER, *this->m_currFilter)) {
|
2023-05-07 23:27:43 +02:00
|
|
|
if (prevFilterLength > this->m_currFilter->length())
|
|
|
|
*this->m_sortedOccurrences = *this->m_foundOccurrences;
|
2023-03-17 11:32:08 +01:00
|
|
|
|
|
|
|
if (this->m_filterTask.isRunning())
|
|
|
|
this->m_filterTask.interrupt();
|
|
|
|
|
2023-05-07 23:27:43 +02:00
|
|
|
if (!this->m_currFilter->empty()) {
|
2023-04-08 20:59:33 +02:00
|
|
|
this->m_filterTask = TaskManager::createTask("Filtering", currOccurrences.size(), [this, provider, &currOccurrences](Task &task) {
|
|
|
|
u64 progress = 0;
|
2023-11-10 20:47:08 +01:00
|
|
|
std::erase_if(currOccurrences, [this, provider, &task, &progress](const auto ®ion) {
|
2023-04-08 20:59:33 +02:00
|
|
|
task.update(progress);
|
|
|
|
progress += 1;
|
|
|
|
|
2023-05-07 23:27:43 +02:00
|
|
|
return !hex::containsIgnoreCase(this->decodeValue(provider, region), this->m_currFilter.get(provider));
|
2023-11-10 20:47:08 +01:00
|
|
|
});
|
2023-03-17 11:32:08 +01:00
|
|
|
});
|
|
|
|
}
|
2022-07-29 18:49:43 +02:00
|
|
|
}
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
2023-04-01 11:04:07 +02:00
|
|
|
if (ImGui::BeginTable("##entries", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
2022-07-29 13:59:57 +02:00
|
|
|
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) {
|
2023-11-10 20:47:08 +01:00
|
|
|
std::sort(currOccurrences.begin(), currOccurrences.end(), [this, &sortSpecs, provider](const Occurrence &left, const 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();
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("0x{:08X}", foundItem.region.getStartAddress());
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}", hex::toByteString(foundItem.region.getSize()));
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
ImGui::PushID(i);
|
|
|
|
|
2023-04-01 11:04:07 +02:00
|
|
|
auto value = this->decodeValue(provider, foundItem, 256);
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}", value);
|
2022-07-29 13:59:57 +02:00
|
|
|
ImGui::SameLine();
|
2023-05-28 11:50:10 +02:00
|
|
|
if (ImGui::Selectable("##line", foundItem.selected, ImGuiSelectableFlags_SpanAllColumns)) {
|
|
|
|
if (ImGui::GetIO().KeyCtrl) {
|
|
|
|
foundItem.selected = !foundItem.selected;
|
|
|
|
} else {
|
|
|
|
for (auto &occurrence : *this->m_sortedOccurrences)
|
|
|
|
occurrence.selected = false;
|
|
|
|
foundItem.selected = true;
|
|
|
|
ImHexApi::HexEditor::setSelection(foundItem.region.getStartAddress(), foundItem.region.getSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drawContextMenu(foundItem, value);
|
2022-07-29 13:59:57 +02:00
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clipper.End();
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|