From 29c4896cda6e0933a5acb5402ff9ec4fb9ea7e90 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Thu, 29 Aug 2019 16:50:12 -0400 Subject: Add settings API to query keys and json stringified values. --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 2 ++ python/binaryview.py | 5 ++++- python/settings.py | 21 +++++++++++++++++++++ settings.cpp | 24 ++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 20db0aef..7e18a259 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4537,6 +4537,7 @@ namespace BinaryNinja bool RegisterSetting(const std::string& key, const std::string& properties); bool Contains(const std::string& key); bool IsEmpty(); + std::vector Keys(); template T QueryProperty(const std::string& key, const std::string& property); @@ -4560,6 +4561,7 @@ namespace BinaryNinja bool ResetAll(Ref view = nullptr, BNSettingsScope scope = SettingsAutoScope); template T Get(const std::string& key, Ref view = nullptr, BNSettingsScope* scope = nullptr); + std::string GetJson(const std::string& key, Ref view = nullptr, BNSettingsScope* scope = nullptr); bool Set(const std::string& key, bool value, Ref view = nullptr, BNSettingsScope scope = SettingsAutoScope); bool Set(const std::string& key, double value, Ref view = nullptr, BNSettingsScope scope = SettingsAutoScope); diff --git a/binaryninjacore.h b/binaryninjacore.h index 852e2a7e..1bf9b6c0 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -3945,6 +3945,7 @@ extern "C" BINARYNINJACOREAPI bool BNSettingsRegisterSetting(BNSettings* settings, const char* key, const char* properties); BINARYNINJACOREAPI bool BNSettingsContains(BNSettings* settings, const char* key); BINARYNINJACOREAPI bool BNSettingsIsEmpty(BNSettings* settings); + BINARYNINJACOREAPI const char** BNSettingsKeysList(BNSettings* settings, size_t* inoutSize); BINARYNINJACOREAPI const char** BNSettingsQueryPropertyStringList(BNSettings* settings, const char* key, const char* property, size_t* inoutSize); BINARYNINJACOREAPI bool BNSettingsUpdateProperty(BNSettings* settings, const char* key, const char* property); BINARYNINJACOREAPI bool BNSettingsUpdateBoolProperty(BNSettings* settings, const char* key, const char* property, bool value); @@ -3969,6 +3970,7 @@ extern "C" BINARYNINJACOREAPI uint64_t BNSettingsGetUInt64(BNSettings* settings, const char* key, BNBinaryView* view, BNSettingsScope* scope); BINARYNINJACOREAPI char* BNSettingsGetString(BNSettings* settings, const char* key, BNBinaryView* view, BNSettingsScope* scope); BINARYNINJACOREAPI const char** BNSettingsGetStringList(BNSettings* settings, const char* key, BNBinaryView* view, BNSettingsScope* scope, size_t* inoutSize); + BINARYNINJACOREAPI char* BNSettingsGetJsonString(BNSettings* settings, const char* key, BNBinaryView* view, BNSettingsScope* scope); BINARYNINJACOREAPI bool BNSettingsSetBool(BNSettings* settings, BNBinaryView* view, BNSettingsScope scope, const char* key, bool value); BINARYNINJACOREAPI bool BNSettingsSetDouble(BNSettings* settings, BNBinaryView* view, BNSettingsScope scope, const char* key, double value); diff --git a/python/binaryview.py b/python/binaryview.py index a6bb6882..c5d9c363 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -687,7 +687,10 @@ class BinaryViewType(with_metaclass(_BinaryViewTypeMetaclass, object)): return settings.Settings(handle=load_settings) def get_load_settings_for_data(self, data): - load_settings = core.BNGetBinaryViewLoadSettingsForData(self.handle, data.handle) + view_handle = None + if data is not None: + view_handle = data.handle + load_settings = core.BNGetBinaryViewLoadSettingsForData(self.handle, view_handle) if load_settings is None: return None return settings.Settings(handle=load_settings) diff --git a/python/settings.py b/python/settings.py index 95501622..66f84fe9 100644 --- a/python/settings.py +++ b/python/settings.py @@ -111,6 +111,15 @@ class Settings(object): def is_empty(self): return core.BNSettingsIsEmpty(self.handle) + def keys(self): + length = ctypes.c_ulonglong() + result = core.BNSettingsKeysList(self.handle, ctypes.byref(length)) + out_list = [] + for i in range(length.value): + out_list.append(pyNativeStr(result[i])) + core.BNFreeStringList(result, length) + return out_list + def query_property_string_list(self, key, property_name): length = ctypes.c_ulonglong() result = core.BNSettingsQueryPropertyStringList(self.handle, key, property_name, ctypes.byref(length)) @@ -173,6 +182,11 @@ class Settings(object): core.BNFreeStringList(result, length) return out_list + def get_json(self, key, view = None): + if view is not None: + view = view.handle + return core.BNSettingsGetJsonString(self.handle, key, view, None) + def get_bool_with_scope(self, key, view = None, scope = SettingsScope.SettingsAutoScope): if view is not None: view = view.handle @@ -213,6 +227,13 @@ class Settings(object): core.BNFreeStringList(result, length) return (out_list, SettingsScope(c_scope.value)) + def get_json_with_scope(self, key, view = None, scope = SettingsScope.SettingsAutoScope): + if view is not None: + view = view.handle + c_scope = core.SettingsScopeEnum(scope) + result = core.BNSettingsGetJsonString(self.handle, key, view, ctypes.byref(c_scope)) + return (result, SettingsScope(c_scope.value)) + def set_bool(self, key, value, view = None, scope = SettingsScope.SettingsAutoScope): if view is not None: view = view.handle diff --git a/settings.cpp b/settings.cpp index fdc74335..46119905 100644 --- a/settings.cpp +++ b/settings.cpp @@ -58,6 +58,21 @@ bool Settings::IsEmpty() } +vector Settings::Keys() +{ + size_t size = 0; + char** outBuffer = (char**)BNSettingsKeysList(m_object, &size); + + vector result; + result.reserve(size); + for (size_t i = 0; i < size; i++) + result.emplace_back(outBuffer[i]); + + BNFreeStringList(outBuffer, size); + return result; +} + + template<> vector Settings::QueryProperty>(const string& key, const string& property) { size_t size = 0; @@ -232,6 +247,15 @@ template<> vector Settings::Get>(const string& key, Ref view, BNSettingsScope* scope) +{ + char* tmpStr = BNSettingsGetJsonString(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + string result(tmpStr); + BNFreeString(tmpStr); + return result; +} + + bool Settings::Set(const string& key, bool value, Ref view, BNSettingsScope scope) { return BNSettingsSetBool(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); -- cgit v1.3.1