//! Photoshop image resource blocks ("Photoshop 3.0", 8BIM) and the IPTC-IIM records inside them. use crate::{Ctx, Group, text}; pub(crate) const PHOTOSHOP_SIGNATURE: &[u8] = b"Photoshop 3.0\0"; /// Reads an APP13 payload (starting with "Photoshop 3.0\0"). pub(crate) fn read_photoshop(payload: &[u8], ctx: &mut Ctx) { ctx.push(Group::Iptc, "Photoshop block", crate::bytes_label(payload.len())); let Some(mut b) = payload.strip_prefix(PHOTOSHOP_SIGNATURE) else { ctx.warn("A Photoshop block has an unknown layout; it is removed all the same."); return; }; let mut others = 0usize; while b.len() >= 4 { if &b[..4] != b"8BIM" { ctx.warn("Part of the Photoshop block is damaged; it is removed all the same."); break; } let Some(id) = text::be16(b, 4) else { break }; let Some(&name_len) = b.get(6) else { break }; // Pascal string, the whole (length byte + name) padded to an even size. let name_total = (1 + usize::from(name_len) + 1) & !1; let size_at = 6 + name_total; let Some(size) = text::be32(b, size_at) else { break }; let data_at = size_at + 4; let Some(data) = usize::try_from(size).ok().and_then(|s| b.get(data_at..data_at.checked_add(s)?)) else { ctx.warn("A Photoshop block ends too early; it is removed all the same."); break; }; match id { 0x0404 => read_iim(data, ctx), 0x0409 | 0x040C => ctx.push(Group::Other, "Photoshop thumbnail", crate::bytes_label(data.len())), 0x0422 | 0x0423 => { crate::exif_items::read_block(data, ctx); } 0x0424 => crate::xmp::read_packet(data, Group::Xmp, "XMP packet (in Photoshop block)", ctx), _ => others += 1, } let next = data_at + data.len() + (data.len() & 1); b = b.get(next..).unwrap_or(&[]); } if others > 0 { ctx.push(Group::Other, "Photoshop resources", format!("{others}")); } } /// IPTC-IIM datasets: 0x1C, record, dataset, length (u16, or an extended length), data. pub(crate) fn read_iim(mut b: &[u8], ctx: &mut Ctx) { while let [0x1C, record, dataset, ..] = *b { let Some(len) = text::be16(b, 3) else { break }; let (len, head) = if len & 0x8000 != 0 { // Extended dataset: the low 15 bits give how many bytes the real length takes. let n = usize::from(len & 0x7FFF); if n == 0 || n > 4 { break; } let Some(bytes) = b.get(5..5 + n) else { break }; (bytes.iter().fold(0usize, |acc, &x| (acc << 8) | usize::from(x)), 5 + n) } else { (usize::from(len), 5) }; let Some(data) = b.get(head..head.saturating_add(len)) else { ctx.warn("The IPTC data ends too early; it is removed all the same."); break; }; if record == 2 && dataset != 0 { let name = dataset_name(dataset).map_or_else(|| format!("IPTC 2:{dataset}"), str::to_string); ctx.push(Group::Iptc, name, text::lossy(data)); } b = &b[head + len..]; } } fn dataset_name(d: u8) -> Option<&'static str> { Some(match d { 5 => "Object name", 25 => "Keyword", 40 => "Special instructions", 55 => "Date created", 60 => "Time created", 62 => "Digital creation date", 65 => "Originating program", 80 => "By-line (author)", 85 => "By-line title", 90 => "City", 92 => "Sub-location", 95 => "Province or state", 100 => "Country code", 101 => "Country", 103 => "Transmission reference", 105 => "Headline", 110 => "Credit", 115 => "Source", 116 => "Copyright notice", 118 => "Contact", 120 => "Caption", 122 => "Caption writer", _ => return None, }) }