dvb-si 17.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
//! Bit-level reader/writer shared by the Rec. ITU-T H.222.0 (08/2023)
//! | ISO/IEC 13818-1:2023 ยง2.6 descriptors whose syntax tables are written in
//! bits (not bytes). Internal helper, not part of the public API.
//!
//! The writer checks every value against its field width with
//! [`broadcast_core::len::fit_bits`] (an over-wide value is
//! `Error::FieldOverflow`, never masked), so the descriptors built on it
//! cannot silently truncate a field.

use crate::error::{Error, Result};
use alloc::vec::Vec;
use broadcast_core::bits::{BitBuf, BitError};
use broadcast_core::error::FieldOverflow;
use broadcast_core::len::fit_bits;

/// Bits in one byte, the width every whole-byte bit access uses.
pub(crate) const BITS_PER_BYTE: usize = u8::BITS as usize;
const MISALIGNED_BYTES: &str = "whole-byte access at a non-byte-aligned bit position";
pub(crate) const DESCRIPTOR_HEADER_LEN: usize = 2;

/// A bit-field width as callers state it: a `usize`, a `u8`/`u32` constant or
/// an untyped literal (which Rust types as `i32`).
pub(crate) trait Width: Copy {
    fn width(self) -> usize;
}

macro_rules! impl_width {
    ($($t:ty),*) => {$(
        impl Width for $t {
            fn width(self) -> usize {
                usize::try_from(self).unwrap_or(usize::MAX)
            }
        }
    )*};
}

impl_width!(usize, u8, u32, i32);

/// Sign-extend the low `bits` of `raw` (two's complement).
pub(crate) fn sign_extend(raw: u64, bits: usize) -> i64 {
    if raw & (1u64 << (bits - 1)) != 0 {
        (raw as i64) | (!0i64 << bits)
    } else {
        raw as i64
    }
}

/// `val` as the low `bits` of a two's-complement field, or `FieldOverflow`
/// when it does not fit.
pub(crate) fn signed_field(val: i64, bits: usize, field: &'static str) -> Result<u64> {
    let max = (1i64 << (bits - 1)) - 1;
    let min = -(1i64 << (bits - 1));
    if val < min || val > max {
        return Err(FieldOverflow::new(field, val.unsigned_abs(), max as u64).into());
    }
    Ok(val as u64 & ((1u64 << bits) - 1))
}

/// MSB-first bit reader over a descriptor body, a thin error-mapping adapter
/// over [`broadcast_core::bits::BitReader`].
pub(crate) struct BitReader<'a> {
    data: &'a [u8],
    inner: broadcast_core::bits::BitReader<'a>,
    what: &'static str,
}

impl<'a> BitReader<'a> {
    pub(crate) fn new(data: &'a [u8], what: &'static str) -> Self {
        Self {
            data,
            inner: broadcast_core::bits::BitReader::new(data),
            what,
        }
    }

    fn short(&self, need_bits: usize) -> Error {
        Error::BufferTooShort(broadcast_core::error::BufferTooShort::new(
            (self.inner.bits_read() + need_bits).div_ceil(BITS_PER_BYTE),
            self.data.len(),
            self.what,
        ))
    }

    /// Bits not yet consumed.
    pub(crate) fn remaining_bits(&self) -> usize {
        self.inner.bits_remaining()
    }

    /// Whole bytes not yet consumed (the reader must be byte aligned).
    pub(crate) fn remaining_bytes(&self) -> usize {
        self.remaining_bits() / BITS_PER_BYTE
    }

    /// Read `n` (1..=32) bits.
    pub(crate) fn bits(&mut self, n: usize) -> Result<u32> {
        let v = self.bits64(n)?;
        Ok(u32::try_from(v).unwrap_or(u32::MAX))
    }

    /// Read `n` (1..=64) bits.
    pub(crate) fn bits64(&mut self, n: impl Width) -> Result<u64> {
        let n = n.width();
        let width = u32::try_from(n).unwrap_or(u32::MAX);
        self.inner
            .read_bits(width, self.what)
            .map_err(|_| self.short(n))
    }

    /// Read an `n`-bit two's-complement signed field.
    pub(crate) fn signed(&mut self, n: impl Width) -> Result<i64> {
        let n = n.width();
        Ok(sign_extend(self.bits64(n)?, n))
    }

    /// Bits consumed so far.
    pub(crate) fn bits_consumed(&self) -> usize {
        self.inner.bits_read()
    }

    /// Move forward to absolute bit position `target_bits`, stopping at the end.
    pub(crate) fn seek_forward(&mut self, target_bits: usize) {
        let target = target_bits.min(self.inner.total_bits());
        let _ = self
            .inner
            .skip_bits(target - self.inner.bits_read(), self.what);
    }

    /// Skip `n` bits.
    pub(crate) fn skip(&mut self, n: impl Width) -> Result<()> {
        let n = n.width();
        self.inner
            .skip_bits(n, self.what)
            .map_err(|_| self.short(n))
    }

    /// Read `n` (1..=8) bits as a `u8`.
    pub(crate) fn u8(&mut self, n: usize) -> Result<u8> {
        Ok(u8::try_from(self.bits(n)?).unwrap_or(u8::MAX))
    }

    /// Read `n` (1..=16) bits as a `u16`.
    pub(crate) fn u16(&mut self, n: usize) -> Result<u16> {
        Ok(u16::try_from(self.bits(n)?).unwrap_or(u16::MAX))
    }

    pub(crate) fn flag(&mut self) -> Result<bool> {
        Ok(self.bits(1)? == 1)
    }

    /// Read `n` whole bytes. Errors (in release builds too) when the reader
    /// is not byte aligned, instead of silently reading shifted data.
    pub(crate) fn bytes(&mut self, n: usize) -> Result<&'a [u8]> {
        if !self.inner.is_byte_aligned() {
            return Err(Error::ValueOutOfRange {
                field: self.what,
                reason: MISALIGNED_BYTES,
            });
        }
        let start = self.inner.bits_read() / BITS_PER_BYTE;
        self.inner
            .skip_bits(n * BITS_PER_BYTE, self.what)
            .map_err(|_| self.short(n * BITS_PER_BYTE))?;
        Ok(&self.data[start..start + n])
    }

    /// Everything left. Errors when the reader is not byte aligned (the
    /// leftover would otherwise be silently dropped).
    pub(crate) fn rest(&mut self) -> Result<&'a [u8]> {
        let n = self.remaining_bytes();
        self.bytes(n)
    }
}

enum Sink<'a> {
    Growable(BitBuf),
    Fixed(broadcast_core::bits::BitWriter<'a>),
    Counting(usize),
}

/// MSB-first bit writer; every field is range-checked. The one writer of this
/// crate, over one of three sinks of [`broadcast_core::bits`]: a growable
/// buffer ([`new`](Self::new)), a caller's fixed slice ([`over`](Self::over))
/// or a bare bit count ([`counting`](Self::counting), which allocates nothing,
/// for sizing).
pub(crate) struct BitWriter<'a> {
    sink: Sink<'a>,
}

impl BitWriter<'static> {
    pub(crate) fn new() -> Self {
        Self {
            sink: Sink::Growable(BitBuf::default()),
        }
    }

    /// A writer that only counts the bits it is given.
    pub(crate) fn counting() -> Self {
        Self {
            sink: Sink::Counting(0),
        }
    }
}

impl<'a> BitWriter<'a> {
    /// A writer into `buf`; a field that does not fit is an error, never a
    /// silent truncation.
    pub(crate) fn over(buf: &'a mut [u8]) -> Self {
        Self {
            sink: Sink::Fixed(broadcast_core::bits::BitWriter::new(buf)),
        }
    }

    /// Append `value` as an `n`-bit field named `field`.
    pub(crate) fn put(&mut self, value: u64, n: usize, field: &'static str) -> Result<()> {
        let n32 = u32::try_from(n).unwrap_or(u32::MAX);
        let v = fit_bits(value, n32, field)?;
        let overflow = || Error::FieldOverflow(FieldOverflow::new(field, value, 0));
        match &mut self.sink {
            Sink::Growable(buf) => buf.write_bits(v, n32, field).map_err(|_| overflow()),
            Sink::Fixed(writer) => writer
                .write_bits(v, n32, field)
                .map_err(BitError::into_error),
            Sink::Counting(bits) => {
                *bits += n;
                Ok(())
            }
        }
    }

    pub(crate) fn flag(&mut self, v: bool, field: &'static str) -> Result<()> {
        self.put(u64::from(v), 1, field)
    }

    /// Append `value` as an `n`-bit field.
    pub(crate) fn write_u(&mut self, n: impl Width, value: u64) -> Result<()> {
        self.put(value, n.width(), "bit field")
    }

    /// Append `value` as an `n`-bit two's-complement field.
    pub(crate) fn write_i(&mut self, n: impl Width, value: i64) -> Result<()> {
        let n = n.width();
        self.put(
            signed_field(value, n, "signed bit field")?,
            n,
            "signed bit field",
        )
    }

    /// Append `n` zero bits.
    pub(crate) fn write_zero(&mut self, n: impl Width) -> Result<()> {
        self.put(0, n.width(), "zero bits")
    }

    /// Bits written so far.
    pub(crate) fn bits_written(&self) -> usize {
        match &self.sink {
            Sink::Growable(buf) => buf.bits_written(),
            Sink::Fixed(writer) => writer.bits_written(),
            Sink::Counting(bits) => *bits,
        }
    }

    /// Append whole bytes to a growable writer. Errors (in release builds
    /// too) when the writer is not byte aligned, instead of emitting shifted
    /// data.
    pub(crate) fn bytes(&mut self, b: &[u8]) -> Result<()> {
        let misaligned = || Error::ValueOutOfRange {
            field: "byte run",
            reason: MISALIGNED_BYTES,
        };
        match &mut self.sink {
            Sink::Growable(buf) => buf.write_bytes(b, "b").map_err(|_| misaligned()),
            Sink::Fixed(_) | Sink::Counting(_) => Err(misaligned()),
        }
    }

    /// The bytes written to a growable writer (empty for the other sinks).
    pub(crate) fn into_vec(self) -> Vec<u8> {
        match self.sink {
            Sink::Growable(buf) => buf.into_vec(),
            Sink::Fixed(_) | Sink::Counting(_) => Vec::new(),
        }
    }
}

/// Implement `Parse` / `Serialize` / `DescriptorDef` for an owned descriptor
/// type that provides `parse_body(&[u8]) -> Result<Self>` and
/// `write_body(&self) -> Result<Vec<u8>>`.
macro_rules! impl_h222_descriptor {
    ($ty:ty, $tag:expr, $what:literal, $name:literal) => {
        impl<'a> broadcast_core::Parse<'a> for $ty {
            type Error = crate::error::Error;

            fn parse(bytes: &'a [u8]) -> crate::error::Result<Self> {
                let body = $crate::descriptors::descriptor_body(
                    bytes,
                    $tag,
                    $what,
                    concat!("unexpected tag for ", $what),
                )?;
                Self::parse_body(body)
            }
        }

        impl broadcast_core::Serialize for $ty {
            type Error = crate::error::Error;

            fn serialized_len(&self) -> usize {
                // An unencodable value is reported by `serialize_into`.
                self.write_body()
                    .map_or(0, |b| $crate::bit_io::DESCRIPTOR_HEADER_LEN + b.len())
            }

            fn serialize_into(&self, buf: &mut [u8]) -> crate::error::Result<usize> {
                let body = self.write_body()?;
                let len = $crate::bit_io::DESCRIPTOR_HEADER_LEN + body.len();
                ::broadcast_core::len::need_out(buf, len, stringify!($ty))?;
                $crate::descriptors::write_descriptor_header(buf, $tag, body.len())?;
                buf[$crate::bit_io::DESCRIPTOR_HEADER_LEN..len].copy_from_slice(&body);
                Ok(len)
            }
        }

        impl crate::traits::DescriptorDef<'_> for $ty {
            const TAG: u8 = $tag;
            const NAME: &'static str = $name;
        }
    };
}
pub(crate) use impl_h222_descriptor;

/// Error for a value the syntax cannot express.
pub(crate) fn invalid(tag: u8, reason: &'static str) -> Error {
    Error::InvalidDescriptor { tag, reason }
}

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

    #[test]
    fn reader_bytes_rejects_misaligned_access() {
        let mut r = BitReader::new(&[0xFF, 0xAA, 0xBB], "test");
        r.bits(3).unwrap();
        assert!(matches!(
            r.bytes(1).unwrap_err(),
            Error::ValueOutOfRange { field: "test", .. }
        ));
        let mut r = BitReader::new(&[0xFF, 0xAA, 0xBB], "test");
        r.bits(8).unwrap();
        assert_eq!(r.bytes(2).unwrap(), &[0xAA, 0xBB]);
    }

    #[test]
    fn reader_rest_surfaces_misalignment_instead_of_dropping_data() {
        let mut r = BitReader::new(&[0xFF, 0xAA, 0xBB], "test");
        r.bits(3).unwrap();
        assert!(matches!(
            r.rest().unwrap_err(),
            Error::ValueOutOfRange { field: "test", .. }
        ));
        let mut r = BitReader::new(&[0xFF, 0xAA, 0xBB], "test");
        r.bits(8).unwrap();
        assert_eq!(r.rest().unwrap(), &[0xAA, 0xBB]);
        assert!(r.rest().unwrap().is_empty());
    }

    #[test]
    fn writer_bytes_rejects_misaligned_access() {
        let mut w = BitWriter::new();
        w.put(1, 3, "x").unwrap();
        assert!(matches!(
            w.bytes(&[1]).unwrap_err(),
            Error::ValueOutOfRange { .. }
        ));
        let mut w = BitWriter::new();
        w.put(0xAB, 8, "x").unwrap();
        w.bytes(&[1]).unwrap();
        assert_eq!(w.into_vec(), [0xAB, 1]);
    }

    #[test]
    fn writer_and_reader_agree_across_byte_boundaries() {
        let mut w = BitWriter::new();
        w.put(0b101, 3, "a").unwrap();
        w.put(0x1FF, 9, "b").unwrap();
        w.put(0xDEAD_BEEF, 32, "c").unwrap();
        w.flag(true, "d").unwrap();
        assert!(matches!(w.put(4, 2, "e"), Err(Error::FieldOverflow(_))));
        let bytes = w.into_vec();
        assert_eq!(bytes, [0xBF, 0xFD, 0xEA, 0xDB, 0xEE, 0xF8]);
        let mut r = BitReader::new(&bytes, "t");
        assert_eq!(r.bits(3).unwrap(), 0b101);
        assert_eq!(r.bits(9).unwrap(), 0x1FF);
        assert_eq!(r.bits64(32).unwrap(), 0xDEAD_BEEF);
        assert!(r.flag().unwrap());
        assert_eq!(r.remaining_bits(), 3);
        assert!(matches!(r.bits(4).unwrap_err(), Error::BufferTooShort(_)));
    }
}