22import ctypes
33from collections import OrderedDict
44import zlib
5+ import zstd
56import struct
67import io
78import logging
@@ -144,7 +145,11 @@ def _decode_type_inner(self, kw, type_id, array):
144145
145146def decode (reader , type_converters ):
146147 MIN_SUPPORTED_VERSIONS = 15
147- MAX_SUPPORTED_VERSION = 25
148+ MAX_SUPPORTED_VERSION = 70
149+
150+ # `zstd` data will start with a magic number frame
151+ # the value of which is `0xfd2fb528`
152+ ZSTD_MAGIC_NUMBER = b"\x28 \xb5 \x2f \xfd "
148153
149154 header = reader .read (12 )
150155 fig_version = struct .unpack ("<I" , header [8 :12 ])[0 ]
@@ -159,10 +164,29 @@ def decode(reader, type_converters):
159164
160165 segment_header = reader .read (4 )
161166 size = struct .unpack ("<I" , segment_header )[0 ]
162- data = io .BytesIO (zlib .decompress (reader .read (size ), wbits = - 15 ))
163- schema = KiwiSchema (data )
167+ compressedSchema = reader .read (size )
168+
169+ schemaData = b""
170+ # Check to see if the first four bytes are the zstd magic number.
171+ # If so we need to use zstd to decompress, not zlib
172+ if compressedSchema .startswith (ZSTD_MAGIC_NUMBER ):
173+ schemaData = io .BytesIO (zstd .decompress (compressedSchema ))
174+ else :
175+ schemaData = io .BytesIO (zlib .decompress (compressedSchema , wbits = - 15 ))
176+
177+ schema = KiwiSchema (schemaData )
164178
165179 segment_header = reader .read (4 )
166180 size = struct .unpack ("<I" , segment_header )[0 ]
167- data = io .BytesIO (zlib .decompress (reader .read (size ), wbits = - 15 ))
181+ compressedData = reader .read (size )
182+
183+ data = b""
184+ # Check to see if the first four bytes are the zstd magic number.
185+ # If so we need to use zstd to decompress, not zlib
186+
187+ if compressedData .startswith (ZSTD_MAGIC_NUMBER ):
188+ data = io .BytesIO (zstd .decompress (compressedData ))
189+ else :
190+ data = io .BytesIO (zlib .decompress (compressedData , wbits = - 15 ))
191+
168192 return KiwiDecoder (schema , type_converters ).decode (data , "Message" )
0 commit comments