merge ws/cm-formats-archivecad
This commit is contained in:
commit
c4609a5f90
40 files changed
+194
-533
No files matched your search
@@ -1,8 +1,5 @@
|
||||
--!strict
|
||||
-- archive/7z/Header: the 7z header parser (7zFormat.txt from the LZMA SDK / 7-Zip, Igor Pavlov, public domain):
|
||||
-- streams info (pack info, folders with their coders and bind pairs, substreams), files info (names, empty streams /
|
||||
-- files, anti items, times, attributes). Not --!native: a parse failure raises an Err value that parse() catches and
|
||||
-- returns; any other error is a bug and propagates (the tests' fuzz pass sees it).
|
||||
-- archive/7z/Header: the 7z header parser (7zFormat.txt, LZMA SDK, Igor Pavlov, public domain): streams, files info.
|
||||
local Text = require("@rosetta/codecs/text/Text")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
@@ -505,8 +502,6 @@ local function header(r: Bytes.Reader): Parsed
|
||||
return p
|
||||
end
|
||||
|
||||
-- parse(buf, offset, length) -> Parsed | nil, Err. An encoded header returns { encoded = streams } for the caller to
|
||||
-- decode and parse again.
|
||||
function Header.parse(b: buffer, o: number, n: number): (Parsed?, Err.Err?)
|
||||
local r = Bytes.reader(b, o, n)
|
||||
local ok, res = pcall(function(): Parsed
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
--!strict
|
||||
-- archive/7z: 7z archives (7zFormat.txt from the LZMA SDK, Igor Pavlov, public domain), read as an Archive whose
|
||||
-- entries decode lazily (a folder is decoded once on first read, then sliced). Supports: plain and encoded (compressed)
|
||||
-- headers, solid and non-solid folders, coder chains of copy / LZMA / LZMA2 / Deflate / BZip2 /
|
||||
-- Zstd (7-Zip-zstd's plain frames; untested: stock 7-Zip cannot write it) with BCJ (x86, PowerPC, ARM,
|
||||
-- ARM-Thumb, SPARC, ARM64) and delta filters, empty files and directories, anti items (skipped), UTF-16 names, mtimes,
|
||||
-- Windows / Unix attributes, folder and file CRC-32s (verified on read).
|
||||
-- Not supported (Err.UNSUPPORTED naming the method, on read of the affected entries): PPMd, BCJ2, Deflate64, other
|
||||
-- plug-in codecs (Brotli, LZ4, ...), AES encryption, multi-volume archives, external header references; writing.
|
||||
-- archive/7z: 7z (7zFormat.txt, LZMA SDK, public domain). Supports: encoded headers, solid folders, copy / LZMA /
|
||||
-- LZMA2 / Deflate / BZip2 / Zstd (7-Zip-zstd frames) with BCJ and delta filters, anti items (skipped), CRC-32s.
|
||||
-- Not supported: PPMd, BCJ2, Deflate64, other plug-in codecs, AES, multi-volume, external header references, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -31,8 +26,8 @@ local SevenZip = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a folder (solid block) or header that decodes to more than this (default 2^30)
|
||||
checkCrc: boolean?, -- verify CRC-32s on read (default true)
|
||||
maxBytes: number?, -- default 2^30, per folder or header
|
||||
checkCrc: boolean?, -- default true
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -99,7 +94,6 @@ local function packStart(ctx: Ctx, s: Header.Streams, index: number): number
|
||||
end
|
||||
|
||||
local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buffer?, Err.Err?)
|
||||
-- coder -> first in / out stream index
|
||||
local inBase, outBase = {}, {}
|
||||
local ni, no = 0, 0
|
||||
for i, c in f.coders do
|
||||
@@ -130,7 +124,6 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf
|
||||
if size > ctx.maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, string.format("folder of %d bytes is over maxBytes", size))
|
||||
end
|
||||
-- the coder's input: another coder's output, or a packed stream
|
||||
local inIdx = inBase[ci]
|
||||
local src: buffer, so: number, sl: number
|
||||
local bound = nil
|
||||
@@ -292,7 +285,6 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?
|
||||
local s: Header.Streams = parsed.streams or { packPos = 0, packSizes = {}, folders = {}, subSizes = {}, subCrcs = {} }
|
||||
a.meta.folders = #s.folders
|
||||
a.meta.solid = false
|
||||
-- folder caches and the substream -> folder map
|
||||
local cache: { [number]: buffer } = {}
|
||||
local subFolder: { number }, subOffset: { number } = {}, {}
|
||||
do
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
--!strict
|
||||
-- archive/adf: Amiga disk images (.adf: 880 KB double density, 1.76 MB high density, and other sizes holding an
|
||||
-- AmigaDOS volume without a partition table) read as an Archive: the boot block's DOS type (OFS / FFS, international,
|
||||
-- directory cache: meta.fs / meta.intl / meta.dircache), the root block (volume name, dates), directories through
|
||||
-- their 72-slot hash tables and hash chains, files through their header and extension block tables (FFS) or the
|
||||
-- OFS data block chain, soft links (entry.link) and hard links (entry.hardlink = the path linked to; a hard-linked
|
||||
-- file reads its target's data). Protection bits (entry.protect, AmigaDOS order: bit 0 = delete forbidden ...),
|
||||
-- comments (entry.comment) and dates (days / minutes / ticks since 1978-01-01, as unix seconds) are mapped; names are
|
||||
-- ISO-8859-1. Header block checksums are verified (Err.CHECKSUM). Written from the AmigaDOS file system description
|
||||
-- (the ADF-FAQ by Laurent Clévy). Not supported: writing, long-name file systems (DOS\6 / DOS\7), RDB hard-disk
|
||||
-- images (use the partition contents), dircache blocks (not needed to read).
|
||||
-- archive/adf: Amiga disk images (AmigaDOS OFS / FFS, intl, dircache; ADF-FAQ by Laurent Clévy): 72-slot hash tables,
|
||||
-- extension blocks / OFS chains, soft and hard links, protection bits, comments, dates since 1978-01-01, ISO-8859-1
|
||||
-- names, header checksums. Not supported: writing, DOS\6 / DOS\7, RDB hard-disk images, dircache blocks.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -89,7 +82,6 @@ local function date(d: Disk, blk: number, off: number): number
|
||||
return (u32(d, blk, off) * 86400 + u32(d, blk, off + 4) * 60 + u32(d, blk, off + 8) // 50) + EPOCH
|
||||
end
|
||||
|
||||
-- the data blocks of a file header (FFS: the header and extension tables; OFS uses the same tables)
|
||||
local function dataBlocks(d: Disk, hdr: number, maxBlocks: number): ({ number }?, string?)
|
||||
local out = {}
|
||||
local blk = hdr
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
--!strict
|
||||
-- archive/applesingle: AppleSingle (magic 0x00051600) and AppleDouble (0x00051607) files, versions 1 and 2 (RFC 1740):
|
||||
-- the entry table with the data fork (1), resource fork (2), real name (3), comment (4), file dates (8, version 2:
|
||||
-- seconds since 2000-01-01; version 1's file info (7) as Mac dates) and Finder info (9: type, creator, flags).
|
||||
-- Decodes to an Archive in the containers/MacFile convention (the data fork is the entry `name`, a resource fork the
|
||||
-- entry `name.rsrc`, with macType / macCreator / finderFlags / created / mtime / comment); the name is the real name
|
||||
-- entry, else the file name without the "._" AppleDouble prefix (opts.name), else "data". An AppleDouble file holds
|
||||
-- no data fork: only its resource fork entry is listed, and meta.double = true. Writes version 2 AppleSingle (or AppleDouble with
|
||||
-- opts.double) from any Archive's first Mac file or a buffer. Written from RFC 1740. Not supported: icon, ProDOS /
|
||||
-- MS-DOS / AFP info entries (kept in meta.entries as id + length).
|
||||
-- archive/applesingle: AppleSingle (0x00051600) / AppleDouble (0x00051607) v1 and v2 (RFC 1740): data fork (1),
|
||||
-- resource fork (2), real name (3), comment (4), dates (8; v1 file info 7), Finder info (9), as containers/MacFile.
|
||||
-- Supports: read, write v2. Not supported: icon, ProDOS / MS-DOS / AFP info entries (kept in meta.entries).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -25,10 +19,10 @@ local AppleSingle = {
|
||||
caps = { decode = true, encode = true, forks = true, variants = { "applesingle", "appledouble" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = { name: string?, maxBytes: number? } -- name: the file name (the api passes it)
|
||||
export type DecodeOptions = { name: string?, maxBytes: number? }
|
||||
export type EncodeOptions = {
|
||||
double: boolean?, -- write AppleDouble (no data fork) instead of AppleSingle (default false)
|
||||
name: string?, -- the name of a buffer / string value (default "data")
|
||||
double: boolean?, -- default false
|
||||
name: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
--!strict
|
||||
-- archive/ar: Unix ar archives ("!<arch>\n"): System V / GNU (names ending in "/", the "//" long-name table, the "/"
|
||||
-- and "/SYM64/" symbol tables) and BSD (inline "#1/<len>" names, the "__.SYMDEF" symbol table), read into the standard
|
||||
-- Archive value and written (opts.variant "gnu", the default, or "bsd") from any Archive value. Symbol tables are not
|
||||
-- entries: their symbols land in meta.symbols ({ name, member }). Debian packages (.deb) are ar archives: their
|
||||
-- meta.payload names the data.tar.* member (the api loads a .deb as that data tar, meta.outer = this archive) and
|
||||
-- meta.control holds the fields of the control file in control.tar / .gz / .xz / .zst / .bz2 / .lzma (decompressed
|
||||
-- with codecs/compress/*, read with containers/Tar). Written from the ar(5) manual pages (GNU binutils, FreeBSD) and
|
||||
-- deb(5) (dpkg); no code ported. Not supported: thin archives ("!<thin>"), writing symbol tables (a re-written
|
||||
-- library needs ranlib), writing .deb control data (any entries write as a plain ar).
|
||||
-- archive/ar: Unix ar ("!<arch>\n"; ar(5), deb(5); no code ported): System V / GNU ("//" names, "/" and "/SYM64/"
|
||||
-- symbols) and BSD ("#1/<len>", "__.SYMDEF"); .deb payload and control fields. Supports: read, write gnu / bsd.
|
||||
-- Not supported: thin archives ("!<thin>"), writing symbol tables or .deb control data.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -31,10 +25,10 @@ local Ar = {
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxEntries: number?, -- default 1e6
|
||||
maxBytes: number?, -- .deb: the largest control.tar to decompress (default 2^26)
|
||||
maxBytes: number?, -- .deb control.tar limit (default 2^26)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
variant: ("gnu" | "bsd")?, -- default: the source archive's ("bsd" for a .deb: plain names), else "gnu"
|
||||
variant: ("gnu" | "bsd")?, -- default: the source's ("bsd" for a .deb), else "gnu"
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -134,7 +128,6 @@ local function controlFields(s: string): { [string]: string }
|
||||
return out
|
||||
end
|
||||
|
||||
-- control.tar[.ext] -> the control file's fields, or nil
|
||||
local function debControl(b: buffer, o: number, n: number, name: string, maxBytes: number): { [string]: string }?
|
||||
local ext = string.match(name, "^control%.tar%.?(%w*)$")
|
||||
local raw: buffer?
|
||||
@@ -183,7 +176,7 @@ function Ar.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local variant = "gnu"
|
||||
local longNames: string? = nil
|
||||
local symbols: { { name: string, offset: number } }? = nil
|
||||
local offsets: { [number]: string } = {} -- header offset (from the start) -> member name
|
||||
local offsets: { [number]: string } = {}
|
||||
local count = 0
|
||||
local p = base + 8
|
||||
while p < e do
|
||||
@@ -281,7 +274,6 @@ function Ar.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
a.meta.symbols = out
|
||||
end
|
||||
a.meta.source = { format = "ar", variant = variant }
|
||||
-- Debian packages
|
||||
local list = a._entries
|
||||
if list[1] and list[1].name == "debian-binary" then
|
||||
a.meta.source.variant = "deb"
|
||||
@@ -400,7 +392,6 @@ function Ar.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty ar Archive: a:add(name, data, { mtime, mode }); a:write({ variant = "bsd" })
|
||||
function Ar.new(): any
|
||||
local a = Archive.new("ar", encode)
|
||||
a.meta.source = { format = "ar", variant = "gnu" }
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- archive/arj: ARJ archives (Robert Jung) read into an Archive: the main header (archive name, comment, host OS,
|
||||
-- dates) and every local header (CRC-32 checked), files stored (method 0), compressed with methods 1..3 (the LHA-style
|
||||
-- static Huffman LZSS of codecs/compress/Lzh, "lh6" tables) or 4 (codecs/compress/Lzh "arj4"), directories (file type
|
||||
-- 3), DOS times (read as UTC), file modes, comments; each file's CRC-32 is verified when read (Err.CHECKSUM).
|
||||
-- Written from the ARJ technical information (arj's technote.txt). Not supported: garbled (encrypted) files,
|
||||
-- multi-volume archives (entries continued to / from another volume), chapters, writing (Err.UNSUPPORTED for those
|
||||
-- entries / calls).
|
||||
-- archive/arj: ARJ (Robert Jung; arj's technote.txt): headers CRC-32 checked, methods 0 stored, 1..3 Lzh "lh6", 4 Lzh
|
||||
-- "arj4", directories (file type 3), DOS times as UTC; file CRC-32 verified. Not supported: garbled (encrypted) files,
|
||||
-- multi-volume archives, chapters, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -25,7 +21,7 @@ local Arj = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse an entry larger than this (default 2^30)
|
||||
maxBytes: number?, -- default 2^30
|
||||
maxEntries: number?, -- default 65536
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- archive/binhex: BinHex 4.0 (.hqx) files: the 6-bit text encoding, the RLE90 run-length layer, the header (name,
|
||||
-- type / creator, Finder flags) and the data and resource forks, each with its CRC-16/XMODEM (verified:
|
||||
-- Err.CHECKSUM). Decodes to an Archive in the containers/MacFile convention (the data fork is the entry `name`, a
|
||||
-- resource fork the entry `name.rsrc`, with macType / macCreator / finderFlags); writes any Archive's first Mac file
|
||||
-- (or a buffer) back, 64 characters a line, as BinHex 4.0 tools do. Written from the BinHex 4.0 definition (Peter
|
||||
-- Lewis' / RFC 1741 notes). Not supported: BinHex 2 / 3 / 5 (BinHex 5 is MacBinary inside BinHex 4: unwrap it with
|
||||
-- archive/macbinary), several files in one .hqx (only the first is read).
|
||||
-- archive/binhex: BinHex 4.0 (.hqx; RFC 1741 notes): 6-bit text, RLE90, header and forks each with CRC-16/XMODEM, as
|
||||
-- containers/MacFile; writes 64 characters a line, as BinHex 4.0 tools do. Not supported: BinHex 2 / 3 / 5, several
|
||||
-- files in one .hqx (the first is read).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -25,7 +21,7 @@ local Binhex = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = { maxBytes: number? } -- default 2^30
|
||||
export type EncodeOptions = { name: string? } -- the name of a buffer / string value (default "data")
|
||||
export type EncodeOptions = { name: string? }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local ALPHABET = "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr"
|
||||
@@ -46,7 +42,6 @@ function Binhex.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the 6-bit characters between the colons -> bytes, then RLE90 expanded (nil, message on bad input)
|
||||
local function unhex(text: string, maxBytes: number): (buffer?, string?)
|
||||
local banner = string.find(text, BANNER, 1, true)
|
||||
if not banner then
|
||||
@@ -190,7 +185,6 @@ function Binhex.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
body:u16be(Crc16.ccitt(part, 0, buffer.len(part)))
|
||||
end
|
||||
local src = body:finish()
|
||||
-- RLE90
|
||||
local rle = Bytes.writer(buffer.len(src) + 16)
|
||||
local n = buffer.len(src)
|
||||
local i = 0
|
||||
@@ -244,7 +238,6 @@ function Binhex.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty BinHex Archive: a:add(name, data, { macType, macCreator }); add "name.rsrc" for a resource fork
|
||||
function Binhex.new(): any
|
||||
local a = Archive.new("binhex", encoder)
|
||||
a.meta.source = { format = "binhex" }
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
--!strict
|
||||
-- archive/br: a Brotli (RFC 7932) stream as a single-entry archive (meta.stream: the api unwraps a .tar.br to the tar
|
||||
-- inside). Decoding through codecs/compress/Brotli. The entry is named after opts.name without ".br" (else "data").
|
||||
-- Supports: read (eager decompression). Not supported: write (no Brotli compressor). Brotli has no magic number:
|
||||
-- sniff accepts only data that decodes cleanly (the whole stream for small files, a 64 KB prefix for big ones), at a
|
||||
-- low confidence, so the extension decides between candidates.
|
||||
-- archive/br: a Brotli (RFC 7932) stream as a single-entry archive (codecs/compress/Brotli). Supports: read.
|
||||
-- Not supported: write. No magic number: sniff accepts only data that decodes cleanly, at a low confidence.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -22,8 +19,8 @@ local Br = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
name: string?, -- the file name (the api passes it): the entry is named after it
|
||||
maxBytes: number?, -- refuse output larger than this (default 2^30)
|
||||
name: string?,
|
||||
maxBytes: number?, -- default 2^30
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
--!strict
|
||||
-- archive/bz2: a .bz2 file as a single-stream archive: one entry, the decompressed data (codecs/compress/Bzip2; concatenated
|
||||
-- streams are joined, as bzip2 -d does). The entry is named after the file without its .bz2 (".tbz2" -> ".tar"),
|
||||
-- else "data"; the api unwraps a .tar.bz2 into the tar inside (meta.stream). New in rosetta. Not supported:
|
||||
-- writing (codecs/compress/Bzip2 has no compressor), randomised blocks (bzip2 0.9.0 and older).
|
||||
-- archive/bz2: a .bz2 file as a single-stream archive of one entry (codecs/compress/Bzip2). Supports: read,
|
||||
-- concatenated streams joined as bzip2 -d does. Not supported: writing, randomised blocks (bzip2 0.9.0 and older).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -21,8 +19,8 @@ local Bz2 = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse larger output (default 2^30)
|
||||
name: string?, -- the file name (the api passes it): names the entry
|
||||
maxBytes: number?, -- default 2^30
|
||||
name: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- archive/cab/Sum: the cabinet data block checksum (the XOR of the data's little-endian 32-bit words, the last 1..3
|
||||
-- bytes packed most significant first, then the block's cbData / cbUncomp word). Written from the CAB format
|
||||
-- description ([MS-CAB] 2.4, CFDATA.csum).
|
||||
-- archive/cab/Sum: the CFDATA.csum checksum ([MS-CAB] 2.4): XOR of LE u32 words, the last 1..3 bytes packed most
|
||||
-- significant first, then the cbData / cbUncomp word.
|
||||
|
||||
local readu8, readu32, blen = buffer.readu8, buffer.readu32, buffer.len
|
||||
local bxor, bor, lshift = bit32.bxor, bit32.bor, bit32.lshift
|
||||
|
||||
local Sum = {}
|
||||
|
||||
-- of(buf, off, len, seed) -> the checksum of buf[off, off + len) continuing from seed (0 for a new sum)
|
||||
function Sum.of(buf: buffer, off: number, len: number, seed: number): number
|
||||
if off < 0 or len <= 0 or off + len > blen(buf) then
|
||||
return seed
|
||||
@@ -34,7 +32,6 @@ function Sum.of(buf: buffer, off: number, len: number, seed: number): number
|
||||
return bxor(s, tail)
|
||||
end
|
||||
|
||||
-- block(buf, dataAt, cbData, cbUncomp) -> the csum field of a CFDATA block with these sizes over buf[dataAt, +cbData)
|
||||
function Sum.block(buf: buffer, dataAt: number, cbData: number, cbUncomp: number): number
|
||||
return bxor(Sum.of(buf, dataAt, cbData, 0), cbData + cbUncomp * 65536)
|
||||
end
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
--!strict
|
||||
-- archive/cab: Microsoft Cabinet files (MSCF 1.3) read into an Archive: the header with its reserve areas and
|
||||
-- cabinet set fields, folders (CFFOLDER) compressed with none, MSZIP (each CFDATA block "CK" + raw deflate whose
|
||||
-- dictionary carries over from the block before), LZX (codecs/compress/Lzx, windows 2^15..2^21) or Quantum
|
||||
-- (codecs/compress/Quantum, windows 2^10..2^21), the files (CFFILE) with their MS-DOS attributes, DOS times (local
|
||||
-- time without a zone, read as UTC) and the UTF-8 name flag; every data block's checksum is verified (Err.CHECKSUM)
|
||||
-- when a file of its folder is read. A folder is decompressed once per read and kept for the next file of the same
|
||||
-- folder. Writes one folder, stored or MSZIP (independent 32 KB deflate blocks), from any Archive. Written from the
|
||||
-- Microsoft Cabinet format specification ([MS-CAB]). Not supported: files continued from / to another cabinet of a
|
||||
-- set (Err.UNSUPPORTED for those entries, and for the first folder of a cabinet continued from a previous one),
|
||||
-- signed / self-extracting stubs before the header.
|
||||
-- archive/cab: Microsoft Cabinet (MSCF 1.3, [MS-CAB]): folders none / MSZIP ("CK" + raw deflate, dictionary carried
|
||||
-- over between blocks) / LZX (2^15..2^21) / Quantum (2^10..2^21), DOS times as UTC, checksums verified; writes one
|
||||
-- stored or MSZIP folder. Not supported: files spanning cabinets of a set, signed / self-extracting stubs.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -30,14 +23,14 @@ local Cab = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a folder that decompresses to more than this (default 2^30)
|
||||
maxBytes: number?, -- default 2^30
|
||||
maxEntries: number?, -- default 65535
|
||||
verify: boolean?, -- check the data block checksums (default true)
|
||||
verify: boolean?, -- default true
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
compression: ("mszip" | "none")?, -- default "mszip"
|
||||
level: number?, -- deflate level 0..9 (default 6)
|
||||
setId: number?, -- the cabinet set id (default 0)
|
||||
level: number?, -- 0..9 (default 6)
|
||||
setId: number?, -- default 0
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -63,8 +56,7 @@ type Folder = {
|
||||
}
|
||||
type Block = { at: number, len: number, size: number }
|
||||
|
||||
-- the CFDATA blocks of a folder (checksums checked), their total output and whether the folder runs into the next
|
||||
-- cabinet (a block with cbUncomp 0)
|
||||
-- a block with cbUncomp 0 runs into the next cabinet
|
||||
local function blocks(r: Bytes.Reader, f: Folder, dataReserve: number, verify: boolean, maxBytes: number): ({ Block }?, any, boolean?)
|
||||
local list: { Block } = {}
|
||||
local p = f.dataAt
|
||||
@@ -95,7 +87,6 @@ local function blocks(r: Bytes.Reader, f: Folder, dataReserve: number, verify: b
|
||||
return list, total, false
|
||||
end
|
||||
|
||||
-- the decompressed data of a folder's blocks
|
||||
local function inflateFolder(buf: buffer, list: { Block }, total: number, typ: number): (buffer?, Err.Err?)
|
||||
local method = bit32.band(typ, 15)
|
||||
local out = buffer.create(total)
|
||||
@@ -191,7 +182,6 @@ function Cab.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
flags = flags,
|
||||
reserve = { header = headerReserve, folder = folderReserve, data = dataReserve },
|
||||
}
|
||||
-- folders
|
||||
local folders: { Folder } = {}
|
||||
local methods = {}
|
||||
for i = 1, cFolders do
|
||||
@@ -216,7 +206,6 @@ function Cab.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
if cFiles > maxEntries then
|
||||
return nil, Err.new(Err.LIMIT, "more than maxEntries files")
|
||||
end
|
||||
-- the folder cache: the last decompressed folder
|
||||
local cachedIndex, cachedData, cachedRuns = 0, nil :: buffer?, false
|
||||
local function folderData(f: Folder): (buffer?, Err.Err?, boolean?)
|
||||
if cachedIndex == f.index then
|
||||
@@ -233,7 +222,6 @@ function Cab.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
cachedIndex, cachedData, cachedRuns = f.index, data, runs == true
|
||||
return data, nil, runs
|
||||
end
|
||||
-- files
|
||||
local fr = r:sub(coffFiles)
|
||||
for _ = 1, cFiles do
|
||||
local size, offset, iFolder = fr:u32le(), fr:u32le(), fr:u16le()
|
||||
@@ -306,7 +294,6 @@ function Cab.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(archive, opts?) -> a cabinet with one folder holding every file (directories are implied by the names)
|
||||
function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o: EncodeOptions = opts or {}
|
||||
local compression = o.compression or "mszip"
|
||||
@@ -334,7 +321,6 @@ function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if total > 0x7FFF8000 then
|
||||
return nil, Err.new(Err.ARG, "more than 2 GB of data")
|
||||
end
|
||||
-- the folder data, in 32 KB blocks
|
||||
local folder = buffer.create(total)
|
||||
local p = 0
|
||||
for _, f in files do
|
||||
@@ -369,7 +355,6 @@ function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
s += n
|
||||
end
|
||||
local blob = data:finish()
|
||||
-- names and the file table
|
||||
local table_ = Bytes.writer(64 + #files * 32)
|
||||
local offset = 0
|
||||
for _, f in files do
|
||||
@@ -417,7 +402,6 @@ function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty cab Archive: a:add(name, data, { mtime }); a:write({ compression = "none" | "mszip" })
|
||||
function Cab.new(): any
|
||||
local a = Archive.new("cab", encode)
|
||||
a.meta.source = { format = "cab" }
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
--!strict
|
||||
-- archive/cpio: cpio archives (newc, crc, odc, old binary in both byte orders; see containers/Cpio) read into the
|
||||
-- standard Archive value (entries read lazily: a copy of the stored bytes) and written as newc (default), crc or odc
|
||||
-- from any Archive value (a tar, a zip, ...: list() + read()). Entries carry the cpio fields: type (tar letters),
|
||||
-- mode (permission bits; the type is in type), uid, gid, nlink, ino, devmajor / devminor, rdevmajor / rdevminor,
|
||||
-- link (symlinks), hardlink (the first link of a hard link group; every link reads the data). Tar hard links ("1")
|
||||
-- written as cpio share an inode with their target. Not supported: tar-only fields (uname, gname: dropped).
|
||||
-- archive/cpio: cpio (newc, crc, odc, old binary in both byte orders; containers/Cpio). Supports: read (lazy), write as
|
||||
-- newc / crc / odc; tar hard links ("1") share an inode with their target. Not supported: uname / gname (dropped).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -24,12 +20,12 @@ local CpioFormat = {
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxEntries: number?, -- default 1e6
|
||||
verify: boolean?, -- crc archives: check the data sums (default true)
|
||||
verify: boolean?, -- default true
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
variant: ("newc" | "crc" | "odc")?, -- default: the source archive's (old binary: newc), else "newc"
|
||||
format: string?, -- "newc", "crc" or "odc" here too; any other value (the api's "cpio") means the variant default
|
||||
block: number?, -- pad to a multiple of this many bytes (default 512, as GNU cpio; 1 = none)
|
||||
format: string?, -- any other value (the api's "cpio") means the variant default
|
||||
block: number?, -- default 512, as GNU cpio; 1 = none
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -153,7 +149,6 @@ function CpioFormat.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Er
|
||||
rdevminor = num(e.rdevminor),
|
||||
})
|
||||
end
|
||||
-- opts.format may also be the api's format name ("cpio"): the variant default then
|
||||
local o: any = opts or {}
|
||||
local src = type(archive.meta) == "table" and type(archive.meta.source) == "table" and archive.meta.source
|
||||
local own = if src and src.format == "cpio" and (src.variant == "crc" or src.variant == "odc") then src.variant else "newc"
|
||||
@@ -162,7 +157,6 @@ function CpioFormat.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Er
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty cpio Archive: a:add(name, data, { mtime, mode, dir }); a:write()
|
||||
function CpioFormat.new(): any
|
||||
local a = Archive.new("cpio", encode)
|
||||
a.meta.source = { format = "cpio" }
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
--!strict
|
||||
-- archive/d64: Commodore disk images as an Archive: D64 (1541: 35 / 40 / 42 tracks, with or without the error-info
|
||||
-- block), D71 (1571: 70 tracks, double sided) and D81 (1581: 80 tracks of 40 sectors). The BAM / header (disk name and
|
||||
-- id in meta.disk / meta.id), the directory chain, and every file's sector chain; PRG / SEQ / USR / REL files are
|
||||
-- entries (entry.type, entry.blocks, entry.locked, entry.closed; REL files read their data chain, the side sectors are
|
||||
-- skipped), deleted entries are skipped, unclosed ("splat") files are flagged closed = false. Names are PETSCII
|
||||
-- translated as VICE's c1541 does (0x41..0x5A -> a..z, 0xC1..0xDA and 0x61..0x7A -> A..Z, other bytes to U+E0xx so the
|
||||
-- name survives a round trip); entry.rawName keeps the 16 bytes. Writes D64 (35 tracks) / D71 / D81 images from any
|
||||
-- Archive (files as PRG, or entry.type), allocating sectors near the directory track with interleave 10 as CBM DOS
|
||||
-- does (not the same sectors as a real drive). Written from the 1541 / 1571 / 1581 DOS documentation (Peter Schepers'
|
||||
-- disk format notes). Not supported: GEOS VLIR files (read as their first chain), 1581 partitions (CBM type) contents.
|
||||
-- archive/d64: Commodore D64 (1541: 35 / 40 / 42 tracks, error block), D71, D81 (Peter Schepers' notes); PETSCII names
|
||||
-- as VICE's c1541 (0x41..0x5A -> a..z, 0xC1..0xDA / 0x61..0x7A -> A..Z, else U+E0xx); writes with interleave 10 as
|
||||
-- CBM DOS (not a real drive's sectors). Not supported: GEOS VLIR files (first chain), 1581 partition contents.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -28,8 +21,8 @@ local D64 = {
|
||||
export type DecodeOptions = { maxEntries: number? } -- default 4096
|
||||
export type EncodeOptions = {
|
||||
variant: ("d64" | "d71" | "d81")?, -- default: the variant read, else "d64"
|
||||
disk: string?, -- disk name (default "rosetta" / the name read)
|
||||
id: string?, -- two-character disk id (default "fl")
|
||||
disk: string?, -- default "rosetta" / the name read
|
||||
id: string?, -- default "fl"
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -129,7 +122,6 @@ local function encoder(a: any, o: any?): (buffer?, Err.Err?)
|
||||
return D64.encode(a, o)
|
||||
end
|
||||
|
||||
-- the bytes of a sector chain starting at (t, s); nil, message when it leaves the disk or loops
|
||||
local function chain(b: buffer, base: number, geo: Geometry, t: number, s: number): (buffer?, string?)
|
||||
local w = Bytes.writer()
|
||||
local seen: { [number]: boolean } = {}
|
||||
@@ -174,7 +166,6 @@ function D64.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
a.meta.tracks = geo.tracks
|
||||
a.meta.errorInfo = r:size() ~= geo.total
|
||||
a.meta.source = { format = "d64", variant = geo.kind, tracks = geo.tracks }
|
||||
-- the directory chain
|
||||
local t, s = buffer.readu8(b, hdr), buffer.readu8(b, hdr + 1)
|
||||
local seen: { [number]: boolean } = {}
|
||||
local count = 0
|
||||
@@ -236,7 +227,6 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- BAM layout per variant: where each track's free count + bitmap lives
|
||||
local function bamEntry(geo: Geometry, img: buffer, t: number): (number, number, number)
|
||||
-- returns (offset of the free count, offset of the bitmap, bitmap bytes)
|
||||
local dir = geo.offsets[geo.dirTrack]
|
||||
if geo.kind == "d81" then
|
||||
local side = if t <= 40 then 1 else 2
|
||||
@@ -286,7 +276,6 @@ function D64.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local function pad16(s: string): string
|
||||
return s .. string.rep("\160", 16 - #s)
|
||||
end
|
||||
-- header / BAM
|
||||
local firstDir
|
||||
if variant == "d81" then
|
||||
buffer.writestring(img, dir, "\40\3\68\0")
|
||||
@@ -413,7 +402,6 @@ function D64.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new(variant?) -> an empty disk: a:add(name, data); a:write({ disk = "name", id = "ab" })
|
||||
function D64.new(variant: string?): any
|
||||
local a = Archive.new("d64", encoder)
|
||||
a.meta.variant = variant or "d64"
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
--!strict
|
||||
-- archive/fat: FAT12 / FAT16 / FAT32 disk images (floppy images, and hard-disk images with an MBR partition table: the
|
||||
-- first FAT partition, or opts.partition) as an Archive, through containers/Fat: VFAT long names, NT lower-case 8.3
|
||||
-- names, directories (entries ending in "/"), file dates (DOS local time read as UTC), attributes (entry.attr) and the
|
||||
-- 8.3 name (entry.shortName); files read lazily. meta.fat = 12 / 16 / 32, meta.label, meta.oem. Writes FAT12 floppy
|
||||
-- images (1.44 MB by default; 360 / 720 / 1200 / 2880 KB with opts.size) from any Archive, with long names where a
|
||||
-- name is not a plain 8.3 name; mtools and fsck.fat read them. Not supported: writing FAT16 / FAT32, exFAT, boot code.
|
||||
-- archive/fat: FAT12 / FAT16 / FAT32 images (floppy, or the first FAT partition of an MBR) through containers/Fat.
|
||||
-- Supports: VFAT long names, NT lower-case 8.3, DOS dates as UTC; writes FAT12 floppies (360..2880 KB) that mtools and
|
||||
-- fsck.fat read. Not supported: writing FAT16 / FAT32, exFAT, boot code.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -23,12 +20,12 @@ local FatFormat = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
partition: number?, -- MBR partition 1..4 (default: the first FAT partition, or the image itself)
|
||||
partition: number?, -- 1..4 (default: the first FAT partition, or the image itself)
|
||||
maxEntries: number?, -- default 65536
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
size: number?, -- floppy size in KB: 360 / 720 / 1200 / 1440 / 2880 (default 1440)
|
||||
label: string?, -- volume label
|
||||
size: number?, -- KB: 360 / 720 / 1200 / 1440 / 2880 (default 1440)
|
||||
label: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -114,7 +111,6 @@ function FatFormat.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty floppy: a:add("dir/file.txt", data, { mtime }); a:write({ size = 1440, label = "DISK" })
|
||||
function FatFormat.new(): any
|
||||
local a = Archive.new("fat", encoder)
|
||||
a.meta.source = { format = "fat" }
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
-- archive/grp: Build engine GRP archives (Duke Nukem 3D, Shadow Warrior, Blood: "KenSilverman", a u32 file count,
|
||||
-- 16-byte entries of a 12-byte name and a u32 size, then the files back to back), read into the standard Archive
|
||||
-- (entries read lazily) and written from any Archive value (names upper-cased to 8.3 by the caller's choice: they
|
||||
-- are stored as given, at most 12 bytes). Written from Ken Silverman's GRP description (kenbuild); no code taken.
|
||||
-- Not supported: the "GRP 2" / RFF / SSI variants.
|
||||
-- archive/grp: Build engine GRP ("KenSilverman", u32 count, 16-byte entries: 12-byte name + u32 size, then the data),
|
||||
-- from Ken Silverman's description; no code taken. Supports: read (lazy), write (names stored as given, at most 12
|
||||
-- bytes). Not supported: the "GRP 2" / RFF / SSI variants.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -23,7 +21,7 @@ local Grp = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = { maxEntries: number? } -- default 1e6
|
||||
export type EncodeOptions = { upper: boolean? } -- upper-case the names (Build looks them up case-insensitively; default false)
|
||||
export type EncodeOptions = { upper: boolean? } -- Build looks names up case-insensitively; default false
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MAGIC = "KenSilverman"
|
||||
@@ -105,7 +103,6 @@ function Grp.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty GRP Archive: a:add(name, data); a:write()
|
||||
function Grp.new(): any
|
||||
local a = Archive.new("grp", encode)
|
||||
a.meta.source = { format = "grp" }
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
-- archive/gzip: gzip files (RFC 1952) as a single-stream Archive: one entry, named after the header's FNAME (else the
|
||||
-- file name without .gz, else "data"), with the header's MTIME; concatenated members decode as one stream, as gzip -d
|
||||
-- does. Writing: an Archive with one entry (or a buffer / string) becomes one member with FNAME and MTIME; an Archive
|
||||
-- with several entries is written as a tar inside the gzip (a .tar.gz). The api unwraps a .tar.gz / .tgz on load (the
|
||||
-- inner tar, meta.compression = "gzip"). Deflate is codecs/compress/Deflate. Not supported: FHCRC verification.
|
||||
-- archive/gzip: gzip (RFC 1952) as a single-stream archive of one entry (FNAME, MTIME; codecs/compress/Deflate).
|
||||
-- Supports: concatenated members as one stream (as gzip -d); write (several entries: a .tar.gz).
|
||||
-- Not supported: FHCRC verification.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -23,11 +21,11 @@ local Gzip = {
|
||||
caps = { decode = true, encode = true, stream = true },
|
||||
}
|
||||
|
||||
export type DecodeOptions = { name: string? } -- the file name (the api passes it): names the entry without FNAME
|
||||
export type DecodeOptions = { name: string? }
|
||||
export type EncodeOptions = {
|
||||
level: number?, -- 0..9, default 6
|
||||
name: string?, -- FNAME (default: the entry's name; none for a buffer)
|
||||
mtime: number?, -- MTIME (default: the entry's mtime, else 0)
|
||||
name: string?, -- default: the entry's name; none for a buffer
|
||||
mtime: number?, -- default: the entry's mtime, else 0
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -36,7 +34,6 @@ function Gzip.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- header(r) -> { name, comment, mtime, os } of the first member (nil fields when absent)
|
||||
local function header(b: buffer, o: number, e: number): { [string]: any }
|
||||
local h: { [string]: any } = {}
|
||||
if o + 10 > e then
|
||||
@@ -171,7 +168,6 @@ function Gzip.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new(data?, name?) -> a gzip Archive holding one entry (add() replaces / adds; several entries write a .tar.gz)
|
||||
function Gzip.new(data: (buffer | string)?, name: string?): any
|
||||
local a = Archive.new("gzip", function(arc: any, o: any?): (buffer?, Err.Err?)
|
||||
return Gzip.encode(arc, o)
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
--!strict
|
||||
-- archive/iso: ISO 9660 CD / DVD images (.iso) read into the standard Archive value through containers/Iso9660:
|
||||
-- plain ISO 9660 names, Joliet and Rock Ridge (names, modes, owners, times, symlinks, relocated deep directories;
|
||||
-- Rock Ridge is used when present, else Joliet, else the plain names: opts.prefer picks another), multi-extent files.
|
||||
-- Directories are dir entries; file data reads lazily (a copy of the extents). meta.iso holds the volume facts
|
||||
-- (volumeId, systemId, publisher, blockSize, blocks, created, joliet level, rockRidge, tree) and meta.boot the El
|
||||
-- Torito boot catalog (platform, bootable, media, load segment, sector count, image LBA) when there is one.
|
||||
-- Not supported: writing, UDF, interleaved files (read: Err.UNSUPPORTED), zisofs-compressed files (read: the stored
|
||||
-- bytes), multi-session images (the first session).
|
||||
-- archive/iso: ISO 9660 images through containers/Iso9660. Supports: plain names, Joliet, Rock Ridge (RR, else Joliet,
|
||||
-- else plain), multi-extent files, El Torito boot catalog (meta.boot). Not supported: writing, UDF, interleaved files
|
||||
-- (UNSUPPORTED), zisofs (stored bytes), multi-session images (the first session).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -25,7 +20,7 @@ local Iso = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
prefer: ("rockridge" | "joliet" | "iso9660")?, -- the name set (default: Rock Ridge, else Joliet, else plain)
|
||||
prefer: ("rockridge" | "joliet" | "iso9660")?, -- default: Rock Ridge, else Joliet, else plain
|
||||
maxEntries: number?, -- default 1e6
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
--!strict
|
||||
-- archive/lha: LHA / LZH archives (and LArc .lzs) read into an Archive: header levels 0, 1, 2 and 3 with their
|
||||
-- extended headers (file and directory names, Unix permissions / uid / gid / user and group names / mtime, MS-DOS
|
||||
-- attributes, Windows timestamps, comments), methods lh0 / lz4 (stored), lhd (directory), lh1, lh4, lh5, lh6, lh7,
|
||||
-- lhx, lzs and lz5 through codecs/compress/Lzh; every file's CRC-16 (ARC) is verified (Err.CHECKSUM) when read.
|
||||
-- Unix LHA symlinks ("name|target") become link entries; all-capital names from MS-DOS-like systems read in lower
|
||||
-- case (as lhasa and LHa do). Level 0 / 1 MS-DOS times are read as UTC. Writes header
|
||||
-- level 2 archives with stored (lh0) data from any Archive. Written from the LHA header documentation (Haruyasu
|
||||
-- Yoshizaki's LHa notes, lhasa's lha_file_header.c as a reference, ISC). Not supported: pm0 / pm1 / pm2 (PMarc),
|
||||
-- compression when writing, multi-volume archives, self-extracting stubs (the archive must start at offset 0 or
|
||||
-- at the first header found in the first 64 KB).
|
||||
-- archive/lha: LHA / LZH (and LArc .lzs; Yoshizaki's LHa notes, lhasa's lha_file_header.c, ISC): header levels 0-3,
|
||||
-- methods lh0 / lz4 / lhd / lh1 / lh4-lh7 / lhx / lzs / lz5, CRC-16 (ARC), "name|target" symlinks; writes level 2,
|
||||
-- stored. Not supported: PMarc pm0-pm2, compression when writing, multi-volume, stubs beyond the first 64 KB.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -28,7 +21,7 @@ local Lha = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse an entry larger than this (default 2^30)
|
||||
maxBytes: number?, -- default 2^30
|
||||
maxEntries: number?, -- default 65536
|
||||
}
|
||||
export type EncodeOptions = {}
|
||||
@@ -58,7 +51,7 @@ function Lha.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a path with "/" separators, without a leading "/" and without "." / ".." components (as lhasa collapses them)
|
||||
-- "." / ".." collapsed as lhasa does
|
||||
local function path(s: string): string
|
||||
s = string.gsub(s, "[\\\255]", "/")
|
||||
local parts = {}
|
||||
@@ -90,7 +83,6 @@ type Header = {
|
||||
dataAt: number,
|
||||
}
|
||||
|
||||
-- the extended headers from offset p (size field width w), until a zero size; returns the end offset
|
||||
local function extended(r: Bytes.Reader, p: number, w: number, first: number, h: Header, limit: number): (number?, string?)
|
||||
local size = first
|
||||
local guard = 0
|
||||
@@ -138,7 +130,6 @@ local OS = { [0x4D] = "msdos", [0x55] = "unix", [0x32] = "os2", [0x39] = "os9",
|
||||
[0x48] = "human68k", [0x43] = "cpm", [0x46] = "flex", [0x6D] = "mac", [0x77] = "windows95", [0x57] = "windowsnt",
|
||||
[0x52] = "runser", [0x54] = "townsos", [0x58] = "xosk", [0x41] = "amiga", [0x61] = "atari", [0x4A] = "java" }
|
||||
|
||||
-- one header at p -> Header (h.dataAt: where the data starts), or nil at the end marker
|
||||
local function header(r: Bytes.Reader, p: number): (Header?, string?)
|
||||
local limit = r:size()
|
||||
if p >= limit or r:sub(p, 1):u8() == 0 then
|
||||
@@ -241,7 +232,6 @@ function Lha.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local maxBytes = o.maxBytes or 2 ^ 30
|
||||
local maxEntries = o.maxEntries or 65536
|
||||
local p = 0
|
||||
-- a self-extracting stub: find the first header
|
||||
if not isHeader(r:peek(22)) then
|
||||
local head = r:peek(65536)
|
||||
local at = nil
|
||||
@@ -383,7 +373,7 @@ function Lha.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
dirPart, base = full .. "/", ""
|
||||
end
|
||||
local h = Bytes.writer()
|
||||
h:u16le(0) -- total size, patched
|
||||
h:u16le(0)
|
||||
h:string(if e.dir then "-lhd-" else "-lh0-")
|
||||
h:u32le(buffer.len(data))
|
||||
h:u32le(buffer.len(data))
|
||||
@@ -424,7 +414,6 @@ function Lha.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty LHA Archive: a:add(name, data, { mtime, mode }); a:write() (level 2 headers, stored data)
|
||||
function Lha.new(): any
|
||||
local a = Archive.new("lha", encoder)
|
||||
a.meta.source = { format = "lha" }
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
--!strict
|
||||
-- archive/lz4: LZ4 frame files (.lz4: lz4 doc/lz4_Frame_format.md 1.6.x) as a single-stream Archive: one entry (the
|
||||
-- decompressed data), named after the file without .lz4 (.tlz4 -> .tar), else "data"; meta.stream = true, so the api
|
||||
-- unwraps a .tar.lz4. Reading: concatenated frames, skippable frames (magic 0x184D2A50..5F, skipped), the legacy
|
||||
-- frame (magic 0x184C2102, 8 MB blocks), independent and linked blocks, block sizes 64 KB .. 4 MB, uncompressed
|
||||
-- blocks, the header checksum, block checksums and the content checksum (XXH32, codecs/compress/Xxh32: verified
|
||||
-- unless opts.verify = false), the content size (checked). Writing: one frame of independent blocks
|
||||
-- (codecs/compress/Lz4), with opts for the block size, block checksums, the content checksum and the content size;
|
||||
-- several entries are written as a tar inside. Written from the specification; no code ported. Not supported:
|
||||
-- dictionaries (frames with a dictionary ID: Err.UNSUPPORTED), writing linked blocks or legacy frames.
|
||||
-- archive/lz4: LZ4 frames (lz4_Frame_format.md 1.6.x) as a single-stream archive. Supports: concatenated, skippable
|
||||
-- (0x184D2A50..5F) and legacy (0x184C2102, 8 MB blocks) frames, linked blocks, 64 KB .. 4 MB, XXH32 checksums; writes
|
||||
-- one frame of independent blocks. Not supported: dictionaries, writing linked blocks or legacy frames.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -27,15 +21,15 @@ local Lz4Format = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse output larger than this (default 2^30)
|
||||
verify: boolean?, -- check the block / content checksums (default true; the header checksum is always checked)
|
||||
name: string?, -- the file name (the api passes it): names the entry
|
||||
maxBytes: number?, -- default 2^30
|
||||
verify: boolean?, -- default true; the header checksum is always checked
|
||||
name: string?,
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
blockSize: number?, -- 65536, 262144, 1048576 or 4194304 (default 4 MB, as the lz4 tool)
|
||||
blockChecksum: boolean?, -- default false
|
||||
contentChecksum: boolean?, -- default true
|
||||
contentSize: boolean?, -- store the content size (default false)
|
||||
contentSize: boolean?, -- default false
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -71,7 +65,6 @@ end
|
||||
|
||||
type Out = { buf: buffer, n: number, max: number }
|
||||
|
||||
-- room for k more bytes in the output (doubling, up to max) | false
|
||||
local function room(out: Out, k: number): boolean
|
||||
local need = out.n + k
|
||||
if need > out.max then
|
||||
@@ -85,7 +78,6 @@ local function room(out: Out, k: number): boolean
|
||||
return true
|
||||
end
|
||||
|
||||
-- one LZ4 frame at p -> the input offset after it | nil, Err
|
||||
local function frame(b: buffer, p: number, e: number, out: Out, verify: boolean): (number?, Err.Err?)
|
||||
if p + 7 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "frame header past the end", p)
|
||||
@@ -210,7 +202,6 @@ local function legacy(b: buffer, p: number, e: number, out: Out): (number?, Err.
|
||||
return q, nil
|
||||
end
|
||||
|
||||
-- decompress(buf, offset?, length?, opts?) -> the data of every frame | nil, Err
|
||||
function Lz4Format.decompress(src: buffer | string, offset: number?, length: number?, opts: DecodeOptions?): (buffer?, Err.Err?)
|
||||
local b, o, n, aerr = Bytes.span("Lz4.decompress", src, offset, length)
|
||||
if not b then
|
||||
@@ -280,7 +271,6 @@ function Lz4Format.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- compress(data, opts?) -> one LZ4 frame | nil, Err
|
||||
function Lz4Format.compress(data: buffer | string, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local b, o, n, aerr = Bytes.span("Lz4.compress", data, nil, nil)
|
||||
if not b then
|
||||
@@ -341,7 +331,6 @@ function Lz4Format.compress(data: buffer | string, opts: EncodeOptions?): (buffe
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
-- encode(value, opts?): a buffer / string, an Archive's one file, or a tar of the Archive's entries, as one frame
|
||||
function Lz4Format.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local data, err = Archive.streamData(value)
|
||||
if not data then
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- archive/macbinary: MacBinary I, II and III files (a 128-byte header with the name, type / creator, Finder flags,
|
||||
-- dates and fork lengths, then the data and resource forks padded to 128 bytes, then the Get Info comment). II / III
|
||||
-- headers carry a CRC-16/XMODEM (verified: Err.CHECKSUM), III the 'mBIN' signature; MacBinary I is recognised by its
|
||||
-- zero bytes and sane lengths. Decodes to an Archive in the containers/MacFile convention (the data fork is the entry
|
||||
-- `name`, a resource fork the entry `name.rsrc`, with macType / macCreator / finderFlags / created / mtime /
|
||||
-- comment); writes MacBinary III (readable as II) from any Archive's first Mac file or a buffer. Written from the
|
||||
-- MacBinary II / III specifications (Yves Lempereur et al.). Not supported: the secondary header's content (skipped).
|
||||
-- archive/macbinary: MacBinary I / II / III (128-byte header, forks padded to 128, Get Info comment; II / III
|
||||
-- CRC-16/XMODEM, III 'mBIN'), from the MacBinary II / III specs (Lempereur et al.), as containers/MacFile.
|
||||
-- Supports: read, write III. Not supported: the secondary header's content (skipped).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -25,10 +21,9 @@ local MacBinary = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = { maxBytes: number? } -- default 2^30
|
||||
export type EncodeOptions = { name: string? } -- the name of a buffer / string value (default "data")
|
||||
export type EncodeOptions = { name: string? }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
-- version(b, o, len) -> "III" / "II" / "I" / nil: which MacBinary header sits at o
|
||||
local function version(b: buffer, o: number, len: number): string?
|
||||
if len < 128 then
|
||||
return nil
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- archive/mpq/Crypt: the MPQ encryption table, the name hash (types 0 = table index, 1 / 2 = name check, 3 = file
|
||||
-- key) and block decryption in place. Written from the MPQ format description (Justin Olbrantz / Jean-Francois
|
||||
-- Roy, "The MoPaQ Archive Format"; StormLib's documentation); no code taken. All arithmetic is mod 2^32 and every
|
||||
-- value handed to bit32 is an integer in 0..2^32 - 1 (both runtimes agree).
|
||||
-- archive/mpq/Crypt: the MPQ encryption table, name hash (0 = table index, 1 / 2 = name check, 3 = file key) and
|
||||
-- block decryption, from "The MoPaQ Archive Format" (Olbrantz / Roy) and StormLib's docs; no code taken. Mod 2^32.
|
||||
local readu32, writeu32 = buffer.readu32, buffer.writeu32
|
||||
local bxor, band, bor, lshift, rshift, bnot = bit32.bxor, bit32.band, bit32.bor, bit32.lshift, bit32.rshift, bit32.bnot
|
||||
|
||||
@@ -26,7 +24,7 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
-- hash(name, kind) -> u32: ASCII upper case, '/' as '\'
|
||||
-- name hash: ASCII upper case, '/' as '\'
|
||||
function Crypt.hash(name: string, kind: number): number
|
||||
local s1, s2 = 0x7FED7FED, 0xEEEEEEEE
|
||||
local base = kind * 0x100
|
||||
@@ -43,7 +41,7 @@ function Crypt.hash(name: string, kind: number): number
|
||||
return s1
|
||||
end
|
||||
|
||||
-- decrypt(buf, at, n, key): n bytes (whole u32s; a trailing partial word is left as is) at buf[at..] in place
|
||||
-- whole u32s; a trailing partial word is left as is
|
||||
function Crypt.decrypt(buf: buffer, at: number, n: number, key: number)
|
||||
local s2 = 0xEEEEEEEE
|
||||
local k = key % TWO32
|
||||
@@ -57,7 +55,7 @@ function Crypt.decrypt(buf: buffer, at: number, n: number, key: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- base name of an archived path (the file key uses it)
|
||||
-- the file key uses the base name
|
||||
function Crypt.baseName(name: string): string
|
||||
return string.match(name, "([^\\/]*)$") or name
|
||||
end
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
--!strict
|
||||
-- archive/mpq: Blizzard MPQ archives (MoPaQ: Diablo, StarCraft, Warcraft II / III, WoW, StarCraft II), read into the
|
||||
-- standard Archive. Supports: header versions 1-4 with the classic hash and block tables (encrypted, found at offset
|
||||
-- 0, at any 512-byte boundary (prefixed archives / installers) or through the "MPQ\x1B" user data header), the hi-block
|
||||
-- table (files past 4 GB), names from "(listfile)" (plus opts.names), unnamed files as "File00000123.xxx" (block
|
||||
-- index), encrypted files (key from the name, MPQ_FILE_FIX_KEY), sector offset tables, single-unit files, imploded
|
||||
-- files (codecs/compress/Explode), compressed sectors: zlib (codecs/compress/Deflate), PKWARE DCL implode, bzip2
|
||||
-- (codecs/compress/Bzip2), LZMA (codecs/compress/Lzma), sparse, and combinations; deleted and delete-marker blocks
|
||||
-- are skipped. Written from the MPQ format description (Justin Olbrantz / Jean-Francois Roy, "The MoPaQ Archive
|
||||
-- Format", and StormLib's documentation); no code taken.
|
||||
-- Not supported (Err.UNSUPPORTED on read): Huffman and IMA ADPCM sectors (WAVE files), patch files, the HET / BET
|
||||
-- tables of archives without classic tables, encrypted files whose name is unknown, compressed hash / block tables,
|
||||
-- writing.
|
||||
-- archive/mpq: Blizzard MPQ ("The MoPaQ Archive Format", Olbrantz / Roy; StormLib docs; no code taken). Supports:
|
||||
-- header v1-4, hash / block / hi-block tables, prefixed archives, (listfile), encrypted files, imploded and zlib /
|
||||
-- PKWARE / bzip2 / LZMA / sparse sectors. Not supported: Huffman / ADPCM sectors, patch files, HET / BET, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -34,9 +25,9 @@ local Mpq = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
names: { string }?, -- extra file names to look up (beside the archive's own "(listfile)")
|
||||
names: { string }?,
|
||||
maxEntries: number?, -- default 1e6
|
||||
maxBytes: number?, -- the largest file read (default 2^30)
|
||||
maxBytes: number?, -- default 2^30
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -85,7 +76,6 @@ local function unsparse(b: buffer, at: number, n: number, want: number): (buffer
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- one compressed sector (mask byte first) -> want bytes
|
||||
local function decompress(b: buffer, at: number, n: number, want: number): (buffer?, Err.Err?)
|
||||
if n < 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "empty compressed sector")
|
||||
@@ -151,7 +141,6 @@ end
|
||||
|
||||
type Block = { offset: number, csize: number, size: number, flags: number }
|
||||
|
||||
-- read a file's bytes (the archive buffer, the absolute block offset, the file name for the key)
|
||||
local function readFile(b: buffer, limit: number, blk: Block, at: number, name: string?, sectorSize: number, maxBytes: number): (buffer?, Err.Err?)
|
||||
local flags = blk.flags
|
||||
if band(flags, FLAG_PATCH) ~= 0 then
|
||||
@@ -250,7 +239,6 @@ local function encode(_archive: any, _opts: any?): (buffer?, Err.Err?)
|
||||
return nil, Err.new(Err.UNSUPPORTED, "MPQ archives are read-only")
|
||||
end
|
||||
|
||||
-- the header: its absolute offset in r (0, a 512-byte boundary, or behind "MPQ\x1B") | nil
|
||||
local function findHeader(r: Bytes.Reader): number?
|
||||
local size = r.limit - r.base
|
||||
local p = 0
|
||||
@@ -328,7 +316,6 @@ function Mpq.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local maxBytes = opts and opts.maxBytes or 2 ^ 30
|
||||
local mask = hashCount - 1
|
||||
local pow2 = band(hashCount, mask) == 0
|
||||
-- lookup(name) -> block index (1-based) | nil
|
||||
local function lookup(name: string): number?
|
||||
local h0, h1, h2 = Crypt.hash(name, 0), Crypt.hash(name, 1), Crypt.hash(name, 2)
|
||||
local start = if pow2 then band(h0, mask) else h0 % hashCount
|
||||
@@ -352,7 +339,6 @@ function Mpq.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local blk = blocks[bi]
|
||||
return readFile(r.buf, r.limit, blk, r.base + h + blk.offset, name, sectorSize, maxBytes)
|
||||
end
|
||||
-- names: (listfile) and opts.names
|
||||
local names: { string } = {}
|
||||
for _, internal in { "(attributes)", "(signature)", "(user data)" } do
|
||||
table.insert(names, internal)
|
||||
@@ -413,7 +399,6 @@ function Mpq.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- hash(name, kind) -> the MPQ name hash (kind 0 = table index, 1 / 2 = name checks, 3 = file key)
|
||||
Mpq.hash = Crypt.hash
|
||||
|
||||
return Mpq
|
||||
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
-- archive/pak: Quake / Quake II / Half-Life PAK archives ("PACK": 56-byte names, 64-byte directory entries) and the
|
||||
-- SiN variant ("SPAK": 120-byte names), read into the standard Archive (entries read lazily: a copy of the stored
|
||||
-- bytes) and written from any Archive value. Written from the format description in the Quake source release
|
||||
-- (common.c: dpackheader_t / dpackfile_t) and the Quake specs; no code taken.
|
||||
-- Not supported: Daikatana's compressed PAK entries.
|
||||
-- archive/pak: Quake / Quake II / Half-Life PAK ("PACK": 56-byte names, 64-byte entries) and SiN ("SPAK": 120-byte
|
||||
-- names), from Quake's common.c (dpackheader_t / dpackfile_t) and the Quake specs; no code taken. Supports: read
|
||||
-- (lazy), write. Not supported: Daikatana's compressed entries.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -125,7 +123,6 @@ function Pak.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new(variant?) -> an empty PAK Archive: a:add(name, data); a:write()
|
||||
function Pak.new(variant: ("quake" | "sin")?): any
|
||||
local a = Archive.new("pak", encode)
|
||||
a.meta.source = { format = "pak", variant = variant or "quake" }
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
--!strict
|
||||
-- archive/rpm: RPM packages (format v3 / v4), read into the standard Archive value. The 96-byte lead, the signature
|
||||
-- header (padded to 8 bytes) and the main header are parsed (every tag into meta.rpm.tags; the common ones by name in
|
||||
-- meta.rpm); the file list comes from the header (dirnames / basenames / dirindexes, or the old filenames tag, with
|
||||
-- sizes / long sizes, modes, mtimes, link targets, owners) and lists without touching the payload. The payload
|
||||
-- (gzip / bzip2 / xz / lzma / zstd or none, through codecs/compress/*) is decompressed on the first read and parsed
|
||||
-- as a cpio archive (containers/Cpio); entry names drop the leading "./" and "/". Ghost files (%ghost) are not
|
||||
-- listed (they are not in the payload). Written from the RPM file format documentation (rpm.org, the LSB package
|
||||
-- format chapter); no code ported. Not supported: writing, signature / digest verification, the "stripped cpio"
|
||||
-- payload of packages with files over 4 GiB (Err.UNSUPPORTED on read), delta rpms.
|
||||
-- archive/rpm: RPM v3 / v4 (rpm.org, LSB package format; no code ported): 96-byte lead, signature header (8-byte
|
||||
-- aligned), main header; the payload (gzip / bzip2 / xz / lzma / zstd / none) is cpio; %ghost files not listed.
|
||||
-- Not supported: writing, signature / digest checks, "stripped cpio" (files over 4 GiB), delta rpms.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -30,7 +24,7 @@ local Rpm = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- the largest decompressed payload (default 2^30)
|
||||
maxBytes: number?, -- default 2^30
|
||||
maxEntries: number?, -- default 1e6
|
||||
}
|
||||
|
||||
@@ -39,7 +33,6 @@ local LEAD_MAGIC = "\237\171\238\219"
|
||||
local HEADER_MAGIC = "\142\173\232\1"
|
||||
local RPMFILE_GHOST = 64
|
||||
|
||||
-- the tags meta.rpm names
|
||||
local NAMES: { [number]: string } = {
|
||||
[1000] = "name",
|
||||
[1001] = "version",
|
||||
@@ -73,7 +66,6 @@ function Rpm.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a header structure at p -> { [tag] = value }, end offset | nil, Err
|
||||
local function header(b: buffer, p: number, e: number): (any, any)
|
||||
if p + 16 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "header past the end", p)
|
||||
@@ -146,7 +138,6 @@ local function header(b: buffer, p: number, e: number): (any, any)
|
||||
return tags, se
|
||||
end
|
||||
|
||||
-- the file list of the header: { name, size, mode, mtime, link, user, group, flags }
|
||||
local function files(tags: { [number]: any }): { any }
|
||||
local names: { string } = {}
|
||||
local bn, dn, di = tags[1117], tags[1118], tags[1116]
|
||||
@@ -181,7 +172,6 @@ local function strip(name: string): string
|
||||
return (string.gsub(name, "^%.?/+", ""))
|
||||
end
|
||||
|
||||
-- the payload: decompressed, then its cpio entries by stripped name
|
||||
local function payload(b: buffer, p: number, e: number, compressor: string?, o: DecodeOptions): (any, Err.Err?)
|
||||
local n = e - p
|
||||
local opts: any = { maxBytes = o.maxBytes or 2 ^ 30 }
|
||||
@@ -313,7 +303,6 @@ function Rpm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- cpio(r, opts?) -> the decompressed payload (a cpio archive, as rpm2cpio prints it) | nil, Err
|
||||
function Rpm.cpio(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?)
|
||||
local a, err = Rpm.decode(r, opts)
|
||||
if not a then
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
--!strict
|
||||
-- archive/tar: tar archives (ustar, pax, GNU long names; see containers/Tar), read into the standard Archive value
|
||||
-- (entries read lazily: a copy of the stored bytes) and written from any Archive value (a tar, a zip, a 7z: list() +
|
||||
-- read()). Compressed tars (.tar.gz / .tgz / .tar.zst / .tar.xz / .tar.bz2 / .tar.br) load through the api, which
|
||||
-- unwraps the single-stream archive and sniffs again (meta.compression names the outer format).
|
||||
-- Not supported: sparse files, multi-volume archives.
|
||||
-- archive/tar: tar archives (ustar, pax, GNU long names; containers/Tar). Supports: read (lazy), write from any
|
||||
-- Archive. Not supported: sparse files, multi-volume archives.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -110,7 +107,6 @@ function TarFormat.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty tar Archive: a:add(name, data, { mtime, mode, dir }); a:write()
|
||||
function TarFormat.new(): any
|
||||
local a = Archive.new("tar", encode)
|
||||
a.meta.source = { format = "tar" }
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- archive/uu: uuencoded, xxencoded and GNU "begin-base64" text as an Archive: every "begin <mode> <name>" block of a
|
||||
-- text (mail, news, shar parts: the text around the blocks is ignored) is one entry (entry.mode = the octal mode);
|
||||
-- one block makes a single-stream archive (meta.stream: the api unwraps a uuencoded .tar.gz). The variant of each
|
||||
-- block (uu / xx / base64) is detected from its characters and line lengths and kept in entry.variant and
|
||||
-- meta.source.variant. Written from the POSIX uuencode description and the xxencode alphabet; written as GNU sharutils
|
||||
-- does (45 bytes a line, "`" for zero, base64 lines of 60 characters + "===="). Not supported: multi-part postings
|
||||
-- split over several files (concatenate the parts first), yEnc (archive/yenc), BinHex (archive/binhex).
|
||||
-- archive/uu: uuencode (POSIX), xxencode and GNU "begin-base64" blocks, one entry each (variant detected per block);
|
||||
-- written as GNU sharutils does (45 bytes a line, "`" for zero; base64 60 characters + "===="). Not supported: postings
|
||||
-- split over several files, yEnc (archive/yenc), BinHex (archive/binhex).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -23,11 +19,11 @@ local Uu = {
|
||||
caps = { decode = true, encode = true, variants = { "uu", "xx", "base64" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = { maxBytes: number?, name: string? } -- maxBytes: default 2^30
|
||||
export type DecodeOptions = { maxBytes: number?, name: string? } -- default 2^30
|
||||
export type EncodeOptions = {
|
||||
variant: ("uu" | "xx" | "base64")?, -- default "uu"
|
||||
mode: number?, -- file mode when the entry has none (default 0o644)
|
||||
name: string?, -- the name of a buffer / string value (default "data")
|
||||
mode: number?, -- default 0o644
|
||||
name: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -50,7 +46,6 @@ function Uu.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the value of character c in a variant, or nil
|
||||
local function value(c: number, xx: boolean): number?
|
||||
if xx then
|
||||
return XX_VALUE[c]
|
||||
@@ -61,7 +56,6 @@ local function value(c: number, xx: boolean): number?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- does a data line fit the variant: every character valid and the length matching the count character
|
||||
local function fits(line: string, xx: boolean): boolean
|
||||
local n = value(string.byte(line, 1) or 0, xx)
|
||||
if not n then
|
||||
@@ -79,7 +73,6 @@ local function fits(line: string, xx: boolean): boolean
|
||||
return true
|
||||
end
|
||||
|
||||
-- decode the uu / xx lines of a block into a buffer (nil, message on a bad line)
|
||||
local function decodeLines(lines: { string }, xx: boolean, maxBytes: number): (buffer?, string?)
|
||||
local total = 0
|
||||
for _, l in lines do
|
||||
@@ -266,7 +259,6 @@ function Uu.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty uu Archive: a:add(name, data, { mode }); a:write({ variant })
|
||||
function Uu.new(): any
|
||||
local a = Archive.new("uu", encoder)
|
||||
a.meta.source = { format = "uu" }
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
--!strict
|
||||
-- archive/vpk: Valve VPK archives (Source / Source 2: version 1 and 2 directory files, "pak01_dir.vpk"), read into the
|
||||
-- standard Archive and written from any Archive value. Reading: the extension / path / name tree, preload bytes
|
||||
-- (stored in the tree) followed by the data, data embedded in the directory file (archive index 0x7fff) or in the
|
||||
-- numbered part files "<name>_NNN.vpk", found through opts.resolve(fileName) / opts.files[fileName] (the name comes
|
||||
-- from opts.name, "x_dir.vpk" -> "x_000.vpk", default "pak01"); each entry's CRC-32 is checked on read; version 2's
|
||||
-- MD5 section is kept in meta. Writing: one self-contained directory file (every entry embedded, no preload),
|
||||
-- version 2 by default with its tree / chunk / file MD5s (version = 1 for the older header). Written from the
|
||||
-- Valve Developer Community VPK file format page; no code taken.
|
||||
-- Not supported: Respawn (Titanfall) VPKs, signature checks, writing multi-part archives.
|
||||
-- archive/vpk: Valve VPK v1 / v2 directory files (Valve Developer Community page; no code taken): preload bytes,
|
||||
-- embedded (index 0x7fff) or part-file data ("<name>_NNN.vpk"), CRC-32 checked; writes one self-contained v2 file
|
||||
-- with its MD5s. Not supported: Respawn (Titanfall) VPKs, signature checks, writing multi-part archives.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -29,10 +23,10 @@ local Vpk = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
verify: boolean?, -- check each entry's CRC-32 on read (default true)
|
||||
resolve: ((name: string) -> (buffer | string)?)?, -- the part files ("pak01_000.vpk", ...)
|
||||
verify: boolean?, -- default true
|
||||
resolve: ((name: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
name: string?, -- the directory file's name ("pak01_dir.vpk"), for the part file names
|
||||
name: string?, -- "pak01_dir.vpk" -> "pak01_000.vpk"
|
||||
maxEntries: number?, -- default 1e6
|
||||
}
|
||||
export type EncodeOptions = { version: number? } -- 1 or 2 (default: the source's, else 2)
|
||||
@@ -55,7 +49,6 @@ local function encode(archive: any, opts: any?): (buffer?, Err.Err?)
|
||||
return Vpk.encode(archive, opts)
|
||||
end
|
||||
|
||||
-- the part file `index` as a buffer (cached per archive)
|
||||
local function partLoader(opts: DecodeOptions?): (number) -> (buffer?, Err.Err?)
|
||||
local cache: { [number]: buffer } = {}
|
||||
local prefix = "pak01"
|
||||
@@ -307,7 +300,6 @@ function Vpk.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new(version?) -> an empty VPK Archive (version 2 by default): a:add(name, data); a:write()
|
||||
function Vpk.new(version: number?): any
|
||||
local a = Archive.new("vpk", encode)
|
||||
a.meta.source = { format = "vpk", version = version or 2 }
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
--!strict
|
||||
-- archive/wad/Map: a Doom / Heretic / Hexen map (the binary lumps after a map marker: THINGS, LINEDEFS, SIDEDEFS,
|
||||
-- VERTEXES, SECTORS; Hexen format when BEHAVIOR is present) parsed into plain tables (Map.parse) and built into a
|
||||
-- Scene (Map.scene): sector floors and ceilings (sector outlines chained from the linedefs, triangulated with
|
||||
-- codecs/model/Polygon), walls (one-sided middles; lower / upper walls and masked middles of two-sided lines, the
|
||||
-- sky hack between two sky ceilings) with the vanilla texture alignment (x / y offsets, upper / lower unpegged), one
|
||||
-- primitive per flat / wall texture, things as child nodes standing on their sector's floor. Units are map units, z
|
||||
-- up. Written from the Unofficial Doom Specs and the Hexen specs; no code taken. UDMF (TEXTMAP) maps are not
|
||||
-- supported.
|
||||
-- archive/wad/Map: Doom / Heretic / Hexen binary map lumps (Hexen when BEHAVIOR is present) to tables and a Scene, with
|
||||
-- vanilla texture alignment (offsets, unpegged) and the sky hack; z up, map units. From the Unofficial Doom Specs and
|
||||
-- the Hexen specs; no code taken. Not supported: UDMF (TEXTMAP).
|
||||
local Types = require("@rosetta/Types")
|
||||
local Polygon = require("@rosetta/codecs/model/Polygon")
|
||||
|
||||
@@ -29,7 +24,6 @@ export type MapData = {
|
||||
vertices: { number }, -- x1, y1, x2, y2, ...
|
||||
sectors: { Sector },
|
||||
}
|
||||
-- texture sizes / images by kind ("flat" | "texture") and name: what the WAD knows
|
||||
export type Textures = {
|
||||
size: (kind: string, name: string) -> (number?, number?),
|
||||
image: ((kind: string, name: string) -> Types.Image?)?,
|
||||
@@ -46,7 +40,6 @@ local function side(v: number): number?
|
||||
return if v == 0xFFFF then nil else v + 1
|
||||
end
|
||||
|
||||
-- parse(lumps, hexen) -> MapData | nil, message: lumps = { THINGS, LINEDEFS, SIDEDEFS, VERTEXES, SECTORS } (Lump)
|
||||
function Map.parse(lumps: { [string]: Lump }, hexen: boolean): (MapData?, string?)
|
||||
for _, k in { "LINEDEFS", "SIDEDEFS", "VERTEXES", "SECTORS" } do
|
||||
if not lumps[k] then
|
||||
@@ -144,9 +137,8 @@ function Map.parse(lumps: { [string]: Lump }, hexen: boolean): (MapData?, string
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sector outlines
|
||||
-- loops(m) -> loops[sector] = { { vertex ids } }: each sector's edges (front sides v1 -> v2, back sides v2 -> v1;
|
||||
-- lines with the sector on both sides left out) chained into closed loops, taking the sharpest right turn at forks;
|
||||
-- chains that do not close are dropped
|
||||
-- sector loops: front sides v1 -> v2, back sides v2 -> v1, lines with the sector on both sides left out; the sharpest
|
||||
-- right turn at forks; unclosed chains dropped
|
||||
local function loops(m: MapData): { [number]: { { number } } }
|
||||
local edges: { [number]: { { number } } } = {}
|
||||
local nv = #m.vertices // 2
|
||||
@@ -247,7 +239,6 @@ type Batch = { kind: string, name: string, pos: { number }, nrm: { number }, uv:
|
||||
|
||||
local LOWER_UNPEGGED, UPPER_UNPEGGED, TWO_SIDED = 16, 8, 4
|
||||
|
||||
-- scene(m, tex, name) -> Scene
|
||||
function Map.scene(m: MapData, tex: Textures, name: string?): Types.Scene
|
||||
local batches: { [string]: Batch } = {}
|
||||
local order: { string } = {}
|
||||
@@ -269,7 +260,6 @@ function Map.scene(m: MapData, tex: Textures, name: string?): Types.Scene
|
||||
end
|
||||
local V = m.vertices
|
||||
local nv = #V // 2
|
||||
-- floors and ceilings
|
||||
local secLoops = loops(m)
|
||||
for sec, ls in secLoops do
|
||||
local s = m.sectors[sec]
|
||||
@@ -301,7 +291,6 @@ function Map.scene(m: MapData, tex: Textures, name: string?): Types.Scene
|
||||
end
|
||||
end
|
||||
end
|
||||
-- walls
|
||||
local function quad(tname: string, x1: number, y1: number, x2: number, y2: number, zb: number, zt: number, xoff: number, len: number, texTop: number, yoff: number)
|
||||
if tname == "-" or tname == "" or zt <= zb then
|
||||
return
|
||||
@@ -363,7 +352,6 @@ function Map.scene(m: MapData, tex: Textures, name: string?): Types.Scene
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the scene
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
local mesh: Types.Mesh = { name = name, primitives = {} }
|
||||
@@ -410,7 +398,6 @@ function Map.scene(m: MapData, tex: Textures, name: string?): Types.Scene
|
||||
table.insert(scene.meshes, mesh)
|
||||
scene.nodes[1] = { name = name, children = {}, mesh = 1 }
|
||||
scene.roots[1] = 1
|
||||
-- things, on the floor of the sector under them
|
||||
for i, th in m.things do
|
||||
local z = th.z or 0
|
||||
for sec, ls in secLoops do
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- archive/wad/Picture: Doom graphics lumps to rgba8 through a u32 palette (Pixels.palette of PLAYPAL): the column
|
||||
-- "picture" format of patches, sprites and menu graphics (width, height, left / top offsets, column offsets, posts of
|
||||
-- topdelta / length / pad / pixels / pad; DeePsea tall patches: a topdelta not above the previous one is relative)
|
||||
-- and raw flats (64 x 64, 64 x 128, 128 x 128, 256 x 256, Heretic's 64 x 65 / 64 x 200 and any w x 64 strip).
|
||||
-- Transparent picture pixels are 0 (alpha 0). Written from the Unofficial Doom Specs and the Doom source release's
|
||||
-- r_data.c description; no code taken. Never throws: every read is checked against the lump's end.
|
||||
-- archive/wad/Picture: Doom picture lumps (column posts: topdelta / length / pad / pixels / pad; DeePsea tall patches:
|
||||
-- a topdelta not above the previous is relative) and raw flats to rgba8, from the Unofficial Doom Specs and r_data.c;
|
||||
-- no code taken. Transparent pixels are 0.
|
||||
local readu8, readu16, readi16, readu32, writeu32 = buffer.readu8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.writeu32
|
||||
|
||||
local Picture = {}
|
||||
|
||||
-- valid(b, at, n) -> width, height, left, top when the lump looks like a picture (every column offset inside it)
|
||||
function Picture.header(b: buffer, at: number, n: number): (number?, number, number, number)
|
||||
if n < 8 then
|
||||
return nil, 0, 0, 0
|
||||
@@ -28,7 +24,6 @@ function Picture.header(b: buffer, at: number, n: number): (number?, number, num
|
||||
return w, h, readi16(b, at + 4), readi16(b, at + 6)
|
||||
end
|
||||
|
||||
-- decode(b, at, n, pal, out) -> true | false, message: the picture's pixels into out (w * h * 4 bytes, zeroed)
|
||||
function Picture.decode(b: buffer, at: number, n: number, pal: buffer, out: buffer, w: number, h: number): (boolean, string?)
|
||||
local stop = at + n
|
||||
for x = 0, w - 1 do
|
||||
@@ -63,7 +58,6 @@ function Picture.decode(b: buffer, at: number, n: number, pal: buffer, out: buff
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- flatSize(n) -> width, height of a raw flat of n bytes (nil when no shape fits)
|
||||
function Picture.flatSize(n: number): (number?, number)
|
||||
if n == 4096 then
|
||||
return 64, 64
|
||||
@@ -84,15 +78,12 @@ function Picture.flatSize(n: number): (number?, number)
|
||||
return nil, 0
|
||||
end
|
||||
|
||||
-- flat(b, at, w, h, pal, out): raw indexes, row by row
|
||||
function Picture.flat(b: buffer, at: number, w: number, h: number, pal: buffer, out: buffer)
|
||||
for i = 0, w * h - 1 do
|
||||
writeu32(out, i * 4, readu32(pal, readu8(b, at + i) * 4))
|
||||
end
|
||||
end
|
||||
|
||||
-- blit(src, sw, sh, dst, dw, dh, ox, oy): the non-transparent pixels of an rgba8 image onto another at (ox, oy),
|
||||
-- clipped (composite wall textures from patches)
|
||||
function Picture.blit(src: buffer, sw: number, sh: number, dst: buffer, dw: number, dh: number, ox: number, oy: number)
|
||||
local x0, x1 = math.max(0, -ox), math.min(sw, dw - ox) - 1
|
||||
for y = math.max(0, -oy), math.min(sh, dh - oy) - 1 do
|
||||
|
||||
@@ -1,20 +1,7 @@
|
||||
--!strict
|
||||
-- archive/wad: Doom-engine WAD files (IWAD / PWAD: Doom, Heretic, Hexen, Strife, Freedoom, ...), read into the
|
||||
-- standard Archive and written from any Archive value, plus the lumps as game data:
|
||||
-- Wad.lumps(a) every lump in file order with its namespace (sprites / patches / flats / ...) and map
|
||||
-- Wad.palette(a, i?) PLAYPAL palette i (default 0) as a u32 palette buffer
|
||||
-- Wad.image(a, name) a flat, patch, sprite or graphic (the column picture format) as an rgba8 Image
|
||||
-- (meta.offset = { left, top } for pictures; transparent pixels alpha 0)
|
||||
-- Wad.texture(a, name) a wall texture composed from its patches (TEXTURE1 / TEXTURE2 + PNAMES)
|
||||
-- Wad.maps(a) Wad.mapData(a, name) Wad.map(a, name, opts)
|
||||
-- the maps; their things / linedefs / sidedefs / vertices / sectors as tables; a Scene
|
||||
-- (floors, ceilings and walls per texture, things as nodes: archive/wad/Map)
|
||||
-- Entry names: the lump names, except lumps of a map, named "<map>/<lump>" ("MAP01/THINGS"), and later lumps
|
||||
-- repeating an earlier name, named "<name>~<n>" (n = 2, 3, ...), so every entry is unique; writing strips both again
|
||||
-- (the last path component, without its "~n", upper-cased, at most 8 bytes). Written from the Unofficial Doom
|
||||
-- Specs (Matthew Fell) and the Doom source release's w_wad.c / r_data.c description; no code taken.
|
||||
-- Not supported: UDMF (TEXTMAP) map geometry (its lumps are listed and readable), Strife's TEXTURE format variant,
|
||||
-- big-endian (Jaguar) WADs, compressed Jaguar lumps.
|
||||
-- archive/wad: Doom-engine WADs (IWAD / PWAD; Unofficial Doom Specs, w_wad.c / r_data.c; no code taken), plus lumps,
|
||||
-- palettes, pictures, wall textures and maps. Entry names: "<map>/<lump>", repeats "<name>~<n>". Not supported: UDMF
|
||||
-- geometry (lumps listed), Strife's TEXTURE variant, big-endian (Jaguar) WADs, compressed Jaguar lumps.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -39,18 +26,18 @@ local Wad = {
|
||||
export type DecodeOptions = { maxEntries: number? } -- default 1e6
|
||||
export type EncodeOptions = { type: ("IWAD" | "PWAD")? } -- default: the source's, else "PWAD"
|
||||
export type Lump = {
|
||||
name: string, -- the lump name as stored (upper case)
|
||||
entry: string, -- the Archive entry name
|
||||
index: number, -- 1-based position in the directory
|
||||
name: string,
|
||||
entry: string,
|
||||
index: number,
|
||||
offset: number,
|
||||
size: number,
|
||||
namespace: string?, -- "sprites" | "patches" | "flats" | "colormaps" | "textures" (between *_START / *_END markers)
|
||||
map: string?, -- the map this lump belongs to
|
||||
namespace: string?, -- between *_START / *_END markers
|
||||
map: string?,
|
||||
marker: boolean?, -- a zero-size marker (F_START, MAP01, ...)
|
||||
}
|
||||
export type MapOptions = {
|
||||
textures: boolean?, -- build texture images (flats / composed wall textures) into scene.textures (default true)
|
||||
palette: number?, -- PLAYPAL palette index (default 0)
|
||||
textures: boolean?, -- default true
|
||||
palette: number?, -- default 0
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -139,7 +126,6 @@ function Wad.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
if isMapMarker then
|
||||
map, udmf = name, nextName == "TEXTMAP"
|
||||
end
|
||||
-- namespaces
|
||||
local marker: boolean = false
|
||||
local pre = string.match(name, "^(%w+)_START$")
|
||||
local post = string.match(name, "^(%w+)_END$")
|
||||
@@ -215,7 +201,7 @@ local function info(a: any): any
|
||||
return type(a) == "table" and type(a.meta) == "table" and a.meta.wad or nil
|
||||
end
|
||||
|
||||
-- the last lump named name (PWAD order: later lumps override), preferring namespace ns when given
|
||||
-- the last lump named name (PWAD order: later lumps override)
|
||||
local function find(w: any, name: string, ns: string?): Lump?
|
||||
local up = string.upper(name)
|
||||
local best: Lump? = nil
|
||||
@@ -238,20 +224,17 @@ local function find(w: any, name: string, ns: string?): Lump?
|
||||
return best
|
||||
end
|
||||
|
||||
-- new(type?) -> an empty WAD Archive ("PWAD" by default): a:add(name, data); a:write()
|
||||
function Wad.new(kind: ("IWAD" | "PWAD")?): any
|
||||
local a = Archive.new("wad", encode)
|
||||
a.meta.source = { format = "wad", type = kind or "PWAD" }
|
||||
return a
|
||||
end
|
||||
|
||||
-- lumps(a) -> { Lump } in directory order (nil when a is not a decoded WAD)
|
||||
function Wad.lumps(a: any): { Lump }?
|
||||
local w = info(a)
|
||||
return w and w.lumps
|
||||
end
|
||||
|
||||
-- palette(a, index?) -> a 256-entry u32 palette (Pixels layout) from PLAYPAL | nil, Err
|
||||
function Wad.palette(a: any, index: number?): (buffer?, Err.Err?)
|
||||
local w = info(a)
|
||||
if not w then
|
||||
@@ -265,8 +248,6 @@ function Wad.palette(a: any, index: number?): (buffer?, Err.Err?)
|
||||
return Pixels.palette(w.buf, w.base + l.offset + i * 768, 256), nil
|
||||
end
|
||||
|
||||
-- image(a, name, opts?) -> an rgba8 Image of a flat (in the flats namespace) or a picture (patch, sprite, graphic)
|
||||
-- | nil, Err. opts.palette: the PLAYPAL index (default 0); opts.kind: "flat" | "picture" to force the decoding.
|
||||
function Wad.image(a: any, name: string, opts: { palette: number?, kind: string? }?): (Types.Image?, Err.Err?)
|
||||
local w = info(a)
|
||||
if not w then
|
||||
@@ -307,7 +288,6 @@ function Wad.image(a: any, name: string, opts: { palette: number?, kind: string?
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- the texture definitions (TEXTURE1 + TEXTURE2 with PNAMES): name -> { width, height, patches = { { x, y, patch } } }
|
||||
local function textureDefs(w: any): { [string]: any }
|
||||
if w.textures then
|
||||
return w.textures
|
||||
@@ -359,7 +339,6 @@ local function textureDefs(w: any): { [string]: any }
|
||||
return defs
|
||||
end
|
||||
|
||||
-- texture(a, name, opts?) -> the composed rgba8 Image of a wall texture | nil, Err (opts.palette)
|
||||
function Wad.texture(a: any, name: string, opts: { palette: number? }?): (Types.Image?, Err.Err?)
|
||||
local w = info(a)
|
||||
if not w then
|
||||
@@ -383,7 +362,6 @@ function Wad.texture(a: any, name: string, opts: { palette: number? }?): (Types.
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- maps(a) -> { map names } in file order
|
||||
function Wad.maps(a: any): { string }
|
||||
local w = info(a)
|
||||
local out: { string } = {}
|
||||
@@ -399,7 +377,6 @@ function Wad.maps(a: any): { string }
|
||||
return out
|
||||
end
|
||||
|
||||
-- mapData(a, name) -> Map.MapData (things, linedefs, sidedefs, vertices, sectors as tables, 1-based references)
|
||||
function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
|
||||
local w = info(a)
|
||||
if not w then
|
||||
@@ -425,9 +402,6 @@ function Wad.mapData(a: any, name: string): (Map.MapData?, Err.Err?)
|
||||
return m, nil
|
||||
end
|
||||
|
||||
-- map(a, name, opts?) -> a Scene of the map (z up, map units): one primitive per flat / wall texture (materials and
|
||||
-- textures named after them; texture.image when opts.textures ~= false and the WAD holds it), things as child nodes
|
||||
-- (extras.thing = { type, angle, flags, ... }); meta.source.map = the MapData
|
||||
function Wad.map(a: any, name: string, opts: MapOptions?): (Types.Scene?, Err.Err?)
|
||||
local m, err = Wad.mapData(a, name)
|
||||
if not m then
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
--!strict
|
||||
-- archive/xz: .xz (xz file format 1.2) and .lzma ("LZMA alone") files, read as single-stream archives holding one
|
||||
-- entry (the decompressed data). Built on codecs/compress/Lzma. Supports: concatenated streams, stream padding, every block
|
||||
-- layout, checks none / CRC-32 / CRC-64 (verified; SHA-256 is not verified), filters LZMA2, delta and the BCJ filters
|
||||
-- archive/xz: .xz (xz file format 1.2) and .lzma ("LZMA alone") as single-stream archives (codecs/compress/Lzma).
|
||||
-- Supports: concatenated streams, padding, checks none / CRC-32 / CRC-64 (SHA-256 unverified), LZMA2, delta, BCJ
|
||||
-- x86 / PowerPC / ARM / ARM-Thumb / SPARC / ARM64. Not supported: writing, IA-64 / RISC-V filters.
|
||||
-- The entry is named after the file (opts.name without .xz / .lzma; .txz -> .tar), else "data". meta.stream = true:
|
||||
-- the api unwraps an inner archive (a .tar.xz loads as the tar).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -24,8 +21,8 @@ local Xz = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse output larger than this (default 2^30)
|
||||
name: string?, -- the file name (the api passes it): names the entry
|
||||
maxBytes: number?, -- default 2^30
|
||||
name: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- archive/yenc: yEnc-encoded binaries (yEnc 1.3: =ybegin / =ypart / =yend, byte + 42 with "=" escapes) as an Archive:
|
||||
-- every =ybegin block is a file; the parts of a multi-part file found in the same text are joined at their =ypart
|
||||
-- begin offsets. The part CRC (pcrc32) and, once all parts are there, the file CRC (crc32) and size are verified
|
||||
-- (Err.CHECKSUM / Err.CORRUPT); missing parts leave a short file with meta.incomplete listing it. One file makes a
|
||||
-- single-stream archive (meta.stream). Written from the yEnc 1.3 draft (Jürgen Helbing); writing produces one
|
||||
-- single-part block per file with 128-character lines, escaping NUL, LF, CR, "=" and a leading / trailing TAB, space
|
||||
-- or ".", as uuenview / yencode do. Not supported: joining parts spread over several files (concatenate them first).
|
||||
-- archive/yenc: yEnc 1.3 (=ybegin / =ypart / =yend, byte + 42, "=" escapes; Jürgen Helbing's draft): parts in one text
|
||||
-- joined, pcrc32 / crc32 / size verified; writes one 128-character-line part per file, escaping NUL, LF, CR, "=" and a
|
||||
-- leading / trailing TAB, space or ".", as uuenview / yencode do. Not supported: parts spread over several files.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -23,10 +19,10 @@ local Yenc = {
|
||||
caps = { decode = true, encode = true, multipart = true },
|
||||
}
|
||||
|
||||
export type DecodeOptions = { maxBytes: number?, name: string? } -- maxBytes: default 2^30
|
||||
export type DecodeOptions = { maxBytes: number?, name: string? } -- default 2^30
|
||||
export type EncodeOptions = {
|
||||
line: number?, -- characters per line (default 128)
|
||||
name: string?, -- the name of a buffer / string value (default "data")
|
||||
line: number?, -- default 128
|
||||
name: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -53,7 +49,6 @@ local function fields(line: string): { [string]: string }
|
||||
return f
|
||||
end
|
||||
|
||||
-- decode the data lines [i, j) of src into out at o; returns the new o (stops at the end of out)
|
||||
local function decodeData(src: buffer, i: number, j: number, out: buffer, o: number): number
|
||||
local lim = buffer.len(out)
|
||||
local esc = false
|
||||
@@ -78,7 +73,6 @@ local function decodeData(src: buffer, i: number, j: number, out: buffer, o: num
|
||||
return o
|
||||
end
|
||||
|
||||
-- count the decoded bytes of the data lines [i, j)
|
||||
local function countData(src: buffer, i: number, j: number): number
|
||||
local n, esc = 0, false
|
||||
for p = i, j - 1 do
|
||||
@@ -267,7 +261,6 @@ function Yenc.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty yEnc Archive: a:add(name, data); a:write({ line })
|
||||
function Yenc.new(): any
|
||||
local a = Archive.new("yenc", encoder)
|
||||
a.meta.source = { format = "yenc" }
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
-- archive/z: Unix compress files (.Z: LZW, magic 1F 9D, 9..16-bit codes, block mode) as a single-stream Archive: one
|
||||
-- entry, named after the file without .Z (.taz / .tZ -> .tar), else "data"; meta.stream = true, so the api unwraps a
|
||||
-- .tar.Z into the tar. Writing: an Archive with one file entry (or a buffer / string) becomes that file compressed,
|
||||
-- byte for byte what ncompress writes; an Archive with several entries is written as a tar inside (a .tar.Z). The LZW is
|
||||
-- codecs/compress/UnixCompress. Not supported: pack (.z, 1F 1E) and compact (1F FF) files.
|
||||
-- archive/z: Unix compress (.Z: LZW, magic 1F 9D, 9..16-bit codes, block mode) as a single-stream archive of one
|
||||
-- entry (codecs/compress/UnixCompress). Supports: read; write byte for byte as ncompress (several entries: a .tar.Z).
|
||||
-- Not supported: pack (.z, 1F 1E) and compact (1F FF) files.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -22,8 +20,8 @@ local Z = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse output larger than this (default 2^30)
|
||||
name: string?, -- the file name (the api passes it): names the entry
|
||||
maxBytes: number?, -- default 2^30
|
||||
name: string?,
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
maxBits: number?, -- 9..16 (compress -b), default 16
|
||||
@@ -39,7 +37,6 @@ function Z.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the entry name: the file name without .Z; .taz / .tz name a tar
|
||||
local function entryName(opts: DecodeOptions): string
|
||||
local name = Archive.streamName(opts, { "z" })
|
||||
local stem, ext = string.match(name, "^(.+)%.(%w+)$")
|
||||
@@ -84,7 +81,6 @@ function Z.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new(data?, name?) -> a .Z Archive holding one entry
|
||||
function Z.new(data: (buffer | string)?, name: string?): any
|
||||
local a = Archive.new("z", encode)
|
||||
a.meta.stream = true
|
||||
|
||||
@@ -1,18 +1,7 @@
|
||||
--!strict
|
||||
-- archive/zip: ZIP archives as the standard Archive (Types.Archive: list / read / add / write) on containers/Zip.
|
||||
-- Absorbed from zip-luau 121d4b0 (same author, no third-party code; the reader, now containers/Zip) plus a new
|
||||
-- writer. Supports: reading stored and deflated entries (CRC-checked), ZIP64, data descriptors, archive comments,
|
||||
-- prefixed archives (self-extractors), UTF-8 names, case-insensitive / basename lookup; writing stored and deflated
|
||||
-- entries (codecs/compress/Deflate), unchanged entries copied without recompressing, ZIP64 when needed.
|
||||
-- Not supported: encryption (traditional PKWARE or AES), compression methods other than store / deflate (bzip2,
|
||||
-- LZMA, zstd entries are listed but not readable), multi-disk archives.
|
||||
--
|
||||
-- local a = Zip.decode(Bytes.reader(buf)) -- or F.archive.load(buf)
|
||||
-- for _, e in a:list() do print(e.name, e.size, e.method) end
|
||||
-- local data = a:read("dir/file.txt")
|
||||
-- a:add("new.txt", "hello", { level = 9 }) -- or { method = "store", mtime = os.time() }
|
||||
-- local out = a:write({ comment = "made here" }) -- or Zip.encode(a) / F.save(a, nil, { format = "zip" })
|
||||
-- local b = Zip.create() -- an empty archive
|
||||
-- archive/zip: ZIP archives on containers/Zip, absorbed from zip-luau 121d4b0 (same author) plus a new writer.
|
||||
-- Supports: stored / deflated entries (CRC-checked), ZIP64, data descriptors, comments, self-extractors, UTF-8 names;
|
||||
-- writing, unchanged entries copied. Not supported: encryption, methods other than store / deflate, multi-disk.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -32,7 +21,7 @@ local Zip = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
verify: boolean?, -- check every entry's CRC-32 on read (default true)
|
||||
verify: boolean?, -- default true
|
||||
}
|
||||
export type AddOptions = Container.AddOptions
|
||||
export type WriteOptions = Container.FinishOptions
|
||||
@@ -78,7 +67,6 @@ function Zip.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- decode(r, opts?) -> Archive | nil, Err
|
||||
function Zip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?, Err.Err?)
|
||||
local zip, err = Container.open(r)
|
||||
if not zip then
|
||||
@@ -87,14 +75,11 @@ function Zip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?, Err
|
||||
return new(zip, not (opts and opts.verify == false)), nil
|
||||
end
|
||||
|
||||
-- create() -> an empty Archive
|
||||
function Zip.create(): Types.Archive
|
||||
return new(nil, true)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(archive, opts?) -> buffer | nil, Err. archive: an Archive (any format's: its entries are read and written
|
||||
-- here), or a plain table { [name] = buffer | string }.
|
||||
function Zip.encode(value: any, opts: WriteOptions?): (buffer?, Err.Err?)
|
||||
if type(value) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "encode: needs an Archive or a { name = data } table")
|
||||
@@ -132,9 +117,7 @@ function Zip.encode(value: any, opts: WriteOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- the Archive methods (Types.Archive)
|
||||
|
||||
-- list() -> { ArchiveEntry } in archive order (+ crc32, comment)
|
||||
function Archive.list(self: any): { Types.ArchiveEntry }
|
||||
local out = {}
|
||||
for _, name in self._order do
|
||||
@@ -165,7 +148,6 @@ function Archive.list(self: any): { Types.ArchiveEntry }
|
||||
return out
|
||||
end
|
||||
|
||||
-- read(name) -> buffer | nil, Err: an exact name, else the reader's case-insensitive / basename lookup
|
||||
function Archive.read(self: any, name: string): (buffer?, Err.Err?)
|
||||
if type(name) ~= "string" then
|
||||
return nil, Err.new(Err.ARG, "read: name must be a string")
|
||||
@@ -188,7 +170,6 @@ function Archive.read(self: any, name: string): (buffer?, Err.Err?)
|
||||
return nil, Err.new(Err.ARG, "no entry " .. name)
|
||||
end
|
||||
|
||||
-- add(name, data, opts?): add or replace an entry (data: buffer or string; nil for a directory "name/")
|
||||
function Archive.add(self: any, name: string, data: any, opts: AddOptions?)
|
||||
local b: buffer = if type(data) == "string" then buffer.fromstring(data) elseif type(data) == "buffer" then data else buffer.create(0)
|
||||
if self._items[name] == nil then
|
||||
@@ -197,7 +178,6 @@ function Archive.add(self: any, name: string, data: any, opts: AddOptions?)
|
||||
self._items[name] = { data = b, opts = opts or {} }
|
||||
end
|
||||
|
||||
-- remove(name) -> true when there was such an entry
|
||||
function Archive.remove(self: any, name: string): boolean
|
||||
if self._items[name] == nil then
|
||||
return false
|
||||
@@ -210,7 +190,6 @@ function Archive.remove(self: any, name: string): boolean
|
||||
return true
|
||||
end
|
||||
|
||||
-- write(opts?) -> buffer | nil, Err: the archive with every change; unchanged entries are copied as they are
|
||||
function Archive.write(self: any, opts: WriteOptions?): (buffer?, Err.Err?)
|
||||
local w = Container.writer()
|
||||
for _, name in self._order do
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
--!strict
|
||||
-- archive/zoo: Zoo archives (Rahul Dhesi's zoo 1.x / 2.x) read into an Archive: the archive header, the chain of
|
||||
-- directory entries (type 1, and type 2 with long names, directory names, Unix-style attributes and version numbers),
|
||||
-- deleted entries skipped, entries stored (method 0), LZW-compressed (method 1: codecs/compress/Lzw "zoo", 13-bit
|
||||
-- codes) or lzh-compressed (method 2: codecs/compress/Lzh "lh5"); every file's CRC-16 (ARC) is verified when read
|
||||
-- (Err.CHECKSUM). MS-DOS dates read as UTC. Written from zoo 2.10's file format description (zoo.h / ar.h). Not
|
||||
-- supported: writing, file generations beyond keeping the newest (older versions of a name get ";<version>").
|
||||
-- archive/zoo: Zoo 1.x / 2.x (zoo 2.10's zoo.h / ar.h): directory entries type 1 and 2, methods 0 stored, 1 LZW
|
||||
-- (13-bit), 2 lh5; CRC-16 (ARC) verified; MS-DOS dates as UTC. Not supported: writing, file generations beyond the
|
||||
-- newest (older versions of a name get ";<version>").
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -25,7 +22,7 @@ local Zoo = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse an entry larger than this (default 2^30)
|
||||
maxBytes: number?, -- default 2^30
|
||||
maxEntries: number?, -- default 65536
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
--!strict
|
||||
-- archive/zst: a Zstandard-compressed file (.zst, RFC 8878) as a single-stream archive holding one entry, decoded
|
||||
-- through codecs/compress/Zstd. The entry is named after the file without its extension (opts.name; ".tzst" -> ".tar"), else
|
||||
-- "data". The api unwraps it when the entry is itself an archive (.tar.zst loads as the tar). Read-only.
|
||||
-- archive/zst: a Zstandard file (.zst, RFC 8878) as a single-stream archive of one entry (codecs/compress/Zstd).
|
||||
-- Supports: read; the entry is named after the file without its extension (".tzst" -> ".tar"), else "data".
|
||||
-- Not supported: dictionaries, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
@@ -21,9 +20,9 @@ local Zst = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a decompressed size over this (default 2^30)
|
||||
verify: boolean?, -- check the content checksum (default true)
|
||||
name: string?, -- the file name, for the entry name (the api passes it)
|
||||
maxBytes: number?, -- default 2^30
|
||||
verify: boolean?, -- default true
|
||||
name: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
--!strict
|
||||
-- cad/step/Brep: the injected B-rep plug-in (Rosetta.plugins.brep; rosetta never requires a kernel). Normalises
|
||||
-- the plug-in table, calls it under pcall (kernels throw on bad geometry) and turns its tessellated meshes into a
|
||||
-- Scene. Interpreted on purpose: the plug-in's errors unwind through here.
|
||||
--
|
||||
-- The interface (every function may throw; results are checked):
|
||||
-- read(text: string, opts) -> { bodies = { Body }, stats = { ... }? } STEP text -> B-rep bodies (body.name?,
|
||||
-- body.color? = Color3 | { r, g, b } | { 1, 2, 3 })
|
||||
-- write(bodies: { Body }, opts) -> string bodies -> STEP text
|
||||
-- tessellate(body: Body, opts) -> { positions = { vector | {x, y, z} }, normals = { ... }?, indices = { 1-based
|
||||
-- triples }, faceOf = { [triangle] = Face (face.color?) }? }
|
||||
-- or brep-luau's modules as they are: { Step = , StepBrep = , StepWrite = , Tess = } (adapted here: read =
|
||||
-- StepBrep.read(Step.parse(text)), write = StepWrite.write with options.colors from body.color, tessellate = Tess.body).
|
||||
-- cad/step/Brep: the injected B-rep plug-in (Rosetta.plugins.brep; never required by rosetta), called under pcall.
|
||||
-- Interface (each may throw): read(text, opts) -> { bodies, stats? }; write(bodies, opts) -> text;
|
||||
-- tessellate(body, opts) -> { positions, normals?, indices (1-based triples), faceOf? }; or brep-luau's modules as-is.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
@@ -30,7 +21,7 @@ local function rgbOf(c: any): (number?, number?, number?)
|
||||
if type(c) == "table" then
|
||||
return c.r or c.R or c[1], c.g or c.G or c[2], c.b or c.B or c[3]
|
||||
end
|
||||
return (c :: any).R, (c :: any).G, (c :: any).B -- Color3
|
||||
return (c :: any).R, (c :: any).G, (c :: any).B
|
||||
end)
|
||||
if ok and type(r) == "number" and type(g) == "number" and type(b) == "number" then
|
||||
return r, g, b
|
||||
@@ -38,7 +29,6 @@ local function rgbOf(c: any): (number?, number?, number?)
|
||||
return nil, nil, nil
|
||||
end
|
||||
|
||||
-- plugin(p) -> the normalised interface, or nil + the reason
|
||||
function Brep.plugin(p: any): (Plugin?, string?)
|
||||
if type(p) ~= "table" then
|
||||
return nil, "needs the B-rep plug-in (Rosetta.plugins.brep = { read, write, tessellate }, e.g. brep-luau's { Step, StepBrep, StepWrite, Tess })"
|
||||
@@ -82,7 +72,6 @@ function Brep.plugin(p: any): (Plugin?, string?)
|
||||
return nil, "the B-rep plug-in has neither read() nor brep-luau's Step + StepBrep modules"
|
||||
end
|
||||
|
||||
-- find(opts) -> the plug-in from opts.brep or opts.plugins.brep
|
||||
function Brep.find(opts: any): (Plugin?, Err.Err?)
|
||||
local p = nil
|
||||
if type(opts) == "table" then
|
||||
@@ -98,7 +87,6 @@ function Brep.find(opts: any): (Plugin?, Err.Err?)
|
||||
return plug, nil
|
||||
end
|
||||
|
||||
-- read(plug, text, opts) -> bodies, result | nil, Err
|
||||
function Brep.read(plug: Plugin, text: string, opts: any): (({ any })?, any, Err.Err?)
|
||||
local ok, res = pcall(plug.read, text, opts)
|
||||
if not ok then
|
||||
@@ -110,7 +98,6 @@ function Brep.read(plug: Plugin, text: string, opts: any): (({ any })?, any, Err
|
||||
return res.bodies, res, nil
|
||||
end
|
||||
|
||||
-- write(plug, bodies, opts) -> text | nil, Err
|
||||
function Brep.write(plug: Plugin, bodies: { any }, opts: any): (string?, Err.Err?)
|
||||
local w = plug.write
|
||||
if not w then
|
||||
@@ -130,7 +117,7 @@ local function xyz(p: any): (number, number, number)
|
||||
if type(p) == "table" then
|
||||
return tonumber(p.x or p.X or p[1]) or 0, tonumber(p.y or p.Y or p[2]) or 0, tonumber(p.z or p.Z or p[3]) or 0
|
||||
end
|
||||
return (p :: any).X, (p :: any).Y, (p :: any).Z -- vector / Vector3
|
||||
return (p :: any).X, (p :: any).Y, (p :: any).Z
|
||||
end
|
||||
|
||||
local function linear(c: number): number
|
||||
@@ -138,8 +125,7 @@ local function linear(c: number): number
|
||||
return if c <= 0.04045 then c / 12.92 else ((c + 0.055) / 1.055) ^ 2.4
|
||||
end
|
||||
|
||||
-- scene(plug, bodies, opts) -> Scene: one node + mesh per body, one primitive per face colour (materials shared by
|
||||
-- colour; baseColor = the STEP colour converted from sRGB to linear, extras.color = the colour as written)
|
||||
-- baseColor = the STEP colour converted from sRGB to linear; extras.color = the colour as written
|
||||
function Brep.scene(plug: Plugin, bodies: { any }, opts: any): (Types.Scene?, Err.Err?)
|
||||
local tess = plug.tessellate
|
||||
if not tess then
|
||||
@@ -196,7 +182,6 @@ function Brep.scene(plug: Plugin, bodies: { any }, opts: any): (Types.Scene?, Er
|
||||
end
|
||||
end
|
||||
local br, bg, bb = rgbOf(type(body) == "table" and body.color or nil)
|
||||
-- triangles grouped by colour, in first-seen order
|
||||
local groups: { { mat: number, tris: { number } } } = {}
|
||||
local byMat: { [number]: { mat: number, tris: { number } } } = {}
|
||||
local faceOf = if type(mesh.faceOf) == "table" then mesh.faceOf else {}
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- cad/step/Parse: ISO 10303-21 (STEP physical file, editions 1-3) text -> header + entity instances. A single pass
|
||||
-- over the text: comments, the HEADER section, any number of DATA sections (edition 3 `DATA('name', ('schema'));`),
|
||||
-- ANCHOR / REFERENCE / SIGNATURE sections kept as raw text, simple and complex (external mapping) instances, every
|
||||
-- parameter kind (integer, real, string with its \S\ \X\ \X2\ \X4\ escapes decoded to UTF-8, enumeration, binary,
|
||||
-- entity reference, $, *, list, typed parameter). Never throws: failures return nil, message, offset.
|
||||
--
|
||||
-- Values: numbers; strings; { enum = "T" }; { ref = 12 }; { binary = "0FF" }; { typed = "LENGTH_MEASURE", value = v };
|
||||
-- lists as arrays; NULL / DERIVED sentinels (from the caller). A real literal with an integral value (`0.`, `1.E2`)
|
||||
-- is recorded in the metatable of the list (or typed table) holding it (`__reals = { [index | "value"] = true }`), so
|
||||
-- the writer prints it back as a real; integral numbers without that mark are written as integers.
|
||||
-- cad/step/Parse: ISO 10303-21 text -> header + entity instances, one pass; never throws (nil, message, offset).
|
||||
-- An integral real (`0.`, `1.E2`) is marked in its holder's metatable (`__reals = { [index | "value"] = true }`).
|
||||
local Parse = {}
|
||||
|
||||
local sbyte, ssub, sfind, schar, floor = string.byte, string.sub, string.find, string.char, math.floor
|
||||
@@ -32,7 +24,6 @@ local function fail(ctx: Ctx, msg: string, at: number): nil
|
||||
return nil
|
||||
end
|
||||
|
||||
-- the next position that is not white space or inside a /* comment */ (n + 1 at the end; an open comment -> error)
|
||||
local function skip(ctx: Ctx, i: number): number
|
||||
local s, n = ctx.s, ctx.n
|
||||
while i <= n do
|
||||
@@ -60,7 +51,6 @@ local function isWord(c: number): boolean
|
||||
return (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 95 or (c >= 48 and c <= 57)
|
||||
end
|
||||
|
||||
-- keyword at i ([!]A-Z_ A-Z0-9_*) -> word, next; nil when there is none
|
||||
local function keyword(ctx: Ctx, i: number): (string?, number)
|
||||
local s, n = ctx.s, ctx.n
|
||||
local j = i
|
||||
@@ -77,7 +67,6 @@ local function keyword(ctx: Ctx, i: number): (string?, number)
|
||||
return string.upper(ssub(s, i, j - 1)), j
|
||||
end
|
||||
|
||||
-- UTF-8 of a code point
|
||||
local function utf8char(cp: number): string
|
||||
if cp < 0x80 then
|
||||
return schar(cp)
|
||||
@@ -100,9 +89,7 @@ local function hexAt(s: string, i: number, k: number): number?
|
||||
return tonumber(h, 16)
|
||||
end
|
||||
|
||||
-- the text between the quotes of a string literal -> its value ('' -> ', \\ -> \, \S\c, \X\hh, \X2\..\X0\, \X4\..\X0\,
|
||||
-- \P?\ page switches ignored: every page reads as ISO 8859-1). Bytes >= 0x80 written raw (common, though not allowed)
|
||||
-- pass through.
|
||||
-- Part 21 escapes: '' \\ \S\c \X\hh \X2\..\X0\ \X4\..\X0\; \P?\ ignored (ISO 8859-1); raw bytes >= 0x80 pass through
|
||||
function Parse.unescape(raw: string): string
|
||||
if not sfind(raw, "[\\']") then
|
||||
return raw
|
||||
@@ -185,11 +172,9 @@ local function markReal(t: { [any]: any }, key: any)
|
||||
mt.__reals[key] = true
|
||||
end
|
||||
|
||||
-- one parameter at i -> value, next, integralReal (nil value + ctx.err on failure)
|
||||
local value: (ctx: Ctx, i: number, depth: number) -> (any, number, boolean)
|
||||
|
||||
local function list(ctx: Ctx, i: number, depth: number): ({ any }?, number)
|
||||
-- i is just past "("
|
||||
local s = ctx.s
|
||||
local out = {}
|
||||
i = skip(ctx, i)
|
||||
@@ -309,7 +294,6 @@ value = function(ctx: Ctx, i: number, depth: number): (any, number, boolean)
|
||||
end
|
||||
Parse.value = value
|
||||
|
||||
-- the simple record at i: NAME(params) -> type, args, next
|
||||
local function record(ctx: Ctx, i: number): (string?, { any }?, number)
|
||||
local word, j = keyword(ctx, i)
|
||||
if not word then
|
||||
@@ -326,7 +310,6 @@ local function record(ctx: Ctx, i: number): (string?, { any }?, number)
|
||||
return word, args, k
|
||||
end
|
||||
|
||||
-- expect(ctx, i, ch) -> next position after ch (skipping blanks), or nil
|
||||
local function expect(ctx: Ctx, i: number, ch: number, what: string): number?
|
||||
i = skip(ctx, i)
|
||||
if sbyte(ctx.s, i) ~= ch then
|
||||
@@ -353,7 +336,6 @@ export type Result = {
|
||||
terminated: boolean,
|
||||
}
|
||||
|
||||
-- parse(s, opts) -> Result | nil, message, offset (1-based position in s)
|
||||
function Parse.parse(s: string, null: any, derived: any, maxEntities: number): (Result?, string?, number?)
|
||||
local ctx: Ctx = { s = s, n = #s, err = nil, errAt = nil, null = null, derived = derived }
|
||||
local res: Result = {
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- cad/step/Write: header + entity instances -> ISO 10303-21 text. Values as cad/step/Parse produces them; strings
|
||||
-- re-escaped (' -> '', \ -> \\, UTF-8 outside printable ASCII -> \X2\..\X0\ / \X4\..\X0\, control bytes and invalid
|
||||
-- UTF-8 -> \X\hh), reals printed with the fewest digits (15..17) that read back to the same double. Never throws:
|
||||
-- failures return nil, message.
|
||||
-- cad/step/Write: header + entity instances -> ISO 10303-21 text (escaped strings, shortest round-trip reals).
|
||||
local Write = {}
|
||||
|
||||
local sformat, sbyte, ssub, sfind, floor, huge = string.format, string.byte, string.sub, string.find, math.floor, math.huge
|
||||
|
||||
local MAX_DEPTH = 100
|
||||
|
||||
-- the shortest %g form that reads back exactly, in Part 21 syntax (a '.' always, upper-case E)
|
||||
-- Part 21 real: a '.' always, upper-case E
|
||||
function Write.real(v: number): string?
|
||||
if v ~= v or v == huge or v == -huge then
|
||||
return nil
|
||||
@@ -45,7 +42,6 @@ local function hex(v: number, k: number): string
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- a string's Part 21 literal (with the quotes)
|
||||
function Write.str(s: string): string
|
||||
if not sfind(s, "[^ -&(-[%]^-~]") then -- printable ASCII without ' and \
|
||||
return "'" .. s .. "'"
|
||||
@@ -72,7 +68,6 @@ function Write.str(s: string): string
|
||||
end
|
||||
i += 1
|
||||
else
|
||||
-- a UTF-8 sequence -> code point
|
||||
local cp: number? = nil
|
||||
local len = 1
|
||||
if c >= 0xC2 and c < 0xE0 then
|
||||
@@ -235,7 +230,6 @@ value = function(ctx: Ctx, v: any, real: boolean, depth: number): boolean
|
||||
end
|
||||
Write.value = value
|
||||
|
||||
-- record(ctx, type, args) -> appends TYPE(args)
|
||||
function Write.record(ctx: Ctx, t: any, args: any): boolean
|
||||
if not isName(t) then
|
||||
ctx.err = "bad entity type " .. tostring(t)
|
||||
|
||||
@@ -1,24 +1,7 @@
|
||||
--!strict
|
||||
-- cad/step: STEP physical files (ISO 10303-21 editions 1-3, "Part 21": AP203 / AP214 / AP242 and any other schema).
|
||||
-- New code. Reads and writes the entity graph (Types.Cad) fully in-library: the header (FILE_DESCRIPTION / FILE_NAME /
|
||||
-- FILE_SCHEMA + any other header entity), every DATA section (edition 3 named sections: entity.section), simple and
|
||||
-- complex instances (type "COMPLEX", parts = { { type, args } }), every parameter kind: integers and reals (numbers;
|
||||
-- integral reals such as `0.` keep their real-ness through a metatable mark, see Parse), strings (escapes decoded to
|
||||
-- UTF-8: \S\ \X\ \X2\ \X4\, '' and \\), { enum = "T" } (.T. / .F. booleans too), { ref = id }, { binary = "0FF" },
|
||||
-- { typed = "LENGTH_MEASURE", value = v }, lists, Step.NULL ($) and Step.DERIVED (*), comments; ANCHOR / REFERENCE /
|
||||
-- SIGNATURE sections kept as raw text (meta.source.extra) and written back. parse -> write -> parse gives the same
|
||||
-- entities. Writes one instance per line in id order, strings escaped as Part 21 requires (\X2\ for non-ASCII).
|
||||
--
|
||||
-- B-rep bodies and tessellation come from an injected kernel, never required by rosetta:
|
||||
-- F.plugins.brep = { read = fn(text, opts) -> { bodies, stats? }, write = fn(bodies, opts) -> text,
|
||||
-- tessellate = fn(body, opts) -> { positions, normals?, indices (1-based), faceOf? } }
|
||||
-- or brep-luau's modules as they are: F.plugins.brep = { Step = , StepBrep = , StepWrite = , Tess = }
|
||||
-- (the full contract is in cad/step/Brep). Step.toBodies(cad, opts) / Step.fromBodies(bodies, opts) /
|
||||
-- Step.tessellate(cad | bodies, opts) take it from opts.brep or opts.plugins.brep; F.cad.toBodies / fromBodies /
|
||||
-- tessellate pass F.plugins. Tessellated scenes: one node + mesh per body, one primitive per face colour, z up,
|
||||
-- millimetres (meta.unit = 0.001).
|
||||
-- Not supported: STEP-XML (ISO 10303-28), compressed .stpZ, edition 3 value instances (#NAME, @constants: CORRUPT),
|
||||
-- code pages other than ISO 8859-1 in \S\ escapes (\P?\ is ignored).
|
||||
-- cad/step: STEP physical files (ISO 10303-21 eds 1-3; AP203 / AP214 / AP242 / any schema). Supports: header, DATA
|
||||
-- sections, simple and complex instances, every parameter kind, ANCHOR / REFERENCE / SIGNATURE kept raw; B-rep via the
|
||||
-- injected plug-in (cad/step/Brep). Not supported: STEP-XML (10303-28), .stpZ, ed. 3 value instances, \P?\ code pages.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -41,19 +24,19 @@ local Step = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse larger files (default 2^30)
|
||||
maxEntities: number?, -- refuse more instances (default 2^24)
|
||||
maxBytes: number?, -- default 2^30
|
||||
maxEntities: number?, -- default 2^24
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
schema: string?, -- "AP203" | "AP214" | "AP242" | a schema name: overrides the header's FILE_SCHEMA
|
||||
name: string?, -- FILE_NAME name (default: the header's, else "model")
|
||||
timestamp: string?, -- FILE_NAME time_stamp when the header has none (default "1970-01-01T00:00:00")
|
||||
crlf: boolean?, -- CRLF line ends (default LF)
|
||||
schema: string?, -- overrides the header's FILE_SCHEMA
|
||||
name: string?, -- default: the header's, else "model"
|
||||
timestamp: string?, -- default "1970-01-01T00:00:00"
|
||||
crlf: boolean?,
|
||||
}
|
||||
export type BrepOptions = {
|
||||
brep: any?, -- the plug-in (else opts.plugins.brep)
|
||||
brep: any?,
|
||||
plugins: { [string]: any }?,
|
||||
chord: number?, -- tessellation sag in millimetres (default 0.05); opts.tess = the kernel's own options instead
|
||||
chord: number?, -- millimetres (default 0.05)
|
||||
tess: { [string]: any }?,
|
||||
}
|
||||
|
||||
@@ -68,7 +51,6 @@ local SCHEMAS = {
|
||||
}
|
||||
Step.SCHEMAS = SCHEMAS
|
||||
|
||||
-- a FILE_SCHEMA name -> "AP203" | "AP214" | "AP242" | the name itself
|
||||
local function schemaOf(name: string): string
|
||||
local n = string.upper(string.match(name, "^%s*([^%s{]+)") or name)
|
||||
if n == "CONFIG_CONTROL_DESIGN" or string.find(n, "^AP203") then
|
||||
@@ -203,7 +185,6 @@ function Step.encode(cad: Types.Cad, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- entities by section, in id order
|
||||
local ids = {}
|
||||
for id, e in cad.entities do
|
||||
if type(id) ~= "number" or id ~= math.floor(id) or id < 0 or type(e) ~= "table" then
|
||||
@@ -257,7 +238,6 @@ function Step.encode(cad: Types.Cad, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- get(cad, id | { ref = id }) -> the entity
|
||||
function Step.get(cad: Types.Cad, id: any): Types.StepEntity?
|
||||
if type(id) == "table" then
|
||||
id = id.ref
|
||||
@@ -265,7 +245,6 @@ function Step.get(cad: Types.Cad, id: any): Types.StepEntity?
|
||||
return cad.entities[id]
|
||||
end
|
||||
|
||||
-- ids(cad) -> the instance ids in ascending order
|
||||
function Step.ids(cad: Types.Cad): { number }
|
||||
local out = {}
|
||||
for id in cad.entities do
|
||||
@@ -275,7 +254,6 @@ function Step.ids(cad: Types.Cad): { number }
|
||||
return out
|
||||
end
|
||||
|
||||
-- byType(cad, "ADVANCED_FACE") -> the ids of the instances of that type (complex ones with such a part too), ascending
|
||||
function Step.byType(cad: Types.Cad, name: string): { number }
|
||||
local n = string.upper(name)
|
||||
local out = {}
|
||||
@@ -295,7 +273,6 @@ function Step.byType(cad: Types.Cad, name: string): { number }
|
||||
return out
|
||||
end
|
||||
|
||||
-- part(entity, "TYPE") -> the args of that type (the entity's own, or one part of a complex instance)
|
||||
function Step.part(e: Types.StepEntity?, name: string): { any }?
|
||||
if not e then
|
||||
return nil
|
||||
@@ -313,7 +290,6 @@ function Step.part(e: Types.StepEntity?, name: string): { any }?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- add(cad, type, args) -> id: a new instance after the highest id
|
||||
function Step.add(cad: Types.Cad, type: string, args: { any }): number
|
||||
local top = 0
|
||||
for id in cad.entities do
|
||||
@@ -327,7 +303,6 @@ function Step.add(cad: Types.Cad, type: string, args: { any }): number
|
||||
return id
|
||||
end
|
||||
|
||||
-- real(list, index) -> list: marks list[index] as a real (an integral value is then written `3.`, not `3`)
|
||||
function Step.real(t: { [any]: any }, key: any): { [any]: any }
|
||||
local mt: any = getmetatable(t)
|
||||
if not mt then
|
||||
@@ -338,12 +313,10 @@ function Step.real(t: { [any]: any }, key: any): { [any]: any }
|
||||
return t
|
||||
end
|
||||
|
||||
-- new(schema?) -> an empty Cad ("AP214" by default)
|
||||
function Step.new(schema: string?): Types.Cad
|
||||
return { kind = "cad", schema = schema or "AP214", header = {}, entities = {}, meta = { count = 0 } }
|
||||
end
|
||||
|
||||
-- plugin(p) -> the normalised B-rep plug-in interface (see cad/step/Brep) | nil, reason
|
||||
Step.plugin = Brep.plugin
|
||||
|
||||
local function textOf(cad: Types.Cad): (string?, Err.Err?)
|
||||
@@ -354,7 +327,6 @@ local function textOf(cad: Types.Cad): (string?, Err.Err?)
|
||||
return buffer.tostring(buf), nil
|
||||
end
|
||||
|
||||
-- toBodies(cad, opts) -> bodies, result (the plug-in's read result: stats, warnings, ...) | nil, Err
|
||||
function Step.toBodies(cad: Types.Cad, opts: BrepOptions?): (({ any })?, any)
|
||||
local plug, perr = Brep.find(opts)
|
||||
if not plug then
|
||||
@@ -371,7 +343,6 @@ function Step.toBodies(cad: Types.Cad, opts: BrepOptions?): (({ any })?, any)
|
||||
return bodies, res
|
||||
end
|
||||
|
||||
-- fromBodies(bodies, opts) -> Cad (the plug-in writes STEP text, parsed here) | nil, Err
|
||||
function Step.fromBodies(bodies: { any }, opts: BrepOptions?): (Types.Cad?, Err.Err?)
|
||||
if type(bodies) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "needs a list of bodies")
|
||||
@@ -387,7 +358,6 @@ function Step.fromBodies(bodies: { any }, opts: BrepOptions?): (Types.Cad?, Err.
|
||||
return Step.decode(Bytes.reader(buffer.fromstring(text)))
|
||||
end
|
||||
|
||||
-- tessellate(cad | bodies, opts) -> Scene (one node + mesh per body, z up, millimetres) | nil, Err
|
||||
function Step.tessellate(value: any, opts: BrepOptions?): (Types.Scene?, Err.Err?)
|
||||
local plug, perr = Brep.find(opts)
|
||||
if not plug then
|
||||
|
||||
Reference in new issue
Block a user