diff --git a/examples/file.rs b/examples/file.rs index 0db50fd..bb8f1d3 100644 --- a/examples/file.rs +++ b/examples/file.rs @@ -24,7 +24,7 @@ fn main() { } Err(e) => { eprintln!("Looks like something went wrong 😔"); - eprintln!("{}", e); + eprintln!("{e}"); exit(1); } } diff --git a/src/matchers/doc.rs b/src/matchers/doc.rs index 71c3a03..bd32231 100644 --- a/src/matchers/doc.rs +++ b/src/matchers/doc.rs @@ -131,11 +131,11 @@ fn ole2(buf: &[u8]) -> Option { } fn check_msooml(buf: &[u8], offset: usize) -> Option { - if compare_bytes(buf, &[b'w', b'o', b'r', b'd', b'/'], offset) { + if compare_bytes(buf, b"word/", offset) { Some(DocType::DOCX) - } else if compare_bytes(buf, &[b'p', b'p', b't', b'/'], offset) { + } else if compare_bytes(buf, b"ppt/", offset) { Some(DocType::PPTX) - } else if compare_bytes(buf, &[b'x', b'l', b'/'], offset) { + } else if compare_bytes(buf, b"xl/", offset) { Some(DocType::XLSX) } else { None diff --git a/src/matchers/mod.rs b/src/matchers/mod.rs index 30ed410..b14bf91 100644 --- a/src/matchers/mod.rs +++ b/src/matchers/mod.rs @@ -26,3 +26,9 @@ pub(crate) fn compare_bytes(slice: &[u8], sub_slice: &[u8], start_offset: usize) true } + +pub(crate) fn match_bytes(slice: &[u8], sub_slice: &[u8]) -> bool { + slice + .windows(sub_slice.len()) + .any(|window| window == sub_slice) +} diff --git a/src/matchers/video.rs b/src/matchers/video.rs index dbf0f67..c8b31b1 100644 --- a/src/matchers/video.rs +++ b/src/matchers/video.rs @@ -12,37 +12,22 @@ pub fn is_m4v(buf: &[u8]) -> bool { /// Returns whether a buffer is MKV video data. pub fn is_mkv(buf: &[u8]) -> bool { - (buf.len() > 15 - && buf[0] == 0x1A + buf.len() > 256 + && buf[0] == 0x1a && buf[1] == 0x45 - && buf[2] == 0xDF - && buf[3] == 0xA3 - && buf[4] == 0x93 - && buf[5] == 0x42 - && buf[6] == 0x82 - && buf[7] == 0x88 - && buf[8] == 0x6D - && buf[9] == 0x61 - && buf[10] == 0x74 - && buf[11] == 0x72 - && buf[12] == 0x6F - && buf[13] == 0x73 - && buf[14] == 0x6B - && buf[15] == 0x61) - || (buf.len() > 38 - && buf[31] == 0x6D - && buf[32] == 0x61 - && buf[33] == 0x74 - && buf[34] == 0x72 - && buf[35] == 0x6f - && buf[36] == 0x73 - && buf[37] == 0x6B - && buf[38] == 0x61) + && buf[2] == 0xdf + && buf[3] == 0xa3 + && crate::match_bytes(&buf[..256], b"\x42\x82\x88matroska") } /// Returns whether a buffer is WEBM video data. pub fn is_webm(buf: &[u8]) -> bool { - buf.len() > 3 && buf[0] == 0x1A && buf[1] == 0x45 && buf[2] == 0xDF && buf[3] == 0xA3 + buf.len() > 256 + && buf[0] == 0x1a + && buf[1] == 0x45 + && buf[2] == 0xdf + && buf[3] == 0xa3 + && crate::match_bytes(&buf[..256], b"\x42\x82\x84webm") } /// Returns whether a buffer is Quicktime MOV video data.