dvb-si 3.1.2

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
//! 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 + raw CRID entry loop + CRC-32.

use crate::error::{Error, Result};
use crate::traits::Table;
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;

// ── length constants ──────────────────────────────────────────────────────────

/// Bytes 0-2: table_id (1) + flags+section_length (2).
const HEADER_LEN: usize = 3;

/// Bytes 3-12: service_id(2) + flags+version+cni(1) + section_number(1)
/// + last_section_number(1) + transport_stream_id(2) + original_network_id(2)
/// + prepend_strings_length(1).
const EXTENSION_LEN: usize = 10;

/// Minimum total encoded length: header + extension + CRC.
const MIN_SECTION_LEN: usize = HEADER_LEN + EXTENSION_LEN + CRC_LEN;

/// Bytes occupied by the trailing CRC-32 field.
const CRC_LEN: usize = 4;

// ── struct ────────────────────────────────────────────────────────────────────

/// Content Identifier Table (ETSI TS 102 323 v1.4.1 §12.2).
///
/// Variable-length fields are kept as raw byte slices borrowing from the source
/// buffer. Callers that need to iterate CRID entries may walk `crid_entries`
/// directly per the wire format:
///
/// ```text
/// for each entry:
///   crid_ref             (2 bytes, u16 big-endian)
///   prepend_string_index (1 byte)
///   unique_string_length (1 byte)
///   unique_string_bytes  (unique_string_length bytes)
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Cit<'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,

    /// Raw prepend-string block. The wire `prepend_strings_length` byte is
    /// derived from `prepend_strings.len()` on serialize (≤ 255). Entries are
    /// null-terminated ASCII/DVB-text fragments; addressed by index from the
    /// CRID loop.
    pub prepend_strings: &'a [u8],

    /// Raw CRID entry loop (everything between the prepend-string block and the
    /// CRC-32). Walk per the format documented on the struct.
    pub crid_entries: &'a [u8],
}

// ── Parse ─────────────────────────────────────────────────────────────────────

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

    fn parse(bytes: &'a [u8]) -> Result<Self> {
        // Minimum-length guard.
        if bytes.len() < MIN_SECTION_LEN {
            return Err(Error::BufferTooShort {
                need: MIN_SECTION_LEN,
                have: bytes.len(),
                what: "Cit",
            });
        }

        // table_id check.
        if bytes[0] != TABLE_ID {
            return Err(Error::UnexpectedTableId {
                table_id: bytes[0],
                what: "Cit",
                expected: &[TABLE_ID],
            });
        }

        // section_length: lower 4 bits of byte 1 || byte 2 (12 bits total).
        let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
        let total = HEADER_LEN + section_length;
        if bytes.len() < total {
            return Err(Error::SectionLengthOverflow {
                declared: section_length,
                available: bytes.len() - HEADER_LEN,
            });
        }

        // private_indicator: bit 6 of byte 1 (section_syntax_indicator is bit 7).
        let private_indicator = (bytes[1] & 0x40) != 0;

        // Extension header (bytes 3..13).
        let service_id = u16::from_be_bytes([bytes[3], bytes[4]]);
        // byte 5: reserved(2) | version_number(5) | current_next_indicator(1)
        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];

        // Prepend-string block.
        let ps_start = HEADER_LEN + EXTENSION_LEN;
        let ps_end = ps_start + prepend_strings_length as usize;

        // Ensure prepend_strings block fits before the CRC.
        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 = &bytes[ps_start..ps_end];

        // Raw CRID entry loop: everything between prepend_strings and the CRC.
        let crid_entries = &bytes[ps_end..payload_end];

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

// ── Serialize ─────────────────────────────────────────────────────────────────

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

    fn serialized_len(&self) -> usize {
        HEADER_LEN + EXTENSION_LEN + self.prepend_strings.len() + self.crid_entries.len() + 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(),
            });
        }

        // prepend_strings_length is an 8-bit wire field, derived from the slice.
        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;

        // Byte 0: table_id.
        buf[0] = TABLE_ID;

        // Byte 1: section_syntax_indicator(1) | private_indicator(1) | reserved(2) | section_length[11:8](4).
        // section_syntax_indicator = 1 (long-form section per DVB convention).
        buf[1] = 0x80
            | (u8::from(self.private_indicator) << 6)
            | 0x30 // reserved bits set
            | ((section_length >> 8) as u8 & 0x0F);

        // Byte 2: section_length[7:0].
        buf[2] = (section_length & 0xFF) as u8;

        // Extension header.
        buf[3..5].copy_from_slice(&self.service_id.to_be_bytes());
        buf[5] = 0xC0 // reserved(2) = 11
            | ((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;

        // Prepend strings.
        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);

        // CRID entries.
        let crid_end = ps_end + self.crid_entries.len();
        buf[ps_end..crid_end].copy_from_slice(self.crid_entries);

        // CRC-32: compute over everything up to (but not including) the CRC slot.
        let crc = dvb_common::crc32_mpeg2::compute(&buf[..crid_end]);
        buf[crid_end..len].copy_from_slice(&crc.to_be_bytes());

        Ok(len)
    }
}

// ── Table impl ────────────────────────────────────────────────────────────────

impl<'a> Table<'a> for Cit<'a> {
    const TABLE_ID: u8 = TABLE_ID;
    const PID: u16 = PID;
}

impl<'a> crate::traits::TableDef<'a> for Cit<'a> {
    const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
    const NAME: &'static str = "CONTENT_IDENTIFIER";
}

// ── tests ─────────────────────────────────────────────────────────────────────

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

    /// Build a syntactically valid CIT section from its constituent fields.
    ///
    /// `prepend_strings` and `crid_entries` are raw byte slices; CRC is zeroed
    /// (matching the serializer convention).
    #[allow(clippy::too_many_arguments)]
    fn build_cit(
        service_id: u16,
        version: u8,
        current_next: bool,
        section_number: u8,
        last_section_number: u8,
        transport_stream_id: u16,
        original_network_id: u16,
        prepend_strings: &[u8],
        crid_entries: &[u8],
    ) -> Vec<u8> {
        let cit = Cit {
            private_indicator: false,
            service_id,
            version_number: version,
            current_next_indicator: current_next,
            section_number,
            last_section_number,
            transport_stream_id,
            original_network_id,
            prepend_strings,
            crid_entries,
        };
        let mut buf = vec![0u8; cit.serialized_len()];
        cit.serialize_into(&mut buf).unwrap();
        buf
    }

    #[test]
    fn parse_happy_path_no_crid_entries() {
        // A CIT section with no prepend strings and no CRID entries is the
        // minimal valid form; commonly seen on transponders that carry the CIT
        // structure but have no current programme mapping.
        let prepend = b"CRID://example.com\x00";
        let bytes = build_cit(0x1234, 3, true, 0, 0, 0x0064, 0x0002, prepend, &[]);
        let cit = Cit::parse(&bytes).unwrap();

        assert_eq!(cit.service_id, 0x1234);
        assert_eq!(cit.version_number, 3);
        assert!(cit.current_next_indicator);
        assert_eq!(cit.section_number, 0);
        assert_eq!(cit.last_section_number, 0);
        assert_eq!(cit.transport_stream_id, 0x0064);
        assert_eq!(cit.original_network_id, 0x0002);
        assert_eq!(cit.prepend_strings, prepend);
        assert_eq!(cit.crid_entries, &[] as &[u8]);
    }

    #[test]
    fn parse_happy_path_with_crid_entries() {
        // Two synthetic CRID entries:
        //   entry 0: crid_ref=0x0001, prepend_string_index=0x00, unique="ep1"
        //   entry 1: crid_ref=0x0002, prepend_string_index=0xFF (full CRID in unique), unique="crid://bbc.co.uk/EV-1"
        let prepend = b"crid://bbc.co.uk/\x00";
        let mut crid_entries: Vec<u8> = Vec::new();
        // Entry 0
        crid_entries.extend_from_slice(&0x0001u16.to_be_bytes()); // crid_ref
        crid_entries.push(0x00); // prepend_string_index
        let unique0 = b"ep1";
        crid_entries.push(unique0.len() as u8); // unique_string_length
        crid_entries.extend_from_slice(unique0);
        // Entry 1
        crid_entries.extend_from_slice(&0x0002u16.to_be_bytes());
        crid_entries.push(0xFF); // no prepend
        let unique1 = b"crid://bbc.co.uk/EV-1";
        crid_entries.push(unique1.len() as u8);
        crid_entries.extend_from_slice(unique1);

        let bytes = build_cit(
            0xABCD,
            7,
            true,
            1,
            3,
            0x01F4,
            0x0028,
            prepend,
            &crid_entries,
        );
        let cit = Cit::parse(&bytes).unwrap();

        assert_eq!(cit.service_id, 0xABCD);
        assert_eq!(cit.version_number, 7);
        assert_eq!(cit.section_number, 1);
        assert_eq!(cit.last_section_number, 3);
        assert_eq!(cit.transport_stream_id, 0x01F4);
        assert_eq!(cit.original_network_id, 0x0028);
        assert_eq!(cit.prepend_strings, prepend);
        assert_eq!(cit.crid_entries, crid_entries.as_slice());
    }

    #[test]
    fn parse_rejects_wrong_table_id() {
        let mut bytes = build_cit(0x0001, 0, true, 0, 0, 0x0001, 0x0001, &[], &[]);
        bytes[0] = 0x40; // Not 0x77.
        assert!(matches!(
            Cit::parse(&bytes).unwrap_err(),
            Error::UnexpectedTableId { table_id: 0x40, .. }
        ));
    }

    #[test]
    fn parse_rejects_buffer_too_short() {
        // Hand-craft a buffer that is shorter than MIN_SECTION_LEN.
        let short = [TABLE_ID, 0x00];
        assert!(matches!(
            Cit::parse(&short).unwrap_err(),
            Error::BufferTooShort { .. }
        ));
    }

    #[test]
    fn parse_rejects_section_length_overflow() {
        let mut bytes = build_cit(0x0001, 0, true, 0, 0, 0x0001, 0x0001, &[], &[]);
        // Inflate the declared section_length to exceed actual buffer size.
        let fake_sl: u16 = (bytes.len() as u16) + 100 - HEADER_LEN as u16;
        bytes[1] = (bytes[1] & 0xF0) | ((fake_sl >> 8) as u8 & 0x0F);
        bytes[2] = (fake_sl & 0xFF) as u8;
        assert!(matches!(
            Cit::parse(&bytes).unwrap_err(),
            Error::SectionLengthOverflow { .. }
        ));
    }

    #[test]
    fn serialize_round_trip() {
        let prepend = b"crid://example.com/\x00";
        let crid_entries = {
            let mut v: Vec<u8> = Vec::new();
            v.extend_from_slice(&0x0042u16.to_be_bytes());
            v.push(0x00);
            let unique = b"episode42";
            v.push(unique.len() as u8);
            v.extend_from_slice(unique);
            v
        };

        let original = Cit {
            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: &crid_entries,
        };

        let mut buf = vec![0u8; original.serialized_len()];
        original.serialize_into(&mut buf).unwrap();
        let parsed = Cit::parse(&buf).unwrap();

        assert_eq!(parsed.private_indicator, original.private_indicator);
        assert_eq!(parsed.service_id, original.service_id);
        assert_eq!(parsed.version_number, original.version_number);
        assert_eq!(
            parsed.current_next_indicator,
            original.current_next_indicator
        );
        assert_eq!(parsed.section_number, original.section_number);
        assert_eq!(parsed.last_section_number, original.last_section_number);
        assert_eq!(parsed.transport_stream_id, original.transport_stream_id);
        assert_eq!(parsed.original_network_id, original.original_network_id);
        assert_eq!(parsed.prepend_strings, original.prepend_strings);
        assert_eq!(parsed.crid_entries, original.crid_entries);
    }

    #[test]
    fn serialize_rejects_output_buffer_too_small() {
        let cit = Cit {
            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: &[],
            crid_entries: &[],
        };
        let mut buf = vec![0u8; 2]; // Far too small.
        assert!(matches!(
            cit.serialize_into(&mut buf).unwrap_err(),
            Error::OutputBufferTooSmall { .. }
        ));
    }
}