#[inline]
#[cfg(any(feature = "ext", feature = "fat", feature = "exfat", feature = "btrfs"))]
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]
#[cfg(all(feature = "lzbitmap", any(feature = "btrfs", feature = "apfs")))]
pub(crate) fn get_u24(buf: &[u8], off: usize) -> u32 {
u32::from_le_bytes([buf[off], buf[off + 1], buf[off + 2], 0])
}
#[inline]
#[cfg(any(
feature = "ext",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
))]
pub(crate) fn get_u64(buf: &[u8], off: usize) -> u64 {
u64::from_le_bytes([
buf[off],
buf[off + 1],
buf[off + 2],
buf[off + 3],
buf[off + 4],
buf[off + 5],
buf[off + 6],
buf[off + 7],
])
}
#[inline]
#[cfg(any(feature = "ext", feature = "apfs", feature = "btrfs"))]
pub(crate) fn get_u32_be(buf: &[u8], off: usize) -> u32 {
u32::from_be_bytes([buf[off], buf[off + 1], buf[off + 2], buf[off + 3]])
}
#[inline]
#[cfg(feature = "ext")]
pub(crate) fn get_u64_be(buf: &[u8], off: usize) -> u64 {
u64::from_be_bytes(get_arr(buf, off))
}
#[inline]
#[cfg(any(
feature = "ext",
feature = "fat",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
))]
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]
#[cfg(any(feature = "ext", feature = "fat", feature = "exfat", feature = "btrfs"))]
pub(crate) fn put_u8(buf: &mut [u8], off: usize, v: u8) {
buf[off] = v;
}
#[inline]
#[cfg(any(
feature = "ext",
feature = "fat",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
))]
pub(crate) fn put_u16(buf: &mut [u8], off: usize, v: u16) {
buf[off..off + 2].copy_from_slice(&v.to_le_bytes());
}
#[inline]
#[cfg(any(
feature = "ext",
feature = "fat",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
))]
pub(crate) fn put_u32(buf: &mut [u8], off: usize, v: u32) {
buf[off..off + 4].copy_from_slice(&v.to_le_bytes());
}
#[inline]
#[cfg(any(
feature = "ext",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
))]
pub(crate) fn put_u64(buf: &mut [u8], off: usize, v: u64) {
buf[off..off + 8].copy_from_slice(&v.to_le_bytes());
}
#[inline]
#[cfg(any(feature = "ext", feature = "btrfs", all(feature = "apfs", test)))]
pub(crate) fn put_u32_be(buf: &mut [u8], off: usize, v: u32) {
buf[off..off + 4].copy_from_slice(&v.to_be_bytes());
}
#[inline]
#[cfg(any(
feature = "ext",
feature = "fat",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
))]
pub(crate) fn put_arr(buf: &mut [u8], off: usize, v: &[u8]) {
buf[off..off + v.len()].copy_from_slice(v);
}
#[cfg(all(
test,
any(
feature = "ext",
feature = "fat",
feature = "exfat",
feature = "btrfs",
feature = "apfs"
)
))]
mod tests {
use super::*;
#[test]
fn scalar_round_trips() {
let mut buf = [0u8; 16];
put_u16(&mut buf, 1, 0x3456);
put_u32(&mut buf, 3, 0x789a_bcde);
assert_eq!(get_u16(&buf, 1), 0x3456);
assert_eq!(get_u32(&buf, 3), 0x789a_bcde);
assert_eq!(&buf[3..7], &[0xde, 0xbc, 0x9a, 0x78]);
}
#[cfg(any(feature = "ext", feature = "fat", feature = "exfat", feature = "btrfs"))]
#[test]
fn a_byte_round_trips() {
let mut buf = [0u8; 2];
put_u8(&mut buf, 1, 0x12);
assert_eq!(get_u8(&buf, 1), 0x12);
assert_eq!(buf, [0, 0x12]);
}
#[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]
#[cfg(any(feature = "exfat", feature = "btrfs", feature = "apfs"))]
fn the_sixty_four_bit_pair_round_trips_and_writes_the_low_byte_first() {
let mut buf = [0u8; 16];
put_u64(&mut buf, 3, 0x0123_4567_89ab_cdef);
assert_eq!(
&buf[3..11],
&[0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01]
);
assert_eq!(get_u64(&buf, 3), 0x0123_4567_89ab_cdef);
put_u64(&mut buf, 0, u64::MAX);
assert_eq!(get_u64(&buf, 0), u64::MAX);
}
#[test]
#[cfg(feature = "ext")]
fn the_big_endian_pair_writes_the_other_order() {
let mut buf = [0u8; 8];
put_u32_be(&mut buf, 1, 0x789a_bcde);
assert_eq!(&buf[1..5], &[0x78, 0x9a, 0xbc, 0xde]);
assert_eq!(get_u32_be(&buf, 1), 0x789a_bcde);
put_u32(&mut buf, 1, 0x789a_bcde);
assert_eq!(get_u32_be(&buf, 1), 0xdebc_9a78);
}
#[test]
#[cfg(feature = "ext")]
fn the_big_endian_sixty_four_bit_read_takes_the_high_byte_first() {
let buf = [0xff, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
assert_eq!(get_u64_be(&buf, 1), 0x0123_4567_89ab_cdef);
}
}