broadcast-common 15.0.0

Foundation for the rust-broadcast crates: CRC-32/MPEG-2, BCD + MJD time codecs, hex and base64 codecs, 33-bit/PCR clock arithmetic, wrap-safe modular counters, SDP media sections and the bounded XML reader. no_std + alloc.
Documentation
//! Shared primitives of the rust-broadcast workspace, one layer above the
//! [`broadcast_core`] wire contract: the CRC-32/MPEG-2 that protects PSI/SI
//! sections, the BCD and MJD time codecs of ETSI EN 300 468, the 33-bit
//! PTS/DTS and 27 MHz PCR clock arithmetic of ITU-T H.222.0, wrap-safe
//! counters for RTP and SRT sequence numbers, BER lengths and SMPTE ST 336
//! KLV triplets, odd parity, hex, the SDP media-section model the RTP crates
//! exchange, bounded whole-document reads, and (behind features) base64,
//! ISO 639 to BCP 47 language tags and the bounded XML reader every
//! XML-reading crate shares. Each of these is defined here exactly once so
//! the format crates above (`mpeg-ts`, `mpeg-pes`, `dvb-si`, `scte35-splice`,
//! `srt-runtime`, `transmux`, ...) cannot disagree about it.
//!
//! It depends on `broadcast-core` and re-exports nothing from it: import the
//! `Parse`/`Serialize` traits, bit I/O and error kinds as
//! `broadcast_core::...`. The crate is `no_std` + `alloc` without the `std`
//! feature, forbids `unsafe`, does no I/O outside [`bounded`], and has no
//! shared error enum: each module returns its own small typed error. HTTP and
//! runtime helpers live in `broadcast-net`.
//!
//! # Quickstart
//!
//! Check the CRC of a real PAT section, decode a BCD byte and take a
//! wrap-safe PTS distance:
//!
//! ```
//! use broadcast_common::{bcd, clock33, crc32_mpeg2};
//!
//! let pat = [
//!     0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x04, 0x01, 0xE0, 0x64,
//!     0xF9, 0xB4, 0x63, 0xEF,
//! ];
//! assert_eq!(crc32_mpeg2::verify_trailer(&pat, "PAT"), Some(Ok(())));
//! assert_eq!(crc32_mpeg2::compute(&pat[..pat.len() - 4]), 0xF9B4_63EF);
//!
//! assert_eq!(bcd::from_bcd_byte(0x42), Some(42));
//!
//! let just_before_wrap = clock33::WRAP_33BIT - 10;
//! assert_eq!(clock33::signed_distance(just_before_wrap, 5), 15);
//! assert_eq!(clock33::add(just_before_wrap, 15), 5);
//! ```
//!
//! # How do I...
//!
//! - Compute a CRC-32/MPEG-2: [`crc32_mpeg2::compute`],
//!   `examples/crc_and_bcd.rs`.
//! - Check a section's trailing CRC or write one in place:
//!   [`crc32_mpeg2::verify_trailer`], [`crc32_mpeg2::seal`]; the Quickstart
//!   above checks one.
//! - Decode or encode a BCD byte: [`bcd::from_bcd_byte`],
//!   [`bcd::to_bcd_byte`], `examples/crc_and_bcd.rs`.
//! - Decode or encode a wider BCD field: [`bcd::bcd_to_decimal`],
//!   [`bcd::decimal_to_bcd`]; no example yet.
//! - Decode a DVB UTC time (MJD + BCD) or BCD duration:
//!   [`time::decode_mjd_bcd`], [`time::encode_mjd_bcd`],
//!   [`time::decode_bcd_duration`], `examples/dvb_time.rs`.
//! - Read or write a 5-byte PTS/DTS marker-bit field:
//!   [`clock33::unpack_marked`], [`clock33::pack_marked`],
//!   `examples/timestamps.rs`.
//! - Compare or step PTS/DTS values across the 33-bit wrap:
//!   [`clock33::Clock33`], `examples/timestamps.rs`.
//! - Do the same on raw `u64` ticks: [`clock33::signed_distance`],
//!   [`clock33::add`]; the Quickstart above.
//! - Build or split a 27 MHz PCR: [`clock33::Clock27`],
//!   [`clock33::pcr_split`], `examples/timestamps.rs`.
//! - Unwrap a 16-bit RTP or 31-bit SRT counter onto a timeline:
//!   [`modular::Modulus`], [`modular::Unwrapper`], `examples/timestamps.rs`.
//! - Walk or build KLV triplets: [`klv::KlvItem`], [`klv::walk_klv_items`],
//!   `examples/klv_items.rs`.
//! - Decode or encode a BER length on its own: [`ber::decode_ber_length`],
//!   [`ber::encode_ber_length`]; no example yet.
//! - Check or set odd parity on teletext and CEA-608 bytes:
//!   [`parity::has_odd_parity`], [`parity::strip_parity`]; no example yet.
//! - Hex-encode or decode an opaque field: [`hex::hex_encode`],
//!   [`hex::hex_decode_into`]; no example yet.
//! - Describe an RTP media section without an SDP library:
//!   [`sdp::SdpMedia`], [`sdp::SdpAttribute`]; no example yet.
//! - Read a small document whole under a named size limit:
//!   [`bounded::read_bounded`], [`bounded::Limit`]; no example yet.
//! - Read a bounded, DOCTYPE-free XML document into a tree (`xml` feature):
//!   [`xml::read_tree`], [`xml_chars::XmlLimits`]; no example yet.
//! - Declare a `#[repr]` spec enum with checked conversions:
//!   [`wire_enum!`]; see its documentation.
//!
//! # Modules
//!
//! Wire codecs: [`crc32_mpeg2`] (CRC-32/MPEG-2), [`bcd`] (binary-coded
//! decimal), [`time`] (MJD + BCD UTC time and BCD durations), [`parity`]
//! (odd-parity bytes), [`ber`] (BER definite-form lengths), [`klv`] (SMPTE
//! ST 336 Key-Length-Value triplets), [`hex`](mod@hex) (hex text of byte
//! fields), [`base64`](mod@base64) (RFC 4648, feature `base64`).
//!
//! Clocks and counters: [`clock33`] (33-bit PTS/DTS, 27 MHz PCR and the
//! marker-bit timestamp field), [`modular`] (wrap-safe counters of any width
//! up to 63 bits).
//!
//! Session and language vocabulary: [`sdp`] (the SDP media-section model),
//! [`lang`] (ISO 639 to BCP 47, feature `lang`).
//!
//! Bounded input: [`bounded`] (size-limited whole-document reads, `std`),
//! [`xml`] (the bounded namespace-aware XML tree reader, feature `xml`),
//! [`xml_chars`] (XML 1.0 character-data checks, limits and the writer,
//! feature `xml`).
//!
//! Registry support: [`erased`] (the type-erased value face behind the
//! `dvb-si` and `dvb-t2mi` runtime registries, with
//! [`declare_object_trait!`]).
//!
//! # Feature flags
//!
//! - `std` (default): links `std` and adds [`bounded`]; off gives `no_std` +
//!   `alloc`.
//! - `jiff`: `jiff::Timestamp` faces of the [`time`] codecs.
//! - `serde`: `serde::Serialize` on the shared types that carry one, and the
//!   serde half of [`declare_object_trait!`].
//! - `xml`: [`xml`] and [`xml_chars`] (implies `std`).
//! - `lang`: [`lang`] and the root [`bcp47_language`] (implies `std`).
//! - `base64`: [`base64`](mod@base64).
//!
//! # Spec citations
//!
//! - ITU-T H.222.0 v9 (08/2023) / ISO/IEC 13818-1: the CRC-32 decoder model
//!   (Annex A), the 33-bit PTS/DTS and 27 MHz PCR clocks and the marker-bit
//!   timestamp field (§2.4.3.4, §2.4.3.7).
//! - ETSI EN 300 468 V1.20.1: the CRC-32 (Annex A), MJD + BCD UTC time and
//!   BCD durations (Annex C).
//! - ETSI TS 102 773 V1.4.1 Annex A: the same CRC-32 over T2-MI packets.
//! - ETSI EN 300 706 V1.2.1 §8.1: the odd-parity protected byte.
//! - SMPTE ST 336, as constrained for MXF by SMPTE ST 377-1:2019 §6.3: KLV
//!   triplets and BER lengths.
//! - RFC 4566 §5.13, §5.14: the SDP media section.
//! - RFC 4648: base64. RFC 5646: BCP 47 language tags. XML 1.0 §2.2: the
//!   `Char` production.
//!
//! # Reserved-bit and trailing-byte policy
//!
//! One workspace-wide rule for every wire crate (issue #1139):
//!
//! - **Parse is tolerant.** A non-conformant reserved bit, or trailing data
//!   that real senders emit, is accepted rather than rejected (a crate may
//!   choose stricter validation, as `dvb-t2mi` does for RFU bits, but must
//!   document it).
//! - **Serialize preserves what was parsed.** Reserved bits are captured on
//!   parse in typed, spec-named fields and re-emitted verbatim, so a stream
//!   from a sender that deviates from the governing edition re-serializes
//!   **byte-identically** (`dvb-si` descriptors, `mpeg-ps`, `rmt-flute`,
//!   `cc-data` fixed bits, `rtp-packet`'s `rtcp` `profile_extensions`, `isobmff`
//!   flags). There is no raw passthrough blob.
//! - **Constructed values default to the edition value**: a struct built by
//!   hand (`new(..)` / `Default`) carries `1` for `reserved_future_use` and
//!   H.222.0 "reserved", `0` for `reserved_zero_future_use`, with any explicit
//!   per-field exception documented on the field. (A crate that has not
//!   adopted preserve-on-parse documents which fields it normalizes to the
//!   spec value on serialize; `mpeg-ts`, `mpeg-pes` and `mpeg-ps` preserve.)
//! - A caller that needs byte-for-byte preservation of reserved bits in a
//!   not-yet-migrated crate **keeps the original bytes** rather than
//!   re-serializing.
//! - Trailing bytes inside a descriptor or section body are never silently
//!   dropped without a documented decision: hold them in a typed field, or
//!   document the field as "ignored".
//! - Reserved bytes are always **written explicitly** by `serialize_into`;
//!   never rely on a pre-zeroed output buffer.
//!
//! The round-trip gate is byte-exact against the original fixture slice
//! (see `docs/CRATE-ACCEPTANCE.md`).

#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

extern crate alloc;

#[cfg(feature = "base64")]
pub mod base64;
pub mod bcd;
pub mod ber;
#[cfg(feature = "std")]
pub mod bounded;
pub mod clock33;
pub mod crc32_mpeg2;
pub mod erased;
pub mod hex;
pub mod klv;
#[cfg(feature = "lang")]
pub mod lang;
pub mod modular;
pub mod parity;
pub mod sdp;
pub mod time;
mod wire_enum;
#[cfg(feature = "xml")]
pub mod xml;
#[cfg(feature = "xml")]
pub mod xml_chars;

#[cfg(feature = "lang")]
pub use lang::bcp47_language;