Class: Zstd::StreamEncoder

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

Constant Summary collapse

INSIZE =
SIZET2NUM(ZSTD_CStreamInSize())
OUTSIZE =
SIZET2NUM(ZSTD_CStreamOutSize())

Instance Method Summary collapse

Constructor Details

#initialize(compression_parameters, predict) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'ext/extzstd_stream.c', line 45

static VALUE
enc_init(VALUE self, VALUE params, VALUE predict)
{
    /*
     * ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
     * ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
     * ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
     *                                              ZSTD_parameters params, unsigned long long pledgedSrcSize);
     */

    ZSTD_CStream *p = getencoder(self);
    const void *predictp;
    size_t predictsize;
    if (NIL_P(predict)) {
        predictp = NULL;
        predictsize = 0;
    } else {
        rb_check_type(predict, RUBY_T_STRING);
        RSTRING_GETMEM(predict, predictp, predictsize);
    }

    if (extzstd_params_p(params)) {
        ZSTD_parameters *paramsp = extzstd_getparams(params);
        size_t s = ZSTD_initCStream_advanced(p, predictp, predictsize, *paramsp, 0);
        extzstd_check_error(s);
    } else {
        size_t s = ZSTD_initCStream_usingDict(p, predictp, predictsize, aux_num2int(params, 1));
        extzstd_check_error(s);
    }
    return self;
}

Instance Method Details

#end(dest, maxdest) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'ext/extzstd_stream.c', line 142

static VALUE
enc_end(VALUE self, VALUE dest, VALUE maxdest)
{
    /*
     * ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
     */

    ZSTD_CStream *p = getencoder(self);
    rb_check_type(dest, RUBY_T_STRING);
    rb_str_modify(dest);
    rb_str_set_len(dest, 0);
    rb_str_modify_expand(dest, NUM2SIZET(maxdest));

    ZSTD_outBuffer output = { RSTRING_PTR(dest), rb_str_capacity(dest), 0 };
    size_t s = ZSTD_endStream(p, &output);
    extzstd_check_error(s);
    if (output.size > 0) {
        rb_str_set_len(dest, output.pos);
        return dest;
    } else {
        return Qnil;
    }
}

#flush(dest, maxdest) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/extzstd_stream.c', line 117

static VALUE
enc_flush(VALUE self, VALUE dest, VALUE maxdest)
{
    /*
     * ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
     */

    ZSTD_CStream *p = getencoder(self);

    rb_check_type(dest, RUBY_T_STRING);
    rb_str_modify(dest);
    rb_str_set_len(dest, 0);
    rb_str_modify_expand(dest, NUM2SIZET(maxdest));

    ZSTD_outBuffer output = { RSTRING_PTR(dest), rb_str_capacity(dest), 0 };
    size_t s = ZSTD_flushStream(p, &output);
    extzstd_check_error(s);
    if (output.size > 0) {
        rb_str_set_len(dest, output.pos);
        return dest;
    } else {
        return Qnil;
    }
}

#reset(pledged_srcsize) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'ext/extzstd_stream.c', line 166

static VALUE
enc_reset(VALUE self, VALUE pledged_srcsize)
{
    /*
     * ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
     */

    size_t s = ZSTD_resetCStream(getencoder(self), NUM2ULL(pledged_srcsize));
    extzstd_check_error(s);
    return self;
}

#sizeofObject



178
179
180
181
182
183
184
185
186
187
188
# File 'ext/extzstd_stream.c', line 178

static VALUE
enc_sizeof(VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
     */

    size_t s = ZSTD_sizeof_CStream(getencoder(self));
    extzstd_check_error(s);
    return SIZET2NUM(s);
}

#update(src, srcoff, dest, maxdest) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/extzstd_stream.c', line 77

static VALUE
enc_update(VALUE self, VALUE src, VALUE srcoff, VALUE dest, VALUE maxdest)
{
    /*
     * ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
     */

    ZSTD_CStream *p = getencoder(self);

    rb_check_type(src, RUBY_T_STRING);
    const char *q = RSTRING_PTR(src);
    const char *qq = RSTRING_END(src);
    const char *q1 = q + NUM2SIZET(srcoff);
    if (q1 < q || q1 > qq) {
        rb_raise(rb_eArgError,
                "``srcoff'' is out of range (given %"PRIuSIZE", expect 0..%"PRIuSIZE")",
                q1 - q, qq - q);
    }

    rb_check_type(dest, RUBY_T_STRING);
    rb_str_modify(dest);
    rb_str_set_len(dest, 0);
    rb_str_modify_expand(dest, NUM2SIZET(maxdest));
    rb_obj_infect(self, src);
    rb_obj_infect(dest, self);
    char *r = RSTRING_PTR(dest);
    const char *rr = r + rb_str_capacity(dest);

    ZSTD_inBuffer input = { q, qq - q, q1 - q };
    ZSTD_outBuffer output = { r, rr - r, 0 };
    size_t s = ZSTD_compressStream(p, &output, &input);
    extzstd_check_error(s);
    rb_str_set_len(dest, output.pos);
    if (input.pos == input.size) {
        return Qnil;
    } else {
        return SIZET2NUM(input.pos);
    }
}