|
| 1 | +// Usage: cargo run --example dump_type_library <type_library_path> |
| 2 | + |
| 3 | +use binaryninja::binary_view::BinaryView; |
| 4 | +use binaryninja::file_metadata::FileMetadata; |
| 5 | +use binaryninja::type_library::TypeLibrary; |
| 6 | +use binaryninja::type_printer::{CoreTypePrinter, TokenEscapingType}; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let type_lib_str = std::env::args().nth(1).expect("No type library provided"); |
| 10 | + let type_lib_path = std::path::Path::new(&type_lib_str); |
| 11 | + |
| 12 | + println!("Starting session..."); |
| 13 | + // This loads all the core architecture, platform, etc plugins |
| 14 | + let _headless_session = |
| 15 | + binaryninja::headless::Session::new().expect("Failed to initialize session"); |
| 16 | + |
| 17 | + let type_lib = TypeLibrary::load_from_file(type_lib_path).expect("Failed to load type library"); |
| 18 | + let named_types = type_lib.named_types(); |
| 19 | + println!("Name: `{}`", type_lib.name()); |
| 20 | + println!("GUID: `{}`", type_lib.guid()); |
| 21 | + |
| 22 | + // Print out all the types as a c header. |
| 23 | + let type_lib_header_path = type_lib_path.with_extension("h"); |
| 24 | + println!( |
| 25 | + "Dumping {} types to: `{:?}`", |
| 26 | + named_types.len(), |
| 27 | + type_lib_header_path |
| 28 | + ); |
| 29 | + let type_printer = CoreTypePrinter::default(); |
| 30 | + let empty_bv = |
| 31 | + BinaryView::from_data(&FileMetadata::new(), &[]).expect("Failed to create empty view"); |
| 32 | + let printed_types = type_printer |
| 33 | + .print_all_types( |
| 34 | + &type_lib.named_types(), |
| 35 | + &empty_bv, |
| 36 | + 4, |
| 37 | + TokenEscapingType::NoTokenEscapingType, |
| 38 | + ) |
| 39 | + .expect("Failed to print types"); |
| 40 | + |
| 41 | + // Write the header to disk. |
| 42 | + std::fs::write(type_lib_header_path, printed_types) |
| 43 | + .expect("Failed to write type library header"); |
| 44 | +} |
0 commit comments