use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};
pub const TABLE_ID: u8 = 0x77;
pub const PID: u16 = 0x0012;
const HEADER_LEN: usize = 3;
const EXTENSION_LEN: usize = 10;
const MIN_SECTION_LEN: usize = HEADER_LEN + EXTENSION_LEN + CRC_LEN;
const CRC_LEN: usize = 4;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Cit<'a> {
pub private_indicator: bool,
pub service_id: u16,
pub version_number: u8,
pub current_next_indicator: bool,
pub section_number: u8,
pub last_section_number: u8,
pub transport_stream_id: u16,
pub original_network_id: u16,
pub prepend_strings: &'a [u8],
pub crid_entries: &'a [u8],
}
impl<'a> Parse<'a> for Cit<'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: "Cit",
});
}
if bytes[0] != TABLE_ID {
return Err(Error::UnexpectedTableId {
table_id: bytes[0],
what: "Cit",
expected: &[TABLE_ID],
});
}
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,
});
}
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 = &bytes[ps_start..ps_end];
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,
})
}
}
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(),
});
}
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;
buf[0] = TABLE_ID;
buf[1] = 0x80
| (u8::from(self.private_indicator) << 6)
| 0x30 | ((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 crid_end = ps_end + self.crid_entries.len();
buf[ps_end..crid_end].copy_from_slice(self.crid_entries);
let crc = dvb_common::crc32_mpeg2::compute(&buf[..crid_end]);
buf[crid_end..len].copy_from_slice(&crc.to_be_bytes());
Ok(len)
}
}
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";
}
#[cfg(test)]
mod tests {
use super::*;
#[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() {
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() {
let prepend = b"crid://bbc.co.uk/\x00";
let mut crid_entries: Vec<u8> = Vec::new();
crid_entries.extend_from_slice(&0x0001u16.to_be_bytes()); crid_entries.push(0x00); let unique0 = b"ep1";
crid_entries.push(unique0.len() as u8); crid_entries.extend_from_slice(unique0);
crid_entries.extend_from_slice(&0x0002u16.to_be_bytes());
crid_entries.push(0xFF); 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; assert!(matches!(
Cit::parse(&bytes).unwrap_err(),
Error::UnexpectedTableId { table_id: 0x40, .. }
));
}
#[test]
fn parse_rejects_buffer_too_short() {
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, &[], &[]);
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]; assert!(matches!(
cit.serialize_into(&mut buf).unwrap_err(),
Error::OutputBufferTooSmall { .. }
));
}
}