I've got a Data
of deflate (zlib)-compressed data that decompresses properly using NSData.decompressed()
, but does not decompress properly using compression_decode_buffer()
. The working code looks like this:
let compressedData = Data(bytesNoCopy: buf.baseAddress!, count: readCount, deallocator: .none)
let dataWithoutHeader = compressedData[2...]
let ucData = try (dataWithoutHeader as NSData).decompressed(using: .zlib) as Data
The non-working code looks like this:
let samples = try [Float](unsafeUninitializedCapacity: sampleCount)
{ buffer, initializedCount in
print("Count: \(initializedCount)")
try compressedData.withUnsafeBytes<UInt8>
{ (inCompressedBytes: UnsafeRawBufferPointer) -> Void in
let destBufferSize = buffer.count * MemoryLayout<Float>.size
let scratchBuffer = UnsafeMutableRawBufferPointer.allocate(byteCount: compression_decode_scratch_buffer_size(COMPRESSION_ZLIB), alignment: MemoryLayout<Int>.alignment)
defer { scratchBuffer.deallocate() }
let decompressedSize = compression_decode_buffer(buffer.baseAddress!, destBufferSize,
inCompressedBytes.baseAddress!, inCompressedBytes.count,
scratchBuffer.baseAddress!, COMPRESSION_ZLIB)
print("Actual decompressed size: \(decompressedSize), destBufferSize: \(destBufferSize)")
}
initializedCount = sampleCount
}
It ends up printing:
Actual decompressed size: 46510, destBufferSize: 1048576
(1048576 is the correct size. What data is returned does not appear to be correct.)
I have tried it both with and without the first two bytes of the compressed data buffer, and with and without providing a scratch buffer.