|
| 1 | +import Compression |
| 2 | +import Foundation |
| 3 | + |
| 4 | +public enum LZFSEError: Error, CustomStringConvertible { |
| 5 | + case openInput(String) |
| 6 | + case openOutput(String) |
| 7 | + case streamInit |
| 8 | + case read |
| 9 | + case write |
| 10 | + case process |
| 11 | + |
| 12 | + public var description: String { |
| 13 | + switch self { |
| 14 | + case .openInput(let path): "cannot open input \(path)" |
| 15 | + case .openOutput(let path): "cannot open output \(path)" |
| 16 | + case .streamInit: "compression_stream_init failed" |
| 17 | + case .read: "read failed" |
| 18 | + case .write: "write failed" |
| 19 | + case .process: "compression_stream_process failed" |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// Streaming LZFSE codec over Apple's Compression framework. Release assembly uses this exact |
| 25 | +/// implementation to compress guest payloads, while the installed app uses the same format when it |
| 26 | +/// expands those payloads on first launch. Keeping the codec in the signed runner avoids an ambient |
| 27 | +/// Homebrew or system-tool dependency in the public release path. |
| 28 | +public enum LZFSE { |
| 29 | + private static let chunk = 1 << 20 |
| 30 | + |
| 31 | + public static func compress(source: String, destination: String) throws { |
| 32 | + try transform(source: source, destination: destination, operation: COMPRESSION_STREAM_ENCODE) |
| 33 | + } |
| 34 | + |
| 35 | + public static func decompress(source: String, destination: String) throws { |
| 36 | + try transform(source: source, destination: destination, operation: COMPRESSION_STREAM_DECODE) |
| 37 | + } |
| 38 | + |
| 39 | + private static func transform( |
| 40 | + source: String, |
| 41 | + destination: String, |
| 42 | + operation: compression_stream_operation |
| 43 | + ) throws { |
| 44 | + guard FileManager.default.isReadableFile(atPath: source), |
| 45 | + let input = InputStream(fileAtPath: source) else { |
| 46 | + throw LZFSEError.openInput(source) |
| 47 | + } |
| 48 | + guard let output = OutputStream(toFileAtPath: destination, append: false) else { |
| 49 | + throw LZFSEError.openOutput(destination) |
| 50 | + } |
| 51 | + input.open() |
| 52 | + output.open() |
| 53 | + defer { |
| 54 | + input.close() |
| 55 | + output.close() |
| 56 | + } |
| 57 | + |
| 58 | + let sourceBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: chunk) |
| 59 | + let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: chunk) |
| 60 | + defer { |
| 61 | + sourceBuffer.deallocate() |
| 62 | + destinationBuffer.deallocate() |
| 63 | + } |
| 64 | + |
| 65 | + var stream = compression_stream( |
| 66 | + dst_ptr: destinationBuffer, |
| 67 | + dst_size: chunk, |
| 68 | + src_ptr: UnsafePointer(sourceBuffer), |
| 69 | + src_size: 0, |
| 70 | + state: nil |
| 71 | + ) |
| 72 | + guard compression_stream_init(&stream, operation, COMPRESSION_LZFSE) |
| 73 | + == COMPRESSION_STATUS_OK else { |
| 74 | + throw LZFSEError.streamInit |
| 75 | + } |
| 76 | + defer { compression_stream_destroy(&stream) } |
| 77 | + |
| 78 | + // compression_stream_init resets the caller-owned source/destination fields. |
| 79 | + stream.src_ptr = UnsafePointer(sourceBuffer) |
| 80 | + stream.src_size = 0 |
| 81 | + stream.dst_ptr = destinationBuffer |
| 82 | + stream.dst_size = chunk |
| 83 | + |
| 84 | + var inputExhausted = false |
| 85 | + while true { |
| 86 | + if stream.src_size == 0, !inputExhausted { |
| 87 | + let read = input.read(sourceBuffer, maxLength: chunk) |
| 88 | + if read < 0 { throw LZFSEError.read } |
| 89 | + if read == 0 { inputExhausted = true } |
| 90 | + stream.src_ptr = UnsafePointer(sourceBuffer) |
| 91 | + stream.src_size = read |
| 92 | + } |
| 93 | + |
| 94 | + let flags = inputExhausted ? Int32(COMPRESSION_STREAM_FINALIZE.rawValue) : 0 |
| 95 | + let status = compression_stream_process(&stream, flags) |
| 96 | + guard status == COMPRESSION_STATUS_OK || status == COMPRESSION_STATUS_END else { |
| 97 | + throw LZFSEError.process |
| 98 | + } |
| 99 | + |
| 100 | + // Compression.framework owns the partially filled destination buffer while it returns |
| 101 | + // OK. Publish and reset that buffer only once it is full, or publish the final partial |
| 102 | + // buffer when the stream ends. Resetting a partially filled buffer between OK calls |
| 103 | + // produces a malformed stream once a payload crosses the input chunk boundary. |
| 104 | + let produced: Int |
| 105 | + switch status { |
| 106 | + case COMPRESSION_STATUS_OK where stream.dst_size == 0: |
| 107 | + produced = chunk |
| 108 | + case COMPRESSION_STATUS_END: |
| 109 | + produced = chunk - stream.dst_size |
| 110 | + default: |
| 111 | + produced = 0 |
| 112 | + } |
| 113 | + var offset = 0 |
| 114 | + while offset < produced { |
| 115 | + let written = output.write( |
| 116 | + destinationBuffer + offset, |
| 117 | + maxLength: produced - offset |
| 118 | + ) |
| 119 | + if written <= 0 { throw LZFSEError.write } |
| 120 | + offset += written |
| 121 | + } |
| 122 | + if status == COMPRESSION_STATUS_OK, stream.dst_size == 0 { |
| 123 | + stream.dst_ptr = destinationBuffer |
| 124 | + stream.dst_size = chunk |
| 125 | + } |
| 126 | + |
| 127 | + if status == COMPRESSION_STATUS_END { return } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments