|
| 1 | +// |
| 2 | +// HybridGzipCompressor.swift |
| 3 | +// FastIO |
| 4 | +// |
| 5 | +// Created by Mike Grabowski on 11/11/2024. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import NitroModules |
| 10 | +import Compression |
| 11 | + |
| 12 | +class HybridCompressor : HybridCompressorSpec { |
| 13 | + private var stream: compression_stream |
| 14 | + private var status: compression_status |
| 15 | + |
| 16 | + private var format: CompressionAlgorithm |
| 17 | + private var totalSize: UInt32 = 0 |
| 18 | + |
| 19 | + init(algorithm: CompressionAlgorithm) { |
| 20 | + stream = compression_stream() |
| 21 | + status = compression_stream_init(&stream, COMPRESSION_STREAM_ENCODE, COMPRESSION_ZLIB) |
| 22 | + format = algorithm |
| 23 | + } |
| 24 | + |
| 25 | + deinit { |
| 26 | + compression_stream_destroy(&stream) |
| 27 | + } |
| 28 | + |
| 29 | + private func compressBuffer(source: UnsafePointer<UInt8>, sourceSize: Int, finalize: Bool = false) throws -> ArrayBufferHolder { |
| 30 | + let headerSize: Int = if totalSize == 0 { |
| 31 | + switch format { |
| 32 | + case .gzip: 10 |
| 33 | + case .deflate: 2 |
| 34 | + case .deflateRaw: 0 |
| 35 | + } |
| 36 | + } else { |
| 37 | + 0 |
| 38 | + } |
| 39 | + let footerSize = (format == .gzip && finalize) ? 8 : 0 |
| 40 | + |
| 41 | + let destBufferSize = 64 * 1024 + headerSize + footerSize |
| 42 | + let destBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destBufferSize) |
| 43 | + |
| 44 | + if headerSize > 0 { |
| 45 | + switch format { |
| 46 | + case .gzip: |
| 47 | + let header = getGzipHeader() |
| 48 | + destBuffer.update(from: header, count: header.count) |
| 49 | + case .deflate: |
| 50 | + let header = getDeflateHeader() |
| 51 | + destBuffer.update(from: header, count: header.count) |
| 52 | + default: |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if format == .gzip && sourceSize > 0 { |
| 58 | + updateCRC32(data: source, size: sourceSize) |
| 59 | + } |
| 60 | + |
| 61 | + totalSize = (totalSize &+ UInt32(sourceSize)) & 0xffffffff |
| 62 | + |
| 63 | + stream.src_ptr = source |
| 64 | + stream.src_size = sourceSize |
| 65 | + stream.dst_ptr = destBuffer.advanced(by: headerSize) |
| 66 | + stream.dst_size = 64 * 1024 |
| 67 | + |
| 68 | + status = compression_stream_process(&stream, Int32(finalize ? COMPRESSION_STREAM_FINALIZE.rawValue : 0)) |
| 69 | + |
| 70 | + guard status != COMPRESSION_STATUS_ERROR else { |
| 71 | + destBuffer.deallocate() |
| 72 | + throw RuntimeError.error(withMessage: "Compression error") |
| 73 | + } |
| 74 | + |
| 75 | + guard stream.src_size == 0 else { |
| 76 | + destBuffer.deallocate() |
| 77 | + throw RuntimeError.error(withMessage: "Unexpected remaining input data.") |
| 78 | + } |
| 79 | + |
| 80 | + let currentOffset = headerSize + (64 * 1024 - stream.dst_size) |
| 81 | + |
| 82 | + if footerSize > 0 { |
| 83 | + let footer = getGzipFooter() |
| 84 | + destBuffer.advanced(by: currentOffset).update(from: footer, count: footer.count) |
| 85 | + } |
| 86 | + |
| 87 | + let deleteFunc = SwiftClosure { |
| 88 | + destBuffer.deallocate() |
| 89 | + } |
| 90 | + |
| 91 | + return ArrayBufferHolder.makeBuffer(destBuffer, currentOffset + footerSize, deleteFunc) |
| 92 | + } |
| 93 | + |
| 94 | + func compress(chunk: ArrayBufferHolder) throws -> ArrayBufferHolder { |
| 95 | + return try compressBuffer( |
| 96 | + source: UnsafePointer(chunk.data.assumingMemoryBound(to: UInt8.self)), |
| 97 | + sourceSize: chunk.size |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + func finalize() throws -> ArrayBufferHolder { |
| 102 | + let emptyBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1) |
| 103 | + defer { |
| 104 | + emptyBuffer.deallocate() |
| 105 | + } |
| 106 | + |
| 107 | + return try compressBuffer( |
| 108 | + source: UnsafePointer(emptyBuffer), |
| 109 | + sourceSize: 0, |
| 110 | + finalize: true |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + /* Gzip */ |
| 115 | + private var crc32: UInt32 = 0 |
| 116 | + |
| 117 | + private func getGzipHeader() -> [UInt8] { |
| 118 | + // GZIP header format: |
| 119 | + // 1F 8B - Magic number |
| 120 | + // 08 - Deflate compression method |
| 121 | + // 00 - Flags |
| 122 | + // 00 00 00 00 - Timestamp |
| 123 | + // 00 - Extra flags |
| 124 | + // FF - OS (unknown) |
| 125 | + return [0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff] |
| 126 | + } |
| 127 | + |
| 128 | + private func getGzipFooter() -> [UInt8] { |
| 129 | + var footer = [UInt8](repeating: 0, count: 8) |
| 130 | + // CRC32 (4 bytes) |
| 131 | + footer[0] = UInt8(crc32 & 0xff) |
| 132 | + footer[1] = UInt8((crc32 >> 8) & 0xff) |
| 133 | + footer[2] = UInt8((crc32 >> 16) & 0xff) |
| 134 | + footer[3] = UInt8((crc32 >> 24) & 0xff) |
| 135 | + // Input size modulo 2^32 (4 bytes) |
| 136 | + footer[4] = UInt8(totalSize & 0xff) |
| 137 | + footer[5] = UInt8((totalSize >> 8) & 0xff) |
| 138 | + footer[6] = UInt8((totalSize >> 16) & 0xff) |
| 139 | + footer[7] = UInt8((totalSize >> 24) & 0xff) |
| 140 | + return footer |
| 141 | + } |
| 142 | + |
| 143 | + private func updateCRC32(data: UnsafePointer<UInt8>, size: Int) { |
| 144 | + var crc = ~crc32 |
| 145 | + for i in 0..<size { |
| 146 | + let byte = data[i] |
| 147 | + crc = (crc >> 8) ^ crcTable[Int((crc & 0xFF) ^ UInt32(byte))] |
| 148 | + } |
| 149 | + crc32 = ~crc |
| 150 | + } |
| 151 | + |
| 152 | + /* Deflate */ |
| 153 | + private func getDeflateHeader() -> [UInt8] { |
| 154 | + // Deflate header (CMF, FLG) |
| 155 | + return [0x78, 0x9c] |
| 156 | + } |
| 157 | + |
| 158 | + var hybridContext = margelo.nitro.HybridContext() |
| 159 | + var memorySize: Int { |
| 160 | + return getSizeOf(self) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// CRC32 lookup table |
| 165 | +private let crcTable: [UInt32] = { |
| 166 | + var table = [UInt32](repeating: 0, count: 256) |
| 167 | + for i in 0..<256 { |
| 168 | + var crc = UInt32(i) |
| 169 | + for _ in 0..<8 { |
| 170 | + crc = (crc >> 1) ^ ((crc & 1) == 1 ? 0xEDB88320 : 0) |
| 171 | + } |
| 172 | + table[i] = crc |
| 173 | + } |
| 174 | + return table |
| 175 | +}() |
0 commit comments