use alloc::{borrow::ToOwned, vec::Vec};
use thiserror::Error;
use crate::serialize::binary::Restrict;
pub struct BinDecoder<'a> {
buffer: &'a [u8], remaining: &'a [u8], }
pub(crate) type DecodeResult<T> = Result<T, DecodeError>;
#[derive(Clone, Copy, Debug, Error)]
#[non_exhaustive]
pub enum DecodeError {
#[error("unexpected end of input reached")]
InsufficientBytes,
#[error("the index passed to BinDecoder::slice_from must be greater than the decoder position")]
InvalidPreviousIndex,
#[error("label points to data not prior to idx: {idx} ptr: {ptr}")]
PointerNotPriorToLabel {
idx: usize,
ptr: u16,
},
#[error("label bytes exceed 63: {0}")]
LabelBytesTooLong(usize),
#[error("unrecognized label code: {0:b}")]
UnrecognizedLabelCode(u8),
#[error("name label data exceed 255: {0}")]
DomainNameTooLong(usize),
#[error("overlapping labels name {label} other {other}")]
LabelOverlapsWithOther {
label: usize,
other: usize,
},
}
impl<'a> BinDecoder<'a> {
pub fn new(buffer: &'a [u8]) -> Self {
BinDecoder {
buffer,
remaining: buffer,
}
}
pub fn pop(&mut self) -> DecodeResult<Restrict<u8>> {
if let Some((first, remaining)) = self.remaining.split_first() {
self.remaining = remaining;
return Ok(Restrict::new(*first));
}
Err(DecodeError::InsufficientBytes)
}
pub fn len(&self) -> usize {
self.remaining.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn peek(&self) -> Option<Restrict<u8>> {
Some(Restrict::new(*self.remaining.first()?))
}
pub fn index(&self) -> usize {
self.buffer.len() - self.remaining.len()
}
pub fn clone(&self, index_at: u16) -> Self {
BinDecoder {
buffer: self.buffer,
remaining: &self.buffer[index_at as usize..],
}
}
pub fn read_character_data(&mut self) -> DecodeResult<Restrict<&[u8]>> {
let length = self.pop()?.unverified() as usize;
self.read_slice(length)
}
pub fn read_vec(&mut self, len: usize) -> DecodeResult<Restrict<Vec<u8>>> {
self.read_slice(len).map(|s| s.map(ToOwned::to_owned))
}
pub fn read_slice(&mut self, len: usize) -> DecodeResult<Restrict<&'a [u8]>> {
if len > self.remaining.len() {
return Err(DecodeError::InsufficientBytes);
}
let (read, remaining) = self.remaining.split_at(len);
self.remaining = remaining;
Ok(Restrict::new(read))
}
pub fn slice_from(&self, index: usize) -> DecodeResult<&'a [u8]> {
if index > self.index() {
return Err(DecodeError::InvalidPreviousIndex);
}
Ok(&self.buffer[index..self.index()])
}
pub fn read_u8(&mut self) -> DecodeResult<Restrict<u8>> {
self.pop()
}
pub fn read_u16(&mut self) -> DecodeResult<Restrict<u16>> {
Ok(self
.read_slice(2)?
.map(|s| u16::from_be_bytes([s[0], s[1]])))
}
pub fn read_i32(&mut self) -> DecodeResult<Restrict<i32>> {
Ok(self.read_slice(4)?.map(|s| {
assert!(s.len() == 4);
i32::from_be_bytes([s[0], s[1], s[2], s[3]])
}))
}
pub fn read_u32(&mut self) -> DecodeResult<Restrict<u32>> {
Ok(self.read_slice(4)?.map(|s| {
assert!(s.len() == 4);
u32::from_be_bytes([s[0], s[1], s[2], s[3]])
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_slice() {
let deadbeef = b"deadbeef";
let mut decoder = BinDecoder::new(deadbeef);
let read = decoder.read_slice(4).expect("failed to read dead");
assert_eq!(&read.unverified(), b"dead");
let read = decoder.read_slice(2).expect("failed to read be");
assert_eq!(&read.unverified(), b"be");
let read = decoder.read_slice(0).expect("failed to read nothing");
assert_eq!(&read.unverified(), b"");
assert!(decoder.read_slice(3).is_err());
}
#[test]
fn test_read_slice_from() {
let deadbeef = b"deadbeef";
let mut decoder = BinDecoder::new(deadbeef);
decoder.read_slice(4).expect("failed to read dead");
let read = decoder.slice_from(0).expect("failed to get slice");
assert_eq!(&read, b"dead");
decoder.read_slice(2).expect("failed to read be");
let read = decoder.slice_from(4).expect("failed to get slice");
assert_eq!(&read, b"be");
decoder.read_slice(0).expect("failed to read nothing");
let read = decoder.slice_from(4).expect("failed to get slice");
assert_eq!(&read, b"be");
assert!(decoder.slice_from(10).is_err());
}
}