@@ -3006,7 +3006,10 @@ def decompress(self, data):
30063006 # buffer. So if the output buffer is partially filled and the input
30073007 # is exhausted, there's nothing more to write. So we've done all we
30083008 # can.
3009- elif in_buffer .pos == in_buffer .size and out_buffer .pos < out_buffer .size :
3009+ elif (
3010+ in_buffer .pos == in_buffer .size
3011+ and out_buffer .pos < out_buffer .size
3012+ ):
30103013 break
30113014 else :
30123015 out_buffer .pos = 0
@@ -3715,7 +3718,13 @@ def memory_size(self):
37153718 """
37163719 return lib .ZSTD_sizeof_DCtx (self ._dctx )
37173720
3718- def decompress (self , data , max_output_size = 0 ):
3721+ def decompress (
3722+ self ,
3723+ data ,
3724+ max_output_size = 0 ,
3725+ read_across_frames = False ,
3726+ allow_extra_data = True ,
3727+ ):
37193728 """
37203729 Decompress data in a single operation.
37213730
@@ -3727,11 +3736,20 @@ def decompress(self, data, max_output_size=0):
37273736 similar). If the input does not contain a full frame, an exception will
37283737 be raised.
37293738
3730- If the input contains multiple frames, only the first frame will be
3731- decompressed. If you need to decompress multiple frames, use an API
3732- like :py:meth:`ZstdCompressor.stream_reader` with
3739+ ``read_across_frames`` controls whether to read multiple zstandard
3740+ frames in the input. When False, decompression stops after reading the
3741+ first frame. This feature is not yet implemented but the argument is
3742+ provided for forward API compatibility when the default is changed to
3743+ True in a future release. For now, if you need to decompress multiple
3744+ frames, use an API like :py:meth:`ZstdCompressor.stream_reader` with
37333745 ``read_across_frames=True``.
37343746
3747+ ``allow_extra_data`` controls how to handle extra input data after a
3748+ fully decoded frame. If False, any extra data (which could be a valid
3749+ zstd frame) will result in ``ZstdError`` being raised. If True, extra
3750+ data is silently ignored. The default will likely change to False in a
3751+ future release when ``read_across_frames`` defaults to True.
3752+
37353753 If the input contains extra data after a full frame, that extra input
37363754 data is silently ignored. This behavior is undesirable in many scenarios
37373755 and will likely be changed or controllable in a future release (see
@@ -3783,6 +3801,11 @@ def decompress(self, data, max_output_size=0):
37833801 ``bytes`` representing decompressed output.
37843802 """
37853803
3804+ if read_across_frames :
3805+ raise ZstdError (
3806+ "ZstdDecompressor.read_across_frames=True is not yet implemented"
3807+ )
3808+
37863809 self ._ensure_dctx ()
37873810
37883811 data_buffer = ffi .from_buffer (data )
@@ -3830,6 +3853,13 @@ def decompress(self, data, max_output_size=0):
38303853 "decompression error: decompressed %d bytes; expected %d"
38313854 % (zresult , output_size )
38323855 )
3856+ elif not allow_extra_data and in_buffer .pos < in_buffer .size :
3857+ count = in_buffer .size - in_buffer .pos
3858+
3859+ raise ZstdError (
3860+ "compressed input contains %d bytes of unused data, which is disallowed"
3861+ % count
3862+ )
38333863
38343864 return ffi .buffer (result_buffer , out_buffer .pos )[:]
38353865
0 commit comments