Skip to content
File

Blob: firmware/vendor/sctp-proto/src/param/param_header.rs

rust63 lines
1use super::{param_type::*, *};
2 
3use bytes::BufMut;
4 
5#[derive(Debug, Clone, PartialEq)]
6pub(crate) struct ParamHeader {
7 pub(crate) typ: ParamType,
8 pub(crate) value_length: u16,
9}
10 
11pub(crate) const PARAM_HEADER_LENGTH: usize = 4;
12 
13/// String makes paramHeader printable
14impl fmt::Display for ParamHeader {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 write!(f, "{}", self.typ)
17 }
18}
19 
20impl Param for ParamHeader {
21 fn header(&self) -> ParamHeader {
22 self.clone()
23 }
24 
25 fn unmarshal(raw: &Bytes) -> Result<Self> {
26 if raw.len() < PARAM_HEADER_LENGTH {
27 return Err(Error::ErrParamHeaderTooShort);
28 }
29 
30 let reader = &mut raw.clone();
31 
32 let typ: ParamType = reader.get_u16().into();
33 
34 let len = reader.get_u16() as usize;
35 if len < PARAM_HEADER_LENGTH || raw.len() < len {
36 return Err(Error::ErrParamHeaderTooShort);
37 }
38 
39 Ok(ParamHeader {
40 typ,
41 value_length: (len - PARAM_HEADER_LENGTH) as u16,
42 })
43 }
44 
45 fn marshal_to(&self, writer: &mut BytesMut) -> Result<usize> {
46 writer.put_u16(self.typ.into());
47 writer.put_u16(self.value_length + PARAM_HEADER_LENGTH as u16);
48 Ok(writer.len())
49 }
50 
51 fn value_length(&self) -> usize {
52 self.value_length as usize
53 }
54 
55 fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
56 Box::new(self.clone())
57 }
58 
59 fn as_any(&self) -> &(dyn Any + Send + Sync) {
60 self
61 }
62}