extzstd 0.0.3.CONCEPT → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/LICENSE +6 -6
  4. data/README.md +26 -45
  5. data/contrib/zstd/CHANGELOG +555 -0
  6. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  7. data/contrib/zstd/CONTRIBUTING.md +392 -0
  8. data/contrib/zstd/COPYING +339 -0
  9. data/contrib/zstd/LICENSE +13 -9
  10. data/contrib/zstd/Makefile +414 -0
  11. data/contrib/zstd/README.md +170 -45
  12. data/contrib/zstd/TESTING.md +44 -0
  13. data/contrib/zstd/appveyor.yml +289 -0
  14. data/contrib/zstd/lib/BUCK +234 -0
  15. data/contrib/zstd/lib/Makefile +354 -0
  16. data/contrib/zstd/lib/README.md +179 -0
  17. data/contrib/zstd/{common → lib/common}/bitstream.h +170 -130
  18. data/contrib/zstd/lib/common/compiler.h +175 -0
  19. data/contrib/zstd/lib/common/cpu.h +215 -0
  20. data/contrib/zstd/lib/common/debug.c +24 -0
  21. data/contrib/zstd/lib/common/debug.h +114 -0
  22. data/contrib/zstd/{common → lib/common}/entropy_common.c +79 -94
  23. data/contrib/zstd/lib/common/error_private.c +55 -0
  24. data/contrib/zstd/lib/common/error_private.h +80 -0
  25. data/contrib/zstd/{common → lib/common}/fse.h +153 -93
  26. data/contrib/zstd/{common → lib/common}/fse_decompress.c +37 -82
  27. data/contrib/zstd/lib/common/huf.h +340 -0
  28. data/contrib/zstd/{common → lib/common}/mem.h +154 -78
  29. data/contrib/zstd/lib/common/pool.c +344 -0
  30. data/contrib/zstd/lib/common/pool.h +84 -0
  31. data/contrib/zstd/lib/common/threading.c +121 -0
  32. data/contrib/zstd/lib/common/threading.h +155 -0
  33. data/contrib/zstd/{common → lib/common}/xxhash.c +85 -75
  34. data/contrib/zstd/{common → lib/common}/xxhash.h +85 -73
  35. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  36. data/contrib/zstd/lib/common/zstd_errors.h +94 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +447 -0
  38. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +194 -303
  39. data/contrib/zstd/lib/compress/hist.c +183 -0
  40. data/contrib/zstd/lib/compress/hist.h +75 -0
  41. data/contrib/zstd/lib/compress/huf_compress.c +798 -0
  42. data/contrib/zstd/lib/compress/zstd_compress.c +4278 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1125 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  49. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  50. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  52. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  54. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.c +1138 -0
  56. data/contrib/zstd/lib/compress/zstd_lazy.h +67 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.c +619 -0
  58. data/contrib/zstd/lib/compress/zstd_ldm.h +110 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.c +1200 -0
  60. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.c +2143 -0
  62. data/contrib/zstd/lib/compress/zstdmt_compress.h +192 -0
  63. data/contrib/zstd/lib/decompress/huf_decompress.c +1248 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  65. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress.c +1885 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1432 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  69. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +189 -0
  70. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +86 -69
  71. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  73. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.c +1236 -0
  75. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  77. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +5 -5
  78. data/contrib/zstd/lib/dictBuilder/fastcover.c +757 -0
  79. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +437 -347
  80. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  81. data/contrib/zstd/lib/legacy/zstd_legacy.h +415 -0
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +272 -292
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +26 -32
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +162 -392
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +26 -32
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +162 -391
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +27 -33
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +195 -604
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +26 -32
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +300 -575
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +22 -31
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +165 -592
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +54 -67
  94. data/contrib/zstd/lib/legacy/zstd_v07.c +4541 -0
  95. data/contrib/zstd/lib/legacy/zstd_v07.h +187 -0
  96. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  97. data/contrib/zstd/lib/zstd.h +2090 -0
  98. data/ext/depend +2 -0
  99. data/ext/extconf.rb +18 -5
  100. data/ext/extzstd.c +296 -214
  101. data/ext/extzstd.h +81 -36
  102. data/ext/extzstd_nogvls.h +0 -117
  103. data/ext/extzstd_stream.c +622 -0
  104. data/ext/libzstd_conf.h +8 -0
  105. data/ext/zstd_common.c +11 -0
  106. data/ext/zstd_compress.c +15 -0
  107. data/ext/zstd_decompress.c +6 -0
  108. data/ext/zstd_dictbuilder.c +10 -0
  109. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  110. data/ext/zstd_legacy_v01.c +3 -1
  111. data/ext/zstd_legacy_v02.c +3 -1
  112. data/ext/zstd_legacy_v03.c +3 -1
  113. data/ext/zstd_legacy_v04.c +3 -1
  114. data/ext/zstd_legacy_v05.c +3 -1
  115. data/ext/zstd_legacy_v06.c +3 -1
  116. data/ext/zstd_legacy_v07.c +3 -0
  117. data/gemstub.rb +27 -21
  118. data/lib/extzstd.rb +82 -161
  119. data/lib/extzstd/version.rb +1 -1
  120. data/test/test_basic.rb +19 -6
  121. metadata +127 -59
  122. data/contrib/zstd/common/error_private.h +0 -125
  123. data/contrib/zstd/common/error_public.h +0 -77
  124. data/contrib/zstd/common/huf.h +0 -228
  125. data/contrib/zstd/common/zstd.h +0 -475
  126. data/contrib/zstd/common/zstd_common.c +0 -91
  127. data/contrib/zstd/common/zstd_internal.h +0 -238
  128. data/contrib/zstd/compress/huf_compress.c +0 -577
  129. data/contrib/zstd/compress/zbuff_compress.c +0 -327
  130. data/contrib/zstd/compress/zstd_compress.c +0 -3074
  131. data/contrib/zstd/compress/zstd_opt.h +0 -1046
  132. data/contrib/zstd/decompress/huf_decompress.c +0 -894
  133. data/contrib/zstd/decompress/zbuff_decompress.c +0 -294
  134. data/contrib/zstd/decompress/zstd_decompress.c +0 -1362
  135. data/contrib/zstd/dictBuilder/zdict.h +0 -113
  136. data/contrib/zstd/legacy/zstd_legacy.h +0 -140
  137. data/ext/extzstd_buffered.c +0 -265
  138. data/ext/zstd_amalgam.c +0 -18
@@ -0,0 +1,8 @@
1
+ #ifndef RUBY_ZSTD_LIBZSTD_CONF_H
2
+ #define RUBY_ZSTD_LIBZSTD_CONF_H 1
3
+
4
+ #define ZSTD_LEGACY_SUPPORT 1
5
+ #define MEM_MODULE 1
6
+ #define visibility(v) visibility("hidden")
7
+
8
+ #endif /* RUBY_ZSTD_LIBZSTD_CONF_H */
@@ -0,0 +1,11 @@
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/common/entropy_common.c"
4
+ #include "../contrib/zstd/lib/common/error_private.c"
5
+ #include "../contrib/zstd/lib/common/fse_decompress.c"
6
+
7
+ #undef CHECK_F
8
+ #include "../contrib/zstd/lib/common/pool.c"
9
+ #include "../contrib/zstd/lib/common/threading.c"
10
+ #include "../contrib/zstd/lib/common/xxhash.c"
11
+ #include "../contrib/zstd/lib/common/zstd_common.c"
@@ -0,0 +1,15 @@
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/compress/fse_compress.c"
4
+ #include "../contrib/zstd/lib/compress/huf_compress.c"
5
+
6
+ #undef CHECK_F
7
+ #include "../contrib/zstd/lib/compress/zstd_compress.c"
8
+ #include "../contrib/zstd/lib/compress/zstd_compress_literals.c"
9
+ #include "../contrib/zstd/lib/compress/zstd_compress_sequences.c"
10
+ #include "../contrib/zstd/lib/compress/zstd_double_fast.c"
11
+ #include "../contrib/zstd/lib/compress/zstd_fast.c"
12
+ #include "../contrib/zstd/lib/compress/zstd_lazy.c"
13
+ #include "../contrib/zstd/lib/compress/zstd_ldm.c"
14
+ #include "../contrib/zstd/lib/compress/zstd_opt.c"
15
+ #include "../contrib/zstd/lib/compress/hist.c"
@@ -0,0 +1,6 @@
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/decompress/zstd_decompress.c"
4
+ #include "../contrib/zstd/lib/decompress/zstd_decompress_block.c"
5
+ #include "../contrib/zstd/lib/decompress/huf_decompress.c"
6
+ #include "../contrib/zstd/lib/decompress/zstd_ddict.c"
@@ -0,0 +1,10 @@
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/dictBuilder/zdict.c"
4
+ #include "../contrib/zstd/lib/dictBuilder/divsufsort.c"
5
+
6
+ #undef DISPLAY
7
+ #undef DISPLAYLEVEL
8
+ #undef DISPLAYUPDATE
9
+ #define prime4bytes cover_prime4bytes
10
+ #include "../contrib/zstd/lib/dictBuilder/cover.c"
@@ -0,0 +1,3 @@
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/dictBuilder/fastcover.c"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v01.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v01.c"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v02.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v02.c"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v03.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v03.c"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v04.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v04.c"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v05.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v05.c"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v06.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v06.c"
@@ -0,0 +1,3 @@
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v07.c"
data/gemstub.rb CHANGED
@@ -1,35 +1,41 @@
1
-
2
- file "lib/extzstd/version.rb" => %w(README.md) do
3
- reg = /^\s*\*\s+version:\s*(\d+(?:\.\w+)+)\s*$/i
4
- unless File.read("README.md", mode: "rt") =~ reg
5
- raise "``version'' is not defined or bad syntax in ``README.md''"
6
- end
7
-
8
- GEMSTUB.version = ver = String($1)
9
-
10
- File.write "lib/extzstd/version.rb", <<-"EOS", mode: "wb"
11
- module Zstd
12
- VERSION = #{ver.inspect}
13
- end
14
- EOS
1
+ verreg = /^\s*\*\s+version:\s*(\d+(?:\.\w+)+)\s*$/i
2
+ unless File.read("README.md", mode: "rt") =~ verreg
3
+ raise "``version'' is not defined or bad syntax in ``README.md''"
15
4
  end
16
5
 
17
- require_relative "lib/extzstd/version"
6
+ version = String($1)
18
7
 
19
8
  GEMSTUB = Gem::Specification.new do |s|
20
9
  s.name = "extzstd"
21
- s.version = Zstd::VERSION
10
+ s.version = version
22
11
  s.summary = "ruby bindings for Zstandard (zstd)"
23
12
  s.description = <<EOS
24
- unoficial ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
13
+ unoficial ruby bindings for Zstandard (zstd) <https://github.com/facebook/zstd>.
25
14
  EOS
26
- s.homepage = "https://osdn.jp/projects/rutsubo/"
27
- s.license = "2-clause BSD License"
15
+ s.homepage = "https://github.com/dearblue/ruby-extzstd/"
16
+ s.license = "BSD-2-Clause"
28
17
  s.author = "dearblue"
29
- s.email = "dearblue@users.osdn.me"
18
+ s.email = "dearblue@users.noreply.github.com"
30
19
 
31
20
  s.required_ruby_version = ">= 2.0"
32
- s.add_development_dependency "rake"
21
+ s.add_development_dependency "rake", ">= 12.0"
22
+ end
23
+
24
+ LIB << "lib/extzstd/version.rb"
25
+
26
+ file "lib/extzstd/version.rb" => %w(README.md) do
27
+ GEMSTUB.version = version
28
+
29
+ mkpath "lib/extzstd"
30
+ File.write "lib/extzstd/version.rb", <<-"EOS", mode: "wb"
31
+ module Zstd
32
+ VERSION = #{version.inspect}
33
+ end
34
+ EOS
33
35
  end
34
36
 
35
37
  EXTRA.concat(FileList["contrib/**/*"])
38
+
39
+ filter = %r(contrib/zstd/(?:build|contrib|doc|examples|lib/dll|programs|tests|zlibWrapper))
40
+ DOC.reject! { |path| path =~ filter }
41
+ EXTRA.reject! { |path| path =~ filter }
@@ -2,7 +2,7 @@
2
2
 
3
3
  ver = RUBY_VERSION.slice(/\d+\.\d+/)
4
4
  soname = File.basename(__FILE__, ".rb") << ".so"
5
- require_relative File.join(ver, soname)
5
+ require File.join(ver, soname)
6
6
 
7
7
  require_relative "extzstd/version"
8
8
 
@@ -12,44 +12,82 @@ 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 Internals
16
+ unless String.method_defined? :b
17
+ refine String do
18
+ def b
19
+ dup.force_encoding(Encoding::BINARY)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ using Internals
26
+
27
+ refine String do
28
+ def to_zstd(params = nil, dict: nil)
29
+ ContextLess.encode(self, "".b, nil, dict, params)
30
+ end
31
+
32
+ def unzstd(size = nil, dict: nil)
33
+ Decoder.open(self, dict) { |d| return d.read(size) }
34
+ end
35
+ end
36
+
37
+ refine Object do
38
+ def to_zstd(params = nil, dict: nil, &block)
39
+ Encoder.open(self, params, dict, &block)
40
+ end
41
+
42
+ def unzstd(dict: nil, &block)
43
+ Decoder.open(self, dict, &block)
44
+ end
45
+ end
46
+
47
+ using Zstd
48
+
15
49
  #
16
50
  # call-seq:
17
- # encode(src_string, level = nil, dict = nil) -> zstd string
18
- # encode(src_string, encoder_params, dict = nil) -> zstd string
19
- # encode(outport, level = nil, dict = nil) -> zstd encoder
20
- # encode(outport, level = nil, dict = nil) { |encoder| ... } -> yield returned value
21
- # encode(outport, encoder_params, dict = nil) -> zstd encoder
22
- # encode(outport, encoder_params, dict = nil) { |encoder| ... } -> yield returned value
51
+ # encode(src_string, level = nil, opts = {}) -> zstd string
52
+ # encode(src_string, encode_params, opts = {}) -> zstd string
53
+ # encode(outport, level = nil, opts = {}) -> zstd encoder
54
+ # encode(outport, level = nil, opts = {}) { |encoder| ... } -> yield returned value
55
+ # encode(outport, encode_params, opts = {}) -> zstd encoder
56
+ # encode(outport, encode_params, opts = {}) { |encoder| ... } -> yield returned value
23
57
  #
24
- def self.encode(src, *args, &block)
25
- if src.kind_of?(String)
26
- dest = Aux::EMPTY_BUFFER.dup
27
- return Encoder.open(dest, *args) { |e| e.write src; dest }
28
- end
29
-
30
- Encoder.open(src, *args, &block)
58
+ # [src_string (string)]
59
+ # [outport (io liked object)]
60
+ # [level = nil (integer or nil)]
61
+ # [encode_params (instance of Zstd::Parameters)]
62
+ # [opts dict: nil (string or nil)]
63
+ def self.encode(src, *args, **opts, &block)
64
+ src.to_zstd(*args, **opts, &block)
31
65
  end
32
66
 
33
67
  #
34
68
  # call-seq:
35
- # decode(zstd_string, maxsize = nil, dict = nil) -> string
36
- # decode(zstd_stream, maxsize = nil, dict = nil) -> zstd decoder
37
- # decode(zstd_stream, maxsize = nil, dict = nil) { |decoder| ... } -> yield returned value
69
+ # decode(zstd_string, maxsize = nil, dict: nil) -> string
70
+ # decode(zstd_stream, dict: nil) -> zstd decoder
71
+ # decode(zstd_stream, dict: nil) { |decoder| ... } -> yield returned value
38
72
  #
39
- def self.decode(src, maxsize = nil, dict = nil, &block)
40
- if src.kind_of?(String)
41
- maxsize &&= maxsize.to_i
42
- return Decoder.open(src, dict) { |d| return d.read(maxsize) }
43
- end
73
+ def self.decode(src, *args, **opts, &block)
74
+ src.unzstd(*args, **opts, &block)
75
+ end
44
76
 
45
- Decoder.open(src, dict, &block)
77
+ class << Zstd
78
+ alias compress encode
79
+ alias decompress decode
80
+ alias uncompress decode
46
81
  end
47
82
 
48
- class Encoder < Struct.new(:encoder, :outport, :destbuf, :status)
83
+ Compressor = Encoder
84
+ StreamEncoder = Encoder
85
+
86
+ class Encoder
49
87
  #
50
88
  # call-seq:
51
89
  # open(outport, level = nil, dict = nil) -> zstd encoder
52
- # open(outport, encoder_params, dict = nil) { |encoder| ... } -> yield returned value
90
+ # open(outport, encode_params, dict = nil) { |encoder| ... } -> yield returned value
53
91
  #
54
92
  def self.open(outport, *args)
55
93
  e = new(outport, *args)
@@ -59,68 +97,24 @@ module Zstd
59
97
  begin
60
98
  yield e
61
99
  ensure
62
- e.close rescue nil unless e.eof?
100
+ e.close unless e.eof?
63
101
  end
64
102
  end
65
103
 
66
- #
67
- # call-seq:
68
- # initialize(outport, level = nil, dict = nil)
69
- # initialize(outport, encoder_params, dict = nil)
70
- #
71
- # +outport+ need has +.<<+ method.
72
- #
73
- def initialize(outport, params = nil, dict = nil)
74
- encoder = BufferedEncoder.new(params, dict)
75
- super encoder, outport, "".force_encoding(Encoding::BINARY), [true]
76
- end
77
-
78
- def eof
79
- !status[0]
80
- end
81
-
82
- alias eof? eof
83
-
84
- def close
85
- return nil if eof?
86
- encoder.end(destbuf, BufferedEncoder.recommended_outsize)
87
- outport << destbuf
88
- status[0] = false
89
- nil
90
- end
91
-
92
- def write(buf)
93
- raise IOError, "closed stream" if eof?
94
-
95
- off = 0
96
- rest = buf.bytesize
97
- outsize = BufferedEncoder.recommended_outsize
98
- while off && off < rest
99
- off = encoder.continue(buf, off, destbuf, outsize)
100
- outport << destbuf
101
- end
102
-
103
- self
104
+ def self.encode(src, params = nil, dest: nil, dict: nil)
105
+ ContextLess.encode(src, dest || "".b, nil, dict, params)
104
106
  end
105
107
 
106
- alias << write
107
-
108
- def flush
109
- raise IOError, "closed stream" if eof?
110
-
111
- off = 0
112
- encoder.flush(destbuf, BufferedEncoder.recommended_outsize)
113
- outport << destbuf
114
-
115
- self
108
+ class << Encoder
109
+ alias compress encode
116
110
  end
117
111
  end
118
112
 
119
- class Decoder < Struct.new(:decoder, :inport, :readbuf, :destbuf, :status)
120
- STATUS_CLOSED = nil
121
- STATUS_READY = 0
122
- STATUS_INPORT_EOF = 1
113
+ Uncompressor = Decoder
114
+ Decompressor = Decoder
115
+ StreamDecoder = Decoder
123
116
 
117
+ class Decoder
124
118
  #
125
119
  # call-seq:
126
120
  # open(inport, dict = nil) -> decoder
@@ -143,77 +137,20 @@ module Zstd
143
137
  end
144
138
  end
145
139
 
146
- def initialize(inport, dict = nil)
147
- raise Error, "require .read method - <%s:0x%08x>" % [inport.class, inport.object_id << 1] unless inport.respond_to?(:read)
148
- super(BufferedDecoder.new(dict), inport, StringIO.new(Aux::EMPTY_BUFFER.dup), StringIO.new(Aux::EMPTY_BUFFER.dup), STATUS_READY)
149
- end
150
-
151
- def close
152
- decoder.reset
153
- inport.close rescue nil if inport.respond_to?(:close)
154
- readbuf.clear
155
- #destbuf.clear
156
- self.status = STATUS_CLOSED
157
- nil
158
- end
159
-
160
- def eof
161
- !status
162
- end
163
-
164
- alias eof? eof
165
-
166
- def read(size = nil, dest = Aux::EMPTY_BUFFER.dup)
167
- dest ||= Aux::EMPTY_BUFFER.dup
168
- Aux.change_binary(dest) do
169
- #dest.clear
170
- dest[0 .. -1] = Aux::EMPTY_BUFFER # keep allocated heap
171
-
172
- return dest if size == 0
173
-
174
- d = Aux::EMPTY_BUFFER.dup
175
- until size && size <= 0
176
- if destbuf.eof?
177
- break unless fetch
178
- end
140
+ def self.decode(src, dest: nil, dict: nil)
141
+ # NOTE: ContextLess.decode は伸長時のサイズが必要なため、常に利用できるわけではない
142
+ # ContextLess.decode(src, dest || "".b, nil, dict)
179
143
 
180
- destbuf.read(size, d)
181
- dest << d
182
-
183
- size -= d.bytesize if size
184
- end
185
- end
186
-
187
- if dest.empty?
188
- nil
189
- else
190
- dest
191
- end
144
+ new(StringIO.new(src), dict).read(nil, dest)
192
145
  end
193
146
 
194
- private
195
- def fetch
196
- return nil if eof? || status == STATUS_INPORT_EOF
197
-
198
- while true
199
- if readbuf.eof?
200
- readbuf.string[0 .. -1] = Aux::EMPTY_BUFFER
201
- readbuf.rewind
202
- unless inport.read(BufferedDecoder.recommended_insize, readbuf.string)
203
- self.status = STATUS_INPORT_EOF
204
- return nil
205
- end
206
- end
207
-
208
- off = decoder.continue(readbuf.string, readbuf.pos, destbuf.string, BufferedDecoder.recommended_outsize)
209
- readbuf.pos = off if off
210
- destbuf.rewind
211
- return self if destbuf.size > 0
212
- end
147
+ class << Decoder
148
+ alias decompress decode
149
+ alias uncompress decode
213
150
  end
214
151
  end
215
152
 
216
- class EncodeParameters
153
+ class Parameters
217
154
  def inspect
218
155
  "#<#{self.class} windowlog=#{windowlog}, chainlog=#{chainlog}, " \
219
156
  "hashlog=#{hashlog}, searchlog=#{searchlog}, " \
@@ -233,26 +170,10 @@ module Zstd
233
170
  q.breakable " "
234
171
  q.text "searchlength=#{searchlength},"
235
172
  q.breakable " "
173
+ q.text "targetlength=#{targetlength},"
174
+ q.breakable " "
236
175
  q.text "strategy=#{strategy}>"
237
176
  end
238
177
  end
239
178
  end
240
-
241
- module Aux
242
- EMPTY_BUFFER = "".force_encoding(Encoding::BINARY).freeze
243
-
244
- def self.io_read(io, size, buf)
245
- raise Error, "encounted EOF (read error)" unless io.read(size, buf)
246
- raise Error, "read size too small (read error)" unless buf.bytesize == size
247
- buf
248
- end
249
-
250
- def self.change_binary(str)
251
- e = str.encoding
252
- str.force_encoding(Encoding::BINARY)
253
- yield
254
- ensure
255
- str.force_encoding e rescue nil if e
256
- end
257
- end
258
179
  end