diff --git a/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift b/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift index 2cc11210..90f0351f 100644 --- a/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift +++ b/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift @@ -87,7 +87,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation { var entitlements: Entitlements // if the executable doesn't already have entitlements, that's // okay. We don't have to throw an error. - if let entitlementsData = context.signerImpl.analyze(executable: executableURL), + if let entitlementsData = try? context.signerImpl.analyze(executable: executableURL), let decodedEntitlements = try? PropertyListDecoder().decode(Entitlements.self, from: entitlementsData) { entitlements = decodedEntitlements diff --git a/Supersign/Model/Entitlements/Entitlements.swift b/Supersign/Model/Entitlements/Entitlements.swift index 6642f0f5..7b87d607 100644 --- a/Supersign/Model/Entitlements/Entitlements.swift +++ b/Supersign/Model/Entitlements/Entitlements.swift @@ -17,14 +17,10 @@ public struct EntitlementContainer: ProtoCodableContainer { // allows access to a parsable entitlements list while not losing unparsed entitlements when it's modified public struct Entitlements { - public enum Error: Swift.Error { - case dataCorrupted - } - private let decoder = PropertyListDecoder() private let encoder = PropertyListEncoder() - private var dict: [String: PlistItem] + private var dict: [String: AnyEncodable] public init(entitlements: [Entitlement]) throws { dict = [:] @@ -49,9 +45,7 @@ public struct Entitlements { let newEnts = Dictionary(uniqueKeysWithValues: new.map { (type(of: $0).identifier, $0) }) oldEnts.keys.filter { newEnts[$0] == nil }.forEach { dict[$0] = nil } - try newEnts.forEach { - dict[$0] = try PlistItem(value: $1.plistValue(withEncoder: encoder)) - } + newEnts.forEach { dict[$0] = AnyEncodable($1) } } public mutating func setEntitlements(_ new: [Entitlement]) throws { @@ -64,7 +58,8 @@ extension Entitlements: Codable { public init(from decoder: Decoder) throws { self.dict = try decoder.singleValueContainer() - .decode([String: PlistItem].self) + .decode([String: CodableElement].self) + .mapValues(AnyEncodable.init) } public func encode(to encoder: Encoder) throws { diff --git a/Supersign/SignerImpl/SignerImpl.swift b/Supersign/SignerImpl/SignerImpl.swift index c6e60990..cfbbccbf 100644 --- a/Supersign/SignerImpl/SignerImpl.swift +++ b/Supersign/SignerImpl/SignerImpl.swift @@ -20,6 +20,7 @@ public struct SignerImpl { public enum Error: LocalizedError { case notFound + case badFilePath case signer(String?) public var errorDescription: String? { @@ -28,6 +29,10 @@ public struct SignerImpl { return NSLocalizedString( "signer_impl.error.not_found", value: "No signer implementation found", comment: "" ) + case .badFilePath: + return NSLocalizedString( + "signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: "" + ) case .signer(let error?): return error case .signer(nil): @@ -48,13 +53,12 @@ public struct SignerImpl { analyze = signer.analyze } - public static func all() -> [SignerImpl] { - CLinkedList(start: signer_list).map(SignerImpl.init) + private static func all() -> AnySequence { + .init(CLinkedList(first: signer_list).lazy.map(SignerImpl.init)) } public static func first() throws -> SignerImpl { - guard let impl = all().first else { throw Error.notFound } - return impl + try all().makeIterator().next().orThrow(Error.notFound) } private func _sign( @@ -68,10 +72,11 @@ public struct SignerImpl { encoder.outputFormat = .xml let entsArray: [entitlements_data_t] = try entitlementMapping.map { url, ents in try url.withUnsafeFileSystemRepresentation { bundlePath in - try encoder.encode(ents).withUnsafeBytes { bytes in + guard let bundlePath = bundlePath else { throw Error.badFilePath } + return try encoder.encode(ents).withUnsafeBytes { bytes in let bound = bytes.bindMemory(to: Int8.self) return entitlements_data_t( - bundle_path: bundlePath, data: bound.baseAddress, len: bound.count + bundle_path: bundlePath, data: bound.baseAddress!, len: bound.count ) } } @@ -87,11 +92,11 @@ public struct SignerImpl { guard sign( app.path, - certBound.baseAddress, + certBound.baseAddress!, certBound.count, - privBound.baseAddress, + privBound.baseAddress!, privBound.count, - ents.baseAddress, + ents.baseAddress!, ents.count, progress, &exception @@ -127,13 +132,17 @@ public struct SignerImpl { } } - public func analyze(executable: URL) -> Data? { - executable.withUnsafeFileSystemRepresentation({ (path: UnsafePointer?) -> Data? in + public func analyze(executable: URL) throws -> Data { + try executable.withUnsafeFileSystemRepresentation { (path: UnsafePointer?) -> Data in + var exception: UnsafeMutablePointer? + defer { exception.map { free($0) } } var len = 0 - guard let out = analyze(path, &len) else { return nil } + guard let out = analyze(path!, &len, &exception) else { + throw Error.signer(exception.map { String(cString: $0) }) + } defer { free(out) } - return Data(bytes: UnsafeRawPointer(out), count: len) - }) + return Data(bytes: out, count: len) + } } }