Can't get compression_decode_buffer() to work

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.

Answered by JetForMe in 798598022

I did eventually get this to work. I needed to skip the two-byte header after all. Not sure why it didn't work the previous times I tried doing that.

Folks seem be helping you over on your Swift Forums, so I’ll reply over there too.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I did eventually get this to work. I needed to skip the two-byte header after all. Not sure why it didn't work the previous times I tried doing that.

Can't get compression_decode_buffer() to work
 
 
Q