Class: Zstd

Inherits:
Object
  • Object
show all
Defined in:
lib/zstd.rb,
lib/zstd/version.rb,
ext/zstd/zstd.c

Constant Summary collapse

VERSION =
"1.1.2.1"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'ext/zstd/zstd.c', line 154

static VALUE initialize(int argc, VALUE *argv, VALUE self) {
  VALUE dic_arg;
  struct dic_struct *ds;
  Data_Get_Struct(self, struct dic_struct, ds);
  ds->cdic = NULL;
  ds->cctx = NULL;
  ds->ddic = NULL;
  ds->dctx = NULL;
  
  rb_scan_args(argc, argv, "01", &dic_arg);

  if (!NIL_P(dic_arg)) {
    Check_Type(dic_arg, RUBY_T_STRING);    
    const char* dic_ptr = RSTRING_PTR(dic_arg);
    ds->cdic = createCDict_or_raise(dic_ptr, 19);
    ds->cctx = ZSTD_createCCtx();
    ds->ddic = createDDict_or_raise(dic_ptr);
    ds->dctx = ZSTD_createDCtx();
  }

  return self;
}

Instance Method Details

#compress(*args) ⇒ Object



63
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
# File 'ext/zstd/zstd.c', line 63

static VALUE compress(int argc, VALUE *argv, VALUE self) {
  VALUE src_buf, dst_buf, comp_level_arg;
  const char* src_ptr;
  char* dst_ptr;
  size_t src_size, dst_size, res;
  int comp_level;

  rb_scan_args(argc, argv, "11", &src_buf, &comp_level_arg);
  
  Check_Type(src_buf, RUBY_T_STRING);
  src_ptr = RSTRING_PTR(src_buf);
  src_size = RSTRING_LEN(src_buf);
  
  dst_size = ZSTD_compressBound(src_size);
  dst_buf = rb_str_new(NULL, dst_size);
  dst_ptr = RSTRING_PTR(dst_buf);

  comp_level = 3;
  if (!NIL_P(comp_level_arg)) {
    Check_Type(comp_level_arg, RUBY_T_FIXNUM);
    comp_level = FIX2INT(comp_level_arg);    
  }

  struct dic_struct *ds;
  Data_Get_Struct(self, struct dic_struct, ds);

  if (ds->cdic == NULL) {
    res = ZSTD_compress((void*)dst_ptr, dst_size, (const void*)src_ptr, src_size, comp_level);
  } else {    
    res = ZSTD_compress_usingCDict(ds->cctx, (void*)dst_ptr, dst_size, (const void*)src_ptr, src_size, ds->cdic);
  }

  if (ZSTD_isError(res)) {
    rb_raise(rb_eRuntimeError, "error compressing: %s", ZSTD_getErrorName(res));
  } else {
    rb_str_resize(dst_buf, res);
  }

  return dst_buf;
}

#decompress(*args) ⇒ Object



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
129
130
131
132
133
134
135
136
# File 'ext/zstd/zstd.c', line 104

static VALUE decompress(int argc, VALUE *argv, VALUE self) {
  VALUE src_buf, dst_buf;
  const char* src_ptr;
  char* dst_ptr;
  size_t src_size, dst_size, res;
  
  rb_scan_args(argc, argv, "10", &src_buf);

  Check_Type(src_buf, RUBY_T_STRING);
  src_ptr = RSTRING_PTR(src_buf);
  src_size = RSTRING_LEN(src_buf);

  dst_size = ZSTD_getDecompressedSize(src_ptr, src_size);
  dst_buf = rb_str_new(NULL, dst_size);
  dst_ptr = RSTRING_PTR(dst_buf);

  struct dic_struct *ds;
  Data_Get_Struct(self, struct dic_struct, ds);

  if (ds->ddic == NULL) {
    res = ZSTD_decompress((void*)dst_ptr, dst_size, (const void*)src_ptr, src_size);
  } else {
    res = ZSTD_decompress_usingDDict(ds->dctx, (void*)dst_ptr, dst_size, (const void*)src_ptr, src_size, ds->ddic);
  }

  if (ZSTD_isError(res)) {
    rb_raise(rb_eRuntimeError, "error decompressing: %s", ZSTD_getErrorName(res));
  } else {
    rb_str_resize(dst_buf, res);
  }

  return dst_buf;  
}