pub struct ZbiItem<B: SplitByteSlice> {
pub header: Ref<B, ZbiHeader>,
pub payload: B,
}Expand description
ZbiItem is element representation in ZbiContainer
It contains of header and payload. Both contain references to actual location in buffer.
And payload goes right after header in the buffer.
Header must be ZBI_ALIGNMENT_USIZE aligned in the buffer.
The length field specifies the actual payload length and does not include the size of padding.
Since all headers in ZbiContainer are ZBI_ALIGNMENT_USIZE aligned payload may be followed by padding,
which is included in ZbiContainer length, but not in each ZbiItem
Fields§
§header: Ref<B, ZbiHeader>ZBI header
payload: BPayload corresponding to ZBI header
Implementations§
Source§impl<B: SplitByteSlice + PartialEq> ZbiItem<B>
impl<B: SplitByteSlice + PartialEq> ZbiItem<B>
Sourcepub fn parse(buffer: B) -> Result<(ZbiItem<B>, B), ZbiError>
pub fn parse(buffer: B) -> Result<(ZbiItem<B>, B), ZbiError>
Attempts to parse provided buffer.
§Arguments
buffer- buffer to parse (can be mutable if further changes to element is required)
§Returns
Ok((ZbiItem, tail))- if parsing was successful function returnsZbiItemand tail of buffer that wasn’t used.Err(ZbiError)- if parsing fails Err is returned.
§Example
use zbi::ZbiItem;
let (zbi_item, tail) = ZbiItem::parse(buffer).unwrap();
println!("{}", zbi_item.header.type_);
println!("{}", tail.len());
assert_eq!(zbi_item.header.length, zbi_item.payload.len() as u32);Sourcepub fn is_valid(&self) -> Result<(), ZbiError>
pub fn is_valid(&self) -> Result<(), ZbiError>
Validates ZbiItem header values.
§Example
use zbi::{ZbiItem, ZbiError};
// E.g. if `header.magic = 0` this is invalid value.
let (zbi_item, tail) = ZbiItem::parse(buffer).unwrap();
assert_eq!(zbi_item.header.magic, 0);
assert_eq!(zbi_item.is_valid(), Err(ZbiError::BadMagic));Source§impl<B: SplitByteSliceMut + PartialEq> ZbiItem<B>
impl<B: SplitByteSliceMut + PartialEq> ZbiItem<B>
Sourcepub fn new(
buffer: B,
type_: ZbiType,
extra: u32,
flags: ZbiFlags,
payload_len: usize,
) -> Result<(ZbiItem<B>, B), ZbiError>
pub fn new( buffer: B, type_: ZbiType, extra: u32, flags: ZbiFlags, payload_len: usize, ) -> Result<(ZbiItem<B>, B), ZbiError>
Create ZbiItem with provided information and payload length.
§Result
(ZbiItem, tail)- returned on success.ZbiItemwould have payload of requested length. And tail would be remaining part of thebufferthat wasn’t used.ZbiError::BadAlignment- if buffer wasn’t aligned.ZbiError::TooBig- if buffer is not long enough to holdZbiHeader+payloadofpayload_len.ZbiError::PlatformBadLength- ifpayload_lenvalue is bigger thanu32::MAX
§Example
use zbi::{ZbiItem, ZbiFlags, ZbiType};
let (item, _tail) = ZbiItem::new(
&mut buffer[..],
ZbiType::KernelX64,
0,
ZbiFlags::default(),
2,
).unwrap();
assert_eq!(item.header.length, 2);
assert_eq!(item.payload.len(), 2);Trait Implementations§
Auto Trait Implementations§
impl<B> Freeze for ZbiItem<B>where
B: Freeze,
impl<B> RefUnwindSafe for ZbiItem<B>where
B: RefUnwindSafe,
impl<B> Send for ZbiItem<B>where
B: Send,
impl<B> Sync for ZbiItem<B>where
B: Sync,
impl<B> Unpin for ZbiItem<B>where
B: Unpin,
impl<B> UnsafeUnpin for ZbiItem<B>where
B: UnsafeUnpin,
impl<B> UnwindSafe for ZbiItem<B>where
B: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more