From 0dcf02f8915a51535b3a67eeef4872fb8aa96592 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 13 Nov 2020 12:07:05 +0100 Subject: [PATCH] Actually display signed and floating point data in the right format --- include/views/highlight.hpp | 11 +++++++--- include/views/view_pattern.hpp | 2 +- source/views/view_hexeditor.cpp | 6 +++--- source/views/view_pattern.cpp | 22 +++++++++++++++----- source/views/view_pattern_data.cpp | 32 +++++++++++++++++++++--------- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/include/views/highlight.hpp b/include/views/highlight.hpp index 3c21fffb5..f6a886f3a 100644 --- a/include/views/highlight.hpp +++ b/include/views/highlight.hpp @@ -5,14 +5,19 @@ namespace hex { + struct VariableType { + size_t size; + enum class Kind { Unsigned, Signed, FloatingPoint } kind; + }; + struct Highlight { - Highlight(u64 offset, size_t size, u32 color, std::string name) - : offset(offset), size(size), color(color), name(name) { + Highlight(u64 offset, VariableType type, u32 color, std::string name) + : offset(offset), type(type), color(color), name(name) { } u64 offset; - size_t size; + VariableType type; u32 color; std::string name; }; diff --git a/include/views/view_pattern.hpp b/include/views/view_pattern.hpp index 09af47eed..aeeac61b5 100644 --- a/include/views/view_pattern.hpp +++ b/include/views/view_pattern.hpp @@ -31,7 +31,7 @@ namespace hex { ImGui::FileBrowser m_fileBrowser; - void setHighlight(u64 offset, size_t size, std::string name, u32 color = 0); + void setHighlight(u64 offset, std::string name, lang::Token::TypeToken::Type type, size_t size = 0, u32 color = 0); void parsePattern(char *buffer); s32 highlightUsingDecls(std::vector &ast, lang::ASTNodeTypeDecl* currTypeDeclNode, lang::ASTNodeVariableDecl* currVarDec, u64 offset, std::string name); diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 1d3434a65..826265ad2 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -35,12 +35,12 @@ namespace hex { this->m_memoryEditor.HighlightFn = [](const ImU8 *data, size_t off, bool next) -> bool { ViewHexEditor *_this = (ViewHexEditor *) data; - for (auto&[offset, size, color, name] : _this->m_highlights) { - if (next && off == (offset + size)) { + for (auto&[offset, type, color, name] : _this->m_highlights) { + if (next && off == (offset + type.size)) { return false; } - if (off >= offset && off < (offset + size)) { + if (off >= offset && off < (offset + type.size)) { _this->m_memoryEditor.HighlightColor = color; return true; } diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 587c838ec..044f2c225 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -75,14 +75,25 @@ namespace hex { } - void ViewPattern::setHighlight(u64 offset, size_t size, std::string name, u32 color) { + void ViewPattern::setHighlight(u64 offset, std::string name, lang::Token::TypeToken::Type type, size_t size, u32 color) { if (color == 0) color = std::mt19937(std::random_device()())(); color &= ~0xFF00'0000; color |= 0x5000'0000; - this->m_highlights.emplace_back(offset, size, color, name); + VariableType varType = { 0 }; + + switch (static_cast(type) & 0x0F) { + default: + case 0: varType.kind = VariableType::Kind::Unsigned; break; + case 1: varType.kind = VariableType::Kind::Signed; break; + case 2: varType.kind = VariableType::Kind::FloatingPoint; break; + } + + varType.size = size; + + this->m_highlights.emplace_back(offset, varType, color, name); } template T> @@ -120,7 +131,8 @@ namespace hex { u64 offset = varNode->getOffset().value(); if (varNode->getVariableType() != lang::Token::TypeToken::Type::CustomType) { - this->setHighlight(offset, (static_cast(varNode->getVariableType()) >> 4) * varNode->getArraySize(), varNode->getVariableName()); + size_t size = static_cast(varNode->getVariableType()) >> 4 * varNode->getArraySize(); + this->setHighlight(offset, varNode->getVariableName(), varNode->getVariableType(), size); } else { for (auto &structNode : findNodes(lang::ASTNode::Type::Struct, ast)) if (varNode->getCustomVariableTypeName() == structNode->getName()) { @@ -163,7 +175,7 @@ namespace hex { if (currTypeDeclNode->getAssignedType() != lang::Token::TypeToken::Type::CustomType) { size_t size = (static_cast(currTypeDeclNode->getAssignedType()) >> 4); - this->setHighlight(offset, size, name); + this->setHighlight(offset, name, currTypeDeclNode->getAssignedType(), size); offset += size; } else { bool foundType = false; @@ -218,7 +230,7 @@ namespace hex { if (var->getArraySize() > 1) memberName += "[" + std::to_string(i) + "]"; - this->setHighlight(offset, size, memberName); + this->setHighlight(offset, memberName, var->getVariableType(), size); offset += size; } } else { diff --git a/source/views/view_pattern_data.cpp b/source/views/view_pattern_data.cpp index b153b72d3..d82b323ab 100644 --- a/source/views/view_pattern_data.cpp +++ b/source/views/view_pattern_data.cpp @@ -36,20 +36,34 @@ namespace hex { if (this->m_dataProvider != nullptr && this->m_dataProvider->isReadable()) { - for (auto&[offset, size, color, name] : this->m_highlights) { - std::vector buffer(size + 1, 0x00); + for (auto&[offset, type, color, name] : this->m_highlights) { + std::vector buffer(type.size + 1, 0x00); - this->m_dataProvider->read(offset, buffer.data(), size); + this->m_dataProvider->read(offset, buffer.data(), type.size); - if (size <= 8) { + if (type.size <= 8) { u64 data = 0; - std::memcpy(&data, buffer.data(), size); + std::memcpy(&data, buffer.data(), type.size); - ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] %lu (0x%08lx) \"%s\"", offset, - offset + size, data, data, - makeDisplayable(buffer.data(), buffer.size()).c_str()); + switch (type.kind) { + case VariableType::Kind::Unsigned: + ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] %lu (0x%08lx) \"%s\"", offset, + offset + type.size, data, data, + makeDisplayable(buffer.data(), buffer.size()).c_str()); + break; + case VariableType::Kind::Signed: + ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] %ld (0x%08lx) \"%s\"", offset, + offset + type.size, data, data, + makeDisplayable(buffer.data(), buffer.size()).c_str()); + break; + case VariableType::Kind::FloatingPoint: + ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] %f (0x%08lx) \"%s\"", offset, + offset + type.size, data, data, + makeDisplayable(buffer.data(), buffer.size()).c_str()); + break; + } } else - ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] [ ARRAY ] \"%s\"", offset, offset + size, + ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] [ ARRAY ] \"%s\"", offset, offset + type.size, makeDisplayable(buffer.data(), buffer.size()).c_str()); } }