Skip to content

Commit

Permalink
ux: Added support for pasting bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Apr 16, 2021
1 parent a3b3eeb commit 59dd372
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions include/views/view_hexeditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace hex {

enum class Language { C, Cpp, CSharp, Rust, Python, Java, JavaScript };
void copyBytes();
void pasteBytes();
void copyString();
void copyLanguageArray(Language language);
void copyHexView();
Expand Down
7 changes: 4 additions & 3 deletions plugins/builtin/source/lang/de_DE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.error.open", "Öffnen der Datei fehlgeschlagen!" },
{ "hex.view.hexeditor.menu.edit.undo", "Rückgängig" },
{ "hex.view.hexeditor.menu.edit.redo", "Wiederholen" },
{ "hex.view.hexeditor.menu.edit.copy", "Kopieren als..." },
{ "hex.view.hexeditor.copy.bytes", "Bytes" },
{ "hex.view.hexeditor.copy.hex", "Hex String" },
{ "hex.view.hexeditor.menu.edit.copy", "Kopieren" },
{ "hex.view.hexeditor.menu.edit.copy_as", "Kopieren als..." },
{ "hex.view.hexeditor.copy.hex", "String" },
{ "hex.view.hexeditor.copy.c", "C Array" },
{ "hex.view.hexeditor.copy.cpp", "C++ Array" },
{ "hex.view.hexeditor.copy.csharp", "C# Array" },
Expand All @@ -198,6 +198,7 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.copy.js", "JavaScript Array" },
{ "hex.view.hexeditor.copy.ascii", "ASCII Art" },
{ "hex.view.hexeditor.copy.html", "HTML" },
{ "hex.view.hexeditor.menu.edit.paste", "Einfügen" },
{ "hex.view.hexeditor.menu.edit.bookmark", "Lesezeichen erstellen" },
{ "hex.view.hexeditor.menu.edit.set_base", "Basisadresse setzen" },

Expand Down
7 changes: 4 additions & 3 deletions plugins/builtin/source/lang/en_US.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.error.open", "Failed to open file!" },
{ "hex.view.hexeditor.menu.edit.undo", "Undo" },
{ "hex.view.hexeditor.menu.edit.redo", "Redo" },
{ "hex.view.hexeditor.menu.edit.copy", "Copy as..." },
{ "hex.view.hexeditor.copy.bytes", "Bytes" },
{ "hex.view.hexeditor.copy.hex", "Hex String" },
{ "hex.view.hexeditor.menu.edit.copy", "Copy" },
{ "hex.view.hexeditor.menu.edit.copy_as", "Copy as..." },
{ "hex.view.hexeditor.copy.hex", "String" },
{ "hex.view.hexeditor.copy.c", "C Array" },
{ "hex.view.hexeditor.copy.cpp", "C++ Array" },
{ "hex.view.hexeditor.copy.csharp", "C# Array" },
Expand All @@ -198,6 +198,7 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.copy.js", "JavaScript Array" },
{ "hex.view.hexeditor.copy.ascii", "ASCII Art" },
{ "hex.view.hexeditor.copy.html", "HTML" },
{ "hex.view.hexeditor.menu.edit.paste", "Paste" },
{ "hex.view.hexeditor.menu.edit.bookmark", "Create bookmark" },
{ "hex.view.hexeditor.menu.edit.set_base", "Set base address" },

Expand Down
7 changes: 4 additions & 3 deletions plugins/builtin/source/lang/it_IT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.error.open", "Impossibile aprire il File!" },
{ "hex.view.hexeditor.menu.edit.undo", "Annulla" },
{ "hex.view.hexeditor.menu.edit.redo", "Ripeti" },
{ "hex.view.hexeditor.menu.edit.copy", "Copia come..." },
{ "hex.view.hexeditor.copy.bytes", "Bytes" },
{ "hex.view.hexeditor.copy.hex", "Stringa esadecimale" },
{ "hex.view.hexeditor.menu.edit.copy", "Copia" },
{ "hex.view.hexeditor.menu.edit.copy_as", "Copia come..." },
{ "hex.view.hexeditor.copy.hex", "Stringa" },
{ "hex.view.hexeditor.copy.c", "C Array" },
{ "hex.view.hexeditor.copy.cpp", "C++ Array" },
{ "hex.view.hexeditor.copy.csharp", "C# Array" },
Expand All @@ -198,6 +198,7 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.copy.js", "JavaScript Array" },
{ "hex.view.hexeditor.copy.ascii", "ASCII Art" },
{ "hex.view.hexeditor.copy.html", "HTML" },
{ "hex.view.hexeditor.menu.edit.paste", "Incolla" },
{ "hex.view.hexeditor.menu.edit.bookmark", "Crea segnalibro" },
{ "hex.view.hexeditor.menu.edit.set_base", "Imposta indirizzo di base" },

Expand Down
61 changes: 57 additions & 4 deletions source/views/view_hexeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,15 @@ namespace hex {
} else if (mods == GLFW_MOD_CONTROL && key == 'O') {
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.open_file"_lang); });
return true;
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_ALT) && key == 'C') {
} else if (mods == GLFW_MOD_CONTROL && key == 'C') {
this->copyBytes();
return true;
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == 'C') {
this->copyString();
return true;
} else if (mods == GLFW_MOD_CONTROL && key == 'V') {
this->pasteBytes();
return true;
}

return false;
Expand Down Expand Up @@ -646,6 +649,46 @@ namespace hex {
ImGui::SetClipboardText(str.c_str());
}

void ViewHexEditor::pasteBytes() {
auto provider = SharedData::currentProvider;

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);

std::string clipboard = ImGui::GetClipboardText();

// Check for non-hex characters
bool isValidHexString = std::find_if(clipboard.begin(), clipboard.end(), [](char c) {
return !std::isxdigit(c) && !std::isspace(c);
}) == clipboard.end();

if (!isValidHexString) return;

// Remove all whitespace
std::erase_if(clipboard, [](char c) { return std::isspace(c); });

// Only paste whole bytes
if (clipboard.length() % 2 != 0) return;

// Convert hex string to bytes
std::vector<u8> buffer(clipboard.length() / 2, 0x00);
u32 stringIndex = 0;
for (u8 &byte : buffer) {
for (u8 i = 0; i < 2; i++) {
byte <<= 4;

char c = clipboard[stringIndex];

if (c >= '0' && c <= '9') byte |= (c - '0');
else if (c >= 'a' && c <= 'f') byte |= (c - 'a') + 0xA;
else if (c >= 'A' && c <= 'F') byte |= (c - 'A') + 0xA;
}
}

// Write bytes
provider->write(start - provider->getBaseAddress(), buffer.data(), std::min(end - start + 1, buffer.size()));
}

void ViewHexEditor::copyString() {
auto provider = SharedData::currentProvider;

Expand Down Expand Up @@ -1104,9 +1147,14 @@ R"(
if (ImGui::MenuItem("hex.view.hexeditor.menu.edit.redo"_lang, "CTRL + Y", false, SharedData::currentProvider != nullptr))
SharedData::currentProvider->redo();

if (ImGui::BeginMenu("hex.view.hexeditor.menu.edit.copy"_lang, this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) {
if (ImGui::MenuItem("hex.view.hexeditor.copy.bytes"_lang, "CTRL + ALT + C"))
this->copyBytes();
ImGui::Separator();

bool bytesSelected = this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1;

if (ImGui::MenuItem("hex.view.hexeditor.menu.edit.copy"_lang, "CTRL + C", false, bytesSelected))
this->copyBytes();

if (ImGui::BeginMenu("hex.view.hexeditor.menu.edit.copy_as"_lang, bytesSelected)) {
if (ImGui::MenuItem("hex.view.hexeditor.copy.hex"_lang, "CTRL + SHIFT + C"))
this->copyString();

Expand Down Expand Up @@ -1137,6 +1185,11 @@ R"(
ImGui::EndMenu();
}

if (ImGui::MenuItem("hex.view.hexeditor.menu.edit.paste"_lang, "CTRL + V", false, bytesSelected))
this->pasteBytes();

ImGui::Separator();

if (ImGui::MenuItem("hex.view.hexeditor.menu.edit.bookmark"_lang, nullptr, false, this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) {
auto base = SharedData::currentProvider->getBaseAddress();

Expand Down

0 comments on commit 59dd372

Please sign in to comment.