11use binaryninjacore_sys:: * ;
2+ use std:: fmt:: { Debug , Formatter } ;
23
34use crate :: rc:: { Guard , RefCountable } ;
45use crate :: {
@@ -71,13 +72,20 @@ impl TypeLibrary {
7172
7273 /// Looks up the first type library found with a matching name. Keep in mind that names are not
7374 /// necessarily unique.
75+ ///
76+ /// NOTE: If the type library architecture's associated platform has not been initialized, this will
77+ /// return `None`. To make sure that the platform has been initialized, one should instead get the type
78+ /// libraries through [`Platform::get_type_libraries_by_name`].
7479 pub fn from_name ( arch : CoreArchitecture , name : & str ) -> Option < Ref < TypeLibrary > > {
7580 let name = name. to_cstr ( ) ;
7681 let handle = unsafe { BNLookupTypeLibraryByName ( arch. handle , name. as_ptr ( ) ) } ;
7782 NonNull :: new ( handle) . map ( |h| unsafe { TypeLibrary :: ref_from_raw ( h) } )
7883 }
7984
80- /// Attempts to grab a type library associated with the provided Architecture and GUID pair
85+ /// Attempts to grab a type library associated with the provided Architecture and GUID pair.
86+ ///
87+ /// NOTE: If the associated platform for the architecture has not been initialized,
88+ /// this will return `None`. Avoid calling this outside of a view context.
8189 pub fn from_guid ( arch : CoreArchitecture , guid : & str ) -> Option < Ref < TypeLibrary > > {
8290 let guid = guid. to_cstr ( ) ;
8391 let handle = unsafe { BNLookupTypeLibraryByGuid ( arch. handle , guid. as_ptr ( ) ) } ;
@@ -92,9 +100,10 @@ impl TypeLibrary {
92100 }
93101
94102 /// The primary name associated with this type library
95- pub fn name ( & self ) -> Option < BnString > {
103+ pub fn name ( & self ) -> String {
96104 let result = unsafe { BNGetTypeLibraryName ( self . as_raw ( ) ) } ;
97- ( !result. is_null ( ) ) . then ( || unsafe { BnString :: from_raw ( result) } )
105+ assert ! ( !result. is_null( ) ) ;
106+ unsafe { BnString :: into_string ( result) }
98107 }
99108
100109 /// Sets the name of a type library instance that has not been finalized
@@ -108,9 +117,10 @@ impl TypeLibrary {
108117 /// dependencies on it recorded as "libc_generic", allowing a type library to be used across
109118 /// multiple platforms where each has a specific libc that also provides the name "libc_generic"
110119 /// as an `alternate_name`.
111- pub fn dependency_name ( & self ) -> Option < BnString > {
120+ pub fn dependency_name ( & self ) -> String {
112121 let result = unsafe { BNGetTypeLibraryDependencyName ( self . as_raw ( ) ) } ;
113- ( !result. is_null ( ) ) . then ( || unsafe { BnString :: from_raw ( result) } )
122+ assert ! ( !result. is_null( ) ) ;
123+ unsafe { BnString :: into_string ( result) }
114124 }
115125
116126 /// Sets the dependency name of a type library instance that has not been finalized
@@ -120,9 +130,10 @@ impl TypeLibrary {
120130 }
121131
122132 /// Returns the GUID associated with the type library
123- pub fn guid ( & self ) -> Option < BnString > {
133+ pub fn guid ( & self ) -> String {
124134 let result = unsafe { BNGetTypeLibraryGuid ( self . as_raw ( ) ) } ;
125- ( !result. is_null ( ) ) . then ( || unsafe { BnString :: from_raw ( result) } )
135+ assert ! ( !result. is_null( ) ) ;
136+ unsafe { BnString :: into_string ( result) }
126137 }
127138
128139 /// Sets the GUID of a type library instance that has not been finalized
@@ -179,10 +190,10 @@ impl TypeLibrary {
179190 }
180191
181192 /// Retrieves a metadata associated with the given key stored in the type library
182- pub fn query_metadata ( & self , key : & str ) -> Option < Metadata > {
193+ pub fn query_metadata ( & self , key : & str ) -> Option < Ref < Metadata > > {
183194 let key = key. to_cstr ( ) ;
184195 let result = unsafe { BNTypeLibraryQueryMetadata ( self . as_raw ( ) , key. as_ptr ( ) ) } ;
185- ( !result. is_null ( ) ) . then ( || unsafe { Metadata :: from_raw ( result) } )
196+ ( !result. is_null ( ) ) . then ( || unsafe { Metadata :: ref_from_raw ( result) } )
186197 }
187198
188199 /// Stores an object for the given key in the current type library. Objects stored using
@@ -208,10 +219,10 @@ impl TypeLibrary {
208219 }
209220
210221 /// Retrieves the metadata associated with the current type library.
211- pub fn metadata ( & self ) -> Metadata {
222+ pub fn metadata ( & self ) -> Ref < Metadata > {
212223 let md_handle = unsafe { BNTypeLibraryGetMetadata ( self . as_raw ( ) ) } ;
213224 assert ! ( !md_handle. is_null( ) ) ;
214- unsafe { Metadata :: from_raw ( md_handle) }
225+ unsafe { Metadata :: ref_from_raw ( md_handle) }
215226 }
216227
217228 // TODO: implement TypeContainer
@@ -300,6 +311,23 @@ impl TypeLibrary {
300311 }
301312}
302313
314+ impl Debug for TypeLibrary {
315+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
316+ f. debug_struct ( "TypeLibrary" )
317+ . field ( "name" , & self . name ( ) )
318+ . field ( "dependency_name" , & self . dependency_name ( ) )
319+ . field ( "arch" , & self . arch ( ) )
320+ . field ( "guid" , & self . guid ( ) )
321+ . field ( "alternate_names" , & self . alternate_names ( ) . to_vec ( ) )
322+ . field ( "platform_names" , & self . platform_names ( ) . to_vec ( ) )
323+ . field ( "metadata" , & self . metadata ( ) )
324+ // These two are too verbose.
325+ // .field("named_objects", &self.named_objects().to_vec())
326+ // .field("named_types", &self.named_types().to_vec())
327+ . finish ( )
328+ }
329+ }
330+
303331unsafe impl RefCountable for TypeLibrary {
304332 unsafe fn inc_ref ( handle : & Self ) -> Ref < Self > {
305333 Ref :: new ( Self {
0 commit comments