lz4-ruby 0.1.6-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,125 @@
1
+ #include <ruby.h>
2
+ #include "lz4.h"
3
+ #include "lz4hc.h"
4
+
5
+ typedef int (*CompressFunc)(const char *source, char *dest, int isize);
6
+
7
+ static VALUE lz4;
8
+ static VALUE lz4_error;
9
+
10
+ static int encode_varbyte(int value, char *buf) {
11
+ buf[0] = value & 0x7f; value >>= 7;
12
+ if (value == 0) { return 1; }
13
+ buf[0] |= 0x80;
14
+
15
+ buf[1] = value & 0x7f; value >>= 7;
16
+ if (value == 0) { return 2; }
17
+ buf[1] |= 0x80;
18
+
19
+ buf[2] = value & 0x7f; value >>= 7;
20
+ if (value == 0) { return 3; }
21
+ buf[2] |= 0x80;
22
+
23
+ buf[3] = value & 0x7f; value >>= 7;
24
+ if (value == 0) { return 4; }
25
+ buf[3] |= 0x80;
26
+
27
+ buf[4] = value & 0x7f;
28
+ return 5;
29
+ }
30
+
31
+ static int decode_varbyte(const char *src, int len, int *value) {
32
+ if (len < 1) { return -1; }
33
+
34
+ *value = src[0] & 0x7f;
35
+ if ((src[0] & 0x80) == 0) { return 1; }
36
+ if (len < 2) { return -1; }
37
+
38
+ *value |= (src[1] & 0x7f) << 7;
39
+ if ((src[1] & 0x80) == 0) { return 2; }
40
+ if (len < 3) { return -1; }
41
+
42
+ *value |= (src[2] & 0x7f) << 14;
43
+ if ((src[2] & 0x80) == 0) { return 3; }
44
+ if (len < 4) { return -1; }
45
+
46
+ *value |= (src[3] & 0x7f) << 21;
47
+ if ((src[3] & 0x80) == 0) { return 4; }
48
+ if (len < 5) { return -1; }
49
+
50
+ *value |= (src[4] & 0x7f) << 28;
51
+
52
+ return 5;
53
+ }
54
+
55
+ static VALUE compress(CompressFunc compressor, VALUE self, VALUE source, VALUE src_size_prm) {
56
+ const char *src_p = NULL;
57
+ char varbyte[5];
58
+ char *buf = NULL;
59
+ VALUE result;
60
+ int src_size;
61
+ int varbyte_len;
62
+ int buf_size;
63
+ int comp_size;
64
+
65
+ Check_Type(source, T_STRING);
66
+ src_p = RSTRING_PTR(source);
67
+ src_size = NUM2INT(src_size_prm);
68
+ buf_size = LZ4_compressBound(src_size);
69
+
70
+ varbyte_len = encode_varbyte(src_size, varbyte);
71
+
72
+ result = rb_str_new(NULL, buf_size + varbyte_len);
73
+ buf = RSTRING_PTR(result);
74
+
75
+ memcpy(buf, varbyte, varbyte_len);
76
+
77
+ comp_size = compressor(src_p, buf + varbyte_len, src_size);
78
+ rb_str_resize(result, comp_size + varbyte_len);
79
+
80
+ return result;
81
+ }
82
+
83
+ static VALUE lz4_ruby_compress(VALUE self, VALUE source, VALUE src_size) {
84
+ return compress(LZ4_compress, self, source, src_size);
85
+ }
86
+
87
+ static VALUE lz4_ruby_compressHC(VALUE self, VALUE source, VALUE src_size) {
88
+ return compress(LZ4_compressHC, self, source, src_size);
89
+ }
90
+
91
+ static VALUE lz4_ruby_uncompress(VALUE self, VALUE source) {
92
+ const char *src_p = NULL;
93
+ char *buf = NULL;
94
+ VALUE result;
95
+ int src_size;
96
+ int varbyte_len;
97
+ int buf_size = 0;
98
+ int read_bytes;
99
+
100
+ Check_Type(source, T_STRING);
101
+ src_p = RSTRING_PTR(source);
102
+ src_size = RSTRING_LEN(source);
103
+
104
+ varbyte_len = decode_varbyte(src_p, src_size, &buf_size);
105
+
106
+ result = rb_str_new(NULL, buf_size);
107
+ buf = RSTRING_PTR(result);
108
+
109
+ read_bytes = LZ4_uncompress(src_p + varbyte_len, buf, buf_size);
110
+ if (read_bytes < 0) {
111
+ rb_raise(lz4_error, "Compressed data is maybe corrupted.");
112
+ }
113
+
114
+ return result;
115
+ }
116
+
117
+ void Init_lz4ruby(void) {
118
+ lz4 = rb_define_module("LZ4Native");
119
+
120
+ rb_define_module_function(lz4, "compress", lz4_ruby_compress, 2);
121
+ rb_define_module_function(lz4, "compressHC", lz4_ruby_compressHC, 2);
122
+ rb_define_module_function(lz4, "uncompress", lz4_ruby_uncompress, 1);
123
+
124
+ lz4_error = rb_define_class_under(lz4, "Error", rb_eStandardError);
125
+ }
Binary file
Binary file
data/lib/lz4-ruby.rb ADDED
@@ -0,0 +1,23 @@
1
+ if /(mswin|mingw)/ =~ RUBY_PLATFORM
2
+ /(\d+\.\d+)/ =~ RUBY_VERSION
3
+ ver = $1
4
+ require "#{ver}/lz4ruby.so"
5
+ else
6
+ require 'lz4ruby'
7
+ end
8
+
9
+ class LZ4
10
+ def self.compress(source, src_size = nil)
11
+ src_size = source.length if src_size == nil
12
+ return LZ4Native::compress(source, src_size)
13
+ end
14
+
15
+ def self.compressHC(source, src_size = nil)
16
+ src_size = source.length if src_size == nil
17
+ return LZ4Native::compressHC(source, src_size)
18
+ end
19
+
20
+ def self.uncompress(source)
21
+ return LZ4Native::uncompress(source)
22
+ end
23
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'ext/lz4ruby'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+
17
+ build_native = <<EOS
18
+ cd ext/lz4-ruby
19
+ ruby extconf.rb
20
+ make clean
21
+ make
22
+ EOS
23
+ `#{build_native}`
24
+
25
+ require 'lz4-ruby'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,87 @@
1
+ require 'helper'
2
+
3
+ class TestLz4Ruby < Test::Unit::TestCase
4
+ LOOP_COUNT = 257
5
+
6
+ @@random = Random.new(123)
7
+
8
+ context "LZ4::compress" do
9
+ should "empty text" do
10
+ compressed = LZ4::compress("")
11
+ uncompressed = LZ4::uncompress(compressed)
12
+ assert_empty("", uncompressed)
13
+ end
14
+
15
+ LOOP_COUNT.times do |t|
16
+ len = t + 1
17
+ text = "a" * len
18
+
19
+ should "text of #{len} \"a\"'s" do
20
+ compressed = LZ4::compress(text)
21
+ uncompressed = LZ4::uncompress(compressed)
22
+ assert_equal(text, uncompressed)
23
+ end
24
+ end
25
+
26
+ LOOP_COUNT.times do |t|
27
+ len = t + 1
28
+ text = @@random.bytes(len)
29
+
30
+ should "random text of #{len} bytes" do
31
+ compressed = LZ4::compress(text)
32
+ uncompressed = LZ4::uncompress(compressed)
33
+ assert_equal(text, uncompressed)
34
+ end
35
+ end
36
+
37
+ text = "b" * LOOP_COUNT
38
+ LOOP_COUNT.times do |t|
39
+ len = t + 1
40
+ should "#{len} bytes substring of #{LOOP_COUNT} bytes \"b\"'s" do
41
+ compressed = LZ4::compress(text, len)
42
+ uncompressed = LZ4::uncompress(compressed)
43
+ assert_equal(text[0, len], uncompressed)
44
+ end
45
+ end
46
+ end
47
+
48
+ context "LZ4::compressHC" do
49
+ should "empty text" do
50
+ compressed = LZ4::compressHC("")
51
+ uncompressed = LZ4::uncompress(compressed)
52
+ assert_empty("", uncompressed)
53
+ end
54
+
55
+ LOOP_COUNT.times do |t|
56
+ len = t + 1
57
+ text = "a" * len
58
+
59
+ should "text of #{len} \"a\"'s" do
60
+ compressed = LZ4::compressHC(text)
61
+ uncompressed = LZ4::uncompress(compressed)
62
+ assert_equal(text, uncompressed)
63
+ end
64
+ end
65
+
66
+ LOOP_COUNT.times do |t|
67
+ len = t + 1
68
+ text = @@random.bytes(len)
69
+
70
+ should "random text of #{len} bytes" do
71
+ compressed = LZ4::compressHC(text)
72
+ uncompressed = LZ4::uncompress(compressed)
73
+ assert_equal(text, uncompressed)
74
+ end
75
+ end
76
+
77
+ text = "b" * LOOP_COUNT
78
+ LOOP_COUNT.times do |t|
79
+ len = t + 1
80
+ should "#{len} bytes substring of #{LOOP_COUNT} bytes \"b\"'s" do
81
+ compressed = LZ4::compressHC(text, len)
82
+ uncompressed = LZ4::uncompress(compressed)
83
+ assert_equal(text[0, len], uncompressed)
84
+ end
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lz4-ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 6
10
+ version: 0.1.6
11
+ platform: x86-mingw32
12
+ authors:
13
+ - KOMIYA Atsushi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ name: shoulda
23
+ prerelease: false
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ name: rdoc
37
+ prerelease: false
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 31
44
+ segments:
45
+ - 3
46
+ - 12
47
+ version: "3.12"
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ type: :development
51
+ name: bundler
52
+ prerelease: false
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 23
59
+ segments:
60
+ - 1
61
+ - 0
62
+ - 0
63
+ version: 1.0.0
64
+ requirement: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ type: :development
67
+ name: jeweler
68
+ prerelease: false
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 49
75
+ segments:
76
+ - 1
77
+ - 8
78
+ - 3
79
+ version: 1.8.3
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ name: rake-compiler
84
+ prerelease: false
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirement: *id005
95
+ description: Ruby bindings for LZ4. LZ4 is a very fast lossless compression algorithm.
96
+ email: komiya.atsushi@gmail.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ files:
105
+ - .document
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - benchmarking/bench.rb
112
+ - benchmarking/bench_all.sh
113
+ - benchmarking/compressor.rb
114
+ - benchmarking/compressor_lz4.rb
115
+ - benchmarking/compressor_lzo.rb
116
+ - benchmarking/compressor_snappy.rb
117
+ - ext/lz4ruby/extconf.rb
118
+ - ext/lz4ruby/lz4.c
119
+ - ext/lz4ruby/lz4.h
120
+ - ext/lz4ruby/lz4hc.c
121
+ - ext/lz4ruby/lz4hc.h
122
+ - ext/lz4ruby/lz4ruby.c
123
+ - lib/1.8/lz4ruby.so
124
+ - lib/1.9/lz4ruby.so
125
+ - lib/lz4-ruby.rb
126
+ - test/helper.rb
127
+ - test/test_lz4-ruby.rb
128
+ homepage: http://github.com/komiya-atsushi/lz4-ruby
129
+ licenses:
130
+ - MIT
131
+ post_install_message:
132
+ rdoc_options: []
133
+
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 3
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ requirements: []
155
+
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.24
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Ruby bindings for LZ4 (Extremely Fast Compression algorithm).
161
+ test_files: []
162
+