diff --git a/src/dbg/bookmark.cpp b/src/dbg/bookmark.cpp index 02abc78d..c642697a 100644 --- a/src/dbg/bookmark.cpp +++ b/src/dbg/bookmark.cpp @@ -2,58 +2,25 @@ #include "module.h" #include "memory.h" -struct BookmarkSerializer : JSONWrapper +struct BookmarkSerializer : AddrInfoSerializer { - bool Save(const BOOKMARKSINFO & value) override - { - setString("module", value.mod); - setHex("address", value.addr); - setBool("manual", value.manual); - return true; - } - - bool Load(BOOKMARKSINFO & value) override - { - value.manual = true; - getBool("manual", value.manual); //legacy support - return getString("module", value.mod) && - getHex("address", value.addr); - } }; -struct Bookmarks : SerializableModuleHashMap +struct Bookmarks : AddrInfoHashMap { - void AdjustValue(BOOKMARKSINFO & value) const override - { - value.addr += ModBaseFromName(value.mod); - } - -protected: const char* jsonKey() const override { return "bookmarks"; } - - duint makeKey(const BOOKMARKSINFO & value) const override - { - return ModHashFromName(value.mod) + value.addr; - } }; static Bookmarks bookmarks; bool BookmarkSet(duint Address, bool Manual) { - // Validate the incoming address - if(!MemIsValidReadPtr(Address)) - return false; - BOOKMARKSINFO bookmark; - if(!ModNameFromAddr(Address, bookmark.mod, true)) - *bookmark.mod = '\0'; - bookmark.addr = Address - ModBaseFromAddr(Address); - bookmark.manual = Manual; - + if(!bookmarks.PrepareValue(bookmark, Address, Manual)) + return false; auto key = Bookmarks::VaKey(Address); if(bookmarks.Contains(key)) return bookmarks.Delete(key); @@ -72,12 +39,7 @@ bool BookmarkDelete(duint Address) void BookmarkDelRange(duint Start, duint End, bool Manual) { - bookmarks.DeleteRange(Start, End, [Manual](duint start, duint end, const BOOKMARKSINFO & value) - { - if(Manual ? !value.manual : value.manual) //ignore non-matching entries - return false; - return value.addr >= start && value.addr < end; - }); + bookmarks.DeleteRange(Start, End, Manual); } void BookmarkCacheSave(JSON Root) diff --git a/src/dbg/bookmark.h b/src/dbg/bookmark.h index 6f240c5d..8d266058 100644 --- a/src/dbg/bookmark.h +++ b/src/dbg/bookmark.h @@ -2,12 +2,10 @@ #define _BOOKMARK_H #include "_global.h" +#include "addrinfo.h" -struct BOOKMARKSINFO +struct BOOKMARKSINFO : AddrInfo { - char mod[MAX_MODULE_SIZE]; - duint addr; - bool manual; }; bool BookmarkSet(duint Address, bool Manual); diff --git a/src/dbg/comment.cpp b/src/dbg/comment.cpp index 4e657f4c..d9204b40 100644 --- a/src/dbg/comment.cpp +++ b/src/dbg/comment.cpp @@ -1,76 +1,47 @@ #include "comment.h" -#include "threading.h" -#include "module.h" -#include "memory.h" -struct CommentSerializer : JSONWrapper +struct CommentSerializer : AddrInfoSerializer { bool Save(const COMMENTSINFO & value) override { - setString("module", value.mod); - setHex("address", value.addr); + AddrInfoSerializer::Save(value); setString("text", value.text); - setBool("manual", value.manual); return true; } bool Load(COMMENTSINFO & value) override { - value.manual = true; - getBool("manual", value.manual); //legacy support - return getString("module", value.mod) && - getHex("address", value.addr) && + return AddrInfoSerializer::Load(value) && getString("text", value.text); } }; -struct Comments : SerializableModuleHashMap +struct Comments : AddrInfoHashMap { - void AdjustValue(COMMENTSINFO & value) const override - { - value.addr += ModBaseFromName(value.mod); - } - -protected: const char* jsonKey() const override { return "comments"; } - - duint makeKey(const COMMENTSINFO & value) const override - { - return ModHashFromName(value.mod) + value.addr; - } }; static Comments comments; bool CommentSet(duint Address, const char* Text, bool Manual) { - // A valid memory address must be supplied - if(!MemIsValidReadPtr(Address)) - return false; - // Make sure the string is supplied, within bounds, and not a special delimiter if(!Text || Text[0] == '\1' || strlen(Text) >= MAX_COMMENT_SIZE - 1) return false; - // Delete the comment if no text was supplied if(Text[0] == '\0') { CommentDelete(Address); return true; } - - // Fill out the structure + // Fill in the structure + add to database COMMENTSINFO comment; + if(!comments.PrepareValue(comment, Address, Manual)) + return false; strcpy_s(comment.text, Text); - if(!ModNameFromAddr(Address, comment.mod, true)) - *comment.mod = '\0'; - - comment.manual = Manual; - comment.addr = Address - ModBaseFromAddr(Address); - return comments.Add(comment); } @@ -93,12 +64,7 @@ bool CommentDelete(duint Address) void CommentDelRange(duint Start, duint End, bool Manual) { - comments.DeleteRange(Start, End, [Manual](duint start, duint end, const COMMENTSINFO & value) - { - if(Manual ? !value.manual : value.manual) //ignore non-matching entries - return false; - return value.addr >= start && value.addr < end; - }); + comments.DeleteRange(Start, End, Manual); } void CommentCacheSave(JSON Root) diff --git a/src/dbg/comment.h b/src/dbg/comment.h index 98e808a7..5ff17d09 100644 --- a/src/dbg/comment.h +++ b/src/dbg/comment.h @@ -2,13 +2,11 @@ #define _COMMENT_H #include "_global.h" +#include "addrinfo.h" -struct COMMENTSINFO +struct COMMENTSINFO : AddrInfo { - char mod[MAX_MODULE_SIZE]; - duint addr; char text[MAX_COMMENT_SIZE]; - bool manual; }; bool CommentSet(duint Address, const char* Text, bool Manual); diff --git a/src/dbg/label.cpp b/src/dbg/label.cpp index 1945cb50..823d687f 100644 --- a/src/dbg/label.cpp +++ b/src/dbg/label.cpp @@ -1,81 +1,48 @@ #include "label.h" -#include "threading.h" -#include "module.h" -#include "memory.h" -#include "console.h" -struct CommentSerializer : JSONWrapper +struct LabelSerializer : AddrInfoSerializer { bool Save(const LABELSINFO & value) override { - setString("module", value.mod); - setHex("address", value.addr); + AddrInfoSerializer::Save(value); setString("text", value.text); - setBool("manual", value.manual); return true; } bool Load(LABELSINFO & value) override { - value.manual = true; - getBool("manual", value.manual); //legacy support - return getString("module", value.mod) && - getHex("address", value.addr) && + return AddrInfoSerializer::Load(value) && getString("text", value.text); } }; -struct Labels : SerializableModuleHashMap +struct Labels : AddrInfoHashMap { - void AdjustValue(LABELSINFO & value) const override - { - value.addr += ModBaseFromName(value.mod); - } - -protected: const char* jsonKey() const override { return "labels"; } - - duint makeKey(const LABELSINFO & value) const override - { - return ModHashFromName(value.mod) + value.addr; - } }; static Labels labels; bool LabelSet(duint Address, const char* Text, bool Manual) { - // A valid memory address must be supplied - if(!MemIsValidReadPtr(Address)) - return false; - // Make sure the string is supplied, within bounds, and not a special delimiter - if(!Text || Text[0] == '\1' || strlen(Text) >= MAX_LABEL_SIZE - 1) + if(!Text || Text[0] == '\1' || strlen(Text) >= MAX_LABEL_SIZE - 1 || strstr(Text, "&")) return false; - - // Labels cannot be "address" of actual variables - if(strstr(Text, "&")) - return false; - // Delete the label if no text was supplied if(Text[0] == '\0') { LabelDelete(Address); return true; } - - // Fill out the structure data - LABELSINFO labelInfo; - labelInfo.manual = Manual; - labelInfo.addr = Address - ModBaseFromAddr(Address); - strcpy_s(labelInfo.text, Text); - if(!ModNameFromAddr(Address, labelInfo.mod, true)) - *labelInfo.mod = '\0'; - - return labels.Add(labelInfo); + // Fill in the structure + add to database + LABELSINFO label; + if(!labels.PrepareValue(label, Address, Manual)) + return false; + strcpy_s(label.text, Text); + return labels.Add(label); } bool LabelFromString(const char* Text, duint* Address) @@ -107,12 +74,7 @@ bool LabelDelete(duint Address) void LabelDelRange(duint Start, duint End, bool Manual) { - labels.DeleteRange(Start, End, [Manual](duint start, duint end, const LABELSINFO & value) - { - if(Manual ? !value.manual : value.manual) //ignore non-matching entries - return false; - return value.addr >= start && value.addr < end; - }); + labels.DeleteRange(Start, End, Manual); } void LabelCacheSave(JSON Root) diff --git a/src/dbg/label.h b/src/dbg/label.h index 4a0ffa6b..7c70f644 100644 --- a/src/dbg/label.h +++ b/src/dbg/label.h @@ -2,13 +2,11 @@ #define _LABEL_H #include "_global.h" +#include "addrinfo.h" -struct LABELSINFO +struct LABELSINFO : AddrInfo { - char mod[MAX_MODULE_SIZE]; - duint addr; char text[MAX_LABEL_SIZE]; - bool manual; }; bool LabelSet(duint Address, const char* Text, bool Manual); diff --git a/src/dbg/serializablemap.h b/src/dbg/serializablemap.h index 9597d483..dd4b642c 100644 --- a/src/dbg/serializablemap.h +++ b/src/dbg/serializablemap.h @@ -1,6 +1,7 @@ #include "_global.h" #include "threading.h" #include "module.h" +#include "memory.h" template class JSONWrapper @@ -301,7 +302,7 @@ struct SerializableModuleHashMap : SerializableUnorderedMap inRange) + void DeleteRangeWhere(duint start, duint end, std::function inRange) { // Are all comments going to be deleted? // 0x00000000 - 0xFFFFFFFF @@ -327,4 +328,72 @@ struct SerializableModuleHashMap : SerializableUnorderedMap +struct AddrInfoSerializer : JSONWrapper +{ + static_assert(std::is_base_of::value, "TValue is not derived from AddrInfo"); + + bool Save(const TValue & value) override + { + setString("module", value.mod); + setHex("address", value.addr); + setBool("manual", value.manual); + return true; + } + + bool Load(TValue & value) override + { + value.manual = true; //legacy support + getBool("manual", value.manual); + return getString("module", value.mod) && + getHex("address", value.addr); + } +}; + +template +struct AddrInfoHashMap : SerializableModuleHashMap +{ + static_assert(std::is_base_of::value, "TValue is not derived from AddrInfo"); + static_assert(std::is_base_of, TSerializer>::value, "TSerializer is not derived from AddrInfoSerializer"); + + void AdjustValue(TValue & value) const override + { + value.addr += ModBaseFromName(value.mod); + } + + bool PrepareValue(TValue & value, duint addr, bool manual) + { + if(!MemIsValidReadPtr(addr)) + return false; + if(!ModNameFromAddr(addr, value.mod, true)) + *value.mod = '\0'; + value.manual = manual; + value.addr = addr - ModBaseFromAddr(addr); + return true; + } + + void DeleteRange(duint start, duint end, bool manual) + { + DeleteRangeWhere(start, end, [manual](duint start, duint end, const TValue & value) + { + if(manual ? !value.manual : value.manual) //ignore non-matching entries + return false; + return value.addr >= start && value.addr < end; + }); + } + +protected: + duint makeKey(const TValue & value) const override + { + return ModHashFromName(value.mod) + value.addr; + } }; \ No newline at end of file diff --git a/src/dbg/xrefs.cpp b/src/dbg/xrefs.cpp index 313058fd..bc87b8ad 100644 --- a/src/dbg/xrefs.cpp +++ b/src/dbg/xrefs.cpp @@ -1,22 +1,17 @@ #include "xrefs.h" -#include "module.h" -#include "memory.h" -#include "threading.h" +#include "addrinfo.h" -struct XREFSINFO +struct XREFSINFO : AddrInfo { - char mod[MAX_MODULE_SIZE]; - duint address; XREFTYPE type; std::unordered_map references; }; -struct XrefSerializer : JSONWrapper +struct XrefSerializer : AddrInfoSerializer { bool Save(const XREFSINFO & value) override { - setString("module", value.mod); - setHex("address", value.address); + AddrInfoSerializer::Save(value); auto references = json_array(); for(const auto & itr : value.references) { @@ -31,8 +26,7 @@ struct XrefSerializer : JSONWrapper bool Load(XREFSINFO & value) override { - if(!getString("module", value.mod) || - !getHex("address", value.address)) + if(!AddrInfoSerializer::Load(value)) return false; auto references = get("references"); if(!references) @@ -52,36 +46,24 @@ struct XrefSerializer : JSONWrapper } }; -struct Xrefs : SerializableModuleHashMap +struct Xrefs : AddrInfoHashMap { - void AdjustValue(XREFSINFO & value) const override - { - value.address += ModBaseFromName(value.mod); - } - -protected: const char* jsonKey() const override { return "xrefs"; } - - duint makeKey(const XREFSINFO & value) const override - { - return ModHashFromName(value.mod) + value.address; - } }; static Xrefs xrefs; bool XrefAdd(duint Address, duint From) { - // Make sure memory is readable - if(!MemIsValidReadPtr(Address) || !MemIsValidReadPtr(From)) + XREFSINFO info; + if(!MemIsValidReadPtr(From) || !xrefs.PrepareValue(info, Address, false)) return false; // Fail if boundary exceeds module size auto moduleBase = ModBaseFromAddr(Address); - if(moduleBase != ModBaseFromAddr(From)) return false; @@ -103,10 +85,6 @@ bool XrefAdd(duint Address, duint From) auto found = mapData.find(key); if(found == mapData.end()) { - XREFSINFO info; - if(!ModNameFromAddr(Address, info.mod, true)) - *info.mod = '\0'; - info.address = Address - moduleBase; info.type = xrefRecord.type; info.references.insert({ xrefRecord.addr, xrefRecord }); mapData.insert({ key, info }); @@ -162,10 +140,7 @@ bool XrefDeleteAll(duint Address) void XrefDelRange(duint Start, duint End) { - xrefs.DeleteRange(Start, End, [](duint start, duint end, const XREFSINFO & value) - { - return value.address >= start && value.address <= end; - }); + xrefs.DeleteRange(Start, End, false); } void XrefCacheSave(JSON Root) diff --git a/src/dbg/xrefs.h b/src/dbg/xrefs.h index c341e004..edd650d3 100644 --- a/src/dbg/xrefs.h +++ b/src/dbg/xrefs.h @@ -3,8 +3,6 @@ #include "_global.h" - - bool XrefAdd(duint Address, duint From); bool XrefGet(duint Address, XREF_INFO* List); duint XrefGetCount(duint Address);