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



824
825
826
827
828
829
830
831
832
# File 'ext/blockapi.c', line 824

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

    blkdec_setup(argc, argv, Qnil, p);

    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 時間を必要とします。



960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# File 'ext/blockapi.c', line 960

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

    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.



943
944
945
946
947
948
# File 'ext/blockapi.c', line 943

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)


930
931
932
933
934
935
# File 'ext/blockapi.c', line 930

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)


906
907
908
909
910
911
912
913
914
915
916
917
918
# File 'ext/blockapi.c', line 906

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



839
840
841
842
843
844
845
846
847
# File 'ext/blockapi.c', line 839

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

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

    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 時間を必要とします。



862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'ext/blockapi.c', line 862

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