mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-08 20:09:54 +00:00
Canonicalize read_varint
There is a slight note we only implement u64 varint's, while Monero does it for arbitrary uints, yet that's not relevant at this time. It is documented in #25.
This commit is contained in:
parent
c5beee5648
commit
755d021f8e
1 changed files with 7 additions and 1 deletions
|
@ -76,8 +76,14 @@ pub fn read_varint<R: io::Read>(r: &mut R) -> io::Result<u64> {
|
|||
let mut res = 0;
|
||||
while {
|
||||
let b = read_byte(r)?;
|
||||
if (bits != 0) && (b == 0) {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "non-canonical varint"))?;
|
||||
}
|
||||
if ((bits + 7) > 64) && (b >= (1 << (64 - bits))) {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "varint overflow"))?;
|
||||
}
|
||||
|
||||
res += u64::from(b & (!VARINT_CONTINUATION_MASK)) << bits;
|
||||
// TODO: Error if bits exceed u64
|
||||
bits += 7;
|
||||
b & VARINT_CONTINUATION_MASK == VARINT_CONTINUATION_MASK
|
||||
} {}
|
||||
|
|
Loading…
Reference in a new issue