dvb_si/descriptors/
stuffing.rs1use crate::error::{Error, Result};
8use crate::traits::Descriptor;
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x42;
13const HEADER_LEN: usize = 2;
14const MAX_BODY_LEN: usize = u8::MAX as usize;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize))]
24#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
25pub struct StuffingDescriptor<'a> {
26 pub stuffing_bytes: &'a [u8],
28}
29
30impl<'a> Parse<'a> for StuffingDescriptor<'a> {
31 type Error = crate::error::Error;
32 fn parse(bytes: &'a [u8]) -> Result<Self> {
33 if bytes.len() < HEADER_LEN {
34 return Err(Error::BufferTooShort {
35 need: HEADER_LEN,
36 have: bytes.len(),
37 what: "StuffingDescriptor header",
38 });
39 }
40 if bytes[0] != TAG {
41 return Err(Error::InvalidDescriptor {
42 tag: bytes[0],
43 reason: "unexpected tag for stuffing_descriptor",
44 });
45 }
46 let length = bytes[1] as usize;
47 let end = HEADER_LEN + length;
48 if bytes.len() < end {
49 return Err(Error::BufferTooShort {
50 need: end,
51 have: bytes.len(),
52 what: "StuffingDescriptor body",
53 });
54 }
55 Ok(Self {
56 stuffing_bytes: &bytes[HEADER_LEN..end],
57 })
58 }
59}
60
61impl Serialize for StuffingDescriptor<'_> {
62 type Error = crate::error::Error;
63 fn serialized_len(&self) -> usize {
64 HEADER_LEN + self.stuffing_bytes.len()
65 }
66
67 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
68 let len = self.serialized_len();
69 if buf.len() < len {
70 return Err(Error::OutputBufferTooSmall {
71 need: len,
72 have: buf.len(),
73 });
74 }
75 if self.stuffing_bytes.len() > MAX_BODY_LEN {
77 return Err(Error::SectionLengthOverflow {
78 declared: self.stuffing_bytes.len(),
79 available: MAX_BODY_LEN,
80 });
81 }
82 buf[0] = TAG;
83 buf[1] = self.stuffing_bytes.len() as u8;
84 buf[HEADER_LEN..len].copy_from_slice(self.stuffing_bytes);
85 Ok(len)
86 }
87}
88
89impl<'a> Descriptor<'a> for StuffingDescriptor<'a> {
90 const TAG: u8 = TAG;
91 fn descriptor_length(&self) -> u8 {
92 self.stuffing_bytes.len() as u8
93 }
94}
95
96impl<'a> crate::traits::DescriptorDef<'a> for StuffingDescriptor<'a> {
97 const TAG: u8 = TAG;
98 const NAME: &'static str = "STUFFING";
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn parse_arbitrary_bytes() {
107 let bytes = [TAG, 4, 0x00, 0xFF, 0xAB, 0x7E];
108 let d = StuffingDescriptor::parse(&bytes).unwrap();
109 assert_eq!(d.stuffing_bytes, &[0x00, 0xFF, 0xAB, 0x7E]);
110 }
111
112 #[test]
113 fn parse_empty_body_valid() {
114 let bytes = [TAG, 0];
115 let d = StuffingDescriptor::parse(&bytes).unwrap();
116 assert!(d.stuffing_bytes.is_empty());
117 }
118
119 #[test]
120 fn parse_rejects_wrong_tag() {
121 assert!(matches!(
122 StuffingDescriptor::parse(&[0x43, 0]).unwrap_err(),
123 Error::InvalidDescriptor { tag: 0x43, .. }
124 ));
125 }
126
127 #[test]
128 fn parse_rejects_short_buffer() {
129 let bytes = [TAG, 4, 0x00, 0xFF];
131 assert!(matches!(
132 StuffingDescriptor::parse(&bytes).unwrap_err(),
133 Error::BufferTooShort { .. }
134 ));
135 }
136
137 #[test]
138 fn serialize_round_trip() {
139 let d = StuffingDescriptor {
140 stuffing_bytes: &[0xDE, 0xAD, 0xBE, 0xEF],
141 };
142 let mut buf = vec![0u8; d.serialized_len()];
143 d.serialize_into(&mut buf).unwrap();
144 assert_eq!(StuffingDescriptor::parse(&buf).unwrap(), d);
145 }
146
147 #[test]
148 fn serialize_rejects_small_buffer() {
149 let d = StuffingDescriptor {
150 stuffing_bytes: &[0x01, 0x02],
151 };
152 let mut tiny = [0u8; 3];
153 assert!(matches!(
154 d.serialize_into(&mut tiny).unwrap_err(),
155 Error::OutputBufferTooSmall { .. }
156 ));
157 }
158
159 #[test]
160 fn serialize_rejects_over_range_body() {
161 let big = vec![0u8; 256];
162 let d = StuffingDescriptor {
163 stuffing_bytes: &big,
164 };
165 let mut buf = vec![0u8; d.serialized_len()];
166 assert!(matches!(
167 d.serialize_into(&mut buf).unwrap_err(),
168 Error::SectionLengthOverflow { .. }
169 ));
170 }
171
172 #[cfg(feature = "serde")]
173 #[test]
174 fn serde_serialize_stable() {
175 let d = StuffingDescriptor {
180 stuffing_bytes: &[0x11, 0x22, 0x33],
181 };
182 let json = serde_json::to_string(&d).unwrap();
183 assert!(json.contains("17") && json.contains("34") && json.contains("51"));
184 let d2 = StuffingDescriptor {
186 stuffing_bytes: &[0x11, 0x22, 0x33],
187 };
188 assert_eq!(json, serde_json::to_string(&d2).unwrap());
189 }
190}