threatflux_binary_analysis/formats/
raw.rs1use crate::{
4 types::{
5 Architecture, BinaryFormat as Format, BinaryMetadata, Endianness, Export, Import, Section,
6 SectionPermissions, SectionType, SecurityFeatures, Symbol,
7 },
8 BinaryFormatParser, BinaryFormatTrait, Result,
9};
10
11pub struct RawParser;
13
14impl BinaryFormatParser for RawParser {
15 fn parse(data: &[u8]) -> Result<Box<dyn BinaryFormatTrait>> {
16 Ok(Box::new(RawBinary::new(data)))
17 }
18
19 fn can_parse(_data: &[u8]) -> bool {
20 true }
22}
23
24pub struct RawBinary {
26 #[allow(dead_code)]
27 data: Vec<u8>,
28 metadata: BinaryMetadata,
29 sections: Vec<Section>,
30}
31
32impl RawBinary {
33 fn new(data: &[u8]) -> Self {
34 let metadata = BinaryMetadata {
35 size: data.len(),
36 format: Format::Raw,
37 architecture: Architecture::Unknown,
38 entry_point: None,
39 base_address: Some(0),
40 timestamp: None,
41 compiler_info: None,
42 endian: Endianness::Little,
43 security_features: SecurityFeatures::default(),
44 };
45
46 let sections = vec![Section {
48 name: ".data".to_string(),
49 address: 0,
50 size: data.len() as u64,
51 offset: 0,
52 permissions: SectionPermissions {
53 read: true,
54 write: false,
55 execute: false,
56 },
57 section_type: SectionType::Data,
58 data: if data.len() <= 1024 {
59 Some(data.to_vec())
60 } else {
61 None
62 },
63 }];
64
65 Self {
66 data: data.to_vec(),
67 metadata,
68 sections,
69 }
70 }
71}
72
73impl BinaryFormatTrait for RawBinary {
74 fn format_type(&self) -> Format {
75 Format::Raw
76 }
77
78 fn architecture(&self) -> Architecture {
79 Architecture::Unknown
80 }
81
82 fn entry_point(&self) -> Option<u64> {
83 None
84 }
85
86 fn sections(&self) -> &[Section] {
87 &self.sections
88 }
89
90 fn symbols(&self) -> &[Symbol] {
91 &[] }
93
94 fn imports(&self) -> &[Import] {
95 &[] }
97
98 fn exports(&self) -> &[Export] {
99 &[] }
101
102 fn metadata(&self) -> &BinaryMetadata {
103 &self.metadata
104 }
105}