Skip to content

Probe common magic types within their priority band - #172

Merged
jeremy merged 1 commit into
mainfrom
canon-cr2-detection
Aug 20, 2026
Merged

jeremy merged 1 commit into
mainfrom
canon-cr2-detection

Conversation

@jeremy

@jeremy jeremy commented Aug 20, 2026

Copy link
Copy Markdown
Member

Fixes #141.

Root cause — an ordering bug, not a missing matcher

Tika ships an image/x-canon-cr2 matcher at priority 60 (TIFF header + CR marker at offset 8), deliberately ranked above image/tiff's priority-50 matcher — which also matches every CR2 file's TIFF header. But the generator's common-types optimization moved its curated matchers to the front of the whole MAGIC table: image/tiff probed at index 23 while the CR2 matcher sat at 125 and could never win. Every CR2 file content-detected as image/tiff — exactly the issue's reproduction.

Fix

Fold the common-types rank into the priority sort key: common types now probe first within their own priority band but can no longer preempt matchers Tika explicitly ranks above them. Every declared priority is respected; the fix covers all ~120 above-50 matchers shadowed the same way, not just CR2. Cost: a bounded handful of extra byte comparisons before the common block on an operation by_magic documents as slow.

Type-name split

The runtime override mapped both cr2 and crw to generic image/x-raw-canon, so extension/name lookups disagreed with the (now working) content magic:

  • .cr2 → image/x-canon-cr2 (Tika's specific type, already a subclass of image/tiff, so declared-type and filename refinement keep working — the issue's own file(1) reference output)
  • .crw stays image/x-raw-canon — Tika's own type for CIFF, with its HEAPCCDR magic — keeping the image/tiff parent link for refinement

Verification

  • Content-only detection: image/x-canon-cr2 in both endiannesses; plain TIFF still image/tiff; CRW still image/x-raw-canon
  • for(io, name: "photo.cr2", declared_type: "image/tiff") → image/x-canon-cr2
  • Fixtures are minimal synthesized headers (TIFF byte order + CR version marker + empty IFD; CIFF HEAPCCDR), authored for this suite — no third-party image content
  • 603 runs, 0 failures; tables:check green

Fixes Canon CR2 detection (#141). Tika ships an image/x-canon-cr2
matcher at priority 60 — a TIFF header with a CR marker at offset 8 —
deliberately ranked above image/tiff's priority-50 matcher, which also
matches every CR2 file. But the generator's common-types optimization
moved the curated common matchers to the front of the whole MAGIC
table, so image/tiff probed at index 23 while the CR2 matcher sat at
125 and never won. Every CR2 file content-detected as image/tiff.

Fold the common-types rank into the priority sort instead: common
types are now probed first within their own priority band, but can no
longer preempt matchers Tika explicitly ranks above them. The ~120
above-50 matchers now probe ahead of the common block — a handful of
bounded byte comparisons per call, on an operation documented as slow —
and every declared priority is respected.

Also resolve the .cr2 type-name split: the runtime override mapped
both cr2 and crw to the generic image/x-raw-canon, so extension and
name lookups disagreed with the (now working) content magic. The .cr2
extension belongs to image/x-canon-cr2, which already subclasses
image/tiff, so declared-type and filename refinement keep working.
image/x-raw-canon keeps CRW — Tika's own type for it, with its CIFF
magic — plus the image/tiff parent link for refinement.

Fixtures are minimal synthesized headers (TIFF byte order, CR version
marker, empty IFD; CIFF HEAPCCDR for CRW) in both endiannesses,
authored for this suite. The existing .cr2-named fixtures move from
image/x-raw-canon to image/x-canon-cr2 to match the reconciled type.

Fixes #141
@jeremy
jeremy merged commit f71831d into main Aug 20, 2026
20 checks passed
@jeremy
jeremy deleted the canon-cr2-detection branch August 20, 2026 15:32
jeremy added a commit that referenced this pull request Aug 21, 2026
…tchers

#172 fixed CR2 detection by folding the common-types rank into the priority
sort, so common types were only probed first within their own priority band.
That put the ~120 matchers Tika ranks above 50 ahead of every common type:
detecting a JPEG went from 25 reads to 255, PNG from 26 to 256, PDF from 72
to 293, with wall time rising accordingly on every call.

Move the common types back to the front of the table, but have each one
carry along the higher-priority matchers for its own subtypes, so promoting
a common type can never preempt a more specific verdict for the same
content. image/x-canon-cr2 (priority 60, a subtype of image/tiff) still
probes immediately ahead of image/tiff, the MP4 and HEIF family ahead of
video/quicktime's broad ftyp match, and the Ogg codecs ahead of their
containers. Parent links are now collected for every type during
generation, not only glob-bearing ones, so the subtype walk sees the whole
hierarchy.

JPEG, PNG and GIF are back to 25, 26 and 28 reads at the same table
positions as before #172; PDF takes 85, the cost of the carried matchers.
A generator test pins the ordering rule and a read-budget test pins the
fast path.
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.

[Bug] Cannon .CR2 file is not properly detected

1 participant