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
use endian::{Endian, BigEndian, LittleEndian};
use error::Error;
use tag;
use tag::{Context, Tag};
use value::Value;
use value::get_type_info;
const TIFF_BE: u16 = 0x4d4d;
const TIFF_LE: u16 = 0x4949;
const TIFF_FORTY_TWO: u16 = 0x002a;
const TIFF_BE_SIG: [u8; 4] = [0x4d, 0x4d, 0x00, 0x2a];
const TIFF_LE_SIG: [u8; 4] = [0x49, 0x49, 0x2a, 0x00];
#[derive(Debug)]
pub struct Field<'a> {
pub tag: Tag,
pub thumbnail: bool,
pub value: Value<'a>,
}
pub fn parse_exif(data: &[u8]) -> Result<(Vec<Field>, bool), Error> {
if data.len() < 8 {
return Err(Error::InvalidFormat("Truncated TIFF header"));
}
match BigEndian::loadu16(data, 0) {
TIFF_BE => parse_exif_sub::<BigEndian>(data).map(|v| (v, false)),
TIFF_LE => parse_exif_sub::<LittleEndian>(data).map(|v| (v, true)),
_ => Err(Error::InvalidFormat("Invalid TIFF byte order")),
}
}
fn parse_exif_sub<E>(data: &[u8])
-> Result<Vec<Field>, Error> where E: Endian {
if E::loadu16(data, 2) != TIFF_FORTY_TWO {
return Err(Error::InvalidFormat("Invalid forty two"));
}
let ifd_offset = E::loadu32(data, 4) as usize;
parse_ifd::<E>(data, ifd_offset, Context::Tiff, false)
}
fn parse_ifd<E>(data: &[u8], offset: usize, ctx: Context, thumbnail: bool)
-> Result<Vec<Field>, Error> where E: Endian {
if data.len() < offset || data.len() - offset < 2 {
return Err(Error::InvalidFormat("Truncated IFD"));
}
let count = E::loadu16(data, offset) as usize;
if data.len() - offset - 2 < count * 12 {
return Err(Error::InvalidFormat("Truncated IFD"));
}
let mut fields = Vec::with_capacity(count);
for i in 0..count as usize {
let tag = E::loadu16(data, offset + 2 + i * 12);
let typ = E::loadu16(data, offset + 2 + i * 12 + 2);
let cnt = E::loadu32(data, offset + 2 + i * 12 + 4) as usize;
let ofs = E::loadu32(data, offset + 2 + i * 12 + 8) as usize;
let (unitlen, parser) = get_type_info::<E>(typ);
let vallen = try!(unitlen.checked_mul(cnt).ok_or(
Error::InvalidFormat("Invalid entry count")));
let val;
if unitlen == 0 {
val = Value::Unknown(typ, cnt as u32, ofs as u32);
} else if vallen <= 4 {
val = parser(data, offset + 2 + i * 12 + 8, cnt);
} else {
if data.len() < ofs || data.len() - ofs < vallen {
return Err(Error::InvalidFormat("Truncated IFD"));
}
val = parser(data, ofs, cnt);
}
let tag = Tag(ctx, tag);
if tag == tag::ExifIFDPointer {
let mut v = try!(
parse_ifd::<E>(data, ofs, Context::Exif, thumbnail));
fields.append(&mut v);
} else if tag == tag::GPSInfoIFDPointer {
let mut v = try!(
parse_ifd::<E>(data, ofs, Context::Gps, thumbnail));
fields.append(&mut v);
} else if tag == tag::InteropIFDPointer {
let mut v = try!(
parse_ifd::<E>(data, ofs, Context::Interop, thumbnail));
fields.append(&mut v);
} else {
fields.push(Field { tag: tag, thumbnail: thumbnail, value: val });
}
}
if data.len() - offset - 2 - count * 12 < 4 {
return Err(Error::InvalidFormat("Truncated IFD"));
}
let next_ifd_offset = E::loadu32(data, offset + 2 + count * 12) as usize;
if next_ifd_offset != 0 {
if ctx != Context::Tiff || thumbnail {
return Err(Error::InvalidFormat("Unexpected next IFD"));
}
let mut v = try!(
parse_ifd::<E>(data, next_ifd_offset, Context::Tiff, true));
fields.append(&mut v);
}
Ok(fields)
}
pub fn is_tiff(buf: &[u8]) -> bool {
buf.starts_with(&TIFF_BE_SIG) || buf.starts_with(&TIFF_LE_SIG)
}
#[cfg(test)]
mod tests {
use error::Error;
use super::*;
#[test]
fn inf_loop_by_next() {
let data = b"MM\0\x2a\0\0\0\x08\
\0\x01\x01\0\0\x03\0\0\0\x01\0\x14\0\0\0\0\0\x08";
assert_err_pat!(parse_exif(data),
Error::InvalidFormat("Unexpected next IFD"));
}
}