From 58deaa6b293947ac4dc21599e63896922c45614b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 24 Nov 2020 02:00:22 +0100 Subject: [PATCH] Added importing from base64 encoded file --- include/crypto.hpp | 3 + include/views/view_hexeditor.hpp | 3 + source/crypto.cpp | 22 ++++ source/views/view_hexeditor.cpp | 220 +++++++++++++++++++------------ 4 files changed, 167 insertions(+), 81 deletions(-) diff --git a/include/crypto.hpp b/include/crypto.hpp index 4a6598882..c6fd83b68 100644 --- a/include/crypto.hpp +++ b/include/crypto.hpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace hex { @@ -21,4 +22,6 @@ namespace hex { std::array sha384(prv::Provider* &data, u64 offset, size_t size); std::array sha512(prv::Provider* &data, u64 offset, size_t size); + std::vector decode64(const std::vector &input); + std::vector encode64(const std::vector &input); } \ No newline at end of file diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index 88c359ef6..7ed0eef42 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -45,11 +45,14 @@ namespace hex { s64 m_gotoAddress = 0; + std::vector m_importData; void drawSearchPopup(); void drawGotoPopup(); void openFile(std::string path); + bool saveToFile(std::string path, const std::vector& data); + bool loadFromFile(std::string path, std::vector& data); enum class Language { C, Cpp, CSharp, Rust, Python, Java, JavaScript }; void copyBytes(); diff --git a/source/crypto.cpp b/source/crypto.cpp index 077583520..f854f251a 100644 --- a/source/crypto.cpp +++ b/source/crypto.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include #include @@ -215,4 +217,24 @@ namespace hex { return result; } + std::vector decode64(const std::vector &input) { + size_t outputSize = (3 * input.size()) / 4; + std::vector output(outputSize + 1, 0x00); + + if (EVP_DecodeBlock(output.data(), reinterpret_cast(input.data()), input.size()) != outputSize) + return { }; + + return output; + } + + std::vector encode64(const std::vector &input) { + size_t outputSize = 4 * ((input.size() + 2) / 3); + std::vector output(outputSize + 1, 0x00); + + if (EVP_EncodeBlock(output.data(), reinterpret_cast(input.data()), input.size()) != outputSize) + return { }; + + return output; + } + } \ No newline at end of file diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index a858a3e1f..368c963c1 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -5,6 +5,11 @@ #include +#include "crypto.hpp" + +#undef __STRICT_ANSI__ +#include + namespace hex { ViewHexEditor::ViewHexEditor(prv::Provider* &dataProvider, std::vector &patternData) @@ -115,8 +120,113 @@ namespace hex { this->openFile(this->m_fileBrowser.selected_path); } + if (this->m_fileBrowser.showFileDialog("Open Base64 File", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN)) { + std::vector base64; + this->loadFromFile(this->m_fileBrowser.selected_path, base64); + + if (!base64.empty()) { + this->m_importData = decode64(base64); + ImGui::OpenPopup("Save File"); + } + + } + + if (this->m_fileBrowser.showFileDialog("Save File", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE)) { + this->saveToFile(this->m_fileBrowser.selected_path, this->m_importData); + this->openFile(this->m_fileBrowser.selected_path); + } + } + void ViewHexEditor::createMenu() { + + if (ImGui::BeginMenu("File")) { + if (ImGui::MenuItem("Open File...", "CTRL + O")) { + View::doLater([]{ ImGui::OpenPopup("Open File"); }); + } + + if (ImGui::BeginMenu("Import...")) { + if (ImGui::MenuItem("Base64 File")) { + View::doLater([]{ ImGui::OpenPopup("Open Base64 File"); }); + } + + ImGui::EndMenu(); + } + + ImGui::Separator(); + + if (ImGui::MenuItem("Search", "CTRL + F")) { + View::doLater([]{ ImGui::OpenPopup("Search"); }); + } + + if (ImGui::MenuItem("Goto", "CTRL + G")) { + View::doLater([]{ ImGui::OpenPopup("Goto"); }); + } + + ImGui::EndMenu(); + } + + if (ImGui::BeginMenu("Edit")) { + if (ImGui::BeginMenu("Copy as...", this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) { + 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::Separator(); + + if (ImGui::MenuItem("Editor View")) + this->copyHexView(); + if (ImGui::MenuItem("HTML")) + this->copyHexViewHTML(); + + ImGui::EndMenu(); + } + + + ImGui::EndMenu(); + } + } + + bool ViewHexEditor::handleShortcut(int key, int mods) { + if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_F) { + ImGui::OpenPopup("Search"); + return true; + } else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_G) { + ImGui::OpenPopup("Goto"); + return true; + } else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_O) { + ImGui::OpenPopup("Open File"); + return true; + } else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_ALT) && key == GLFW_KEY_C) { + this->copyBytes(); + return true; + } else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == GLFW_KEY_C) { + this->copyString(); + return true; + } + + return false; + } + + void ViewHexEditor::openFile(std::string path) { if (this->m_dataProvider != nullptr) delete this->m_dataProvider; @@ -125,6 +235,35 @@ namespace hex { View::postEvent(Events::DataChanged); } + bool ViewHexEditor::saveToFile(std::string path, const std::vector& data) { + FILE *file = fopen(path.c_str(), "wb"); + + if (file == nullptr) + return false; + + fwrite(data.data(), 1, data.size(), file); + fclose(file); + + return true; + } + + bool ViewHexEditor::loadFromFile(std::string path, std::vector& data) { + FILE *file = fopen(path.c_str(), "rb"); + + if (file == nullptr) + return false; + + fseek(file, 0, SEEK_END); + size_t size = ftello64(file); + rewind(file); + + data.resize(size); + fread(data.data(), 1, data.size(), file); + fclose(file); + + return true; + } + void ViewHexEditor::copyBytes() { 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); @@ -363,87 +502,6 @@ R"( ImGui::SetClipboardText(str.c_str()); } - void ViewHexEditor::createMenu() { - - if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem("Open File...", "CTRL + O")) { - View::doLater([]{ ImGui::OpenPopup("Open File"); }); - } - - ImGui::Separator(); - - if (ImGui::MenuItem("Search", "CTRL + F")) { - View::doLater([]{ ImGui::OpenPopup("Search"); }); - } - - if (ImGui::MenuItem("Goto", "CTRL + G")) { - View::doLater([]{ ImGui::OpenPopup("Goto"); }); - } - - ImGui::EndMenu(); - } - - if (ImGui::BeginMenu("Edit")) { - if (ImGui::BeginMenu("Copy as...", this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) { - 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::Separator(); - - if (ImGui::MenuItem("Editor View")) - this->copyHexView(); - if (ImGui::MenuItem("HTML")) - this->copyHexViewHTML(); - - ImGui::EndMenu(); - } - - - ImGui::EndMenu(); - } - } - - bool ViewHexEditor::handleShortcut(int key, int mods) { - if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_F) { - ImGui::OpenPopup("Search"); - return true; - } else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_G) { - ImGui::OpenPopup("Goto"); - return true; - } else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_O) { - ImGui::OpenPopup("Open File"); - return true; - } else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_ALT) && key == GLFW_KEY_C) { - this->copyBytes(); - return true; - } else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == GLFW_KEY_C) { - this->copyString(); - return true; - } - - return false; - } - - static std::vector> findString(prv::Provider* &provider, std::string string) { std::vector> results;