Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import Foundation
struct ReleaseFixtures { static func tar(_ entries: [(String, UInt8, String, Data)]) throws -> Data { var archive = Data() for (name, kind, link, content) in entries { var header = [UInt8](repeating: 0, count: 512) func field(_ value: String, _ start: Int, _ length: Int) throws { let bytes = Array(value.utf8) guard bytes.count <= length else { throw DevError("Fixture field exceeds tar limits") } header.replaceSubrange(start..<(start + bytes.count), with: bytes) } try field(name, 0, 100) try field("0000644\0", 100, 8) try field("0000000\0", 108, 8) try field("0000000\0", 116, 8) try field(String(format: "%011o", content.count) + "\0", 124, 12) try field("00000000000\0", 136, 12) header.replaceSubrange(148..<156, with: [UInt8](repeating: 32, count: 8)) header[156] = kind try field(link, 157, 100) try field("ustar\0", 257, 6) try field("00", 263, 2) let checksum = header.reduce(0) { $0 + Int($1) } try field(String(format: "%06o", checksum) + "\0 ", 148, 8) archive.append(contentsOf: header) archive.append(content) archive.append(Data(repeating: 0, count: (512 - content.count % 512) % 512)) } archive.append(Data(repeating: 0, count: 1024)) return archive }
static func archive(_ args: [String]) throws { guard args.count == 2 else { throw DevError("Usage: release-archive-fixture ATTACK OUTPUT") } let names = [ "traversal": "../outside", "absolute": "/tmp/outside", "hardlink": "Fixture.app/hard", "symlink": "Fixture.app/link/file", "escaping": "Fixture.app/link", "chain": "Fixture.app/a", "fifo": "Fixture.app/fifo", "duplicate": "Fixture.app/file", "absolute-link": "Fixture.app/link", "dangling": "Fixture.app/link", "wrong-root": "Other.app/file", ] guard let name = names[args[0]] else { throw DevError("Unknown archive fixture") } let entries: [(String, UInt8, String, Data)] switch args[0] { case "hardlink": entries = [(name, 49, "Fixture.app/file", Data())] case "fifo": entries = [(name, 54, "", Data())] case "escaping", "chain", "absolute-link", "dangling": let links = [ "escaping": "../../outside", "chain": "a", "absolute-link": "/tmp/outside", "dangling": "missing", ] guard let link = links[args[0]] else { throw DevError("Missing archive fixture link") } entries = [(name, 50, link, Data())] case "symlink": entries = [("Fixture.app/link", 50, ".", Data()), (name, 48, "", Data("x".utf8))] case "duplicate": entries = [(name, 48, "", Data("x".utf8)), (name, 48, "", Data("x".utf8))] default: entries = [(name, 48, "", Data("x".utf8))] } let compressed = try DevProcess.checked("/usr/bin/gzip", ["-c"], input: tar(entries)) try compressed.write(to: URL(fileURLWithPath: args[1])) }
static func seedInputs(_ args: [String]) throws { guard args.count == 1 else { throw DevError("Usage: update-seed-fixtures DIRECTORY") } let root = URL(fileURLWithPath: args[0]) let canonical = try Data(contentsOf: root.appendingPathComponent("seed")) let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".utf8) guard canonical.count >= 2, let index = alphabet.firstIndex(of: canonical[canonical.count - 2]), index + 1 < alphabet.count else { throw DevError("Invalid canonical fixture seed") } var noncanonical = canonical noncanonical[noncanonical.count - 2] = alphabet[index + 1] guard Data(base64Encoded: canonical) == Data(base64Encoded: noncanonical) else { throw DevError("Fixture seed did not preserve decoded bytes") } let cases: [String: Data] = [ "empty": Data(), "malformed": Data("fixture-invalid-base64".utf8), "utf8": Data([255]), "noncanonical": noncanonical, "newline": canonical + Data([10]), "space": Data([32]) + canonical, "short": Data(repeating: 0, count: 31).base64EncodedData(), "long": Data(repeating: 0, count: 33).base64EncodedData(), "keypair": Data(repeating: 0, count: 64).base64EncodedData(), ] for (name, value) in cases { try value.write(to: root.appendingPathComponent(name + ".seed-input")) } }}