use crate::descriptors::DescriptorLoop;
use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};
pub const TABLE_ID: u8 = 0x4B;
pub const PID: u16 = 0x0000;
const MIN_SECTION_LEN: usize = HEADER_LEN + FIXED_BODY_LEN + COMMON_DESC_LEN_FIELD + CRC_LEN;
const HEADER_LEN: usize = 3;
const FIXED_BODY_LEN: usize = 9;
const COMMON_DESC_LEN_FIELD: usize = 2;
const CRC_LEN: usize = 4;
const OFFSET_ACTION_TYPE: usize = HEADER_LEN;
const OFFSET_OUI_HASH: usize = HEADER_LEN + 1;
const OFFSET_FLAGS: usize = HEADER_LEN + 2;
const OFFSET_SECTION_NUMBER: usize = HEADER_LEN + 3;
const OFFSET_LAST_SECTION_NUMBER: usize = HEADER_LEN + 4;
const OFFSET_OUI: usize = HEADER_LEN + 5;
const OFFSET_PROCESSING_ORDER: usize = HEADER_LEN + 8;
const OFFSET_COMMON_DESC_LEN: usize = HEADER_LEN + FIXED_BODY_LEN;
const VERSION_NUMBER_MASK: u8 = 0x3E;
const VERSION_NUMBER_SHIFT: u8 = 1;
const CURRENT_NEXT_MASK: u8 = 0x01;
const LENGTH_HIGH_NIBBLE_MASK: u8 = 0x0F;
const FLAGS_RESERVED_BITS: u8 = 0xC0;
const SECTION_LEN_BYTE1_FLAGS: u8 = 0xB0;
const RESERVED_NIBBLE: u8 = 0xF0;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Unt<'a> {
pub action_type: u8,
pub oui_hash: u8,
pub version_number: u8,
pub current_next_indicator: bool,
pub section_number: u8,
pub last_section_number: u8,
pub oui: u32,
pub processing_order: u8,
pub common_descriptors: DescriptorLoop<'a>,
pub platform_loop: &'a [u8],
}
impl<'a> Parse<'a> for Unt<'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: "Unt",
});
}
if bytes[0] != TABLE_ID {
return Err(Error::UnexpectedTableId {
table_id: bytes[0],
what: "Unt",
expected: &[TABLE_ID],
});
}
let section_length =
(((bytes[1] & LENGTH_HIGH_NIBBLE_MASK) 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 action_type = bytes[OFFSET_ACTION_TYPE];
let oui_hash = bytes[OFFSET_OUI_HASH];
let flags_byte = bytes[OFFSET_FLAGS];
let version_number = (flags_byte & VERSION_NUMBER_MASK) >> VERSION_NUMBER_SHIFT;
let current_next_indicator = (flags_byte & CURRENT_NEXT_MASK) != 0;
let section_number = bytes[OFFSET_SECTION_NUMBER];
let last_section_number = bytes[OFFSET_LAST_SECTION_NUMBER];
let oui = ((bytes[OFFSET_OUI] as u32) << 16)
| ((bytes[OFFSET_OUI + 1] as u32) << 8)
| (bytes[OFFSET_OUI + 2] as u32);
let processing_order = bytes[OFFSET_PROCESSING_ORDER];
let cdl = (((bytes[OFFSET_COMMON_DESC_LEN] & LENGTH_HIGH_NIBBLE_MASK) as usize) << 8)
| bytes[OFFSET_COMMON_DESC_LEN + 1] as usize;
let common_desc_start = OFFSET_COMMON_DESC_LEN + COMMON_DESC_LEN_FIELD;
let common_desc_end = common_desc_start + cdl;
if common_desc_end > total - CRC_LEN {
return Err(Error::SectionLengthOverflow {
declared: cdl,
available: (total - CRC_LEN).saturating_sub(common_desc_start),
});
}
let common_descriptors = DescriptorLoop::new(&bytes[common_desc_start..common_desc_end]);
let platform_loop_start = common_desc_end;
let platform_loop_end = total - CRC_LEN;
let platform_loop = &bytes[platform_loop_start..platform_loop_end];
Ok(Unt {
action_type,
oui_hash,
version_number,
current_next_indicator,
section_number,
last_section_number,
oui,
processing_order,
common_descriptors,
platform_loop,
})
}
}
impl Serialize for Unt<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN
+ FIXED_BODY_LEN
+ COMMON_DESC_LEN_FIELD
+ self.common_descriptors.len()
+ self.platform_loop.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 - HEADER_LEN) as u16;
buf[0] = TABLE_ID;
buf[1] = SECTION_LEN_BYTE1_FLAGS | ((section_length >> 8) as u8 & LENGTH_HIGH_NIBBLE_MASK);
buf[2] = (section_length & 0xFF) as u8;
buf[OFFSET_ACTION_TYPE] = self.action_type;
buf[OFFSET_OUI_HASH] = self.oui_hash;
buf[OFFSET_FLAGS] = FLAGS_RESERVED_BITS
| ((self.version_number & 0x1F) << VERSION_NUMBER_SHIFT)
| u8::from(self.current_next_indicator);
buf[OFFSET_SECTION_NUMBER] = self.section_number;
buf[OFFSET_LAST_SECTION_NUMBER] = self.last_section_number;
buf[OFFSET_OUI] = ((self.oui >> 16) & 0xFF) as u8;
buf[OFFSET_OUI + 1] = ((self.oui >> 8) & 0xFF) as u8;
buf[OFFSET_OUI + 2] = (self.oui & 0xFF) as u8;
buf[OFFSET_PROCESSING_ORDER] = self.processing_order;
let cdl = self.common_descriptors.len() as u16;
buf[OFFSET_COMMON_DESC_LEN] =
RESERVED_NIBBLE | ((cdl >> 8) as u8 & LENGTH_HIGH_NIBBLE_MASK);
buf[OFFSET_COMMON_DESC_LEN + 1] = (cdl & 0xFF) as u8;
let common_start = OFFSET_COMMON_DESC_LEN + COMMON_DESC_LEN_FIELD;
let common_end = common_start + self.common_descriptors.len();
buf[common_start..common_end].copy_from_slice(self.common_descriptors.raw());
let plat_end = common_end + self.platform_loop.len();
buf[common_end..plat_end].copy_from_slice(self.platform_loop);
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 Unt<'a> {
const TABLE_ID: u8 = TABLE_ID;
const PID: u16 = PID;
}
impl<'a> crate::traits::TableDef<'a> for Unt<'a> {
const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
const NAME: &'static str = "UPDATE_NOTIFICATION";
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(clippy::too_many_arguments)]
fn build_unt(
action_type: u8,
oui_hash: u8,
version_number: u8,
current_next_indicator: bool,
section_number: u8,
last_section_number: u8,
oui: u32,
processing_order: u8,
common_descs: &[u8],
platform_loop: &[u8],
) -> Vec<u8> {
let section_length = FIXED_BODY_LEN
+ COMMON_DESC_LEN_FIELD
+ common_descs.len()
+ platform_loop.len()
+ CRC_LEN;
let mut v: Vec<u8> = Vec::with_capacity(HEADER_LEN + section_length);
v.push(TABLE_ID);
v.push(SECTION_LEN_BYTE1_FLAGS | ((section_length >> 8) as u8 & LENGTH_HIGH_NIBBLE_MASK));
v.push((section_length & 0xFF) as u8);
v.push(action_type);
v.push(oui_hash);
let flags = FLAGS_RESERVED_BITS
| ((version_number & 0x1F) << VERSION_NUMBER_SHIFT)
| u8::from(current_next_indicator);
v.push(flags);
v.push(section_number);
v.push(last_section_number);
v.push(((oui >> 16) & 0xFF) as u8);
v.push(((oui >> 8) & 0xFF) as u8);
v.push((oui & 0xFF) as u8);
v.push(processing_order);
let cdl = common_descs.len() as u16;
v.push(RESERVED_NIBBLE | ((cdl >> 8) as u8 & LENGTH_HIGH_NIBBLE_MASK));
v.push((cdl & 0xFF) as u8);
v.extend_from_slice(common_descs);
v.extend_from_slice(platform_loop);
v.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
v
}
#[test]
fn parse_happy_path() {
let oui: u32 = 0x00_01_5A;
let oui_hash: u8 = 0x01 ^ 0x5A;
let common_descs: &[u8] = &[0x66, 0x04, 0x00, 0x0A, 0x00, 0x00];
let bytes = build_unt(
0x01, oui_hash,
7, true, 0, 0, oui,
0x00, common_descs,
&[], );
let unt = Unt::parse(&bytes).expect("parse must succeed");
assert_eq!(unt.action_type, 0x01);
assert_eq!(unt.oui_hash, oui_hash);
assert_eq!(unt.version_number, 7);
assert!(unt.current_next_indicator);
assert_eq!(unt.section_number, 0);
assert_eq!(unt.last_section_number, 0);
assert_eq!(unt.oui, oui);
assert_eq!(unt.processing_order, 0x00);
assert_eq!(unt.common_descriptors.raw(), common_descs);
assert_eq!(unt.platform_loop, &[] as &[u8]);
}
#[test]
fn parse_current_next_false() {
let bytes = build_unt(0x01, 0x5B, 1, false, 1, 2, 0x00015A, 0x01, &[], &[]);
let unt = Unt::parse(&bytes).unwrap();
assert!(!unt.current_next_indicator);
assert_eq!(unt.section_number, 1);
assert_eq!(unt.last_section_number, 2);
}
#[test]
fn parse_preserves_platform_loop() {
let plat: &[u8] = &[0x00, 0x04, 0x00, 0x00, 0x00, 0x00];
let bytes = build_unt(0x01, 0x5B, 3, true, 0, 0, 0x00015A, 0xFF, &[], plat);
let unt = Unt::parse(&bytes).unwrap();
assert_eq!(unt.platform_loop, plat);
assert_eq!(unt.processing_order, 0xFF);
}
#[test]
fn parse_rejects_wrong_table_id() {
let mut bytes = build_unt(0x01, 0x5B, 0, true, 0, 0, 0x00015A, 0x00, &[], &[]);
bytes[0] = 0x4A; let err = Unt::parse(&bytes).unwrap_err();
assert!(
matches!(err, Error::UnexpectedTableId { table_id: 0x4A, .. }),
"expected UnexpectedTableId(0x4A), got {err:?}"
);
}
#[test]
fn parse_rejects_short_buffer() {
let err = Unt::parse(&[TABLE_ID, 0x00]).unwrap_err();
assert!(
matches!(err, Error::BufferTooShort { .. }),
"expected BufferTooShort, got {err:?}"
);
}
#[test]
fn serialize_rejects_small_output_buffer() {
let unt = Unt {
action_type: 0x01,
oui_hash: 0x5B,
version_number: 0,
current_next_indicator: true,
section_number: 0,
last_section_number: 0,
oui: 0x00015A,
processing_order: 0x00,
common_descriptors: DescriptorLoop::new(&[]),
platform_loop: &[],
};
let mut buf = vec![0u8; unt.serialized_len() - 1];
let err = unt.serialize_into(&mut buf).unwrap_err();
assert!(
matches!(err, Error::OutputBufferTooSmall { .. }),
"expected OutputBufferTooSmall, got {err:?}"
);
}
#[test]
fn serialize_round_trip() {
let common_descs: &[u8] = &[0x66, 0x04, 0x00, 0x0A, 0x00, 0x00];
let plat: &[u8] = &[0x00, 0x04, 0x00, 0x00, 0x00, 0x00];
let original = Unt {
action_type: 0x01,
oui_hash: 0x5B,
version_number: 15,
current_next_indicator: true,
section_number: 2,
last_section_number: 5,
oui: 0x00015A,
processing_order: 0x02,
common_descriptors: DescriptorLoop::new(common_descs),
platform_loop: plat,
};
let mut buf = vec![0u8; original.serialized_len()];
original
.serialize_into(&mut buf)
.expect("serialize must succeed");
let reparsed = Unt::parse(&buf).expect("reparse must succeed");
assert_eq!(original, reparsed);
}
}