summaryrefslogtreecommitdiff
path: root/plugins/bntl_utils/src/helper.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2026-02-11 18:04:07 -0800
committerMason Reed <35282038+emesare@users.noreply.github.com>2026-02-23 00:09:44 -0800
commit37008b7fa16837d04c1658868646cad681cbe035 (patch)
tree577c2b62ee47c78a5d31d11f2aa610e441f5c808 /plugins/bntl_utils/src/helper.rs
parent837f8590be80b7c98162e70e4f0c1814b83e9d7b (diff)
Add BNTL utility plugin
Allow users to easily create, diff, dump and validate type libraries Supports the following formats: - C header files (via core type parsers) - Binary files (collects exported and imported functions) - WinMD files (via `windows-metadata` crate) - Existing type library files (for easy fixups) - Apiset files (to resolve through forwarded windows dlls) Can be invoked as a regular plugin via UI commands or via CLI. Processing of type libraries inherently requires external linking, processing will automatically merge and deduplicate colliding type libraries so prefer to use inside a project or a directory and process all information (for a given platform) at once, rather than smaller invocations.
Diffstat (limited to 'plugins/bntl_utils/src/helper.rs')
-rw-r--r--plugins/bntl_utils/src/helper.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/plugins/bntl_utils/src/helper.rs b/plugins/bntl_utils/src/helper.rs
new file mode 100644
index 00000000..3fc1a047
--- /dev/null
+++ b/plugins/bntl_utils/src/helper.rs
@@ -0,0 +1,45 @@
+use binaryninja::rc::Ref;
+use binaryninja::types::{NamedTypeReference, Type, TypeClass, TypeLibrary};
+use std::path::Path;
+use walkdir::WalkDir;
+
+pub fn path_to_type_libraries(path: &Path) -> Vec<Ref<TypeLibrary>> {
+ WalkDir::new(path)
+ .into_iter()
+ .filter_map(|e| e.ok())
+ .filter(|e| e.file_type().is_file())
+ .filter(|e| e.path().extension().map_or(false, |ext| ext == "bntl"))
+ .filter_map(|e| TypeLibrary::load_from_file(e.path()))
+ .collect::<Vec<_>>()
+}
+
+pub fn visit_type_reference(ty: &Type, visit: &mut impl FnMut(&NamedTypeReference)) {
+ if let Some(ntr) = ty.get_named_type_reference() {
+ visit(&ntr);
+ }
+ match ty.type_class() {
+ TypeClass::StructureTypeClass => {
+ let structure = ty.get_structure().unwrap();
+ for field in structure.members() {
+ visit_type_reference(&field.ty.contents, visit);
+ }
+ for base in structure.base_structures() {
+ visit(&base.ty);
+ }
+ }
+ TypeClass::PointerTypeClass => {
+ visit_type_reference(&ty.child_type().unwrap().contents, visit);
+ }
+ TypeClass::ArrayTypeClass => {
+ visit_type_reference(&ty.child_type().unwrap().contents, visit);
+ }
+ TypeClass::FunctionTypeClass => {
+ let params = ty.parameters().unwrap();
+ for param in params {
+ visit_type_reference(&param.ty.contents, visit);
+ }
+ visit_type_reference(&ty.return_value().unwrap().contents, visit);
+ }
+ _ => {}
+ }
+}