extzstd 0.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/README.md +38 -56
  4. data/contrib/zstd/CHANGELOG +613 -0
  5. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  6. data/contrib/zstd/CONTRIBUTING.md +406 -0
  7. data/contrib/zstd/COPYING +339 -0
  8. data/contrib/zstd/Makefile +420 -0
  9. data/contrib/zstd/README.md +179 -41
  10. data/contrib/zstd/TESTING.md +44 -0
  11. data/contrib/zstd/appveyor.yml +292 -0
  12. data/contrib/zstd/lib/BUCK +234 -0
  13. data/contrib/zstd/lib/Makefile +451 -0
  14. data/contrib/zstd/lib/README.md +207 -0
  15. data/contrib/zstd/{common → lib/common}/bitstream.h +187 -138
  16. data/contrib/zstd/lib/common/compiler.h +288 -0
  17. data/contrib/zstd/lib/common/cpu.h +213 -0
  18. data/contrib/zstd/lib/common/debug.c +24 -0
  19. data/contrib/zstd/lib/common/debug.h +107 -0
  20. data/contrib/zstd/lib/common/entropy_common.c +362 -0
  21. data/contrib/zstd/{common → lib/common}/error_private.c +25 -12
  22. data/contrib/zstd/{common → lib/common}/error_private.h +14 -10
  23. data/contrib/zstd/{common → lib/common}/fse.h +173 -92
  24. data/contrib/zstd/{common → lib/common}/fse_decompress.c +149 -85
  25. data/contrib/zstd/lib/common/huf.h +361 -0
  26. data/contrib/zstd/{common → lib/common}/mem.h +115 -59
  27. data/contrib/zstd/lib/common/pool.c +350 -0
  28. data/contrib/zstd/lib/common/pool.h +84 -0
  29. data/contrib/zstd/lib/common/threading.c +122 -0
  30. data/contrib/zstd/lib/common/threading.h +155 -0
  31. data/contrib/zstd/{common → lib/common}/xxhash.c +55 -96
  32. data/contrib/zstd/{common → lib/common}/xxhash.h +23 -47
  33. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  34. data/contrib/zstd/lib/common/zstd_deps.h +111 -0
  35. data/contrib/zstd/lib/common/zstd_errors.h +95 -0
  36. data/contrib/zstd/lib/common/zstd_internal.h +478 -0
  37. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +214 -319
  38. data/contrib/zstd/lib/compress/hist.c +181 -0
  39. data/contrib/zstd/lib/compress/hist.h +75 -0
  40. data/contrib/zstd/lib/compress/huf_compress.c +913 -0
  41. data/contrib/zstd/lib/compress/zstd_compress.c +5208 -0
  42. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1203 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +433 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +849 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  49. data/contrib/zstd/lib/compress/zstd_cwksp.h +561 -0
  50. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  52. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  54. data/contrib/zstd/lib/compress/zstd_lazy.c +1412 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.h +87 -0
  56. data/contrib/zstd/lib/compress/zstd_ldm.c +660 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.h +116 -0
  58. data/contrib/zstd/lib/compress/zstd_opt.c +1345 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  60. data/contrib/zstd/lib/compress/zstdmt_compress.c +1811 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.h +110 -0
  62. data/contrib/zstd/lib/decompress/huf_decompress.c +1350 -0
  63. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  65. data/contrib/zstd/lib/decompress/zstd_decompress.c +1930 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1540 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +62 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +190 -0
  69. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +68 -45
  70. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  71. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  73. data/contrib/zstd/lib/dictBuilder/cover.c +1245 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  75. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +0 -0
  77. data/contrib/zstd/lib/dictBuilder/fastcover.c +758 -0
  78. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +318 -194
  79. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  80. data/contrib/zstd/{legacy → lib/legacy}/zstd_legacy.h +171 -15
  81. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +191 -124
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +19 -5
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +125 -125
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +19 -5
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +125 -124
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +20 -6
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +151 -299
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +19 -5
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +237 -243
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +19 -6
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +130 -143
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +18 -5
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.c +158 -157
  94. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.h +19 -5
  95. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  96. data/contrib/zstd/lib/zstd.h +2391 -0
  97. data/ext/depend +2 -0
  98. data/ext/extconf.rb +15 -6
  99. data/ext/extzstd.c +76 -145
  100. data/ext/extzstd.h +80 -31
  101. data/ext/extzstd_stream.c +417 -142
  102. data/ext/libzstd_conf.h +8 -0
  103. data/ext/zstd_common.c +10 -7
  104. data/ext/zstd_compress.c +14 -5
  105. data/ext/zstd_decompress.c +5 -4
  106. data/ext/zstd_dictbuilder.c +9 -4
  107. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  108. data/ext/zstd_legacy_v01.c +3 -1
  109. data/ext/zstd_legacy_v02.c +3 -1
  110. data/ext/zstd_legacy_v03.c +3 -1
  111. data/ext/zstd_legacy_v04.c +3 -1
  112. data/ext/zstd_legacy_v05.c +3 -1
  113. data/ext/zstd_legacy_v06.c +3 -1
  114. data/ext/zstd_legacy_v07.c +3 -1
  115. data/gemstub.rb +10 -24
  116. data/lib/extzstd.rb +64 -179
  117. data/lib/extzstd/version.rb +6 -1
  118. data/test/test_basic.rb +9 -6
  119. metadata +113 -57
  120. data/HISTORY.ja +0 -5
  121. data/contrib/zstd/common/entropy_common.c +0 -225
  122. data/contrib/zstd/common/huf.h +0 -228
  123. data/contrib/zstd/common/zstd_common.c +0 -83
  124. data/contrib/zstd/common/zstd_errors.h +0 -60
  125. data/contrib/zstd/common/zstd_internal.h +0 -267
  126. data/contrib/zstd/compress/huf_compress.c +0 -533
  127. data/contrib/zstd/compress/zbuff_compress.c +0 -319
  128. data/contrib/zstd/compress/zstd_compress.c +0 -3264
  129. data/contrib/zstd/compress/zstd_opt.h +0 -900
  130. data/contrib/zstd/decompress/huf_decompress.c +0 -883
  131. data/contrib/zstd/decompress/zbuff_decompress.c +0 -252
  132. data/contrib/zstd/decompress/zstd_decompress.c +0 -1842
  133. data/contrib/zstd/dictBuilder/zdict.h +0 -111
  134. data/contrib/zstd/zstd.h +0 -640
@@ -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 */
data/ext/zstd_common.c CHANGED
@@ -1,8 +1,11 @@
1
- #define ZSTD_LEGACY_SUPPORT 1
2
- #define MEM_MODULE 1
1
+ #include "libzstd_conf.h"
3
2
 
4
- #include "../contrib/zstd/common/entropy_common.c"
5
- #include "../contrib/zstd/common/error_private.c"
6
- #include "../contrib/zstd/common/fse_decompress.c"
7
- #include "../contrib/zstd/common/xxhash.c"
8
- #include "../contrib/zstd/common/zstd_common.c"
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"
data/ext/zstd_compress.c CHANGED
@@ -1,6 +1,15 @@
1
- #define ZSTD_LEGACY_SUPPORT 1
2
- #define MEM_MODULE 1
1
+ #include "libzstd_conf.h"
3
2
 
4
- #include "../contrib/zstd/compress/fse_compress.c"
5
- #include "../contrib/zstd/compress/huf_compress.c"
6
- #include "../contrib/zstd/compress/zstd_compress.c"
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"
@@ -1,5 +1,6 @@
1
- #define ZSTD_LEGACY_SUPPORT 1
2
- #define MEM_MODULE 1
1
+ #include "libzstd_conf.h"
3
2
 
4
- #include "../contrib/zstd/decompress/zstd_decompress.c"
5
- #include "../contrib/zstd/decompress/huf_decompress.c"
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"
@@ -1,5 +1,10 @@
1
- #define ZSTD_LEGACY_SUPPORT 1
2
- #define MEM_MODULE 1
1
+ #include "libzstd_conf.h"
3
2
 
4
- #include "../contrib/zstd/dictBuilder/zdict.c"
5
- #include "../contrib/zstd/dictBuilder/divsufsort.c"
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"
@@ -1 +1,3 @@
1
- #include "../contrib/zstd/legacy/zstd_v07.c"
1
+ #include "libzstd_conf.h"
2
+
3
+ #include "../contrib/zstd/lib/legacy/zstd_v07.c"
data/gemstub.rb CHANGED
@@ -1,37 +1,23 @@
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''"
4
- end
5
-
6
- version = String($1)
1
+ require_relative "lib/extzstd/version"
7
2
 
8
3
  GEMSTUB = Gem::Specification.new do |s|
9
4
  s.name = "extzstd"
10
- s.version = version
5
+ s.version = Zstd::VERSION
11
6
  s.summary = "ruby bindings for Zstandard (zstd)"
12
7
  s.description = <<EOS
13
- unoficial ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
8
+ unoficial ruby bindings for Zstandard (zstd) <https://github.com/facebook/zstd>.
14
9
  EOS
15
- s.homepage = "https://osdn.jp/projects/rutsubo/"
16
- s.license = "2-clause BSD License"
10
+ s.homepage = "https://github.com/dearblue/ruby-extzstd/"
11
+ s.license = "BSD-2-Clause"
17
12
  s.author = "dearblue"
18
13
  s.email = "dearblue@users.osdn.me"
19
14
 
20
15
  s.required_ruby_version = ">= 2.0"
21
- s.add_development_dependency "rake"
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
16
+ s.add_development_dependency "rake", ">= 12.0"
35
17
  end
36
18
 
37
19
  EXTRA.concat(FileList["contrib/**/*"])
20
+
21
+ filter = %r(contrib/zstd/(?:build|contrib|doc|examples|lib/dll|programs|tests|zlibWrapper))
22
+ DOC.reject! { |path| path =~ filter }
23
+ EXTRA.reject! { |path| path =~ filter }
data/lib/extzstd.rb CHANGED
@@ -2,9 +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)
6
-
7
- require_relative "extzstd/version"
5
+ require File.join(ver, soname)
8
6
 
9
7
  require "stringio"
10
8
 
@@ -12,6 +10,40 @@ require "stringio"
12
10
  # This is ruby bindings for zstd <https://github.com/Cyan4973/zstd> the compression library.
13
11
  #
14
12
  module Zstd
13
+ module Internals
14
+ unless String.method_defined? :b
15
+ refine String do
16
+ def b
17
+ dup.force_encoding(Encoding::BINARY)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ using Internals
24
+
25
+ refine String do
26
+ def to_zstd(params = nil, dict: nil)
27
+ ContextLess.encode(self, "".b, nil, dict, params)
28
+ end
29
+
30
+ def unzstd(size = nil, dict: nil)
31
+ Decoder.open(self, dict) { |d| return d.read(size) }
32
+ end
33
+ end
34
+
35
+ refine Object do
36
+ def to_zstd(params = nil, dict: nil, &block)
37
+ Encoder.open(self, params, dict, &block)
38
+ end
39
+
40
+ def unzstd(dict: nil, &block)
41
+ Decoder.open(self, dict, &block)
42
+ end
43
+ end
44
+
45
+ using Zstd
46
+
15
47
  #
16
48
  # call-seq:
17
49
  # encode(src_string, level = nil, opts = {}) -> zstd string
@@ -26,12 +58,8 @@ module Zstd
26
58
  # [level = nil (integer or nil)]
27
59
  # [encode_params (instance of Zstd::Parameters)]
28
60
  # [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)
61
+ def self.encode(src, *args, **opts, &block)
62
+ src.to_zstd(*args, **opts, &block)
35
63
  end
36
64
 
37
65
  #
@@ -40,26 +68,20 @@ module Zstd
40
68
  # decode(zstd_stream, dict: nil) -> zstd decoder
41
69
  # decode(zstd_stream, dict: nil) { |decoder| ... } -> yield returned value
42
70
  #
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
71
+ def self.decode(src, *args, **opts, &block)
72
+ src.unzstd(*args, **opts, &block)
73
+ end
58
74
 
59
- Decoder.open(src, dict, &block)
75
+ class << Zstd
76
+ alias compress encode
77
+ alias decompress decode
78
+ alias uncompress decode
60
79
  end
61
80
 
62
- class Encoder < Struct.new(:encoder, :outport, :destbuf, :status)
81
+ Compressor = Encoder
82
+ StreamEncoder = Encoder
83
+
84
+ class Encoder
63
85
  #
64
86
  # call-seq:
65
87
  # open(outport, level = nil, dict = nil) -> zstd encoder
@@ -73,70 +95,24 @@ module Zstd
73
95
  begin
74
96
  yield e
75
97
  ensure
76
- e.close rescue nil unless e.eof?
98
+ e.close unless e.eof?
77
99
  end
78
100
  end
79
101
 
80
- #
81
- # call-seq:
82
- # initialize(outport, level = nil, dict = nil)
83
- # initialize(outport, encode_params, dict = nil)
84
- #
85
- # +outport+ need has +.<<+ method.
86
- #
87
- def initialize(outport, params = nil, dict = nil)
88
- encoder = StreamEncoder.new(params, dict)
89
- super encoder, outport, "".force_encoding(Encoding::BINARY), [true]
90
- end
91
-
92
- def eof
93
- !status[0]
102
+ def self.encode(src, params = nil, dest: nil, dict: nil)
103
+ ContextLess.encode(src, dest || "".b, nil, dict, params)
94
104
  end
95
105
 
96
- alias eof? eof
97
-
98
- def close
99
- return nil if eof?
100
- encoder.end(destbuf, StreamEncoder::OUTSIZE)
101
- outport << destbuf
102
- status[0] = false
103
- nil
104
- end
105
-
106
- def write(buf)
107
- raise IOError, "closed stream" if eof?
108
-
109
- off = 0
110
- rest = buf.bytesize
111
- outsize = StreamEncoder::OUTSIZE
112
- while off && off < rest
113
- off = encoder.update(buf, off, destbuf, outsize)
114
- outport << destbuf
115
- end
116
-
117
- self
118
- end
119
-
120
- alias << write
121
-
122
- def flush
123
- raise IOError, "closed stream" if eof?
124
-
125
- off = 0
126
- encoder.flush(destbuf, StreamEncoder::OUTSIZE)
127
- outport << destbuf
128
-
129
- self
106
+ class << Encoder
107
+ alias compress encode
130
108
  end
131
109
  end
132
110
 
133
- class Decoder
134
- attr_reader :decoder, :inport, :readbuf, :destbuf, :status, :pos
135
-
136
- STATUS_CLOSED = nil
137
- STATUS_READY = 0
138
- STATUS_INPORT_EOF = 1
111
+ Uncompressor = Decoder
112
+ Decompressor = Decoder
113
+ StreamDecoder = Decoder
139
114
 
115
+ class Decoder
140
116
  #
141
117
  # call-seq:
142
118
  # open(inport, dict = nil) -> decoder
@@ -159,89 +135,16 @@ module Zstd
159
135
  end
160
136
  end
161
137
 
162
- def initialize(inport, dict = nil)
163
- raise Error, "require .read method - <%s:0x%08x>" % [inport.class, inport.object_id << 1] unless inport.respond_to?(:read)
164
- @decoder = StreamDecoder.new(dict)
165
- @inport = inport
166
- @readbuf = StringIO.new(Aux::EMPTY_BUFFER.dup)
167
- @destbuf = StringIO.new(Aux::EMPTY_BUFFER.dup)
168
- @status = STATUS_READY
169
- @pos = 0
170
- end
171
-
172
- def close
173
- inport.close rescue nil if inport.respond_to?(:close)
174
- readbuf.truncate 0
175
- destbuf.truncate 0
176
- @status = STATUS_CLOSED
177
- nil
178
- end
179
-
180
- def eof
181
- !status
182
- end
183
-
184
- alias eof? eof
185
-
186
- def read(size = nil, dest = Aux::EMPTY_BUFFER.dup)
187
- dest ||= Aux::EMPTY_BUFFER.dup
188
- size &&= size.to_i
189
- Aux.change_binary(dest) do
190
- #dest.clear
191
- dest[0 .. -1] = Aux::EMPTY_BUFFER unless dest.empty? # keep allocated heap
192
- return dest unless !size || size > 0
193
-
194
- d = Aux::EMPTY_BUFFER.dup
195
- until size && size <= 0
196
- if destbuf.eof?
197
- break unless fetch
198
- end
199
-
200
- destbuf.read(size, d)
201
- dest << d
138
+ def self.decode(src, dest: nil, dict: nil)
139
+ # NOTE: ContextLess.decode は伸長時のサイズが必要なため、常に利用できるわけではない
140
+ # ContextLess.decode(src, dest || "".b, nil, dict)
202
141
 
203
- size -= d.bytesize if size
204
- end
205
- end
206
-
207
- if dest.empty?
208
- nil
209
- else
210
- @pos += dest.bytesize
211
- dest
212
- end
142
+ new(StringIO.new(src), dict).read(nil, dest)
213
143
  end
214
144
 
215
- private
216
- def fetch
217
- return nil if eof?
218
-
219
- destbuf.rewind
220
- destbuf.truncate 0
221
-
222
- while true
223
- if readbuf.eof? && status != STATUS_INPORT_EOF
224
- readbuf.rewind
225
- unless inport.read StreamDecoder::INSIZE, readbuf.string
226
- @status = STATUS_INPORT_EOF
227
- end
228
- end
229
-
230
- begin
231
- off = decoder.update readbuf.string, readbuf.pos, destbuf.string, StreamDecoder::OUTSIZE
232
- readbuf.pos = off if off
233
- return self if destbuf.size > 0
234
- break if readbuf.eof? && status == STATUS_INPORT_EOF
235
- rescue Zstd::InitMissingError
236
- break if readbuf.eof? && status == STATUS_INPORT_EOF
237
- raise
238
- end
239
- end
240
-
241
- # when readbuf.eof? && status == STATUS_INPORT_EOF
242
-
243
- @status = nil
244
- nil
145
+ class << Decoder
146
+ alias decompress decode
147
+ alias uncompress decode
245
148
  end
246
149
  end
247
150
 
@@ -271,22 +174,4 @@ module Zstd
271
174
  end
272
175
  end
273
176
  end
274
-
275
- module Aux
276
- EMPTY_BUFFER = "".force_encoding(Encoding::BINARY).freeze
277
-
278
- def self.io_read(io, size, buf)
279
- raise Error, "encounted EOF (read error)" unless io.read(size, buf)
280
- raise Error, "read size too small (read error)" unless buf.bytesize == size
281
- buf
282
- end
283
-
284
- def self.change_binary(str)
285
- e = str.encoding
286
- str.force_encoding(Encoding::BINARY)
287
- yield
288
- ensure
289
- str.force_encoding e rescue nil if e
290
- end
291
- end
292
177
  end