use crate::descriptors::DescriptorLoop;
use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};
pub const TABLE_ID: u8 = 0x4C;
pub const PID: u16 = 0x0000;
pub const ACTION_TYPE_STREAM_ANNOUNCEMENT: u8 = 0x01;
const OUTER_HEADER_LEN: usize = 3;
const INT_FIXED_LEN: usize = 9;
const LOOP_LEN_FIELD: usize = 2;
const CRC_LEN: usize = 4;
const MIN_SECTION_LEN: usize = OUTER_HEADER_LEN + INT_FIXED_LEN + LOOP_LEN_FIELD + CRC_LEN;
const OFF_ACTION_TYPE: usize = 3;
const OFF_PLATFORM_ID_HASH: usize = 4;
const OFF_VERSION_BYTE: usize = 5;
const OFF_SECTION_NUMBER: usize = 6;
const OFF_LAST_SECTION_NUMBER: usize = 7;
const OFF_PLATFORM_ID: usize = 8;
const OFF_PROCESSING_ORDER: usize = 11;
const OFF_PLATFORM_DESC_LEN: usize = 12;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Int<'a> {
pub action_type: u8,
pub platform_id_hash: u8,
pub version_number: u8,
pub current_next_indicator: bool,
pub section_number: u8,
pub last_section_number: u8,
pub platform_id: u32,
pub processing_order: u8,
pub platform_descriptors: DescriptorLoop<'a>,
pub loops: &'a [u8],
}
impl<'a> Parse<'a> for Int<'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: "Int",
});
}
if bytes[0] != TABLE_ID {
return Err(Error::UnexpectedTableId {
table_id: bytes[0],
what: "Int",
expected: &[TABLE_ID],
});
}
let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
let total = OUTER_HEADER_LEN + section_length;
if bytes.len() < total {
return Err(Error::SectionLengthOverflow {
declared: section_length,
available: bytes.len() - OUTER_HEADER_LEN,
});
}
let action_type = bytes[OFF_ACTION_TYPE];
let platform_id_hash = bytes[OFF_PLATFORM_ID_HASH];
let version_byte = bytes[OFF_VERSION_BYTE];
let version_number = (version_byte >> 1) & 0x1F;
let current_next_indicator = (version_byte & 0x01) != 0;
let section_number = bytes[OFF_SECTION_NUMBER];
let last_section_number = bytes[OFF_LAST_SECTION_NUMBER];
let platform_id = ((bytes[OFF_PLATFORM_ID] as u32) << 16)
| ((bytes[OFF_PLATFORM_ID + 1] as u32) << 8)
| bytes[OFF_PLATFORM_ID + 2] as u32;
let processing_order = bytes[OFF_PROCESSING_ORDER];
let plat_desc_len = (((bytes[OFF_PLATFORM_DESC_LEN] & 0x0F) as usize) << 8)
| bytes[OFF_PLATFORM_DESC_LEN + 1] as usize;
let plat_desc_start = OFF_PLATFORM_DESC_LEN + LOOP_LEN_FIELD;
let plat_desc_end = plat_desc_start + plat_desc_len;
if plat_desc_end > total - CRC_LEN {
return Err(Error::SectionLengthOverflow {
declared: plat_desc_len,
available: (total - CRC_LEN).saturating_sub(plat_desc_start),
});
}
let platform_descriptors = DescriptorLoop::new(&bytes[plat_desc_start..plat_desc_end]);
let loops_start = plat_desc_end;
let loops_end = total - CRC_LEN;
let loops = &bytes[loops_start..loops_end];
Ok(Int {
action_type,
platform_id_hash,
version_number,
current_next_indicator,
section_number,
last_section_number,
platform_id,
processing_order,
platform_descriptors,
loops,
})
}
}
impl Serialize for Int<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
OUTER_HEADER_LEN
+ INT_FIXED_LEN
+ LOOP_LEN_FIELD + self.platform_descriptors.len()
+ self.loops.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(),
});
}
let section_length = (len - OUTER_HEADER_LEN) as u16;
buf[0] = TABLE_ID;
buf[1] = 0xF0 | ((section_length >> 8) as u8 & 0x0F);
buf[2] = (section_length & 0xFF) as u8;
buf[OFF_ACTION_TYPE] = self.action_type;
buf[OFF_PLATFORM_ID_HASH] = self.platform_id_hash;
buf[OFF_VERSION_BYTE] =
0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
buf[OFF_SECTION_NUMBER] = self.section_number;
buf[OFF_LAST_SECTION_NUMBER] = self.last_section_number;
buf[OFF_PLATFORM_ID] = ((self.platform_id >> 16) & 0xFF) as u8;
buf[OFF_PLATFORM_ID + 1] = ((self.platform_id >> 8) & 0xFF) as u8;
buf[OFF_PLATFORM_ID + 2] = (self.platform_id & 0xFF) as u8;
buf[OFF_PROCESSING_ORDER] = self.processing_order;
let pdl = self.platform_descriptors.len() as u16;
buf[OFF_PLATFORM_DESC_LEN] = 0xF0 | ((pdl >> 8) as u8 & 0x0F);
buf[OFF_PLATFORM_DESC_LEN + 1] = (pdl & 0xFF) as u8;
let plat_start = OFF_PLATFORM_DESC_LEN + LOOP_LEN_FIELD;
let plat_end = plat_start + self.platform_descriptors.len();
buf[plat_start..plat_end].copy_from_slice(self.platform_descriptors.raw());
let loops_start = plat_end;
let loops_end = loops_start + self.loops.len();
buf[loops_start..loops_end].copy_from_slice(self.loops);
let crc_pos = len - CRC_LEN;
let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
Ok(len)
}
}
impl<'a> Table<'a> for Int<'a> {
const TABLE_ID: u8 = TABLE_ID;
const PID: u16 = PID;
}
impl<'a> crate::traits::TableDef<'a> for Int<'a> {
const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
const NAME: &'static str = "IP_MAC_NOTIFICATION";
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(clippy::too_many_arguments)]
fn build_int(
action_type: u8,
platform_id_hash: u8,
version_number: u8,
current_next_indicator: bool,
section_number: u8,
last_section_number: u8,
platform_id: u32,
processing_order: u8,
platform_desc: &[u8],
loops: &[u8],
) -> Vec<u8> {
let int = Int {
action_type,
platform_id_hash,
version_number,
current_next_indicator,
section_number,
last_section_number,
platform_id,
processing_order,
platform_descriptors: DescriptorLoop::new(platform_desc),
loops,
};
let mut buf = vec![0u8; int.serialized_len()];
int.serialize_into(&mut buf).unwrap();
buf
}
#[test]
fn parse_happy_path() {
let bytes = build_int(
ACTION_TYPE_STREAM_ANNOUNCEMENT,
0x12 ^ 0x34, 3,
true,
0,
0,
0x00_12_34,
0x00,
&[0x81, 0x02, 0xAB, 0xCD],
&[],
);
let int = Int::parse(&bytes).unwrap();
assert_eq!(int.action_type, ACTION_TYPE_STREAM_ANNOUNCEMENT);
assert_eq!(int.platform_id_hash, 0x12 ^ 0x34);
assert_eq!(int.version_number, 3);
assert!(int.current_next_indicator);
assert_eq!(int.section_number, 0);
assert_eq!(int.last_section_number, 0);
assert_eq!(int.platform_id, 0x00_12_34);
assert_eq!(int.processing_order, 0x00);
assert_eq!(
int.platform_descriptors.raw(),
&[0x81, 0x02, 0xAB, 0xCD][..]
);
assert_eq!(int.loops, &[] as &[u8]);
}
#[test]
fn parse_happy_path_with_loops() {
let fake_loops: [u8; 4] = [
0xF0, 0x00, 0xF0, 0x00, ];
let bytes = build_int(
0x01,
0x56,
5,
false,
1,
1,
0x00_56_78,
0x01,
&[],
&fake_loops,
);
let int = Int::parse(&bytes).unwrap();
assert_eq!(int.platform_id, 0x00_56_78);
assert_eq!(int.version_number, 5);
assert!(!int.current_next_indicator);
assert_eq!(int.loops, &fake_loops[..]);
}
#[test]
fn parse_rejects_wrong_table_id() {
let mut bytes = build_int(0x01, 0x00, 0, true, 0, 0, 0x000001, 0x00, &[], &[]);
bytes[0] = 0x4B; let err = Int::parse(&bytes).unwrap_err();
assert!(matches!(
err,
Error::UnexpectedTableId { table_id: 0x4B, .. }
));
}
#[test]
fn parse_rejects_buffer_too_short() {
let err = Int::parse(&[TABLE_ID, 0xF0]).unwrap_err();
assert!(matches!(err, Error::BufferTooShort { what: "Int", .. }));
}
#[test]
fn serialize_round_trip() {
let plat_desc = [0x7C, 0x04, 0x01, 0x02, 0x03, 0x04];
let fake_loops: [u8; 4] = [0xF0, 0x00, 0xF0, 0x00];
let bytes = build_int(
ACTION_TYPE_STREAM_ANNOUNCEMENT,
0xAB,
15,
true,
2,
3,
0x00_AB_CD,
0x00,
&plat_desc,
&fake_loops,
);
let int = Int::parse(&bytes).unwrap();
let mut buf = vec![0u8; int.serialized_len()];
int.serialize_into(&mut buf).unwrap();
let re = Int::parse(&buf).unwrap();
assert_eq!(int, re);
assert_eq!(re.action_type, ACTION_TYPE_STREAM_ANNOUNCEMENT);
assert_eq!(re.platform_id_hash, 0xAB);
assert_eq!(re.version_number, 15);
assert!(re.current_next_indicator);
assert_eq!(re.section_number, 2);
assert_eq!(re.last_section_number, 3);
assert_eq!(re.platform_id, 0x00_AB_CD);
assert_eq!(re.processing_order, 0x00);
assert_eq!(re.platform_descriptors.raw(), &plat_desc[..]);
assert_eq!(re.loops, &fake_loops[..]);
}
#[test]
fn serialize_rejects_too_small_output_buffer() {
let int = Int {
action_type: 0x01,
platform_id_hash: 0x00,
version_number: 0,
current_next_indicator: true,
section_number: 0,
last_section_number: 0,
platform_id: 0,
processing_order: 0,
platform_descriptors: DescriptorLoop::new(&[]),
loops: &[],
};
let mut buf = vec![0u8; 2]; let err = int.serialize_into(&mut buf).unwrap_err();
assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
}
#[test]
fn platform_id_24bit_boundary() {
let bytes = build_int(0x01, 0xFF, 0, true, 0, 0, 0x00FF_FFFF, 0x00, &[], &[]);
let int = Int::parse(&bytes).unwrap();
assert_eq!(int.platform_id, 0x00FF_FFFF);
}
}