From 25a691dae4ff62f5eb06f7bf700b5e4de7bae067 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Mon, 12 May 2025 15:56:58 -0400 Subject: [Rust] Add type library example --- rust/examples/dump_type_library.rs | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 rust/examples/dump_type_library.rs (limited to 'rust/examples') 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 + +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"); +} -- cgit v1.3.1