Module: Zstd

Defined in:
lib/zstd-ruby.rb,
lib/zstd-ruby/version.rb,
ext/zstdruby/zstdruby.c

Constant Summary collapse

VERSION =
"1.5.0.0"

Class Method Summary collapse

Class Method Details

.compress(*args) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'ext/zstdruby/zstdruby.c', line 10

static VALUE compress(int argc, VALUE *argv, VALUE self)
{
  VALUE input_value;
  VALUE compression_level_value;
  rb_scan_args(argc, argv, "11", &input_value, &compression_level_value);

  Check_Type(input_value, RUBY_T_STRING);
  const char* input_data = RSTRING_PTR(input_value);
  size_t input_size = RSTRING_LEN(input_value);

  int compression_level;
  if (NIL_P(compression_level_value)) {
    compression_level = 0; // The default. See ZSTD_CLEVEL_DEFAULT in zstd_compress.c
  } else {
    compression_level = NUM2INT(compression_level_value);
  }

  // do compress
  size_t max_compressed_size = ZSTD_compressBound(input_size);

  VALUE output = rb_str_new(NULL, max_compressed_size);
  char* output_data = RSTRING_PTR(output);

  size_t compressed_size = ZSTD_compress((void*)output_data, max_compressed_size,
                                         (const void*)input_data, input_size, compression_level);

  if (ZSTD_isError(compressed_size)) {
    rb_raise(rb_eRuntimeError, "%s: %s", "compress failed", ZSTD_getErrorName(compressed_size));
  } else {
    rb_str_resize(output, compressed_size);
  }

  return output;
}

.decompress(input) ⇒ Object


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/zstdruby/zstdruby.c', line 82

static VALUE decompress(VALUE self, VALUE input)
{
  Check_Type(input, T_STRING);
  const char* input_data = RSTRING_PTR(input);
  size_t input_size = RSTRING_LEN(input);

  uint64_t uncompressed_size = ZSTD_getDecompressedSize(input_data, input_size);

  if (uncompressed_size == 0) {
    return decompress_buffered(input_data, input_size);
  }

  VALUE output = rb_str_new(NULL, uncompressed_size);
  char* output_data = RSTRING_PTR(output);

  size_t decompress_size = ZSTD_decompress((void*)output_data, uncompressed_size,
                                           (const void*)input_data, input_size);

  if (ZSTD_isError(decompress_size)) {
    rb_raise(rb_eRuntimeError, "%s: %s", "decompress error", ZSTD_getErrorName(decompress_size));
  }

  return output;
}

.zstd_versionObject


4
5
6
7
8
# File 'ext/zstdruby/zstdruby.c', line 4

static VALUE zstdVersion(VALUE self)
{
  unsigned version = ZSTD_versionNumber();
  return INT2NUM(version);
}