extzstd 0.0.1.CONCEPT → 0.0.2.CONCEPT

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,8 +3,26 @@
3
3
  VALUE mZstd;
4
4
  VALUE eError;
5
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
+
6
24
  static inline void
7
- zstd_encode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
25
+ zstd_s_encode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
8
26
  {
9
27
  switch (argc) {
10
28
  case 1:
@@ -46,31 +64,50 @@ zstd_encode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsiz
46
64
  * encode(src, size, dest) -> dest with encoded string
47
65
  */
48
66
  static VALUE
49
- zstd_encode(int argc, VALUE argv[], VALUE mod)
67
+ zstd_s_encode(int argc, VALUE argv[], VALUE mod)
50
68
  {
51
69
  VALUE src, dest;
52
70
  size_t maxsize;
53
- zstd_encode_args(argc, argv, &src, &dest, &maxsize);
71
+ zstd_s_encode_args(argc, argv, &src, &dest, &maxsize);
54
72
  const char *srcp;
55
73
  size_t srcsize;
56
74
  RSTRING_GETMEM(src, srcp, srcsize);
57
- size_t s = ZSTD_compress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
75
+ rb_obj_infect(dest, src);
76
+ size_t s = aux_ZSTD_compress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
58
77
  if (ZSTD_isError(s)) {
59
78
  rb_raise(eError,
60
- "%s:%d:%s: ZSTD_compress error - %s (%d)",
61
- __FILE__, __LINE__, __func__,
62
- ZSTD_getErrorName(s), (int)s);
79
+ "failed ZSTD_compress - %s(%d) in %s:%d:%s",
80
+ ZSTD_getErrorName(s), (int)s,
81
+ __FILE__, __LINE__, __func__);
63
82
  }
64
83
  if (s > maxsize) {
65
84
  rb_bug("%s:%d:%s: detect buffer overflow in ZSTD_compress - maxsize is %zd, but returned size is %zd",
66
- __FILE__, __LINE__, __func__, maxsize, s);
85
+ __FILE__, __LINE__, __func__, maxsize, s);
67
86
  }
68
87
  rb_str_set_len(dest, s);
69
88
  return dest;
70
89
  }
71
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
+
72
109
  static inline void
73
- zstd_decode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
110
+ zstd_s_decode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
74
111
  {
75
112
  switch (argc) {
76
113
  case 2:
@@ -98,35 +135,57 @@ zstd_decode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsiz
98
135
  * decode(src, size, dest) -> dest with decoded string
99
136
  */
100
137
  static VALUE
101
- zstd_decode(int argc, VALUE argv[], VALUE mod)
138
+ zstd_s_decode(int argc, VALUE argv[], VALUE mod)
102
139
  {
103
140
  VALUE src, dest;
104
141
  size_t maxsize;
105
- zstd_decode_args(argc, argv, &src, &dest, &maxsize);
142
+ zstd_s_decode_args(argc, argv, &src, &dest, &maxsize);
106
143
  const char *srcp;
107
144
  size_t srcsize;
108
145
  RSTRING_GETMEM(src, srcp, srcsize);
109
- size_t s = ZSTD_decompress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
146
+ rb_obj_infect(dest, src);
147
+ size_t s = aux_ZSTD_decompress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
110
148
  if (ZSTD_isError(s)) {
111
149
  rb_raise(eError,
112
- "%s:%d:%s: ZSTD_compress error - %s (%d)",
113
- __FILE__, __LINE__, __func__,
114
- ZSTD_getErrorName(s), (int)s);
150
+ "failed ZSTD_decompress - %s(%d) in %s:%d:%s",
151
+ ZSTD_getErrorName(s), (int)s,
152
+ __FILE__, __LINE__, __func__);
115
153
  }
116
154
  if (s > maxsize) {
117
155
  rb_bug("%s:%d:%s: detect buffer overflow in ZSTD_compress - maxsize is %zd, but returned size is %zd",
118
- __FILE__, __LINE__, __func__, maxsize, s);
156
+ __FILE__, __LINE__, __func__, maxsize, s);
119
157
  }
120
158
  rb_str_set_len(dest, s);
121
159
  return dest;
122
160
  }
123
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
+
124
175
  void
125
176
  Init_extzstd(void)
126
177
  {
127
178
  mZstd = rb_define_module("Zstd");
128
- rb_define_singleton_method(mZstd, "encode", RUBY_METHOD_FUNC(zstd_encode), -1);
129
- rb_define_singleton_method(mZstd, "decode", RUBY_METHOD_FUNC(zstd_decode), -1);
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);
130
189
 
131
190
  eError = rb_define_class_under(mZstd, "Error", rb_eRuntimeError);
132
191
 
@@ -1,13 +1,15 @@
1
1
  #ifndef EXTZSTD_H
2
2
  #define EXTZSTD_H 1
3
3
 
4
+ #include <stdarg.h>
4
5
  #include <ruby.h>
6
+ #include <ruby/thread.h>
5
7
  #include <zstd.h>
6
8
  #include <zstd_static.h>
7
9
 
10
+ #define RDOCFAKE(DUMMY_CODE)
11
+
8
12
  extern VALUE mZstd;
9
- extern VALUE cStreamEncoder;
10
- extern VALUE cStreamDecoder;
11
13
  extern VALUE eError;
12
14
 
13
15
  void init_extzstd_stream(void);
@@ -49,4 +51,29 @@ getref(VALUE v, const rb_data_type_t *type)
49
51
  return checkref(v, getrefp(v, type));
50
52
  }
51
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
+
52
79
  #endif /* !EXTZSTD_H */
data/gemstub.rb CHANGED
@@ -5,12 +5,12 @@ GEMSTUB = Gem::Specification.new do |s|
5
5
  s.version = Zstd::VERSION
6
6
  s.summary = "ruby bindings for Zstandard (zstd)"
7
7
  s.description = <<EOS
8
- ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
8
+ unoficial ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
9
9
  EOS
10
- s.homepage = "http://sourceforge.jp/projects/rutsubo/"
10
+ s.homepage = "https://osdn.jp/projects/rutsubo/"
11
11
  s.license = "2-clause BSD License"
12
12
  s.author = "dearblue"
13
- s.email = "dearblue@users.sourceforge.jp"
13
+ s.email = "dearblue@users.osdn.me"
14
14
 
15
15
  s.required_ruby_version = ">= 2.0"
16
16
  s.add_development_dependency "rake", "~> 10.0"
@@ -1,6 +1,6 @@
1
1
  #vim: set fileencoding:utf-8
2
2
 
3
- ver = RbConfig::CONFIG["ruby_version"]
3
+ ver = RbConfig::CONFIG["ruby_version"].slice(/\d+\.\d+/)
4
4
  soname = File.basename(__FILE__, ".rb") << ".so"
5
5
  lib = File.join(File.dirname(__FILE__), ver, soname)
6
6
  if File.file?(lib)
@@ -10,3 +10,129 @@ else
10
10
  end
11
11
 
12
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
@@ -1,5 +1,3 @@
1
- require "rubygems"
2
-
3
1
  module Zstd
4
- VERSION = Gem::Version.new("0.0.1.CONCEPT")
2
+ VERSION = "0.0.2.CONCEPT"
5
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extzstd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.CONCEPT
4
+ version: 0.0.2.CONCEPT
5
5
  platform: ruby
6
6
  authors:
7
7
  - dearblue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-26 00:00:00.000000000 Z
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -25,14 +25,15 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '10.0'
27
27
  description: |
28
- ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
29
- email: dearblue@users.sourceforge.jp
28
+ unoficial ruby bindings for Zstandard (zstd) <https://github.com/Cyan4973/zstd>.
29
+ email: dearblue@users.osdn.me
30
30
  executables: []
31
31
  extensions:
32
32
  - ext/extconf.rb
33
33
  extra_rdoc_files:
34
34
  - LICENSE
35
35
  - README.md
36
+ - contrib/zstd/LICENSE
36
37
  - ext/extzstd-stream.c
37
38
  - ext/extzstd.c
38
39
  - ext/extzstd.h
@@ -58,12 +59,16 @@ files:
58
59
  - gemstub.rb
59
60
  - lib/extzstd.rb
60
61
  - lib/extzstd/version.rb
61
- homepage: http://sourceforge.jp/projects/rutsubo/
62
+ homepage: https://osdn.jp/projects/rutsubo/
62
63
  licenses:
63
64
  - 2-clause BSD License
64
65
  metadata: {}
65
66
  post_install_message:
66
- rdoc_options: []
67
+ rdoc_options:
68
+ - "--charset"
69
+ - UTF-8
70
+ - "-m"
71
+ - README.md
67
72
  require_paths:
68
73
  - lib
69
74
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -78,8 +83,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
83
  version: 1.3.1
79
84
  requirements: []
80
85
  rubyforge_project:
81
- rubygems_version: 2.4.6
86
+ rubygems_version: 2.4.8
82
87
  signing_key:
83
88
  specification_version: 4
84
89
  summary: ruby bindings for Zstandard (zstd)
85
90
  test_files: []
91
+ has_rdoc: