Reputation: 27
I am working with zlib data from 3 days and I cannot get out of this problem:
The original zlib compressed data hex is as follows: 789c34c9410e82301005d0bbfc756b5a832ee62a94900146255620ed80314defae1b772f79050a5af6180df21feb06f2ce6002153c84930ec2dacf8b4a3a38821a7fbefcbed7c4a3805ab401775679f3c76e69b27bb6c259bd1d6cf3bc5d034c0978cd635a7300b993ab1dba5abf000000ffff
And the hex that I generate using the zlib python library is the following: 789c35c9410e82301005d0bbfc756b5a832ee62a94900146255620ed80314defae1b772f79050a5af6180df21feb06f2ce6002153c84930ec2dacf8b4a3a38821a7fbefcbed7c4a3805ab401775679f3c76e69b27bb6c259bd1d6cf3bc5d034c0978cd635a7300b993ab1dba5abfb1bd28150000ffff
Can anyone explain to me the difference between the two values?
import zlib, json
ZLIB_SUFFIX = b'\x00\x00\xff\xff'
data = json.dumps({
"t": None,
"s": None,
"op": 10,
"d": {
"heartbeat_interval": 41250,
"_trace": [
'["gateway-prd-us-east1-b-4kf6",{"micros":0.0}]'
]
}
}, separators=(',', ':')).encode('utf-8')
deflate = zlib.compressobj(6, zlib.DEFLATED, zlib.MAX_WBITS)
result = deflate.compress(data) + deflate.flush() + ZLIB_SUFFIX
print(result)
Upvotes: 1
Views: 82
Reputation: 112502
The original stream is not terminated, and hence invalid, and ends with an empty stored block. The one you generated is terminated and valid, but is followed by an extraneous 00 00 ff ff
. Both decompress to the same data, though the original is not validated with a check value. The one you generated is.
Your ZLIB_SUFFIX
is not any such thing. What it is is a zero length and the complement of that length that would follow stored block header bits in a deflate stream. However it has no such meaning if it does not follow stored block header bits in a deflate stream, which is the case in the one you generated.
Upvotes: 4