jpegxl_rs/
utils.rs

1/*
2This file is part of jpegxl-rs.
3
4jpegxl-rs is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9jpegxl-rs is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with jpegxl-rs.  If not, see <https://www.gnu.org/licenses/>.
16*/
17
18//! Utils functions when a decoder or encoder is not needed
19
20use jpegxl_sys::decode::{JxlSignature, JxlSignatureCheck};
21
22/// Check if the signature of the input is valid.
23/// Return `None` if it needs more data.
24#[must_use]
25pub fn check_valid_signature(buf: &[u8]) -> Option<bool> {
26    use JxlSignature::{Codestream, Container, Invalid, NotEnoughBytes};
27
28    match unsafe { JxlSignatureCheck(buf.as_ptr(), buf.len()) } {
29        NotEnoughBytes => None,
30        Invalid => Some(false),
31        Codestream | Container => Some(true),
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::tests::SAMPLE_JXL;
39
40    use pretty_assertions::assert_eq;
41
42    #[test]
43    fn test_signature() {
44        assert!(check_valid_signature(&[]).is_none());
45        assert_eq!(check_valid_signature(&[0; 64]), Some(false));
46        assert_eq!(check_valid_signature(SAMPLE_JXL), Some(true));
47    }
48}