From 6436615567547349434351468012512505109552 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Mon, 6 Nov 2023 15:28:59 -0500 Subject: Type Browser --- ui/typeeditor.h | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 ui/typeeditor.h (limited to 'ui/typeeditor.h') diff --git a/ui/typeeditor.h b/ui/typeeditor.h new file mode 100644 index 00000000..4de99778 --- /dev/null +++ b/ui/typeeditor.h @@ -0,0 +1,168 @@ + +#pragma once + +#include "tokenizedtextwidget.h" +#include "uitypes.h" + +class BINARYNINJAUIAPI TypeEditor: public TokenizedTextWidget +{ +public: + struct SavedCursorPosition + { + struct PositionData + { + BinaryNinja::QualifiedName typeName; + TokenizedTextWidgetCursorPosition position; + size_t structOffset; + std::pair lineStart; + }; + bool restoreCursor; + bool restoreTop; + TokenizedTextWidgetSelectionStyle style; + PositionData cursor; + PositionData base; + PositionData top; + }; + +private: + Q_OBJECT + + PlatformRef m_platform; + std::optional m_typeContainer; + std::optional m_binaryView; + // Empty view for bv-requiring operations + mutable std::optional m_emptyView; + std::vector m_typeNames; + + // line index -> index of first line from wrapped line + std::vector m_lineUnwrapIndex; + // line index -> type name + std::vector m_lineTypeRefs; + // type name -> index of first line + std::map m_lineTypeStarts; + // type name -> { offset -> index of first { line, token } at offset } + std::map>> m_lineTypeOffsetStarts; + // type name -> { offset -> index of last { line, token } at offset } + std::map>> m_lineTypeOffsetEnds; + // line index -> line + std::vector m_typeLines; + + TokenizedTextWidgetCursorPosition m_originalBase; + + bool m_wrapLines; + bool m_showInherited; + +public: + TypeEditor(QWidget* parent); + + static void registerActions(); + void bindActions(); + + PlatformRef platform() const { return m_platform; } + void setPlatform(PlatformRef platform) { m_platform = platform; } + + std::optional binaryView() const { return m_binaryView; } + void setBinaryView(std::optional binaryView) { m_binaryView = binaryView; } + + std::optional> typeContainer() const; + void setTypeContainer(std::optional container); + + std::vector typeNames() const { return m_typeNames; } + void setTypeNames(const std::vector& names); + + int selectedLineCount() const; + std::unordered_set selectedLineStarts() const; + int selectedRootTypeCount() const; + std::unordered_set selectedRootTypes() const; + std::optional firstWrappedLineIndexForLineIndex(size_t lineIndex) const; + std::optional lastWrappedLineIndexForLineIndex(size_t lineIndex) const; + std::optional> typeLineAtIndex(size_t lineIndex) const; + std::optional> typeLineAtPosition(const TokenizedTextWidgetCursorPosition& position) const; + std::optional rootTypeNameAtIndex(size_t lineIndex) const; + std::optional rootTypeNameAtPosition(const TokenizedTextWidgetCursorPosition& position) const; + std::optional rootTypeAtIndex(size_t lineIndex) const; + std::optional rootTypeAtPosition(const TokenizedTextWidgetCursorPosition& position) const; + std::optional offsetAtIndex(size_t lineIndex) const; + std::optional offsetAtPosition(const TokenizedTextWidgetCursorPosition& position) const; + std::optional relativeOffsetAtPosition(const TokenizedTextWidgetCursorPosition& position) const; + std::optional firstPositionForOffset(const BinaryNinja::QualifiedName& name, uint64_t offset) const; + std::optional lastPositionForOffset(const BinaryNinja::QualifiedName& name, uint64_t offset) const; + void selectOffsetRange(const BinaryNinja::QualifiedName& name, uint64_t start, uint64_t end); + + SavedCursorPosition saveCursorPosition() const; + void restoreCursorPosition(const SavedCursorPosition& position); + + bool canCreateAllMembersForStructure(); + void createAllMembersForStructure(); + bool canCreateCurrentMemberForStructure(); + void createCurrentMemberForStructure(); + bool canRename(); + void rename(); + void renameRoot(); + void renameMember(); + bool canUndefine(); + void undefine(); + void undefineRoots(); + void undefineMembers(); + bool canCreateArray(); + void createArray(); + bool canChangeType(); + void changeType(); + void changeTypeRoot(); + void changeTypeAddMember(); + void changeTypeMember(); + bool canChangeTypeMembers(); + void changeTypeMembers(TypeRef newType); + bool canSetStructureSize(); + void setStructureSize(); + bool canAddUserXref(); + void addUserXref(); + bool canMakePointer(); + void makePointer(); + bool canMakeCString(); + void makeCString(); + bool canMakeUTF16String(); + void makeUTF16String(); + bool canMakeUTF32String(); + void makeUTF32String(); + bool canCycleIntegerSize(); + void cycleIntegerSize(); + bool canCycleFloatSize(); + void cycleFloatSize(); + bool canInvertIntegerSize(); + void invertIntegerSize(); + bool canMakeInt8(); + void makeInt8(); + bool canMakeInt16(); + void makeInt16(); + bool canMakeInt32(); + void makeInt32(); + bool canMakeInt64(); + void makeInt64(); + bool canMakeFloat32(); + void makeFloat32(); + bool canMakeFloat64(); + void makeFloat64(); + bool canGoToAddress(bool selecting); + void goToAddress(bool selecting); + void toggleWrapLines(); + void toggleShowInherited(); + + std::string getDebugText(); + +Q_SIGNALS: + void typeNameNavigated(const std::string& typeName); + void currentTypeUpdated(const BinaryNinja::QualifiedName& typeName); + void currentTypeDeleted(const BinaryNinja::QualifiedName& typeName); + void currentTypeNameUpdated(const BinaryNinja::QualifiedName& typeName); + +private: + void updateLines(); + BinaryViewRef binaryViewOrEmpty() const; + void updateInTransaction(std::function transaction); + void updateInTransaction(std::function transaction); + std::string dumpType(TypeRef type); + + void forEachMember(const TokenizedTextWidgetCursorPosition& begin, const TokenizedTextWidgetCursorPosition& end, + std::function func, bool childrenFirst = false); +}; -- cgit v1.3.1