Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: add supposer for zstd skippable frames (v2) #88

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/matchers/archive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::{TryFrom, TryInto};
marcospb19 marked this conversation as resolved.
Show resolved Hide resolved

/// Returns whether a buffer is an ePub.
pub fn is_epub(buf: &[u8]) -> bool {
crate::book::is_epub(buf)
Expand Down Expand Up @@ -199,9 +201,42 @@ pub fn is_dcm(buf: &[u8]) -> bool {
buf.len() > 131 && buf[128] == 0x44 && buf[129] == 0x49 && buf[130] == 0x43 && buf[131] == 0x4D
}

const ZSTD_SKIP_START: usize = 0x184D2A50;
const ZSTD_SKIP_MASK: usize = 0xFFFFFFF0;

/// Returns whether a buffer is a Zstd archive.
// Zstandard compressed data is made of one or more frames.
// There are two frame formats defined by Zstandard: Zstandard frames and Skippable frames.
// See more details from https://tools.ietf.org/id/draft-kucherawy-dispatch-zstd-00.html#rfc.section.2
pub fn is_zst(buf: &[u8]) -> bool {
buf.len() > 3 && buf[0] == 0x28 && buf[1] == 0xB5 && buf[2] == 0x2F && buf[3] == 0xFD
if buf.len() > 3 && buf[0] == 0x28 && buf[1] == 0xB5 && buf[2] == 0x2F && buf[3] == 0xFD {
return true;
}

if buf.len() < 8 {
return false;
}

let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap());
let Ok(magic) = usize::try_from(magic) else {
return false;
};

if magic & ZSTD_SKIP_MASK != ZSTD_SKIP_START {
return false;
}

let data_len = u32::from_le_bytes(buf[4..8].try_into().unwrap());
let Ok(data_len) = usize::try_from(data_len) else {
return false;
};

if buf.len() < 8 + data_len {
return false;
}

let next_frame = &buf[8 + data_len..];
is_zst(next_frame)
}

/// Returns whether a buffer is a MSI Windows Installer archive.
Expand Down
Binary file added testdata/sample.skippable.zst
Binary file not shown.
8 changes: 7 additions & 1 deletion tests/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ test_format!(
);

test_format!(Archive, "application/zstd", "zst", zst, "sample.tar.zst");

test_format!(Archive, "application/x-cpio", "cpio", cpio, "sample.cpio");
test_format!(
Archive,
"application/zstd",
"zst",
zst_skip,
"sample.skippable.zst"
);