use std::io::{Cursor, Read, Seek, SeekFrom};
use crate::{error::Result, item::ItemKind, structure::Structure, thumbnail::Thumbnail};
#[allow(dead_code)]
mod tags {
pub const IMAGE_WIDTH: u16 = 0x0100;
pub const IMAGE_LENGTH: u16 = 0x0101;
pub const MAKE: u16 = 0x010f;
pub const MODEL: u16 = 0x0110;
pub const ORIENTATION: u16 = 0x0112;
pub const SOFTWARE: u16 = 0x0131;
pub const DATE_TIME: u16 = 0x0132;
pub const ARTIST: u16 = 0x013b;
pub const COPYRIGHT: u16 = 0x8298;
pub const EXIF_IFD_POINTER: u16 = 0x8769;
pub const EXPOSURE_TIME: u16 = 0x829a;
pub const F_NUMBER: u16 = 0x829d;
pub const ISO_SPEED: u16 = 0x8827;
pub const DATE_TIME_ORIGINAL: u16 = 0x9003;
pub const FOCAL_LENGTH: u16 = 0x920a;
pub const JPEG_INTERCHANGE_FORMAT: u16 = 0x0201;
pub const JPEG_INTERCHANGE_FORMAT_LENGTH: u16 = 0x0202;
}
#[allow(dead_code)]
mod types {
pub const BYTE: u16 = 1;
pub const ASCII: u16 = 2;
pub const SHORT: u16 = 3;
pub const LONG: u16 = 4;
pub const RATIONAL: u16 = 5;
}
#[derive(Debug, Clone, Copy)]
enum ByteOrder {
LittleEndian,
BigEndian,
}
impl ByteOrder {
fn read_u16(&self, data: &[u8]) -> u16 {
match self {
ByteOrder::LittleEndian => u16::from_le_bytes([data[0], data[1]]),
ByteOrder::BigEndian => u16::from_be_bytes([data[0], data[1]]),
}
}
fn read_u32(&self, data: &[u8]) -> u32 {
match self {
ByteOrder::LittleEndian => u32::from_le_bytes([data[0], data[1], data[2], data[3]]),
ByteOrder::BigEndian => u32::from_be_bytes([data[0], data[1], data[2], data[3]]),
}
}
}
#[derive(Debug)]
pub struct ThumbnailInfo {
pub offset: u32,
pub size: u32,
pub width: Option<u32>,
pub height: Option<u32>,
}
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct ExifInfo {
pub make: Option<String>,
pub model: Option<String>,
pub orientation: Option<u16>,
pub software: Option<String>,
pub date_time: Option<String>,
pub date_time_original: Option<String>,
pub artist: Option<String>,
pub copyright: Option<String>,
}
impl std::fmt::Display for ExifInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut parts = Vec::new();
if let Some(ref make) = self.make {
parts.push(make.clone());
}
if let Some(ref model) = self.model {
parts.push(model.clone());
}
if let Some(dt) = self.date_time_original.as_ref().or(self.date_time.as_ref()) {
parts.push(dt.clone());
}
if parts.is_empty() {
write!(f, "(no metadata)")
} else {
write!(f, "{}", parts.join(" | "))
}
}
}
const MAX_IFD_TAGS: u16 = 1000;
pub fn parse_thumbnail_info(exif_data: &[u8]) -> Result<Option<ThumbnailInfo>> {
if exif_data.len() < 8 {
return Ok(None);
}
let mut cursor = Cursor::new(exif_data);
let mut header = [0u8; 8];
cursor.read_exact(&mut header)?;
let byte_order = match &header[0..2] {
b"II" => ByteOrder::LittleEndian,
b"MM" => ByteOrder::BigEndian,
_ => return Ok(None), };
let magic = byte_order.read_u16(&header[2..4]);
if magic != 0x002a {
return Ok(None);
}
let ifd0_offset = byte_order.read_u32(&header[4..8]);
if ifd0_offset as usize >= exif_data.len() {
return Ok(None); }
let ifd1_offset = match parse_ifd(&mut cursor, ifd0_offset, byte_order, exif_data.len())? {
Some(offset) => offset,
None => return Ok(None), };
if ifd1_offset as usize >= exif_data.len() {
return Ok(None); }
parse_thumbnail_from_ifd(&mut cursor, ifd1_offset, byte_order, exif_data.len())
}
pub fn parse_exif_info(exif_data: &[u8]) -> Result<Option<ExifInfo>> {
if exif_data.len() < 8 {
return Ok(None);
}
let mut cursor = Cursor::new(exif_data);
let mut header = [0u8; 8];
cursor.read_exact(&mut header)?;
let byte_order = match &header[0..2] {
b"II" => ByteOrder::LittleEndian,
b"MM" => ByteOrder::BigEndian,
_ => return Ok(None),
};
let magic = byte_order.read_u16(&header[2..4]);
if magic != 0x002a {
return Ok(None);
}
let ifd0_offset = byte_order.read_u32(&header[4..8]);
if ifd0_offset as usize >= exif_data.len() {
return Ok(None);
}
parse_exif_from_ifd(&cursor, ifd0_offset, byte_order, exif_data)
}
fn parse_exif_from_ifd(
cursor: &Cursor<&[u8]>,
offset: u32,
byte_order: ByteOrder,
data: &[u8],
) -> Result<Option<ExifInfo>> {
if offset as usize >= data.len() {
return Ok(None);
}
let mut cursor = cursor.clone();
cursor.seek(SeekFrom::Start(offset as u64))?;
let mut count_bytes = [0u8; 2];
if cursor.read_exact(&mut count_bytes).is_err() {
return Ok(None);
}
let tag_count = byte_order.read_u16(&count_bytes);
if tag_count > MAX_IFD_TAGS {
return Ok(None);
}
let mut info = ExifInfo::default();
let mut exif_ifd_offset: Option<u32> = None;
for _ in 0..tag_count {
let mut tag_bytes = [0u8; 12];
if cursor.read_exact(&mut tag_bytes).is_err() {
break;
}
let tag_id = byte_order.read_u16(&tag_bytes[0..2]);
let tag_type = byte_order.read_u16(&tag_bytes[2..4]);
let count = byte_order.read_u32(&tag_bytes[4..8]);
let value_or_offset = byte_order.read_u32(&tag_bytes[8..12]);
match tag_id {
tags::MAKE => {
info.make = read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
tags::MODEL => {
info.model = read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
tags::ORIENTATION if tag_type == types::SHORT => {
info.orientation = Some(value_or_offset as u16)
}
tags::SOFTWARE => {
info.software = read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
tags::DATE_TIME => {
info.date_time = read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
tags::ARTIST => {
info.artist = read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
tags::COPYRIGHT => {
info.copyright = read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
tags::EXIF_IFD_POINTER if tag_type == types::LONG => {
exif_ifd_offset = Some(value_or_offset)
}
_ => {}
}
}
if let Some(exif_offset) = exif_ifd_offset {
if let Some(dt_orig) = parse_exif_subifd_datetime(data, exif_offset, byte_order) {
info.date_time_original = Some(dt_orig);
}
}
Ok(Some(info))
}
fn read_ascii_tag(
data: &[u8],
tag_type: u16,
count: u32,
value_or_offset: u32,
byte_order: ByteOrder,
) -> Option<String> {
if tag_type != types::ASCII || count == 0 {
return None;
}
let bytes = if count <= 4 {
let val_bytes = match byte_order {
ByteOrder::LittleEndian => value_or_offset.to_le_bytes(),
ByteOrder::BigEndian => value_or_offset.to_be_bytes(),
};
val_bytes[..count as usize].to_vec()
} else {
let offset = value_or_offset as usize;
let end = offset + count as usize;
if end > data.len() {
return None;
}
data[offset..end].to_vec()
};
String::from_utf8(bytes)
.ok()
.map(|s| s.trim_end_matches('\0').trim().to_string())
.filter(|s| !s.is_empty())
}
fn parse_exif_subifd_datetime(data: &[u8], offset: u32, byte_order: ByteOrder) -> Option<String> {
if offset as usize >= data.len() {
return None;
}
let mut cursor = Cursor::new(data);
cursor.seek(SeekFrom::Start(offset as u64)).ok()?;
let mut count_bytes = [0u8; 2];
cursor.read_exact(&mut count_bytes).ok()?;
let tag_count = byte_order.read_u16(&count_bytes);
if tag_count > MAX_IFD_TAGS {
return None;
}
for _ in 0..tag_count {
let mut tag_bytes = [0u8; 12];
if cursor.read_exact(&mut tag_bytes).is_err() {
break;
}
let tag_id = byte_order.read_u16(&tag_bytes[0..2]);
let tag_type = byte_order.read_u16(&tag_bytes[2..4]);
let count = byte_order.read_u32(&tag_bytes[4..8]);
let value_or_offset = byte_order.read_u32(&tag_bytes[8..12]);
if tag_id == tags::DATE_TIME_ORIGINAL {
return read_ascii_tag(data, tag_type, count, value_or_offset, byte_order);
}
}
None
}
fn parse_ifd(
cursor: &mut Cursor<&[u8]>,
offset: u32,
byte_order: ByteOrder,
data_len: usize,
) -> Result<Option<u32>> {
if offset as usize >= data_len {
return Ok(None);
}
cursor.seek(SeekFrom::Start(offset as u64))?;
let mut count_bytes = [0u8; 2];
if cursor.read_exact(&mut count_bytes).is_err() {
return Ok(None); }
let tag_count = byte_order.read_u16(&count_bytes);
if tag_count > MAX_IFD_TAGS {
return Ok(None); }
cursor.seek(SeekFrom::Current((tag_count as i64) * 12))?;
let mut next_bytes = [0u8; 4];
if cursor.read_exact(&mut next_bytes).is_err() {
return Ok(None); }
let next_offset = byte_order.read_u32(&next_bytes);
if next_offset == 0 || next_offset as usize >= data_len {
Ok(None)
} else {
Ok(Some(next_offset))
}
}
fn parse_thumbnail_from_ifd(
cursor: &mut Cursor<&[u8]>,
offset: u32,
byte_order: ByteOrder,
data_len: usize,
) -> Result<Option<ThumbnailInfo>> {
if offset as usize >= data_len {
return Ok(None);
}
cursor.seek(SeekFrom::Start(offset as u64))?;
let mut count_bytes = [0u8; 2];
if cursor.read_exact(&mut count_bytes).is_err() {
return Ok(None);
}
let tag_count = byte_order.read_u16(&count_bytes);
if tag_count > MAX_IFD_TAGS {
return Ok(None);
}
let mut thumb_offset = None;
let mut thumb_size = None;
let mut width = None;
let mut height = None;
for _ in 0..tag_count {
let mut tag_bytes = [0u8; 12];
if cursor.read_exact(&mut tag_bytes).is_err() {
break;
}
let tag_id = byte_order.read_u16(&tag_bytes[0..2]);
let tag_type = byte_order.read_u16(&tag_bytes[2..4]);
let value = byte_order.read_u32(&tag_bytes[8..12]);
if tag_type == types::SHORT || tag_type == types::LONG {
match tag_id {
tags::JPEG_INTERCHANGE_FORMAT => thumb_offset = Some(value),
tags::JPEG_INTERCHANGE_FORMAT_LENGTH => thumb_size = Some(value),
tags::IMAGE_WIDTH => width = Some(value),
tags::IMAGE_LENGTH => height = Some(value),
_ => {}
}
}
}
match (thumb_offset, thumb_size) {
(Some(offset), Some(size)) => Ok(Some(ThumbnailInfo {
offset,
size,
width,
height,
})),
_ => Ok(None),
}
}
fn tiff_data(data: &[u8]) -> &[u8] {
data.strip_prefix(b"Exif\0\0").unwrap_or(data)
}
impl Structure {
pub fn exif_info<R: Read + Seek>(&self, src: &mut R) -> Result<Option<ExifInfo>> {
match self.items_of(ItemKind::Exif).next() {
Some(e) => parse_exif_info(tiff_data(&self.read_item(src, e)?)),
None => Ok(None),
}
}
pub(crate) fn exif_thumbnail<R: Read + Seek>(&self, src: &mut R) -> Result<Option<Thumbnail>> {
let Some(e) = self.items_of(ItemKind::Exif).next() else {
return Ok(None);
};
let data = self.read_item(src, e)?;
let exif = tiff_data(&data);
let Some(info) = parse_thumbnail_info(exif)? else {
return Ok(None);
};
let start = info.offset as usize;
let Some(end) = start.checked_add(info.size as usize) else {
return Ok(None);
};
Ok(exif.get(start..end).map(|bytes| {
let mut t = Thumbnail::new(bytes.to_vec()).with_media_type("image/jpeg");
if let (Some(w), Some(h)) = (info.width, info.height) {
t = t.with_dimensions(w, h);
}
t
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_byte_order() {
let be = ByteOrder::BigEndian;
let le = ByteOrder::LittleEndian;
assert_eq!(be.read_u16(&[0x12, 0x34]), 0x1234);
assert_eq!(le.read_u16(&[0x34, 0x12]), 0x1234);
assert_eq!(be.read_u32(&[0x12, 0x34, 0x56, 0x78]), 0x12345678);
assert_eq!(le.read_u32(&[0x78, 0x56, 0x34, 0x12]), 0x12345678);
}
}