Files
rosetta/src/Rosetta/Sniffs.luau
T

4238 lines
112 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
local P_audio_mp3_Tags: any = {}
do -- audio/mp3/Tags: id3v2Size
local Tags = {}
local band, bor, lshift = bit32.band, bit32.bor, bit32.lshift
local ru8 = buffer.readu8
local function syncsafe(b: buffer, o: number): number
return bor(lshift(band(ru8(b, o), 0x7F), 21), lshift(band(ru8(b, o + 1), 0x7F), 14), lshift(band(ru8(b, o + 2), 0x7F), 7), band(ru8(b, o + 3), 0x7F))
end
function Tags.id3v2Size(b: buffer, o: number, len: number): number
if len - o < 10 or buffer.readstring(b, o, 3) ~= "ID3" then
return 0
end
if band(ru8(b, o + 5), 15) ~= 0 then
return 0
end
for k = 6, 9 do
if band(ru8(b, o + k), 0x80) ~= 0 then
return 0
end
end
local size = syncsafe(b, o + 6) + 10
if band(ru8(b, o + 5), 16) ~= 0 then
size += 10
end
return size
end
P_audio_mp3_Tags.id3v2Size = Tags.id3v2Size
end
local P_codecs_audio_Aac: any = {}
do -- codecs/audio/Aac: adts
local Aac = {}
local Bits = require("@rosetta/core/Bits")
Aac.RATES = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 }
local function objectType(br: Bits.BitReader): number
local t = br:read(5)
if t == 31 then
t = 32 + br:read(6)
end
return t
end
type Adts = {
headerSize: number, -- 7, or 9 with a CRC
frameSize: number,
objectType: number,
sfIndex: number,
sampleRate: number,
channelConfig: number,
blocks: number, -- raw_data_blocks in the frame (1 .. 4)
protected: boolean,
}
function Aac.adts(b: buffer, o: number, limit: number): Adts?
if o < 0 or o + 7 > limit or o + 7 > buffer.len(b) then
return nil
end
local b0, b1, b2, b3, b4, b5, b6 = buffer.readu8(b, o), buffer.readu8(b, o + 1), buffer.readu8(b, o + 2), buffer.readu8(b, o + 3), buffer.readu8(b, o + 4), buffer.readu8(b, o + 5), buffer.readu8(b, o + 6)
if b0 ~= 0xFF or bit32.band(b1, 0xF6) ~= 0xF0 then -- sync, layer 0
return nil
end
local protected = bit32.band(b1, 1) == 0
local profile = bit32.rshift(b2, 6)
local sfi = bit32.band(bit32.rshift(b2, 2), 15)
local ch = bit32.band(b2, 1) * 4 + bit32.rshift(b3, 6)
local size = bit32.band(b3, 3) * 2048 + b4 * 8 + bit32.rshift(b5, 5)
local blocks = bit32.band(b6, 3) + 1
local hs = if protected then 9 else 7
if sfi > 12 or size < hs then
return nil
end
return {
headerSize = hs,
frameSize = size,
objectType = profile + 1,
sfIndex = sfi,
sampleRate = Aac.RATES[sfi + 1],
channelConfig = ch,
blocks = blocks,
protected = protected,
}
end
P_codecs_audio_Aac.adts = Aac.adts
end
local P_codecs_audio_Ac3_Tables: any = {}
do -- codecs/audio/Ac3/Tables: BITRATES, RATES, frameWords
local T = {}
T.BITRATES = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640 }
T.RATES = { 48000, 44100, 32000 }
function T.frameWords(fscod: number, frmsizecod: number): number
local kbps = T.BITRATES[frmsizecod // 2 + 1]
if not kbps or fscod > 2 then
return 0
end
if fscod == 0 then
return 2 * kbps
elseif fscod == 2 then
return 3 * kbps
end
return kbps * 1536000 // 705600 + frmsizecod % 2
end
P_codecs_audio_Ac3_Tables.BITRATES = T.BITRATES
P_codecs_audio_Ac3_Tables.RATES = T.RATES
P_codecs_audio_Ac3_Tables.frameWords = T.frameWords
end
local P_codecs_audio_Ac3: any = {}
do -- codecs/audio/Ac3: header
local Ac3 = {}
local T = P_codecs_audio_Ac3_Tables
local readu8 = buffer.readu8
local NFCHANS = { [0] = 2, 1, 2, 3, 3, 4, 4, 5 }
type Header = {
sampleRate: number,
frameSize: number,
bitrate: number,
channels: number,
acmod: number,
lfeon: boolean,
bsid: number,
}
function Ac3.header(b: buffer, o: number, limit: number): Header?
if o < 0 or o + 8 > limit or o + 8 > buffer.len(b) then
return nil
end
if readu8(b, o) ~= 0x0B or readu8(b, o + 1) ~= 0x77 then
return nil
end
local bsid = bit32.rshift(readu8(b, o + 5), 3)
if bsid > 10 then
if bsid > 16 then
return nil
end
-- E-AC-3: strmtyp(2) substreamid(3) frmsiz(11) fscod(2) ...
local words = bit32.band(readu8(b, o + 2), 7) * 256 + readu8(b, o + 3) + 1
local fs = bit32.rshift(readu8(b, o + 4), 6)
return {
sampleRate = if fs < 3 then T.RATES[fs + 1] else 0,
frameSize = words * 2,
bitrate = 0,
channels = 0,
acmod = bit32.band(bit32.rshift(readu8(b, o + 4), 1), 7),
lfeon = false,
bsid = bsid,
}
end
local b4 = readu8(b, o + 4)
local fscod, frmsizecod = bit32.rshift(b4, 6), bit32.band(b4, 63)
local words = T.frameWords(fscod, frmsizecod)
if words == 0 then
return nil
end
local acmod = bit32.rshift(readu8(b, o + 6), 5)
-- lfeon: after acmod and the optional cmixlev / surmixlev / dsurmod fields
local skip = 0
if bit32.band(acmod, 1) == 1 and acmod ~= 1 then
skip += 2
end
if bit32.band(acmod, 4) ~= 0 then
skip += 2
end
if acmod == 2 then
skip += 2
end
local w = readu8(b, o + 6) * 256 + readu8(b, o + 7)
local lfeon = bit32.extract(w, 12 - skip, 1) == 1
local shift = if bsid > 8 then bsid - 8 else 0
return {
sampleRate = T.RATES[fscod + 1] // 2 ^ shift,
frameSize = words * 2,
bitrate = T.BITRATES[frmsizecod // 2 + 1] * 1000 // 2 ^ shift,
channels = NFCHANS[acmod] + (if lfeon then 1 else 0),
acmod = acmod,
lfeon = lfeon,
bsid = bsid,
}
end
P_codecs_audio_Ac3.header = Ac3.header
end
local P_codecs_audio_Mpa_Tables: any = {}
do -- codecs/audio/Mpa/Tables: halfrate
local T = {}
T.halfrate = { [0] = 0,
4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80, 0, 4, 8, 12, 16, 20, 24, 28, 32, 40,
48, 56, 64, 72, 80, 0, 16, 24, 28, 32, 40, 48, 56, 64, 72, 80, 88, 96, 112, 128, 0, 16, 20, 24,
28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 0, 16, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
160, 192, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224,
}
P_codecs_audio_Mpa_Tables.halfrate = T.halfrate
end
local P_codecs_audio_Mpa_Header: any = {}
do -- codecs/audio/Mpa/Header: compare, frameBytes, padding, valid
local H = {}
local T = P_codecs_audio_Mpa_Tables
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
local ru8 = buffer.readu8
local HALFRATE = T.halfrate
local HZ = { [0] = 44100, 48000, 32000 }
function H.valid(b: buffer, o: number): boolean
if ru8(b, o) ~= 0xFF then
return false
end
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
return (band(h1, 0xF0) == 0xF0 or band(h1, 0xFE) == 0xE2)
and band(rshift(h1, 1), 3) ~= 0
and rshift(h2, 4) ~= 15
and band(rshift(h2, 2), 3) ~= 3
end
local valid = H.valid
function H.compare(b: buffer, o1: number, o2: number): boolean
if not valid(b, o2) then
return false
end
local a1, a2 = ru8(b, o1 + 1), ru8(b, o1 + 2)
local c1, c2 = ru8(b, o2 + 1), ru8(b, o2 + 2)
return band(bxor(a1, c1), 0xFE) == 0
and band(bxor(a2, c2), 0x0C) == 0
and (band(a2, 0xF0) == 0) == (band(c2, 0xF0) == 0)
end
function H.bitrateKbps(h1: number, h2: number): number
local mpeg1 = if band(h1, 8) ~= 0 then 1 else 0
local layer = band(rshift(h1, 1), 3)
return 2 * HALFRATE[mpeg1 * 45 + (layer - 1) * 15 + rshift(h2, 4)]
end
local bitrateKbps = H.bitrateKbps
function H.sampleRate(h1: number, h2: number): number
local hz = HZ[band(rshift(h2, 2), 3)]
if band(h1, 8) == 0 then
hz //= 2
end
if band(h1, 0x10) == 0 then
hz //= 2
end
return hz
end
local sampleRate = H.sampleRate
function H.frameSamples(h1: number): number
if band(h1, 6) == 6 then
return 384
elseif band(h1, 14) == 2 then
return 576
end
return 1152
end
local frameSamples = H.frameSamples
function H.frameBytes(b: buffer, o: number, freeSize: number): number
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
local hz = sampleRate(h1, h2)
local fb = frameSamples(h1) * bitrateKbps(h1, h2) * 125 // hz
if band(h1, 6) == 6 then
fb = band(fb, 0xFFFFFFFC)
end
return if fb ~= 0 then fb else freeSize
end
function H.padding(b: buffer, o: number): number
if band(ru8(b, o + 2), 2) == 0 then
return 0
end
return if band(ru8(b, o + 1), 6) == 6 then 4 else 1
end
P_codecs_audio_Mpa_Header.compare = H.compare
P_codecs_audio_Mpa_Header.frameBytes = H.frameBytes
P_codecs_audio_Mpa_Header.padding = H.padding
P_codecs_audio_Mpa_Header.valid = H.valid
end
local P_codecs_audio_Musepack: any = {}
do -- codecs/audio/Musepack: magic
local Musepack = {}
function Musepack.magic(buf: buffer, p: number, limit: number): number
if p + 4 > limit then
return 0
end
local m = buffer.readstring(buf, p, 4)
if m == "MPCK" then
return 8
end
if string.sub(m, 1, 3) == "MP+" then
return 7
end
return 0
end
P_codecs_audio_Musepack.magic = Musepack.magic
end
local P_codecs_audio_SoundFont: any = {}
do -- codecs/audio/SoundFont: sniff
local SoundFont = {}
local Bytes = require("@rosetta/core/Bytes")
function SoundFont.sniff(r: Bytes.Reader): boolean
return r:peek(4) == "RIFF" and r:peek(4, 8) == "sfbk"
end
P_codecs_audio_SoundFont.sniff = SoundFont.sniff
end
local P_codecs_compress_Brotli = lazy(function()
return require("@rosetta/codecs/compress/Brotli")
end)
local P_codecs_compress_Deflate_Inflate = lazy(function()
return require("@rosetta/codecs/compress/Deflate/Inflate")
end)
local P_codecs_compress_Deflate: any = {}
do -- codecs/compress/Deflate: peek
local Deflate = {}
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Inflate = P_codecs_compress_Deflate_Inflate
local args = Bytes.span
local function shrink(out: buffer, n: number): buffer
if buffer.len(out) == n then
return out
end
local b = buffer.create(n)
buffer.copy(b, 0, out, 0, n)
return b
end
local function inflateErr(msg: string, at: number?): Err.Err
local code = if string.find(msg, "truncated", 1, true) then Err.TRUNCATED else Err.CORRUPT
return Err.new(code, "inflate: " .. msg, at)
end
local function gzipBody(b: buffer, p: number, e: number): (number?, Err.Err?)
local flg = buffer.readu8(b, p + 3)
if flg >= 0x20 then
return nil, Err.new(Err.CORRUPT, "gzip: reserved flags set", p + 3)
end
local q = p + 10
if bit32.band(flg, 4) ~= 0 then -- FEXTRA
if q + 2 > e then
return nil, Err.new(Err.TRUNCATED, "gzip: truncated header", q)
end
q += 2 + buffer.readu16(b, q)
end
for _, bit in { 8, 16 } do -- FNAME, FCOMMENT: zero-terminated
if bit32.band(flg, bit) ~= 0 then
while q < e and buffer.readu8(b, q) ~= 0 do
q += 1
end
q += 1
end
end
if bit32.band(flg, 2) ~= 0 then -- FHCRC
q += 2
end
if q >= e then
return nil, Err.new(Err.TRUNCATED, "gzip: truncated header", q)
end
return q, nil
end
function Deflate.peek(buf: buffer | string, offset: number?, length: number?, n: number): (buffer?, Err.Err?)
local b, o, l, aerr = args("Deflate.peek", buf, offset, length)
if not b then
return nil, aerr
end
if type(n) ~= "number" or n < 1 then
return nil, Err.new(Err.ARG, "Deflate.peek: bad size " .. tostring(n))
end
local e = o + l
local q = o
if l >= 18 and buffer.readu8(b, o) == 0x1f and buffer.readu8(b, o + 1) == 0x8b and buffer.readu8(b, o + 2) == 8 then
local herr
q, herr = gzipBody(b, o, e)
if not q then
return nil, herr
end
elseif l >= 6 then
local cmf, flg = buffer.readu8(b, o), buffer.readu8(b, o + 1)
if cmf % 16 == 8 and cmf // 16 <= 7 and (cmf * 256 + flg) % 31 == 0 and bit32.band(flg, 0x20) == 0 then
q = o + 2
end
end
local out, k = Inflate.raw(b, q :: number, e, n, true, true)
if not out then
return nil, inflateErr(tostring(k), q)
end
return shrink(out, k), nil
end
P_codecs_compress_Deflate.peek = Deflate.peek
end
local P_codecs_compress_Zstd_Decode = lazy(function()
return require("@rosetta/codecs/compress/Zstd/Decode")
end)
local P_codecs_compress_Zstd: any = {}
do -- codecs/compress/Zstd: MAGIC, decompress
local Zstd = {}
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Decode = P_codecs_compress_Zstd_Decode
type Options = {
maxBytes: number?,
verify: boolean?,
}
Zstd.MAGIC = "\40\181\47\253"
function Zstd.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
local b, o, l, aerr = Bytes.span("Zstd.decompress", buf, offset, length)
if not b then
return nil, aerr
end
local maxBytes = if opts and opts.maxBytes ~= nil then opts.maxBytes else 2 ^ 30
if type(maxBytes) ~= "number" or maxBytes < 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad maxBytes " .. tostring(maxBytes))
end
local verify = not (opts and opts.verify == false)
local out, len, consumed, at = Decode.decompress(b, o, o + l, maxBytes, verify)
if not out then
return nil, Err.from(consumed, at, len)
end
if buffer.len(out) ~= len then
local r = buffer.create(len)
buffer.copy(r, 0, out, 0, len)
out = r
end
return out, consumed
end
P_codecs_compress_Zstd.MAGIC = Zstd.MAGIC
P_codecs_compress_Zstd.decompress = Zstd.decompress
end
local P_codecs_text_KeyValues_Binary: any = {}
do -- codecs/text/KeyValues/Binary: sniff
local Binary = {}
local readu8, readu32, readi32, readf32 = buffer.readu8, buffer.readu32, buffer.readi32, buffer.readf32
function Binary.sniff(b: buffer, o: number, n: number): boolean
if n < 3 then
return false
end
local t = readu8(b, o)
if t ~= 0 and t ~= 1 and t ~= 2 then
return false
end
for i = 1, math.min(n - 1, 64) do
local c = readu8(b, o + i)
if c == 0 then
return i > 1
elseif c < 32 then
return false
end
end
return false
end
P_codecs_text_KeyValues_Binary.sniff = Binary.sniff
end
local P_codecs_text_KeyValues: any = {}
do -- codecs/text/KeyValues: isBinary
local KeyValues = {}
local Binary = P_codecs_text_KeyValues_Binary
KeyValues.isBinary = Binary.sniff
P_codecs_text_KeyValues.isBinary = KeyValues.isBinary
end
local P_codecs_text_Nbt_Value: any = {}
do -- codecs/text/Nbt/Value: NAMES
local Value = {}
Value.NAMES = {
[0] = "end",
"byte",
"short",
"int",
"long",
"float",
"double",
"byteArray",
"string",
"list",
"compound",
"intArray",
"longArray",
} :: { [number]: string }
Value.IDS = {} :: { [string]: number }
for id = 0, 12 do
Value.IDS[Value.NAMES[id]] = id
end
P_codecs_text_Nbt_Value.NAMES = Value.NAMES
end
local P_codecs_text_Nbt: any = {}
do -- codecs/text/Nbt: peek
local Nbt = {}
local Deflate = P_codecs_compress_Deflate
local Value = P_codecs_text_Nbt_Value
type Peek = {
compression: string,
endian: "big" | "little",
header: boolean,
rootType: string,
name: string,
firstType: string?,
firstName: string?,
head: buffer,
}
local function plausibleName(b: buffer, p: number, n: number): string?
if p + n > buffer.len(b) or n > 256 then
return nil
end
local s = buffer.readstring(b, p, n)
if string.find(s, "[%z\1-\31\127]") then
return nil
end
return s
end
function Nbt.peek(buf: buffer, offset: number?, length: number?): Peek?
local o = offset or 0
local l = length or (buffer.len(buf) - o)
if l < 3 then
return nil
end
local c0, c1 = buffer.readu8(buf, o), buffer.readu8(buf, o + 1)
local head: buffer
local comp = "none"
if c0 == 0x1f and c1 == 0x8b or (c0 % 16 == 8 and c0 < 0x80 and (c0 * 256 + c1) % 31 == 0) then
local h = Deflate.peek(buf, o, l, 1024)
if not h then
return nil
end
head, comp = h, if c0 == 0x1f then "gzip" else "zlib"
else
local n = math.min(l, 1024)
head = buffer.create(n)
buffer.copy(head, 0, buf, o, n)
end
local hn = buffer.len(head)
local function at(p: number, endian: string, header: boolean): Peek?
if p + 3 > hn then
return nil
end
local t = buffer.readu8(head, p)
if t ~= 10 and t ~= 9 then
return nil
end
local function u16(q: number): number
local a, b = buffer.readu8(head, q), buffer.readu8(head, q + 1)
return if endian == "big" then a * 256 + b else b * 256 + a
end
local nl = u16(p + 1)
local name = plausibleName(head, p + 3, nl)
if not name then
return nil
end
local res: Peek = { compression = comp, endian = endian :: any, header = header, rootType = Value.NAMES[t], name = name, head = head }
local q = p + 3 + nl
if t == 10 and q < hn then
local ft = buffer.readu8(head, q)
if ft > 12 then
return nil
end
if ft == 0 then
res.firstType = "end"
elseif q + 3 <= hn then
local fname = plausibleName(head, q + 3, u16(q + 1))
if fname == nil and q + 3 + u16(q + 1) <= hn then
return nil
end
res.firstType, res.firstName = Value.NAMES[ft], fname
end
end
return res
end
if hn >= 12 and buffer.readu32(head, 4) == (if comp == "none" then l else -1) - 8 then
local r = at(8, "little", true)
if r then
return r
end
end
return at(0, "big", false) or at(0, "little", false)
end
P_codecs_text_Nbt.peek = Nbt.peek
end
local P_codecs_text_Text: any = {}
do -- codecs/text/Text: toUtf8, trim, utf16
local Text = {}
function Text.utf8char(c: number): string
local char = string.char
if c < 0x80 then
return char(c)
elseif c < 0x800 then
return char(0xC0 + c // 64, 0x80 + c % 64)
elseif c < 0x10000 then
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
end
Text.CP1252 = {
[0x80] = 0x20AC, [0x82] = 0x201A, [0x83] = 0x0192, [0x84] = 0x201E, [0x85] = 0x2026, [0x86] = 0x2020,
[0x87] = 0x2021, [0x88] = 0x02C6, [0x89] = 0x2030, [0x8A] = 0x0160, [0x8B] = 0x2039, [0x8C] = 0x0152,
[0x8E] = 0x017D, [0x91] = 0x2018, [0x92] = 0x2019, [0x93] = 0x201C, [0x94] = 0x201D, [0x95] = 0x2022,
[0x96] = 0x2013, [0x97] = 0x2014, [0x98] = 0x02DC, [0x99] = 0x2122, [0x9A] = 0x0161, [0x9B] = 0x203A,
[0x9C] = 0x0153, [0x9E] = 0x017E, [0x9F] = 0x0178,
}
function Text.trim(s: string): string
return (string.match(s, "^%s*(.-)%s*$") :: string)
end
function Text.utf16(b: buffer, o: number, n: number, bigEndian: boolean?): string
local out = {}
local k = 0
local readu16 = buffer.readu16
local function unit(at: number): number
local v = readu16(b, at)
return if bigEndian then (v % 256) * 256 + v // 256 else v
end
while k + 1 < n do
local c = unit(o + k)
k += 2
if c >= 0xD800 and c < 0xDC00 and k + 1 < n then
local lo = unit(o + k)
if lo >= 0xDC00 and lo < 0xE000 then
c = 0x10000 + (c - 0xD800) * 1024 + (lo - 0xDC00)
k += 2
end
end
if c >= 0xD800 and c < 0xE000 then
c = 0xFFFD
end
table.insert(out, Text.utf8char(c))
end
return table.concat(out)
end
function Text.toUtf8(text: string, warnings: { string }?): string
if string.sub(text, 1, 3) == "\239\187\191" then
text = string.sub(text, 4)
end
if utf8.len(text) then
return text
end
if warnings then
table.insert(warnings, "text is not UTF-8: read as Windows-1252")
end
local out = table.create(#text)
for i = 1, #text do
local b = string.byte(text, i)
if b < 0x80 then
out[i] = string.char(b)
else
out[i] = utf8.char(if b < 0xA0 then Text.CP1252[b] or 0xFFFD else b)
end
end
return table.concat(out)
end
P_codecs_text_Text.toUtf8 = Text.toUtf8
P_codecs_text_Text.trim = Text.trim
P_codecs_text_Text.utf16 = Text.utf16
end
local P_containers_Fat: any = {}
do -- containers/Fat: open
local Fat = {}
local readu8, readu16, readu32 = buffer.readu8, buffer.readu16, buffer.readu32
type Volume = {
b: buffer,
base: number, -- byte offset of the volume (partition) in b
limit: number,
bps: number, -- bytes per sector
spc: number, -- sectors per cluster
fatOffset: number, -- byte offset of the first FAT (relative to base)
fatSize: number, -- bytes per FAT
rootOffset: number, -- FAT12 / 16: the fixed root directory
rootEntries: number,
rootCluster: number, -- FAT32
dataOffset: number, -- byte offset of cluster 2
clusters: number, -- data clusters
bits: number, -- 12 / 16 / 32
label: string?,
oem: string,
media: number,
partition: number?,
}
local function bpbAt(b: buffer, o: number, len: number): (Volume?, string?)
if len < 512 then
return nil, "truncated: smaller than a boot sector"
end
local jump = readu8(b, o)
local bps = readu16(b, o + 11)
local spc = readu8(b, o + 13)
if (jump ~= 0xEB and jump ~= 0xE9 and jump ~= 0xE8) or (bps ~= 512 and bps ~= 1024 and bps ~= 2048 and bps ~= 4096) then
return nil, "no FAT boot sector"
end
if spc == 0 or bit32.band(spc, spc - 1) ~= 0 then
return nil, "bad sectors per cluster"
end
local reserved, nfats, rootEntries = readu16(b, o + 14), readu8(b, o + 16), readu16(b, o + 17)
local total = readu16(b, o + 19)
if total == 0 then
total = readu32(b, o + 32)
end
local fatSectors = readu16(b, o + 22)
local fat32 = fatSectors == 0
if fat32 then
fatSectors = readu32(b, o + 36)
end
if reserved == 0 or nfats == 0 or fatSectors == 0 or total == 0 then
return nil, "bad BPB"
end
local rootSectors = math.ceil(rootEntries * 32 / bps)
local dataSector = reserved + nfats * fatSectors + rootSectors
if dataSector >= total then
return nil, "bad BPB: no data area"
end
local clusters = (total - dataSector) // spc
local bits = if clusters < 4085 then 12 elseif clusters < 65525 then 16 else 32
if fat32 and bits ~= 32 then
bits = 32 -- some small FAT32 volumes (mkfs.fat -F 32 on tiny images) are FAT32 regardless of the count
end
if total * bps > len then
-- images cut short at the last used sector are common: allow, reads past the end fail
total = len // bps
end
local labelAt = if bits == 32 then 71 else 43
local sig = readu8(b, o + (if bits == 32 then 66 else 38))
local label: string? = if sig == 0x29 then (string.gsub(buffer.readstring(b, o + labelAt, 11), " +$", "")) else nil
if label == "NO NAME" or label == "" then
label = nil
end
return {
b = b,
base = o,
limit = o + len,
bps = bps,
spc = spc,
fatOffset = reserved * bps,
fatSize = fatSectors * bps,
rootOffset = (reserved + nfats * fatSectors) * bps,
rootEntries = rootEntries,
rootCluster = if bits == 32 then readu32(b, o + 44) else 0,
dataOffset = dataSector * bps,
clusters = clusters,
bits = bits,
label = label,
oem = buffer.readstring(b, o + 3, 8),
media = readu8(b, o + 21),
}, nil
end
type OpenOptions = { partition: number? } -- 1..4: the MBR partition (default: the first FAT one)
local FAT_TYPES = { [0x01] = true, [0x04] = true, [0x06] = true, [0x0B] = true, [0x0C] = true, [0x0E] = true, [0x11] = true, [0x14] = true, [0x16] = true, [0x1B] = true, [0x1C] = true, [0x1E] = true }
function Fat.open(b: buffer, off: number, len: number, opts: OpenOptions?): (Volume?, string?)
local o: OpenOptions = opts or {}
off = math.max(0, off)
len = math.min(len, buffer.len(b) - off)
if len < 512 then
return nil, "truncated: smaller than a boot sector"
end
if not o.partition then
local v = bpbAt(b, off, len)
if v then
return v, nil
end
end
-- an MBR: 0x55AA and four partition entries
if readu8(b, off + 510) ~= 0x55 or readu8(b, off + 511) ~= 0xAA then
return nil, "no FAT boot sector and no partition table"
end
for i = 1, 4 do
local e = off + 446 + (i - 1) * 16
local ptype = readu8(b, e + 4)
local lba, count = readu32(b, e + 8), readu32(b, e + 12)
if (o.partition == i or (not o.partition and FAT_TYPES[ptype])) and ptype ~= 0 then
if lba * 512 >= len then
return nil, "truncated: partition " .. i .. " starts past the end"
end
local v, err = bpbAt(b, off + lba * 512, math.min(count * 512, len - lba * 512))
if v then
v.partition = i
end
return v, err
end
end
return nil, if o.partition then "no partition " .. o.partition else "no FAT partition"
end
P_containers_Fat.open = Fat.open
end
local P_containers_IsoBmff: any = {}
do -- containers/IsoBmff: sniff
local IsoBmff = {}
local Bytes = require("@rosetta/core/Bytes")
local TOP: { [string]: boolean } = {
ftyp = true, moov = true, mdat = true, free = true, skip = true, wide = true, pnot = true, moof = true,
styp = true, sidx = true, meta = true, uuid = true, PICT = true,
}
function IsoBmff.sniff(r: Bytes.Reader): string?
local head = r:peek(12)
if #head < 8 then
return nil
end
local typ = string.sub(head, 5, 8)
if typ == "ftyp" then
return if #head >= 12 then string.sub(head, 9, 12) else ""
end
if TOP[typ] then
local size = string.byte(head, 1) * 16777216 + string.byte(head, 2) * 65536 + string.byte(head, 3) * 256 + string.byte(head, 4)
if size == 1 or size == 0 or size >= 8 then
return ""
end
end
return nil
end
P_containers_IsoBmff.sniff = IsoBmff.sniff
end
local P_containers_Riff: any = {}
do -- containers/Riff: sniff
local Riff = {}
function Riff.sniff(r: any): string?
local magic = r:peek(4)
if magic ~= "RIFF" and magic ~= "RIFX" and magic ~= "RF64" and magic ~= "BW64" then
return nil
end
local form = r:peek(4, 8)
return if #form == 4 then form else nil
end
P_containers_Riff.sniff = Riff.sniff
end
local P_containers_Tar: any = {}
do -- containers/Tar: valid
local Tar = {}
local BLOCK = 512
local function zeros(b: buffer, o: number): boolean
for i = 0, BLOCK - 1, 4 do
if buffer.readu32(b, o + i) ~= 0 then
return false
end
end
return true
end
local function number(b: buffer, o: number, n: number): number?
local first = buffer.readu8(b, o)
if first >= 0x80 then
local v = first - (if first >= 0xC0 then 0x100 else 0x80) -- 0xFF.. = negative
for i = 1, n - 1 do
v = v * 256 + buffer.readu8(b, o + i)
end
return v
end
local s = buffer.readstring(b, o, n)
s = string.match(s, "^[ %z]*([0-7]*)") or ""
if s == "" then
return 0
end
return tonumber(s, 8)
end
local function checksum(b: buffer, o: number): (number, number)
local u, s = 0, 0
for i = 0, BLOCK - 1 do
local v = if i >= 148 and i < 156 then 32 else buffer.readu8(b, o + i)
u += v
s += if v >= 128 then v - 256 else v
end
return u, s
end
function Tar.valid(b: buffer, o: number): boolean
if o + BLOCK > buffer.len(b) or zeros(b, o) then
return false
end
local want = number(b, o + 148, 8)
local u, s = checksum(b, o)
return want == u or want == s
end
P_containers_Tar.valid = Tar.valid
end
local P_model_fbx_Binary: any = {}
do -- model/fbx/Binary: MAGIC
local Binary = {}
local MAGIC = "Kaydara FBX Binary \0"
Binary.MAGIC = MAGIC
P_model_fbx_Binary.MAGIC = Binary.MAGIC
end
local P_model_ms3d_Read: any = {}
do -- model/ms3d/Read: MAGIC
local Read = {}
Read.MAGIC = "MS3D000000"
P_model_ms3d_Read.MAGIC = Read.MAGIC
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 = P_codecs_audio_Aac
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 = P_codecs_audio_Ac3
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 = P_containers_IsoBmff
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 = P_codecs_audio_Mpa_Header
local Tags = P_audio_mp3_Tags
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 = P_codecs_audio_Musepack
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 = P_containers_Riff
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 = P_containers_IsoBmff
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 = P_codecs_text_Text
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 = P_codecs_compress_Deflate
local Zstd = P_codecs_compress_Zstd
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/fbx
local Bytes = require("@rosetta/core/Bytes")
local Binary = P_model_fbx_Binary
local MAGIC = Binary.MAGIC
S["model/fbx"] = function(r: Bytes.Reader): number
if r:peek(21) == MAGIC then
return 1
end
local head = r:peek(2048)
if string.find(head, "FBXHeaderExtension:", 1, true) then
return 0.95
end
if string.find(head, "^; FBX %d") then
return 0.8
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 = P_model_ms3d_Read
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 = P_codecs_text_Nbt
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 = P_codecs_compress_Brotli
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 = P_containers_Fat
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 = P_containers_Tar
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 = P_codecs_compress_Deflate
local Tar = P_containers_Tar
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 = P_codecs_compress_Zstd
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 = P_codecs_text_Nbt
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 = P_codecs_text_KeyValues
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 = P_codecs_audio_SoundFont
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 = P_codecs_text_Text
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