From b13d50bac18c341b98e6a3002ac9f17f6c8a8df4 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Wed, 28 May 2025 08:16:45 -0400 Subject: Eliminate jsoncpp usage from the AnalysisContext C++ API object. --- binaryninjaapi.h | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index cba3c874..ca5b630c 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -48,6 +48,7 @@ #include "binaryninjacore.h" #include "exceptions.h" #include "json/json.h" +#include "rapidjsonwrapper.h" #include "vendor/nlohmann/json.hpp" #include #include @@ -10014,12 +10015,8 @@ namespace BinaryNinja { /*! \ingroup workflow */ - class AnalysisContext : - public CoreRefCountObject + class AnalysisContext : public CoreRefCountObject { - std::unique_ptr m_reader; - Json::StreamWriterBuilder m_builder; - public: AnalysisContext(BNAnalysisContext* analysisContext); virtual ~AnalysisContext(); @@ -10084,27 +10081,34 @@ namespace BinaryNinja { */ void SetHighLevelILFunction(Ref highLevelIL); + bool Inform(const char* request); bool Inform(const std::string& request); -#if ((__cplusplus >= 201403L) || (_MSVC_LANG >= 201703L)) template bool Inform(Args... args) { - // using T = std::variant; // FIXME: remove type duplicates - using T = std::variant>; - std::vector unpackedArgs {args...}; - Json::Value request(Json::arrayValue); - for (auto& arg : unpackedArgs) - std::visit(overload {[&](Ref arch) { request.append(Json::Value(arch->GetName())); }, - [&](uint64_t val) { request.append(Json::Value(val)); }, - [&](auto& val) { - request.append(Json::Value(std::forward(val))); - }}, - arg); - - return Inform(Json::writeString(m_builder, request)); + rapidjson::Document request(rapidjson::kArrayType); + rapidjson::Document::AllocatorType& allocator = request.GetAllocator(); + request.Reserve(sizeof...(args), allocator); + ([&] { + using T = std::decay_t; + if constexpr (std::is_same_v>) + { + auto archName = args->GetName(); + request.PushBack(rapidjson::Value(archName.c_str(), archName.length(), allocator), allocator); + } + else if constexpr (std::is_same_v) + request.PushBack(rapidjson::Value(args.c_str(), args.length(), allocator), allocator); + else if constexpr (std::is_same_v) + request.PushBack(rapidjson::Value(args, allocator), allocator); + else + request.PushBack(rapidjson::Value(args), allocator); + }(), ...); + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + request.Accept(writer); + return Inform(buffer.GetString()); } -#endif }; /*! -- cgit v1.3.1