ext/zstdruby/streaming_decompress.c in zstd-ruby-1.5.6.1 vs ext/zstdruby/streaming_decompress.c in zstd-ruby-1.5.6.2

- old
+ new

@@ -1,9 +1,9 @@ -#include <common.h> +#include "common.h" struct streaming_decompress_t { - ZSTD_DCtx* ctx; + ZSTD_DCtx* dctx; VALUE buf; size_t buf_size; }; static void @@ -19,13 +19,13 @@ static void streaming_decompress_free(void *p) { struct streaming_decompress_t *sd = p; - ZSTD_DCtx* ctx = sd->ctx; - if (ctx != NULL) { - ZSTD_freeDCtx(ctx); + ZSTD_DCtx* dctx = sd->dctx; + if (dctx != NULL) { + ZSTD_freeDCtx(dctx); } xfree(sd); } static size_t @@ -59,28 +59,33 @@ static VALUE rb_streaming_decompress_allocate(VALUE klass) { struct streaming_decompress_t* sd; VALUE obj = TypedData_Make_Struct(klass, struct streaming_decompress_t, &streaming_decompress_type, sd); - sd->ctx = NULL; + sd->dctx = NULL; sd->buf = Qnil; sd->buf_size = 0; return obj; } static VALUE -rb_streaming_decompress_initialize(VALUE obj) +rb_streaming_decompress_initialize(int argc, VALUE *argv, VALUE obj) { + VALUE kwargs; + rb_scan_args(argc, argv, "00:", &kwargs); + struct streaming_decompress_t* sd; TypedData_Get_Struct(obj, struct streaming_decompress_t, &streaming_decompress_type, sd); size_t const buffOutSize = ZSTD_DStreamOutSize(); - ZSTD_DCtx* ctx = ZSTD_createDCtx(); - if (ctx == NULL) { + ZSTD_DCtx* dctx = ZSTD_createDCtx(); + if (dctx == NULL) { rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDCtx error"); } - sd->ctx = ctx; + set_decompress_params(dctx, kwargs); + + sd->dctx = dctx; sd->buf = rb_str_new(NULL, buffOutSize); sd->buf_size = buffOutSize; return obj; } @@ -97,47 +102,23 @@ TypedData_Get_Struct(obj, struct streaming_decompress_t, &streaming_decompress_type, sd); const char* output_data = RSTRING_PTR(sd->buf); VALUE result = rb_str_new(0, 0); while (input.pos < input.size) { ZSTD_outBuffer output = { (void*)output_data, sd->buf_size, 0 }; - size_t const ret = ZSTD_decompressStream(sd->ctx, &output, &input); + size_t const ret = ZSTD_decompressStream(sd->dctx, &output, &input); if (ZSTD_isError(ret)) { - rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret)); + rb_raise(rb_eRuntimeError, "decompress error error code: %s", ZSTD_getErrorName(ret)); } rb_str_cat(result, output.dst, output.pos); } return result; } -static VALUE -rb_streaming_decompress_addstr(VALUE obj, VALUE src) -{ - StringValue(src); - const char* input_data = RSTRING_PTR(src); - size_t input_size = RSTRING_LEN(src); - ZSTD_inBuffer input = { input_data, input_size, 0 }; - - struct streaming_decompress_t* sd; - TypedData_Get_Struct(obj, struct streaming_decompress_t, &streaming_decompress_type, sd); - const char* output_data = RSTRING_PTR(sd->buf); - - while (input.pos < input.size) { - ZSTD_outBuffer output = { (void*)output_data, sd->buf_size, 0 }; - size_t const result = ZSTD_decompressStream(sd->ctx, &output, &input); - if (ZSTD_isError(result)) { - rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(result)); - } - } - return obj; -} - extern VALUE rb_mZstd, cStreamingDecompress; void zstd_ruby_streaming_decompress_init(void) { VALUE cStreamingDecompress = rb_define_class_under(rb_mZstd, "StreamingDecompress", rb_cObject); rb_define_alloc_func(cStreamingDecompress, rb_streaming_decompress_allocate); - rb_define_method(cStreamingDecompress, "initialize", rb_streaming_decompress_initialize, 0); + rb_define_method(cStreamingDecompress, "initialize", rb_streaming_decompress_initialize, -1); rb_define_method(cStreamingDecompress, "decompress", rb_streaming_decompress_decompress, 1); - rb_define_method(cStreamingDecompress, "<<", rb_streaming_decompress_addstr, 1); } -