diff --git a/lib/libimhex/include/hex/providers/provider.hpp b/lib/libimhex/include/hex/providers/provider.hpp index 83994f98d..7eacfe3bc 100644 --- a/lib/libimhex/include/hex/providers/provider.hpp +++ b/lib/libimhex/include/hex/providers/provider.hpp @@ -28,7 +28,8 @@ namespace hex::prv { virtual void read(u64 offset, void *buffer, size_t size, bool overlays = true); virtual void write(u64 offset, const void *buffer, size_t size); - virtual void resize(ssize_t newSize); + virtual void resize(size_t newSize); + virtual void insert(u64 offset, size_t size); virtual void save(); virtual void saveAs(const fs::path &path); diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index f3b98dd02..ebd01a0b0 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -34,7 +34,23 @@ namespace hex::prv { void Provider::save() { } void Provider::saveAs(const fs::path &path) { } - void Provider::resize(ssize_t newSize) { } + void Provider::resize(size_t newSize) { } + + void Provider::insert(u64 offset, size_t size) { + auto &patches = getPatches(); + + std::vector> patchesToMove; + + for (auto &[address, value] : patches) { + if (address > offset) + patchesToMove.emplace_back(address, value); + } + + for (const auto &[address, value] : patchesToMove) + patches.erase(address); + for (const auto &[address, value] : patchesToMove) + patches.insert({ address + offset, value }); + } void Provider::applyOverlays(u64 offset, void *buffer, size_t size) { for (auto &overlay : this->m_overlays) { diff --git a/main/source/window/win_window.cpp b/main/source/window/win_window.cpp index 01c950855..504253fd5 100644 --- a/main/source/window/win_window.cpp +++ b/main/source/window/win_window.cpp @@ -23,10 +23,10 @@ namespace hex { - static LONG_PTR oldWndProc; - static float titleBarHeight; - static ImGuiMouseCursor mouseCursorIcon; - static BOOL compositionEnabled = false; + static LONG_PTR g_oldWndProc; + static float g_titleBarHeight; + static ImGuiMouseCursor g_mouseCursorIcon; + static BOOL g_compositionEnabled = false; static LRESULT windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { @@ -37,7 +37,7 @@ RECT &rect = *reinterpret_cast(lParam); RECT client = rect; - CallWindowProc((WNDPROC)oldWndProc, hwnd, uMsg, wParam, lParam); + CallWindowProc((WNDPROC)g_oldWndProc, hwnd, uMsg, wParam, lParam); if (IsMaximized(hwnd)) { WINDOWINFO windowInfo = { .cbSize = sizeof(WINDOWINFO) }; @@ -60,23 +60,23 @@ switch (cursorPos) { case HTRIGHT: case HTLEFT: - mouseCursorIcon = ImGuiMouseCursor_ResizeEW; + g_mouseCursorIcon = ImGuiMouseCursor_ResizeEW; break; case HTTOP: case HTBOTTOM: - mouseCursorIcon = ImGuiMouseCursor_ResizeNS; + g_mouseCursorIcon = ImGuiMouseCursor_ResizeNS; break; case HTTOPLEFT: case HTBOTTOMRIGHT: - mouseCursorIcon = ImGuiMouseCursor_ResizeNWSE; + g_mouseCursorIcon = ImGuiMouseCursor_ResizeNWSE; break; case HTTOPRIGHT: case HTBOTTOMLEFT: - mouseCursorIcon = ImGuiMouseCursor_ResizeNESW; + g_mouseCursorIcon = ImGuiMouseCursor_ResizeNESW; break; case HTCAPTION: case HTCLIENT: - mouseCursorIcon = ImGuiMouseCursor_None; + g_mouseCursorIcon = ImGuiMouseCursor_None; break; } @@ -129,7 +129,7 @@ return HTBOTTOMRIGHT; case RegionClient: default: - if ((cursor.y < (window.top + titleBarHeight * 2)) && !(ImGui::IsAnyItemHovered() || ImGui::IsPopupOpen(nullptr, ImGuiPopupFlags_AnyPopupId))) + if ((cursor.y < (window.top + g_titleBarHeight * 2)) && !(ImGui::IsAnyItemHovered() || ImGui::IsPopupOpen(nullptr, ImGuiPopupFlags_AnyPopupId))) return HTCAPTION; else break; } @@ -161,7 +161,7 @@ default: break; } - return CallWindowProc((WNDPROC)oldWndProc, hwnd, uMsg, wParam, lParam); + return CallWindowProc((WNDPROC)g_oldWndProc, hwnd, uMsg, wParam, lParam); } @@ -233,7 +233,7 @@ // Setup borderless window auto hwnd = glfwGetWin32Window(this->m_window); - oldWndProc = ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)windowProc); + g_oldWndProc = ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)windowProc); MARGINS borderless = { 1, 1, 1, 1 }; ::DwmExtendFrameIntoClientArea(hwnd, &borderless); @@ -266,12 +266,12 @@ } void Window::beginNativeWindowFrame() { - titleBarHeight = ImGui::GetCurrentWindow()->MenuBarHeight(); + g_titleBarHeight = ImGui::GetCurrentWindow()->MenuBarHeight(); } void Window::endNativeWindowFrame() { - if (mouseCursorIcon != ImGuiMouseCursor_None) { - ImGui::SetMouseCursor(mouseCursorIcon); + if (g_mouseCursorIcon != ImGuiMouseCursor_None) { + ImGui::SetMouseCursor(g_mouseCursorIcon); } switch (ImGui::GetMouseCursor()) { @@ -310,7 +310,7 @@ } void Window::drawTitleBar() { - auto buttonSize = ImVec2(titleBarHeight * 1.5F, titleBarHeight - 1); + auto buttonSize = ImVec2(g_titleBarHeight * 1.5F, g_titleBarHeight - 1); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_MenuBarBg)); diff --git a/plugins/builtin/include/content/providers/file_provider.hpp b/plugins/builtin/include/content/providers/file_provider.hpp index 0c870b3ea..67985ee42 100644 --- a/plugins/builtin/include/content/providers/file_provider.hpp +++ b/plugins/builtin/include/content/providers/file_provider.hpp @@ -29,7 +29,9 @@ namespace hex::plugin::builtin::prv { void read(u64 offset, void *buffer, size_t size, bool overlays) override; void write(u64 offset, const void *buffer, size_t size) override; - void resize(ssize_t newSize) override; + + void resize(size_t newSize) override; + void insert(u64 offset, size_t size) override; void readRaw(u64 offset, void *buffer, size_t size) override; void writeRaw(u64 offset, const void *buffer, size_t size) override; diff --git a/plugins/builtin/include/content/providers/gdb_provider.hpp b/plugins/builtin/include/content/providers/gdb_provider.hpp index 79fe1ad0d..654eac6e9 100644 --- a/plugins/builtin/include/content/providers/gdb_provider.hpp +++ b/plugins/builtin/include/content/providers/gdb_provider.hpp @@ -22,7 +22,6 @@ class GDBProvider : public hex::prv::Provider { void read(u64 offset, void *buffer, size_t size, bool overlays) override; void write(u64 offset, const void *buffer, size_t size) override; - void resize(ssize_t newSize) override; void readRaw(u64 offset, void *buffer, size_t size) override; void writeRaw(u64 offset, const void *buffer, size_t size) override; @@ -48,6 +47,8 @@ class GDBProvider : public hex::prv::Provider { std::string m_ipAddress; int m_port; + u64 m_size; + constexpr static size_t CacheLineSize = 0x1000; struct CacheLine { diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 251aad575..96d3157b8 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -101,16 +101,40 @@ namespace hex::plugin::builtin::prv { } } - void FileProvider::resize(ssize_t newSize) { + void FileProvider::resize(size_t newSize) { this->close(); { - File(this->m_path, File::Mode::Write).setSize(newSize); + auto file = File(this->m_path, File::Mode::Write); + + file.setSize(newSize); + this->m_fileSize = file.getSize(); } this->open(); } + void FileProvider::insert(u64 offset, size_t size) { + auto oldSize = this->getActualSize(); + this->resize(oldSize + size); + + std::vector buffer(0x1000); + const std::vector zeroBuffer(0x1000); + + auto position = oldSize; + while (position > offset) { + size_t readSize = (position >= (offset + buffer.size())) ? buffer.size() : (position - offset); + + position -= readSize; + + this->readRaw(position, buffer.data(), readSize); + this->writeRaw(position, zeroBuffer.data(), readSize); + this->writeRaw(position + size, buffer.data(), readSize); + } + + Provider::insert(offset, size); + } + size_t FileProvider::getActualSize() const { return this->m_fileSize; } diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index 54c31539a..034cba187 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -112,7 +112,7 @@ namespace hex::plugin::builtin::prv { } - GDBProvider::GDBProvider() : Provider() { + GDBProvider::GDBProvider() : Provider(), m_size(0xFFFF'FFFF) { } @@ -218,12 +218,8 @@ namespace hex::plugin::builtin::prv { } - void GDBProvider::resize(ssize_t newSize) { - - } - size_t GDBProvider::getActualSize() const { - return 0xFFFF'FFFF; + return this->m_size; } std::string GDBProvider::getName() const { @@ -303,6 +299,12 @@ namespace hex::plugin::builtin::prv { ImGui::InputText("hex.builtin.provider.gdb.ip"_lang, this->m_ipAddress.data(), this->m_ipAddress.capacity(), ImGuiInputTextFlags_CallbackEdit, ImGui::UpdateStringSizeCallback, &this->m_ipAddress); ImGui::InputInt("hex.builtin.provider.gdb.port"_lang, &this->m_port, 0, 0); + ImGui::Separator(); + + ImGui::TextUnformatted("0x"); + ImGui::SameLine(); + ImGui::InputScalar("hex.common.size"_lang, ImGuiDataType_U64, &this->m_size, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal); + if (this->m_port < 0) this->m_port = 0; else if (this->m_port > 0xFFFF) diff --git a/plugins/builtin/source/content/views/view_hexeditor.cpp b/plugins/builtin/source/content/views/view_hexeditor.cpp index a16a3f6a1..ba45e6998 100644 --- a/plugins/builtin/source/content/views/view_hexeditor.cpp +++ b/plugins/builtin/source/content/views/view_hexeditor.cpp @@ -317,16 +317,40 @@ namespace hex::plugin::builtin { } if (ImGui::BeginPopupModal("hex.builtin.view.hexeditor.menu.edit.resize"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { - ImGui::InputScalar("hex.common.size"_lang, ImGuiDataType_U64, &this->m_resizeSize, nullptr, nullptr, "0x%016llx", ImGuiInputTextFlags_CharsHexadecimal); + ImGui::TextUnformatted("0x"); + ImGui::SameLine(); + ImGui::InputScalar("hex.common.size"_lang, ImGuiDataType_U64, &this->m_resizeSize, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal); ImGui::NewLine(); confirmButtons("hex.common.set"_lang, "hex.common.cancel"_lang, - [this, &provider]{ - provider->resize(this->m_resizeSize); - ImGui::CloseCurrentPopup(); - }, []{ - ImGui::CloseCurrentPopup(); - }); + [this, &provider]{ + provider->resize(this->m_resizeSize); + ImGui::CloseCurrentPopup(); + }, + [] { + ImGui::CloseCurrentPopup(); + }); + + if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) + ImGui::CloseCurrentPopup(); + + ImGui::EndPopup(); + } + + if (ImGui::BeginPopupModal("hex.builtin.view.hexeditor.menu.edit.insert"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::TextUnformatted("0x"); + ImGui::SameLine(); + ImGui::InputScalar("hex.common.size"_lang, ImGuiDataType_U64, &this->m_resizeSize, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal); + ImGui::NewLine(); + + confirmButtons("hex.common.set"_lang, "hex.common.cancel"_lang, + [this, &provider] { + provider->insert(std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd), this->m_resizeSize); + ImGui::CloseCurrentPopup(); + }, + [] { + ImGui::CloseCurrentPopup(); + }); if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) ImGui::CloseCurrentPopup(); @@ -1010,6 +1034,13 @@ namespace hex::plugin::builtin { ImGui::OpenPopup("hex.builtin.view.hexeditor.menu.edit.resize"_lang); }); } + + if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.edit.insert"_lang, nullptr, false, providerValid && provider->isResizable())) { + View::doLater([this]{ + this->m_resizeSize = 0; + ImGui::OpenPopup("hex.builtin.view.hexeditor.menu.edit.insert"_lang); + }); + } } void ViewHexEditor::registerEvents() { diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index a81270389..41e0da81d 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -244,7 +244,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.hexeditor.menu.edit.select_all", "Alles auswählen" }, { "hex.builtin.view.hexeditor.menu.edit.bookmark", "Lesezeichen erstellen" }, { "hex.builtin.view.hexeditor.menu.edit.set_base", "Basisadresse setzen" }, - { "hex.builtin.view.hexeditor.menu.edit.resize", "Grösse ändern" }, + { "hex.builtin.view.hexeditor.menu.edit.resize", "Grösse ändern..." }, + { "hex.builtin.view.hexeditor.menu.edit.insert", "Einsetzen..." }, { "hex.builtin.view.information.name", "Dateninformationen" }, { "hex.builtin.view.information.control", "Einstellungen" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index f61413974..089a78f67 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -246,7 +246,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.hexeditor.menu.edit.select_all", "Select all" }, { "hex.builtin.view.hexeditor.menu.edit.bookmark", "Create bookmark" }, { "hex.builtin.view.hexeditor.menu.edit.set_base", "Set base address" }, - { "hex.builtin.view.hexeditor.menu.edit.resize", "Resize" }, + { "hex.builtin.view.hexeditor.menu.edit.resize", "Resize..." }, + { "hex.builtin.view.hexeditor.menu.edit.insert", "Insert..." }, { "hex.builtin.view.information.name", "Data Information" }, { "hex.builtin.view.information.control", "Control" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 9daafaa90..2ab5a7684 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -243,7 +243,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.hexeditor.menu.edit.select_all", "Seleziona tutti" }, { "hex.builtin.view.hexeditor.menu.edit.bookmark", "Crea segnalibro" }, { "hex.builtin.view.hexeditor.menu.edit.set_base", "Imposta indirizzo di base" }, - { "hex.builtin.view.hexeditor.menu.edit.resize", "Ridimensiona" }, + { "hex.builtin.view.hexeditor.menu.edit.resize", "Ridimensiona..." }, + //{ "hex.builtin.view.hexeditor.menu.edit.insert", "Insert..." }, { "hex.builtin.view.information.name", "Informazione sui Dati" }, { "hex.builtin.view.information.control", "Controllo" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 8a7d79a87..bde138f18 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -243,7 +243,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.hexeditor.menu.edit.select_all", "全选" }, { "hex.builtin.view.hexeditor.menu.edit.bookmark", "添加书签" }, { "hex.builtin.view.hexeditor.menu.edit.set_base", "设置基地址" }, - { "hex.builtin.view.hexeditor.menu.edit.resize", "修改大小" }, + { "hex.builtin.view.hexeditor.menu.edit.resize", "修改大小..." }, + //{ "hex.builtin.view.hexeditor.menu.edit.insert", "Insert..." }, { "hex.builtin.view.information.name", "数据信息" }, { "hex.builtin.view.information.control", "控制" },