use core::{
convert::Infallible,
marker::PhantomData,
};
use qubit_codec::{
Codec,
DecodeFailure,
};
use crate::{
Leb128DecodeError,
Leb128DecodePolicy,
};
#[inline(always)]
pub(in crate::codec) fn map_leb128_decode_failure(
error: Leb128DecodeError,
) -> DecodeFailure<Leb128DecodeError> {
if let Some(required) = error.required() {
DecodeFailure::incomplete_with_source(error, required)
} else {
let consumed = error.consumed().expect(
"invalid LEB128 decode errors always report consumed bytes",
);
DecodeFailure::invalid(error, consumed)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Leb128Codec<T, P> {
marker: PhantomData<fn() -> (T, P)>,
}
macro_rules! impl_unsigned_leb128_codec {
($ty:ty) => {
impl<P> Leb128Codec<$ty, P>
where
P: Leb128DecodePolicy,
{
pub const MIN_UNITS_PER_VALUE: usize =
<Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize =
<Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize =
<Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[inline(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> Result<($ty, core::num::NonZeroUsize), Leb128DecodeError> {
debug_assert!(
input.len().saturating_sub(input_index)
>= Self::MIN_UNITS_PER_VALUE
);
let (value, consumed) = unsafe {
read_uleb_unchecked::<P>(
input,
input_index,
<$ty>::BITS,
Self::MAX_DECODE_UNITS_PER_VALUE,
)?
};
Ok((value as $ty, consumed))
}
#[must_use = "the returned byte count determines the encoded payload range"]
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
unsafe {
write_uleb_unchecked(output, output_index, value as u128)
}
}
}
impl<P> Codec for Leb128Codec<$ty, P>
where
P: Leb128DecodePolicy,
{
type Value = $ty;
type Unit = u8;
type DecodeError = Leb128DecodeError;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = 1;
const MAX_ENCODE_UNITS_PER_VALUE: usize =
(<$ty>::BITS as usize).div_ceil(7);
const MAX_DECODE_UNITS_PER_VALUE: usize =
(<$ty>::BITS as usize).div_ceil(7);
#[inline(always)]
fn encode_len(&self, value: &$ty) -> usize {
uleb_encoded_len(*value as u128)
}
#[inline(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
debug_assert!(
input.len().saturating_sub(input_index)
>= Self::MIN_UNITS_PER_VALUE
);
unsafe { Self::decode(input, input_index) }
.map_err(map_leb128_decode_failure)
}
#[inline(always)]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
let required = self.encode_len(value);
debug_assert!(
output.len().saturating_sub(output_index) >= required
);
let written =
unsafe { Self::encode(*value, output, output_index) };
Ok(written)
}
}
};
}
macro_rules! impl_signed_leb128_codec {
($ty:ty) => {
impl<P> Leb128Codec<$ty, P>
where
P: Leb128DecodePolicy,
{
pub const MIN_UNITS_PER_VALUE: usize =
<Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize =
<Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize =
<Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[inline(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> Result<($ty, core::num::NonZeroUsize), Leb128DecodeError> {
debug_assert!(
input.len().saturating_sub(input_index)
>= Self::MIN_UNITS_PER_VALUE
);
let (value, consumed) = unsafe {
read_sleb_unchecked::<P>(
input,
input_index,
<$ty>::BITS,
Self::MAX_DECODE_UNITS_PER_VALUE,
)?
};
Ok((value as $ty, consumed))
}
#[must_use = "the returned byte count determines the encoded payload range"]
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
unsafe {
write_sleb_unchecked(output, output_index, value as i128)
}
}
}
impl<P> Codec for Leb128Codec<$ty, P>
where
P: Leb128DecodePolicy,
{
type Value = $ty;
type Unit = u8;
type DecodeError = Leb128DecodeError;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = 1;
const MAX_ENCODE_UNITS_PER_VALUE: usize =
(<$ty>::BITS as usize).div_ceil(7);
const MAX_DECODE_UNITS_PER_VALUE: usize =
(<$ty>::BITS as usize).div_ceil(7);
#[inline(always)]
fn encode_len(&self, value: &$ty) -> usize {
sleb_encoded_len(*value as i128)
}
#[inline(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
debug_assert!(
input.len().saturating_sub(input_index)
>= Self::MIN_UNITS_PER_VALUE
);
unsafe { Self::decode(input, input_index) }
.map_err(map_leb128_decode_failure)
}
#[inline(always)]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
let required = self.encode_len(value);
debug_assert!(
output.len().saturating_sub(output_index) >= required
);
let written =
unsafe { Self::encode(*value, output, output_index) };
Ok(written)
}
}
};
}
impl_unsigned_leb128_codec!(u8);
impl_unsigned_leb128_codec!(u16);
impl_unsigned_leb128_codec!(u32);
impl_unsigned_leb128_codec!(u64);
impl_unsigned_leb128_codec!(u128);
impl_unsigned_leb128_codec!(usize);
impl_signed_leb128_codec!(i8);
impl_signed_leb128_codec!(i16);
impl_signed_leb128_codec!(i32);
impl_signed_leb128_codec!(i64);
impl_signed_leb128_codec!(i128);
impl_signed_leb128_codec!(isize);
#[inline]
unsafe fn read_uleb_unchecked<P>(
input: &[u8],
index: usize,
bits: u32,
max_bytes: usize,
) -> Result<(u128, core::num::NonZeroUsize), Leb128DecodeError>
where
P: Leb128DecodePolicy,
{
debug_assert!(input.len().saturating_sub(index) >= 1);
let available = input.len().saturating_sub(index).min(max_bytes);
match unsafe {
read_uleb_prefix_unchecked::<P>(
input, index, bits, max_bytes, available,
)
} {
Ok(Some((value, consumed))) => {
debug_assert!(consumed > 0);
let consumed = qubit_utils::nonzero(consumed);
Ok((value, consumed))
}
Ok(None) => {
debug_assert!(
available < usize::MAX,
"available byte count overflowed"
);
let required = qubit_utils::nonzero(available + 1);
Err(Leb128DecodeError::incomplete(index, required, available))
}
Err(error) => Err(error),
}
}
#[inline]
unsafe fn read_uleb_prefix_unchecked<P>(
input: &[u8],
index: usize,
bits: u32,
max_bytes: usize,
available: usize,
) -> Result<Option<(u128, usize)>, Leb128DecodeError>
where
P: Leb128DecodePolicy,
{
debug_assert!(
available <= max_bytes,
"available bytes exceed LEB128 maximum width"
);
let mut value = 0u128;
let mut shift = 0u32;
for offset in 0..available {
let byte =
unsafe { qubit_utils::UncheckedSlice::read(input, index + offset) };
let payload = u128::from(byte & 0x7F);
value |= payload << shift;
if byte & 0x80 == 0 {
if offset == max_bytes - 1
&& !unsigned_final_payload_fits(byte, bits, offset)
{
return Err(malformed_decode_error(
index,
index + offset,
offset + 1,
));
}
let consumed = offset + 1;
if P::STRICT && !is_canonical_uleb_terminal(offset, byte) {
return Err(noncanonical_decode_error(index, consumed));
}
return Ok(Some((value, consumed)));
}
shift += 7;
}
if available < max_bytes {
return Ok(None);
}
Err(malformed_decode_error(
index,
index + max_bytes - 1,
max_bytes,
))
}
#[inline]
unsafe fn read_sleb_unchecked<P>(
input: &[u8],
index: usize,
bits: u32,
max_bytes: usize,
) -> Result<(i128, core::num::NonZeroUsize), Leb128DecodeError>
where
P: Leb128DecodePolicy,
{
debug_assert!(input.len().saturating_sub(index) >= 1);
let available = input.len().saturating_sub(index).min(max_bytes);
match unsafe {
read_sleb_prefix_unchecked::<P>(
input, index, bits, max_bytes, available,
)
} {
Ok(Some((value, consumed))) => {
debug_assert!(consumed > 0);
let consumed = qubit_utils::nonzero(consumed);
Ok((value, consumed))
}
Ok(None) => {
debug_assert!(
available < usize::MAX,
"available byte count overflowed"
);
let required = qubit_utils::nonzero(available + 1);
Err(Leb128DecodeError::incomplete(index, required, available))
}
Err(error) => Err(error),
}
}
#[inline]
unsafe fn read_sleb_prefix_unchecked<P>(
input: &[u8],
index: usize,
bits: u32,
max_bytes: usize,
available: usize,
) -> Result<Option<(i128, usize)>, Leb128DecodeError>
where
P: Leb128DecodePolicy,
{
debug_assert!(
available <= max_bytes,
"available bytes exceed LEB128 maximum width"
);
let mut value = 0i128;
let mut shift = 0u32;
let mut previous_byte = 0u8;
for offset in 0..available {
let byte =
unsafe { qubit_utils::UncheckedSlice::read(input, index + offset) };
let payload = i128::from(byte & 0x7F);
value |= payload << shift;
if byte & 0x80 == 0 {
if offset == max_bytes - 1
&& !signed_final_payload_fits(byte, bits, offset)
{
return Err(malformed_decode_error(
index,
index + offset,
offset + 1,
));
}
if byte & 0x40 != 0 && shift + 7 < i128::BITS {
value |= (!0i128) << (shift + 7);
}
let consumed = offset + 1;
if P::STRICT
&& !is_canonical_sleb_terminal(offset, previous_byte, byte)
{
return Err(noncanonical_decode_error(index, consumed));
}
return Ok(Some((value, consumed)));
}
previous_byte = byte;
shift += 7;
}
if available < max_bytes {
return Ok(None);
}
Err(malformed_decode_error(
index,
index + max_bytes - 1,
max_bytes,
))
}
#[cold]
fn malformed_decode_error(
start_index: usize,
error_index: usize,
consumed: usize,
) -> Leb128DecodeError {
debug_assert!(consumed > 0, "malformed LEB128 errors must consume bytes");
let consumed = qubit_utils::nonzero(consumed);
Leb128DecodeError::malformed(start_index, error_index, consumed)
}
#[cold]
fn noncanonical_decode_error(
index: usize,
consumed: usize,
) -> Leb128DecodeError {
debug_assert!(
consumed > 0,
"non-canonical LEB128 errors must consume bytes"
);
let consumed = qubit_utils::nonzero(consumed);
Leb128DecodeError::noncanonical(index, consumed)
}
#[must_use]
#[inline(always)]
fn unsigned_final_payload_fits(byte: u8, bits: u32, offset: usize) -> bool {
let used_bits = bits - offset as u32 * 7;
byte >> used_bits == 0
}
#[must_use]
#[inline(always)]
fn signed_final_payload_fits(byte: u8, bits: u32, offset: usize) -> bool {
let used_bits = bits - offset as u32 * 7;
let payload = byte & 0x7F;
let used_mask = ((1u16 << used_bits) - 1) as u8;
let unused_mask = 0x7F & !used_mask;
let sign_bit = 1u8 << (used_bits - 1);
if payload & sign_bit == 0 {
payload & unused_mask == 0
} else {
payload & unused_mask == unused_mask
}
}
#[must_use]
#[inline(always)]
fn is_canonical_uleb_terminal(offset: usize, byte: u8) -> bool {
offset == 0 || byte & 0x7F != 0
}
#[must_use]
#[inline(always)]
fn is_canonical_sleb_terminal(
offset: usize,
previous_byte: u8,
byte: u8,
) -> bool {
if offset == 0 {
return true;
}
let payload = byte & 0x7F;
!((payload == 0 && previous_byte & 0x40 == 0)
|| (payload == 0x7F && previous_byte & 0x40 != 0))
}
unsafe fn write_uleb_unchecked(
output: &mut [u8],
index: usize,
mut value: u128,
) -> usize {
let mut offset = 0;
loop {
let mut byte = (value & 0x7F) as u8;
value >>= 7;
if value != 0 {
byte |= 0x80;
}
unsafe {
qubit_utils::UncheckedSlice::write(output, index + offset, byte);
}
offset += 1;
if value == 0 {
return offset;
}
}
}
#[must_use]
#[inline(always)]
pub(in crate::codec) fn uleb_encoded_len(mut value: u128) -> usize {
let mut len = 0;
loop {
value >>= 7;
len += 1;
if value == 0 {
return len;
}
}
}
unsafe fn write_sleb_unchecked(
output: &mut [u8],
index: usize,
mut value: i128,
) -> usize {
let mut offset = 0;
loop {
let mut byte = (value & 0x7F) as u8;
let sign_bit_set = byte & 0x40 != 0;
value >>= 7;
let done =
(value == 0 && !sign_bit_set) || (value == -1 && sign_bit_set);
if !done {
byte |= 0x80;
}
unsafe {
qubit_utils::UncheckedSlice::write(output, index + offset, byte);
}
offset += 1;
if done {
return offset;
}
}
}
#[must_use]
#[inline(always)]
fn sleb_encoded_len(mut value: i128) -> usize {
let mut len = 0;
loop {
let byte = (value & 0x7F) as u8;
let sign_bit_set = byte & 0x40 != 0;
value >>= 7;
let done =
(value == 0 && !sign_bit_set) || (value == -1 && sign_bit_set);
len += 1;
if done {
return len;
}
}
}