Class: ZstdNative::DStream

Inherits:
Object
  • Object
show all
Defined in:
ext/zstd_native/zstd_native.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'ext/zstd_native/zstd_native.c', line 484

static VALUE dstream_initialize(VALUE self) {
    ZSTD_DStream *stream = ZSTD_createDStream();
    if (!stream) {
        rb_raise(eZstdError, "Failed to create decompression stream");
    }

    size_t result = ZSTD_initDStream(stream);
    if (ZSTD_isError(result)) {
        ZSTD_freeDStream(stream);
        rb_raise(eZstdError, "Failed to initialize decompression stream: %s", ZSTD_getErrorName(result));
    }

    DATA_PTR(self) = stream;
    return self;
}

Class Method Details

.in_sizeObject



537
538
539
# File 'ext/zstd_native/zstd_native.c', line 537

static VALUE dstream_in_size(VALUE self) {
    return SIZET2NUM(ZSTD_DStreamInSize());
}

.out_sizeObject



541
542
543
# File 'ext/zstd_native/zstd_native.c', line 541

static VALUE dstream_out_size(VALUE self) {
    return SIZET2NUM(ZSTD_DStreamOutSize());
}

Instance Method Details

#decompress(data) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'ext/zstd_native/zstd_native.c', line 504

static VALUE dstream_decompress(VALUE self, VALUE data) {
    Check_Type(data, T_STRING);

    ZSTD_DStream *stream;
    TypedData_Get_Struct(self, ZSTD_DStream, &dstream_type, stream);

    const char *src = RSTRING_PTR(data);
    size_t src_size = RSTRING_LEN(data);

    ZSTD_inBuffer input = {src, src_size, 0};

    size_t out_capacity = ZSTD_DStreamOutSize();
    VALUE result = rb_str_buf_new(out_capacity);
    char *dst = RSTRING_PTR(result);
    size_t total_written = 0;

    while (input.pos < input.size) {
        ZSTD_outBuffer output = {dst + total_written, out_capacity - total_written, 0};
        size_t remaining = ZSTD_decompressStream(stream, &output, &input);
        check_zstd_error(remaining, "Stream decompression failed");
        total_written += output.pos;

        if (total_written >= out_capacity && input.pos < input.size) {
            out_capacity *= 2;
            rb_str_resize(result, out_capacity);
            dst = RSTRING_PTR(result);
        }
    }

    rb_str_set_len(result, total_written);
    return result;
}