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



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'ext/extzstd_stream.c', line 351

static VALUE
dec_init(int argc, VALUE argv[], VALUE self)
{
    /*
     * 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_createDStream(),
            "failed ZSTD_createDStream()");

    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));
        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



556
557
558
559
560
561
# File 'ext/extzstd_stream.c', line 556

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

#eofObject Also known as: eof?



550
551
552
553
554
# File 'ext/extzstd_stream.c', line 550

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

#posObject



586
587
588
589
590
591
# File 'ext/extzstd_stream.c', line 586

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

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



502
503
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
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'ext/extzstd_stream.c', line 502

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



563
564
565
566
567
568
569
570
571
572
# File 'ext/extzstd_stream.c', line 563

static VALUE
dec_reset(VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);
     */
    size_t s = ZSTD_resetDStream(decoder_context(self)->context);
    extzstd_check_error(s);
    return self;
}

#sizeofObject



574
575
576
577
578
579
580
581
582
583
584
# File 'ext/extzstd_stream.c', line 574

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);
}