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