ari mcstatusface
This commit is contained in:
commit
b0de168dda
7 changed files with 321 additions and 0 deletions
38
src/leb128.rs
Normal file
38
src/leb128.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
const CONTINUE_BIT: u8 = 0x80;
|
||||
|
||||
pub struct LEB128;
|
||||
|
||||
impl LEB128 {
|
||||
pub fn write_leb128(buffer: &mut Vec<u8>, mut value: u64) -> usize {
|
||||
let mut size: usize = 0;
|
||||
loop {
|
||||
let mut byte: u8 = (value & (std::u8::MAX as u64)).try_into().unwrap();
|
||||
value >>= 7;
|
||||
if value != 0 {
|
||||
byte |= CONTINUE_BIT;
|
||||
}
|
||||
|
||||
buffer.push(byte);
|
||||
size += 1;
|
||||
|
||||
if value == 0 {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_leb128(buffer: &[u8]) -> (u32, usize) {
|
||||
let mut result: u32 = 0;
|
||||
let mut shift: usize = 0;
|
||||
let mut offset: usize = 0;
|
||||
loop {
|
||||
let byte: u8 = buffer[offset];
|
||||
result |= ((byte & (std::i8::MAX as u8)) as u32) << (shift as u32);
|
||||
offset += 1;
|
||||
if byte & CONTINUE_BIT == 0 || offset == 4 {
|
||||
return (result, offset);
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue