use crate::block::opts::*;
use crate::block::util::*;
use bytes::Buf;
use tracing::*;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SectionHeader {
pub endianness: Endianness,
pub major_version: u16,
pub minor_version: u16,
pub section_length: Option<u64>,
pub shb_hardware: String,
pub shb_os: String,
pub shb_userappl: String,
}
impl FromBytes for SectionHeader {
fn parse<T: Buf>(mut buf: T, endianness: Endianness) -> Result<SectionHeader, BlockError> {
ensure_remaining!(buf, 12);
buf.advance(4); let major_version = read_u16(&mut buf, endianness);
let minor_version = read_u16(&mut buf, endianness);
let section_length = match read_i64(&mut buf, endianness) {
-1 => None,
x => match u64::try_from(x) {
Ok(x) => Some(x),
Err(_) => {
warn!("SHB lists the length as {x}, but this is invalid");
None
}
},
};
let mut shb_hardware = String::new();
let mut shb_os = String::new();
let mut shb_userappl = String::new();
parse_options(buf, endianness, |option_type, option_bytes| {
match option_type {
2 => shb_hardware = String::from_utf8_lossy(&option_bytes).to_string(),
3 => shb_os = String::from_utf8_lossy(&option_bytes).to_string(),
4 => shb_userappl = String::from_utf8_lossy(&option_bytes).to_string(),
_ => (), }
});
Ok(SectionHeader {
endianness,
major_version,
minor_version,
section_length,
shb_hardware,
shb_os,
shb_userappl,
})
}
}