ws-arch2: archive/adf (Amiga OFS / FFS, intl, dircache; DD / HD; read)

This commit is contained in:
4DBug committed 2026-10-31 14:20:36 -05:00
1 parent 66d785de11
commit f8a26fc699
5 files changed
+435

No files matched your search

+12
View File
@@ -554,6 +554,18 @@ local list: { Entry } = {
return require("@formats/archive/7z")
end,
},
{
name = "adf",
category = "archive",
path = "archive/adf",
extensions = { "adf" },
mime = { "application/x-amiga-disk-format" },
deps = { "core/Err", "core/Bytes", "containers/Archive" },
caps = { decode = true, encode = false, variants = { "ofs", "ffs" } },
load = function()
return require("@formats/archive/adf")
end,
},
{
name = "applesingle",
category = "archive",
+288
View File
@@ -0,0 +1,288 @@
--!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).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Archive = require("@formats/containers/Archive")
-- ------------------------------------------------------------------------------------------------ contract
local Adf = {
name = "adf",
category = "archive",
extensions = { "adf" },
mime = { "application/x-amiga-disk-format" },
deps = { "core/Err", "core/Bytes", "containers/Archive" },
caps = { decode = true, encode = false, variants = { "ofs", "ffs" } },
}
export type DecodeOptions = { maxEntries: number? } -- default 65536
-- ------------------------------------------------------------------------------------------------ constants
local BS = 512
local HT = 72 -- hash table slots in a 512-byte block
local T_HEADER, T_DATA, T_LIST = 2, 8, 16
local ST_ROOT, ST_USERDIR, ST_SOFTLINK, ST_LINKDIR, ST_FILE, ST_LINKFILE = 1, 2, 3, 4, -3, -4
local EPOCH = 252460800 -- 1978-01-01 in unix seconds
-- ------------------------------------------------------------------------------------------------ sniff
function Adf.sniff(r: Bytes.Reader): number
local n = r:size()
if n < 4 * BS or n % BS ~= 0 then
return 0
end
local h = r:peek(4)
if string.sub(h, 1, 3) ~= "DOS" or string.byte(h, 4) > 5 then
return 0
end
return if n == 901120 or n == 1802240 then 0.95 else 0.6
end
-- ------------------------------------------------------------------------------------------------ decode
local function latin1(s: string): string
if not string.find(s, "[\128-\255]") then
return s
end
local out = table.create(#s)
for i = 1, #s do
out[i] = utf8.char(string.byte(s, i))
end
return table.concat(out)
end
type Disk = { b: buffer, base: number, blocks: number }
local function u32(d: Disk, blk: number, off: number): number
local o = d.base + blk * BS + off
return buffer.readu8(d.b, o) * 16777216 + buffer.readu8(d.b, o + 1) * 65536 + buffer.readu8(d.b, o + 2) * 256 + buffer.readu8(d.b, o + 3)
end
local function i32(d: Disk, blk: number, off: number): number
local v = u32(d, blk, off)
return if v >= 0x80000000 then v - 0x100000000 else v
end
local function bstr(d: Disk, blk: number, off: number, max: number): string
local o = d.base + blk * BS + off
local n = math.min(buffer.readu8(d.b, o), max)
return latin1(buffer.readstring(d.b, o + 1, n))
end
local function checksumOk(d: Disk, blk: number): boolean
local sum = 0
for i = 0, BS - 4, 4 do
sum = (sum + u32(d, blk, i)) % 0x100000000
end
return sum == 0
end
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
local seen: { [number]: boolean } = {}
while blk ~= 0 do
if blk < 2 or blk >= d.blocks or seen[blk] then
return nil, "bad extension block " .. blk
end
seen[blk] = true
if blk ~= hdr and (u32(d, blk, 0) ~= T_LIST or not checksumOk(d, blk)) then
return nil, "bad extension block " .. blk
end
local n = u32(d, blk, 8)
if n > HT then
return nil, "bad block count in " .. blk
end
for k = 0, n - 1 do
local db = u32(d, blk, 24 + (HT - 1 - k) * 4)
if db < 2 or db >= d.blocks then
return nil, "bad data block " .. db
end
table.insert(out, db)
if #out > maxBlocks then
return nil, "more data blocks than the disk has"
end
end
blk = u32(d, blk, BS - 8)
end
return out, nil
end
local function readFile(d: Disk, hdr: number, ofs: boolean): (buffer?, string?)
local size = u32(d, hdr, BS - 188)
if size > d.blocks * BS then
return nil, "file larger than the disk"
end
local list, err = dataBlocks(d, hdr, d.blocks)
if not list then
return nil, err
end
local out = buffer.create(size)
local o = 0
for _, db in list do
if o >= size then
break
end
if ofs then
if u32(d, db, 0) ~= T_DATA then
return nil, "data block " .. db .. " is not an OFS data block"
end
local n = math.min(u32(d, db, 12), 488, size - o)
buffer.copy(out, o, d.b, d.base + db * BS + 24, n)
o += n
else
local n = math.min(BS, size - o)
buffer.copy(out, o, d.b, d.base + db * BS, n)
o += n
end
end
if o < size then
return nil, "file data ends early"
end
return out, nil
end
function Adf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local o: DecodeOptions = opts or {}
local n = r:size()
if n < 4 * BS or n % BS ~= 0 then
return nil, Err.new(Err.CORRUPT, "not a whole number of 512-byte blocks")
end
local h = r:peek(4)
if string.sub(h, 1, 3) ~= "DOS" then
return nil, Err.new(Err.CORRUPT, "no DOS boot block")
end
local flags = string.byte(h, 4)
if flags > 5 then
return nil, Err.new(Err.UNSUPPORTED, "DOS\\" .. flags .. " (long names) file systems")
end
local d: Disk = { b = r.buf, base = r.base, blocks = n // BS }
local ofs = bit32.band(flags, 1) == 0
local root = (d.blocks - 1 + 2) // 2
if u32(d, root, 0) ~= T_HEADER or i32(d, root, BS - 4) ~= ST_ROOT then
return nil, Err.new(Err.CORRUPT, "no root block at " .. root)
end
if not checksumOk(d, root) then
return nil, Err.new(Err.CHECKSUM, "root block checksum")
end
local a = Archive.new("adf")
a.meta.volume = bstr(d, root, BS - 80, 30)
a.meta.fs = if ofs then "ofs" else "ffs"
a.meta.intl = flags >= 2
a.meta.dircache = flags >= 4
a.meta.dosType = flags
a.meta.modified = date(d, root, BS - 92)
a.meta.created = date(d, root, BS - 28)
a.meta.source = { format = "adf", fs = a.meta.fs, blocks = d.blocks }
local maxEntries = o.maxEntries or 65536
local count = 0
local pathOf: { [number]: string } = {}
local links: { { entry: any, target: number } } = {}
local visited: { [number]: boolean } = { [root] = true }
local function walk(dir: number, prefix: string): (boolean, Err.Err?)
for slot = 0, HT - 1 do
local blk = u32(d, dir, 24 + slot * 4)
while blk ~= 0 do
if blk < 2 or blk >= d.blocks or visited[blk] then
return false, Err.new(Err.CORRUPT, "bad or looping hash chain at block " .. blk)
end
visited[blk] = true
count += 1
if count > maxEntries then
return false, Err.new(Err.LIMIT, "more than maxEntries entries")
end
if u32(d, blk, 0) ~= T_HEADER then
return false, Err.new(Err.CORRUPT, "block " .. blk .. " is not a header block")
end
if not checksumOk(d, blk) then
return false, Err.new(Err.CHECKSUM, "header block " .. blk .. " checksum")
end
local name = prefix .. bstr(d, blk, BS - 80, 30)
local st = i32(d, blk, BS - 4)
local e: any = {
name = name,
size = 0,
dir = false,
mtime = date(d, blk, BS - 92),
protect = u32(d, blk, BS - 192),
comment = bstr(d, blk, BS - 184, 79),
method = "store",
}
if e.comment == "" then
e.comment = nil
end
if st == ST_USERDIR then
e.name = name .. "/"
e.dir = true
pathOf[blk] = e.name
Archive.put(a, e)
local ok, err = walk(blk, e.name)
if not ok then
return false, err
end
elseif st == ST_FILE then
e.size = u32(d, blk, BS - 188)
pathOf[blk] = name
local hb = blk
e.load = function(): (buffer?, Err.Err?)
local data, err = readFile(d, hb, ofs)
if not data then
return nil, Err.new(Err.CORRUPT, tostring(err))
end
return data, nil
end
Archive.put(a, e)
elseif st == ST_SOFTLINK then
local lo = d.base + blk * BS + 24
local s = buffer.readstring(d.b, lo, BS - 24 - 200)
e.link = latin1(string.match(s, "^[^%z]*") or "")
e.type = "symlink"
Archive.put(a, e)
elseif st == ST_LINKFILE or st == ST_LINKDIR then
e.type = "hardlink"
e.dir = st == ST_LINKDIR
if e.dir then
e.name = name .. "/"
end
Archive.put(a, e)
table.insert(links, { entry = e, target = u32(d, blk, BS - 44) })
end
blk = u32(d, blk, BS - 16)
end
end
return true, nil
end
local ok, err = walk(root, "")
if not ok then
return nil, err
end
-- hard links: the linked path, and a file link reads its target's data
for _, l in links do
local target = pathOf[l.target]
l.entry.hardlink = target
if target and not l.entry.dir then
local te = a._byName[target]
if te then
l.entry.size = te.size
l.entry.load = te.load
end
end
end
return a, nil
end
return Adf