1use crate::error::{Error, Result};
34use broadcast_common::len::{need_in, need_out};
35use broadcast_common::{Parse, Serialize};
36
37pub const TABLE_ID: u8 = 0x75;
39
40const HEADER_LEN: usize = 3;
46
47const EXTENSION_HEADER_LEN: usize = 5;
50
51use super::CRC_LEN;
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))]
63#[non_exhaustive]
64pub struct ContainerSection<'a> {
65 pub section_syntax_indicator: bool,
69 pub private_indicator: bool,
71 pub container_id: u16,
73 pub version_number: u8,
75 pub current_next_indicator: bool,
77 pub section_number: u8,
79 pub last_section_number: u8,
81 pub container_data: &'a [u8],
84 #[cfg_attr(feature = "serde", serde(skip))]
86 pub reserved_b1: u8,
87 #[cfg_attr(feature = "serde", serde(skip))]
89 pub reserved_b5: u8,
90}
91
92impl<'a> ContainerSection<'a> {
93 #[must_use]
95 pub fn new(
96 private_indicator: bool,
97 container_id: u16,
98 version_number: u8,
99 current_next_indicator: bool,
100 section_number: u8,
101 last_section_number: u8,
102 container_data: &'a [u8],
103 ) -> Self {
104 Self {
105 section_syntax_indicator: true,
106 private_indicator,
107 container_id,
108 version_number,
109 current_next_indicator,
110 section_number,
111 last_section_number,
112 container_data,
113 reserved_b1: super::RESERVED_2,
114 reserved_b5: super::RESERVED_2,
115 }
116 }
117}
118
119impl<'a> Parse<'a> for ContainerSection<'a> {
120 type Error = crate::error::Error;
121
122 fn parse(bytes: &'a [u8]) -> Result<Self> {
123 need_in(bytes, MIN_LEN, "ContainerSection")?;
124
125 if bytes[0] != TABLE_ID {
126 return Err(Error::UnexpectedTableId {
127 table_id: bytes[0],
128 what: "ContainerSection",
129 expected: &[TABLE_ID],
130 });
131 }
132
133 let section_length = super::section_length_of(bytes);
137 let total = super::check_section_length(bytes.len(), HEADER_LEN, section_length, MIN_LEN)?;
138
139 let private_indicator = (bytes[1] & super::SECTION_B1_PRIVATE_INDICATOR) != 0;
140
141 let container_id = crate::wire::be_u16(bytes, 3, "container")?;
143 let version_number = super::version_number_of(bytes[5]);
145 let current_next_indicator = super::current_next_of(bytes[5]);
146 let section_number = bytes[6];
147 let last_section_number = bytes[7];
148
149 let data_start = HEADER_LEN + EXTENSION_HEADER_LEN;
151 let data_end = total - CRC_LEN;
152 let container_data = &bytes[data_start..data_end];
153
154 Ok(ContainerSection {
155 section_syntax_indicator: bytes[1] & super::SECTION_B1_SSI != 0,
156 private_indicator,
157 container_id,
158 version_number,
159 current_next_indicator,
160 section_number,
161 last_section_number,
162 container_data,
163 reserved_b1: super::b1_reserved_of(bytes[1]),
164 reserved_b5: super::b5_reserved_of(bytes[5]),
165 })
166 }
167}
168
169impl Serialize for ContainerSection<'_> {
170 type Error = crate::error::Error;
171
172 fn serialized_len(&self) -> usize {
173 HEADER_LEN + EXTENSION_HEADER_LEN + self.container_data.len() + CRC_LEN
174 }
175
176 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
177 let len = self.serialized_len();
178 need_out(buf, len)?;
179
180 buf[0] = TABLE_ID;
182 buf[1] = super::b1_of(
185 self.section_syntax_indicator,
186 self.private_indicator,
187 self.reserved_b1,
188 )?;
189 super::write_section_length(buf, len - HEADER_LEN)?;
191
192 buf[3..5].copy_from_slice(&self.container_id.to_be_bytes());
194 buf[5] = super::b5_of(
195 self.reserved_b5,
196 self.version_number,
197 self.current_next_indicator,
198 )?;
199 buf[6] = self.section_number;
200 buf[7] = self.last_section_number;
201
202 let data_start = HEADER_LEN + EXTENSION_HEADER_LEN;
204 let data_end = data_start + self.container_data.len();
205 buf[data_start..data_end].copy_from_slice(self.container_data);
206
207 super::write_crc(buf, data_end);
209
210 Ok(len)
211 }
212}
213impl<'a> crate::traits::TableDef<'a> for ContainerSection<'a> {
214 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
215 const NAME: &'static str = "CONTAINER";
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221
222 fn build_container(
225 container_id: u16,
226 version: u8,
227 current_next: bool,
228 section_number: u8,
229 last_section_number: u8,
230 container_data: &[u8],
231 ) -> Vec<u8> {
232 let c = ContainerSection::new(
233 true,
234 container_id,
235 version,
236 current_next,
237 section_number,
238 last_section_number,
239 container_data,
240 );
241 let mut buf = vec![0u8; c.serialized_len()];
242 c.serialize_into(&mut buf).unwrap();
243 buf
244 }
245
246 #[test]
247 fn parse_happy_path() {
248 let data = [0x00u8, 0xDE, 0xAD, 0xBE, 0xEF];
249 let bytes = build_container(0x1234, 7, true, 1, 3, &data);
250 let c = ContainerSection::parse(&bytes).unwrap();
251 assert!(c.private_indicator);
252 assert_eq!(c.container_id, 0x1234);
253 assert_eq!(c.version_number, 7);
254 assert!(c.current_next_indicator);
255 assert_eq!(c.section_number, 1);
256 assert_eq!(c.last_section_number, 3);
257 assert_eq!(c.container_data, &data[..]);
258 }
259
260 #[test]
261 fn parse_empty_container_data() {
262 let bytes = build_container(0x0000, 0, false, 0, 0, &[]);
263 let c = ContainerSection::parse(&bytes).unwrap();
264 assert_eq!(c.container_id, 0x0000);
265 assert_eq!(c.version_number, 0);
266 assert!(!c.current_next_indicator);
267 assert!(c.container_data.is_empty());
268 }
269
270 #[test]
271 fn parse_rejects_wrong_tag() {
272 let mut bytes = build_container(0x0001, 0, true, 0, 0, &[]);
273 bytes[0] = 0x70; assert!(matches!(
275 ContainerSection::parse(&bytes).unwrap_err(),
276 Error::UnexpectedTableId { table_id: 0x70, .. }
277 ));
278 }
279
280 #[test]
281 fn parse_rejects_short_buffer() {
282 assert!(matches!(
283 ContainerSection::parse(&[0x75, 0x80]).unwrap_err(),
284 Error::BufferTooShort(..)
285 ));
286 }
287
288 #[test]
289 fn parse_rejects_section_length_overflow() {
290 let mut bytes = build_container(0x0001, 0, true, 0, 0, &[]);
291 let fake_sl: u16 = (bytes.len() as u16) + 100 - HEADER_LEN as u16;
292 bytes[1] = (bytes[1] & 0xF0) | ((fake_sl >> 8) as u8 & 0x0F);
293 bytes[2] = (fake_sl & 0xFF) as u8;
294 assert!(matches!(
295 ContainerSection::parse(&bytes).unwrap_err(),
296 Error::SectionLengthOverflow { .. }
297 ));
298 }
299
300 #[test]
301 fn serialize_round_trip() {
302 let data = [0x01u8, 0x02, 0x03];
303 let original = ContainerSection::new(false, 0xABCD, 15, false, 2, 4, &data);
304 let mut buf = vec![0u8; original.serialized_len()];
305 original.serialize_into(&mut buf).unwrap();
306 assert_eq!(ContainerSection::parse(&buf).unwrap(), original);
307 }
308
309 #[test]
310 fn serialize_rejects_output_buffer_too_small() {
311 let c = ContainerSection::new(false, 0x0001, 0, true, 0, 0, &[]);
312 let mut buf = vec![0u8; 2];
313 assert!(matches!(
314 c.serialize_into(&mut buf).unwrap_err(),
315 Error::OutputBufferTooSmall(..)
316 ));
317 }
318
319 #[test]
320 fn table_trait_constants() {
321 assert_eq!(TABLE_ID, 0x75);
322 }
323
324 #[test]
325 fn parse_rejects_zero_section_length() {
326 let mut buf = vec![0u8; 64];
327 buf[0] = TABLE_ID;
328 buf[1] = 0xF0;
329 buf[2] = 0x00;
330 for b in &mut buf[3..] {
331 *b = 0xFF;
332 }
333 assert!(matches!(
334 ContainerSection::parse(&buf).unwrap_err(),
335 Error::SectionLengthOverflow { .. }
336 ));
337 }
338
339 #[test]
343 fn serialize_rejects_container_data_exceeding_section_length() {
344 let data = vec![0xAAu8; 4087];
345 let c = ContainerSection::new(false, 1, 0, true, 0, 0, &data);
346 let mut buf = vec![0u8; c.serialized_len()];
347 assert!(matches!(
348 c.serialize_into(&mut buf).unwrap_err(),
349 Error::FieldOverflow(_)
350 ));
351 }
352
353 #[test]
354 fn serialize_accepts_container_data_at_section_length_boundary() {
355 let data = vec![0xAAu8; 4086];
356 let c = ContainerSection::new(false, 1, 0, true, 0, 0, &data);
357 let mut buf = vec![0u8; c.serialized_len()];
358 c.serialize_into(&mut buf).expect("boundary fits");
359 let reparsed = ContainerSection::parse(&buf).expect("reparse");
360 assert_eq!(c, reparsed);
361 }
362
363 #[cfg(feature = "serde")]
364 #[test]
365 fn serde_json_round_trip() {
366 let data = [0xCAu8, 0xFE];
371 let bytes = build_container(0xBEEF, 9, true, 0, 0, &data);
372 let c = ContainerSection::parse(&bytes).unwrap();
373 let v: serde_json::Value = serde_json::to_value(&c).unwrap();
374 assert_eq!(v["container_id"], 0xBEEF);
375 assert_eq!(v["version_number"], 9);
376 assert_eq!(v["current_next_indicator"], true);
377 assert_eq!(v["container_data"], serde_json::json!([0xCA, 0xFE]));
378 }
379}