use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};
pub const TABLE_ID: u8 = 0x7C;
pub const PID: u16 = 0x0000;
pub const FONT_INFO_TYPE_STYLE_WEIGHT: u8 = 0x00;
pub const FONT_INFO_TYPE_FILE_URI: u8 = 0x01;
pub const FONT_INFO_TYPE_FONT_SIZE: u8 = 0x02;
const HEADER_LEN: usize = 8;
const SECTION_LENGTH_PREFIX: usize = 3;
const CRC_LEN: usize = 4;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum FontInfo<'a> {
StyleWeight {
style: u8,
weight: u8,
},
FileUri {
format: u8,
uri: &'a [u8],
},
FontSize {
size: u16,
info: &'a [u8],
},
LengthDelimited {
font_info_type: u8,
info: &'a [u8],
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct DownloadableFontInfoSection<'a> {
pub font_id_extension: u16,
pub font_id: u8,
pub version_number: u8,
pub current_next_indicator: bool,
pub section_number: u8,
pub last_section_number: u8,
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),
});
}
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 => {
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;
}
_ => {
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, FontInfo::FileUri { uri, .. } => 1 + 2 + uri.len(), FontInfo::FontSize { info, .. } => 1 + 2 + 1 + info.len(), FontInfo::LengthDelimited { info, .. } => 1 + 1 + info.len(), })
.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;
buf[1] = 0xB0 | ((section_length >> 8) as u8 & 0x0F);
buf[2] = (section_length & 0xFF) as u8;
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());
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;
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;
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;
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::*;
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;
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
}
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, (2u8 << 5) | (2u8 << 1), FONT_INFO_TYPE_FILE_URI, 0x01, uri.len() as u8, ];
b.extend_from_slice(uri);
b.push(FONT_INFO_TYPE_FONT_SIZE);
b.extend_from_slice(&24u16.to_be_bytes());
b.push(2);
b.extend_from_slice(b"px");
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() {
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; 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() {
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();
let reparsed = DownloadableFontInfoSection::parse(&bytes).unwrap();
assert_eq!(serde_json::to_string(&reparsed).unwrap(), j);
assert!(j.contains("\"font_id\":1"));
}
}