Softmagic Text Classification#
GNU file runs magic-rule matching in two distinct passes before optionally appending a text-class suffix to the final description. libmagic-rs currently implements only a subset of this behavior. This article documents the full GNU file contract, the current libmagic-rs implementation state, and the open work in issue #382.
GNU file Two-Pass Model#
GNU file's file_buffer() function runs file_softmagic twice:
- Pass 1 ā BINTEST. Evaluates rules whose test type is binary (numeric/date/float/offset/der/guid types, or string rules with
/b, or untagged string rules that are not UTF-8). If any message-bearing rule fires, classification is complete ā no text tail is appended. - Pass 2 ā TEXTTEST. Runs only when pass 1 produced no output and
file_encodingdetermines the buffer is text-like (and longer than 1 byte).file_ascmagicevaluates text-typed rules and then appends a text-class suffix.
Each top-level rule's pass assignment is determined by its first line only (set_test_type in upstream apprentice.c):
| Type | Pass |
|---|---|
| Numeric, date, float, offset, DER, GUID | BINTEST |
string/pstring/string16 without /t | BINTEST |
string with /t | TEXTTEST |
regex/search with /b | BINTEST |
regex/search with /t | TEXTTEST |
regex/search (untagged) | TEXTTEST if looks_utf8(pattern) > 0, else BINTEST |
default/use/indirect/clear at top level | Neither (skipped in both passes) |
Child rules never contribute to the pass assignment. An indirect re-entry always runs as BINTEST.
Text-Class Suffix and Tail Rewrite#
When pass 2 fires, upstream ascmagic.c appends the charset class and then performs two string rewrites:
file_replace(" text$", ", ")
file_replace(" text executable$", ", ")
It then re-appends " executable" when appropriate, producing outputs like:
C source, ASCII textPOSIX shell script, ASCII text executableXML 1.0 document, ASCII text
The .result fixtures under third_party/tests/ reflect this rewritten form.
macOS host divergence. The macOS/Apple build of file-5.41 does not perform this rewrite and emits c program text, ASCII text instead of C source, ASCII text. This is a host-specific quirk, not the canonical contract; oracle tests comparing against a macOS file binary must normalize for this difference.
Current libmagic-rs Behavior#
libmagic-rs does not implement the two-pass model or append the text-class tail to magic-rule descriptions. The current behavior:
evaluate_rulesinsrc/evaluator/engine/mod.rsruns a single hierarchical evaluation pass with no BINTEST/TEXTTEST distinction.StringFlags::text_testandStringFlags::bin_testparse the/tand/bflags but treat them as informational hints; they do not alter comparison or pass routing.build_resultinsrc/lib.rsusesclassify_fallbackfromsrc/output/ascmagic.rsonly when rule evaluation produces no description at all. It does not append a suffix to existing descriptions.
The classify_fallback function returns one of "empty", "ASCII text", "UTF-8 Unicode text", or "data" . Note that libmagic-rs returns "UTF-8 Unicode text" where GNU file prints "Unicode text, UTF-8 text" ā the label is stale and will need correction as part of the issue #382 work.
MIME Mapping Interaction#
MimeMapper::get_mime_type picks the longest keyword found anywhere in the description string . This creates a correctness hazard once the text tail is appended: a description like HTML document, ASCII text contains both "html" ā text/html and "text" ā text/plain, and the longer-keyword rule currently selects "html" correctly ā but the presence of "ascii text" in the string widens the match surface.
The reliable fix, noted in the background context for this topic, is to derive MIME from the pre-tail magic description, before the , ASCII text suffix is concatenated. The MIME lookup must not see the appended tail.
The keyword table lives in MIME_MAPPINGS (static LazyLock<HashMap>; entries include ("ascii", "text/plain"), ("utf-8", "text/plain"), and ("text", "text/plain")).
Open Work: Issue #382#
Issue #382 tracks implementing the full text-class-append behavior. Key design constraints from the issue:
- Append is conditional: binary or already-fully-classified descriptions (e.g., ELF) must not receive a tail.
- The implementation touches
MagicDatabase::build_resultandsrc/output/ascmagic.rs. - MIME lookup must be computed from the pre-tail description.
- The differential test harness currently strips text-suffix differences to suppress noise; those suppressions can be removed once #382 lands.
- A full plan document was referenced as
docs/plans/2026-10-04-001-feat-ascmagic-text-class-append-plan.mdbut has not yet been committed to the main branch at time of writing.
Related tracking references: commit #376 (prior fallback work), issue #379 (strength ordering), issue #498 (bounded inert descriptions).
Key Source References#
| File | Role |
|---|---|
src/output/ascmagic.rs | classify_fallback ā fallback text/data classifier |
src/lib.rs | build_result ā where fallback is invoked and tail append will land |
src/evaluator/engine/mod.rs | evaluate_rules ā single-pass rule evaluator |
src/parser/ast.rs | StringFlags::text_test / bin_test ā /t and /b flag fields |
src/mime.rs | MimeMapper::get_mime_type ā longest-keyword MIME lookup |
third_party/tests/ | .result fixtures reflecting the rewritten tail form |
| Issue #382 | Tracking issue for text-class-append implementation |