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
use std::collections::HashMap;
use std::io::Read;
use memory::repr::FlatMemoryRepr;
use memory::repr::process::ModuleData;
use std::fs::OpenOptions;
use std::path::Path;
pub mod hex;
pub enum FileRepr {
IntelHEX(HashMap<u8, FlatMemoryRepr>),
Executable(ModuleData),
Flat(FlatMemoryRepr)
}
pub fn load_from_path(path: &Path) -> Result<FileRepr, String> {
let mut file = OpenOptions::new().read(true).open(path).unwrap();
let metadata = match file.metadata() {
Ok(metadata) => metadata,
Err(e) => { return Err(e.to_string()); }
};
let mut bytes = vec![0; metadata.len() as usize];
match file.read(&mut bytes) {
Ok(_) => { },
Err(e) => { return Err(e.to_string()); }
};
match hex::from_hex(&bytes) {
Ok(repr) => Ok(FileRepr::IntelHEX(repr)),
Err(e) => {
println!("{}", e);
match ModuleData::load_from(&bytes, format!("{}", path.display())) {
Some(data) => { Ok(FileRepr::Executable(data)) }
None => {
Ok(FileRepr::Flat({
let mut repr = FlatMemoryRepr::of(bytes);
if let Some(x) = path.to_str() {
repr.name = x.to_owned();
};
repr
}))
}
}
}
}
}