summaryrefslogtreecommitdiff
path: root/plugins/bntl_utils/cli/src/diff.rs
blob: 1eedb97013b3e19e658f0b120441df4f03f6d6a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use binaryninja::types::TypeLibrary;
use bntl_utils::diff::TILDiff;
use clap::Args;
use std::path::PathBuf;

#[derive(Debug, Args)]
pub struct DiffArgs {
    pub file_a: PathBuf,
    pub file_b: PathBuf,
    /// Path to write the `.diff` file to.
    pub output_path: PathBuf,
    /// Timeout in seconds for the diff operation to complete, if provided the diffing will begin
    /// to approximate after the deadline has passed.
    #[clap(long)]
    pub timeout: Option<u64>,
}

impl DiffArgs {
    pub fn execute(&self) {
        let type_lib_a =
            TypeLibrary::load_from_file(&self.file_a).expect("Failed to load type library");
        let type_lib_b =
            TypeLibrary::load_from_file(&self.file_b).expect("Failed to load type library");

        let diff_result =
            match TILDiff::new().diff((&self.file_a, &type_lib_a), (&self.file_b, &type_lib_b)) {
                Ok(diff_result) => diff_result,
                Err(err) => {
                    tracing::error!("Failed to diff type libraries: {}", err);
                    return;
                }
            };
        tracing::info!("Similarity Ratio: {}", diff_result.ratio);
        std::fs::write(&self.output_path, diff_result.diff).unwrap();
        tracing::info!("Diff written to: {}", self.output_path.display());
    }
}