From e4879f7546888c95c7198c676fa356bde3013dfe Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 20 Nov 2020 11:57:14 +0100 Subject: [PATCH] Added copy programming language array to hex editor --- include/views/view_hexeditor.hpp | 3 + source/views/view_hexeditor.cpp | 129 +++++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 6 deletions(-) diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index e31dc1ecb..9ae0c9e78 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -50,8 +50,11 @@ namespace hex { void drawGotoPopup(); void openFile(std::string path); + + enum class Language { C, Cpp, CSharp, Rust, Python, Java, JavaScript }; void copyBytes(); void copyString(); + void copyLanguageArray(Language language); }; diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 0beae6dd2..8862c92fb 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -102,7 +102,7 @@ namespace hex { std::string str; for (const auto &byte : buffer) - str += hex::format("%x ", byte); + str += hex::format("%02x ", byte); str.pop_back(); ImGui::SetClipboardText(str.c_str()); @@ -121,6 +121,106 @@ namespace hex { ImGui::SetClipboardText(buffer.c_str()); } + void ViewHexEditor::copyLanguageArray(Language language) { + 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; + switch (language) { + case Language::C: + str += "const unsigned char data[" + std::to_string(buffer.size()) + "] = { "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " };"; + break; + case Language::Cpp: + str += "const std::array data = { "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " };"; + break; + case Language::Java: + str += "final byte[] data = { "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " };"; + break; + case Language::CSharp: + str += "const byte[] data = { "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " };"; + break; + case Language::Rust: + str += "let data: [u8, " + std::to_string(buffer.size()) + "] = [ "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " ];"; + break; + case Language::Python: + str += "data = bytes([ "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " ]);"; + break; + case Language::JavaScript: + str += "const data = new Uint8Array([ "; + + for (const auto &byte : buffer) + str += hex::format("0x%02x, ", byte); + + // Remove trailing comma + str.pop_back(); + str.pop_back(); + + str += " ]);"; + break; + } + + ImGui::SetClipboardText(str.c_str()); + } + void ViewHexEditor::createMenu() { if (ImGui::BeginMenu("File")) { @@ -142,13 +242,30 @@ namespace hex { } if (ImGui::BeginMenu("Edit")) { - if (ImGui::MenuItem("Copy bytes", "CTRL + ALT + C")) { - this->copyBytes(); + if (ImGui::BeginMenu("Copy as...")) { + if (ImGui::MenuItem("Bytes", "CTRL + ALT + C")) + 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")) + this->copyLanguageArray(Language::Cpp); + if (ImGui::MenuItem("C# Array")) + this->copyLanguageArray(Language::CSharp); + if (ImGui::MenuItem("Rust Array")) + this->copyLanguageArray(Language::Rust); + if (ImGui::MenuItem("Python Array")) + this->copyLanguageArray(Language::Python); + if (ImGui::MenuItem("Java Array")) + this->copyLanguageArray(Language::Java); + if (ImGui::MenuItem("JavaScript Array")) + this->copyLanguageArray(Language::JavaScript); + + ImGui::EndMenu(); } - if (ImGui::MenuItem("Copy string", "CTRL + SHIFT + C")) { - this->copyString(); - } ImGui::EndMenu(); }