Skip to main content

dvb_si/
section.rs

1//! Generic PSI/SI section framing — 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 [`crate::tables::tot::TotSection`].
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 dvb_common::crc32_mpeg2 as crc;
37use dvb_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 = &section_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 = &section_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 dvb_common::crc32_mpeg2::compute as crc32;
305
306    // ── Helper: build a minimal long-form section with correct CRC ───────────
307
308    /// Build a syntactically valid long-form section byte vector.
309    ///
310    /// Layout: [table_id, flags+len_hi, len_lo, ext_hi, ext_lo,
311    ///          ver_cni, sec_num, last_sec_num, ...payload..., crc(4)]
312    fn make_long_section(
313        table_id: u8,
314        extension_id: u16,
315        version: u8,
316        current_next: bool,
317        section_number: u8,
318        last_section_number: u8,
319        payload: &[u8],
320    ) -> Vec<u8> {
321        // section_length = 5 (extension header) + payload.len() + 4 (CRC)
322        let section_length: u16 = (5 + payload.len() + 4) as u16;
323
324        // reserved(2) | version(5) | current_next(1)
325        let ver_cni = 0xC0u8 | ((version & 0x1F) << 1) | (current_next as u8);
326        let mut buf: Vec<u8> = vec![
327            table_id,
328            // section_syntax_indicator=1, private_indicator=0, reserved=0b11, upper 4 bits of section_length
329            0x80 | 0x30 | ((section_length >> 8) as u8 & 0x0F),
330            (section_length & 0xFF) as u8,
331            (extension_id >> 8) as u8,
332            (extension_id & 0xFF) as u8,
333            ver_cni,
334            section_number,
335            last_section_number,
336        ];
337        buf.extend_from_slice(payload);
338
339        // Compute CRC over bytes so far, append as big-endian u32.
340        let crc = crc32(&buf);
341        buf.push((crc >> 24) as u8);
342        buf.push((crc >> 16) as u8);
343        buf.push((crc >> 8) as u8);
344        buf.push(crc as u8);
345
346        buf
347    }
348
349    // ── Test 1 ───────────────────────────────────────────────────────────────
350
351    #[test]
352    fn parse_rejects_buffer_shorter_than_3_bytes() {
353        for bad_len in [0usize, 1, 2] {
354            let buf = vec![0x00u8; bad_len];
355            let err = Section::parse(&buf).unwrap_err();
356            assert!(
357                matches!(err, Error::BufferTooShort { need: 3, have, .. } if have == bad_len),
358                "expected BufferTooShort for len={bad_len}, got {err:?}"
359            );
360        }
361    }
362
363    // ── Test 2 ───────────────────────────────────────────────────────────────
364
365    #[test]
366    fn parse_reads_table_id_syntax_indicator_and_length() {
367        // Construct a minimal 13-byte long-form section with no payload.
368        // section_length = 5 (extension header) + 0 (payload) + 4 (CRC) = 9
369        // BUT we need section_length >= min for a valid section: 9 → total = 12, which is 12 bytes, not 13.
370        // Let's use 1 byte of payload so section_length = 10, total = 13.
371        let raw = make_long_section(0x42, 0x1234, 3, true, 0, 0, &[0xAB]);
372        assert_eq!(raw.len(), 13);
373
374        let section = Section::parse(&raw).unwrap();
375        assert_eq!(section.table_id, 0x42);
376        assert!(section.section_syntax_indicator);
377        // section_length = 5 + 1 + 4 = 10
378        assert_eq!(section.section_length, 10);
379    }
380
381    // ── Test 3 ───────────────────────────────────────────────────────────────
382
383    #[test]
384    fn parse_rejects_when_section_length_exceeds_buffer() {
385        // Build a 3-byte header that claims section_length = 100 bytes of data.
386        // Buffer is only 3 bytes (just the header), so total = 103 > 3.
387        let buf = [
388            0x00u8, // table_id
389            0x80u8, // section_syntax_indicator=1, section_length upper nibble = 0
390            100u8,  // section_length lower byte = 100 → total = 103
391        ];
392        let err = Section::parse(&buf).unwrap_err();
393        assert!(
394            matches!(
395                err,
396                Error::SectionLengthOverflow {
397                    declared: 103,
398                    available: 3
399                }
400            ),
401            "expected SectionLengthOverflow, got {err:?}"
402        );
403    }
404
405    // ── Test 4 ───────────────────────────────────────────────────────────────
406
407    #[test]
408    fn parse_reads_extension_id_version_current_next_section_numbers() {
409        let raw = make_long_section(
410            0x02,          // PMT table_id
411            0xBEEF,        // extension_id / program_number
412            7,             // version_number
413            true,          // current_next
414            2,             // section_number
415            5,             // last_section_number
416            &[0x00, 0x00], // dummy payload
417        );
418
419        let section = Section::parse(&raw).unwrap();
420        assert_eq!(section.extension_id, 0xBEEF);
421        assert_eq!(section.version_number, 7);
422        assert!(section.current_next_indicator);
423        assert_eq!(section.section_number, 2);
424        assert_eq!(section.last_section_number, 5);
425    }
426
427    #[test]
428    fn parse_reads_current_next_indicator_false() {
429        // Same as test 4 but with current_next = false (bit 0 of byte 5 cleared).
430        let raw = make_long_section(
431            0x02,   // PMT table_id
432            0xBEEF, // extension_id
433            7,      // version_number
434            false,  // current_next — the field under test
435            2,      // section_number
436            5,      // last_section_number
437            &[0x00, 0x00],
438        );
439
440        let section = Section::parse(&raw).unwrap();
441        assert!(!section.current_next_indicator);
442        // Confirm the other fields are unaffected by flipping bit 0.
443        assert_eq!(section.extension_id, 0xBEEF);
444        assert_eq!(section.version_number, 7);
445        assert_eq!(section.section_number, 2);
446        assert_eq!(section.last_section_number, 5);
447    }
448
449    // ── Test 5 ───────────────────────────────────────────────────────────────
450
451    #[test]
452    fn payload_slice_excludes_header_and_crc() {
453        let inner_payload = &[0x01u8, 0x02, 0x03, 0x04, 0x05];
454        let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, inner_payload);
455
456        let section = Section::parse(&raw).unwrap();
457        assert_eq!(section.payload(), inner_payload);
458    }
459
460    // ── Test 6 ───────────────────────────────────────────────────────────────
461
462    #[test]
463    fn validate_crc_accepts_matching_crc32() {
464        let raw = make_long_section(0x00, 0x0001, 1, true, 0, 0, &[0xDE, 0xAD, 0xBE, 0xEF]);
465        let section = Section::parse(&raw).unwrap();
466        section.validate_crc(&raw).expect("CRC should match");
467    }
468
469    // ── Test 7 ───────────────────────────────────────────────────────────────
470
471    #[test]
472    fn validate_crc_rejects_flipped_bit() {
473        let mut raw = make_long_section(0x00, 0x0001, 1, true, 0, 0, &[0xDE, 0xAD, 0xBE, 0xEF]);
474        // Flip a bit inside the payload (byte 8 is the first payload byte) BEFORE
475        // parsing.  The 4-byte CRC at the tail of `raw` was computed over the
476        // original (un-flipped) bytes and is NOT updated here, so after the flip:
477        //   • `raw[..raw.len()-4]` contains corrupted data, and
478        //   • `raw[raw.len()-4..]` still holds the CRC of the original data.
479        // Parsing after the flip captures that old CRC into `section.crc32`.
480        // `validate_crc` then recomputes over the corrupted bytes and detects the
481        // mismatch — which is exactly the invariant we are testing.
482        // (Parsing before the flip would immutably borrow `raw` for the lifetime
483        // of `section` — the compiler would then reject `raw[8] ^= 0x01` because
484        // a mutable borrow conflicts with any live shared borrow.)
485        raw[8] ^= 0x01;
486
487        let section = Section::parse(&raw).unwrap();
488        let err = section.validate_crc(&raw).unwrap_err();
489        assert!(
490            matches!(err, Error::CrcMismatch { .. }),
491            "expected CrcMismatch, got {err:?}"
492        );
493    }
494
495    // ── Test (Fix 1 TDD) ─────────────────────────────────────────────────────
496
497    #[test]
498    fn validate_crc_rejects_raw_slice_shorter_than_crc_len() {
499        let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, &[0xDE, 0xAD]);
500        let section = Section::parse(&raw).unwrap();
501        // Pass an empty slice — shorter than CRC_LEN bytes.
502        let err = section.validate_crc(&[]).unwrap_err();
503        assert!(
504            matches!(err, Error::BufferTooShort { need: CRC_LEN, .. }),
505            "expected BufferTooShort(need=CRC_LEN), got {err:?}"
506        );
507    }
508
509    // ── Test 8 ───────────────────────────────────────────────────────────────
510
511    #[test]
512    fn short_form_section_has_no_crc() {
513        // TDT-style short-form section: section_syntax_indicator = 0.
514        // section_length = 5 (5 bytes of payload), total = 8 bytes.
515        let buf = [
516            0x70u8, // table_id (TDT)
517            0x70u8, // SSI=0, private=1, reserved=0b11, upper nibble of section_length=0
518            0x05u8, // section_length = 5
519            // 5 bytes of "UTC time" payload
520            0xE0, 0x00, 0x00, 0x00, 0x00,
521        ];
522
523        let section = Section::parse(&buf).unwrap();
524        assert!(!section.section_syntax_indicator);
525        assert!(section.crc32.is_none());
526        // validate_crc on short-form should return Ok(()) vacuously.
527        section
528            .validate_crc(&buf)
529            .expect("short-form: no CRC to validate");
530        // Payload is the 5 bytes after the 3-byte header.
531        assert_eq!(section.payload(), &buf[3..]);
532    }
533}