diff options
| -rw-r--r-- | binaryninjaapi.h | 5 | ||||
| -rw-r--r-- | binaryninjacore.h | 5 | ||||
| -rw-r--r-- | interaction.cpp | 60 | ||||
| -rw-r--r-- | python/interaction.py | 67 |
4 files changed, 120 insertions, 17 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 76df58ce..d0101b22 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -5136,6 +5136,11 @@ __attribute__ ((format (printf, 1, 2))) uint64_t addressResult; std::string stringResult; size_t indexResult; + bool hasDefault; + int64_t intDefault; + uint64_t addressDefault; + std::string stringDefault; + size_t indexDefault; static FormInputField Label(const std::string& text); static FormInputField Separator(); diff --git a/binaryninjacore.h b/binaryninjacore.h index 4587f1f9..8a0cf7d5 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -2344,6 +2344,11 @@ extern "C" uint64_t addressResult; char* stringResult; size_t indexResult; + bool hasDefault; + int64_t intDefault; + uint64_t addressDefault; + const char* stringDefault; + size_t indexDefault; }; struct BNInteractionHandlerCallbacks diff --git a/interaction.cpp b/interaction.cpp index 57da8ea6..7a7fa6bb 100644 --- a/interaction.cpp +++ b/interaction.cpp @@ -11,6 +11,7 @@ FormInputField FormInputField::Label(const string& text) FormInputField result; result.type = LabelFormField; result.prompt = text; + result.hasDefault = false; return result; } @@ -19,6 +20,7 @@ FormInputField FormInputField::Separator() { FormInputField result; result.type = SeparatorFormField; + result.hasDefault = false; return result; } @@ -28,6 +30,7 @@ FormInputField FormInputField::TextLine(const string& prompt) FormInputField result; result.type = TextLineFormField; result.prompt = prompt; + result.hasDefault = false; return result; } @@ -37,6 +40,7 @@ FormInputField FormInputField::MultilineText(const string& prompt) FormInputField result; result.type = MultilineTextFormField; result.prompt = prompt; + result.hasDefault = false; return result; } @@ -46,6 +50,7 @@ FormInputField FormInputField::Integer(const string& prompt) FormInputField result; result.type = IntegerFormField; result.prompt = prompt; + result.hasDefault = false; return result; } @@ -57,6 +62,7 @@ FormInputField FormInputField::Address(const std::string& prompt, BinaryView* vi result.prompt = prompt; result.view = view; result.currentAddress = currentAddress; + result.hasDefault = false; return result; } @@ -67,6 +73,7 @@ FormInputField FormInputField::Choice(const string& prompt, const vector<string> result.type = ChoiceFormField; result.prompt = prompt; result.choices = choices; + result.hasDefault = false; return result; } @@ -77,6 +84,7 @@ FormInputField FormInputField::OpenFileName(const string& prompt, const string& result.type = OpenFileNameFormField; result.prompt = prompt; result.ext = ext; + result.hasDefault = false; return result; } @@ -88,6 +96,7 @@ FormInputField FormInputField::SaveFileName(const string& prompt, const string& result.prompt = prompt; result.ext = ext; result.defaultName = defaultName; + result.hasDefault = false; return result; } @@ -98,6 +107,7 @@ FormInputField FormInputField::DirectoryName(const string& prompt, const string& result.type = DirectoryNameFormField; result.prompt = prompt; result.defaultName = defaultName; + result.hasDefault = false; return result; } @@ -338,6 +348,31 @@ static bool GetFormInputCallback(void* ctxt, BNFormInputField* fieldBuf, size_t fields.push_back(FormInputField::Label(fieldBuf[i].prompt)); break; } + fields.back().hasDefault = fieldBuf[i].hasDefault; + if (fieldBuf[i].hasDefault) + { + switch (fieldBuf[i].type) + { + case TextLineFormField: + case MultilineTextFormField: + case OpenFileNameFormField: + case SaveFileNameFormField: + case DirectoryNameFormField: + fields.back().stringDefault = fieldBuf[i].stringDefault; + break; + case IntegerFormField: + fields.back().intDefault = fieldBuf[i].intDefault; + break; + case AddressFormField: + fields.back().addressDefault = fieldBuf[i].addressDefault; + break; + case ChoiceFormField: + fields.back().indexDefault = fieldBuf[i].indexDefault; + break; + default: + break; + } + } } if (!handler->GetFormInput(fields, title)) @@ -547,6 +582,31 @@ bool BinaryNinja::GetFormInput(vector<FormInputField>& fields, const string& tit default: break; } + fieldBuf[i].hasDefault = fields[i].hasDefault; + if (fields[i].hasDefault) + { + switch (fields[i].type) + { + case TextLineFormField: + case MultilineTextFormField: + case OpenFileNameFormField: + case SaveFileNameFormField: + case DirectoryNameFormField: + fieldBuf[i].stringDefault = fields[i].stringDefault.c_str(); + break; + case IntegerFormField: + fieldBuf[i].intDefault = fields[i].intDefault; + break; + case AddressFormField: + fieldBuf[i].addressDefault = fields[i].addressDefault; + break; + case ChoiceFormField: + fieldBuf[i].indexDefault = fields[i].indexDefault; + break; + default: + break; + } + } } bool ok = BNGetFormInput(fieldBuf, fields.size(), title.c_str()); diff --git a/python/interaction.py b/python/interaction.py index 708f97ab..13cf93c6 100644 --- a/python/interaction.py +++ b/python/interaction.py @@ -41,6 +41,7 @@ class LabelField(object): def _fill_core_struct(self, value): value.type = FormInputFieldType.LabelFormField + value.hasDefault = False value.prompt = self._text def _fill_core_result(self, value): @@ -65,6 +66,7 @@ class SeparatorField(object): """ def _fill_core_struct(self, value): value.type = FormInputFieldType.SeparatorFormField + value.hasDefault = False def _fill_core_result(self, value): pass @@ -77,13 +79,17 @@ class TextLineField(object): """ ``TextLineField`` Adds prompt for text string input. Result is stored in self.result as a string on completion. """ - def __init__(self, prompt): + def __init__(self, prompt, default=None): self._prompt = prompt + self._default = default self._result = None def _fill_core_struct(self, value): value.type = FormInputFieldType.TextLineFormField value.prompt = self._prompt + value.hasDefault = self._default is not None + if self._default is not None: + value.stringDefault = self._default def _fill_core_result(self, value): value.stringResult = core.BNAllocString(str(self._result)) @@ -115,13 +121,17 @@ class MultilineTextField(object): ``MultilineTextField`` add multi-line text string input field. Result is stored in self.result as a string. This option is not supported on the command-line. """ - def __init__(self, prompt): + def __init__(self, prompt, default=None): self._prompt = prompt + self._default = default self._result = None def _fill_core_struct(self, value): value.type = FormInputFieldType.MultilineTextFormField value.prompt = self._prompt + value.hasDefault = self._default is not None + if self._default is not None: + value.stringDefault = self._default def _fill_core_result(self, value): value.stringResult = core.BNAllocString(str(self._result)) @@ -152,13 +162,17 @@ class IntegerField(object): """ ``IntegerField`` add prompt for integer. Result is stored in self.result as an int. """ - def __init__(self, prompt): + def __init__(self, prompt, default=None): self._prompt = prompt + self._default = default self._result = None def _fill_core_struct(self, value): value.type = FormInputFieldType.IntegerFormField value.prompt = self._prompt + value.hasDefault = self._default is not None + if self._default is not None: + value.intDefault = self._default def _fill_core_result(self, value): value.intResult = self._result @@ -194,10 +208,11 @@ class AddressField(object): disregarded. Additionally where as in the UI the result defaults to hexadecimal on the command-line 0x must be \ specified. """ - def __init__(self, prompt, view=None, current_address=0): + def __init__(self, prompt, view=None, current_address=0, default=None): self._prompt = prompt self._view = view self._current_address = current_address + self._default = default self._result = None def _fill_core_struct(self, value): @@ -207,6 +222,9 @@ class AddressField(object): if self._view is not None: value.view = self._view.handle value.currentAddress = self._current_address + value.hasDefault = self._default is not None + if self._default is not None: + value.addressDefault = self._default def _fill_core_result(self, value): value.addressResult = self._result @@ -258,11 +276,11 @@ class ChoiceField(object): :attr str prompt: prompt to be presented to the user :attr list(str) choices: list of choices to choose from - """ - def __init__(self, prompt, choices): + def __init__(self, prompt, choices, default=None): self._prompt = prompt self._choices = choices + self._default = default self._result = None def _fill_core_struct(self, value): @@ -273,6 +291,9 @@ class ChoiceField(object): choice_buf[i] = self._choices[i].encode('charmap') value.choices = choice_buf value.count = len(self._choices) + value.hasDefault = self._default is not None + if self._default is not None: + value.indexDefault = self._default def _fill_core_result(self, value): value.indexResult = self._result @@ -312,15 +333,19 @@ class OpenFileNameField(object): """ ``OpenFileNameField`` prompts the user to specify a file name to open. Result is stored in self.result as a string. """ - def __init__(self, prompt, ext=""): + def __init__(self, prompt, ext="", default=None): self._prompt = prompt self._ext = ext + self._default = default self._result = None def _fill_core_struct(self, value): value.type = FormInputFieldType.OpenFileNameFormField value.prompt = self._prompt value.ext = self._ext + value.hasDefault = self._default is not None + if self._default is not None: + value.stringDefault = self._default def _fill_core_result(self, value): value.stringResult = core.BNAllocString(str(self.result)) @@ -360,10 +385,11 @@ class SaveFileNameField(object): """ ``SaveFileNameField`` prompts the user to specify a file name to save. Result is stored in self.result as a string. """ - def __init__(self, prompt, ext="", default_name=""): + def __init__(self, prompt, ext="", default_name="", default=None): self._prompt = prompt self._ext = ext self._default_name = default_name + self._default = default self._result = None def _fill_core_struct(self, value): @@ -371,6 +397,9 @@ class SaveFileNameField(object): value.prompt = self._prompt value.ext = self._ext value.defaultName = self._default_name + value.hasDefault = self._default is not None + if self._default is not None: + value.stringDefault = self._default def _fill_core_result(self, value): value.stringResult = core.BNAllocString(str(self._result)) @@ -420,15 +449,19 @@ class DirectoryNameField(object): ``DirectoryNameField`` prompts the user to specify a directory name to open. Result is stored in self.result as a string. """ - def __init__(self, prompt, default_name=""): + def __init__(self, prompt, default_name="", default=None): self._prompt = prompt self._default_name = default_name + self._default = default self._result = None def _fill_core_struct(self, value): value.type = FormInputFieldType.DirectoryNameFormField value.prompt = self._prompt value.defaultName = self._default_name + value.hasDefault = self._default is not None + if self._default is not None: + value.stringDefault = self._default def _fill_core_result(self, value): value.stringResult = core.BNAllocString(str(self._result)) @@ -621,27 +654,27 @@ class InteractionHandler(object): elif fields[i].type == FormInputFieldType.SeparatorFormField: field_objs.append(SeparatorField()) elif fields[i].type == FormInputFieldType.TextLineFormField: - field_objs.append(TextLineField(fields[i].prompt)) + field_objs.append(TextLineField(fields[i].prompt, default=fields[i].stringDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.MultilineTextFormField: - field_objs.append(MultilineTextField(fields[i].prompt)) + field_objs.append(MultilineTextField(fields[i].prompt, default=fields[i].stringDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.IntegerFormField: - field_objs.append(IntegerField(fields[i].prompt)) + field_objs.append(IntegerField(fields[i].prompt, default=fields[i].intDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.AddressFormField: view = None if fields[i].view: view = binaryview.BinaryView(handle = core.BNNewViewReference(fields[i].view)) - field_objs.append(AddressField(fields[i].prompt, view, fields[i].currentAddress)) + field_objs.append(AddressField(fields[i].prompt, view, fields[i].currentAddress, default=fields[i].addressDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.ChoiceFormField: choices = [] for j in range(0, fields[i].count): choices.append(fields[i].choices[j]) - field_objs.append(ChoiceField(fields[i].prompt, choices)) + field_objs.append(ChoiceField(fields[i].prompt, choices, default=fields[i].choiceDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.OpenFileNameFormField: - field_objs.append(OpenFileNameField(fields[i].prompt, fields[i].ext)) + field_objs.append(OpenFileNameField(fields[i].prompt, fields[i].ext, default=fields[i].stringDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.SaveFileNameFormField: - field_objs.append(SaveFileNameField(fields[i].prompt, fields[i].ext, fields[i].defaultName)) + field_objs.append(SaveFileNameField(fields[i].prompt, fields[i].ext, fields[i].defaultName, default=fields[i].stringDefault if fields[i].hasDefault else None)) elif fields[i].type == FormInputFieldType.DirectoryNameFormField: - field_objs.append(DirectoryNameField(fields[i].prompt, fields[i].defaultName)) + field_objs.append(DirectoryNameField(fields[i].prompt, fields[i].defaultName, default=fields[i].stringDefault if fields[i].hasDefault else None)) else: field_objs.append(LabelField(fields[i].prompt)) if not self.get_form_input(field_objs, title): |
