extzstd 0.0.2.CONCEPT-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/ext/extzstd.c ADDED
@@ -0,0 +1,193 @@
1
+ #include "extzstd.h"
2
+
3
+ VALUE mZstd;
4
+ VALUE eError;
5
+
6
+ static size_t
7
+ aux_ZSTD_compress_nogvl(va_list *vp)
8
+ {
9
+ char *dest = va_arg(*vp, char *);
10
+ size_t destsize = va_arg(*vp, size_t);
11
+ const char *src = va_arg(*vp, const char *);
12
+ size_t srcsize = va_arg(*vp, size_t);
13
+ return ZSTD_compress(dest, destsize, src, srcsize);
14
+ }
15
+
16
+ static inline size_t
17
+ aux_ZSTD_compress(char *dest, size_t destsize, const char *src, size_t srcsize)
18
+ {
19
+ return (size_t)aux_thread_call_without_gvl(
20
+ (void *(*)(void *))aux_ZSTD_compress_nogvl, NULL,
21
+ dest, destsize, src, srcsize);
22
+ }
23
+
24
+ static inline void
25
+ zstd_s_encode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
26
+ {
27
+ switch (argc) {
28
+ case 1:
29
+ *src = argv[0];
30
+ rb_check_type(*src, RUBY_T_STRING);
31
+ *maxsize = ZSTD_compressBound(RSTRING_LEN(*src));
32
+ *dest = rb_str_buf_new(*maxsize);
33
+ return;
34
+ case 2:
35
+ *src = argv[0];
36
+ rb_check_type(*src, RUBY_T_STRING);
37
+ *dest = argv[1];
38
+ if (rb_type_p(*dest, RUBY_T_STRING)) {
39
+ *maxsize = ZSTD_compressBound(RSTRING_LEN(*src));
40
+ rb_str_resize(*dest, *maxsize);
41
+ } else {
42
+ *maxsize = NUM2SIZET(*dest);
43
+ *dest = rb_str_buf_new(*maxsize);
44
+ }
45
+ return;
46
+ case 3:
47
+ *src = argv[0];
48
+ rb_check_type(*src, RUBY_T_STRING);
49
+ *maxsize = NUM2SIZET(argv[1]);
50
+ *dest = argv[2];
51
+ rb_check_type(*dest, RUBY_T_STRING);
52
+ rb_str_resize(*dest, *maxsize);
53
+ return;
54
+ default:
55
+ rb_error_arity(argc, 1, 3);
56
+ }
57
+ }
58
+
59
+ /*
60
+ * call-seq:
61
+ * encode(src) -> encoded string
62
+ * encode(src, size) -> encoded string
63
+ * encode(src, dest) -> dest with encoded string
64
+ * encode(src, size, dest) -> dest with encoded string
65
+ */
66
+ static VALUE
67
+ zstd_s_encode(int argc, VALUE argv[], VALUE mod)
68
+ {
69
+ VALUE src, dest;
70
+ size_t maxsize;
71
+ zstd_s_encode_args(argc, argv, &src, &dest, &maxsize);
72
+ const char *srcp;
73
+ size_t srcsize;
74
+ RSTRING_GETMEM(src, srcp, srcsize);
75
+ rb_obj_infect(dest, src);
76
+ size_t s = aux_ZSTD_compress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
77
+ if (ZSTD_isError(s)) {
78
+ rb_raise(eError,
79
+ "failed ZSTD_compress - %s(%d) in %s:%d:%s",
80
+ ZSTD_getErrorName(s), (int)s,
81
+ __FILE__, __LINE__, __func__);
82
+ }
83
+ if (s > maxsize) {
84
+ rb_bug("%s:%d:%s: detect buffer overflow in ZSTD_compress - maxsize is %zd, but returned size is %zd",
85
+ __FILE__, __LINE__, __func__, maxsize, s);
86
+ }
87
+ rb_str_set_len(dest, s);
88
+ return dest;
89
+ }
90
+
91
+ static size_t
92
+ aux_ZSTD_decompress_nogvl(va_list *vp)
93
+ {
94
+ char *dest = va_arg(*vp, char *);
95
+ size_t destsize = va_arg(*vp, size_t);
96
+ const char *src = va_arg(*vp, const char *);
97
+ size_t srcsize = va_arg(*vp, size_t);
98
+ return ZSTD_decompress(dest, destsize, src, srcsize);
99
+ }
100
+
101
+ static inline size_t
102
+ aux_ZSTD_decompress(char *dest, size_t destsize, const char *src, size_t srcsize)
103
+ {
104
+ return (size_t)aux_thread_call_without_gvl(
105
+ (void *(*)(void *))aux_ZSTD_decompress_nogvl, NULL,
106
+ dest, destsize, src, srcsize);
107
+ }
108
+
109
+ static inline void
110
+ zstd_s_decode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
111
+ {
112
+ switch (argc) {
113
+ case 2:
114
+ *src = argv[0];
115
+ rb_check_type(*src, RUBY_T_STRING);
116
+ *maxsize = NUM2SIZET(argv[1]);
117
+ *dest = rb_str_buf_new(*maxsize);
118
+ return;
119
+ case 3:
120
+ *src = argv[0];
121
+ rb_check_type(*src, RUBY_T_STRING);
122
+ *maxsize = NUM2SIZET(argv[1]);
123
+ *dest = argv[2];
124
+ rb_check_type(*dest, RUBY_T_STRING);
125
+ rb_str_resize(*dest, *maxsize);
126
+ return;
127
+ default:
128
+ rb_error_arity(argc, 2, 3);
129
+ }
130
+ }
131
+
132
+ /*
133
+ * call-seq:
134
+ * decode(src, size) -> decoded string
135
+ * decode(src, size, dest) -> dest with decoded string
136
+ */
137
+ static VALUE
138
+ zstd_s_decode(int argc, VALUE argv[], VALUE mod)
139
+ {
140
+ VALUE src, dest;
141
+ size_t maxsize;
142
+ zstd_s_decode_args(argc, argv, &src, &dest, &maxsize);
143
+ const char *srcp;
144
+ size_t srcsize;
145
+ RSTRING_GETMEM(src, srcp, srcsize);
146
+ rb_obj_infect(dest, src);
147
+ size_t s = aux_ZSTD_decompress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
148
+ if (ZSTD_isError(s)) {
149
+ rb_raise(eError,
150
+ "failed ZSTD_decompress - %s(%d) in %s:%d:%s",
151
+ ZSTD_getErrorName(s), (int)s,
152
+ __FILE__, __LINE__, __func__);
153
+ }
154
+ if (s > maxsize) {
155
+ rb_bug("%s:%d:%s: detect buffer overflow in ZSTD_compress - maxsize is %zd, but returned size is %zd",
156
+ __FILE__, __LINE__, __func__, maxsize, s);
157
+ }
158
+ rb_str_set_len(dest, s);
159
+ return dest;
160
+ }
161
+
162
+ static VALUE
163
+ libver_s_to_s(VALUE ver)
164
+ {
165
+ static VALUE str;
166
+ if (!str) {
167
+ str = rb_sprintf("%d.%d.%d",
168
+ ZSTD_VERSION_MAJOR,
169
+ ZSTD_VERSION_MINOR,
170
+ ZSTD_VERSION_RELEASE);
171
+ }
172
+ return str;
173
+ }
174
+
175
+ void
176
+ Init_extzstd(void)
177
+ {
178
+ mZstd = rb_define_module("Zstd");
179
+ rb_define_singleton_method(mZstd, "encode", RUBY_METHOD_FUNC(zstd_s_encode), -1);
180
+ rb_define_singleton_method(mZstd, "decode", RUBY_METHOD_FUNC(zstd_s_decode), -1);
181
+
182
+ VALUE libver = rb_ary_new3(3,
183
+ INT2FIX(ZSTD_VERSION_MAJOR),
184
+ INT2FIX(ZSTD_VERSION_MINOR),
185
+ INT2FIX(ZSTD_VERSION_RELEASE));
186
+ rb_define_singleton_method(libver, "to_s", RUBY_METHOD_FUNC(libver_s_to_s), 0);
187
+ rb_obj_freeze(libver);
188
+ rb_define_const(mZstd, "LIBRARY_VERSION", libver);
189
+
190
+ eError = rb_define_class_under(mZstd, "Error", rb_eRuntimeError);
191
+
192
+ init_extzstd_stream();
193
+ }
data/ext/extzstd.h ADDED
@@ -0,0 +1,79 @@
1
+ #ifndef EXTZSTD_H
2
+ #define EXTZSTD_H 1
3
+
4
+ #include <stdarg.h>
5
+ #include <ruby.h>
6
+ #include <ruby/thread.h>
7
+ #include <zstd.h>
8
+ #include <zstd_static.h>
9
+
10
+ #define RDOCFAKE(DUMMY_CODE)
11
+
12
+ extern VALUE mZstd;
13
+ extern VALUE eError;
14
+
15
+ void init_extzstd_stream(void);
16
+
17
+ static inline void
18
+ referror(VALUE v)
19
+ {
20
+ rb_raise(rb_eRuntimeError,
21
+ "invalid reference - not initialized yet (#<%s:%p>)",
22
+ rb_obj_classname(v), (void *)v);
23
+ }
24
+
25
+ static inline void
26
+ reiniterror(VALUE v)
27
+ {
28
+ rb_raise(rb_eRuntimeError,
29
+ "already initialized (#<%s:%p>)",
30
+ rb_obj_classname(v), (void *)v);
31
+ }
32
+
33
+ static inline void *
34
+ checkref(VALUE v, void *p)
35
+ {
36
+ if (!p) { referror(v); }
37
+ return p;
38
+ }
39
+
40
+ static inline void *
41
+ getrefp(VALUE v, const rb_data_type_t *type)
42
+ {
43
+ void *p;
44
+ TypedData_Get_Struct(v, void, type, p);
45
+ return p;
46
+ }
47
+
48
+ static inline void *
49
+ getref(VALUE v, const rb_data_type_t *type)
50
+ {
51
+ return checkref(v, getrefp(v, type));
52
+ }
53
+
54
+ static inline VALUE
55
+ aux_str_modify_expand(VALUE s, size_t z)
56
+ {
57
+ rb_check_type(s, RUBY_T_STRING);
58
+ size_t size = RSTRING_LEN(s);
59
+ if (z > size) {
60
+ rb_str_modify_expand(s, z - size);
61
+ } else {
62
+ rb_str_modify(s);
63
+ }
64
+ return s;
65
+ }
66
+
67
+ static inline void *
68
+ aux_thread_call_without_gvl(void *(*func)(void *), void (*cancel)(void *), ...)
69
+ {
70
+ va_list va1, va2;
71
+ va_start(va1, cancel);
72
+ va_start(va2, cancel);
73
+ void *p = rb_thread_call_without_gvl(func, &va1, cancel, &va2);
74
+ va_end(va1);
75
+ va_end(va2);
76
+ return p;
77
+ }
78
+
79
+ #endif /* !EXTZSTD_H */
data/gemstub.rb ADDED
@@ -0,0 +1,19 @@
1
+ require_relative "lib/extzstd/version"
2
+
3
+ GEMSTUB = Gem::Specification.new do |s|
4
+ s.name = "extzstd"
5
+ s.version = Zstd::VERSION
6
+ s.summary = "ruby bindings for Zstandard (zstd)"
7
+ s.description = <<EOS
8
+ unoficial ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
9
+ EOS
10
+ s.homepage = "https://osdn.jp/projects/rutsubo/"
11
+ s.license = "2-clause BSD License"
12
+ s.author = "dearblue"
13
+ s.email = "dearblue@users.osdn.me"
14
+
15
+ s.required_ruby_version = ">= 2.0"
16
+ s.add_development_dependency "rake", "~> 10.0"
17
+ end
18
+
19
+ EXTRA.concat(FileList["contrib/**/*"])
Binary file
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ module Zstd
2
+ VERSION = "0.0.2.CONCEPT"
3
+ end
data/lib/extzstd.rb ADDED
@@ -0,0 +1,138 @@
1
+ #vim: set fileencoding:utf-8
2
+
3
+ ver = RbConfig::CONFIG["ruby_version"].slice(/\d+\.\d+/)
4
+ soname = File.basename(__FILE__, ".rb") << ".so"
5
+ lib = File.join(File.dirname(__FILE__), ver, soname)
6
+ if File.file?(lib)
7
+ require_relative File.join(ver, soname)
8
+ else
9
+ require_relative soname
10
+ end
11
+
12
+ require_relative "extzstd/version"
13
+
14
+ require "stringio"
15
+
16
+ if false
17
+ def p(*args)
18
+ sf = File.basename(caller(1, 1)[0])
19
+ args.each do |mesg|
20
+ $stderr.puts "#{sf}: #{mesg.inspect}\n"
21
+ end
22
+ return *args
23
+ end
24
+ end
25
+
26
+ module Zstd
27
+ module Aux
28
+ module_function
29
+ def io_read(io, size, buf)
30
+ raise Error, "encounted EOF (read error)" unless io.read(size, buf)
31
+ raise Error, "read size too small (read error)" unless buf.bytesize == size
32
+ buf
33
+ end
34
+ end
35
+
36
+ class Decoder < Struct.new(:decoder, :import, :readbuf, :destbuf, :status)
37
+ BLOCKSIZE = 1 << 18
38
+ STATUS_TERMINATE = nil
39
+ STATUS_BLOCK_APPROACH = 1
40
+ STATUS_INBLOCK = 2
41
+
42
+ #
43
+ # call-seq:
44
+ # open(import) -> decoder
45
+ # open(import) { |decoder| ... } -> yield returned value
46
+ #
47
+ # [import]
48
+ # String instance or +read+ method haved Object.
49
+ #
50
+ def self.open(import)
51
+ import = StringIO.new(import) if import.kind_of?(String)
52
+ dec = new(import)
53
+
54
+ return dec unless block_given?
55
+
56
+ begin
57
+ yield(dec)
58
+ ensure
59
+ dec.close rescue nil
60
+ end
61
+ end
62
+
63
+ def initialize(import)
64
+ raise Error, "require .read method - <%s:0x%08x>" % [import.class, import.object_id << 1] unless import.respond_to?(:read)
65
+ super(LowLevelDecoder.new, import, "".b, "".b, STATUS_BLOCK_APPROACH)
66
+
67
+ # read header
68
+ Aux.io_read(import, decoder.next_srcsize, readbuf)
69
+ decoder.decode(readbuf, "", 0)
70
+ end
71
+
72
+ def close
73
+ decoder.reset
74
+ import.close rescue nil if import.respond_to?(:close)
75
+ readbuf.clear
76
+ destbuf.clear
77
+ self.status = STATUS_TERMINATE
78
+ nil
79
+ end
80
+
81
+ def eof
82
+ !status
83
+ end
84
+
85
+ alias eof? eof
86
+
87
+ def read(size = nil, dest = "".b)
88
+ dest.clear
89
+ destenc = dest.encoding
90
+ dest.force_encoding Encoding::BINARY
91
+
92
+ until size && size <= 0
93
+ if destbuf.empty?
94
+ unless fetch
95
+ return nil if dest.empty?
96
+ break
97
+ end
98
+ end
99
+
100
+ d = destbuf.slice!(0, size || destbuf.bytesize)
101
+ dest << d
102
+
103
+ size -= d.bytesize if size
104
+ end
105
+
106
+ dest
107
+ ensure
108
+ dest.force_encoding destenc rescue nil if destenc
109
+ end
110
+
111
+ private
112
+ def fetch
113
+ return nil if eof?
114
+
115
+ while true
116
+ if status == STATUS_INBLOCK
117
+ s = decoder.next_srcsize
118
+ if s > 0
119
+ Aux.io_read(import, s, readbuf)
120
+ return destbuf if decoder.decode(readbuf, destbuf, BLOCKSIZE)
121
+ next
122
+ end
123
+ self.status = STATUS_BLOCK_APPROACH
124
+ end
125
+
126
+ # status == STATUS_BLOCK_APPROACH
127
+ s = decoder.next_srcsize
128
+ if s == 0
129
+ self.status = STATUS_TERMINATE
130
+ return nil
131
+ end
132
+ Aux.io_read(import, s, readbuf)
133
+ decoder.decode(readbuf, "", 0)
134
+ self.status = STATUS_INBLOCK
135
+ end
136
+ end
137
+ end
138
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extzstd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.CONCEPT
5
+ platform: x86-mingw32
6
+ authors:
7
+ - dearblue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ description: |
28
+ unoficial ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
29
+ email: dearblue@users.osdn.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.md
35
+ - contrib/zstd/LICENSE
36
+ - ext/extzstd-stream.c
37
+ - ext/extzstd.c
38
+ - ext/extzstd.h
39
+ - lib/extzstd.rb
40
+ - lib/extzstd/version.rb
41
+ files:
42
+ - LICENSE
43
+ - README.md
44
+ - Rakefile
45
+ - contrib/zstd/LICENSE
46
+ - contrib/zstd/Makefile
47
+ - contrib/zstd/fse.c
48
+ - contrib/zstd/fse.h
49
+ - contrib/zstd/fse_static.h
50
+ - contrib/zstd/libzstd.pc.in
51
+ - contrib/zstd/zstd.c
52
+ - contrib/zstd/zstd.h
53
+ - contrib/zstd/zstd_static.h
54
+ - ext/extconf.rb
55
+ - ext/extzstd-stream.c
56
+ - ext/extzstd.c
57
+ - ext/extzstd.h
58
+ - gemstub.rb
59
+ - lib/2.0/extzstd.so
60
+ - lib/2.1/extzstd.so
61
+ - lib/2.2/extzstd.so
62
+ - lib/extzstd.rb
63
+ - lib/extzstd/version.rb
64
+ homepage: https://osdn.jp/projects/rutsubo/
65
+ licenses:
66
+ - 2-clause BSD License
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options:
70
+ - "--charset"
71
+ - UTF-8
72
+ - "-m"
73
+ - README.md
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '2.0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">"
84
+ - !ruby/object:Gem::Version
85
+ version: 1.3.1
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.8
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: ruby bindings for Zstandard (zstd)
92
+ test_files: []
93
+ has_rdoc: