ui/ux: Complete rewrite of the Hash view

This commit is contained in:
WerWolv
2022-05-30 16:36:46 +02:00
parent 05862ae991
commit fe6be686b7
15 changed files with 457 additions and 343 deletions

View File

@@ -61,7 +61,7 @@ namespace hex::plugin::builtin {
ImGui::BeginTooltip();
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
ImGui::TableNextRow();
ImGui::TableNextColumn();

View File

@@ -1,294 +1,180 @@
#include "content/views/view_hashes.hpp"
#include <hex/providers/provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <vector>
namespace hex::plugin::builtin {
ViewHashes::ViewHashes() : View("hex.builtin.view.hashes.name") {
EventManager::subscribe<EventDataChanged>(this, [this]() {
this->m_shouldInvalidate = true;
EventManager::subscribe<EventRegionSelected>(this, [this](const Region &) {
for (auto &function : this->m_hashFunctions)
function.reset();
});
EventManager::subscribe<EventRegionSelected>(this, [this](Region region) {
if (this->m_shouldMatchSelection) {
if (region.address == size_t(-1)) {
this->m_hashRegion[0] = this->m_hashRegion[1] = 0;
} else {
this->m_hashRegion[0] = region.address;
this->m_hashRegion[1] = region.size;
ImHexApi::HexEditor::addTooltipProvider([this](u64 address, const u8 *data, size_t size) {
hex::unused(data);
auto selection = ImHexApi::HexEditor::getSelection();
if (ImGui::GetIO().KeyShift) {
if (!this->m_hashFunctions.empty() && selection.has_value() && selection->overlaps(Region { address, size })) {
ImGui::BeginTooltip();
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted("hex.builtin.view.hashes.name"_lang);
ImGui::Separator();
ImGui::Indent();
if (ImGui::BeginTable("##hashes_tooltip", 3, ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) {
auto provider = ImHexApi::Provider::get();
for (auto &function : this->m_hashFunctions) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", function.getName());
ImGui::TableNextColumn();
ImGui::TextFormatted(" ");
ImGui::TableNextColumn();
if (provider != nullptr)
ImGui::TextFormatted("{}", crypt::encode16(function.get(*selection, provider)));
}
ImGui::EndTable();
}
ImGui::Unindent();
ImGui::EndTable();
}
ImGui::EndTooltip();
}
this->m_shouldInvalidate = true;
}
});
}
ViewHashes::~ViewHashes() {
EventManager::unsubscribe<EventDataChanged>(this);
EventManager::unsubscribe<EventRegionSelected>(this);
}
template<size_t Size>
static void formatBigHexInt(std::array<u8, Size> dataArray, char *buffer, size_t bufferSize) {
for (size_t i = 0; i < dataArray.size(); i++)
snprintf(buffer + 2 * i, bufferSize - 2 * i, "%02X", dataArray[i]);
}
void ViewHashes::drawContent() {
const auto &hashes = ContentRegistry::Hashes::impl::getHashes();
if (this->m_selectedHash == nullptr && !hashes.empty()) {
this->m_selectedHash = hashes.front();
}
if (ImGui::Begin(View::toWindowName("hex.builtin.view.hashes.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
if (ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav)) {
if (ImGui::BeginCombo("hex.builtin.view.hashes.function"_lang, this->m_selectedHash != nullptr ? this->m_selectedHash->getName().c_str() : "")) {
auto provider = ImHexApi::Provider::get();
if (ImHexApi::Provider::isValid() && provider->isAvailable()) {
ImGui::TextUnformatted("hex.builtin.common.region"_lang);
ImGui::Separator();
ImGui::InputScalarN("##nolabel", ImGuiDataType_U64, this->m_hashRegion, 2, nullptr, nullptr, "%08X", ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.match_selection"_lang, &this->m_shouldMatchSelection);
if (ImGui::IsItemEdited()) {
// Force execution of Region Selection Event
ImHexApi::HexEditor::setSelection(0, 0);
this->m_shouldInvalidate = true;
}
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.settings"_lang);
ImGui::Separator();
if (ImGui::BeginCombo("hex.builtin.view.hashes.function"_lang, hashFunctionNames[this->m_currHashFunction].second, 0)) {
for (size_t i = 0; i < hashFunctionNames.size(); i++) {
bool is_selected = (static_cast<size_t>(this->m_currHashFunction) == i);
if (ImGui::Selectable(hashFunctionNames[i].second, is_selected))
this->m_currHashFunction = i;
if (is_selected)
ImGui::SetItemDefaultFocus(); // Set the initial focus when opening the combo (scrolling + for keyboard navigation support in the upcoming navigation branch)
}
ImGui::EndCombo();
this->m_shouldInvalidate = true;
}
size_t dataSize = provider->getSize();
if (this->m_hashRegion[1] >= provider->getBaseAddress() + dataSize)
this->m_hashRegion[1] = provider->getBaseAddress() + dataSize;
switch (hashFunctionNames[this->m_currHashFunction].first) {
case HashFunctions::Crc8:
{
static int polynomial = 0x07, init = 0x0000, xorout = 0x0000;
static bool reflectIn = false, reflectOut = false;
ImGui::InputInt("hex.builtin.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::InputInt("hex.builtin.view.hashes.xorout"_lang, &xorout, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.reflectIn"_lang, &reflectIn);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.reflectOut"_lang, &reflectOut);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::InputInt("hex.builtin.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::NewLine();
static u8 result = 0;
if (this->m_shouldInvalidate)
result = crypt::crc8(provider, this->m_hashRegion[0], this->m_hashRegion[1], polynomial, init, xorout, reflectIn, reflectOut);
char buffer[sizeof(result) * 2 + 1];
snprintf(buffer, sizeof(buffer), "%02X", result);
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Crc16:
{
static int polynomial = 0x8005, init = 0x0000, xorout = 0x0000;
static bool reflectIn = false, reflectOut = false;
ImGui::InputInt("hex.builtin.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::InputInt("hex.builtin.view.hashes.xorout"_lang, &xorout, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.reflectIn"_lang, &reflectIn);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.reflectOut"_lang, &reflectOut);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::InputInt("hex.builtin.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::NewLine();
static u16 result = 0;
if (this->m_shouldInvalidate)
result = crypt::crc16(provider, this->m_hashRegion[0], this->m_hashRegion[1], polynomial, init, xorout, reflectIn, reflectOut);
char buffer[sizeof(result) * 2 + 1];
snprintf(buffer, sizeof(buffer), "%04X", result);
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Crc32:
{
static int polynomial = 0x04C11DB7, init = 0xFFFFFFFF, xorout = 0xFFFFFFFF;
static bool reflectIn = true, reflectOut = true;
ImGui::InputInt("hex.builtin.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::InputInt("hex.builtin.view.hashes.xorout"_lang, &xorout, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.reflectIn"_lang, &reflectIn);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::Checkbox("hex.builtin.common.reflectOut"_lang, &reflectOut);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::InputInt("hex.builtin.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
ImGui::NewLine();
static u32 result = 0;
if (this->m_shouldInvalidate)
result = crypt::crc32(provider, this->m_hashRegion[0], this->m_hashRegion[1], polynomial, init, xorout, reflectIn, reflectOut);
char buffer[sizeof(result) * 2 + 1];
snprintf(buffer, sizeof(buffer), "%08X", result);
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Md5:
{
static std::array<u8, 16> result = { 0 };
if (this->m_shouldInvalidate)
result = crypt::md5(provider, this->m_hashRegion[0], this->m_hashRegion[1]);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Sha1:
{
static std::array<u8, 20> result = { 0 };
if (this->m_shouldInvalidate)
result = crypt::sha1(provider, this->m_hashRegion[0], this->m_hashRegion[1]);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Sha224:
{
static std::array<u8, 28> result = { 0 };
if (this->m_shouldInvalidate)
result = crypt::sha224(provider, this->m_hashRegion[0], this->m_hashRegion[1]);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Sha256:
{
static std::array<u8, 32> result;
if (this->m_shouldInvalidate)
result = crypt::sha256(provider, this->m_hashRegion[0], this->m_hashRegion[1]);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Sha384:
{
static std::array<u8, 48> result;
if (this->m_shouldInvalidate)
result = crypt::sha384(provider, this->m_hashRegion[0], this->m_hashRegion[1]);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case HashFunctions::Sha512:
{
static std::array<u8, 64> result;
if (this->m_shouldInvalidate)
result = crypt::sha512(provider, this->m_hashRegion[0], this->m_hashRegion[1]);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.hashes.result"_lang);
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
for (const auto hash : hashes) {
if (ImGui::Selectable(hash->getName().c_str(), this->m_selectedHash == hash)) {
this->m_selectedHash = hash;
this->m_newHashName.clear();
}
}
this->m_shouldInvalidate = false;
ImGui::EndCombo();
}
if (this->m_newHashName.empty() && this->m_selectedHash != nullptr)
this->m_newHashName = hex::format("{} {}", this->m_selectedHash->getName(), static_cast<const char *>("hex.builtin.view.hashes.hash"_lang));
if (ImGui::BeginChild("##settings", ImVec2(ImGui::GetContentRegionAvailWidth(), 200_scaled), true)) {
if (this->m_selectedHash != nullptr) {
auto startPos = ImGui::GetCursorPosY();
this->m_selectedHash->draw();
// Check if no elements have been added
if (startPos == ImGui::GetCursorPosY()) {
ImGui::TextFormattedCentered("hex.builtin.view.hashes.no_settings"_lang);
}
}
}
ImGui::EndChild();
ImGui::NewLine();
ImGui::InputText("##Name", this->m_newHashName);
ImGui::SameLine();
ImGui::BeginDisabled(this->m_newHashName.empty() || this->m_selectedHash == nullptr);
if (ImGui::IconButton(ICON_VS_ADD, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
if (this->m_selectedHash != nullptr)
this->m_hashFunctions.push_back(this->m_selectedHash->create(this->m_newHashName));
}
ImGui::EndDisabled();
if (ImGui::BeginTable("##hashes", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Borders, ImVec2(ImGui::GetContentRegionAvailWidth(), ImGui::GetTextLineHeightWithSpacing() * 10))) {
ImGui::TableSetupColumn("hex.builtin.view.hashes.name"_lang);
ImGui::TableSetupColumn("hex.builtin.view.hashes.type"_lang);
ImGui::TableSetupColumn("hex.builtin.view.hashes.result"_lang, ImGuiTableColumnFlags_WidthStretch);
ImGui::TableHeadersRow();
auto provider = ImHexApi::Provider::get();
auto selection = ImHexApi::HexEditor::getSelection();
std::optional<u32> indexToRemove;
for (u32 i = 0; i < this->m_hashFunctions.size(); i++) {
auto &function = this->m_hashFunctions[i];
ImGui::PushID(i);
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::PushStyleColor(ImGuiCol_Header, 0x00);
ImGui::PushStyleColor(ImGuiCol_HeaderActive, 0x00);
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, 0x00);
ImGui::Selectable(function.getName().c_str(), false);
ImGui::PopStyleColor(3);
{
const auto ContextMenuId = hex::format("Context Menu {}", i);
if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right))
ImGui::OpenPopup(ContextMenuId.c_str());
if (ImGui::BeginPopup(ContextMenuId.c_str())) {
if (ImGui::MenuItem("hex.builtin.view.hashes.remove"_lang))
indexToRemove = i;
ImGui::EndPopup();
}
}
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", function.getType()->getName());
ImGui::TableNextColumn();
std::string result;
if (provider != nullptr && selection.has_value())
result = crypt::encode16(function.get(*selection, provider));
else
result = "???";
ImGui::PushItemWidth(ImGui::GetContentRegionAvailWidth());
ImGui::InputText("##result", result, ImGuiInputTextFlags_ReadOnly);
ImGui::PopItemWidth();
ImGui::PopID();
}
if (indexToRemove.has_value()) {
this->m_hashFunctions.erase(this->m_hashFunctions.begin() + indexToRemove.value());
}
ImGui::EndTable();
}
ImGui::NewLine();
ImGui::TextWrapped("%s", static_cast<const char *>("hex.builtin.view.hashes.hover_info"_lang));
}
ImGui::End();
}

View File

@@ -439,7 +439,7 @@ namespace hex::plugin::builtin {
ImGui::BeginTooltip();
for (const auto &[id, tooltip] : tooltips) {
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
ImGui::TableNextRow();
ImGui::TableNextColumn();

View File

@@ -254,7 +254,7 @@ namespace hex::plugin::builtin {
if (child != nullptr) {
ImGui::BeginTooltip();
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
ImGui::TableNextRow();
ImGui::TableNextColumn();