mpeg_ts/section.rs
1//! Generic PSI/SI section framing — ITU-T H.222.0 §2.4 (= ISO/IEC 13818-1) and ETSI EN 300 468 §5.1.1.
2//!
3//! Every PSI and SI table is carried in one or more sections. This module
4//! parses the **section header** and exposes the payload + CRC for
5//! table-specific parsers to consume.
6//!
7//! # Section layout
8//!
9//! ```text
10//! byte 0: table_id (8 bits)
11//! byte 1 bit 7: section_syntax_indicator (1 bit)
12//! byte 1 bit 6: private_indicator (1 bit)
13//! byte 1 bits 5-4: reserved (2 bits — ignored)
14//! byte 1 bits 3-0 + byte 2: section_length (12 bits)
15//!
16//! Long-form (section_syntax_indicator == 1):
17//! byte 3-4: table_id_extension (16 bits)
18//! byte 5: reserved(2) | version_number(5) | current_next_indicator(1)
19//! byte 6: section_number (8 bits)
20//! byte 7: last_section_number (8 bits)
21//! byte 8..(total-4): payload
22//! last 4 bytes: CRC_32
23//!
24//! Short-form (section_syntax_indicator == 0, e.g. TDT):
25//! byte 3..(3+section_length): payload (no extension header, no CRC)
26//! ```
27//!
28//! NOTE the TOT exception: the TOT (0x73) also sets SSI=0 but DOES end with a
29//! CRC_32 (EN 300 468 §5.2.6). Parsing it through this generic short-form
30//! path folds the CRC into `payload` — use the table-specific `TotSection` parser instead.
31//!
32//! `section_length` counts bytes *after* the 3-byte section header, so the
33//! total section size is `section_length + 3`.
34
35use crate::error::{Error, Result};
36use broadcast_common::crc32_mpeg2 as crc;
37use broadcast_common::{Parse, Serialize};
38
39// Minimum bytes to read the section header (table_id + section_syntax_indicator
40// + section_length field = 3 bytes).
41const MIN_HEADER_LEN: usize = 3;
42
43// Long-form header adds: extension_id(2) + version/cni(1) + sec_num(1) +
44// last_sec_num(1) = 5 bytes.
45const LONG_FORM_EXTRA: usize = 5;
46
47// CRC occupies the last 4 bytes of every long-form section.
48const CRC_LEN: usize = 4;
49
50/// A parsed PSI/SI section header, borrowing the raw input buffer for payload.
51///
52/// Created via `Section::parse(bytes)`. Does **not** validate the CRC on
53/// construction — call [`Section::validate_crc`] explicitly.
54#[derive(Debug, Clone, PartialEq, Eq)]
55#[cfg_attr(feature = "serde", derive(serde::Serialize))]
56pub struct Section<'a> {
57 /// Table identifier.
58 pub table_id: u8,
59 /// When `true` the section uses the long-form syntax (has extension header
60 /// and CRC). When `false` only the 3-byte header is present (short form,
61 /// e.g. TDT — but see the module docs for the TOT exception: SSI=0 yet
62 /// CRC present; parse TOT via `tables::tot`, not this path).
63 pub section_syntax_indicator: bool,
64 /// Private indicator bit (meaning is table-specific).
65 pub private_indicator: bool,
66 /// Number of bytes following byte 2 of the section header.
67 pub section_length: u16,
68 /// Table ID extension (aka `table_id_extension`). Present only for
69 /// long-form sections; zero for short-form.
70 pub extension_id: u16,
71 /// Version number (5 bits). Present only for long-form sections.
72 pub version_number: u8,
73 /// `current_next_indicator` flag. Present only for long-form sections.
74 pub current_next_indicator: bool,
75 /// Section number within the table sub-table.
76 pub section_number: u8,
77 /// Number of the last section in the table sub-table.
78 pub last_section_number: u8,
79 /// Section payload: excludes the header bytes and the trailing CRC for
80 /// long-form sections. For short-form sections this is bytes
81 /// `3..(section_length + 3)`.
82 pub payload: &'a [u8],
83 /// Declared CRC value (last 4 bytes, big-endian). `None` for short-form
84 /// sections which carry no CRC.
85 pub crc32: Option<u32>,
86}
87
88impl<'a> Section<'a> {
89 /// Return the payload slice (same as the `payload` field — convenience
90 /// getter for code that has a `&Section` reference).
91 #[inline]
92 pub fn payload(&self) -> &'a [u8] {
93 self.payload
94 }
95
96 /// Validate the CRC-32 of the section against `raw` — the complete section
97 /// bytes (including header and CRC suffix).
98 ///
99 /// For short-form sections (`section_syntax_indicator == false`) this
100 /// returns `Ok(())` immediately because no CRC is present.
101 ///
102 /// # Errors
103 ///
104 /// Returns [`Error::CrcMismatch`] when the computed CRC over
105 /// `raw[..raw.len() - 4]` does not match the declared value at
106 /// `raw[raw.len()-4..]`.
107 pub fn validate_crc(&self, raw: &[u8]) -> Result<()> {
108 let expected = match self.crc32 {
109 None => return Ok(()), // short-form — no CRC
110 Some(v) => v,
111 };
112
113 // Guard: raw must be at least CRC_LEN bytes for a valid long-form section.
114 if raw.len() < CRC_LEN {
115 return Err(Error::BufferTooShort {
116 need: CRC_LEN,
117 have: raw.len(),
118 what: "CRC suffix in validate_crc",
119 });
120 }
121
122 // The CRC covers everything up to (but not including) the 4 CRC bytes.
123 let covered = &raw[..raw.len() - CRC_LEN];
124 let computed = crc::compute(covered);
125
126 if computed != expected {
127 return Err(Error::CrcMismatch { computed, expected });
128 }
129 Ok(())
130 }
131}
132
133impl<'a> Parse<'a> for Section<'a> {
134 type Error = crate::error::Error;
135 /// Parse a complete section from `bytes`.
136 ///
137 /// # Errors
138 ///
139 /// - [`Error::BufferTooShort`] — fewer than 3 bytes supplied.
140 /// - [`Error::SectionLengthOverflow`] — `section_length` field declares
141 /// more data than `bytes` contains.
142 fn parse(bytes: &'a [u8]) -> Result<Self> {
143 // ── 3-byte common header ────────────────────────────────────────────
144 if bytes.len() < MIN_HEADER_LEN {
145 return Err(Error::BufferTooShort {
146 need: MIN_HEADER_LEN,
147 have: bytes.len(),
148 what: "section header",
149 });
150 }
151
152 let table_id = bytes[0];
153 let section_syntax_indicator = (bytes[1] & 0x80) != 0;
154 let private_indicator = (bytes[1] & 0x40) != 0;
155 let section_length = (((bytes[1] & 0x0F) as u16) << 8) | (bytes[2] as u16);
156
157 // Total section size is section_length + 3 (the 3-byte header itself
158 // is not counted by section_length).
159 let total = (section_length as usize) + MIN_HEADER_LEN;
160
161 if bytes.len() < total {
162 return Err(Error::SectionLengthOverflow {
163 declared: total,
164 available: bytes.len(),
165 });
166 }
167
168 // Work only inside the declared section boundary.
169 let section_bytes = &bytes[..total];
170
171 if !section_syntax_indicator {
172 // ── Short-form section (e.g. TDT) ──────────────────────────────
173 // No extension header, no CRC. Payload is everything after the
174 // 3-byte header.
175 let payload = §ion_bytes[MIN_HEADER_LEN..];
176 return Ok(Section {
177 table_id,
178 section_syntax_indicator,
179 private_indicator,
180 section_length,
181 extension_id: 0,
182 version_number: 0,
183 current_next_indicator: false,
184 section_number: 0,
185 last_section_number: 0,
186 payload,
187 crc32: None,
188 });
189 }
190
191 // ── Long-form section ───────────────────────────────────────────────
192 // Minimum size for a valid long-form section:
193 // 3 (common header) + 5 (extension header) + 4 (CRC) = 12 bytes.
194 let min_long = MIN_HEADER_LEN + LONG_FORM_EXTRA + CRC_LEN;
195 if section_bytes.len() < min_long {
196 return Err(Error::BufferTooShort {
197 need: min_long,
198 have: section_bytes.len(),
199 what: "long-form section extension header + CRC",
200 });
201 }
202
203 let extension_id = ((bytes[3] as u16) << 8) | (bytes[4] as u16);
204 let version_number = (bytes[5] >> 1) & 0x1F;
205 let current_next_indicator = (bytes[5] & 0x01) != 0;
206 let section_number = bytes[6];
207 let last_section_number = bytes[7];
208
209 // Payload: bytes[8 .. total-4] (excludes 5-byte extension header
210 // counting from offset 3, and the 4-byte CRC at the end).
211 let payload_start = MIN_HEADER_LEN + LONG_FORM_EXTRA;
212 let payload_end = total - CRC_LEN;
213 let payload = §ion_bytes[payload_start..payload_end];
214
215 // Read declared CRC from last 4 bytes of the section (big-endian).
216 let crc_offset = total - CRC_LEN;
217 let crc32 = Some(
218 ((section_bytes[crc_offset] as u32) << 24)
219 | ((section_bytes[crc_offset + 1] as u32) << 16)
220 | ((section_bytes[crc_offset + 2] as u32) << 8)
221 | (section_bytes[crc_offset + 3] as u32),
222 );
223
224 Ok(Section {
225 table_id,
226 section_syntax_indicator,
227 private_indicator,
228 section_length,
229 extension_id,
230 version_number,
231 current_next_indicator,
232 section_number,
233 last_section_number,
234 payload,
235 crc32,
236 })
237 }
238}
239
240impl Serialize for Section<'_> {
241 type Error = crate::error::Error;
242 fn serialized_len(&self) -> usize {
243 // Total size = section_length + 3 (the 3-byte base header precedes
244 // the bytes counted by section_length).
245 usize::from(self.section_length) + MIN_HEADER_LEN
246 }
247
248 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
249 let need = self.serialized_len();
250 if buf.len() < need {
251 return Err(Error::OutputBufferTooSmall {
252 need,
253 have: buf.len(),
254 });
255 }
256
257 // Byte 0: table_id
258 buf[0] = self.table_id;
259
260 // Byte 1: SSI | PI | 2-bit reserved (set high per spec) | length hi 4 bits
261 let length_hi = ((self.section_length >> 8) as u8) & 0x0F;
262 let ssi = u8::from(self.section_syntax_indicator) << 7;
263 let pi = u8::from(self.private_indicator) << 6;
264 // Reserved bits 5..4 are 'reserved' per §5.1.1 — convention is both set.
265 buf[1] = ssi | pi | 0x30 | length_hi;
266
267 // Byte 2: length low 8 bits
268 buf[2] = (self.section_length & 0xFF) as u8;
269
270 if self.section_syntax_indicator {
271 // Long form: 5 bytes of extension header, then payload, then CRC.
272 buf[3] = (self.extension_id >> 8) as u8;
273 buf[4] = (self.extension_id & 0xFF) as u8;
274 // Byte 5: 2-bit reserved (both high) | 5-bit version | 1-bit current_next
275 let version = (self.version_number & 0x1F) << 1;
276 let cni = u8::from(self.current_next_indicator);
277 buf[5] = 0xC0 | version | cni;
278 buf[6] = self.section_number;
279 buf[7] = self.last_section_number;
280
281 let payload_start = MIN_HEADER_LEN + LONG_FORM_EXTRA;
282 let payload_end = payload_start + self.payload.len();
283 buf[payload_start..payload_end].copy_from_slice(self.payload);
284
285 // Append CRC — use the declared value to preserve round-trip identity.
286 let crc = self
287 .crc32
288 .unwrap_or_else(|| crc::compute(&buf[..payload_end]));
289 let crc_start = payload_end;
290 buf[crc_start..crc_start + CRC_LEN].copy_from_slice(&crc.to_be_bytes());
291 } else {
292 // Short form: no extension header, no CRC.
293 let payload_end = MIN_HEADER_LEN + self.payload.len();
294 buf[MIN_HEADER_LEN..payload_end].copy_from_slice(self.payload);
295 }
296
297 Ok(need)
298 }
299}
300
301#[cfg(test)]
302mod tests {
303 use super::*;
304 use alloc::vec;
305 use alloc::vec::Vec;
306 use broadcast_common::crc32_mpeg2::compute as crc32;
307
308 // ── Helper: build a minimal long-form section with correct CRC ───────────
309
310 /// Build a syntactically valid long-form section byte vector.
311 ///
312 /// Layout: [table_id, flags+len_hi, len_lo, ext_hi, ext_lo,
313 /// ver_cni, sec_num, last_sec_num, ...payload..., crc(4)]
314 fn make_long_section(
315 table_id: u8,
316 extension_id: u16,
317 version: u8,
318 current_next: bool,
319 section_number: u8,
320 last_section_number: u8,
321 payload: &[u8],
322 ) -> Vec<u8> {
323 // section_length = 5 (extension header) + payload.len() + 4 (CRC)
324 let section_length: u16 = (5 + payload.len() + 4) as u16;
325
326 // reserved(2) | version(5) | current_next(1)
327 let ver_cni = 0xC0u8 | ((version & 0x1F) << 1) | (current_next as u8);
328 let mut buf: Vec<u8> = vec![
329 table_id,
330 // section_syntax_indicator=1, private_indicator=0, reserved=0b11, upper 4 bits of section_length
331 0x80 | 0x30 | ((section_length >> 8) as u8 & 0x0F),
332 (section_length & 0xFF) as u8,
333 (extension_id >> 8) as u8,
334 (extension_id & 0xFF) as u8,
335 ver_cni,
336 section_number,
337 last_section_number,
338 ];
339 buf.extend_from_slice(payload);
340
341 // Compute CRC over bytes so far, append as big-endian u32.
342 let crc = crc32(&buf);
343 buf.push((crc >> 24) as u8);
344 buf.push((crc >> 16) as u8);
345 buf.push((crc >> 8) as u8);
346 buf.push(crc as u8);
347
348 buf
349 }
350
351 // ── Test 1 ───────────────────────────────────────────────────────────────
352
353 #[test]
354 fn parse_rejects_buffer_shorter_than_3_bytes() {
355 for bad_len in [0usize, 1, 2] {
356 let buf = vec![0x00u8; bad_len];
357 let err = Section::parse(&buf).unwrap_err();
358 assert!(
359 matches!(err, Error::BufferTooShort { need: 3, have, .. } if have == bad_len),
360 "expected BufferTooShort for len={bad_len}, got {err:?}"
361 );
362 }
363 }
364
365 // ── Test 2 ───────────────────────────────────────────────────────────────
366
367 #[test]
368 fn parse_reads_table_id_syntax_indicator_and_length() {
369 // Construct a minimal 13-byte long-form section with no payload.
370 // section_length = 5 (extension header) + 0 (payload) + 4 (CRC) = 9
371 // BUT we need section_length >= min for a valid section: 9 → total = 12, which is 12 bytes, not 13.
372 // Let's use 1 byte of payload so section_length = 10, total = 13.
373 let raw = make_long_section(0x42, 0x1234, 3, true, 0, 0, &[0xAB]);
374 assert_eq!(raw.len(), 13);
375
376 let section = Section::parse(&raw).unwrap();
377 assert_eq!(section.table_id, 0x42);
378 assert!(section.section_syntax_indicator);
379 // section_length = 5 + 1 + 4 = 10
380 assert_eq!(section.section_length, 10);
381 }
382
383 // ── Test 3 ───────────────────────────────────────────────────────────────
384
385 #[test]
386 fn parse_rejects_when_section_length_exceeds_buffer() {
387 // Build a 3-byte header that claims section_length = 100 bytes of data.
388 // Buffer is only 3 bytes (just the header), so total = 103 > 3.
389 let buf = [
390 0x00u8, // table_id
391 0x80u8, // section_syntax_indicator=1, section_length upper nibble = 0
392 100u8, // section_length lower byte = 100 → total = 103
393 ];
394 let err = Section::parse(&buf).unwrap_err();
395 assert!(
396 matches!(
397 err,
398 Error::SectionLengthOverflow {
399 declared: 103,
400 available: 3
401 }
402 ),
403 "expected SectionLengthOverflow, got {err:?}"
404 );
405 }
406
407 // ── Test 4 ───────────────────────────────────────────────────────────────
408
409 #[test]
410 fn parse_reads_extension_id_version_current_next_section_numbers() {
411 let raw = make_long_section(
412 0x02, // PMT table_id
413 0xBEEF, // extension_id / program_number
414 7, // version_number
415 true, // current_next
416 2, // section_number
417 5, // last_section_number
418 &[0x00, 0x00], // dummy payload
419 );
420
421 let section = Section::parse(&raw).unwrap();
422 assert_eq!(section.extension_id, 0xBEEF);
423 assert_eq!(section.version_number, 7);
424 assert!(section.current_next_indicator);
425 assert_eq!(section.section_number, 2);
426 assert_eq!(section.last_section_number, 5);
427 }
428
429 #[test]
430 fn parse_reads_current_next_indicator_false() {
431 // Same as test 4 but with current_next = false (bit 0 of byte 5 cleared).
432 let raw = make_long_section(
433 0x02, // PMT table_id
434 0xBEEF, // extension_id
435 7, // version_number
436 false, // current_next — the field under test
437 2, // section_number
438 5, // last_section_number
439 &[0x00, 0x00],
440 );
441
442 let section = Section::parse(&raw).unwrap();
443 assert!(!section.current_next_indicator);
444 // Confirm the other fields are unaffected by flipping bit 0.
445 assert_eq!(section.extension_id, 0xBEEF);
446 assert_eq!(section.version_number, 7);
447 assert_eq!(section.section_number, 2);
448 assert_eq!(section.last_section_number, 5);
449 }
450
451 // ── Test 5 ───────────────────────────────────────────────────────────────
452
453 #[test]
454 fn payload_slice_excludes_header_and_crc() {
455 let inner_payload = &[0x01u8, 0x02, 0x03, 0x04, 0x05];
456 let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, inner_payload);
457
458 let section = Section::parse(&raw).unwrap();
459 assert_eq!(section.payload(), inner_payload);
460 }
461
462 // ── Test 6 ───────────────────────────────────────────────────────────────
463
464 #[test]
465 fn validate_crc_accepts_matching_crc32() {
466 let raw = make_long_section(0x00, 0x0001, 1, true, 0, 0, &[0xDE, 0xAD, 0xBE, 0xEF]);
467 let section = Section::parse(&raw).unwrap();
468 section.validate_crc(&raw).expect("CRC should match");
469 }
470
471 // ── Test 7 ───────────────────────────────────────────────────────────────
472
473 #[test]
474 fn validate_crc_rejects_flipped_bit() {
475 let mut raw = make_long_section(0x00, 0x0001, 1, true, 0, 0, &[0xDE, 0xAD, 0xBE, 0xEF]);
476 // Flip a bit inside the payload (byte 8 is the first payload byte) BEFORE
477 // parsing. The 4-byte CRC at the tail of `raw` was computed over the
478 // original (un-flipped) bytes and is NOT updated here, so after the flip:
479 // • `raw[..raw.len()-4]` contains corrupted data, and
480 // • `raw[raw.len()-4..]` still holds the CRC of the original data.
481 // Parsing after the flip captures that old CRC into `section.crc32`.
482 // `validate_crc` then recomputes over the corrupted bytes and detects the
483 // mismatch — which is exactly the invariant we are testing.
484 // (Parsing before the flip would immutably borrow `raw` for the lifetime
485 // of `section` — the compiler would then reject `raw[8] ^= 0x01` because
486 // a mutable borrow conflicts with any live shared borrow.)
487 raw[8] ^= 0x01;
488
489 let section = Section::parse(&raw).unwrap();
490 let err = section.validate_crc(&raw).unwrap_err();
491 assert!(
492 matches!(err, Error::CrcMismatch { .. }),
493 "expected CrcMismatch, got {err:?}"
494 );
495 }
496
497 // ── Test (Fix 1 TDD) ─────────────────────────────────────────────────────
498
499 #[test]
500 fn validate_crc_rejects_raw_slice_shorter_than_crc_len() {
501 let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, &[0xDE, 0xAD]);
502 let section = Section::parse(&raw).unwrap();
503 // Pass an empty slice — shorter than CRC_LEN bytes.
504 let err = section.validate_crc(&[]).unwrap_err();
505 assert!(
506 matches!(err, Error::BufferTooShort { need: CRC_LEN, .. }),
507 "expected BufferTooShort(need=CRC_LEN), got {err:?}"
508 );
509 }
510
511 // ── Test 8 ───────────────────────────────────────────────────────────────
512
513 #[test]
514 fn short_form_section_has_no_crc() {
515 // TDT-style short-form section: section_syntax_indicator = 0.
516 // section_length = 5 (5 bytes of payload), total = 8 bytes.
517 let buf = [
518 0x70u8, // table_id (TDT)
519 0x70u8, // SSI=0, private=1, reserved=0b11, upper nibble of section_length=0
520 0x05u8, // section_length = 5
521 // 5 bytes of "UTC time" payload
522 0xE0, 0x00, 0x00, 0x00, 0x00,
523 ];
524
525 let section = Section::parse(&buf).unwrap();
526 assert!(!section.section_syntax_indicator);
527 assert!(section.crc32.is_none());
528 // validate_crc on short-form should return Ok(()) vacuously.
529 section
530 .validate_crc(&buf)
531 .expect("short-form: no CRC to validate");
532 // Payload is the 5 bytes after the 3-byte header.
533 assert_eq!(section.payload(), &buf[3..]);
534 }
535}