binwalk/signatures/
bzip2.rs

1use crate::extractors::bzip2::bzip2_decompressor;
2use crate::signatures::common::{SignatureError, SignatureResult, CONFIDENCE_HIGH};
3
4/// Human readable description
5pub const DESCRIPTION: &str = "bzip2 compressed data";
6
7/// Bzip2 magic bytes; includes the magic bytes, version number, block size, and compressed magic signature
8pub fn bzip2_magic() -> Vec<Vec<u8>> {
9    vec![
10        b"BZh91AY&SY".to_vec(),
11        b"BZh81AY&SY".to_vec(),
12        b"BZh71AY&SY".to_vec(),
13        b"BZh61AY&SY".to_vec(),
14        b"BZh51AY&SY".to_vec(),
15        b"BZh41AY&SY".to_vec(),
16        b"BZh31AY&SY".to_vec(),
17        b"BZh21AY&SY".to_vec(),
18        b"BZh11AY&SY".to_vec(),
19    ]
20}
21
22/// Bzip2 header parser
23pub fn bzip2_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
24    // Return value
25    let mut result = SignatureResult {
26        description: DESCRIPTION.to_string(),
27        offset,
28        confidence: CONFIDENCE_HIGH,
29        ..Default::default()
30    };
31
32    let dry_run = bzip2_decompressor(file_data, offset, None);
33
34    if dry_run.success {
35        if let Some(bzip2_size) = dry_run.size {
36            result.size = bzip2_size;
37            result.description =
38                format!("{}, total size: {} bytes", result.description, result.size);
39            return Ok(result);
40        }
41    }
42
43    Err(SignatureError)
44}