mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
monero: match varint decoding (#513)
* monero: match varint decoding * Fix build and clippy
This commit is contained in:
@@ -12,11 +12,21 @@ use curve25519_dalek::{
|
|||||||
const VARINT_CONTINUATION_MASK: u8 = 0b1000_0000;
|
const VARINT_CONTINUATION_MASK: u8 = 0b1000_0000;
|
||||||
|
|
||||||
mod sealed {
|
mod sealed {
|
||||||
pub trait VarInt: TryInto<u64> + TryFrom<u64> + Copy {}
|
pub trait VarInt: TryInto<u64> + TryFrom<u64> + Copy {
|
||||||
impl VarInt for u8 {}
|
const BITS: usize;
|
||||||
impl VarInt for u32 {}
|
}
|
||||||
impl VarInt for u64 {}
|
impl VarInt for u8 {
|
||||||
impl VarInt for usize {}
|
const BITS: usize = 8;
|
||||||
|
}
|
||||||
|
impl VarInt for u32 {
|
||||||
|
const BITS: usize = 32;
|
||||||
|
}
|
||||||
|
impl VarInt for u64 {
|
||||||
|
const BITS: usize = 64;
|
||||||
|
}
|
||||||
|
impl VarInt for usize {
|
||||||
|
const BITS: usize = core::mem::size_of::<usize>() * 8;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will panic if the VarInt exceeds u64::MAX
|
// This will panic if the VarInt exceeds u64::MAX
|
||||||
@@ -102,7 +112,7 @@ pub(crate) fn read_varint<R: Read, U: sealed::VarInt>(r: &mut R) -> io::Result<U
|
|||||||
if (bits != 0) && (b == 0) {
|
if (bits != 0) && (b == 0) {
|
||||||
Err(io::Error::other("non-canonical varint"))?;
|
Err(io::Error::other("non-canonical varint"))?;
|
||||||
}
|
}
|
||||||
if ((bits + 7) > 64) && (b >= (1 << (64 - bits))) {
|
if ((bits + 7) >= U::BITS) && (b >= (1 << (U::BITS - bits))) {
|
||||||
Err(io::Error::other("varint overflow"))?;
|
Err(io::Error::other("varint overflow"))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user