Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS compression_encode_buffer doesn't include zlib header?

Tags:

ios

swift

zlib

I'm using compression_encode_buffer with COMPRESSION_ZLIB to zlib-compress data in an iOS app, but the result is missing the zlib header.

For example:

size = compression_encode_buffer(
    destinationBuffer, destinationBufferSize,
    sourceBuffer, sourceBufferSize, nil,
    COMPRESSION_ZLIB
)

Will return the bytes:

AD 53 C1 8E 9B 30 …

While using, for example, Python's data.encode("zlib") on the same data will return:

78 9C AD 53 C1 8E 9B 30 …
^^ ^^ correct zlib header

What's up with that? Why isn't the header being included? And is there a "correct" way to add it?

like image 645
David Wolever Avatar asked Feb 08 '16 21:02

David Wolever


1 Answers

The two bytes are a zlib header, not a magic number. Most likely you are missing the final 4 bytes of the stream (the ADLER32 checksum) as well, and only have the "deflate" datastream that you expected to be wrapped in a zlib datatstream. The iOS documentation says as much:

ZLIB
The encoded format is the raw DEFLATE format as described
in IETF RFC 1951 Using the ZLIB library

They should have called the compression method "DEFLATE" not "ZLIB".

See this related question about dealing with ZLIB vs DEFLATE data.

like image 173
Glenn Randers-Pehrson Avatar answered Dec 08 '22 11:12

Glenn Randers-Pehrson