use super::{CRC_LEN, SectionBits};
use crate::descriptors::DescriptorLoop;
use crate::error::{Error, Result};
use broadcast_core::len::need_out;
const MIN_HEADER_LEN: usize = 3;
const EXTENSION_HEADER_LEN: usize = 5;
const MIN_SECTION_LEN: usize = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + CRC_LEN;
pub(super) struct Parsed<'a> {
pub(super) table_id_extension: u16,
pub(super) version_number: u8,
pub(super) current_next_indicator: bool,
pub(super) section_number: u8,
pub(super) last_section_number: u8,
pub(super) descriptors: DescriptorLoop<'a>,
pub(super) bits: SectionBits,
}
pub(super) struct View<'v, 'a> {
pub(super) table_id_extension: u16,
pub(super) version_number: u8,
pub(super) current_next_indicator: bool,
pub(super) section_number: u8,
pub(super) last_section_number: u8,
pub(super) descriptors: &'v DescriptorLoop<'a>,
pub(super) bits: &'v SectionBits,
}
pub(super) fn parse<'a>(
bytes: &'a [u8],
expected: &'static [u8],
what: &'static str,
ext_what: &'static str,
) -> Result<Parsed<'a>> {
if bytes.len() < MIN_SECTION_LEN {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(MIN_SECTION_LEN, bytes.len(), what),
));
}
if !expected.contains(&bytes[0]) {
return Err(Error::UnexpectedTableId {
table_id: bytes[0],
what,
expected,
});
}
let total = super::check_section_length(
bytes.len(),
MIN_HEADER_LEN,
super::section_length_of(bytes),
MIN_SECTION_LEN,
)?;
let desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN;
Ok(Parsed {
table_id_extension: crate::wire::be_u16(bytes, super::TABLE_ID_EXTENSION_OFFSET, ext_what)?,
version_number: super::version_number_of(bytes[super::VERSION_FLAGS_OFFSET]),
current_next_indicator: super::current_next_of(bytes[super::VERSION_FLAGS_OFFSET]),
section_number: bytes[super::SECTION_NUMBER_OFFSET],
last_section_number: bytes[super::LAST_SECTION_NUMBER_OFFSET],
descriptors: DescriptorLoop::new(&bytes[desc_start..total - CRC_LEN]),
bits: SectionBits::parse(bytes[1], Some(bytes[super::VERSION_FLAGS_OFFSET])),
})
}
pub(super) fn serialized_len(descriptors: &DescriptorLoop<'_>) -> usize {
MIN_HEADER_LEN + EXTENSION_HEADER_LEN + descriptors.len() + CRC_LEN
}
pub(super) fn serialize_into(table_id: u8, view: &View<'_, '_>, buf: &mut [u8]) -> Result<usize> {
let len = serialized_len(view.descriptors);
need_out(buf, len, "serialize_into")?;
buf[0] = table_id;
buf[1] = view.bits.b1()?;
super::write_section_length(buf, len - MIN_HEADER_LEN)?;
buf[3..5].copy_from_slice(&view.table_id_extension.to_be_bytes());
buf[super::VERSION_FLAGS_OFFSET] = view
.bits
.b5(view.version_number, view.current_next_indicator)?;
buf[super::SECTION_NUMBER_OFFSET] = view.section_number;
buf[super::LAST_SECTION_NUMBER_OFFSET] = view.last_section_number;
let desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN;
buf[desc_start..desc_start + view.descriptors.len()].copy_from_slice(view.descriptors.raw());
broadcast_common::crc32_mpeg2::seal(buf, len - CRC_LEN)?;
Ok(len)
}