//! A small, bounded scan of an XMP packet for the properties people care about. Not an XML parser: it looks for //! `prefix:name="value"` attributes and `...` elements and takes the text inside. use crate::{Ctx, Group, text}; const PROPS: &[(&str, &str)] = &[ ("dc:creator", "Creator"), ("dc:rights", "Rights"), ("dc:title", "Title"), ("dc:description", "Description"), ("dc:subject", "Keywords"), ("xmp:CreatorTool", "Creator tool"), ("xmp:CreateDate", "Created"), ("xmp:ModifyDate", "Modified"), ("xmp:MetadataDate", "Metadata date"), ("pdf:Producer", "Producer"), ("pdf:Keywords", "Keywords"), ("photoshop:AuthorsPosition", "Author's position"), ("photoshop:CaptionWriter", "Caption writer"), ("photoshop:City", "City"), ("photoshop:State", "State"), ("photoshop:Country", "Country"), ("photoshop:Credit", "Credit"), ("photoshop:Source", "Source"), ("photoshop:DateCreated", "Date created"), ("Iptc4xmpCore:Location", "Location"), ("xmpRights:Owner", "Rights owner"), ("exif:GPSLatitude", "GPS latitude"), ("exif:GPSLongitude", "GPS longitude"), ("exif:DateTimeOriginal", "Taken"), ("tiff:Make", "Camera make"), ("tiff:Model", "Camera model"), ("tiff:Artist", "Artist"), ("aux:SerialNumber", "Camera serial number"), ("aux:Lens", "Lens"), ("exifEX:BodySerialNumber", "Camera serial number"), ("xmpMM:DocumentID", "Document ID"), ("xmpMM:OriginalDocumentID", "Original document ID"), ("xmpMM:InstanceID", "Instance ID"), ("stEvt:softwareAgent", "Edited with"), ]; /// Adds an item for the packet itself and one per property found. pub(crate) fn read_packet(packet: &[u8], group: Group, what: &str, ctx: &mut Ctx) { ctx.push(group, what, crate::bytes_label(packet.len())); let s = String::from_utf8_lossy(packet); for &(prop, label) in PROPS { if let Some(v) = find_value(&s, prop).filter(|v| !v.is_empty()) { ctx.push(group, format!("{label} ({prop})"), v); } } } fn find_value(s: &str, prop: &str) -> Option { element_value(s, prop).or_else(|| attribute_value(s, prop)) } fn attribute_value(s: &str, prop: &str) -> Option { let mut from = 0; while let Some(i) = s.get(from..)?.find(prop) { let at = from + i; from = at + prop.len(); let before_ok = s[..at].chars().next_back().is_some_and(char::is_whitespace); let after = s[from..].trim_start(); let Some(after) = after.strip_prefix('=') else { continue }; let after = after.trim_start(); let Some(quote) = after.chars().next().filter(|c| *c == '"' || *c == '\'') else { continue }; if !before_ok { continue; } let body = &after[1..]; let end = body.find(quote)?; return Some(text::xml_unescape(body[..end].trim())); } None } fn element_value(s: &str, prop: &str) -> Option { let open = format!("<{prop}"); let close = format!(""); let mut from = 0; while let Some(i) = s.get(from..)?.find(&open) { let start = from + i + open.len(); from = start; let next = s[start..].chars().next()?; if !(next == '>' || next == '/' || next.is_whitespace()) { continue; } let tag_end = start + s[start..].find('>')?; if s[..tag_end].ends_with('/') { // or an empty element: nothing to show here. continue; } let end = tag_end + s[tag_end..].find(&close)?; let inner = &s[tag_end + 1..end]; return Some(text_nodes(inner)); } None } /// The text between tags, trimmed, joined with "; ". fn text_nodes(inner: &str) -> String { let mut parts: Vec = Vec::new(); let mut rest = inner; loop { let (txt, after) = match rest.find('<') { Some(i) => (&rest[..i], Some(&rest[i..])), None => (rest, None), }; let t = txt.trim(); if !t.is_empty() { parts.push(text::xml_unescape(t)); } match after.and_then(|a| a.find('>').map(|j| &a[j + 1..])) { Some(next) => rest = next, None => break, } } parts.join("; ") } #[cfg(test)] mod tests { use super::*; #[test] fn finds_both_forms() { let x = r#" JanEva no"#; assert_eq!(find_value(x, "dc:creator").as_deref(), Some("Jan; Eva")); assert_eq!(find_value(x, "xmp:CreatorTool").as_deref(), Some("Tool & Co")); assert_eq!(find_value(x, "tiff:Make").as_deref(), Some("Apple")); assert_eq!(find_value(x, "dc:title"), None); assert_eq!(find_value("