Repository Public License
1// ORIX Convert — Packages/ORIXConvertCore/Sources/ConvertCore/Detection/ContainerSniffers.swift2// Description: Bounded readers for the container formats the detector has to look inside: ZIP (central directory or3// leading local headers), OLE2 compound files (directory stream names), TIFF IFD0 tags, ISO-BMFF boxes.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Compression6import Foundation78struct ZipListing {9 struct Entry {10 let name: String11 let method: Int12 let compressedSize: Int6413 let localHeaderOffset: Int6414 }1516 var entries: [Entry] = []17 /// `true` when the names come from the central directory (the whole archive is listed, up to the bounds).18 var fromCentralDirectory = false19 var names: [String] { entries.map(\.name) }2021 static let maxEntries = 20_00022 static let maxCentralDirectoryBytes = 4 << 2023 static let maxEntryContent = 1 << 202425 /// Lists a ZIP archive through its central directory; falls back to walking the first local headers when the26 /// end of the archive is missing (truncated download).27 static func read(_ src: any ByteSource) -> ZipListing {28 if let listing = readCentralDirectory(src), !listing.entries.isEmpty { return listing }29 return readLocalHeaders(src)30 }3132 private static func readCentralDirectory(_ src: any ByteSource) -> ZipListing? {33 let tailLength = Int(min(src.size, 65_557))34 guard tailLength >= 22 else { return nil }35 let tailOffset = src.size - Int64(tailLength)36 let tail = src.read(at: tailOffset, count: tailLength)37 guard tail.count >= 22 else { return nil }38 var eocd = -139 var i = tail.count - 2240 while i >= 0 {41 if tail[i] == 0x50, tail.has([0x50, 0x4B, 0x05, 0x06], at: i) { eocd = i; break }42 i -= 143 }44 guard eocd >= 0, var total = tail.u16le(eocd + 10), var cdSize = tail.u32le(eocd + 12).map(Int64.init),45 var cdOffset = tail.u32le(eocd + 16).map(Int64.init) else { return nil }46 if cdOffset == 0xFFFF_FFFF || total == 0xFFFF || cdSize == 0xFFFF_FFFF {47 // ZIP64: locator sits right before the end record.48 let locator = eocd - 2049 guard locator >= 0, tail.has([0x50, 0x4B, 0x06, 0x07], at: locator), let recordOffset = tail.u64le(locator + 8),50 recordOffset < UInt64(src.size) else { return nil }51 let record = src.read(at: Int64(recordOffset), count: 56)52 guard record.has([0x50, 0x4B, 0x06, 0x06]), let n = record.u64le(32), let s = record.u64le(40), let o = record.u64le(48),53 s < UInt64(Int64.max), o < UInt64(Int64.max) else { return nil }54 total = Int(min(n, UInt64(maxEntries)))55 cdSize = Int64(s)56 cdOffset = Int64(o)57 }58 guard cdOffset >= 0, cdOffset < src.size, cdSize > 0 else { return nil }59 let cd = src.read(at: cdOffset, count: Int(min(cdSize, Int64(maxCentralDirectoryBytes))))60 var listing = ZipListing()61 listing.fromCentralDirectory = true62 var o = 063 while listing.entries.count < min(total, maxEntries), cd.has([0x50, 0x4B, 0x01, 0x02], at: o) {64 guard let method = cd.u16le(o + 10), let comp = cd.u32le(o + 20), let nameLen = cd.u16le(o + 28),65 let extraLen = cd.u16le(o + 30), let commentLen = cd.u16le(o + 32), let local = cd.u32le(o + 42),66 o + 46 + nameLen <= cd.count else { break }67 let name = String(decoding: cd[(o + 46)..<(o + 46 + nameLen)], as: UTF8.self)68 listing.entries.append(Entry(name: name, method: method, compressedSize: Int64(comp), localHeaderOffset: Int64(local)))69 o += 46 + nameLen + extraLen + commentLen70 }71 return listing72 }7374 private static func readLocalHeaders(_ src: any ByteSource) -> ZipListing {75 var listing = ZipListing()76 var offset: Int64 = 077 while listing.entries.count < 64 {78 let header = src.read(at: offset, count: 30 + 1024)79 guard header.has([0x50, 0x4B, 0x03, 0x04]), let flags = header.u16le(6), let method = header.u16le(8),80 let comp = header.u32le(18), let nameLen = header.u16le(26), let extraLen = header.u16le(28),81 30 + nameLen <= header.count else { break }82 let name = String(decoding: header[30..<(30 + nameLen)], as: UTF8.self)83 listing.entries.append(Entry(name: name, method: method, compressedSize: Int64(comp), localHeaderOffset: offset))84 // With a data descriptor the size is unknown here: the walk cannot continue safely.85 if flags & 0x08 != 0, comp == 0 { break }86 offset += Int64(30 + nameLen + extraLen + comp)87 }88 return listing89 }9091 /// Uncompressed content of an entry (stored or deflated), bounded to `maxEntryContent` bytes.92 func content(of name: String, in src: any ByteSource, limit: Int = ZipListing.maxEntryContent) -> [UInt8]? {93 guard let entry = entries.first(where: { $0.name == name }) else { return nil }94 let header = src.read(at: entry.localHeaderOffset, count: 30)95 guard header.has([0x50, 0x4B, 0x03, 0x04]), let nameLen = header.u16le(26), let extraLen = header.u16le(28) else { return nil }96 let dataOffset = entry.localHeaderOffset + Int64(30 + nameLen + extraLen)97 let wanted = entry.compressedSize > 0 ? Int(min(entry.compressedSize, Int64(limit))) : limit98 let raw = src.read(at: dataOffset, count: wanted)99 switch entry.method {100 case 0: return raw101 case 8: return BoundedDecoder.decode(raw, algorithm: COMPRESSION_ZLIB, maxOutput: limit)102 default: return nil103 }104 }105}106107enum OLE2Sniffer {108 /// Names of the directory entries of a compound file (bounded walk of the directory chain).109 static func directoryNames(_ src: any ByteSource) -> [String] {110 let header = src.read(at: 0, count: 512)111 guard header.count == 512, let shift = header.u16le(30), shift == 9 || shift == 12,112 var sector = header.u32le(48) else { return [] }113 let sectorSize = 1 << shift114 let entriesPerFAT = sectorSize / 4115 func offset(_ s: Int) -> Int64 { Int64(s + 1) * Int64(sectorSize) }116 var names: [String] = []117 var visited = Set<Int>()118 for _ in 0..<32 {119 guard sector < 0xFFFF_FFFA, visited.insert(sector).inserted else { break }120 let data = src.read(at: offset(sector), count: sectorSize)121 guard !data.isEmpty else { break }122 var o = 0123 while o + 128 <= data.count {124 if let length = data.u16le(o + 64), length >= 2, length <= 64, let type = data.u8(o + 66), type != 0 {125 var units: [UInt16] = []126 var k = 0127 while k + 1 < length - 2 + 1, k + 1 < 64 {128 let unit = UInt16(data[o + k]) | UInt16(data[o + k + 1]) << 8129 if unit == 0 { break }130 units.append(unit)131 k += 2132 }133 names.append(String(decoding: units, as: UTF16.self))134 }135 o += 128136 }137 // Next sector of the chain, read from the FAT (only FAT sectors listed in the header DIFAT are followed).138 let fatIndex = sector / entriesPerFAT139 guard fatIndex < 109, let fatSector = header.u32le(76 + 4 * fatIndex), fatSector < 0xFFFF_FFFA else { break }140 let cell = src.read(at: offset(fatSector) + Int64((sector % entriesPerFAT) * 4), count: 4)141 guard let next = cell.u32le(0) else { break }142 sector = next143 }144 return names145 }146}147148enum TIFFSniffer {149 /// Tag numbers of the first image file directory (bounded), or an empty set when unreadable.150 static func firstDirectoryTags(_ p: [UInt8], _ src: any ByteSource) -> Set<Int> {151 let little = p.has([0x49, 0x49])152 guard let ifdOffset = little ? p.u32le(4) : p.u32be(4), ifdOffset >= 8, Int64(ifdOffset) < src.size else { return [] }153 let head = src.read(at: Int64(ifdOffset), count: 2 + 12 * 512)154 guard let count = little ? head.u16le(0) : head.u16be(0), count > 0 else { return [] }155 var tags = Set<Int>()156 for i in 0..<min(count, 512) {157 guard let tag = little ? head.u16le(2 + 12 * i) : head.u16be(2 + 12 * i) else { break }158 tags.insert(tag)159 }160 return tags161 }162}163164enum ISOBMFFSniffer {165 /// Major brand followed by the compatible brands of a leading `ftyp` box.166 static func brands(_ p: [UInt8]) -> [String]? {167 guard p.hasASCII("ftyp", at: 4), let size = p.u32be(0), p.count >= 12 else { return nil }168 var brands = [p.latin1(8, 4)]169 let end = min(max(size, 16), min(p.count, 16 + 4 * 64))170 var o = 16171 while o + 4 <= end {172 brands.append(p.latin1(o, 4))173 o += 4174 }175 return brands176 }177178 /// Looks for an `alac` sample entry in the movie box (bounded walk of top-level boxes, bounded read of `moov`).179 static func hasALACSampleEntry(_ src: any ByteSource) -> Bool {180 var offset: Int64 = 0181 for _ in 0..<32 {182 let header = src.read(at: offset, count: 16)183 guard var size = header.u32be(0).map(Int64.init), header.count >= 8 else { return false }184 if size == 1 {185 guard let large = header.u64be(8), large < UInt64(Int64.max) else { return false }186 size = Int64(large)187 } else if size == 0 {188 size = src.size - offset189 }190 guard size >= 8, size <= src.size - offset else { return false }191 if header.hasASCII("moov", at: 4) {192 let moov = src.read(at: offset, count: Int(min(size, 1 << 20)))193 var from = 0194 while let stsd = moov.find(Array("stsd".utf8), from: from) {195 // stsd: 4 version/flags, 4 entry count, then entry size + codec fourcc.196 if moov.hasASCII("alac", at: stsd + 16) { return true }197 from = stsd + 4198 }199 return false200 }201 offset += size202 guard offset < src.size else { return false }203 }204 return false205 }206}207