Skip to content

Commit

Permalink
Merge pull request #90 from SpringMT/fix/streaming_decomporess-compress
Browse files Browse the repository at this point in the history
fix: decompress with streaming compress binary
  • Loading branch information
SpringMT authored May 23, 2024
2 parents 183e9cb + b3ed0f9 commit 01ebc7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 7 additions & 7 deletions ext/zstdruby/zstdruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@ static VALUE rb_compress_using_dict(int argc, VALUE *argv, VALUE self)

static VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t input_size)
{
VALUE output_string = rb_str_new(NULL, 0);
ZSTD_outBuffer output = { NULL, 0, 0 };

ZSTD_inBuffer input = { input_data, input_size, 0 };
VALUE result = rb_str_new(0, 0);

while (input.pos < input.size) {
ZSTD_outBuffer output = { NULL, 0, 0 };
output.size += ZSTD_DStreamOutSize();
rb_str_resize(output_string, output.size);
VALUE output_string = rb_str_new(NULL, output.size);
output.dst = RSTRING_PTR(output_string);

size_t ret = zstd_stream_decompress(dctx, &output, &input, false);
if (ZSTD_isError(ret)) {
ZSTD_freeDCtx(dctx);
rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(ret));
}
rb_str_cat(result, output.dst, output.pos);
}
rb_str_resize(output_string, output.pos);
ZSTD_freeDCtx(dctx);
return output_string;
return result;
}

static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
Expand Down Expand Up @@ -134,7 +134,7 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
VALUE output = rb_str_new(NULL, uncompressed_size);
char* output_data = RSTRING_PTR(output);

size_t const decompress_size = zstd_decompress(dctx, output_data, uncompressed_size, input_data, input_size, false);
size_t const decompress_size = zstd_decompress(dctx, output_data, uncompressed_size, input_data, input_size, false);
if (ZSTD_isError(decompress_size)) {
rb_raise(rb_eRuntimeError, "%s: %s", "decompress error", ZSTD_getErrorName(decompress_size));
}
Expand Down
9 changes: 8 additions & 1 deletion spec/zstd-ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def to_str
expect(decompressed.force_encoding('UTF-8')).to eq('あああ')
end

it 'should work hash equal streaming compress' do
simple_compressed = Zstd.compress('あ')
stream = Zstd::StreamingCompress.new
stream << "あ"
streaming_compressed = stream.finish
expect(Zstd.decompress(simple_compressed).force_encoding('UTF-8').hash).to eq(Zstd.decompress(streaming_compressed).force_encoding('UTF-8').hash)
end

it 'should raise exception with unsupported object' do
expect { Zstd.decompress(Object.new) }.to raise_error(TypeError)
end
Expand All @@ -111,4 +119,3 @@ def to_str
end
end
end

0 comments on commit 01ebc7f

Please sign in to comment.