1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::io::{Read, Write};
use crate::error::{BinverseResult, RenameSymbol};
pub const MAX_LEN: usize = 10;
pub fn read<R: Read>(r: &mut R) -> BinverseResult<u64> {
let mut x: u64 = 0;
let mut s = 0;
let mut b = [0_u8; 1];
for i in 0..MAX_LEN {
r.read_exact(&mut b)?;
let b = b[0];
if b < 0x80 {
if i == MAX_LEN-1 && b > 1 {
return Err(RenameSymbol::VarIntOverflow)
}
return Ok(x | (b as u64) << s)
}
x |= ((b&0x7f) as u64) << s;
s += 7;
}
Err(RenameSymbol::VarIntOverflow)
}
pub fn write<W: Write>(mut x: u64, w: &mut W) -> Result<(), RenameSymbol> {
while x >= 0x80 {
w.write_all(&[x as u8 | 0x80])?;
x >>= 7;
}
w.write_all(&[x as u8])?;
Ok(())
}