Skip to content

Add executable and object file signatures - #2

Merged
andrew merged 2 commits into
mainfrom
executable-signatures
Aug 3, 2026
Merged

Add executable and object file signatures#2
andrew merged 2 commits into
mainfrom
executable-signatures

Conversation

@andrew

@andrew andrew commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Adds ELF, Mach-O, PE/COFF, WebAssembly, ar, and zstd to the binary signature registry, and exports the Format* constants so callers can switch on Result.Format without string literals.

Mach-O covers the four thin magics plus universal (FAT_MAGIC and FAT_MAGIC_64; the fat header is always big-endian on disk so the byte-swapped FAT_CIGAM constants are not on-disk signatures). The universal case checks that nfat_arch is in a plausible range so Java class files, which share the CA FE BA BE prefix, fall through unclassified rather than matching as Mach-O.

PE follows the e_lfanew offset from the DOS header to the PE\0\0 signature, bounded to the 512-byte sniff window so prefixResultCanChange stays correct. Files with a larger DOS stub are not recognised.

encoding/binary is now imported for the two uint32 field reads.

Closes #1.

Detect ELF, Mach-O (thin and universal, both byte orders), PE/COFF,
WebAssembly, ar, and zstd. PE follows e_lfanew within the 512-byte sniff
window. Universal Mach-O is gated on a plausible architecture count so
Java class files sharing the CA FE BA BE prefix are not misclassified.

Export the Format* constants so callers can switch on Result.Format
without string literals.

Closes #1

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds native executable/object and additional archive/compression signatures to the magic detector, and exposes exported Format* constants so downstream callers can switch on Result.Format without string literals.

Changes:

  • Exported Format* constants and updated all internal/tests to use them.
  • Added binary signatures for zstd, ELF, Mach-O (thin + universal with Java-class disambiguation), WASM, ar, and PE/COFF (bounded e_lfanew indirection).
  • Expanded tests and documentation to cover the new formats and edge cases (PE bounds, Mach-O vs Java class files).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
magic.go Exports Format* constants and adds MIME types for new formats; updates default text format assignment to FormatText.
signatures.go Adds new binary signatures and implements Mach-O fat header and PE header detection (bounded to sniffLength).
signatures_test.go Adds/updates registry tests for new formats plus targeted tests for PE bounds and Mach-O-vs-Java-class behavior.
text_test.go Updates expectations to use exported FormatText.
magic_test.go Updates expectations to use exported format constants.
README.md Documents the expanded format registry, exported constants usage, and the Mach-O/PE edge-case behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Per mach-o/fat.h the fat header is always big-endian on disk; FAT_CIGAM
and FAT_CIGAM_64 are memory-order constants for LE readers, not
alternative on-disk byte sequences. Keep FAT_MAGIC and FAT_MAGIC_64
gated on the big-endian nfat_arch check and add a negative test for the
swapped prefix.
@andrew
andrew merged commit a804821 into main Aug 3, 2026
4 checks passed
@andrew
andrew deleted the executable-signatures branch August 3, 2026 15:47
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.

Add executable and object file signatures

2 participants