Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatKnowledge/Sources/ZipArchive.swift3// Description: Minimal read-only ZIP reader (central directory, stored + raw-DEFLATE entries, CRC-32 check) used to open DOCX files without a dependency.4import Compression5import Foundation6import zlib78struct ZipArchive {9 enum ZipError: Error, Equatable { case notZip, corrupt(String), encrypted, unsupported(String), missing(String), tooLarge }1011 struct Entry { var name: String; var method: UInt16; var flags: UInt16; var crc: UInt32; var compressedSize: Int; var uncompressedSize: Int; var localOffset: Int }1213 let data: Data14 private(set) var entries: [String: Entry] = [:]1516 init(data: Data) throws {17 self.data = data18 let n = data.count19 guard n >= 22, u32(0) == 0x0403_4b50 || u32(0) == 0x0605_4b50 else { throw ZipError.notZip }20 // end of central directory: scan backwards over the optional comment21 var eocd = -1; var i = n - 2222 let floor = max(0, n - 22 - 65_535)23 while i >= floor { if u32(i) == 0x0605_4b50 { eocd = i; break }; i -= 1 }24 guard eocd >= 0 else { throw ZipError.corrupt("end of central directory not found") }25 let total = Int(u16(eocd + 10)); let cdSize = Int(u32(eocd + 12)); let cdOffset = Int(u32(eocd + 16))26 guard total != 0xFFFF, cdOffset != 0xFFFF_FFFF else { throw ZipError.unsupported("ZIP64 archives are not supported") }27 guard cdOffset >= 0, cdSize >= 0, cdOffset + cdSize <= n else { throw ZipError.corrupt("central directory out of bounds") }28 var p = cdOffset29 for _ in 0..<total {30 guard p + 46 <= n, u32(p) == 0x0201_4b50 else { throw ZipError.corrupt("bad central directory entry") }31 let nameLen = Int(u16(p + 28)), extraLen = Int(u16(p + 30)), commentLen = Int(u16(p + 32))32 guard p + 46 + nameLen <= n else { throw ZipError.corrupt("truncated entry name") }33 let name = String(decoding: data.subdata(in: (data.startIndex + p + 46)..<(data.startIndex + p + 46 + nameLen)), as: UTF8.self)34 entries[name] = Entry(name: name, method: u16(p + 10), flags: u16(p + 8), crc: u32(p + 16), compressedSize: Int(u32(p + 20)),35 uncompressedSize: Int(u32(p + 24)), localOffset: Int(u32(p + 42)))36 p += 46 + nameLen + extraLen + commentLen37 }38 }3940 func contents(of name: String, maxSize: Int = 256 * 1_048_576) throws -> Data {41 guard let e = entries[name] else { throw ZipError.missing(name) }42 if e.flags & 1 != 0 { throw ZipError.encrypted }43 guard e.uncompressedSize <= maxSize else { throw ZipError.tooLarge }44 let n = data.count45 guard e.localOffset + 30 <= n, u32(e.localOffset) == 0x0403_4b50 else { throw ZipError.corrupt("bad local header for \(name)") }46 let start = e.localOffset + 30 + Int(u16(e.localOffset + 26)) + Int(u16(e.localOffset + 28))47 guard start + e.compressedSize <= n else { throw ZipError.corrupt("entry data out of bounds") }48 let raw = data.subdata(in: (data.startIndex + start)..<(data.startIndex + start + e.compressedSize))49 let out: Data50 switch e.method {51 case 0: out = raw52 case 8:53 guard e.uncompressedSize > 0 else { out = Data(); break }54 var buffer = Data(count: e.uncompressedSize)55 let written = buffer.withUnsafeMutableBytes { dst -> Int in56 raw.withUnsafeBytes { src -> Int in57 guard let d = dst.bindMemory(to: UInt8.self).baseAddress, let s = src.bindMemory(to: UInt8.self).baseAddress else { return 0 }58 return compression_decode_buffer(d, e.uncompressedSize, s, raw.count, nil, COMPRESSION_ZLIB)59 }60 }61 guard written == e.uncompressedSize else { throw ZipError.corrupt("could not inflate \(name)") }62 out = buffer63 default: throw ZipError.unsupported("compression method \(e.method)")64 }65 let crc = out.withUnsafeBytes { UInt32(crc32(0, $0.bindMemory(to: UInt8.self).baseAddress, UInt32($0.count))) }66 guard crc == e.crc else { throw ZipError.corrupt("checksum mismatch in \(name)") }67 return out68 }6970 private func u16(_ o: Int) -> UInt16 { data.withUnsafeBytes { $0.loadUnaligned(fromByteOffset: o, as: UInt16.self) } }71 private func u32(_ o: Int) -> UInt32 { data.withUnsafeBytes { $0.loadUnaligned(fromByteOffset: o, as: UInt32.self) } }72}73