1use crate::error::{Error, Result};
34use dvb_common::{Parse, Serialize};
35
36pub const TABLE_ID: u8 = 0x75;
38
39pub const PID: u16 = 0x0000;
42
43const HEADER_LEN: usize = 3;
45
46const EXTENSION_HEADER_LEN: usize = 5;
49
50const CRC_LEN: usize = 4;
52
53const MIN_LEN: usize = HEADER_LEN + EXTENSION_HEADER_LEN + CRC_LEN;
55
56#[derive(Debug, Clone, PartialEq, Eq)]
61#[cfg_attr(feature = "serde", derive(serde::Serialize))]
62#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
63pub struct ContainerSection<'a> {
64 pub private_indicator: bool,
66 pub container_id: u16,
68 pub version_number: u8,
70 pub current_next_indicator: bool,
72 pub section_number: u8,
74 pub last_section_number: u8,
76 pub container_data: &'a [u8],
79}
80
81impl<'a> Parse<'a> for ContainerSection<'a> {
82 type Error = crate::error::Error;
83
84 fn parse(bytes: &'a [u8]) -> Result<Self> {
85 if bytes.len() < MIN_LEN {
86 return Err(Error::BufferTooShort {
87 need: MIN_LEN,
88 have: bytes.len(),
89 what: "ContainerSection",
90 });
91 }
92
93 if bytes[0] != TABLE_ID {
94 return Err(Error::UnexpectedTableId {
95 table_id: bytes[0],
96 what: "ContainerSection",
97 expected: &[TABLE_ID],
98 });
99 }
100
101 let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
105 let total = super::check_section_length(bytes.len(), HEADER_LEN, section_length, MIN_LEN)?;
106
107 let private_indicator = (bytes[1] & 0x40) != 0;
108
109 let container_id = u16::from_be_bytes([bytes[3], bytes[4]]);
111 let version_number = (bytes[5] >> 1) & 0x1F;
113 let current_next_indicator = (bytes[5] & 0x01) != 0;
114 let section_number = bytes[6];
115 let last_section_number = bytes[7];
116
117 let data_start = HEADER_LEN + EXTENSION_HEADER_LEN;
119 let data_end = total - CRC_LEN;
120 let container_data = &bytes[data_start..data_end];
121
122 Ok(ContainerSection {
123 private_indicator,
124 container_id,
125 version_number,
126 current_next_indicator,
127 section_number,
128 last_section_number,
129 container_data,
130 })
131 }
132}
133
134impl Serialize for ContainerSection<'_> {
135 type Error = crate::error::Error;
136
137 fn serialized_len(&self) -> usize {
138 HEADER_LEN + EXTENSION_HEADER_LEN + self.container_data.len() + CRC_LEN
139 }
140
141 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
142 let len = self.serialized_len();
143 if buf.len() < len {
144 return Err(Error::OutputBufferTooSmall {
145 need: len,
146 have: buf.len(),
147 });
148 }
149
150 let section_length = (len - HEADER_LEN) as u16;
151 if section_length > 0x0FFF {
152 return Err(Error::SectionLengthOverflow {
153 declared: section_length as usize,
154 available: 0x0FFF,
155 });
156 }
157
158 buf[0] = TABLE_ID;
160 buf[1] = 0x80
163 | (u8::from(self.private_indicator) << 6)
164 | 0x30
165 | ((section_length >> 8) as u8 & 0x0F);
166 buf[2] = (section_length & 0xFF) as u8;
168
169 buf[3..5].copy_from_slice(&self.container_id.to_be_bytes());
171 buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1)
173 | u8::from(self.current_next_indicator);
174 buf[6] = self.section_number;
175 buf[7] = self.last_section_number;
176
177 let data_start = HEADER_LEN + EXTENSION_HEADER_LEN;
179 let data_end = data_start + self.container_data.len();
180 buf[data_start..data_end].copy_from_slice(self.container_data);
181
182 let crc = dvb_common::crc32_mpeg2::compute(&buf[..data_end]);
184 buf[data_end..len].copy_from_slice(&crc.to_be_bytes());
185
186 Ok(len)
187 }
188}
189impl<'a> crate::traits::TableDef<'a> for ContainerSection<'a> {
190 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
191 const NAME: &'static str = "CONTAINER";
192}
193
194#[cfg(test)]
195mod tests {
196 use super::*;
197
198 fn build_container(
201 container_id: u16,
202 version: u8,
203 current_next: bool,
204 section_number: u8,
205 last_section_number: u8,
206 container_data: &[u8],
207 ) -> Vec<u8> {
208 let c = ContainerSection {
209 private_indicator: true,
210 container_id,
211 version_number: version,
212 current_next_indicator: current_next,
213 section_number,
214 last_section_number,
215 container_data,
216 };
217 let mut buf = vec![0u8; c.serialized_len()];
218 c.serialize_into(&mut buf).unwrap();
219 buf
220 }
221
222 #[test]
223 fn parse_happy_path() {
224 let data = [0x00u8, 0xDE, 0xAD, 0xBE, 0xEF];
225 let bytes = build_container(0x1234, 7, true, 1, 3, &data);
226 let c = ContainerSection::parse(&bytes).unwrap();
227 assert!(c.private_indicator);
228 assert_eq!(c.container_id, 0x1234);
229 assert_eq!(c.version_number, 7);
230 assert!(c.current_next_indicator);
231 assert_eq!(c.section_number, 1);
232 assert_eq!(c.last_section_number, 3);
233 assert_eq!(c.container_data, &data[..]);
234 }
235
236 #[test]
237 fn parse_empty_container_data() {
238 let bytes = build_container(0x0000, 0, false, 0, 0, &[]);
239 let c = ContainerSection::parse(&bytes).unwrap();
240 assert_eq!(c.container_id, 0x0000);
241 assert_eq!(c.version_number, 0);
242 assert!(!c.current_next_indicator);
243 assert!(c.container_data.is_empty());
244 }
245
246 #[test]
247 fn parse_rejects_wrong_tag() {
248 let mut bytes = build_container(0x0001, 0, true, 0, 0, &[]);
249 bytes[0] = 0x70; assert!(matches!(
251 ContainerSection::parse(&bytes).unwrap_err(),
252 Error::UnexpectedTableId { table_id: 0x70, .. }
253 ));
254 }
255
256 #[test]
257 fn parse_rejects_short_buffer() {
258 assert!(matches!(
259 ContainerSection::parse(&[0x75, 0x80]).unwrap_err(),
260 Error::BufferTooShort { .. }
261 ));
262 }
263
264 #[test]
265 fn parse_rejects_section_length_overflow() {
266 let mut bytes = build_container(0x0001, 0, true, 0, 0, &[]);
267 let fake_sl: u16 = (bytes.len() as u16) + 100 - HEADER_LEN as u16;
268 bytes[1] = (bytes[1] & 0xF0) | ((fake_sl >> 8) as u8 & 0x0F);
269 bytes[2] = (fake_sl & 0xFF) as u8;
270 assert!(matches!(
271 ContainerSection::parse(&bytes).unwrap_err(),
272 Error::SectionLengthOverflow { .. }
273 ));
274 }
275
276 #[test]
277 fn serialize_round_trip() {
278 let data = [0x01u8, 0x02, 0x03];
279 let original = ContainerSection {
280 private_indicator: false,
281 container_id: 0xABCD,
282 version_number: 15,
283 current_next_indicator: false,
284 section_number: 2,
285 last_section_number: 4,
286 container_data: &data,
287 };
288 let mut buf = vec![0u8; original.serialized_len()];
289 original.serialize_into(&mut buf).unwrap();
290 assert_eq!(ContainerSection::parse(&buf).unwrap(), original);
291 }
292
293 #[test]
294 fn serialize_rejects_output_buffer_too_small() {
295 let c = ContainerSection {
296 private_indicator: false,
297 container_id: 0x0001,
298 version_number: 0,
299 current_next_indicator: true,
300 section_number: 0,
301 last_section_number: 0,
302 container_data: &[],
303 };
304 let mut buf = vec![0u8; 2];
305 assert!(matches!(
306 c.serialize_into(&mut buf).unwrap_err(),
307 Error::OutputBufferTooSmall { .. }
308 ));
309 }
310
311 #[test]
312 fn table_trait_constants() {
313 assert_eq!(TABLE_ID, 0x75);
314 assert_eq!(PID, 0x0000);
315 }
316
317 #[test]
318 fn parse_rejects_zero_section_length() {
319 let mut buf = vec![0u8; 64];
320 buf[0] = TABLE_ID;
321 buf[1] = 0xF0;
322 buf[2] = 0x00;
323 for b in &mut buf[3..] {
324 *b = 0xFF;
325 }
326 assert!(matches!(
327 ContainerSection::parse(&buf).unwrap_err(),
328 Error::SectionLengthOverflow { .. }
329 ));
330 }
331
332 #[cfg(feature = "serde")]
333 #[test]
334 fn serde_json_round_trip() {
335 let data = [0xCAu8, 0xFE];
340 let bytes = build_container(0xBEEF, 9, true, 0, 0, &data);
341 let c = ContainerSection::parse(&bytes).unwrap();
342 let v: serde_json::Value = serde_json::to_value(&c).unwrap();
343 assert_eq!(v["container_id"], 0xBEEF);
344 assert_eq!(v["version_number"], 9);
345 assert_eq!(v["current_next_indicator"], true);
346 assert_eq!(v["container_data"], serde_json::json!([0xCA, 0xFE]));
347 }
348}