comments: containers Archive / Demux / Ebml / MpegPs / Riff

This commit is contained in:
4DBug committed 2026-11-01 01:57:14 -05:00
1 parent e5a8e62fbc
commit a355bf3ca3
5 files changed
+6 -113

No files matched your search

+4 -30
View File
@@ -1,14 +1,6 @@
--!strict
-- containers/Archive: the in-memory Archive value (Types.Archive) that archive formats return. Entries hold their data
-- or a lazy loader (decompressed on read); add() appends or replaces an entry; write() calls the format's encoder
-- (attached by the format), so an archive read from a .tar writes back as a .tar, and any Archive (a zip, a 7z) can be
-- encoded by any writable archive format through its list() / read().
--
-- local a = Archive.new("tar", Tar.encode) -- encode(archive, opts) -> buffer | nil, Err
-- Archive.put(a, { name = "a.txt", size = 3, data = buf, mtime = 0 })
-- Archive.put(a, { name = "b.bin", size = n, load = function() return inflate(...) end })
-- a:list() a:read("a.txt") a:add("c.txt", "text") a:write()
--
-- containers/Archive: the in-memory Archive value (Types.Archive) archive formats return. Entries hold their data or
-- a lazy loader; write() calls the encoder the format attached, and any Archive can be encoded by any archive writer.
-- Single-stream archives (gzip, zst, xz, bz2, br) set meta.stream = true and hold one entry; the api unwraps them
-- (a .tar.gz loads as the tar inside). Never throws: read of a missing entry or a failed loader returns nil, Err.
local Err = require("@rosetta/core/Err")
@@ -34,7 +26,6 @@ export type Encoder = (archive: any, opts: any?) -> (buffer?, Err.Err?)
local Methods = {}
Methods.__index = Methods
-- the public fields of an entry (no data / loader)
local function public(e: Entry): Types.ArchiveEntry
local out: any = {}
for k, v in e :: any do
@@ -53,7 +44,6 @@ function Methods.list(self: any): { Types.ArchiveEntry }
return out
end
-- read(name) -> buffer | nil, Err (directories read as an empty buffer)
function Methods.read(self: any, name: string): (buffer?, Err.Err?)
local e: Entry? = self._byName[name]
if not e then
@@ -75,7 +65,6 @@ function Methods.read(self: any, name: string): (buffer?, Err.Err?)
return buffer.create(0), nil
end
-- add(name, data, opts?) -> (): opts.mtime (unix seconds), opts.dir, opts.mode; replaces an entry of the same name
function Methods.add(self: any, name: string, data: buffer | string, opts: any?)
local b: buffer = if type(data) == "string" then buffer.fromstring(data) elseif type(data) == "buffer" then data else buffer.create(0)
local o = opts or {}
@@ -90,7 +79,6 @@ function Methods.add(self: any, name: string, data: buffer | string, opts: any?)
Archive.put(self, e)
end
-- write(opts?) -> buffer | nil, Err: the archive's own format (opts.format is handled by the api)
function Methods.write(self: any, opts: any?): (buffer?, Err.Err?)
local enc: Encoder? = self._encode
if not enc then
@@ -99,7 +87,6 @@ function Methods.write(self: any, opts: any?): (buffer?, Err.Err?)
return enc(self, opts)
end
-- new(format, encode?) -> an empty Archive
function Archive.new(format: string, encode: Encoder?): any
return setmetatable({
kind = "archive",
@@ -111,7 +98,6 @@ function Archive.new(format: string, encode: Encoder?): any
}, Methods)
end
-- put(archive, entry): append (or replace by name) an entry
function Archive.put(a: any, e: Entry)
local old = a._byName[e.name]
if old then
@@ -125,8 +111,6 @@ function Archive.put(a: any, e: Entry)
a._byName[e.name] = e
end
-- entries(value) -> { { entry, read: () -> (buffer?, Err?) } } for any Archive-shaped value (ours or another
-- format's): what encoders iterate. nil when the value is not an archive.
function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () -> (buffer?, Err.Err?) } }?
if type(value) ~= "table" or value.kind ~= "archive" or type(value.list) ~= "function" or type(value.read) ~= "function" then
return nil
@@ -153,7 +137,6 @@ function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () ->
return out
end
-- slice(buf, at, n) -> a loader for Entry.load: a copy of buf[at, at + n) (stored entries of uncompressed archives)
function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err.Err?)
return function(): (buffer?, Err.Err?)
local out = buffer.create(n)
@@ -162,8 +145,6 @@ function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err.
end
end
-- collect(value) -> { { entry, data } } | nil, Err: every entry of an Archive-shaped value with its bytes read
-- (directories: data = an empty buffer), for writers of simple archives (PAK, WAD, GRP, VPK, ...)
function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buffer } }?, Err.Err?)
local items = Archive.entries(value)
if not items then
@@ -184,8 +165,6 @@ function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buff
return out, nil
end
-- files(value, name?) -> { { name, data, entry } } | nil, Err: the files (directories skipped) of any Archive value,
-- read; a buffer or a string is one file named name (default "data"). What single-file and encoding writers take.
function Archive.files(value: any, name: string?): ({ { name: string, data: buffer, entry: any } }?, Err.Err?)
if type(value) == "buffer" or type(value) == "string" then
local b = if type(value) == "string" then buffer.fromstring(value) else value
@@ -208,8 +187,6 @@ function Archive.files(value: any, name: string?): ({ { name: string, data: buff
return out, nil
end
-- streamData(value) -> what a single-stream encoder compresses: a buffer / string as it is, an Archive's one file, or
-- a tar of the Archive's entries (files, directories, symlinks with their mode / mtime) | nil, Err
function Archive.streamData(value: any): (buffer?, Err.Err?)
if type(value) == "buffer" then
return value, nil
@@ -240,8 +217,7 @@ function Archive.streamData(value: any): (buffer?, Err.Err?)
return Tar.write(list)
end
-- fromDos(stamp) -> unix seconds | nil: an MS-DOS date and time (stamp = date * 65536 + time, as LHA / ARJ / ZIP store
-- them little endian; local time without a zone, read as UTC); nil for a zero or invalid date
-- MS-DOS date / time: stamp = date * 65536 + time (LHA / ARJ / ZIP); local time without a zone, read as UTC
function Archive.fromDos(stamp: number): number?
local time, date = stamp % 65536, stamp // 65536
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
@@ -252,7 +228,7 @@ function Archive.fromDos(stamp: number): number?
return Types.Date.daysFromCivil(y, m, d) * 86400 + secs
end
-- toDos(unix?) -> date * 65536 + time (clamped to 1980-01-01 .. 2107-12-31)
-- MS-DOS dates cover 1980-01-01 .. 2107-12-31
function Archive.toDos(t: number?): number
if type(t) ~= "number" or t ~= t or t < 315532800 then
return 0x210000 -- 1980-01-01 00:00
@@ -267,8 +243,6 @@ function Archive.toDos(t: number?): number
return ((y - 1980) * 512 + m * 32 + d) * 65536 + (rem // 3600) * 2048 + ((rem // 60) % 60) * 32 + (rem % 60) // 2
end
-- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension
-- (".tgz" -> ".tar"), else "data"
function Archive.streamName(opts: any?, exts: { string }): string
local name = if type(opts) == "table" and type(opts.name) == "string" then opts.name else nil
if not name then