GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
//! Minimal XML writer and read helpers over `roxmltree`.

use std::fmt::Write as _;

use roxmltree::Node;

pub(crate) fn escape(s: &str) -> String {
    let mut o = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => o.push_str("&"),
            '<' => o.push_str("&lt;"),
            '>' => o.push_str("&gt;"),
            '"' => o.push_str("&quot;"),
            '\'' => o.push_str("&apos;"),
            c if (c as u32) < 0x20 && !matches!(c, '\n' | '\r' | '\t') => {}
            c => o.push(c),
        }
    }
    o
}

/// Like `escape`, but also encodes whitespace that attribute-value
/// normalization would otherwise turn into spaces.
pub(crate) fn escape_attr(s: &str) -> String {
    let mut o = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '\n' => o.push_str("&#10;"),
            '\r' => o.push_str("&#13;"),
            '\t' => o.push_str("&#9;"),
            c => o.push_str(&escape(c.encode_utf8(&mut [0; 4]))),
        }
    }
    o
}

/// Pretty-printing XML writer (two-space indent, one element per line).
pub(crate) struct XmlWriter {
    out: String,
    stack: Vec<String>,
}

impl XmlWriter {
    pub fn new(doctype: Option<&str>) -> Self {
        let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        if let Some(d) = doctype {
            let _ = writeln!(out, "<!DOCTYPE {d}>");
        }
        Self { out, stack: Vec::new() }
    }

    fn indent(&mut self) {
        for _ in 0..self.stack.len() {
            self.out.push_str("  ");
        }
    }

    fn start_tag(&mut self, name: &str, attrs: &[(&str, &str)]) {
        self.indent();
        self.out.push('<');
        self.out.push_str(name);
        for (k, v) in attrs {
            let _ = write!(self.out, " {k}=\"{}\"", escape_attr(v));
        }
    }

    pub fn open(&mut self, name: &str, attrs: &[(&str, &str)]) {
        self.start_tag(name, attrs);
        self.out.push_str(">\n");
        self.stack.push(name.to_string());
    }

    pub fn close(&mut self) {
        let Some(name) = self.stack.pop() else { return };
        self.indent();
        let _ = writeln!(self.out, "</{name}>");
    }

    pub fn empty(&mut self, name: &str, attrs: &[(&str, &str)]) {
        self.start_tag(name, attrs);
        self.out.push_str("/>\n");
    }

    /// `<name attrs>value</name>`.
    pub fn text_attrs(&mut self, name: &str, attrs: &[(&str, &str)], value: &str) {
        self.start_tag(name, attrs);
        let _ = writeln!(self.out, ">{}</{name}>", escape(value));
    }

    pub fn text(&mut self, name: &str, value: impl std::fmt::Display) {
        self.text_attrs(name, &[], &value.to_string());
    }

    pub fn finish(mut self) -> String {
        while !self.stack.is_empty() {
            self.close();
        }
        self.out
    }
}

pub(crate) fn child<'a, 'i>(n: Node<'a, 'i>, name: &str) -> Option<Node<'a, 'i>> {
    n.children().find(|c| c.is_element() && c.tag_name().name() == name)
}

pub(crate) fn children<'a, 'i>(n: Node<'a, 'i>, name: &'static str) -> impl Iterator<Item = Node<'a, 'i>> {
    n.children().filter(move |c| c.is_element() && c.tag_name().name() == name)
}

pub(crate) fn elements<'a, 'i>(n: Node<'a, 'i>) -> impl Iterator<Item = Node<'a, 'i>> {
    n.children().filter(|c| c.is_element())
}

/// Trimmed text of a node's direct text content.
pub(crate) fn text<'a>(n: Node<'a, '_>) -> &'a str {
    n.text().map(str::trim).unwrap_or("")
}

/// Text of the element at `path` below `n`.
pub(crate) fn path_text<'a>(n: Node<'a, '_>, path: &[&str]) -> Option<&'a str> {
    let mut cur = n;
    for p in path {
        cur = child(cur, p)?;
    }
    Some(text(cur))
}

pub(crate) fn child_text<'a>(n: Node<'a, '_>, name: &str) -> Option<&'a str> {
    child(n, name).map(text)
}

pub(crate) fn child_i64(n: Node<'_, '_>, name: &str) -> Option<i64> {
    child_text(n, name).and_then(|t| t.parse::<i64>().ok().or_else(|| t.parse::<f64>().ok().map(|f| f.round() as i64)))
}

pub(crate) fn child_f64(n: Node<'_, '_>, name: &str) -> Option<f64> {
    child_text(n, name).and_then(|t| t.parse::<f64>().ok())
}

pub(crate) fn child_bool(n: Node<'_, '_>, name: &str) -> Option<bool> {
    child_text(n, name).map(|t| t.eq_ignore_ascii_case("true") || t == "1")
}

pub(crate) fn bool_str(b: bool) -> &'static str {
    if b { "TRUE" } else { "FALSE" }
}

/// Format an f64 compactly (no trailing zeros, no exponent for normal ranges).
pub(crate) fn num(v: f64) -> String {
    if v == v.round() && v.abs() < 1e15 {
        return format!("{}", v as i64);
    }
    if !v.is_finite() {
        return "0".into();
    }
    // Shortest representation that parses back to the same f64.
    format!("{v}")
}

pub(crate) fn parse(text: &str) -> Result<roxmltree::Document<'_>, String> {
    // FCP7 XML commonly carries a plain `<!DOCTYPE xmeml>` declaration, which
    // roxmltree accepts only with DTD parsing enabled. Internal subsets are not
    // needed by any supported interchange format and can expand entities far
    // beyond the input size, so reject them before handing the document to the
    // parser. External entities remain unresolved because no resolver is set.
    // One forward pass: comments, CDATA sections and processing instructions are skipped (a
    // `<!DOCTYPE` inside one is not a declaration), and quoted literals are skipped so a `>`
    // inside a system identifier cannot hide the `[`.
    if has_internal_subset(text) {
        return Err("internal DTD subsets are not supported".into());
    }
    // A node ceiling keeps a large document from multiplying parse work with the file's size
    // (audit: FilmCraft F3). Real interchange documents are far below it.
    const MAX_NODES: u32 = 1_000_000;
    let opts = roxmltree::ParsingOptions { allow_dtd: true, nodes_limit: MAX_NODES, ..Default::default() };
    roxmltree::Document::parse_with_options(text, opts).map_err(|e| e.to_string())
}

/// Whether a `<!DOCTYPE` declaration in `text` opens an internal subset (`[`) before its closing
/// `>`. Linear in the text: each part of it is looked at once.
fn has_internal_subset(text: &str) -> bool {
    let mut at = 0;
    while let Some(off) = text[at..].find('<') {
        let start = at + off;
        let rest = &text[start..];
        // What closes a section that has to be skipped whole.
        let skip = [("<!--", "-->"), ("<![CDATA[", "]]>"), ("<?", "?>")].into_iter().find(|(open, _)| rest.starts_with(open));
        if let Some((open, close)) = skip {
            match rest[open.len()..].find(close) {
                Some(end) => at = start + open.len() + end + close.len(),
                None => return false,
            }
        } else if let Some(decl) = rest.strip_prefix("<!DOCTYPE") {
            let mut quote = None;
            let mut end = None;
            for (i, c) in decl.char_indices() {
                match (quote, c) {
                    (Some(q), _) if q == c => quote = None,
                    (Some(_), _) => {}
                    (None, '"' | '\'') => quote = Some(c),
                    (None, '[') => return true,
                    (None, '>') => {
                        end = Some(i + 1);
                        break;
                    }
                    _ => {}
                }
            }
            match end {
                Some(e) => at = start + "<!DOCTYPE".len() + e,
                None => return false,
            }
        } else {
            at = start + 1;
        }
    }
    false
}

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

    #[test]
    fn writer_roundtrip() {
        let mut w = XmlWriter::new(Some("xmeml"));
        w.open("a", &[("x", "1 & 2")]);
        w.text("b", "<hi>");
        w.empty("c", &[]);
        let s = w.finish();
        let d = parse(&s).unwrap();
        let a = d.root_element();
        assert_eq!(a.attribute("x"), Some("1 & 2"));
        assert_eq!(child_text(a, "b"), Some("<hi>"));
        assert!(child(a, "c").is_some());
        assert_eq!(num(1.5), "1.5");
        assert_eq!(num(100.0), "100");
        assert_eq!(num(-0.0), "0");
        assert_eq!(num(0.1 + 0.2).parse::<f64>().unwrap(), 0.1 + 0.2);
    }

    #[test]
    fn internal_dtd_subsets_are_rejected_wherever_they_hide() {
        assert!(parse("<!DOCTYPE xmeml><xmeml/>").is_ok());
        assert!(parse("<!DOCTYPE xmeml SYSTEM \"a>b\"><xmeml/>").is_ok());
        // the first marker sits in a comment; the real declaration follows
        let hidden = "<!-- <!DOCTYPE x> --><!DOCTYPE xmeml [<!ENTITY a \"aaaa\">]><xmeml>&a;</xmeml>";
        assert_eq!(parse(hidden).unwrap_err(), "internal DTD subsets are not supported");
        // a `>` inside the system literal must not end the declaration early
        let quoted = "<!DOCTYPE xmeml SYSTEM \"a>b\" [<!ENTITY a \"aaaa\">]><xmeml>&a;</xmeml>";
        assert_eq!(parse(quoted).unwrap_err(), "internal DTD subsets are not supported");
    }

    #[test]
    fn doctype_scan_is_linear_in_the_input() {
        // Many markers inside one comment, and many with no closing `>` or `[` at all, were
        // rescanned from each marker to the end.
        let marker = "<!DOCTYPE ";
        let in_comment = format!("<!--{}--><x/>", marker.repeat(100_000));
        let unclosed = format!("<x>{}", marker.repeat(100_000));
        let start = std::time::Instant::now();
        assert!(parse(&in_comment).is_ok());
        assert!(parse(&unclosed).is_err());
        assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
        // A declaration after a CDATA section and a processing instruction is still found.
        assert!(parse("<?pi <!DOCTYPE a> ?><!DOCTYPE x [<!ENTITY a \"b\">]><x/>").is_err());
    }

    #[test]
    fn attr_whitespace_roundtrip() {
        let mut w = XmlWriter::new(None);
        w.open("a", &[("name", "x\ny\tz\r")]);
        w.text("b", "a\nb");
        let s = w.finish();
        let d = parse(&s).unwrap();
        let a = d.root_element();
        assert_eq!(a.attribute("name"), Some("x\ny\tz\r"));
        assert_eq!(child(a, "b").and_then(|b| b.text()), Some("a\nb"));
    }

    #[test]
    fn rejects_internal_dtd_entity_expansion() {
        let text = "<!DOCTYPE root [<!ENTITY word 'expanded'>]><root>&word;</root>";
        assert_eq!(parse(text).unwrap_err(), "internal DTD subsets are not supported");
    }
}