diff --git a/Sources/AssetKit/BOM/BOMTree.swift b/Sources/AssetKit/BOM/BOMTree.swift index 89e38c9..4b7d9b9 100644 --- a/Sources/AssetKit/BOM/BOMTree.swift +++ b/Sources/AssetKit/BOM/BOMTree.swift @@ -64,15 +64,36 @@ struct BOMTree { leaf.writeBE(valueBlockIDs[i]) leaf.writeBE(keyBlockIDs[i]) } - if leaf.offset < Int(blockSize) { - leaf.writeZeros(Int(blockSize) - leaf.offset) + // BOM trees whose keys all have the same length store them inline: + // the header gives that length and actool follows the leaf entries + // with 4 zero bytes and a copy of every key, sizing the leaf + // blockSize + the total key length. With keys of different lengths + // (image names in FACETKEYS, appearance names), the header says + // 0xFFFFFFFF and the leaf has no copy. App Store Connect reads keys + // through this copy when the length is fixed: without it, or with a + // fixed length on keys that differ, it reports the app icon as + // missing (ITMS-90022, 90023, 91111). + let keyLengths = Set(sorted.map(\.key.count)) + let fixedKeyLength: Int? = keyLengths.count == 1 ? keyLengths.first : nil + if fixedKeyLength != nil { + leaf.writeZeros(4) + for entry in sorted { + leaf.write(entry.key) + } + } + let leafSize = Int(blockSize) + (fixedKeyLength != nil ? sorted.reduce(0) { $0 + $1.key.count } : 0) + if leaf.offset < leafSize { + leaf.writeZeros(leafSize - leaf.offset) } let leafID = bom.addBlock(leaf.data) - // Tree header block. actool's tree headers are 29 bytes -- 21 bytes - // of fixed fields plus 8 trailing zeros (probably reserved/align). - // Match the layout so all trees in our output are the same size as - // the reference. + // Tree header block, 29 bytes like actool's. After the fixed fields + // actool writes the key length as a big-endian u32 when all keys + // have the same length (18 or 20 for RENDITIONS, 7 for a FACETKEYS + // tree holding only "AppIcon"), 0xFFFFFFFF when they differ, then 4 + // zero bytes. App Store Connect never finishes processing a build + // whose catalog leaves it at 0: the upload stays in PROCESSING with + // no error. var header = ByteWriter() header.writeBE(treeMagic) header.writeBE(UInt32(1)) // version @@ -80,7 +101,8 @@ struct BOMTree { header.writeBE(blockSize) header.writeBE(UInt32(sorted.count)) // pathCount header.write(byte: 0) // isPathInternal - header.writeZeros(8) // trailing reserved + header.writeBE(fixedKeyLength.map { UInt32($0) } ?? 0xFFFF_FFFF) // key length + header.writeZeros(4) // reserved return bom.addBlock(header.data) } diff --git a/Sources/AssetKit/CAR/CSIWriter.swift b/Sources/AssetKit/CAR/CSIWriter.swift index 2bdeace..eeb5962 100644 --- a/Sources/AssetKit/CAR/CSIWriter.swift +++ b/Sources/AssetKit/CAR/CSIWriter.swift @@ -16,7 +16,15 @@ enum CSIWriter { .bitmapFlag, .bytesPerRow(width: body.width), ]) - let payload = MLECBody.encode(width: body.width, height: body.height, pixelsBGRA: body.pixelsBGRA) + // actool writes 1 in the MLEC field after the magic for images and 3 + // for app icons (see MLECBody). With 3, iOS draws an image's + // transparent pixels black. + let payload = MLECBody.encode( + width: body.width, + height: body.height, + pixelsBGRA: body.pixelsBGRA, + pixelMode: body.kind == .appIcon ? .opaque : .alpha + ) // Bit 4 (0x10): generic-image category; set for `.image`, cleared // for `.appIcon`. Matches what UIImage(named:) walks for imageset diff --git a/Sources/AssetKit/CAR/MLECBody.swift b/Sources/AssetKit/CAR/MLECBody.swift index 8164c57..61bb216 100644 --- a/Sources/AssetKit/CAR/MLECBody.swift +++ b/Sources/AssetKit/CAR/MLECBody.swift @@ -5,8 +5,8 @@ import Foundation /// Layout verified against actool's reference Assets.car: /// /// MLEC magic 4 bytes -/// compressionType u32 (0 = raw, 3 = LZFSE) -/// bytesPerPixel u32 (4 for BGRA8) +/// pixelMode u32 (1 = with alpha, 3 = opaque, see PixelMode) +/// compression u32 (4 = LZFSE) /// chunkCount u32 (1 or 3) /// then chunkCount * KCBC chunks /// @@ -23,8 +23,36 @@ import Foundation /// single chunk covering the whole image. The 3-chunk split is mimicry /// rather than a correctness requirement: CoreUI accepts both layouts. enum MLECBody { - static func encode(width: UInt32, height: UInt32, pixelsBGRA: [UInt8]) -> Data { - let bytesPerRow = Int(width) * 4 + /// Rows are padded to a multiple of 32 bytes, like actool, and the + /// `bytesPerRow` TLV says so. App Store Connect never finishes processing + /// a build whose catalog stores tightly packed rows that are not already + /// a multiple of 32 bytes (a 180 px icon: 720 bytes instead of 736). + static func bytesPerRow(width: UInt32) -> Int { + (Int(width) * 4 + 31) & ~31 + } + + /// The u32 after the MLEC magic. actool writes 1 for every image with + /// transparency (534 of 534 renditions in Xcode's own catalogs) and 3 only + /// in app icon catalogs (the store icon, icon atlases). iOS treats a 3 + /// bitmap as opaque: its transparent pixels come out black. + enum PixelMode: UInt32 { + case alpha = 1 + case opaque = 3 + } + + static func encode(width: UInt32, height: UInt32, pixelsBGRA: [UInt8], pixelMode: PixelMode = .alpha) -> Data { + let packedRow = Int(width) * 4 + let bytesPerRow = Self.bytesPerRow(width: width) + var padded = pixelsBGRA + if bytesPerRow != packedRow { + padded = [UInt8](repeating: 0, count: bytesPerRow * Int(height)) + for row in 0.. 0, "LZFSE encoding failed for \(input.count)-byte buffer") return Array(output.prefix(encoded)) } + + /// Decodes an LZFSE stream whose decoded size is at most `capacity`. + /// Only used to check the encoder's output. + static func decode(_ input: [UInt8], capacity: Int = 1 << 24) -> [UInt8] { + var output = [UInt8](repeating: 0, count: capacity) + let decoded = input.withUnsafeBufferPointer { inBuf -> Int in + output.withUnsafeMutableBufferPointer { outBuf in + lzfse_decode_buffer(outBuf.baseAddress!, capacity, inBuf.baseAddress!, input.count, nil) + } + } + return Array(output.prefix(decoded)) + } } diff --git a/Tests/AssetKitTests/RowPaddingTests.swift b/Tests/AssetKitTests/RowPaddingTests.swift new file mode 100644 index 0000000..fdb19e7 --- /dev/null +++ b/Tests/AssetKitTests/RowPaddingTests.swift @@ -0,0 +1,43 @@ +import Foundation +import Testing +@testable import AssetKit + +@Suite("Bitmap row padding") +struct RowPaddingTests { + @Test("Rows are padded to 32 bytes, in the TLV and in the pixels") + func rowPadding() throws { + for (width, expected) in [(20, 96), (29, 128), (87, 352), (167, 672), (180, 736), (256, 1024)] { + #expect(MLECBody.bytesPerRow(width: UInt32(width)) == expected, "\(width)") + } + // 29 px wide, 3 rows: each stored row is 128 bytes, the last 12 zero + let width = 29, height = 3 + let pixels = [UInt8](repeating: 0xAB, count: width * 4 * height) + let body = MLECBody.encode(width: UInt32(width), height: UInt32(height), pixelsBGRA: pixels) + func le32(_ o: Int) -> Int { + Int(body[body.startIndex + o]) | Int(body[body.startIndex + o + 1]) << 8 + | Int(body[body.startIndex + o + 2]) << 16 | Int(body[body.startIndex + o + 3]) << 24 + } + // 3 rows divide by 3: one KCBC chunk per row. MLEC header 16 bytes, chunk header 20 bytes + var offset = 16 + for _ in 0..<3 { + let size = le32(offset + 16) + let raw = LZFSE.decode(Array(body[body.startIndex + offset + 20..