summaryrefslogtreecommitdiff
path: root/plugins/bntl_utils/src/command/dump.rs
blob: 7b48900d4e94e6226b32dd8ff929629d0c92667a (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use crate::command::{InputDirectoryField, OutputDirectoryField};
use crate::dump::TILDump;
use crate::helper::path_to_type_libraries;
use binaryninja::background_task::BackgroundTask;
use binaryninja::command::GlobalCommand;
use binaryninja::interaction::Form;

pub struct Dump;

impl Dump {
    pub fn execute() {
        let mut form = Form::new("Dump to C Header");
        // TODO: The choice to select what to include?
        form.add_field(InputDirectoryField::field());
        form.add_field(OutputDirectoryField::field());
        if !form.prompt() {
            return;
        }
        let output_dir = OutputDirectoryField::from_form(&form).unwrap();
        let input_path = InputDirectoryField::from_form(&form).unwrap();

        let bg_task = BackgroundTask::new("Dumping type libraries...", true).enter();

        let type_libraries = path_to_type_libraries(&input_path);
        for type_lib in &type_libraries {
            if bg_task.is_cancelled() {
                return;
            }
            bg_task.set_progress_text(&format!("Dumping '{}'...", type_lib.name()));
            let dump = match TILDump::new()
                .with_type_libs(type_libraries.clone())
                .dump(&type_lib)
            {
                Ok(dump) => dump,
                Err(err) => {
                    tracing::error!("Failed to dump type library: {}", err);
                    return;
                }
            };

            let output_path = output_dir.join(format!("{}.h", type_lib.name()));
            if let Err(e) = std::fs::write(&output_path, dump) {
                tracing::error!("Failed to write dump to {}: {}", output_path.display(), e);
            }
            tracing::info!("Dump written to {}", output_path.display());
        }
    }
}

impl GlobalCommand for Dump {
    fn action(&self) {
        std::thread::spawn(move || {
            Dump::execute();
        });
    }

    fn valid(&self) -> bool {
        true
    }
}