From c558b0c44bc80edaaf016e899c391f39dddebeef Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Thu, 29 Aug 2024 19:07:43 -0400 Subject: Fix open with options with rust plugins --- rust/src/custombinaryview.rs | 71 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 13 deletions(-) (limited to 'rust/src/custombinaryview.rs') diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs index c427b6ab..1ec0ca80 100644 --- a/rust/src/custombinaryview.rs +++ b/rust/src/custombinaryview.rs @@ -81,25 +81,47 @@ where actual_parent: &data, }; - if let Ok(bv) = view_type.create_custom_view(&data, builder) { - // force a leak of the Ref; failure to do this would result - // in the refcount going to 0 in the process of returning it - // to the core -- we're transferring ownership of the Ref here - Ref::into_raw(bv.handle).handle - } else { - error!("CustomBinaryViewType::create_custom_view returned Err"); - - ptr::null_mut() + match view_type.create_custom_view(&data, builder) { + Ok(bv) => { + // force a leak of the Ref; failure to do this would result + // in the refcount going to 0 in the process of returning it + // to the core -- we're transferring ownership of the Ref here + Ref::into_raw(bv.handle).handle + }, + Err(_) => { + error!("CustomBinaryViewType::create_custom_view returned Err"); + ptr::null_mut() + } } }) } - - #[allow(clippy::extra_unused_type_parameters)] // TODO : This is bad; need to finish this stub - extern "C" fn cb_parse(_ctxt: *mut c_void, _data: *mut BNBinaryView) -> *mut BNBinaryView + + extern "C" fn cb_parse(ctxt: *mut c_void, data: *mut BNBinaryView) -> *mut BNBinaryView where T: CustomBinaryViewType, { - ffi_wrap!("BinaryViewTypeBase::parse", ptr::null_mut()) + ffi_wrap!("BinaryViewTypeBase::parse", unsafe { + let view_type = &*(ctxt as *mut T); + let data = BinaryView::from_raw(BNNewViewReference(data)); + + let builder = CustomViewBuilder { + view_type, + actual_parent: &data, + }; + + match view_type.parse_custom_view(&data, builder) { + Ok(bv) => { + // force a leak of the Ref; failure to do this would result + // in the refcount going to 0 in the process of returning it + // to the core -- we're transferring ownership of the Ref here + Ref::into_raw(bv.handle).handle + }, + Err(_) => { + error!("CustomBinaryViewType::parse returned Err"); + ptr::null_mut() + } + } + }) } extern "C" fn cb_load_settings(ctxt: *mut c_void, data: *mut BNBinaryView) -> *mut BNSettings @@ -212,6 +234,20 @@ pub trait BinaryViewTypeExt: BinaryViewTypeBase { unsafe { Ok(BinaryView::from_raw(handle)) } } + + fn parse(&self, data: &BinaryView) -> Result> { + let handle = unsafe { BNParseBinaryViewOfType(self.as_ref().0, data.handle) }; + + if handle.is_null() { + error!( + "failed to parse BinaryView of BinaryViewType '{}'", + self.name() + ); + return Err(()); + } + + unsafe { Ok(BinaryView::from_raw(handle)) } + } } impl BinaryViewTypeExt for T {} @@ -301,6 +337,15 @@ pub trait CustomBinaryViewType: 'static + BinaryViewTypeBase + Sync { data: &BinaryView, builder: CustomViewBuilder<'builder, Self>, ) -> Result>; + + fn parse_custom_view<'builder>( + &self, + data: &BinaryView, + builder: CustomViewBuilder<'builder, Self>, + ) -> Result> { + // TODO: Check to make sure data.type_name is not Self::type_name ? + self.create_custom_view(data, builder) + } } /// Represents a request from the core to instantiate a custom BinaryView -- cgit v1.3.1