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