dvb-si 6.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Content Identifier Table — ETSI TS 102 323 v1.4.1 §12.2.
//!
//! The CIT maps content reference identifiers (CRIDs) to events for a given
//! service. Carried on PID 0x0012 (shared with the EIT) with table_id 0x77.
//! Structure: fixed header + prepend-string block + typed CRID entry loop + CRC-32.
//!
//! The CRID entry loop is unfolded into [`CridEntry`] instances (Table 119,
//! §12.2). The prepend-string block is a flat byte array of null-terminated
//! fragments addressed by index; it is kept raw (`&[u8]`) since each entry is
//! just a single byte — there is no per-entry sub-structure to unfold.

use crate::error::{Error, Result};
use crate::text::DvbText;
use dvb_common::{Parse, Serialize};

/// `table_id` for Content Identifier Table.
pub const TABLE_ID: u8 = 0x77;

/// PID on which CIT sections are carried.
///
/// Note: PID 0x0012 is shared with the EIT family; demultiplexers must filter
/// by table_id in addition to PID to isolate CIT sections.
pub const PID: u16 = 0x0012;

const HEADER_LEN: usize = 3;
const EXTENSION_LEN: usize = 10;
const CRC_LEN: usize = 4;
const MIN_SECTION_LEN: usize = HEADER_LEN + EXTENSION_LEN + CRC_LEN;

const CRID_REF_LEN: usize = 2;
const CRID_ENTRY_FIXED_LEN: usize = CRID_REF_LEN + 1 + 1;

/// A single CRID entry in the CIT loop (Table 119, §12.2).
///
/// Wire layout: `crid_ref(16) | prepend_string_index(8) | unique_string_length(8)
/// | unique_string_byte[8]×unique_string_length`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CridEntry<'a> {
    /// `crid_ref` — 16-bit reference into the CRID resolution system.
    pub crid_ref: u16,
    /// `prepend_string_index` — index into the prepend-string block.
    /// `0xFF` means no prepend string (the unique string is the full CRID).
    pub prepend_string_index: u8,
    /// `unique_string` — the unique portion of the CRID, decoded as DVB text.
    pub unique_string: DvbText<'a>,
}

/// Content Identifier Table (ETSI TS 102 323 v1.4.1 §12.2, Table 119).
///
/// The `crid_entries` loop is unfolded into typed [`CridEntry`] instances.
/// The `prepend_strings` block is kept as a raw byte slice (flat array of
/// null-terminated fragments with no per-entry sub-structure to type).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct CitSection<'a> {
    /// `private_indicator` bit from byte 1.
    pub private_indicator: bool,
    /// `service_id` — identifies the container this section belongs to
    /// (table_id_extension, bytes 3-4).
    pub service_id: u16,
    /// 5-bit `version_number`.
    pub version_number: u8,
    /// `current_next_indicator` bit.
    pub current_next_indicator: bool,
    /// Section counter within the sub-table.
    pub section_number: u8,
    /// Final section number in the sub-table.
    pub last_section_number: u8,
    /// `transport_stream_id` of the carrying TS.
    pub transport_stream_id: u16,
    /// `original_network_id` of the originating network.
    pub original_network_id: u16,
    /// Prepend-string block (null-terminated fragments addressed by index),
    /// decoded as DVB text.
    pub prepend_strings: DvbText<'a>,
    /// CRID entry loop — unfolded per Table 119.
    pub crid_entries: Vec<CridEntry<'a>>,
}

impl<'a> CitSection<'a> {
    /// Resolve a prepend string by its `prepend_string_index`.
    ///
    /// Returns `None` if `index` is out of range. The block is a sequence of
    /// null-terminated fragments; index 0 is the first fragment, index 1 the
    /// second, etc. The returned slice includes everything up to (but not
    /// including) the terminating NUL byte; an empty slice means the index
    /// points to an empty or immediately-terminated fragment.
    pub fn prepend_string(&self, index: u8) -> Option<&[u8]> {
        let raw: &[u8] = &self.prepend_strings;
        let mut remaining: &[u8] = raw;
        let mut current: u8 = 0;
        while !remaining.is_empty() {
            let nul_pos = remaining
                .iter()
                .position(|&b| b == 0)
                .unwrap_or(remaining.len());
            let fragment = &remaining[..nul_pos];
            if current == index {
                return Some(fragment);
            }
            current += 1;
            remaining = if nul_pos < remaining.len() {
                &remaining[nul_pos + 1..]
            } else {
                &[]
            };
        }
        None
    }
}

fn crid_entry_serialized_len(e: &CridEntry) -> usize {
    CRID_ENTRY_FIXED_LEN + e.unique_string.len()
}

impl<'a> Parse<'a> for CitSection<'a> {
    type Error = crate::error::Error;

    fn parse(bytes: &'a [u8]) -> Result<Self> {
        if bytes.len() < MIN_SECTION_LEN {
            return Err(Error::BufferTooShort {
                need: MIN_SECTION_LEN,
                have: bytes.len(),
                what: "CitSection",
            });
        }
        if bytes[0] != TABLE_ID {
            return Err(Error::UnexpectedTableId {
                table_id: bytes[0],
                what: "CitSection",
                expected: &[TABLE_ID],
            });
        }

        let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
        let total =
            super::check_section_length(bytes.len(), HEADER_LEN, section_length, MIN_SECTION_LEN)?;

        let private_indicator = (bytes[1] & 0x40) != 0;
        let service_id = u16::from_be_bytes([bytes[3], bytes[4]]);
        let version_number = (bytes[5] >> 1) & 0x1F;
        let current_next_indicator = (bytes[5] & 0x01) != 0;
        let section_number = bytes[6];
        let last_section_number = bytes[7];
        let transport_stream_id = u16::from_be_bytes([bytes[8], bytes[9]]);
        let original_network_id = u16::from_be_bytes([bytes[10], bytes[11]]);
        let prepend_strings_length = bytes[12];

        let ps_start = HEADER_LEN + EXTENSION_LEN;
        let ps_end = ps_start + prepend_strings_length as usize;
        let payload_end = total - CRC_LEN;
        if ps_end > payload_end {
            return Err(Error::SectionLengthOverflow {
                declared: prepend_strings_length as usize,
                available: payload_end.saturating_sub(ps_start),
            });
        }
        let prepend_strings = DvbText::new(&bytes[ps_start..ps_end]);

        let mut pos = ps_end;
        let mut crid_entries = Vec::new();
        while pos < payload_end {
            if pos + CRID_ENTRY_FIXED_LEN > payload_end {
                return Err(Error::BufferTooShort {
                    need: pos + CRID_ENTRY_FIXED_LEN,
                    have: payload_end,
                    what: "CitSection crid_entry",
                });
            }
            let crid_ref = u16::from_be_bytes([bytes[pos], bytes[pos + 1]]);
            let prepend_string_index = bytes[pos + 2];
            let unique_string_length = bytes[pos + 3] as usize;
            pos += CRID_ENTRY_FIXED_LEN;
            if pos + unique_string_length > payload_end {
                return Err(Error::BufferTooShort {
                    need: pos + unique_string_length,
                    have: payload_end,
                    what: "CitSection unique_string",
                });
            }
            let unique_string = DvbText::new(&bytes[pos..pos + unique_string_length]);
            pos += unique_string_length;
            crid_entries.push(CridEntry {
                crid_ref,
                prepend_string_index,
                unique_string,
            });
        }

        Ok(CitSection {
            private_indicator,
            service_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            transport_stream_id,
            original_network_id,
            prepend_strings,
            crid_entries,
        })
    }
}

impl Serialize for CitSection<'_> {
    type Error = crate::error::Error;

    fn serialized_len(&self) -> usize {
        HEADER_LEN
            + EXTENSION_LEN
            + self.prepend_strings.len()
            + self
                .crid_entries
                .iter()
                .map(crid_entry_serialized_len)
                .sum::<usize>()
            + CRC_LEN
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        if buf.len() < len {
            return Err(Error::OutputBufferTooSmall {
                need: len,
                have: buf.len(),
            });
        }
        if self.prepend_strings.len() > u8::MAX as usize {
            return Err(Error::SectionLengthOverflow {
                declared: self.prepend_strings.len(),
                available: u8::MAX as usize,
            });
        }

        let section_length = (len - HEADER_LEN) as u16;
        if section_length > 0x0FFF {
            return Err(Error::SectionLengthOverflow {
                declared: section_length as usize,
                available: 0x0FFF,
            });
        }
        buf[0] = TABLE_ID;
        buf[1] = super::SECTION_B1_SSI
            | (u8::from(self.private_indicator) << 6)
            | super::SECTION_B1_RESERVED_HI
            | ((section_length >> 8) as u8 & 0x0F);
        buf[2] = (section_length & 0xFF) as u8;

        buf[3..5].copy_from_slice(&self.service_id.to_be_bytes());
        buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
        buf[6] = self.section_number;
        buf[7] = self.last_section_number;
        buf[8..10].copy_from_slice(&self.transport_stream_id.to_be_bytes());
        buf[10..12].copy_from_slice(&self.original_network_id.to_be_bytes());
        buf[12] = self.prepend_strings.len() as u8;

        let ps_start = HEADER_LEN + EXTENSION_LEN;
        let ps_end = ps_start + self.prepend_strings.len();
        buf[ps_start..ps_end].copy_from_slice(&self.prepend_strings);

        let mut pos = ps_end;
        for entry in &self.crid_entries {
            buf[pos..pos + 2].copy_from_slice(&entry.crid_ref.to_be_bytes());
            buf[pos + 2] = entry.prepend_string_index;
            buf[pos + 3] = entry.unique_string.len() as u8;
            pos += CRID_ENTRY_FIXED_LEN;
            buf[pos..pos + entry.unique_string.len()].copy_from_slice(&entry.unique_string);
            pos += entry.unique_string.len();
        }

        let crc = dvb_common::crc32_mpeg2::compute(&buf[..pos]);
        buf[pos..pos + CRC_LEN].copy_from_slice(&crc.to_be_bytes());
        Ok(len)
    }
}
impl<'a> crate::traits::TableDef<'a> for CitSection<'a> {
    const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
    const NAME: &'static str = "CONTENT_IDENTIFIER";
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_happy_path_no_crid_entries() {
        let prepend = DvbText::new(b"CRID://example.com\x00");
        let cit = CitSection {
            private_indicator: false,
            service_id: 0x1234,
            version_number: 3,
            current_next_indicator: true,
            section_number: 0,
            last_section_number: 0,
            transport_stream_id: 0x0064,
            original_network_id: 0x0002,
            prepend_strings: prepend,
            crid_entries: Vec::new(),
        };
        let mut buf = vec![0u8; cit.serialized_len()];
        cit.serialize_into(&mut buf).unwrap();
        let parsed = CitSection::parse(&buf).unwrap();
        assert_eq!(parsed.service_id, 0x1234);
        assert_eq!(parsed.version_number, 3);
        assert!(parsed.current_next_indicator);
        assert_eq!(parsed.prepend_strings, prepend);
        assert!(parsed.crid_entries.is_empty());
    }

    #[test]
    fn parse_happy_path_with_crid_entries() {
        let prepend = DvbText::new(b"crid://bbc.co.uk/\x00");
        let entries = vec![
            CridEntry {
                crid_ref: 0x0001,
                prepend_string_index: 0x00,
                unique_string: DvbText::new(b"ep1"),
            },
            CridEntry {
                crid_ref: 0x0002,
                prepend_string_index: 0xFF,
                unique_string: DvbText::new(b"crid://bbc.co.uk/EV-1"),
            },
        ];
        let cit = CitSection {
            private_indicator: false,
            service_id: 0xABCD,
            version_number: 7,
            current_next_indicator: true,
            section_number: 1,
            last_section_number: 3,
            transport_stream_id: 0x01F4,
            original_network_id: 0x0028,
            prepend_strings: prepend,
            crid_entries: entries,
        };
        let mut buf = vec![0u8; cit.serialized_len()];
        cit.serialize_into(&mut buf).unwrap();
        let parsed = CitSection::parse(&buf).unwrap();
        assert_eq!(parsed.service_id, 0xABCD);
        assert_eq!(parsed.crid_entries.len(), 2);
        assert_eq!(parsed.crid_entries[0].crid_ref, 0x0001);
        assert_eq!(parsed.crid_entries[0].prepend_string_index, 0x00);
        assert_eq!(parsed.crid_entries[0].unique_string, DvbText::new(b"ep1"));
        assert_eq!(parsed.crid_entries[1].crid_ref, 0x0002);
        assert_eq!(parsed.crid_entries[1].prepend_string_index, 0xFF);
        assert_eq!(
            parsed.crid_entries[1].unique_string,
            DvbText::new(b"crid://bbc.co.uk/EV-1")
        );
    }

    #[test]
    fn byte_exact_round_trip() {
        let prepend = DvbText::new(b"crid://example.com/\x00");
        let entries = vec![CridEntry {
            crid_ref: 0x0042,
            prepend_string_index: 0x00,
            unique_string: DvbText::new(b"episode42"),
        }];
        let original = CitSection {
            private_indicator: true,
            service_id: 0x4321,
            version_number: 15,
            current_next_indicator: false,
            section_number: 2,
            last_section_number: 4,
            transport_stream_id: 0x03E8,
            original_network_id: 0x0050,
            prepend_strings: prepend,
            crid_entries: entries,
        };
        let mut buf = vec![0u8; original.serialized_len()];
        original.serialize_into(&mut buf).unwrap();
        let parsed = CitSection::parse(&buf).unwrap();
        let mut buf2 = vec![0u8; parsed.serialized_len()];
        parsed.serialize_into(&mut buf2).unwrap();
        assert_eq!(buf, buf2, "byte-exact re-serialize");
        assert_eq!(parsed.crid_entries.len(), 1);
        assert_eq!(parsed.crid_entries[0].crid_ref, 0x0042);
        assert_eq!(
            parsed.crid_entries[0].unique_string,
            DvbText::new(b"episode42")
        );
    }

    #[test]
    fn parse_rejects_wrong_table_id() {
        let cit = CitSection {
            private_indicator: false,
            service_id: 0x0001,
            version_number: 0,
            current_next_indicator: true,
            section_number: 0,
            last_section_number: 0,
            transport_stream_id: 0x0001,
            original_network_id: 0x0001,
            prepend_strings: DvbText::new(&[]),
            crid_entries: Vec::new(),
        };
        let mut buf = vec![0u8; cit.serialized_len()];
        cit.serialize_into(&mut buf).unwrap();
        buf[0] = 0x40;
        assert!(matches!(
            CitSection::parse(&buf).unwrap_err(),
            Error::UnexpectedTableId { table_id: 0x40, .. }
        ));
    }

    #[test]
    fn parse_rejects_buffer_too_short() {
        assert!(matches!(
            CitSection::parse(&[TABLE_ID, 0x00]).unwrap_err(),
            Error::BufferTooShort { .. }
        ));
    }

    #[test]
    fn parse_rejects_truncated_crid_entry() {
        let prepend = DvbText::new(&[]);
        let cit = CitSection {
            private_indicator: false,
            service_id: 0x0001,
            version_number: 0,
            current_next_indicator: true,
            section_number: 0,
            last_section_number: 0,
            transport_stream_id: 0x0001,
            original_network_id: 0x0001,
            prepend_strings: prepend,
            crid_entries: Vec::new(),
        };
        let mut buf = vec![0u8; cit.serialized_len()];
        cit.serialize_into(&mut buf).unwrap();
        let mut truncated = buf.clone();
        truncated.truncate(buf.len() - 2);
        let sl = (truncated.len() - HEADER_LEN) as u16;
        truncated[1] = (truncated[1] & 0xF0) | ((sl >> 8) as u8 & 0x0F);
        truncated[2] = (sl & 0xFF) as u8;
        assert!(CitSection::parse(&truncated).is_err());
    }

    #[test]
    fn serialize_rejects_output_buffer_too_small() {
        let cit = CitSection {
            private_indicator: false,
            service_id: 0x0001,
            version_number: 0,
            current_next_indicator: true,
            section_number: 0,
            last_section_number: 0,
            transport_stream_id: 0x0001,
            original_network_id: 0x0001,
            prepend_strings: DvbText::new(&[]),
            crid_entries: Vec::new(),
        };
        let mut buf = vec![0u8; 2];
        assert!(matches!(
            cit.serialize_into(&mut buf).unwrap_err(),
            Error::OutputBufferTooSmall { .. }
        ));
    }

    #[test]
    fn parse_rejects_zero_section_length() {
        let mut buf = vec![0u8; 64];
        buf[0] = TABLE_ID;
        buf[1] = 0xF0;
        buf[2] = 0x00;
        for b in &mut buf[3..] {
            *b = 0xFF;
        }
        assert!(matches!(
            CitSection::parse(&buf).unwrap_err(),
            Error::SectionLengthOverflow { .. }
        ));
    }

    #[test]
    fn parse_handwritten_cit_no_entries() {
        let mut bytes: Vec<u8> = vec![
            0x77, 0xF0, 0x0E, 0x12, 0x34, 0xC7, 0x00, 0x00, 0x00, 0x64, 0x00, 0x02, 0x00,
        ];
        let crc = dvb_common::crc32_mpeg2::compute(&bytes);
        bytes.extend_from_slice(&crc.to_be_bytes());
        let cit = CitSection::parse(&bytes).unwrap();
        assert_eq!(cit.service_id, 0x1234);
        assert_eq!(cit.transport_stream_id, 0x0064);
        assert!(cit.crid_entries.is_empty());
    }

    #[test]
    fn prepend_string_resolver() {
        let cit = CitSection {
            private_indicator: false,
            service_id: 0x0001,
            version_number: 0,
            current_next_indicator: true,
            section_number: 0,
            last_section_number: 0,
            transport_stream_id: 0x0001,
            original_network_id: 0x0001,
            prepend_strings: DvbText::new(b"crid://example.com/\x00crid://other.com/\x00"),
            crid_entries: Vec::new(),
        };
        assert_eq!(cit.prepend_string(0), Some(&b"crid://example.com/"[..]));
        assert_eq!(cit.prepend_string(1), Some(&b"crid://other.com/"[..]));
        assert_eq!(cit.prepend_string(2), None);
    }
}