yaz0 0.1.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b98ce34773d7c314618d2dd957582ae19852e5741cc71a5a2703db1e1b8d6f71
4
- data.tar.gz: c85ece9160207be1ce84c2edb5b0935c2cee6bf1d84b3b0aa0dd43c25f76ffaa
3
+ metadata.gz: 4dea3dd6f151a4a0aeebb87258fd9ca033d92a91462ebe2ad9e121777c23aea5
4
+ data.tar.gz: c9885eb2142c21af2026b226c3c2a4175e8b7f3f1ac745d013135b282c963703
5
5
  SHA512:
6
- metadata.gz: b5c80c5a7a0c28240fd3d315ebe5f31aa02a13e64012935778176d26e46575b4f81e197a68c0faa4e343e9d475aa9da344a546b0578bd3b34917b9585fcce35c
7
- data.tar.gz: 9ba7d55ba7dc73ef3b1bd6e9d041112de31a2055b9eff0b48eb6fb5ec1a86832268161be3c48238b35284d49d0a3b18c535ae84f1ddb295ca6543f7c37421eda
6
+ metadata.gz: 483a8e9de03d2c6f0e5fea0a0be1b048a2b46641528e3b00e165effc7a2794d3b04aa84d8aa5d07c9509baee680ee7783cba3fd0fa44b2e9435c44fd03011904
7
+ data.tar.gz: 243f77ffbd66fe1c0df94e474dcf92297e792bfb02fb3481e5518a5627c733c505cf5f282ff338a9d6a692ccd5d39e9c6f5b3984f364ac390d99976be454b572
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_set_len(buffer_out, yaz0OutputChunkSize(s));
110
+ rb_funcall(io_out, rb_intern("write"), 1, buffer_out);
111
+ rb_str_set_len(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_set_len(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.1.1"
2
+ VERSION = "0.4.0"
3
3
  end
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")