202 lines
7.3 KiB
Luau
202 lines
7.3 KiB
Luau
--!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).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Crc16 = require("@formats/core/Crc16")
|
|
local Archive = require("@formats/containers/Archive")
|
|
local MacFile = require("@formats/containers/MacFile")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local MacBinary = {
|
|
name = "macbinary",
|
|
category = "archive",
|
|
extensions = { "bin", "macbin" },
|
|
mime = { "application/x-macbinary" },
|
|
deps = { "core/Err", "core/Bytes", "core/Crc16", "containers/Archive", "containers/MacFile" },
|
|
caps = { decode = true, encode = true, forks = true, variants = { "I", "II", "III" } },
|
|
}
|
|
|
|
export type DecodeOptions = { maxBytes: number? } -- default 2^30
|
|
export type EncodeOptions = { name: string? } -- the name of a buffer / string value (default "data")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 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
|
|
end
|
|
local u8 = function(i: number): number
|
|
return buffer.readu8(b, o + i)
|
|
end
|
|
if u8(0) ~= 0 or u8(74) ~= 0 then
|
|
return nil
|
|
end
|
|
local nameLen = u8(1)
|
|
if nameLen < 1 or nameLen > 63 then
|
|
return nil
|
|
end
|
|
local function be32(i: number): number
|
|
return u8(i) * 16777216 + u8(i + 1) * 65536 + u8(i + 2) * 256 + u8(i + 3)
|
|
end
|
|
local dlen, rlen = be32(83), be32(87)
|
|
if dlen > 0x7FFFFFFF or rlen > 0x7FFFFFFF then
|
|
return nil
|
|
end
|
|
local crc = u8(124) * 256 + u8(125)
|
|
if crc == Crc16.ccitt(b, o, 124) and (crc ~= 0 or u8(122) >= 129) then
|
|
return if buffer.readstring(b, o + 102, 4) == "mBIN" then "III" else "II"
|
|
end
|
|
if buffer.readstring(b, o + 102, 4) == "mBIN" then
|
|
return nil -- III with a bad CRC
|
|
end
|
|
-- MacBinary I: byte 82 zero, bytes 99..125 zero, the forks within the file
|
|
if u8(82) ~= 0 then
|
|
return nil
|
|
end
|
|
for i = 99, 125 do
|
|
if u8(i) ~= 0 then
|
|
return nil
|
|
end
|
|
end
|
|
local need = 128 + math.ceil(dlen / 128) * 128 + rlen
|
|
return if need <= len + 127 then "I" else nil
|
|
end
|
|
|
|
function MacBinary.sniff(r: Bytes.Reader): number
|
|
local n = math.min(r:remaining(), 128)
|
|
if n < 128 then
|
|
return 0
|
|
end
|
|
local v = version(r.buf, r.base + r.pos, r.limit - r.base - r.pos)
|
|
return if v == "III" then 0.95 elseif v == "II" then 0.8 elseif v == "I" then 0.3 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
local function encoder(a: any, o: any?): (buffer?, Err.Err?)
|
|
return MacBinary.encode(a, o)
|
|
end
|
|
|
|
local function pad(n: number): number
|
|
return math.ceil(n / 128) * 128
|
|
end
|
|
|
|
function MacBinary.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local o: DecodeOptions = opts or {}
|
|
local len = r.limit - r.base
|
|
local v = version(r.buf, r.base, len)
|
|
if not v then
|
|
if len >= 128 and buffer.readstring(r.buf, r.base + 102, 4) == "mBIN" then
|
|
return nil, Err.new(Err.CHECKSUM, "header CRC mismatch")
|
|
end
|
|
return nil, Err.new(if len < 128 then Err.TRUNCATED else Err.CORRUPT, "not a MacBinary header")
|
|
end
|
|
local h = r:sub(0, 128)
|
|
h:seek(1)
|
|
local name = h:string(h:u8())
|
|
h:seek(65)
|
|
local ftype, creator = h:string(4), h:string(4)
|
|
local flagsHi = h:u8()
|
|
h:seek(83)
|
|
local dlen, rlen = h:u32be(), h:u32be()
|
|
local created, modified = h:u32be(), h:u32be()
|
|
local commentLen = h:u16be()
|
|
local flagsLo = h:u8()
|
|
h:seek(120)
|
|
local secondary = h:u16be()
|
|
if v == "I" then
|
|
commentLen, flagsLo, secondary = 0, 0, 0
|
|
end
|
|
if dlen + rlen > (o.maxBytes or 2 ^ 30) then
|
|
return nil, Err.new(Err.LIMIT, "forks larger than maxBytes")
|
|
end
|
|
r:seek(128 + pad(secondary))
|
|
local data = r:bytes(dlen)
|
|
r:seek(128 + pad(secondary) + pad(dlen))
|
|
local rsrc = r:bytes(rlen)
|
|
local comment: string? = nil
|
|
if commentLen > 0 and r:seek(128 + pad(secondary) + pad(dlen) + pad(rlen)) then
|
|
comment = MacFile.name(r:string(math.min(commentLen, r:remaining())))
|
|
end
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
local a = Archive.new("macbinary", encoder)
|
|
MacFile.put(a, {
|
|
name = MacFile.name(name),
|
|
data = data,
|
|
rsrc = rsrc,
|
|
type = ftype,
|
|
creator = creator,
|
|
flags = flagsHi * 256 + flagsLo,
|
|
created = MacFile.fromMac(created),
|
|
modified = MacFile.fromMac(modified),
|
|
comment = comment,
|
|
})
|
|
a.meta.stream = rlen == 0
|
|
a.meta.source = { format = "macbinary", version = v }
|
|
return a, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function MacBinary.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local o: EncodeOptions = opts or {}
|
|
local files, err = MacFile.collect(value, o.name)
|
|
if not files then
|
|
return nil, err
|
|
end
|
|
local f = files[1]
|
|
if not f then
|
|
return nil, Err.new(Err.ARG, "nothing to encode")
|
|
end
|
|
local name = MacFile.roman(f.name)
|
|
if #name < 1 or #name > 63 then
|
|
return nil, Err.new(Err.ARG, "the name must be 1..63 bytes")
|
|
end
|
|
local dlen, rlen = buffer.len(f.data), buffer.len(f.rsrc)
|
|
local comment = if f.comment then MacFile.roman(f.comment) else ""
|
|
local out = buffer.create(128 + pad(dlen) + pad(rlen) + #comment)
|
|
buffer.writeu8(out, 1, #name)
|
|
buffer.writestring(out, 2, name)
|
|
buffer.writestring(out, 65, f.type)
|
|
buffer.writestring(out, 69, f.creator)
|
|
buffer.writeu8(out, 73, bit32.rshift(f.flags, 8))
|
|
local function be32(at: number, v: number)
|
|
buffer.writeu32(out, at, bit32.byteswap(v))
|
|
end
|
|
be32(83, dlen)
|
|
be32(87, rlen)
|
|
be32(91, MacFile.toMac(f.created or f.modified))
|
|
be32(95, MacFile.toMac(f.modified))
|
|
buffer.writeu8(out, 99, bit32.rshift(#comment, 8))
|
|
buffer.writeu8(out, 100, bit32.band(#comment, 255))
|
|
buffer.writeu8(out, 101, bit32.band(f.flags, 255))
|
|
buffer.writestring(out, 102, "mBIN")
|
|
buffer.writeu8(out, 122, 130)
|
|
buffer.writeu8(out, 123, 129)
|
|
local crc = Crc16.ccitt(out, 0, 124)
|
|
buffer.writeu8(out, 124, bit32.rshift(crc, 8))
|
|
buffer.writeu8(out, 125, bit32.band(crc, 255))
|
|
buffer.copy(out, 128, f.data)
|
|
buffer.copy(out, 128 + pad(dlen), f.rsrc)
|
|
buffer.writestring(out, 128 + pad(dlen) + pad(rlen), comment)
|
|
return out, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ extras
|
|
-- new() -> an empty MacBinary Archive: a:add(name, data); add "name.rsrc" for a resource fork
|
|
function MacBinary.new(): any
|
|
local a = Archive.new("macbinary", encoder)
|
|
a.meta.source = { format = "macbinary" }
|
|
return a
|
|
end
|
|
|
|
return MacBinary
|