use crate::btree::read_btree_extents;
use crate::error::XfsError;
use crate::inode::{Inode, InodeFormat};
use crate::superblock::Superblock;
const STARTOFF_BITS: u32 = 54;
const STARTBLOCK_HI_BITS: u32 = 9;
const STARTBLOCK_LO_BITS: u32 = 43;
const BLOCKCOUNT_BITS: u32 = 21;
#[inline]
const fn low_mask(n: u32) -> u64 {
if n >= 64 {
u64::MAX } else {
(1u64 << n) - 1
}
}
fn size_error(need: usize, have: usize) -> XfsError {
XfsError::Truncated {
structure: "extent-list file (size exceeds image)",
need,
have,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BmbtRec {
pub startoff: u64,
pub startblock: u64,
pub blockcount: u64,
pub unwritten: bool,
}
impl BmbtRec {
#[must_use]
pub fn unpack(raw: &[u8; 16]) -> Self {
let l0 = u64::from_be_bytes([
raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6], raw[7],
]);
let l1 = u64::from_be_bytes([
raw[8], raw[9], raw[10], raw[11], raw[12], raw[13], raw[14], raw[15],
]);
let unwritten = (l0 >> 63) & 1 == 1;
let startoff = (l0 >> STARTBLOCK_HI_BITS) & low_mask(STARTOFF_BITS);
let startblock_hi = l0 & low_mask(STARTBLOCK_HI_BITS);
let startblock_lo = (l1 >> BLOCKCOUNT_BITS) & low_mask(STARTBLOCK_LO_BITS);
let startblock = (startblock_hi << STARTBLOCK_LO_BITS) | startblock_lo;
let blockcount = l1 & low_mask(BLOCKCOUNT_BITS);
Self {
startoff,
startblock,
blockcount,
unwritten,
}
}
}
#[must_use]
pub fn read_extents(fork: &[u8], nextents: u32) -> Vec<BmbtRec> {
let mut recs = Vec::new();
for i in 0..nextents as usize {
let start = i * 16;
let Some(chunk) = fork.get(start..start + 16) else {
break;
};
let mut raw = [0u8; 16];
raw.copy_from_slice(chunk);
recs.push(BmbtRec::unpack(&raw));
}
recs
}
pub fn read_file_from_fork(
image: &[u8],
sb: &Superblock,
fork: &[u8],
nextents: u32,
size: u64,
) -> Result<Vec<u8>, XfsError> {
let recs = read_extents(fork, nextents);
assemble_extents(image, sb, &recs, size)
}
pub fn assemble_extents(
image: &[u8],
sb: &Superblock,
recs: &[BmbtRec],
size: u64,
) -> Result<Vec<u8>, XfsError> {
let size = match usize::try_from(size) {
Ok(s) => s,
Err(_) => return Err(size_error(usize::MAX, image.len())), };
if size > image.len() {
return Err(size_error(size, image.len()));
}
let blocksize = sb.blocksize as usize;
let mut out = vec![0u8; size];
for rec in recs {
let dst_start = (rec.startoff as usize).saturating_mul(blocksize);
let ext_bytes = (rec.blockcount as usize).saturating_mul(blocksize);
let src_start = (rec.startblock as usize).saturating_mul(blocksize);
let dst_end = dst_start.saturating_add(ext_bytes).min(size);
if dst_end <= dst_start {
continue; }
let want = dst_end - dst_start;
let src_end = src_start.saturating_add(want);
let Some(src) = image.get(src_start..src_end) else {
continue;
};
let Some(dst) = out.get_mut(dst_start..dst_end) else {
continue; };
dst.copy_from_slice(src);
}
Ok(out)
}
impl Superblock {
pub fn read_file(&self, image: &[u8], inode: &Inode) -> Result<Vec<u8>, XfsError> {
match inode.format {
InodeFormat::Extents => {
read_file_from_fork(image, self, &inode.data_fork, inode.nextents, inode.size)
}
InodeFormat::Btree => {
let recs = read_btree_extents(image, self, &inode.data_fork)?;
assemble_extents(image, self, &recs, inode.size)
}
_ => assemble_extents(image, self, &[], inode.size),
}
}
}