Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Stream motion-photo file processing

Neeraj Gupta3fa1a9e31f57
diff --git a/rust/crates/photos/src/motion_photo/ftyp.rs b/rust/crates/photos/src/motion_photo/ftyp.rs
index fd2c240db1..88ddaf67a5 100644
--- a/rust/crates/photos/src/motion_photo/ftyp.rs
+++ b/rust/crates/photos/src/motion_photo/ftyp.rs
@@ -1,4 +1,5 @@
-use super::VideoIndex;
+use super::{BUFFER_SIZE, VideoIndex};
+use std::io::{self, Read};
const FTYP_BOX_MIN_SIZE: u32 = 8;
@@ -14,62 +15,107 @@ const KNOWN_VIDEO_BRANDS: &[[u8; 4]] = &[
// Some phones embed preview and full MP4s in either order. Return the largest
// valid segment.
-pub(super) fn find_largest_ftyp_segment(bytes: &[u8]) -> Option<VideoIndex> {
- let len = bytes.len();
- if len < 12 {
- return None;
- }
+pub(super) fn find_largest_ftyp_segment<R: Read>(
+ mut reader: R,
+ size: usize,
+) -> io::Result<Option<VideoIndex>> {
+ // Retain enough bytes to recognize a size/type/brand header across reads.
+ const OVERLAP: usize = 11;
+ let mut buffer = vec![0; BUFFER_SIZE + OVERLAP];
+ let mut carried = 0;
+ let mut offset = 0;
+ let mut remaining = size;
+ let mut previous_start = None;
+ let mut best: Option<VideoIndex> = None;
- let mut starts: Vec<usize> = Vec::new();
- let last = len.saturating_sub(8);
- let mut i = 4;
- while i <= last {
- if bytes[i] == b'f' && bytes[i + 1] == b't' && bytes[i + 2] == b'y' && bytes[i + 3] == b'p'
- {
- let box_start = i - 4;
- let box_size = u32::from_be_bytes([
- bytes[box_start],
- bytes[box_start + 1],
- bytes[box_start + 2],
- bytes[box_start + 3],
- ]);
+ while remaining > 0 {
+ let length = remaining.min(BUFFER_SIZE);
+ reader.read_exact(&mut buffer[carried..carried + length])?;
+ let available = carried + length;
+ for (position, header) in buffer[..available].windows(12).enumerate() {
+ if &header[4..8] != b"ftyp" {
+ continue;
+ }
+ let start = offset + position;
+ let box_size = u32::from_be_bytes([header[0], header[1], header[2], header[3]]);
// An ftyp box at byte 0 belongs to a standalone MP4.
- if (FTYP_BOX_MIN_SIZE..=FTYP_BOX_MAX_SIZE).contains(&box_size) && box_start > 0 {
- let brand: [u8; 4] = [bytes[i + 4], bytes[i + 5], bytes[i + 6], bytes[i + 7]];
- if KNOWN_VIDEO_BRANDS.contains(&brand) {
- starts.push(box_start);
- }
+ if start == 0
+ || !(FTYP_BOX_MIN_SIZE..=FTYP_BOX_MAX_SIZE).contains(&box_size)
+ || !KNOWN_VIDEO_BRANDS
+ .iter()
+ .any(|brand| header[8..12] == *brand)
+ {
+ continue;
+ }
+ if let Some(previous) = previous_start {
+ keep_largest(&mut best, previous, start);
}
+ previous_start = Some(start);
}
- i += 1;
+ carried = available.min(OVERLAP);
+ buffer.copy_within(available - carried..available, 0);
+ offset += available - carried;
+ remaining -= length;
}
+ if let Some(start) = previous_start {
+ keep_largest(&mut best, start, size);
+ }
+ Ok(best)
+}
- if starts.is_empty() {
- return None;
+fn keep_largest(best: &mut Option<VideoIndex>, start: usize, end: usize) {
+ if best
+ .as_ref()
+ .is_none_or(|index| end - start > index.end - index.start)
+ {
+ *best = Some(VideoIndex { start, end });
}
- if starts.len() == 1 {
- return Some(VideoIndex {
- start: starts[0],
- end: len,
- });
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::io::Cursor;
+
+ #[test]
+ fn recognizes_headers_across_chunks_and_keeps_first_on_ties() {
+ let header = b"\0\0\0\x10ftypmp42\0\0\0\0";
+ for boundary in [1, 3, 5] {
+ for split in 0..12 {
+ let start = BUFFER_SIZE * boundary - split;
+ let mut source = vec![0; start];
+ source.extend_from_slice(header);
+ source.extend_from_slice(header);
+ assert_eq!(
+ find_largest_ftyp_segment(Cursor::new(&source), source.len()).unwrap(),
+ Some(VideoIndex {
+ start,
+ end: start + header.len()
+ }),
+ "header split at {split} near chunk {boundary}"
+ );
+ }
+ }
}
- let mut best_index = VideoIndex {
- start: starts[0],
- end: starts[1],
- };
- let mut best_size = 0usize;
- for (idx, &start) in starts.iter().enumerate() {
- let end = if idx + 1 < starts.len() {
- starts[idx + 1]
- } else {
- len
- };
- let size = end - start;
- if size > best_size {
- best_size = size;
- best_index = VideoIndex { start, end };
+ #[test]
+ fn limits_read_requests_and_reports_truncated_input() {
+ struct ShortReads(Cursor<Vec<u8>>);
+ impl Read for ShortReads {
+ fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
+ assert!(buffer.len() <= BUFFER_SIZE);
+ let length = buffer.len().min(17);
+ self.0.read(&mut buffer[..length])
+ }
}
+ let length = BUFFER_SIZE * 3 + 7;
+ let mut reader = ShortReads(Cursor::new(vec![0; length]));
+ assert_eq!(
+ find_largest_ftyp_segment(&mut reader, length).unwrap(),
+ None
+ );
+ assert_eq!(reader.0.position(), length as u64);
+ let error = find_largest_ftyp_segment(Cursor::new([0; 12]), 13).unwrap_err();
+ assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof);
}
- Some(best_index)
}
diff --git a/rust/crates/photos/src/motion_photo/mod.rs b/rust/crates/photos/src/motion_photo/mod.rs
index 15724227a1..ab3013a6e1 100644
--- a/rust/crates/photos/src/motion_photo/mod.rs
+++ b/rust/crates/photos/src/motion_photo/mod.rs
@@ -4,9 +4,13 @@ mod xmp;
use ftyp::find_largest_ftyp_segment;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
-use std::fs;
+use std::fs::{self, File, OpenOptions};
+use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Component, Path, PathBuf};
+const BUFFER_SIZE: usize = 64 * 1024;
+const MAX_VIDEO_BYTES_SIZE: usize = 64 * 1024 * 1024;
+
const ITEM_LENGTH_OFFSET_KEY: &str = "Item:Length";
const GCAMERA_MOTION_PHOTO: &str = "GCamera:MotionPhoto";
const ITEM_MIME_TYPE: &str = "Item:Mime";
@@ -25,6 +29,8 @@ pub enum MotionPhotoError {
InvalidIndex,
InvalidFileName,
VideoNotFound,
+ VideoTooLarge,
+ XmpTooLarge,
}
impl Display for MotionPhotoError {
@@ -35,6 +41,11 @@ impl Display for MotionPhotoError {
Self::InvalidIndex => write!(f, "invalid video index"),
Self::InvalidFileName => write!(f, "invalid output file name"),
Self::VideoNotFound => write!(f, "unable to find video index"),
+ Self::VideoTooLarge => write!(
+ f,
+ "video exceeds {MAX_VIDEO_BYTES_SIZE} byte limit; use file extraction"
+ ),
+ Self::XmpTooLarge => write!(f, "xmp exceeds {} byte limit", xmp::MAX_XMP_SIZE),
}
}
}
@@ -50,40 +61,64 @@ impl From<std::io::Error> for MotionPhotoError {
pub fn get_motion_video_index_from_path<P: AsRef<Path>>(
file_path: P,
) -> Result<Option<VideoIndex>, MotionPhotoError> {
- let bytes = fs::read(file_path)?;
- Ok(get_motion_video_index(&bytes))
+ let mut file = File::open(file_path)?;
+ let size = file_size(&file)?;
+ get_motion_video_index(&mut file, size)
+}
+
+fn file_size(file: &File) -> Result<usize, MotionPhotoError> {
+ usize::try_from(file.metadata()?.len()).map_err(|_| MotionPhotoError::InvalidIndex)
}
-fn get_motion_video_index(bytes: &[u8]) -> Option<VideoIndex> {
- if let Some(index) = find_largest_ftyp_segment(bytes) {
- return Some(index);
+fn get_motion_video_index<R: Read + Seek>(
+ reader: &mut R,
+ size: usize,
+) -> Result<Option<VideoIndex>, MotionPhotoError> {
+ reader.rewind()?;
+ if let Some(index) = find_largest_ftyp_segment(&mut *reader, size)? {
+ return Ok(Some(index));
}
- extract_video_index_from_xmp(bytes)
+ reader.rewind()?;
+ match xmp::extract_xmp(reader.take(size as u64)) {
+ Ok(data) => Ok(extract_video_index_from_xmp(&data, size)),
+ Err(MotionPhotoError::Xml(_)) => Ok(None),
+ // Propagate limit and I/O errors so callers preserve existing motion-photo tags.
+ Err(err) => Err(err),
+ }
}
+// Extract up to 64 MiB into memory. Use file extraction for larger videos.
pub fn extract_motion_video_from_path<P: AsRef<Path>>(
file_path: P,
index: Option<VideoIndex>,
) -> Result<Option<Vec<u8>>, MotionPhotoError> {
- let bytes = fs::read(file_path)?;
- extract_motion_video(&bytes, index).map(|video| Some(video.to_vec()))
+ let mut file = File::open(file_path)?;
+ let size = file_size(&file)?;
+ let index = resolve_video_index(&mut file, size, index)?;
+ let length = index.end - index.start;
+ if length > MAX_VIDEO_BYTES_SIZE {
+ return Err(MotionPhotoError::VideoTooLarge);
+ }
+ file.seek(SeekFrom::Start(index.start as u64))?;
+ let mut video = vec![0; length];
+ file.read_exact(&mut video)?;
+ Ok(Some(video))
}
-fn extract_motion_video(
- bytes: &[u8],
+fn resolve_video_index<R: Read + Seek>(
+ reader: &mut R,
+ size: usize,
index: Option<VideoIndex>,
-) -> Result<&[u8], MotionPhotoError> {
- let video_index = index.or_else(|| get_motion_video_index(bytes));
- let Some(video_index) = video_index else {
- return Err(MotionPhotoError::VideoNotFound);
+) -> Result<VideoIndex, MotionPhotoError> {
+ let index = match index {
+ Some(index) => index,
+ None => get_motion_video_index(reader, size)?.ok_or(MotionPhotoError::VideoNotFound)?,
};
-
- if video_index.start >= video_index.end || video_index.end > bytes.len() {
+ if index.start >= index.end || index.end > size {
return Err(MotionPhotoError::InvalidIndex);
}
-
- Ok(&bytes[video_index.start..video_index.end])
+ Ok(index)
}
pub fn extract_motion_video_file_from_path<P: AsRef<Path>, Q: AsRef<Path>>(
@@ -94,14 +129,40 @@ pub fn extract_motion_video_file_from_path<P: AsRef<Path>, Q: AsRef<Path>>(
) -> Result<Option<PathBuf>, MotionPhotoError> {
validate_output_file_name(file_name)?;
- let bytes = fs::read(file_path)?;
- let video = extract_motion_video(&bytes, index)?;
+ let mut source = File::open(file_path)?;
+ let size = file_size(&source)?;
+ let index = resolve_video_index(&mut source, size, index)?;
+ source.seek(SeekFrom::Start(index.start as u64))?;
fs::create_dir_all(destination_directory.as_ref())?;
let output = destination_directory.as_ref().join(file_name);
- fs::write(&output, video)?;
+ // The output may alias the source. Copy forward before truncating so writes
+ // at offset zero never overwrite bytes that have not been read yet.
+ let mut destination = OpenOptions::new()
+ .write(true)
+ .create(true)
+ .truncate(false)
+ .open(&output)?;
+ let length = index.end - index.start;
+ copy_video(&mut source, &mut destination, length)?;
+ destination.set_len(length as u64)?;
Ok(Some(output))
}
+fn copy_video<R: Read, W: Write>(
+ source: &mut R,
+ destination: &mut W,
+ mut remaining: usize,
+) -> io::Result<()> {
+ let mut buffer = vec![0; BUFFER_SIZE];
+ while remaining > 0 {
+ let length = remaining.min(buffer.len());
+ source.read_exact(&mut buffer[..length])?;
+ destination.write_all(&buffer[..length])?;
+ remaining -= length;
+ }
+ Ok(())
+}
+
fn validate_output_file_name(file_name: &str) -> Result<(), MotionPhotoError> {
if file_name.is_empty() || file_name.contains('/') || file_name.contains('\\') {
return Err(MotionPhotoError::InvalidFileName);
@@ -117,14 +178,15 @@ fn validate_output_file_name(file_name: &str) -> Result<(), MotionPhotoError> {
pub fn extract_xmp_from_path<P: AsRef<Path>>(
file_path: P,
) -> Result<HashMap<String, String>, MotionPhotoError> {
- let bytes = fs::read(file_path)?;
- xmp::extract_xmp(&bytes)
+ let file = File::open(file_path)?;
+ let size = file_size(&file)?;
+ xmp::extract_xmp(file.take(size as u64))
}
-fn extract_video_index_from_xmp(bytes: &[u8]) -> Option<VideoIndex> {
- let xmp_data = xmp::extract_xmp(bytes).ok()?;
- let size = bytes.len();
-
+fn extract_video_index_from_xmp(
+ xmp_data: &HashMap<String, String>,
+ size: usize,
+) -> Option<VideoIndex> {
for offset_key in FILE_OFFSET_KEYS {
let Some(raw_offset) = xmp_data.get(offset_key) else {
continue;
@@ -133,14 +195,13 @@ fn extract_video_index_from_xmp(bytes: &[u8]) -> Option<VideoIndex> {
continue;
};
- if offset_key == ITEM_LENGTH_OFFSET_KEY
- && offset_from_end + offset_from_end < size
- && !has_motion_photo_tags(&xmp_data)
- {
+ if offset_from_end == 0 || offset_from_end > size {
continue;
}
-
- if offset_from_end == 0 || offset_from_end > size {
+ if offset_key == ITEM_LENGTH_OFFSET_KEY
+ && offset_from_end < size - offset_from_end
+ && !has_motion_photo_tags(xmp_data)
+ {
continue;
}
@@ -168,8 +229,25 @@ fn has_motion_photo_tags(xmp_data: &HashMap<String, String>) -> bool {
mod tests {
use super::*;
use std::fs;
+ use std::io::Cursor;
use tempfile::tempdir;
+ fn get_motion_video_index(bytes: &[u8]) -> Option<VideoIndex> {
+ super::get_motion_video_index(&mut Cursor::new(bytes), bytes.len()).expect("read index")
+ }
+
+ fn extract_motion_video(
+ bytes: &[u8],
+ index: Option<VideoIndex>,
+ ) -> Result<Vec<u8>, MotionPhotoError> {
+ let mut reader = Cursor::new(bytes);
+ let index = resolve_video_index(&mut reader, bytes.len(), index)?;
+ reader.seek(SeekFrom::Start(index.start as u64))?;
+ let mut result = Vec::new();
+ copy_video(&mut reader, &mut result, index.end - index.start)?;
+ Ok(result)
+ }
+
fn make_ftyp_box(brand: [u8; 4]) -> Vec<u8> {
let mut buf = Vec::with_capacity(16);
buf.extend_from_slice(&16u32.to_be_bytes());
@@ -324,10 +402,12 @@ mod tests {
#[test]
fn skips_invalid_item_length_without_motion_tags() {
- let mut bytes = b"prefix-data".to_vec();
- bytes.extend_from_slice(xmp_with_offset(8, "").as_bytes());
- bytes.extend_from_slice(b"small-tail");
- assert_eq!(get_motion_video_index(&bytes), None);
+ for offset in [8, usize::MAX] {
+ let mut bytes = b"prefix-data".to_vec();
+ bytes.extend_from_slice(xmp_with_offset(offset, "").as_bytes());
+ bytes.extend_from_slice(b"small-tail");
+ assert_eq!(get_motion_video_index(&bytes), None);
+ }
}
#[test]
@@ -416,12 +496,15 @@ mod tests {
fn file_extraction_preserves_selected_bytes_when_overwriting() {
let temp = tempdir().expect("temp dir");
let image = temp.path().join("source.jpg");
- let source = b"image-prefix-video-data-trailing-preview";
- let index = VideoIndex { start: 13, end: 23 };
+ let source: Vec<u8> = (0..BUFFER_SIZE * 3 + 19).map(|i| (i % 251) as u8).collect();
+ let index = VideoIndex {
+ start: 13,
+ end: source.len() - 7,
+ };
for file_name in ["clip.mp4", "source.jpg"] {
- fs::write(&image, source).expect("write source");
- fs::write(temp.path().join(file_name), source).expect("write existing output");
+ fs::write(&image, &source).expect("write source");
+ fs::write(temp.path().join(file_name), &source).expect("write existing output");
let output = extract_motion_video_file_from_path(
&image,
temp.path(),
@@ -441,7 +524,7 @@ mod tests {
16,
"GPano:ProjectionType=\"equirectangular\" GCamera:MotionPhoto=\"1\"",
);
- let xmp = xmp::extract_xmp(&bytes).expect("xmp must parse");
+ let xmp = xmp::extract_xmp(bytes.as_slice()).expect("xmp must parse");
assert_eq!(
xmp.get("GPano:ProjectionType"),
Some(&"equirectangular".to_string())
@@ -485,4 +568,103 @@ mod tests {
None
);
}
+
+ #[test]
+ fn file_extraction_preserves_source_aliases() {
+ let temp = tempdir().unwrap();
+ let image = temp.path().join("source.jpg");
+ let source: Vec<u8> = (0..BUFFER_SIZE * 2 + 9).map(|i| (i % 251) as u8).collect();
+ fs::write(&image, &source).unwrap();
+ fs::hard_link(&image, temp.path().join("hard.mp4")).unwrap();
+ #[cfg(unix)]
+ std::os::unix::fs::symlink(&image, temp.path().join("symbolic.mp4")).unwrap();
+
+ for output in ["hard.mp4", "symbolic.mp4"] {
+ if !temp.path().join(output).exists() {
+ continue;
+ }
+ for start in [0, 11] {
+ fs::write(&image, &source).unwrap();
+ let end = source.len() - 5;
+ extract_motion_video_file_from_path(
+ &image,
+ temp.path(),
+ output,
+ Some(VideoIndex { start, end }),
+ )
+ .unwrap();
+ assert_eq!(fs::read(&image).unwrap(), source[start..end]);
+ assert_eq!(
+ fs::read(temp.path().join(output)).unwrap(),
+ source[start..end]
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn file_extraction_streams_videos_above_the_byte_api_limit() {
+ let temp = tempdir().unwrap();
+ let image = temp.path().join("source.jpg");
+ let length = MAX_VIDEO_BYTES_SIZE + 1;
+ let mut file = File::create(&image).unwrap();
+ file.set_len(length as u64 + 1).unwrap();
+ file.seek(SeekFrom::Start(length as u64)).unwrap();
+ file.write_all(b"Z").unwrap();
+ let index = VideoIndex {
+ start: 1,
+ end: length + 1,
+ };
+ assert!(matches!(
+ extract_motion_video_from_path(&image, Some(index.clone())),
+ Err(MotionPhotoError::VideoTooLarge)
+ ));
+ let output =
+ extract_motion_video_file_from_path(&image, temp.path(), "clip.mp4", Some(index))
+ .unwrap()
+ .unwrap();
+ let mut result = File::open(output).unwrap();
+ assert_eq!(result.metadata().unwrap().len(), length as u64);
+ result.seek(SeekFrom::End(-1)).unwrap();
+ let mut last = [0];
+ result.read_exact(&mut last).unwrap();
+ assert_eq!(last, *b"Z");
+ }
+
+ #[test]
+ fn copying_is_bounded_and_rejects_short_input() {
+ struct BoundedReader(Cursor<Vec<u8>>);
+ impl Read for BoundedReader {
+ fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
+ assert!(buffer.len() <= BUFFER_SIZE);
+ self.0.read(buffer)
+ }
+ }
+ let length = BUFFER_SIZE * 3 + 5;
+ let mut source = BoundedReader(Cursor::new(vec![7; length]));
+ let mut output = Vec::new();
+ copy_video(&mut source, &mut output, length).unwrap();
+ assert_eq!(output, vec![7; length]);
+ let error = copy_video(&mut Cursor::new([1; 3]), &mut io::sink(), 4).unwrap_err();
+ assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof);
+ }
+
+ #[test]
+ fn ftyp_takes_precedence_over_oversized_xmp() {
+ let mut bytes = b"<x:xmpmeta>".to_vec();
+ bytes.resize(xmp::MAX_XMP_SIZE + 1, b' ');
+ assert!(matches!(
+ super::get_motion_video_index(&mut Cursor::new(&bytes), bytes.len()),
+ Err(MotionPhotoError::XmpTooLarge)
+ ));
+ let start = bytes.len();
+ bytes.extend_from_slice(&make_ftyp_box(*b"mp42"));
+ assert_eq!(
+ get_motion_video_index(&bytes),
+ Some(VideoIndex {
+ start,
+ end: bytes.len()
+ })
+ );
+ }
}
diff --git a/rust/crates/photos/src/motion_photo/xmp.rs b/rust/crates/photos/src/motion_photo/xmp.rs
index 5c86305645..0abb5be9e3 100644
--- a/rust/crates/photos/src/motion_photo/xmp.rs
+++ b/rust/crates/photos/src/motion_photo/xmp.rs
@@ -1,21 +1,55 @@
-use super::MotionPhotoError;
+use super::{BUFFER_SIZE, MotionPhotoError};
use std::collections::HashMap;
+use std::io::{BufRead, BufReader, Read};
const XMP_MARKER_BEGIN: &[u8] = b"<x:xmpmeta";
const XMP_MARKER_END: &[u8] = b"</x:xmpmeta>";
-pub(crate) fn extract_xmp(source: &[u8]) -> Result<HashMap<String, String>, MotionPhotoError> {
- let (begin, end) = extract_xmp_bounds(source)
- .ok_or_else(|| MotionPhotoError::Xml("xmp markers not found".to_string()))?;
- let xml_bytes = &source[begin..end];
- parse_xmp_attributes(xml_bytes)
+// Match the input budget of the EXIF reader; the photo itself can be larger.
+pub(super) const MAX_XMP_SIZE: usize = 8 * 1024 * 1024;
+
+pub(super) fn extract_xmp<R: Read>(source: R) -> Result<HashMap<String, String>, MotionPhotoError> {
+ let mut reader = BufReader::with_capacity(BUFFER_SIZE, source);
+ let mut matched = 0;
+ while matched < XMP_MARKER_BEGIN.len() {
+ let bytes = reader.fill_buf()?;
+ if bytes.is_empty() {
+ return Err(MotionPhotoError::Xml("xmp markers not found".to_string()));
+ }
+ let consumed = through_marker(bytes, XMP_MARKER_BEGIN, &mut matched);
+ reader.consume(consumed);
+ }
+
+ let mut xml = XMP_MARKER_BEGIN.to_vec();
+ matched = 0;
+ while matched < XMP_MARKER_END.len() {
+ let bytes = reader.fill_buf()?;
+ if bytes.is_empty() {
+ return Err(MotionPhotoError::Xml("xmp markers not found".to_string()));
+ }
+ let consumed = through_marker(bytes, XMP_MARKER_END, &mut matched);
+ if consumed > MAX_XMP_SIZE - xml.len() {
+ return Err(MotionPhotoError::XmpTooLarge);
+ }
+ xml.extend_from_slice(&bytes[..consumed]);
+ reader.consume(consumed);
+ }
+ parse_xmp_attributes(&xml)
}
-fn extract_xmp_bounds(source: &[u8]) -> Option<(usize, usize)> {
- let offset_begin = find_subslice(source, XMP_MARKER_BEGIN)?;
- let offset_end_start = find_subslice(&source[offset_begin..], XMP_MARKER_END)? + offset_begin;
- let offset_end = offset_end_start + XMP_MARKER_END.len();
- Some((offset_begin, offset_end))
+fn through_marker(bytes: &[u8], marker: &[u8], matched: &mut usize) -> usize {
+ for (index, &byte) in bytes.iter().enumerate() {
+ if byte == marker[*matched] {
+ *matched += 1;
+ if *matched == marker.len() {
+ return index + 1;
+ }
+ } else {
+ // Both markers contain '<' only at the start.
+ *matched = usize::from(byte == marker[0]);
+ }
+ }
+ bytes.len()
}
fn parse_xmp_attributes(xml_bytes: &[u8]) -> Result<HashMap<String, String>, MotionPhotoError> {
@@ -59,11 +93,79 @@ fn parse_xmp_attributes(xml_bytes: &[u8]) -> Result<HashMap<String, String>, Mot
Ok(result)
}
-fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
- if needle.is_empty() {
- return Some(0);
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn restarts_marker_matching_after_a_partial_match() {
+ for marker in [XMP_MARKER_BEGIN, XMP_MARKER_END] {
+ for prefix_length in 1..marker.len() {
+ let mut matched = 0;
+ through_marker(&marker[..prefix_length], marker, &mut matched);
+ assert_eq!(through_marker(marker, marker, &mut matched), marker.len());
+ assert_eq!(matched, marker.len());
+ }
+ }
+ }
+
+ #[test]
+ fn recognizes_both_markers_across_chunks() {
+ let begin = b"<x:xmpmeta>";
+ let attributes = br#"<rdf:Description GPano:ProjectionType="equirectangular"/>"#;
+ for marker in [XMP_MARKER_BEGIN, XMP_MARKER_END] {
+ for split in 1..marker.len() {
+ let prefix = if marker == XMP_MARKER_BEGIN {
+ BUFFER_SIZE - split
+ } else {
+ BUFFER_SIZE - split - begin.len() - attributes.len()
+ };
+ let source = [
+ vec![0; prefix],
+ begin.to_vec(),
+ attributes.to_vec(),
+ XMP_MARKER_END.to_vec(),
+ ]
+ .concat();
+ let result = extract_xmp(source.as_slice()).unwrap();
+ assert_eq!(
+ result.get("GPano:ProjectionType").unwrap(),
+ "equirectangular"
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn preserves_first_packet_attribute_and_utf8_semantics() {
+ let mut source = vec![0; BUFFER_SIZE * 2];
+ source.extend_from_slice(br#"<x:xmpmeta><rdf:Description xmlns:rdf="ignored" xml:lang="ignored" GPano:ProjectionType="old" note="A &amp; B"/><rdf:Description GPano:ProjectionType="cylindrical" invalid=""#);
+ source.push(0xff);
+ source.extend_from_slice(br#""/></x:xmpmeta><x:xmpmeta><rdf:Description GPano:ProjectionType="ignored"/></x:xmpmeta>"#);
+ let data = extract_xmp(source.as_slice()).unwrap();
+ assert_eq!(data.len(), 3);
+ assert_eq!(data.get("GPano:ProjectionType").unwrap(), "cylindrical");
+ assert_eq!(data.get("note").unwrap(), "A & B");
+ assert_eq!(data.get("invalid").unwrap(), "\u{fffd}");
+ }
+
+ #[test]
+ fn bounds_metadata_without_limiting_photo_size() {
+ let begin = b"<x:xmpmeta>";
+ let mut xml = begin.to_vec();
+ xml.resize(MAX_XMP_SIZE - XMP_MARKER_END.len(), b' ');
+ xml.extend_from_slice(XMP_MARKER_END);
+ // Trailing image data does not count toward the metadata budget.
+ xml.extend_from_slice(&[0; 32]);
+ assert!(extract_xmp(xml.as_slice()).unwrap().is_empty());
+ xml.insert(begin.len(), b' ');
+ assert!(matches!(
+ extract_xmp(xml.as_slice()),
+ Err(MotionPhotoError::XmpTooLarge)
+ ));
+ assert!(matches!(
+ extract_xmp(b"<x:xmpmeta>".as_slice()),
+ Err(MotionPhotoError::Xml(_))
+ ));
}
- haystack
- .windows(needle.len())
- .position(|window| window == needle)
}
diff --git a/rust/crates/photos/tests/motion_photos_assets_test.rs b/rust/crates/photos/tests/motion_photos_assets_test.rs
index c5cb106c74..c304a9ada7 100644
--- a/rust/crates/photos/tests/motion_photos_assets_test.rs
+++ b/rust/crates/photos/tests/motion_photos_assets_test.rs
@@ -1,5 +1,5 @@
use ente_photos::{
- extract_motion_video_file_from_path, extract_motion_video_from_path,
+ extract_motion_video_file_from_path, extract_motion_video_from_path, extract_xmp_from_path,
get_motion_video_index_from_path,
};
use std::path::{Path, PathBuf};
@@ -154,3 +154,22 @@ fn file_extraction_matches_video_bytes_when_fixtures_present() {
}
}
}
+
+#[test]
+fn reads_xmp_attributes_when_fixtures_present() {
+ for (name, length, mime) in [
+ ("motionphoto.heic", "80", "video/mp4"),
+ ("pixel_6_small_video.jpg", "1789460", "video/mp4"),
+ ("dual_mp4_video_first.jpg", "6123713", "video/mp4"),
+ ("dual_mp4_video_last.jpg", "4048544", "video/mp4"),
+ ("pixel_8.jpg", "9554", "image/jpeg"),
+ ] {
+ let Some(path) = fixture(name) else {
+ eprintln!("Skipping: external fixture {name} not present");
+ return;
+ };
+ let data = extract_xmp_from_path(path).expect("extract fixture XMP");
+ assert_eq!(data.get("Item:Length").unwrap(), length, "{name}");
+ assert_eq!(data.get("Item:Mime").unwrap(), mime, "{name}");
+ }
+}