Class: Zstd::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/extzstd.rb,
ext/extzstd_stream.c

Constant Summary collapse

INSIZE =
SIZET2NUM(ZSTD_DStreamInSize())
OUTSIZE =
SIZET2NUM(ZSTD_DStreamOutSize())

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inport, predict = Qnil) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'ext/extzstd_stream.c', line 385

static VALUE
dec_init(int argc, VALUE argv[], VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
     *
     * ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
     * ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
     */

    VALUE inport, predict;

    switch (argc) {
    case 1:
        inport = argv[0];
        predict = Qnil;
        break;
    case 2:
        inport = argv[0];
        predict = argv[1];
        break;
    default:
        rb_error_arity(argc, 1, 2);
    }

    struct decoder *p = getdecoder(self);
    if (p->context) {
        rb_raise(rb_eTypeError,
                "initialized context already - #<%s:%p>",
                rb_obj_classname(self), (void *)self);
    }

    AUX_TRY_WITH_GC(
            p->context = ZSTD_createDCtx(),
            "failed ZSTD_createDCtx()");

    //ZSTD_DCtx_reset
    //ZSTD_DCtx_loadDictionary
    if (NIL_P(predict)) {
        //size_t s = ZSTD_initDStream(p->context);
        //extzstd_check_error(s);
    } else {
        rb_check_type(predict, RUBY_T_STRING);
        predict = rb_str_new_frozen(predict);
        //size_t s = ZSTD_initDStream_usingDict(p->context, RSTRING_PTR(predict), RSTRING_LEN(predict));
        size_t s = ZSTD_DCtx_loadDictionary(p->context, RSTRING_PTR(predict), RSTRING_LEN(predict));
        extzstd_check_error(s);
    }

    p->inport = inport;
    p->predict = predict;

    return self;
}

Class Method Details

.decode(src, dest: nil, dict: nil) ⇒ Object Also known as: decompress, uncompress



138
139
140
141
142
143
# File 'lib/extzstd.rb', line 138

def self.decode(src, dest: nil, dict: nil)
  # NOTE: ContextLess.decode は伸長時のサイズが必要なため、常に利用できるわけではない
  # ContextLess.decode(src, dest || "".b, nil, dict)

  new(StringIO.new(src), dict).read(nil, dest)
end

.open(inport, dict = nil) ⇒ Object

call-seq:

open(inport, dict = nil) -> decoder
open(inport, dict = nil) { |decoder| ... } -> yield returned value
inport

String instance or read method haved Object.



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/extzstd.rb', line 124

def self.open(inport, dict = nil)
  inport = StringIO.new(inport) if inport.kind_of?(String)

  dec = new(inport, dict)

  return dec unless block_given?

  begin
    yield(dec)
  ensure
    dec.close rescue nil
  end
end

Instance Method Details

#closeObject



596
597
598
599
600
601
# File 'ext/extzstd_stream.c', line 596

static VALUE
dec_close(VALUE self)
{
    decoder_context(self)->reached_eof = 1;
    return Qnil;
}

#eofObject Also known as: eof?



590
591
592
593
594
# File 'ext/extzstd_stream.c', line 590

static VALUE
dec_eof(VALUE self)
{
    return (decoder_context(self)->reached_eof == 0 ? Qfalse : Qtrue);
}

#posObject



625
626
627
628
629
630
# File 'ext/extzstd_stream.c', line 625

static VALUE
dec_pos(VALUE self)
{
    decoder_context(self); /* check only */
    return INT2FIX(0);
}

#readObject #read(readsize, buf = "".b) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'ext/extzstd_stream.c', line 542

static VALUE
dec_read(int argc, VALUE argv[], VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
     */

    ssize_t size;
    VALUE buf;
    dec_read_args(argc, argv, self, &buf, &size);

    struct decoder *p = decoder_context(self);

    if (size == 0) {
        rb_str_set_len(buf, 0);
        return buf;
    } else if (size > 0) {
        size = dec_read_decode(self, p, RSTRING_PTR(buf), size);
        rb_str_set_len(buf, size);
    } else {
        /* if (size < 0) */

        size_t capa = EXT_READ_GROWUP_SIZE;

        for (;;) {
            aux_str_modify_expand(buf, capa);
            size = dec_read_decode(self, p, RSTRING_PTR(buf) + RSTRING_LEN(buf), capa - RSTRING_LEN(buf));
            rb_str_set_len(buf, RSTRING_LEN(buf) + size);
            if (size == 0) { break; }
            size = rb_str_capacity(buf);
            if (size > RSTRING_LEN(buf)) { break; }
            if (size > EXT_READ_DOUBLE_GROWUP_LIMIT_SIZE) {
                capa += EXT_READ_DOUBLE_GROWUP_LIMIT_SIZE;
            } else {
                capa *= 2;
            }
        }
    }

    rb_obj_infect(buf, self);

    if (RSTRING_LEN(buf) == 0) {
        return Qnil;
    } else {
        return buf;
    }
}

#resetObject



603
604
605
606
607
608
609
610
611
# File 'ext/extzstd_stream.c', line 603

static VALUE
dec_reset(VALUE self)
{
    //> ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);

    size_t s = ZSTD_DCtx_reset(decoder_context(self)->context, ZSTD_reset_session_only);
    extzstd_check_error(s);
    return self;
}

#sizeofObject



613
614
615
616
617
618
619
620
621
622
623
# File 'ext/extzstd_stream.c', line 613

static VALUE
dec_sizeof(VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
     */

    size_t s = ZSTD_sizeof_DStream(decoder_context(self)->context);
    extzstd_check_error(s);
    return SIZET2NUM(s);
}