mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #15093 from storytold/refactor/exif-shared-spans
Use shared EXIF camera-field reader and writer
This commit is contained in:
4 files changed
+38
-352
No files matched your search
Generated
+28
-6
@@ -1363,7 +1363,7 @@ name = "craft-crs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6#ad8faa1c136ce289e9f132c9137d0cd913bd4ef6"
|
||||
dependencies = [
|
||||
"craft-metadata",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
|
||||
"craft-xml",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1468,7 +1468,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132
|
||||
dependencies = [
|
||||
"craft-heif",
|
||||
"craft-jpeg",
|
||||
"craft-metadata",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
@@ -1504,6 +1504,18 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=163f1b20315ffacaf8a6e085f4ae7fc568c08ff2#163f1b20315ffacaf8a6e085f4ae7fc568c08ff2"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=163f1b20315ffacaf8a6e085f4ae7fc568c08ff2)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
@@ -1511,7 +1523,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.1",
|
||||
"chrono",
|
||||
"craft-tiff",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
@@ -1604,8 +1616,8 @@ source = "git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-crw",
|
||||
"craft-metadata",
|
||||
"craft-tiff",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
|
||||
"jxl-oxide",
|
||||
"miniz_oxide 0.9.1",
|
||||
"rayon",
|
||||
@@ -1632,6 +1644,15 @@ dependencies = [
|
||||
"vectorcraft-pathops",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=163f1b20315ffacaf8a6e085f4ae7fc568c08ff2#163f1b20315ffacaf8a6e085f4ae7fc568c08ff2"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
@@ -5463,6 +5484,7 @@ dependencies = [
|
||||
name = "photocraft-algo"
|
||||
version = "0.23.1"
|
||||
dependencies = [
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=163f1b20315ffacaf8a6e085f4ae7fc568c08ff2)",
|
||||
"criterion",
|
||||
"photocraft-color",
|
||||
"photocraft-geom",
|
||||
@@ -5686,7 +5708,7 @@ dependencies = [
|
||||
"craft-pdn",
|
||||
"craft-psd",
|
||||
"craft-svg",
|
||||
"craft-tiff",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
|
||||
"craft-xcf",
|
||||
"craft-xml",
|
||||
"crc32fast",
|
||||
|
||||
@@ -64,6 +64,8 @@ craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "ad8faa1c13
|
||||
craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "ad8faa1c136ce289e9f132c9137d0cd913bd4ef6", features = ["tree-render", "tree-render-system-fonts"] }
|
||||
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "ad8faa1c136ce289e9f132c9137d0cd913bd4ef6" }
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "ad8faa1c136ce289e9f132c9137d0cd913bd4ef6" }
|
||||
# Camera-field EXIF reader/writer, shared prerequisite craft-libs#1563.
|
||||
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "163f1b20315ffacaf8a6e085f4ae7fc568c08ff2" }
|
||||
craft-crs = { git = "https://github.com/storytold/craft-libs", rev = "ad8faa1c136ce289e9f132c9137d0cd913bd4ef6" }
|
||||
# Interface translations: lookup, fallback, CLDR plurals and language names over the craft-i18n
|
||||
# catalogs that `crates/ui-egui/build.rs` embeds from `CRAFT_I18N_DIR` (storytold/craft-i18n, at
|
||||
|
||||
@@ -10,6 +10,7 @@ rust-version.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-metadata = { workspace = true }
|
||||
photocraft-geom = { workspace = true }
|
||||
photocraft-color = { workspace = true }
|
||||
photocraft-raster = { workspace = true }
|
||||
|
||||
+7
-346
@@ -1,237 +1,13 @@
|
||||
//! Minimal EXIF reader for the camera settings the photography commands use (exposure, aperture,
|
||||
//! ISO, focal length, camera and lens names). Input is the raw TIFF-structured EXIF payload
|
||||
//! (`II*\0` / `MM\0*`, as stored in `Document.metadata.exif`); malformed data yields `None`
|
||||
//! fields, never a panic. Tag numbers are from the EXIF 2.32 specification (CIPA DC-008).
|
||||
//! Camera-field EXIF compatibility facade. Parsing, bounds and TIFF serialization live in
|
||||
//! craft-metadata; the raw-date and numeric selection semantics are preserved there.
|
||||
|
||||
/// Camera settings read from EXIF.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct CameraInfo {
|
||||
pub make: Option<String>,
|
||||
pub model: Option<String>,
|
||||
pub lens: Option<String>,
|
||||
/// Seconds.
|
||||
pub exposure_time: Option<f64>,
|
||||
pub f_number: Option<f64>,
|
||||
pub iso: Option<f64>,
|
||||
/// Millimetres.
|
||||
pub focal_length: Option<f64>,
|
||||
/// 35 mm-equivalent focal length in millimetres.
|
||||
pub focal_length_35mm: Option<f64>,
|
||||
/// DateTimeOriginal as stored (`YYYY:MM:DD HH:MM:SS`).
|
||||
pub date_taken: Option<String>,
|
||||
}
|
||||
pub use craft_metadata::camera_fields::{CameraInfo, read, try_build};
|
||||
|
||||
impl CameraInfo {
|
||||
/// Linear exposure factor `t · ISO / N²` (relative brightness of the image for one scene).
|
||||
pub fn exposure_factor(&self) -> Option<f64> {
|
||||
let t = self.exposure_time?;
|
||||
let n = self.f_number.unwrap_or(1.0).max(0.1);
|
||||
Some(t * self.iso.unwrap_or(100.0) / 100.0 / (n * n))
|
||||
}
|
||||
}
|
||||
|
||||
/// Text bytes decoded per EXIF read (real Make/Model/Lens/Date strings are a few dozen bytes).
|
||||
const MAX_TEXT_BYTES: usize = 1 << 20;
|
||||
|
||||
struct Tiff<'a> {
|
||||
b: &'a [u8],
|
||||
le: bool,
|
||||
/// Text bytes this read may still decode: duplicate tags can share one large blank value.
|
||||
text_left: std::cell::Cell<usize>,
|
||||
}
|
||||
|
||||
impl Tiff<'_> {
|
||||
fn u16(&self, o: usize) -> Option<u16> {
|
||||
let s = self.b.get(o..o.checked_add(2)?)?;
|
||||
Some(if self.le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) })
|
||||
}
|
||||
fn u32(&self, o: usize) -> Option<u32> {
|
||||
let s = self.b.get(o..o.checked_add(4)?)?;
|
||||
let a = [s[0], s[1], s[2], s[3]];
|
||||
Some(if self.le { u32::from_le_bytes(a) } else { u32::from_be_bytes(a) })
|
||||
}
|
||||
/// Entries of the IFD at `off`: (tag, type, count, value offset field position).
|
||||
fn ifd(&self, off: usize) -> Vec<(u16, u16, u32, usize)> {
|
||||
let Some(n) = self.u16(off) else { return Vec::new() };
|
||||
(0..n as usize)
|
||||
.filter_map(|k| {
|
||||
// Checked: offsets come from the file, and usize is 32 bits on wasm.
|
||||
let e = off.checked_add(2)?.checked_add(k * 12)?;
|
||||
Some((self.u16(e)?, self.u16(e.checked_add(2)?)?, self.u32(e.checked_add(4)?)?, e.checked_add(8)?))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn data_at(&self, typ: u16, count: u32, field: usize) -> Option<usize> {
|
||||
let size = match typ {
|
||||
1 | 2 | 6 | 7 => 1usize,
|
||||
3 | 8 => 2,
|
||||
4 | 9 | 11 => 4,
|
||||
5 | 10 | 12 => 8,
|
||||
_ => return None,
|
||||
}
|
||||
.checked_mul(usize::try_from(count).ok()?)?;
|
||||
let at = if size <= 4 { field } else { usize::try_from(self.u32(field)?).ok()? };
|
||||
// The whole value must lie inside the payload, not just its first element.
|
||||
self.b.get(at..at.checked_add(size)?)?;
|
||||
Some(at)
|
||||
}
|
||||
fn number(&self, typ: u16, count: u32, field: usize) -> Option<f64> {
|
||||
let at = self.data_at(typ, count, field)?;
|
||||
match typ {
|
||||
3 => self.u16(at).map(f64::from),
|
||||
4 => self.u32(at).map(f64::from),
|
||||
5 => {
|
||||
let (n, d) = (self.u32(at)?, self.u32(at.checked_add(4)?)?);
|
||||
(d != 0).then(|| n as f64 / d as f64)
|
||||
}
|
||||
10 => {
|
||||
let (n, d) = (self.u32(at)? as i32, self.u32(at.checked_add(4)?)? as i32);
|
||||
(d != 0).then(|| n as f64 / d as f64)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn text(&self, typ: u16, count: u32, field: usize) -> Option<String> {
|
||||
#[cfg(test)]
|
||||
TEXT_DECODES.with(|c| c.set(c.get() + 1));
|
||||
if typ != 2 {
|
||||
return None;
|
||||
}
|
||||
let at = self.data_at(typ, count, field)?;
|
||||
let n = usize::try_from(count).ok()?;
|
||||
self.text_left.set(self.text_left.get().checked_sub(n)?);
|
||||
let s = self.b.get(at..at.checked_add(n)?)?;
|
||||
let s = String::from_utf8_lossy(s).trim_end_matches('\0').trim().to_string();
|
||||
(!s.is_empty()).then_some(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local!(static TEXT_DECODES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) });
|
||||
|
||||
/// Reads [`CameraInfo`] from a TIFF-structured EXIF payload (an `Exif\0\0` prefix is skipped).
|
||||
pub fn read(exif: &[u8]) -> CameraInfo {
|
||||
let b = exif.strip_prefix(b"Exif\0\0").unwrap_or(exif);
|
||||
let mut info = CameraInfo::default();
|
||||
let le = match b.get(..4) {
|
||||
Some([b'I', b'I', 42, 0]) => true,
|
||||
Some([b'M', b'M', 0, 42]) => false,
|
||||
_ => return info,
|
||||
};
|
||||
let t = Tiff { b, le, text_left: std::cell::Cell::new(MAX_TEXT_BYTES) };
|
||||
let Some(ifd0) = t.u32(4) else { return info };
|
||||
let mut queue = vec![ifd0 as usize];
|
||||
let mut seen = Vec::new();
|
||||
while let Some(off) = queue.pop() {
|
||||
if seen.contains(&off) || seen.len() > 8 {
|
||||
continue;
|
||||
}
|
||||
seen.push(off);
|
||||
for (tag, typ, count, field) in t.ifd(off) {
|
||||
match tag {
|
||||
// Duplicate TIFF text tags can point at the same large payload. Keep the first
|
||||
// recognized value and do not repeatedly decode/copy later duplicates.
|
||||
0x010F if info.make.is_none() => info.make = t.text(typ, count, field),
|
||||
0x0110 if info.model.is_none() => info.model = t.text(typ, count, field),
|
||||
0x829A => info.exposure_time = t.number(typ, count, field),
|
||||
0x829D => info.f_number = t.number(typ, count, field),
|
||||
0x8827 => info.iso = t.number(typ, count, field),
|
||||
0x920A => info.focal_length = t.number(typ, count, field),
|
||||
0xA405 => info.focal_length_35mm = t.number(typ, count, field).filter(|v| *v > 0.0),
|
||||
0xA434 if info.lens.is_none() => info.lens = t.text(typ, count, field),
|
||||
0x9003 if info.date_taken.is_none() => info.date_taken = t.text(typ, count, field),
|
||||
0x8769 => {
|
||||
if let Some(p) = t.u32(field) {
|
||||
queue.push(p as usize);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
info
|
||||
}
|
||||
|
||||
/// Builds a little-endian EXIF payload (used by tests and by synthetic fixtures).
|
||||
/// Build a TIFF-structured EXIF payload, preserving the existing infallible API.
|
||||
/// Returns an empty payload when the shared writer refuses an oversized value. New callers
|
||||
/// that need the error should use [`try_build`].
|
||||
pub fn build(info: &CameraInfo) -> Vec<u8> {
|
||||
// IFD0: Make, Model, ExifIFD pointer; Exif IFD: the rest. Data area after the IFDs.
|
||||
let mut ifd0: Vec<(u16, u16, u32, Vec<u8>)> = Vec::new();
|
||||
let mut exif: Vec<(u16, u16, u32, Vec<u8>)> = Vec::new();
|
||||
let text = |s: &str| {
|
||||
let mut v = s.as_bytes().to_vec();
|
||||
v.push(0);
|
||||
v
|
||||
};
|
||||
let rational = |v: f64| {
|
||||
let d = 10000u32;
|
||||
let mut b = ((v * d as f64).round() as u32).to_le_bytes().to_vec();
|
||||
b.extend_from_slice(&d.to_le_bytes());
|
||||
b
|
||||
};
|
||||
if let Some(s) = &info.make {
|
||||
ifd0.push((0x010F, 2, s.len() as u32 + 1, text(s)));
|
||||
}
|
||||
if let Some(s) = &info.model {
|
||||
ifd0.push((0x0110, 2, s.len() as u32 + 1, text(s)));
|
||||
}
|
||||
if let Some(v) = info.exposure_time {
|
||||
exif.push((0x829A, 5, 1, rational(v)));
|
||||
}
|
||||
if let Some(v) = info.f_number {
|
||||
exif.push((0x829D, 5, 1, rational(v)));
|
||||
}
|
||||
if let Some(v) = info.iso {
|
||||
exif.push((0x8827, 3, 1, (v as u16).to_le_bytes().to_vec()));
|
||||
}
|
||||
if let Some(s) = &info.date_taken {
|
||||
exif.push((0x9003, 2, s.len() as u32 + 1, text(s)));
|
||||
}
|
||||
if let Some(v) = info.focal_length {
|
||||
exif.push((0x920A, 5, 1, rational(v)));
|
||||
}
|
||||
if let Some(v) = info.focal_length_35mm {
|
||||
exif.push((0xA405, 3, 1, (v as u16).to_le_bytes().to_vec()));
|
||||
}
|
||||
if let Some(s) = &info.lens {
|
||||
exif.push((0xA434, 2, s.len() as u32 + 1, text(s)));
|
||||
}
|
||||
let ifd_len = |n: usize| 2 + 12 * n + 4;
|
||||
let ifd0_off = 8usize;
|
||||
let n0 = ifd0.len() + 1;
|
||||
let exif_off = ifd0_off + ifd_len(n0);
|
||||
let mut data_off = exif_off + ifd_len(exif.len());
|
||||
let mut out = b"II*\0".to_vec();
|
||||
out.extend_from_slice(&(ifd0_off as u32).to_le_bytes());
|
||||
let mut data = Vec::new();
|
||||
let mut write_ifd = |out: &mut Vec<u8>, entries: &[(u16, u16, u32, Vec<u8>)], extra: Option<u32>| {
|
||||
let n = entries.len() + usize::from(extra.is_some());
|
||||
out.extend_from_slice(&(n as u16).to_le_bytes());
|
||||
for (tag, typ, count, bytes) in entries {
|
||||
out.extend_from_slice(&tag.to_le_bytes());
|
||||
out.extend_from_slice(&typ.to_le_bytes());
|
||||
out.extend_from_slice(&count.to_le_bytes());
|
||||
if bytes.len() <= 4 {
|
||||
let mut f = bytes.clone();
|
||||
f.resize(4, 0);
|
||||
out.extend_from_slice(&f);
|
||||
} else {
|
||||
out.extend_from_slice(&(data_off as u32).to_le_bytes());
|
||||
data.extend_from_slice(bytes);
|
||||
data_off += bytes.len();
|
||||
}
|
||||
}
|
||||
if let Some(p) = extra {
|
||||
out.extend_from_slice(&0x8769u16.to_le_bytes());
|
||||
out.extend_from_slice(&4u16.to_le_bytes());
|
||||
out.extend_from_slice(&1u32.to_le_bytes());
|
||||
out.extend_from_slice(&p.to_le_bytes());
|
||||
}
|
||||
out.extend_from_slice(&0u32.to_le_bytes());
|
||||
};
|
||||
write_ifd(&mut out, &ifd0, Some(exif_off as u32));
|
||||
write_ifd(&mut out, &exif, None);
|
||||
out.extend_from_slice(&data);
|
||||
out
|
||||
try_build(info).unwrap_or_default()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -263,119 +39,4 @@ mod tests {
|
||||
let _ = read(&b[..cut]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Hostile payloads: huge counts, offsets near `u32::MAX`, self-referencing IFDs and random
|
||||
/// bytes read as `None` fields instead of panicking or overflowing (usize is 32 bits on wasm).
|
||||
#[test]
|
||||
fn blank_duplicate_text_tags_share_one_decode_budget() {
|
||||
// Blank values never fill `make`, so every duplicate would decode the same 1 MiB again;
|
||||
// the per-read budget stops after the first, so the later real value is not reached.
|
||||
let blank = MAX_TEXT_BYTES;
|
||||
let entry = |count: u32, off: u32| {
|
||||
let mut e = Vec::new();
|
||||
e.extend_from_slice(&0x010Fu16.to_le_bytes());
|
||||
e.extend_from_slice(&2u16.to_le_bytes());
|
||||
e.extend_from_slice(&count.to_le_bytes());
|
||||
e.extend_from_slice(&off.to_le_bytes());
|
||||
e
|
||||
};
|
||||
let mut b = b"II*\0".to_vec();
|
||||
b.extend_from_slice(&8u32.to_le_bytes());
|
||||
b.extend_from_slice(&3u16.to_le_bytes());
|
||||
b.extend(entry(blank as u32, 50));
|
||||
b.extend(entry(blank as u32, 50));
|
||||
b.extend(entry(2, u32::from_le_bytes(*b"M\0\0\0")));
|
||||
b.extend_from_slice(&0u32.to_le_bytes());
|
||||
assert_eq!(b.len(), 50);
|
||||
b.extend(std::iter::repeat_n(b' ', blank));
|
||||
assert_eq!(read(&b).make, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_text_tags_keep_the_first_value_without_copying_large_duplicates() {
|
||||
let value = vec![b'X'; 2 * 1024 * 1024];
|
||||
let mut b = b"II*\0".to_vec();
|
||||
b.extend_from_slice(&8u32.to_le_bytes());
|
||||
// IFD0: Make="M", a duplicate Make pointing at a 2 MiB payload, and the Exif IFD link.
|
||||
b.extend_from_slice(&3u16.to_le_bytes());
|
||||
b.extend_from_slice(&0x010Fu16.to_le_bytes());
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&2u32.to_le_bytes());
|
||||
b.extend_from_slice(b"M\0\0\0");
|
||||
b.extend_from_slice(&0x010Fu16.to_le_bytes());
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&(value.len() as u32).to_le_bytes());
|
||||
b.extend_from_slice(&80u32.to_le_bytes());
|
||||
b.extend_from_slice(&0x8769u16.to_le_bytes());
|
||||
b.extend_from_slice(&4u16.to_le_bytes());
|
||||
b.extend_from_slice(&1u32.to_le_bytes());
|
||||
b.extend_from_slice(&50u32.to_le_bytes());
|
||||
b.extend_from_slice(&0u32.to_le_bytes());
|
||||
// Exif IFD at 50: DateTimeOriginal="D" and the same large duplicate payload at offset 80.
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&0x9003u16.to_le_bytes());
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&2u32.to_le_bytes());
|
||||
b.extend_from_slice(b"D\0\0\0");
|
||||
b.extend_from_slice(&0x9003u16.to_le_bytes());
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&(value.len() as u32).to_le_bytes());
|
||||
b.extend_from_slice(&80u32.to_le_bytes());
|
||||
b.extend_from_slice(&0u32.to_le_bytes());
|
||||
b.extend_from_slice(&value);
|
||||
|
||||
TEXT_DECODES.with(|c| c.set(0));
|
||||
let info = read(&b);
|
||||
assert_eq!(info.make.as_deref(), Some("M"));
|
||||
assert_eq!(info.date_taken.as_deref(), Some("D"));
|
||||
// One decode per tag: duplicates must be skipped before `text` copies their payload.
|
||||
assert_eq!(TEXT_DECODES.with(|c| c.get()), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_payloads_never_panic() {
|
||||
let entry = |tag: u16, typ: u16, count: u32, value: u32| {
|
||||
let mut e = tag.to_le_bytes().to_vec();
|
||||
e.extend_from_slice(&typ.to_le_bytes());
|
||||
e.extend_from_slice(&count.to_le_bytes());
|
||||
e.extend_from_slice(&value.to_le_bytes());
|
||||
e
|
||||
};
|
||||
let mut b = b"II*\0".to_vec();
|
||||
b.extend_from_slice(&8u32.to_le_bytes());
|
||||
let entries = [
|
||||
entry(0x010F, 2, u32::MAX, 8), // Make: count far past the end
|
||||
entry(0x0110, 2, 16, u32::MAX - 1), // Model: offset at the top of the range
|
||||
entry(0x829A, 5, u32::MAX, 20), // ExposureTime: count overflows the size
|
||||
entry(0x829D, 5, 1, u32::MAX - 3), // FNumber: rational straddles u32::MAX
|
||||
entry(0x9003, 2, 0x4000_0000, 8), // DateTimeOriginal: size overflow on 32 bits
|
||||
entry(0x8769, 4, 1, 8), // Exif IFD pointing back at IFD0
|
||||
entry(0x8769, 4, 1, u32::MAX), // ... and past the end
|
||||
];
|
||||
b.extend_from_slice(&(entries.len() as u16 + 3).to_le_bytes()); // claims more entries than exist
|
||||
for e in entries {
|
||||
b.extend_from_slice(&e);
|
||||
}
|
||||
assert_eq!(read(&b), CameraInfo::default());
|
||||
let mut be = b"MM\0*".to_vec();
|
||||
be.extend_from_slice(&u32::MAX.to_be_bytes());
|
||||
assert_eq!(read(&be), CameraInfo::default());
|
||||
// Deterministic noise behind a valid header, and every truncation of a real payload.
|
||||
let mut x = 0x2545_F491u32;
|
||||
for _ in 0..200 {
|
||||
let mut n = b"II*\0\x08\0\0\0".to_vec();
|
||||
for _ in 0..96 {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
n.push(x as u8);
|
||||
}
|
||||
let _ = read(&n);
|
||||
}
|
||||
let good =
|
||||
build(&CameraInfo { make: Some("Make".into()), date_taken: Some("2024:02:17 12:40:19".into()), exposure_time: Some(0.004), ..Default::default() });
|
||||
for cut in 0..good.len() {
|
||||
let _ = read(&good[..cut]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user