From 8767400b712f12f459556844e27d9b9f5ff037bd Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Wed, 27 Nov 2024 12:15:23 -0500 Subject: Add line formatter API and a generic line formatter plugin --- binaryninjaapi.h | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index f8d7504a..92e6b397 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4057,6 +4057,10 @@ namespace BinaryNinja { DisassemblyTextLineTypeInfo typeInfo; DisassemblyTextLine(); + + size_t GetTotalWidth() const; + size_t GetAddressAndIndentationWidth() const; + std::vector GetAddressAndIndentationTokens() const; }; /*! @@ -10175,6 +10179,10 @@ namespace BinaryNinja { DisassemblySettings(BNDisassemblySettings* settings); DisassemblySettings* Duplicate(); + static Ref GetDefaultSettings(); + static Ref GetDefaultGraphSettings(); + static Ref GetDefaultLinearSettings(); + bool IsOptionSet(BNDisassemblyOption option) const; void SetOption(BNDisassemblyOption option, bool state = true); @@ -13698,6 +13706,82 @@ namespace BinaryNinja { std::set GetSSAVariables(); }; + struct LineFormatterSettings + { + Ref highLevelIL; + size_t desiredLineLength; + size_t minimumContentLength; + size_t tabWidth; + std::string languageName; + std::string commentStartString; + std::string commentEndString; + std::string annotationStartString; + std::string annotationEndString; + + /*! Gets the default line formatter settings for High Level IL code. + + \param settings The settings for reformatting. + \param func High Level IL function to be reformatted. + \return Settings for reformatting. + */ + static LineFormatterSettings GetDefault(DisassemblySettings* settings, HighLevelILFunction* func); + + /*! Gets the default line formatter settings for a language representation function. + + \param settings The settings for reformatting. + \param func Language representation function to be reformatted. + \return Settings for reformatting. + */ + static LineFormatterSettings GetLanguageRepresentationSettings( + DisassemblySettings* settings, LanguageRepresentationFunction* func); + + static LineFormatterSettings FromAPIObject(const BNLineFormatterSettings* settings); + BNLineFormatterSettings ToAPIObject() const; + }; + + class LineFormatter : public StaticCoreRefCountObject + { + std::string m_nameForRegister; + + static BNDisassemblyTextLine* FormatLinesCallback(void* ctxt, BNDisassemblyTextLine* inLines, size_t inCount, + const BNLineFormatterSettings* settings, size_t* outCount); + static void FreeLinesCallback(void* ctxt, BNDisassemblyTextLine* lines, size_t count); + + public: + LineFormatter(const std::string& name); + LineFormatter(BNLineFormatter* formatter); + + /*! Registers the line formatter. + + \param formatter The line formatter to register. + */ + static void Register(LineFormatter* formatter); + + static std::vector> GetList(); + static Ref GetByName(const std::string& name); + static Ref GetDefault(); + + /*! Reformats the given list of lines. Returns a new list of lines containing the reformatted code. + + \param lines The lines to reformat. + \param settings The settings for reformatting. + \return A new list of reformatted lines. + */ + virtual std::vector FormatLines( + const std::vector& lines, const LineFormatterSettings& settings) = 0; + }; + + class CoreLineFormatter : public LineFormatter + { + public: + CoreLineFormatter(BNLineFormatter* formatter); + + std::vector FormatLines( + const std::vector& lines, const LineFormatterSettings& settings) override; + }; + + class LanguageRepresentationFunctionType; + /*! LanguageRepresentationFunction represents a single function in a registered high level language. \ingroup highlevelil @@ -13707,7 +13791,8 @@ namespace BinaryNinja { BNFreeLanguageRepresentationFunction> { public: - LanguageRepresentationFunction(Architecture* arch, Function* func, HighLevelILFunction* highLevelIL); + LanguageRepresentationFunction(LanguageRepresentationFunctionType* type, Architecture* arch, Function* func, + HighLevelILFunction* highLevelIL); LanguageRepresentationFunction(BNLanguageRepresentationFunction* func); /*! Gets the lines of tokens for a given High Level IL instruction. @@ -13746,6 +13831,7 @@ namespace BinaryNinja { */ BNHighlightColor GetHighlight(BasicBlock* block); + Ref GetLanguage() const; Ref GetArchitecture() const; Ref GetFunction() const; Ref GetHighLevelILFunction() const; @@ -13894,6 +13980,13 @@ namespace BinaryNinja { */ virtual Ref GetTypeParser() { return nullptr; } + /*! Returns the line formatter for formatting code in this language. If NULL is returned, the default + formatter will be used. + + \return The optional formatter for formatting code in this language. + */ + virtual Ref GetLineFormatter() { return nullptr; } + /*! Returns a list of lines representing a function prototype in this language. If no lines are returned, the default C-style prototype will be used. @@ -13920,6 +14013,7 @@ namespace BinaryNinja { static bool IsValidCallback(void* ctxt, BNBinaryView* view); static BNTypePrinter* GetTypePrinterCallback(void* ctxt); static BNTypeParser* GetTypeParserCallback(void* ctxt); + static BNLineFormatter* GetLineFormatterCallback(void* ctxt); static BNDisassemblyTextLine* GetFunctionTypeTokensCallback( void* ctxt, BNFunction* func, BNDisassemblySettings* settings, size_t* count); static void FreeLinesCallback(void* ctxt, BNDisassemblyTextLine* lines, size_t count); @@ -13934,6 +14028,7 @@ namespace BinaryNinja { bool IsValid(BinaryView* view) override; Ref GetTypePrinter() override; Ref GetTypeParser() override; + Ref GetLineFormatter() override; std::vector GetFunctionTypeTokens( Function* func, DisassemblySettings* settings = nullptr) override; }; -- cgit v1.3.1