Skip to main content

pcf_debug/plugin/
raw.rs

1//! The fallback decoder: works for any partition, used for `TYPE_RAW` blobs and
2//! anything no other decoder claims.
3
4use super::{Decoded, FieldNode, FieldValue, PartitionDecoder, PartitionMeta};
5
6/// How many leading bytes to surface as a preview in the field tree. The full
7/// hexdump is the job of the hexdump renderer, not this decoder.
8const PREVIEW_BYTES: usize = 32;
9
10pub struct RawDecoder;
11
12impl PartitionDecoder for RawDecoder {
13    fn name(&self) -> &'static str {
14        "raw"
15    }
16
17    fn matches(&self, _meta: &PartitionMeta, _data: &[u8]) -> bool {
18        // The unconditional final fallback.
19        true
20    }
21
22    fn decode(&self, _meta: &PartitionMeta, data: &[u8]) -> Decoded {
23        let printable = data.iter().filter(|&&b| (0x20..0x7f).contains(&b)).count();
24        let ratio = if data.is_empty() {
25            0.0
26        } else {
27            printable as f64 / data.len() as f64
28        };
29
30        let mut fields = vec![
31            FieldNode::leaf(
32                "size",
33                FieldValue::U64(data.len() as u64),
34                (0, data.len() as u64),
35            ),
36            FieldNode::leaf(
37                "printable_ascii_ratio",
38                FieldValue::Text(format!("{:.0}%", ratio * 100.0)),
39                (0, data.len() as u64),
40            ),
41        ];
42
43        let preview_len = data.len().min(PREVIEW_BYTES);
44        fields.push(FieldNode::leaf(
45            "preview",
46            FieldValue::Bytes(data[..preview_len].to_vec()),
47            (0, preview_len as u64),
48        ));
49
50        Decoded {
51            format_name: "RAW".into(),
52            fields,
53            warnings: Vec::new(),
54        }
55    }
56}