diff --git a/src/file.rs b/src/file.rs index f44fd30..b9784db 100644 --- a/src/file.rs +++ b/src/file.rs @@ -236,6 +236,21 @@ impl<'a> FileReader { self.read(&mut buf); u128::from_be_bytes(buf) } + + pub fn read_vint(&mut self) -> u128 { + // as defined in the RAR 5.0 spec + let mut result = 0; + let mut shift = 0u16; + loop { + let byte = self.read_u8(); + result |= ((byte & 0x7F) as u128) << shift; + if byte & 0x80 == 0 { + break; + } + shift += 7; + } + result + } } impl Clone for FileReader { @@ -368,4 +383,20 @@ impl<'a> FileWriter { pub fn write_u128be(&mut self, n: &u128) { self.write(&n.to_be_bytes()); } + + pub fn write_vint(&mut self, n: &u128) { + // as defined in the RAR 5.0 spec + let mut n = *n; + loop { + let mut byte = (n & 0x7F) as u8; + n >>= 7; + if n != 0 { + byte |= 0x80; + } + self.write(&[byte]); + if n == 0 { + break; + } + } + } }