Module: Zstd
- Defined in:
- lib/zstd-ruby.rb,
lib/zstd-ruby/version.rb,
lib/zstd-ruby/stream_reader.rb,
lib/zstd-ruby/stream_writer.rb,
ext/zstdruby/main.c
Defined Under Namespace
Classes: CDict, DDict, StreamReader, StreamWriter, StreamingCompress, StreamingDecompress
Constant Summary collapse
- VERSION =
"2.0.4"
Class Method Summary collapse
- .compress(*args) ⇒ Object
- .decompress(*args) ⇒ Object
- .read_skippable_frame(input_value) ⇒ Object
- .write_skippable_frame(*args) ⇒ Object
- .zstd_version ⇒ Object
Class Method Details
.compress(*args) ⇒ Object
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 |
# File 'ext/zstdruby/zstdruby.c', line 11 static VALUE rb_compress(int argc, VALUE *argv, VALUE self) { VALUE input_value; VALUE kwargs; rb_scan_args(argc, argv, "10:", &input_value, &kwargs); StringValue(input_value); ZSTD_CCtx* const ctx = ZSTD_createCCtx(); if (ctx == NULL) { rb_raise(rb_eRuntimeError, "%s", "ZSTD_createCCtx error"); } set_compress_params(ctx, kwargs); char* input_data = RSTRING_PTR(input_value); size_t input_size = RSTRING_LEN(input_value); 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 const ret = zstd_compress(ctx, output_data, max_compressed_size, input_data, input_size, false); ZSTD_freeCCtx(ctx); if (ZSTD_isError(ret)) { rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret)); } rb_str_resize(output, ret); return output; } |
.decompress(*args) ⇒ Object
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 118 119 120 121 122 123 124 125 126 127 128 |
# File 'ext/zstdruby/zstdruby.c', line 74 static VALUE rb_decompress(int argc, VALUE *argv, VALUE self) { VALUE input_value, kwargs; rb_scan_args(argc, argv, "10:", &input_value, &kwargs); StringValue(input_value); size_t in_size = RSTRING_LEN(input_value); const unsigned char *in_r = (const unsigned char *)RSTRING_PTR(input_value); unsigned char *in = ALLOC_N(unsigned char, in_size); memcpy(in, in_r, in_size); size_t off = 0; const uint32_t ZSTD_MAGIC = 0xFD2FB528U; const uint32_t SKIP_LO = 0x184D2A50U; /* ...5F */ while (off + 4 <= in_size) { uint32_t magic = (uint32_t)in[off] | ((uint32_t)in[off+1] << 8) | ((uint32_t)in[off+2] << 16) | ((uint32_t)in[off+3] << 24); if ((magic & 0xFFFFFFF0U) == (SKIP_LO & 0xFFFFFFF0U)) { if (off + 8 > in_size) break; uint32_t skipLen = (uint32_t)in[off+4] | ((uint32_t)in[off+5] << 8) | ((uint32_t)in[off+6] << 16) | ((uint32_t)in[off+7] << 24); size_t adv = (size_t)8 + (size_t)skipLen; if (off + adv > in_size) break; off += adv; continue; } if (magic == ZSTD_MAGIC) { ZSTD_DCtx *dctx = ZSTD_createDCtx(); if (!dctx) { xfree(in); rb_raise(rb_eRuntimeError, "ZSTD_createDCtx failed"); } VALUE out = decode_one_frame(dctx, in + off, in_size - off, kwargs); ZSTD_freeDCtx(dctx); xfree(in); RB_GC_GUARD(input_value); return out; } off += 1; } xfree(in); RB_GC_GUARD(input_value); rb_raise(rb_eRuntimeError, "not a zstd frame (magic not found)"); } |
.read_skippable_frame(input_value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'ext/zstdruby/skippable_frame.c', line 37 static VALUE rb_read_skippable_frame(VALUE self, VALUE input_value) { char* input_data = RSTRING_PTR(input_value); size_t input_size = RSTRING_LEN(input_value); if (ZSTD_isSkippableFrame(input_data, input_size) == 0) { return Qnil; } // ref https://github.com/facebook/zstd/blob/321490cd5b9863433b3d44816d04012874e5ecdb/tests/fuzzer.c#L2096 size_t const skipLen = 129 * 1024; VALUE output = rb_str_new(NULL, skipLen); char* output_data = RSTRING_PTR(output); unsigned readMagic; size_t output_size = ZSTD_readSkippableFrame((void*)output_data, skipLen, &readMagic, (const void*)input_data, input_size); if (ZSTD_isError(output_size)) { rb_raise(rb_eRuntimeError, "%s: %s", "read skippable frame failed", ZSTD_getErrorName(output_size)); } rb_str_resize(output, output_size); return output; } |
.write_skippable_frame(*args) ⇒ Object
5 6 7 8 9 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 |
# File 'ext/zstdruby/skippable_frame.c', line 5 static VALUE rb_write_skippable_frame(int argc, VALUE *argv, VALUE self) { VALUE input_value; VALUE skip_value; VALUE kwargs; rb_scan_args(argc, argv, "2:", &input_value, &skip_value, &kwargs); ID kwargs_keys[1]; kwargs_keys[0] = rb_intern("magic_variant"); VALUE kwargs_values[1]; rb_get_kwargs(kwargs, kwargs_keys, 0, 1, kwargs_values); unsigned magic_variant = (kwargs_values[0] != Qundef) ? (NUM2INT(kwargs_values[0])) : 0; StringValue(input_value); StringValue(skip_value); char* input_data = RSTRING_PTR(input_value); size_t input_size = RSTRING_LEN(input_value); char* skip_data = RSTRING_PTR(skip_value); size_t skip_size = RSTRING_LEN(skip_value); size_t dst_size = input_size + ZSTD_SKIPPABLEHEADERSIZE + skip_size; VALUE output = rb_str_new(input_data, dst_size); char* output_data = RSTRING_PTR(output); size_t output_size = ZSTD_writeSkippableFrame((void*)output_data, dst_size, (const void*)skip_data, skip_size, magic_variant); if (ZSTD_isError(output_size)) { rb_raise(rb_eRuntimeError, "%s: %s", "write skippable frame failed", ZSTD_getErrorName(output_size)); } rb_str_resize(output, output_size); return output; } |
.zstd_version ⇒ Object
5 6 7 8 9 |
# File 'ext/zstdruby/zstdruby.c', line 5 static VALUE zstdVersion(VALUE self) { unsigned version = ZSTD_versionNumber(); return INT2NUM(version); } |