From 488288a698df8eb7a7f418739ca895ee677b9ff5 Mon Sep 17 00:00:00 2001 From: sobaq <109554668+sobaq@users.noreply.github.com> Date: Mon, 8 Jul 2024 17:05:45 +0930 Subject: [PATCH] fix mkv and webm detection --- src/matchers/mod.rs | 6 ++++++ src/matchers/video.rs | 37 +++++++++++-------------------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/matchers/mod.rs b/src/matchers/mod.rs index 30ed410..f64ad35 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()) + .find(|window| window == &sub_slice) + .is_some() +} \ No newline at end of file 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.