1use std::io::{BufRead, ErrorKind};
28
29use crate::endian::{Endian, LittleEndian};
30use crate::error::Error;
31use crate::util::{BufReadExt as _, ReadExt as _};
32
33const FCC_RIFF: [u8; 4] = *b"RIFF";
35const FCC_WEBP: [u8; 4] = *b"WEBP";
36const FCC_EXIF: [u8; 4] = *b"EXIF";
37
38pub fn get_exif_attr<R>(reader: &mut R)
40 -> Result<Vec<u8>, Error> where R: BufRead {
41 match get_exif_attr_sub(reader) {
42 Err(Error::Io(ref e)) if e.kind() == ErrorKind::UnexpectedEof =>
43 Err(Error::InvalidFormat("Broken WebP file")),
44 r => r,
45 }
46}
47
48fn get_exif_attr_sub<R>(reader: &mut R)
49 -> Result<Vec<u8>, Error> where R: BufRead {
50 let mut sig = [0; 12];
51 reader.read_exact(&mut sig)?;
52 if sig[0..4] != FCC_RIFF || sig[8..12] != FCC_WEBP {
53 return Err(Error::InvalidFormat("Not a WebP file"));
54 }
55 let mut file_size = LittleEndian::loadu32(&sig, 4) as usize;
56 file_size = file_size.checked_sub(4)
57 .ok_or(Error::InvalidFormat("Invalid header file size"))?;
58
59 while file_size > 0 {
61 file_size = file_size.checked_sub(8)
62 .ok_or(Error::InvalidFormat("Chunk overflowing parent"))?;
63 let mut cheader = [0; 8];
64 reader.read_exact(&mut cheader)?;
65 let mut size = LittleEndian::loadu32(&cheader, 4) as usize;
66 file_size = file_size.checked_sub(size)
67 .ok_or(Error::InvalidFormat("Chunk overflowing parent"))?;
68 if cheader[0..4] == FCC_EXIF {
69 let mut payload = Vec::new();
70 reader.read_exact_len(&mut payload, size)?;
71 return Ok(payload);
72 }
73 if size % 2 != 0 && file_size > 0 {
74 file_size -= 1;
75 size = size.checked_add(1).expect("ex-file_size - size > 0");
76 }
77 reader.discard_exact(size)?;
78 }
79 Err(Error::NotFound("WebP"))
80}
81
82pub fn is_webp(buf: &[u8]) -> bool {
83 buf.len() >= 12 && buf[0..4] == FCC_RIFF && buf[8..12] == FCC_WEBP
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn truncated() {
92 let mut data = b"RIFF\x10\0\0\0WEBPEXIF\x04\0\0\0Exif".to_vec();
93 assert_eq!(get_exif_attr(&mut &data[..]).unwrap(), b"Exif");
94 while let Some(_) = data.pop() {
95 get_exif_attr(&mut &data[..]).unwrap_err();
96 }
97 }
98
99 #[test]
100 fn no_exif() {
101 let data = b"RIFF\x0c\0\0\0WEBPwhat\0\0\0\0";
102 assert_err_pat!(get_exif_attr(&mut &data[..]), Error::NotFound(_));
103 }
104
105 #[test]
106 fn empty() {
107 let data = b"RIFF\x16\0\0\0WEBPodd_\x01\0\0\0X\0EXIF\0\0\0\0";
108 assert_ok!(get_exif_attr(&mut &data[..]), b"");
109 }
110
111 #[test]
112 fn non_empty() {
113 let data = b"RIFF\x1a\0\0\0WEBPeven\x02\0\0\0XYEXIF\x03\0\0\0abcd";
114 assert_ok!(get_exif_attr(&mut &data[..]), b"abc");
115 }
116
117 #[test]
118 fn read_first() {
119 let data = b"RIFF\x18\0\0\0WEBPEXIF\x02\0\0\0abEXIF\x02\0\0\0cd";
120 assert_ok!(get_exif_attr(&mut &data[..]), b"ab");
121 }
122
123 #[test]
124 fn out_of_toplevel_chunk() {
125 let data = b"RIFF\x0e\0\0\0WEBPwhat\x02\0\0\0abEXIF\x02\0\0\0cd";
126 assert_err_pat!(get_exif_attr(&mut &data[..]), Error::NotFound(_));
127 }
128
129 #[test]
130 fn overflowing_parent() {
131 let mut data = b"RIFF\x10\0\0\0WEBPEXIF\x04\0\0\0Exif".to_vec();
132 assert_eq!(get_exif_attr(&mut &data[..]).unwrap(), b"Exif");
133 for x in 0x05..=0x0f {
134 data[4] = x;
135 assert_err_pat!(get_exif_attr(&mut &data[..]),
136 Error::InvalidFormat(_));
137 }
138 data[4] = 0x04;
139 assert_err_pat!(get_exif_attr(&mut &data[..]), Error::NotFound(_));
140 }
141
142 #[test]
143 fn odd_at_last_without_padding() {
144 let data = b"RIFF\x17\0\0\0WEBPwhat\0\0\0\0EXIF\x03\0\0\0abc";
145 assert_ok!(get_exif_attr(&mut &data[..]), b"abc");
146 }
147}