broadcast-common 12.0.0

Foundation for the rust-broadcast crates: symmetric Parse/Serialize traits, CRC-32/MPEG-2, BCD + MJD time codecs, bit I/O, 33-bit/PCR clock arithmetic, checked field widths, container-mux and Stage traits. no_std + alloc.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Bounded namespace-aware XML reading into a small element tree.
//!
//! quick-xml does the lexing, entity and namespace resolution. This layer adds
//! the hostile-input bounds ([`XmlLimits`]), refuses DOCTYPE declarations (so
//! no entity can be declared, hence no entity-expansion attack), and validates
//! every character against the XML 1.0 `Char` production. Every element below
//! the root is kept with its own namespace recorded; only a foreign element at
//! the root is skipped whole, so a host schema's unqualified children and its
//! foreign-namespace extensions are both readable through [`Elem::attr_in`].

use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use quick_xml::NsReader;
use quick_xml::events::{BytesStart, Event};
use quick_xml::name::ResolveResult;

use crate::xml_chars::{LimitExceeded, XmlBudget, XmlLimits};

/// One way an element or attribute name resolves: the caller's target
/// namespace, no namespace, or a foreign one. An owner classifies each URI it
/// understands so the reader can record it, and passes the target namespace,
/// which is the only namespace a root element may have.
pub trait Namespace: Copy + Eq {
    /// The target namespace: its elements are kept in the tree.
    const TARGET: Self;

    /// No namespace at all.
    const NONE: Self;

    /// Classify a resolved namespace URI (an unprefixed attribute resolves to
    /// no namespace and is never classified).
    fn classify(uri: &str) -> Self;
}

/// One attribute of a start tag, namespace declarations excluded.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Attr<N> {
    /// The attribute's own namespace (`N::NONE` for an unprefixed attribute).
    pub ns: N,
    /// Its local name.
    pub name: String,
    /// Its normalized, unescaped value.
    pub value: String,
}

/// One element of the tree: its namespace, local name, attributes, kept
/// children and concatenated character data.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Elem<N> {
    /// The element's namespace.
    pub ns: N,
    /// Its local name.
    pub name: String,
    /// Its attributes, in document order.
    pub attrs: Vec<Attr<N>>,
    /// Its children, in document order.
    pub children: Vec<Elem<N>>,
    /// Its direct character data (text nodes that are immediate
    /// children of this element); descendant text is not included.
    pub text: String,
}

impl<N: Namespace> Elem<N> {
    /// The value of the unprefixed attribute `name`.
    #[must_use]
    pub fn attr(&self, name: &str) -> Option<&str> {
        self.attr_in(N::NONE, name)
    }

    /// The value of the attribute `name` in namespace `ns`.
    #[must_use]
    pub fn attr_in(&self, ns: N, name: &str) -> Option<&str> {
        self.attrs
            .iter()
            .find(|a| a.ns == ns && a.name == name)
            .map(|a| a.value.as_str())
    }

    /// Every direct child element named `name` in the target namespace.
    pub fn children_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a Elem<N>> + 'a {
        self.children
            .iter()
            .filter(move |c| c.ns == N::TARGET && c.name == name)
    }

    /// The first direct child element named `name` in the target namespace.
    #[must_use]
    pub fn child(&self, name: &str) -> Option<&Elem<N>> {
        self.children
            .iter()
            .find(|c| c.ns == N::TARGET && c.name == name)
    }

    /// Whether a direct child element named `name` exists in the target
    /// namespace.
    #[must_use]
    pub fn has_child(&self, name: &str) -> bool {
        self.child(name).is_some()
    }

    /// All direct character data inside this element (descendant text
    /// is not included).
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }
}

/// Why [`read_tree`] rejected a document.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum XmlError {
    /// The bytes are not UTF-8.
    InvalidUtf8 {
        /// Length of the valid prefix.
        valid_up_to: usize,
    },
    /// The document is not well-formed XML, or uses a construct this reader
    /// refuses (a DOCTYPE).
    NotWellFormed {
        /// Byte offset where the reader stopped.
        position: usize,
        /// What was wrong.
        message: String,
    },
    /// The root element's local name is not `root`.
    WrongRoot {
        /// The local name actually found.
        found: String,
    },
    /// A hostile-input bound was exceeded.
    Limit(LimitExceeded),
}

impl Namespace for () {
    const TARGET: Self = ();
    const NONE: Self = ();

    fn classify(_uri: &str) -> Self {}
}

const BOM: char = '\u{FEFF}';

struct Reader<'a, N> {
    inner: NsReader<&'a [u8]>,
    budget: XmlBudget,
    marker: core::marker::PhantomData<N>,
}

fn ns_of<N: Namespace>(result: ResolveResult<'_>) -> Result<N, String> {
    match result {
        ResolveResult::Unbound => Ok(N::NONE),
        ResolveResult::Bound(ns) => Ok(N::classify(ns.into_inner())),
        ResolveResult::Unknown(prefix) => Err(format!("unknown namespace prefix '{prefix}'")),
    }
}

impl<N: Namespace> Reader<'_, N> {
    fn attrs(&self, e: &BytesStart<'_>) -> Result<Vec<Attr<N>>, XmlError> {
        let attributes = crate::xml_chars::read_attributes_checked(
            &self.inner,
            e,
            |m| self.fail(m),
            |count, value| self.admit_attribute(count, value),
        )?;
        Ok(attributes
            .into_iter()
            .map(|a| Attr {
                ns: a.namespace.as_deref().map_or(N::NONE, N::classify),
                name: a.name,
                value: a.value,
            })
            .collect())
    }

    fn admit_attribute(&self, count: usize, value: &str) -> Result<(), XmlError> {
        self.budget
            .admit_attribute(count, value)
            .map_err(XmlError::Limit)
    }

    fn char_data(&self, event: &Event<'_>) -> Result<Option<String>, XmlError> {
        crate::xml_chars::char_data(event).map_err(|m| self.fail(m))
    }

    fn fail(&self, message: impl Into<String>) -> XmlError {
        XmlError::NotWellFormed {
            position: usize::try_from(self.inner.buffer_position()).unwrap_or(usize::MAX),
            message: message.into(),
        }
    }
}

/// A start tag that has not been closed yet: the kept element under
/// construction, or the marker for a foreign root that is skipped whole.
enum Frame<N> {
    Kept(Elem<N>),
    Branch,
}

/// Read `xml` into an element tree rooted at `root` in namespace `N::TARGET`.
///
/// The root must be `root` in `N::TARGET` (else [`XmlError::WrongRoot`]); it
/// and every element below it are kept with their resolved namespace recorded.
/// `limits` bounds the document size, depth, element count and attribute
/// size/count.
///
/// # Errors
/// [`XmlError`] for invalid UTF-8, a malformed or refused document, a root
/// element that is not `root` in the target namespace, or an exceeded bound.
pub fn read_tree<N: Namespace>(
    xml: &[u8],
    root: &str,
    limits: &XmlLimits,
) -> Result<Elem<N>, XmlError> {
    let budget = XmlBudget::new(limits.clone());
    budget.check_input(xml.len()).map_err(XmlError::Limit)?;
    let text = core::str::from_utf8(xml).map_err(|e| XmlError::InvalidUtf8 {
        valid_up_to: e.valid_up_to(),
    })?;
    let text = text.strip_prefix(BOM).unwrap_or(text);
    let mut reader = Reader::<N> {
        inner: quick_xml::NsReader::from_str(text),
        budget,
        marker: core::marker::PhantomData,
    };

    let mut stack: Vec<Frame<N>> = Vec::new();
    let mut doc_root: Option<Elem<N>> = None;

    loop {
        let (ns_of_event, event) = match reader.inner.read_resolved_event() {
            Ok((ns, event)) => (ns_of::<N>(ns), event),
            Err(e) => return Err(reader.fail(e.to_string())),
        };
        if let Some(piece) = reader.char_data(&event)? {
            if stack.is_empty() {
                if matches!(event, Event::CData(_)) {
                    return Err(reader.fail("character data outside the root element"));
                }
                if piece
                    .chars()
                    .any(|c| !matches!(c, ' ' | '\t' | '\r' | '\n'))
                {
                    return Err(reader.fail("character data outside the root element"));
                }
            }
            match stack.last_mut() {
                Some(Frame::Kept(top)) => top.text.push_str(&piece),
                Some(Frame::Branch) => {}
                None => {}
            }
            continue;
        }
        match event {
            Event::Start(ref e) | Event::Empty(ref e) => {
                let is_empty = matches!(event, Event::Empty(_));
                if doc_root.is_some() && stack.is_empty() {
                    return Err(reader.fail("more than one root element"));
                }
                reader.budget.open_element().map_err(XmlError::Limit)?;
                if is_empty {
                    reader.budget.close_element();
                }
                let local = e.local_name().into_inner();
                let resolved = ns_of_event.map_err(|m| reader.fail(m))?;
                if stack.is_empty() && (resolved != N::TARGET || local != root) {
                    return Err(XmlError::WrongRoot {
                        found: local.to_owned(),
                    });
                }
                let keep_element = resolved != N::TARGET || local == root || !stack.is_empty();
                let frame = if keep_element {
                    Frame::Kept(Elem {
                        ns: resolved,
                        name: local.to_string(),
                        attrs: reader.attrs(e)?,
                        children: Vec::new(),
                        text: String::new(),
                    })
                } else {
                    Frame::Branch
                };
                if is_empty {
                    attach(frame, &mut stack, &mut doc_root);
                } else {
                    stack.push(frame);
                }
            }
            Event::End(_) => match stack.pop() {
                Some(frame) => {
                    reader.budget.close_element();
                    attach(frame, &mut stack, &mut doc_root);
                }
                None => return Err(reader.fail("unexpected end tag")),
            },
            Event::DocType(_) => return Err(reader.fail("DOCTYPE declarations are not allowed")),
            Event::Eof => {
                if !stack.is_empty() {
                    return Err(reader.fail("unexpected end of input inside an element"));
                }
                return doc_root.ok_or_else(|| reader.fail("no root element"));
            }
            _ => {}
        }
    }
}

fn attach<N: Namespace>(frame: Frame<N>, stack: &mut [Frame<N>], root: &mut Option<Elem<N>>) {
    let Frame::Kept(elem) = frame else {
        return;
    };
    match stack.last_mut() {
        Some(Frame::Kept(parent)) => {
            parent.children.push(elem);
        }
        Some(Frame::Branch) => {}
        None => *root = Some(elem),
    }
}

impl From<LimitExceeded> for XmlError {
    fn from(e: LimitExceeded) -> Self {
        Self::Limit(e)
    }
}

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

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum Ns {
        None,
        Target,
        Other,
    }

    impl Namespace for Ns {
        const TARGET: Self = Ns::Target;
        const NONE: Self = Ns::None;

        fn classify(uri: &str) -> Self {
            match uri {
                "urn:target" => Self::Target,
                _ => Self::Other,
            }
        }
    }

    fn read(xml: &str) -> Result<Elem<Ns>, XmlError> {
        read_tree(xml.as_bytes(), "Root", &XmlLimits::default())
    }

    #[test]
    fn foreign_children_keep_their_namespace_and_children_survive() {
        let tree = read(
            r#"<Root xmlns="urn:target" xmlns:x="urn:other" a="1" x:b="2"><x:Child><Child>t</Child></x:Child></Root>"#,
        )
        .unwrap();
        assert_eq!(tree.attr("a"), Some("1"));
        assert_eq!(tree.attr_in(Ns::Other, "b"), Some("2"));
        assert_eq!(tree.children.len(), 1);
        assert_eq!(tree.children[0].ns, Ns::Other);
        assert_eq!(tree.children[0].name, "Child");
        assert_eq!(tree.children[0].child("Child").unwrap().text(), "t");
    }

    #[test]
    fn wrong_namespace_root_is_reported() {
        let err = read(r#"<Root xmlns="urn:other"/>"#).unwrap_err();
        assert!(matches!(err, XmlError::WrongRoot { .. }), "{err:?}");
    }

    #[test]
    fn doctype_is_refused() {
        let err = read(r#"<!DOCTYPE x [<!ENTITY e "v">]><Root>&e;</Root>"#).unwrap_err();
        assert!(matches!(err, XmlError::NotWellFormed { .. }), "{err:?}");
    }

    #[test]
    fn namespace_less_elements_and_text() {
        let tree = read(r#"<Root xmlns="urn:target">a<B>c</B>b</Root>"#).unwrap();
        assert_eq!(tree.text(), "ab");
        assert_eq!(tree.child("B").unwrap().text(), "c");
    }

    #[test]
    fn mixed_text_pieces_accumulate() {
        let tree = read(r#"<Root xmlns="urn:target">abc&amp;defab<![CDATA[<x>]]>cda&#13;b</Root>"#)
            .unwrap();
        assert_eq!(tree.text(), "abc&defab<x>cda\rb");
    }
}

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

    #[derive(Clone, Copy, PartialEq, Eq)]
    enum Ns {
        None,
        Other,
    }
    impl Namespace for Ns {
        const TARGET: Self = Ns::None;
        const NONE: Self = Ns::None;
        fn classify(_uri: &str) -> Self {
            Ns::Other
        }
    }

    #[test]
    fn leading_nbsp_rejected() {
        let xml = "\u{00A0}<Root/>";
        let res = read_tree::<Ns>(xml.as_bytes(), "Root", &XmlLimits::default());
        assert!(res.is_err());
    }

    #[test]
    fn trailing_cdata_rejected() {
        let xml = "<Root/><![CDATA[x]]>";
        let res = read_tree::<Ns>(xml.as_bytes(), "Root", &XmlLimits::default());
        assert!(res.is_err());
    }

    #[test]
    fn leading_xml_s_accepted() {
        let xml = "\r\n\t <Root/>";
        let res = read_tree::<Ns>(xml.as_bytes(), "Root", &XmlLimits::default());
        assert!(res.is_ok());
    }
}