From 81e5c945b43a4e6799540c7ce2d3e06f236c0d21 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 20 Nov 2020 13:25:55 +0100 Subject: [PATCH] Added copy hex view as string option --- include/views/view_hexeditor.hpp | 1 + source/views/view_hexeditor.cpp | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index 9ae0c9e78..b1564f781 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -55,6 +55,7 @@ namespace hex { void copyBytes(); void copyString(); void copyLanguageArray(Language language); + void copyHexView(); }; diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 6f0edaa2c..f75507df9 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -221,6 +221,51 @@ namespace hex { ImGui::SetClipboardText(str.c_str()); } + void ViewHexEditor::copyHexView() { + size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); + size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); + + size_t copySize = (end - start) + 1; + + std::vector buffer(copySize, 0x00); + this->m_dataProvider->read(start, buffer.data(), buffer.size()); + + std::string str = "Hex View 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\n"; + + + for (u32 col = start >> 4; col <= (end >> 4); col++) { + str += hex::format("%08lx ", col << 4); + for (u64 i = 0 ; i < 16; i++) { + + if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF)) + str += " "; + else + str += hex::format("%02lx ", buffer[((col << 4) - start) + i]); + + if ((i & 0xF) == 0x7) + str += " "; + } + + str += " "; + + for (u64 i = 0 ; i < 16; i++) { + + if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF)) + str += " "; + else { + u8 c = buffer[((col << 4) - start) + i]; + char displayChar = (c < 32 || c >= 128) ? '.' : c; + str += hex::format("%c", displayChar); + } + } + + str += "\n"; + } + + + ImGui::SetClipboardText(str.c_str()); + } + void ViewHexEditor::createMenu() { if (ImGui::BeginMenu("File")) { @@ -247,7 +292,9 @@ namespace hex { this->copyBytes(); if (ImGui::MenuItem("Hex String", "CTRL + SHIFT + C")) this->copyString(); + ImGui::Separator(); + if (ImGui::MenuItem("C Array")) this->copyLanguageArray(Language::C); if (ImGui::MenuItem("C++ Array")) @@ -263,6 +310,11 @@ namespace hex { if (ImGui::MenuItem("JavaScript Array")) this->copyLanguageArray(Language::JavaScript); + ImGui::Separator(); + + if (ImGui::MenuItem("Editor View")) + this->copyHexView(); + ImGui::EndMenu(); }