504 lines
16 KiB
Luau
504 lines
16 KiB
Luau
--!strict
|
|
-- containers/Iso9660: ISO 9660 (ECMA-119) images, read; from ECMA-119, SUSP / RRIP 1.12, Joliet and El Torito 1.0, no
|
|
-- code ported. Name sets: Rock Ridge, else Joliet (%/@, %/C, %/E), else plain (";1" and a trailing "." dropped);
|
|
-- multi-extent files joined, CL / RE / PL relocations resolved, El Torito catalog in info.boot. Not supported: zisofs
|
|
-- (read returns the stored bytes), interleaved files (Err.UNSUPPORTED), UDF, writing.
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Text = require("@rosetta/codecs/text/Text")
|
|
|
|
local Iso9660 = {}
|
|
|
|
export type Extent = { at: number, size: number } -- byte offset (from the image start) and length
|
|
export type Entry = {
|
|
name: string, -- the path, "/" separated, no leading "/"
|
|
dir: boolean,
|
|
size: number,
|
|
extents: { Extent },
|
|
mtime: number?, -- unix seconds
|
|
type: string, -- tar letters: "0" file, "5" directory, "2" symlink, "3" / "4" devices, "6" fifo, "s" socket
|
|
mode: number?, -- Rock Ridge: the permission bits
|
|
uid: number?,
|
|
gid: number?,
|
|
nlink: number?,
|
|
link: string?, -- Rock Ridge symlink target
|
|
rdev: number?, -- Rock Ridge PN: high * 2^32 + low
|
|
hidden: boolean?,
|
|
interleaved: boolean?,
|
|
zisofs: boolean?,
|
|
}
|
|
export type ReadOptions = {
|
|
prefer: ("rockridge" | "joliet" | "iso9660")?, -- the name set (default: Rock Ridge, else Joliet, else plain)
|
|
maxEntries: number?, -- default 1e6
|
|
}
|
|
|
|
local SECTOR = 2048
|
|
local S_TYPES: { [number]: string } = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2", [0x2000] = "3", [0x6000] = "4", [0x1000] = "6", [0xC000] = "s" }
|
|
|
|
local function u16(b: buffer, o: number): number
|
|
return buffer.readu16(b, o)
|
|
end
|
|
local function u32(b: buffer, o: number): number
|
|
return buffer.readu32(b, o)
|
|
end
|
|
local function str(b: buffer, o: number, n: number): string
|
|
return (string.gsub(buffer.readstring(b, o, n), "[ %z]+$", ""))
|
|
end
|
|
|
|
-- a 7-byte directory record date -> unix seconds
|
|
local function recDate(b: buffer, o: number): number?
|
|
local y = buffer.readu8(b, o)
|
|
local m, d = buffer.readu8(b, o + 1), buffer.readu8(b, o + 2)
|
|
if m < 1 or m > 12 or d < 1 or d > 31 then
|
|
return nil
|
|
end
|
|
local off = buffer.readi8(b, o + 6)
|
|
return Types.Date.toUnix({ kind = "datetime", year = 1900 + y, month = m, day = d, hour = buffer.readu8(b, o + 3), minute = buffer.readu8(b, o + 4), second = buffer.readu8(b, o + 5), offset = off * 15 })
|
|
end
|
|
|
|
-- a 17-byte descriptor date ("YYYYMMDDHHMMSScc" + offset) -> unix seconds
|
|
local function longDate(b: buffer, o: number): number?
|
|
local s = buffer.readstring(b, o, 16)
|
|
local y, mo, d, h, mi, se = string.match(s, "^(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d)%d%d$")
|
|
if not y or tonumber(y) == 0 then
|
|
return nil
|
|
end
|
|
local off = buffer.readi8(b, o + 16)
|
|
return Types.Date.toUnix({ kind = "datetime", year = tonumber(y), month = tonumber(mo), day = tonumber(d), hour = tonumber(h), minute = tonumber(mi), second = tonumber(se), offset = off * 15 })
|
|
end
|
|
|
|
type Susp = { [string]: any }
|
|
|
|
local function suspEntries(b: buffer, base: number, limit: number, o: number, e: number, bs: number, budget: { number }): { { any } }
|
|
local out = {}
|
|
while o + 4 <= e do
|
|
local sig = buffer.readstring(b, o, 2)
|
|
local len = buffer.readu8(b, o + 2)
|
|
if len < 4 or o + len > e then
|
|
break
|
|
end
|
|
if sig == "ST" then
|
|
break
|
|
elseif sig == "CE" and len >= 28 then
|
|
local blk, off, n = u32(b, o + 4), u32(b, o + 12), u32(b, o + 20)
|
|
local at = base + blk * bs + off
|
|
budget[1] -= 1
|
|
if budget[1] < 0 or at < base or at + n > limit or n > 65536 then
|
|
break
|
|
end
|
|
-- the rest of this area first, then the continuation (it holds what did not fit)
|
|
local rest = suspEntries(b, base, limit, o + len, e, bs, budget)
|
|
for _, x in rest do
|
|
table.insert(out, x)
|
|
end
|
|
local cont = suspEntries(b, base, limit, at, at + n, bs, budget)
|
|
for _, x in cont do
|
|
table.insert(out, x)
|
|
end
|
|
return out
|
|
end
|
|
table.insert(out, { sig, o, len })
|
|
o += len
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function rockRidge(b: buffer, base: number, limit: number, o: number, e: number, bs: number): Susp
|
|
local rr: Susp = {}
|
|
local name: { string }? = nil
|
|
local slParts: { string }? = nil
|
|
local slRoot = false
|
|
local slCont = false
|
|
for _, x in suspEntries(b, base, limit, o, e, bs, { 32 }) do
|
|
local sig, at, len = x[1], x[2], x[3]
|
|
if sig == "NM" and len >= 5 then
|
|
local flags = buffer.readu8(b, at + 4)
|
|
if bit32.band(flags, 6) == 0 then
|
|
name = name or {}
|
|
table.insert(name :: { string }, buffer.readstring(b, at + 5, len - 5))
|
|
end
|
|
elseif sig == "PX" and len >= 36 then
|
|
rr.mode, rr.nlink, rr.uid, rr.gid = u32(b, at + 4), u32(b, at + 12), u32(b, at + 20), u32(b, at + 28)
|
|
elseif sig == "PN" and len >= 20 then
|
|
rr.rdev = u32(b, at + 4) * 4294967296 + u32(b, at + 12)
|
|
elseif sig == "TF" and len >= 5 then
|
|
local flags = buffer.readu8(b, at + 4)
|
|
local w = if bit32.band(flags, 0x80) ~= 0 then 17 else 7
|
|
local q = at + 5
|
|
for bit = 0, 6 do
|
|
if bit32.band(flags, bit32.lshift(1, bit)) ~= 0 then
|
|
if q + w > at + len then
|
|
break
|
|
end
|
|
if bit == 1 then
|
|
rr.mtime = if w == 17 then longDate(b, q) else recDate(b, q)
|
|
end
|
|
q += w
|
|
end
|
|
end
|
|
elseif sig == "SL" and len >= 5 then
|
|
slParts = slParts or {}
|
|
local parts = slParts :: { string }
|
|
local q = at + 5
|
|
while q + 2 <= at + len do
|
|
local cf, cl = buffer.readu8(b, q), buffer.readu8(b, q + 1)
|
|
if q + 2 + cl > at + len then
|
|
break
|
|
end
|
|
local piece = if bit32.band(cf, 2) ~= 0 then "." elseif bit32.band(cf, 4) ~= 0 then ".." else buffer.readstring(b, q + 2, cl)
|
|
if bit32.band(cf, 8) ~= 0 then
|
|
slRoot = #parts == 0
|
|
piece = ""
|
|
end
|
|
if slCont and #parts > 0 then
|
|
parts[#parts] ..= piece
|
|
elseif bit32.band(cf, 8) == 0 or #parts > 0 then
|
|
table.insert(parts, piece)
|
|
end
|
|
slCont = bit32.band(cf, 1) ~= 0
|
|
q += 2 + cl
|
|
end
|
|
elseif sig == "CL" and len >= 12 then
|
|
rr.child = u32(b, at + 4)
|
|
elseif sig == "RE" then
|
|
rr.relocated = true
|
|
elseif sig == "ZF" then
|
|
rr.zisofs = true
|
|
end
|
|
end
|
|
if name then
|
|
rr.name = table.concat(name)
|
|
end
|
|
if slParts then
|
|
rr.link = (if slRoot then "/" else "") .. table.concat(slParts, "/")
|
|
end
|
|
return rr
|
|
end
|
|
|
|
type Record = {
|
|
name: string,
|
|
dir: boolean,
|
|
lba: number,
|
|
size: number,
|
|
ear: number,
|
|
flags: number,
|
|
interleave: boolean,
|
|
mtime: number?,
|
|
rr: Susp?,
|
|
}
|
|
|
|
local function records(b: buffer, base: number, limit: number, at: number, size: number, bs: number, joliet: boolean, rrSkip: number?): ({ Record }?, Err.Err?)
|
|
if at + size > limit then
|
|
return nil, Err.new(Err.TRUNCATED, "directory past the end", at)
|
|
end
|
|
local out: { Record } = {}
|
|
local p = at
|
|
local e = at + size
|
|
while p < e do
|
|
local len = buffer.readu8(b, p)
|
|
if len == 0 then
|
|
p = at + ((p - at) // SECTOR + 1) * SECTOR -- the rest of the sector is padding
|
|
continue
|
|
end
|
|
if len < 34 or p + len > e then
|
|
return nil, Err.new(Err.CORRUPT, "bad directory record", p)
|
|
end
|
|
local nl = buffer.readu8(b, p + 32)
|
|
if 33 + nl > len then
|
|
return nil, Err.new(Err.CORRUPT, "bad name length", p + 32)
|
|
end
|
|
local raw = buffer.readstring(b, p + 33, nl)
|
|
if raw ~= "\0" and raw ~= "\1" then
|
|
local flags = buffer.readu8(b, p + 25)
|
|
local name
|
|
if joliet then
|
|
name = Text.utf16(b, p + 33, nl, true)
|
|
else
|
|
name = raw
|
|
end
|
|
name = string.gsub(name, ";%d*$", "")
|
|
if not joliet and bit32.band(flags, 2) == 0 then
|
|
name = string.gsub(name, "%.$", "")
|
|
end
|
|
local r: Record = {
|
|
name = name,
|
|
dir = bit32.band(flags, 2) ~= 0,
|
|
lba = u32(b, p + 2),
|
|
size = u32(b, p + 10),
|
|
ear = buffer.readu8(b, p + 1),
|
|
flags = flags,
|
|
interleave = buffer.readu8(b, p + 26) ~= 0 or buffer.readu8(b, p + 27) ~= 0,
|
|
mtime = recDate(b, p + 18),
|
|
rr = nil,
|
|
}
|
|
if rrSkip then
|
|
local su = p + 33 + nl + (if nl % 2 == 0 then 1 else 0) + rrSkip
|
|
r.rr = rockRidge(b, base, limit, su, p + len, bs)
|
|
end
|
|
table.insert(out, r)
|
|
end
|
|
p += len
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
local function root(b: buffer, vd: number): (number, number)
|
|
return u32(b, vd + 156 + 2), u32(b, vd + 156 + 10)
|
|
end
|
|
|
|
-- Rock Ridge in use: the root's "." record starts its system use area with SP (BE EF); its skip length
|
|
local function rockRidgeSkip(b: buffer, base: number, limit: number, lba: number, bs: number): number?
|
|
local p = base + lba * bs
|
|
if p + 34 > limit then
|
|
return nil
|
|
end
|
|
local len = buffer.readu8(b, p)
|
|
local nl = buffer.readu8(b, p + 32)
|
|
local su = p + 33 + nl + (if nl % 2 == 0 then 1 else 0)
|
|
if len < 34 or su + 7 > p + len or p + len > limit then
|
|
return nil
|
|
end
|
|
if buffer.readstring(b, su, 2) == "SP" and buffer.readu8(b, su + 4) == 0xBE and buffer.readu8(b, su + 5) == 0xEF then
|
|
return buffer.readu8(b, su + 6)
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function boot(b: buffer, base: number, limit: number, vd: number, bs: number): { [string]: any }?
|
|
if str(b, vd + 7, 32) ~= "EL TORITO SPECIFICATION" then
|
|
return nil
|
|
end
|
|
local cat = u32(b, vd + 0x47)
|
|
local p = base + cat * bs
|
|
local info: { [string]: any } = { catalog = cat }
|
|
if p + 64 > limit or buffer.readu8(b, p) ~= 1 or buffer.readu8(b, p + 30) ~= 0x55 or buffer.readu8(b, p + 31) ~= 0xAA then
|
|
return info
|
|
end
|
|
local sum = 0
|
|
for i = 0, 30, 2 do
|
|
sum += u16(b, p + i)
|
|
end
|
|
info.valid = sum % 65536 == 0
|
|
info.platform = buffer.readu8(b, p + 1)
|
|
info.id = str(b, p + 4, 24)
|
|
local d = p + 32
|
|
info.bootable = buffer.readu8(b, d) == 0x88
|
|
info.media = buffer.readu8(b, d + 1) -- 0 no emulation, 1..3 floppies, 4 hard disk
|
|
info.loadSegment = u16(b, d + 2)
|
|
info.systemType = buffer.readu8(b, d + 4)
|
|
info.sectors = u16(b, d + 6) -- 512-byte virtual sectors
|
|
info.lba = u32(b, d + 8)
|
|
return info
|
|
end
|
|
|
|
function Iso9660.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, any)
|
|
local base = offset or 0
|
|
local limit = math.min(buffer.len(b), base + (length or (buffer.len(b) - base)))
|
|
local o: ReadOptions = opts or {}
|
|
local maxEntries = o.maxEntries or 1e6
|
|
local pvd: number? = nil
|
|
local svd: number? = nil
|
|
local jolietLevel: number? = nil
|
|
local bootInfo = nil
|
|
local p = base + 16 * SECTOR
|
|
for _ = 1, 64 do
|
|
if p + SECTOR > limit then
|
|
return nil, Err.new(Err.TRUNCATED, "volume descriptors past the end", p)
|
|
end
|
|
if buffer.readstring(b, p + 1, 5) ~= "CD001" then
|
|
return nil, Err.new(Err.CORRUPT, "bad volume descriptor", p)
|
|
end
|
|
local t = buffer.readu8(b, p)
|
|
if t == 255 then
|
|
break
|
|
elseif t == 1 and not pvd then
|
|
pvd = p
|
|
elseif t == 2 then
|
|
local esc = buffer.readstring(b, p + 88, 3)
|
|
if esc == "%/@" or esc == "%/C" or esc == "%/E" then
|
|
svd = p
|
|
jolietLevel = if esc == "%/@" then 1 elseif esc == "%/C" then 2 else 3
|
|
end
|
|
elseif t == 0 then
|
|
bootInfo = bootInfo or { vd = p }
|
|
end
|
|
p += SECTOR
|
|
end
|
|
if not pvd then
|
|
return nil, Err.new(Err.CORRUPT, "no primary volume descriptor", base + 16 * SECTOR)
|
|
end
|
|
local pv = pvd :: number
|
|
local bs = u16(b, pv + 128)
|
|
if bs ~= 512 and bs ~= 1024 and bs ~= 2048 then
|
|
return nil, Err.new(Err.CORRUPT, "bad logical block size " .. bs, pv + 128)
|
|
end
|
|
local info: { [string]: any } = {
|
|
blockSize = bs,
|
|
blocks = u32(b, pv + 80),
|
|
systemId = str(b, pv + 8, 32),
|
|
volumeId = str(b, pv + 40, 32),
|
|
volumeSet = str(b, pv + 190, 128),
|
|
publisher = str(b, pv + 318, 128),
|
|
preparer = str(b, pv + 446, 128),
|
|
application = str(b, pv + 574, 128),
|
|
created = longDate(b, pv + 813),
|
|
modified = longDate(b, pv + 830),
|
|
joliet = jolietLevel,
|
|
}
|
|
if bootInfo then
|
|
info.boot = boot(b, base, limit, bootInfo.vd, bs)
|
|
end
|
|
local rlba, rsize = root(b, pv)
|
|
local rrSkip = rockRidgeSkip(b, base, limit, rlba, bs)
|
|
info.rockRidge = rrSkip ~= nil
|
|
local tree = o.prefer or (if rrSkip then "rockridge" elseif svd then "joliet" else "iso9660")
|
|
if tree == "joliet" and not svd then
|
|
tree = "iso9660"
|
|
elseif tree == "rockridge" and not rrSkip then
|
|
tree = "iso9660"
|
|
end
|
|
info.tree = tree
|
|
local joliet = tree == "joliet"
|
|
local useRR = if tree == "rockridge" then rrSkip else nil
|
|
if joliet then
|
|
rlba, rsize = root(b, svd :: number)
|
|
end
|
|
if tree == "joliet" then
|
|
info.volumeId = (string.gsub(Text.utf16(b, (svd :: number) + 40, 32, true), "[ %z]+$", ""))
|
|
end
|
|
local out: { Entry } = {}
|
|
local visited: { [number]: boolean } = {}
|
|
local stack = { { lba = rlba, size = rsize, path = "" } }
|
|
local relocatedDirs: { [string]: boolean } = {}
|
|
while #stack > 0 do
|
|
local d = table.remove(stack) :: any
|
|
if visited[d.lba] then
|
|
continue
|
|
end
|
|
visited[d.lba] = true
|
|
local list, err = records(b, base, limit, base + d.lba * bs, d.size, bs, joliet, useRR)
|
|
if not list then
|
|
return nil, err
|
|
end
|
|
local i = 1
|
|
local subdirs = {}
|
|
while i <= #list do
|
|
local r = list[i]
|
|
-- multi-extent: this record and the following ones of the same name
|
|
local extents: { Extent } = { { at = base + (r.lba + r.ear) * bs, size = r.size } }
|
|
local total = r.size
|
|
local last = r
|
|
while bit32.band(last.flags, 0x80) ~= 0 and i < #list and list[i + 1].name == r.name do
|
|
i += 1
|
|
last = list[i]
|
|
table.insert(extents, { at = base + (last.lba + last.ear) * bs, size = last.size })
|
|
total += last.size
|
|
end
|
|
i += 1
|
|
if bit32.band(r.flags, 4) ~= 0 then
|
|
continue -- an associated file (resource fork)
|
|
end
|
|
local rr: any = r.rr
|
|
if rr and rr.relocated then
|
|
continue -- shown where its CL placeholder is
|
|
end
|
|
local name = if rr and rr.name then rr.name else r.name
|
|
local path = d.path .. name
|
|
local isDir = r.dir
|
|
local lba: number, size: number = r.lba, r.size
|
|
if rr and rr.child then
|
|
-- a deep directory relocated by Rock Ridge: the placeholder points at it; its size is its "." record's
|
|
isDir = true
|
|
lba = rr.child
|
|
local q = base + lba * bs
|
|
size = if q + 14 <= limit then u32(b, q + 10) else 0
|
|
end
|
|
local typ = if isDir then "5" else "0"
|
|
if rr and rr.mode then
|
|
typ = S_TYPES[bit32.band(rr.mode, 0xF000)] or typ
|
|
end
|
|
local e: Entry = {
|
|
name = path,
|
|
dir = isDir,
|
|
size = if isDir or typ ~= "0" then 0 else total,
|
|
extents = if isDir or typ ~= "0" then {} else extents,
|
|
mtime = if rr and rr.mtime then rr.mtime else r.mtime,
|
|
type = typ,
|
|
mode = if rr and rr.mode then bit32.band(rr.mode, 0xFFF) else nil,
|
|
uid = rr and rr.uid,
|
|
gid = rr and rr.gid,
|
|
nlink = rr and rr.nlink,
|
|
link = if rr and typ == "2" then rr.link else nil,
|
|
rdev = rr and rr.rdev,
|
|
hidden = if bit32.band(r.flags, 1) ~= 0 then true else nil,
|
|
interleaved = if r.interleave then true else nil,
|
|
zisofs = if rr and rr.zisofs then true else nil,
|
|
}
|
|
table.insert(out, e)
|
|
if #out > maxEntries then
|
|
return nil, Err.new(Err.LIMIT, "more than " .. maxEntries .. " entries")
|
|
end
|
|
if isDir then
|
|
table.insert(subdirs, { lba = lba, size = size, path = path .. "/" })
|
|
end
|
|
end
|
|
for k = #subdirs, 1, -1 do
|
|
table.insert(stack, subdirs[k])
|
|
end
|
|
if useRR then
|
|
for _, r in list do
|
|
if r.rr and r.rr.relocated then
|
|
relocatedDirs[d.path] = true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- rr_moved (genisoimage's home of relocated directories): hidden when nothing else is in it
|
|
if useRR then
|
|
for dirPath in relocatedDirs do
|
|
local name = string.sub(dirPath, 1, -2)
|
|
local holds = false
|
|
for _, e in out do
|
|
if string.sub(e.name, 1, #dirPath) == dirPath then
|
|
holds = true
|
|
break
|
|
end
|
|
end
|
|
if not holds then
|
|
for k, e in out do
|
|
if e.name == name then
|
|
table.remove(out, k)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- archive order: depth-first walk order is kept; directories sorted as the image stores them
|
|
return out, info
|
|
end
|
|
|
|
function Iso9660.extract(b: buffer, e: Entry, limit: number?): (buffer?, Err.Err?)
|
|
if e.interleaved then
|
|
return nil, Err.new(Err.UNSUPPORTED, "interleaved file " .. e.name)
|
|
end
|
|
local lim = limit or buffer.len(b)
|
|
local out = buffer.create(e.size)
|
|
local q = 0
|
|
for _, x in e.extents do
|
|
if x.at < 0 or x.at + x.size > lim then
|
|
return nil, Err.new(Err.TRUNCATED, "file " .. e.name .. " data past the end", x.at)
|
|
end
|
|
buffer.copy(out, q, b, x.at, x.size)
|
|
q += x.size
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
function Iso9660.valid(b: buffer, offset: number?): boolean
|
|
local p = (offset or 0) + 16 * SECTOR
|
|
return p + 6 <= buffer.len(b) and buffer.readstring(b, p + 1, 5) == "CD001"
|
|
end
|
|
|
|
return Iso9660
|