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
use std::io::{BufRead, ErrorKind};
use crate::error::Error;
use crate::util::{read8, read16};
mod marker {
pub const P: u8 = 0xff;
pub const Z: u8 = 0x00; pub const TEM: u8 = 0x01;
pub const RST0: u8 = 0xd0;
pub const RST7: u8 = 0xd7;
pub const SOI: u8 = 0xd8;
pub const EOI: u8 = 0xd9;
pub const SOS: u8 = 0xda;
pub const APP1: u8 = 0xe1;
}
const JPEG_SIG: [u8; 2] = [marker::P, marker::SOI];
const EXIF_ID: [u8; 6] = [0x45, 0x78, 0x69, 0x66, 0x00, 0x00];
pub fn get_exif_attr<R>(reader: &mut R)
-> Result<Vec<u8>, Error> where R: BufRead {
match get_exif_attr_sub(reader) {
Err(Error::Io(ref e)) if e.kind() == ErrorKind::UnexpectedEof =>
Err(Error::InvalidFormat("Broken JPEG file")),
r => r,
}
}
fn get_exif_attr_sub<R>(reader: &mut R)
-> Result<Vec<u8>, Error> where R: BufRead {
let mut soi = [0u8; 2];
reader.read_exact(&mut soi)?;
if soi != [marker::P, marker::SOI] {
return Err(Error::InvalidFormat("Not a JPEG file"));
}
loop {
reader.read_until(marker::P, &mut Vec::new())?;
let mut code;
loop {
code = read8(reader)?;
if code != marker::P { break; }
}
match code {
marker::Z | marker::TEM | marker::RST0..=marker::RST7 => continue,
marker::SOI => return Err(Error::InvalidFormat("Unexpected SOI")),
marker::EOI => return Err(Error::NotFound("JPEG")),
_ => {},
}
let len = read16(reader)?.checked_sub(2)
.ok_or(Error::InvalidFormat("Invalid segment length"))?;
let mut seg = vec![0; len.into()];
reader.read_exact(&mut seg)?;
if code == marker::APP1 && seg.starts_with(&EXIF_ID) {
seg.drain(..EXIF_ID.len());
return Ok(seg);
}
if code == marker::SOS {
}
}
}
pub fn is_jpeg(buf: &[u8]) -> bool {
buf.starts_with(&JPEG_SIG)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn truncated() {
let sets: &[&[u8]] = &[
b"",
b"\xff",
b"\xff\xd8",
b"\xff\xd8\x00",
b"\xff\xd8\xff",
b"\xff\xd8\xff\xe1\x00\x08\x03\x04",
];
for &data in sets {
assert_err_pat!(get_exif_attr(&mut &data[..]),
Error::InvalidFormat("Broken JPEG file"));
}
let mut data = b"\xff\xd8\xff\xe1\x00\x08Exif\0\0".to_vec();
assert_eq!(get_exif_attr(&mut &data[..]).unwrap(), b"");
while let Some(_) = data.pop() {
get_exif_attr(&mut &data[..]).unwrap_err();
}
}
#[test]
fn no_exif() {
let data = b"\xff\xd8\xff\xd9";
assert_err_pat!(get_exif_attr(&mut &data[..]),
Error::NotFound(_));
}
#[test]
fn out_of_sync() {
let data = b"\xff\xd8\x01\x02\x03\xff\x00\xff\xd9";
assert_err_pat!(get_exif_attr(&mut &data[..]),
Error::NotFound(_));
}
#[test]
fn empty() {
let data = b"\xff\xd8\xff\xe1\x00\x08Exif\0\0\xff\xd9";
assert_ok!(get_exif_attr(&mut &data[..]), []);
}
#[test]
fn non_empty() {
let data = b"\xff\xd8\xff\xe1\x00\x0aExif\0\0\xbe\xad\xff\xd9";
assert_ok!(get_exif_attr(&mut &data[..]), [0xbe, 0xad]);
}
}