Skip to content

zstd: provide a "contains magic number" func #253

@mvdan

Description

@mvdan

That is, to detect if a chunk of bytes has the zstd magic number as a prefix.

There are Go modules like https://github.com/h2non/filetype, which support lots of file types. But at the end of the day, I just need to check four bytes, and it seems unnecessary to add an extra module dependency when I already depend on a zstd package :)

For example, here's a sample implementation I wrote, which could be used like if zstd.HasMagic(chunk) { ...:

// HasMagic reports whether src begins with zstd v1's magic number,
// 0xFD2FB528 in little-endian format.
func HasMagic(src []byte) bool {
        return len(src) >= 4 &&
                src[0] == 0x28 && src[1] == 0xB5 &&
                src[2] == 0x2f && src[3] == 0xFD
}

I only raised this as an issue instead of a PR because the patch is tiny and I feel like there's a lot of room for bikeshedding, or discussion around whether or not we want this.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions