Skip to main content

simp_rexif/
image.rs

1use crate::types::ExifError;
2
3use std::fmt::{self, Display};
4
5#[derive(Copy, Clone, Eq, PartialEq)]
6pub enum FileType {
7    Unknown,
8    JPEG,
9    TIFF,
10}
11
12impl Display for FileType {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        f.write_str(self.as_str())
15    }
16}
17
18impl FileType {
19    pub fn as_str(&self) -> &'static str {
20        match self {
21            Self::Unknown => "application/octet-stream",
22            Self::JPEG => "image/jpeg",
23            Self::TIFF => "image/tiff",
24        }
25    }
26}
27
28/// Detect the type of an image contained in a byte buffer
29pub(crate) fn detect_type(contents: &[u8]) -> FileType {
30    if contents.len() < 11 {
31        return FileType::Unknown;
32    }
33
34    if contents[0] == 0xff && contents[1] == 0xd8 && contents[2] == 0xff && // contents[3] == 0xe0 &&
35        contents[6] == b'J' && contents[7] == b'F' && contents[8] == b'I' && contents[9] == b'F' &&
36            contents[10] == 0
37    {
38        return FileType::JPEG;
39    }
40    if contents[0] == 0xff && contents[1] == 0xd8 && contents[2] == 0xff && // contents[3] == 0xe0
41        contents[6] == b'E' && contents[7] == b'x' && contents[8] == b'i' && contents[9] == b'f' &&
42            contents[10] == 0
43    {
44        return FileType::JPEG;
45    }
46    if contents[0] == b'I' && contents[1] == b'I' && contents[2] == 42 && contents[3] == 0 {
47        /* TIFF little-endian */
48        return FileType::TIFF;
49    }
50    if contents[0] == b'M' && contents[1] == b'M' && contents[2] == 0 && contents[3] == 42 {
51        /* TIFF big-endian */
52        return FileType::TIFF;
53    }
54    FileType::Unknown
55}
56
57/// Find the embedded TIFF in a JPEG image (that in turn contains the EXIF data)
58pub fn find_embedded_tiff_in_jpeg(contents: &[u8]) -> Result<(usize, usize), ExifError> {
59    let mut offset = 2_usize;
60
61    while offset < contents.len() {
62        if contents.len() < (offset + 4) {
63            return Err(ExifError::JpegWithoutExif("JPEG truncated in marker header".into()));
64        }
65
66        let marker: u16 = u16::from(contents[offset]) * 256 + u16::from(contents[offset + 1]);
67
68        if marker < 0xff00 {
69            return Err(ExifError::JpegWithoutExif(format!("Invalid marker {marker:x}")));
70        }
71
72        offset += 2;
73        let size = (contents[offset] as usize) * 256 + (contents[offset + 1] as usize);
74
75        if size < 2 {
76            return Err(ExifError::JpegWithoutExif("JPEG marker size must be at least 2 (because of the size word)".into()));
77        }
78        if contents.len() < (offset + size) {
79            return Err(ExifError::JpegWithoutExif("JPEG truncated in marker body".into()));
80        }
81
82        if marker == 0xffe1 {
83            if size < 8 {
84                return Err(ExifError::JpegWithoutExif("EXIF preamble truncated".into()));
85            }
86
87            if contents[offset + 2..offset + 8] != [b'E', b'x', b'i', b'f', 0, 0] {
88                return Err(ExifError::JpegWithoutExif("EXIF preamble unrecognized".into()));
89            }
90
91            // The offset and size of the block, excluding size and 'Exif\0\0'.
92            return Ok((offset + 8, size - 8));
93        }
94        if marker == 0xffda {
95            // last marker
96            return Err(ExifError::JpegWithoutExif("Last mark found and no EXIF".into()));
97        }
98        offset += size;
99    }
100
101    Err(ExifError::JpegWithoutExif("Scan past EOF and no EXIF found".into()))
102}