extzstd 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.ja.md +13 -0
  3. data/README.md +17 -14
  4. data/contrib/zstd/{NEWS → CHANGELOG} +115 -2
  5. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  6. data/contrib/zstd/Makefile +99 -53
  7. data/contrib/zstd/README.md +59 -39
  8. data/contrib/zstd/TESTING.md +1 -1
  9. data/contrib/zstd/appveyor.yml +17 -6
  10. data/contrib/zstd/lib/BUCK +29 -2
  11. data/contrib/zstd/lib/Makefile +118 -21
  12. data/contrib/zstd/lib/README.md +84 -44
  13. data/contrib/zstd/lib/common/bitstream.h +17 -33
  14. data/contrib/zstd/lib/common/compiler.h +62 -8
  15. data/contrib/zstd/lib/common/cpu.h +215 -0
  16. data/contrib/zstd/lib/common/debug.c +44 -0
  17. data/contrib/zstd/lib/common/debug.h +134 -0
  18. data/contrib/zstd/lib/common/entropy_common.c +16 -1
  19. data/contrib/zstd/lib/common/error_private.c +7 -0
  20. data/contrib/zstd/lib/common/fse.h +48 -44
  21. data/contrib/zstd/lib/common/fse_decompress.c +3 -3
  22. data/contrib/zstd/lib/common/huf.h +169 -113
  23. data/contrib/zstd/lib/common/mem.h +20 -2
  24. data/contrib/zstd/lib/common/pool.c +135 -49
  25. data/contrib/zstd/lib/common/pool.h +40 -21
  26. data/contrib/zstd/lib/common/threading.c +2 -2
  27. data/contrib/zstd/lib/common/threading.h +12 -12
  28. data/contrib/zstd/lib/common/xxhash.c +3 -2
  29. data/contrib/zstd/lib/common/zstd_common.c +3 -6
  30. data/contrib/zstd/lib/common/zstd_errors.h +17 -7
  31. data/contrib/zstd/lib/common/zstd_internal.h +76 -48
  32. data/contrib/zstd/lib/compress/fse_compress.c +89 -209
  33. data/contrib/zstd/lib/compress/hist.c +203 -0
  34. data/contrib/zstd/lib/compress/hist.h +95 -0
  35. data/contrib/zstd/lib/compress/huf_compress.c +188 -80
  36. data/contrib/zstd/lib/compress/zstd_compress.c +2500 -1203
  37. data/contrib/zstd/lib/compress/zstd_compress_internal.h +463 -62
  38. data/contrib/zstd/lib/compress/zstd_double_fast.c +321 -131
  39. data/contrib/zstd/lib/compress/zstd_double_fast.h +13 -4
  40. data/contrib/zstd/lib/compress/zstd_fast.c +335 -108
  41. data/contrib/zstd/lib/compress/zstd_fast.h +12 -6
  42. data/contrib/zstd/lib/compress/zstd_lazy.c +654 -313
  43. data/contrib/zstd/lib/compress/zstd_lazy.h +44 -16
  44. data/contrib/zstd/lib/compress/zstd_ldm.c +310 -420
  45. data/contrib/zstd/lib/compress/zstd_ldm.h +63 -26
  46. data/contrib/zstd/lib/compress/zstd_opt.c +773 -325
  47. data/contrib/zstd/lib/compress/zstd_opt.h +31 -5
  48. data/contrib/zstd/lib/compress/zstdmt_compress.c +1468 -518
  49. data/contrib/zstd/lib/compress/zstdmt_compress.h +96 -45
  50. data/contrib/zstd/lib/decompress/huf_decompress.c +518 -282
  51. data/contrib/zstd/lib/decompress/zstd_ddict.c +240 -0
  52. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  53. data/contrib/zstd/lib/decompress/zstd_decompress.c +613 -1513
  54. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1311 -0
  55. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  56. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +175 -0
  57. data/contrib/zstd/lib/dictBuilder/cover.c +194 -113
  58. data/contrib/zstd/lib/dictBuilder/cover.h +112 -0
  59. data/contrib/zstd/lib/dictBuilder/divsufsort.c +3 -3
  60. data/contrib/zstd/lib/dictBuilder/fastcover.c +740 -0
  61. data/contrib/zstd/lib/dictBuilder/zdict.c +142 -106
  62. data/contrib/zstd/lib/dictBuilder/zdict.h +115 -49
  63. data/contrib/zstd/lib/legacy/zstd_legacy.h +44 -12
  64. data/contrib/zstd/lib/legacy/zstd_v01.c +41 -10
  65. data/contrib/zstd/lib/legacy/zstd_v01.h +12 -7
  66. data/contrib/zstd/lib/legacy/zstd_v02.c +37 -12
  67. data/contrib/zstd/lib/legacy/zstd_v02.h +12 -7
  68. data/contrib/zstd/lib/legacy/zstd_v03.c +38 -12
  69. data/contrib/zstd/lib/legacy/zstd_v03.h +12 -7
  70. data/contrib/zstd/lib/legacy/zstd_v04.c +55 -174
  71. data/contrib/zstd/lib/legacy/zstd_v04.h +12 -7
  72. data/contrib/zstd/lib/legacy/zstd_v05.c +59 -31
  73. data/contrib/zstd/lib/legacy/zstd_v05.h +12 -7
  74. data/contrib/zstd/lib/legacy/zstd_v06.c +48 -20
  75. data/contrib/zstd/lib/legacy/zstd_v06.h +10 -5
  76. data/contrib/zstd/lib/legacy/zstd_v07.c +62 -29
  77. data/contrib/zstd/lib/legacy/zstd_v07.h +10 -5
  78. data/contrib/zstd/lib/zstd.h +1346 -832
  79. data/ext/extzstd.c +27 -19
  80. data/ext/extzstd_stream.c +20 -4
  81. data/ext/zstd_compress.c +1 -0
  82. data/ext/zstd_decompress.c +4 -0
  83. data/ext/zstd_dictbuilder.c +4 -0
  84. data/ext/zstd_dictbuilder_fastcover.c +5 -0
  85. data/lib/extzstd.rb +52 -220
  86. data/lib/extzstd/version.rb +1 -1
  87. metadata +21 -7
  88. data/contrib/zstd/circle.yml +0 -63
@@ -79,13 +79,15 @@ extzstd_make_errorf(ssize_t errcode, const char *fmt, ...)
79
79
  VALUE e;
80
80
 
81
81
  if (fmt && strlen(fmt) > 0) {
82
+ VALUE args[] = { SSIZET2NUM(errcode), Qnil };
82
83
  va_list va;
83
84
  va_start(va, fmt);
84
- VALUE mesg = rb_vsprintf(fmt, va);
85
+ args[1] = rb_vsprintf(fmt, va);
85
86
  va_end(va);
86
- return AUX_FUNCALL(extzstd_eError, id_initialize, SSIZET2NUM(errcode), mesg);
87
+ return rb_class_new_instance(2, args, extzstd_eError);
87
88
  } else {
88
- return AUX_FUNCALL(extzstd_eError, id_initialize, SSIZET2NUM(errcode));
89
+ VALUE args[] = { SSIZET2NUM(errcode) };
90
+ return rb_class_new_instance(1, args, extzstd_eError);
89
91
  }
90
92
  }
91
93
 
@@ -158,6 +160,7 @@ init_constants(void)
158
160
  rb_define_const(mConstants, "ZSTD_BTLAZY2", INT2NUM(ZSTD_btlazy2));
159
161
  rb_define_const(mConstants, "ZSTD_BTOPT", INT2NUM(ZSTD_btopt));
160
162
  rb_define_const(mConstants, "ZSTD_BTULTRA", INT2NUM(ZSTD_btultra));
163
+ rb_define_const(mConstants, "ZSTD_BTULTRA2", INT2NUM(ZSTD_btultra2));
161
164
  rb_define_const(mConstants, "ZSTD_WINDOWLOG_MAX", INT2NUM(ZSTD_WINDOWLOG_MAX));
162
165
  rb_define_const(mConstants, "ZSTD_WINDOWLOG_MIN", INT2NUM(ZSTD_WINDOWLOG_MIN));
163
166
  rb_define_const(mConstants, "ZSTD_HASHLOG_MAX", INT2NUM(ZSTD_HASHLOG_MAX));
@@ -167,10 +170,10 @@ init_constants(void)
167
170
  rb_define_const(mConstants, "ZSTD_HASHLOG3_MAX", INT2NUM(ZSTD_HASHLOG3_MAX));
168
171
  rb_define_const(mConstants, "ZSTD_SEARCHLOG_MAX", INT2NUM(ZSTD_SEARCHLOG_MAX));
169
172
  rb_define_const(mConstants, "ZSTD_SEARCHLOG_MIN", INT2NUM(ZSTD_SEARCHLOG_MIN));
170
- rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
171
- rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
172
- rb_define_const(mConstants, "ZSTD_TARGETLENGTH_MAX", INT2NUM(ZSTD_TARGETLENGTH_MAX));
173
- rb_define_const(mConstants, "ZSTD_TARGETLENGTH_MIN", INT2NUM(ZSTD_TARGETLENGTH_MIN));
173
+ //rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
174
+ //rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
175
+ //rb_define_const(mConstants, "ZSTD_TARGETLENGTH_MAX", INT2NUM(ZSTD_TARGETLENGTH_MAX));
176
+ //rb_define_const(mConstants, "ZSTD_TARGETLENGTH_MIN", INT2NUM(ZSTD_TARGETLENGTH_MIN));
174
177
 
175
178
  rb_define_const(mConstants, "FAST", INT2NUM(ZSTD_fast));
176
179
  rb_define_const(mConstants, "DFAST", INT2NUM(ZSTD_dfast));
@@ -180,6 +183,7 @@ init_constants(void)
180
183
  rb_define_const(mConstants, "BTLAZY2", INT2NUM(ZSTD_btlazy2));
181
184
  rb_define_const(mConstants, "BTOPT", INT2NUM(ZSTD_btopt));
182
185
  rb_define_const(mConstants, "BTULTRA", INT2NUM(ZSTD_btultra));
186
+ rb_define_const(mConstants, "BTULTRA2", INT2NUM(ZSTD_btultra2));
183
187
  rb_define_const(mConstants, "WINDOWLOG_MAX", INT2NUM(ZSTD_WINDOWLOG_MAX));
184
188
  rb_define_const(mConstants, "WINDOWLOG_MIN", INT2NUM(ZSTD_WINDOWLOG_MIN));
185
189
  rb_define_const(mConstants, "HASHLOG_MAX", INT2NUM(ZSTD_HASHLOG_MAX));
@@ -189,10 +193,10 @@ init_constants(void)
189
193
  rb_define_const(mConstants, "HASHLOG3_MAX", INT2NUM(ZSTD_HASHLOG3_MAX));
190
194
  rb_define_const(mConstants, "SEARCHLOG_MAX", INT2NUM(ZSTD_SEARCHLOG_MAX));
191
195
  rb_define_const(mConstants, "SEARCHLOG_MIN", INT2NUM(ZSTD_SEARCHLOG_MIN));
192
- rb_define_const(mConstants, "SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
193
- rb_define_const(mConstants, "SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
194
- rb_define_const(mConstants, "TARGETLENGTH_MAX", INT2NUM(ZSTD_TARGETLENGTH_MAX));
195
- rb_define_const(mConstants, "TARGETLENGTH_MIN", INT2NUM(ZSTD_TARGETLENGTH_MIN));
196
+ //rb_define_const(mConstants, "SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
197
+ //rb_define_const(mConstants, "SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
198
+ //rb_define_const(mConstants, "TARGETLENGTH_MAX", INT2NUM(ZSTD_TARGETLENGTH_MAX));
199
+ //rb_define_const(mConstants, "TARGETLENGTH_MIN", INT2NUM(ZSTD_TARGETLENGTH_MIN));
196
200
  }
197
201
 
198
202
  /*
@@ -243,7 +247,7 @@ extzstd_params_alloc(ZSTD_parameters **p)
243
247
  * [opts contentlog: nil]
244
248
  * [opts hashlog: nil]
245
249
  * [opts searchlog: nil]
246
- * [opts searchlength: nil]
250
+ * [opts minmatch: nil]
247
251
  * [opts targetlength: nil]
248
252
  * [opts strategy: nil]
249
253
  */
@@ -276,9 +280,12 @@ params_init(int argc, VALUE argv[], VALUE v)
276
280
  SETUP_PARAM(p->cParams.chainLog, opts, "chainlog", NUM2UINT);
277
281
  SETUP_PARAM(p->cParams.hashLog, opts, "hashlog", NUM2UINT);
278
282
  SETUP_PARAM(p->cParams.searchLog, opts, "searchlog", NUM2UINT);
279
- SETUP_PARAM(p->cParams.searchLength, opts, "searchlength", NUM2UINT);
283
+ SETUP_PARAM(p->cParams.minMatch, opts, "minmatch", NUM2UINT);
280
284
  SETUP_PARAM(p->cParams.targetLength, opts, "targetlength", NUM2UINT);
281
285
  SETUP_PARAM(p->cParams.strategy, opts, "strategy", NUM2UINT);
286
+ SETUP_PARAM(p->fParams.contentSizeFlag, opts, "contentsize", RTEST);
287
+ SETUP_PARAM(p->fParams.checksumFlag, opts, "checksum", RTEST);
288
+ SETUP_PARAM(p->fParams.noDictIDFlag, opts, "nodictid", RTEST);
282
289
  #undef SETUP_PARAM
283
290
  }
284
291
 
@@ -322,7 +329,7 @@ IMP_PARAMS(params_windowlog, params_set_windowlog, windowLog);
322
329
  IMP_PARAMS(params_chainlog, params_set_chainlog, chainLog);
323
330
  IMP_PARAMS(params_hashlog, params_set_hashlog, hashLog);
324
331
  IMP_PARAMS(params_searchlog, params_set_searchlog, searchLog);
325
- IMP_PARAMS(params_searchlength, params_set_searchlength, searchLength);
332
+ IMP_PARAMS(params_minmatch, params_set_minmatch, minMatch);
326
333
  IMP_PARAMS(params_targetlength, params_set_targetlength, targetLength);
327
334
  IMP_PARAMS(params_strategy, params_set_strategy, strategy);
328
335
 
@@ -375,8 +382,8 @@ params_s_get_preset(int argc, VALUE argv[], VALUE mod)
375
382
  * Document-method: Zstd::Parameters#hashlog=
376
383
  * Document-method: Zstd::Parameters#searchlog
377
384
  * Document-method: Zstd::Parameters#searchlog=
378
- * Document-method: Zstd::Parameters#searchlength
379
- * Document-method: Zstd::Parameters#searchlength=
385
+ * Document-method: Zstd::Parameters#minmatch
386
+ * Document-method: Zstd::Parameters#minmatch=
380
387
  * Document-method: Zstd::Parameters#targetlength
381
388
  * Document-method: Zstd::Parameters#targetlength=
382
389
  * Document-method: Zstd::Parameters#strategy
@@ -403,8 +410,8 @@ init_params(void)
403
410
  rb_define_method(extzstd_cParams, "hashlog=", RUBY_METHOD_FUNC(params_set_hashlog), 1);
404
411
  rb_define_method(extzstd_cParams, "searchlog", RUBY_METHOD_FUNC(params_searchlog), 0);
405
412
  rb_define_method(extzstd_cParams, "searchlog=", RUBY_METHOD_FUNC(params_set_searchlog), 1);
406
- rb_define_method(extzstd_cParams, "searchlength", RUBY_METHOD_FUNC(params_searchlength), 0);
407
- rb_define_method(extzstd_cParams, "searchlength=", RUBY_METHOD_FUNC(params_set_searchlength), 1);
413
+ rb_define_method(extzstd_cParams, "minmatch", RUBY_METHOD_FUNC(params_minmatch), 0);
414
+ rb_define_method(extzstd_cParams, "minmatch=", RUBY_METHOD_FUNC(params_set_minmatch), 1);
408
415
  rb_define_method(extzstd_cParams, "targetlength", RUBY_METHOD_FUNC(params_targetlength), 0);
409
416
  rb_define_method(extzstd_cParams, "targetlength=", RUBY_METHOD_FUNC(params_set_targetlength), 1);
410
417
  rb_define_method(extzstd_cParams, "strategy", RUBY_METHOD_FUNC(params_strategy), 0);
@@ -432,7 +439,8 @@ dict_s_train_from_buffer(VALUE mod, VALUE src, VALUE dict_capacity)
432
439
  size_t capa = NUM2SIZET(dict_capacity);
433
440
  VALUE dict = rb_str_buf_new(capa);
434
441
  size_t srcsize = RSTRING_LEN(src);
435
- size_t s = ZDICT_trainFromBuffer(RSTRING_PTR(dict), capa, RSTRING_PTR(src), &srcsize, 1);
442
+ const ZDICT_legacy_params_t params = { 0 };
443
+ size_t s = ZDICT_trainFromBuffer_legacy(RSTRING_PTR(dict), capa, RSTRING_PTR(src), &srcsize, 1, params);
436
444
  extzstd_check_error(s);
437
445
  rb_str_set_len(dict, s);
438
446
  return dict;
@@ -145,7 +145,7 @@ enc_init(int argc, VALUE argv[], VALUE self)
145
145
 
146
146
  if (extzstd_params_p(params)) {
147
147
  ZSTD_parameters *paramsp = extzstd_getparams(params);
148
- size_t s = ZSTD_initCStream_advanced(p->context, predictp, predictsize, *paramsp, 0);
148
+ size_t s = ZSTD_initCStream_advanced(p->context, predictp, predictsize, *paramsp, -1);
149
149
  extzstd_check_error(s);
150
150
  } else {
151
151
  size_t s = ZSTD_initCStream_usingDict(p->context, predictp, predictsize, aux_num2int(params, 1));
@@ -166,7 +166,7 @@ enc_write(VALUE self, VALUE src)
166
166
  */
167
167
 
168
168
  struct encoder *p = encoder_context(self);
169
- rb_check_type(src, RUBY_T_STRING);
169
+ src = rb_String(src);
170
170
  ZSTD_inBuffer input = { RSTRING_PTR(src), RSTRING_LEN(src), 0 };
171
171
 
172
172
  while (input.pos < input.size) {
@@ -463,7 +463,7 @@ dec_read_args(int argc, VALUE argv[], VALUE self, VALUE *buf, ssize_t *size)
463
463
  if (NIL_P(argv[0])) {
464
464
  *size = -1;
465
465
 
466
- if (argc == 1) {
466
+ if (argc == 1 || NIL_P(argv[1])) {
467
467
  *buf = rb_str_buf_new(EXT_READ_GROWUP_SIZE);
468
468
  } else {
469
469
  rb_check_type(argv[1], RUBY_T_STRING);
@@ -479,7 +479,7 @@ dec_read_args(int argc, VALUE argv[], VALUE self, VALUE *buf, ssize_t *size)
479
479
  (intptr_t)*size);
480
480
  }
481
481
 
482
- if (argc == 1) {
482
+ if (argc == 1 || NIL_P(argv[1])) {
483
483
  *buf = rb_str_buf_new(*size);
484
484
  } else {
485
485
  rb_check_type(argv[1], RUBY_T_STRING);
@@ -553,6 +553,13 @@ dec_eof(VALUE self)
553
553
  return (decoder_context(self)->reached_eof == 0 ? Qfalse : Qtrue);
554
554
  }
555
555
 
556
+ static VALUE
557
+ dec_close(VALUE self)
558
+ {
559
+ decoder_context(self)->reached_eof = 1;
560
+ return Qnil;
561
+ }
562
+
556
563
  static VALUE
557
564
  dec_reset(VALUE self)
558
565
  {
@@ -576,6 +583,13 @@ dec_sizeof(VALUE self)
576
583
  return SIZET2NUM(s);
577
584
  }
578
585
 
586
+ static VALUE
587
+ dec_pos(VALUE self)
588
+ {
589
+ decoder_context(self); /* check only */
590
+ return INT2FIX(0);
591
+ }
592
+
579
593
  static void
580
594
  init_decoder(void)
581
595
  {
@@ -587,8 +601,10 @@ init_decoder(void)
587
601
  rb_define_method(cStreamDecoder, "read", dec_read, -1);
588
602
  rb_define_method(cStreamDecoder, "eof", dec_eof, 0);
589
603
  rb_define_alias(cStreamDecoder, "eof?", "eof");
604
+ rb_define_method(cStreamDecoder, "close", dec_close, 0);
590
605
  rb_define_method(cStreamDecoder, "reset", dec_reset, 0);
591
606
  rb_define_method(cStreamDecoder, "sizeof", dec_sizeof, 0);
607
+ rb_define_method(cStreamDecoder, "pos", dec_pos, 0);
592
608
  }
593
609
 
594
610
  /*
@@ -12,3 +12,4 @@
12
12
  #include "../contrib/zstd/lib/compress/zstd_lazy.c"
13
13
  #include "../contrib/zstd/lib/compress/zstd_ldm.c"
14
14
  #include "../contrib/zstd/lib/compress/zstd_opt.c"
15
+ #include "../contrib/zstd/lib/compress/hist.c"
@@ -3,4 +3,8 @@
3
3
  #define visibility(v) visibility("hidden")
4
4
 
5
5
  #include "../contrib/zstd/lib/decompress/zstd_decompress.c"
6
+ #include "../contrib/zstd/lib/decompress/zstd_decompress_block.c"
7
+
8
+ #undef CHECK_F
6
9
  #include "../contrib/zstd/lib/decompress/huf_decompress.c"
10
+ #include "../contrib/zstd/lib/decompress/zstd_ddict.c"
@@ -4,4 +4,8 @@
4
4
 
5
5
  #include "../contrib/zstd/lib/dictBuilder/zdict.c"
6
6
  #include "../contrib/zstd/lib/dictBuilder/divsufsort.c"
7
+
8
+ #undef DISPLAY
9
+ #undef DISPLAYLEVEL
10
+ #undef DISPLAYUPDATE
7
11
  #include "../contrib/zstd/lib/dictBuilder/cover.c"
@@ -0,0 +1,5 @@
1
+ #define ZSTD_LEGACY_SUPPORT 1
2
+ #define MEM_MODULE 1
3
+ #define visibility(v) visibility("hidden")
4
+
5
+ #include "../contrib/zstd/lib/dictBuilder/fastcover.c"
@@ -12,6 +12,32 @@ require "stringio"
12
12
  # This is ruby bindings for zstd <https://github.com/Cyan4973/zstd> the compression library.
13
13
  #
14
14
  module Zstd
15
+ module Aux
16
+ EMPTY_BUFFER = "".force_encoding(Encoding::BINARY).freeze
17
+ end
18
+
19
+ refine String do
20
+ def to_zstd(params = nil, dict: nil)
21
+ ContextLess.encode(self, Aux::EMPTY_BUFFER.dup, nil, dict, params)
22
+ end
23
+
24
+ def unzstd(size = nil, dict: nil)
25
+ Decoder.open(self, dict) { |d| return d.read(size) }
26
+ end
27
+ end
28
+
29
+ refine Object do
30
+ def to_zstd(params = nil, dict: nil, &block)
31
+ Encoder.open(self, params, dict, &block)
32
+ end
33
+
34
+ def unzstd(dict: nil, &block)
35
+ Decoder.open(self, dict, &block)
36
+ end
37
+ end
38
+
39
+ using Zstd
40
+
15
41
  #
16
42
  # call-seq:
17
43
  # encode(src_string, level = nil, opts = {}) -> zstd string
@@ -26,12 +52,8 @@ module Zstd
26
52
  # [level = nil (integer or nil)]
27
53
  # [encode_params (instance of Zstd::Parameters)]
28
54
  # [opts dict: nil (string or nil)]
29
- def self.encode(src, params = nil, dict: nil, &block)
30
- if src.kind_of?(String)
31
- return ContextLess.encode(src, Aux::EMPTY_BUFFER.dup, nil, dict, params)
32
- end
33
-
34
- Encoder.open(src, params, dict, &block)
55
+ def self.encode(src, *args, &block)
56
+ src.to_zstd(*args, &block)
35
57
  end
36
58
 
37
59
  #
@@ -40,25 +62,17 @@ module Zstd
40
62
  # decode(zstd_stream, dict: nil) -> zstd decoder
41
63
  # decode(zstd_stream, dict: nil) { |decoder| ... } -> yield returned value
42
64
  #
43
- def self.decode(src, *args, dict: nil, &block)
44
- if src.kind_of?(String)
45
- case args.size
46
- when 0
47
- return ContextLess.decode(src, Aux::EMPTY_BUFFER.dup, nil, dict)
48
- when 1
49
- Decoder.open(src, dict) { |d| return d.read(args[0].to_i) }
50
- else
51
- raise ArgumentError, "wrong argument number (given #{args.size}, expect 1 or 2)"
52
- end
53
- end
54
-
55
- unless args.empty?
56
- raise ArgumentError, "wrong argument number (given #{args.size}, expect 1)"
57
- end
65
+ def self.decode(src, *args, &block)
66
+ src.unzstd(*args, &block)
67
+ end
58
68
 
59
- Decoder.open(src, dict, &block)
69
+ class << Zstd
70
+ alias compress encode
71
+ alias decompress decode
72
+ alias uncompress decode
60
73
  end
61
74
 
75
+ Compressor = Encoder
62
76
  StreamEncoder = Encoder
63
77
 
64
78
  class Encoder
@@ -75,82 +89,21 @@ module Zstd
75
89
  begin
76
90
  yield e
77
91
  ensure
78
- e.close rescue nil unless e.eof?
92
+ e.close unless e.eof?
79
93
  end
80
94
  end
81
- end
82
95
 
83
- class Encoder < Struct.new(:encoder, :outport, :destbuf, :status)
84
- #
85
- # call-seq:
86
- # open(outport, level = nil, dict = nil) -> zstd encoder
87
- # open(outport, encode_params, dict = nil) { |encoder| ... } -> yield returned value
88
- #
89
- def self.open(outport, *args)
90
- e = new(outport, *args)
91
-
92
- return e unless block_given?
93
-
94
- begin
95
- yield e
96
- ensure
97
- e.close rescue nil unless e.eof?
98
- end
96
+ def self.encode(src, params = nil, dest: nil, dict: nil)
97
+ ContextLess.encode(src, dest || Aux::EMPTY_BUFFER.dup, nil, dict, params)
99
98
  end
100
99
 
101
- #
102
- # call-seq:
103
- # initialize(outport, level = nil, dict = nil)
104
- # initialize(outport, encode_params, dict = nil)
105
- #
106
- # +outport+ need has +.<<+ method.
107
- #
108
- def initialize(outport, params = nil, dict = nil)
109
- encoder = StreamEncoder.new(params, dict)
110
- super encoder, outport, "".force_encoding(Encoding::BINARY), [true]
111
- end
112
-
113
- def eof
114
- !status[0]
115
- end
116
-
117
- alias eof? eof
118
-
119
- def close
120
- return nil if eof?
121
- encoder.end(destbuf, StreamEncoder::OUTSIZE)
122
- outport << destbuf
123
- status[0] = false
124
- nil
125
- end
126
-
127
- def write(buf)
128
- raise IOError, "closed stream" if eof?
129
-
130
- off = 0
131
- rest = buf.bytesize
132
- outsize = StreamEncoder::OUTSIZE
133
- while off && off < rest
134
- off = encoder.update(buf, off, destbuf, outsize)
135
- outport << destbuf
136
- end
137
-
138
- self
139
- end
140
-
141
- alias << write
142
-
143
- def flush
144
- raise IOError, "closed stream" if eof?
145
-
146
- off = 0
147
- encoder.flush(destbuf, StreamEncoder::OUTSIZE)
148
- outport << destbuf
149
-
150
- self
100
+ class << Encoder
101
+ alias compress encode
151
102
  end
152
- end if false
103
+ end
153
104
 
105
+ Uncompressor = Decoder
106
+ Decompressor = Decoder
154
107
  StreamDecoder = Decoder
155
108
 
156
109
  class Decoder
@@ -175,122 +128,19 @@ module Zstd
175
128
  dec.close rescue nil
176
129
  end
177
130
  end
178
- end
179
-
180
- class Decoder
181
- attr_reader :decoder, :inport, :readbuf, :destbuf, :status, :pos
182
-
183
- STATUS_CLOSED = nil
184
- STATUS_READY = 0
185
- STATUS_INPORT_EOF = 1
186
-
187
- #
188
- # call-seq:
189
- # open(inport, dict = nil) -> decoder
190
- # open(inport, dict = nil) { |decoder| ... } -> yield returned value
191
- #
192
- # [inport]
193
- # String instance or +read+ method haved Object.
194
- #
195
- def self.open(inport, dict = nil)
196
- inport = StringIO.new(inport) if inport.kind_of?(String)
197
-
198
- dec = new(inport, dict)
199
-
200
- return dec unless block_given?
201
131
 
202
- begin
203
- yield(dec)
204
- ensure
205
- dec.close rescue nil
206
- end
207
- end
132
+ def self.decode(src, dest: nil, dict: nil)
133
+ # NOTE: ContextLess.decode は伸長時のサイズが必要なため、常に利用できるわけではない
134
+ # ContextLess.decode(src, dest || Aux::EMPTY_BUFFER.dup, nil, dict)
208
135
 
209
- def initialize(inport, dict = nil)
210
- raise Error, "require .read method - <%s:0x%08x>" % [inport.class, inport.object_id << 1] unless inport.respond_to?(:read)
211
- @decoder = StreamDecoder.new(dict)
212
- @inport = inport
213
- @readbuf = StringIO.new(Aux::EMPTY_BUFFER.dup)
214
- @destbuf = StringIO.new(Aux::EMPTY_BUFFER.dup)
215
- @status = STATUS_READY
216
- @pos = 0
136
+ new(StringIO.new(src), dict).read(nil, dest)
217
137
  end
218
138
 
219
- def close
220
- inport.close rescue nil if inport.respond_to?(:close)
221
- readbuf.truncate 0
222
- destbuf.truncate 0
223
- @status = STATUS_CLOSED
224
- nil
139
+ class << Decoder
140
+ alias decompress decode
141
+ alias uncompress decode
225
142
  end
226
-
227
- def eof
228
- !status
229
- end
230
-
231
- alias eof? eof
232
-
233
- def read(size = nil, dest = Aux::EMPTY_BUFFER.dup)
234
- dest ||= Aux::EMPTY_BUFFER.dup
235
- size &&= size.to_i
236
- Aux.change_binary(dest) do
237
- #dest.clear
238
- dest[0 .. -1] = Aux::EMPTY_BUFFER unless dest.empty? # keep allocated heap
239
- return dest unless !size || size > 0
240
-
241
- d = Aux::EMPTY_BUFFER.dup
242
- until size && size <= 0
243
- if destbuf.eof?
244
- break unless fetch
245
- end
246
-
247
- destbuf.read(size, d)
248
- dest << d
249
-
250
- size -= d.bytesize if size
251
- end
252
- end
253
-
254
- if dest.empty?
255
- nil
256
- else
257
- @pos += dest.bytesize
258
- dest
259
- end
260
- end
261
-
262
- private
263
- def fetch
264
- return nil if eof?
265
-
266
- destbuf.rewind
267
- destbuf.truncate 0
268
-
269
- while true
270
- if readbuf.eof? && status != STATUS_INPORT_EOF
271
- readbuf.rewind
272
- unless inport.read StreamDecoder::INSIZE, readbuf.string
273
- @status = STATUS_INPORT_EOF
274
- end
275
- end
276
-
277
- begin
278
- off = decoder.update readbuf.string, readbuf.pos, destbuf.string, StreamDecoder::OUTSIZE
279
- readbuf.pos = off if off
280
- return self if destbuf.size > 0
281
- break if readbuf.eof? && status == STATUS_INPORT_EOF
282
- rescue Zstd::InitMissingError
283
- break if readbuf.eof? && status == STATUS_INPORT_EOF
284
- raise
285
- end
286
- end
287
-
288
- # when readbuf.eof? && status == STATUS_INPORT_EOF
289
-
290
- @status = nil
291
- nil
292
- end
293
- end if false
143
+ end
294
144
 
295
145
  class Parameters
296
146
  def inspect
@@ -318,22 +168,4 @@ module Zstd
318
168
  end
319
169
  end
320
170
  end
321
-
322
- module Aux
323
- EMPTY_BUFFER = "".force_encoding(Encoding::BINARY).freeze
324
-
325
- def self.io_read(io, size, buf)
326
- raise Error, "encounted EOF (read error)" unless io.read(size, buf)
327
- raise Error, "read size too small (read error)" unless buf.bytesize == size
328
- buf
329
- end
330
-
331
- def self.change_binary(str)
332
- e = str.encoding
333
- str.force_encoding(Encoding::BINARY)
334
- yield
335
- ensure
336
- str.force_encoding e rescue nil if e
337
- end
338
- end
339
171
  end