# Mac encodings fixtures (tools/fixtures.py) for archive/binhex, archive/macbinary and archive/applesingle: files with # data and resource forks, type / creator / Finder flags and dates, imported into an HFS volume by hfsutils (`hcopy -m` # of a MacBinary we build here) and exported by hfsutils as MacBinary II (`hcopy -m`) and BinHex 4.0 (`hcopy -b`); # AppleSingle / AppleDouble v1 and v2 built here per RFC 1740. Every case's reference listing (but AppleSingle v1's, # which unar does not read) comes from The Unarchiver (`lsar -j`, `unar -forks visible`: names, fork sizes, type / creator, data CRCs). Our writers' output # (tools/fixtures/_arch2_ours.luau with a resource fork) is listed and extracted by unar the same way. # tests/gen/mac.luau { cases = { { format, name, data, entries = { { name, size, crc, macType?, macCreator? } } } }, # ours = { { format, variant?, ok, detail } } } import importlib.util, json, os, shutil, struct, zlib _s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py")) A = importlib.util.module_from_spec(_s) _s.loader.exec_module(A) INPUTS = ["tools/fixtures/_arch2.py", "tools/fixtures/_arch2_ours.luau", "src/Formats/archive/binhex", "src/Formats/archive/macbinary", "src/Formats/archive/applesingle", "src/Formats/containers/MacFile.luau"] PKGS = ["hfsutils", "unar"] MAC_EPOCH = 2082844800 def crc16(data): c = 0 for b in data: c ^= b << 8 for _ in range(8): c = ((c << 1) ^ 0x1021) & 0xFFFF if c & 0x8000 else (c << 1) & 0xFFFF return c def macbinary(name, data, rsrc, ftype, creator, flags, mtime): h = bytearray(128) nm = name.encode("mac_roman") h[1] = len(nm) h[2:2 + len(nm)] = nm h[65:73] = ftype + creator h[73] = flags >> 8 struct.pack_into(">IIII", h, 83, len(data), len(rsrc), mtime + MAC_EPOCH, mtime + MAC_EPOCH) h[101] = flags & 255 h[122], h[123] = 129, 129 struct.pack_into(">H", h, 124, crc16(bytes(h[:124]))) pad = lambda b: b + b"\0" * (-len(b) % 128) return bytes(h) + pad(data) + pad(rsrc) def apple(magic, version, entries): filler = b"Macintosh " if version == 1 else b"\0" * 16 hdr = struct.pack(">II16sH", magic, version << 16, filler, len(entries)) off, tab, body = len(hdr) + 12 * len(entries), b"", b"" for i, d in entries: tab += struct.pack(">III", i, off + len(body), len(d)) body += d return hdr + tab + body def listing(path, outdir, known): """unar's view: [{name, size, crc, rsrc, macType, macCreator}]; names unar %-escaped become the known name""" j = json.loads(A.tool(PKGS, ["lsar", "-j", path])) A.tool(PKGS, ["unar", "-q", "-f", "-forks", "visible", "-o", outdir, path]) out = [] for e in j["lsarContents"]: name = raw = e["XADFileName"] if "%" in name and "%" not in known: name = known rsrc = bool(e.get("XADIsResourceFork")) if rsrc and not e.get("XADFileSize"): continue # an empty resource fork: not an entry in our convention fn = os.path.join(outdir, raw + (".rsrc" if rsrc else "")) b = open(fn, "rb").read() if os.path.exists(fn) else b"" if rsrc: # unar writes the resource fork as AppleDouble: take entry 2 n = struct.unpack_from(">H", b, 24)[0] for k in range(n): i, o, l = struct.unpack_from(">III", b, 26 + 12 * k) if i == 2: b = b[o:o + l] d = {"name": name + (".rsrc" if rsrc else ""), "size": e.get("XADFileSize", 0), "crc": zlib.crc32(b)} if "XADFileType" in e: d["macType"] = struct.pack(">I", e["XADFileType"]).decode("latin-1") d["macCreator"] = struct.pack(">I", e["XADFileCreator"]).decode("latin-1") out.append(d) if any(e["name"].endswith(".rsrc") for e in out): # an empty data fork beside a resource fork: no entry out = [e for e in out if e["size"] or e["name"].endswith(".rsrc")] return out def generate(ctx): rng = ctx.rng(17) files = [ ("Read Me", b"Hello from the data fork\r" * 40, rng.randbytes(1500), b"TEXT", b"ttxt", 0x0100), ("Appé π", rng.randbytes(3000) + b"\x90" * 300 + b"\x90\x00\x90", b"", b"APPL", b"????", 0), ("rsrc only", b"", bytes(range(256)) * 4 + b"\x00" * 600, b"rsrc", b"RSED", 0x2000), ("runs", b"A" * 1000 + b"\x90" * 7 + b"B" * 2, b"\x90\x90", b"BINA", b"tool", 0), ] tmp = A.tmpdir() cases = [] try: vol = os.path.join(tmp, "v.hfs") with open(vol, "wb") as f: f.truncate(4 * 1024 * 1024) script = ["hformat -l Fixtures v.hfs >/dev/null"] for k, (name, data, rsrc, t, c, fl) in enumerate(files): open(os.path.join(tmp, "in%d.bin" % k), "wb").write(macbinary(name, data, rsrc, t, c, fl, A.MTIME)) script.append("hcopy -m in%d.bin :" % k) for k, (name, *_ ) in enumerate(files): script.append("hcopy -m ':%s' out%d.bin" % (name, k)) script.append("hcopy -b ':%s' out%d.hqx" % (name, k)) script.append("humount") # hfsutils names are Mac OS Roman bytes: the script file is written in that encoding script.insert(1, "hmount v.hfs >/dev/null") open(os.path.join(tmp, "run.sh"), "wb").write(("set -e\n" + "\n".join(script) + "\n").encode("mac_roman")) A.tool(PKGS, ["sh", "run.sh"], cwd=tmp) for k, (name, data, rsrc, t, c, fl) in enumerate(files): for fmt, fn in (("macbinary", "out%d.bin" % k), ("binhex", "out%d.hqx" % k)): p = os.path.join(tmp, fn) cases.append({"format": fmt, "name": "hfsutils %s %s" % (fmt, name), "data": open(p, "rb").read(), "entries": listing(p, os.path.join(tmp, "x" + fn), name)}) # AppleSingle v2, AppleDouble v2, AppleSingle v1 fi = t + c + struct.pack(">H", fl) + b"\0" * 22 dates = struct.pack(">iiii", A.MTIME - 946684800, A.MTIME - 946684800, -2 ** 31, -2 ** 31) nm = name.encode("mac_roman") for label, blob in ( ("applesingle v2", apple(0x51600, 2, [(3, nm), (8, dates), (9, fi), (1, data), (2, rsrc)])), ("appledouble v2", apple(0x51607, 2, [(9, fi), (2, rsrc), (3, nm)])), ("applesingle v1", apple(0x51600, 1, [(3, nm), (7, struct.pack(">III", A.MTIME + MAC_EPOCH, A.MTIME + MAC_EPOCH, 0) + b"\0" * 4), (9, fi), (1, data), (2, rsrc)])), ): p = os.path.join(tmp, "as%d-%s" % (k, label.replace(" ", ""))) open(p, "wb").write(blob) if label.endswith("v1"): # unar reads version 2 only: the entries we built ents = [{"name": name, "size": len(data), "crc": zlib.crc32(data), "macType": t.decode(), "macCreator": c.decode()}] if data or not rsrc else [] if rsrc: ents.append({"name": name + ".rsrc", "size": len(rsrc), "crc": zlib.crc32(rsrc)}) else: ents = listing(p, p + ".x", name) cases.append({"format": "applesingle", "name": "%s %s" % (label, name), "data": blob, "entries": ents}) ours = [] for fmt, opts in (("binhex", "mac=true"), ("macbinary", "mac=true"), ("applesingle", "mac=true"), ("applesingle", "mac=true,double=true")): blob = A.ours(ctx, fmt, opts) p = os.path.join(tmp, "ours-" + fmt + opts.replace(",", "").replace("=", "")) open(p, "wb").write(blob) got = {e["name"]: e for e in listing(p, p + ".x", "hello.txt")} hello, rs = A.FILES[0][1], A.content(700, 3) want = {"hello.txt.rsrc": zlib.crc32(rs)} if "double" not in opts: want["hello.txt"] = zlib.crc32(hello) ok = all(n in got and got[n]["crc"] == c for n, c in want.items()) and all(e.get("macType") == "TEXT" for e in got.values()) ours.append({"format": fmt, "variant": opts, "ok": ok, "detail": json.dumps(list(got.values()))}) ctx.emit("mac", {"cases": cases, "ours": ours}) finally: shutil.rmtree(tmp)