@@ -39,7 +39,7 @@ static VALUE rb_compress(int argc, VALUE *argv, VALUE self)
3939 return output ;
4040}
4141
42- VALUE decompress_buffered (ZSTD_DCtx * dctx , const char * input_data , size_t input_size )
42+ VALUE decompress_buffered (ZSTD_DCtx * dctx , const char * input_data , size_t input_size , bool free_ctx )
4343{
4444 ZSTD_inBuffer input = { input_data , input_size , 0 };
4545 VALUE result = rb_str_new (0 , 0 );
@@ -52,12 +52,12 @@ VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t input_
5252
5353 size_t ret = zstd_stream_decompress (dctx , & output , & input , false);
5454 if (ZSTD_isError (ret )) {
55- ZSTD_freeDCtx (dctx );
55+ if ( free_ctx ) ZSTD_freeDCtx (dctx );
5656 rb_raise (rb_eRuntimeError , "%s: %s" , "ZSTD_decompressStream failed" , ZSTD_getErrorName (ret ));
5757 }
5858 rb_str_cat (result , output .dst , output .pos );
5959 }
60- ZSTD_freeDCtx (dctx );
60+ if ( free_ctx ) ZSTD_freeDCtx (dctx );
6161 return result ;
6262}
6363
@@ -81,9 +81,9 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
8181 }
8282 // ZSTD_decompressStream may be called multiple times when ZSTD_CONTENTSIZE_UNKNOWN, causing slowness.
8383 // Therefore, we will not standardize on ZSTD_decompressStream
84- if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN ) {
85- return decompress_buffered (dctx , input_data , input_size );
86- }
84+ if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN ) {
85+ return decompress_buffered (dctx , input_data , input_size , true );
86+ }
8787
8888 VALUE output = rb_str_new (NULL , uncompressed_size );
8989 char * output_data = RSTRING_PTR (output );
0 commit comments