-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathcreate.rs
More file actions
276 lines (244 loc) · 10.2 KB
/
create.rs
File metadata and controls
276 lines (244 loc) · 10.2 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::command::{InputDirectoryField, OutputDirectoryField};
use crate::process::{new_processing_state_background_thread, TypeLibProcessor};
use crate::validate::TypeLibValidater;
use binaryninja::background_task::BackgroundTask;
use binaryninja::binary_view::BinaryView;
use binaryninja::command::{Command, GlobalCommand, ProjectCommand};
use binaryninja::interaction::{Form, FormInputField, MessageBoxButtonSet, MessageBoxIcon};
use binaryninja::platform::Platform;
use binaryninja::project::Project;
use std::thread;
pub struct CreateFromCurrentView;
impl Command for CreateFromCurrentView {
fn action(&self, view: &BinaryView) {
let mut form = Form::new("Create From View");
// TODO: The choice to select what types to include
form.add_field(OutputDirectoryField::field());
if !form.prompt() {
return;
}
let output_dir = OutputDirectoryField::from_form(&form).unwrap();
let Some(default_platform) = view.default_platform() else {
tracing::error!("No default platform set for view");
return;
};
let file_path = view.file().file_path();
let file_name = file_path.file_name().unwrap_or_default().to_string_lossy();
let processor = TypeLibProcessor::new(&file_name, &default_platform.name());
let data = match processor.process_view(file_path, view) {
Ok(data) => data,
Err(err) => {
tracing::error!("Failed to process view: {}", err);
return;
}
}
.prune();
let attached_libraries = view
.type_libraries()
.iter()
.map(|t| t.to_owned())
.chain(data.type_libraries.iter().map(|t| t.to_owned()))
.collect::<Vec<_>>();
let mut validator = TypeLibValidater::new()
.with_platform(&default_platform)
.with_type_libraries(attached_libraries);
for type_library in data.type_libraries {
let output_path = output_dir.join(format!("{}.bntl", type_library.name()));
let validation_result = validator.validate(&type_library);
if !validation_result.issues.is_empty() {
tracing::error!(
"Found {} issues in type library '{}'",
validation_result.issues.len(),
type_library.name()
);
match validation_result.render_report() {
Ok(rendered) => {
view.show_html_report(&type_library.name(), &rendered, "");
if let Err(e) = std::fs::write(output_path.with_extension("html"), rendered)
{
tracing::error!(
"Failed to write validation report to {}: {}",
output_path.display(),
e
);
}
}
Err(err) => tracing::error!("Failed to render validation report: {}", err),
}
}
if type_library.write_to_file(&output_path) {
tracing::info!(
"Created type library '{}': {}",
type_library.name(),
output_path.display()
);
} else {
tracing::error!("Failed to write type library to {}", output_path.display());
}
}
}
fn valid(&self, _view: &BinaryView) -> bool {
true
}
}
pub struct NameField;
impl NameField {
pub fn field() -> FormInputField {
FormInputField::TextLine {
prompt: "Dependency Name".to_string(),
default: Some("foo.dll".to_string()),
value: None,
}
}
pub fn from_form(form: &Form) -> Option<String> {
let field = form.get_field_with_name("Dependency Name")?;
field.try_value_string()
}
}
pub struct PlatformField;
impl PlatformField {
pub fn field() -> FormInputField {
FormInputField::TextLine {
prompt: "Platform Name".to_string(),
default: Some("windows-x86_64".to_string()),
value: None,
}
}
pub fn from_form(form: &Form) -> Option<String> {
let field = form.get_field_with_name("Platform Name")?;
field.try_value_string()
}
}
pub struct CreateFromDirectory;
impl CreateFromDirectory {
pub fn execute() {
let mut form = Form::new("Create From Directory");
// TODO: The choice to select what types to include
form.add_field(InputDirectoryField::field());
form.add_field(PlatformField::field());
form.add_field(NameField::field());
form.add_field(OutputDirectoryField::field());
if !form.prompt() {
return;
}
let input_dir = InputDirectoryField::from_form(&form).unwrap();
let platform_name = PlatformField::from_form(&form).unwrap();
let default_name = NameField::from_form(&form).unwrap();
let output_dir = OutputDirectoryField::from_form(&form).unwrap();
let Some(default_platform) = Platform::by_name(&platform_name) else {
tracing::error!("Invalid platform name: {}", platform_name);
return;
};
let processor = TypeLibProcessor::new(&default_name, &default_platform.name());
let background_task = BackgroundTask::new("Processing started...", true);
new_processing_state_background_thread(background_task.clone(), processor.state());
let data = processor.process_directory(&input_dir);
background_task.finish();
let pruned_data = match data {
// Prune off empty type libraries, no need to save them.
Ok(data) => data.prune(),
Err(err) => {
binaryninja::interaction::show_message_box(
"Failed to process directory",
&err.to_string(),
MessageBoxButtonSet::OKButtonSet,
MessageBoxIcon::ErrorIcon,
);
tracing::error!("Failed to process directory: {}", err);
return;
}
};
for type_library in pruned_data.type_libraries {
// Place the type libraries in a folder with the architecture name, as that is necessary
// information for the user to correctly place the following type libraries in the user directory.
let arch_output_path = output_dir.join(type_library.arch().name());
let _ = std::fs::create_dir_all(&arch_output_path);
let output_path = arch_output_path.join(format!("{}.bntl", type_library.name()));
if type_library.write_to_file(&output_path) {
tracing::info!(
"Created type library '{}': {}",
type_library.name(),
output_path.display()
);
} else {
tracing::error!("Failed to write type library to {}", output_path.display());
}
}
}
}
impl GlobalCommand for CreateFromDirectory {
fn action(&self) {
thread::spawn(move || {
CreateFromDirectory::execute();
});
}
fn valid(&self) -> bool {
true
}
}
pub struct CreateFromProject;
impl CreateFromProject {
pub fn execute(project: &Project) {
let mut form = Form::new("Create From Project");
// TODO: The choice to select what types to include
form.add_field(PlatformField::field());
form.add_field(NameField::field());
form.add_field(OutputDirectoryField::field());
if !form.prompt() {
return;
}
let platform_name = PlatformField::from_form(&form).unwrap();
let default_name = NameField::from_form(&form).unwrap();
let output_dir = OutputDirectoryField::from_form(&form).unwrap();
let Some(default_platform) = Platform::by_name(&platform_name) else {
tracing::error!("Invalid platform name: {}", platform_name);
return;
};
let processor = TypeLibProcessor::new(&default_name, &default_platform.name());
let background_task = BackgroundTask::new("Processing started...", true);
new_processing_state_background_thread(background_task.clone(), processor.state());
let data = processor.process_project(project);
background_task.finish();
let finalized_data = match data {
// Prune off empty type libraries, no need to save them.
Ok(data) => data.finalized(&default_name),
Err(err) => {
binaryninja::interaction::show_message_box(
"Failed to process project",
&err.to_string(),
MessageBoxButtonSet::OKButtonSet,
MessageBoxIcon::ErrorIcon,
);
tracing::error!("Failed to process project: {}", err);
return;
}
};
for type_library in finalized_data.type_libraries {
// Place the type libraries in a folder with the architecture name, as that is necessary
// information for the user to correctly place the following type libraries in the user directory.
let arch_output_path = output_dir.join(type_library.arch().name());
let _ = std::fs::create_dir_all(&arch_output_path);
let output_path = arch_output_path.join(format!("{}.bntl", type_library.name()));
if type_library.write_to_file(&output_path) {
tracing::info!(
"Created type library '{}': {}",
type_library.name(),
output_path.display()
);
} else {
tracing::error!("Failed to write type library to {}", output_path.display());
}
}
}
}
impl ProjectCommand for CreateFromProject {
fn action(&self, project: &Project) {
let owned_project = project.to_owned();
thread::spawn(move || {
CreateFromProject::execute(&owned_project);
});
}
fn valid(&self, _project: &Project) -> bool {
true
}
}