yaz0 0.2.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 672cee69a6f75dd7377cf5eb17baeb36275224f98021be0a26f575488b29ea35
4
- data.tar.gz: 7ac698b98f48d2816a2b0852e1b8937a9f6d77613b94e7e8cc81f8e7ff03df08
3
+ metadata.gz: 42f0c89e2edf019afff5f5e96f7cbf1797036ab93ed0725cf58efcdf23607e8f
4
+ data.tar.gz: 03440e46538b5197eea2d7d490ace2137d83879948ab23e066c779ceffad070b
5
5
  SHA512:
6
- metadata.gz: 488b8a8e52fc4e9d74caf1db27383cd6be5a5307c8a703b74cc3ec4c175c71bf883c1c4fc82146646ed79a53728c4d6508638914840b5ca16633a0bcbf61f97c
7
- data.tar.gz: a06e7544dbad27a7d1647ba621d2abadea7e17cb4fed47a1fbf78e91e4fff81af56955f7423b028265bcd2b51cfadc724ff9dd79b48d72cf7dffc4103f2c0863
6
+ metadata.gz: 20d3ce38706053cce8975aa079e02b267418f80c4eca0f054b0a09d6116c9a74ce8badbfb4fd12f7c7e88ef074b16d00bef251275961ce38cec6510997e4c3f8
7
+ data.tar.gz: f5ae97c8115585c113fc5cd139c393c253cb1efc217815e108afdd4f720db315b2e19dc688696f63f3385a0771583a5a2dfc64192ffb71eabfecfe7ecf026a13
data/Gemfile CHANGED
@@ -3,6 +3,6 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in yaz0.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
6
+ gem "rake", "~> 13.0"
7
7
  gem "rake-compiler"
8
8
  gem "rspec", "~> 3.0"
@@ -0,0 +1,165 @@
1
+ #include "ext_yaz0.h"
2
+ #include <stdio.h>
3
+ #include <stdint.h>
4
+
5
+ #define BUFSIZE 0x4000
6
+
7
+ static void ext_yaz0_stream_free(void*);
8
+
9
+ static VALUE class_yaz0_stream;
10
+ static VALUE class_yaz0_error;
11
+ static VALUE class_yaz0_error_bad_magic;
12
+ static VALUE class_yaz0_error_end_of_file;
13
+
14
+ static struct rb_data_type_struct type_yaz0_stream = {
15
+ "yaz0_stream",
16
+ { NULL, ext_yaz0_stream_free, NULL },
17
+ NULL,
18
+ NULL,
19
+ RUBY_TYPED_FREE_IMMEDIATELY
20
+ };
21
+
22
+ static void
23
+ ext_yaz0_stream_free(void* s)
24
+ {
25
+ yaz0Destroy((Yaz0Stream*)s);
26
+ }
27
+
28
+ static VALUE
29
+ ext_yaz0_stream_alloc(VALUE klass)
30
+ {
31
+ Yaz0Stream* s;
32
+ int ret;
33
+
34
+ ret = yaz0Init(&s);
35
+ if (ret == YAZ0_OUT_OF_MEMORY)
36
+ rb_raise(rb_eNoMemError, "Out of memory");
37
+ return TypedData_Wrap_Struct(klass, &type_yaz0_stream, s);
38
+ }
39
+
40
+ static void*
41
+ run_NoGVL(void* arg)
42
+ {
43
+ return (void*)(intptr_t)yaz0Run((Yaz0Stream*)arg);
44
+ }
45
+
46
+ static VALUE
47
+ run(VALUE self, VALUE io_in, VALUE io_out, int compress, int size, int level)
48
+ {
49
+ Yaz0Stream* s;
50
+ VALUE buffer_in;
51
+ VALUE buffer_out;
52
+ VALUE tmp;
53
+ int in_is_str;
54
+ int ret;
55
+
56
+ in_is_str = TYPE(io_in) == T_STRING;
57
+
58
+ TypedData_Get_Struct(self, Yaz0Stream, &type_yaz0_stream, s);
59
+ if (compress)
60
+ yaz0ModeCompress(s, size, level);
61
+ else
62
+ yaz0ModeDecompress(s);
63
+
64
+ /* Init the buffers */
65
+ if (!in_is_str)
66
+ {
67
+ buffer_in = rb_str_new(NULL, 0);
68
+ rb_str_resize(buffer_in, BUFSIZE);
69
+ rb_gc_register_address(&buffer_in);
70
+ rb_funcall(io_in, rb_intern("read"), 2, INT2FIX(BUFSIZE), buffer_in);
71
+ }
72
+ else
73
+ {
74
+ buffer_in = rb_obj_dup(io_in);
75
+ rb_gc_register_address(&buffer_in);
76
+ }
77
+ yaz0Input(s, RSTRING_PTR(buffer_in), (uint32_t)RSTRING_LEN(buffer_in));
78
+
79
+ buffer_out = rb_str_new(NULL, 0);
80
+ rb_str_resize(buffer_out, BUFSIZE);
81
+ rb_gc_register_address(&buffer_out);
82
+ yaz0Output(s, RSTRING_PTR(buffer_out), BUFSIZE);
83
+
84
+ for (;;)
85
+ {
86
+ ret = (int)(intptr_t)rb_thread_call_without_gvl(run_NoGVL, s, RUBY_UBF_IO, NULL);
87
+ switch (ret)
88
+ {
89
+ case YAZ0_NEED_AVAIL_IN:
90
+ /* Need more input */
91
+ if (in_is_str)
92
+ {
93
+ rb_gc_unregister_address(&buffer_in);
94
+ rb_gc_unregister_address(&buffer_out);
95
+ rb_raise(class_yaz0_error_end_of_file, "Unexpected end of file");
96
+ }
97
+
98
+ tmp = rb_funcall(io_in, rb_intern("read"), 2, INT2FIX(BUFSIZE), buffer_in);
99
+ if (tmp == Qnil)
100
+ {
101
+ rb_gc_unregister_address(&buffer_in);
102
+ rb_gc_unregister_address(&buffer_out);
103
+ rb_raise(class_yaz0_error_end_of_file, "Unexpected end of file");
104
+ }
105
+ yaz0Input(s, RSTRING_PTR(buffer_in), (uint32_t)RSTRING_LEN(buffer_in));
106
+ break;
107
+ case YAZ0_NEED_AVAIL_OUT:
108
+ /* Need more output */
109
+ rb_str_resize(buffer_out, yaz0OutputChunkSize(s));
110
+ rb_funcall(io_out, rb_intern("write"), 1, buffer_out);
111
+ rb_str_resize(buffer_out, BUFSIZE);
112
+ yaz0Output(s, RSTRING_PTR(buffer_out), BUFSIZE);
113
+ break;
114
+ case YAZ0_BAD_MAGIC:
115
+ rb_gc_unregister_address(&buffer_in);
116
+ rb_gc_unregister_address(&buffer_out);
117
+ rb_raise(class_yaz0_error_bad_magic, "Bad magic");
118
+ break;
119
+ case YAZ0_OK:
120
+ goto end;
121
+ }
122
+ }
123
+
124
+ end:
125
+ /* There might still be unflushed output */
126
+ rb_str_resize(buffer_out, yaz0OutputChunkSize(s));
127
+ rb_funcall(io_out, rb_intern("write"), 1, buffer_out);
128
+
129
+ rb_gc_unregister_address(&buffer_in);
130
+ rb_gc_unregister_address(&buffer_out);
131
+
132
+ return Qnil;
133
+ }
134
+
135
+ static VALUE
136
+ ext_yaz0_stream_raw_decompress(VALUE self, VALUE io_in, VALUE io_out)
137
+ {
138
+ return run(self, io_in, io_out, 0, 0, 0);
139
+ }
140
+
141
+ static VALUE
142
+ ext_yaz0_stream_raw_compress(VALUE self, VALUE io_in, VALUE io_out, VALUE size, VALUE level)
143
+ {
144
+ Check_Type(size, T_FIXNUM);
145
+ Check_Type(level, T_FIXNUM);
146
+ return run(self, io_in, io_out, 1, FIX2INT(size), FIX2INT(level));
147
+ }
148
+
149
+ void
150
+ Init_yaz0(void)
151
+ {
152
+ VALUE mod;
153
+ mod = rb_define_module("Yaz0");
154
+
155
+ /* Error classes */
156
+ class_yaz0_error = rb_define_class_under(mod, "Error", rb_eStandardError);
157
+ class_yaz0_error_bad_magic = rb_define_class_under(mod, "BadMagicError", class_yaz0_error);
158
+ class_yaz0_error_end_of_file = rb_define_class_under(mod, "EndOfFileError", class_yaz0_error);
159
+
160
+ /* Stream */
161
+ class_yaz0_stream = rb_define_class_under(mod, "Stream", rb_cObject);
162
+ rb_define_alloc_func(class_yaz0_stream, ext_yaz0_stream_alloc);
163
+ rb_define_method(class_yaz0_stream, "raw_decompress", ext_yaz0_stream_raw_decompress, 2);
164
+ rb_define_method(class_yaz0_stream, "raw_compress", ext_yaz0_stream_raw_compress, 4);
165
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef EXT_YAZ0_H
2
+ #define EXT_YAZ0_H 1
3
+
4
+ #include <ruby.h>
5
+ #include <ruby/thread.h>
6
+ #include <yaz0.h>
7
+
8
+ #endif /* YAZ0_H */
data/ext/yaz0/extconf.rb CHANGED
@@ -1,3 +1,13 @@
1
1
  require "mkmf"
2
2
 
3
- create_makefile("yaz0/yaz0")
3
+ extension_name = 'yaz0/yaz0'
4
+ dir_config(extension_name)
5
+
6
+ libyaz0_src = Dir[File.join(__dir__, "../../libyaz0/src/libyaz0/**/*.c")].map{|x| File.expand_path(x)}
7
+
8
+ $srcs = libyaz0_src + ["ext_yaz0.c"]
9
+
10
+ $VPATH << File.expand_path(File.join(__dir__, "../../libyaz0/src/libyaz0"))
11
+ $INCFLAGS << " -I#{File.expand_path(File.join(__dir__, "../../libyaz0/include"))}"
12
+
13
+ create_makefile(extension_name)
data/lib/yaz0/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yaz0
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/yaz0/yaz0.so ADDED
Binary file
data/lib/yaz0.rb CHANGED
@@ -1,5 +1,47 @@
1
+ require "stringio"
1
2
  require "yaz0/version"
2
3
  require "yaz0/yaz0"
3
4
 
4
5
  module Yaz0
6
+ class Stream
7
+ def decompress(src, dst = nil)
8
+ if dst
9
+ raw_decompress(src, dst)
10
+ nil
11
+ else
12
+ dst = StringIO.new
13
+ raw_decompress(src, dst)
14
+ dst.string
15
+ end
16
+ end
17
+
18
+ def compress(src, dst_or_opts = nil, opts = {})
19
+ if dst_or_opts.is_a?(Hash)
20
+ opts = dst_or_opts
21
+ dst = nil
22
+ else
23
+ dst = dst_or_opts
24
+ end
25
+
26
+ level = opts[:level] || 6
27
+ size = opts[:size] || src.size
28
+
29
+ if dst
30
+ raw_compress(src, dst, size, level)
31
+ nil
32
+ else
33
+ dst = StringIO.new
34
+ raw_compress(src, dst, size, level)
35
+ dst.string
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.decompress(*args)
41
+ Yaz0::Stream.new.decompress(*args)
42
+ end
43
+
44
+ def self.compress(*args)
45
+ Yaz0::Stream.new.compress(*args)
46
+ end
5
47
  end
@@ -0,0 +1,34 @@
1
+ #ifndef YAZ0_H
2
+ #define YAZ0_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #if defined(__cplusplus)
8
+ # define YAZ0_API extern "C"
9
+ #else
10
+ # define YAZ0_API
11
+ #endif
12
+
13
+ #define YAZ0_OK 0
14
+ #define YAZ0_NEED_AVAIL_IN 1
15
+ #define YAZ0_NEED_AVAIL_OUT 2
16
+ #define YAZ0_BAD_MAGIC (-1)
17
+ #define YAZ0_OUT_OF_MEMORY (-2)
18
+
19
+ #define YAZ0_DEFAULT_LEVEL 6
20
+
21
+ typedef struct Yaz0Stream Yaz0Stream;
22
+
23
+ YAZ0_API int yaz0Init(Yaz0Stream** stream);
24
+ YAZ0_API int yaz0Destroy(Yaz0Stream* stream);
25
+ YAZ0_API int yaz0ModeDecompress(Yaz0Stream* stream);
26
+ YAZ0_API int yaz0ModeCompress(Yaz0Stream* stream, uint32_t size, int level);
27
+ YAZ0_API int yaz0Run(Yaz0Stream* stream);
28
+ YAZ0_API int yaz0Input(Yaz0Stream* stream, const void* data, uint32_t size);
29
+ YAZ0_API int yaz0Output(Yaz0Stream* stream, void* data, uint32_t size);
30
+
31
+ YAZ0_API uint32_t yaz0OutputChunkSize(const Yaz0Stream* stream);
32
+ YAZ0_API uint32_t yaz0DecompressedSize(const Yaz0Stream* stream);
33
+
34
+ #endif /* YAZ0_H */
@@ -0,0 +1,3 @@
1
+ file(GLOB SOURCES "*.c")
2
+ add_library(libyaz0 STATIC ${SOURCES})
3
+ set_target_properties(libyaz0 PROPERTIES OUTPUT_NAME "yaz0")