xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Model / Mobileprovision.swift
Mobileprovision.swift86 lines · 2.9 KiBRaw
| 1 | // |
| 2 | // Mobileprovision.swift |
| 3 | // Supercharge |
| 4 | // |
| 5 | // Created by Kabir Oberai on 07/10/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import CXKit |
| 11 | |
| 12 | public final class Mobileprovision: @unchecked Sendable { |
| 13 | |
| 14 | public enum Error: Swift.Error { |
| 15 | case invalidProfile |
| 16 | } |
| 17 | |
| 18 | public struct Digest: Decodable { |
| 19 | public let name: String |
| 20 | public let uuid: String |
| 21 | public let teamIdentifiers: [DeveloperServicesTeam.ID] |
| 22 | public let creationDate: Date |
| 23 | public let expirationDate: Date |
| 24 | public let devices: [String] |
| 25 | public let certificates: [Certificate] |
| 26 | public let entitlements: Entitlements |
| 27 | |
| 28 | private enum CodingKeys: String, CodingKey { |
| 29 | case name = "Name" |
| 30 | case uuid = "UUID" |
| 31 | case teamIdentifiers = "TeamIdentifier" |
| 32 | case creationDate = "CreationDate" |
| 33 | case expirationDate = "ExpirationDate" |
| 34 | case devices = "ProvisionedDevices" |
| 35 | case certificates = "DeveloperCertificates" |
| 36 | case entitlements = "Entitlements" |
| 37 | } |
| 38 | |
| 39 | public init(from decoder: Decoder) throws { |
| 40 | let container = try decoder.container(keyedBy: CodingKeys.self) |
| 41 | name = try container.decode(String.self, forKey: .name) |
| 42 | uuid = try container.decode(String.self, forKey: .uuid) |
| 43 | teamIdentifiers = try container.decode([DeveloperServicesTeam.ID].self, forKey: .teamIdentifiers) |
| 44 | creationDate = try container.decode(Date.self, forKey: .creationDate) |
| 45 | expirationDate = try container.decode(Date.self, forKey: .expirationDate) |
| 46 | // App Store profiles have no ProvisionedDevices key. |
| 47 | devices = try container.decodeIfPresent([String].self, forKey: .devices) ?? [] |
| 48 | certificates = try container.decode([Certificate].self, forKey: .certificates) |
| 49 | entitlements = try container.decode(Entitlements.self, forKey: .entitlements) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | let raw: mobileprovision_t |
| 54 | |
| 55 | public init(data: Data) throws { |
| 56 | guard let profile: mobileprovision_t = data.withUnsafeBytes({ |
| 57 | guard let base = $0.baseAddress else { return nil } |
| 58 | return mobileprovision_create_from_data(base, $0.count) |
| 59 | }) else { |
| 60 | throw Error.invalidProfile |
| 61 | } |
| 62 | self.raw = profile |
| 63 | } |
| 64 | |
| 65 | deinit { |
| 66 | mobileprovision_free(raw) |
| 67 | } |
| 68 | |
| 69 | public func digest() throws -> Digest { |
| 70 | var len = 0 |
| 71 | guard let ptr = mobileprovision_get_digest(raw, &len), len > 0 else { |
| 72 | throw Error.invalidProfile |
| 73 | } |
| 74 | let data = Data( |
| 75 | bytesNoCopy: UnsafeMutableRawPointer(mutating: ptr), |
| 76 | count: len, |
| 77 | deallocator: .none |
| 78 | ) |
| 79 | return try PropertyListDecoder().decode(Digest.self, from: data) |
| 80 | } |
| 81 | |
| 82 | public func data() throws -> Data { |
| 83 | try Data { mobileprovision_copy_data(raw, &$0) }.orThrow(Error.invalidProfile) |
| 84 | } |
| 85 | |
| 86 | } |