use std::io::{Read, Write};
use std::num::TryFromIntError;
use crate::Variable;
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct Unsigned(pub(crate) u128);
impl Unsigned {
pub(crate) fn encode_be_bytes<W: Write, const N: usize>(
mut value: [u8; N],
mut output: W,
) -> std::io::Result<usize> {
if N == 16 && value[0] >> 4 != 0 {
return Err(std::io::Error::from(std::io::ErrorKind::InvalidData));
}
let (total_length, extra_bytes) = value
.iter()
.enumerate()
.find_map(|(index, &byte)| {
if byte > 0 {
let extra_bytes = N - 1 - index;
if byte < 16 {
Some((extra_bytes + 1, extra_bytes))
} else {
Some((extra_bytes + 2, extra_bytes + 1))
}
} else {
None
}
})
.unwrap_or((0, 0));
let total_length = total_length.max(1);
let extra_bytes_encoded = (extra_bytes as u8) << 4;
if total_length > N {
output.write_all(&[extra_bytes_encoded])?;
output.write_all(&value)?;
} else {
value[N - total_length] |= extra_bytes_encoded;
output.write_all(&value[N - total_length..])?;
}
Ok(total_length)
}
pub(crate) fn decode_variable_bytes<R: Read, const N: usize>(
mut input: R,
) -> std::io::Result<[u8; N]> {
let mut buffer = [0_u8; N];
input.read_exact(&mut buffer[0..1])?;
let first_byte = buffer[0];
let length = (first_byte >> 4) as usize;
if length > N {
return Err(std::io::Error::from(std::io::ErrorKind::InvalidData));
}
input.read_exact(&mut buffer[N - length..])?;
match N - length {
0 => {
buffer[0] |= first_byte & 0b1111;
}
1 => {
buffer[0] &= 0b1111;
}
_ => {
buffer[N - 1 - length] |= first_byte & 0b1111;
buffer[0] = 0;
}
}
Ok(buffer)
}
}
impl Variable for Unsigned {
fn encode_variable<W: Write>(&self, output: W) -> std::io::Result<usize> {
Self::encode_be_bytes(self.0.to_be_bytes(), output)
}
fn decode_variable<R: Read>(input: R) -> std::io::Result<Self> {
let buffer = Self::decode_variable_bytes(input)?;
Ok(Self(u128::from_be_bytes(buffer)))
}
}
macro_rules! impl_primitive_from_varint {
($ty:ty) => {
impl TryFrom<Unsigned> for $ty {
type Error = TryFromIntError;
fn try_from(value: Unsigned) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
};
}
macro_rules! impl_varint_from_primitive {
($ty:ty, $dest:ty) => {
impl From<$ty> for Unsigned {
fn from(value: $ty) -> Self {
Self(<$dest>::from(value))
}
}
};
}
impl_varint_from_primitive!(u8, u128);
impl_varint_from_primitive!(u16, u128);
impl_varint_from_primitive!(u32, u128);
impl_varint_from_primitive!(u64, u128);
impl_varint_from_primitive!(u128, u128);
impl_primitive_from_varint!(u8);
impl_primitive_from_varint!(u16);
impl_primitive_from_varint!(u32);
impl_primitive_from_varint!(u64);
impl_primitive_from_varint!(usize);
impl From<Unsigned> for u128 {
fn from(value: Unsigned) -> Self {
value.0
}
}
impl From<usize> for Unsigned {
fn from(value: usize) -> Self {
Self(value as u128)
}
}