Skip to content

Commit 01ebc7f

Browse files
authored
Merge pull request #90 from SpringMT/fix/streaming_decomporess-compress
fix: decompress with streaming compress binary
2 parents 183e9cb + b3ed0f9 commit 01ebc7f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

ext/zstdruby/zstdruby.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,24 @@ static VALUE rb_compress_using_dict(int argc, VALUE *argv, VALUE self)
8787

8888
static VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t input_size)
8989
{
90-
VALUE output_string = rb_str_new(NULL, 0);
91-
ZSTD_outBuffer output = { NULL, 0, 0 };
92-
9390
ZSTD_inBuffer input = { input_data, input_size, 0 };
91+
VALUE result = rb_str_new(0, 0);
92+
9493
while (input.pos < input.size) {
94+
ZSTD_outBuffer output = { NULL, 0, 0 };
9595
output.size += ZSTD_DStreamOutSize();
96-
rb_str_resize(output_string, output.size);
96+
VALUE output_string = rb_str_new(NULL, output.size);
9797
output.dst = RSTRING_PTR(output_string);
9898

9999
size_t ret = zstd_stream_decompress(dctx, &output, &input, false);
100100
if (ZSTD_isError(ret)) {
101101
ZSTD_freeDCtx(dctx);
102102
rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(ret));
103103
}
104+
rb_str_cat(result, output.dst, output.pos);
104105
}
105-
rb_str_resize(output_string, output.pos);
106106
ZSTD_freeDCtx(dctx);
107-
return output_string;
107+
return result;
108108
}
109109

110110
static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
@@ -134,7 +134,7 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
134134
VALUE output = rb_str_new(NULL, uncompressed_size);
135135
char* output_data = RSTRING_PTR(output);
136136

137-
size_t const decompress_size = zstd_decompress(dctx, output_data, uncompressed_size, input_data, input_size, false);
137+
size_t const decompress_size = zstd_decompress(dctx, output_data, uncompressed_size, input_data, input_size, false);
138138
if (ZSTD_isError(decompress_size)) {
139139
rb_raise(rb_eRuntimeError, "%s: %s", "decompress error", ZSTD_getErrorName(decompress_size));
140140
}

spec/zstd-ruby_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ def to_str
8787
expect(decompressed.force_encoding('UTF-8')).to eq('あああ')
8888
end
8989

90+
it 'should work hash equal streaming compress' do
91+
simple_compressed = Zstd.compress('あ')
92+
stream = Zstd::StreamingCompress.new
93+
stream << "あ"
94+
streaming_compressed = stream.finish
95+
expect(Zstd.decompress(simple_compressed).force_encoding('UTF-8').hash).to eq(Zstd.decompress(streaming_compressed).force_encoding('UTF-8').hash)
96+
end
97+
9098
it 'should raise exception with unsupported object' do
9199
expect { Zstd.decompress(Object.new) }.to raise_error(TypeError)
92100
end
@@ -111,4 +119,3 @@ def to_str
111119
end
112120
end
113121
end
114-

0 commit comments

Comments
 (0)