Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
iOS shell: read each index chunk completely before hashing it
v0lumehi32e9538e0cd5
diff --git a/frontend-ios/Sources/ShellHost/RomfsIdentity.swift b/frontend-ios/Sources/ShellHost/RomfsIdentity.swiftindex 777c440b..2173eba5 100644--- a/frontend-ios/Sources/ShellHost/RomfsIdentity.swift+++ b/frontend-ios/Sources/ShellHost/RomfsIdentity.swift@@ -7,7 +7,8 @@ struct RomfsIdentity: Equatable, Sendable {var size: UInt64var index: UInt64- private static let offsetBasis: UInt64 = 0xcbf29ce484222325, prime: UInt64 = 0x100000001b3, chunk: UInt64 = 1 << 20+ static let chunkSize: UInt64 = 1 << 20+ private static let offsetBasis: UInt64 = 0xcbf29ce484222325, prime: UInt64 = 0x100000001b3/// FNV-1a 64 over the little-endian 64-bit words of `bytes`, zero-padded to a multiple of 8 (`romfs_id_mix`).static func mix(_ hash: UInt64, _ bytes: [UInt8]) -> UInt64 {@@ -41,7 +42,9 @@ struct RomfsIdentity: Equatable, Sendable {}/// The identity of the image at `url`, nil for a missing, cut off or invalid image (`romfs_id_of_image`).- static func ofImage(at url: URL) -> RomfsIdentity? {+ /// The index is mixed in chunks of `chunk` bytes, each read completely first (a read may return less, on a file provider or an+ /// external drive) and only the last chunk padded, as `romfs_id_of_image` does. The chunk is a parameter for tests.+ static func ofImage(at url: URL, chunk: UInt64 = chunkSize) -> RomfsIdentity? {guard let handle = try? FileHandle(forReadingFrom: url) else { return nil }defer { try? handle.close() }guard let total = try? handle.seekToEnd(), (try? handle.seek(toOffset: 0)) != nil,@@ -55,9 +58,14 @@ struct RomfsIdentity: Equatable, Sendable {try handle.seek(toOffset: indexOffset)var left = indexSizewhile left > 0 {- guard let data = try handle.read(upToCount: Int(min(left, chunk))), !data.isEmpty else { return nil }- hash = mix(hash, [UInt8](data))- left -= UInt64(data.count)+ let want = Int(min(left, chunk))+ var piece = Data()+ while piece.count < want {+ guard let part = try handle.read(upToCount: want - piece.count), !part.isEmpty else { return nil } // cut off+ piece.append(part)+ }+ hash = mix(hash, [UInt8](piece))+ left -= UInt64(want)}} catch { return nil }return RomfsIdentity(size: total, index: hash)diff --git a/frontend-ios/Sources/ShellHost/ShellHostModel.swift b/frontend-ios/Sources/ShellHost/ShellHostModel.swiftindex d7ead35b..fc5d9204 100644--- a/frontend-ios/Sources/ShellHost/ShellHostModel.swift+++ b/frontend-ios/Sources/ShellHost/ShellHostModel.swift@@ -120,6 +120,8 @@ public struct ShellHostConfig: Sendable {e.problem == nil ? e.manifest.flatMap { m in RomfsIdentity.parse(m.romfsIdentity).map { (m.titleID, $0) } } : nil}guard !wanted.isEmpty else { return }+ // `shellData.resolve()` above opened the bookmark's security scope and `BookmarkStore` keeps it open while this folder is+ // chosen (until another is stored or it is cleared), so the detached scan and its header reads run inside it.shellScan = Task { [weak self] inlet found = await Task.detached(priority: .utility) { () -> [String: URL] inlet index = ShellDataIndex(folder: folder)diff --git a/frontend-ios/Tests/ShellHostTests/ShellDataFolderTests.swift b/frontend-ios/Tests/ShellHostTests/ShellDataFolderTests.swiftindex 232e1505..cd041b8e 100644--- a/frontend-ios/Tests/ShellHostTests/ShellDataFolderTests.swift+++ b/frontend-ios/Tests/ShellHostTests/ShellDataFolderTests.swift@@ -49,6 +49,22 @@ import ShellUI#expect(RomfsIdentity.parse("DMROMFSID1 fixture") == nil && RomfsIdentity.parse("DMROMFSID1 size=5") == nil && RomfsIdentity.parse("size=5 index=ab") == nil)}+ /// An index of 3 MiB and 5 bytes spans four 1 MiB chunks, the last one padded. The expected value is what `romfs_id_of_image` gives+ /// for this image. A smaller chunk (a multiple of 8) must give the same hash.+ @Test func aLargeIndexHashesTheSameInAnyChunkSize() throws {+ let box = try Sandbox()+ let index = (0..<(3 * (1 << 20) + 5)).map { UInt8(truncatingIfNeeded: $0 &* 31 &+ 7) }+ let url = box.root.appendingPathComponent("big.img")+ try place(Self.dmromfs(index, data: [UInt8](repeating: 0x42, count: 16)), at: url)+ let want = RomfsIdentity(size: 3_145_781, index: 0x3a99427aa95b96e1)+ #expect(RomfsIdentity.ofImage(at: url) == want)+ #expect(RomfsIdentity.ofImage(at: url, chunk: 4096) == want)+ #expect(RomfsIdentity.ofImage(at: url, chunk: 8) == want)+ // Cut off inside the index: not an image.+ try Data(contentsOf: url).prefix(2_000_000).write(to: url)+ #expect(RomfsIdentity.ofImage(at: url) == nil)+ }+@Test func anInvalidOrCutOffImageHasNoIdentity() throws {let box = try Sandbox()let good = Self.dmromfs(Self.indexA)