use super::{ParseError, get_u16, get_u32, join64, put_u16, put_u32};
pub const EXTENT_MAGIC: u16 = 0xf30a;
pub const EXTENT_ENTRY_SIZE: usize = 12;
pub const EXTENT_TAIL_LEN: usize = 4;
const UNINIT_LEN_LIMIT: u16 = 32768;
const UNINIT_MAX_LEN: u16 = UNINIT_LEN_LIMIT - 1;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ExtentHeader {
pub entries: u16,
pub max: u16,
pub depth: u16,
pub generation: u32,
}
impl ExtentHeader {
pub const SIZE: usize = EXTENT_ENTRY_SIZE;
#[must_use]
pub fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut b = [0u8; Self::SIZE];
put_u16(&mut b, 0, EXTENT_MAGIC);
put_u16(&mut b, 2, self.entries);
put_u16(&mut b, 4, self.max);
put_u16(&mut b, 6, self.depth);
put_u32(&mut b, 8, self.generation);
b
}
pub fn from_bytes(b: &[u8; Self::SIZE]) -> Result<Self, ParseError> {
let magic = get_u16(b, 0);
if magic != EXTENT_MAGIC {
return Err(ParseError::BadMagic {
structure: "ExtentHeader",
found: u32::from(magic),
expected: u32::from(EXTENT_MAGIC),
});
}
Ok(Self {
entries: get_u16(b, 2),
max: get_u16(b, 4),
depth: get_u16(b, 6),
generation: get_u32(b, 8),
})
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ExtentIdx {
pub block: u32,
pub leaf: u64,
}
impl ExtentIdx {
pub const SIZE: usize = EXTENT_ENTRY_SIZE;
#[must_use]
pub fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut b = [0u8; Self::SIZE];
put_u32(&mut b, 0, self.block);
put_u32(&mut b, 4, self.leaf as u32);
put_u16(&mut b, 8, (self.leaf >> 32) as u16);
b
}
#[must_use]
pub fn from_bytes(b: &[u8; Self::SIZE]) -> Self {
Self {
block: get_u32(b, 0),
leaf: join64(get_u32(b, 4), u32::from(get_u16(b, 8))),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ExtentLeaf {
pub block: u32,
pub len: u16,
pub start: u64,
pub initialized: bool,
}
impl ExtentLeaf {
pub const SIZE: usize = EXTENT_ENTRY_SIZE;
pub fn to_bytes(&self) -> Result<[u8; Self::SIZE], ParseError> {
let invalid = || ParseError::InvalidField {
structure: "ExtentLeaf",
field: "ee_len",
value: u64::from(self.len),
};
let raw_len = if self.initialized {
if self.len > UNINIT_LEN_LIMIT {
return Err(invalid());
}
self.len
} else {
if self.len == 0 || self.len > UNINIT_MAX_LEN {
return Err(invalid());
}
self.len + UNINIT_LEN_LIMIT
};
let mut b = [0u8; Self::SIZE];
put_u32(&mut b, 0, self.block);
put_u16(&mut b, 4, raw_len);
put_u16(&mut b, 6, (self.start >> 32) as u16);
put_u32(&mut b, 8, self.start as u32);
Ok(b)
}
#[must_use]
pub fn from_bytes(b: &[u8; Self::SIZE]) -> Self {
let raw_len = get_u16(b, 4);
let (len, initialized) = if raw_len > UNINIT_LEN_LIMIT {
(raw_len - UNINIT_LEN_LIMIT, false)
} else {
(raw_len, true)
};
Self {
block: get_u32(b, 0),
len,
start: join64(get_u32(b, 8), u32::from(get_u16(b, 6))),
initialized,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn header_round_trips() {
let h = ExtentHeader {
entries: 1,
max: 4,
depth: 0,
generation: 0,
};
assert_eq!(ExtentHeader::from_bytes(&h.to_bytes()).unwrap(), h);
assert_eq!(&h.to_bytes()[0..2], &[0x0a, 0xf3]);
}
#[test]
fn header_rejects_bad_magic() {
let mut b = ExtentHeader {
entries: 0,
max: 4,
depth: 0,
generation: 0,
}
.to_bytes();
b[0] = 0;
assert!(matches!(
ExtentHeader::from_bytes(&b),
Err(ParseError::BadMagic { .. })
));
}
#[test]
fn idx_round_trips_with_high_bits() {
let e = ExtentIdx {
block: 0x1234,
leaf: 0x1_0000_5678,
};
assert_eq!(ExtentIdx::from_bytes(&e.to_bytes()), e);
}
#[test]
fn leaf_round_trips_initialized() {
let e = ExtentLeaf {
block: 0,
len: 4,
start: 7,
initialized: true,
};
assert_eq!(ExtentLeaf::from_bytes(&e.to_bytes().unwrap()), e);
}
#[test]
fn leaf_round_trips_uninitialized_and_high_start() {
let e = ExtentLeaf {
block: 100,
len: 8,
start: 0x1_0000_0000,
initialized: false,
};
let d = ExtentLeaf::from_bytes(&e.to_bytes().unwrap());
assert_eq!(d, e);
assert_eq!(get_u16(&e.to_bytes().unwrap(), 4), 8 + UNINIT_LEN_LIMIT);
}
#[test]
fn every_encodable_leaf_round_trips_and_the_rest_are_refused() {
let leaf = |len, initialized| ExtentLeaf {
block: 42,
len,
start: 1000,
initialized,
};
for (len, initialized) in [
(0, true), (1, true),
(UNINIT_LEN_LIMIT, true), (1, false),
(UNINIT_MAX_LEN, false), ] {
let e = leaf(len, initialized);
let bytes = e
.to_bytes()
.unwrap_or_else(|err| panic!("{e:?} must encode: {err}"));
assert_eq!(
ExtentLeaf::from_bytes(&bytes),
e,
"{e:?} did not read back as itself"
);
}
for (len, initialized, why) in [
(
0,
false,
"an uninitialized run of no blocks encodes as an initialized 32768-block run",
),
(
UNINIT_LEN_LIMIT,
false,
"an uninitialized run of 32768 blocks overruns the field",
),
(
UNINIT_LEN_LIMIT + 1,
true,
"an initialized run past 32768 blocks encodes as an uninitialized one",
),
] {
let e = leaf(len, initialized);
assert!(
matches!(
e.to_bytes(),
Err(ParseError::InvalidField {
structure: "ExtentLeaf",
field: "ee_len",
value,
}) if value == u64::from(len)
),
"{e:?} must be refused: {why}"
);
}
}
}