diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index 2547e73d8..d668de277 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -6,7 +6,7 @@ mod binary; mod bytes; mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; -pub use xml::{parse_xml, parse_xml_with_limits}; +pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index be225d3fb..d30b938d0 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -1,5 +1,18 @@ //! XML property-list reader ported from Craft Anything at 8f3b7751. use crate::{DateValue, FormatError, Limits, Value}; + +/// XML-specific representation policy, separate from resource allowances. +#[derive(Clone, Copy, Debug)] +pub struct XmlOptions { + /// Trim surrounding whitespace in date text, as the original Craft Anything reader does. + /// Disable for consumers that retain dates as opaque strings, such as Luminar presets. + pub trim_date_text: bool, +} +impl Default for XmlOptions { + fn default() -> Self { + Self { trim_date_text: true } + } +} fn decode_base64(text: &str, url_safe: bool) -> Option> { let mut out = Vec::with_capacity(text.len() / 4 * 3); let mut acc = 0u32; @@ -216,6 +229,7 @@ struct Xml { nodes: usize, limits: Limits, bytes_left: usize, + options: XmlOptions, } impl Xml { @@ -288,7 +302,10 @@ impl Xml { .map(Value::Real) .ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } - "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), + "date" => { + let text = self.text_until("date")?; + Ok(Value::Date(DateValue::Text(if self.options.trim_date_text { text.trim().to_string() } else { text }))) + } "data" => { let t = self.text_until("data")?; base64_decode(&t).map(Value::Data).ok_or_else(|| FormatError::Malformed("bad base64 in ".into())) @@ -364,6 +381,11 @@ pub fn parse_xml(data: &[u8]) -> Result { /// `max_nodes` both cap values. Nesting never exceeds 256 even with a larger caller limit. /// Data text consumes its encoded size, conservatively including whitespace. pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result { + parse_xml_with_options(data, limits, XmlOptions::default()) +} + +/// Parse bounded XML using an explicit date-text representation policy. +pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) -> Result { if data.len() > limits.max_input_bytes { return Err(FormatError::TooLarge("input bytes".into())); } @@ -385,7 +407,7 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Resultkv", limits).is_ok()); } + #[test] + fn callers_can_preserve_opaque_date_whitespace() { + let xml = b" 2026-01-01T00:00:00Z "; + assert_eq!(parse_xml(xml).unwrap(), Value::Date(DateValue::Text("2026-01-01T00:00:00Z".into()))); + assert_eq!( + parse_xml_with_options(xml, Limits::default(), XmlOptions { trim_date_text: false }).unwrap(), + Value::Date(DateValue::Text(" 2026-01-01T00:00:00Z ".into())) + ); + } }