#include "views/view_hashes.hpp" #include "providers/provider.hpp" #include "utils.hpp" #include namespace hex { ViewHashes::ViewHashes(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { View::subscribeEvent(Events::DataChanged, [this](void*){ this->m_shouldInvalidate = true; }); } ViewHashes::~ViewHashes() { View::unsubscribeEvent(Events::DataChanged); } void ViewHashes::createView() { if (!this->m_windowOpen) return; if (ImGui::Begin("Hashing", &this->m_windowOpen)) { ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav); ImGui::NewLine(); if (this->m_dataProvider != nullptr && this->m_dataProvider->isAvailable()) { ImGui::Combo("Hash Function", &this->m_currHashFunction, HashFunctionNames,sizeof(HashFunctionNames) / sizeof(const char *)); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; ImGui::NewLine(); ImGui::Separator(); ImGui::NewLine(); ImGui::InputInt("Begin", &this->m_hashStart, 0, 0, ImGuiInputTextFlags_CharsHexadecimal); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; ImGui::InputInt("End", &this->m_hashEnd, 0, 0, ImGuiInputTextFlags_CharsHexadecimal); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; size_t dataSize = this->m_dataProvider->getSize(); if (this->m_hashEnd >= dataSize) this->m_hashEnd = dataSize - 1; ImGui::NewLine(); ImGui::Separator(); ImGui::NewLine(); if (this->m_hashEnd >= this->m_hashStart) { std::vector buffer; if (this->m_shouldInvalidate) { buffer = std::vector(this->m_hashEnd - this->m_hashStart + 1, 0x00); this->m_dataProvider->read(this->m_hashStart, buffer.data(), buffer.size()); } switch (this->m_currHashFunction) { case 0: // CRC16 { static int polynomial = 0, init = 0; ImGui::InputInt("Initial Value", &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; ImGui::InputInt("Polynomial", &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; ImGui::NewLine(); ImGui::Separator(); ImGui::NewLine(); static u16 result = 0; if (this->m_shouldInvalidate) result = crc16(buffer.data(), buffer.size(), polynomial, init); ImGui::LabelText("##nolabel", "%X", result); } break; case 1: // CRC32 { static int polynomial = 0, init = 0; ImGui::InputInt("Initial Value", &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; ImGui::InputInt("Polynomial", &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal); if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true; ImGui::NewLine(); ImGui::Separator(); ImGui::NewLine(); static u32 result = 0; if (this->m_shouldInvalidate) result = crc32(buffer.data(), buffer.size(), polynomial, init); ImGui::LabelText("##nolabel", "%X", result); } break; case 2: // MD5 { static std::array result; if (this->m_shouldInvalidate) result = md5(buffer.data(), buffer.size()); ImGui::LabelText("##nolabel", "%08X%08X%08X%08X", __builtin_bswap32(result[0]), __builtin_bswap32(result[1]), __builtin_bswap32(result[2]), __builtin_bswap32(result[3])); } break; } } this->m_shouldInvalidate = false; } ImGui::EndChild(); } ImGui::End(); } void ViewHashes::createMenu() { if (ImGui::BeginMenu("View")) { ImGui::MenuItem("Hash View", "", &this->m_windowOpen); ImGui::EndMenu(); } } }