Skip to content

Commit 8f3f756

Browse files
committed
WIP: IDB Import refactor
1 parent bcb9451 commit 8f3f756

File tree

11 files changed

+1552
-463
lines changed

11 files changed

+1552
-463
lines changed

Cargo.lock

Lines changed: 55 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/idb_import/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ crate-type = ["cdylib"]
1313
anyhow = { version = "1.0.86", features = ["backtrace"] }
1414
binaryninja.workspace = true
1515
binaryninjacore-sys.workspace = true
16-
idb-rs = { git = "https://github.com/Vector35/idb-rs", tag = "0.1.12" }
17-
tracing = "0.1"
16+
idb-rs = { git = "https://github.com/Vector35/idb-rs", tag = "0.1.13" }
17+
tracing = "0.1"
18+
serde_json = "1.0"
19+
serde = { version = "1.0", features = ["derive"] }

plugins/idb_import/src/addr_info.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

plugins/idb_import/src/commands.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use binaryninja::interaction::{Form, FormInputField};
2+
use std::path::PathBuf;
3+
4+
pub mod create_til;
5+
pub mod load_file;
6+
7+
pub struct LoadFileField {
8+
filter: String,
9+
default: Option<String>,
10+
}
11+
12+
impl LoadFileField {
13+
#[allow(unused)]
14+
pub fn new(filter: &str) -> Self {
15+
Self {
16+
filter: filter.to_string(),
17+
default: None,
18+
}
19+
}
20+
21+
pub fn with_default(filter: &str, default: &str) -> Self {
22+
Self {
23+
filter: filter.to_string(),
24+
default: Some(default.to_string()),
25+
}
26+
}
27+
28+
pub fn field(&self) -> FormInputField {
29+
FormInputField::OpenFileName {
30+
prompt: "File Path".to_string(),
31+
// TODO: This is called extension but is really a filter.
32+
extension: Some(self.filter.clone()),
33+
default: self.default.clone(),
34+
value: None,
35+
}
36+
}
37+
38+
pub fn from_form(form: &Form) -> Option<PathBuf> {
39+
let field = form.get_field_with_name("File Path")?;
40+
let field_value = field.try_value_string()?;
41+
Some(PathBuf::from(field_value))
42+
}
43+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::commands::LoadFileField;
2+
use crate::mapper::IDBMapper;
3+
use crate::parse::IDBFileParser;
4+
use binaryninja::binary_view::{BinaryView, BinaryViewExt};
5+
use binaryninja::command::Command;
6+
use binaryninja::interaction::Form;
7+
use std::fs::File;
8+
use std::io::BufReader;
9+
use std::path::PathBuf;
10+
11+
pub struct LoadIDBFile;
12+
13+
impl Command for LoadIDBFile {
14+
fn action(&self, view: &BinaryView) {
15+
let mut form = Form::new("Load SVD File");
16+
// let mut load_settings = LoadSettings::from_view_settings(view);
17+
let mut default_path = PathBuf::from(&view.file().filename());
18+
default_path.set_extension("idb");
19+
let file_field =
20+
LoadFileField::with_default("*.idb;;*.i64;;*.til", &default_path.to_string_lossy());
21+
form.add_field(file_field.field());
22+
if !form.prompt() {
23+
return;
24+
}
25+
let Some(file_path) = LoadFileField::from_form(&form) else {
26+
return;
27+
};
28+
let Ok(file) = File::open(&file_path) else {
29+
tracing::error!("Failed to open file: {}", file_path.display());
30+
return;
31+
};
32+
let mut file_reader = BufReader::new(file);
33+
let file_parser = IDBFileParser::new();
34+
match file_parser.parse(&mut file_reader) {
35+
Ok(idb_info) => {
36+
let idb_str = serde_json::to_string_pretty(&idb_info).unwrap();
37+
std::fs::write(&file_path.with_extension("json"), idb_str)
38+
.expect("Failed to write IDB info to JSON file");
39+
IDBMapper::new(idb_info).map_to_view(view);
40+
}
41+
Err(e) => {
42+
tracing::error!("Failed to parse IDB file: {}", e);
43+
}
44+
}
45+
}
46+
47+
fn valid(&self, _view: &BinaryView) -> bool {
48+
true
49+
}
50+
}

0 commit comments

Comments
 (0)