nx_media_sniff.nx
buildroot/runtime/nx_media_sniff.nx
about
nx_media_sniff.nx -- determine media type from magic bytes / MIME / extension.
Four-tier detection (highest trust first):
1. Magic-byte signature (binary formats; reads actual content)
2. MIME-string parsing (when the source provided Content-Type)
3. URL/path extension (text formats + container disambiguation)
4. mostly-printable (LAST-RESORT fallback -> TEXT)
Returns NX_MEDIA_TYPE_* (see nx_media_pool.nx). Returns
NX_MEDIA_TYPE_UNKNOWN when all four tiers fail.
===== 2026-07-30 REWRITE (rule 3: rewrite, do not patch-cascade) =====
ROOT DEFECT MEASURED: the old sniffer knew only 6 magics (PNG/JPEG/WebP/GIF/
MP4/WebM + MP3/WAV/OGG). It had NO MPEG-TS detector and no ".ts" extension
row, so the ENTIRE 53,567-file cam recording corpus migrated into the vault
as type=unknown (proven by reading live NXR1 records in
knowledge/store/mvault-seg-1785089367.docs: every _cam.ts record showed
type=unknown while _cam.jpg posters showed image). MV_TYPE_MODEL3D and
MV_TYPE_BOOK were likewise unreachable from bytes -- there was no sniff code
to return. This rewrite closes the door properly: STL/PLY/GLB/FBX 3D,
PDF/EPUB/MOBI/DJVU books, WARC web captures, and the full A/V container set.
TRUST-ORDER FIX: "mostly printable" is a FALLBACK HEURISTIC, not a magic.
The old code returned it from inside the magic tier, which meant an ASCII
STL or a .obj mesh was sealed as TEXT before the extension tier ever ran.
nx_sniff_bytes_strict() omits that fallback so the composite can order the
tiers honestly; nx_sniff_bytes() keeps its original contract for existing
callers.
CONTAINER REFINEMENT: ZIP and generic-TEXT are CONTAINER verdicts, not final
ones -- .epub/.cbz/.3mf/.wacz are all "PK\x03\x04", and .obj/.gcode/.dae are
all plain text. For those two verdicts ONLY, the extension tier refines the
answer. Every other magic still wins outright.
Per the boundary cardinal: sniffer is a TRUST BOUNDARY -- treats
every byte as adversarial. Always reads through bounded loops
per the JPL Rule 2 cardinal. No const arrays (the CONST[i] trap
compiles clean then reads garbage); every signature is a literal.
See: docs/NISHI_MEDIA_INGESTION_ROADMAP.md
dependencies 2 imports · 8 importers
imports: nx_syscalls.nxnx_media_pool.nx
imported by: nx_entity_media.nxnx_media_fetch.nxnx_media_gather.nxnx_mvault.nxnx_mvault_ingest.nxnx_mvault_reindex.nxnx_mvault_walk.nxnx_text_ingest_test.nx
structs
| none |
consts
| none |
functions
| 72 | func nx_sniff_load_u8(p: *u8, i: i64, n: i64) -> i64 |
| 81 | func nx_sniff_at(b: *u8, n: i64, off: i64, lit: *u8, llen: i64) -> i64 |
| 95 | func nx_sniff_find(b: *u8, n: i64, limit: i64, lit: *u8, llen: i64) -> i64 |
| 108 | func nx_sniff_u32le(b: *u8, n: i64, off: i64) -> i64 |
| 121 | func nx_sniff_is_png(b: *u8, n: i64) -> i64 |
| 135 | func nx_sniff_is_jpeg(b: *u8, n: i64) -> i64 |
| 144 | func nx_sniff_is_gif(b: *u8, n: i64) -> i64 |
| 159 | func nx_sniff_is_webp(b: *u8, n: i64) -> i64 |
| 173 | func nx_sniff_is_bmp(b: *u8, n: i64) -> i64 calls 1: nx_sniff_at |
| 180 | func nx_sniff_is_tiff(b: *u8, n: i64) -> i64 |
| 200 | func nx_sniff_is_ico(b: *u8, n: i64) -> i64 |
| 213 | func nx_sniff_is_psd(b: *u8, n: i64) -> i64 |
| 219 | func nx_sniff_is_qoi(b: *u8, n: i64) -> i64 |
| 225 | func nx_sniff_is_dds(b: *u8, n: i64) -> i64 |
| 231 | func nx_sniff_is_exr(b: *u8, n: i64) -> i64 |
| 241 | func nx_sniff_is_jxl(b: *u8, n: i64) -> i64 |
| 249 | func nx_sniff_is_netpbm(b: *u8, n: i64) -> i64 |
| 267 | func nx_sniff_is_mp4(b: *u8, n: i64) -> i64 |
| 279 | func nx_sniff_isobmff_kind(b: *u8, n: i64) -> i64 |
| 313 | func nx_sniff_is_webm(b: *u8, n: i64) -> i64 |
| 328 | func nx_sniff_is_mpegts(b: *u8, n: i64) -> i64 |
| 338 | func nx_sniff_is_m2ts(b: *u8, n: i64) -> i64 |
| 347 | func nx_sniff_is_mpegps(b: *u8, n: i64) -> i64 |
| 359 | func nx_sniff_is_avi(b: *u8, n: i64) -> i64 |
| 367 | func nx_sniff_is_flv(b: *u8, n: i64) -> i64 |
| 375 | func nx_sniff_is_asf(b: *u8, n: i64) -> i64 |
| 391 | func nx_sniff_is_mp3(b: *u8, n: i64) -> i64 |
| 408 | func nx_sniff_is_wav(b: *u8, n: i64) -> i64 |
| 422 | func nx_sniff_is_ogg(b: *u8, n: i64) -> i64 |
| 432 | func nx_sniff_is_flac(b: *u8, n: i64) -> i64 |
| 438 | func nx_sniff_is_aiff(b: *u8, n: i64) -> i64 |
| 449 | func nx_sniff_is_midi(b: *u8, n: i64) -> i64 |
| 455 | func nx_sniff_is_ape(b: *u8, n: i64) -> i64 |
| 467 | func nx_sniff_is_glb(b: *u8, n: i64) -> i64 |
| 473 | func nx_sniff_is_ply(b: *u8, n: i64) -> i64 |
| 485 | func nx_sniff_is_stl_ascii(b: *u8, n: i64) -> i64 |
| 497 | func nx_sniff_is_stl_binary(b: *u8, n: i64, filesize: i64) -> i64 |
| 510 | func nx_sniff_is_fbx(b: *u8, n: i64) -> i64 |
| 516 | func nx_sniff_is_blend(b: *u8, n: i64) -> i64 |
| 522 | func nx_sniff_is_vox(b: *u8, n: i64) -> i64 |
| 528 | func nx_sniff_is_step(b: *u8, n: i64) -> i64 |
| 536 | func nx_sniff_is_pdf(b: *u8, n: i64) -> i64 |
| 543 | func nx_sniff_is_epub(b: *u8, n: i64) -> i64 |
| 551 | func nx_sniff_is_mobi(b: *u8, n: i64) -> i64 |
| 557 | func nx_sniff_is_djvu(b: *u8, n: i64) -> i64 |
| 563 | func nx_sniff_is_chm(b: *u8, n: i64) -> i64 |
| 569 | func nx_sniff_is_rtf(b: *u8, n: i64) -> i64 |
| 582 | func nx_sniff_is_warc(b: *u8, n: i64) -> i64 |
| 588 | func nx_sniff_is_mhtml(b: *u8, n: i64) -> i64 |
| 598 | func nx_sniff_is_zip(b: *u8, n: i64) -> i64 |
| 608 | func nx_sniff_is_rar(b: *u8, n: i64) -> i64 |
| 614 | func nx_sniff_is_7z(b: *u8, n: i64) -> i64 |
| 629 | func nx_sniff_is_html_token(b: *u8, n: i64) -> i64 |
| 653 | func nx_sniff_is_svg_token(b: *u8, n: i64) -> i64 |
| 660 | func nx_sniff_is_mostly_printable(b: *u8, n: i64) -> i64 |
| 691 | func nx_sniff_bytes_strict(b: *u8, n: i64, filesize: i64) -> i64 |
| 768 | func nx_sniff_bytes(b: *u8, n: i64) -> i64 |
| 782 | func nx_sniff_mime_eq(mime: *u8, mlen: i64, lit: *u8, llen: i64) -> i64 |
| 800 | func nx_sniff_mime(mime: *u8, mlen: i64) -> i64 |
| 869 | func nx_sniff_ext_eq(p: *u8, plen: i64, off: i64, called by 4: nx_sniff_ext_imagenx_sniff_ext_avnx_sniff_ext_model3dnx_sniff_ext_doc calls 1: nx_sniff_load_u8 |
| 882 | func nx_sniff_ext_off(path: *u8, plen: i64) -> i64 |
| 903 | func nx_sniff_ext_image(p: *u8, plen: i64, off: i64) -> i64 |
| 959 | func nx_sniff_ext_av(p: *u8, plen: i64, off: i64) -> i64 |
| 1029 | func nx_sniff_ext_model3d(p: *u8, plen: i64, off: i64) -> i64 |
| 1075 | func nx_sniff_ext_doc(p: *u8, plen: i64, off: i64) -> i64 |
| 1147 | func nx_sniff_extension(path: *u8, plen: i64) -> i64 |
| 1169 | func nx_sniff_full(b: *u8, n: i64, filesize: i64, |
| 1196 | func nx_sniff(b: *u8, n: i64, |