dvb-si 11.1.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
//! STD Descriptor — ISO/IEC 13818-1 §2.6.32 (tag 0x11).
//!
//! Indicates whether the leak method of buffer management is valid.

use super::descriptor_body;
use crate::error::{Error, Result};
use broadcast_common::{Parse, Serialize};

/// Descriptor tag for STD_descriptor.
pub const TAG: u8 = 0x11;
const HEADER_LEN: usize = 2;
/// `reserved(7)` ahead of `leak_valid_flag` (Table 2-70), emitted as ones
/// (r03-W7; confirmed by the TSDuck oracle PMT).
const RESERVED_7_BITS: u8 = 0xFE;
const BODY_LEN: u8 = 1;

/// STD Descriptor.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct StdDescriptor {
    /// Leak valid flag — 1 means the leak method is applicable.
    pub leak_valid_flag: bool,
}

impl<'a> Parse<'a> for StdDescriptor {
    type Error = crate::error::Error;

    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = descriptor_body(
            bytes,
            TAG,
            "StdDescriptor",
            "unexpected tag for STD_descriptor",
        )?;
        if body.len() != BODY_LEN as usize {
            return Err(Error::InvalidDescriptor {
                tag: TAG,
                reason: "STD_descriptor length must equal 1",
            });
        }
        let leak_valid_flag = (body[0] & 0x01) != 0;
        Ok(Self { leak_valid_flag })
    }
}

impl Serialize for StdDescriptor {
    type Error = crate::error::Error;

    fn serialized_len(&self) -> usize {
        HEADER_LEN + (BODY_LEN as usize)
    }

    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(),
            });
        }
        buf[0] = TAG;
        buf[1] = BODY_LEN;
        buf[HEADER_LEN] = RESERVED_7_BITS | self.leak_valid_flag as u8;
        Ok(len)
    }
}
impl crate::traits::DescriptorDef<'_> for StdDescriptor {
    const TAG: u8 = TAG;
    const NAME: &'static str = "STD";
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_leak_valid() {
        let bytes = [TAG, 1, 0b0000_0001];
        let d = StdDescriptor::parse(&bytes).unwrap();
        assert!(d.leak_valid_flag);
    }

    #[test]
    fn parse_leak_not_valid() {
        let bytes = [TAG, 1, 0b0000_0000];
        let d = StdDescriptor::parse(&bytes).unwrap();
        assert!(!d.leak_valid_flag);
    }

    #[test]
    fn serialize_round_trip() {
        let d = StdDescriptor {
            leak_valid_flag: true,
        };
        let mut buf = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut buf).unwrap();
        let reparsed = StdDescriptor::parse(&buf).unwrap();
        assert_eq!(d, reparsed);
    }

    #[test]
    fn parse_rejects_wrong_tag() {
        let err = StdDescriptor::parse(&[0x12, 1, 0]).unwrap_err();
        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x12, .. }));
    }

    #[test]
    fn parse_rejects_wrong_length() {
        let err = StdDescriptor::parse(&[TAG, 0]).unwrap_err();
        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
    }

    #[test]
    fn serialize_rejects_small_buffer() {
        let d = StdDescriptor {
            leak_valid_flag: false,
        };
        let mut tiny = vec![0u8; 2];
        let err = d.serialize_into(&mut tiny).unwrap_err();
        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
    }

    /// r03-W7 — H.222.0 Table 2-70 (ISO/IEC 13818-1:2021). Evidence for reserved=1 is the TSDuck-compiled oracle
    /// PMT `tsduck-mpeg-simple-pmt.bin` (TSDuck only; no real broadcast capture carries it). (1) conformant bytes (reserved=1) round-trip byte-exactly;
    /// (2) the same descriptor with reserved=0 (non-conformant sender) parses to
    /// equal typed fields and re-serializes to the normalized reserved=1 form.
    #[test]
    fn reserved_bits_one_on_serialize_and_zero_input_normalized() {
        let conformant: &[u8] = &[TAG, 1, 0xFF];
        let zeroed: &[u8] = &[TAG, 1, 0x01];
        let d = StdDescriptor::parse(conformant).unwrap();
        let mut out = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut out).unwrap();
        assert_eq!(out, conformant);

        let dz = StdDescriptor::parse(zeroed).unwrap();
        assert_eq!(dz, d, "typed fields must not depend on reserved bits");
        let mut outz = vec![0u8; dz.serialized_len()];
        dz.serialize_into(&mut outz).unwrap();
        assert_eq!(outz, conformant);
    }
}