Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decompress with streaming compress binary #90

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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