Class: ZstdNative::DCtx

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

Instance Method Summary collapse

Instance Method Details

#decompress(data) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/zstd_native/zstd_native.c', line 246

static VALUE dctx_decompress(VALUE self, VALUE data) {
    Check_Type(data, T_STRING);

    ZSTD_DCtx *ctx;
    TypedData_Get_Struct(self, ZSTD_DCtx, &dctx_type, ctx);

    const char *src = RSTRING_PTR(data);
    size_t src_size = RSTRING_LEN(data);

    unsigned long long const dst_capacity = ZSTD_getFrameContentSize(src, src_size);

    if (dst_capacity == ZSTD_CONTENTSIZE_ERROR) {
        rb_raise(eZstdError, "Invalid compressed data");
    }
    if (dst_capacity == ZSTD_CONTENTSIZE_UNKNOWN) {
        rb_raise(eZstdError, "Content size unknown");
    }

    VALUE result = rb_str_buf_new(dst_capacity);
    char *dst = RSTRING_PTR(result);

    size_t decompressed_size = ZSTD_decompressDCtx(ctx, dst, dst_capacity, src, src_size);
    check_zstd_error(decompressed_size, "DCtx decompression failed");

    rb_str_set_len(result, decompressed_size);
    return result;
}

#load_dictionary(dict) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/zstd_native/zstd_native.c', line 283

static VALUE dctx_load_dictionary(VALUE self, VALUE dict) {
    Check_Type(dict, T_STRING);

    ZSTD_DCtx *ctx;
    TypedData_Get_Struct(self, ZSTD_DCtx, &dctx_type, ctx);

    const char *dict_data = RSTRING_PTR(dict);
    size_t dict_size = RSTRING_LEN(dict);

    size_t result = ZSTD_DCtx_loadDictionary(ctx, dict_data, dict_size);
    check_zstd_error(result, "Failed to load dictionary");
    return Qnil;
}

#ref_ddict(ddict_val) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
# File 'ext/zstd_native/zstd_native.c', line 297

static VALUE dctx_ref_ddict(VALUE self, VALUE ddict_val) {
    ZSTD_DCtx *ctx;
    TypedData_Get_Struct(self, ZSTD_DCtx, &dctx_type, ctx);

    ZSTD_DDict *ddict;
    TypedData_Get_Struct(ddict_val, ZSTD_DDict, &ddict_type, ddict);

    size_t result = ZSTD_DCtx_refDDict(ctx, ddict);
    check_zstd_error(result, "Failed to reference DDict");
    return Qnil;
}

#ref_prefix(prefix) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/zstd_native/zstd_native.c', line 309

static VALUE dctx_ref_prefix(VALUE self, VALUE prefix) {
    Check_Type(prefix, T_STRING);

    ZSTD_DCtx *ctx;
    TypedData_Get_Struct(self, ZSTD_DCtx, &dctx_type, ctx);

    const char *prefix_data = RSTRING_PTR(prefix);
    size_t prefix_size = RSTRING_LEN(prefix);

    size_t result = ZSTD_DCtx_refPrefix(ctx, prefix_data, prefix_size);
    check_zstd_error(result, "Failed to reference prefix");
    return Qnil;
}

#resetObject



274
275
276
277
278
279
280
281
# File 'ext/zstd_native/zstd_native.c', line 274

static VALUE dctx_reset(VALUE self) {
    ZSTD_DCtx *ctx;
    TypedData_Get_Struct(self, ZSTD_DCtx, &dctx_type, ctx);

    size_t result = ZSTD_DCtx_reset(ctx, ZSTD_reset_session_only);
    check_zstd_error(result, "DCtx reset failed");
    return Qnil;
}

#sizeofObject



323
324
325
326
327
# File 'ext/zstd_native/zstd_native.c', line 323

static VALUE dctx_sizeof(VALUE self) {
    ZSTD_DCtx *ctx;
    TypedData_Get_Struct(self, ZSTD_DCtx, &dctx_type, ctx);
    return SIZET2NUM(ZSTD_sizeof_DCtx(ctx));
}