Repository navigation
resolvers: expand magic32 metaformat subformat metadata and fixtures - #1628
Conversation
reyammer
left a comment
There was a problem hiding this comment.
Note: This is a review from yanickf@'s bot.
Review Verdict: CONCERNS
Summary & Risk Assessment
This is a clean, well-scoped follow-up to #1624 (#1618). Expanding Subformat from naming existing branches to also reading O(1) fixed-offset header fields inside already-validated containers (without loops, scanning, extra allocations, or changes to ContentType / VETOS) fits naturally on top of the thread-local SUBFORMAT mechanism, and several Commit 1 changes (isobmff.rs direct match tables replacing .contains() scans, executable.rs folding windows_pe_subformat into a single pass) simplify the existing code. None of the newly surfaced subformats collide with standalone labels in assets/content_types.yaml, and all PRONOM PUID, LoC FDD, and SQLite application_id constants match upstream specs/registries.
Runtime/regression risk is low (ContentType decisions are unchanged and all slice reads are bounds-checked), with three [medium] items below — two real-world container layout details in ar.rs and audio.rs (both still fixable in O(1) fixed-offset arithmetic), plus a question/cleanup around the 93 committed binary stubs in tests_data/resolvers/subformats/.
Findings
-
[medium]rust/lib/src/resolvers/magic/verifiers/meta/ar.rs:76-83— Modern BSD / macOS static libraries (#1/<len>+__.SYMDEF) are classified as"Unix ar (BSD extended filename)"instead of"Unix ar (BSD/macOS symbol table)"- Why: In 4.4BSD and Darwin/macOS (
ar,ranlib,libtool,llvm-ar --format=bsd,llvm-ar --format=darwin), archive symbol table members (__.SYMDEF,__.SYMDEF SORTED,__.SYMDEF_64,__.SYMDEF_64 SORTED) use the BSD#1/<len>extended filename header (e.g.!<arch>\n#1/12 ...\n__.SYMDEF\0\0\0or#1/20+__.SYMDEF SORTED\0\0\0\0), placing the actual member name at fixed offset@68right after`\n(@66..68). Matching onlyname_hdrat@8..24meansAR_BSD_SYMDEFonly triggers on legacy pre-4.4BSD inline headers (like!ar_bsd_symdef.a), whereas real-world BSD and macOS.astatic libraries hit#1/and report"Unix ar (BSD extended filename)". - Suggested fix: Check
@68in O(1) when#1/<len>declares at least 9 name bytes (nis already validated ASCII0x20..=0x7Eof length 16):plus a unit test inn if n.starts_with(b"__.SYMDEF") => with_subformat(Label::Ar, &AR_BSD_SYMDEF), n if n.starts_with(b"#1/") => { let is_symdef = (n[3] == b'9' || n[4].is_ascii_digit()) && eq_at(prefix, 68, b"__.SYMDEF"); with_subformat(Label::Ar, if is_symdef { &AR_BSD_SYMDEF } else { &AR_BSD_EXT }) }
resolvers_populate_o1_header_subformatsfor a#1/12or#1/20__.SYMDEFarchive.
- Why: In 4.4BSD and Darwin/macOS (
-
[medium]rust/lib/src/resolvers/magic/verifiers/category/audio.rs:135-165— Multiplexed Ogg files with anOgg Skeleton(fishead\0) track report"Ogg Skeleton"(fmt/944) instead of their media codec (fmt/945Ogg Theora,fmt/203Ogg Vorbis, etc.)- Why: Per the Xiph Ogg Skeleton 3.0/4.0 specification (and PRONOM
fmt/945's variant signature),fishead\0is a track-index metadata stream whose BOS page must appear as Page 0 (@0) of the physical Ogg bitstream, immediately ahead of the primary media track's BOS page (\x80theora,\x01vorbis,OpusHead,BBCD, etc.) at Page 1. Returning&OGG_SKELETONon Page 0 causes Skeleton-indexed.ogv/.ogafiles (such asffmpeg2theora/ Wikimedia Theora videos) to lose both their codec subformat ("Ogg Theora"/"Ogg Vorbis") and their specific PRONOM PUID (fmt/945/fmt/203), falling back to genericfmt/944/fdd000026. - Suggested fix: Because a
fishead\0BOS packet always fits in a single lacing segment (page_segments == 1: 64 bytes in Skeleton 3.0, 80 bytes in Skeleton 4.0,< 255), peeking at Page 1 requires no loop or scan — just one O(1) offset jumpnext = payload_off + usize::from(prefix[HEADER_FIXED_LEN]):Skeleton-only stubs (if let Some(mut sub) = prefix.get(payload_off..).and_then(bos_subformat) { if core::ptr::eq(sub, &OGG_SKELETON) && page_segments == 1 && let Some(seg_len) = u8_at(prefix, HEADER_FIXED_LEN) { let next = payload_off + usize::from(seg_len); if eq_at(prefix, next, MAGIC) && matches!(u8_at(prefix, next + HEADER_TYPE), Some(0x02 | 0x06)) && le32(prefix, next + PAGE_SEQ_NO) == Some(0) && let Some(next_segs @ 1..) = u8_at(prefix, next + PAGE_SEGMENTS) && let Some(media_sub) = prefix .get(next + HEADER_FIXED_LEN + usize::from(next_segs)..) .and_then(bos_subformat) && !core::ptr::eq(media_sub, &OGG_SKELETON) { sub = media_sub; } } return with_subformat(Label::Ogg, sub); }
!ogg_skeleton.ogx) continue to return"Ogg Skeleton", while Skeleton+Theora / Skeleton+Vorbis files resolve to their media codec and PUID/FDD.
- Why: Per the Xiph Ogg Skeleton 3.0/4.0 specification (and PRONOM
-
[medium]tests_data/resolvers/subformats/*&rust/lib/src/resolvers.rs:612-1243— 93 committed synthetic binary stubs (including 11 truncated ISOBMFF boxes and an undocumented!filename prefix) vs. in-memory test builders- Why:
#1624tested 437 subformats across 129 content types with zero committed binary files, using readable in-memory slice builders inresolvers.rs(matchingAGENTS.mdandwebsite-ng/src/content/docs/contributing/developer-guide.md:95), and this PR already tests the same verifiers in memory inresolvers_populate_magic32_subformatsandresolvers_populate_o1_header_subformats(resolvers.rs:1246-1463). All 93 files intests_data/resolvers/subformats/are synthetic zero-filled stubs (4–27 nonzero bytes, or output of thearchive(&[stored(b"mimetype", ...)], b"")helper atresolvers.rs:1469), which are opaque ingit diffand add ~590 lines of table boilerplate inresolvers.rs:612-1200.- Inside the committed fixtures:
- All 11
tests_data/resolvers/subformats/isobmff/*files are 16 bytes on disk (0x10) yet declarebox_len = 20(0x14) or24(0x18) at@0..4, leaving every ISOBMFF fixture truncated mid-ftypbox (box_len > file_size). - 59 of the 93 filenames start with
!, but unlikeresolvers_identify_fixtures(resolvers.rs:298),resolvers_identify_subformat_fixturesnever strips or checks!,tests_data/README.mdstill only listsbasic/andmitra/, and!is applied inconsistently across equally synthetic/truncated stubs (e.g.isobmff/3gp_r4.3gp,heif_heic.heic,elf/elf64_le_dyn.elf, andsqlite/*.sqlitehave no!, whereas!3gp_r6.3gpand!elf64_le_exec.elfdo).
- All 11
- Options:
- Option A (Preferred unless external tools consume this directory): Drop
tests_data/resolvers/subformats/and fold any cases not already covered byresolvers_populate_magic32_subformats/resolvers_populate_o1_header_subformatsinto in-memory builders inrust/lib/src/resolvers.rs(matching#1624). - Option B (If an on-disk subformat micro-corpus is intentionally wanted alongside
tests_data/resolvers/fixturesfor multi-tool comparison): Fix the 11isobmff/*fixtures sobox_lenmatches the file length (0x10or pad to20/24bytes), and documentresolvers/{fixtures,subformats}and the!filename prefix convention intests_data/README.md(and/or a comment onresolvers_identify_subformat_fixtures).
- Option A (Preferred unless external tools consume this directory): Drop
- Why:
-
[nit]rust/lib/src/resolvers/magic/verifiers/meta/zip.rs:863-865—ZIP_EMPTY_ARCHIVEonsize > 22vs. commented empty ZIPsif size >= 22 && prefix.len() >= 22 && starts(prefix, ZIP_EMPTY_ARCHIVE)labels inputs withsize > 22and!valid(22 empty-EOCD bytes followed by trailing bytes) as"Empty ZIP archive", whereas a valid empty ZIP with an archive comment (validwithPK\x05\x06+ 16 zero bytes at@0..20andcomment_len > 0at@20..22) falls through toif validand gets bareLabel::Zip. Gating&ZIP_EMPTYon(valid && starts(prefix, bytes!(ZIP_EOCD_SIGNATURE, &[0u8; 16]))) || (size == 22 && starts(prefix, ZIP_EMPTY_ARCHIVE))(while still returning bareSome(Label::Zip)forsize > 22 && starts(prefix, ZIP_EMPTY_ARCHIVE)soContentTypestays identical) handles both cleanly.
-
[nit]rust/lib/src/resolvers/magic/verifiers/overlap.rs:58— JVM.classmajor version45- Major version
45covers both JDK 1.0.2 (45.0..=45.3) and JDK 1.1 (45.3..=45.65535);"Java 1.0/1.1 class file"(or checkingminorat@4) is slightly more exact, though"Java 1.1 class file"matches commonfile(1)convention.
- Major version
Questions / Notes
- Intent of
tests_data/resolvers/subformats/: Is this directory intended for external multi-tool benchmarking (like Mitra /tests_data/resolvers/fixtures), or purely forrust/libunit tests? - SQLite
"Sqlr"(APP_SQLAR): Note that SQLite's built-insqlite3 -AcSQLAR archiver (shell.c.in) leavesapplication_id == 0rather than"Sqlr"; keepingAPP_SQLARis harmless (zero false-positive risk, and files withapplication_id == 0stay bareLabel::Sqlite), just worth noting thatsqlite3 -Acarchives won't hit this branch.
7b65fd6 to
70fd4f0
Compare
|
Thanks for the thorough review! Rebased onto
(Note on CI: all 23 Rust/Python/JS/KB/CodeQL checks are green; the |
Record subformat names (and more specific PRONOM PUID / LoC FDD overrides where applicable) from header fields already parsed and validated by the ISOBMFF, SQLite 3, ELF, Mach-O, Windows PE, and Java .class verifiers: - ISOBMFF: name all MP4, HEIF, and 3GPP major brands without altering the brand allowlists, replacing linear slice scans with direct matches. - SQLite 3: distinguish MBTiles header vs schema matches and map well-known registered application_id values at @68 on Label::Sqlite. - ELF: combine already-validated class, endianness, and e_type (@16) via a static lookup table while preserving fmt/688..fmt/691. - Mach-O: combine already-validated width, endianness, and filetype (@12) via a static lookup table while preserving fmt/692 and fmt/693. - Windows PE: combine PE32/PE32+, DLL flag, and subsystem (@68) in a single header pass, removing the redundant e_lfanew re-read. - Java .class: map already-validated major versions 45..=71 (@6) to Java 1.1..Java 27 class files while keeping 72..=80 as bare javabytecode. No ContentType decisions, VETOS, or KB outputs are changed. Adds minimal subformat fixtures under tests_data/resolvers/subformats/ and unit tests.
… and ZIP
Populate finer-grained `subformat` metadata (and more specific PRONOM PUID /
LoC FDD identifiers where standardized) for five magic32-dispatched
metaformats using only O(1) fixed-offset header checks within already-matched
containers, with zero scanning and zero changes to `ContentType` decisions,
`VETOS`, or generated KB artifacts:
- Ogg (`audio.rs`): inspect the BOS identification packet header at
`@(27 + page_segments)` via a single-byte switch (Vorbis `fmt/203`/
`fdd000117`, Opus `fmt/946`, Theora `fmt/945`, FLAC `fmt/947`/`fdd000227`,
Speex `fmt/948`/`fdd000260`, Skeleton, Kate, CELT, Dirac, Daala, MIDI, PCM).
- Unix ar (`ar.rs`): classify the first member header name at `@8..24` on
`Label::Ar` (GNU/SVR4 symbol table, 64-bit symbol table, long filename
table, BSD/macOS symbol table, BSD extended filename).
- RIFF/RIFX WAVE (`riff.rs`): inspect only the initial chunk at `@12`
(`"fmt "` + `wFormatTag` at `@20` for PCM `fmt/141`/`fdd000002`, Extensible
`fmt/143`/`fdd000001`, MS ADPCM, IEEE float, A-Law, mu-Law, IMA ADPCM, MP3;
`"bext"` + EBU Tech 3285 `Version` at `@366` for Broadcast WAVE v0 `fmt/1`,
v1 `fmt/2`/`fdd000356`, v2 `fmt/527`/`fdd000357`, and generic BWF; plus
big-endian `RIFX WAVE` format tags) without walking chunks.
- TIFF (`image.rs`): recognize Canon RAW 2 (`"CR\x02\x00"` at `@8..12` with
IFD0 offset >= 16) on little-endian TIFF (`fmt/592`/`fdd000241`).
- ZIP (`zip.rs`): reuse the already-extracted stored `"mimetype"` first-entry
payload on `Label::Zip` fallback paths for OpenDocument subtypes/templates
(`odg`, `odf`, `odb`, `odc`, `odi`, `ott`, `ots`, `otp`, `otg`, `odm` with
`fdd000247`), Krita (`fmt/999`), OpenRaster (`fmt/998`), and Adobe InDesign
IDML (`fmt/1184`), plus `Empty ZIP archive` on 22-byte EOCD archives.
Adds 40 minimal subformat test fixtures under
`tests_data/resolvers/subformats/{ar,ogg,tiff,wav,zip}/` and unit tests
covering both fixtures and boundary/edge cases.
Map the SQLite 3 application_id "RFLT" (@68) to "Reflet database (.reflet)" on Label::Sqlite and add a minimal 132-byte header fixture under tests_data/resolvers/subformats/sqlite/reflet.reflet with test coverage.
…consolidate fixtures
70fd4f0 to
e34140a
Compare
|
Thank you! Merging. |
Resolve the conflict with google#1628 by taking main's ftyp verifier; the mif1 handling is reworked in the next commit.
Follow-up to #1624 (#1618): expand
RegularFileType::metadata(subformat, plus more specificpronom_puid/loc_fddoverrides where standardized) across majormagic32-dispatched metaformats, with minimal test fixtures undertests_data/resolvers/fixtures/and zero changes toContentTypedecisions,VETOS, or generated KB artifacts.Commit 1: Zero-new-parsing subformats (fields already parsed & validated)
isobmff.rs): name all 35 MP4, 8 HEIF, and 25 3GPP major brands via directmatch(replacing linear.contains()scans), preserving brand allowlists.sqlite.rs): distinguish MBTilesMPBXheader vs schema matches and map 22 well-known registeredapplication_idvalues (@68) onLabel::Sqlite.elf.rs): combine already-validated class, endianness, ande_type(@16) via a static lookup table, preservingfmt/688..fmt/691.executable.rs): combine already-validated width, endianness, andfiletype(@12) via a static lookup table, preservingfmt/692andfmt/693.executable.rs): combine PE32/PE32+, DLL flag, and subsystem (@68) in a single header pass, preservingfmt/899andfmt/900..class(overlap.rs): map already-validated major versions45..=71(@6) to Java 1.0/1.1..Java 27 class files while leaving72..=80bare.Commit 2: O(1) fixed-offset header subformats (zero scanning)
audio.rs): inspect the BOS identification packet header at@(27 + page_segments)via a 1-byte switch (Vorbisfmt/203/fdd000117, Opusfmt/946, Theorafmt/945, FLACfmt/947/fdd000227, Speexfmt/948/fdd000260, Skeleton, Kate, CELT, Dirac, Daala, MIDI, PCM), with a single O(1) Page 1 peek when Page 0 isOgg Skeleton(fishead\0).ar(ar.rs): classify the first member header name at@8..24onLabel::Ar(GNU/SVR4 symbol table, 64-bit symbol table, long filename table, BSD/macOS inline /<len>symbol tables, BSD extended filename).riff.rs): inspect only the initial chunk at@12("fmt "+wFormatTagat@20for PCMfmt/141/fdd000002, Extensiblefmt/143/fdd000001, MS ADPCM, IEEE float, A-Law, mu-Law, IMA ADPCM, MP3;"bext"+ EBU Tech 3285Versionat@366for Broadcast WAVE v0fmt/1, v1fmt/2/fdd000356, v2fmt/527/fdd000357, and generic BWF; plus big-endianRIFX WAVEformat tags) without walking chunks.image.rs): identify Canon RAW 2 ("CR\x02\x00"at@8..12with IFD0 offset>= 16) on little-endian TIFF (fmt/592/fdd000241).zip.rs): reuse the already-extracted stored"mimetype"first-entry payload onLabel::Zipfallback paths for OpenDocument subtypes/templates (odg,odf,odb,odc,odi,ott,ots,otp,otg,odmwithfdd000247), Krita (fmt/999), OpenRaster (fmt/998), and Adobe InDesign IDML (fmt/1184), plusEmpty ZIP archiveon empty EOCD archives (with or without an archive comment).Commit 3: Reflet SQLite
application_id("RFLT")sqlite.rs): mapapplication_id"RFLT"(@68) to"Reflet database (.reflet)"onLabel::Sqlite(23 SQLiteapplication_idsubformats total).Commit 4: Review fixes & fixture layout consolidation
ar.rs(#1/<len>+__.SYMDEF),audio.rs(Ogg Skeleton Page 1 codec peek),zip.rs(empty ZIP comment vs trailing bytes), andoverlap.rs(Java 1.0/1.1 class file).tests_data/resolvers/subformats/directly intotests_data/resolvers/fixtures/(_ar,_elf,_isobmff,_macho,_ogg,_pe,_riff,_sqlite,_tiff,_zip,bytecode), restore fullftypbox lengths (20/24B) on the 11_isobmff/fixtures, and documentresolvers/fixtures/and the"!"synthesis convention intests_data/README.md.Testing & Performance
rustup run nightly cargo fmt -- --checkacross allrust/crates (verified againstrust/rustfmt.toml)rustup run stable&rustup run nightly./test.shinrust/lib(unit tests + doctest,clippy --all-targets --features=pipeline,serde -- --deny=warnings,RUSTDOCFLAGS=--deny=warnings cargo doc --features=_doc)rust/cli,rust/pyo3,rust/ffitest suitesuv run scripts/sync_kb.py --self-test&--check,check_copyright.py,check_documentation.pyContentTypedrift, 0 panics, 0 metadata leakage.