1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use strum::{Display, EnumDiscriminants, EnumString};
use thiserror::Error;
mod xml;
#[derive(Copy, Clone, Debug, PartialEq, Eq, EnumString, Display)]
#[strum(serialize_all = "lowercase")]
#[non_exhaustive]
pub enum HashType {
    Sha256,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, EnumDiscriminants)]
#[non_exhaustive]
pub enum HashValue {
    Sha256([u8; 32]),
}
impl HashValue {
    pub fn to_type(&self) -> HashType {
        match self {
            HashValue::Sha256(_) => HashType::Sha256,
        }
    }
    pub fn as_slice(&self) -> &[u8] {
        match self {
            HashValue::Sha256(v) => v,
        }
    }
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlockRange {
    offset: u64,
    length: u64,
    checksum: HashValue,
}
impl BlockRange {
    pub fn checksum(&self) -> HashValue {
        self.checksum
    }
    pub fn offset(&self) -> u64 {
        self.offset
    }
    pub fn length(&self) -> u64 {
        self.length
    }
}
#[derive(Clone, Debug)]
pub struct Bmap {
    image_size: u64,
    block_size: u64,
    blocks: u64,
    mapped_blocks: u64,
    checksum_type: HashType,
    blockmap: Vec<BlockRange>,
}
impl Bmap {
    pub fn builder() -> BmapBuilder {
        BmapBuilder::default()
    }
    pub fn from_xml(xml: &str) -> Result<Self, xml::XmlError> {
        xml::from_xml(xml)
    }
    pub fn image_size(&self) -> u64 {
        self.image_size
    }
    pub const fn block_size(&self) -> u64 {
        self.block_size
    }
    pub fn blocks(&self) -> u64 {
        self.blocks
    }
    pub fn mapped_blocks(&self) -> u64 {
        self.mapped_blocks
    }
    pub fn checksum_type(&self) -> HashType {
        self.checksum_type
    }
    pub fn block_map(&self) -> impl ExactSizeIterator + Iterator<Item = &BlockRange> {
        self.blockmap.iter()
    }
    pub fn total_mapped_size(&self) -> u64 {
        self.block_size * self.mapped_blocks
    }
}
#[derive(Clone, Debug, Error)]
pub enum BmapBuilderError {
    #[error("Image size missing")]
    MissingImageSize,
    #[error("Block size missing")]
    MissingBlockSize,
    #[error("Blocks missing")]
    MissingBlocks,
    #[error("Mapped blocks missing")]
    MissingMappedBlocks,
    #[error("Checksum type missing")]
    MissingChecksumType,
    #[error("No block ranges")]
    NoBlockRanges,
}
#[derive(Clone, Debug, Default)]
pub struct BmapBuilder {
    image_size: Option<u64>,
    block_size: Option<u64>,
    blocks: Option<u64>,
    checksum_type: Option<HashType>,
    mapped_blocks: Option<u64>,
    blockmap: Vec<BlockRange>,
}
impl BmapBuilder {
    pub fn image_size(&mut self, size: u64) -> &mut Self {
        self.image_size = Some(size);
        self
    }
    pub fn block_size(&mut self, block_size: u64) -> &mut Self {
        self.block_size = Some(block_size);
        self
    }
    pub fn blocks(&mut self, blocks: u64) -> &mut Self {
        self.blocks = Some(blocks);
        self
    }
    pub fn mapped_blocks(&mut self, blocks: u64) -> &mut Self {
        self.mapped_blocks = Some(blocks);
        self
    }
    pub fn checksum_type(&mut self, checksum_type: HashType) -> &mut Self {
        self.checksum_type = Some(checksum_type);
        self
    }
    pub fn add_block_range(&mut self, start: u64, end: u64, checksum: HashValue) -> &mut Self {
        let bs = self.block_size.expect("Blocksize needs to be set first");
        let total = self.image_size.expect("Image size needs to be set first");
        let offset = start * bs;
        let length = (total - offset).min((end - start + 1) * bs);
        self.add_byte_range(offset, length, checksum)
    }
    pub fn add_byte_range(&mut self, offset: u64, length: u64, checksum: HashValue) -> &mut Self {
        let range = BlockRange {
            offset,
            length,
            checksum,
        };
        self.blockmap.push(range);
        self
    }
    pub fn build(self) -> Result<Bmap, BmapBuilderError> {
        let image_size = self.image_size.ok_or(BmapBuilderError::MissingImageSize)?;
        let block_size = self.block_size.ok_or(BmapBuilderError::MissingBlockSize)?;
        let blocks = self.blocks.ok_or(BmapBuilderError::MissingBlocks)?;
        let mapped_blocks = self
            .mapped_blocks
            .ok_or(BmapBuilderError::MissingMappedBlocks)?;
        let checksum_type = self
            .checksum_type
            .ok_or(BmapBuilderError::MissingChecksumType)?;
        let blockmap = self.blockmap;
        Ok(Bmap {
            image_size,
            block_size,
            blocks,
            mapped_blocks,
            checksum_type,
            blockmap,
        })
    }
}
#[cfg(test)]
mod test {
    use super::*;
    use std::str::FromStr;
    #[test]
    fn hashes() {
        assert_eq!("sha256", &HashType::Sha256.to_string());
        assert_eq!(HashType::Sha256, HashType::from_str("sha256").unwrap());
        let h = HashValue::Sha256([0; 32]);
        assert_eq!(HashType::Sha256, h.to_type());
    }
}