comments: containers Avi / Zip
This commit is contained in:
1 parent
24cbddaafa
commit
9c096a6331
2 files changed
+11
-60
No files matched your search
@@ -1,16 +1,7 @@
|
||||
--!strict
|
||||
-- containers/Avi: the AVI RIFF form (Microsoft AVI 1.0 + the OpenDML 1.02 extensions). New in rosetta.
|
||||
-- Reading: hdrl (avih, strl: strh / strf / strn / indx), every RIFF piece (RIFF AVI + RIFF AVIX ...), and the chunk
|
||||
-- index of every stream: from the OpenDML super index (indx -> ix## standard / field indexes), else idx1 (offsets
|
||||
-- relative to the movi list or absolute, detected), else a linear scan of the movi lists (LIST rec included).
|
||||
-- Writing: AVI 1.0 (avih, strl, LIST movi, idx1) with an OpenDML odml / dmlh header, chunks interleaved as given.
|
||||
--
|
||||
-- local avi, err = Avi.read(buf, base, limit)
|
||||
-- avi.streams[i] = { type = "vids" | "auds" | ..., handler, scale, rate, start, length, sampleSize, strf
|
||||
-- (buffer), name, chunks = { { offset, size, key } } } -- offsets of the chunk data, absolute
|
||||
-- local w = Avi.writer({ streams = {...}, avih = {...} }); w:chunk(streamIndex, data, key); local file = w:finish()
|
||||
-- local bi = Avi.bitmapInfo(strf) -- a BITMAPINFOHEADER: size, depth, FourCC, palette (also Matroska VFW)
|
||||
--
|
||||
-- containers/Avi: the AVI RIFF form (Microsoft AVI 1.0 + OpenDML 1.02), read and written. New in rosetta. The chunk
|
||||
-- index comes from the OpenDML super index (indx -> ix##), else idx1 (movi-relative or absolute, detected), else a
|
||||
-- scan of the movi lists. Writing: AVI 1.0 (avih, strl, LIST movi, idx1) with an odml / dmlh header.
|
||||
-- Never throws: malformed data returns nil, Err (a damaged index falls back to scanning).
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
@@ -48,8 +39,6 @@ local function u32(b: buffer, o: number): number
|
||||
return readu32(b, o)
|
||||
end
|
||||
|
||||
-- chunk walker over [p, limit): calls fn(id, dataOffset, size, listType?) for each chunk (LIST bodies are not
|
||||
-- entered); returns where it stopped
|
||||
local function walk(b: buffer, p: number, limit: number, fn: (id: string, data: number, size: number, ltype: string?) -> boolean?): number
|
||||
while p + 8 <= limit do
|
||||
local id = readstring(b, p, 4)
|
||||
@@ -193,7 +182,6 @@ local function readIndex(b: buffer, d: number, n: number, s: Stream, limit: numb
|
||||
return false
|
||||
end
|
||||
|
||||
-- read(buf, base, limit) -> File | nil, Err
|
||||
function Avi.read(b: buffer, base: number, limit: number): (File?, Err.Err?)
|
||||
if limit - base < 12 or readstring(b, base, 4) ~= "RIFF" or readstring(b, base + 8, 4) ~= "AVI " then
|
||||
return nil, Err.new(Err.CORRUPT, "not an AVI file", base)
|
||||
@@ -342,7 +330,6 @@ function Avi.read(b: buffer, base: number, limit: number): (File?, Err.Err?)
|
||||
return file, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ writing
|
||||
export type WStream = {
|
||||
type: string, -- "vids" | "auds"
|
||||
handler: string, -- fccHandler (4 chars)
|
||||
@@ -364,7 +351,6 @@ export type WOptions = {
|
||||
software: string?,
|
||||
}
|
||||
|
||||
-- write(opts, chunks) -> buffer: chunks = { { stream (1-based), data: buffer, key: boolean } } in file order
|
||||
function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
local ns = #opts.streams
|
||||
local lengths = table.create(ns, 0)
|
||||
@@ -397,7 +383,6 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
local riffSize = w:tell()
|
||||
w:u32le(0)
|
||||
w:string("AVI ")
|
||||
-- hdrl
|
||||
w:string("LIST")
|
||||
local hdrlSize = w:tell()
|
||||
w:u32le(0)
|
||||
@@ -475,7 +460,6 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
w:u32le(4)
|
||||
w:u32le(frames)
|
||||
w:setU32le(hdrlSize, w:tell() - hdrlSize - 4)
|
||||
-- INFO
|
||||
local sw = (opts.software or "rosetta") .. "\0"
|
||||
if #sw % 2 == 1 then
|
||||
sw ..= "\0"
|
||||
@@ -486,7 +470,6 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
w:string("ISFT")
|
||||
w:u32le(#sw)
|
||||
w:string(sw)
|
||||
-- movi
|
||||
w:string("LIST")
|
||||
local moviSize = w:tell()
|
||||
w:u32le(0)
|
||||
@@ -508,7 +491,6 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
table.insert(index, { id, if key then 0x10 else 0, at - moviBase, n })
|
||||
end
|
||||
w:setU32le(moviSize, w:tell() - moviSize - 4)
|
||||
-- idx1
|
||||
w:string("idx1")
|
||||
w:u32le(#index * 16)
|
||||
for _, e in index do
|
||||
@@ -521,7 +503,6 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
return w:finish()
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ BITMAPINFO
|
||||
export type BitmapInfo = {
|
||||
width: number,
|
||||
height: number, -- signed: > 0 = bottom-up rows
|
||||
@@ -531,9 +512,8 @@ export type BitmapInfo = {
|
||||
extra: buffer?, -- the bytes after the 40-byte header (palette included), nil when none
|
||||
}
|
||||
|
||||
-- bitmapInfo(f) -> the BITMAPINFOHEADER facts of an AVI video strf (or a Matroska V_MS/VFW/FOURCC CodecPrivate) |
|
||||
-- nil when shorter than 40 bytes. The palette is the last (2^bits) * 4 bytes after the header (B, G, R, x entries;
|
||||
-- a trailing "BottomUp" tag is skipped), as the VfW codecs store it.
|
||||
-- BITMAPINFOHEADER: the palette is the last (2^bits) * 4 bytes after the header (B, G, R, x; a trailing "BottomUp"
|
||||
-- tag skipped), as the VfW codecs store it
|
||||
function Avi.bitmapInfo(f: buffer): BitmapInfo?
|
||||
local n = buffer.len(f)
|
||||
if n < 40 then
|
||||
|
||||
@@ -1,23 +1,9 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- containers/Zip: the ZIP container (PKWARE APPNOTE 6.3): a reader and a writer. The reader is absorbed from
|
||||
-- zip-luau 121d4b0 (same author, no third-party code): the end of central directory record (searched back over an
|
||||
-- archive comment), the central directory, ZIP64 (records, locator, extra fields), a prefix shift (self-extractor
|
||||
-- stubs), stored and deflated entries with a CRC-32 check, data descriptors, a wrong local header fallback,
|
||||
-- case-insensitive / basename lookup that skips __MACOSX. The DEFLATE codec is now required directly
|
||||
-- (codecs/compress/Deflate) instead of injected. The writer is new: stored / deflated entries (codecs/compress/Deflate compress, stored
|
||||
-- when that is not smaller), UTF-8 names, DOS times, raw copies of existing entries, ZIP64 when needed.
|
||||
--
|
||||
-- local zip, err = Zip.open(buf) -- buffer, string or core/Bytes Reader
|
||||
-- for _, e in zip:list() do print(e.name, e.size) end
|
||||
-- local data, err = zip:read("Main.bmp") -- exact, then case-insensitive, then by basename; CRC-checked
|
||||
--
|
||||
-- local w = Zip.writer()
|
||||
-- w:add("readme.txt", "hello", { level = 9 }) -- or { method = "store" }, mtime = unix seconds
|
||||
-- local file = w:finish({ comment = "..." })
|
||||
--
|
||||
-- Never throws: corrupt archives and bad arguments return nil, Err. Parsing runs interpreted (it is cold); the native
|
||||
-- code it reaches (Deflate, Crc32) does not throw either.
|
||||
-- containers/Zip: ZIP (PKWARE APPNOTE 6.3), read and written. The reader is absorbed from zip-luau 121d4b0 (same author,
|
||||
-- no third-party code): EOCD search, ZIP64, prefix shifts (self-extractor stubs), stored / deflated entries, data
|
||||
-- descriptors. The writer is new: stored / deflated, UTF-8 names, DOS times, raw entry copies, ZIP64 when needed.
|
||||
-- Never throws: corrupt archives and bad arguments return nil, Err.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
@@ -50,7 +36,6 @@ export type Entry = {
|
||||
cdExtraLen: number,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ read
|
||||
local readu8, readu16, readu32 = buffer.readu8, buffer.readu16, buffer.readu32
|
||||
|
||||
local SIG_LOCAL, SIG_CD, SIG_EOCD = 0x04034b50, 0x02014b50, 0x06054b50
|
||||
@@ -85,7 +70,6 @@ local Archive = {}
|
||||
Archive.__index = Archive
|
||||
|
||||
|
||||
-- isZip(buf) -> true when buf starts like a zip (a local header, or the end record of an empty archive)
|
||||
function Zip.isZip(buf: buffer | string): boolean
|
||||
local b = if type(buf) == "string" then buffer.fromstring(string.sub(buf, 1, 4)) else buf
|
||||
return sigAt(b, 0, SIG_LOCAL) or sigAt(b, 0, SIG_EOCD)
|
||||
@@ -253,8 +237,6 @@ local function parse(b: buffer): (any, Err.Err?)
|
||||
}, Archive)
|
||||
end
|
||||
|
||||
-- open(src) -> Archive | nil, Err. src: a buffer, a string, or a core/Bytes Reader (its range is copied when it is a
|
||||
-- part of a bigger buffer)
|
||||
function Zip.open(src: any): (any, Err.Err?)
|
||||
local b: buffer
|
||||
if type(src) == "string" then
|
||||
@@ -279,14 +261,11 @@ function Zip.open(src: any): (any, Err.Err?)
|
||||
return res, err
|
||||
end
|
||||
|
||||
-- every entry (directories included), in central directory order
|
||||
function Archive.list(self: any): { Entry }
|
||||
return self.entries
|
||||
end
|
||||
|
||||
-- find(name) -> entry | nil: the exact path, else the path case-insensitively, else the basename
|
||||
-- case-insensitively (skins sometimes nest everything in a folder). "\" and "/" are the same; directories and
|
||||
-- macOS "__MACOSX/" / "._" files never match by basename.
|
||||
-- lookup: exact, then case-insensitive, then by basename (never __MACOSX/ or ._ files); "\\" and "/" are the same
|
||||
function Archive.find(self: any, name: string): Entry?
|
||||
local n = normalize(name)
|
||||
local e = self._exact[n]
|
||||
@@ -327,8 +306,7 @@ local function extract(b: buffer, e: Entry, dataStart: number): (buffer?, any)
|
||||
return out
|
||||
end
|
||||
|
||||
-- the data offset candidates of an entry: the local header's own lengths, then the central directory's (some writers
|
||||
-- get the local extra length wrong)
|
||||
-- data offset candidates: the local header's lengths, then the central directory's (some writers get the local extra wrong)
|
||||
function Zip._starts(b: buffer, e: Entry): { number }
|
||||
local o = e.offset
|
||||
local starts = {}
|
||||
@@ -345,7 +323,6 @@ function Zip._starts(b: buffer, e: Entry): { number }
|
||||
return starts
|
||||
end
|
||||
|
||||
-- read(entry | name, verify = true) -> buffer | nil, Err
|
||||
function Archive.read(self: any, entry: Entry | string, verify: boolean?): (buffer?, Err.Err?)
|
||||
local e: Entry? = if type(entry) == "string" then self:find(entry) else entry :: Entry
|
||||
if not e then
|
||||
@@ -390,7 +367,6 @@ function Archive.read(self: any, entry: Entry | string, verify: boolean?): (buff
|
||||
return nil, Err.new(if firstErr and string.find(firstErr, "CRC", 1, true) then Err.CHECKSUM else code, e.name .. ": " .. tostring(firstErr))
|
||||
end
|
||||
|
||||
-- readString(entry | name, verify?) -> string | nil, Err
|
||||
function Archive.readString(self: any, entry: Entry | string, verify: boolean?): (string?, Err.Err?)
|
||||
local out, err = self:read(entry, verify)
|
||||
if not out then
|
||||
@@ -399,7 +375,6 @@ function Archive.readString(self: any, entry: Entry | string, verify: boolean?):
|
||||
return buffer.tostring(out), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ write
|
||||
local Writer = {}
|
||||
Writer.__index = Writer
|
||||
|
||||
@@ -416,7 +391,6 @@ export type FinishOptions = {
|
||||
zip64: boolean?, -- force the ZIP64 records (default: only when counts, sizes or offsets need them)
|
||||
}
|
||||
|
||||
-- writer() -> Writer
|
||||
function Zip.writer(): any
|
||||
return setmetatable({ files = {} :: { any } }, Writer)
|
||||
end
|
||||
@@ -463,7 +437,6 @@ local function isAscii(s: string): boolean
|
||||
return string.find(s, "[\128-\255]") == nil
|
||||
end
|
||||
|
||||
-- add(name, data, opts?) -> true | false, Err: compresses now (data: buffer or string)
|
||||
function Writer.add(self: any, name: string, data: any, opts: AddOptions?): (boolean, Err.Err?)
|
||||
if type(name) ~= "string" or name == "" or #name > 65535 then
|
||||
return false, Err.new(Err.ARG, "add: bad entry name")
|
||||
@@ -513,7 +486,6 @@ function Writer.add(self: any, name: string, data: any, opts: AddOptions?): (boo
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- addRaw(entry, archive): copy an entry of an opened archive without recompressing it -> true | false, Err
|
||||
function Writer.addRaw(self: any, e: Entry, archive: any, newName: string?): (boolean, Err.Err?)
|
||||
local b = archive.buf
|
||||
local start: number? = nil
|
||||
@@ -544,7 +516,6 @@ function Writer.addRaw(self: any, e: Entry, archive: any, newName: string?): (bo
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- finish(opts?) -> the archive | nil, Err
|
||||
function Writer.finish(self: any, opts: FinishOptions?): (buffer?, Err.Err?)
|
||||
local comment = opts and opts.comment or ""
|
||||
if #comment > 65535 then
|
||||
|
||||
Reference in new issue
Block a user