1use super::exif::*;
2use super::exifpost::*;
3use super::ifdformat::*;
4use super::lowlevel::*;
5use super::types::*;
6use std::borrow::Cow;
7
8type InExifResult = Result<(), ExifError>;
9
10pub(crate) fn parse_exif_entry(ifd: IfdEntry, warnings: &mut Vec<String>, kind: IfdKind) -> ExifEntry {
14 let (tag, unit, format, min_count, max_count, more_readable) = tag_to_exif(ifd.tag);
15 let value = match tag_value_new(&ifd) {
16 Some(v) => v,
17 None => TagValue::Invalid(ifd.data.clone(), ifd.le, ifd.format as u16, ifd.count),
18 };
19
20 let e = ExifEntry {
21 value_more_readable: more_readable(ifd.tag, &value).unwrap_or(Cow::Borrowed("")),
22 namespace: ifd.namespace,
23 ifd,
24 tag,
25 unit: unit.into(),
26 value,
27 kind,
28 };
29
30 if tag == ExifTag::UnknownToMe {
31 return e;
33 }
34
35 if (((tag as u32) & 0xffff) as u16) != e.ifd.tag
40 || (min_count == -1
41 && (format != IfdFormat::Ascii
42 && format != IfdFormat::Undefined
43 && format != IfdFormat::Unknown))
44 || (min_count != -1 && format == IfdFormat::Ascii)
45 {
46 panic!("Internal error {:x}", e.ifd.tag);
47 }
48
49 if format != e.ifd.format {
50 warnings.push(format!(
51 "EXIF tag {:x} {} ({}), expected format {} ({:?}), found {} ({:?})",
52 e.ifd.tag, e.ifd.tag, tag, format as u8, format, e.ifd.format as u8, e.ifd.format
53 ));
54 }
55
56 if min_count != -1 && ((e.ifd.count as i32) < min_count || (e.ifd.count as i32) > max_count) {
57 warnings.push(format!(
58 "EXIF tag {:x} {} ({:?}), format {}, expected count {}..{} found {}",
59 e.ifd.tag, e.ifd.tag, tag, format as u8, min_count, max_count, e.ifd.count
60 ));
61 }
62 e
63}
64
65pub fn parse_ifd(
67 subifd: bool,
68 le: bool,
69 count: u16,
70 contents: &[u8]
71) -> Option<(Vec<IfdEntry>, usize)> {
72 let mut entries: Vec<IfdEntry> = Vec::new();
73
74 for i in 0..count {
75 let mut offset = (i as usize) * 12;
76 let tag = read_u16(le, contents.get(offset..)?)?;
77 offset += 2;
78 let format = read_u16(le, contents.get(offset..)?)?;
79 offset += 2;
80 let count = read_u32(le, contents.get(offset..)?)?;
81 offset += 4;
82 let data = contents.get(offset..offset + 4)?.to_vec();
83
84 let entry = IfdEntry {
85 namespace: Namespace::Standard,
86 tag,
87 format: IfdFormat::new(format),
88 count,
89 ifd_data: data,
90 le,
91 ext_data: Vec::new(),
92 data: Vec::new(),
93 };
94 entries.push(entry);
95 }
96
97 let next_ifd = if subifd {
98 0
99 } else {
100 read_u32(le, &contents[count as usize * 12..])? as usize
101 };
102
103 Some((entries, next_ifd))
104}
105
106fn parse_exif_ifd(
108 le: bool,
109 contents: &[u8],
110 ioffset: usize,
111 exif_entries: &mut Vec<ExifEntry>,
112 warnings: &mut Vec<String>,
113 kind: IfdKind,
114) -> InExifResult {
115 let mut offset = ioffset;
116
117 if contents.len() < (offset + 2) {
118 return Err(ExifError::ExifIfdTruncated(
119 format!("Truncated {:?} at dir entry count ({} < {})", kind, contents.len(), (offset + 2)),
120 ));
121 }
122
123 let count = read_u16(
124 le,
125 contents
126 .get(offset..)
127 .ok_or(ExifError::IfdTruncated)?,
128 ).ok_or(ExifError::IfdTruncated)?;
129 let ifd_length = (count as usize) * 12;
130 offset += 2;
131
132 if contents.len() < (offset + ifd_length) {
133 return Err(ExifError::ExifIfdTruncated("Truncated at dir listing".into()));
134 }
135
136 let ifd_content = &contents
137 .get(offset..offset + ifd_length)
138 .ok_or(ExifError::IfdTruncated)?;
139 let (ifd, _) = parse_ifd(true, le, count, ifd_content).ok_or(ExifError::IfdTruncated)?;
140
141 for mut entry in ifd {
142 if !entry.copy_data(contents) {
143 continue;
145 }
146 let exif_entry = parse_exif_entry(entry, warnings, kind);
147 exif_entries.push(exif_entry);
148 }
149
150 Ok(())
151}
152
153pub fn parse_ifds(
155 le: bool,
156 ifd0_offset: usize,
157 contents: &[u8],
158 warnings: &mut Vec<String>,
159) -> ExifEntryResult {
160 let mut offset = ifd0_offset;
161 let mut exif_entries: Vec<ExifEntry> = Vec::new();
162
163 match parse_exif_ifd(le, contents, offset, &mut exif_entries, warnings, IfdKind::Ifd0) {
166 Ok(()) => true,
167 Err(e) => return Err(e),
168 };
169
170 let count = read_u16(
174 le,
175 contents
176 .get(offset..offset + 2)
177 .ok_or(ExifError::IfdTruncated)?,
178 ).ok_or(ExifError::IfdTruncated)?;
179 let ifd_length = (count as usize) * 12 + 4;
180 offset += 2;
181
182 let ifd_content = &contents
183 .get(offset..offset + ifd_length)
184 .ok_or(ExifError::IfdTruncated)?;
185 let (ifd, _) = parse_ifd(false, le, count, ifd_content).ok_or(ExifError::IfdTruncated)?;
186
187 for entry in &ifd {
188 let ifd_kind = if entry.tag == (((ExifTag::ExifOffset as u32) & 0xffff) as u16) {
190 IfdKind::Exif
191 } else if entry.tag == (((ExifTag::GPSOffset as u32) & 0xffff) as u16) {
192 IfdKind::Gps
194 } else {
195 continue;
196 };
197
198 let exif_offset = entry.try_data_as_offset().unwrap_or(!0);
199 if contents.len() < exif_offset {
200 return Err(ExifError::ExifIfdTruncated("Exif SubIFD goes past EOF".into()));
201 }
202 parse_exif_ifd(le, contents, exif_offset, &mut exif_entries, warnings, ifd_kind)?;
203 }
204
205 for n in 0..exif_entries.len() {
206 let (begin, end) = exif_entries.split_at_mut(n);
207 let (entry, end) = end.split_first_mut().unwrap();
208 exif_postprocessing(entry, begin, end);
209 }
210
211 Ok(exif_entries)
212}
213
214pub fn parse_tiff(contents: &[u8], warnings: &mut Vec<String>) -> (ExifEntryResult, bool) {
216 let mut le = false;
217
218 if contents.len() < 8 {
219 return (Err(ExifError::TiffTruncated), false);
220 } else if contents[0] == b'I' && contents[1] == b'I' && contents[2] == 42 && contents[3] == 0 {
221 le = true;
223 } else if contents[0] == b'M' && contents[1] == b'M' && contents[2] == 0 && contents[3] == 42 {
224 } else {
226 let err = format!("Preamble is {:x} {:x} {:x} {:x}", contents[0], contents[1], contents[2], contents[3]);
227 return (Err(ExifError::TiffBadPreamble(err)), false);
228 }
229
230 let offset = read_u32(le, &contents[4..]).unwrap() as usize;
231
232 (parse_ifds(le, offset, contents, warnings), le)
233}