Skip to content

Commit

Permalink
feat: Implemented the RAR vint data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Aug 2, 2024
1 parent 3dc5a7b commit 724fc97
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit 724fc97

Please sign in to comment.