Skip to main content

threatflux_binary_analysis/formats/
raw.rs

1//! Raw binary format parser
2
3use crate::{
4    BinaryFormatParser, BinaryFormatTrait, Result,
5    types::{
6        Architecture, BinaryFormat as Format, BinaryMetadata, Endianness, Export, Import, Section,
7        SectionPermissions, SectionType, SecurityFeatures, Symbol,
8    },
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    metadata: BinaryMetadata,
27    sections: Vec<Section>,
28}
29
30impl RawBinary {
31    fn new(data: &[u8]) -> Self {
32        let metadata = BinaryMetadata {
33            size: data.len(),
34            format: Format::Raw,
35            architecture: Architecture::Unknown,
36            entry_point: None,
37            base_address: None,
38            timestamp: None,
39            compiler_info: None,
40            endian: Endianness::Unknown,
41            security_features: SecurityFeatures::default(),
42        };
43
44        // Create a single section for the entire data
45        let sections = vec![Section {
46            name: ".data".to_string(),
47            address: 0,
48            size: data.len() as u64,
49            offset: 0,
50            file_size: data.len() as u64,
51            permissions: SectionPermissions {
52                read: true,
53                write: false,
54                execute: false,
55            },
56            section_type: SectionType::Data,
57            data: if data.len() <= 1024 {
58                Some(data.to_vec())
59            } else {
60                None
61            },
62        }];
63
64        Self { metadata, sections }
65    }
66}
67
68impl BinaryFormatTrait for RawBinary {
69    fn format_type(&self) -> Format {
70        Format::Raw
71    }
72
73    fn architecture(&self) -> Architecture {
74        Architecture::Unknown
75    }
76
77    fn entry_point(&self) -> Option<u64> {
78        None
79    }
80
81    fn sections(&self) -> &[Section] {
82        &self.sections
83    }
84
85    fn symbols(&self) -> &[Symbol] {
86        &[] // No symbols in raw binary
87    }
88
89    fn imports(&self) -> &[Import] {
90        &[] // No imports in raw binary
91    }
92
93    fn exports(&self) -> &[Export] {
94        &[] // No exports in raw binary
95    }
96
97    fn metadata(&self) -> &BinaryMetadata {
98        &self.metadata
99    }
100}