Skip to content

fix: detect zstd streams starting with a skippable frame - #1116

Open
Sanjays2402 wants to merge 2 commits into
ouch-org:mainfrom
Sanjays2402:fix-zstd-skippable-frame-detection
Open

Sanjays2402 wants to merge 2 commits into
ouch-org:mainfrom
Sanjays2402:fix-zstd-skippable-frame-detection

Conversation

@Sanjays2402

Copy link
Copy Markdown

Fixes #1115.

try_infer_format only recognized a zstd stream starting with the standard frame magic, so a stream starting with a skippable frame (magics 0x184D2A50-0x184D2A5F, RFC 8878 section 3.1.1.3) was not detected at all. Without an extension that meant a hard "Cannot decompress files" error, even though the zstd decoder itself handles the file fine (--format zst worked, and file reports it as Zstandard data).

This teaches is_zst to also accept a leading skippable frame magic, so those streams are detected as zst like any other.

Test: added try_infer_format_recognizes_zstd_skippable_frame_magic, covering both ends of the skippable range and one magic just outside it. It fails on main and passes with this change. I also verified end to end with a real zstd file prefixed by a skippable frame: it failed to decompress without an extension before, and decompresses fine after. Full suite green: cargo test --profile fast (54 unit, 57 integration, 13 UI, plus mime and format-flag tests), clippy clean, cargo +nightly fmt -- --check clean.

@valoq

valoq commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

The new test fails on main and passes with the change. pzstd output without an extension now decompresses correctly, and .zst files from pzstd no longer get the “might be misnamed” message.

The comment needs two fixes:

  • Skippable frames are defined in RFC 8878 section 3.1.2. Section 3.1.1.3 covers compressed blocks.
  • A stream that starts with a skippable frame is not always zstd. LZ4 uses the same skippable frames, and such LZ4 files are now detected as zstd.

the length check is not needed because buf always holds 270 bytes. A slice pattern is shorter and passes the same tests.

        // Zstd frame magic or the skippable frame magic that pzstd writes first
        buf.starts_with(&[0x28, 0xB5, 0x2F, 0xFD]) || matches!(buf, [0x50..=0x5F, 0x2A, 0x4D, 0x18, ..])

Fix the RFC reference (skippable frames are defined in RFC 8878
section 3.1.2, not 3.1.1.3), note in the comment that an LZ4 stream
starting with a skippable frame is detected as zstd here too, and
replace the length check with the shorter slice pattern.
@Sanjays2402

Copy link
Copy Markdown
Author

Thanks, fixed the comment (skippable frames are RFC 8878 section 3.1.2) and switched to the slice pattern. I also noted the LZ4 caveat in the comment, since an LZ4 stream starting with a skippable frame is still detected as zstd here. Detection behavior is unchanged.

@valoq valoq self-assigned this Nov 18, 2026
@valoq
valoq self-requested a review November 18, 2026 11:43

@valoq valoq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zstd streams starting with a skippable frame are not detected from their signature

2 participants