@@ -39,7 +39,7 @@ static VALUE rb_compress(int argc, VALUE *argv, VALUE self)
39
39
return output ;
40
40
}
41
41
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 )
43
43
{
44
44
ZSTD_inBuffer input = { input_data , input_size , 0 };
45
45
VALUE result = rb_str_new (0 , 0 );
@@ -52,12 +52,12 @@ VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t input_
52
52
53
53
size_t ret = zstd_stream_decompress (dctx , & output , & input , false);
54
54
if (ZSTD_isError (ret )) {
55
- ZSTD_freeDCtx (dctx );
55
+ if ( free_ctx ) ZSTD_freeDCtx (dctx );
56
56
rb_raise (rb_eRuntimeError , "%s: %s" , "ZSTD_decompressStream failed" , ZSTD_getErrorName (ret ));
57
57
}
58
58
rb_str_cat (result , output .dst , output .pos );
59
59
}
60
- ZSTD_freeDCtx (dctx );
60
+ if ( free_ctx ) ZSTD_freeDCtx (dctx );
61
61
return result ;
62
62
}
63
63
@@ -81,9 +81,9 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
81
81
}
82
82
// ZSTD_decompressStream may be called multiple times when ZSTD_CONTENTSIZE_UNKNOWN, causing slowness.
83
83
// 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
+ }
87
87
88
88
VALUE output = rb_str_new (NULL , uncompressed_size );
89
89
char * output_data = RSTRING_PTR (output );
0 commit comments