use std::borrow::Cow;
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
pub enum BitError {
#[error("read of {width} bit(s) at bit {position} exceeds the {available}-bit buffer")]
OutOfBounds {
position: usize,
width: usize,
available: usize,
},
#[error("integer read of {width} bits exceeds the 64-bit maximum")]
TooWide {
width: u32,
},
}
#[derive(Clone, Copy, Debug)]
pub struct BitCursor<'a> {
data: &'a [u8],
position: usize,
}
impl<'a> BitCursor<'a> {
#[must_use]
pub const fn new(data: &'a [u8]) -> Self {
Self { data, position: 0 }
}
#[must_use]
pub const fn data(&self) -> &'a [u8] {
self.data
}
#[must_use]
pub const fn position(&self) -> usize {
self.position
}
#[must_use]
pub const fn len_bits(&self) -> usize {
self.data.len() * 8
}
#[must_use]
pub const fn remaining(&self) -> usize {
self.len_bits().saturating_sub(self.position)
}
pub const fn seek(&mut self, position: usize) {
self.position = position;
}
pub fn skip(&mut self, bits: usize) -> Result<(), BitError> {
let end = self.position.saturating_add(bits);
if end > self.len_bits() {
return Err(BitError::OutOfBounds {
position: self.position,
width: bits,
available: self.len_bits(),
});
}
self.position = end;
Ok(())
}
#[inline]
pub fn read_uint(&mut self, width: u32) -> Result<u64, BitError> {
let value = self.peek_uint(self.position, width)?;
self.position += width as usize;
Ok(value)
}
#[inline]
pub fn peek_uint(&self, position: usize, width: u32) -> Result<u64, BitError> {
if width > 64 {
return Err(BitError::TooWide { width });
}
if width == 0 {
return Ok(0);
}
let end = position.saturating_add(width as usize);
if end > self.len_bits() {
return Err(BitError::OutOfBounds {
position,
width: width as usize,
available: self.len_bits(),
});
}
let byte = position / 8;
let bit_in_byte = (position % 8) as u32;
let window = if let Some(slice) = self.data.get(byte..byte + 16) {
let mut buf = [0u8; 16];
buf.copy_from_slice(slice);
u128::from_be_bytes(buf)
} else {
let mut buf = [0u8; 16];
let tail = self.data.get(byte..).unwrap_or_default();
let take = tail.len().min(16);
if let (Some(dst), Some(src)) = (buf.get_mut(..take), tail.get(..take)) {
dst.copy_from_slice(src);
}
u128::from_be_bytes(buf)
};
let shift = 128 - u32::from(bit_in_byte as u8) - width;
let mask = if width == 64 {
u64::MAX
} else {
(1u64 << width) - 1
};
Ok(((window >> shift) as u64) & mask)
}
#[inline]
pub fn peek_next(&self, width: u32) -> Result<u64, BitError> {
self.peek_uint(self.position, width)
}
pub fn read_bytes(&mut self, width: usize) -> Result<Cow<'a, [u8]>, BitError> {
let out_len = width.div_ceil(8);
let pad = out_len * 8 - width;
if let Some(slice) = self.aligned_slice(width) {
return Ok(Cow::Borrowed(slice));
}
let start = self.position;
self.bounds_check(width)?;
let mut out = vec![0u8; out_len];
for (index, slot) in out.iter_mut().enumerate() {
let (from, take) = match (index * 8).checked_sub(pad) {
Some(offset) => (start + offset, 8),
None => (start, 8 - pad as u32),
};
*slot = self.peek_uint(from, take)? as u8;
}
self.position = start + width;
Ok(Cow::Owned(out))
}
pub fn read_bytes_left_aligned(&mut self, width: usize) -> Result<Cow<'a, [u8]>, BitError> {
if let Some(slice) = self.aligned_slice(width) {
return Ok(Cow::Borrowed(slice));
}
let start = self.position;
self.bounds_check(width)?;
let out_len = width.div_ceil(8);
let mut out = vec![0u8; out_len];
for (index, slot) in out.iter_mut().enumerate() {
let offset = index * 8;
let take = u32::try_from(width - offset).unwrap_or(8).min(8);
*slot = (self.peek_uint(start + offset, take)? as u8) << (8 - take);
}
self.position = start + width;
Ok(Cow::Owned(out))
}
fn aligned_slice(&mut self, width: usize) -> Option<&'a [u8]> {
if self.position % 8 != 0 || width % 8 != 0 {
return None;
}
let start = self.position / 8;
let end = start.checked_add(width / 8)?;
let slice = self.data.get(start..end)?;
self.position += width;
Some(slice)
}
fn bounds_check(&self, width: usize) -> Result<(), BitError> {
let end = self.position.saturating_add(width);
if end > self.len_bits() {
return Err(BitError::OutOfBounds {
position: self.position,
width,
available: self.len_bits(),
});
}
Ok(())
}
}
#[must_use]
pub fn twos_complement(value: u64, width: u32) -> i64 {
if width == 0 || width > 64 {
return value as i64;
}
let shift = 64 - width;
((value << shift) as i64) >> shift
}
#[must_use]
pub fn twos_complement_unmasked(value: u64, width: u32) -> Option<i64> {
if width == 0 || width >= 64 {
return Some(value as i64);
}
if value & (1u64 << (width - 1)) == 0 {
return i64::try_from(value).ok();
}
i64::try_from(i128::from(value) - (1i128 << width)).ok()
}
#[must_use]
pub fn sign_magnitude(value: u64, width: u32) -> i64 {
if width == 0 || width > 64 {
return value as i64;
}
let sign_bit = 1u64 << (width - 1);
let magnitude = (value & (sign_bit - 1)) as i64;
if value & sign_bit == 0 {
magnitude
} else {
-magnitude
}
}
#[must_use]
pub fn ones_complement(value: u64, width: u32) -> i64 {
if width == 0 || width > 64 {
return value as i64;
}
let sign_bit = 1u64 << (width - 1);
if value & sign_bit == 0 {
value as i64
} else {
let mask = if width == 64 {
u64::MAX
} else {
(1u64 << width) - 1
};
-(((!value) & mask) as i64)
}
}
#[must_use]
pub fn swap_byte_order(value: u64, width: u32) -> u64 {
let bytes = (width as usize).div_ceil(8);
if bytes <= 1 {
return value;
}
let mut out = 0u64;
let mut remaining = value;
for _ in 0..bytes {
out = (out << 8) | (remaining & 0xFF);
remaining >>= 8;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn naive_read(data: &[u8], position: usize, width: u32) -> Option<u64> {
if position + width as usize > data.len() * 8 {
return None;
}
let mut value: u64 = 0;
for offset in 0..width as usize {
let bit_index = position + offset;
let byte = *data.get(bit_index / 8)?;
let bit = (byte >> (7 - (bit_index % 8))) & 1;
value = (value << 1) | u64::from(bit);
}
Some(value)
}
#[test]
fn documented_example_matches() {
let data = [0b0011_0101u8, 0b1100_1010];
let mut cursor = BitCursor::new(&data);
cursor.seek(2);
assert_eq!(cursor.read_uint(9), Ok(0b1_1010_1110));
assert_eq!(cursor.position(), 11);
}
#[test]
fn sixty_four_bits_at_a_bit_offset_spans_nine_bytes() {
let data: [u8; 9] = [0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF];
for offset in 0..8 {
let mut cursor = BitCursor::new(&data);
cursor.seek(offset);
let got = cursor.read_uint(64).expect("in range");
let want = naive_read(&data, offset, 64).expect("in range");
assert_eq!(got, want, "offset {offset}");
}
}
#[test]
fn reads_past_the_end_are_errors_not_panics() {
let data = [0xAAu8; 2];
let mut cursor = BitCursor::new(&data);
cursor.seek(9);
assert!(matches!(
cursor.read_uint(8),
Err(BitError::OutOfBounds { .. })
));
assert!(matches!(
cursor.read_uint(65),
Err(BitError::TooWide { .. })
));
let mut empty = BitCursor::new(&[]);
assert!(matches!(
empty.read_uint(1),
Err(BitError::OutOfBounds { .. })
));
assert_eq!(empty.read_uint(0), Ok(0));
}
#[test]
fn aligned_byte_reads_borrow() {
let data = [1u8, 2, 3, 4];
let mut cursor = BitCursor::new(&data);
let bytes = cursor.read_bytes(16).expect("in range");
assert!(matches!(bytes, Cow::Borrowed(_)));
assert_eq!(&*bytes, &[1, 2]);
assert_eq!(cursor.position(), 16);
}
#[test]
fn unaligned_bytes_are_right_aligned() {
let data = [0xABu8, 0xCD];
let mut cursor = BitCursor::new(&data);
cursor.seek(4);
let bytes = cursor.read_bytes(12).expect("in range");
assert_eq!(&*bytes, &[0x0B, 0xCD]);
}
#[test]
fn left_aligned_bytes_pad_on_the_right() {
let data = [0xABu8, 0xCD];
let mut cursor = BitCursor::new(&data);
cursor.seek(4);
let bytes = cursor.read_bytes_left_aligned(12).expect("in range");
assert_eq!(&*bytes, &[0xBC, 0xD0]);
}
fn naive_twos_complement(value: u64, width: u32) -> i128 {
let modulus = 1i128 << width;
let value = i128::from(value) % modulus;
if value >= modulus / 2 {
value - modulus
} else {
value
}
}
#[test]
fn signed_interpretations() {
assert_eq!(twos_complement(0xFF, 8), -1);
assert_eq!(twos_complement(0x80, 8), -128);
assert_eq!(twos_complement(0x7F, 8), 127);
assert_eq!(twos_complement(u64::MAX, 64), -1);
assert_eq!(twos_complement(1 << 62, 63), -(1i64 << 62));
assert_eq!(twos_complement((1 << 62) - 1, 63), (1i64 << 62) - 1);
assert_eq!(twos_complement(u64::MAX >> 1, 63), -1);
assert_eq!(twos_complement(1, 1), -1);
assert_eq!(twos_complement(0, 1), 0);
assert_eq!(sign_magnitude(0b1000_0001, 8), -1);
assert_eq!(sign_magnitude(0b0000_0001, 8), 1);
assert_eq!(ones_complement(0b1111_1110, 8), -1);
assert_eq!(ones_complement(0b0000_0001, 8), 1);
}
#[test]
fn byte_order_swap() {
assert_eq!(swap_byte_order(0x1234, 16), 0x3412);
assert_eq!(swap_byte_order(0x12345678, 32), 0x78563412);
assert_eq!(swap_byte_order(0x12, 8), 0x12);
}
proptest::proptest! {
#[test]
fn matches_naive_reference(
data in proptest::collection::vec(proptest::num::u8::ANY, 1..24),
offset_seed in 0usize..200,
width in 0u32..=64,
) {
let total_bits = data.len() * 8;
let offset = offset_seed % (total_bits + 1);
let mut cursor = BitCursor::new(&data);
cursor.seek(offset);
let got = cursor.read_uint(width);
match naive_read(&data, offset, width) {
Some(want) => {
proptest::prop_assert_eq!(got, Ok(want));
proptest::prop_assert_eq!(cursor.position(), offset + width as usize);
}
None => proptest::prop_assert!(got.is_err()),
}
}
#[test]
fn wide_reads_at_every_bit_offset(
data in proptest::collection::vec(proptest::num::u8::ANY, 9..17),
offset in 0usize..8,
width in 57u32..=64,
) {
let mut cursor = BitCursor::new(&data);
cursor.seek(offset);
let want = naive_read(&data, offset, width).expect("buffer is long enough");
proptest::prop_assert_eq!(cursor.read_uint(width), Ok(want));
}
#[test]
fn bytes_and_integers_agree(
data in proptest::collection::vec(proptest::num::u8::ANY, 1..20),
offset_seed in 0usize..200,
width in 1usize..=64,
) {
let total_bits = data.len() * 8;
let offset = offset_seed % (total_bits + 1);
if offset + width > total_bits {
return Ok(());
}
let mut as_int = BitCursor::new(&data);
as_int.seek(offset);
let integer = as_int.read_uint(width as u32).expect("in range");
let mut as_bytes = BitCursor::new(&data);
as_bytes.seek(offset);
let bytes = as_bytes.read_bytes(width).expect("in range");
let mut rebuilt = 0u64;
for byte in bytes.iter() {
rebuilt = (rebuilt << 8) | u64::from(*byte);
}
proptest::prop_assert_eq!(rebuilt, integer);
proptest::prop_assert_eq!(as_int.position(), as_bytes.position());
}
#[test]
fn twos_complement_matches_naive_reference(
bits in proptest::num::u64::ANY,
width in 1u32..=64,
) {
let mask = if width == 64 { u64::MAX } else { (1u64 << width) - 1 };
let value = bits & mask;
proptest::prop_assert_eq!(
i128::from(twos_complement(value, width)),
naive_twos_complement(value, width),
"value {} width {}", value, width
);
}
#[test]
fn unmasked_sign_extension_agrees_within_the_field(
bits in proptest::num::u64::ANY,
width in 1u32..=64,
) {
let mask = if width == 64 { u64::MAX } else { (1u64 << width) - 1 };
let value = bits & mask;
proptest::prop_assert_eq!(
twos_complement_unmasked(value, width),
Some(twos_complement(value, width)),
"value {} width {}", value, width
);
}
#[test]
fn unmasked_sign_extension_keeps_the_high_bits(
bits in proptest::num::u64::ANY,
width in 1u32..=56,
) {
let expected = if bits & (1u64 << (width - 1)) == 0 {
i128::from(bits)
} else {
i128::from(bits) - (1i128 << width)
};
proptest::prop_assert_eq!(
twos_complement_unmasked(bits, width).map(i128::from),
i64::try_from(expected).ok().map(i128::from),
"value {} width {}", bits, width
);
}
#[test]
fn failures_leave_the_cursor_untouched(
data in proptest::collection::vec(proptest::num::u8::ANY, 0..8),
offset in 0usize..200,
width in 0u32..=80,
) {
let mut cursor = BitCursor::new(&data);
cursor.seek(offset);
if cursor.read_uint(width).is_err() {
proptest::prop_assert_eq!(cursor.position(), offset);
}
}
}
}