tools/gen_registry.py now also writes src/Rosetta/Sniffs.luau: each format's sniff copied (tools/sniff_extract.py) with the top-level locals, types and load-time loops it reaches; requires outside core/ become proxies that require on first field access. 198 of 199 sniffs are copied (model/fbx reads Binary.MAGIC at load time: its module sniffs). Registry entries gain sniff (0 copy, 1 copy reaching a codec / container, 2 module) and variant (the module has variant(r)). init.luau detect: the extension's formats first, then by sniff cost and registry order; a sniff runs only while its cap (1, or LATE) could still win, ties to registry order, so results equal the eager path. F.detect loads the winner only when it has variant(). tests/detect.luau: every copied sniff against its module's on samples of every fixture module (tools/fixtures/detect.py), scores within 0..1, and F.detect against the eager reference with no name, the right / wrong / unknown extension and per category. Outside init / Registry / gen_registry: tools/fixtures.py (LAST generators run after the others and rerun when their outputs change), tools/lint_rules.py (Sniffs is api layer, skipped by DUP), docs/STYLE.md and docs/API.md notes, build/ regenerated.
3385 lines
88 KiB
Luau
3385 lines
88 KiB
Luau
--!strict
|
|
--!optimize 2
|
|
-- Sniffs: GENERATED by tools/gen_registry.py from the format folders - do not edit.
|
|
-- Each format's sniff (format path -> sniff(r)), copied from its init.luau with the top-level locals and types it
|
|
-- reaches (tools/sniff_extract.py), so detection requires no format module. Requires outside core/ are proxies that
|
|
-- require on first use. Formats whose sniff needs their module (Registry sniff = 2) are not here.
|
|
local S: { [string]: (any) -> number } = {}
|
|
|
|
local function lazy(load: () -> any): any
|
|
local proxy = {}
|
|
return setmetatable(proxy, {
|
|
__index = function(_, k: any): any
|
|
local m = load()
|
|
setmetatable(proxy, { __index = m })
|
|
return m[k]
|
|
end,
|
|
})
|
|
end
|
|
|
|
do -- image/ani
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/ani"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "RIFF" and r:peek(4, 8) == "ACON" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/art
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local readu8, readu16, readu32, readi16 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi16
|
|
local function header(b: buffer, o: number, limit: number): (number?, number?, number?, number?, boolean?)
|
|
local p = o
|
|
local prefixed = false
|
|
if limit - p >= 8 and buffer.readstring(b, p, 8) == "BUILDART" then
|
|
p += 8
|
|
prefixed = true
|
|
end
|
|
if limit - p < 16 or readu32(b, p) ~= 1 then
|
|
return nil, nil, nil, nil, nil
|
|
end
|
|
local first, last = readu32(b, p + 8), readu32(b, p + 12)
|
|
if last < first or last - first >= 65536 then
|
|
return nil, nil, nil, nil, nil
|
|
end
|
|
return p + 16, first, last, readu32(b, p + 4), prefixed
|
|
end
|
|
S["image/art"] = function(r: Bytes.Reader): number
|
|
local at, first, last = header(r.buf, r.base, r.limit)
|
|
if not at or not first or not last then
|
|
return 0
|
|
end
|
|
local n = last - first + 1
|
|
local p = at :: number
|
|
if r.limit - p < n * 8 then
|
|
return 0
|
|
end
|
|
local total = 0
|
|
for i = 0, n - 1 do
|
|
local w, h = readi16(r.buf, p + i * 2), readi16(r.buf, p + n * 2 + i * 2)
|
|
if w < 0 or h < 0 then
|
|
return 0
|
|
end
|
|
total += w * h
|
|
end
|
|
local size = p + n * 8 + total - r.base
|
|
local have = r.limit - r.base
|
|
return if size == have then 0.9 elseif size < have and have - size < 4096 then 0.5 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/blp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/blp"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
return if m == "BLP1" or m == "BLP2" then 1 elseif m == "BLP0" then 0.8 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/bmp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/bmp"] = function(r: Bytes.Reader): number
|
|
if r:peek(2) ~= "BM" then
|
|
return 0
|
|
end
|
|
local n = r:peekU8(14) + math.max(r:peekU8(15), 0) * 256
|
|
if r:peekU8(16) == 0 and r:peekU8(17) == 0 and (n == 12 or n == 16 or n == 40 or n == 52 or n == 56 or n == 64 or n == 108 or n == 124) then
|
|
return 0.9
|
|
end
|
|
return 0.2
|
|
end
|
|
end
|
|
|
|
do -- image/dds
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local MAGIC = "DDS "
|
|
S["image/dds"] = function(r: Bytes.Reader): number
|
|
return math.max(Sniff.magic(r, MAGIC .. "\124\0\0\0"), Sniff.magic(r, MAGIC, 0, 0.6))
|
|
end
|
|
end
|
|
|
|
do -- image/exr
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local MAGIC = "\118\47\49\1"
|
|
S["image/exr"] = function(r: Bytes.Reader): number
|
|
return Sniff.magic(r, MAGIC)
|
|
end
|
|
end
|
|
|
|
do -- image/gif
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/gif"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(6)
|
|
return if s == "GIF89a" or s == "GIF87a" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/hdr
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/hdr"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(10)
|
|
if s == "#?RADIANCE" then
|
|
return 1
|
|
end
|
|
if string.sub(s, 1, 6) == "#?RGBE" then
|
|
return 1
|
|
end
|
|
if string.sub(s, 1, 2) == "#?" and string.match(s, "^#%?%u") then
|
|
return 0.5
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- image/icns
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["image/icns"] = function(r: Bytes.Reader): number
|
|
return Sniff.magic(r, "icns")
|
|
end
|
|
end
|
|
|
|
do -- image/ico
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local readu8 = buffer.readu8
|
|
S["image/ico"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 22 then
|
|
return 0
|
|
end
|
|
local b, o = r.buf, r.pos
|
|
local typ = buffer.readu16(b, o + 2)
|
|
local count = buffer.readu16(b, o + 4)
|
|
if buffer.readu16(b, o) ~= 0 or (typ ~= 1 and typ ~= 2) or count == 0 then
|
|
return 0
|
|
end
|
|
-- the first entry: reserved byte 0, a size and offset inside the file
|
|
local size, off = buffer.readu32(b, o + 14), buffer.readu32(b, o + 18)
|
|
if readu8(b, o + 9) ~= 0 or size == 0 or off < 6 + count * 16 then
|
|
return 0
|
|
end
|
|
return 0.8
|
|
end
|
|
end
|
|
|
|
do -- image/ilbm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/ilbm"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "FORM" then
|
|
return 0
|
|
end
|
|
local t = r:peek(4, 8)
|
|
return if t == "ILBM" or t == "PBM " then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/jpeg
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/jpeg"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(3)
|
|
return if s == "\255\216\255" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/ktx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local ID1 = "\171KTX 11\187\r\n\26\n"
|
|
local ID2 = "\171KTX 20\187\r\n\26\n"
|
|
S["image/ktx"] = function(r: Bytes.Reader): number
|
|
return math.max(Sniff.magic(r, ID2), Sniff.magic(r, ID1))
|
|
end
|
|
end
|
|
|
|
do -- image/lmp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function kindOf(r: Bytes.Reader): string?
|
|
local n = r:remaining()
|
|
if n == 768 then
|
|
return "palette"
|
|
elseif n == 16385 or n == 16384 then
|
|
return "colormap"
|
|
end
|
|
if n < 9 then
|
|
return nil
|
|
end
|
|
local w, h = buffer.readu32(r.buf, r.pos), buffer.readu32(r.buf, r.pos + 4)
|
|
if w > 0 and h > 0 and w <= 4096 and h <= 4096 and 8 + w * h == n then
|
|
return "qpic"
|
|
end
|
|
return nil
|
|
end
|
|
S["image/lmp"] = function(r: Bytes.Reader): number
|
|
local k = kindOf(r)
|
|
return if k == "qpic" then 0.3 elseif k then 0.05 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/pcx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/pcx"] = function(r: Bytes.Reader): number
|
|
if r:size() < 128 then
|
|
return 0
|
|
end
|
|
local m, v, e, bpp = r:peekU8(0), r:peekU8(1), r:peekU8(2), r:peekU8(3)
|
|
if m ~= 10 or (v ~= 0 and v ~= 2 and v ~= 3 and v ~= 4 and v ~= 5) or e > 1 then
|
|
return 0
|
|
end
|
|
return if bpp == 1 or bpp == 2 or bpp == 4 or bpp == 8 then 0.8 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/png
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local SIGNATURE = "\137PNG\r\n\26\n"
|
|
S["image/png"] = function(r: Bytes.Reader): number
|
|
return if r:peek(8) == SIGNATURE then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/pnm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/pnm"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 3 then
|
|
return 0
|
|
end
|
|
local a, b, c = r:peekU8(0), r:peekU8(1), r:peekU8(2)
|
|
if a ~= 80 then -- 'P'
|
|
return 0
|
|
end
|
|
local ws = c == 32 or (c >= 9 and c <= 13)
|
|
if b >= 49 and b <= 55 and ws then -- P1..P7
|
|
return 0.9
|
|
end
|
|
if (b == 70 or b == 102) and ws then -- PF / Pf
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- image/qoi
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/qoi"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "qoif" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/sgi
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["image/sgi"] = function(r: Bytes.Reader): number
|
|
if Sniff.magic(r, "\1\218") == 0 or r:size() < 512 then
|
|
return 0
|
|
end
|
|
local st, bpc = r:peekU8(2), r:peekU8(3)
|
|
return if st <= 1 and (bpc == 1 or bpc == 2) then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/spr
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/spr"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 36 or r:peek(4) ~= "IDSP" then
|
|
return 0
|
|
end
|
|
local v = buffer.readi32(r.buf, r.pos + 4)
|
|
return if v == 1 or v == 2 or v == 32 then 1 else 0.3
|
|
end
|
|
end
|
|
|
|
do -- image/sunras
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local MAGIC = "\89\166\106\149"
|
|
S["image/sunras"] = function(r: Bytes.Reader): number
|
|
return Sniff.magic(r, MAGIC)
|
|
end
|
|
end
|
|
|
|
do -- image/tga
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local TYPES = { [1] = true, [2] = true, [3] = true, [9] = true, [10] = true, [11] = true }
|
|
local FOOTER = "TRUEVISION-XFILE.\0"
|
|
local readu8 = buffer.readu8
|
|
type Header = {
|
|
idLen: number,
|
|
cmapType: number,
|
|
imageType: number,
|
|
cmapFirst: number,
|
|
cmapLen: number,
|
|
cmapBits: number,
|
|
width: number,
|
|
height: number,
|
|
depth: number,
|
|
desc: number,
|
|
}
|
|
local function header(r: Bytes.Reader): Header?
|
|
if r.limit - r.base < 18 then
|
|
return nil
|
|
end
|
|
local b, o = r.buf, r.base
|
|
local function u16(q: number): number
|
|
return readu8(b, o + q) + readu8(b, o + q + 1) * 256
|
|
end
|
|
local h: Header = {
|
|
idLen = readu8(b, o),
|
|
cmapType = readu8(b, o + 1),
|
|
imageType = readu8(b, o + 2),
|
|
cmapFirst = u16(3),
|
|
cmapLen = u16(5),
|
|
cmapBits = readu8(b, o + 7),
|
|
width = u16(12),
|
|
height = u16(14),
|
|
depth = readu8(b, o + 16),
|
|
desc = readu8(b, o + 17),
|
|
}
|
|
return h
|
|
end
|
|
local function implausible(h: Header): string?
|
|
local t = h.imageType
|
|
if not TYPES[t] then
|
|
return "image type " .. t
|
|
end
|
|
if h.cmapType > 1 then
|
|
return "colour map type " .. h.cmapType
|
|
end
|
|
local mapped = t == 1 or t == 9
|
|
if mapped and (h.cmapType ~= 1 or (h.depth ~= 8 and h.depth ~= 16)) then
|
|
return "colour-mapped image without a colour map"
|
|
end
|
|
if h.cmapType == 1 and h.cmapLen > 0 and h.cmapBits ~= 15 and h.cmapBits ~= 16 and h.cmapBits ~= 24 and h.cmapBits ~= 32 then
|
|
return "colour map entry size " .. h.cmapBits
|
|
end
|
|
if (t == 2 or t == 10) and h.depth ~= 15 and h.depth ~= 16 and h.depth ~= 24 and h.depth ~= 32 then
|
|
return "pixel depth " .. h.depth
|
|
end
|
|
if (t == 3 or t == 11) and h.depth ~= 8 and h.depth ~= 16 then
|
|
return "grey pixel depth " .. h.depth
|
|
end
|
|
if h.width == 0 or h.height == 0 then
|
|
return "zero size"
|
|
end
|
|
if h.desc >= 64 then
|
|
return "interleaving / reserved descriptor bits"
|
|
end
|
|
return nil
|
|
end
|
|
S["image/tga"] = function(r: Bytes.Reader): number
|
|
local h = header(r)
|
|
if not h or implausible(h) then
|
|
return 0
|
|
end
|
|
local n = r.limit - r.base
|
|
if n >= 44 and buffer.readstring(r.buf, r.limit - 18, 18) == FOOTER then
|
|
return 0.9
|
|
end
|
|
return 0.2
|
|
end
|
|
end
|
|
|
|
do -- image/tiff
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/tiff"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(4)
|
|
if s == "II*\0" or s == "MM\0*" or s == "II+\0" or s == "MM\0+" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- image/tim
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/tim"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 20 or r:peek(4) ~= "\16\0\0\0" then
|
|
return 0
|
|
end
|
|
local flags = r:peekU8(4)
|
|
if flags > 0x0F or (flags % 8) > 3 or r:peek(3, 5) ~= "\0\0\0" then
|
|
return 0
|
|
end
|
|
return 0.8
|
|
end
|
|
end
|
|
|
|
do -- image/vtf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/vtf"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "VTF\0" and r:peekU8(4) == 7 then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- image/webp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["image/webp"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) == "RIFF" and r:peek(4, 8) == "WEBP" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- image/xbm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["image/xbm"] = function(r: Bytes.Reader): number
|
|
local s = Sniff.text(r, 256, true)
|
|
if string.match(s, "^#define%s+[%w_]*_?width%s+%d+") or string.match(s, "^/%*.-%*/%s*#define%s+[%w_]*width%s+%d+") then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- image/xpm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["image/xpm"] = function(r: Bytes.Reader): number
|
|
local s = Sniff.text(r, 64, true)
|
|
if string.sub(s, 1, 9) == "/* XPM */" or string.sub(s, 1, 6) == "! XPM2" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- audio/aac
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Aac = lazy(function()
|
|
return require("@rosetta/codecs/audio/Aac")
|
|
end)
|
|
local readu8 = buffer.readu8
|
|
local function id3Size(b: buffer, p: number, limit: number): number
|
|
if p + 10 > limit or buffer.readstring(b, p, 3) ~= "ID3" then
|
|
return 0
|
|
end
|
|
local s = 0
|
|
for i = 6, 9 do
|
|
s = s * 128 + bit32.band(readu8(b, p + i), 127)
|
|
end
|
|
local footer = if bit32.band(readu8(b, p + 5), 0x10) ~= 0 then 10 else 0
|
|
return 10 + s + footer
|
|
end
|
|
S["audio/aac"] = function(r: Bytes.Reader): number
|
|
local b, p, limit = r.buf, r.pos, r.limit
|
|
p += id3Size(b, p, limit)
|
|
local h = Aac.adts(b, p, limit)
|
|
if not h then
|
|
return 0
|
|
end
|
|
local h2 = Aac.adts(b, p + h.frameSize, limit)
|
|
if h2 and h2.sfIndex == h.sfIndex and h2.channelConfig == h.channelConfig then
|
|
return 0.9
|
|
end
|
|
return if p + h.frameSize >= limit then 0.7 else 0.4
|
|
end
|
|
end
|
|
|
|
do -- audio/ac3
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Ac3 = lazy(function()
|
|
return require("@rosetta/codecs/audio/Ac3")
|
|
end)
|
|
S["audio/ac3"] = function(r: Bytes.Reader): number
|
|
local b, p, limit = r.buf, r.pos, r.limit
|
|
local h = Ac3.header(b, p, limit)
|
|
if not h then
|
|
return 0
|
|
end
|
|
local h2 = Ac3.header(b, p + h.frameSize, limit)
|
|
if h2 and h2.sampleRate == h.sampleRate then
|
|
return 0.9
|
|
end
|
|
return if p + h.frameSize >= limit then 0.6 else 0.3
|
|
end
|
|
end
|
|
|
|
do -- audio/adpcm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/adpcm"] = function(_r: Bytes.Reader): number
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- audio/adx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/adx"] = function(r: Bytes.Reader): number
|
|
if r:peekU8(0) ~= 0x80 or r:peekU8(1) ~= 0 then
|
|
return 0
|
|
end
|
|
local off = r:peekU8(2) * 256 + r:peekU8(3)
|
|
return if r:peek(6, off - 2) == "(c)CRI" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/aiff
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/aiff"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 12 or r:peek(4) ~= "FORM" then
|
|
return 0
|
|
end
|
|
local form = buffer.readstring(r.buf, r.pos + 8, 4)
|
|
return if form == "AIFF" or form == "AIFC" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/ape
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/ape"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(4)
|
|
if head == "MAC " or head == "MACF" then -- MACF: float samples
|
|
return 1
|
|
end
|
|
if string.sub(head, 1, 3) == "ID3" then
|
|
local sz = 0
|
|
for i = 6, 9 do
|
|
local v = r:peekU8(i)
|
|
if v < 0 then
|
|
return 0
|
|
end
|
|
sz = sz * 128 + bit32.band(v, 127)
|
|
end
|
|
local footer = bit32.band(math.max(r:peekU8(5), 0), 0x10) ~= 0
|
|
local id = r:peek(4, 10 + sz + (if footer then 10 else 0))
|
|
return if id == "MAC " or id == "MACF" then 1 else 0
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- audio/au
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/au"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == ".snd" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/brstm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/brstm"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
if m ~= "RSTM" and m ~= "CSTM" and m ~= "FSTM" then
|
|
return 0
|
|
end
|
|
local bom = r:peek(2, 4)
|
|
return if bom == "\254\255" or bom == "\255\254" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/caf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/caf"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(8)
|
|
if #h == 8 and string.sub(h, 1, 4) == "caff" and string.byte(h, 5) == 0 and string.byte(h, 6) == 1 then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- audio/flac
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/flac"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(4)
|
|
if head == "fLaC" then
|
|
return 1
|
|
end
|
|
if head == "OggS" then
|
|
-- the first page's packet: 0x7F "FLAC" (the Ogg FLAC mapping)
|
|
local nseg = r:peekU8(26)
|
|
if nseg > 0 and r:peek(5, 27 + nseg) == "\127FLAC" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
if string.sub(head, 1, 3) == "ID3" then
|
|
-- an ID3v2 tag, then fLaC (mp3 files start with ID3 too)
|
|
local sz = 0
|
|
for i = 6, 9 do
|
|
local v = r:peekU8(i)
|
|
if v < 0 then
|
|
return 0
|
|
end
|
|
sz = sz * 128 + bit32.band(v, 127)
|
|
end
|
|
local footer = bit32.band(math.max(r:peekU8(5), 0), 0x10) ~= 0
|
|
return if r:peek(4, 10 + sz + (if footer then 10 else 0)) == "fLaC" then 1 else 0
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- audio/m4a
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local IsoBmff = lazy(function()
|
|
return require("@rosetta/containers/IsoBmff")
|
|
end)
|
|
local AUDIO_BRANDS: { [string]: number } = { ["M4A "] = 1, ["M4B "] = 1, ["M4P "] = 1, ["M4V "] = 0.3 }
|
|
S["audio/m4a"] = function(r: Bytes.Reader): number
|
|
local brand = IsoBmff.sniff(r)
|
|
if brand == nil then
|
|
return 0
|
|
end
|
|
if AUDIO_BRANDS[brand] then
|
|
return AUDIO_BRANDS[brand]
|
|
end
|
|
if brand == "avif" or brand == "avis" or brand == "heic" or brand == "heix" or brand == "mif1" or brand == "msf1" or brand == "crx " then
|
|
return 0
|
|
end
|
|
return 0.4 -- an MP4 / MOV that may hold audio; a video format claims it more strongly
|
|
end
|
|
end
|
|
|
|
do -- audio/mp3
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Header = lazy(function()
|
|
return require("@rosetta/codecs/audio/Mpa/Header")
|
|
end)
|
|
local Tags = lazy(function()
|
|
return require("@rosetta/audio/mp3/Tags")
|
|
end)
|
|
S["audio/mp3"] = function(r: Bytes.Reader): number
|
|
local b, p, lim = r.buf, r.pos, r.limit
|
|
local id3 = false
|
|
for _ = 1, 4 do -- (ffmpeg writes a tag even when the input had one: tags can follow each other)
|
|
local n = if p + 3 <= lim and buffer.readstring(b, p, 3) == "ID3" then Tags.id3v2Size(b, p, lim) else 0
|
|
if n <= 0 then
|
|
break
|
|
end
|
|
id3 = true
|
|
p += n
|
|
end
|
|
if p + 4 > lim or not Header.valid(b, p) then
|
|
return if id3 then 0.5 else 0
|
|
end
|
|
local fb = Header.frameBytes(b, p, 0)
|
|
if fb > 0 then
|
|
fb += Header.padding(b, p)
|
|
if p + fb + 4 <= lim and Header.compare(b, p, p + fb) then
|
|
return 0.9
|
|
end
|
|
if p + fb == lim then
|
|
return 0.6
|
|
end
|
|
end
|
|
return if id3 then 0.7 else 0.25
|
|
end
|
|
end
|
|
|
|
do -- audio/mpc
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Musepack = lazy(function()
|
|
return require("@rosetta/codecs/audio/Musepack")
|
|
end)
|
|
local function id3v2(b: buffer, p: number, limit: number): number
|
|
if p + 10 > limit or buffer.readstring(b, p, 3) ~= "ID3" then
|
|
return 0
|
|
end
|
|
local n = 10
|
|
for i = 6, 9 do
|
|
n += bit32.band(buffer.readu8(b, p + i), 127) * 128 ^ (9 - i)
|
|
end
|
|
return n + (if bit32.band(buffer.readu8(b, p + 5), 0x10) ~= 0 then 10 else 0)
|
|
end
|
|
S["audio/mpc"] = function(r: Bytes.Reader): number
|
|
local b, p, limit = r.buf, r.pos, r.limit
|
|
p += id3v2(b, p, limit)
|
|
local v = Musepack.magic(b, p, limit)
|
|
if v == 8 then
|
|
return 1
|
|
end
|
|
return if v == 7 then 0.9 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/opus
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/opus"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "OggS" then
|
|
return 0
|
|
end
|
|
local nseg = r:peekU8(26)
|
|
if nseg < 1 then
|
|
return 0
|
|
end
|
|
return if r:peek(8, 27 + nseg) == "OpusHead" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/qoa
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/qoa"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "qoaf" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/tta
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function id3Size(r: Bytes.Reader): number
|
|
if r:peek(3) ~= "ID3" then
|
|
return 0
|
|
end
|
|
local sz = 0
|
|
for i = 6, 9 do
|
|
local v = r:peekU8(i)
|
|
if v < 0 then
|
|
return 0
|
|
end
|
|
sz = sz * 128 + bit32.band(v, 127)
|
|
end
|
|
local footer = bit32.band(math.max(r:peekU8(5), 0), 0x10) ~= 0
|
|
return 10 + sz + (if footer then 10 else 0)
|
|
end
|
|
S["audio/tta"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4, id3Size(r)) == "TTA1" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/vag
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/vag"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
return if m == "VAGp" or m == "VAGi" or m == "KVAG" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/voc
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "Creative Voice File\26"
|
|
S["audio/voc"] = function(r: Bytes.Reader): number
|
|
return if r:peek(20) == MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/vorbis
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/vorbis"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 35 or r:peek(4) ~= "OggS" then
|
|
return 0
|
|
end
|
|
local b, p = r.buf, r.pos
|
|
local nseg = buffer.readu8(b, p + 26)
|
|
local d = p + 27 + nseg
|
|
if d + 7 > r.limit then
|
|
return 0
|
|
end
|
|
return if buffer.readu8(b, d) == 1 and buffer.readstring(b, d + 1, 6) == "vorbis" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/wav
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Riff = lazy(function()
|
|
return require("@rosetta/containers/Riff")
|
|
end)
|
|
S["audio/wav"] = function(r: Bytes.Reader): number
|
|
return if Riff.sniff(r) == "WAVE" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- audio/wavpack
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["audio/wavpack"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "wvpk" then
|
|
return 0
|
|
end
|
|
local v = r:peekU8(9)
|
|
return if v == 4 then 1 else 0.5
|
|
end
|
|
end
|
|
|
|
do -- video/avi
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/avi"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) == "RIFF" and r:peek(4, 8) == "AVI " then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- video/fli
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local FLI, FLC, DTA = 0xAF11, 0xAF12, 0xAF44
|
|
S["video/fli"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(18)
|
|
if #s < 18 then
|
|
return 0
|
|
end
|
|
local magic = string.byte(s, 5) + string.byte(s, 6) * 256
|
|
if magic ~= FLI and magic ~= FLC and magic ~= DTA then
|
|
return 0
|
|
end
|
|
local w = string.byte(s, 9) + string.byte(s, 10) * 256
|
|
local h = string.byte(s, 11) + string.byte(s, 12) * 256
|
|
if w > 4096 or h > 4096 then
|
|
return 0
|
|
end
|
|
return 0.9
|
|
end
|
|
end
|
|
|
|
do -- video/flv
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/flv"] = function(r: Bytes.Reader): number
|
|
if r:remaining() >= 9 and r:peek(3) == "FLV" then
|
|
return if r:peekU8(3) == 1 then 1 else 0.6
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- video/h264
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/h264"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(5)
|
|
if not s or #s < 4 then
|
|
return 0
|
|
end
|
|
local hdr
|
|
if string.byte(s, 1) == 0 and string.byte(s, 2) == 0 and string.byte(s, 3) == 1 then
|
|
hdr = string.byte(s, 4)
|
|
elseif #s >= 5 and string.byte(s, 1) == 0 and string.byte(s, 2) == 0 and string.byte(s, 3) == 0 and string.byte(s, 4) == 1 then
|
|
hdr = string.byte(s, 5)
|
|
else
|
|
return 0
|
|
end
|
|
if hdr >= 128 then
|
|
return 0
|
|
end
|
|
local t = hdr % 32
|
|
if t == 7 then
|
|
return 0.8
|
|
elseif t == 9 or t == 6 or t == 8 then
|
|
return 0.5
|
|
elseif t == 5 then
|
|
return 0.4
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- video/mkv
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/mkv"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "\26\69\223\163" then
|
|
return 0
|
|
end
|
|
local head = r:peek(math.min(64, r:remaining()))
|
|
if string.find(head, "webm", 1, true) or string.find(head, "matroska", 1, true) then
|
|
return 1
|
|
end
|
|
return 0.5
|
|
end
|
|
end
|
|
|
|
do -- video/mp4
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local IsoBmff = lazy(function()
|
|
return require("@rosetta/containers/IsoBmff")
|
|
end)
|
|
local IMAGE_BRANDS = { avif = true, avis = true, heic = true, heix = true, mif1 = true, msf1 = true, ["crx "] = true }
|
|
local AUDIO_BRANDS = { ["M4A "] = true, ["M4B "] = true, ["M4P "] = true }
|
|
S["video/mp4"] = function(r: Bytes.Reader): number
|
|
local brand = IsoBmff.sniff(r)
|
|
if brand == nil or IMAGE_BRANDS[brand] then
|
|
return 0
|
|
end
|
|
if AUDIO_BRANDS[brand] then
|
|
return 0.2
|
|
end
|
|
if brand == "M4V " then
|
|
return 0.9
|
|
end
|
|
return if brand == "" then 0.6 else 0.8
|
|
end
|
|
end
|
|
|
|
do -- video/mpegps
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/mpegps"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) == "\0\0\1\186" then
|
|
return 0.9
|
|
end
|
|
if r:peek(4) == "\0\0\1\179" then
|
|
return 0.5 -- an MPEG-1 / 2 video elementary stream (sequence header)
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- video/mpegts
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local SIZES = { 188, 192 }
|
|
local function packetSize(r: Bytes.Reader): (number, number)
|
|
local n = r:remaining()
|
|
local head = r:peek(2048 + 2 * 192)
|
|
local lim = math.min(n, 2048)
|
|
local start = 0
|
|
while start < lim do
|
|
local f = string.find(head, "G", start + 1, true) -- 0x47
|
|
if not f or f > lim then
|
|
break
|
|
end
|
|
start = f - 1
|
|
for _, size in SIZES do
|
|
local hits, need = 0, math.min(3, (n - start - 1) // size + 1)
|
|
for k = 0, 2 do
|
|
local at = start + k * size
|
|
if at < n and string.byte(head, at + 1) == 0x47 then
|
|
hits += 1
|
|
end
|
|
end
|
|
if hits >= need and (need >= 2 or start == 0) then
|
|
if size == 192 and start >= 4 then
|
|
return 192, start - 4
|
|
elseif size == 188 then
|
|
return 188, start
|
|
end
|
|
end
|
|
end
|
|
start += 1
|
|
end
|
|
return 0, 0
|
|
end
|
|
S["video/mpegts"] = function(r: Bytes.Reader): number
|
|
local s, off = packetSize(r)
|
|
if s == 0 then
|
|
return 0
|
|
end
|
|
if off > 0 then
|
|
return 0.6
|
|
end
|
|
return if r:remaining() >= 3 * s then 0.9 else 0.5
|
|
end
|
|
end
|
|
|
|
do -- video/ogv
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/ogv"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "OggS" then
|
|
return 0
|
|
end
|
|
local b, p, lim = r.buf, r.pos, r.limit
|
|
for _ = 1, 8 do
|
|
if p + 27 > lim or buffer.readstring(b, p, 4) ~= "OggS" or buffer.readu8(b, p + 5) % 4 < 2 then
|
|
return 0
|
|
end
|
|
local nseg = buffer.readu8(b, p + 26)
|
|
local d = p + 27 + nseg
|
|
if d + 7 <= lim and buffer.readu8(b, d) == 0x80 and buffer.readstring(b, d + 1, 6) == "theora" then
|
|
return 1
|
|
end
|
|
if d > lim then
|
|
return 0
|
|
end
|
|
local size = 0
|
|
for i = 0, nseg - 1 do
|
|
size += buffer.readu8(b, p + 27 + i)
|
|
end
|
|
p = d + size
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- video/smk
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["video/smk"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(4)
|
|
if s ~= "SMK2" and s ~= "SMK4" then
|
|
return 0
|
|
end
|
|
return 1
|
|
end
|
|
end
|
|
|
|
do -- video/y4m
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "YUV4MPEG2 "
|
|
S["video/y4m"] = function(r: Bytes.Reader): number
|
|
return if r:peek(10) == MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/3ds
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/3ds"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(8)
|
|
if #h < 8 or string.byte(h, 1) ~= 0x4D or string.byte(h, 2) ~= 0x4D then
|
|
return 0
|
|
end
|
|
local b1, b2, b3, b4, c1, c2 = string.byte(h, 3, 8)
|
|
local size = b1 + b2 * 256 + b3 * 65536 + b4 * 16777216
|
|
local child = c1 + c2 * 256
|
|
if size < 16 or not (child == 0x0002 or child == 0x3D3D or child == 0xB000) then
|
|
return 0
|
|
end
|
|
return if size == r.limit - r.pos then 0.95 else 0.7
|
|
end
|
|
end
|
|
|
|
do -- model/3mf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/3mf"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "PK\3\4" then
|
|
return 0
|
|
end
|
|
local n = r:size()
|
|
local tail = buffer.readstring(r.buf, r.base + math.max(0, n - 65536), math.min(n, 65536))
|
|
if string.find(tail, "3D/[^/]*%.model") or string.find(tail, "3dmodel.model", 1, true) then
|
|
return 0.95
|
|
end
|
|
local head = r:peek(256)
|
|
if string.find(head, "3D/", 1, true) then
|
|
return 0.7
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/ase
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Text = lazy(function()
|
|
return require("@rosetta/codecs/text/Text")
|
|
end)
|
|
local MAGIC = "*3DSMAX_ASCIIEXPORT"
|
|
local function utf16(r: Bytes.Reader): (boolean?, boolean)
|
|
local h = r:peek(2)
|
|
if h == "\255\254" then
|
|
return true, false
|
|
elseif h == "\254\255" then
|
|
return true, true
|
|
elseif #h == 2 and string.byte(h, 2) == 0 and string.byte(h, 1) == 42 then
|
|
return true, false
|
|
elseif #h == 2 and string.byte(h, 1) == 0 and string.byte(h, 2) == 42 then
|
|
return true, true
|
|
end
|
|
return nil, false
|
|
end
|
|
local function text(r: Bytes.Reader, n: number?): string
|
|
local wide, be = utf16(r)
|
|
local len = r.limit - r.pos
|
|
if n then
|
|
len = math.min(len, n)
|
|
end
|
|
if wide then
|
|
local skip = if r:peek(2) == "\255\254" or r:peek(2) == "\254\255" then 2 else 0
|
|
return Text.utf16(r.buf, r.pos + skip, len - skip, be)
|
|
end
|
|
return Text.toUtf8(buffer.readstring(r.buf, r.pos, len))
|
|
end
|
|
S["model/ase"] = function(r: Bytes.Reader): number
|
|
local h = string.match(text(r, 256), "^%s*(%*[%w_]+)")
|
|
if h == MAGIC then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/b3d
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/b3d"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) == "BB3D" then
|
|
return if r:peek(4, 8) ~= "" then 1 else 0.8
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/blend
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Deflate = lazy(function()
|
|
return require("@rosetta/codecs/compress/Deflate")
|
|
end)
|
|
local Zstd = lazy(function()
|
|
return require("@rosetta/codecs/compress/Zstd")
|
|
end)
|
|
S["model/blend"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(7)
|
|
if head == "BLENDER" then
|
|
return 1
|
|
end
|
|
local n = r.limit - r.base
|
|
if string.sub(head, 1, 3) == "\31\139\8" then
|
|
local inner = Deflate.peek(r.buf, r.base, n, 7)
|
|
return if inner and buffer.tostring(inner) == "BLENDER" then 1 else 0
|
|
end
|
|
if string.sub(head, 1, 4) == Zstd.MAGIC then
|
|
if n > 4 * 2 ^ 20 then
|
|
return 0.5
|
|
end
|
|
local out = Zstd.decompress(r.buf, r.base, n, { verify = false })
|
|
return if out and buffer.len(out) >= 7 and buffer.readstring(out, 0, 7) == "BLENDER" then 1 else 0
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/bsp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/bsp"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 64 then
|
|
return 0
|
|
end
|
|
local m = r:peek(4)
|
|
local v = buffer.readi32(r.buf, r.pos + (if m == "IBSP" then 4 else 0))
|
|
if m == "BSP2" or m == "2PSB" then
|
|
v = 29
|
|
end
|
|
if m == "IBSP" then
|
|
return if v == 38 or v == 46 or v == 47 then 1 else 0.3
|
|
end
|
|
if v == 29 or v == 30 then
|
|
local size = r:remaining()
|
|
for i = 0, 14 do
|
|
local o, n = buffer.readi32(r.buf, r.pos + 4 + i * 8), buffer.readi32(r.buf, r.pos + 8 + i * 8)
|
|
if o < 0 or n < 0 or o + n > size then
|
|
return 0
|
|
end
|
|
end
|
|
return 0.6
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/buildmap
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local SECTOR_SIZE, WALL_SIZE, SPRITE_SIZE = 40, 32, 44
|
|
local HEADER = 22 -- version, posx, posy, posz, ang, cursectnum, then u16 numsectors at 20
|
|
local readu16 = buffer.readu16
|
|
local function layout(b: buffer, o: number, limit: number): (number?, number?, number?, number?)
|
|
if limit - o < HEADER then
|
|
return nil, nil, nil, nil
|
|
end
|
|
local ns = readu16(b, o + 20)
|
|
local p = o + HEADER + ns * SECTOR_SIZE
|
|
if p + 2 > limit then
|
|
return nil, nil, nil, nil
|
|
end
|
|
local nw = readu16(b, p)
|
|
p += 2 + nw * WALL_SIZE
|
|
if p + 2 > limit then
|
|
return nil, nil, nil, nil
|
|
end
|
|
local nsp = readu16(b, p)
|
|
p += 2 + nsp * SPRITE_SIZE
|
|
return ns, nw, nsp, p
|
|
end
|
|
S["model/buildmap"] = function(r: Bytes.Reader): number
|
|
local b, o, limit = r.buf, r.base, r.limit
|
|
if limit - o < HEADER then
|
|
return 0
|
|
end
|
|
local v = buffer.readu32(b, o)
|
|
if v ~= 7 and v ~= 8 and v ~= 9 then
|
|
return 0
|
|
end
|
|
local ns, _, _, stop = layout(b, o, limit)
|
|
if not ns or not stop or stop > limit then
|
|
return 0
|
|
end
|
|
return if stop == limit then 0.9 else 0.5
|
|
end
|
|
end
|
|
|
|
do -- model/d3map
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/d3map"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 512, true)
|
|
for _ = 1, 16 do
|
|
local rest = string.match(head, "^//[^\n]*\n%s*(.*)$")
|
|
if not rest then
|
|
break
|
|
end
|
|
head = rest
|
|
end
|
|
return if string.match(head, "^Version%s+[123]%s") then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/dae
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/dae"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(2048)
|
|
if string.sub(head, 1, 4) == "PK\3\4" then
|
|
local nlen = string.byte(head, 27) or 0
|
|
local name = string.lower(string.sub(head, 31, 30 + nlen))
|
|
if string.find(name, "%.dae$") or name == "manifest.xml" then
|
|
return 0.95
|
|
end
|
|
return 0
|
|
end
|
|
-- UTF-16 files: drop the zero bytes
|
|
if string.find(string.sub(head, 1, 4), "\0", 1, true) then
|
|
head = string.gsub(head, "%z", "")
|
|
end
|
|
if string.find(head, "<COLLADA", 1, true) then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/gltf
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/gltf"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) == "glTF" then
|
|
return 1
|
|
end
|
|
local head = r:peek(1024)
|
|
local s = Sniff.dropBom(head)
|
|
if string.find(s, "^%s*{") and string.find(s, "\"asset\"", 1, true) then
|
|
return 0.9
|
|
end
|
|
if string.find(s, "^%s*{") and (string.find(s, "\"accessors\"", 1, true) or string.find(s, "\"bufferViews\"", 1, true)) then
|
|
return 0.6
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/godot
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/godot"] = function(r: Bytes.Reader): number
|
|
local h = Sniff.text(r, 256)
|
|
if string.match(h, "^%s*%[gd_scene[%s%]]") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/iqm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "INTERQUAKEMODEL\0"
|
|
S["model/iqm"] = function(r: Bytes.Reader): number
|
|
return if r:peek(16) == MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/ldraw
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/ldraw"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 2048, true)
|
|
if string.find(head, "^0%s+FILE%s") then
|
|
return 0.9
|
|
end
|
|
if string.find(head, "\n%s*1%s+%S+" .. string.rep("%s+[-+%deE.]+", 12) .. "%s+[^\n]+%.[dDlL][aAdD][tTrR]%s*\r?\n") then
|
|
return 0.8
|
|
end
|
|
if string.find(head, "^0[ \t]+%a") and string.find(head, "\n%s*[1-5]%s+%S+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]") then
|
|
return 0.4
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/lwo
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local VERSIONS = { LWO2 = true, LWOB = true, LXOB = true }
|
|
S["model/lwo"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "FORM" then
|
|
return 0
|
|
end
|
|
local v = r:peek(4, 8)
|
|
return if VERSIONS[v] then 1 elseif v == "LWO3" then 0.8 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/map
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/map"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(math.min(r:remaining(), 4096))
|
|
if #head < 2 or string.find(head, "[%z\1-\8\14-\31]") then
|
|
return 0
|
|
end
|
|
if string.sub(head, 1, 3) == "\239\187\191" then
|
|
head = string.sub(head, 4)
|
|
end
|
|
local s = head
|
|
local game = false
|
|
while true do
|
|
local c = string.match(s, "^%s*//[^\n]*\n?()")
|
|
if not c then
|
|
break
|
|
end
|
|
if string.match(s, "^%s*//%s*Game:") then
|
|
game = true
|
|
end
|
|
s = string.sub(s, c :: number)
|
|
end
|
|
local body = string.match(s, '^%s*{%s*"[^"\n]*"%s*"') or string.match(s, "^%s*{%s*{%s*[%(bp]")
|
|
if not body then
|
|
return if game and string.match(s, "^%s*{") then 0.6 else 0
|
|
end
|
|
return if game or string.find(head, '"classname"', 1, true) then 0.9 else 0.6
|
|
end
|
|
end
|
|
|
|
do -- model/mca
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local COMPRESSION = { [1] = "gzip", [2] = "zlib", [3] = "none", [4] = "lz4", [127] = "custom" }
|
|
S["model/mca"] = function(r: Bytes.Reader): number
|
|
local n = r.limit - r.base
|
|
if n < 8192 then
|
|
return 0
|
|
end
|
|
local sectors = (n + 4095) // 4096
|
|
local used, b = 0, r.buf
|
|
for i = 0, 1023 do
|
|
local e = bit32.byteswap(buffer.readu32(b, r.base + i * 4))
|
|
if e ~= 0 then
|
|
local off, cnt = e // 256, e % 256
|
|
if off < 2 or cnt == 0 or off + cnt > sectors then
|
|
return 0
|
|
end
|
|
used += 1
|
|
if used == 1 then
|
|
local p = r.base + off * 4096
|
|
if p + 5 > r.limit then
|
|
return 0
|
|
end
|
|
local c = buffer.readu8(b, p + 4) % 128
|
|
if COMPRESSION[c] == nil then
|
|
return 0
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return if used > 0 then 0.8 else 0.2
|
|
end
|
|
end
|
|
|
|
do -- model/md2
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/md2"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 68 then
|
|
return 0
|
|
end
|
|
return if Sniff.magic(r, "IDP2") == 1 and r:peekU8(4) == 8 then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/md3
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/md3"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 108 then
|
|
return 0
|
|
end
|
|
return if Sniff.magic(r, "IDP3") == 1 and r:peekU8(4) == 15 then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/md5
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/md5"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 512, true)
|
|
head = string.gsub(head, "^//[^\n]*\n%s*", "")
|
|
return if string.match(head, "^MD5Version%s+%d+") then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/mdl
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/mdl"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
local v = r:peekU8(4)
|
|
if (m == "IDPO" and v == 6) or (m == "IDST" and v == 10) then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/ms3d
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Read = lazy(function()
|
|
return require("@rosetta/model/ms3d/Read")
|
|
end)
|
|
S["model/ms3d"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(14)
|
|
if string.sub(h, 1, 10) ~= Read.MAGIC then
|
|
return 0
|
|
end
|
|
local v = string.byte(h, 11)
|
|
return if v == 3 or v == 4 then 1 else 0.6
|
|
end
|
|
end
|
|
|
|
do -- model/obj
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/obj"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(512)
|
|
if string.find(head, "\0", 1, true) then
|
|
return 0
|
|
end
|
|
if string.find(head, "^%s*v%s+[%-%d%.]") or string.find(head, "\n%s*v%s+[%-%d%.]") then
|
|
return 0.6
|
|
end
|
|
if string.find(head, "^#") and (string.find(head, "\nmtllib ") or string.find(head, "\no ") or string.find(head, "\ng ")) then
|
|
return 0.4
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/off
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/off"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(512)
|
|
for _ = 1, 16 do
|
|
local rest = string.match(head, "^%s*#[^\n]*\n(.*)$")
|
|
if not rest then
|
|
break
|
|
end
|
|
head = rest
|
|
end
|
|
local kw = string.match(head, "^%s*(%w+)")
|
|
if kw and string.match(kw, "^S?T?C?N?4?n?OFF$") then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/ply
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/ply"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(4)
|
|
return if h == "ply\n" or h == "ply\r" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/pmx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/pmx"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(8)
|
|
if string.sub(head, 1, 4) == "PMX " then
|
|
return 1
|
|
end
|
|
if string.sub(head, 1, 7) == "Pmd\0\0\128\63" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/proc
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/proc"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 64, true)
|
|
if string.match(head, "^mapProcFile%d%d%d") then
|
|
return 1
|
|
end
|
|
if string.match(head, '^"?PROC"?%s+"?%d') then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/qb
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/qb"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 24 or r:peek(4) ~= "\1\1\0\0" then
|
|
return 0
|
|
end
|
|
local b, p = r.buf, r.pos
|
|
local fmt, z, comp, mask = buffer.readu32(b, p + 4), buffer.readu32(b, p + 8), buffer.readu32(b, p + 12), buffer.readu32(b, p + 16)
|
|
if fmt > 1 or z > 1 or comp > 1 or mask > 1 then
|
|
return 0
|
|
end
|
|
return 0.9
|
|
end
|
|
end
|
|
|
|
do -- model/rbxm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "<roblox!\137\255\13\10\26\10"
|
|
S["model/rbxm"] = function(r: Bytes.Reader): number
|
|
return if r:peek(14) == MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/rbxmx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local find, format, match, gmatch = string.find, string.format, string.match, string.gmatch
|
|
S["model/rbxmx"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(256)
|
|
head = string.gsub(head, "^\239\187\191", "")
|
|
head = string.gsub(head, "^%s*<%?xml[^>]*%?>", "")
|
|
if match(head, "^%s*<roblox[%s>]") then
|
|
return if find(head, "version=", 1, true) then 1 else 0.8
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/robloxmesh
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/robloxmesh"] = function(r: Bytes.Reader): number
|
|
return if string.match(r:peek(13), "^version [1-7]%.%d%d[\r\n]") then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/schematic
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Nbt = lazy(function()
|
|
return require("@rosetta/codecs/text/Nbt")
|
|
end)
|
|
local LITEMATIC_HINTS = { "Regions", "EnclosingSize", "RegionCount", "TotalVolume", "MinecraftDataVersion" }
|
|
local SPONGE_HINTS = { "BlockData", "PaletteMax" } -- v1 / v2 fields (some writers leave the root unnamed)
|
|
local function hinted(head: string, hints: { string }): boolean
|
|
for _, h in hints do
|
|
if string.find(head, string.char(0, #h) .. h, 1, true) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
S["model/schematic"] = function(r: Bytes.Reader): number
|
|
local p = Nbt.peek(r.buf, r.base, r.limit - r.base)
|
|
if not p or p.rootType ~= "compound" or p.endian ~= "big" then
|
|
return 0
|
|
end
|
|
if p.name == "Schematic" or (p.firstName == "Schematic" and p.firstType == "compound") then
|
|
return 1
|
|
end
|
|
local head = buffer.tostring(p.head)
|
|
return if hinted(head, LITEMATIC_HINTS) or hinted(head, SPONGE_HINTS) then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/smd
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/smd"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 256, true)
|
|
head = string.gsub(head, "^//[^\n]*\n%s*", "")
|
|
if string.match(head, "^version%s+1%s") and string.find(head, "nodes", 1, true) then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/sourcemdl
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/sourcemdl"] = function(r: Bytes.Reader): number
|
|
if r.limit - r.base < 8 or r:peek(4) ~= "IDST" then
|
|
return 0
|
|
end
|
|
local v = buffer.readi32(r.buf, r.base + 4)
|
|
return if v >= 44 and v <= 49 then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/stl
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function lower5(s: string): boolean
|
|
return string.lower(s) == "solid"
|
|
end
|
|
S["model/stl"] = function(r: Bytes.Reader): number
|
|
local size = r:remaining()
|
|
if size >= 84 then
|
|
local c = r:peek(4, 80)
|
|
local n = string.byte(c, 1) + string.byte(c, 2) * 256 + string.byte(c, 3) * 65536 + string.byte(c, 4) * 16777216
|
|
if 84 + 50 * n == size then
|
|
return 0.9
|
|
end
|
|
end
|
|
local head = r:peek(512)
|
|
local s = string.match(head, "^%s*(%a+)")
|
|
if s and lower5(s) then
|
|
local l = string.lower(head)
|
|
return if string.find(l, "facet", 1, true) or string.find(l, "endsolid", 1, true) then 0.9 else 0.5
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/t3d
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/t3d"] = function(r: Bytes.Reader): number
|
|
local h = Sniff.text(r, 512)
|
|
local first = string.match(h, "^%s*(Begin%s+%a+)")
|
|
if first == nil then
|
|
return 0
|
|
end
|
|
local kind = string.lower(string.match(first, "%a+$") or "")
|
|
if kind == "map" or kind == "actor" or kind == "level" or kind == "brush" or kind == "polylist" then
|
|
return 0.9
|
|
end
|
|
return 0.3
|
|
end
|
|
end
|
|
|
|
do -- model/udmf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/udmf"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 1024, true)
|
|
for _ = 1, 32 do
|
|
local rest = string.match(head, "^//[^\n]*\n%s*(.*)$") or string.match(head, "^/%*.-%*/%s*(.*)$")
|
|
if not rest then
|
|
break
|
|
end
|
|
head = rest
|
|
end
|
|
if string.match(head, "^[Nn][Aa][Mm][Ee][Ss][Pp][Aa][Cc][Ee]%s*=%s*\"") then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/usda
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function isUsdz(r: Bytes.Reader): boolean
|
|
if r:peek(4) ~= "PK\3\4" then
|
|
return false
|
|
end
|
|
local lo, hi = r:peekU8(26), r:peekU8(27)
|
|
if lo < 0 or hi < 0 then
|
|
return false
|
|
end
|
|
local nameLen = lo + hi * 256
|
|
local name = string.lower(r:peek(30 + nameLen))
|
|
name = string.sub(name, 31)
|
|
return string.find(name, "%.usd[ac]?$") ~= nil
|
|
end
|
|
S["model/usda"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(8)
|
|
if string.sub(head, 1, 5) == "#usda" or string.sub(head, 1, 8) == "\239\187\191#usda" then
|
|
return 1
|
|
end
|
|
if head == "PXR-USDC" then
|
|
return 0.9 -- USDC magic, so the error says it is unsupported
|
|
end
|
|
if isUsdz(r) then
|
|
return 0.95
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/vbsp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local HEADER = 8 + 64 * 16 + 4
|
|
S["model/vbsp"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < HEADER or r:peek(4) ~= "VBSP" then
|
|
return 0
|
|
end
|
|
local v = buffer.readi32(r.buf, r.pos + 4)
|
|
return if v >= 19 and v <= 21 then 1 else 0.5
|
|
end
|
|
end
|
|
|
|
do -- model/vmf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["model/vmf"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 512, true)
|
|
if string.find(head, "%z") then
|
|
return 0
|
|
end
|
|
if string.match(head, "^versioninfo%s*{") or string.match(head, '^"versioninfo"%s*{') then
|
|
return 0.9
|
|
end
|
|
if string.match(head, "^world%s*{") or string.match(head, "^visgroups%s*{") then
|
|
return 0.6
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- model/vox
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/vox"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "VOX " and r:peek(4, 8) == "MAIN" then 1 elseif r:peek(4) == "VOX " then 0.8 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/vrml
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/vrml"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(16)
|
|
if string.sub(head, 1, 3) == "\239\187\191" then
|
|
head = string.sub(head, 4)
|
|
end
|
|
return if string.sub(head, 1, 10) == "#VRML V2.0" then 1 elseif string.sub(head, 1, 6) == "#VRML " then 0.6 else 0
|
|
end
|
|
end
|
|
|
|
do -- model/x
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local ENCODINGS = { ["txt "] = "text", ["bin "] = "binary", tzip = "text", bzip = "binary" }
|
|
S["model/x"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "xof " then
|
|
return 0
|
|
end
|
|
return if ENCODINGS[r:peek(4, 8)] then 1 else 0.5
|
|
end
|
|
end
|
|
|
|
do -- model/x3d
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["model/x3d"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(1024)
|
|
if string.sub(head, 1, 3) == "\239\187\191" then
|
|
head = string.sub(head, 4)
|
|
end
|
|
if string.sub(head, 1, 5) == "#X3D " then
|
|
return 1
|
|
end
|
|
if string.sub(head, 1, 4) == "\224\0\0\1" and string.find(head, "x3d", 1, true) then
|
|
return 0.9
|
|
end
|
|
if string.find(head, "<X3D[%s>]") or string.find(head, "<!DOCTYPE X3D", 1, true) then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- cad/step
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["cad/step"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 256)
|
|
head = string.gsub(head, "^%s*/%*.-%*/", "")
|
|
if string.find(head, "^%s*ISO%-10303%-21%s*;") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/bdf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/bdf"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(32)
|
|
return if string.match(head, "^STARTFONT%s+2%.%d") then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- font/bmfont
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/bmfont"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(64)
|
|
if string.sub(head, 1, 4) == "BMF\3" then
|
|
return 1
|
|
end
|
|
if string.match(head, "^info%s+face=") or string.match(head, "^info%s+") and string.find(head, "size=", 1, true) then
|
|
return 0.9
|
|
end
|
|
if string.find(head, "<font>", 1, true) or (string.find(head, "<?xml", 1, true) and string.find(r:peek(512), "<font>", 1, true)) then
|
|
return if string.find(r:peek(512), "<info", 1, true) then 0.9 else 0.3
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/eot
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = 0x504C
|
|
S["font/eot"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(36)
|
|
if #h < 36 then
|
|
return 0
|
|
end
|
|
local ver = string.sub(h, 9, 12)
|
|
local magic = string.byte(h, 35) + string.byte(h, 36) * 256
|
|
if magic == MAGIC and (ver == "\0\0\1\0" or ver == "\1\0\2\0" or ver == "\2\0\2\0") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/fon
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function isFnt(h: string): boolean
|
|
if #h < 4 then
|
|
return false
|
|
end
|
|
local ver = string.byte(h, 1) + string.byte(h, 2) * 256
|
|
return ver == 0x200 or ver == 0x300
|
|
end
|
|
S["font/fon"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(64)
|
|
if string.sub(h, 1, 2) == "MZ" and #h >= 64 then
|
|
local lfanew = string.byte(h, 61) + string.byte(h, 62) * 256 + string.byte(h, 63) * 65536
|
|
local sig = r:peek(2, lfanew)
|
|
return if sig == "NE" then 0.6 elseif sig == "PE" then 0.2 else 0
|
|
end
|
|
if isFnt(h) and #h >= 6 then
|
|
local size = string.byte(h, 3) + string.byte(h, 4) * 256 + string.byte(h, 5) * 65536 + string.byte(h, 6) * 16777216
|
|
return if size == r:size() then 0.9 elseif size <= r:size() and size > 118 then 0.3 else 0
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/hershey
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function header(line: string): (number?, number?)
|
|
if #line < 10 then
|
|
return nil, nil
|
|
end
|
|
local id = tonumber((string.gsub(string.sub(line, 1, 5), "^%s+", "")))
|
|
local n = tonumber((string.gsub(string.sub(line, 6, 8), "^%s+", "")))
|
|
if not id or not n or n < 1 or string.find(string.sub(line, 1, 8), "[^%d ]") then
|
|
return nil, nil
|
|
end
|
|
return id, n
|
|
end
|
|
S["font/hershey"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(256)
|
|
local line = string.match(head, "^([^\r\n]*)")
|
|
local id, n = header(line or "")
|
|
if not id or not n then
|
|
return 0
|
|
end
|
|
local body = string.sub(line :: string, 9)
|
|
if string.find(body, "[^ -~]") then
|
|
return 0
|
|
end
|
|
return if #body == math.min(2 * n, 64) or #body == 2 * n then 0.7 else 0.3
|
|
end
|
|
end
|
|
|
|
do -- font/pcf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/pcf"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "\1fcp" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- font/psf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/psf"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(4)
|
|
if h == "\114\181\74\134" then
|
|
return 1
|
|
end
|
|
if string.sub(h, 1, 2) == "\54\4" and #h >= 4 and string.byte(h, 3) <= 7 and string.byte(h, 4) > 0 then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/ttf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/ttf"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
if m == "\0\1\0\0" or m == "OTTO" or m == "ttcf" or m == "true" then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/type1
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/type1"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(64)
|
|
if string.sub(h, 1, 2) == "\128\1" then
|
|
h = string.sub(h, 7)
|
|
end
|
|
if string.sub(h, 1, 14) == "%!PS-AdobeFont" or string.sub(h, 1, 10) == "%!FontType" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- font/woff
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/woff"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "wOFF" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- font/woff2
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["font/woff2"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "wOF2" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/7z
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "7z\188\175\39\28"
|
|
S["archive/7z"] = function(r: Bytes.Reader): number
|
|
return if r:peek(6) == MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/adf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local BS = 512
|
|
S["archive/adf"] = function(r: Bytes.Reader): number
|
|
local n = r:size()
|
|
if n < 4 * BS or n % BS ~= 0 then
|
|
return 0
|
|
end
|
|
local h = r:peek(4)
|
|
if string.sub(h, 1, 3) ~= "DOS" or string.byte(h, 4) > 5 then
|
|
return 0
|
|
end
|
|
return if n == 901120 or n == 1802240 then 0.95 else 0.6
|
|
end
|
|
end
|
|
|
|
do -- archive/applesingle
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/applesingle"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(8)
|
|
if #h < 8 then
|
|
return 0
|
|
end
|
|
local magic = string.sub(h, 1, 4)
|
|
local ver = string.sub(h, 5, 8)
|
|
if (magic == "\0\5\22\0" or magic == "\0\5\22\7") and (ver == "\0\1\0\0" or ver == "\0\2\0\0") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/ar
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "!<arch>\n"
|
|
S["archive/ar"] = function(r: Bytes.Reader): number
|
|
if r:peek(8) == MAGIC then
|
|
return if r:peek(13, 68) == "debian-binary" then 1 else 0.9
|
|
end
|
|
return if r:peek(8) == "!<thin>\n" then 0.5 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/arj
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/arj"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(4)
|
|
if #h == 4 and string.byte(h, 1) == 0x60 and string.byte(h, 2) == 0xEA then
|
|
local size = string.byte(h, 3) + string.byte(h, 4) * 256
|
|
return if size > 0 and size <= 2600 then 0.9 else 0
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/binhex
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local BANNER = "(This file must be converted with BinHex"
|
|
S["archive/binhex"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(2048)
|
|
local at = string.find(head, BANNER, 1, true)
|
|
if not at then
|
|
return 0
|
|
end
|
|
return if at == 1 then 1 else 0.8
|
|
end
|
|
end
|
|
|
|
do -- archive/br
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Brotli = lazy(function()
|
|
return require("@rosetta/codecs/compress/Brotli")
|
|
end)
|
|
local SNIFF_WHOLE = 262144 -- streams up to this size are decoded whole by sniff
|
|
local SNIFF_PREFIX = 65536
|
|
S["archive/br"] = function(r: Bytes.Reader): number
|
|
local n = r:remaining()
|
|
if n == 0 then
|
|
return 0
|
|
end
|
|
if n <= SNIFF_WHOLE then
|
|
local out, consumed = Brotli.decompress(r.buf, r.pos, n, { maxBytes = 2 ^ 26 })
|
|
if out and consumed == r.limit then
|
|
return if buffer.len(out) > 0 then 0.3 else 0.05
|
|
end
|
|
return 0
|
|
end
|
|
local _, err = Brotli.decompress(r.buf, r.pos, SNIFF_PREFIX, { maxBytes = 2 ^ 22 })
|
|
if err and (err.code == Err.TRUNCATED or err.code == Err.LIMIT) then
|
|
return 0.05
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/bz2
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/bz2"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(10)
|
|
if #h < 10 or string.sub(h, 1, 3) ~= "BZh" then
|
|
return 0
|
|
end
|
|
local lv = string.byte(h, 4)
|
|
if lv < 0x31 or lv > 0x39 then
|
|
return 0
|
|
end
|
|
local magic = string.sub(h, 5, 10)
|
|
if magic == "\x31\x41\x59\x26\x53\x59" or magic == "\x17\x72\x45\x38\x50\x90" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/cab
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/cab"] = function(r: Bytes.Reader): number
|
|
if r:peek(8) == "MSCF\0\0\0\0" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/cpio
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/cpio"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(6)
|
|
if m == "070701" or m == "070702" or m == "070707" then
|
|
return 1
|
|
end
|
|
local b2 = r:peek(2)
|
|
if (b2 == "\199\113" or b2 == "\113\199") and r:remaining() >= 26 then
|
|
return 0.5
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/d64
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
type Geometry = { kind: string, tracks: number, offsets: { number }, spt: { number }, dirTrack: number, total: number }
|
|
local function geometry(kind: string, tracks: number): Geometry
|
|
local offsets, spt = {}, {}
|
|
local o = 0
|
|
for t = 1, tracks do
|
|
local n
|
|
if kind == "d81" then
|
|
n = 40
|
|
else
|
|
local s = if kind == "d71" and t > 35 then t - 35 else t
|
|
n = if s <= 17 then 21 elseif s <= 24 then 19 elseif s <= 30 then 18 else 17
|
|
end
|
|
offsets[t], spt[t] = o, n
|
|
o += n * 256
|
|
end
|
|
return { kind = kind, tracks = tracks, offsets = offsets, spt = spt, dirTrack = if kind == "d81" then 40 else 18, total = o }
|
|
end
|
|
local function detect(len: number): Geometry?
|
|
for _, g in { { "d64", 35 }, { "d64", 40 }, { "d64", 42 }, { "d71", 70 }, { "d81", 80 } } :: { { any } } do
|
|
local geo = geometry(g[1] :: string, g[2] :: number)
|
|
local sectors = geo.total // 256
|
|
if len == geo.total or (g[1] ~= "d81" and len == geo.total + sectors) then
|
|
return geo
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
S["archive/d64"] = function(r: Bytes.Reader): number
|
|
local geo = detect(r:size())
|
|
if not geo then
|
|
return 0
|
|
end
|
|
-- the BAM / header sector: dir track link and DOS version byte
|
|
local h = r:sub(geo.offsets[geo.dirTrack], 256)
|
|
local t, s, v = h:u8(), h:u8(), h:u8()
|
|
if geo.kind == "d81" then
|
|
return if t == 40 and s == 3 and v == 0x44 then 0.9 else 0.3
|
|
end
|
|
return if t == 18 and s == 1 and (v == 0x41 or v == 0) then 0.9 else 0.3
|
|
end
|
|
end
|
|
|
|
do -- archive/fat
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Fat = lazy(function()
|
|
return require("@rosetta/containers/Fat")
|
|
end)
|
|
S["archive/fat"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 512 then
|
|
return 0
|
|
end
|
|
local v = Fat.open(r.buf, r.base + r.pos, r.limit - r.base - r.pos)
|
|
if not v then
|
|
return 0
|
|
end
|
|
return if v.partition then 0.6 else 0.8
|
|
end
|
|
end
|
|
|
|
do -- archive/grp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local MAGIC = "KenSilverman"
|
|
S["archive/grp"] = function(r: Bytes.Reader): number
|
|
return Sniff.magic(r, MAGIC)
|
|
end
|
|
end
|
|
|
|
do -- archive/gzip
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/gzip"] = function(r: Bytes.Reader): number
|
|
return if r:peek(3) == "\31\139\8" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/iso
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/iso"] = function(r: Bytes.Reader): number
|
|
return if r:peek(5, 16 * 2048 + 1) == "CD001" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/lha
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local METHODS: { [string]: boolean } = {
|
|
["-lh0-"] = true, ["-lz4-"] = true, ["-lhd-"] = true, ["-lh1-"] = true, ["-lh4-"] = true, ["-lh5-"] = true,
|
|
["-lh6-"] = true, ["-lh7-"] = true, ["-lhx-"] = true, ["-lzs-"] = true, ["-lz5-"] = true, ["-lh2-"] = true,
|
|
["-lh3-"] = true, ["-pm0-"] = true, ["-pm1-"] = true, ["-pm2-"] = true, ["-lk7-"] = true,
|
|
}
|
|
local function isHeader(s: string): boolean
|
|
if #s < 22 then
|
|
return false
|
|
end
|
|
local m = string.sub(s, 3, 7)
|
|
local level = string.byte(s, 21)
|
|
return METHODS[m] == true and level <= 3
|
|
end
|
|
S["archive/lha"] = function(r: Bytes.Reader): number
|
|
if isHeader(r:peek(22)) then
|
|
return 0.95
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/lz4
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/lz4"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
if m == "\4\34\77\24" then
|
|
return 1
|
|
elseif m == "\2\33\76\24" then
|
|
return 0.9
|
|
elseif #m == 4 and string.byte(m, 1) >= 0x50 and string.byte(m, 1) <= 0x5F and string.sub(m, 2) == "\42\77\24" then
|
|
-- a skippable frame (zstd shares the magic): an LZ4 frame after it
|
|
local h = r:peek(4, 4)
|
|
if #h == 4 then
|
|
local n = string.byte(h, 1) + string.byte(h, 2) * 256 + string.byte(h, 3) * 65536 + string.byte(h, 4) * 16777216
|
|
if r:peek(4, 8 + n) == "\4\34\77\24" then
|
|
return 1
|
|
end
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/macbinary
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc16 = require("@rosetta/core/Crc16")
|
|
local function version(b: buffer, o: number, len: number): string?
|
|
if len < 128 then
|
|
return nil
|
|
end
|
|
local u8 = function(i: number): number
|
|
return buffer.readu8(b, o + i)
|
|
end
|
|
if u8(0) ~= 0 or u8(74) ~= 0 then
|
|
return nil
|
|
end
|
|
local nameLen = u8(1)
|
|
if nameLen < 1 or nameLen > 63 then
|
|
return nil
|
|
end
|
|
local function be32(i: number): number
|
|
return u8(i) * 16777216 + u8(i + 1) * 65536 + u8(i + 2) * 256 + u8(i + 3)
|
|
end
|
|
local dlen, rlen = be32(83), be32(87)
|
|
if dlen > 0x7FFFFFFF or rlen > 0x7FFFFFFF then
|
|
return nil
|
|
end
|
|
local crc = u8(124) * 256 + u8(125)
|
|
if crc == Crc16.ccitt(b, o, 124) and (crc ~= 0 or u8(122) >= 129) then
|
|
return if buffer.readstring(b, o + 102, 4) == "mBIN" then "III" else "II"
|
|
end
|
|
if buffer.readstring(b, o + 102, 4) == "mBIN" then
|
|
return nil -- III with a bad CRC
|
|
end
|
|
-- MacBinary I: byte 82 zero, bytes 99..125 zero, the forks within the file
|
|
if u8(82) ~= 0 then
|
|
return nil
|
|
end
|
|
for i = 99, 125 do
|
|
if u8(i) ~= 0 then
|
|
return nil
|
|
end
|
|
end
|
|
local need = 128 + math.ceil(dlen / 128) * 128 + rlen
|
|
return if need <= len + 127 then "I" else nil
|
|
end
|
|
S["archive/macbinary"] = function(r: Bytes.Reader): number
|
|
local n = math.min(r:remaining(), 128)
|
|
if n < 128 then
|
|
return 0
|
|
end
|
|
local v = version(r.buf, r.base + r.pos, r.limit - r.base - r.pos)
|
|
return if v == "III" then 0.95 elseif v == "II" then 0.8 elseif v == "I" then 0.3 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/mpq
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/mpq"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
if m == "MPQ\x1A" or m == "MPQ\x1B" then
|
|
return 1
|
|
end
|
|
-- a prefixed archive (an installer stub): the header on a 512-byte boundary in the first 64 KB
|
|
for at = 512, math.min(65536, r:remaining() - 32), 512 do
|
|
if r:peek(4, at) == "MPQ\x1A" then
|
|
return 0.6
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/pak
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["archive/pak"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 12 then
|
|
return 0
|
|
end
|
|
return math.max(Sniff.magic(r, "PACK", 0, 0.9), Sniff.magic(r, "SPAK", 0, 0.9))
|
|
end
|
|
end
|
|
|
|
do -- archive/rpm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local LEAD_MAGIC = "\237\171\238\219"
|
|
S["archive/rpm"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == LEAD_MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/tar
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Tar = lazy(function()
|
|
return require("@rosetta/containers/Tar")
|
|
end)
|
|
S["archive/tar"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 512 then
|
|
return 0
|
|
end
|
|
if not Tar.valid(r.buf, r.pos) then
|
|
return 0
|
|
end
|
|
return if r:peek(5, 257) == "ustar" then 0.95 else 0.6
|
|
end
|
|
end
|
|
|
|
do -- archive/unitypackage
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Deflate = lazy(function()
|
|
return require("@rosetta/codecs/compress/Deflate")
|
|
end)
|
|
local Tar = lazy(function()
|
|
return require("@rosetta/containers/Tar")
|
|
end)
|
|
local function guidOf(name: string): (string?, string?)
|
|
local n = string.gsub(name, "^%./", "")
|
|
local guid, rest = string.match(n, "^(%x+)/?(.*)$")
|
|
if guid and #guid == 32 then
|
|
return string.lower(guid), rest
|
|
end
|
|
return nil, nil
|
|
end
|
|
S["archive/unitypackage"] = function(r: Bytes.Reader): number
|
|
if r:peek(3) ~= "\31\139\8" then
|
|
return 0
|
|
end
|
|
local head = Deflate.peek(r.buf, r.pos, r.limit - r.pos, 512)
|
|
if not head or buffer.len(head) < 512 or not Tar.valid(head, 0) then
|
|
return 0
|
|
end
|
|
local z = 0
|
|
while z < 100 and buffer.readu8(head, z) ~= 0 do
|
|
z += 1
|
|
end
|
|
return if guidOf(buffer.readstring(head, 0, z)) then 1 else 0 -- ties with archive/gzip go to the extension
|
|
end
|
|
end
|
|
|
|
do -- archive/uu
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/uu"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(4096)
|
|
if string.match(head, "^begin%-base64 [0-7]+ [^\r\n]") or string.match(head, "^begin [0-7]+ [^\r\n]") then
|
|
return 0.9
|
|
end
|
|
if string.match(head, "\nbegin%-base64 [0-7]+ [^\r\n]") or string.match(head, "\nbegin [0-7][0-7][0-7]+ [^\r\n]") then
|
|
return 0.5
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/vpk
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/vpk"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 12 or r:peek(4) ~= "\x34\x12\xAA\x55" then
|
|
return 0
|
|
end
|
|
local v = r:peekU8(4)
|
|
return if v == 1 or v == 2 then 1 else 0.5
|
|
end
|
|
end
|
|
|
|
do -- archive/vswap
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local readu8, readu16, readi16, writeu32 = buffer.readu8, buffer.readu16, buffer.readi16, buffer.writeu32
|
|
local function header(r: Bytes.Reader): (number?, number, number)
|
|
if r:remaining() < 6 then
|
|
return nil, 0, 0
|
|
end
|
|
local n, ss, so = readu16(r.buf, r.base), readu16(r.buf, r.base + 2), readu16(r.buf, r.base + 4)
|
|
if n == 0 or ss > so or so > n or 6 + n * 6 > r:remaining() then
|
|
return nil, 0, 0
|
|
end
|
|
return n, ss, so
|
|
end
|
|
S["archive/vswap"] = function(r: Bytes.Reader): number
|
|
local n = header(r)
|
|
if not n then
|
|
return 0
|
|
end
|
|
local size = r.limit - r.base
|
|
local b = r.buf
|
|
local last = 6 + n * 6
|
|
local nonzero = 0
|
|
for i = 0, n - 1 do
|
|
local o = buffer.readu32(b, r.base + 6 + i * 4)
|
|
local l = readu16(b, r.base + 6 + n * 4 + i * 2)
|
|
if o ~= 0 then
|
|
if o < last or o + l > size then
|
|
return 0
|
|
end
|
|
last = o
|
|
nonzero += 1
|
|
end
|
|
end
|
|
return if nonzero > 0 then 0.6 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/wad
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/wad"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 12 then
|
|
return 0
|
|
end
|
|
local m = r:peek(4)
|
|
if m ~= "IWAD" and m ~= "PWAD" then
|
|
return 0
|
|
end
|
|
return 0.9
|
|
end
|
|
end
|
|
|
|
do -- archive/wad2
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/wad2"] = function(r: Bytes.Reader): number
|
|
if r:remaining() < 12 then
|
|
return 0
|
|
end
|
|
local m = r:peek(4)
|
|
return if m == "WAD2" or m == "WAD3" then 0.9 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/xz
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local XZ_MAGIC = "\253" .. "7zXZ\0"
|
|
local function aloneScore(r: Bytes.Reader): number
|
|
local h = r:peek(14)
|
|
if #h < 14 then
|
|
return 0
|
|
end
|
|
local d = string.byte(h, 1)
|
|
if d >= 225 or string.byte(h, 14) ~= 0 then
|
|
return 0
|
|
end
|
|
local dict = string.byte(h, 2) + string.byte(h, 3) * 256 + string.byte(h, 4) * 65536 + string.byte(h, 5) * 16777216
|
|
local okDict = false
|
|
for n = 12, 31 do
|
|
if dict == 2 ^ n or (n < 31 and dict == 2 ^ n + 2 ^ (n - 1)) then
|
|
okDict = true
|
|
break
|
|
end
|
|
end
|
|
if not okDict then
|
|
return 0
|
|
end
|
|
local s = string.sub(h, 6, 13)
|
|
if s ~= string.rep("\255", 8) and (string.byte(h, 11) ~= 0 or string.byte(h, 12) ~= 0 or string.byte(h, 13) ~= 0) then
|
|
return 0
|
|
end
|
|
return if d == 0x5D then 0.4 else 0.2
|
|
end
|
|
S["archive/xz"] = function(r: Bytes.Reader): number
|
|
if r:peek(6) == XZ_MAGIC then
|
|
return 1
|
|
end
|
|
return aloneScore(r)
|
|
end
|
|
end
|
|
|
|
do -- archive/yenc
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/yenc"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(4096)
|
|
if string.match(head, "^=ybegin ") then
|
|
return 0.95
|
|
end
|
|
return if string.find(head, "\n=ybegin [^\n]*name=") then 0.6 else 0
|
|
end
|
|
end
|
|
|
|
do -- archive/z
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/z"] = function(r: Bytes.Reader): number
|
|
if r:peek(2) ~= "\31\157" then
|
|
return 0
|
|
end
|
|
local f = r:peekU8(2)
|
|
return if f >= 0 and bit32.band(f, 0x60) == 0 and bit32.band(f, 0x1F) >= 9 and bit32.band(f, 0x1F) <= 16 then 1 else 0.3
|
|
end
|
|
end
|
|
|
|
do -- archive/zip
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["archive/zip"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(4)
|
|
-- ZIP-based formats (docx, jar, 3mf, ...) score higher in their own sniff
|
|
if s == "PK\3\4" or s == "PK\5\6" then
|
|
return 0.6
|
|
end
|
|
if string.sub(s, 1, 2) == "MZ" then
|
|
return 0 -- a self-extractor opens with Zip.decode / opts.format = "zip"
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- archive/zoo
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local TAG = 0xFDC4A7DC
|
|
S["archive/zoo"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(24)
|
|
if #h < 24 then
|
|
return 0
|
|
end
|
|
local tag = string.byte(h, 21) + string.byte(h, 22) * 256 + string.byte(h, 23) * 65536 + string.byte(h, 24) * 16777216
|
|
if tag ~= TAG then
|
|
return 0
|
|
end
|
|
return if string.sub(h, 1, 4) == "ZOO " then 1 else 0.8
|
|
end
|
|
end
|
|
|
|
do -- archive/zst
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Zstd = lazy(function()
|
|
return require("@rosetta/codecs/compress/Zstd")
|
|
end)
|
|
S["archive/zst"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
if m == Zstd.MAGIC then
|
|
return 1
|
|
end
|
|
-- a skippable frame (0x184D2A50 .. 0x184D2A5F) is usually followed by a zstd frame
|
|
if #m == 4 and string.sub(m, 2, 4) == "\42\77\24" and bit32.band(string.byte(m, 1), 0xF0) == 0x50 then
|
|
return 0.5
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/cbor
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/cbor"] = function(r: Bytes.Reader): number
|
|
return if r:peek(3) == "\217\217\247" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- data/csv
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local find, sub, byte, gsub = string.find, string.sub, string.byte, string.gsub
|
|
S["data/csv"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(1024)
|
|
if find(head, "%z") then
|
|
return 0
|
|
end
|
|
local l1, l2 = string.match(head, "^([^\r\n]*)\r?\n([^\r\n]*)")
|
|
if not l1 or l1 == "" then
|
|
return 0
|
|
end
|
|
for _, d in { ",", "\t", ";" } do
|
|
local _, c1 = gsub(l1, d, "")
|
|
local _, c2 = gsub(l2 :: string, d, "")
|
|
if c1 > 0 and c1 == c2 then
|
|
return 0.15
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/ini
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/ini"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(512)
|
|
if string.find(head, "%z") then
|
|
return 0
|
|
end
|
|
local first = string.match(head, "^[%s]*([^\r\n]*)")
|
|
while first and string.match(first, "^[;#]") do
|
|
head = string.match(head, "^[^\n]*\n(.*)$") or ""
|
|
first = string.match(head, "^[%s]*([^\r\n]*)")
|
|
end
|
|
if first and string.match(first, "^%[[^%]]+%]%s*$") then
|
|
return 0.2
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/json
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/json"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 64)
|
|
local first = string.match(head, "^[ \t\r\n]*(.)")
|
|
if first == "{" then
|
|
local nxt = string.match(head, "^[^{]*{[ \t\r\n]*(.)")
|
|
return if nxt == nil or nxt == '"' or nxt == "}" then 0.6 else 0.1
|
|
elseif first == "[" then
|
|
-- past any nested '[': a JSON value must start (TOML / INI "[section]" and "[[table]]" do not)
|
|
local nxt = string.match(head, "^[ \t\r\n]*[%[ \t\r\n]*(.)")
|
|
if nxt == nil or string.find('{"-0123456789tfn]', nxt, 1, true) then
|
|
return 0.3
|
|
end
|
|
return 0
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/msgpack
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/msgpack"] = function(_r: Bytes.Reader): number
|
|
return 0 -- no magic: by extension or opts.format only
|
|
end
|
|
end
|
|
|
|
do -- data/nbt
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Nbt = lazy(function()
|
|
return require("@rosetta/codecs/text/Nbt")
|
|
end)
|
|
S["data/nbt"] = function(r: Bytes.Reader): number
|
|
local p = Nbt.peek(r.buf, r.base, r.limit - r.base)
|
|
if not p or p.rootType ~= "compound" then
|
|
return 0
|
|
end
|
|
if p.compression ~= "none" then
|
|
return if p.firstType then 1 else 0.5 -- gzip wrapped NBT: ties with archive/gzip go to the extension
|
|
end
|
|
return if p.firstType and p.firstName then 0.6 else 0.2
|
|
end
|
|
end
|
|
|
|
do -- data/plist
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/plist"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(256)
|
|
if string.sub(head, 1, 6) == "bplist" then
|
|
return 1
|
|
end
|
|
if string.find(head, "<!DOCTYPE plist", 1, true) or string.find(head, "<plist", 1, true) then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/q3shader
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local KEYWORDS = { "surfaceparm", "map", "clampmap", "animmap", "blendfunc", "rgbgen", "alphagen", "tcgen", "tcmod",
|
|
"cull", "deformvertexes", "qer_editorimage", "nopicmip", "nomipmaps", "skyparms", "q3map_", "depthwrite", "alphafunc" }
|
|
S["data/q3shader"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(math.min(r:remaining(), 2048))
|
|
if #head < 4 or string.find(head, "[%z\1-\8\14-\31]") then
|
|
return 0
|
|
end
|
|
local s = string.gsub(string.gsub(head, "/%*.-%*/", " "), "//[^\n]*", "")
|
|
if not string.match(s, "^%s*[^%s{}\"]+%s*{") then
|
|
return 0
|
|
end
|
|
local lower = string.lower(s)
|
|
for _, k in KEYWORDS do
|
|
if string.find(lower, "%f[%w_]" .. k) then
|
|
return 0.7
|
|
end
|
|
end
|
|
return 0.2
|
|
end
|
|
end
|
|
|
|
do -- data/q3skin
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/q3skin"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(math.min(r:remaining(), 1024))
|
|
if #head < 3 or string.find(head, "[%z\1-\8\14-\31]") then
|
|
return 0
|
|
end
|
|
local lines, good, paths = 0, 0, 0
|
|
for line in string.gmatch(head, "[^\r\n]+") do
|
|
if not string.match(line, "^%s*$") then
|
|
lines += 1
|
|
local surface, shader = string.match(line, "^%s*([%w_%-]+)%s*,%s*([^,%s]*)%s*$")
|
|
if surface and shader == "" and string.sub(string.lower(surface), 1, 4) == "tag_" then
|
|
good += 1
|
|
elseif shader and string.find(shader, "/", 1, true) then
|
|
good += 1
|
|
paths += 1
|
|
end
|
|
end
|
|
end
|
|
return if lines > 0 and good == lines and paths > 0 then 0.5 else 0
|
|
end
|
|
end
|
|
|
|
do -- data/snbt
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["data/snbt"] = function(r: Bytes.Reader): number
|
|
-- SNBT has what JSON never has: unquoted keys, typed numbers (1b, 2.5f), [I; arrays, single-quoted strings
|
|
local head = Sniff.text(r, 512, true)
|
|
local first = string.sub(head, 1, 1)
|
|
if first ~= "{" and first ~= "[" then
|
|
return 0
|
|
end
|
|
if string.find(head, "[{,]%s*[%a_][%w_%-%.%+]*%s*:") or string.find(head, "%d[bBsSlLfFdD]%s*[,}%]]")
|
|
or string.find(head, "%[[BIL]%s*;") or string.find(head, "[:,%[]%s*'")
|
|
then
|
|
return 0.7
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/toml
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/toml"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(512)
|
|
if string.find(head, "%z") then
|
|
return 0
|
|
end
|
|
for line in string.gmatch(head, "[^\n]+") do
|
|
local l = string.match(line, "^[ \t]*(.-)[ \t\r]*$") :: string
|
|
if l ~= "" and string.sub(l, 1, 1) ~= "#" then
|
|
if string.match(l, "^%[%[?[%w_%-%.\"' ]+%]%]?$") or string.match(l, "^[%w_%-%.\"']+[ \t]*=[ \t]*[\"'%[{%dtf%+%-in]") then
|
|
return 0.2
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/tres
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["data/tres"] = function(r: Bytes.Reader): number
|
|
local h = Sniff.text(r, 256)
|
|
if string.match(h, "^%s*%[gd_resource[%s%]]") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/vdf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local KeyValues = lazy(function()
|
|
return require("@rosetta/codecs/text/KeyValues")
|
|
end)
|
|
S["data/vdf"] = function(r: Bytes.Reader): number
|
|
local n = r:remaining()
|
|
if KeyValues.isBinary(r.buf, r.pos, n) then
|
|
return 0.3
|
|
end
|
|
local head = Sniff.text(r, 256, true)
|
|
if string.find(head, "%z") then
|
|
return 0
|
|
end
|
|
head = string.gsub(head, "^//[^\n]*\n%s*", "")
|
|
if string.match(head, '^"[^"\n]+"%s*{') or string.match(head, '^"[^"\n]+"%s*%[[^%]\n]*%]%s*{') then
|
|
return 0.6
|
|
end
|
|
if string.match(head, "^#base%s") or string.match(head, "^#include%s") then
|
|
return 0.5
|
|
end
|
|
if string.match(head, '^[%w_]+%s*\n%s*{%s*\n%s*"') then
|
|
return 0.3
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/xml
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/xml"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 64)
|
|
if string.match(head, "^<%?xml[ \t\r\n]") or string.sub(head, 1, 4) == "\255\254<\0" or string.sub(head, 1, 4) == "\254\255\0<" then
|
|
return 0.5
|
|
end
|
|
if string.match(head, "^[ \t\r\n]*<[%a!]") then
|
|
return 0.3
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- data/yaml
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["data/yaml"] = function(r: Bytes.Reader): number
|
|
local head = r:peek(256)
|
|
if string.find(head, "%z") then
|
|
return 0
|
|
end
|
|
if string.match(head, "^%%YAML ") then
|
|
return 0.9
|
|
end
|
|
if string.match(head, "^%-%-%-[ \r\n]") then
|
|
return 0.5
|
|
end
|
|
local line = string.match(head, "^[ \t\r\n]*([^\r\n]*)")
|
|
if line and (string.match(line, "^[%w_%-\"']+[%w_%- \"']*:[ ]") or string.match(line, "^[%w_%-]+:$") or string.match(line, "^%- ")) then
|
|
return 0.2
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- vector/gamemaps
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "TED5v1.0"
|
|
S["vector/gamemaps"] = function(r: Bytes.Reader): number
|
|
if r:peek(8) == MAGIC then
|
|
return 1
|
|
end
|
|
-- MAPHEAD: the RLEW tag 0xABCD, then 100 offsets (0 / -1 when absent)
|
|
if r:remaining() >= 402 and r:remaining() <= 1024 and buffer.readu16(r.buf, r.base) == 0xABCD then
|
|
local first = buffer.readi32(r.buf, r.base + 2)
|
|
if first > 0 then
|
|
return 0.7
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- vector/ldtk
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["vector/ldtk"] = function(r: Bytes.Reader): number
|
|
local h = Sniff.text(r, 1024)
|
|
if not string.match(h, "^%s*{") then
|
|
return 0
|
|
end
|
|
if string.find(h, "\"fileType\"%s*:%s*\"LDtk Project JSON\"") then
|
|
return 1
|
|
end
|
|
if string.find(h, "\"jsonVersion\"", 1, true) and string.find(h, "\"worldLayout\"", 1, true) then
|
|
return 0.8
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- vector/ogmo
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
S["vector/ogmo"] = function(r: Bytes.Reader): number
|
|
local h = Sniff.text(r, 2048)
|
|
if not string.match(h, "^%s*{") or not string.find(h, "\"ogmoVersion\"", 1, true) then
|
|
return 0
|
|
end
|
|
if string.find(h, "\"layerGridDefaultSize\"", 1, true) or string.find(h, "\"levelPaths\"", 1, true) then
|
|
return 1
|
|
end
|
|
return 0.9
|
|
end
|
|
end
|
|
|
|
do -- vector/svg
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["vector/svg"] = function(r: Bytes.Reader): number
|
|
local h = Sniff.text(r, 4096)
|
|
if not string.match(h, "^%s*<") then
|
|
return 0
|
|
end
|
|
if string.find(h, "<svg[%s>:]") or string.find(h, "<%w+:svg[%s>]") then
|
|
return 0.95
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- vector/tiled
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function head(r: Bytes.Reader): string
|
|
local h = Sniff.text(r, 1024)
|
|
return h
|
|
end
|
|
S["vector/tiled"] = function(r: Bytes.Reader): number
|
|
local h = head(r)
|
|
if string.match(h, "^%s*<") then
|
|
if string.find(h, "<map[%s>]") or string.find(h, "<tileset[%s>]") then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
if string.match(h, "^%s*{") then
|
|
-- Tiled writes its keys sorted: "type" comes last, so look at the tail too
|
|
local n = r:remaining()
|
|
local tail = if n > 1024 then r:peek(1024, n - 1024) else ""
|
|
for _, part in { h, tail } do
|
|
if string.find(part, "\"type\"%s*:%s*\"map\"") or string.find(part, "\"type\"%s*:%s*\"tileset\"") then
|
|
return 0.8
|
|
end
|
|
end
|
|
if string.find(h, "\"tilewidth\"", 1, true) and (string.find(h, "\"orientation\"", 1, true) or string.find(h, "\"tilesets\"", 1, true)) then
|
|
return 0.6
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- layered/aseprite
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["layered/aseprite"] = function(r: Bytes.Reader): number
|
|
if r:peekU8(4) == 0xE0 and r:peekU8(5) == 0xA5 then
|
|
local d = r:peekU8(12)
|
|
if (d == 32 or d == 16 or d == 8) and r:peekU8(13) == 0 then
|
|
return 1
|
|
end
|
|
return 0.5
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- layered/ora
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MIME = "image/openraster"
|
|
S["layered/ora"] = function(r: Bytes.Reader): number
|
|
-- a zip whose first (stored) entry is "mimetype" holding image/openraster
|
|
if r:peek(4) == "PK\3\4" and r:peek(8, 30) == "mimetype" and r:peek(#MIME, 38) == MIME then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- layered/psd
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["layered/psd"] = function(r: Bytes.Reader): number
|
|
local h = r:peek(6)
|
|
if string.sub(h, 1, 4) == "8BPS" and (string.sub(h, 5, 6) == "\0\1" or string.sub(h, 5, 6) == "\0\2") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- subtitle/ass
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["subtitle/ass"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 64)
|
|
if string.match(head, "^%s*%[[Ss]cript [Ii]nfo%]") then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- subtitle/srt
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local TIMING = "^%s*(%d+):(%d%d?):(%d%d?)[,.](%d+)%s*%-%->%s*(%d+):(%d%d?):(%d%d?)[,.](%d+)%s*(.-)%s*$"
|
|
local function frac(digits: string): number
|
|
return (tonumber(digits) :: number) / 10 ^ #digits
|
|
end
|
|
local function timing(line: string): (number?, number?, string?)
|
|
local h1, m1, s1, f1, h2, m2, s2, f2, rest = string.match(line, TIMING)
|
|
if not h1 then
|
|
return nil, nil, nil
|
|
end
|
|
local a = tonumber(h1) :: number * 3600 + tonumber(m1) :: number * 60 + tonumber(s1) :: number + frac(f1 :: string)
|
|
local b = tonumber(h2) :: number * 3600 + tonumber(m2) :: number * 60 + tonumber(s2) :: number + frac(f2 :: string)
|
|
return a, b, rest
|
|
end
|
|
S["subtitle/srt"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 512)
|
|
head = string.gsub(head, "\r", "\n")
|
|
local first, second = string.match(head, "^%s*([^\n]*)\n([^\n]*)")
|
|
if first and string.match(first, "^%d+%s*$") and second and timing(second) then
|
|
return 0.9
|
|
end
|
|
if first and timing(first) then
|
|
return 0.6
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- subtitle/vtt
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["subtitle/vtt"] = function(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 10)
|
|
if string.sub(head, 1, 6) == "WEBVTT" then
|
|
local c = string.sub(head, 7, 7)
|
|
if c == "" or c == " " or c == "\t" or c == "\n" or c == "\r" then
|
|
return 1
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/669
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function validHeader(r: Bytes.Reader): boolean
|
|
if r:size() < 497 then
|
|
return false
|
|
end
|
|
local magic = r:peek(2)
|
|
if magic ~= "if" and magic ~= "JN" then
|
|
return false
|
|
end
|
|
local nsmp, npat, restart = r:peekU8(110), r:peekU8(111), r:peekU8(112)
|
|
if nsmp > 64 or restart >= 128 or npat > 128 then
|
|
return false
|
|
end
|
|
local bad = 0
|
|
for i = 2, 109 do
|
|
local c = r:peekU8(i)
|
|
if c > 0 and c <= 31 then
|
|
bad += 1
|
|
if bad > 40 then
|
|
return false
|
|
end
|
|
end
|
|
end
|
|
for i = 0, 127 do
|
|
local o, t, b = r:peekU8(113 + i), r:peekU8(241 + i), r:peekU8(369 + i)
|
|
if (o >= 128 and o < 0xFE) or (o < 128 and t == 0) or t > 15 or b >= 64 then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
S["music/669"] = function(r: Bytes.Reader): number
|
|
return if validHeader(r) then 0.6 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/ahx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/ahx"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
return if m and #m == 4 and string.sub(m, 1, 3) == "THX" and string.byte(m, 4) < 3 then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/far
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/far"] = function(r: Bytes.Reader): number
|
|
if r:size() >= 98 and r:peek(4) == "FAR\xFE" and r:peek(3, 44) == "\r\n\x1A" and r:peekU8(47) + r:peekU8(48) * 256 >= 98 then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/hmp
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local HMI_MAGIC = "HMI-MIDISONG061595"
|
|
S["music/hmp"] = function(r: Bytes.Reader): number
|
|
return if r:peek(8) == "HMIMIDIP" or r:peek(18) == HMI_MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/hvl
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/hvl"] = function(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
return if m and #m == 4 and string.sub(m, 1, 3) == "HVL" and string.byte(m, 4) <= 1 then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/it
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/it"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "IMPM" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/med
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function validHeader(r: Bytes.Reader): boolean
|
|
if r:size() < 52 or r:peek(3) ~= "MMD" then
|
|
return false
|
|
end
|
|
local v = r:peekU8(3)
|
|
if v < 0x30 or v > 0x33 then
|
|
return false
|
|
end
|
|
local h = r:sub(8, 44)
|
|
local songOff = h:u32be()
|
|
h:skip(4)
|
|
local blockOff = h:u32be()
|
|
h:skip(4)
|
|
local smpOff = h:u32be()
|
|
return songOff >= 52 and songOff + 63 * 8 + 284 <= r:size() and blockOff >= 52 and (smpOff == 0 or smpOff >= 52)
|
|
end
|
|
S["music/med"] = function(r: Bytes.Reader): number
|
|
return if validHeader(r) then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/midi
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/midi"] = function(r: Bytes.Reader): number
|
|
if r:peek(4) == "MThd" then
|
|
return 1
|
|
end
|
|
if r:peek(4) == "RIFF" and r:peek(4, 8) == "RMID" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/mod
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local FIXED: { [string]: number } = {
|
|
["M.K."] = 4,
|
|
["M!K!"] = 4,
|
|
["M&K!"] = 4,
|
|
["N.T."] = 4,
|
|
["FLT4"] = 4,
|
|
["FLT8"] = 8,
|
|
["CD81"] = 8,
|
|
["OKTA"] = 8,
|
|
["OCTA"] = 8,
|
|
["EXO4"] = 4,
|
|
["EXO8"] = 8,
|
|
}
|
|
local function signatureChannels(sig: string): number?
|
|
local fixed = FIXED[sig]
|
|
if fixed then
|
|
return fixed
|
|
end
|
|
local d = string.match(sig, "^(%d)CHN$")
|
|
if d then
|
|
local n = tonumber(d) :: number
|
|
return if n >= 1 then n else nil
|
|
end
|
|
local dd = string.match(sig, "^(%d%d)C[HN]$")
|
|
if dd then
|
|
local n = tonumber(dd) :: number
|
|
return if n >= 1 and n <= 32 then n else nil
|
|
end
|
|
local t = string.match(sig, "^TDZ(%d)$")
|
|
if t then
|
|
local n = tonumber(t) :: number
|
|
return if n >= 1 then n else nil
|
|
end
|
|
return nil
|
|
end
|
|
S["music/mod"] = function(r: Bytes.Reader): number
|
|
if r:size() < 1084 then
|
|
return 0
|
|
end
|
|
local sig = r:peek(4, 1080)
|
|
if signatureChannels(sig) then
|
|
return 0.8
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/mtm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/mtm"] = function(r: Bytes.Reader): number
|
|
if r:size() >= 66 and r:peek(3) == "MTM" and r:peekU8(3) < 0x20 and r:peekU8(33) >= 1 and r:peekU8(33) <= 32 then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/mus
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/mus"] = function(r: Bytes.Reader): number
|
|
return if r:peek(4) == "MUS\x1a" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/okt
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/okt"] = function(r: Bytes.Reader): number
|
|
return if r:peek(8) == "OKTASONG" then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/ptm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function validHeader(r: Bytes.Reader): boolean
|
|
if r:size() < 608 or r:peek(4, 44) ~= "PTMF" or r:peekU8(28) ~= 26 or r:peekU8(30) > 2 then
|
|
return false
|
|
end
|
|
local h = r:sub(32, 10)
|
|
local nord, nsmp, npat, nch, flags = h:u16le(), h:u16le(), h:u16le(), h:u16le(), h:u16le()
|
|
return flags == 0 and nch >= 1 and nch <= 32 and nord >= 1 and nord <= 256 and nsmp >= 1 and nsmp <= 255 and npat >= 1 and npat <= 128
|
|
end
|
|
S["music/ptm"] = function(r: Bytes.Reader): number
|
|
return if validHeader(r) then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/s3m
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/s3m"] = function(r: Bytes.Reader): number
|
|
if r:size() >= 48 and r:peek(4, 44) == "SCRM" then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/sf2
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local SoundFont = lazy(function()
|
|
return require("@rosetta/codecs/audio/SoundFont")
|
|
end)
|
|
S["music/sf2"] = function(r: Bytes.Reader): number
|
|
return if SoundFont.sniff(r) then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/stm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local function validHeader(r: Bytes.Reader): boolean
|
|
if r:size() < 48 then
|
|
return false
|
|
end
|
|
local eof, ftype, major, minor = r:peekU8(28), r:peekU8(29), r:peekU8(30), r:peekU8(31)
|
|
local npat, gv = r:peekU8(33), r:peekU8(34)
|
|
if ftype ~= 2 or (eof ~= 0x1A and eof ~= 2) or major ~= 2 or (minor ~= 0 and minor ~= 10 and minor ~= 20 and minor ~= 21) then
|
|
return false
|
|
end
|
|
if npat > 64 or (gv > 64 and gv ~= 0x58) then
|
|
return false
|
|
end
|
|
for i = 20, 27 do
|
|
local c = r:peekU8(i)
|
|
if c < 0x20 or c >= 0x7F then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
S["music/stm"] = function(r: Bytes.Reader): number
|
|
return if validHeader(r) then 0.7 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/ult
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/ult"] = function(r: Bytes.Reader): number
|
|
local v = r:peekU8(14)
|
|
if r:size() >= 48 and r:peek(14) == "MAS_UTrack_V00" and v >= 0x31 and v <= 0x34 then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- music/umx
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "\xC1\x83\x2A\x9E"
|
|
type Header = { version: number, nameCount: number, nameOffset: number, exportCount: number, exportOffset: number, importCount: number, importOffset: number }
|
|
local function header(r: Bytes.Reader): Header?
|
|
if r:size() < 36 or r:peek(4) ~= MAGIC then
|
|
return nil
|
|
end
|
|
local h = r:sub(4, 32)
|
|
local version = h:u16le()
|
|
h:skip(6)
|
|
local hd: Header = {
|
|
version = version,
|
|
nameCount = h:u32le(),
|
|
nameOffset = h:u32le(),
|
|
exportCount = h:u32le(),
|
|
exportOffset = h:u32le(),
|
|
importCount = h:u32le(),
|
|
importOffset = h:u32le(),
|
|
}
|
|
if h.err or hd.nameOffset < 36 or hd.exportOffset < 36 or hd.importOffset < 36 then
|
|
return nil
|
|
end
|
|
if hd.nameCount == 0 or hd.exportCount == 0 or hd.importCount == 0 or hd.nameCount > 2 ^ 20 or hd.exportCount > 2 ^ 20 or hd.importCount > 2 ^ 20 then
|
|
return nil
|
|
end
|
|
return hd
|
|
end
|
|
local function index(r: Bytes.Reader): number
|
|
local b = r:u8()
|
|
local neg = b >= 0x80
|
|
local v = b % 64
|
|
if bit32.band(b, 0x40) ~= 0 then
|
|
local shift = 6
|
|
repeat
|
|
b = r:u8()
|
|
v += (b % 128) * 2 ^ shift
|
|
shift += 7
|
|
until b < 0x80 or shift >= 32
|
|
end
|
|
return if neg then -v else v
|
|
end
|
|
local function names(r: Bytes.Reader, h: Header): { string }
|
|
local list = {}
|
|
local nr = r:sub(h.nameOffset)
|
|
for _ = 1, math.min(h.nameCount, 65536) do
|
|
if nr:remaining() < 5 then
|
|
break
|
|
end
|
|
if h.version >= 64 then
|
|
index(nr)
|
|
end
|
|
table.insert(list, string.lower(nr:cstring(1024)))
|
|
nr:skip(4)
|
|
end
|
|
return list
|
|
end
|
|
S["music/umx"] = function(r: Bytes.Reader): number
|
|
local h = header(r)
|
|
if not h then
|
|
return 0
|
|
end
|
|
return if table.find(names(r, h), "music") then 1 else 0.3
|
|
end
|
|
end
|
|
|
|
do -- music/xm
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local MAGIC = "Extended Module: "
|
|
S["music/xm"] = function(r: Bytes.Reader): number
|
|
return if r:peek(17) == MAGIC then 1 else 0
|
|
end
|
|
end
|
|
|
|
do -- music/xmi
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["music/xmi"] = function(r: Bytes.Reader): number
|
|
local id, kind = r:peek(4), r:peek(4, 8)
|
|
if id == "FORM" and (kind == "XDIR" or kind == "XMID") then
|
|
return 1
|
|
end
|
|
return if id == "CAT " and kind == "XMID" then 0.9 else 0
|
|
end
|
|
end
|
|
|
|
do -- playlist/m3u
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
S["playlist/m3u"] = function(r: Bytes.Reader): number
|
|
local s = Sniff.text(r, 512)
|
|
s = (string.match(s, "^%s*(.*)$") :: string)
|
|
if string.sub(s, 1, 7) == "#EXTM3U" then
|
|
return 0.95
|
|
end
|
|
if string.find(s, "^#EXTINF:") or string.find(s, "^#EXT%-X%-") then
|
|
return 0.8
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
do -- playlist/pls
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Text = lazy(function()
|
|
return require("@rosetta/codecs/text/Text")
|
|
end)
|
|
S["playlist/pls"] = function(r: Bytes.Reader): number
|
|
local s = Sniff.text(r, 64)
|
|
local first = string.match(s, "^%s*([^\r\n]*)")
|
|
return if first and string.lower(Text.trim(first)) == "[playlist]" then 0.95 else 0
|
|
end
|
|
end
|
|
|
|
do -- playlist/xspf
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local NS = "http://xspf.org/ns/0/"
|
|
S["playlist/xspf"] = function(r: Bytes.Reader): number
|
|
local s = r:peek(1024)
|
|
local i = string.find(s, "<%w*:?playlist[%s>]")
|
|
if i and string.find(s, NS, 1, true) then
|
|
return 0.95
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
return S
|