Skip to content

Commit

Permalink
zstd: check for skippable frames as well; fix #619 (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile authored Dec 19, 2024
1 parent 511d137 commit 4657583
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/magic/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ func InstallShieldCab(raw []byte, _ uint32) bool {
}

// Zstd matches a Zstandard archive file.
// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
func Zstd(raw []byte, limit uint32) bool {
return len(raw) >= 4 &&
(0x22 <= raw[0] && raw[0] <= 0x28 || raw[0] == 0x1E) && // Different Zstandard versions.
bytes.HasPrefix(raw[1:], []byte{0xB5, 0x2F, 0xFD})
if len(raw) < 4 {
return false
}
sig := binary.LittleEndian.Uint32(raw)
// Check for Zstandard frames and skippable frames.
return (sig >= 0xFD2FB522 && sig <= 0xFD2FB528) ||
(sig >= 0x184D2A50 && sig <= 0x184D2A5F)
}

// CRX matches a Chrome extension file: a zip archive prepended by a package header.
Expand Down
1 change: 1 addition & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ var testcases = []testcase{
{"xz", "\xfd7zXZ\x00", "application/x-xz", true},
{"zip", "PK\x03\x04", "application/zip", true},
{"zst", "(\xb5/\xfd", "application/zstd", true},
{"zst skippable frame", "\x50\x2A\x4D\x18", "application/zstd", false},
}

func TestDetect(t *testing.T) {
Expand Down

0 comments on commit 4657583

Please sign in to comment.