const CONTINUE_BIT: u8 = 0x80; pub struct LEB128; impl LEB128 { pub fn write_leb128(buffer: &mut Vec, 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; } } }