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
//
// Copyright (c) 2020 KAMADA Ken'ichi.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//

use std::io::{BufRead, ErrorKind};

use crate::endian::{Endian, LittleEndian};
use crate::error::Error;
use crate::util::{BufReadExt as _, ReadExt as _};

// Chunk identifiers for RIFF.
const FCC_RIFF: [u8; 4] = *b"RIFF";
const FCC_WEBP: [u8; 4] = *b"WEBP";
const FCC_EXIF: [u8; 4] = *b"EXIF";

// Get the contents of the Exif chunk from a WebP file.
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 WebP file")),
        r => r,
    }
}

fn get_exif_attr_sub<R>(reader: &mut R)
                        -> Result<Vec<u8>, Error> where R: BufRead {
    let mut sig = [0; 12];
    reader.read_exact(&mut sig)?;
    if sig[0..4] != FCC_RIFF || sig[8..12] != FCC_WEBP {
        return Err(Error::InvalidFormat("Not a WebP file"));
    }
    let mut file_size = LittleEndian::loadu32(&sig, 4) as usize;
    file_size = file_size.checked_sub(4)
        .ok_or(Error::InvalidFormat("Invalid header file size"))?;

    // Scan the series of chunks.
    while file_size > 0 {
        file_size = file_size.checked_sub(8)
            .ok_or(Error::InvalidFormat("Chunk overflowing parent"))?;
        let mut cheader = [0; 8];
        reader.read_exact(&mut cheader)?;
        let mut size = LittleEndian::loadu32(&cheader, 4) as usize;
        file_size = file_size.checked_sub(size)
            .ok_or(Error::InvalidFormat("Chunk overflowing parent"))?;
        if cheader[0..4] == FCC_EXIF {
            let mut payload = Vec::new();
            reader.read_exact_len(&mut payload, size)?;
            return Ok(payload);
        }
        if size % 2 != 0 && file_size > 0 {
            file_size -= 1;
            size = size.checked_add(1).expect("ex-file_size - size > 0");
        }
        reader.discard_exact(size)?;
    }
    Err(Error::NotFound("WebP"))
}

pub fn is_webp(buf: &[u8]) -> bool {
    buf.len() >= 12 && buf[0..4] == FCC_RIFF && buf[8..12] == FCC_WEBP
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn truncated() {
        let mut data = b"RIFF\x10\0\0\0WEBPEXIF\x04\0\0\0Exif".to_vec();
        assert_eq!(get_exif_attr(&mut &data[..]).unwrap(), b"Exif");
        while let Some(_) = data.pop() {
            get_exif_attr(&mut &data[..]).unwrap_err();
        }
    }

    #[test]
    fn no_exif() {
        let data = b"RIFF\x0c\0\0\0WEBPwhat\0\0\0\0";
        assert_err_pat!(get_exif_attr(&mut &data[..]), Error::NotFound(_));
    }

    #[test]
    fn empty() {
        let data = b"RIFF\x16\0\0\0WEBPodd_\x01\0\0\0X\0EXIF\0\0\0\0";
        assert_ok!(get_exif_attr(&mut &data[..]), b"");
    }

    #[test]
    fn non_empty() {
        let data = b"RIFF\x1a\0\0\0WEBPeven\x02\0\0\0XYEXIF\x03\0\0\0abcd";
        assert_ok!(get_exif_attr(&mut &data[..]), b"abc");
    }

    #[test]
    fn read_first() {
        let data = b"RIFF\x18\0\0\0WEBPEXIF\x02\0\0\0abEXIF\x02\0\0\0cd";
        assert_ok!(get_exif_attr(&mut &data[..]), b"ab");
    }

    #[test]
    fn out_of_toplevel_chunk() {
        let data = b"RIFF\x0e\0\0\0WEBPwhat\x02\0\0\0abEXIF\x02\0\0\0cd";
        assert_err_pat!(get_exif_attr(&mut &data[..]), Error::NotFound(_));
    }

    #[test]
    fn overflowing_parent() {
        let mut data = b"RIFF\x10\0\0\0WEBPEXIF\x04\0\0\0Exif".to_vec();
        assert_eq!(get_exif_attr(&mut &data[..]).unwrap(), b"Exif");
        for x in 0x05..=0x0f {
            data[4] = x;
            assert_err_pat!(get_exif_attr(&mut &data[..]),
                            Error::InvalidFormat(_));
        }
        data[4] = 0x04;
        assert_err_pat!(get_exif_attr(&mut &data[..]), Error::NotFound(_));
    }

    #[test]
    fn odd_at_last_without_padding() {
        let data = b"RIFF\x17\0\0\0WEBPwhat\0\0\0\0EXIF\x03\0\0\0abc";
        assert_ok!(get_exif_attr(&mut &data[..]), b"abc");
    }
}