1 //
2 // Copyright (c) 2020 KAMADA Ken'ichi.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 // SUCH DAMAGE.
25 //
26
27 use std::io::{BufRead, ErrorKind};
28
29 use crate::endian::{Endian, LittleEndian};
30 use crate::error::Error;
31 use crate::util::{BufReadExt as _, ReadExt as _};
32
33 // Chunk identifiers for RIFF.
34 const FCC_RIFF: [u8; 4] = *b"RIFF";
35 const FCC_WEBP: [u8; 4] = *b"WEBP";
36 const FCC_EXIF: [u8; 4] = *b"EXIF";
37
38 // Get the contents of the Exif chunk from a WebP file.
get_exif_attr<R>(reader: &mut R) -> Result<Vec<u8>, Error> where R: BufRead39 pub 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
get_exif_attr_sub<R>(reader: &mut R) -> Result<Vec<u8>, Error> where R: BufRead48 fn 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 // Scan the series of chunks.
60 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
is_webp(buf: &[u8]) -> bool82 pub 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)]
87 mod tests {
88 use super::*;
89
90 #[test]
truncated()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]
no_exif()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]
empty()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]
non_empty()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]
read_first()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]
out_of_toplevel_chunk()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]
overflowing_parent()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]
odd_at_last_without_padding()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 }
148