Files
Pictor-public/crates/meta/src/webp.rs
T

106 lines
3.4 KiB
Rust

//! WebP at the RIFF chunk level: EXIF and XMP chunks are dropped, VP8X flags cleared to match, the rest copied.
use crate::{Ctx, Group, MetaError, exif_items, text, xmp};
const MAX_CHUNKS: usize = 1_000_000;
const FLAG_EXIF: u8 = 0x08;
const FLAG_XMP: u8 = 0x04;
struct Chunk {
id: [u8; 4],
/// From the id to the end of the data, including the pad byte when there is one.
span: (usize, usize),
data: (usize, usize),
}
struct Parsed {
chunks: Vec<Chunk>,
/// Bytes after the RIFF container.
trailing: usize,
error: Option<MetaError>,
}
fn parse(b: &[u8]) -> Parsed {
let mut p = Parsed { chunks: Vec::new(), trailing: 0, error: None };
let declared = text::le32(b, 4).map_or(0, |s| s as usize).saturating_add(8);
let end = if declared > b.len() {
p.error = Some(MetaError::truncated());
b.len()
} else {
p.trailing = b.len() - declared;
declared
};
let mut pos = 12;
while pos < end {
if p.chunks.len() >= MAX_CHUNKS {
p.error = Some(MetaError::Damaged("it has too many chunks".into()));
break;
}
let (Some(id), Some(size)) = (b.get(pos..pos + 4), text::le32(b, pos + 4)) else {
p.error = Some(MetaError::truncated());
break;
};
let data_start = pos + 8;
let Some(data_end) = data_start.checked_add(size as usize).filter(|&e| e <= end) else {
p.error = Some(MetaError::truncated());
break;
};
// Odd sizes are followed by a pad byte; a missing pad at the very end is tolerated.
let span_end = (data_end + (size as usize & 1)).min(end);
p.chunks.push(Chunk { id: [id[0], id[1], id[2], id[3]], span: (pos, span_end), data: (data_start, data_end) });
pos = span_end;
}
p
}
fn is_dropped(id: &[u8; 4]) -> bool {
id == b"EXIF" || id == b"XMP "
}
pub(crate) fn read(b: &[u8], ctx: &mut Ctx) -> Result<(), MetaError> {
let parsed = parse(b);
for c in &parsed.chunks {
let data = &b[c.data.0..c.data.1];
match &c.id {
b"EXIF" => {
let raw = if data.starts_with(b"Exif\0\0") { &data[6..] } else { data };
exif_items::read_block(raw, ctx);
}
b"XMP " => xmp::read_packet(data, Group::Xmp, "XMP packet", ctx),
_ => {}
}
}
if parsed.trailing > 0 {
ctx.push(Group::Other, "Data after the end of the image", crate::bytes_label(parsed.trailing));
}
if let Some(e) = parsed.error {
if parsed.chunks.is_empty() {
return Err(e);
}
ctx.warn(format!("{e} Only part of it could be read."));
}
Ok(())
}
pub(crate) fn strip(b: &[u8]) -> Result<Vec<u8>, MetaError> {
let parsed = parse(b);
if let Some(e) = parsed.error {
return Err(e);
}
let mut out = Vec::with_capacity(b.len());
out.extend_from_slice(b"RIFF\0\0\0\0WEBP");
for c in parsed.chunks.iter().filter(|c| !is_dropped(&c.id)) {
let start = out.len();
out.extend_from_slice(&b[c.span.0..c.span.1]);
if &c.id == b"VP8X" && c.data.1 > c.data.0 {
out[start + 8] &= !(FLAG_EXIF | FLAG_XMP);
}
if (c.span.1 - c.span.0) % 2 == 1 {
out.push(0);
}
}
let riff_size = u32::try_from(out.len() - 8).map_err(|_| MetaError::Write("the file is too large".into()))?;
out[4..8].copy_from_slice(&riff_size.to_le_bytes());
Ok(out)
}