diff options
| author | Michael Krasnitski <michael.krasnitski@gmail.com> | 2023-01-06 02:04:49 -0500 |
|---|---|---|
| committer | Kyle Martin <krm504@nyu.edu> | 2023-01-10 00:11:27 -0500 |
| commit | 5b51a4ff188c426d8f90ac71ee6dde0fa3b5a495 (patch) | |
| tree | e7a8b87d95061e549ee48063999b838c56dba3a7 /rust/src/types.rs | |
| parent | 7eb2b8fc3bd39324a74e02652c8f678073947812 (diff) | |
Clean up various builders
Diffstat (limited to 'rust/src/types.rs')
| -rw-r--r-- | rust/src/types.rs | 120 |
1 files changed, 33 insertions, 87 deletions
diff --git a/rust/src/types.rs b/rust/src/types.rs index 75475b6e..5d524c76 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -295,10 +295,14 @@ impl TypeBuilder { pub(crate) unsafe fn from_raw(handle: *mut BNTypeBuilder) -> Self { debug_assert!(!handle.is_null()); - Self { handle } } + // Chainable terminal + pub fn finalize(&self) -> Ref<Type> { + unsafe { Type::ref_from_raw(BNFinalizeTypeBuilder(self.handle)) } + } + // Settable properties pub fn set_const<T: Into<Conf<bool>>>(&self, value: T) -> &Self { @@ -313,12 +317,6 @@ impl TypeBuilder { self } - // Chainable terminal - - pub fn finalize(&self) -> Ref<Type> { - unsafe { Type::ref_from_raw(BNFinalizeTypeBuilder(self.handle)) } - } - // Readable properties pub fn type_class(&self) -> TypeClass { @@ -424,7 +422,7 @@ impl TypeBuilder { if result.is_null() { Err(()) } else { - Ok(Enumeration::ref_from_raw(result)) + Ok(unsafe { Enumeration::ref_from_raw(result) }) } } @@ -670,13 +668,11 @@ pub struct Type { impl Type { unsafe fn from_raw(handle: *mut BNType) -> Self { debug_assert!(!handle.is_null()); - Self { handle } } pub(crate) unsafe fn ref_from_raw(handle: *mut BNType) -> Ref<Self> { debug_assert!(!handle.is_null()); - Ref::new(Self { handle }) } @@ -794,7 +790,7 @@ impl Type { if result.is_null() { Err(()) } else { - Ok(Enumeration::ref_from_raw(result)) + Ok(unsafe { Enumeration::ref_from_raw(result) }) } } @@ -1157,12 +1153,6 @@ impl Type { } } -impl From<&TypeBuilder> for Ref<Type> { - fn from(builder: &TypeBuilder) -> Self { - unsafe { Type::ref_from_raw(BNFinalizeTypeBuilder(builder.handle)) } - } -} - impl fmt::Display for Type { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", unsafe { @@ -1390,7 +1380,7 @@ impl EnumerationBuilder { } pub fn finalize(&self) -> Ref<Enumeration> { - Enumeration::new(self) + unsafe { Enumeration::ref_from_raw(BNFinalizeEnumerationBuilder(self.handle)) } } pub fn append<S: BnStrCompatible>(&self, name: S) -> &Self { @@ -1474,21 +1464,18 @@ pub struct Enumeration { } impl Enumeration { - pub fn new(builder: &EnumerationBuilder) -> Ref<Self> { - unsafe { - let handle = BNFinalizeEnumerationBuilder(builder.handle); - Ref::new(Self { handle }) - } - } - - fn from_raw(handle: *mut BNEnumeration) -> Self { + unsafe fn from_raw(handle: *mut BNEnumeration) -> Self { debug_assert!(!handle.is_null()); Self { handle } } - pub(crate) fn ref_from_raw(handle: *mut BNEnumeration) -> Ref<Self> { + pub(crate) unsafe fn ref_from_raw(handle: *mut BNEnumeration) -> Ref<Self> { debug_assert!(!handle.is_null()); - unsafe { Ref::new(Self { handle }) } + Ref::new(Self { handle }) + } + + pub fn builder() -> EnumerationBuilder { + EnumerationBuilder::new() } pub fn members(&self) -> Vec<EnumerationMember> { @@ -1509,15 +1496,9 @@ impl Enumeration { } } -impl From<&EnumerationBuilder> for Ref<Enumeration> { - fn from(builder: &EnumerationBuilder) -> Self { - Enumeration::new(builder) - } -} - unsafe impl RefCountable for Enumeration { unsafe fn inc_ref(handle: &Self) -> Ref<Self> { - Ref::new(Self::from_raw(BNNewEnumerationReference(handle.handle))) + Self::ref_from_raw(BNNewEnumerationReference(handle.handle)) } unsafe fn dec_ref(handle: &Self) { @@ -1543,15 +1524,15 @@ pub struct StructureBuilder { pub(crate) handle: *mut BNStructureBuilder, } -/// ``` +/// ```rust /// // Includes /// use binaryninja::types::{Structure, Type}; /// /// // Define struct, set size (in bytes) -/// let mut my_custom_struct = Structure::new();BnStr::from_raw(raw.name) -/// let field_1 = Self::named_int(5, false, "my_weird_int_type"); -/// let field_2 = Self::int(4, false); -/// let field_3 = Self::int(8, false); +/// let mut my_custom_struct = StructureBuilder::new(); +/// let field_1 = Type::named_int(5, false, "my_weird_int_type"); +/// let field_2 = Type::int(4, false); +/// let field_3 = Type::int(8, false); /// /// // Assign those fields /// my_custom_struct.append(&field_1, "field_4"); @@ -1580,7 +1561,7 @@ impl StructureBuilder { // Chainable terminal pub fn finalize(&self) -> Ref<Structure> { - Structure::new(self) + unsafe { Structure::ref_from_raw(BNFinalizeStructureBuilder(self.handle)) } } // Chainable builders/setters @@ -1609,13 +1590,18 @@ impl StructureBuilder { self } + pub fn set_structure_type(&self, t: StructureType) -> &Self { + unsafe { BNSetStructureBuilderType(self.handle, t) }; + self + } + pub fn append<'a, S: BnStrCompatible, T: Into<Conf<&'a Type>>>( - &'a self, + &self, t: T, name: S, access: MemberAccess, scope: MemberScope, - ) -> &'a Self { + ) -> &Self { let name = name.into_bytes_with_nul(); unsafe { BNAddStructureBuilderMember( @@ -1672,11 +1658,6 @@ impl StructureBuilder { self } - pub fn set_structure_type(&self, t: StructureType) -> &Self { - unsafe { BNSetStructureBuilderType(self.handle, t) }; - self - } - // Getters pub fn width(&self) -> u64 { @@ -1740,40 +1721,7 @@ pub struct Structure { pub(crate) handle: *mut BNStructure, } -/// ``` -/// // Includes -/// use binaryninja::types::{Structure, Type}; -/// -/// // Define struct, set size (in bytes) -/// let mut my_custom_struct = Structure::new(); -/// my_custom_struct.set_width(17); -/// -/// // Create some fields for the struct -/// let field_1 = Self::named_int(5, false, "my_weird_int_type"); -/// let field_2 = Self::int(4, false); -/// let field_3 = Self::int(8, false); -/// -/// // Assign those fields -/// my_custom_struct.append(&field_1, "field_4"); -/// my_custom_struct.insert(&field_1, "field_1", 0); -/// my_custom_struct.insert(&field_2, "field_2", 5); -/// my_custom_struct.insert(&field_3, "field_3", 9); -/// -/// // Convert structure to type -/// let my_custom_structure_type = Self::structure_type(&mut my_custom_struct); -/// -/// // Add the struct to the binary view to use in analysis -/// let bv = unsafe { BinaryView::from_raw(view) }; -/// bv.define_user_type("my_custom_struct", &my_custom_structure_type); -/// ``` impl Structure { - pub fn new(builder: &StructureBuilder) -> Ref<Self> { - unsafe { - let handle = BNFinalizeStructureBuilder(builder.handle); - Ref::new(Self { handle }) - } - } - unsafe fn from_raw(handle: *mut BNStructure) -> Self { debug_assert!(!handle.is_null()); Self { handle } @@ -1784,6 +1732,10 @@ impl Structure { Ref::new(Self { handle }) } + pub fn builder() -> StructureBuilder { + StructureBuilder::new() + } + pub fn width(&self) -> u64 { unsafe { BNGetStructureWidth(self.handle) } } @@ -1815,12 +1767,6 @@ impl Structure { // TODO : The other methods in the python version (alignment, packed, type, members, remove, replace, etc) } -impl From<&StructureBuilder> for Ref<Structure> { - fn from(builder: &StructureBuilder) -> Self { - Structure::new(builder) - } -} - impl Debug for Structure { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "Structure {{")?; |
