git_features/
decode.rs

1use std::io::Read;
2
3/// Decode variable int numbers from a `Read` implementation.
4///
5/// Note: currently overflow checks are only done in debug mode.
6#[inline]
7pub fn leb64_from_read(mut r: impl Read) -> Result<(u64, usize), std::io::Error> {
8    let mut b = [0u8; 1];
9    let mut i = 0;
10    r.read_exact(&mut b)?;
11    i += 1;
12    let mut value = b[0] as u64 & 0x7f;
13    while b[0] & 0x80 != 0 {
14        r.read_exact(&mut b)?;
15        i += 1;
16        debug_assert!(i <= 10, "Would overflow value at 11th iteration");
17        value += 1;
18        value = (value << 7) + (b[0] as u64 & 0x7f)
19    }
20    Ok((value, i))
21}
22
23/// Decode variable int numbers.
24#[inline]
25pub fn leb64(d: &[u8]) -> (u64, usize) {
26    let mut i = 0;
27    let mut c = d[i];
28    i += 1;
29    let mut value = c as u64 & 0x7f;
30    while c & 0x80 != 0 {
31        c = d[i];
32        i += 1;
33        debug_assert!(i <= 10, "Would overflow value at 11th iteration");
34        value += 1;
35        value = (value << 7) + (c as u64 & 0x7f)
36    }
37    (value, i)
38}