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:
Luke Parker 2022-08-21 08:58:28 -04:00
parent c5beee5648
commit 755d021f8e
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6

View file

@ -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
} {}