merge ws/perf-core
This commit is contained in:
commit
de755ca036
37 files changed
+5317
-2562
No files matched your search
+43
-13
@@ -652,23 +652,47 @@ Types.null = table.freeze(setmetatable({}, nullMeta))
|
||||
-- Objects (maps) whose key order matters, and empty objects (which would otherwise read back as empty arrays), carry
|
||||
-- an "object" metatable with their key order. Decoders mark empty objects always and every object when asked
|
||||
-- (opts.ordered); encoders write an object's keys in that order (keys added later, or plain tables: sorted).
|
||||
local OBJECT = { __object = true }
|
||||
-- The key order is the __keys field of the object's own metatable, or (for a table that came with a metatable of its
|
||||
-- own) an entry in a weak map.
|
||||
local objectMeta: { [any]: { any } } = setmetatable({}, { __mode = "k" }) :: any -- object -> its key order
|
||||
local function orderOf(v: any): { any }?
|
||||
local mt: any = getmetatable(v)
|
||||
if type(mt) == "table" then
|
||||
local k: any = rawget(mt, "__keys")
|
||||
if k ~= nil then
|
||||
return k
|
||||
end
|
||||
end
|
||||
return objectMeta[v]
|
||||
end
|
||||
local NOKEYS: { any } = table.freeze({}) :: any
|
||||
function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any }
|
||||
local o: { [any]: any } = t or {}
|
||||
if getmetatable(o) == nil then
|
||||
setmetatable(o, OBJECT)
|
||||
local k: { any } = keys or NOKEYS
|
||||
local mt: any = getmetatable(o)
|
||||
if mt == nil or (type(mt) == "table" and rawget(mt, "__keys") ~= nil) then
|
||||
setmetatable(o, { __object = true, __keys = k })
|
||||
else
|
||||
objectMeta[o] = k
|
||||
end
|
||||
objectMeta[o] = keys or {}
|
||||
return o
|
||||
end
|
||||
function Types.isObject(v: any): boolean
|
||||
return type(v) == "table" and objectMeta[v] ~= nil
|
||||
return type(v) == "table" and orderOf(v) ~= nil
|
||||
end
|
||||
function Types.set(o: { [any]: any }, k: any, v: any)
|
||||
local order = objectMeta[o]
|
||||
local order = orderOf(o)
|
||||
if order and o[k] == nil and v ~= nil then
|
||||
table.insert(order, k)
|
||||
if order == NOKEYS then
|
||||
local mt: any = getmetatable(o)
|
||||
if type(mt) == "table" and rawget(mt, "__keys") == NOKEYS then
|
||||
mt.__keys = { k }
|
||||
else
|
||||
objectMeta[o] = { k }
|
||||
end
|
||||
else
|
||||
table.insert(order, k)
|
||||
end
|
||||
end
|
||||
o[k] = v
|
||||
end
|
||||
@@ -702,25 +726,31 @@ function Types.isArray(t: { [any]: any }, emptyIsMap: boolean?): boolean
|
||||
return c == n
|
||||
end
|
||||
function Types.keys(t: { [any]: any }): { any }
|
||||
local order = objectMeta[t]
|
||||
local order = orderOf(t)
|
||||
local out, seen = {}, {}
|
||||
local n = 0
|
||||
if order then
|
||||
for _, k in order do
|
||||
if t[k] ~= nil and not seen[k] then
|
||||
seen[k] = true
|
||||
table.insert(out, k)
|
||||
n += 1
|
||||
out[n] = k
|
||||
end
|
||||
end
|
||||
end
|
||||
local rest = {}
|
||||
local rest: { any }? = nil
|
||||
for k in next, t do
|
||||
if not seen[k] then
|
||||
table.insert(rest, k)
|
||||
if rest then
|
||||
table.insert(rest, k)
|
||||
else
|
||||
rest = { k }
|
||||
end
|
||||
end
|
||||
end
|
||||
if #rest > 0 then
|
||||
if rest then
|
||||
table.sort(rest, keyLess)
|
||||
table.move(rest, 1, #rest, #out + 1, out)
|
||||
table.move(rest, 1, #rest, n + 1, out)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
@@ -202,6 +202,13 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
local bb, bc = 0, 0
|
||||
|
||||
local function fill()
|
||||
if bc <= 24 and ip + 4 <= lim then
|
||||
bb = bor(bb, lshift(readu32(src, ip), bc))
|
||||
local nb = rshift(32 - bc, 3)
|
||||
ip += nb
|
||||
bc += nb * 8
|
||||
return
|
||||
end
|
||||
while bc <= 24 do
|
||||
local byte = 0
|
||||
if ip < lim then
|
||||
|
||||
@@ -11,9 +11,6 @@ local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buf
|
||||
|
||||
local Decode = {}
|
||||
|
||||
-- CRC-32/BZIP2 table (polynomial 0x04C11DB7, not reflected: core/Crc32)
|
||||
local CT = Crc32.MSB_TABLE
|
||||
Decode.crcTable = CT
|
||||
|
||||
local MASK = table.create(33, 0)
|
||||
for i = 0, 32 do
|
||||
@@ -413,8 +410,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
return nil, "block larger than its declared size", "corrupt"
|
||||
end
|
||||
local v = MTF[idx + 1]
|
||||
for k = idx, 1, -1 do
|
||||
MTF[k + 1] = MTF[k]
|
||||
if idx > 8 then
|
||||
table.move(MTF, 1, idx, 2)
|
||||
else
|
||||
for k = idx, 1, -1 do
|
||||
MTF[k + 1] = MTF[k]
|
||||
end
|
||||
end
|
||||
MTF[1] = v
|
||||
local ch = SEQ[v + 1]
|
||||
@@ -447,7 +448,7 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
out = grow(out, op, op + nblock + 4096)
|
||||
end
|
||||
local cap = blen(out)
|
||||
local crc = 0xFFFFFFFF
|
||||
local blockStart = op
|
||||
local tPos = rshift(readu32(T, origPtr * 4), 8)
|
||||
local last, run = -1, 0
|
||||
local i = 0
|
||||
@@ -468,9 +469,6 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
end
|
||||
bfill(out, op, last, ch)
|
||||
op += ch
|
||||
for _ = 1, ch do
|
||||
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), last) + 1])
|
||||
end
|
||||
end
|
||||
run = 0
|
||||
last = -1
|
||||
@@ -490,13 +488,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
||||
end
|
||||
writeu8(out, op, ch)
|
||||
op += 1
|
||||
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), ch) + 1])
|
||||
end
|
||||
end
|
||||
if op > maxBytes then
|
||||
return nil, "output larger than maxBytes", "limit"
|
||||
end
|
||||
crc = bxor(crc, 0xFFFFFFFF)
|
||||
local crc = bxor(Crc32.msb(out, blockStart, op - blockStart, 0xFFFFFFFF), 0xFFFFFFFF)
|
||||
if crc ~= storedCrc then
|
||||
return nil, string.format("block CRC mismatch (%08x, expected %08x)", crc, storedCrc), "checksum"
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
-- Written from the format description (no third-party code); randomised blocks (bzip2 0.9.0 and older) are unsupported.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
local Decode = require("@rosetta/codecs/compress/Bzip2/Decode")
|
||||
|
||||
local Bzip2 = {}
|
||||
@@ -33,14 +34,9 @@ function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?
|
||||
end
|
||||
|
||||
function Bzip2.crc(buf: buffer, offset: number?, length: number?): number
|
||||
local CT = Decode.crcTable
|
||||
local o = offset or 0
|
||||
local e = o + (length or (buffer.len(buf) - o))
|
||||
local c = 0xFFFFFFFF
|
||||
for i = o, e - 1 do
|
||||
c = bit32.bxor(bit32.lshift(c, 8), CT[bit32.bxor(bit32.rshift(c, 24), buffer.readu8(buf, i)) + 1])
|
||||
end
|
||||
return bit32.bxor(c, 0xFFFFFFFF)
|
||||
local n = length or (buffer.len(buf) - o)
|
||||
return bit32.bxor(Crc32.msb(buf, o, n, 0xFFFFFFFF), 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
return Bzip2
|
||||
@@ -5,7 +5,7 @@
|
||||
-- block the cheapest of stored / fixed / dynamic Huffman, as zlib's levels.
|
||||
local Huffman = require("@rosetta/codecs/entropy/Huffman")
|
||||
|
||||
local readu8, writeu8, writeu16 = buffer.readu8, buffer.writeu8, buffer.writeu16
|
||||
local readu8, readu32, writeu8, writeu16 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu16
|
||||
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
|
||||
local floor = math.floor
|
||||
|
||||
@@ -136,27 +136,72 @@ local function storedBlock(o: Out, src: buffer, s: number, e: number, final: boo
|
||||
end
|
||||
|
||||
local function emitSymbols(o: Out, L: { number }, D: { number }, nsym: number, lcode: { number }, llen: { number }, dcodes: { number }, dlen: { number })
|
||||
ensure(o, nsym * 6 + 8) -- at most 15 + 5 + 15 + 13 bits a symbol
|
||||
local buf, op, acc, cnt = o.buf, o.op, o.acc, o.cnt
|
||||
-- acc holds cnt < 16 pending bits; each field is at most 15 bits, so acc stays below 2^31
|
||||
for i = 1, nsym do
|
||||
local l = L[i]
|
||||
if l < 256 then
|
||||
put(o, lcode[l + 1], llen[l + 1])
|
||||
acc += lcode[l + 1] * POW[cnt + 1]
|
||||
cnt += llen[l + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
else
|
||||
local len = l - 256
|
||||
local c = LCODE[len]
|
||||
put(o, lcode[c + 1], llen[c + 1])
|
||||
acc += lcode[c + 1] * POW[cnt + 1]
|
||||
cnt += llen[c + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
local xb = LEXTRA[len]
|
||||
if xb > 0 then
|
||||
put(o, len - LBASEOF[len], xb)
|
||||
acc += (len - LBASEOF[len]) * POW[cnt + 1]
|
||||
cnt += xb
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
end
|
||||
local d = D[i]
|
||||
local dc = dcode(d)
|
||||
put(o, dcodes[dc + 1], dlen[dc + 1])
|
||||
local dc = if d <= 256 then DSMALL[d] else DLARGE[(d - 1) // 128 + 1]
|
||||
acc += dcodes[dc + 1] * POW[cnt + 1]
|
||||
cnt += dlen[dc + 1]
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
xb = DEXT[dc + 1]
|
||||
if xb > 0 then
|
||||
put(o, d - DBASE[dc + 1], xb)
|
||||
acc += (d - DBASE[dc + 1]) * POW[cnt + 1]
|
||||
cnt += xb
|
||||
if cnt >= 16 then
|
||||
writeu16(buf, op, acc % 65536)
|
||||
acc //= 65536
|
||||
op += 2
|
||||
cnt -= 16
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
while cnt >= 8 do
|
||||
writeu8(buf, op, acc % 256)
|
||||
acc //= 256
|
||||
op += 1
|
||||
cnt -= 8
|
||||
end
|
||||
o.op, o.acc, o.cnt = op, acc, cnt
|
||||
put(o, lcode[257], llen[257]) -- end of block
|
||||
end
|
||||
|
||||
@@ -319,11 +364,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local function hash(p: number): number
|
||||
return ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE
|
||||
end
|
||||
local function insert(p: number)
|
||||
local h = hash(p) + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
end
|
||||
local function find(p: number, steps: number): (number, number)
|
||||
local maxLen = math.min(MAXMATCH, e - p)
|
||||
if maxLen < 3 then
|
||||
@@ -332,9 +372,20 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local best, bestDist = 2, 0
|
||||
local c = head[hash(p) + 1]
|
||||
local limit = p - WSIZE
|
||||
while c >= 0 and c > limit and steps > 0 do
|
||||
if readu8(src, c + best) == readu8(src, p + best) and readu8(src, c) == readu8(src, p) then
|
||||
local p0, pb = readu8(src, p), readu8(src, p + 2)
|
||||
local max4 = maxLen - 4
|
||||
if limit < -1 then
|
||||
limit = -1
|
||||
end
|
||||
for _ = 1, steps do
|
||||
if c <= limit then
|
||||
break
|
||||
end
|
||||
if readu8(src, c + best) == pb and readu8(src, c) == p0 then
|
||||
local k = 1
|
||||
while k <= max4 and readu32(src, c + k) == readu32(src, p + k) do
|
||||
k += 4
|
||||
end
|
||||
while k < maxLen and readu8(src, c + k) == readu8(src, p + k) do
|
||||
k += 1
|
||||
end
|
||||
@@ -343,6 +394,7 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
if k >= nice or k >= maxLen then
|
||||
break
|
||||
end
|
||||
pb = readu8(src, p + k)
|
||||
end
|
||||
end
|
||||
local nc = prev[c % WSIZE + 1]
|
||||
@@ -350,7 +402,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
break
|
||||
end
|
||||
c = nc
|
||||
steps -= 1
|
||||
end
|
||||
if best < 3 then
|
||||
return 0, 0
|
||||
@@ -369,7 +420,9 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
local mlen, mdist = 0, 0
|
||||
if p <= stop3 then
|
||||
mlen, mdist = find(p, maxChain)
|
||||
insert(p)
|
||||
local h = ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
if mlen >= 3 and mlen < lazyMax and p + 1 <= stop3 then
|
||||
local l2, d2 = find(p + 1, if mlen >= good then maxChain // 4 else maxChain)
|
||||
if l2 > mlen then
|
||||
@@ -377,7 +430,9 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
L[nsym] = readu8(src, p)
|
||||
D[nsym] = 0
|
||||
p += 1
|
||||
insert(p)
|
||||
h = ((readu8(src, p) * 251 + readu8(src, p + 1)) * 251 + readu8(src, p + 2)) % HSIZE + 1
|
||||
prev[p % WSIZE + 1] = head[h]
|
||||
head[h] = p
|
||||
mlen, mdist = l2, d2
|
||||
end
|
||||
end
|
||||
@@ -388,9 +443,16 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
|
||||
D[nsym] = mdist
|
||||
local q = p + 1
|
||||
local qe = math.min(p + mlen, stop3 + 1)
|
||||
while q < qe do
|
||||
insert(q)
|
||||
q += 1
|
||||
if q < qe then
|
||||
local b1, b2 = readu8(src, q), readu8(src, q + 1)
|
||||
while q < qe do
|
||||
local b3 = readu8(src, q + 2)
|
||||
local h = ((b1 * 251 + b2) * 251 + b3) % HSIZE + 1
|
||||
prev[q % WSIZE + 1] = head[h]
|
||||
head[h] = q
|
||||
b1, b2 = b2, b3
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
p += mlen
|
||||
else
|
||||
|
||||
@@ -23,6 +23,19 @@ local DEXT = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
|
||||
-- RFC 1951 3.2.7: the order the code-length code lengths are sent in
|
||||
local CLORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }
|
||||
|
||||
-- table symbols: literals 0..255, end of block 256, invalid 257..511 (codes 286 / 287), lengths 512 + base * 8 + extra
|
||||
-- bits; distances base * 16 + extra bits
|
||||
local LMAP, DMAP = table.create(288, 300), table.create(30, 0)
|
||||
for i = 0, 256 do
|
||||
LMAP[i + 1] = i
|
||||
end
|
||||
for i = 1, 29 do
|
||||
LMAP[257 + i] = 512 + LBASE[i] * 8 + LEXT[i]
|
||||
end
|
||||
for i = 1, 30 do
|
||||
DMAP[i] = DBASE[i] * 16 + DEXT[i]
|
||||
end
|
||||
|
||||
local LITCAP = Huffman.capacity(288, LITBITS, 15)
|
||||
local DISTCAP = Huffman.capacity(32, DISTBITS, 15)
|
||||
local CLCAP = Huffman.capacity(19, CLBITS, 7)
|
||||
@@ -35,12 +48,15 @@ do
|
||||
bfill(l, 144, 9, 112)
|
||||
bfill(l, 256, 7, 24)
|
||||
bfill(l, 280, 8, 8)
|
||||
FLITS = Huffman.buildLsb(l, 0, 288, FLIT, LITBITS)
|
||||
FLITS = Huffman.buildLsbMap(l, 0, 288, FLIT, LITBITS, LMAP)
|
||||
local d = bcreate(30)
|
||||
bfill(d, 0, 5, 30)
|
||||
FDISTS = Huffman.buildLsb(d, 0, 30, FDIST, DISTBITS)
|
||||
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)
|
||||
@@ -62,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
|
||||
@@ -133,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
|
||||
@@ -240,8 +254,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
if readu8(lens, 256) == 0 then
|
||||
return nil, "missing end-of-block code"
|
||||
end
|
||||
lsub = Huffman.buildLsb(lens, 0, hlit, lt, LITBITS)
|
||||
dsub = Huffman.buildLsb(lens, hlit, hdist, dt, DISTBITS)
|
||||
lsub = Huffman.buildLsbMap(lens, 0, hlit, lt, LITBITS, LMAP)
|
||||
dsub = Huffman.buildLsbMap(lens, hlit, hdist, dt, DISTBITS, DMAP)
|
||||
if lsub < 0 or dsub < 0 then
|
||||
return nil, "invalid literal/length or distance code set"
|
||||
end
|
||||
@@ -299,13 +313,12 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
writeu8(out, op, sym)
|
||||
op += 1
|
||||
elseif sym == 256 then
|
||||
break
|
||||
else
|
||||
sym -= 256
|
||||
if sym > 29 then
|
||||
return nil, "invalid literal/length code"
|
||||
elseif sym < 512 then
|
||||
if sym == 256 then
|
||||
break
|
||||
end
|
||||
return nil, "invalid literal/length code"
|
||||
else
|
||||
if bitcnt < 20 then
|
||||
if pos + 4 <= endpos then
|
||||
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
||||
@@ -327,8 +340,8 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
end
|
||||
end
|
||||
local len = LBASE[sym]
|
||||
local xb = LEXT[sym]
|
||||
local len = rshift(sym, 3) - 64
|
||||
local xb = band(sym, 7)
|
||||
if xb > 0 then
|
||||
len += band(bitbuf, lshift(1, xb) - 1)
|
||||
bitbuf = rshift(bitbuf, xb)
|
||||
@@ -348,12 +361,9 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
bitbuf = rshift(bitbuf, l)
|
||||
bitcnt -= l
|
||||
local dsym = rshift(e, 5) + 1
|
||||
if dsym > 30 then
|
||||
return nil, "invalid distance code"
|
||||
end
|
||||
local dist = DBASE[dsym]
|
||||
xb = DEXT[dsym]
|
||||
local dsym = rshift(e, 5)
|
||||
local dist = rshift(dsym, 4)
|
||||
xb = band(dsym, 15)
|
||||
if xb > 0 then
|
||||
if bitcnt < 13 then
|
||||
if pos + 4 <= endpos then
|
||||
@@ -394,8 +404,11 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex
|
||||
end
|
||||
local s = op - dist
|
||||
if dist >= len then
|
||||
if len <= 16 and op + 16 <= cap then
|
||||
-- straight-line 16-byte copy for simple LuaJIT traces; it reads only bytes before op or already copied ones
|
||||
if len <= 8 and op + 8 <= cap then
|
||||
-- straight-line 8- / 16-byte copies for simple LuaJIT traces; they read only bytes before op or already copied ones
|
||||
writeu32(out, op, readu32(out, s))
|
||||
writeu32(out, op + 4, readu32(out, s + 4))
|
||||
elseif len <= 16 and op + 16 <= cap then
|
||||
writeu32(out, op, readu32(out, s))
|
||||
writeu32(out, op + 4, readu32(out, s + 4))
|
||||
writeu32(out, op + 8, readu32(out, s + 8))
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
-- codecs/compress/Lzma/Crc64: CRC-64/XZ (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42), xz check type 4.
|
||||
-- Written from the xz file format specification; the 64-bit value is kept as two u32 halves (hi, lo).
|
||||
local band, bxor, rshift, lshift, bor = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.bor
|
||||
local readu8 = buffer.readu8
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
local THI, TLO = table.create(256, 0), table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
@@ -21,14 +21,43 @@ for n = 0, 255 do
|
||||
THI[n + 1], TLO[n + 1] = hi, lo
|
||||
end
|
||||
|
||||
-- slice-by-4: H[k] / L[k][n] = the register after byte n and k zero bytes
|
||||
local H1, H2, H3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
local L1, L2, L3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
local hi, lo = THI[n + 1], TLO[n + 1]
|
||||
for k = 1, 3 do
|
||||
local idx = band(lo, 255) + 1
|
||||
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
|
||||
hi = bxor(THI[idx], rshift(hi, 8))
|
||||
if k == 1 then
|
||||
H1[n + 1], L1[n + 1] = hi, lo
|
||||
elseif k == 2 then
|
||||
H2[n + 1], L2[n + 1] = hi, lo
|
||||
else
|
||||
H3[n + 1], L3[n + 1] = hi, lo
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local Crc64 = {}
|
||||
|
||||
function Crc64.of(b: buffer, o: number, n: number): (number, number)
|
||||
local hi, lo = 0xFFFFFFFF, 0xFFFFFFFF
|
||||
for i = o, o + n - 1 do
|
||||
local i, e = o, o + n
|
||||
local e4 = e - 4
|
||||
while i <= e4 do
|
||||
local w = bxor(lo, readu32(b, i))
|
||||
local a, c, d, f = band(w, 255) + 1, band(rshift(w, 8), 255) + 1, band(rshift(w, 16), 255) + 1, rshift(w, 24) + 1
|
||||
lo = bxor(L3[a], L2[c], L1[d], TLO[f], hi)
|
||||
hi = bxor(H3[a], H2[c], H1[d], THI[f])
|
||||
i += 4
|
||||
end
|
||||
while i < e do
|
||||
local idx = band(bxor(lo, readu8(b, i)), 255) + 1
|
||||
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
|
||||
hi = bxor(THI[idx], rshift(hi, 8))
|
||||
i += 1
|
||||
end
|
||||
return bxor(hi, 0xFFFFFFFF), bxor(lo, 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
@@ -659,14 +659,24 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string
|
||||
local ml = ML_BASE[mlCode]
|
||||
local nb = ML_BITS[mlCode]
|
||||
if nb > 0 then
|
||||
ml += peek(b, s, off, nb)
|
||||
off -= nb
|
||||
local lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
ml += band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
ml += peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
end
|
||||
local ll = LL_BASE[llCode]
|
||||
nb = LL_BITS[llCode]
|
||||
if nb > 0 then
|
||||
ll += peek(b, s, off, nb)
|
||||
off -= nb
|
||||
local lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
ll += band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
ll += peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
end
|
||||
-- repeat offsets
|
||||
local offset
|
||||
@@ -699,15 +709,31 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string
|
||||
end
|
||||
-- state updates (not after the last sequence): LL, ML, OF
|
||||
if i < nbSeq then
|
||||
local lo
|
||||
nb = band(rshift(llE, 8), 255)
|
||||
llS = rshift(llE, 16) + peek(b, s, off, nb)
|
||||
off -= nb
|
||||
lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
llS = rshift(llE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
llS = rshift(llE, 16) + peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
nb = band(rshift(mlE, 8), 255)
|
||||
mlS = rshift(mlE, 16) + peek(b, s, off, nb)
|
||||
off -= nb
|
||||
lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
mlS = rshift(mlE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
mlS = rshift(mlE, 16) + peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
nb = band(rshift(ofE, 8), 255)
|
||||
ofS = rshift(ofE, 16) + peek(b, s, off, nb)
|
||||
off -= nb
|
||||
lo = off - nb
|
||||
if lo >= 0 and s + rshift(lo, 3) <= fastEnd then
|
||||
ofS = rshift(ofE, 16) + band(rshift(readu32(b, s + rshift(lo, 3)), band(lo, 7)), lshift(1, nb) - 1)
|
||||
else
|
||||
ofS = rshift(ofE, 16) + peek(b, s, off, nb)
|
||||
end
|
||||
off = lo
|
||||
end
|
||||
-- execute
|
||||
if lp + ll > litEnd then
|
||||
|
||||
@@ -38,7 +38,7 @@ end
|
||||
local count = table.create(MAXLEN + 1, 0)
|
||||
local nextc = table.create(MAXLEN + 1, 0)
|
||||
|
||||
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean): number
|
||||
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean, map: { number }?): number
|
||||
for i = 1, MAXLEN + 1 do
|
||||
count[i] = 0
|
||||
end
|
||||
@@ -76,7 +76,7 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
|
||||
if l > 0 then
|
||||
local c = nextc[l]
|
||||
nextc[l] = c + 1
|
||||
local entry = s * 32 + l
|
||||
local entry = (if map then map[s + 1] else s) * 32 + l
|
||||
if l <= P then
|
||||
if msb then
|
||||
local first = lshift(c, P - l)
|
||||
@@ -136,6 +136,11 @@ function Huffman.buildLsb(lens: buffer, off: number, n: number, tab: buffer, P:
|
||||
return build(lens, off, n, tab, P, false)
|
||||
end
|
||||
|
||||
-- map[s + 1] replaces symbol s in the entries (values < 2^27)
|
||||
function Huffman.buildLsbMap(lens: buffer, off: number, n: number, tab: buffer, P: number, map: { number }): number
|
||||
return build(lens, off, n, tab, P, false, map)
|
||||
end
|
||||
|
||||
function Huffman.buildMsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
|
||||
return build(lens, off, n, tab, P, true)
|
||||
end
|
||||
|
||||
@@ -29,6 +29,8 @@ local ESC: { [number]: string } = {
|
||||
[116] = "\t",
|
||||
}
|
||||
|
||||
local WS: { [number]: boolean } = { [32] = true, [9] = true, [10] = true, [13] = true }
|
||||
|
||||
local utf8char = Text.utf8char
|
||||
Decode.utf8char = utf8char
|
||||
|
||||
@@ -164,7 +166,9 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
local value: any
|
||||
|
||||
while true do
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
local c = byte(s, i)
|
||||
local haveValue = true
|
||||
if c == 123 then -- {
|
||||
@@ -172,7 +176,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if depth > maxDepth then
|
||||
return nil, "nested deeper than " .. maxDepth, i - 1
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
||||
i = i + 1
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
local t = {}
|
||||
c = byte(s, i)
|
||||
if c == 125 then
|
||||
@@ -184,7 +191,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if k == nil then
|
||||
return nil, ni, at
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
||||
i = ni
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if byte(s, i) ~= 58 then
|
||||
return nil, if i > n then "truncated object" else "expected ':'", i - 1
|
||||
end
|
||||
@@ -200,7 +210,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if depth > maxDepth then
|
||||
return nil, "nested deeper than " .. maxDepth, i - 1
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
||||
i = i + 1
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if byte(s, i) == 93 then
|
||||
i += 1
|
||||
depth -= 1
|
||||
@@ -256,10 +269,15 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
t[cnt] = value
|
||||
stackK[depth] = cnt
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
c = byte(s, i)
|
||||
if c == 44 then -- ,
|
||||
i = match(s, "^[ \t\n\r]*()", i + 1) :: any
|
||||
i = i + 1
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if ord then
|
||||
if byte(s, i) ~= 34 then
|
||||
return nil, if i > n then "truncated object" else "expected a key", i - 1
|
||||
@@ -268,7 +286,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
|
||||
if k == nil then
|
||||
return nil, ni, at
|
||||
end
|
||||
i = match(s, "^[ \t\n\r]*()", ni) :: any
|
||||
i = ni
|
||||
if WS[byte(s, i) or 0] then
|
||||
i = match(s, "^[ \t\n\r]*()", i) :: any
|
||||
end
|
||||
if byte(s, i) ~= 58 then
|
||||
return nil, if i > n then "truncated object" else "expected ':'", i - 1
|
||||
end
|
||||
|
||||
@@ -140,18 +140,23 @@ type State = {
|
||||
maxDepth: number,
|
||||
null: any,
|
||||
err: string?,
|
||||
nls: { string }, -- "\n" .. indent * (i - 1)
|
||||
}
|
||||
|
||||
local function put(st: State, s: string)
|
||||
local n = st.n + 1
|
||||
st.out[n] = s
|
||||
st.n = n
|
||||
local function newline(st: State, depth: number): string
|
||||
local r = "\n" .. string.rep(st.indent :: string, depth)
|
||||
st.nls[depth + 1] = r
|
||||
return r
|
||||
end
|
||||
|
||||
local function value(st: State, v: any, depth: number): boolean
|
||||
local tv = type(v)
|
||||
local out = st.out
|
||||
local n: number
|
||||
if tv == "string" then
|
||||
put(st, quote(v, st.ascii, st.slash))
|
||||
n = st.n + 1
|
||||
out[n] = quote(v, st.ascii, st.slash)
|
||||
st.n = n
|
||||
elseif tv == "number" then
|
||||
local s = number(v)
|
||||
if not s then
|
||||
@@ -161,14 +166,22 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
end
|
||||
s = "null"
|
||||
end
|
||||
put(st, s)
|
||||
n = st.n + 1
|
||||
out[n] = s
|
||||
st.n = n
|
||||
elseif tv == "boolean" then
|
||||
put(st, if v then "true" else "false")
|
||||
n = st.n + 1
|
||||
out[n] = if v then "true" else "false"
|
||||
st.n = n
|
||||
elseif v == nil or v == st.null or v == Types.null then
|
||||
put(st, "null")
|
||||
n = st.n + 1
|
||||
out[n] = "null"
|
||||
st.n = n
|
||||
elseif tv == "table" then
|
||||
if Types.Date.is(v) then
|
||||
put(st, '"' .. tostring(v) .. '"')
|
||||
n = st.n + 1
|
||||
out[n] = '"' .. tostring(v) .. '"'
|
||||
st.n = n
|
||||
return true
|
||||
end
|
||||
if depth >= st.maxDepth then
|
||||
@@ -178,31 +191,44 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
local ind = st.indent
|
||||
local nl, nl2 = "", ""
|
||||
if ind then
|
||||
nl = "\n" .. string.rep(ind, depth + 1)
|
||||
nl2 = "\n" .. string.rep(ind, depth)
|
||||
local nls = st.nls
|
||||
nl = nls[depth + 2] or newline(st, depth + 1)
|
||||
nl2 = nls[depth + 1] or newline(st, depth)
|
||||
end
|
||||
if kind(v, st.emptyObject) == "array" then
|
||||
local n = #v
|
||||
if n == 0 then
|
||||
put(st, "[]")
|
||||
local len = #v
|
||||
if len == 0 then
|
||||
n = st.n + 1
|
||||
out[n] = "[]"
|
||||
st.n = n
|
||||
return true
|
||||
end
|
||||
put(st, "[")
|
||||
for i = 1, n do
|
||||
n = st.n + 1
|
||||
out[n] = "["
|
||||
st.n = n
|
||||
for i = 1, len do
|
||||
if i > 1 then
|
||||
put(st, ",")
|
||||
n = st.n + 1
|
||||
out[n] = ","
|
||||
st.n = n
|
||||
end
|
||||
if ind then
|
||||
put(st, nl)
|
||||
n = st.n + 1
|
||||
out[n] = nl
|
||||
st.n = n
|
||||
end
|
||||
if not value(st, v[i], depth + 1) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
if ind then
|
||||
put(st, nl2)
|
||||
n = st.n + 1
|
||||
out[n] = nl2
|
||||
st.n = n
|
||||
end
|
||||
put(st, "]")
|
||||
n = st.n + 1
|
||||
out[n] = "]"
|
||||
st.n = n
|
||||
else
|
||||
local keys: { any }
|
||||
if Types.isObject(v) then
|
||||
@@ -222,17 +248,25 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
end
|
||||
end
|
||||
if #keys == 0 then
|
||||
put(st, "{}")
|
||||
n = st.n + 1
|
||||
out[n] = "{}"
|
||||
st.n = n
|
||||
return true
|
||||
end
|
||||
put(st, "{")
|
||||
n = st.n + 1
|
||||
out[n] = "{"
|
||||
st.n = n
|
||||
local sep = if ind then ": " else ":"
|
||||
for i, k in keys do
|
||||
if i > 1 then
|
||||
put(st, ",")
|
||||
n = st.n + 1
|
||||
out[n] = ","
|
||||
st.n = n
|
||||
end
|
||||
if ind then
|
||||
put(st, nl)
|
||||
n = st.n + 1
|
||||
out[n] = nl
|
||||
st.n = n
|
||||
end
|
||||
local ks: string
|
||||
if type(k) == "string" then
|
||||
@@ -243,16 +277,24 @@ local function value(st: State, v: any, depth: number): boolean
|
||||
st.err = "object key of type " .. type(k)
|
||||
return false
|
||||
end
|
||||
put(st, quote(ks, st.ascii, st.slash))
|
||||
put(st, sep)
|
||||
n = st.n + 1
|
||||
out[n] = quote(ks, st.ascii, st.slash)
|
||||
st.n = n
|
||||
n = st.n + 1
|
||||
out[n] = sep
|
||||
st.n = n
|
||||
if not value(st, v[k], depth + 1) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
if ind then
|
||||
put(st, nl2)
|
||||
n = st.n + 1
|
||||
out[n] = nl2
|
||||
st.n = n
|
||||
end
|
||||
put(st, "}")
|
||||
n = st.n + 1
|
||||
out[n] = "}"
|
||||
st.n = n
|
||||
end
|
||||
elseif tv == "buffer" then
|
||||
st.err = "cannot encode a buffer (encode it as base64 or an array first)"
|
||||
@@ -284,6 +326,7 @@ local function state(opts: Options?, nullv: any?): State
|
||||
maxDepth = o.maxDepth or 1000,
|
||||
null = nullv,
|
||||
err = nil,
|
||||
nls = {},
|
||||
}
|
||||
end
|
||||
Encode.state = state
|
||||
|
||||
@@ -18,6 +18,8 @@ export type Options = {
|
||||
}
|
||||
|
||||
local NAME = "^[%a_:\128-\255][%w_:%.%-\128-\255]*"
|
||||
local TEXT = "^([^<&%]%z\1-\8\11\12\14-\31]*)()<"
|
||||
local ATTR = '^[ \t\n]+([%a_:\128-\255][%w_:%.%-\128-\255]*)[ \t\n]*=[ \t\n]*"([^"<&%c]*)"()'
|
||||
local PREDEF: { [string]: string } = { lt = "<", gt = ">", amp = "&", quot = '"', apos = "'" }
|
||||
|
||||
local utf8char = Text.utf8char
|
||||
@@ -85,7 +87,7 @@ function Parse.toUtf8(s: string): (string?, string)
|
||||
if enc then
|
||||
local e = lower(enc)
|
||||
if e == "utf-8" or e == "utf8" or e == "us-ascii" or e == "ascii" then
|
||||
if find(s, "[\128-\255]") and not utf8.len(s) then
|
||||
if not utf8.len(s) then
|
||||
return nil, "invalid UTF-8"
|
||||
end
|
||||
return s, e
|
||||
@@ -100,7 +102,7 @@ function Parse.toUtf8(s: string): (string?, string)
|
||||
end
|
||||
return nil, "unsupported encoding " .. enc
|
||||
end
|
||||
if find(s, "[\128-\255]") and not utf8.len(s) then
|
||||
if not utf8.len(s) then
|
||||
return nil, "invalid UTF-8"
|
||||
end
|
||||
return s, "utf-8"
|
||||
@@ -494,11 +496,33 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
return nil
|
||||
end
|
||||
local c = byte(s, i)
|
||||
if c ~= 60 and depth > 0 and not keepWs and not preserve[depth] then
|
||||
local w = match(s, "^[ \t\n]*()", i) :: number
|
||||
local cw = byte(s, w)
|
||||
if cw == 60 then
|
||||
i = w
|
||||
c = 60
|
||||
elseif cw then
|
||||
-- text with a non-blank at w and no ']', '&' or control characters before the next '<'
|
||||
local raw, e = match(s, TEXT, i)
|
||||
if raw then
|
||||
i = e
|
||||
return "text", raw
|
||||
end
|
||||
end
|
||||
end
|
||||
if c ~= 60 then -- text
|
||||
local e = find(s, "<", i, true) or (n + 1)
|
||||
local raw = sub(s, i, e - 1)
|
||||
local start = i
|
||||
i = e
|
||||
if depth > 0 and not find(raw, "[%]&%z\1-\8\11\12\14-\31]") then
|
||||
-- no ']]>', control characters or references
|
||||
if keepWs or preserve[depth] or find(raw, "[^ \t\n]") then
|
||||
return "text", raw
|
||||
end
|
||||
continue
|
||||
end
|
||||
local wsOnly = not find(raw, "[^ \t\n]")
|
||||
if depth == 0 then
|
||||
if not wsOnly then
|
||||
@@ -524,6 +548,21 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
end
|
||||
local c2 = byte(s, i + 1)
|
||||
if c2 == 47 then -- </
|
||||
local top = stack[depth]
|
||||
if top then
|
||||
local tl = #top
|
||||
if byte(s, i + 2 + tl) == 62 and sub(s, i + 2, i + 1 + tl) == top then
|
||||
i += 3 + tl
|
||||
stack[depth] = nil
|
||||
nsStack[depth] = nil
|
||||
preserve[depth] = nil
|
||||
depth -= 1
|
||||
if depth == 0 then
|
||||
rootDone = true
|
||||
end
|
||||
return "end", top
|
||||
end
|
||||
end
|
||||
local name = match(s, NAME, i + 2)
|
||||
if not name then
|
||||
return fail("bad end tag", i)
|
||||
@@ -617,11 +656,35 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
end
|
||||
local at = i
|
||||
local p = i + 1 + #name
|
||||
local attrs = Types.object()
|
||||
local attrs = {}
|
||||
local order = {}
|
||||
local na = 0
|
||||
local closed = false
|
||||
local nsMap: { [string]: string }? = nil
|
||||
while true do
|
||||
if byte(s, p) == 62 then
|
||||
p += 1
|
||||
break
|
||||
end
|
||||
-- the common attribute: name="value" with no references, controls or '<' in the value
|
||||
local an, av, ae = match(s, ATTR, p)
|
||||
if an then
|
||||
if attrs[an] ~= nil then
|
||||
return fail("duplicate attribute " .. an, match(s, "^[ \t\n]*()", p) :: number)
|
||||
end
|
||||
attrs[an] = av
|
||||
na += 1
|
||||
order[na] = an
|
||||
if nsOn then
|
||||
if an == "xmlns" or sub(an, 1, 6) == "xmlns:" then
|
||||
local m: { [string]: string } = nsMap or {}
|
||||
nsMap = m
|
||||
m[if an == "xmlns" then "" else sub(an, 7)] = av
|
||||
end
|
||||
end
|
||||
p = ae
|
||||
continue
|
||||
end
|
||||
local q = match(s, "^[ \t\n]*()", p) :: any
|
||||
local cc = byte(s, q)
|
||||
if cc == 62 then
|
||||
@@ -675,7 +738,8 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
|
||||
return fail("duplicate attribute " .. an, q)
|
||||
end
|
||||
attrs[an] = v
|
||||
table.insert(order, an)
|
||||
na += 1
|
||||
order[na] = an
|
||||
if nsOn then
|
||||
if an == "xmlns" or sub(an, 1, 6) == "xmlns:" then
|
||||
local m: { [string]: string } = nsMap or {}
|
||||
|
||||
@@ -78,7 +78,16 @@ function Xml.decode(src: string | buffer, opts: DecodeOptions?): (Node?, Err.Err
|
||||
local cur: any = doc
|
||||
local stack = { doc }
|
||||
local depth = 1
|
||||
for ev, a, b, c in Xml.events(src, o) do
|
||||
local s, terr = text(src)
|
||||
local u, enc = nil, ""
|
||||
if s then
|
||||
u, enc = Parse.toUtf8(s)
|
||||
end
|
||||
if not u then
|
||||
return nil, terr or Err.new(if find(enc, "unsupported", 1, true) then Err.UNSUPPORTED else Err.CORRUPT, enc)
|
||||
end
|
||||
local str: string = u
|
||||
for ev, a, b, c in Parse.events(str, o) do
|
||||
if ev == "start" then
|
||||
local node: any = { name = a, attrs = b, children = {}, parent = if parents then cur else nil }
|
||||
if c then
|
||||
@@ -118,7 +127,7 @@ function Xml.decode(src: string | buffer, opts: DecodeOptions?): (Node?, Err.Err
|
||||
elseif ev == "doctype" then
|
||||
doc.doctype = { raw = a, name = b.name, publicId = b.publicId, systemId = b.systemId, entities = b.entities }
|
||||
elseif ev == "error" then
|
||||
return nil, a
|
||||
return nil, Err.new(errCode(a, b, #str), a, b)
|
||||
end
|
||||
end
|
||||
return doc, nil
|
||||
@@ -126,7 +135,7 @@ end
|
||||
|
||||
function Xml.element(name: string, attrs: { [string]: string }?, children: { any }?): Node
|
||||
local a = attrs or Types.object()
|
||||
if not Types.isObject(a) then
|
||||
if attrs and not Types.isObject(a) then
|
||||
local keys = {}
|
||||
for k in next, a do
|
||||
table.insert(keys, k)
|
||||
@@ -161,24 +170,27 @@ function Xml.setAttr(node: Node, name: string, value: string?)
|
||||
Types.set(node.attrs, name, value)
|
||||
end
|
||||
|
||||
-- name == nil: any element; a name without a prefix also matches prefixed names with that local part
|
||||
local function matches(cn: string, name: string?, plain: boolean): boolean
|
||||
return name == nil or cn == name or (plain and find(cn, ":", 1, true) ~= nil and match(cn, ":(.*)$") == name)
|
||||
end
|
||||
|
||||
function Xml.children(node: Node, name: string?): { Node }
|
||||
local out = {}
|
||||
local plain = name ~= nil and not find(name, ":", 1, true)
|
||||
for _, ch in node.children do
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
|
||||
if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
|
||||
table.insert(out, ch)
|
||||
end
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 and matches(ch.name, name, plain) then
|
||||
table.insert(out, ch)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Xml.child(node: Node, name: string?): Node?
|
||||
local plain = name ~= nil and not find(name, ":", 1, true)
|
||||
for _, ch in node.children do
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
|
||||
if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
|
||||
return ch
|
||||
end
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 and matches(ch.name, name, plain) then
|
||||
return ch
|
||||
end
|
||||
end
|
||||
return nil
|
||||
@@ -186,10 +198,11 @@ end
|
||||
|
||||
function Xml.descendants(node: Node, name: string?): { Node }
|
||||
local out = {}
|
||||
local plain = name ~= nil and not find(name, ":", 1, true)
|
||||
local function walk(n: Node)
|
||||
for _, ch in n.children do
|
||||
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
|
||||
if name == nil or ch.name == name or (not find(name :: string, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
|
||||
if matches(ch.name, name, plain) then
|
||||
table.insert(out, ch)
|
||||
end
|
||||
walk(ch)
|
||||
@@ -207,6 +220,13 @@ function Xml.text(node: any): string
|
||||
if node.name == "#cdata" then
|
||||
return node.text or ""
|
||||
end
|
||||
local kids = node.children
|
||||
local nk = #kids
|
||||
if nk == 0 then
|
||||
return ""
|
||||
elseif nk == 1 and type(kids[1]) == "string" then
|
||||
return kids[1]
|
||||
end
|
||||
local parts = {}
|
||||
local function walk(n: any)
|
||||
for _, ch in n.children do
|
||||
@@ -251,14 +271,32 @@ local TEXT_ESC = { ["&"] = "&", ["<"] = "<", [">"] = ">", ["\r"] = "&#
|
||||
local ATTR_ESC = { ["&"] = "&", ["<"] = "<", [">"] = ">", ['"'] = """, ["\t"] = "	", ["\n"] = " ", ["\r"] = " " }
|
||||
|
||||
function Xml.escape(s: string): string
|
||||
if not find(s, "[&<>\r]") then
|
||||
return s
|
||||
end
|
||||
return (gsub(s, "[&<>\r]", TEXT_ESC))
|
||||
end
|
||||
function Xml.escapeAttr(s: string): string
|
||||
if not find(s, '[&<>"\t\n\r]') then
|
||||
return s
|
||||
end
|
||||
return (gsub(s, '[&<>"\t\n\r]', ATTR_ESC))
|
||||
end
|
||||
|
||||
local goodNames: { [string]: boolean } = {}
|
||||
local nGood = 0
|
||||
local function badName(name: any): boolean
|
||||
return type(name) ~= "string" or not match(name, "^[%a_:\128-\255][%w_:%.%-\128-\255]*$")
|
||||
if goodNames[name] then
|
||||
return false
|
||||
end
|
||||
if type(name) ~= "string" or not match(name, "^[%a_:\128-\255][%w_:%.%-\128-\255]*$") then
|
||||
return true
|
||||
end
|
||||
if nGood < 4096 then
|
||||
goodNames[name] = true
|
||||
nGood += 1
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
@@ -270,6 +308,15 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
ind = o.indent
|
||||
end
|
||||
local selfClose = o.selfClose ~= false
|
||||
local indents: { string } = {}
|
||||
local function indent(depth: number): string
|
||||
local r = indents[depth + 1]
|
||||
if not r then
|
||||
r = "\n" .. string.rep(ind :: string, depth)
|
||||
indents[depth + 1] = r
|
||||
end
|
||||
return r
|
||||
end
|
||||
local out: { string } = {}
|
||||
local err: string? = nil
|
||||
local seen: { [any]: boolean } = {}
|
||||
@@ -324,7 +371,7 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
end
|
||||
table.insert(out, "<" .. name)
|
||||
local attrs = n.attrs or {}
|
||||
for _, k in Types.keys(attrs) do
|
||||
for _, k in (if next(attrs) == nil then attrs else Types.keys(attrs)) do
|
||||
local v = attrs[k]
|
||||
if badName(k) then
|
||||
err = "bad attribute name " .. tostring(k)
|
||||
@@ -355,12 +402,12 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
end
|
||||
for _, c in ch do
|
||||
if pretty then
|
||||
table.insert(out, "\n" .. string.rep(ind :: string, depth + 1))
|
||||
table.insert(out, indent(depth + 1))
|
||||
end
|
||||
write(c, depth + 1)
|
||||
end
|
||||
if pretty then
|
||||
table.insert(out, "\n" .. string.rep(ind :: string, depth))
|
||||
table.insert(out, indent(depth))
|
||||
end
|
||||
table.insert(out, "</" .. name .. ">")
|
||||
end
|
||||
|
||||
@@ -8,11 +8,9 @@
|
||||
-- of a multiplexed file come first when every stream's first packet is written (with flush) before any other.
|
||||
-- Never throws: a bad argument returns false, Err.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Page = require("@rosetta/containers/Ogg/Page")
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
|
||||
local CRC = Page.CRC_TABLE
|
||||
local readu8, writeu8, writeu32, writei32 = buffer.readu8, buffer.writeu8, buffer.writeu32, buffer.writei32
|
||||
local bxor, lshift, rshift = bit32.bxor, bit32.lshift, bit32.rshift
|
||||
local writeu8, writeu32, writei32 = buffer.writeu8, buffer.writeu32, buffer.writei32
|
||||
|
||||
local Mux = {}
|
||||
Mux.__index = Mux
|
||||
@@ -63,11 +61,7 @@ end
|
||||
|
||||
-- the CRC of a page (the crc field written as zero)
|
||||
local function crc(b: buffer, n: number): number
|
||||
local c = 0
|
||||
for i = 0, n - 1 do
|
||||
c = bxor(lshift(c, 8), CRC[bxor(rshift(c, 24), readu8(b, i)) + 1])
|
||||
end
|
||||
return c
|
||||
return Crc32.msb(b, 0, n, 0)
|
||||
end
|
||||
|
||||
function Stream._flush(self: any, eos: boolean)
|
||||
|
||||
@@ -32,10 +32,7 @@ local CRC = Crc32.MSB_TABLE
|
||||
Page.CRC_TABLE = CRC
|
||||
|
||||
local function crcRange(b: buffer, from: number, to: number, crc: number): number
|
||||
for i = from, to - 1 do
|
||||
crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1])
|
||||
end
|
||||
return crc
|
||||
return Crc32.msb(b, from, to - from, crc)
|
||||
end
|
||||
|
||||
-- the Ogg CRC-32 of arbitrary data (the crc field is not zeroed)
|
||||
|
||||
@@ -2,24 +2,38 @@
|
||||
--!native
|
||||
-- core/Adler32: Adler-32 (RFC 1950, the zlib checksum). Absorbed from deflate-luau src/Deflate/Checksum.luau (same author;
|
||||
-- no third-party code). Never throws: the range is clamped.
|
||||
local readu8 = buffer.readu8
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local function adler32(buf: buffer, off: number, len: number, adler: number?): number
|
||||
local a0 = adler or 1
|
||||
local a, b = a0 % 65536, a0 // 65536
|
||||
local i, e = off, off + len
|
||||
while i < e do
|
||||
-- zlib NMAX: the largest n with 255 n (n + 1) / 2 + (n + 1) (BASE - 1) < 2^32
|
||||
local stop = math.min(e, i + 5552)
|
||||
while i < stop do
|
||||
a += readu8(buf, i)
|
||||
b += a
|
||||
i += 1
|
||||
-- 88-byte blocks, two bytes per 16-bit lane of each u32: pl / ph sum bytes 0, 2 / 1, 3 of the words, ql / qh sum
|
||||
-- those prefix sums (each lane stays below 2^16 over 22 words). Byte t of word j (of m) weighs 4 (m - j) - t in b.
|
||||
local e88 = e - 88
|
||||
while i <= e88 do
|
||||
local pl, ph, ql, qh = 0, 0, 0, 0
|
||||
for j = i, i + 84, 4 do
|
||||
local w = readu32(buf, j)
|
||||
pl += band(w, 0x00FF00FF)
|
||||
ph += band(rshift(w, 8), 0x00FF00FF)
|
||||
ql += pl
|
||||
qh += ph
|
||||
end
|
||||
a %= 65521
|
||||
b %= 65521
|
||||
i += 88
|
||||
local s0, s2 = pl % 65536, pl // 65536
|
||||
local s1, s3 = ph % 65536, ph // 65536
|
||||
local q = ql % 65536 + ql // 65536 + qh % 65536 + qh // 65536
|
||||
b = (b + 88 * a + 4 * q - s1 - 2 * s2 - 3 * s3) % 65521
|
||||
a = (a + s0 + s1 + s2 + s3) % 65521
|
||||
end
|
||||
return b * 65536 + a
|
||||
while i < e do
|
||||
a += readu8(buf, i)
|
||||
b += a
|
||||
i += 1
|
||||
end
|
||||
return (b % 65521) * 65536 + a % 65521
|
||||
end
|
||||
|
||||
local function range(buf: buffer, offset: number?, length: number?): (number, number)
|
||||
|
||||
@@ -29,7 +29,19 @@ for _, c in { 9, 10, 11, 12, 13, 32 } do
|
||||
end
|
||||
buffer.writeu8(DEC, string.byte("="), 65)
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local readu8, writeu8, readu16, readu32, writeu16 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.readu32, buffer.writeu16
|
||||
local band, bor, rshift, byteswap = bit32.band, bit32.bor, bit32.rshift, bit32.byteswap
|
||||
local writeu32 = buffer.writeu32
|
||||
|
||||
-- two output characters per 12 input bits (u16, first character in the low byte)
|
||||
local function pairs12(alpha: string): buffer
|
||||
local t = buffer.create(8192)
|
||||
for i = 0, 4095 do
|
||||
buffer.writeu16(t, i * 2, string.byte(alpha, i // 64 + 1) + string.byte(alpha, i % 64 + 1) * 256)
|
||||
end
|
||||
return t
|
||||
end
|
||||
local ENC2, ENC2_URL = pairs12(ALPHA), pairs12(ALPHA_URL)
|
||||
|
||||
function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: number?, length: number?): string
|
||||
local buf: buffer = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
@@ -47,6 +59,24 @@ function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: numb
|
||||
local out = buffer.create(textLen + breaks)
|
||||
local p = 0
|
||||
local col = 0
|
||||
if wrap == 0 then
|
||||
local enc2 = if opts and opts.url then ENC2_URL else ENC2
|
||||
local fast = if o + full * 3 + 1 <= total then full else full - 1 -- readu32 reads one byte past the group
|
||||
for i = 0, fast - 1 do
|
||||
local v = rshift(byteswap(readu32(buf, o + i * 3)), 8)
|
||||
writeu16(out, p, readu16(enc2, rshift(v, 12) * 2))
|
||||
writeu16(out, p + 2, readu16(enc2, band(v, 4095) * 2))
|
||||
p += 4
|
||||
end
|
||||
for i = math.max(fast, 0), full - 1 do
|
||||
local q = o + i * 3
|
||||
local v = readu8(buf, q) * 65536 + readu8(buf, q + 1) * 256 + readu8(buf, q + 2)
|
||||
writeu16(out, p, readu16(enc2, rshift(v, 12) * 2))
|
||||
writeu16(out, p + 2, readu16(enc2, band(v, 4095) * 2))
|
||||
p += 4
|
||||
end
|
||||
full = 0
|
||||
end
|
||||
for i = 0, full - 1 do
|
||||
if wrap > 0 then
|
||||
if col == wrap then
|
||||
@@ -69,7 +99,7 @@ function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: numb
|
||||
writeu8(out, p, 10)
|
||||
p += 1
|
||||
end
|
||||
local q = o + full * 3
|
||||
local q = o + (n - rest)
|
||||
local v = readu8(buf, q) * 65536 + (if rest == 2 then readu8(buf, q + 1) * 256 else 0)
|
||||
writeu8(out, p, readu8(enc, v // 262144))
|
||||
writeu8(out, p + 1, readu8(enc, (v // 4096) % 64))
|
||||
@@ -85,13 +115,51 @@ function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: numb
|
||||
return buffer.tostring(out)
|
||||
end
|
||||
|
||||
-- 12 bits per two input characters (u16 index, first character in the low byte); 0xFFFF unless both are alphabet
|
||||
local DEC2: buffer? = nil
|
||||
local function pairDecoder(): buffer
|
||||
local t = buffer.create(131072)
|
||||
buffer.fill(t, 0, 255)
|
||||
for a = 0, 255 do
|
||||
local da = buffer.readu8(DEC, a)
|
||||
if da < 64 then
|
||||
for b = 0, 255 do
|
||||
local db = buffer.readu8(DEC, b)
|
||||
if db < 64 then
|
||||
buffer.writeu16(t, (a + b * 256) * 2, da * 64 + db)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
DEC2 = t
|
||||
return t
|
||||
end
|
||||
|
||||
function Base64.decode(s: string | buffer): (buffer?, string?)
|
||||
local src: buffer = if type(s) == "string" then buffer.fromstring(s) else s
|
||||
local n = buffer.len(src)
|
||||
local out = buffer.create((n // 4 + 1) * 3)
|
||||
local acc, bits, p = 0, 0, 0
|
||||
local padded = false
|
||||
for i = 0, n - 1 do
|
||||
local i = 0
|
||||
while i < n do
|
||||
if bits == 0 and not padded then
|
||||
-- whole groups of four alphabet characters; out has room for the fourth byte each u32 write adds
|
||||
local dec2 = DEC2 or pairDecoder()
|
||||
while i + 4 <= n do
|
||||
local w = readu32(src, i)
|
||||
local x, y = readu16(dec2, band(w, 65535) * 2), readu16(dec2, rshift(w, 16) * 2)
|
||||
if bor(x, y) >= 4096 then
|
||||
break
|
||||
end
|
||||
writeu32(out, p, rshift(byteswap(x * 4096 + y), 8))
|
||||
p += 3
|
||||
i += 4
|
||||
end
|
||||
if i >= n then
|
||||
break
|
||||
end
|
||||
end
|
||||
local d = readu8(DEC, readu8(src, i))
|
||||
if d < 64 then
|
||||
if padded then
|
||||
@@ -112,6 +180,7 @@ function Base64.decode(s: string | buffer): (buffer?, string?)
|
||||
elseif d ~= 64 then
|
||||
return nil, string.format("bad base64 character 0x%02x at %d", readu8(src, i), i)
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
if bits >= 6 then
|
||||
return nil, "truncated base64 (a single character in the last group)"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
--!native
|
||||
-- core/Crc32: CRC-32 (IEEE 802.3, the zlib / gzip / zip / PNG polynomial 0xEDB88320, reflected), slice-by-8.
|
||||
-- Absorbed from deflate-luau src/Deflate/Checksum.luau (same author; no third-party code). Never throws.
|
||||
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
|
||||
local band, bxor, rshift, lshift, byteswap = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.byteswap
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
local T0, T1, T2, T3 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
@@ -99,10 +99,49 @@ for n = 0, 255 do
|
||||
end
|
||||
Crc32.MSB_TABLE = MSB
|
||||
|
||||
-- slice-by-8 for the MSB-first CRC: M[k][n] = the CRC register after byte n and k zero bytes
|
||||
local M1, M2, M3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
local M4, M5, M6, M7 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
local c = MSB[n + 1]
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M1[n + 1] = c
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M2[n + 1] = c
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M3[n + 1] = c
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M4[n + 1] = c
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M5[n + 1] = c
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M6[n + 1] = c
|
||||
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||
M7[n + 1] = c
|
||||
end
|
||||
|
||||
function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number
|
||||
local c = init or 0
|
||||
for i = off, off + len - 1 do
|
||||
local i, e = off, off + len
|
||||
local e8 = e - 8
|
||||
while i <= e8 do
|
||||
c = bxor(c, byteswap(readu32(buf, i)))
|
||||
local d = byteswap(readu32(buf, i + 4))
|
||||
c = bxor(
|
||||
M7[rshift(c, 24) + 1],
|
||||
M6[band(rshift(c, 16), 255) + 1],
|
||||
M5[band(rshift(c, 8), 255) + 1],
|
||||
M4[band(c, 255) + 1],
|
||||
M3[rshift(d, 24) + 1],
|
||||
M2[band(rshift(d, 16), 255) + 1],
|
||||
M1[band(rshift(d, 8), 255) + 1],
|
||||
MSB[band(d, 255) + 1]
|
||||
)
|
||||
i += 8
|
||||
end
|
||||
while i < e do
|
||||
c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1])
|
||||
i += 1
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
+15
-16
@@ -51,22 +51,21 @@ local function compress(st: State, blk: buffer, o: number)
|
||||
M[i + 1] = readu32(blk, o + i * 4)
|
||||
end
|
||||
local a, b, c, d = st.a, st.b, st.c, st.d
|
||||
for i = 0, 63 do
|
||||
local f
|
||||
if i < 16 then
|
||||
f = bor(band(b, c), band(bnot(b), d))
|
||||
elseif i < 32 then
|
||||
f = bor(band(d, b), band(bnot(d), c))
|
||||
elseif i < 48 then
|
||||
f = bxor(b, c, d)
|
||||
else
|
||||
f = bxor(c, bor(b, bnot(d)))
|
||||
end
|
||||
local tmp = d
|
||||
d = c
|
||||
c = b
|
||||
b = (b + lrotate((a + f + K[i + 1] + M[G[i + 1] + 1]) % 4294967296, S[i + 1])) % 4294967296
|
||||
a = tmp
|
||||
for i = 1, 16 do
|
||||
local f = bor(band(b, c), band(bnot(b), d))
|
||||
a, b, c, d = d, (b + lrotate(a + f + K[i] + M[G[i] + 1], S[i])) % 4294967296, b, c
|
||||
end
|
||||
for i = 17, 32 do
|
||||
local f = bor(band(d, b), band(bnot(d), c))
|
||||
a, b, c, d = d, (b + lrotate(a + f + K[i] + M[G[i] + 1], S[i])) % 4294967296, b, c
|
||||
end
|
||||
for i = 33, 48 do
|
||||
local f = bxor(b, c, d)
|
||||
a, b, c, d = d, (b + lrotate(a + f + K[i] + M[G[i] + 1], S[i])) % 4294967296, b, c
|
||||
end
|
||||
for i = 49, 64 do
|
||||
local f = bxor(c, bor(b, bnot(d)))
|
||||
a, b, c, d = d, (b + lrotate(a + f + K[i] + M[G[i] + 1], S[i])) % 4294967296, b, c
|
||||
end
|
||||
st.a = (st.a + a) % 4294967296
|
||||
st.b = (st.b + b) % 4294967296
|
||||
|
||||
+40
-4
@@ -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
|
||||
|
||||
@@ -151,11 +151,12 @@ function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
|
||||
return m
|
||||
end
|
||||
|
||||
local NUM_FORMATS = { "%.6g", "%.9g", "%.17g" }
|
||||
local function num(v: number): string
|
||||
if v == math.floor(v) and math.abs(v) < 1e15 then
|
||||
return string.format("%d", v)
|
||||
end
|
||||
for _, f in { "%.6g", "%.9g", "%.17g" } do
|
||||
for _, f in NUM_FORMATS do
|
||||
local s = string.format(f, v)
|
||||
if tonumber(s) == v then
|
||||
return s
|
||||
|
||||
+122
-46
@@ -89,6 +89,11 @@ type Prim = {
|
||||
}
|
||||
type Section = { object: string?, group: string?, prims: { Prim }, byKey: { [string]: Prim }, smoothing: string? }
|
||||
|
||||
local TOK = "[ \t]+([^%s#\\]+)"
|
||||
local V2 = "^" .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||
local V3 = "^" .. TOK .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||
local V4 = "^" .. TOK .. TOK .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||
|
||||
function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local triangulate = not (opts and opts.triangulate == false)
|
||||
local flipV = not (opts and opts.flipV == false)
|
||||
@@ -164,7 +169,117 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
return vi, ti, ni
|
||||
end
|
||||
local fs: { string } = {}
|
||||
local SV, ST, SN = {}, {}, {}
|
||||
local function element(key: string, k: number): Err.Err?
|
||||
local mode = if key == "f" then (if triangulate then "triangles" else "polygons") elseif key == "l" then "lines" else "points"
|
||||
if (key == "f" and k < 3) or (key == "l" and k < 2) or k < 1 then
|
||||
return Err.new(Err.CORRUPT, "an element with too few corners on line " .. lineNo)
|
||||
end
|
||||
corners += k
|
||||
if corners > maxV * 4 then
|
||||
return Err.new(Err.LIMIT, "too many face corners")
|
||||
end
|
||||
local p = prim(mode)
|
||||
for i = 1, k do
|
||||
local tok = fs[i]
|
||||
local vi, ti, ni = corner(tok)
|
||||
if not vi then
|
||||
return Err.new(Err.CORRUPT, "bad index '" .. tok .. "' on line " .. lineNo)
|
||||
end
|
||||
SV[i], ST[i], SN[i] = vi, ti, ni
|
||||
if ti >= 0 then
|
||||
p.hasT = true
|
||||
end
|
||||
if ni >= 0 then
|
||||
p.hasN = true
|
||||
end
|
||||
end
|
||||
local cv, ct, cn = p.cv, p.ct, p.cn
|
||||
local o = #cv
|
||||
if mode == "triangles" then
|
||||
for a = 2, k - 1 do
|
||||
cv[o + 1], ct[o + 1], cn[o + 1] = SV[1], ST[1], SN[1]
|
||||
cv[o + 2], ct[o + 2], cn[o + 2] = SV[a], ST[a], SN[a]
|
||||
cv[o + 3], ct[o + 3], cn[o + 3] = SV[a + 1], ST[a + 1], SN[a + 1]
|
||||
o += 3
|
||||
end
|
||||
elseif mode == "lines" then
|
||||
for a = 1, k - 1 do
|
||||
cv[o + 1], ct[o + 1], cn[o + 1] = SV[a], ST[a], SN[a]
|
||||
cv[o + 2], ct[o + 2], cn[o + 2] = SV[a + 1], ST[a + 1], SN[a + 1]
|
||||
o += 2
|
||||
end
|
||||
else
|
||||
for i = 1, k do
|
||||
cv[o + i], ct[o + i], cn[o + i] = SV[i], ST[i], SN[i]
|
||||
end
|
||||
if mode == "polygons" then
|
||||
table.insert(p.faces, k)
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
while pos <= len do
|
||||
-- plain "v x y z", "vt u v", "vn x y z" and three- / four-corner "f" lines; anything else takes the general path
|
||||
local c0 = string.byte(text, pos)
|
||||
if c0 == 118 then -- v
|
||||
local c1 = string.byte(text, pos + 1)
|
||||
if c1 == 32 or c1 == 9 then
|
||||
local x, y, z, ne = string.match(text, V3, pos + 1)
|
||||
if x then
|
||||
local fx, fy, fz = tonumber(x), tonumber(y), tonumber(z)
|
||||
if fx and fy and fz and np < maxV then
|
||||
P[np * 3 + 1], P[np * 3 + 2], P[np * 3 + 3] = fx, fy, fz
|
||||
np += 1
|
||||
lineNo += 1
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
elseif c1 == 110 then -- vn
|
||||
local x, y, z, ne = string.match(text, V3, pos + 2)
|
||||
if x then
|
||||
local fx, fy, fz = tonumber(x), tonumber(y), tonumber(z)
|
||||
if fx and fy and fz and nn < maxV then
|
||||
N[nn * 3 + 1], N[nn * 3 + 2], N[nn * 3 + 3] = fx, fy, fz
|
||||
nn += 1
|
||||
lineNo += 1
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
elseif c1 == 116 then -- vt
|
||||
local u, v, ne = string.match(text, V2, pos + 2)
|
||||
if u then
|
||||
local fu, fv = tonumber(u), tonumber(v)
|
||||
if fu and fv and nt < maxV then
|
||||
UV[nt * 2 + 1], UV[nt * 2 + 2] = fu, if flipV then 1 - fv else fv
|
||||
nt += 1
|
||||
lineNo += 1
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif c0 == 102 then -- f
|
||||
local a, b, c, ne = string.match(text, V3, pos + 1)
|
||||
local k = 3
|
||||
local d = nil
|
||||
if not a then
|
||||
a, b, c, d, ne = string.match(text, V4, pos + 1)
|
||||
k = 4
|
||||
end
|
||||
if a then
|
||||
fs[1], fs[2], fs[3], fs[4] = a, b, c, d
|
||||
lineNo += 1
|
||||
local err = element("f", k)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
local e = string.find(text, "\n", pos, true) or (len + 1)
|
||||
local line = string.sub(text, pos, e - 1)
|
||||
pos = e + 1
|
||||
@@ -229,50 +344,9 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
for tok in string.gmatch(rest, "%S+") do
|
||||
table.insert(fs, tok)
|
||||
end
|
||||
local k = #fs
|
||||
local mode = if key == "f" then (if triangulate then "triangles" else "polygons") elseif key == "l" then "lines" else "points"
|
||||
if (key == "f" and k < 3) or (key == "l" and k < 2) or k < 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "an element with too few corners on line " .. lineNo)
|
||||
end
|
||||
corners += k
|
||||
if corners > maxV * 4 then
|
||||
return nil, Err.new(Err.LIMIT, "too many face corners")
|
||||
end
|
||||
local p = prim(mode)
|
||||
local base = #p.cv
|
||||
for i, tok in fs do
|
||||
local vi, ti, ni = corner(tok)
|
||||
if not vi then
|
||||
return nil, Err.new(Err.CORRUPT, "bad index '" .. tok .. "' on line " .. lineNo)
|
||||
end
|
||||
p.cv[base + i], p.ct[base + i], p.cn[base + i] = vi, ti, ni
|
||||
if ti >= 0 then
|
||||
p.hasT = true
|
||||
end
|
||||
if ni >= 0 then
|
||||
p.hasN = true
|
||||
end
|
||||
end
|
||||
if mode == "triangles" and k > 3 then
|
||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
||||
local o = base
|
||||
for a = 2, k - 1 do
|
||||
for _, j in { 1, a, a + 1 } do
|
||||
o += 1
|
||||
p.cv[o], p.ct[o], p.cn[o] = cv[j], ct[j], cn[j]
|
||||
end
|
||||
end
|
||||
elseif mode == "lines" then
|
||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
||||
local o = base
|
||||
for a = 1, k - 1 do
|
||||
for _, j in { a, a + 1 } do
|
||||
o += 1
|
||||
p.cv[o], p.ct[o], p.cn[o] = cv[j], ct[j], cn[j]
|
||||
end
|
||||
end
|
||||
elseif mode == "polygons" then
|
||||
table.insert(p.faces, k)
|
||||
local err = element(key, #fs)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
elseif key == "o" then
|
||||
object = rest
|
||||
@@ -471,6 +545,7 @@ end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local probe = buffer.create(4)
|
||||
local F32_FORMATS = { "%.6g", "%.7g", "%.8g" }
|
||||
local function f32str(v: number): string
|
||||
if v ~= v or v == math.huge or v == -math.huge then
|
||||
return "0"
|
||||
@@ -480,7 +555,7 @@ local function f32str(v: number): string
|
||||
end
|
||||
buffer.writef32(probe, 0, v)
|
||||
local want = buffer.readf32(probe, 0)
|
||||
for _, f in { "%.6g", "%.7g", "%.8g" } do
|
||||
for _, f in F32_FORMATS do
|
||||
local s = string.format(f, v)
|
||||
buffer.writef32(probe, 0, tonumber(s) :: number)
|
||||
if buffer.readf32(probe, 0) == want then
|
||||
@@ -658,7 +733,8 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
local mode = prim.mode or "triangles"
|
||||
if mode == "triangles" then
|
||||
for t = 0, count // 3 - 1 do
|
||||
face({ at(t * 3), at(t * 3 + 1), at(t * 3 + 2) })
|
||||
local a, b, c = ref(at(t * 3)), ref(at(t * 3 + 1)), ref(at(t * 3 + 2))
|
||||
table.insert(out, if mirror then "f " .. c .. " " .. b .. " " .. a else "f " .. a .. " " .. b .. " " .. c)
|
||||
end
|
||||
elseif mode == "polygons" then
|
||||
local F = if prim.faces then Types.Scene.reader(prim.faces) else nil
|
||||
|
||||
@@ -244,12 +244,13 @@ function Rbxm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err
|
||||
local vals, msg = Binary.decode(t, body, q + 1, be, n, ctx)
|
||||
if vals then
|
||||
local tname = Rbx.TYPE_NAMES[t]
|
||||
for i, ref in cls.refs do
|
||||
local inst = byRef[ref]
|
||||
if pname == "Name" and t == 0x01 then
|
||||
inst.name = vals[i]
|
||||
else
|
||||
inst.properties[pname] = { type = tname, value = vals[i] }
|
||||
if pname == "Name" and t == 0x01 then
|
||||
for i, ref in cls.refs do
|
||||
byRef[ref].name = vals[i]
|
||||
end
|
||||
else
|
||||
for i, ref in cls.refs do
|
||||
byRef[ref].properties[pname] = { type = tname, value = vals[i] }
|
||||
end
|
||||
end
|
||||
elseif msg == "unknown" then
|
||||
@@ -322,7 +323,7 @@ local function columnType(insts: { Rbx.Instance }, pname: string): (number?, str
|
||||
local id, tname = nil, nil
|
||||
for _, inst in insts do
|
||||
local v = inst.properties[pname]
|
||||
if v then
|
||||
if v and (type(v) ~= "table" or tname == nil or v.type ~= tname) then
|
||||
if type(v) ~= "table" or type(v.type) ~= "string" then
|
||||
return nil, "property " .. pname .. " of " .. inst.className .. " is not a { type, value } table"
|
||||
end
|
||||
|
||||
@@ -81,6 +81,10 @@ local function num(s: string?): number
|
||||
if s == nil then
|
||||
return 0
|
||||
end
|
||||
local v = tonumber(s)
|
||||
if v then
|
||||
return v
|
||||
end
|
||||
local t = string.lower((string.gsub(s, "^%s+", "")))
|
||||
t = string.gsub(t, "%s+$", "")
|
||||
if t == "inf" or t == "+inf" or t == "infinity" then
|
||||
@@ -100,8 +104,27 @@ end
|
||||
local function comps(node: Xml.Node?, names: { string }): { number }
|
||||
local out = table.create(#names, 0)
|
||||
if node then
|
||||
-- the children in order, element i usually named names[i]
|
||||
local kids = node.children
|
||||
local k = 1
|
||||
for i, n in names do
|
||||
out[i] = num(childText(node, n))
|
||||
local c = nil
|
||||
while k <= #kids do
|
||||
local ch = kids[k]
|
||||
if type(ch) == "table" then
|
||||
if ch.name == n then
|
||||
c = ch
|
||||
end
|
||||
break
|
||||
end
|
||||
k += 1
|
||||
end
|
||||
if c then
|
||||
k += 1
|
||||
else
|
||||
c = Xml.child(node, n)
|
||||
end
|
||||
out[i] = num(if c then Xml.text(c) else nil)
|
||||
end
|
||||
end
|
||||
return out
|
||||
@@ -117,10 +140,10 @@ end
|
||||
local CF_NAMES = { "X", "Y", "Z", "R00", "R01", "R02", "R10", "R11", "R12", "R20", "R21", "R22" }
|
||||
local PHYS_NAMES = { "Density", "Friction", "Elasticity", "FrictionWeight", "ElasticityWeight" }
|
||||
|
||||
local function readRaw(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
|
||||
local function readRaw(node: Xml.Node, shared: { [string]: string }, cls: string, pname: string): (string, any)
|
||||
local tag = node.name
|
||||
local scalar = SCALAR_TAGS[tag]
|
||||
if tag == "int" and BRICKCOLOR[key] then
|
||||
if tag == "int" and BRICKCOLOR[cls .. "." .. pname] then
|
||||
scalar = "BrickColor"
|
||||
end
|
||||
if scalar then
|
||||
@@ -264,8 +287,8 @@ local function round32(v: any, which: any): any
|
||||
end
|
||||
return v
|
||||
end
|
||||
local function readValue(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
|
||||
local t, v = readRaw(node, shared, key)
|
||||
local function readValue(node: Xml.Node, shared: { [string]: string }, cls: string, pname: string): (string, any)
|
||||
local t, v = readRaw(node, shared, cls, pname)
|
||||
local which = F32[t]
|
||||
if which then
|
||||
v = round32(v, which)
|
||||
@@ -316,7 +339,7 @@ function Rbxmx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Er
|
||||
if type(pnode) == "table" and string.sub(pnode.name, 1, 1) ~= "#" then
|
||||
local pname = pnode.attrs.name
|
||||
if type(pname) == "string" then
|
||||
local t, v = readValue(pnode, shared, cls .. "." .. pname)
|
||||
local t, v = readValue(pnode, shared, cls, pname)
|
||||
if pname == "Name" and (t == "String" or t == "ProtectedString") then
|
||||
inst.name = v
|
||||
else
|
||||
|
||||
@@ -438,11 +438,16 @@ function MeshOps.computeNormals(prim: Primitive, opts: { flat: boolean? }?): (Pr
|
||||
local e2x, e2y, e2z = readf32(pd, c * 12) - ax, readf32(pd, c * 12 + 4) - ay, readf32(pd, c * 12 + 8) - az
|
||||
-- the cross product is twice the area times the unit normal: area weighting for free
|
||||
local nx, ny, nz = e1y * e2z - e1z * e2y, e1z * e2x - e1x * e2z, e1x * e2y - e1y * e2x
|
||||
for _, v in { a, b, c } do
|
||||
acc[v * 3 + 1] += nx
|
||||
acc[v * 3 + 2] += ny
|
||||
acc[v * 3 + 3] += nz
|
||||
end
|
||||
a, b, c = a * 3, b * 3, c * 3
|
||||
acc[a + 1] += nx
|
||||
acc[a + 2] += ny
|
||||
acc[a + 3] += nz
|
||||
acc[b + 1] += nx
|
||||
acc[b + 2] += ny
|
||||
acc[b + 3] += nz
|
||||
acc[c + 1] += nx
|
||||
acc[c + 2] += ny
|
||||
acc[c + 3] += nz
|
||||
end
|
||||
local out = buffer.create(nv * 12)
|
||||
for v = 0, nv - 1 do
|
||||
@@ -500,14 +505,25 @@ function MeshOps.computeTangents(prim: Primitive, opts: { texCoord: number? }?):
|
||||
local r = 1 / den
|
||||
local tx, ty, tz = (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r
|
||||
local bx, by, bz = (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r
|
||||
for _, v in { a, b, c } do
|
||||
tan[v * 3 + 1] += tx
|
||||
tan[v * 3 + 2] += ty
|
||||
tan[v * 3 + 3] += tz
|
||||
bit[v * 3 + 1] += bx
|
||||
bit[v * 3 + 2] += by
|
||||
bit[v * 3 + 3] += bz
|
||||
end
|
||||
local a3, b3, c3 = a * 3, b * 3, c * 3
|
||||
tan[a3 + 1] += tx
|
||||
tan[a3 + 2] += ty
|
||||
tan[a3 + 3] += tz
|
||||
bit[a3 + 1] += bx
|
||||
bit[a3 + 2] += by
|
||||
bit[a3 + 3] += bz
|
||||
tan[b3 + 1] += tx
|
||||
tan[b3 + 2] += ty
|
||||
tan[b3 + 3] += tz
|
||||
bit[b3 + 1] += bx
|
||||
bit[b3 + 2] += by
|
||||
bit[b3 + 3] += bz
|
||||
tan[c3 + 1] += tx
|
||||
tan[c3 + 2] += ty
|
||||
tan[c3 + 3] += tz
|
||||
bit[c3 + 1] += bx
|
||||
bit[c3 + 2] += by
|
||||
bit[c3 + 3] += bz
|
||||
end
|
||||
end
|
||||
local out = buffer.create(nv * 16)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user