summaryrefslogtreecommitdiff
path: root/plugins/bntl_utils/src/diff.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/diff.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/diff.rs')
-rw-r--r--plugins/bntl_utils/src/diff.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/plugins/bntl_utils/src/diff.rs b/plugins/bntl_utils/src/diff.rs
new file mode 100644
index 00000000..18d4dce8
--- /dev/null
+++ b/plugins/bntl_utils/src/diff.rs
@@ -0,0 +1,83 @@
+use crate::dump::TILDump;
+use crate::helper::path_to_type_libraries;
+use binaryninja::types::TypeLibrary;
+use similar::{Algorithm, TextDiff};
+use std::path::{Path, PathBuf};
+use std::time::Duration;
+use thiserror::Error;
+
+#[derive(Error, Debug)]
+pub enum TILDiffError {
+ #[error("Could not determine parent directory for path: {0}")]
+ InvalidPath(PathBuf),
+
+ #[error("Failed to dump type library: {0}")]
+ DumpError(String),
+}
+
+pub struct DiffResult {
+ pub ratio: f32,
+ pub diff: String,
+}
+
+pub struct TILDiff {
+ timeout: Duration,
+}
+
+impl TILDiff {
+ pub fn new() -> Self {
+ Self {
+ timeout: Duration::from_secs(180),
+ }
+ }
+
+ pub fn with_timeout(mut self, timeout: Duration) -> Self {
+ self.timeout = timeout;
+ self
+ }
+
+ pub fn diff(
+ &self,
+ (a_path, a_type_lib): (&Path, &TypeLibrary),
+ (b_path, b_type_lib): (&Path, &TypeLibrary),
+ ) -> Result<DiffResult, TILDiffError> {
+ let a_parent = a_path
+ .parent()
+ .ok_or_else(|| TILDiffError::InvalidPath(a_path.to_path_buf()))?;
+ let b_parent = b_path
+ .parent()
+ .ok_or_else(|| TILDiffError::InvalidPath(b_path.to_path_buf()))?;
+
+ let a_dependencies = path_to_type_libraries(a_parent);
+ let b_dependencies = path_to_type_libraries(b_parent);
+
+ let dumped_a = TILDump::new()
+ .with_type_libs(a_dependencies)
+ .dump(a_type_lib)
+ .map_err(|e| TILDiffError::DumpError(e.to_string()))?;
+
+ let dumped_b = TILDump::new()
+ .with_type_libs(b_dependencies)
+ .dump(b_type_lib)
+ .map_err(|e| TILDiffError::DumpError(e.to_string()))?;
+
+ let diff = TextDiff::configure()
+ .algorithm(Algorithm::Patience)
+ .timeout(self.timeout)
+ .diff_lines(&dumped_a, &dumped_b);
+
+ let diff_content = diff
+ .unified_diff()
+ .context_radius(3)
+ .header(
+ a_path.to_string_lossy().as_ref(),
+ b_path.to_string_lossy().as_ref(),
+ )
+ .to_string();
+
+ Ok(DiffResult {
+ ratio: diff.ratio(),
+ diff: diff_content,
+ })
+ }
+}