perf: F.detect: the extension's format scoring 1 returns at once (65x with a name), archive/br's trial-decode sniff runs last and only when it could still win; mpegts sniff finds sync bytes with string.find; inflate reuses its dynamic-table scratch buffers
This commit is contained in:
1 parent
11b7f89dab
commit
3320339507
3 files changed
+70
-25
No files matched your search
@@ -54,6 +54,9 @@ do
|
|||||||
FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP)
|
FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP)
|
||||||
end
|
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 Inflate = {}
|
||||||
|
|
||||||
local function grow(out: buffer, op: number, need: number): (buffer, number)
|
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
|
end
|
||||||
local pos = start
|
local pos = start
|
||||||
local bitbuf, bitcnt, over = 0, 0, 0
|
local bitbuf, bitcnt, over = 0, 0, 0
|
||||||
local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
local lt, dt = SLIT, SDIST
|
||||||
local clt = bcreate(CLCAP * 4)
|
local clt = SCL
|
||||||
local lens = bcreate(320)
|
local lens = SLENS
|
||||||
local final = 0
|
local final = 0
|
||||||
repeat
|
repeat
|
||||||
-- block header (3 bits); also up to 14 bits of a dynamic header
|
-- 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
|
lt, dt = FLIT, FDIST
|
||||||
lsub, dsub = FLITS, FDISTS
|
lsub, dsub = FLITS, FDISTS
|
||||||
elseif btype == 2 then
|
elseif btype == 2 then
|
||||||
if lt == FLIT then
|
lt, dt = SLIT, SDIST
|
||||||
lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
|
||||||
end
|
|
||||||
local hlit = band(bitbuf, 31) + 257
|
local hlit = band(bitbuf, 31) + 257
|
||||||
local hdist = band(rshift(bitbuf, 5), 31) + 1
|
local hdist = band(rshift(bitbuf, 5), 31) + 1
|
||||||
local hclen = band(rshift(bitbuf, 10), 15) + 4
|
local hclen = band(rshift(bitbuf, 10), 15) + 4
|
||||||
|
|||||||
+40
-4
@@ -76,6 +76,8 @@ local function find(key: string, category: string?): Registry.Entry?
|
|||||||
return nil
|
return nil
|
||||||
end
|
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?)
|
local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?)
|
||||||
if format then
|
if format then
|
||||||
local e = find(format, category)
|
local e = find(format, category)
|
||||||
@@ -86,18 +88,52 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
|
|||||||
end
|
end
|
||||||
local ext = Input.extension(name)
|
local ext = Input.extension(name)
|
||||||
local best: Registry.Entry? = nil
|
local best: Registry.Entry? = nil
|
||||||
local bestScore = 0
|
local bestScore, bestIdx = 0, 0
|
||||||
for _, e in Registry do
|
-- 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
|
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)
|
local m = module(e)
|
||||||
if m and m.sniff then
|
if m and m.sniff then
|
||||||
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
||||||
if ok and type(score) == "number" and score > 0 then
|
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
|
score += 0.01 -- ties go to the extension
|
||||||
end
|
end
|
||||||
if score > bestScore then
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -47,27 +47,35 @@ local CODEC: { [string]: string } = {
|
|||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ sniff
|
-- ------------------------------------------------------------------------------------------------ sniff
|
||||||
-- 188 or 192 (M2TS): three sync bytes a packet apart (fewer in a short file) within the first 2 KB
|
-- 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 function packetSize(r: Bytes.Reader): (number, number)
|
||||||
local n = r:remaining()
|
local n = r:remaining()
|
||||||
for start = 0, math.min(n, 2048) - 1 do
|
local head = r:peek(2048 + 2 * 192)
|
||||||
if r:peekU8(start) == 0x47 then
|
local lim = math.min(n, 2048)
|
||||||
for _, size in { 188, 192 } do
|
local start = 0
|
||||||
local hits, need = 0, math.min(3, (n - start - 1) // size + 1)
|
while start < lim do
|
||||||
for k = 0, 2 do
|
local f = string.find(head, "G", start + 1, true) -- 0x47
|
||||||
local at = start + k * size
|
if not f or f > lim then
|
||||||
if at < n and r:peekU8(at) == 0x47 then
|
break
|
||||||
hits += 1
|
end
|
||||||
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
|
end
|
||||||
if size == 192 and start >= 4 then
|
if hits >= need and (need >= 2 or start == 0) then
|
||||||
return 192, start - 4
|
if size == 192 and start >= 4 then
|
||||||
elseif size == 188 then
|
return 192, start - 4
|
||||||
return 188, start
|
elseif size == 188 then
|
||||||
end
|
return 188, start
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
start += 1
|
||||||
end
|
end
|
||||||
return 0, 0
|
return 0, 0
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in new issue
Block a user