Class: LZ4::BlockDecoder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject #initialize(preset_dictionary) ⇒ Object

INFECTION

self < preset_dictionary



845
846
847
848
849
850
851
852
853
854
# File 'ext/blockapi.c', line 845

static VALUE
blkdec_init(int argc, VALUE argv[], VALUE dec)
{
    struct blockdecoder *p = getdecoder(dec);

    blkdec_setup(argc, argv, Qnil, p);
    rb_obj_infect(dec, p->predict);

    return dec;
}

Class Method Details

.decode(src, dest = "") ⇒ Object .decode(src, max_dest_size, dest = "") ⇒ Object

Decode block LZ4 data.

出力先は、max_dest_size が与えられていない場合、必要に応じて自動的に拡張されます。 この場合、いったん圧縮された LZ4 データを走査するため、事前に僅かな CPU 時間を必要とします。

INFECTION

dest < src



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'ext/blockapi.c', line 994

static VALUE
blkdec_s_decode(int argc, VALUE argv[], VALUE lz4)
{
    VALUE src, dest;
    size_t maxsize;
    blockprocess_args(argc, argv, &src, &dest, &maxsize, NULL, aux_lz4_scansize);

    aux_str_reserve(dest, maxsize);
    rb_str_set_len(dest, 0);
    rb_obj_infect(dest, src);

    int size = LZ4_decompress_safe(RSTRING_PTR(src), RSTRING_PTR(dest), RSTRING_LEN(src), maxsize);
    if (size < 0) {
        rb_raise(extlz4_eError,
                 "failed LZ4_decompress_safe - max_dest_size is too small, or data is corrupted");
    }

    rb_str_set_len(dest, size);

    return dest;
}

.linksize(lz4_blockencoded_data) ⇒ Object

Scan block lz4 data, and get prefix byte size.



974
975
976
977
978
979
# File 'ext/blockapi.c', line 974

static VALUE
blkdec_s_linksize(VALUE mod, VALUE str)
{
    rb_check_type(str, RUBY_T_STRING);
    return SIZET2NUM(aux_lz4_linksize(str));
}

.scansize(lz4_blockencoded_data) ⇒ Integer

Scan block lz4 data, and get decoded byte size.

このメソッドは、block_decode メソッドに max_dest_size なしで利用する場合の検証目的で利用できるようにしてあります。

その他の有用な使い方があるのかは不明です。

Returns:

  • (Integer)


961
962
963
964
965
966
# File 'ext/blockapi.c', line 961

static VALUE
blkdec_s_scansize(VALUE mod, VALUE str)
{
    rb_check_type(str, RUBY_T_STRING);
    return SIZET2NUM(aux_lz4_scansize(str));
}

Instance Method Details

#releasenil Also known as: free

Release allocated internal heap memory.

Returns:

  • (nil)


937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'ext/blockapi.c', line 937

static VALUE
blkdec_release(VALUE lz4)
{
    struct blockdecoder *p = getdecoderp(lz4);
    if (!p) { return Qnil; }
    if (p->context) {
        LZ4_freeStreamDecode(p->context);
        p->context = NULL;
    }
    // TODO: p->predict と p->prefix も rb_str_resize で 0 にするべきか?
    p->predict = Qnil;
    return Qnil;
}

#resetObject #reset(preset_dictionary) ⇒ Object

INFECTION

self < preset_dictionary



864
865
866
867
868
869
870
871
872
873
# File 'ext/blockapi.c', line 864

static VALUE
blkdec_reset(int argc, VALUE argv[], VALUE dec)
{
    struct blockdecoder *p = getdecoder(dec);

    blkdec_setup(argc, argv, p->predict, p);
    rb_obj_infect(dec, p->predict);

    return dec;
}

#update(src, dest = "") ⇒ Object #update(src, max_dest_size, dest = "") ⇒ Object Also known as: decode, decompress, uncompress

Decode block lz4 data of stream block.

Given arguments and return values are same as LZ4#block_decode. See LZ4#block_decode for about its.

出力先は、max_dest_size が与えられていない場合、必要に応じて自動的に拡張されます。 この場合、いったん圧縮された LZ4 データを走査するため、事前に僅かな CPU 時間を必要とします。

INFECTION

dest < self < src



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'ext/blockapi.c', line 891

static VALUE
blkdec_update(int argc, VALUE argv[], VALUE dec)
{
    struct blockdecoder *p = getdecoder(dec);
    if (!p->context) { rb_raise(extlz4_eError, "need reset (context not initialized)"); }
    VALUE src, dest;
    size_t maxsize;
    blockprocess_args(argc, argv, &src, &dest, &maxsize, NULL, aux_lz4_scansize);
    rb_obj_infect(dec, src);
    rb_obj_infect(dest, dec);
    const char *srcp;
    size_t srcsize;
    RSTRING_GETMEM(src, srcp, srcsize);
    LZ4_setStreamDecode(p->context, p->dictbuf, p->dictsize);
    int s = aux_LZ4_decompress_safe_continue(p->context, srcp, RSTRING_PTR(dest), srcsize, maxsize);
    if (s < 0) {
        rb_raise(extlz4_eError,
                "`max_dest_size' too small, or corrupt lz4'd data");
    }
    rb_str_set_len(dest, s);

    /* copy prefix */
    if (s < sizeof(p->dictbuf)) {
        ssize_t discard = (p->dictsize + s) - sizeof(p->dictbuf);
        if (discard > 0) {
            size_t remain = p->dictsize - discard;
            memmove(p->dictbuf, (const char *)(p->dictbuf + discard), remain);
            p->dictsize = remain;
        }

        memcpy(p->dictbuf + p->dictsize, RSTRING_PTR(dest), s);
        p->dictsize += s;
    } else {
        memcpy(p->dictbuf, RSTRING_END(dest) - sizeof(p->dictbuf), sizeof(p->dictbuf));
        p->dictsize = sizeof(p->dictbuf);
    }

    return dest;
}