Module: VibeZstd

Defined in:
lib/vibe_zstd.rb,
lib/vibe_zstd/version.rb,
lib/vibe_zstd/constants.rb,
ext/vibe_zstd/vibe_zstd.c

Defined Under Namespace

Modules: DictAttachPref, Format, LiteralCompressionMode, ResetDirective, Strategy, ThreadLocal Classes: CCtx, CDict, CompressWriter, DCtx, DDict, DecompressReader, DecompressedSizeExceeded, Error

Constant Summary collapse

VERSION =
"1.3.0"

Class Method Summary collapse

Class Method Details

.compress(data, **options) ⇒ Object

Convenience method for one-off compression. Per-call options (level, dict, pledged_size) are passed to #compress; any other keyword is a context parameter (e.g. checksum_flag:, window_log:, workers:, format:) applied to a fresh CCtx.



23
24
25
26
27
# File 'lib/vibe_zstd.rb', line 23

def self.compress(data, **options)
  call_opts = options.slice(*COMPRESS_CALL_OPTIONS)
  ctx_opts = options.except(*COMPRESS_CALL_OPTIONS)
  CCtx.new(**ctx_opts).compress(data, **call_opts)
end

.compress_bound(size) ⇒ Object

VibeZstd.compress_bound(size)



5
6
7
8
9
10
# File 'ext/vibe_zstd/frames.c', line 5

static VALUE
vibe_zstd_compress_bound(VALUE self, VALUE size) {
    size_t src_size = NUM2SIZET(size);
    size_t bound = ZSTD_compressBound(src_size);
    return SIZET2NUM(bound);
}

.decompress(data, **options) ⇒ Object

Convenience method for one-off decompression. Per-call options (dict, initial_capacity, max_decompressed_size/max_size) are passed to #decompress; any other keyword is a context parameter (e.g. format:, window_log_max:) applied to a fresh DCtx.



33
34
35
36
37
# File 'lib/vibe_zstd.rb', line 33

def self.decompress(data, **options)
  call_opts = options.slice(*DECOMPRESS_CALL_OPTIONS)
  ctx_opts = options.except(*DECOMPRESS_CALL_OPTIONS)
  DCtx.new(**ctx_opts).decompress(data, **call_opts)
end

.default_compression_levelObject Also known as: default_level



277
278
279
280
281
# File 'ext/vibe_zstd/vibe_zstd.c', line 277

static VALUE
vibe_zstd_default_c_level(VALUE self) {
    (void)self;
    return INT2NUM(ZSTD_defaultCLevel());
}

.dict_header_size(dict_data) ⇒ Object

VibeZstd.dict_header_size(dict_data)



593
594
595
596
597
598
599
600
601
602
603
604
# File 'ext/vibe_zstd/dict.c', line 593

static VALUE
vibe_zstd_dict_header_size(VALUE self, VALUE dict_data) {
    StringValue(dict_data);
    size_t header_size = ZDICT_getDictHeaderSize(RSTRING_PTR(dict_data), RSTRING_LEN(dict_data));

    // Check for errors
    if (ZDICT_isError(header_size)) {
        rb_raise(rb_eRuntimeError, "Failed to get dictionary header size: %s", ZDICT_getErrorName(header_size));
    }

    return SIZET2NUM(header_size);
}

.each_skippable_frame(data) ⇒ Object

Iterate over all skippable frames in the data Yields [content, magic_variant, offset] for each skippable frame



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vibe_zstd.rb', line 47

def self.each_skippable_frame(data)
  return enum_for(:each_skippable_frame, data) unless block_given?

  offset = 0
  while offset < data.bytesize
    frame_data = data.byteslice(offset..-1)
    frame_size = find_frame_compressed_size(frame_data)

    # Defense: Prevent infinite loop on malformed data
    # A valid frame must have non-zero size (at minimum: frame header)
    raise Error, "Invalid frame: zero or negative size at offset #{offset}" if frame_size <= 0

    if skippable_frame?(frame_data)
      content, magic_variant = read_skippable_frame(frame_data)
      yield content, magic_variant, offset
    end

    offset += frame_size
  end
end

.finalize_dictionary(*args) ⇒ Object

For large datasets, consider using a representative subset of samples.



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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'ext/vibe_zstd/dict.c', line 510

static VALUE
vibe_zstd_finalize_dictionary(int argc, VALUE* argv, VALUE self) {
    VALUE options;
    rb_scan_args(argc, argv, ":", &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    if (NIL_P(options)) {
        rb_raise(rb_eArgError, "finalize_dictionary requires keyword arguments");
    }

    // Get required parameters
    VALUE content_val = rb_hash_aref(options, ID2SYM(rb_intern("content")));
    VALUE samples_val = rb_hash_aref(options, ID2SYM(rb_intern("samples")));
    VALUE max_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_size")));

    if (NIL_P(content_val)) {
        rb_raise(rb_eArgError, "content: parameter is required");
    }
    if (NIL_P(samples_val)) {
        rb_raise(rb_eArgError, "samples: parameter is required");
    }
    if (NIL_P(max_size_val)) {
        rb_raise(rb_eArgError, "max_size: parameter is required");
    }

    // Validate types early
    StringValue(content_val);
    Check_Type(samples_val, T_ARRAY);
    size_t max_size = NUM2SIZET(max_size_val);

    long num_samples = RARRAY_LEN(samples_val);
    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating.
    // Build a private converted-samples array (see vibe_zstd_train_dict for details).
    VALUE converted_samples = rb_ary_new_capa(num_samples);
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples_val, i);
        StringValue(sample);  // Validate type early - may raise TypeError; updates local
        rb_ary_push(converted_samples, sample);
        total_samples_size += RSTRING_LEN(sample);
    }

    // Get optional parameters
    VALUE compression_level_val = rb_hash_aref(options, ID2SYM(rb_intern("compression_level")));
    VALUE dict_id_val = rb_hash_aref(options, ID2SYM(rb_intern("dict_id")));

    // Setup ZDICT_params_t
    ZDICT_params_t params;
    memset(&params, 0, sizeof(params));
    params.compressionLevel = NIL_P(compression_level_val) ? 0 : NUM2INT(compression_level_val);
    params.dictID = NIL_P(dict_id_val) ? 0 : NUM2UINT(dict_id_val);
    params.notificationLevel = 0;

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup
    finalize_dict_ctx ctx = {
        .base = {
            .resources = &resources,
            .result = Qnil,
            .max_dict_size = max_size,
            .num_samples = num_samples,
            .total_samples_size = total_samples_size,
            .samples = converted_samples  // use private array, not caller's array
        },
        .content_val = content_val,
        .params = params
    };

    rb_ensure(finalize_dict_body, (VALUE)&ctx, dict_training_cleanup, (VALUE)&resources);
    return ctx.base.result;
}

.find_frame_compressed_size(data) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/vibe_zstd/frames.c', line 119

static VALUE
vibe_zstd_find_frame_compressed_size(VALUE self, VALUE data) {
    (void)self;
    StringValue(data);

    // Returns compressed size of first complete frame (including header/checksum)
    // Useful for splitting concatenated frames in multi-frame archives
    size_t frame_size = ZSTD_findFrameCompressedSize(RSTRING_PTR(data), RSTRING_LEN(data));

    if (ZSTD_isError(frame_size)) {
        rb_raise(rb_eRuntimeError, "Failed to find frame size: %s", ZSTD_getErrorName(frame_size));
    }

    return SIZET2NUM(frame_size);
}

.frame_content_size(data) ⇒ Object

Get the decompressed content size from a compressed frame Returns nil if size is unknown or data is invalid



41
42
43
# File 'lib/vibe_zstd.rb', line 41

def self.frame_content_size(data)
  DCtx.frame_content_size(data)
end

.get_dict_id(dict_data) ⇒ Object

VibeZstd.get_dict_id(dict_data)



489
490
491
492
493
494
# File 'ext/vibe_zstd/dict.c', line 489

static VALUE
vibe_zstd_get_dict_id(VALUE self, VALUE dict_data) {
    StringValue(dict_data);
    unsigned dict_id = ZDICT_getDictID(RSTRING_PTR(dict_data), RSTRING_LEN(dict_data));
    return UINT2NUM(dict_id);
}

.get_dict_id_from_frame(data) ⇒ Object

VibeZstd.get_dict_id_from_frame(data)



498
499
500
501
502
503
# File 'ext/vibe_zstd/dict.c', line 498

static VALUE
vibe_zstd_get_dict_id_from_frame(VALUE self, VALUE data) {
    StringValue(data);
    unsigned dict_id = ZSTD_getDictID_fromFrame(RSTRING_PTR(data), RSTRING_LEN(data));
    return UINT2NUM(dict_id);
}

.max_compression_levelObject Also known as: max_level



271
272
273
274
275
# File 'ext/vibe_zstd/vibe_zstd.c', line 271

static VALUE
vibe_zstd_max_c_level(VALUE self) {
    (void)self;
    return INT2NUM(ZSTD_maxCLevel());
}

.min_compression_levelObject Also known as: min_level



265
266
267
268
269
# File 'ext/vibe_zstd/vibe_zstd.c', line 265

static VALUE
vibe_zstd_min_c_level(VALUE self) {
    (void)self;
    return INT2NUM(ZSTD_minCLevel());
}

.read_skippable_frame(data) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
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
116
117
# File 'ext/vibe_zstd/frames.c', line 64

static VALUE
vibe_zstd_read_skippable_frame(VALUE self, VALUE data) {
    (void)self;
    StringValue(data);

    if (!ZSTD_isSkippableFrame(RSTRING_PTR(data), RSTRING_LEN(data))) {
        rb_raise(rb_eArgError, "data is not a skippable frame (%zu bytes provided)", RSTRING_LEN(data));
    }

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

    // Content size is in bytes 4-7 (little-endian uint32)
    if (src_size < 8) {
        rb_raise(rb_eArgError, "skippable frame too small (%zu bytes, minimum 8 bytes required)", src_size);
    }

    uint32_t content_size;
    memcpy(&content_size, src + 4, 4);

    // The content size field is attacker-controlled and may claim up to ~4 GiB.
    // A skippable frame's content cannot exceed the bytes actually provided
    // (src_size minus the 8-byte header), so cap the allocation accordingly to
    // prevent a tiny truncated input from forcing a huge allocation. A frame
    // whose declared size exceeds what is present is malformed and
    // ZSTD_readSkippableFrame reports the error below.
    size_t capacity = content_size;
    if (capacity > src_size - 8) {
        capacity = src_size - 8;
    }

    VALUE result = rb_str_buf_new(capacity);
    unsigned magic_variant;

    size_t bytes_read = ZSTD_readSkippableFrame(
        RSTRING_PTR(result),
        capacity,
        &magic_variant,
        src,
        src_size
    );

    if (ZSTD_isError(bytes_read)) {
        rb_raise(rb_eRuntimeError, "Failed to read skippable frame: %s", ZSTD_getErrorName(bytes_read));
    }

    rb_str_set_len(result, bytes_read);

    // Return [content, magic_variant]
    VALUE result_ary = rb_ary_new_capa(2);
    rb_ary_push(result_ary, result);
    rb_ary_push(result_ary, UINT2NUM(magic_variant));
    return result_ary;
}

.skippable_frame?(data) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'ext/vibe_zstd/frames.c', line 12

static VALUE
vibe_zstd_skippable_frame_p(VALUE self, VALUE data) {
    (void)self;
    StringValue(data);
    unsigned result = ZSTD_isSkippableFrame(RSTRING_PTR(data), RSTRING_LEN(data));
    return result ? Qtrue : Qfalse;
}

.train_dict(*args) ⇒ Object

For large datasets, consider training on a representative subset to reduce memory footprint.



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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'ext/vibe_zstd/dict.c', line 248

static VALUE
vibe_zstd_train_dict(int argc, VALUE* argv, VALUE self) {
    VALUE samples, options;
    rb_scan_args(argc, argv, "1:", &samples, &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    Check_Type(samples, T_ARRAY);
    long num_samples = RARRAY_LEN(samples);

    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating.
    // Build a private converted-samples array so that StringValue conversions are
    // retained: rb_ary_entry re-fetches the raw element, so storing the converted
    // value here ensures copy_samples_to_buffer operates on real String objects
    // rather than potentially-non-String objects that merely respond to to_str.
    VALUE converted_samples = rb_ary_new_capa(num_samples);
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        StringValue(sample);  // Validate type early - may raise TypeError; updates local
        rb_ary_push(converted_samples, sample);
        total_samples_size += RSTRING_LEN(sample);
    }

    // Parse options
    VALUE max_dict_size_val = Qnil;
    if (!NIL_P(options)) {
        max_dict_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_dict_size")));
    }

    // Default max dictionary size is 112KB (zstd default)
    size_t max_dict_size = NIL_P(max_dict_size_val) ? (112 * 1024) : NUM2SIZET(max_dict_size_val);

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_dict_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup
    dict_training_ctx ctx = {
        .resources = &resources,
        .result = Qnil,
        .max_dict_size = max_dict_size,
        .num_samples = num_samples,
        .total_samples_size = total_samples_size,
        .samples = converted_samples  // use private array, not caller's array
    };

    rb_ensure(train_dict_basic_body, (VALUE)&ctx, dict_training_cleanup, (VALUE)&resources);
    return ctx.result;
}

.train_dict_cover(*args) ⇒ Object

For large datasets, consider training on a representative subset to reduce memory footprint.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
# File 'ext/vibe_zstd/dict.c', line 308

static VALUE
vibe_zstd_train_dict_cover(int argc, VALUE* argv, VALUE self) {
    VALUE samples, options;
    rb_scan_args(argc, argv, "1:", &samples, &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    Check_Type(samples, T_ARRAY);
    long num_samples = RARRAY_LEN(samples);

    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating.
    // Build a private converted-samples array (see vibe_zstd_train_dict for details).
    VALUE converted_samples = rb_ary_new_capa(num_samples);
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        StringValue(sample);  // Validate type early - may raise TypeError; updates local
        rb_ary_push(converted_samples, sample);
        total_samples_size += RSTRING_LEN(sample);
    }

    // Initialize COVER parameters with defaults
    ZDICT_cover_params_t params;
    memset(&params, 0, sizeof(params));
    params.splitPoint = 1.0;  // Default split point

    // Parse options
    if (!NIL_P(options)) {
        VALUE v;

        v = rb_hash_aref(options, ID2SYM(rb_intern("k")));
        if (!NIL_P(v)) params.k = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("d")));
        if (!NIL_P(v)) params.d = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("steps")));
        if (!NIL_P(v)) params.steps = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("split_point")));
        if (!NIL_P(v)) params.splitPoint = NUM2DBL(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict")));
        if (!NIL_P(v)) params.shrinkDict = RTEST(v) ? 1 : 0;

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict_max_regression")));
        if (!NIL_P(v)) params.shrinkDictMaxRegression = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("nb_threads")));
        if (!NIL_P(v)) params.nbThreads = NUM2UINT(v);
    }

    // Get max_dict_size (default 112KB)
    VALUE max_dict_size_val = Qnil;
    if (!NIL_P(options)) {
        max_dict_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_dict_size")));
    }
    size_t max_dict_size = NIL_P(max_dict_size_val) ? (112 * 1024) : NUM2SIZET(max_dict_size_val);
    params.zParams.compressionLevel = 0;  // Use default compression level

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_dict_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup
    train_dict_cover_ctx ctx = {
        .base = {
            .resources = &resources,
            .result = Qnil,
            .max_dict_size = max_dict_size,
            .num_samples = num_samples,
            .total_samples_size = total_samples_size,
            .samples = converted_samples  // use private array, not caller's array
        },
        .params = params
    };

    rb_ensure(train_dict_cover_body, (VALUE)&ctx, dict_training_cleanup, (VALUE)&resources);
    return ctx.base.result;
}

.train_dict_fast_cover(*args) ⇒ Object

For large datasets, consider training on a representative subset to reduce memory footprint.



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'ext/vibe_zstd/dict.c', line 398

static VALUE
vibe_zstd_train_dict_fast_cover(int argc, VALUE* argv, VALUE self) {
    VALUE samples, options;
    rb_scan_args(argc, argv, "1:", &samples, &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    Check_Type(samples, T_ARRAY);
    long num_samples = RARRAY_LEN(samples);

    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating.
    // Build a private converted-samples array (see vibe_zstd_train_dict for details).
    VALUE converted_samples = rb_ary_new_capa(num_samples);
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        StringValue(sample);  // Validate type early - may raise TypeError; updates local
        rb_ary_push(converted_samples, sample);
        total_samples_size += RSTRING_LEN(sample);
    }

    // Initialize COVER parameters with defaults
    ZDICT_fastCover_params_t params;
    memset(&params, 0, sizeof(params));
    params.splitPoint = 1.0;  // Default split point

    // Parse options
    if (!NIL_P(options)) {
        VALUE v;

        v = rb_hash_aref(options, ID2SYM(rb_intern("k")));
        if (!NIL_P(v)) params.k = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("d")));
        if (!NIL_P(v)) params.d = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("f")));
        if (!NIL_P(v)) params.f = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("split_point")));
        if (!NIL_P(v)) params.splitPoint = NUM2DBL(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("accel")));
        if (!NIL_P(v)) params.accel = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict")));
        if (!NIL_P(v)) params.shrinkDict = RTEST(v) ? 1 : 0;

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict_max_regression")));
        if (!NIL_P(v)) params.shrinkDictMaxRegression = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("nb_threads")));
        if (!NIL_P(v)) params.nbThreads = NUM2UINT(v);
    }

    // Get max_dict_size (default 112KB)
    VALUE max_dict_size_val = Qnil;
    if (!NIL_P(options)) {
        max_dict_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_dict_size")));
    }
    size_t max_dict_size = NIL_P(max_dict_size_val) ? (112 * 1024) : NUM2SIZET(max_dict_size_val);
    params.zParams.compressionLevel = 0;  // Use default compression level

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_dict_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup
    train_dict_fast_cover_ctx ctx = {
        .base = {
            .resources = &resources,
            .result = Qnil,
            .max_dict_size = max_dict_size,
            .num_samples = num_samples,
            .total_samples_size = total_samples_size,
            .samples = converted_samples  // use private array, not caller's array
        },
        .params = params
    };

    rb_ensure(train_dict_fast_cover_body, (VALUE)&ctx, dict_training_cleanup, (VALUE)&resources);
    return ctx.base.result;
}

.version_numberObject

Module-level version and compression level functions



253
254
255
256
257
# File 'ext/vibe_zstd/vibe_zstd.c', line 253

static VALUE
vibe_zstd_version_number(VALUE self) {
    (void)self;
    return UINT2NUM(ZSTD_versionNumber());
}

.version_stringObject



259
260
261
262
263
# File 'ext/vibe_zstd/vibe_zstd.c', line 259

static VALUE
vibe_zstd_version_string(VALUE self) {
    (void)self;
    return rb_str_new_cstr(ZSTD_versionString());
}

.write_skippable_frame(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/vibe_zstd/frames.c', line 20

static VALUE
vibe_zstd_write_skippable_frame(int argc, VALUE *argv, VALUE self) {
    (void)self;
    VALUE data, options;
    rb_scan_args(argc, argv, "11", &data, &options);

    StringValue(data);

    unsigned magic_variant = 0;  // Default to 0
    if (!NIL_P(options)) {
        Check_Type(options, T_HASH);
        VALUE magic_num = rb_hash_aref(options, ID2SYM(rb_intern("magic_number")));
        if (!NIL_P(magic_num)) {
            magic_variant = NUM2UINT(magic_num);
            if (magic_variant > 15) {
                rb_raise(rb_eArgError, "magic_number %u out of bounds (valid: 0-15)", magic_variant);
            }
        }
    }

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

    // Skippable frame structure: 4-byte magic (0x184D2A5X) + 4-byte size + content
    // Decoders skip these frames, allowing custom metadata/padding
    size_t frame_size = 8 + src_size;
    VALUE result = rb_str_buf_new(frame_size);

    size_t written = ZSTD_writeSkippableFrame(
        RSTRING_PTR(result),
        frame_size,
        src,
        src_size,
        magic_variant
    );

    if (ZSTD_isError(written)) {
        rb_raise(rb_eRuntimeError, "Failed to write skippable frame: %s", ZSTD_getErrorName(written));
    }

    rb_str_set_len(result, written);
    return result;
}