diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-12 15:56:58 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | 25a691dae4ff62f5eb06f7bf700b5e4de7bae067 (patch) | |
| tree | e365236cf0856cb9bd54fbb8aa2a54c12f49ac5d /rust/examples | |
| parent | 665c28986e646f9f911987893df4e83c24c492ec (diff) | |
[Rust] Add type library example
Diffstat (limited to 'rust/examples')
| -rw-r--r-- | rust/examples/dump_type_library.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/rust/examples/dump_type_library.rs b/rust/examples/dump_type_library.rs new file mode 100644 index 00000000..00e7fb09 --- /dev/null +++ b/rust/examples/dump_type_library.rs @@ -0,0 +1,44 @@ +// Usage: cargo run --example dump_type_library <type_library_path> + +use binaryninja::binary_view::BinaryView; +use binaryninja::file_metadata::FileMetadata; +use binaryninja::type_library::TypeLibrary; +use binaryninja::type_printer::{CoreTypePrinter, TokenEscapingType}; + +fn main() { + let type_lib_str = std::env::args().nth(1).expect("No type library provided"); + let type_lib_path = std::path::Path::new(&type_lib_str); + + println!("Starting session..."); + // This loads all the core architecture, platform, etc plugins + let _headless_session = + binaryninja::headless::Session::new().expect("Failed to initialize session"); + + let type_lib = TypeLibrary::load_from_file(type_lib_path).expect("Failed to load type library"); + let named_types = type_lib.named_types(); + println!("Name: `{}`", type_lib.name()); + println!("GUID: `{}`", type_lib.guid()); + + // Print out all the types as a c header. + let type_lib_header_path = type_lib_path.with_extension("h"); + println!( + "Dumping {} types to: `{:?}`", + named_types.len(), + type_lib_header_path + ); + let type_printer = CoreTypePrinter::default(); + let empty_bv = + BinaryView::from_data(&FileMetadata::new(), &[]).expect("Failed to create empty view"); + let printed_types = type_printer + .print_all_types( + &type_lib.named_types(), + &empty_bv, + 4, + TokenEscapingType::NoTokenEscapingType, + ) + .expect("Failed to print types"); + + // Write the header to disk. + std::fs::write(type_lib_header_path, printed_types) + .expect("Failed to write type library header"); +} |
