2020-12-06 17:30:55 -08:00
|
|
|
#![macro_use]
|
|
|
|
|
|
|
|
macro_rules! max_leb128_len {
|
|
|
|
($int_ty:ty) => {
|
|
|
|
// The longest LEB128 encoding for an integer uses 7 bits per byte.
|
|
|
|
(std::mem::size_of::<$int_ty>() * 8 + 6) / 7
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the longest LEB128 encoding of all supported integer types.
|
|
|
|
pub const fn max_leb128_len() -> usize {
|
|
|
|
max_leb128_len!(u128)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
macro_rules! impl_write_unsigned_leb128 {
|
2020-12-06 17:30:55 -08:00
|
|
|
($fn_name:ident, $int_ty:ty) => {
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2020-12-06 17:30:55 -08:00
|
|
|
pub fn $fn_name(
|
|
|
|
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len!($int_ty)],
|
|
|
|
mut value: $int_ty,
|
|
|
|
) -> &[u8] {
|
|
|
|
let mut i = 0;
|
|
|
|
|
2020-02-10 18:40:02 +11:00
|
|
|
loop {
|
|
|
|
if value < 0x80 {
|
2020-12-06 17:30:55 -08:00
|
|
|
unsafe {
|
|
|
|
*out.get_unchecked_mut(i).as_mut_ptr() = value as u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
2018-01-09 16:53:35 +01:00
|
|
|
break;
|
2020-02-10 18:40:02 +11:00
|
|
|
} else {
|
2020-12-06 17:30:55 -08:00
|
|
|
unsafe {
|
|
|
|
*out.get_unchecked_mut(i).as_mut_ptr() = ((value & 0x7f) | 0x80) as u8;
|
|
|
|
}
|
|
|
|
|
2020-02-10 18:40:02 +11:00
|
|
|
value >>= 7;
|
2020-12-06 17:30:55 -08:00
|
|
|
i += 1;
|
2018-01-09 16:53:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-06 17:30:55 -08:00
|
|
|
|
|
|
|
unsafe { ::std::mem::MaybeUninit::slice_assume_init_ref(&out.get_unchecked(..i)) }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
};
|
2016-12-14 01:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
impl_write_unsigned_leb128!(write_u16_leb128, u16);
|
|
|
|
impl_write_unsigned_leb128!(write_u32_leb128, u32);
|
|
|
|
impl_write_unsigned_leb128!(write_u64_leb128, u64);
|
|
|
|
impl_write_unsigned_leb128!(write_u128_leb128, u128);
|
|
|
|
impl_write_unsigned_leb128!(write_usize_leb128, usize);
|
|
|
|
|
|
|
|
macro_rules! impl_read_unsigned_leb128 {
|
2020-12-06 17:30:55 -08:00
|
|
|
($fn_name:ident, $int_ty:ty) => {
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
|
2020-02-10 18:40:02 +11:00
|
|
|
let mut result = 0;
|
2018-01-09 16:53:35 +01:00
|
|
|
let mut shift = 0;
|
|
|
|
let mut position = 0;
|
2020-02-10 18:40:02 +11:00
|
|
|
loop {
|
|
|
|
let byte = slice[position];
|
2018-01-09 16:53:35 +01:00
|
|
|
position += 1;
|
|
|
|
if (byte & 0x80) == 0 {
|
2020-02-10 18:40:02 +11:00
|
|
|
result |= (byte as $int_ty) << shift;
|
|
|
|
return (result, position);
|
|
|
|
} else {
|
|
|
|
result |= ((byte & 0x7F) as $int_ty) << shift;
|
2018-01-09 16:53:35 +01:00
|
|
|
}
|
|
|
|
shift += 7;
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
impl_read_unsigned_leb128!(read_u16_leb128, u16);
|
|
|
|
impl_read_unsigned_leb128!(read_u32_leb128, u32);
|
|
|
|
impl_read_unsigned_leb128!(read_u64_leb128, u64);
|
|
|
|
impl_read_unsigned_leb128!(read_u128_leb128, u128);
|
|
|
|
impl_read_unsigned_leb128!(read_usize_leb128, usize);
|
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
macro_rules! impl_write_signed_leb128 {
|
|
|
|
($fn_name:ident, $int_ty:ty) => {
|
|
|
|
#[inline]
|
|
|
|
pub fn $fn_name(
|
|
|
|
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len!($int_ty)],
|
|
|
|
mut value: $int_ty,
|
|
|
|
) -> &[u8] {
|
|
|
|
let mut i = 0;
|
2016-08-24 15:58:40 +03:00
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
loop {
|
|
|
|
let mut byte = (value as u8) & 0x7f;
|
|
|
|
value >>= 7;
|
|
|
|
let more = !(((value == 0) && ((byte & 0x40) == 0))
|
|
|
|
|| ((value == -1) && ((byte & 0x40) != 0)));
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
if more {
|
|
|
|
byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
|
|
}
|
2016-08-24 15:58:40 +03:00
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
unsafe {
|
|
|
|
*out.get_unchecked_mut(i).as_mut_ptr() = byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
if !more {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { ::std::mem::MaybeUninit::slice_assume_init_ref(&out.get_unchecked(..i)) }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2020-12-06 17:30:55 -08:00
|
|
|
};
|
2016-12-14 01:45:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
impl_write_signed_leb128!(write_i16_leb128, i16);
|
|
|
|
impl_write_signed_leb128!(write_i32_leb128, i32);
|
|
|
|
impl_write_signed_leb128!(write_i64_leb128, i64);
|
|
|
|
impl_write_signed_leb128!(write_i128_leb128, i128);
|
|
|
|
impl_write_signed_leb128!(write_isize_leb128, isize);
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2016-08-23 03:56:52 +03:00
|
|
|
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
|
2016-08-24 15:58:40 +03:00
|
|
|
let mut result = 0;
|
|
|
|
let mut shift = 0;
|
|
|
|
let mut position = start_position;
|
|
|
|
let mut byte;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
byte = data[position];
|
|
|
|
position += 1;
|
2019-06-26 14:04:37 +02:00
|
|
|
result |= i128::from(byte & 0x7F) << shift;
|
2016-08-24 15:58:40 +03:00
|
|
|
shift += 7;
|
|
|
|
|
|
|
|
if (byte & 0x80) == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift < 64) && ((byte & 0x40) != 0) {
|
|
|
|
// sign extend
|
|
|
|
result |= -(1 << shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
(result, position - start_position)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|