Skip to content

Commit b13d50b

Browse files
committed
Eliminate jsoncpp usage from the AnalysisContext C++ API object.
1 parent d5080db commit b13d50b

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

binaryninjaapi.h

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "binaryninjacore.h"
4949
#include "exceptions.h"
5050
#include "json/json.h"
51+
#include "rapidjsonwrapper.h"
5152
#include "vendor/nlohmann/json.hpp"
5253
#include <fmt/format.h>
5354
#include <fmt/ranges.h>
@@ -10014,12 +10015,8 @@ namespace BinaryNinja {
1001410015
/*!
1001510016
\ingroup workflow
1001610017
*/
10017-
class AnalysisContext :
10018-
public CoreRefCountObject<BNAnalysisContext, BNNewAnalysisContextReference, BNFreeAnalysisContext>
10018+
class AnalysisContext : public CoreRefCountObject<BNAnalysisContext, BNNewAnalysisContextReference, BNFreeAnalysisContext>
1001910019
{
10020-
std::unique_ptr<Json::CharReader> m_reader;
10021-
Json::StreamWriterBuilder m_builder;
10022-
1002310020
public:
1002410021
AnalysisContext(BNAnalysisContext* analysisContext);
1002510022
virtual ~AnalysisContext();
@@ -10084,27 +10081,34 @@ namespace BinaryNinja {
1008410081
*/
1008510082
void SetHighLevelILFunction(Ref<HighLevelILFunction> highLevelIL);
1008610083

10084+
bool Inform(const char* request);
1008710085
bool Inform(const std::string& request);
1008810086

10089-
#if ((__cplusplus >= 201403L) || (_MSVC_LANG >= 201703L))
1009010087
template <typename... Args>
1009110088
bool Inform(Args... args)
1009210089
{
10093-
// using T = std::variant<Args...>; // FIXME: remove type duplicates
10094-
using T = std::variant<std::string, const char*, uint64_t, Ref<Architecture>>;
10095-
std::vector<T> unpackedArgs {args...};
10096-
Json::Value request(Json::arrayValue);
10097-
for (auto& arg : unpackedArgs)
10098-
std::visit(overload {[&](Ref<Architecture> arch) { request.append(Json::Value(arch->GetName())); },
10099-
[&](uint64_t val) { request.append(Json::Value(val)); },
10100-
[&](auto& val) {
10101-
request.append(Json::Value(std::forward<decltype(val)>(val)));
10102-
}},
10103-
arg);
10104-
10105-
return Inform(Json::writeString(m_builder, request));
10090+
rapidjson::Document request(rapidjson::kArrayType);
10091+
rapidjson::Document::AllocatorType& allocator = request.GetAllocator();
10092+
request.Reserve(sizeof...(args), allocator);
10093+
([&] {
10094+
using T = std::decay_t<decltype(args)>;
10095+
if constexpr (std::is_same_v<T, Ref<Architecture>>)
10096+
{
10097+
auto archName = args->GetName();
10098+
request.PushBack(rapidjson::Value(archName.c_str(), archName.length(), allocator), allocator);
10099+
}
10100+
else if constexpr (std::is_same_v<T, std::string>)
10101+
request.PushBack(rapidjson::Value(args.c_str(), args.length(), allocator), allocator);
10102+
else if constexpr (std::is_same_v<T, const char*>)
10103+
request.PushBack(rapidjson::Value(args, allocator), allocator);
10104+
else
10105+
request.PushBack(rapidjson::Value(args), allocator);
10106+
}(), ...);
10107+
rapidjson::StringBuffer buffer;
10108+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
10109+
request.Accept(writer);
10110+
return Inform(buffer.GetString());
1010610111
}
10107-
#endif
1010810112
};
1010910113

1011010114
/*!

workflow.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "binaryninjaapi.h"
2-
#include "json/json.h"
32
#include "rapidjsonwrapper.h"
43
#include <string>
54
#include <variant>
@@ -8,12 +7,10 @@ using namespace BinaryNinja;
87
using namespace std;
98

109

11-
AnalysisContext::AnalysisContext(BNAnalysisContext* analysisContext) :
12-
m_reader(Json::CharReaderBuilder().newCharReader())
10+
AnalysisContext::AnalysisContext(BNAnalysisContext* analysisContext)
1311
{
1412
// LogError("API-Side AnalysisContext Constructed!");
1513
m_object = analysisContext;
16-
m_builder["indentation"] = "";
1714
}
1815

1916

@@ -104,6 +101,12 @@ void AnalysisContext::SetHighLevelILFunction(Ref<HighLevelILFunction> highLevelI
104101
}
105102

106103

104+
bool AnalysisContext::Inform(const char* request)
105+
{
106+
return BNAnalysisContextInform(m_object, request);
107+
}
108+
109+
107110
bool AnalysisContext::Inform(const string& request)
108111
{
109112
return BNAnalysisContextInform(m_object, request.c_str());

0 commit comments

Comments
 (0)