threatflux_binary_analysis/formats/
raw.rs

1//! Raw binary format parser
2
3use 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
11/// Raw binary format parser
12pub 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 // Raw parser can handle any data
21    }
22}
23
24/// Raw binary representation
25pub 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        // Create a single section for the entire data
47        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        &[] // No symbols in raw binary
92    }
93
94    fn imports(&self) -> &[Import] {
95        &[] // No imports in raw binary
96    }
97
98    fn exports(&self) -> &[Export] {
99        &[] // No exports in raw binary
100    }
101
102    fn metadata(&self) -> &BinaryMetadata {
103        &self.metadata
104    }
105}