diff --git a/src/Rosetta/codecs/compress/Deflate/Inflate.luau b/src/Rosetta/codecs/compress/Deflate/Inflate.luau index 68534c7..6047d27 100644 --- a/src/Rosetta/codecs/compress/Deflate/Inflate.luau +++ b/src/Rosetta/codecs/compress/Deflate/Inflate.luau @@ -54,6 +54,9 @@ do FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP) end +-- scratch tables for dynamic blocks, shared by every call (raw never yields) +local SLIT, SDIST, SCL, SLENS = bcreate(LITCAP * 4), bcreate(DISTCAP * 4), bcreate(CLCAP * 4), bcreate(320) + local Inflate = {} local function grow(out: buffer, op: number, need: number): (buffer, number) @@ -75,9 +78,9 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex end local pos = start local bitbuf, bitcnt, over = 0, 0, 0 - local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer - local clt = bcreate(CLCAP * 4) - local lens = bcreate(320) + local lt, dt = SLIT, SDIST + local clt = SCL + local lens = SLENS local final = 0 repeat -- block header (3 bits); also up to 14 bits of a dynamic header @@ -146,9 +149,7 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex lt, dt = FLIT, FDIST lsub, dsub = FLITS, FDISTS elseif btype == 2 then - if lt == FLIT then - lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer - end + lt, dt = SLIT, SDIST local hlit = band(bitbuf, 31) + 257 local hdist = band(rshift(bitbuf, 5), 31) + 1 local hclen = band(rshift(bitbuf, 10), 15) + 4 diff --git a/src/Rosetta/init.luau b/src/Rosetta/init.luau index 8ffe385..6af74a1 100644 --- a/src/Rosetta/init.luau +++ b/src/Rosetta/init.luau @@ -76,6 +76,8 @@ local function find(key: string, category: string?): Registry.Entry? return nil end +local LATE: { [string]: number } = { br = 0.3 } -- archive/br: a trial decode of up to 256 KB + local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?) if format then local e = find(format, category) @@ -86,18 +88,52 @@ local function detect(buf: buffer, name: string?, category: string?, format: str end local ext = Input.extension(name) local best: Registry.Entry? = nil - local bestScore = 0 - for _, e in Registry do + local bestScore, bestIdx = 0, 0 + -- sniffs score 0..1 (core/Sniff); the extension's format scoring 1 cannot be beaten. Formats with a costly sniff + -- (LATE: the most they score) run last, and only when they could still win; ties go to registry order. + local late: { number }? = nil + for idx, e in Registry do if inCategory(e, category) and (e.caps.decode or e.caps.open) then + local cap = LATE[e.name] + if cap then + late = late or {} + table.insert(late :: { number }, idx) + continue + end local m = module(e) if m and m.sniff then local ok, score = pcall(m.sniff, Bytes.reader(buf)) if ok and type(score) == "number" and score > 0 then - if ext and table.find(e.extensions, ext) then + local extHit = ext and table.find(e.extensions, ext) + if extHit then score += 0.01 -- ties go to the extension end if score > bestScore then - best, bestScore = e, score + best, bestScore, bestIdx = e, score, idx + if extHit and score >= 1.01 then + return e, nil + end + end + end + end + end + end + if late then + for _, idx in late do + local e = Registry[idx] + local extHit = ext and table.find(e.extensions, ext) + local cap = LATE[e.name] + (if extHit then 0.01 else 0) + if cap > bestScore or (cap == bestScore and idx < bestIdx) then + local m = module(e) + if m and m.sniff then + local ok, score = pcall(m.sniff, Bytes.reader(buf)) + if ok and type(score) == "number" and score > 0 then + if extHit then + score += 0.01 + end + if score > bestScore or (score == bestScore and idx < bestIdx) then + best, bestScore, bestIdx = e, score, idx + end end end end diff --git a/src/Rosetta/video/mpegts/init.luau b/src/Rosetta/video/mpegts/init.luau index cabffea..5c0caea 100644 --- a/src/Rosetta/video/mpegts/init.luau +++ b/src/Rosetta/video/mpegts/init.luau @@ -47,27 +47,35 @@ local CODEC: { [string]: string } = { -- ------------------------------------------------------------------------------------------------ sniff -- 188 or 192 (M2TS): three sync bytes a packet apart (fewer in a short file) within the first 2 KB +local SIZES = { 188, 192 } local function packetSize(r: Bytes.Reader): (number, number) local n = r:remaining() - for start = 0, math.min(n, 2048) - 1 do - if r:peekU8(start) == 0x47 then - for _, size in { 188, 192 } 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 r:peekU8(at) == 0x47 then - hits += 1 - end + 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 - 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 + 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