use crate::bytes::{get_u32, get_u64, put_u32, put_u64};
use super::ParseError;
const MODULUS: u64 = 0xFFFF_FFFF;
pub mod kind {
pub const CONTAINER_SUPERBLOCK: u16 = 0x01;
pub const BTREE_ROOT: u16 = 0x02;
pub const BTREE_NODE: u16 = 0x03;
pub const SPACE_MANAGER: u16 = 0x05;
pub const SPACE_MANAGER_CAB: u16 = 0x06;
pub const SPACE_MANAGER_CIB: u16 = 0x07;
pub const SPACE_MANAGER_FREE_QUEUE: u16 = 0x09;
pub const OBJECT_MAP: u16 = 0x0B;
pub const CHECKPOINT_MAP: u16 = 0x0C;
pub const VOLUME_SUPERBLOCK: u16 = 0x0D;
pub const FS_TREE: u16 = 0x0E;
pub const EXTENT_REF_TREE: u16 = 0x0F;
pub const SNAPSHOT_TREE: u16 = 0x10;
pub const REAPER: u16 = 0x11;
pub const REAP_LIST: u16 = 0x12;
pub const OBJECT_MAP_SNAPSHOT: u16 = 0x13;
pub const SNAPSHOT_EXTENSION: u16 = 0x1D;
pub const INTEGRITY_META: u16 = 0x1E;
pub const FILE_EXTENT_TREE: u16 = 0x1F;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Storage {
Virtual,
Ephemeral,
Physical,
Invalid,
}
impl Storage {
#[must_use]
pub const fn type_bits(self) -> u32 {
match self {
Self::Virtual => 0,
Self::Ephemeral => 0x8000_0000,
Self::Physical => 0x4000_0000,
Self::Invalid => 0xC000_0000,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ObjectHeader {
pub checksum: u64,
pub oid: u64,
pub xid: u64,
pub type_word: u32,
pub subtype: u32,
}
impl ObjectHeader {
pub const SIZE: usize = 32;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("object header", Self::SIZE, buf)?;
Ok(Self {
checksum: get_u64(buf, 0),
oid: get_u64(buf, 8),
xid: get_u64(buf, 16),
type_word: get_u32(buf, 24),
subtype: get_u32(buf, 28),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"an object header needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u64(buf, 0, self.checksum);
put_u64(buf, 8, self.oid);
put_u64(buf, 16, self.xid);
put_u32(buf, 24, self.type_word);
put_u32(buf, 28, self.subtype);
}
#[must_use]
pub const fn kind(&self) -> u16 {
(self.type_word & 0xFFFF) as u16
}
#[must_use]
pub const fn storage(&self) -> Storage {
match self.type_word & 0xC000_0000 {
0 => Storage::Virtual,
0x8000_0000 => Storage::Ephemeral,
0x4000_0000 => Storage::Physical,
_ => Storage::Invalid,
}
}
#[must_use]
pub const fn other_flags(&self) -> u32 {
self.type_word & 0x3FFF_0000
}
}
#[must_use]
pub fn checksum(object: &[u8]) -> u64 {
let (mut sum, mut sum_of_sums) = (0u64, 0u64);
for word in object.get(8..).unwrap_or_default().as_chunks::<4>().0 {
let word = u64::from(get_u32(word, 0));
sum += word;
if sum >= MODULUS {
sum -= MODULUS;
}
sum_of_sums += sum;
if sum_of_sums >= MODULUS {
sum_of_sums -= MODULUS;
}
}
let first = MODULUS - (sum + sum_of_sums) % MODULUS;
let second = MODULUS - (sum + first) % MODULUS;
(second << 32) | first
}
#[must_use]
pub fn verify(object: &[u8]) -> bool {
object.len() >= ObjectHeader::SIZE && get_u64(object, 0) == checksum(object)
}
pub fn seal(object: &mut [u8]) {
assert!(
object.len() >= ObjectHeader::SIZE,
"an object needs at least {} bytes and was given {}",
ObjectHeader::SIZE,
object.len()
);
let sum = checksum(object);
put_u64(object, 0, sum);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_header_round_trips() {
let header = ObjectHeader {
checksum: 0x1122_3344_5566_7788,
oid: 0x0400,
xid: 7,
type_word: 0x4000_0002,
subtype: u32::from(kind::OBJECT_MAP),
};
let mut buf = [0u8; ObjectHeader::SIZE];
header.write_to(&mut buf);
assert_eq!(ObjectHeader::read_from(&buf), Ok(header));
assert_eq!(header.kind(), kind::BTREE_ROOT);
assert_eq!(header.storage(), Storage::Physical);
assert_eq!(header.other_flags(), 0);
}
#[test]
fn a_short_buffer_is_too_short_rather_than_a_header() {
assert!(matches!(
ObjectHeader::read_from(&[0u8; 31]),
Err(ParseError::TooShort { need: 32, .. })
));
}
#[test]
fn storage_is_the_top_two_bits_and_both_together_are_invalid() {
let at = |word| ObjectHeader {
checksum: 0,
oid: 0,
xid: 0,
type_word: word,
subtype: 0,
};
assert_eq!(at(0x0000_000D).storage(), Storage::Virtual);
assert_eq!(at(0x8000_0005).storage(), Storage::Ephemeral);
assert_eq!(at(0x4000_0002).storage(), Storage::Physical);
assert_eq!(at(0xC000_0002).storage(), Storage::Invalid);
}
#[test]
fn a_sealed_object_verifies_and_one_changed_byte_does_not() {
let mut block = vec![0u8; 4096];
for (i, b) in block.iter_mut().enumerate().skip(8) {
*b = (i * 7 + 3) as u8;
}
seal(&mut block);
assert!(verify(&block));
block[100] ^= 1;
assert!(!verify(&block));
}
#[test]
fn the_sums_over_a_sealed_object_and_then_its_check_words_come_to_zero() {
let mut block = vec![0u8; 4096];
for (i, b) in block.iter_mut().enumerate().skip(8) {
*b = (i % 251) as u8;
}
seal(&mut block);
let (mut a, mut b) = (0u64, 0u64);
let words = block[8..]
.as_chunks::<4>()
.0
.iter()
.chain(block[..8].as_chunks::<4>().0);
for word in words {
a = (a + u64::from(u32::from_le_bytes([word[0], word[1], word[2], word[3]]))) % MODULUS;
b = (b + a) % MODULUS;
}
assert_eq!((a, b), (0, 0));
}
#[test]
fn a_block_of_zeros_is_not_an_object() {
assert!(!verify(&[0u8; 4096]));
assert_eq!(checksum(&[0u8; 4096]), u64::MAX);
}
#[test]
fn a_check_word_the_sums_bring_to_zero_is_all_ones() {
let mut block = vec![0u8; 4096];
block[8] = 1;
let last = block.len() - 4;
let total = (MODULUS - (checksum(&block) & MODULUS)) % MODULUS;
let half = MODULUS.div_ceil(2);
let d = (MODULUS - total) % MODULUS * half % MODULUS;
put_u32(&mut block, last, d as u32);
seal(&mut block);
assert_eq!(
get_u64(&block, 0) & MODULUS,
MODULUS,
"all ones, never zero"
);
assert!(verify(&block));
put_u32(&mut block, 0, 0);
assert!(!verify(&block));
}
}