rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,158 @@
|
||||
--!strict
|
||||
-- containers/MacFile: classic Mac OS files (a data fork, a resource fork and Finder info) as Archive entries, shared by
|
||||
-- the Mac encodings (archive/binhex, archive/macbinary, archive/applesingle). The convention (unar's "visible forks"):
|
||||
-- the data fork is the entry `name`, a non-empty resource fork is a second entry `name .. ".rsrc"` with
|
||||
-- entry.fork = "resource"; both carry macType / macCreator (4-character strings), finderFlags, created / mtime (unix
|
||||
-- seconds) and comment. Writers pair them back with collect().
|
||||
--
|
||||
-- MacFile.put(archive, { name = "Read Me", data = buf, rsrc = buf2, type = "TEXT", creator = "ttxt" })
|
||||
-- local files, err = MacFile.collect(value, defaultName) -- { MacFile.Info } from any Archive / buffer / string
|
||||
-- MacFile.name(macRomanBytes) -> UTF-8 MacFile.roman(utf8) -> Mac OS Roman bytes
|
||||
-- MacFile.fromMac(secondsSince1904) -> unix seconds MacFile.toMac(unix) -> seconds since 1904 (u32, clamped)
|
||||
-- New code. Never throws.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Archive = require("@rosetta/containers/Archive")
|
||||
|
||||
local MacFile = {}
|
||||
|
||||
export type Info = {
|
||||
name: string,
|
||||
data: buffer,
|
||||
rsrc: buffer,
|
||||
type: string, -- 4 bytes ("\0\0\0\0" when unknown)
|
||||
creator: string,
|
||||
flags: number, -- Finder flags (16 bits)
|
||||
created: number?, -- unix seconds
|
||||
modified: number?,
|
||||
comment: string?,
|
||||
}
|
||||
|
||||
local MAC_EPOCH = 2082844800 -- seconds from 1904-01-01 to 1970-01-01
|
||||
|
||||
function MacFile.fromMac(s: number): number?
|
||||
return if s == 0 then nil else s - MAC_EPOCH
|
||||
end
|
||||
|
||||
function MacFile.toMac(unix: number?): number
|
||||
if type(unix) ~= "number" or unix ~= unix then
|
||||
return 0
|
||||
end
|
||||
return math.clamp(math.floor(unix + MAC_EPOCH), 0, 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
-- Mac OS Roman bytes 0x80..0xFF
|
||||
local ROMAN = {
|
||||
0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
|
||||
0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
|
||||
0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
|
||||
0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8,
|
||||
0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
|
||||
0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
|
||||
0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
|
||||
0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
|
||||
}
|
||||
local FROM_UNICODE: { [number]: number } = {}
|
||||
for i, u in ROMAN do
|
||||
FROM_UNICODE[u] = 127 + i
|
||||
end
|
||||
|
||||
-- name(bytes) -> UTF-8 of a Mac OS Roman string (ASCII passes through)
|
||||
function MacFile.name(s: string): string
|
||||
if not string.find(s, "[\128-\255]") then
|
||||
return s
|
||||
end
|
||||
local out = table.create(#s)
|
||||
for i = 1, #s do
|
||||
local c = string.byte(s, i)
|
||||
out[i] = if c < 128 then string.char(c) else utf8.char(ROMAN[c - 127])
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- roman(utf8) -> Mac OS Roman bytes ("?" for characters it lacks; invalid UTF-8 passes through as bytes)
|
||||
function MacFile.roman(s: string): string
|
||||
if not string.find(s, "[\128-\255]") or not utf8.len(s) then
|
||||
return s
|
||||
end
|
||||
local out = {}
|
||||
for _, c in utf8.codes(s) do
|
||||
table.insert(out, if c < 128 then string.char(c) else string.char(FROM_UNICODE[c] or 63))
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
local function four(s: any): string
|
||||
if type(s) ~= "string" then
|
||||
return "\0\0\0\0"
|
||||
end
|
||||
return string.sub(s .. "\0\0\0\0", 1, 4)
|
||||
end
|
||||
|
||||
-- put(archive, info, noData?): the data fork entry (unless noData: AppleDouble files carry none; or it is empty and
|
||||
-- there is a resource fork) and, when non-empty, the resource fork entry
|
||||
function MacFile.put(a: any, f: Info, noData: boolean?)
|
||||
local common = {
|
||||
macType = f.type,
|
||||
macCreator = f.creator,
|
||||
finderFlags = f.flags,
|
||||
created = f.created,
|
||||
mtime = f.modified,
|
||||
comment = f.comment,
|
||||
dir = false,
|
||||
}
|
||||
local e: any = table.clone(common)
|
||||
e.name, e.size, e.data, e.fork = f.name, buffer.len(f.data), f.data, "data"
|
||||
if not noData and (buffer.len(f.data) > 0 or buffer.len(f.rsrc) == 0) then
|
||||
Archive.put(a, e)
|
||||
end
|
||||
if buffer.len(f.rsrc) > 0 then
|
||||
local r: any = table.clone(common)
|
||||
r.name, r.size, r.data, r.fork = f.name .. ".rsrc", buffer.len(f.rsrc), f.rsrc, "resource"
|
||||
Archive.put(a, r)
|
||||
end
|
||||
end
|
||||
|
||||
-- collect(value, name?) -> the Mac files of any Archive (resource fork entries joined to their data fork entry), a
|
||||
-- buffer or a string (one data fork named name, default "data")
|
||||
function MacFile.collect(value: any, name: string?): ({ Info }?, Err.Err?)
|
||||
local files, err = Archive.files(value, name)
|
||||
if not files then
|
||||
return nil, err
|
||||
end
|
||||
local out: { Info } = {}
|
||||
local byName: { [string]: Info } = {}
|
||||
local function get(n: string, e: any): Info
|
||||
local f = byName[n]
|
||||
if not f then
|
||||
f = {
|
||||
name = n,
|
||||
data = buffer.create(0),
|
||||
rsrc = buffer.create(0),
|
||||
type = four(e.macType),
|
||||
creator = four(e.macCreator),
|
||||
flags = if type(e.finderFlags) == "number" then bit32.band(e.finderFlags, 0xFFFF) else 0,
|
||||
created = if type(e.created) == "number" then e.created else nil,
|
||||
modified = if type(e.mtime) == "number" then e.mtime else nil,
|
||||
comment = if type(e.comment) == "string" then e.comment else nil,
|
||||
}
|
||||
byName[n] = f
|
||||
table.insert(out, f)
|
||||
end
|
||||
return f :: Info
|
||||
end
|
||||
-- data forks first, so a resource fork finds its file whatever the order
|
||||
for _, it in files do
|
||||
if it.entry.fork ~= "resource" then
|
||||
get(it.name, it.entry).data = it.data
|
||||
end
|
||||
end
|
||||
for _, it in files do
|
||||
if it.entry.fork == "resource" then
|
||||
local base = string.match(it.name, "^(.*)%.rsrc$") or it.name
|
||||
get(base, it.entry).rsrc = it.data
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return MacFile
|
||||
Reference in new issue
Block a user