dvb-si 3.1.1

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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Downloadable Font Information Section (DFIS) — ETSI EN 303 560 v1.1.1
//! §5.3.2.3.1 (table_id 0x7C).
//!
//! The DFIS conveys download location and font metadata for a single font or
//! font family. Sections of a Downloadable Font Information Table (DFIT) are
//! carried together on one PID, signalled in the PMT by a `data_broadcast_id`
//! descriptor with `data_broadcast_id` 0x000D (§5.3.2.3.1) — there is no
//! well-known PID, so [`PID`] follows the `dsmcc.rs` "no fixed PID" convention.
//!
//! ## table_id
//!
//! EN 303 560 v1.1.1 §5.3.2.3.1 says `table_id` 0x4C — an acknowledged
//! allocation accident: 0x4C was already the INT. EN 300 468 V1.19.1 Table 2
//! NOTE 2 ("table_id 0x4C was previously accidentally assigned to both of
//! these two DVB specifications, this has now been corrected") reassigns the
//! DFIS to **0x7C**, which is what [`TABLE_ID`] and the crate registry use.
//!
//! ## The 0x02 conditional (resolved against the PDF, pp. 30-31)
//!
//! Table 22's syntax has two consecutive conditionals that both fire for
//! `font_info_type == 0x02`:
//!
//! ```text
//! if (font_info_type == 0x02) { font_size (16) }
//! if (font_info_type >= 0x02) { font_info_length (8) + text_char ... }
//! ```
//!
//! This is **not** a typo and **not** double-handling: a type-0x02 entry
//! carries the 16-bit `font_size` *followed by* the length-prefixed string
//! block. Every `font_info_type >= 0x02` is length-delimited by
//! `font_info_length` (the 0x02 case additionally prefixes the 2-byte
//! `font_size`). This makes types 0x03 (`font_family`) and all reserved types
//! 0x04..=0xFF safely skippable, so they round-trip as
//! [`FontInfo::LengthDelimited`]. Verified against EN 303 560 v1.1.1 PDF
//! pp. 30-31 (Table 22) and p. 32 (Table 23 type allocation).
//!
//! Per crate contract this parser does NOT verify CRC_32 (use
//! `Section::validate_crc`). Reserved bits are ignored on parse; spec-mandated
//! zero fields (`font_id_extension`, `reserved_zero_future_use`) are emitted 0.

use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};

/// table_id for the DFIS — the crate registry value (see module docs re. spec 0x4C).
pub const TABLE_ID: u8 = 0x7C;
/// DFIS has no well-known PID; carried on the PID signalled by the
/// `data_broadcast_id` (0x000D) descriptor in the PMT (§5.3.2.3.1).
pub const PID: u16 = 0x0000;

/// `font_info_type` for style/weight (§5.3.2.3.2.1 Table 23).
pub const FONT_INFO_TYPE_STYLE_WEIGHT: u8 = 0x00;
/// `font_info_type` for a font file URI (Table 23).
pub const FONT_INFO_TYPE_FILE_URI: u8 = 0x01;
/// `font_info_type` for font size in pixels (Table 23).
pub const FONT_INFO_TYPE_FONT_SIZE: u8 = 0x02;

/// table_id(1) + section_length(2) + font_id_extension/font_id(2)
/// + version/cni(1) + section_number(1) + last_section_number(1) = 8-byte header.
const HEADER_LEN: usize = 8;
/// `section_length` counts from just after the field (byte 3) to end of section.
const SECTION_LENGTH_PREFIX: usize = 3;
/// CRC_32 trailer.
const CRC_LEN: usize = 4;

/// One entry in the DFIS font_info loop (§5.3.2.3.1 Table 22).
///
/// Variant is selected by `font_info_type` (Table 23). Reserved types
/// (0x04..=0xFF) are length-delimited and round-trip via [`FontInfo::LengthDelimited`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum FontInfo<'a> {
    /// `font_info_type == 0x00`: style(3) + weight(4) + reserved(1).
    StyleWeight {
        /// `font_style` (§5.3.2.3.2.2 Table 24): 0 undefined, 1 normal, 2 italic, 3 oblique.
        style: u8,
        /// `font_weight` (Table 25): 0 undefined, 1 normal, 2 bold.
        weight: u8,
    },
    /// `font_info_type == 0x01`: reserved(4) + font_file_format(4) + uri_length(8) + uri.
    FileUri {
        /// `font_file_format` (§5.3.2.3.2.3 Table 26): 0 = OFF, 1 = WOFF.
        format: u8,
        /// DVB URI string (UTF-8), `uri_length` bytes.
        uri: &'a [u8],
    },
    /// `font_info_type == 0x02`: font_size(16) followed by the length-delimited block.
    FontSize {
        /// `font_size` — font height in pixels.
        size: u16,
        /// `text_char` block following `font_info_length` (UTF-8).
        info: &'a [u8],
    },
    /// `font_info_type >= 0x03` (incl. reserved): font_info_length(8) + text_char block.
    LengthDelimited {
        /// The `font_info_type` byte as parsed (0x03 = font_family, else reserved).
        font_info_type: u8,
        /// `text_char` block, `font_info_length` bytes (UTF-8 for defined types).
        info: &'a [u8],
    },
}

/// Downloadable Font Information Section (EN 303 560 §5.3.2.3.1, Table 22).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct DownloadableFontInfoSection<'a> {
    /// 9-bit `font_id_extension` — spec-mandated all-zero; together with
    /// `font_id` forms the 16-bit table_id_extension.
    pub font_id_extension: u16,
    /// 7-bit `font_id` identifying the sub_table (one font/family).
    pub font_id: u8,
    /// 5-bit version_number.
    pub version_number: u8,
    /// current_next_indicator bit.
    pub current_next_indicator: bool,
    /// section_number.
    pub section_number: u8,
    /// last_section_number.
    pub last_section_number: u8,
    /// font_info loop entries in wire order.
    pub font_info: Vec<FontInfo<'a>>,
}

impl<'a> Parse<'a> for DownloadableFontInfoSection<'a> {
    type Error = crate::error::Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let min_len = HEADER_LEN + CRC_LEN;
        if bytes.len() < min_len {
            return Err(Error::BufferTooShort {
                need: min_len,
                have: bytes.len(),
                what: "DownloadableFontInfoSection",
            });
        }
        if bytes[0] != TABLE_ID {
            return Err(Error::UnexpectedTableId {
                table_id: bytes[0],
                what: "DownloadableFontInfoSection",
                expected: &[TABLE_ID],
            });
        }
        let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
        let total = SECTION_LENGTH_PREFIX + section_length;
        if bytes.len() < total || total < HEADER_LEN + CRC_LEN {
            return Err(Error::SectionLengthOverflow {
                declared: section_length,
                available: bytes.len().saturating_sub(SECTION_LENGTH_PREFIX),
            });
        }

        // bytes[3..5] = font_id_extension(9) | font_id(7).
        let id_word = u16::from_be_bytes([bytes[3], bytes[4]]);
        let font_id_extension = id_word >> 7;
        let font_id = (id_word & 0x7F) as u8;
        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 loop_end = total - CRC_LEN;
        let mut font_info = Vec::new();
        let mut pos = HEADER_LEN;
        while pos < loop_end {
            let font_info_type = bytes[pos];
            pos += 1;
            match font_info_type {
                FONT_INFO_TYPE_STYLE_WEIGHT => {
                    if pos + 1 > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: 1,
                            available: loop_end - pos,
                        });
                    }
                    let b = bytes[pos];
                    pos += 1;
                    font_info.push(FontInfo::StyleWeight {
                        style: b >> 5,
                        weight: (b >> 1) & 0x0F,
                    });
                }
                FONT_INFO_TYPE_FILE_URI => {
                    if pos + 2 > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: 2,
                            available: loop_end - pos,
                        });
                    }
                    let format = bytes[pos] & 0x0F;
                    let uri_length = bytes[pos + 1] as usize;
                    let uri_start = pos + 2;
                    let uri_end = uri_start + uri_length;
                    if uri_end > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: uri_length,
                            available: loop_end - uri_start,
                        });
                    }
                    font_info.push(FontInfo::FileUri {
                        format,
                        uri: &bytes[uri_start..uri_end],
                    });
                    pos = uri_end;
                }
                FONT_INFO_TYPE_FONT_SIZE => {
                    // font_size(16) then font_info_length(8) + block (Table 22, type >= 0x02).
                    if pos + 3 > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: 3,
                            available: loop_end - pos,
                        });
                    }
                    let size = u16::from_be_bytes([bytes[pos], bytes[pos + 1]]);
                    let info_length = bytes[pos + 2] as usize;
                    let info_start = pos + 3;
                    let info_end = info_start + info_length;
                    if info_end > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: info_length,
                            available: loop_end - info_start,
                        });
                    }
                    font_info.push(FontInfo::FontSize {
                        size,
                        info: &bytes[info_start..info_end],
                    });
                    pos = info_end;
                }
                _ => {
                    // font_info_type >= 0x03: font_info_length(8) + text_char block.
                    if pos + 1 > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: 1,
                            available: loop_end - pos,
                        });
                    }
                    let info_length = bytes[pos] as usize;
                    let info_start = pos + 1;
                    let info_end = info_start + info_length;
                    if info_end > loop_end {
                        return Err(Error::SectionLengthOverflow {
                            declared: info_length,
                            available: loop_end - info_start,
                        });
                    }
                    font_info.push(FontInfo::LengthDelimited {
                        font_info_type,
                        info: &bytes[info_start..info_end],
                    });
                    pos = info_end;
                }
            }
        }

        Ok(DownloadableFontInfoSection {
            font_id_extension,
            font_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            font_info,
        })
    }
}

impl Serialize for DownloadableFontInfoSection<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        let loop_bytes: usize = self
            .font_info
            .iter()
            .map(|f| match f {
                FontInfo::StyleWeight { .. } => 2, // type + 1 packed byte
                FontInfo::FileUri { uri, .. } => 1 + 2 + uri.len(), // type + (fmt|len) + uri
                FontInfo::FontSize { info, .. } => 1 + 2 + 1 + info.len(), // type + size + len + info
                FontInfo::LengthDelimited { info, .. } => 1 + 1 + info.len(), // type + len + info
            })
            .sum();
        HEADER_LEN + loop_bytes + 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(),
            });
        }
        let section_length = (len - SECTION_LENGTH_PREFIX) as u16;
        buf[0] = TABLE_ID;
        // section_syntax_indicator=1, reserved_future_use=0, reserved=11, section_length hi nibble.
        buf[1] = 0xB0 | ((section_length >> 8) as u8 & 0x0F);
        buf[2] = (section_length & 0xFF) as u8;
        // font_id_extension(9) | font_id(7); spec mandates extension all-zero.
        let id_word = ((self.font_id_extension & 0x01FF) << 7) | (self.font_id as u16 & 0x7F);
        buf[3..5].copy_from_slice(&id_word.to_be_bytes());
        // reserved(2)=11, version_number(5), current_next_indicator(1).
        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;

        // 8-bit length prefixes error on over-range payloads rather than
        // silently truncating (the crate's strict serialize idiom).
        let guard_u8 = |len: usize| -> Result<()> {
            if len > u8::MAX as usize {
                return Err(Error::SectionLengthOverflow {
                    declared: len,
                    available: u8::MAX as usize,
                });
            }
            Ok(())
        };

        let mut pos = HEADER_LEN;
        for f in &self.font_info {
            match f {
                FontInfo::StyleWeight { style, weight } => {
                    buf[pos] = FONT_INFO_TYPE_STYLE_WEIGHT;
                    // style(3) | weight(4) | reserved_zero_future_use(1)=0.
                    buf[pos + 1] = ((style & 0x07) << 5) | ((weight & 0x0F) << 1);
                    pos += 2;
                }
                FontInfo::FileUri { format, uri } => {
                    guard_u8(uri.len())?;
                    buf[pos] = FONT_INFO_TYPE_FILE_URI;
                    // reserved_zero_future_use(4)=0 | font_file_format(4).
                    buf[pos + 1] = format & 0x0F;
                    buf[pos + 2] = uri.len() as u8;
                    let s = pos + 3;
                    buf[s..s + uri.len()].copy_from_slice(uri);
                    pos = s + uri.len();
                }
                FontInfo::FontSize { size, info } => {
                    guard_u8(info.len())?;
                    buf[pos] = FONT_INFO_TYPE_FONT_SIZE;
                    buf[pos + 1..pos + 3].copy_from_slice(&size.to_be_bytes());
                    buf[pos + 3] = info.len() as u8;
                    let s = pos + 4;
                    buf[s..s + info.len()].copy_from_slice(info);
                    pos = s + info.len();
                }
                FontInfo::LengthDelimited {
                    font_info_type,
                    info,
                } => {
                    guard_u8(info.len())?;
                    buf[pos] = *font_info_type;
                    buf[pos + 1] = info.len() as u8;
                    let s = pos + 2;
                    buf[s..s + info.len()].copy_from_slice(info);
                    pos = s + info.len();
                }
            }
        }

        let crc = dvb_common::crc32_mpeg2::compute(&buf[..pos]);
        buf[pos..len].copy_from_slice(&crc.to_be_bytes());
        Ok(len)
    }
}

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

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

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

    /// Wrap a font_info loop body in the 8-byte common header + placeholder CRC.
    fn build_section(font_id: u8, version: u8, loop_body: &[u8]) -> Vec<u8> {
        let section_length =
            (HEADER_LEN - SECTION_LENGTH_PREFIX + loop_body.len() + CRC_LEN) as u16;
        // font_id_extension = 0 (spec-mandated), font_id in low 7 bits.
        let id_word = (font_id as u16) & 0x7F;
        let mut v = vec![
            TABLE_ID,
            0xB0 | ((section_length >> 8) as u8 & 0x0F),
            (section_length & 0xFF) as u8,
            (id_word >> 8) as u8,
            (id_word & 0xFF) as u8,
            0xC0 | (version << 1) | 0x01,
            0x00,
            0x00,
        ];
        v.extend_from_slice(loop_body);
        v.extend_from_slice(&[0, 0, 0, 0]);
        v
    }

    /// Build a font_info loop with one of each variant.
    fn mixed_loop() -> Vec<u8> {
        let uri = b"https://f.example/Droid.otf";
        let family = b"Droid Sans";
        let mut b = vec![
            FONT_INFO_TYPE_STYLE_WEIGHT, // type 0x00
            (2u8 << 5) | (2u8 << 1),     // style=2 (italic), weight=2 (bold)
            FONT_INFO_TYPE_FILE_URI,     // type 0x01
            0x01,                        // format=1 (WOFF)
            uri.len() as u8,             // uri_length
        ];
        b.extend_from_slice(uri);
        // type 0x02 — font_size=24, info block "px"
        b.push(FONT_INFO_TYPE_FONT_SIZE);
        b.extend_from_slice(&24u16.to_be_bytes());
        b.push(2);
        b.extend_from_slice(b"px");
        // type 0x03 — font_family
        b.push(0x03);
        b.push(family.len() as u8);
        b.extend_from_slice(family);
        b
    }

    #[test]
    fn parse_header_fields() {
        let bytes = build_section(0x42, 9, &[]);
        let sec = DownloadableFontInfoSection::parse(&bytes).unwrap();
        assert_eq!(sec.font_id, 0x42);
        assert_eq!(sec.font_id_extension, 0);
        assert_eq!(sec.version_number, 9);
        assert!(sec.current_next_indicator);
        assert!(sec.font_info.is_empty());
    }

    #[test]
    fn parse_all_variants() {
        let bytes = build_section(1, 0, &mixed_loop());
        let sec = DownloadableFontInfoSection::parse(&bytes).unwrap();
        assert_eq!(sec.font_info.len(), 4);
        assert_eq!(
            sec.font_info[0],
            FontInfo::StyleWeight {
                style: 2,
                weight: 2
            }
        );
        match &sec.font_info[1] {
            FontInfo::FileUri { format, uri } => {
                assert_eq!(*format, 1);
                assert_eq!(*uri, b"https://f.example/Droid.otf");
            }
            other => panic!("expected FileUri, got {other:?}"),
        }
        match &sec.font_info[2] {
            FontInfo::FontSize { size, info } => {
                assert_eq!(*size, 24);
                assert_eq!(*info, b"px");
            }
            other => panic!("expected FontSize, got {other:?}"),
        }
        match &sec.font_info[3] {
            FontInfo::LengthDelimited {
                font_info_type,
                info,
            } => {
                assert_eq!(*font_info_type, 0x03);
                assert_eq!(*info, b"Droid Sans");
            }
            other => panic!("expected LengthDelimited, got {other:?}"),
        }
    }

    #[test]
    fn reserved_type_round_trips_as_length_delimited() {
        // type 0x77 (reserved) is length-delimited and skippable.
        let mut body = vec![0x77u8, 0x03];
        body.extend_from_slice(&[0xAA, 0xBB, 0xCC]);
        let bytes = build_section(1, 0, &body);
        let sec = DownloadableFontInfoSection::parse(&bytes).unwrap();
        assert_eq!(
            sec.font_info[0],
            FontInfo::LengthDelimited {
                font_info_type: 0x77,
                info: &[0xAA, 0xBB, 0xCC]
            }
        );
    }

    #[test]
    fn parse_rejects_wrong_tag() {
        let mut bytes = build_section(1, 0, &mixed_loop());
        bytes[0] = 0x4C; // INT table_id
        assert!(matches!(
            DownloadableFontInfoSection::parse(&bytes).unwrap_err(),
            Error::UnexpectedTableId { table_id: 0x4C, .. }
        ));
    }

    #[test]
    fn rejects_short_buffer() {
        assert!(matches!(
            DownloadableFontInfoSection::parse(&[0x7C, 0xB0]).unwrap_err(),
            Error::BufferTooShort {
                what: "DownloadableFontInfoSection",
                ..
            }
        ));
    }

    #[test]
    fn uri_length_overflow_rejected() {
        // type 0x01, uri_length 0x20 but no uri bytes present.
        let body = vec![FONT_INFO_TYPE_FILE_URI, 0x01, 0x20];
        let bytes = build_section(1, 0, &body);
        assert!(matches!(
            DownloadableFontInfoSection::parse(&bytes).unwrap_err(),
            Error::SectionLengthOverflow { .. }
        ));
    }

    #[test]
    fn round_trip_all_variants() {
        let bytes = build_section(0x33, 4, &mixed_loop());
        let sec = DownloadableFontInfoSection::parse(&bytes).unwrap();
        let mut buf = vec![0u8; sec.serialized_len()];
        sec.serialize_into(&mut buf).unwrap();
        let re = DownloadableFontInfoSection::parse(&buf).unwrap();
        assert_eq!(sec, re);
    }

    #[test]
    fn table_trait_constants() {
        assert_eq!(<DownloadableFontInfoSection as Table>::TABLE_ID, 0x7C);
        assert_eq!(<DownloadableFontInfoSection as Table>::PID, 0x0000);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn serde_json_round_trip() {
        let bytes = build_section(1, 0, &mixed_loop());
        let sec = DownloadableFontInfoSection::parse(&bytes).unwrap();
        let j = serde_json::to_string(&sec).unwrap();
        // The borrowed `uri`/`info` `&[u8]` fields cannot be JSON-deserialized
        // zero-copy (serde_json renders them as number sequences, not borrowed
        // byte arrays) — the crate-wide constraint affecting every
        // borrowed-slice table (cf. mpe.rs). Exercise the derive through the
        // WIRE form: a re-parse must serialize to byte-identical JSON.
        let reparsed = DownloadableFontInfoSection::parse(&bytes).unwrap();
        assert_eq!(serde_json::to_string(&reparsed).unwrap(), j);
        assert!(j.contains("\"font_id\":1"));
    }
}