336 lines
8.5 KiB
Luau
336 lines
8.5 KiB
Luau
--!strict
|
|
-- containers/Ebml: EBML (RFC 8794), the binary structure under Matroska and WebM. New in formats-luau.
|
|
-- Reading: element headers (IDs keep their length marker bits, as the specs write them: 0x1A45DFA3; sizes drop it;
|
|
-- an all-ones size is "unknown"), children iteration, and the value types (unsigned / signed integers, floats,
|
|
-- strings, binary). Writing: a byte builder with nested master elements whose 8-byte sizes are patched on close, and
|
|
-- minimal-length value elements.
|
|
--
|
|
-- local id, size, data = Ebml.header(buf, pos, limit) -- size = -1: unknown; nil when malformed / truncated
|
|
-- for id, off, size in Ebml.children(buf, off, limit) do ... end
|
|
-- local w = Ebml.writer(); local m = w:open(0x18538067); w:uint(0xD7, 1); w:close(m); local out = w:finish()
|
|
--
|
|
-- Never throws: malformed input gives nil (readers) or stops iteration.
|
|
local Bytes = require("@formats/core/Bytes")
|
|
|
|
local readu8 = buffer.readu8
|
|
|
|
local Ebml = {}
|
|
|
|
Ebml.UNKNOWN = -1
|
|
|
|
-- vint(buf, pos, limit) -> value (marker removed), length | nil. All-ones values (unknown sizes) return -1.
|
|
function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?)
|
|
if pos >= limit then
|
|
return nil, nil
|
|
end
|
|
local b = readu8(buf, pos)
|
|
if b == 0 then
|
|
return nil, nil
|
|
end
|
|
local len = 1
|
|
local mask = 0x80
|
|
while bit32.band(b, mask) == 0 do
|
|
len += 1
|
|
mask = mask // 2
|
|
end
|
|
if pos + len > limit then
|
|
return nil, nil
|
|
end
|
|
local v = bit32.band(b, mask - 1)
|
|
local allOnes = v == mask - 1
|
|
for i = 1, len - 1 do
|
|
local c = readu8(buf, pos + i)
|
|
v = v * 256 + c
|
|
if c ~= 255 then
|
|
allOnes = false
|
|
end
|
|
end
|
|
if allOnes then
|
|
return -1, len
|
|
end
|
|
return v, len
|
|
end
|
|
|
|
-- svint(buf, pos, limit) -> a signed vint (EBML lacing deltas) | nil
|
|
function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?)
|
|
local v, len = Ebml.vint(buf, pos, limit)
|
|
if v == nil or len == nil then
|
|
return nil, nil
|
|
end
|
|
if v == -1 then
|
|
return nil, nil
|
|
end
|
|
return v - (2 ^ (7 * len - 1) - 1), len
|
|
end
|
|
|
|
-- id(buf, pos, limit) -> the element ID with its marker bits, length | nil (IDs are 1..4 bytes)
|
|
function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?)
|
|
if pos >= limit then
|
|
return nil, nil
|
|
end
|
|
local b = readu8(buf, pos)
|
|
local len = if b >= 0x80 then 1 elseif b >= 0x40 then 2 elseif b >= 0x20 then 3 elseif b >= 0x10 then 4 else 0
|
|
if len == 0 or pos + len > limit then
|
|
return nil, nil
|
|
end
|
|
local v = b
|
|
for i = 1, len - 1 do
|
|
v = v * 256 + readu8(buf, pos + i)
|
|
end
|
|
return v, len
|
|
end
|
|
|
|
-- header(buf, pos, limit) -> id, size (-1 = unknown), data offset | nil
|
|
function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number?, number?)
|
|
local id, il = Ebml.id(buf, pos, limit)
|
|
if not id or not il then
|
|
return nil, nil, nil
|
|
end
|
|
local size, sl = Ebml.vint(buf, pos + il, limit)
|
|
if not size or not sl then
|
|
return nil, nil, nil
|
|
end
|
|
return id, size, pos + il + sl
|
|
end
|
|
|
|
-- children(buf, off, limit) -> iterator of id, data offset, size (an unknown size runs to limit; a size past the
|
|
-- limit is clipped; a malformed header ends the iteration)
|
|
function Ebml.children(buf: buffer, off: number, limit: number): () -> (number?, number, number)
|
|
local pos = off
|
|
return function(): (number?, number, number)
|
|
if pos >= limit then
|
|
return nil, 0, 0
|
|
end
|
|
local id, size, data = Ebml.header(buf, pos, limit)
|
|
if not id or not size or not data then
|
|
pos = limit
|
|
return nil, 0, 0
|
|
end
|
|
if size < 0 or data + size > limit then
|
|
size = limit - data
|
|
end
|
|
pos = data + size
|
|
return id, data, size
|
|
end
|
|
end
|
|
|
|
-- uint(buf, off, len) / int / float / string: element values (0 / "" for empty elements)
|
|
function Ebml.uint(buf: buffer, off: number, len: number): number
|
|
local v = 0
|
|
for i = 0, math.min(len, 8) - 1 do
|
|
v = v * 256 + readu8(buf, off + i)
|
|
end
|
|
return v
|
|
end
|
|
function Ebml.int(buf: buffer, off: number, len: number): number
|
|
if len <= 0 then
|
|
return 0
|
|
end
|
|
local v = Ebml.uint(buf, off, len)
|
|
local n = math.min(len, 8)
|
|
if readu8(buf, off) >= 0x80 then
|
|
v -= 2 ^ (8 * n)
|
|
end
|
|
return v
|
|
end
|
|
function Ebml.float(buf: buffer, off: number, len: number): number
|
|
local r = Bytes.reader(buf, off, len)
|
|
if len == 4 then
|
|
return r:f32be()
|
|
elseif len == 8 then
|
|
return r:f64be()
|
|
end
|
|
return 0
|
|
end
|
|
function Ebml.string(buf: buffer, off: number, len: number): string
|
|
local s = buffer.readstring(buf, off, len)
|
|
local z = string.find(s, "\0", 1, true)
|
|
if z then
|
|
s = string.sub(s, 1, z - 1)
|
|
end
|
|
return s
|
|
end
|
|
function Ebml.binary(buf: buffer, off: number, len: number): buffer
|
|
local b = buffer.create(len)
|
|
buffer.copy(b, 0, buf, off, len)
|
|
return b
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ writing
|
|
local W = {}
|
|
W.__index = W
|
|
|
|
export type Writer = typeof(setmetatable({} :: { w: Bytes.Writer }, W))
|
|
|
|
-- writer(capacity?) -> an EBML byte builder
|
|
function Ebml.writer(capacity: number?): Writer
|
|
return setmetatable({ w = Bytes.writer(capacity or 65536) }, W)
|
|
end
|
|
|
|
-- idBytes(id) -> the ID's bytes as written
|
|
local function writeId(w: Bytes.Writer, id: number)
|
|
if id >= 0x1000000 then
|
|
w:u32be(id)
|
|
elseif id >= 0x10000 then
|
|
w:u24be(id)
|
|
elseif id >= 0x100 then
|
|
w:u16be(id)
|
|
else
|
|
w:u8(id)
|
|
end
|
|
end
|
|
|
|
-- size as a minimal vint (n bytes: force a length)
|
|
local function writeSize(w: Bytes.Writer, size: number, n: number?)
|
|
local len = n or 1
|
|
if not n then
|
|
while size >= 2 ^ (7 * len) - 1 do
|
|
len += 1
|
|
end
|
|
end
|
|
local v = size + 2 ^ (7 * len) -- the marker bit
|
|
local bytes = table.create(len, 0)
|
|
for i = len, 1, -1 do
|
|
bytes[i] = v % 256
|
|
v = v // 256
|
|
end
|
|
for i = 1, len do
|
|
w:u8(bytes[i])
|
|
end
|
|
end
|
|
Ebml.writeSize = writeSize
|
|
|
|
-- vintBytes(v, n?) -> the vint encoding of v as a string (lacing, block track numbers)
|
|
function Ebml.vintString(v: number, n: number?): string
|
|
local w = Bytes.writer(8)
|
|
writeSize(w, v, n)
|
|
return buffer.readstring(w:finish(), 0, w:tell())
|
|
end
|
|
|
|
function W.tell(self: Writer): number
|
|
return self.w:tell()
|
|
end
|
|
|
|
-- open(id) -> a handle for close(): writes the ID and an 8-byte size placeholder
|
|
function W.open(self: Writer, id: number): number
|
|
writeId(self.w, id)
|
|
local at = self.w:tell()
|
|
for _ = 1, 8 do
|
|
self.w:u8(0)
|
|
end
|
|
return at
|
|
end
|
|
|
|
-- close(handle): patches the size of the element opened at handle
|
|
function W.close(self: Writer, at: number)
|
|
local w = self.w
|
|
local v = w:tell() - at - 8
|
|
w:setU8(at, 0x01)
|
|
for i = 7, 1, -1 do
|
|
w:setU8(at + i, v % 256)
|
|
v = v // 256
|
|
end
|
|
end
|
|
|
|
-- unknownSize(id): a master element with an unknown size (live streams)
|
|
function W.openUnknown(self: Writer, id: number)
|
|
writeId(self.w, id)
|
|
self.w:u8(0x01)
|
|
for _ = 1, 7 do
|
|
self.w:u8(0xFF)
|
|
end
|
|
end
|
|
|
|
function W.uint(self: Writer, id: number, v: number)
|
|
local n = 1
|
|
while v >= 2 ^ (8 * n) and n < 8 do
|
|
n += 1
|
|
end
|
|
writeId(self.w, id)
|
|
writeSize(self.w, n)
|
|
local bytes = table.create(n, 0)
|
|
local x = v
|
|
for i = n, 1, -1 do
|
|
bytes[i] = x % 256
|
|
x = x // 256
|
|
end
|
|
for i = 1, n do
|
|
self.w:u8(bytes[i])
|
|
end
|
|
end
|
|
|
|
function W.int(self: Writer, id: number, v: number)
|
|
local n = 1
|
|
while (v < -(2 ^ (8 * n - 1)) or v >= 2 ^ (8 * n - 1)) and n < 8 do
|
|
n += 1
|
|
end
|
|
local u = if v < 0 then v + 2 ^ (8 * n) else v
|
|
writeId(self.w, id)
|
|
writeSize(self.w, n)
|
|
local bytes = table.create(n, 0)
|
|
for i = n, 1, -1 do
|
|
bytes[i] = u % 256
|
|
u = u // 256
|
|
end
|
|
for i = 1, n do
|
|
self.w:u8(bytes[i])
|
|
end
|
|
end
|
|
|
|
function W.float(self: Writer, id: number, v: number)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, 8)
|
|
self.w:f64be(v)
|
|
end
|
|
|
|
function W.string(self: Writer, id: number, s: string)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, #s)
|
|
self.w:string(s)
|
|
end
|
|
|
|
-- binary(id, buf, off?, len?)
|
|
function W.binary(self: Writer, id: number, b: buffer, off: number?, len: number?)
|
|
local o = off or 0
|
|
local n = len or (buffer.len(b) - o)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, n)
|
|
self.w:bytes(b, o, n)
|
|
end
|
|
|
|
-- raw bytes (a pre-built element, block headers)
|
|
function W.raw(self: Writer, s: string)
|
|
self.w:string(s)
|
|
end
|
|
function W.rawBuffer(self: Writer, b: buffer, off: number?, len: number?)
|
|
local o = off or 0
|
|
self.w:bytes(b, o, len or (buffer.len(b) - o))
|
|
end
|
|
-- element header for a binary element whose payload follows in pieces: id + size
|
|
function W.header(self: Writer, id: number, size: number)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, size)
|
|
end
|
|
|
|
-- void(n): an EBML Void element of exactly n bytes (n >= 2), for reserved space
|
|
function W.void(self: Writer, n: number)
|
|
self.w:u8(0xEC)
|
|
if n - 2 < 127 then
|
|
writeSize(self.w, n - 2, 1)
|
|
for _ = 1, n - 2 do
|
|
self.w:u8(0)
|
|
end
|
|
else
|
|
writeSize(self.w, n - 9, 8)
|
|
for _ = 1, n - 9 do
|
|
self.w:u8(0)
|
|
end
|
|
end
|
|
end
|
|
|
|
function W.finish(self: Writer): buffer
|
|
return self.w:finish()
|
|
end
|
|
|
|
return Ebml
|