pcap_file/pcapng/blocks/
unknown.rs

1//! Unknown Block.
2
3use std::borrow::Cow;
4use std::io::{Result as IoResult, Write};
5
6use byteorder_slice::ByteOrder;
7use derive_into_owned::IntoOwned;
8
9use super::block_common::{Block, PcapNgBlock};
10use crate::PcapError;
11
12
13/// Unknown block
14#[derive(Clone, Debug, IntoOwned, Eq, PartialEq)]
15pub struct UnknownBlock<'a> {
16    /// Block type
17    pub type_: u32,
18    /// Block length
19    pub length: u32,
20    /// Block value
21    pub value: Cow<'a, [u8]>,
22}
23
24impl<'a> UnknownBlock<'a> {
25    /// Creates a new [`UnknownBlock`]
26    pub fn new(type_: u32, length: u32, value: &'a [u8]) -> Self {
27        UnknownBlock { type_, length, value: Cow::Borrowed(value) }
28    }
29}
30
31impl<'a> PcapNgBlock<'a> for UnknownBlock<'a> {
32    fn from_slice<B: ByteOrder>(_slice: &'a [u8]) -> Result<(&[u8], Self), PcapError>
33    where
34        Self: Sized,
35    {
36        unimplemented!("UnkknownBlock::<as PcapNgBlock>::From_slice shouldn't be called")
37    }
38
39    fn write_to<B: ByteOrder, W: Write>(&self, writer: &mut W) -> IoResult<usize> {
40        writer.write_all(&self.value)?;
41        Ok(self.value.len())
42    }
43
44    fn into_block(self) -> Block<'a> {
45        Block::Unknown(self)
46    }
47}