From 0de62768c8afc5ca27576b59d4591ca8dbbd7cee Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Sat, 24 Jun 2017 01:33:04 -0400 Subject: Adding settings system apis, and binaryview metadata apis --- python/setting.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 python/setting.py (limited to 'python/setting.py') diff --git a/python/setting.py b/python/setting.py new file mode 100644 index 00000000..975f393e --- /dev/null +++ b/python/setting.py @@ -0,0 +1,114 @@ +# Copyright (c) 2015-2017 Vector 35 LLC +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +import ctypes + +# Binary Ninja components +import _binaryninjacore as core + + +class Setting(object): + def __init__(self, plugin_name="core"): + self.plugin_name = plugin_name + + def get_bool(self, name, default_value=False): + return core.BNSettingGetBool(self.plugin_name, name, default_value) + + def get_integer(self, name, default_value=0): + return core.BNSettingGetInteger(self.plugin_name, name, default_value) + + def get_string(self, name, default_value=""): + return core.BNSettingGetString(self.plugin_name, name, default_value) + + def get_integer_list(self, name): + length = ctypes.c_ulonglong() + length.value = 0 + default_list = ctypes.POINTER(ctypes.c_ulonglong)() + result = core.BNSettingGetIntegerList(self.plugin_name, name, default_list, ctypes.byref(length)) + out_list = [] + for i in xrange(length.value): + out_list.append(result[i]) + core.BNFreeSettingIntegerList(result, length) + return out_list + + def get_string_list(self, name): + length = ctypes.c_ulonglong() + length.value = 0 + default_list = ctypes.POINTER(ctypes.c_char_p)() + result = core.BNSettingGetStringList(self.plugin_name, name, default_list, ctypes.byref(length)) + out_list = [] + for i in xrange(length.value): + out_list.append(result[i]) + core.BNFreeSettingStringList(result, length) + return out_list + + def get_double(self, name, default_value=0.0): + return core.BNSettingGetDouble(self.plugin_name, name, default_value) + + def is_bool(self, name): + return core.BNSettingIsBool(self.plugin_name, name) + + def is_integer(self, name): + return core.BNSettingIsInteger(self.plugin_name, name) + + def is_string(self, name): + return core.BNSettingIsString(self.plugin_name, name) + + def is_string_list(self, name): + return core.BNSettingIsStringList(self.plugin_name, name) + + def is_integer_list(self, name): + return core.BNSettingIsIntegerList(self.plugin_name, name) + + def is_double(self, name): + return core.BNSettingIsDouble(self.plugin_name, name) + + def is_present(self, name): + return core.BNSettingIsPresent(self.plugin_name, name) + + def set_bool(self, name, value, auto_flush=True): + return core.BNSettingSetBool(self.plugin_name, name, value, auto_flush) + + def set_integer(self, name, value, auto_flush=True): + return core.BNSettingSetInteger(self.plugin_name, name, value, auto_flush) + + def set_string(self, name, value, auto_flush=True): + return core.BNSettingSetString(self.plugin_name, name, value, auto_flush) + + def set_integerList(self, name, value, auto_flush=True): + length = ctypes.c_ulonglong() + length.value = len(value) + default_list = (ctypes.c_ulonglong * len(value))() + for i in xrange(len(value)): + default_list[i] = value[i] + + return core.BNSettingSetIntegerList(self.plugin_name, name, default_list, length, auto_flush) + + def set_stringList(self, name, value, auto_flush=True): + length = ctypes.c_ulonglong() + length.value = len(value) + default_list = (ctypes.c_char_p * len(value))() + for i in xrange(len(value)): + default_list[i] = str(value[i]) + + return core.BNSettingSetStringList(self.plugin_name, name, default_list, length, auto_flush) + + def set_double(self, name, value, auto_flush=True): + return core.BNSettingSetDouble(self.plugin_name, name, value, auto_flush) -- cgit v1.3.1 From c154e1f9400f6a8838405f4a4fd41dbc6f2fcba3 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Mon, 3 Jul 2017 17:58:04 -0400 Subject: Add setting removal APIs, allow negative integers --- binaryninjaapi.h | 11 +++++++---- binaryninjacore.h | 12 +++++++----- python/setting.py | 33 ++++++++++++++++++++++++++++----- settings.cpp | 28 +++++++++++++++++++--------- 4 files changed, 61 insertions(+), 23 deletions(-) (limited to 'python/setting.py') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 3eb8b82e..f5850ad0 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2868,9 +2868,9 @@ namespace BinaryNinja { public: static bool GetBool(const std::string& settingGroup, const std::string& name, bool defaultValue); - static uint64_t GetInteger(const std::string& settingGroup, const std::string& name, uint64_t defaultValue=0); + static int64_t GetInteger(const std::string& settingGroup, const std::string& name, int64_t defaultValue=0); static std::string GetString(const std::string& settingGroup, const std::string& name, const std::string& defaultValue=""); - static std::vector GetIntegerList(const std::string& settingGroup, const std::string& name, const std::vector& defaultValue={}); + static std::vector GetIntegerList(const std::string& settingGroup, const std::string& name, const std::vector& defaultValue={}); static std::vector GetStringList(const std::string& settingGroup, const std::string& name, const std::vector& defaultValue={}); static double GetDouble(const std::string& settingGroup, const std::string& name, double defaultValue=0.0); @@ -2888,7 +2888,7 @@ namespace BinaryNinja bool autoFlush=true); static bool Set(const std::string& settingGroup, const std::string& name, - uint64_t value, + int64_t value, bool autoFlush=true); static bool Set(const std::string& settingGroup, const std::string& name, @@ -2896,7 +2896,7 @@ namespace BinaryNinja bool autoFlush=true); static bool Set(const std::string& settingGroup, const std::string& name, - const std::vector& value, + const std::vector& value, bool autoFlush=true); static bool Set(const std::string& settingGroup, const std::string& name, @@ -2906,6 +2906,9 @@ namespace BinaryNinja const std::string& name, double value, bool autoFlush=true); + + static bool RemoveSettingGroup(const std::string& settingGroup, bool autoFlush=true); + static bool RemoveSetting(const std::string& settingGroup, const std::string& setting, bool autoFlush=true); static bool FlushSettings(); }; diff --git a/binaryninjacore.h b/binaryninjacore.h index 0394517f..eb9927ab 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -2885,15 +2885,15 @@ extern "C" // Settings APIs BINARYNINJACOREAPI bool BNSettingGetBool(const char* settingGroup, const char* name, bool defaultValue); - BINARYNINJACOREAPI uint64_t BNSettingGetInteger(const char* settingGroup, const char* name, uint64_t defaultValue); + BINARYNINJACOREAPI int64_t BNSettingGetInteger(const char* settingGroup, const char* name, int64_t defaultValue); BINARYNINJACOREAPI char* BNSettingGetString(const char* settingGroup, const char* name, const char* defaultValue); // intoutSize is number of elements in defaultValue one entry and number of elements in return type on exit - BINARYNINJACOREAPI uint64_t* BNSettingGetIntegerList(const char* settingGroup, const char* name, uint64_t* defaultValue, size_t* inoutSize); + BINARYNINJACOREAPI int64_t* BNSettingGetIntegerList(const char* settingGroup, const char* name, int64_t* defaultValue, size_t* inoutSize); // intoutSize is number of elements in defaultValue one entry and number of elements in return type on exit BINARYNINJACOREAPI const char** BNSettingGetStringList(const char* settingGroup, const char* name, const char** defaultValue, size_t* inoutSize); BINARYNINJACOREAPI double BNSettingGetDouble(const char* settingGroup, const char* name, double defaultValue); - BINARYNINJACOREAPI void BNFreeSettingIntegerList(uint64_t* integerList); + BINARYNINJACOREAPI void BNFreeSettingIntegerList(int64_t* integerList); //Check the type of a core setting BINARYNINJACOREAPI bool BNSettingIsBool(const char* name, const char* settingGroup); BINARYNINJACOREAPI bool BNSettingIsInteger(const char* name, const char* settingGroup); @@ -2905,12 +2905,14 @@ extern "C" BINARYNINJACOREAPI bool BNSettingIsPresent(const char* settingGroup, const char* name); BINARYNINJACOREAPI bool BNSettingSetBool(const char* settingGroup, const char* name, bool value, bool autoFlush); - BINARYNINJACOREAPI bool BNSettingSetInteger(const char* settingGroup, const char* name, uint64_t value, bool autoFlush); + BINARYNINJACOREAPI bool BNSettingSetInteger(const char* settingGroup, const char* name, int64_t value, bool autoFlush); BINARYNINJACOREAPI bool BNSettingSetString(const char* settingGroup, const char* name, const char* value, bool autoFlush); BINARYNINJACOREAPI bool BNSettingSetDouble(const char* settingGroup, const char* name, double value, bool autoFlush); - BINARYNINJACOREAPI bool BNSettingSetIntegerList(const char* settingGroup, const char* name, const uint64_t* value, size_t size, bool autoFlush); + BINARYNINJACOREAPI bool BNSettingSetIntegerList(const char* settingGroup, const char* name, const int64_t* value, size_t size, bool autoFlush); BINARYNINJACOREAPI bool BNSettingSetStringList(const char* settingGroup, const char* name, const char** value, size_t size, bool autoFlush); + BINARYNINJACOREAPI bool BNSettingRemoveSetting(const char* settingGroup, const char* setting, bool autoFlush); + BINARYNINJACOREAPI bool BNSettingRemoveSettingGroup(const char* settingGroup, bool autoFlush); BINARYNINJACOREAPI bool BNSettingFlushSettings(); //Metadata APIs diff --git a/python/setting.py b/python/setting.py index 975f393e..d52ac1ec 100644 --- a/python/setting.py +++ b/python/setting.py @@ -40,7 +40,7 @@ class Setting(object): def get_integer_list(self, name): length = ctypes.c_ulonglong() length.value = 0 - default_list = ctypes.POINTER(ctypes.c_ulonglong)() + default_list = ctypes.POINTER(ctypes.c_longlong)() result = core.BNSettingGetIntegerList(self.plugin_name, name, default_list, ctypes.byref(length)) out_list = [] for i in xrange(length.value): @@ -56,7 +56,7 @@ class Setting(object): out_list = [] for i in xrange(length.value): out_list.append(result[i]) - core.BNFreeSettingStringList(result, length) + core.BNFreeStringList(result, length) return out_list def get_double(self, name, default_value=0.0): @@ -92,16 +92,16 @@ class Setting(object): def set_string(self, name, value, auto_flush=True): return core.BNSettingSetString(self.plugin_name, name, value, auto_flush) - def set_integerList(self, name, value, auto_flush=True): + def set_integer_list(self, name, value, auto_flush=True): length = ctypes.c_ulonglong() length.value = len(value) - default_list = (ctypes.c_ulonglong * len(value))() + default_list = (ctypes.c_longlong * len(value))() for i in xrange(len(value)): default_list[i] = value[i] return core.BNSettingSetIntegerList(self.plugin_name, name, default_list, length, auto_flush) - def set_stringList(self, name, value, auto_flush=True): + def set_string_list(self, name, value, auto_flush=True): length = ctypes.c_ulonglong() length.value = len(value) default_list = (ctypes.c_char_p * len(value))() @@ -112,3 +112,26 @@ class Setting(object): def set_double(self, name, value, auto_flush=True): return core.BNSettingSetDouble(self.plugin_name, name, value, auto_flush) + + def set(self, name, value, auto_flush=True): + if isinstance(value, bool): + return self.set_bool(name, value, auto_flush) + elif isinstance(value, int): + return self.set_integer(name, value, auto_flush) + elif isinstance(value, str): + return self.set_string(name, value, auto_flush) + elif isinstance(value, list) and len(value) == 0: + return self.set_integer_list(name, value, auto_flush) + elif isinstance(value, list) and len(value) > 0 and isinstance(value[0], int): + return self.set_integer_list(name, value, auto_flush) + elif isinstance(value, list) and len(value) > 0 and isinstance(value[0], str): + return self.set_string_list(name, value, auto_flush) + elif isinstance(value, float): + return self.set_double(name, value, auto_flush) + raise ValueError("value is not one of (int, bool, float, str, [int], [str]) types") + + def remove_setting_group(self, auto_flush=True): + core.BNSettingRemoveSettingGroup(self.plugin_name, auto_flush) + + def remove_setting(self, setting, auto_flush=True): + core.BNSettingRemoveSetting(self.plugin_name, setting, auto_flush) \ No newline at end of file diff --git a/settings.cpp b/settings.cpp index a60b7e3a..b30e6098 100644 --- a/settings.cpp +++ b/settings.cpp @@ -10,7 +10,7 @@ bool Setting::GetBool(const std::string& pluginName, const std::string& name, bo return BNSettingGetBool(pluginName.c_str(), name.c_str(), defaultValue); } -uint64_t Setting::GetInteger(const std::string& pluginName, const std::string& name, uint64_t defaultValue) +int64_t Setting::GetInteger(const std::string& pluginName, const std::string& name, int64_t defaultValue) { return BNSettingGetInteger(pluginName.c_str(), name.c_str(), defaultValue); } @@ -25,17 +25,17 @@ double Setting::GetDouble(const std::string& pluginName, const std::string& name return BNSettingGetDouble(pluginName.c_str(), name.c_str(), defaultValue); } -std::vector Setting::GetIntegerList(const std::string& pluginName, +std::vector Setting::GetIntegerList(const std::string& pluginName, const std::string& name, - const std::vector& defaultValue) + const std::vector& defaultValue) { - uint64_t* buffer = new uint64_t[defaultValue.size()]; - memcpy(&buffer[0], &defaultValue[0], sizeof(uint64_t) * defaultValue.size()); + int64_t* buffer = new int64_t[defaultValue.size()]; + memcpy(&buffer[0], &defaultValue[0], sizeof(int64_t) * defaultValue.size()); size_t size = defaultValue.size(); - uint64_t* outBuffer = BNSettingGetIntegerList(pluginName.c_str(), name.c_str(), buffer, &size); + int64_t* outBuffer = BNSettingGetIntegerList(pluginName.c_str(), name.c_str(), buffer, &size); delete[] buffer; - vector out(outBuffer, outBuffer + size); + vector out(outBuffer, outBuffer + size); BNFreeSettingIntegerList(buffer); return out; } @@ -107,7 +107,7 @@ bool Setting::Set(const std::string& settingGroup, bool Setting::Set(const std::string& settingGroup, const std::string& name, - uint64_t value, + int64_t value, bool autoFlush) { return BNSettingSetInteger(settingGroup.c_str(), name.c_str(), value, autoFlush); @@ -123,7 +123,7 @@ bool Setting::Set(const std::string& settingGroup, bool Setting::Set(const std::string& settingGroup, const std::string& name, - const std::vector& value, + const std::vector& value, bool autoFlush) { return BNSettingSetIntegerList(settingGroup.c_str(), name.c_str(), &value[0], value.size(), autoFlush); @@ -158,6 +158,16 @@ bool Setting::Set(const std::string& settingGroup, return BNSettingSetDouble(settingGroup.c_str(), name.c_str(), value, autoFlush); } +bool Setting::RemoveSettingGroup(const std::string& settingGroup, bool autoFlush) +{ + return BNSettingRemoveSettingGroup(settingGroup.c_str(), autoFlush); +} + +bool Setting::RemoveSetting(const std::string& settingGroup, const std::string& setting, bool autoFlush) +{ + return BNSettingRemoveSetting(settingGroup.c_str(), setting.c_str(), autoFlush); +} + bool Setting::FlushSettings() { return BNSettingFlushSettings(); -- cgit v1.3.1 From f548d07f5b9f172c9ad089f62751ecd0c5cdc630 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Mon, 3 Jul 2017 22:57:54 -0400 Subject: Allow default settings for integer and string lists --- python/setting.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'python/setting.py') diff --git a/python/setting.py b/python/setting.py index d52ac1ec..d58c8955 100644 --- a/python/setting.py +++ b/python/setting.py @@ -37,21 +37,25 @@ class Setting(object): def get_string(self, name, default_value=""): return core.BNSettingGetString(self.plugin_name, name, default_value) - def get_integer_list(self, name): + def get_integer_list(self, name, default_value=[]): length = ctypes.c_ulonglong() - length.value = 0 - default_list = ctypes.POINTER(ctypes.c_longlong)() + length.value = len(default_value) + default_list = (ctypes.c_longlong * len(default_value))() + for i in range(len(default_value)): + default_list[i] = default_value[i] result = core.BNSettingGetIntegerList(self.plugin_name, name, default_list, ctypes.byref(length)) out_list = [] for i in xrange(length.value): out_list.append(result[i]) - core.BNFreeSettingIntegerList(result, length) + core.BNFreeSettingIntegerList(result) return out_list - def get_string_list(self, name): + def get_string_list(self, name, default_value=[]): length = ctypes.c_ulonglong() - length.value = 0 - default_list = ctypes.POINTER(ctypes.c_char_p)() + length.value = len(default_value) + default_list = (ctypes.c_char_p * len(default_value))() + for i in range(len(default_value)): + default_list[i] = default_value[i] result = core.BNSettingGetStringList(self.plugin_name, name, default_list, ctypes.byref(length)) out_list = [] for i in xrange(length.value): -- cgit v1.3.1