Skip to main content

dash_runtime/
limits.rs

1//! Hostile-input bounds for MPD parsing and segment expansion.
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 = 4 * 1024 * 1024;
9/// Deepest element nesting accepted.
10pub const MAX_XML_DEPTH: usize = 32;
11/// Most elements (of any namespace) accepted in one document.
12pub const MAX_XML_ELEMENTS: usize = 100_000;
13/// Longest single attribute value accepted, in bytes.
14pub const MAX_ATTRIBUTE_BYTES: usize = 8 * 1024;
15/// Most attributes accepted on one element.
16pub const MAX_ATTRIBUTES_PER_ELEMENT: usize = 256;
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/// Most candidate base locations kept when `BaseURL` alternates are combined
34/// across levels.
35pub const MAX_BASE_CANDIDATES: usize = 64;
36
37/// The bounds applied by [`crate::mpd::Mpd::parse_with`]; `Default` is the
38/// `MAX_*` constants above.
39#[derive(Debug, Clone, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct Limits {
42    /// See [`MAX_MPD_BYTES`].
43    pub max_bytes: usize,
44    /// See [`MAX_XML_DEPTH`].
45    pub max_depth: usize,
46    /// See [`MAX_XML_ELEMENTS`].
47    pub max_elements: usize,
48    /// See [`MAX_ATTRIBUTE_BYTES`].
49    pub max_attribute_bytes: usize,
50    /// See [`MAX_ATTRIBUTES_PER_ELEMENT`].
51    pub max_attributes: usize,
52    /// See [`MAX_PERIODS`].
53    pub max_periods: usize,
54    /// See [`MAX_ADAPTATION_SETS_PER_PERIOD`].
55    pub max_adaptation_sets: usize,
56    /// See [`MAX_REPRESENTATIONS_PER_SET`].
57    pub max_representations: usize,
58    /// See [`MAX_TIMELINE_ENTRIES`].
59    pub max_timeline_entries: usize,
60    /// See [`MAX_BASE_URLS_PER_LEVEL`].
61    pub max_base_urls: usize,
62    /// See [`MAX_SEGMENT_URLS`].
63    pub max_segment_urls: usize,
64}
65
66impl Default for Limits {
67    fn default() -> Self {
68        Self {
69            max_bytes: MAX_MPD_BYTES,
70            max_depth: MAX_XML_DEPTH,
71            max_elements: MAX_XML_ELEMENTS,
72            max_attribute_bytes: MAX_ATTRIBUTE_BYTES,
73            max_attributes: MAX_ATTRIBUTES_PER_ELEMENT,
74            max_periods: MAX_PERIODS,
75            max_adaptation_sets: MAX_ADAPTATION_SETS_PER_PERIOD,
76            max_representations: MAX_REPRESENTATIONS_PER_SET,
77            max_timeline_entries: MAX_TIMELINE_ENTRIES,
78            max_base_urls: MAX_BASE_URLS_PER_LEVEL,
79            max_segment_urls: MAX_SEGMENT_URLS,
80        }
81    }
82}