mod dirent;
mod extent;
mod group_desc;
mod htree;
mod inode;
mod orphan;
mod superblock;
mod xattr;
pub use dirent::{
DIR_TAIL_LEN, DirEntry, FileType, min_rec_len, rec_len_from_disk, rec_len_to_disk,
write_dir_tail,
};
pub use extent::{
EXTENT_ENTRY_SIZE, EXTENT_MAGIC, EXTENT_TAIL_LEN, ExtentHeader, ExtentIdx, ExtentLeaf,
};
pub use group_desc::{BG_BLOCK_UNINIT, BG_INODE_UNINIT, BG_INODE_ZEROED, GroupDescriptor};
pub use htree::{
DX_CHECKSUM_OFFSET, DX_ENTRY_LEN, DX_HASH_CONTINUED, DX_MAX_INDIRECT_LEVELS,
DX_NODE_COUNT_OFFSET, DX_ROOT_COUNT_OFFSET, DX_TAIL_LEN, DxEntry, dx_limit, dx_tail_offset,
read_dx_countlimit, read_dx_entries, read_dx_root_info, write_dx_entries, write_dx_node_header,
write_dx_root_header, write_dx_tail,
};
pub use inode::{
GOOD_OLD_FIRST_INODE, GOOD_OLD_SIZE as GOOD_OLD_INODE_SIZE, Inode, InodeFlags, ROOT_INODE_MODE,
Timestamp, extra_isize_for,
};
pub(crate) use inode::{decode_device, encode_device};
pub use orphan::{
ORPHAN_BLOCK_MAGIC, ORPHAN_TAIL_LEN, orphan_entries_len, orphan_tail_bytes, read_orphan_tail,
};
pub use superblock::{SUPERBLOCK_MAGIC, SuperBlock};
pub use xattr::Xattr;
pub(crate) use xattr::{
block_len as xattr_block_len, encode_block, encode_inline, has_empty_name, longest_stored_name,
parse_block, parse_inline, split_for_storage,
};
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ParseError {
#[error("{structure}: need {need} bytes, got {got}")]
#[non_exhaustive]
TooShort {
structure: &'static str,
need: usize,
got: usize,
},
#[error("{structure}: bad magic {found:#06x}, expected {expected:#06x}")]
#[non_exhaustive]
BadMagic {
structure: &'static str,
found: u32,
expected: u32,
},
#[error("{structure}: field {field} has invalid value {value}")]
#[non_exhaustive]
InvalidField {
structure: &'static str,
field: &'static str,
value: u64,
},
}
#[inline]
pub(crate) fn get_u8(buf: &[u8], off: usize) -> u8 {
buf[off]
}
#[inline]
pub(crate) fn get_u16(buf: &[u8], off: usize) -> u16 {
u16::from_le_bytes([buf[off], buf[off + 1]])
}
#[inline]
pub(crate) fn get_u32(buf: &[u8], off: usize) -> u32 {
u32::from_le_bytes([buf[off], buf[off + 1], buf[off + 2], buf[off + 3]])
}
#[inline]
pub(crate) fn get_arr<const N: usize>(buf: &[u8], off: usize) -> [u8; N] {
let mut out = [0u8; N];
out.copy_from_slice(&buf[off..off + N]);
out
}
#[inline]
pub(crate) fn put_u8(buf: &mut [u8], off: usize, v: u8) {
buf[off] = v;
}
#[inline]
pub(crate) fn put_u16(buf: &mut [u8], off: usize, v: u16) {
buf[off..off + 2].copy_from_slice(&v.to_le_bytes());
}
#[inline]
pub(crate) fn put_u32(buf: &mut [u8], off: usize, v: u32) {
buf[off..off + 4].copy_from_slice(&v.to_le_bytes());
}
#[inline]
pub(crate) fn put_arr(buf: &mut [u8], off: usize, v: &[u8]) {
buf[off..off + v.len()].copy_from_slice(v);
}
#[inline]
pub(crate) fn split64(v: u64) -> (u32, u32) {
(v as u32, (v >> 32) as u32)
}
#[inline]
pub(crate) fn join64(lo: u32, hi: u32) -> u64 {
(u64::from(hi) << 32) | u64::from(lo)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scalar_round_trips() {
let mut buf = [0u8; 16];
put_u8(&mut buf, 0, 0x12);
put_u16(&mut buf, 1, 0x3456);
put_u32(&mut buf, 3, 0x789a_bcde);
assert_eq!(get_u8(&buf, 0), 0x12);
assert_eq!(get_u16(&buf, 1), 0x3456);
assert_eq!(get_u32(&buf, 3), 0x789a_bcde);
assert_eq!(&buf[3..7], &[0xde, 0xbc, 0x9a, 0x78]);
}
#[test]
fn array_round_trips() {
let mut buf = [0u8; 8];
put_arr(&mut buf, 2, &[1, 2, 3, 4]);
assert_eq!(get_arr::<4>(&buf, 2), [1, 2, 3, 4]);
}
#[test]
fn split_and_join_are_inverse() {
let v = 0x1234_5678_9abc_def0u64;
let (lo, hi) = split64(v);
assert_eq!(lo, 0x9abc_def0);
assert_eq!(hi, 0x1234_5678);
assert_eq!(join64(lo, hi), v);
}
}