extzstd 0.0.3.CONCEPT-x86-mingw32 → 0.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.ja +5 -0
  3. data/LICENSE +6 -6
  4. data/README.md +35 -22
  5. data/contrib/zstd/LICENSE +13 -9
  6. data/contrib/zstd/README.md +37 -44
  7. data/contrib/zstd/common/entropy_common.c +33 -39
  8. data/contrib/zstd/common/error_private.c +43 -0
  9. data/contrib/zstd/common/error_private.h +11 -60
  10. data/contrib/zstd/common/fse.h +11 -5
  11. data/contrib/zstd/common/fse_decompress.c +14 -16
  12. data/contrib/zstd/common/huf.h +1 -1
  13. data/contrib/zstd/common/mem.h +36 -43
  14. data/contrib/zstd/common/xxhash.c +31 -18
  15. data/contrib/zstd/common/xxhash.h +71 -35
  16. data/contrib/zstd/common/zbuff.h +29 -35
  17. data/contrib/zstd/common/zstd_common.c +24 -32
  18. data/contrib/zstd/common/zstd_errors.h +60 -0
  19. data/contrib/zstd/common/zstd_internal.h +109 -80
  20. data/contrib/zstd/compress/fse_compress.c +9 -6
  21. data/contrib/zstd/compress/huf_compress.c +30 -74
  22. data/contrib/zstd/compress/zbuff_compress.c +43 -51
  23. data/contrib/zstd/compress/zstd_compress.c +953 -763
  24. data/contrib/zstd/compress/zstd_opt.h +115 -261
  25. data/contrib/zstd/decompress/huf_decompress.c +29 -40
  26. data/contrib/zstd/decompress/zbuff_decompress.c +36 -78
  27. data/contrib/zstd/decompress/zstd_decompress.c +976 -496
  28. data/contrib/zstd/dictBuilder/divsufsort.h +5 -5
  29. data/contrib/zstd/dictBuilder/zdict.c +194 -229
  30. data/contrib/zstd/dictBuilder/zdict.h +66 -68
  31. data/contrib/zstd/legacy/zstd_legacy.h +168 -49
  32. data/contrib/zstd/legacy/zstd_v01.c +95 -178
  33. data/contrib/zstd/legacy/zstd_v01.h +12 -32
  34. data/contrib/zstd/legacy/zstd_v02.c +48 -274
  35. data/contrib/zstd/legacy/zstd_v02.h +12 -32
  36. data/contrib/zstd/legacy/zstd_v03.c +48 -274
  37. data/contrib/zstd/legacy/zstd_v03.h +12 -32
  38. data/contrib/zstd/legacy/zstd_v04.c +63 -320
  39. data/contrib/zstd/legacy/zstd_v04.h +13 -33
  40. data/contrib/zstd/legacy/zstd_v05.c +80 -345
  41. data/contrib/zstd/legacy/zstd_v05.h +9 -31
  42. data/contrib/zstd/legacy/zstd_v06.c +48 -458
  43. data/contrib/zstd/legacy/zstd_v06.h +41 -67
  44. data/contrib/zstd/legacy/zstd_v07.c +4544 -0
  45. data/contrib/zstd/legacy/zstd_v07.h +173 -0
  46. data/contrib/zstd/zstd.h +640 -0
  47. data/ext/extconf.rb +7 -3
  48. data/ext/extzstd.c +263 -106
  49. data/ext/extzstd.h +8 -6
  50. data/ext/extzstd_nogvls.h +0 -117
  51. data/ext/extzstd_stream.c +347 -0
  52. data/ext/zstd_common.c +8 -0
  53. data/ext/zstd_compress.c +6 -0
  54. data/ext/zstd_decompress.c +5 -0
  55. data/ext/zstd_dictbuilder.c +5 -0
  56. data/ext/zstd_legacy_v07.c +1 -0
  57. data/gemstub.rb +18 -16
  58. data/lib/2.1/extzstd.so +0 -0
  59. data/lib/2.2/extzstd.so +0 -0
  60. data/lib/2.3/extzstd.so +0 -0
  61. data/lib/extzstd/version.rb +1 -1
  62. data/lib/extzstd.rb +77 -43
  63. data/test/test_basic.rb +11 -6
  64. metadata +23 -11
  65. data/contrib/zstd/common/error_public.h +0 -77
  66. data/contrib/zstd/common/zstd.h +0 -475
  67. data/ext/extzstd_buffered.c +0 -265
  68. data/ext/zstd_amalgam.c +0 -18
  69. data/lib/2.0/extzstd.so +0 -0
data/ext/extzstd.c CHANGED
@@ -1,8 +1,36 @@
1
1
  #include "extzstd.h"
2
- #include <mem.h>
3
- #include <error_public.h>
2
+ #include <zstd/common/mem.h>
3
+ #include <zstd_errors.h>
4
4
  #include <zdict.h>
5
5
 
6
+ static void
7
+ aux_string_pointer(VALUE str, const char **ptr, size_t *size)
8
+ {
9
+ rb_check_type(str, RUBY_T_STRING);
10
+ RSTRING_GETMEM(str, *ptr, *size);
11
+ }
12
+
13
+ static void
14
+ aux_string_pointer_with_nil(VALUE str, const char **ptr, size_t *size)
15
+ {
16
+ if (NIL_P(str)) {
17
+ *ptr = NULL;
18
+ *size = 0;
19
+ } else {
20
+ aux_string_pointer(str, ptr, size);
21
+ }
22
+ }
23
+
24
+ static void
25
+ aux_string_expand_pointer(VALUE str, char **ptr, size_t size)
26
+ {
27
+ rb_check_type(str, RUBY_T_STRING);
28
+ rb_str_modify(str);
29
+ rb_str_set_len(str, 0);
30
+ rb_str_modify_expand(str, size);
31
+ *ptr = RSTRING_PTR(str);
32
+ }
33
+
6
34
  VALUE extzstd_mZstd;
7
35
 
8
36
  /*
@@ -207,22 +235,29 @@ init_constants(void)
207
235
  VALUE mConstants = rb_define_module_under(extzstd_mZstd, "Constants");
208
236
  rb_include_module(extzstd_mZstd, mConstants);
209
237
 
238
+ rb_define_const(mConstants, "ZSTD_MAX_COMPRESSION_LEVEL", INT2NUM(ZSTD_maxCLevel()));
239
+ rb_define_const(mConstants, "MAX_COMPRESSION_LEVEL", INT2NUM(ZSTD_maxCLevel()));
240
+
210
241
  rb_define_const(mConstants, "ZSTD_FAST", INT2NUM(ZSTD_fast));
211
242
  rb_define_const(mConstants, "ZSTD_DFAST", INT2NUM(ZSTD_dfast));
212
243
  rb_define_const(mConstants, "ZSTD_GREEDY", INT2NUM(ZSTD_greedy));
213
244
  rb_define_const(mConstants, "ZSTD_LAZY", INT2NUM(ZSTD_lazy));
214
245
  rb_define_const(mConstants, "ZSTD_LAZY2", INT2NUM(ZSTD_lazy2));
215
246
  rb_define_const(mConstants, "ZSTD_BTLAZY2", INT2NUM(ZSTD_btlazy2));
247
+ rb_define_const(mConstants, "ZSTD_BTOPT", INT2NUM(ZSTD_btopt));
216
248
  rb_define_const(mConstants, "ZSTD_WINDOWLOG_MAX", INT2NUM(ZSTD_WINDOWLOG_MAX));
217
249
  rb_define_const(mConstants, "ZSTD_WINDOWLOG_MIN", INT2NUM(ZSTD_WINDOWLOG_MIN));
218
- rb_define_const(mConstants, "ZSTD_CHAINLOG_MAX", INT2NUM(ZSTD_CHAINLOG_MAX));
219
- rb_define_const(mConstants, "ZSTD_CHAINLOG_MIN", INT2NUM(ZSTD_CHAINLOG_MIN));
220
250
  rb_define_const(mConstants, "ZSTD_HASHLOG_MAX", INT2NUM(ZSTD_HASHLOG_MAX));
221
251
  rb_define_const(mConstants, "ZSTD_HASHLOG_MIN", INT2NUM(ZSTD_HASHLOG_MIN));
252
+ rb_define_const(mConstants, "ZSTD_CHAINLOG_MAX", INT2NUM(ZSTD_CHAINLOG_MAX));
253
+ rb_define_const(mConstants, "ZSTD_CHAINLOG_MIN", INT2NUM(ZSTD_CHAINLOG_MIN));
254
+ rb_define_const(mConstants, "ZSTD_HASHLOG3_MAX", INT2NUM(ZSTD_HASHLOG3_MAX));
222
255
  rb_define_const(mConstants, "ZSTD_SEARCHLOG_MAX", INT2NUM(ZSTD_SEARCHLOG_MAX));
223
256
  rb_define_const(mConstants, "ZSTD_SEARCHLOG_MIN", INT2NUM(ZSTD_SEARCHLOG_MIN));
224
257
  rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
225
258
  rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
259
+ rb_define_const(mConstants, "ZSTD_TARGETLENGTH_MAX", INT2NUM(ZSTD_TARGETLENGTH_MAX));
260
+ rb_define_const(mConstants, "ZSTD_TARGETLENGTH_MIN", INT2NUM(ZSTD_TARGETLENGTH_MIN));
226
261
 
227
262
  rb_define_const(mConstants, "FAST", INT2NUM(ZSTD_fast));
228
263
  rb_define_const(mConstants, "DFAST", INT2NUM(ZSTD_dfast));
@@ -230,16 +265,20 @@ init_constants(void)
230
265
  rb_define_const(mConstants, "LAZY", INT2NUM(ZSTD_lazy));
231
266
  rb_define_const(mConstants, "LAZY2", INT2NUM(ZSTD_lazy2));
232
267
  rb_define_const(mConstants, "BTLAZY2", INT2NUM(ZSTD_btlazy2));
268
+ rb_define_const(mConstants, "BTOPT", INT2NUM(ZSTD_btopt));
233
269
  rb_define_const(mConstants, "WINDOWLOG_MAX", INT2NUM(ZSTD_WINDOWLOG_MAX));
234
270
  rb_define_const(mConstants, "WINDOWLOG_MIN", INT2NUM(ZSTD_WINDOWLOG_MIN));
235
- rb_define_const(mConstants, "CHAINLOG_MAX", INT2NUM(ZSTD_CHAINLOG_MAX));
236
- rb_define_const(mConstants, "CHAINLOG_MIN", INT2NUM(ZSTD_CHAINLOG_MIN));
237
271
  rb_define_const(mConstants, "HASHLOG_MAX", INT2NUM(ZSTD_HASHLOG_MAX));
238
272
  rb_define_const(mConstants, "HASHLOG_MIN", INT2NUM(ZSTD_HASHLOG_MIN));
273
+ rb_define_const(mConstants, "CHAINLOG_MAX", INT2NUM(ZSTD_CHAINLOG_MAX));
274
+ rb_define_const(mConstants, "CHAINLOG_MIN", INT2NUM(ZSTD_CHAINLOG_MIN));
275
+ rb_define_const(mConstants, "HASHLOG3_MAX", INT2NUM(ZSTD_HASHLOG3_MAX));
239
276
  rb_define_const(mConstants, "SEARCHLOG_MAX", INT2NUM(ZSTD_SEARCHLOG_MAX));
240
277
  rb_define_const(mConstants, "SEARCHLOG_MIN", INT2NUM(ZSTD_SEARCHLOG_MIN));
241
278
  rb_define_const(mConstants, "SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
242
279
  rb_define_const(mConstants, "SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
280
+ rb_define_const(mConstants, "TARGETLENGTH_MAX", INT2NUM(ZSTD_TARGETLENGTH_MAX));
281
+ rb_define_const(mConstants, "TARGETLENGTH_MIN", INT2NUM(ZSTD_TARGETLENGTH_MIN));
243
282
  }
244
283
 
245
284
  /*
@@ -249,33 +288,33 @@ init_constants(void)
249
288
  VALUE extzstd_cParams;
250
289
 
251
290
  AUX_IMPLEMENT_CONTEXT(
252
- ZSTD_parameters, encparams_type, "extzstd.EncodeParameters",
253
- encparams_alloc_dummy, NULL, free, NULL,
254
- getencparamsp, getencparams, encparams_p);
291
+ ZSTD_parameters, params_type, "extzstd.Parameters",
292
+ params_alloc_dummy, NULL, free, NULL,
293
+ getparamsp, getparams, params_p);
255
294
 
256
295
  ZSTD_parameters *
257
- extzstd_getencparams(VALUE v)
296
+ extzstd_getparams(VALUE v)
258
297
  {
259
- return getencparams(v);
298
+ return getparams(v);
260
299
  }
261
300
 
262
301
  int
263
- extzstd_encparams_p(VALUE v)
302
+ extzstd_params_p(VALUE v)
264
303
  {
265
- return encparams_p(v);
304
+ return params_p(v);
266
305
  }
267
306
 
268
307
  static VALUE
269
- encparams_alloc(VALUE mod)
308
+ params_alloc(VALUE mod)
270
309
  {
271
310
  ZSTD_parameters *p;
272
- return TypedData_Make_Struct(mod, ZSTD_parameters, &encparams_type, p);
311
+ return TypedData_Make_Struct(mod, ZSTD_parameters, &params_type, p);
273
312
  }
274
313
 
275
314
  VALUE
276
315
  extzstd_params_alloc(ZSTD_parameters **p)
277
316
  {
278
- return TypedData_Make_Struct(extzstd_cParams, ZSTD_parameters, &encparams_type, *p);
317
+ return TypedData_Make_Struct(extzstd_cParams, ZSTD_parameters, &params_type, *p);
279
318
  }
280
319
 
281
320
  /*
@@ -291,42 +330,22 @@ extzstd_params_alloc(ZSTD_parameters **p)
291
330
  * [opts hashlog: nil]
292
331
  * [opts searchlog: nil]
293
332
  * [opts searchlength: nil]
333
+ * [opts targetlength: nil]
294
334
  * [opts strategy: nil]
295
335
  */
296
336
  static VALUE
297
- encparams_init(int argc, VALUE argv[], VALUE v)
337
+ params_init(int argc, VALUE argv[], VALUE v)
298
338
  {
299
- ZSTD_parameters *p = getencparams(v);
339
+ ZSTD_parameters *p = getparams(v);
300
340
  uint64_t sizehint;
301
341
  size_t dictsize;
302
342
  int level;
303
343
  VALUE opts = Qnil;
304
- int argc0 = argc;
305
- if (argc > 0) {
306
- if (rb_type_p(argv[argc - 1], RUBY_T_HASH)) {
307
- opts = argv[argc - 1];
308
- argc --;
309
- }
310
- }
311
- if (argc == 0) {
312
- level = 0;
313
- sizehint = 0;
314
- dictsize = 0;
315
- } else if (argc == 1) {
316
- level = aux_num2int(argv[0], 0);
317
- sizehint = 0;
318
- dictsize = 0;
319
- } else if (argc == 2) {
320
- level = aux_num2int(argv[0], 0);
321
- sizehint = aux_num2int_u64(argv[1], 0);
322
- dictsize = 0;
323
- } else if (argc == 3) {
324
- level = aux_num2int(argv[0], 0);
325
- sizehint = aux_num2int_u64(argv[1], 0);
326
- dictsize = aux_num2int_u64(argv[2], 0);
327
- } else {
328
- rb_raise(rb_eArgError, "wrong number of argument (%d for 0..3 with keywords)", argc0);
329
- }
344
+
345
+ argc = rb_scan_args(argc, argv, "03:", NULL, NULL, NULL, &opts);
346
+ level = argc > 0 ? aux_num2int(argv[0], 0) : 0;
347
+ sizehint = argc > 1 ? aux_num2int_u64(argv[1], 0) : 0;
348
+ dictsize = argc > 2 ? aux_num2int_u64(argv[2], 0) : 0;
330
349
 
331
350
  *p = ZSTD_getParams(level, sizehint, dictsize);
332
351
 
@@ -344,6 +363,7 @@ encparams_init(int argc, VALUE argv[], VALUE v)
344
363
  SETUP_PARAM(p->cParams.hashLog, opts, "hashlog", NUM2UINT);
345
364
  SETUP_PARAM(p->cParams.searchLog, opts, "searchlog", NUM2UINT);
346
365
  SETUP_PARAM(p->cParams.searchLength, opts, "searchlength", NUM2UINT);
366
+ SETUP_PARAM(p->cParams.targetLength, opts, "targetlength", NUM2UINT);
347
367
  SETUP_PARAM(p->cParams.strategy, opts, "strategy", NUM2UINT);
348
368
  #undef SETUP_PARAM
349
369
  }
@@ -352,10 +372,10 @@ encparams_init(int argc, VALUE argv[], VALUE v)
352
372
  }
353
373
 
354
374
  static VALUE
355
- encparams_init_copy(VALUE params, VALUE src)
375
+ params_init_copy(VALUE params, VALUE src)
356
376
  {
357
- ZSTD_parameters *a = getencparams(params);
358
- ZSTD_parameters *b = getencparams(src);
377
+ ZSTD_parameters *a = getparams(params);
378
+ ZSTD_parameters *b = getparams(src);
359
379
  rb_check_frozen(params);
360
380
  rb_obj_infect(params, src);
361
381
  memcpy(a, b, sizeof(*a));
@@ -363,9 +383,9 @@ encparams_init_copy(VALUE params, VALUE src)
363
383
  }
364
384
 
365
385
  //static VALUE
366
- //encparams_validate(VALUE v)
386
+ //params_validate(VALUE v)
367
387
  //{
368
- // ZSTD_validateParams(getencparams(v));
388
+ // ZSTD_validateParams(getparams(v));
369
389
  // return v;
370
390
  //}
371
391
 
@@ -373,28 +393,29 @@ encparams_init_copy(VALUE params, VALUE src)
373
393
  static VALUE \
374
394
  GETTER(VALUE v) \
375
395
  { \
376
- return UINT2NUM(getencparams(v)->cParams.FIELD); \
396
+ return UINT2NUM(getparams(v)->cParams.FIELD); \
377
397
  } \
378
398
  \
379
399
  static VALUE \
380
400
  SETTER(VALUE v, VALUE n) \
381
401
  { \
382
- getencparams(v)->cParams.FIELD = NUM2UINT(n); \
402
+ getparams(v)->cParams.FIELD = NUM2UINT(n); \
383
403
  return n; \
384
404
  } \
385
405
 
386
- //IMP_PARAMS(encparams_srcsize, encparams_set_srcsize, srcSize);
387
- IMP_PARAMS(encparams_windowlog, encparams_set_windowlog, windowLog);
388
- IMP_PARAMS(encparams_chainlog, encparams_set_chainlog, chainLog);
389
- IMP_PARAMS(encparams_hashlog, encparams_set_hashlog, hashLog);
390
- IMP_PARAMS(encparams_searchlog, encparams_set_searchlog, searchLog);
391
- IMP_PARAMS(encparams_searchlength, encparams_set_searchlength, searchLength);
392
- IMP_PARAMS(encparams_strategy, encparams_set_strategy, strategy);
406
+ //IMP_PARAMS(params_srcsize, params_set_srcsize, srcSize);
407
+ IMP_PARAMS(params_windowlog, params_set_windowlog, windowLog);
408
+ IMP_PARAMS(params_chainlog, params_set_chainlog, chainLog);
409
+ IMP_PARAMS(params_hashlog, params_set_hashlog, hashLog);
410
+ IMP_PARAMS(params_searchlog, params_set_searchlog, searchLog);
411
+ IMP_PARAMS(params_searchlength, params_set_searchlength, searchLength);
412
+ IMP_PARAMS(params_targetlength, params_set_targetlength, targetLength);
413
+ IMP_PARAMS(params_strategy, params_set_strategy, strategy);
393
414
 
394
415
  #undef IMP_PARAMS
395
416
 
396
417
  static VALUE
397
- encparams_s_get_preset(int argc, VALUE argv[], VALUE mod)
418
+ params_s_get_preset(int argc, VALUE argv[], VALUE mod)
398
419
  {
399
420
  int level;
400
421
  uint64_t sizehint;
@@ -426,66 +447,72 @@ encparams_s_get_preset(int argc, VALUE argv[], VALUE mod)
426
447
  }
427
448
 
428
449
  ZSTD_parameters *p;
429
- VALUE v = TypedData_Make_Struct(mod, ZSTD_parameters, &encparams_type, p);
450
+ VALUE v = TypedData_Make_Struct(mod, ZSTD_parameters, &params_type, p);
430
451
  *p = ZSTD_getParams(level, sizehint, dictsize);
431
452
  return v;
432
453
  }
433
454
 
434
455
  /*
435
- * Document-method: Zstd::EncodeParameters#windowlog
436
- * Document-method: Zstd::EncodeParameters#windowlog=
437
- * Document-method: Zstd::EncodeParameters#chainlog
438
- * Document-method: Zstd::EncodeParameters#chainlog=
439
- * Document-method: Zstd::EncodeParameters#hashlog
440
- * Document-method: Zstd::EncodeParameters#hashlog=
441
- * Document-method: Zstd::EncodeParameters#searchlog
442
- * Document-method: Zstd::EncodeParameters#searchlog=
443
- * Document-method: Zstd::EncodeParameters#searchlength
444
- * Document-method: Zstd::EncodeParameters#searchlength=
445
- * Document-method: Zstd::EncodeParameters#strategy
446
- * Document-method: Zstd::EncodeParameters#strategy=
456
+ * Document-method: Zstd::Parameters#windowlog
457
+ * Document-method: Zstd::Parameters#windowlog=
458
+ * Document-method: Zstd::Parameters#chainlog
459
+ * Document-method: Zstd::Parameters#chainlog=
460
+ * Document-method: Zstd::Parameters#hashlog
461
+ * Document-method: Zstd::Parameters#hashlog=
462
+ * Document-method: Zstd::Parameters#searchlog
463
+ * Document-method: Zstd::Parameters#searchlog=
464
+ * Document-method: Zstd::Parameters#searchlength
465
+ * Document-method: Zstd::Parameters#searchlength=
466
+ * Document-method: Zstd::Parameters#targetlength
467
+ * Document-method: Zstd::Parameters#targetlength=
468
+ * Document-method: Zstd::Parameters#strategy
469
+ * Document-method: Zstd::Parameters#strategy=
447
470
  *
448
471
  * Get/Set any field from/to struct ZSTD_parameters of C layer.
449
472
  */
450
473
 
451
474
  static void
452
- init_encparams(void)
475
+ init_params(void)
453
476
  {
454
- extzstd_cParams = rb_define_class_under(extzstd_mZstd, "EncodeParameters", rb_cObject);
455
- rb_define_alloc_func(extzstd_cParams, encparams_alloc);
456
- rb_define_method(extzstd_cParams, "initialize", RUBY_METHOD_FUNC(encparams_init), -1);
457
- rb_define_method(extzstd_cParams, "initialize_copy", RUBY_METHOD_FUNC(encparams_init_copy), 1);
458
- //rb_define_method(extzstd_cParams, "validate", RUBY_METHOD_FUNC(encparams_validate), 0);
459
- //rb_define_method(extzstd_cParams, "srcsize", RUBY_METHOD_FUNC(encparams_srcsize), 0);
460
- //rb_define_method(extzstd_cParams, "srcsize=", RUBY_METHOD_FUNC(encparams_set_srcsize), 1);
461
- rb_define_method(extzstd_cParams, "windowlog", RUBY_METHOD_FUNC(encparams_windowlog), 0);
462
- rb_define_method(extzstd_cParams, "windowlog=", RUBY_METHOD_FUNC(encparams_set_windowlog), 1);
463
- rb_define_method(extzstd_cParams, "chainlog", RUBY_METHOD_FUNC(encparams_chainlog), 0);
464
- rb_define_method(extzstd_cParams, "chainlog=", RUBY_METHOD_FUNC(encparams_set_chainlog), 1);
465
- rb_define_method(extzstd_cParams, "hashlog", RUBY_METHOD_FUNC(encparams_hashlog), 0);
466
- rb_define_method(extzstd_cParams, "hashlog=", RUBY_METHOD_FUNC(encparams_set_hashlog), 1);
467
- rb_define_method(extzstd_cParams, "searchlog", RUBY_METHOD_FUNC(encparams_searchlog), 0);
468
- rb_define_method(extzstd_cParams, "searchlog=", RUBY_METHOD_FUNC(encparams_set_searchlog), 1);
469
- rb_define_method(extzstd_cParams, "searchlength", RUBY_METHOD_FUNC(encparams_searchlength), 0);
470
- rb_define_method(extzstd_cParams, "searchlength=", RUBY_METHOD_FUNC(encparams_set_searchlength), 1);
471
- rb_define_method(extzstd_cParams, "strategy", RUBY_METHOD_FUNC(encparams_strategy), 0);
472
- rb_define_method(extzstd_cParams, "strategy=", RUBY_METHOD_FUNC(encparams_set_strategy), 1);
473
-
474
- rb_define_singleton_method(extzstd_cParams, "preset", RUBY_METHOD_FUNC(encparams_s_get_preset), -1);
477
+ extzstd_cParams = rb_define_class_under(extzstd_mZstd, "Parameters", rb_cObject);
478
+ rb_define_alloc_func(extzstd_cParams, params_alloc);
479
+ rb_define_method(extzstd_cParams, "initialize", RUBY_METHOD_FUNC(params_init), -1);
480
+ rb_define_method(extzstd_cParams, "initialize_copy", RUBY_METHOD_FUNC(params_init_copy), 1);
481
+ //rb_define_method(extzstd_cParams, "validate", RUBY_METHOD_FUNC(params_validate), 0);
482
+ //rb_define_method(extzstd_cParams, "srcsize", RUBY_METHOD_FUNC(params_srcsize), 0);
483
+ //rb_define_method(extzstd_cParams, "srcsize=", RUBY_METHOD_FUNC(params_set_srcsize), 1);
484
+ rb_define_method(extzstd_cParams, "windowlog", RUBY_METHOD_FUNC(params_windowlog), 0);
485
+ rb_define_method(extzstd_cParams, "windowlog=", RUBY_METHOD_FUNC(params_set_windowlog), 1);
486
+ rb_define_method(extzstd_cParams, "chainlog", RUBY_METHOD_FUNC(params_chainlog), 0);
487
+ rb_define_method(extzstd_cParams, "chainlog=", RUBY_METHOD_FUNC(params_set_chainlog), 1);
488
+ rb_define_method(extzstd_cParams, "hashlog", RUBY_METHOD_FUNC(params_hashlog), 0);
489
+ rb_define_method(extzstd_cParams, "hashlog=", RUBY_METHOD_FUNC(params_set_hashlog), 1);
490
+ rb_define_method(extzstd_cParams, "searchlog", RUBY_METHOD_FUNC(params_searchlog), 0);
491
+ rb_define_method(extzstd_cParams, "searchlog=", RUBY_METHOD_FUNC(params_set_searchlog), 1);
492
+ rb_define_method(extzstd_cParams, "searchlength", RUBY_METHOD_FUNC(params_searchlength), 0);
493
+ rb_define_method(extzstd_cParams, "searchlength=", RUBY_METHOD_FUNC(params_set_searchlength), 1);
494
+ rb_define_method(extzstd_cParams, "targetlength", RUBY_METHOD_FUNC(params_targetlength), 0);
495
+ rb_define_method(extzstd_cParams, "targetlength=", RUBY_METHOD_FUNC(params_set_targetlength), 1);
496
+ rb_define_method(extzstd_cParams, "strategy", RUBY_METHOD_FUNC(params_strategy), 0);
497
+ rb_define_method(extzstd_cParams, "strategy=", RUBY_METHOD_FUNC(params_set_strategy), 1);
498
+
499
+ rb_define_singleton_method(extzstd_cParams, "preset", RUBY_METHOD_FUNC(params_s_get_preset), -1);
475
500
  rb_define_alias(rb_singleton_class(extzstd_cParams), "[]", "preset");
476
501
  }
477
502
 
478
503
 
479
504
  /*
480
- * zstd dictionary utilities
505
+ * module Zstd::Dictionary
481
506
  */
482
507
 
508
+ static VALUE mDictionary;
509
+
483
510
  /*
484
511
  * call-seq:
485
- * dict_train_from_buffer(src, dict_capacity) -> dictionary'd string
512
+ * train_from_buffer(src, dict_capacity) -> dictionary'd string
486
513
  */
487
514
  static VALUE
488
- ext_s_dict_train_from_buffer(VALUE mod, VALUE src, VALUE dict_capacity)
515
+ dict_s_train_from_buffer(VALUE mod, VALUE src, VALUE dict_capacity)
489
516
  {
490
517
  rb_check_type(src, RUBY_T_STRING);
491
518
  size_t capa = NUM2SIZET(dict_capacity);
@@ -499,14 +526,15 @@ ext_s_dict_train_from_buffer(VALUE mod, VALUE src, VALUE dict_capacity)
499
526
 
500
527
  /*
501
528
  * call-seq:
502
- * dict_add_entropy_tables_from_buffer(dict, dict_capacity, sample) -> dict
529
+ * add_entropy_tables_from_buffer(dict, dict_capacity, sample) -> dict
503
530
  */
504
531
  static VALUE
505
- ext_s_dict_add_entropy_tables_from_buffer(VALUE mod, VALUE dict, VALUE dict_capacity, VALUE sample)
532
+ dict_s_add_entropy_tables_from_buffer(VALUE mod, VALUE dict, VALUE dict_capacity, VALUE sample)
506
533
  {
507
534
  /*
508
- * size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
509
- * const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
535
+ * size_t ZDICT_addEntropyTablesFromBuffer(
536
+ * void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
537
+ * const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
510
538
  */
511
539
 
512
540
  rb_check_type(dict, RUBY_T_STRING);
@@ -514,19 +542,147 @@ ext_s_dict_add_entropy_tables_from_buffer(VALUE mod, VALUE dict, VALUE dict_capa
514
542
  size_t capa = NUM2SIZET(dict_capacity);
515
543
  aux_str_modify_expand(dict, capa);
516
544
  size_t samplesize = RSTRING_LEN(sample);
517
- size_t s = ZDICT_addEntropyTablesFromBuffer(RSTRING_PTR(dict), RSTRING_LEN(dict), capa, RSTRING_PTR(dict), &samplesize, 1);
545
+ size_t s = ZDICT_addEntropyTablesFromBuffer(RSTRING_PTR(dict), RSTRING_LEN(dict), capa, RSTRING_PTR(sample), &samplesize, 1);
518
546
  extzstd_check_error(s);
519
547
  rb_str_set_len(dict, s);
520
548
  return dict;
521
549
  }
522
550
 
551
+ static VALUE
552
+ dict_s_getid(VALUE mod, VALUE dict)
553
+ {
554
+ /*
555
+ * ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize);
556
+ */
557
+
558
+ rb_check_type(dict, RUBY_T_STRING);
559
+ const char *p;
560
+ size_t psize;
561
+ RSTRING_GETMEM(dict, p, psize);
562
+
563
+ size_t s = ZDICT_getDictID(p, psize);
564
+ extzstd_check_error(s);
565
+
566
+ return SIZET2NUM(s);
567
+ }
568
+
523
569
  static void
524
570
  init_dictionary(void)
525
571
  {
526
- rb_define_singleton_method(extzstd_mZstd, "dict_train_from_buffer", ext_s_dict_train_from_buffer, 2);
527
- rb_define_singleton_method(extzstd_mZstd, "dict_add_entropy_tables_from_buffer", ext_s_dict_add_entropy_tables_from_buffer, 3);
572
+ mDictionary = rb_define_module_under(extzstd_mZstd, "Dictionary");
573
+ rb_define_singleton_method(mDictionary, "train_from_buffer", dict_s_train_from_buffer, 2);
574
+ rb_define_singleton_method(mDictionary, "add_entropy_tables_from_buffer", dict_s_add_entropy_tables_from_buffer, 3);
575
+ rb_define_singleton_method(mDictionary, "getid", dict_s_getid, 1);
576
+ }
577
+
578
+ /*
579
+ * module Zstd::ContextLess
580
+ */
581
+
582
+ static VALUE mContextLess;
583
+
584
+ /*
585
+ * call-seq:
586
+ * encode(src, dest, maxdest, params)
587
+ *
588
+ * [RETURN] dest
589
+ * [src (string)]
590
+ * [dest (string)]
591
+ * [maxdest (integer or nil)]
592
+ * [params (nil, integer or Zstd::Parameters)]
593
+ */
594
+ static VALUE
595
+ less_s_encode(VALUE mod, VALUE src, VALUE dest, VALUE maxdest, VALUE predict, VALUE params)
596
+ {
597
+ const char *q;
598
+ size_t qsize;
599
+ aux_string_pointer(src, &q, &qsize);
600
+
601
+ char *r;
602
+ size_t rsize = (NIL_P(maxdest)) ? ZSTD_compressBound(qsize) : NUM2SIZET(maxdest);
603
+ aux_string_expand_pointer(dest, &r, rsize);
604
+ rb_obj_infect(dest, src);
605
+
606
+ const char *d;
607
+ size_t dsize;
608
+ aux_string_pointer_with_nil(predict, &d, &dsize);
609
+ rb_obj_infect(dest, predict);
610
+
611
+ if (extzstd_params_p(params)) {
612
+ /*
613
+ * ZSTDLIB_API size_t ZSTD_compress_advanced(
614
+ * ZSTD_CCtx* ctx,
615
+ * void* dst, size_t dstCapacity,
616
+ * const void* src, size_t srcSize,
617
+ * const void* dict,size_t dictSize,
618
+ * ZSTD_parameters params);
619
+ */
620
+ ZSTD_CCtx *zstd = ZSTD_createCCtx();
621
+ size_t s = ZSTD_compress_advanced(zstd, r, rsize, q, qsize, d, dsize, *extzstd_getparams(params));
622
+ ZSTD_freeCCtx(zstd);
623
+ extzstd_check_error(s);
624
+ rb_str_set_len(dest, s);
625
+ return dest;
626
+ } else {
627
+ /*
628
+ * ZSTDLIB_API size_t ZSTD_compress_usingDict(
629
+ * ZSTD_CCtx* ctx,
630
+ * void* dst, size_t dstCapacity,
631
+ * const void* src, size_t srcSize,
632
+ * const void* dict,size_t dictSize,
633
+ * int compressionLevel);
634
+ */
635
+ ZSTD_CCtx *zstd = ZSTD_createCCtx();
636
+ size_t s = ZSTD_compress_usingDict(zstd, r, rsize, q, qsize, d, dsize, aux_num2int(params, 0));
637
+ ZSTD_freeCCtx(zstd);
638
+ extzstd_check_error(s);
639
+ rb_str_set_len(dest, s);
640
+ return dest;
641
+ }
642
+ }
643
+
644
+ /*
645
+ * call-seq:
646
+ * decode(src, dest, maxdest)
647
+ *
648
+ * [RETURN] dest
649
+ * [src (string)]
650
+ * [dest (string)]
651
+ * [maxdest (integer or nil)]
652
+ */
653
+ static VALUE
654
+ less_s_decode(VALUE mod, VALUE src, VALUE dest, VALUE maxdest, VALUE predict)
655
+ {
656
+ const char *q;
657
+ size_t qsize;
658
+ aux_string_pointer(src, &q, &qsize);
659
+
660
+ char *r;
661
+ size_t rsize = (NIL_P(maxdest)) ? ZSTD_getDecompressedSize(q, qsize) : NUM2SIZET(maxdest);
662
+ aux_string_expand_pointer(dest, &r, rsize);
663
+ rb_obj_infect(dest, src);
664
+
665
+ const char *d;
666
+ size_t dsize;
667
+ aux_string_pointer_with_nil(predict, &d, &dsize);
668
+ rb_obj_infect(dest, predict);
669
+
670
+ ZSTD_DCtx *z = ZSTD_createDCtx();
671
+ size_t s = ZSTD_decompress_usingDict(z, r, rsize, q, qsize, d, dsize);
672
+ ZSTD_freeDCtx(z);
673
+ extzstd_check_error(s);
674
+ rb_str_set_len(dest, s);
675
+
676
+ return dest;
528
677
  }
529
678
 
679
+ static void
680
+ init_contextless(void)
681
+ {
682
+ mContextLess = rb_define_module_under(extzstd_mZstd, "ContextLess");
683
+ rb_define_singleton_method(mContextLess, "encode", less_s_encode, 5);
684
+ rb_define_singleton_method(mContextLess, "decode", less_s_decode, 4);
685
+ }
530
686
 
531
687
  /*
532
688
  * library initializer
@@ -540,7 +696,8 @@ Init_extzstd(void)
540
696
  init_libver();
541
697
  init_error();
542
698
  init_constants();
543
- init_encparams();
699
+ init_params();
544
700
  init_dictionary();
545
- extzstd_init_buffered();
701
+ init_contextless();
702
+ extzstd_init_stream();
546
703
  }
data/ext/extzstd.h CHANGED
@@ -2,8 +2,9 @@
2
2
  #define EXTZSTD_H 1
3
3
 
4
4
  #define ZSTD_LEGACY_SUPPORT 1
5
- #define ZBUFF_STATIC_LINKING_ONLY 1
6
- #include <zbuff.h>
5
+ #define ZDICT_STATIC_LINKING_ONLY 1
6
+ #define ZSTD_STATIC_LINKING_ONLY 1
7
+ #include <zstd.h>
7
8
  #include <stdarg.h>
8
9
  #include <ruby.h>
9
10
  #include <ruby/thread.h>
@@ -23,7 +24,7 @@ extern VALUE extzstd_mZstd;
23
24
  RDOCFAKE(extzstd_mZstd = rb_define_module("Zstd"));
24
25
 
25
26
  extern VALUE extzstd_cParams;
26
- RDOCFAKE(extzstd_cParams = rb_define_class_under(extzstd_mZstd, "EncodeParameters", rb_cObject));
27
+ RDOCFAKE(extzstd_cParams = rb_define_class_under(extzstd_mZstd, "Parameters", rb_cObject));
27
28
 
28
29
  extern VALUE extzstd_mExceptions;
29
30
  extern VALUE extzstd_eError;
@@ -48,13 +49,14 @@ extern VALUE extzstd_eDictionaryWrongError;
48
49
 
49
50
  extern void init_extzstd_stream(void);
50
51
  extern void extzstd_init_buffered(void);
52
+ extern void extzstd_init_stream(void);
51
53
  extern void extzstd_error(ssize_t errcode);
52
54
  extern void extzstd_check_error(ssize_t errcode);
53
55
  extern VALUE extzstd_make_error(ssize_t errcode);
54
56
  extern VALUE extzstd_make_errorf(VALUE exc, const char *fmt, ...);
55
57
 
56
- extern ZSTD_parameters *extzstd_getencparams(VALUE v);
57
- extern int extzstd_encparams_p(VALUE v);
58
+ extern ZSTD_parameters *extzstd_getparams(VALUE v);
59
+ extern int extzstd_params_p(VALUE v);
58
60
  extern VALUE extzstd_params_alloc(ZSTD_parameters **p);
59
61
 
60
62
  static inline void
@@ -161,7 +163,7 @@ aux_num2int_u64(VALUE v, uint64_t default_value)
161
163
  }
162
164
 
163
165
  static inline VALUE
164
- aux_const_dig_str_0(VALUE obj, const char *p[], const char *pp)
166
+ aux_const_dig_str_0(VALUE obj, const char *p[], const char **pp)
165
167
  {
166
168
  for (; p < pp; p ++) {
167
169
  obj = rb_const_get(obj, rb_intern(*p));