From f931beb49ab42394c0b93b4c66b65a71120957cd Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 23 Dec 2024 01:33:52 +0100 Subject: [PATCH] impr: Make inserting bytes at the start of the file with insert mode work better --- lib/libimhex/source/providers/provider.cpp | 5 +++-- plugins/ui/include/ui/hex_editor.hpp | 2 +- plugins/ui/source/ui/hex_editor.cpp | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 5bda5906e..ba00a66af 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -104,7 +104,8 @@ namespace hex::prv { void Provider::insertRaw(u64 offset, u64 size) { auto oldSize = this->getActualSize(); - this->resizeRaw(oldSize + size); + auto newSize = oldSize + size; + this->resizeRaw(newSize); std::vector buffer(0x1000); const std::vector zeroBuffer(0x1000); @@ -116,7 +117,7 @@ namespace hex::prv { position -= readSize; this->readRaw(position, buffer.data(), readSize); - this->writeRaw(position, zeroBuffer.data(), readSize); + this->writeRaw(position, zeroBuffer.data(), newSize - oldSize); this->writeRaw(position + size, buffer.data(), readSize); } } diff --git a/plugins/ui/include/ui/hex_editor.hpp b/plugins/ui/include/ui/hex_editor.hpp index 322dd28a6..70d8d4887 100644 --- a/plugins/ui/include/ui/hex_editor.hpp +++ b/plugins/ui/include/ui/hex_editor.hpp @@ -95,7 +95,7 @@ namespace hex::ui { private: enum class CellType : u8 { None, Hex, ASCII }; - void drawCell(u64 address, const u8 *data, size_t size, bool hovered, CellType cellType); + void drawCell(u64 address, u8 *data, size_t size, bool hovered, CellType cellType); void drawSeparatorLine(u64 address, bool drawVerticalConnector); void drawSelectionFrame(u32 x, u32 y, Region selection, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize, const ImColor &backgroundColor) const; void drawEditor(const ImVec2 &size); diff --git a/plugins/ui/source/ui/hex_editor.cpp b/plugins/ui/source/ui/hex_editor.cpp index a51bd44a5..f69938de5 100644 --- a/plugins/ui/source/ui/hex_editor.cpp +++ b/plugins/ui/source/ui/hex_editor.cpp @@ -281,7 +281,7 @@ namespace hex::ui { - void HexEditor::drawCell(u64 address, const u8 *data, size_t size, bool hovered, CellType cellType) { + void HexEditor::drawCell(u64 address, u8 *data, size_t size, bool hovered, CellType cellType) { static DataVisualizerAscii asciiVisualizer; if (m_shouldUpdateEditingValue && address == m_editingAddress) { @@ -320,6 +320,7 @@ namespace hex::ui { std::memcpy(m_editingBytes.data(), data, size); else if (m_mode == Mode::Insert) { std::memset(m_editingBytes.data(), 0x00, size); + std::memset(data, 0x00, size); m_provider->insert(address, size); }