1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::collections::HashMap;

use nom::{
    branch::alt,
    bytes::complete::{tag, take},
    combinator::{map, map_res, verify},
    error::ErrorKind,
    multi::many_m_n,
    number::{complete::u16, complete::u32, Endianness},
    sequence::tuple,
    IResult, Needed,
};

use crate::{exif::ExifTag, exif::GPSInfo, EntryValue};

use super::ifd::{entry_component_size, get_gps_info, DirectoryEntry, ImageFileDirectory};

/// Parses Exif information from the `input` TIFF data.
///
/// Please note that Exif values are lazy-parsed, meaning that they are only
/// truly parsed when the `Exif::get_value` and `Exif::get_values` methods are
/// called.
///
/// This allows you to parse Exif values on-demand, reducing the parsing
/// overhead.
pub fn parse_exif<'a>(input: &'a [u8]) -> crate::Result<Exif> {
    let (_, header) = Header::parse(input)?;

    // jump to ifd0
    let skip = (header.ifd0_offset) as usize;
    let (remain, _) = take(skip)(input)?;

    let parser = Parser {
        data: input,
        endian: header.endian,
    };

    // parse ifd0
    let (_, ifd0) = parser
        .parse_ifd(input.len() - remain.len())
        .map_err(|e| format!("Parse exif failed; {e}"))?;

    let exif = Exif {
        header,
        ifd0,
        tz: None,
    };

    let tz = exif.get_tz_offset();

    Ok(Exif { tz, ..exif })
}

/// Represents Exif information in a JPEG/HEIF file.
///
/// Please note that Exif values are lazy-parsed, meaning that they are only
/// truly parsed when the `Exif::get_value` and `Exif::get_values` methods are
/// called.

/// This allows you to parse Exif values on-demand, reducing the parsing
/// overhead.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Exif {
    header: Header,
    ifd0: Option<ImageFileDirectory>,
    tz: Option<String>,
}

impl Exif {
    /// Searches for specified tags within the parsed Exif data, and parses the
    /// corresponding values within the found entries. The final result is
    /// returned in the form of a hash table.
    ///
    /// Please note that this method will ignore errors encountered during the
    /// search and parsing process, such as missing tags or errors in parsing
    /// values, and handle them silently.
    pub fn get_values<'b>(&self, tags: &'b [ExifTag]) -> Vec<(&'b ExifTag, EntryValue)> {
        tags.iter()
            .zip(tags.iter())
            .filter_map(|x| {
                self.get_value(x.0)
                    .map(|v| v.map(|v| (x.0, v)))
                    .unwrap_or(None)
            })
            .collect::<Vec<_>>()
    }

    /// Searches for specified `tag` within the parsed Exif structure, and
    /// parses the corresponding value within the found entry.
    pub fn get_value(&self, tag: &ExifTag) -> crate::Result<Option<EntryValue>> {
        self.get_value_by_tag_code(*tag as u16)
    }

    /// Searches for specified `tag` within the parsed Exif structure, and
    /// parses the corresponding value within the found entry.
    pub fn get_value_by_tag_code(&self, tag: u16) -> crate::Result<Option<EntryValue>> {
        self.ifd0
            .as_ref()
            .and_then(|ifd0| {
                ifd0.find(tag).map(|entry| {
                    EntryValue::parse(entry, self.endian(), &self.tz)
                        .map_err(|_| format!("parse value for exif tag {tag:?} failed").into())
                })
            })
            .transpose()
    }

    fn get_tz_offset(&self) -> Option<String> {
        let values = self.get_values(&[ExifTag::OffsetTimeOriginal, ExifTag::OffsetTime]);
        values.into_iter().find_map(|x| {
            if let EntryValue::Text(s) = x.1 {
                Some(s)
            } else {
                None
            }
        })
    }

    /// Searches and parses the found GPS information within the parsed Exif
    /// structure.
    pub fn get_gps_info(&self) -> crate::Result<Option<GPSInfo>> {
        self.ifd0
            .as_ref()
            .and_then(|ifd0| {
                ifd0.gps_ifd()
                    .and_then(|gps_ifd| Some(get_gps_info(gps_ifd, self.endian())))
            })
            .transpose()
    }

    fn endian(&self) -> Endianness {
        self.header.endian
    }
}

const ENTRY_SIZE: usize = 12;

struct Parser<'a> {
    data: &'a [u8],
    endian: Endianness,
}

impl<'a> Parser<'a> {
    fn parse_ifd(&'a self, pos: usize) -> IResult<&'a [u8], Option<ImageFileDirectory>> {
        let input = self.data;
        let endian = self.endian;

        let (remain, entry_num) = u16(endian)(&input[pos..])?;
        if entry_num == 0 {
            return Ok((remain, None));
        }

        // 12 bytes per entry
        if remain.len() < entry_num as usize * ENTRY_SIZE {
            let need = Needed::new(entry_num as usize * ENTRY_SIZE - remain.len());
            return IResult::Err(nom::Err::Incomplete(need));
        }

        let mut pos = input.len() - remain.len();
        let (remain, entries) =
            many_m_n(entry_num as usize, entry_num as usize, |_: &'a [u8]| {
                let (rem, entry) = self.parse_ifd_entry(pos)?;
                pos = input.len() - rem.len();
                Ok((rem, entry))
            })(input)?;

        let entries = entries
            .into_iter()
            .map(|x| (x.tag, x))
            .collect::<HashMap<_, _>>();

        Ok((remain, Some(ImageFileDirectory { entries })))
    }

    fn parse_ifd_entry(&self, pos: usize) -> IResult<&'a [u8], DirectoryEntry> {
        let input = self.data;
        let endian = self.endian;

        if pos + ENTRY_SIZE > input.len() {
            return Err(nom::Err::Incomplete(Needed::new(
                pos + ENTRY_SIZE - input.len(),
            )));
        }
        let entry_data = &input[pos..pos + ENTRY_SIZE];

        let (_, entry) = map_res(
            tuple((u16(endian), u16(endian), u32(endian), u32(endian))),
            |(tag, data_format, components_num, value_or_offset)| {
                // get component_size according to data format
                let component_size = entry_component_size(data_format).map_err(|e| {
                    eprintln!("parse exif entry failed; {e}");
                    nom::Err::Failure(nom::error::make_error(entry_data, ErrorKind::Fail))
                })?;

                // get entry data
                let size = components_num as usize * component_size;
                let data = if size <= 4 {
                    &entry_data[8..8 + size]
                } else {
                    let start = value_or_offset as usize;
                    let end = start + size;
                    if end > input.len() {
                        return Err(nom::Err::Incomplete(Needed::new(end - input.len())));
                    }
                    &input[start..end]
                };

                let data = Vec::from(data);

                let subifd = self.parse_subifd(tag, value_or_offset as usize)?;

                Ok::<_, nom::Err<nom::error::Error<_>>>(DirectoryEntry {
                    tag,
                    data_format,
                    components_num,
                    data,
                    value: value_or_offset,
                    subifd,
                })
            },
        )(entry_data)?;

        Ok((&input[pos + ENTRY_SIZE..], entry))
    }

    fn parse_subifd(
        &self,
        tag: u16,
        offset: usize,
    ) -> Result<Option<ImageFileDirectory>, nom::Err<nom::error::Error<&[u8]>>> {
        let input = self.data;
        let subifd = if tag == ExifTag::ExifOffset as u16 || tag == ExifTag::GPSInfo as u16 {
            if offset > input.len() {
                let need = Needed::new(offset - input.len());
                return Err(nom::Err::Incomplete(need));
            }

            // load from offset
            let (_, ifd) = self.parse_ifd(offset)?;
            ifd
        } else {
            None
        };
        Ok(subifd)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Header {
    pub endian: Endianness,
    pub ifd0_offset: u32,
}

impl Header {
    pub fn parse(input: &[u8]) -> IResult<&[u8], Header> {
        let (remain, endian) = Header::parse_endian(input)?;
        map(
            tuple((verify(u16(endian), |magic| *magic == 0x2a), u32(endian))),
            move |(_, offset)| Header {
                endian,
                ifd0_offset: offset,
            },
        )(remain)
    }

    // pub fn first_ifd<'a>(&self, input: &'a [u8], tag_ids: HashSet<u16>) -> IResult<&'a [u8], IFD> {
    //     // ifd0_offset starts from the beginning of Header, so we should
    //     // subtract the header size, which is 8
    //     let offset = self.ifd0_offset - 8;

    //     // skip to offset
    //     let (_, remain) = take(offset)(input)?;

    //     IFD::parse(remain, self.endian, tag_ids)
    // }

    fn parse_endian(input: &[u8]) -> IResult<&[u8], Endianness> {
        map(alt((tag("MM"), tag("II"))), |endian_marker| {
            if endian_marker == b"MM" {
                Endianness::Big
            } else {
                Endianness::Little
            }
        })(input)
    }
}

#[cfg(test)]
mod tests {
    use std::{fs::File, io::prelude::*, path::Path};

    use test_case::test_case;

    use crate::exif::ExifTag::*;
    use crate::exif::{GPSInfo, LatLng};
    use crate::values::URational;

    use super::*;

    #[test]
    fn header() {
        let buf = [0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x08];

        let (_, header) = Header::parse(&buf).unwrap();
        assert_eq!(
            header,
            Header {
                endian: Endianness::Big,
                ifd0_offset: 8
            }
        );
    }

    #[test_case("exif.jpg")]
    fn test_parse_exif(path: &str) {
        let mut buf = Vec::new();
        open_sample(path).read_to_end(&mut buf).unwrap();
        // println!("file size: {}", buf.len());

        // skip first 12 bytes
        let exif = parse_exif(&buf[12..]).unwrap();

        let entries = exif.get_values(&[
            Unknown,
            Make,
            Model,
            Orientation,
            ImageWidth,
            ImageHeight,
            ISOSpeedRatings,
            ShutterSpeedValue,
            ExposureTime,
            FNumber,
            ExifImageWidth,
            ExifImageHeight,
            DateTimeOriginal,
            CreateDate,
            ModifyDate,
            OffsetTimeOriginal,
            OffsetTime,
            GPSLatitudeRef,
            GPSLatitude,
            GPSLongitudeRef,
            GPSLongitude,
            GPSAltitudeRef,
            GPSAltitude,
            GPSVersionID,
            ImageDescription,
            XResolution,
            YResolution,
            ResolutionUnit,
            Software,
            HostComputer,
            WhitePoint,
            PrimaryChromaticities,
            YCbCrCoefficients,
            ReferenceBlackWhite,
            Copyright,
        ]);

        assert_eq!(
            {
                let mut x = entries
                    .into_iter()
                    .map(|x| (x.0.to_string(), x.1.to_string()))
                    .collect::<Vec<(String, String)>>();
                // Sort by alphabetical order of keys.
                x.sort_by(|a, b| a.0.cmp(&b.0));
                x
            },
            [
                ("CreateDate(0x9004)", "2023-07-09T20:36:33+08:00"),
                ("DateTimeOriginal(0x9003)", "2023-07-09T20:36:33+08:00"),
                ("ExifImageHeight(0xa003)", "4096"),
                ("ExifImageWidth(0xa002)", "3072"),
                ("ExposureTime(0x829a)", "9997/1000000 (0.0100)"),
                ("FNumber(0x829d)", "175/100 (1.7500)"),
                ("GPSAltitude(0x0006)", "0/1 (0.0000)"),
                ("GPSAltitudeRef(0x0005)", "0"),
                ("GPSLatitude(0x0002)", "22/1 (22.0000)"),
                ("GPSLatitudeRef(0x0001)", "N"),
                ("GPSLongitude(0x0004)", "114/1 (114.0000)"),
                ("GPSLongitudeRef(0x0003)", "E"),
                ("ISOSpeedRatings(0x8827)", "454"),
                ("ImageHeight(0x0101)", "4096"),
                ("ImageWidth(0x0100)", "3072"),
                ("Make(0x010f)", "vivo"),
                ("Model(0x0110)", "vivo X90 Pro+"),
                ("ModifyDate(0x0132)", "2023-07-09T20:36:33+08:00"),
                ("OffsetTime(0x9010)", "+08:00"),
                ("OffsetTimeOriginal(0x9011)", "+08:00"),
                ("ResolutionUnit(0x0128)", "2"),
                ("ShutterSpeedValue(0x9201)", "6644/1000 (6.6440)"),
                ("XResolution(0x011a)", "72/1 (72.0000)"),
                ("YResolution(0x011b)", "72/1 (72.0000)")
            ]
            .iter()
            .map(|x| (x.0.to_string(), x.1.to_string()))
            .collect::<Vec<_>>()
        );
    }

    #[test_case(
        "exif.jpg",
        'N',
        [(22, 1), (31, 1), (5208, 100)].into(),
        'E',
        [(114, 1), (1, 1), (1733, 100)].into(),
        0u8,
        (0, 1).into()
    )]
    fn gps_info(
        path: &str,
        latitude_ref: char,
        latitude: LatLng,
        longitude_ref: char,
        longitude: LatLng,
        altitude_ref: u8,
        altitude: URational,
    ) {
        let mut buf = Vec::new();
        open_sample(path).read_to_end(&mut buf).unwrap();

        // skip first 12 bytes
        let exif = parse_exif(&buf[12..]).unwrap();

        let gps = exif.get_gps_info().unwrap().unwrap();
        assert_eq!(
            gps,
            GPSInfo {
                latitude_ref,
                latitude,
                longitude_ref,
                longitude,
                altitude_ref,
                altitude,
            }
        )
    }

    fn open_sample(path: &str) -> impl Read + Seek {
        File::open(Path::new("./testdata").join(path)).unwrap()
    }
}