1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::borrow::Cow;
use std::io::{Result as IoResult, Write};
use byteorder_slice::byteorder::WriteBytesExt;
use byteorder_slice::result::ReadSlice;
use byteorder_slice::ByteOrder;
use derive_into_owned::IntoOwned;
use super::block_common::{Block, PcapNgBlock};
use crate::errors::PcapError;
#[derive(Clone, Debug, IntoOwned, Eq, PartialEq)]
pub struct SimplePacketBlock<'a> {
pub original_len: u32,
pub data: Cow<'a, [u8]>,
}
impl<'a> PcapNgBlock<'a> for SimplePacketBlock<'a> {
fn from_slice<B: ByteOrder>(mut slice: &'a [u8]) -> Result<(&'a [u8], Self), PcapError> {
if slice.len() < 4 {
return Err(PcapError::InvalidField("SimplePacketBlock: block length < 4"));
}
let original_len = slice.read_u32::<B>().unwrap();
let packet = SimplePacketBlock { original_len, data: Cow::Borrowed(slice) };
Ok((&[], packet))
}
fn write_to<B: ByteOrder, W: Write>(&self, writer: &mut W) -> IoResult<usize> {
writer.write_u32::<B>(self.original_len)?;
writer.write_all(&self.data)?;
let pad_len = (4 - (self.data.len() % 4)) % 4;
writer.write_all(&[0_u8; 3][..pad_len])?;
Ok(4 + self.data.len() + pad_len)
}
fn into_block(self) -> Block<'a> {
Block::SimplePacket(self)
}
}