zstd-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +11 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +29 -0
  9. data/README.md +63 -0
  10. data/Rakefile +22 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/ext/zstdruby/extconf.rb +23 -0
  14. data/ext/zstdruby/libzstd/.gitignore +2 -0
  15. data/ext/zstdruby/libzstd/Makefile +133 -0
  16. data/ext/zstdruby/libzstd/README.md +77 -0
  17. data/ext/zstdruby/libzstd/common/bitstream.h +414 -0
  18. data/ext/zstdruby/libzstd/common/entropy_common.c +227 -0
  19. data/ext/zstdruby/libzstd/common/error_private.c +43 -0
  20. data/ext/zstdruby/libzstd/common/error_private.h +76 -0
  21. data/ext/zstdruby/libzstd/common/fse.h +668 -0
  22. data/ext/zstdruby/libzstd/common/fse_decompress.c +329 -0
  23. data/ext/zstdruby/libzstd/common/huf.h +238 -0
  24. data/ext/zstdruby/libzstd/common/mem.h +372 -0
  25. data/ext/zstdruby/libzstd/common/xxhash.c +867 -0
  26. data/ext/zstdruby/libzstd/common/xxhash.h +309 -0
  27. data/ext/zstdruby/libzstd/common/zstd_common.c +77 -0
  28. data/ext/zstdruby/libzstd/common/zstd_errors.h +60 -0
  29. data/ext/zstdruby/libzstd/common/zstd_internal.h +270 -0
  30. data/ext/zstdruby/libzstd/compress/fse_compress.c +850 -0
  31. data/ext/zstdruby/libzstd/compress/huf_compress.c +609 -0
  32. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3291 -0
  33. data/ext/zstdruby/libzstd/compress/zstd_opt.h +919 -0
  34. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +885 -0
  35. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +2154 -0
  36. data/ext/zstdruby/libzstd/deprecated/zbuff.h +210 -0
  37. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +145 -0
  38. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +74 -0
  39. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1913 -0
  40. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.h +67 -0
  41. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +1012 -0
  42. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +111 -0
  43. data/ext/zstdruby/libzstd/dll/example/Makefile +47 -0
  44. data/ext/zstdruby/libzstd/dll/example/README.md +69 -0
  45. data/ext/zstdruby/libzstd/dll/example/build_package.bat +17 -0
  46. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +25 -0
  47. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +179 -0
  48. data/ext/zstdruby/libzstd/dll/libzstd.def +86 -0
  49. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +259 -0
  50. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2095 -0
  51. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +80 -0
  52. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3518 -0
  53. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +79 -0
  54. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3159 -0
  55. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +79 -0
  56. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3795 -0
  57. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +128 -0
  58. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +4056 -0
  59. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +149 -0
  60. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +4167 -0
  61. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +159 -0
  62. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +4540 -0
  63. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +173 -0
  64. data/ext/zstdruby/libzstd/libzstd.pc.in +14 -0
  65. data/ext/zstdruby/libzstd/zstd.h +673 -0
  66. data/ext/zstdruby/zstdruby.c +117 -0
  67. data/ext/zstdruby/zstdruby.h +6 -0
  68. data/lib/zstd-ruby.rb +6 -0
  69. data/lib/zstd-ruby/version.rb +3 -0
  70. data/zstd-ruby.gemspec +37 -0
  71. metadata +170 -0
@@ -0,0 +1,117 @@
1
+ #include "zstdruby.h"
2
+ #include "./libzstd/zstd.h"
3
+
4
+ static VALUE zstdVersion(VALUE self)
5
+ {
6
+ unsigned version = ZSTD_versionNumber();
7
+ return INT2NUM(version);
8
+ }
9
+
10
+ static VALUE compress(int argc, VALUE *argv, VALUE self)
11
+ {
12
+ VALUE input_value;
13
+ VALUE compression_level_value;
14
+ rb_scan_args(argc, argv, "11", &input_value, &compression_level_value);
15
+
16
+ Check_Type(input_value, RUBY_T_STRING);
17
+ const char* input_data = RSTRING_PTR(input_value);
18
+ size_t input_size = RSTRING_LEN(input_value);
19
+
20
+ int compression_level;
21
+ if (NIL_P(compression_level_value)) {
22
+ compression_level = 1;
23
+ } else {
24
+ Check_Type(compression_level_value, RUBY_T_FIXNUM);
25
+ compression_level = FIX2INT(compression_level_value);
26
+ }
27
+
28
+ // do compress
29
+ size_t max_compressed_size = ZSTD_compressBound(input_size);
30
+
31
+ VALUE output = rb_str_new(NULL, max_compressed_size);
32
+ char* output_data = RSTRING_PTR(output);
33
+
34
+ size_t compressed_size = ZSTD_compress((void*)output_data, max_compressed_size,
35
+ (const void*)input_data, input_size, compression_level);
36
+
37
+ if (ZSTD_isError(compressed_size)) {
38
+ rb_raise(rb_eRuntimeError, "%s: %s", "compress failed", ZSTD_getErrorName(compressed_size));
39
+ } else {
40
+ rb_str_resize(output, compressed_size);
41
+ }
42
+
43
+ return output;
44
+ }
45
+
46
+ static VALUE decompress_buffered(const char* input_data, size_t input_size)
47
+ {
48
+ const size_t outputBufferSize = 4096;
49
+
50
+ ZSTD_DStream* const dstream = ZSTD_createDStream();
51
+ if (dstream == NULL) {
52
+ rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDStream failed");
53
+ }
54
+
55
+ size_t initResult = ZSTD_initDStream(dstream);
56
+ if (ZSTD_isError(initResult)) {
57
+ ZSTD_freeDStream(dstream);
58
+ rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_initDStream failed", ZSTD_getErrorName(initResult));
59
+ }
60
+
61
+
62
+ VALUE output_string = rb_str_new(NULL, 0);
63
+ ZSTD_outBuffer output = { NULL, 0, 0 };
64
+
65
+ ZSTD_inBuffer input = { input_data, input_size, 0 };
66
+ while (input.pos < input.size) {
67
+ output.size += outputBufferSize;
68
+ rb_str_resize(output_string, output.size);
69
+ output.dst = RSTRING_PTR(output_string);
70
+
71
+ size_t readHint = ZSTD_decompressStream(dstream, &output, &input);
72
+ if (ZSTD_isError(readHint)) {
73
+ ZSTD_freeDStream(dstream);
74
+ rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(readHint));
75
+ }
76
+ }
77
+
78
+ ZSTD_freeDStream(dstream);
79
+ rb_str_resize(output_string, output.pos);
80
+ return output_string;
81
+ }
82
+
83
+ static VALUE decompress(VALUE self, VALUE input)
84
+ {
85
+ Check_Type(input, T_STRING);
86
+ const char* input_data = RSTRING_PTR(input);
87
+ size_t input_size = RSTRING_LEN(input);
88
+
89
+ uint64_t uncompressed_size = ZSTD_getDecompressedSize(input_data, input_size);
90
+
91
+ if (uncompressed_size == 0) {
92
+ return decompress_buffered(input_data, input_size);
93
+ }
94
+
95
+ VALUE output = rb_str_new(NULL, uncompressed_size);
96
+ char* output_data = RSTRING_PTR(output);
97
+
98
+ size_t decompress_size = ZSTD_decompress((void*)output_data, uncompressed_size,
99
+ (const void*)input_data, input_size);
100
+
101
+ if (ZSTD_isError(decompress_size)) {
102
+ rb_raise(rb_eRuntimeError, "%s: %s", "decompress error", ZSTD_getErrorName(decompress_size));
103
+ }
104
+
105
+ return output;
106
+ }
107
+
108
+ VALUE rb_mZstd;
109
+
110
+ void
111
+ Init_zstdruby(void)
112
+ {
113
+ rb_mZstd = rb_define_module("Zstd");
114
+ rb_define_module_function(rb_mZstd, "zstd_version", zstdVersion, 0);
115
+ rb_define_module_function(rb_mZstd, "compress", compress, -1);
116
+ rb_define_module_function(rb_mZstd, "decompress", decompress, 1);
117
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef ZSTD_RUBY_H
2
+ #define ZSTD_RUBY_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* ZSTD_RUBY_H */
data/lib/zstd-ruby.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "zstd-ruby/version"
2
+ require "zstd-ruby/zstdruby"
3
+
4
+ module Zstd
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module Zstd
2
+ VERSION = "0.1.0"
3
+ end
data/zstd-ruby.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zstd-ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zstd-ruby"
8
+ spec.version = Zstd::VERSION
9
+ spec.authors = ["SpringMT"]
10
+ spec.email = ["today.is.sky.blue.sky@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby binding for zstd(Zstandard - Fast real-time compression algorithm)}
13
+ spec.description = %q{Ruby binding for zstd(Zstandard - Fast real-time compression algorithm). See https://github.com/facebook/zstd}
14
+ spec.homepage = "https://github.com/SpringMT/zstd-ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ #end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features|benchmarks|zstd)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rake-compiler", '~> 1'
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zstd-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SpringMT
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby binding for zstd(Zstandard - Fast real-time compression algorithm).
70
+ See https://github.com/facebook/zstd
71
+ email:
72
+ - today.is.sky.blue.sky@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".gitmodules"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - ext/zstdruby/extconf.rb
89
+ - ext/zstdruby/libzstd/.gitignore
90
+ - ext/zstdruby/libzstd/Makefile
91
+ - ext/zstdruby/libzstd/README.md
92
+ - ext/zstdruby/libzstd/common/bitstream.h
93
+ - ext/zstdruby/libzstd/common/entropy_common.c
94
+ - ext/zstdruby/libzstd/common/error_private.c
95
+ - ext/zstdruby/libzstd/common/error_private.h
96
+ - ext/zstdruby/libzstd/common/fse.h
97
+ - ext/zstdruby/libzstd/common/fse_decompress.c
98
+ - ext/zstdruby/libzstd/common/huf.h
99
+ - ext/zstdruby/libzstd/common/mem.h
100
+ - ext/zstdruby/libzstd/common/xxhash.c
101
+ - ext/zstdruby/libzstd/common/xxhash.h
102
+ - ext/zstdruby/libzstd/common/zstd_common.c
103
+ - ext/zstdruby/libzstd/common/zstd_errors.h
104
+ - ext/zstdruby/libzstd/common/zstd_internal.h
105
+ - ext/zstdruby/libzstd/compress/fse_compress.c
106
+ - ext/zstdruby/libzstd/compress/huf_compress.c
107
+ - ext/zstdruby/libzstd/compress/zstd_compress.c
108
+ - ext/zstdruby/libzstd/compress/zstd_opt.h
109
+ - ext/zstdruby/libzstd/decompress/huf_decompress.c
110
+ - ext/zstdruby/libzstd/decompress/zstd_decompress.c
111
+ - ext/zstdruby/libzstd/deprecated/zbuff.h
112
+ - ext/zstdruby/libzstd/deprecated/zbuff_compress.c
113
+ - ext/zstdruby/libzstd/deprecated/zbuff_decompress.c
114
+ - ext/zstdruby/libzstd/dictBuilder/divsufsort.c
115
+ - ext/zstdruby/libzstd/dictBuilder/divsufsort.h
116
+ - ext/zstdruby/libzstd/dictBuilder/zdict.c
117
+ - ext/zstdruby/libzstd/dictBuilder/zdict.h
118
+ - ext/zstdruby/libzstd/dll/example/Makefile
119
+ - ext/zstdruby/libzstd/dll/example/README.md
120
+ - ext/zstdruby/libzstd/dll/example/build_package.bat
121
+ - ext/zstdruby/libzstd/dll/example/fullbench-dll.sln
122
+ - ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj
123
+ - ext/zstdruby/libzstd/dll/libzstd.def
124
+ - ext/zstdruby/libzstd/legacy/zstd_legacy.h
125
+ - ext/zstdruby/libzstd/legacy/zstd_v01.c
126
+ - ext/zstdruby/libzstd/legacy/zstd_v01.h
127
+ - ext/zstdruby/libzstd/legacy/zstd_v02.c
128
+ - ext/zstdruby/libzstd/legacy/zstd_v02.h
129
+ - ext/zstdruby/libzstd/legacy/zstd_v03.c
130
+ - ext/zstdruby/libzstd/legacy/zstd_v03.h
131
+ - ext/zstdruby/libzstd/legacy/zstd_v04.c
132
+ - ext/zstdruby/libzstd/legacy/zstd_v04.h
133
+ - ext/zstdruby/libzstd/legacy/zstd_v05.c
134
+ - ext/zstdruby/libzstd/legacy/zstd_v05.h
135
+ - ext/zstdruby/libzstd/legacy/zstd_v06.c
136
+ - ext/zstdruby/libzstd/legacy/zstd_v06.h
137
+ - ext/zstdruby/libzstd/legacy/zstd_v07.c
138
+ - ext/zstdruby/libzstd/legacy/zstd_v07.h
139
+ - ext/zstdruby/libzstd/libzstd.pc.in
140
+ - ext/zstdruby/libzstd/zstd.h
141
+ - ext/zstdruby/zstdruby.c
142
+ - ext/zstdruby/zstdruby.h
143
+ - lib/zstd-ruby.rb
144
+ - lib/zstd-ruby/version.rb
145
+ - zstd-ruby.gemspec
146
+ homepage: https://github.com/SpringMT/zstd-ruby
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.6.8
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Ruby binding for zstd(Zstandard - Fast real-time compression algorithm)
170
+ test_files: []