Skip to main content

broadcast_dash/
limits.rs

1//! Hostile-input bounds for MPD parsing and segment description.
2//!
3//! An MPD is fetched from a server the player does not control, so every
4//! dimension of it that costs memory or CPU has a named ceiling and a typed
5//! error ([`crate::error::MpdError::TooLarge`]) instead of an allocation.
6
7/// Largest MPD document accepted, in bytes.
8pub const MAX_MPD_BYTES: usize = broadcast_common::xml_chars::MAX_XML_BYTES;
9/// Deepest element nesting accepted.
10pub const MAX_XML_DEPTH: usize = broadcast_common::xml_chars::MAX_XML_DEPTH;
11/// Most elements (of any namespace) accepted in one document.
12pub const MAX_XML_ELEMENTS: usize = broadcast_common::xml_chars::MAX_XML_ELEMENTS;
13/// Longest single attribute value accepted, in bytes.
14pub const MAX_ATTRIBUTE_BYTES: usize = broadcast_common::xml_chars::MAX_XML_ATTRIBUTE_BYTES;
15/// Most attributes accepted on one element.
16pub const MAX_ATTRIBUTES_PER_ELEMENT: usize = broadcast_common::xml_chars::MAX_XML_ATTRIBUTES;
17/// Most `Period` elements accepted.
18pub const MAX_PERIODS: usize = 1_000;
19/// Most `AdaptationSet` elements accepted in one `Period`.
20pub const MAX_ADAPTATION_SETS_PER_PERIOD: usize = 1_000;
21/// Most `Representation` elements accepted in one `AdaptationSet`.
22pub const MAX_REPRESENTATIONS_PER_SET: usize = 1_000;
23/// Most `S` entries accepted in one `SegmentTimeline`.
24pub const MAX_TIMELINE_ENTRIES: usize = 100_000;
25/// Most `BaseURL` alternates accepted on one level.
26pub const MAX_BASE_URLS_PER_LEVEL: usize = 64;
27/// Most `SegmentURL` entries accepted in one `SegmentList`.
28pub const MAX_SEGMENT_URLS: usize = 100_000;
29/// Most Media Segments one `SegmentTimeline` may describe once `@r` is
30/// expanded. The expansion is lazy (run table, never one entry per segment),
31/// so this bounds the arithmetic, not the memory.
32pub const MAX_TIMELINE_SEGMENTS: u64 = 1_000_000;
33
34/// The bounds applied by [`crate::Mpd::parse_with`]; `Default` is the `MAX_*`
35/// constants above.
36#[derive(Debug, Clone, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct Limits {
39    /// See [`MAX_MPD_BYTES`].
40    pub max_bytes: usize,
41    /// See [`MAX_XML_DEPTH`].
42    pub max_depth: usize,
43    /// See [`MAX_XML_ELEMENTS`].
44    pub max_elements: usize,
45    /// See [`MAX_ATTRIBUTE_BYTES`].
46    pub max_attribute_bytes: usize,
47    /// See [`MAX_ATTRIBUTES_PER_ELEMENT`].
48    pub max_attributes: usize,
49    /// See [`MAX_PERIODS`].
50    pub max_periods: usize,
51    /// See [`MAX_ADAPTATION_SETS_PER_PERIOD`].
52    pub max_adaptation_sets: usize,
53    /// See [`MAX_REPRESENTATIONS_PER_SET`].
54    pub max_representations: usize,
55    /// See [`MAX_TIMELINE_ENTRIES`].
56    pub max_timeline_entries: usize,
57    /// See [`MAX_BASE_URLS_PER_LEVEL`].
58    pub max_base_urls: usize,
59    /// See [`MAX_SEGMENT_URLS`].
60    pub max_segment_urls: usize,
61}
62
63impl Limits {
64    /// The XML-level subset of these bounds as the shared
65    /// [`XmlLimits`](broadcast_common::xml_chars::XmlLimits) the MPD reader
66    /// enforces through an [`XmlBudget`](broadcast_common::xml_chars::XmlBudget).
67    #[must_use]
68    pub fn xml_limits(&self) -> broadcast_common::xml_chars::XmlLimits {
69        let mut xml = broadcast_common::xml_chars::XmlLimits::default();
70        xml.max_bytes = self.max_bytes;
71        xml.max_depth = self.max_depth;
72        xml.max_elements = self.max_elements;
73        xml.max_attribute_bytes = self.max_attribute_bytes;
74        xml.max_attributes = self.max_attributes;
75        xml
76    }
77}
78
79impl Default for Limits {
80    fn default() -> Self {
81        Self {
82            max_bytes: MAX_MPD_BYTES,
83            max_depth: MAX_XML_DEPTH,
84            max_elements: MAX_XML_ELEMENTS,
85            max_attribute_bytes: MAX_ATTRIBUTE_BYTES,
86            max_attributes: MAX_ATTRIBUTES_PER_ELEMENT,
87            max_periods: MAX_PERIODS,
88            max_adaptation_sets: MAX_ADAPTATION_SETS_PER_PERIOD,
89            max_representations: MAX_REPRESENTATIONS_PER_SET,
90            max_timeline_entries: MAX_TIMELINE_ENTRIES,
91            max_base_urls: MAX_BASE_URLS_PER_LEVEL,
92            max_segment_urls: MAX_SEGMENT_URLS,
93        }
94    }
95}