diff --git a/GViewCore/src/App/GViewApp.cpp b/GViewCore/src/App/GViewApp.cpp index 64a56528..b492d638 100644 --- a/GViewCore/src/App/GViewApp.cpp +++ b/GViewCore/src/App/GViewApp.cpp @@ -63,7 +63,8 @@ void GView::App::Run() } bool GView::App::ResetConfiguration() { - IniObject ini; + IniObject ini = {}; + ini.CreateFromFile(GetAppSettingsFile()); // for AppCUI AppCUI::Application::UpdateAppCUISettings(ini, true); diff --git a/GViewCore/src/View/DissasmViewer/CMakeLists.txt b/GViewCore/src/View/DissasmViewer/CMakeLists.txt index 0e5970bc..0571ad6f 100644 --- a/GViewCore/src/View/DissasmViewer/CMakeLists.txt +++ b/GViewCore/src/View/DissasmViewer/CMakeLists.txt @@ -1,6 +1,7 @@ target_sources( GViewCore PRIVATE DissasmViewer.hpp + Config.hpp Config.cpp GoToDialog.cpp Instance.cpp @@ -10,11 +11,9 @@ target_sources( DissasmProperties.cpp DissasmKeyEvents.cpp DissasmX86.cpp - CommentDataWindow.cpp + SingleSelectionDataWindow.cpp AdvancedSelection.hpp AdvancedSelection.cpp DissasmDataTypes.hpp DissasmDataTypes.cpp - DissasmKeys.hpp - DissasmKeys.cpp ) \ No newline at end of file diff --git a/GViewCore/src/View/DissasmViewer/Config.cpp b/GViewCore/src/View/DissasmViewer/Config.cpp index 40d4b880..a8be2f99 100644 --- a/GViewCore/src/View/DissasmViewer/Config.cpp +++ b/GViewCore/src/View/DissasmViewer/Config.cpp @@ -1,14 +1,17 @@ -#include "DissasmViewer.hpp" +#include "Config.hpp" using namespace GView::View::DissasmViewer; using namespace AppCUI::Input; +using namespace AppCUI::Graphics; +using AppCUI::Graphics::Color; -void Config::Update(IniSection sect) +void Config::Update(AppCUI::Utils::IniSection sect) { - sect.UpdateValue("AddNewType", Key::F5, true); - sect.UpdateValue("ShowFileContentKey", Key::F9, true); + for (const auto& cmd : AllKeyboardCommands) { + sect.UpdateValue(cmd.get().Caption, cmd.get().Key, true); + } + sect.UpdateValue("ShowFileContent", true, true); - sect.UpdateValue("AsmExportToFile", Key::F8, true); sect.UpdateValue("DeepScanDissasmOnStart", false, true); } void Config::Initialize() @@ -38,30 +41,19 @@ void Config::Initialize() bool foundSettings = false; auto ini = AppCUI::Application::GetAppSettings(); - if (ini) - { + if (ini) { auto sect = ini->GetSection("DissasmView"); - if (sect.Exists()) - { - this->Keys.AddNewType = sect.GetValue("AddNewType").ToKey(Key::F6); - this->Keys.ShowFileContentKey = sect.GetValue("ShowFileContentKey").ToKey(Key::F9); - this->Keys.ExportAsmToFile = sect.GetValue("AsmExportToFile").ToKey(Key::F8); - this->Keys.JumpBack = sect.GetValue("JumpBack").ToKey(Key::Ctrl | Key::Q); - this->Keys.JumpForward = sect.GetValue("JumpForward").ToKey(Key::Ctrl | Key::E); - this->Keys.DissasmGotoEntrypoint = sect.GetValue("Entrypoint").ToKey(Key::F2); + if (sect.Exists()) { + for (auto& cmd : AllKeyboardCommands) { + cmd.get().Key = sect.GetValue(cmd.get().Caption).ToKey(cmd.get().Key); + } + this->ShowFileContent = sect.GetValue("ShowFileContent").ToBool(true); this->EnableDeepScanDissasmOnStart = sect.GetValue("DeepScanDissasmOnStart").ToBool(false); foundSettings = true; } } - if (!foundSettings) - { - this->Keys.AddNewType = Key::F6; - this->Keys.ShowFileContentKey = Key::F9; - this->Keys.ExportAsmToFile = Key::F8; - this->Keys.JumpBack = Key::Ctrl | Key::Q; - this->Keys.JumpForward = Key::Ctrl | Key::E; - this->Keys.DissasmGotoEntrypoint = Key::F2; + if (!foundSettings) { this->ShowFileContent = true; this->EnableDeepScanDissasmOnStart = false; } diff --git a/GViewCore/src/View/DissasmViewer/Config.hpp b/GViewCore/src/View/DissasmViewer/Config.hpp new file mode 100644 index 00000000..f8f02f14 --- /dev/null +++ b/GViewCore/src/View/DissasmViewer/Config.hpp @@ -0,0 +1,110 @@ +#pragma once + +#include +#include +using AppCUI::uint32; +constexpr uint32 COMMAND_ADD_NEW_TYPE = 100; +constexpr uint32 COMMAND_ADD_SHOW_FILE_CONTENT = 101; +constexpr uint32 COMMAND_EXPORT_ASM_FILE = 102; +constexpr uint32 COMMAND_JUMP_BACK = 103; +constexpr uint32 COMMAND_JUMP_FORWARD = 104; +constexpr uint32 COMMAND_DISSAM_GOTO_ENTRYPOINT = 105; +constexpr uint32 COMMAND_ADD_OR_EDIT_COMMENT = 106; +constexpr uint32 COMMAND_REMOVE_COMMENT = 107; + +using AppCUI::int32; +// TODO: reenable +constexpr int32 RIGHT_CLICK_MENU_CMD_NEW = 0; +constexpr int32 RIGHT_CLICK_MENU_CMD_EDIT = 1; +constexpr int32 RIGHT_CLICK_MENU_CMD_DELETE = 2; + +constexpr int32 RIGHT_CLICK_MENU_CMD_COLLAPSE = 3; +constexpr int32 RIGHT_CLICK_ADD_COMMENT = 4; +constexpr int32 RIGHT_CLICK_REMOVE_COMMENT = 5; +constexpr int32 RIGHT_CLICK_DISSASM_ADD_ZONE = 6; +constexpr int32 RIGHT_CLICK_DISSASM_REMOVE_ZONE = 7; + +namespace GView +{ +namespace View +{ + namespace DissasmViewer + { + using namespace AppCUI; + + struct Config { + struct { + Graphics::ColorPair Normal; + Graphics::ColorPair Highlight; + Graphics::ColorPair HighlightCursorLine; + Graphics::ColorPair Inactive; + Graphics::ColorPair Cursor; + Graphics::ColorPair Line; + Graphics::ColorPair Selection; + Graphics::ColorPair OutsideZone; + Graphics::ColorPair StructureColor; + Graphics::ColorPair DataTypeColor; + Graphics::ColorPair AsmOffsetColor; // 0xsomthing + Graphics::ColorPair AsmIrrelevantInstructionColor; // int3 + Graphics::ColorPair AsmWorkRegisterColor; // eax, ebx,ecx, edx + Graphics::ColorPair AsmStackRegisterColor; // ebp, edi, esi + Graphics::ColorPair AsmCompareInstructionColor; // test, cmp + Graphics::ColorPair AsmFunctionColor; // ret call + Graphics::ColorPair AsmLocationInstruction; // dword ptr[ ] + Graphics::ColorPair AsmJumpInstruction; // jmp + Graphics::ColorPair AsmComment; // comments added by user + Graphics::ColorPair AsmDefaultColor; // rest of things + Graphics::ColorPair AsmTitleColor; + Graphics::ColorPair AsmTitleColumnColor; + } Colors; + + struct DissasmCommand { + Input::Key Key; + const char* Caption; + const char* Explanation; + uint32 CommandId; + }; + + // Command Bar keys + inline static DissasmCommand AddNewTypeCommand = { Input::Key::F6, "AddNewType", "Add new data type", COMMAND_ADD_NEW_TYPE }; + inline static DissasmCommand ShowOrHideFileContentCommand = { + Input::Key::F9, "ShowOrHideFileContent", "Show or hide file content", COMMAND_ADD_SHOW_FILE_CONTENT + }; + inline static DissasmCommand AsmExportFileContentCommand = { + Input::Key::F8, "AsmExportToFile", "Export ASM content to file", COMMAND_EXPORT_ASM_FILE + }; + inline static DissasmCommand JumpBackCommand = { Input::Key::Ctrl | Input::Key::Q, "JumpBack", "Jump to previous location", COMMAND_JUMP_BACK }; + inline static DissasmCommand JumpForwardCommand = { + Input::Key::Ctrl | Input::Key::E, "JumpForward", "Jump to a forward location", COMMAND_JUMP_FORWARD + }; + inline static DissasmCommand GotoEntrypointCommand = { + Input::Key::F2, "GoToEntrypoint", "Go to the entry point of the dissasm zone", COMMAND_DISSAM_GOTO_ENTRYPOINT + }; + + inline static std::array, 6> CommandBarCommands = { + AddNewTypeCommand, ShowOrHideFileContentCommand, AsmExportFileContentCommand, JumpBackCommand, JumpForwardCommand, GotoEntrypointCommand + }; + + // Other keys + inline static DissasmCommand AddOrEditCommentCommand = { Input::Key::C, "AddOrEditComment", "Add or edit comments", COMMAND_ADD_OR_EDIT_COMMENT }; + inline static DissasmCommand RemoveCommentCommand = { Input::Key::Delete, "RemoveComment", "Remove comment", COMMAND_REMOVE_COMMENT }; + inline static DissasmCommand RenameLabelCommand = { Input::Key::N, "RenameLabel", "Rename label or function", COMMAND_REMOVE_COMMENT }; + + inline static std::array, 3> KeyDownCommands = { AddOrEditCommentCommand, + RemoveCommentCommand, + RenameLabelCommand }; + + inline static std::array, 8> AllKeyboardCommands = { + AddNewTypeCommand, ShowOrHideFileContentCommand, AsmExportFileContentCommand, JumpBackCommand, + JumpForwardCommand, GotoEntrypointCommand, AddOrEditCommentCommand, RemoveCommentCommand + }; + bool Loaded; + + bool ShowFileContent; + bool EnableDeepScanDissasmOnStart; + static void Update(AppCUI::Utils::IniSection sect); + void Initialize(); + }; + } // namespace DissasmViewer +} // namespace View +} // namespace GView \ No newline at end of file diff --git a/GViewCore/src/View/DissasmViewer/DissasmKeyEvents.cpp b/GViewCore/src/View/DissasmViewer/DissasmKeyEvents.cpp index 9f3b6ad0..7481063e 100644 --- a/GViewCore/src/View/DissasmViewer/DissasmKeyEvents.cpp +++ b/GViewCore/src/View/DissasmViewer/DissasmKeyEvents.cpp @@ -1,34 +1,28 @@ #include "DissasmViewer.hpp" -#include "DissasmKeys.hpp" #include - using namespace GView::View::DissasmViewer; using namespace AppCUI::Input; void Instance::AnalyzeMousePosition(int x, int y, MousePositionInfo& mpInfo) { mpInfo.location = MouseLocation::Outside; - if (y < 0) - { + if (y < 0) { mpInfo.location = MouseLocation::Outside; return; } - if (y == 0) - { + if (y == 0) { mpInfo.location = MouseLocation::OnHeader; return; } // y>=1 --> check if in buffer auto yPoz = y - 1; - if (x < 0) - { + if (x < 0) { mpInfo.location = MouseLocation::Outside; return; } const auto xPoz = static_cast(x); - if ((xPoz >= Layout.startingTextLineOffset) && (xPoz < Layout.startingTextLineOffset + Layout.textSize)) - { + if ((xPoz >= Layout.startingTextLineOffset) && (xPoz < Layout.startingTextLineOffset + Layout.textSize)) { mpInfo.location = MouseLocation::OnView; mpInfo.offset = xPoz - Layout.startingTextLineOffset; mpInfo.lines = yPoz; @@ -60,8 +54,7 @@ void Instance::MoveTo(int32 offset, int32 lines, AppCUI::Input::Key key, bool se MoveScrollTo(offset, lines); - if ((select) && (zoneId >= 0)) - { + if ((select) && (zoneId >= 0)) { this->selection.UpdateSelection(zoneId, Cursor.ToLinePosition(), ctrl_down, alt_down); // UpdateCurrentSelection(); } @@ -100,28 +93,20 @@ void Instance::MoveScrollTo(int32 offset, int32 lines) Cursor.offset += offset; // this->Cursor.startViewLine += lines; - if (lines < 0) - { - if (lines * -1 >= static_cast(Cursor.lineInView)) - { + if (lines < 0) { + if (lines * -1 >= static_cast(Cursor.lineInView)) { lines += static_cast(Cursor.lineInView); Cursor.lineInView = 0; - if (lines != 0) - { + if (lines != 0) { Cursor.startViewLine += lines; Cursor.hasMovedView = true; } - } - else - { + } else { Cursor.lineInView += lines; } - } - else - { + } else { Cursor.lineInView += lines; - if (Cursor.lineInView > Layout.visibleRows - 1) - { + if (Cursor.lineInView > Layout.visibleRows - 1) { const auto diff = abs(static_cast(Cursor.lineInView) - static_cast(Layout.visibleRows - 1)); Cursor.startViewLine += diff; Cursor.lineInView -= diff; @@ -145,25 +130,18 @@ void Instance::OnMousePressed(int x, int y, Input::MouseButton button, Input::Ke MousePositionInfo mpInfo; AnalyzeMousePosition(x, y, mpInfo); // make sure that consecutive click on the same location will not scroll the view to that location - if (mpInfo.location == MouseLocation::OnView) - { - if (button == MouseButton::Left && (mpInfo.lines != Cursor.lineInView || mpInfo.offset != Cursor.offset)) - { + if (mpInfo.location == MouseLocation::OnView) { + if (button == MouseButton::Left && (mpInfo.lines != Cursor.lineInView || mpInfo.offset != Cursor.offset)) { const int32 linesDiff = static_cast(mpInfo.lines) - Cursor.lineInView; const int32 offsetDiff = static_cast(mpInfo.offset) - Cursor.offset; MoveTo(offsetDiff, linesDiff, keyCode, false); - } - else if (button == MouseButton::Right) - { + } else if (button == MouseButton::Right) { // rightClickOffset = mpInfo.bufferOffset; rightClickMenu.Show(this, x, y); } - } - else if (mpInfo.location == MouseLocation::Outside && !MyLine.buttons.empty()) - { + } else if (mpInfo.location == MouseLocation::Outside && !MyLine.buttons.empty()) { for (const auto& btn : MyLine.buttons) - if (btn.x == x && btn.y == y) - { + if (btn.x == x && btn.y == y) { ChangeZoneCollapseState(btn.zone); break; } @@ -175,8 +153,7 @@ bool Instance::OnMouseDrag(int x, int y, Input::MouseButton button, Input::Key k MousePositionInfo mpInfo; AnalyzeMousePosition(x, y, mpInfo); // make sure that consecutive click on the same location will not scroll the view to that location - if (button == MouseButton::Left && mpInfo.location == MouseLocation::OnView && (mpInfo.lines != Cursor.lineInView || mpInfo.offset != Cursor.offset)) - { + if (button == MouseButton::Left && mpInfo.location == MouseLocation::OnView && (mpInfo.lines != Cursor.lineInView || mpInfo.offset != Cursor.offset)) { const int32 linesDiff = static_cast(mpInfo.lines) - Cursor.lineInView; const int32 offsetDiff = static_cast(mpInfo.offset) - Cursor.offset; MoveTo(offsetDiff, linesDiff, keyCode, true); @@ -187,8 +164,7 @@ bool Instance::OnMouseDrag(int x, int y, Input::MouseButton button, Input::Key k bool Instance::OnMouseWheel(int, int, Input::MouseWheel direction, Input::Key) { - switch (direction) - { + switch (direction) { case MouseWheel::Up: return OnKeyEvent(Key::Up | Key::Ctrl, false); case MouseWheel::Down: @@ -208,8 +184,7 @@ bool Instance::OnKeyEvent(AppCUI::Input::Key keyCode, char16 charCode) if (select) keyCode = static_cast((uint32) keyCode - (uint32) Key::Shift); - switch (keyCode) - { + switch (keyCode) { case Key::Down: if (Cursor.startViewLine + Cursor.lineInView + 1 <= Layout.totalLinesSize) MoveTo(0, 1, keyCode, select); @@ -264,9 +239,6 @@ bool Instance::OnKeyEvent(AppCUI::Input::Key keyCode, char16 charCode) if (this->Cursor.offset < Layout.textSize) MoveScrollTo(1, 0); return true; - case Key::Delete: - RemoveComment(); - return true; case Key::Space: ProcessSpaceKey(); return true; @@ -274,34 +246,36 @@ bool Instance::OnKeyEvent(AppCUI::Input::Key keyCode, char16 charCode) OpenCurrentSelection(); return true; } - if (charCode == ';') - { + + if (keyCode == Config::AddOrEditCommentCommand.Key) { AddComment(); return true; } + if (keyCode == Config::RemoveCommentCommand.Key) { + RemoveComment(); + return true; + } + + if (keyCode == Config::RenameLabelCommand.Key) { + RenameLabel(); + return true; + } return ViewControl::OnKeyEvent(select ? (keyCode | Key::Shift) : keyCode, charCode); } bool Instance::OnUpdateCommandBar(AppCUI::Application::CommandBar& commandBar) { - const AppCUI::Utils::ConstString ShowFileContentText = config.ShowFileContent ? "ShowFileContent" : "HideFileContent"; - commandBar.SetCommand(config.Keys.AddNewType, "AddNewType", COMMAND_ADD_NEW_TYPE); - commandBar.SetCommand(config.Keys.ShowFileContentKey, ShowFileContentText, COMMAND_ADD_SHOW_FILE_CONTENT); - commandBar.SetCommand(config.Keys.ExportAsmToFile, "Export asm file", COMMAND_EXPORT_ASM_FILE); - commandBar.SetCommand(config.Keys.JumpBack, "Jump back", COMMAND_JUMP_BACK); - commandBar.SetCommand(config.Keys.JumpForward, "Jump forward", COMMAND_JUMP_FORWARD); - commandBar.SetCommand(config.Keys.DissasmGotoEntrypoint, "Entry point", COMMAND_DISSAM_GOTO_ENTRYPOINT); + for (const auto& cmd : config.CommandBarCommands) + commandBar.SetCommand(cmd.get().Key, cmd.get().Caption, cmd.get().CommandId); return false; } bool Instance::OnEvent(Reference, Event eventType, int ID) { - if (eventType == Event::Command) - { - switch (ID) - { + if (eventType == Event::Command) { + switch (ID) { case COMMAND_ADD_NEW_TYPE: Dialogs::MessageBox::ShowNotification("Info", "OK!"); return true; @@ -327,20 +301,17 @@ bool Instance::OnEvent(Reference, Event eventType, int ID) case RIGHT_CLICK_DISSASM_REMOVE_ZONE: CommandDissasmRemoveZone(); return true; - case COMMAND_JUMP_BACK: - { + case COMMAND_JUMP_BACK: { if (const auto [canJump, location] = jumps_holder.JumpBack(); canJump) Cursor.restorePosition(location); return true; } - case COMMAND_JUMP_FORWARD: - { + case COMMAND_JUMP_FORWARD: { if (const auto [canJump, location] = jumps_holder.JumpFront(); canJump) Cursor.restorePosition(location); return true; } - case COMMAND_DISSAM_GOTO_ENTRYPOINT: - { + case COMMAND_DISSAM_GOTO_ENTRYPOINT: { ProcessSpaceKey(true); return true; } diff --git a/GViewCore/src/View/DissasmViewer/DissasmKeys.cpp b/GViewCore/src/View/DissasmViewer/DissasmKeys.cpp deleted file mode 100644 index ffd01c02..00000000 --- a/GViewCore/src/View/DissasmViewer/DissasmKeys.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "DissasmKeys.hpp" \ No newline at end of file diff --git a/GViewCore/src/View/DissasmViewer/DissasmKeys.hpp b/GViewCore/src/View/DissasmViewer/DissasmKeys.hpp deleted file mode 100644 index 832bbcf4..00000000 --- a/GViewCore/src/View/DissasmViewer/DissasmKeys.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include -using AppCUI::uint32; -constexpr uint32 COMMAND_ADD_NEW_TYPE = 100; -constexpr uint32 COMMAND_ADD_SHOW_FILE_CONTENT = 101; -constexpr uint32 COMMAND_EXPORT_ASM_FILE = 102; -constexpr uint32 COMMAND_JUMP_BACK = 103; -constexpr uint32 COMMAND_JUMP_FORWARD = 104; -constexpr uint32 COMMAND_DISSAM_GOTO_ENTRYPOINT = 105; - -using AppCUI::int32; -//TODO: reenable -constexpr int32 RIGHT_CLICK_MENU_CMD_NEW = 0; -constexpr int32 RIGHT_CLICK_MENU_CMD_EDIT = 1; -constexpr int32 RIGHT_CLICK_MENU_CMD_DELETE = 2; - -constexpr int32 RIGHT_CLICK_MENU_CMD_COLLAPSE = 3; -constexpr int32 RIGHT_CLICK_ADD_COMMENT = 4; -constexpr int32 RIGHT_CLICK_REMOVE_COMMENT = 5; -constexpr int32 RIGHT_CLICK_DISSASM_ADD_ZONE = 6; -constexpr int32 RIGHT_CLICK_DISSASM_REMOVE_ZONE = 7; diff --git a/GViewCore/src/View/DissasmViewer/DissasmProperties.cpp b/GViewCore/src/View/DissasmViewer/DissasmProperties.cpp index ee667873..0d60a69c 100644 --- a/GViewCore/src/View/DissasmViewer/DissasmProperties.cpp +++ b/GViewCore/src/View/DissasmViewer/DissasmProperties.cpp @@ -13,7 +13,7 @@ bool Instance::GetPropertyValue(uint32 propertyID, PropertyValue& value) switch (propertyID) { case PROP_ID_ADD_NEW_TYPE: - value = config.Keys.AddNewType; + value = config.AddNewTypeCommand.Key; return true; case PROP_ID_DISSASM_LANGUAGE: value = static_cast(settings->defaultLanguage); @@ -22,7 +22,7 @@ bool Instance::GetPropertyValue(uint32 propertyID, PropertyValue& value) value = config.ShowFileContent; return true; case PROP_ID_SHOW_FILE_CONTENT_KEY: - value = config.Keys.ShowFileContentKey; + value = config.ShowOrHideFileContentCommand.Key; return true; } return false; @@ -32,7 +32,7 @@ bool Instance::SetPropertyValue(uint32 propertyID, const PropertyValue& value, S switch (propertyID) { case PROP_ID_ADD_NEW_TYPE: - config.Keys.AddNewType = std::get(value); + config.AddNewTypeCommand.Key = std::get(value); return true; case PROP_ID_DISSASM_LANGUAGE: settings->defaultLanguage = static_cast(std::get(value)); @@ -41,7 +41,7 @@ bool Instance::SetPropertyValue(uint32 propertyID, const PropertyValue& value, S config.ShowFileContent = std::get(value); return true; case PROP_ID_SHOW_FILE_CONTENT_KEY: - config.Keys.ShowFileContentKey = std::get(value); + config.ShowOrHideFileContentCommand.Key= std::get(value); return true; } return false; diff --git a/GViewCore/src/View/DissasmViewer/DissasmViewer.hpp b/GViewCore/src/View/DissasmViewer/DissasmViewer.hpp index 5a7e1874..e134258a 100644 --- a/GViewCore/src/View/DissasmViewer/DissasmViewer.hpp +++ b/GViewCore/src/View/DissasmViewer/DissasmViewer.hpp @@ -11,6 +11,7 @@ #include "AdvancedSelection.hpp" #include "DissasmDataTypes.hpp" +#include "Config.hpp" namespace GView { @@ -26,60 +27,14 @@ namespace View static constexpr size_t DISSAM_MINIMUM_COMMENTS_X = 50; static constexpr size_t DISSAM_MAXIMUM_STRING_PREVIEW = 90; - struct Config - { - struct - { - ColorPair Normal; - ColorPair Highlight; - ColorPair HighlightCursorLine; - ColorPair Inactive; - ColorPair Cursor; - ColorPair Line; - ColorPair Selection; - ColorPair OutsideZone; - ColorPair StructureColor; - ColorPair DataTypeColor; - ColorPair AsmOffsetColor; // 0xsomthing - ColorPair AsmIrrelevantInstructionColor; // int3 - ColorPair AsmWorkRegisterColor; // eax, ebx,ecx, edx - ColorPair AsmStackRegisterColor; // ebp, edi, esi - ColorPair AsmCompareInstructionColor; // test, cmp - ColorPair AsmFunctionColor; // ret call - ColorPair AsmLocationInstruction; // dword ptr[ ] - ColorPair AsmJumpInstruction; // jmp - ColorPair AsmComment; // comments added by user - ColorPair AsmDefaultColor; // rest of things - ColorPair AsmTitleColor; - ColorPair AsmTitleColumnColor; - } Colors; - struct - { - AppCUI::Input::Key AddNewType; - AppCUI::Input::Key ShowFileContentKey; - AppCUI::Input::Key ExportAsmToFile; - AppCUI::Input::Key JumpBack; - AppCUI::Input::Key JumpForward; - AppCUI::Input::Key DissasmGotoEntrypoint; - } Keys; - bool Loaded; - - bool ShowFileContent; - bool EnableDeepScanDissasmOnStart; - static void Update(IniSection sect); - void Initialize(); - }; - - struct DisassemblyZone - { + struct DisassemblyZone { uint64 startingZonePoint; uint64 size; uint64 entryPoint; DisassemblyLanguage language; }; - enum class InternalDissasmType : uint8 - { + enum class InternalDissasmType : uint8 { UInt8, UInt16, UInt32, @@ -97,8 +52,7 @@ namespace View CustomTypesStartingId }; - struct DissasmStructureType - { + struct DissasmStructureType { InternalDissasmType primaryType; std::string_view name; @@ -110,15 +64,9 @@ namespace View uint32 GetExpandedSize() const; }; - enum class DissasmParseZoneType : uint8 - { - StructureParseZone, - DissasmCodeParseZone, - CollapsibleAndTextZone - }; + enum class DissasmParseZoneType : uint8 { StructureParseZone, DissasmCodeParseZone, CollapsibleAndTextZone }; - struct ParseZone - { + struct ParseZone { uint32 startLineIndex; uint32 endingLineIndex; uint32 extendedSize; @@ -129,8 +77,7 @@ namespace View DissasmParseZoneType zoneType; }; - struct DissasmParseStructureZone : public ParseZone - { + struct DissasmParseStructureZone : public ParseZone { int16 structureIndex; DissasmStructureType dissasmType; std::list> types; @@ -139,37 +86,26 @@ namespace View uint64 initialTextFileOffset; }; - struct CollapsibleAndTextData - { + struct CollapsibleAndTextData { uint64 startingOffset; uint64 size; bool canBeCollapsed; }; - struct CollapsibleAndTextZone : public ParseZone - { + struct CollapsibleAndTextZone : public ParseZone { CollapsibleAndTextData data; }; - struct AsmOffsetLine - { + struct AsmOffsetLine { uint64 offset; uint32 line; }; - struct DissasmAsmPreCacheLine - { - enum InstructionFlag : uint8 - { - NoneFlag = 0x00, - CallFlag = 0x1, - PushFlag = 0x2, - JmpFlag = 0x4 - }; + struct DissasmAsmPreCacheLine { + enum InstructionFlag : uint8 { NoneFlag = 0x00, CallFlag = 0x1, PushFlag = 0x2, JmpFlag = 0x4 }; - enum LineArrowToDrawFlag : uint8 - { + enum LineArrowToDrawFlag : uint8 { NoLines = 0x00, DrawLine1 = 0x1, DrawLine2 = 0x2, @@ -199,10 +135,8 @@ namespace View } }; - struct AsmFunctionDetails - { - struct NameType - { + struct AsmFunctionDetails { + struct NameType { const char* name; const char* type; }; @@ -211,8 +145,7 @@ namespace View std::vector params; }; - struct DissasmAsmPreCacheData - { + struct DissasmAsmPreCacheData { std::vector cachedAsmLines; std::unordered_map instructionFlags; uint16 index; @@ -245,8 +178,7 @@ namespace View void ComputeMaxLine() { maxLineSize = 0; - for (const auto& cachedLine : cachedAsmLines) - { + for (const auto& cachedLine : cachedAsmLines) { const auto lineSize = cachedLine.GetLineSize(); if (lineSize > maxLineSize) maxLineSize = lineSize; @@ -288,8 +220,7 @@ namespace View uint32 linesToPrepare); }; - struct DissasmCodeInternalType - { + struct DissasmCodeInternalType { uint32 indexZoneStart; uint32 indexZoneEnd; @@ -318,8 +249,7 @@ namespace View } }; - struct DissasmComments - { + struct DissasmComments { std::unordered_map comments; void AddOrUpdateComment(uint32 line, std::string comment); @@ -328,8 +258,7 @@ namespace View void AdjustCommentsOffsets(uint32 changedLine, bool isAddedLine); }; - struct DissasmCodeZone : public ParseZone - { + struct DissasmCodeZone : public ParseZone { uint32 lastDrawnLine; // optimization not to recompute buffer every time uint32 lastClosestLine; uint32 offsetCacheMaxLine; @@ -352,22 +281,15 @@ namespace View bool isInit; }; - struct MemoryMappingEntry - { + struct MemoryMappingEntry { std::string name; MemoryMappingType type; }; // TODO: improve to be more generic! - enum class DissasmPEConversionType : uint8 - { - FileOffset = 0, - RVA = 1, - VA = 2 - }; + enum class DissasmPEConversionType : uint8 { FileOffset = 0, RVA = 1, VA = 2 }; - struct SettingsData - { + struct SettingsData { String name; DisassemblyLanguage defaultLanguage; @@ -386,8 +308,7 @@ namespace View SettingsData(); }; - struct LayoutDissasm - { + struct LayoutDissasm { uint32 visibleRows; uint32 totalCharactersPerLine; uint32 textSize; // charactersPerLine minus the left parts @@ -397,15 +318,13 @@ namespace View uint32 totalLinesSize; }; - struct AsmData - { + struct AsmData { std::map instructionToColor; std::unordered_map functions; std::deque zonesToClear; }; - struct DrawLineInfo - { + struct DrawLineInfo { const uint8* start; const uint8* end; Character* chLineStart; @@ -430,8 +349,7 @@ namespace View void WriteErrorToScreen(std::string_view error) const; }; - struct CursorState - { + struct CursorState { uint32 startViewLine, lineInView; bool operator==(const CursorState& other) const @@ -455,8 +373,7 @@ namespace View void insert(CursorState&& newState) { for (uint32 i = 0u; i < jumps.size(); i++) - if (jumps[i] == newState) - { + if (jumps[i] == newState) { current_index = i; return; } @@ -483,21 +400,14 @@ namespace View class Instance : public View::ViewControl { - enum class MouseLocation : uint8 - { - OnView, - OnHeader, - Outside - }; - struct MousePositionInfo - { + enum class MouseLocation : uint8 { OnView, OnHeader, Outside }; + struct MousePositionInfo { MouseLocation location; uint32 lines; uint32 offset; }; - struct CursorDissasm - { + struct CursorDissasm { uint32 startViewLine, lineInView, offset; [[nodiscard]] LinePosition ToLinePosition() const; uint64 GetOffset(uint32 textSize) const; @@ -513,23 +423,20 @@ namespace View bool hasMovedView; } Cursor; - struct - { + struct { ColorPair Normal, Line, Highlighted; } CursorColors; LayoutDissasm Layout; - struct - { + struct { // uint8 buffer[256]; uint32 size; uint64 start, end; // bool highlight; } CurrentSelection; - struct ButtonsData - { + struct ButtonsData { int x; int y; SpecialChars c; @@ -538,13 +445,11 @@ namespace View ParseZone* zone; }; - struct - { + struct { std::vector buttons; } MyLine; - struct ZoneLocation - { + struct ZoneLocation { uint32 zoneIndex; uint32 startingLine; uint32 endingLine; @@ -608,6 +513,7 @@ namespace View void AddNewCollapsibleZone(); void AddComment(); void RemoveComment(); + void RenameLabel(); void CommandExportAsmFile(); void ProcessSpaceKey(bool goToEntryPoint = false); void CommandDissasmAddZone(); @@ -648,15 +554,15 @@ namespace View virtual const vector GetPropertiesList() override; }; // Instance - class CommentDataWindow : public Window + class SingleLineEditWindow : public Window { std::string data; - Reference commentTextField; + Reference textField; void Validate(); public: - CommentDataWindow(std::string initialComment); + SingleLineEditWindow(std::string initialText,const char* title); virtual bool OnEvent(Reference, Event eventType, int ID) override; inline std::string GetResult() const { diff --git a/GViewCore/src/View/DissasmViewer/DissasmX86.cpp b/GViewCore/src/View/DissasmViewer/DissasmX86.cpp index 36aa214b..629d0173 100644 --- a/GViewCore/src/View/DissasmViewer/DissasmX86.cpp +++ b/GViewCore/src/View/DissasmViewer/DissasmX86.cpp @@ -682,7 +682,7 @@ inline bool ExtractCallsToInsertFunctionNames( { if (value < offsets[0].offset) value += offsets[0].offset; - const char* prefix = isJump ? "jmp_0x" : "sub_0x"; + const char* prefix = isJump ? "offset_0x" : "sub_0x"; auto callName = FormatFunctionName(value, prefix); callsFound.emplace_back(value, callName.GetText()); } diff --git a/GViewCore/src/View/DissasmViewer/Instance.cpp b/GViewCore/src/View/DissasmViewer/Instance.cpp index 17ab42ce..768f7588 100644 --- a/GViewCore/src/View/DissasmViewer/Instance.cpp +++ b/GViewCore/src/View/DissasmViewer/Instance.cpp @@ -1,7 +1,6 @@ #include #include "DissasmViewer.hpp" -#include "DissasmKeys.hpp" #include #include @@ -34,8 +33,7 @@ const std::array KNOWN_FUNCTIONS = { { "MessageBoxA", { { "hWnd", "HWND" }, { "lpText", "LPCTSTR" }, { "lpCaption", "LPCTSTR" }, { "uType", "UINT" } } } } }; -struct -{ +struct { int commandID; string_view text; // Input::Key shortcutKey = Input::Key::None; @@ -55,14 +53,11 @@ Instance::Instance(Reference obj, Settings* _settings) { this->chars.Fill('*', 1024, ColorPair{ Color::Black, Color::DarkBlue }); // settings - if ((_settings) && (_settings->data)) - { + if ((_settings) && (_settings->data)) { // move settings data pointer this->settings.reset((SettingsData*) _settings->data); //_settings->data = nullptr; //TODO: is this ok? - } - else - { + } else { this->settings.reset(new SettingsData()); } @@ -90,8 +85,7 @@ Instance::Instance(Reference obj, Settings* _settings) this->codePage = CodePageID::DOS_437; - for (auto& menu_command : RIGHT_CLICK_MENU_COMMANDS) - { + for (auto& menu_command : RIGHT_CLICK_MENU_COMMANDS) { menu_command.handle = rightClickMenu.AddCommandItem(menu_command.text, menu_command.commandID); } // rightClickOffset = 0; @@ -134,8 +128,7 @@ void Instance::PaintCursorInformation(AppCUI::Graphics::Renderer& renderer, uint { this->CursorColors.Normal = config.Colors.Normal; this->CursorColors.Highlighted = config.Colors.Highlight; - if (!this->HasFocus()) - { + if (!this->HasFocus()) { this->CursorColors.Normal = config.Colors.Inactive; this->CursorColors.Highlighted = config.Colors.Inactive; } @@ -146,8 +139,7 @@ void Instance::PaintCursorInformation(AppCUI::Graphics::Renderer& renderer, uint return; int x = 0; - switch (height) - { + switch (height) { case 0: break; case 1: @@ -171,14 +163,11 @@ int Instance::PrintCursorPosInfo(int x, int y, uint32 width, bool addSeparator, if (addSeparator) r.WriteSpecialCharacter(x++, y, SpecialChars::BoxVerticalSingleLine, this->CursorColors.Line); - if (Layout.totalLinesSize > 0) - { + if (Layout.totalLinesSize > 0) { LocalString<32> tmp; tmp.Format("%3u%%", (static_cast(Cursor.startViewLine) + Cursor.lineInView) * 100ULL / Layout.totalLinesSize); r.WriteSingleLineText(x, y, tmp.GetText(), this->CursorColors.Normal); - } - else - { + } else { r.WriteSingleLineText(x, y, " 0%", this->CursorColors.Line); } r.WriteSpecialCharacter(x + 4, y, SpecialChars::BoxVerticalSingleLine, this->CursorColors.Line); @@ -263,22 +252,19 @@ void Instance::AddComment() const uint64 offsetEnd = offsetStart + 1; const auto zonesFound = GetZonesIndexesFromPosition(offsetStart, offsetEnd); - if (zonesFound.empty() || zonesFound.size() != 1) - { + if (zonesFound.empty() || zonesFound.size() != 1) { Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); return; } const auto& zone = settings->parseZones[zonesFound[0].zoneIndex]; - if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) - { + if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) { Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); return; } uint32 startingLine = zonesFound[0].startingLine; - if (startingLine == 0 || startingLine == 1) - { + if (startingLine == 0 || startingLine == 1) { Dialogs::MessageBox::ShowNotification("Warning", "Please add comment inside the region, not on the title!"); return; } @@ -291,9 +277,8 @@ void Instance::AddComment() convertedZone->comments.HasComment(startingLine, foundComment); selection.Clear(); - CommentDataWindow dlg(foundComment); - if (dlg.Show() == Dialogs::Result::Ok) - { + SingleLineEditWindow dlg(foundComment,"Add Comment"); + if (dlg.Show() == Dialogs::Result::Ok) { convertedZone->comments.AddOrUpdateComment(startingLine, dlg.GetResult()); } } @@ -305,33 +290,76 @@ void Instance::RemoveComment() const uint64 offsetEnd = offsetStart + 1; const auto zonesFound = GetZonesIndexesFromPosition(offsetStart, offsetEnd); - if (zonesFound.empty() || zonesFound.size() != 1) - { + if (zonesFound.empty() || zonesFound.size() != 1) { Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); return; } const auto& zone = settings->parseZones[zonesFound[0].zoneIndex]; - if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) - { + if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) { Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); return; } - if (zonesFound[0].startingLine == 0) - { + uint32 startingLine = zonesFound[0].startingLine; + if (startingLine == 0 || startingLine == 1) { + Dialogs::MessageBox::ShowNotification("Warning", "Please remove comment inside the region, not on the title!"); + return; + } + startingLine--; + + const auto convertedZone = static_cast(zone.get()); + convertedZone->comments.RemoveComment(startingLine); +} + +void Instance::RenameLabel() +{ + const uint64 offsetStart = Cursor.GetOffset(Layout.textSize); + const uint64 offsetEnd = offsetStart + 1; + + const auto zonesFound = GetZonesIndexesFromPosition(offsetStart, offsetEnd); + if (zonesFound.empty() || zonesFound.size() != 1) { + Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); + return; + } + + const auto& zone = settings->parseZones[zonesFound[0].zoneIndex]; + if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) { + Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); + return; + } + + uint32 startingLine = zonesFound[0].startingLine; + if (startingLine == 0 || startingLine == 1) { Dialogs::MessageBox::ShowNotification("Warning", "Please add comment inside the region, not on the title!"); return; } + startingLine--; const auto convertedZone = static_cast(zone.get()); - convertedZone->comments.RemoveComment(zonesFound[0].startingLine); + startingLine = startingLine - 1; + + // TODO: improve, add searching function to search inside types for the current annotation + auto& annotations = convertedZone->types.back().get().annotations; + auto it = annotations.find(startingLine); + if (it == annotations.end()) { + Dialogs::MessageBox::ShowNotification("Warning", "That line cannot pe renamed!"); + return; + } + + selection.Clear(); + SingleLineEditWindow dlg(it->second.first, "Edit label"); + if (dlg.Show() == Dialogs::Result::Ok) { + const auto res = dlg.GetResult(); + if (!res.empty()) + it->second.first = res; + } + convertedZone->asmPreCacheData.Clear(); } bool Instance::PrepareDrawLineInfo(DrawLineInfo& dli) { - if (dli.recomputeOffsets) - { + if (dli.recomputeOffsets) { this->chars.Resize(Layout.totalCharactersPerLine); dli.recomputeOffsets = false; dli.currentLineFromOffset = this->Cursor.startViewLine; @@ -343,19 +371,15 @@ bool Instance::PrepareDrawLineInfo(DrawLineInfo& dli) // TODO: current algorithm is build with ordered index values, could be improved later with a binary search const uint32 currentLineIndex = dli.currentLineFromOffset + dli.screenLineToDraw; - if (!settings->parseZones.empty()) - { + if (!settings->parseZones.empty()) { auto& zones = settings->parseZones; uint32 zonesCount = (uint32) settings->parseZones.size(); // TODO: optimization -> instead of search every time keep the last zone index inside memory and search from there - for (uint32 i = 0; i < zonesCount; i++) - { - if ((currentLineIndex >= zones[i]->startLineIndex && currentLineIndex < zones[i]->endingLineIndex)) - { + for (uint32 i = 0; i < zonesCount; i++) { + if ((currentLineIndex >= zones[i]->startLineIndex && currentLineIndex < zones[i]->endingLineIndex)) { // struct dli.textLineToDraw = currentLineIndex - zones[i]->startLineIndex; - switch (zones[i]->zoneType) - { + switch (zones[i]->zoneType) { case DissasmParseZoneType::StructureParseZone: return DrawStructureZone(dli, (DissasmParseStructureZone*) zones[i].get()); @@ -370,15 +394,11 @@ bool Instance::PrepareDrawLineInfo(DrawLineInfo& dli) } } // return true; - if (!zonesCount) - { + if (!zonesCount) { assert(false); } - } - else - { - if (!config.ShowFileContent) - { + } else { + if (!config.ShowFileContent) { dli.renderer.WriteSingleLineText( Layout.startingTextLineOffset, 1, "No structures found an File content is hidden. No content to show.", config.Colors.Normal); return true; @@ -394,8 +414,7 @@ bool Instance::PrepareDrawLineInfo(DrawLineInfo& dli) inline void GView::View::DissasmViewer::Instance::UpdateCurrentZoneIndex( const DissasmStructureType& cType, DissasmParseStructureZone* zone, bool increaseOffset) { - if (cType.primaryType >= InternalDissasmType::UInt8 && cType.primaryType <= InternalDissasmType::Int64) - { + if (cType.primaryType >= InternalDissasmType::UInt8 && cType.primaryType <= InternalDissasmType::Int64) { // Uint8 - index 0 -> 1 byte, Int8 -> index 4 -> 1 byte uint32 val = ((uint8) cType.primaryType + 1u) % 4; if (increaseOffset) @@ -408,10 +427,8 @@ inline void GView::View::DissasmViewer::Instance::UpdateCurrentZoneIndex( bool Instance::DrawStructureZone(DrawLineInfo& dli, DissasmParseStructureZone* structureZone) { - if (structureZone->structureIndex == structureZone->extendedSize) - { - while (structureZone->levels.size() > 1) - { + if (structureZone->structureIndex == structureZone->extendedSize) { + while (structureZone->levels.size() > 1) { structureZone->types.pop_back(); structureZone->levels.pop_back(); } @@ -431,23 +448,18 @@ bool Instance::DrawStructureZone(DrawLineInfo& dli, DissasmParseStructureZone* s // levelToReach = 47; // TODO: consider optimization if the levelToReach > levelNow and levelToReach should reach close to 0 then it all should be reset to 0 - while (levelNow < (int16) levelToReach) - { + while (levelNow < (int16) levelToReach) { const DissasmStructureType& currentType = structureZone->types.back(); int currentLevel = structureZone->levels.back(); - switch (currentType.primaryType) - { + switch (currentType.primaryType) { case InternalDissasmType::UnidimnsionalArray: case InternalDissasmType::UserDefined: - if (currentLevel < currentType.internalTypes.size()) - { + if (currentLevel < currentType.internalTypes.size()) { UpdateCurrentZoneIndex(currentType.internalTypes[currentLevel], structureZone, true); structureZone->types.push_back(currentType.internalTypes[currentLevel]); structureZone->levels.push_back(0); - } - else - { + } else { structureZone->types.pop_back(); structureZone->levels.pop_back(); currentLevel = structureZone->levels.back() + 1; @@ -475,17 +487,14 @@ bool Instance::DrawStructureZone(DrawLineInfo& dli, DissasmParseStructureZone* s bool isFromBreak = false; - while (levelNow > (int16) levelToReach) - { + while (levelNow > (int16) levelToReach) { const DissasmStructureType& currentType = structureZone->types.back(); int currentLevel = structureZone->levels.back(); - switch (currentType.primaryType) - { + switch (currentType.primaryType) { case InternalDissasmType::UnidimnsionalArray: case InternalDissasmType::UserDefined: - if (currentLevel > 0) - { + if (currentLevel > 0) { structureZone->levels.pop_back(); currentLevel--; structureZone->levels.push_back(currentLevel); @@ -495,11 +504,8 @@ bool Instance::DrawStructureZone(DrawLineInfo& dli, DissasmParseStructureZone* s anteiorLevel--; structureZone->levels.push_back(anteiorLevel); isFromBreak = false; - } - else - { - if (isFromBreak) - { + } else { + if (isFromBreak) { isFromBreak = false; break; } @@ -541,8 +547,7 @@ bool Instance::WriteStructureToScreen(DrawLineInfo& dli, const DissasmStructureT dli.chNameAndSize = dli.chLineStart + Layout.startingTextLineOffset; auto clearChar = this->chars.GetBuffer(); - for (uint32 i = 0; i < Layout.startingTextLineOffset; i++) - { + for (uint32 i = 0; i < Layout.startingTextLineOffset; i++) { clearChar->Code = codePage[' ']; clearChar->Color = config.Colors.Normal; clearChar++; @@ -550,10 +555,8 @@ bool Instance::WriteStructureToScreen(DrawLineInfo& dli, const DissasmStructureT dli.chText = dli.chNameAndSize; - if (spaces > 0) - { - for (uint32 i = 0; i < spaces; i++) - { + if (spaces > 0) { + for (uint32 i = 0; i < spaces; i++) { dli.chText->Code = codePage[' ']; dli.chText->Color = normalColor; dli.chText++; @@ -563,8 +566,7 @@ bool Instance::WriteStructureToScreen(DrawLineInfo& dli, const DissasmStructureT uint32 typeSize = 0; bool isSignedValue = false; - switch (currentType.primaryType) - { + switch (currentType.primaryType) { case GView::View::DissasmViewer::InternalDissasmType::UInt8: typeSize = 1; isSignedValue = false; @@ -618,8 +620,7 @@ bool Instance::WriteStructureToScreen(DrawLineInfo& dli, const DissasmStructureT return false; } - if (typeSize > 0) - { + if (typeSize > 0) { // TODO: check textFileOffset!! auto buf = this->obj->GetData().Get(structureZone->textFileOffset - typeSize, typeSize, false); @@ -628,13 +629,10 @@ bool Instance::WriteStructureToScreen(DrawLineInfo& dli, const DissasmStructureT for (uint32 i = 0; i < typeSize; i++) buffer[i] = buf[i]; - if (isSignedValue) - { + if (isSignedValue) { int64 value = *(int64*) buffer; AddStringToChars(dli, normalColor, "%s: %lli", currentType.name.data(), value); - } - else - { + } else { uint64 value = *(uint64*) buffer; AddStringToChars(dli, normalColor, "%s: %llu", currentType.name.data(), value); } @@ -667,39 +665,31 @@ bool Instance::DrawCollapsibleAndTextZone(DrawLineInfo& dli, CollapsibleAndTextZ dli.chNameAndSize = dli.chLineStart + Layout.startingTextLineOffset; auto clearChar = dli.chLineStart; - for (uint32 i = 0; i < Layout.startingTextLineOffset; i++) - { + for (uint32 i = 0; i < Layout.startingTextLineOffset; i++) { clearChar->Code = codePage[' ']; clearChar->Color = config.Colors.Normal; clearChar++; } dli.chText = dli.chNameAndSize; - if (zone->data.canBeCollapsed && dli.textLineToDraw == 0) - { + if (zone->data.canBeCollapsed && dli.textLineToDraw == 0) { AddStringToChars(dli, config.Colors.StructureColor, "Collapsible zone [%llu] ", zone->data.size); RegisterStructureCollapseButton(dli, zone->isCollapsed ? SpecialChars::TriangleRight : SpecialChars::TriangleLeft, zone); - } - else - { - if (!zone->isCollapsed) - { + } else { + if (!zone->isCollapsed) { // TODO: hack-ish, maybe find another alternative or reset it down if (!zone->data.canBeCollapsed) dli.textLineToDraw++; uint64 dataNeeded = std::min(zone->data.size, Layout.textSize); - if (zone->data.size / Layout.textSize + 1 == dli.textLineToDraw) - { + if (zone->data.size / Layout.textSize + 1 == dli.textLineToDraw) { dataNeeded = std::min(zone->data.size % Layout.textSize, Layout.textSize); } const uint64 startingOffset = zone->data.startingOffset + (static_cast(dli.textLineToDraw) - 1ull) * Layout.textSize; - if (startingOffset + dataNeeded <= this->obj->GetData().GetSize()) - { + if (startingOffset + dataNeeded <= this->obj->GetData().GetSize()) { const auto buf = this->obj->GetData().Get(startingOffset, static_cast(dataNeeded), false); - if (!buf.IsValid()) - { + if (!buf.IsValid()) { AddStringToChars(dli, config.Colors.StructureColor, "\tInvalid buff at position: %llu", zone->data.startingOffset + zone->data.size); const size_t buffer_size = dli.chText - this->chars.GetBuffer(); @@ -721,8 +711,7 @@ bool Instance::DrawCollapsibleAndTextZone(DrawLineInfo& dli, CollapsibleAndTextZ if (!zone->data.canBeCollapsed) textColor = config.Colors.Normal; - while (dli.start < dli.end) - { + while (dli.start < dli.end) { dli.chText->Code = codePage[*dli.start]; dli.chText->Color = textColor; dli.chText++; @@ -737,9 +726,7 @@ bool Instance::DrawCollapsibleAndTextZone(DrawLineInfo& dli, CollapsibleAndTextZ // const uint32 index = this->Cursor.offset; // dli.chNameAndSize[index].Color = config.Colors.Selection; // } - } - else - { + } else { AddStringToChars(dli, config.Colors.StructureColor, "\tNot enough data for offset: %llu", zone->data.startingOffset + zone->data.size); } } @@ -754,8 +741,7 @@ bool Instance::DrawCollapsibleAndTextZone(DrawLineInfo& dli, CollapsibleAndTextZ bool Instance::DrawDissasmZone(DrawLineInfo& dli, DissasmCodeZone* zone) { - if (zone->zoneDetails.language != DisassemblyLanguage::x86 && zone->zoneDetails.language != DisassemblyLanguage::x64) - { + if (zone->zoneDetails.language != DisassemblyLanguage::x86 && zone->zoneDetails.language != DisassemblyLanguage::x64) { dli.WriteErrorToScreen("Not yet supported language!"); AdjustZoneExtendedSize(zone, 1); return true; @@ -773,8 +759,7 @@ void Instance::RegisterStructureCollapseButton(DrawLineInfo& dli, SpecialChars c void Instance::AddStringToChars(DrawLineInfo& dli, ColorPair pair, string_view stringToAdd) { size_t length = stringToAdd.size(); - for (uint32 i = 0; i < length; i++) - { + for (uint32 i = 0; i < length; i++) { dli.chText->Code = codePage[stringToAdd[i]]; dli.chText->Color = pair; dli.chText++; @@ -792,8 +777,7 @@ void Instance::AddStringToChars(DrawLineInfo& dli, ColorPair pair, const char* f va_end(args); size_t length = strlen(buffer); - for (uint32 i = 0; i < length; i++) - { + for (uint32 i = 0; i < length; i++) { dli.chText->Code = codePage[buffer[i]]; dli.chText->Color = pair; dli.chText++; @@ -802,10 +786,8 @@ void Instance::AddStringToChars(DrawLineInfo& dli, ColorPair pair, const char* f void Instance::HighlightSelectionAndDrawCursorText(DrawLineInfo& dli, uint32 maxLineLength, uint32 availableCharacters) { - for (uint32 i = 0; i < selection.GetCount(); i++) - { - if (selection.HasSelection(i)) - { + for (uint32 i = 0; i < selection.GetCount(); i++) { + if (selection.HasSelection(i)) { const auto selectionStart = selection.GetSelectionStart(i); const auto selectionEnd = selection.GetSelectionEnd(i); auto selectionStorage = selection.GetStorage(i); @@ -815,12 +797,10 @@ void Instance::HighlightSelectionAndDrawCursorText(DrawLineInfo& dli, uint32 max const uint32 selectionEndLine = selectionEnd.line; const uint32 lineToDrawTo = dli.screenLineToDraw + Cursor.startViewLine; - if (selectStartLine <= lineToDrawTo && lineToDrawTo <= selectionEndLine) - { + if (selectStartLine <= lineToDrawTo && lineToDrawTo <= selectionEndLine) { uint32 startingIndex = selectionStart.offset; // % Layout.textSize; uint32 endIndex = selectionEnd.offset % Layout.textSize + 1; - if (!isAltPressed) - { + if (!isAltPressed) { if (selectStartLine < lineToDrawTo) startingIndex = 0; if (lineToDrawTo < selectionEndLine) @@ -830,8 +810,7 @@ void Instance::HighlightSelectionAndDrawCursorText(DrawLineInfo& dli, uint32 max // TODO: variables can be skipped, use startingPointer < EndPointer const auto savedChText = dli.chText; dli.chText = dli.chNameAndSize + startingIndex; - while (startingIndex < endIndex) - { + while (startingIndex < endIndex) { dli.chText->Color = Cfg.Selection.Editor; selectionStorage->push_back(dli.chText->Code); dli.chText++; @@ -844,8 +823,7 @@ void Instance::HighlightSelectionAndDrawCursorText(DrawLineInfo& dli, uint32 max } } - if (Cursor.lineInView == dli.screenLineToDraw) - { + if (Cursor.lineInView == dli.screenLineToDraw) { uint32 index = this->Cursor.offset; if (index < availableCharacters - Layout.startingTextLineOffset) dli.chNameAndSize[index].Color = config.Colors.Selection; @@ -864,8 +842,7 @@ void Instance::HighlightSelectionAndDrawCursorText(DrawLineInfo& dli, uint32 max } } -struct MappingZonesData -{ +struct MappingZonesData { void* data; DissasmParseZoneType zoneType; }; @@ -873,16 +850,13 @@ struct MappingZonesData void Instance::RecomputeDissasmZones() { std::map> mappingData; - for (auto& mapping : this->settings->dissasmTypeMapped) - { + for (auto& mapping : this->settings->dissasmTypeMapped) { mappingData[OffsetToLinePosition(mapping.first).line].push_back({ &mapping.second, DissasmParseZoneType::StructureParseZone }); } - for (auto& dissasmZone : settings->disassemblyZones) - { + for (auto& dissasmZone : settings->disassemblyZones) { mappingData[OffsetToLinePosition(dissasmZone.first).line].push_back({ &dissasmZone.second, DissasmParseZoneType::DissasmCodeParseZone }); } - for (auto& zone : settings->collapsibleAndTextZones) - { + for (auto& zone : settings->collapsibleAndTextZones) { mappingData[OffsetToLinePosition(zone.first).line].push_back({ &zone.second, DissasmParseZoneType::CollapsibleAndTextZone }); } @@ -891,15 +865,12 @@ void Instance::RecomputeDissasmZones() uint32 textLinesOffset = 0; settings->parseZones.clear(); - for (const auto& mapping : mappingData) - { - for (const auto& entry : mapping.second) - { + for (const auto& mapping : mappingData) { + for (const auto& entry : mapping.second) { uint32 zoneStartingLine = mapping.first; if (zoneStartingLine < lastZoneEndingIndex || !config.ShowFileContent) zoneStartingLine = lastZoneEndingIndex; - if (zoneStartingLine > lastZoneEndingIndex) - { + if (zoneStartingLine > lastZoneEndingIndex) { const uint64 startingTextOffset = LinePositionToOffset({ textLinesOffset }, Layout.textSize); textLinesOffset += zoneStartingLine - lastZoneEndingIndex; const uint64 endTextOffset = LinePositionToOffset({ textLinesOffset }, Layout.textSize); @@ -923,10 +894,8 @@ void Instance::RecomputeDissasmZones() settings->parseZones.push_back(std::move(collapsibleZone)); } - switch (entry.zoneType) - { - case DissasmParseZoneType::StructureParseZone: - { + switch (entry.zoneType) { + case DissasmParseZoneType::StructureParseZone: { const auto convertedData = static_cast(entry.data); auto parseZone = std::make_unique(); parseZone->startLineIndex = zoneStartingLine; @@ -949,10 +918,8 @@ void Instance::RecomputeDissasmZones() // lastEndMinusLastOffset = parseZone->endingLineIndex + parseZone->textLinesOffset; lastZoneEndingIndex = parseZone->endingLineIndex; settings->parseZones.push_back(std::move(parseZone)); - } - break; - case DissasmParseZoneType::DissasmCodeParseZone: - { + } break; + case DissasmParseZoneType::DissasmCodeParseZone: { // TODO: resize vectors! -> there could be done some approximations for the best speed const auto convertedData = static_cast(entry.data); auto codeZone = std::make_unique(); @@ -977,10 +944,8 @@ void Instance::RecomputeDissasmZones() // lastEndMinusLastOffset = codeZone->endingLineIndex + codeZone->textLinesOffset; lastZoneEndingIndex = codeZone->endingLineIndex; settings->parseZones.push_back(std::move(codeZone)); - } - break; - case DissasmParseZoneType::CollapsibleAndTextZone: - { + } break; + case DissasmParseZoneType::CollapsibleAndTextZone: { const auto convertedData = static_cast(entry.data); auto collapsibleZone = std::make_unique(); collapsibleZone->data = *convertedData; @@ -998,8 +963,7 @@ void Instance::RecomputeDissasmZones() // lastEndMinusLastOffset = collapsibleZone->endingLineIndex + collapsibleZone->textLinesOffset; lastZoneEndingIndex = collapsibleZone->endingLineIndex; settings->parseZones.push_back(std::move(collapsibleZone)); - } - break; + } break; } } } @@ -1084,17 +1048,13 @@ bool Instance::ProcessSelectedDataToPrintable(UnicodeStringBuilder& usb) { bool found_data = false; AppCUI::Graphics::CodePage cp(AppCUI::Graphics::CodePageID::PrintableAscii); - for (uint32 i = 0; i < selection.GetCount(); i++) - { - if (selection.HasSelection(i)) - { + for (uint32 i = 0; i < selection.GetCount(); i++) { + if (selection.HasSelection(i)) { found_data = true; auto storageData = selection.GetStorage(i); - for (const auto c : *storageData) - { + for (const auto c : *storageData) { FixSizeString<1> cc; - if (c == '\n') - { + if (c == '\n') { CHECK(cc.AddChar((c & 0xFF)), false, ""); CHECK(usb.Add(cc), false, ""); continue; @@ -1105,8 +1065,7 @@ bool Instance::ProcessSelectedDataToPrintable(UnicodeStringBuilder& usb) } } - if (!found_data) - { + if (!found_data) { LocalString<128> message; CHECK(message.AddFormat("No selection", obj->GetData().GetSize()), false, ""); Dialogs::MessageBox::ShowError("Error copying to clipboard (postprocessing)!", message); @@ -1151,17 +1110,13 @@ vector Instance::GetZonesIndexesFromLinePosition(uint32 zoneIndex++; uint32* value = nullptr; - for (uint32 line = lineStart; line <= lineEnd && zoneIndex < zonesCount; line++) - { - if (zones[zoneIndex]->startLineIndex <= line && line < zones[zoneIndex]->endingLineIndex && (result.empty() || result.back().zoneIndex != zoneIndex)) - { + for (uint32 line = lineStart; line <= lineEnd && zoneIndex < zonesCount; line++) { + if (zones[zoneIndex]->startLineIndex <= line && line < zones[zoneIndex]->endingLineIndex && (result.empty() || result.back().zoneIndex != zoneIndex)) { result.push_back({ zoneIndex, line - zones[zoneIndex]->startLineIndex, line - zones[zoneIndex]->startLineIndex }); value = &result[result.size() - 1].endingLine; - } - else if (line >= zones[zoneIndex]->endingLineIndex) + } else if (line >= zones[zoneIndex]->endingLineIndex) zoneIndex++; - else if (value) - { + else if (value) { (*value)++; } } @@ -1171,8 +1126,7 @@ vector Instance::GetZonesIndexesFromLinePosition(uint32 void GView::View::DissasmViewer::Instance::AdjustZoneExtendedSize(ParseZone* zone, uint32 newExtendedSize) { - if (zone->isCollapsed) - { + if (zone->isCollapsed) { zone->extendedSize = newExtendedSize; return; } @@ -1183,10 +1137,8 @@ void GView::View::DissasmViewer::Instance::AdjustZoneExtendedSize(ParseZone* zon const int32 sizeToAdjust = static_cast(newExtendedSize) - zone->extendedSize; zone->endingLineIndex += sizeToAdjust; bool foundZone = false; - for (const auto& availableZone : settings->parseZones) - { - if (foundZone) - { + for (const auto& availableZone : settings->parseZones) { + if (foundZone) { availableZone->startLineIndex += sizeToAdjust; availableZone->endingLineIndex += sizeToAdjust; } @@ -1206,8 +1158,7 @@ bool Instance::WriteTextLineToChars(DrawLineInfo& dli) return false; auto clearChar = this->chars.GetBuffer(); - for (uint32 i = 0; i < Layout.startingTextLineOffset; i++) - { + for (uint32 i = 0; i < Layout.startingTextLineOffset; i++) { clearChar->Code = codePage[' ']; clearChar->Color = config.Colors.Normal; clearChar++; @@ -1224,8 +1175,7 @@ bool Instance::WriteTextLineToChars(DrawLineInfo& dli) bool activ = this->HasFocus(); auto textColor = activ ? config.Colors.Normal : config.Colors.Inactive; - while (dli.start < dli.end) - { + while (dli.start < dli.end) { dli.chText->Code = codePage[*dli.start]; dli.chText->Color = textColor; dli.chText++; @@ -1257,13 +1207,10 @@ void Instance::Paint(AppCUI::Graphics::Renderer& renderer) if (Layout.textSize == 0) return; - if (Cursor.hasMovedView) - { + if (Cursor.hasMovedView) { for (const auto& zone : asmData.zonesToClear) zone->asmPreCacheData.Clear(); - } - else - { + } else { for (const auto& zone : asmData.zonesToClear) zone->asmPreCacheData.Reset(); } @@ -1272,8 +1219,7 @@ void Instance::Paint(AppCUI::Graphics::Renderer& renderer) // TODO: improve this!! selection.ClearStorages(); - for (uint32 tr = 0; tr < this->Layout.visibleRows; tr++) - { + for (uint32 tr = 0; tr < this->Layout.visibleRows; tr++) { dli.screenLineToDraw = tr; if (!PrepareDrawLineInfo(dli)) break; @@ -1293,8 +1239,7 @@ void Instance::Paint(AppCUI::Graphics::Renderer& renderer) // asmData.bufferPool.Draw(renderer, config); - if (!MyLine.buttons.empty()) - { + if (!MyLine.buttons.empty()) { for (const auto& btn : MyLine.buttons) renderer.WriteSpecialCharacter(btn.x, btn.y, btn.c, btn.color); } @@ -1307,11 +1252,9 @@ bool Instance::ShowGoToDialog() const uint32 totalLines = settings->parseZones[settings->parseZones.size() - 1]->endingLineIndex; const uint32 currentLine = Cursor.lineInView + Cursor.startViewLine; GoToDialog dlg(currentLine, totalLines); - if (dlg.Show() == Dialogs::Result::Ok) - { + if (dlg.Show() == Dialogs::Result::Ok) { const auto lineToReach = dlg.GetResultedLine(); - if (lineToReach != currentLine) - { + if (lineToReach != currentLine) { jumps_holder.insert(Cursor.saveState()); MoveTo(0, static_cast(lineToReach) - static_cast(currentLine), Key::None, false); } @@ -1331,8 +1274,7 @@ bool Instance::ShowCopyDialog() if (!ProcessSelectedDataToPrintable(usb)) return false; - if (AppCUI::OS::Clipboard::SetText(usb) == false) - { + if (AppCUI::OS::Clipboard::SetText(usb) == false) { LocalString<128> message; CHECK(message.AddFormat("File size %llu bytes, cache size %llu bytes!", obj->GetData().GetSize(), 100), false, ""); Dialogs::MessageBox::ShowError("Error copying to clipboard (postprocessing)!", message); @@ -1357,8 +1299,7 @@ void Instance::OnStart() this->RecomputeDissasmZones(); uint32 maxSize = 0; - while (settings->maxLocationMemoryMappingSize > 0) - { + while (settings->maxLocationMemoryMappingSize > 0) { maxSize++; settings->maxLocationMemoryMappingSize /= 10; } @@ -1367,10 +1308,8 @@ void Instance::OnStart() GView::Hashes::CRC32 crc32{}; uint32 hashVal = 0; - for (uint32 i = 0; i < KNOWN_FUNCTIONS.size(); i++) - { - if (!crc32.Init(Hashes::CRC32Type::JAMCRC) || !crc32.Update(KNOWN_FUNCTIONS[i].functionName) || !crc32.Final(hashVal)) - { + for (uint32 i = 0; i < KNOWN_FUNCTIONS.size(); i++) { + if (!crc32.Init(Hashes::CRC32Type::JAMCRC) || !crc32.Update(KNOWN_FUNCTIONS[i].functionName) || !crc32.Final(hashVal)) { // show err return; } @@ -1396,10 +1335,8 @@ void Instance::ChangeZoneCollapseState(ParseZone* zoneToChange) zoneToChange->endingLineIndex += sizeToAdjust; bool foundZone = false; - for (auto& zone : settings->parseZones) - { - if (foundZone) - { + for (auto& zone : settings->parseZones) { + if (foundZone) { zone->startLineIndex += sizeToAdjust; zone->endingLineIndex += sizeToAdjust; } @@ -1412,8 +1349,7 @@ void Instance::ChangeZoneCollapseState(ParseZone* zoneToChange) Instance::~Instance() { - while (!settings->buffersToDelete.empty()) - { + while (!settings->buffersToDelete.empty()) { char* bufferToDelete = settings->buffersToDelete.back(); settings->buffersToDelete.pop_back(); delete bufferToDelete; @@ -1429,8 +1365,7 @@ void DissasmAsmPreCacheData::AnnounceCallInstruction(struct DissasmCodeZone* zon const uint32 startingLine = cachedAsmLines.back().currentLine; uint32 pushIndex = 0, pushesRemaining = functionDetails->params.size(); - for (auto it = cachedAsmLines.rbegin(); it != cachedAsmLines.rend() && pushesRemaining; ++it) - { + for (auto it = cachedAsmLines.rbegin(); it != cachedAsmLines.rend() && pushesRemaining; ++it) { if (startingLine - it->currentLine > MAX_LINE_DIFF) break; if (it->flags != DissasmAsmPreCacheLine::InstructionFlag::PushFlag) @@ -1439,12 +1374,9 @@ void DissasmAsmPreCacheData::AnnounceCallInstruction(struct DissasmCodeZone* zon // TODO: improve performance, remove string concatenation as much as possible auto param = std::string(functionDetails->params[pushIndex].name); const auto commentIt = zone->comments.comments.find(it->currentLine); - if (commentIt != zone->comments.comments.end()) - { + if (commentIt != zone->comments.comments.end()) { commentIt->second = param + " " + commentIt->second; - } - else - { + } else { zone->comments.comments.insert({ it->currentLine, param }); } pushesRemaining--; @@ -1460,8 +1392,7 @@ void DissasmComments::AddOrUpdateComment(uint32 line, std::string comment) bool DissasmComments::HasComment(uint32 line, std::string& comment) const { const auto it = comments.find(line - 1); - if (it != comments.end()) - { + if (it != comments.end()) { comment = it->second; return true; } @@ -1471,8 +1402,7 @@ bool DissasmComments::HasComment(uint32 line, std::string& comment) const void DissasmComments::RemoveComment(uint32 line) { const auto it = comments.find(line - 1); - if (it != comments.end()) - { + if (it != comments.end()) { comments.erase(it); return; } @@ -1482,10 +1412,8 @@ void DissasmComments::RemoveComment(uint32 line) void DissasmComments::AdjustCommentsOffsets(uint32 changedLine, bool isAddedLine) { decltype(comments) commentsAjusted = {}; - for (auto& comment : comments) - { - if (comment.first >= changedLine) - { + for (auto& comment : comments) { + if (comment.first >= changedLine) { if (isAddedLine) commentsAjusted.insert({ comment.first + 1, std::move(comment.second) }); else @@ -1502,15 +1430,13 @@ void Instance::ProcessSpaceKey(bool goToEntryPoint) const uint64 offsetEnd = offsetStart + 1; const auto zonesFound = GetZonesIndexesFromPosition(offsetStart, offsetEnd); - if (zonesFound.empty() || zonesFound.size() != 1) - { + if (zonesFound.empty() || zonesFound.size() != 1) { Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a single zone!"); return; } const auto& zone = settings->parseZones[zonesFound[0].zoneIndex]; - if (goToEntryPoint && zone->isCollapsed) - { + if (goToEntryPoint && zone->isCollapsed) { ChangeZoneCollapseState(zone.get()); } if (!goToEntryPoint && zonesFound[0].startingLine == 0) // extending zone @@ -1519,8 +1445,7 @@ void Instance::ProcessSpaceKey(bool goToEntryPoint) return; } - if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) - { + if (zone->zoneType != DissasmParseZoneType::DissasmCodeParseZone) { if (goToEntryPoint) Dialogs::MessageBox::ShowNotification("Warning", "Please make a selection on a dissasm zone!"); return; diff --git a/GViewCore/src/View/DissasmViewer/CommentDataWindow.cpp b/GViewCore/src/View/DissasmViewer/SingleSelectionDataWindow.cpp similarity index 60% rename from GViewCore/src/View/DissasmViewer/CommentDataWindow.cpp rename to GViewCore/src/View/DissasmViewer/SingleSelectionDataWindow.cpp index 83f0f55d..ee2f0a93 100644 --- a/GViewCore/src/View/DissasmViewer/CommentDataWindow.cpp +++ b/GViewCore/src/View/DissasmViewer/SingleSelectionDataWindow.cpp @@ -5,32 +5,32 @@ using namespace GView::View::DissasmViewer; constexpr int32 BTN_ID_OK = 1; constexpr int32 BTN_ID_CANCEL = 2; -void CommentDataWindow::Validate() +void SingleLineEditWindow::Validate() { LocalString<128> tmp; - if (commentTextField->GetText().IsEmpty()) + if (textField->GetText().IsEmpty()) { Dialogs::MessageBox::ShowError("Error", "Please write something in the comment section !"); - commentTextField->SetFocus(); + textField->SetFocus(); } - data = commentTextField->GetText(); + data = textField->GetText(); Exit(Dialogs::Result::Ok); } -CommentDataWindow::CommentDataWindow(std::string initialComment) : Window("Add comment", "d:c,w:60,h:7", WindowFlags::ProcessReturn) +SingleLineEditWindow::SingleLineEditWindow(std::string initialText, const char* title) : Window(title, "d:c,w:60,h:7", WindowFlags::ProcessReturn) { - data = initialComment; - Factory::Label::Create(this, "&Comment", "x:1,y:1,w:8"); - commentTextField = Factory::TextField::Create(this, initialComment, "x:10,y:1,w:46"); - commentTextField->SetHotKey('C'); + data = initialText; + Factory::Label::Create(this, "&Text", "x:1,y:1,w:8"); + textField = Factory::TextField::Create(this, initialText, "x:10,y:1,w:46"); + textField->SetHotKey('T'); Factory::Button::Create(this, "&OK", "l:16,b:0,w:13", BTN_ID_OK); Factory::Button::Create(this, "&Cancel", "l:31,b:0,w:13", BTN_ID_CANCEL); - commentTextField->SetFocus(); + textField->SetFocus(); } -bool CommentDataWindow::OnEvent(Reference, Event eventType, int ID) +bool SingleLineEditWindow::OnEvent(Reference, Event eventType, int ID) { if (eventType == Event::ButtonClicked) {