use deku::ctx::BitSize; use deku::prelude::*; use deku::reader::ReaderRet; use deku::{reader::Reader, DekuError}; pub mod blocks; pub mod groups; pub mod prelude; pub use blocks::Data; /// # Read the size of a FSV block / field /// Documentation: SSV Documentation, page 29 fn size_read(reader: &mut Reader) -> Result { let size_bytes = u8::from_reader_with_ctx(reader, BitSize(8))?; let size: u64 = if size_bytes & 0b1000_0000 == 0 { // If the Most Significant Bit is 0, the size is encoded on 7 bits size_bytes.into() } else { // Else, the 7 following bits indicate the number of bytes of the block containing the size let size_block_len: usize = (size_bytes & 0b0111_1111).into(); if size_block_len > 4 { return Err(DekuError::Parse(format!("Unexpected size block length: {}", size_block_len).into())); }; // The block containing the size is encoded on 1 to 4 bytes let buffer: &mut [u8; 4] = &mut [0; 4]; let write_offset = 4 - size_block_len; match reader.read_bytes(size_block_len, &mut buffer[write_offset..])? { ReaderRet::Bits(_bit_vec) => return Err(DekuError::Parse("Unexpected result reading size bytes: got bits".into())), ReaderRet::Bytes => u32::from_be_bytes(*buffer).into(), } }; Ok(size) } /// # Map bytes to a lossy string /// This function is used to map bytes to a string, ignoring invalid UTF-8 characters /// Example: [0x41, 0x42] -> "AB" /// Example: [48, 49, 50, 51] -> "0123" fn map_bytes_to_lossy_string(data: &[u8]) -> Result { // let data = data.to_vec(); let version: String = String::from_utf8_lossy(data).to_string(); Ok(version) }