lz4-ruby 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ Ruby bindings for {LZ4}[http://code.google.com/p/lz4/].
22
22
 
23
23
  == Benchmark
24
24
 
25
- Tested on VirtualBox VM : 2-core, 4GB RAM (Host : Core i5-2520M / 8GB RAM).
25
+ Tested on VirtualBox VM : 2-core / 4GB RAM (Host : Core i5-2520M / 8GB RAM).
26
26
 
27
27
  === {enwik8}[http://mattmahoney.net/dc/enwik8.zip]
28
28
  method ratio(bpc) comp.time(ms) uncomp.time(ms)
@@ -34,7 +34,6 @@ Tested on VirtualBox VM : 2-core, 4GB RAM (Host : Core i5-2520M / 8GB RAM).
34
34
  == TODO
35
35
 
36
36
  * Write API documents.
37
- * Write test codes.
38
37
  * Support mswin32 platform.
39
38
 
40
39
  == Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -4,8 +4,9 @@ LZ4_REV_NO = 66
4
4
 
5
5
  URL = "http://lz4.googlecode.com/svn-history/r#{LZ4_REV_NO}/trunk/"
6
6
 
7
- def download_from_web(url)
8
- `wget #{url}`
7
+ def download_from_web(file)
8
+ File.delete(file) if File.file?(file)
9
+ `wget #{URL}/#{file}`
9
10
 
10
11
  if $?.exitstatus != 0
11
12
  return false
@@ -14,10 +15,12 @@ def download_from_web(url)
14
15
  return true
15
16
  end
16
17
 
17
- return if !download_from_web("#{URL}/lz4.c")
18
- return if !download_from_web("#{URL}/lz4.h")
19
- return if !download_from_web("#{URL}/lz4hc.c")
20
- return if !download_from_web("#{URL}/lz4hc.h")
18
+ [ "lz4.c",
19
+ "lz4.h",
20
+ "lz4hc.c",
21
+ "lz4hc.h" ].each do |filename|
22
+ exit if !download_from_web(filename)
23
+ end
21
24
 
22
25
  $CFLAGS += " -Wall "
23
26
 
@@ -7,11 +7,58 @@ typedef int (*CompressFunc)(const char *source, char *dest, int isize);
7
7
  static VALUE lz4;
8
8
  static VALUE lz4_error;
9
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
+
10
55
  static VALUE compress(CompressFunc compressor, VALUE self, VALUE source) {
11
56
  const char *src_p = NULL;
57
+ char varbyte[5];
12
58
  char *buf = NULL;
13
59
  VALUE result;
14
60
  int src_size;
61
+ int varbyte_len;
15
62
  int buf_size;
16
63
  int comp_size;
17
64
 
@@ -20,16 +67,15 @@ static VALUE compress(CompressFunc compressor, VALUE self, VALUE source) {
20
67
  src_size = RSTRING_LEN(source);
21
68
  buf_size = LZ4_compressBound(src_size);
22
69
 
23
- result = rb_str_new(NULL, buf_size + 4);
70
+ varbyte_len = encode_varbyte(src_size, varbyte);
71
+
72
+ result = rb_str_new(NULL, buf_size + varbyte_len);
24
73
  buf = RSTRING_PTR(result);
25
74
 
26
- buf[0] = (char)((src_size >> 24) & 0xff);
27
- buf[1] = (char)((src_size >> 16) & 0xff);
28
- buf[2] = (char)((src_size >> 8) & 0xff);
29
- buf[3] = (char)(src_size & 0xff);
75
+ memcpy(buf, varbyte, varbyte_len);
30
76
 
31
- comp_size = compressor(src_p, buf + 4, src_size);
32
- rb_str_resize(result, comp_size + 4);
77
+ comp_size = compressor(src_p, buf + varbyte_len, src_size);
78
+ rb_str_resize(result, comp_size + varbyte_len);
33
79
 
34
80
  return result;
35
81
  }
@@ -47,22 +93,20 @@ static VALUE lz4_ruby_uncompress(VALUE self, VALUE source) {
47
93
  char *buf = NULL;
48
94
  VALUE result;
49
95
  int src_size;
50
- int buf_size;
96
+ int varbyte_len;
97
+ int buf_size = 0;
51
98
  int read_bytes;
52
99
 
53
100
  Check_Type(source, T_STRING);
54
101
  src_p = RSTRING_PTR(source);
55
102
  src_size = RSTRING_LEN(source);
56
103
 
57
- buf_size = ((src_p[0] & 0xffU) << 24)
58
- | ((src_p[1] & 0xffU) << 16)
59
- | ((src_p[2] & 0xffU) << 8)
60
- | (src_p[3] & 0xffU);
104
+ varbyte_len = decode_varbyte(src_p, src_size, &buf_size);
61
105
 
62
106
  result = rb_str_new(NULL, buf_size);
63
107
  buf = RSTRING_PTR(result);
64
108
 
65
- read_bytes = LZ4_uncompress(src_p + 4, buf, buf_size);
109
+ read_bytes = LZ4_uncompress(src_p + varbyte_len, buf, buf_size);
66
110
  if (read_bytes < 0) {
67
111
  rb_raise(lz4_error, "Compressed data is maybe corrupted.");
68
112
  }
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "lz4-ruby"
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["KOMIYA Atsushi"]
12
- s.date = "2012-06-04"
12
+ s.date = "2012-06-05"
13
13
  s.description = "Ruby bindings for LZ4."
14
14
  s.email = "komiya.atsushi@gmail.com"
15
15
  s.extensions = ["ext/extconf.rb"]
@@ -1,8 +1,18 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestLz4Ruby < Test::Unit::TestCase
4
+ LOOP_COUNT = 257
5
+
6
+ @@random = Random.new(123)
7
+
4
8
  context "LZ4::compress" do
5
- 257.times do |t|
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|
6
16
  len = t + 1
7
17
  text = "a" * len
8
18
 
@@ -12,5 +22,46 @@ class TestLz4Ruby < Test::Unit::TestCase
12
22
  assert_equal(text, uncompressed)
13
23
  end
14
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
+ end
37
+
38
+ context "LZ4::compressHC" do
39
+ should "empty text" do
40
+ compressed = LZ4::compressHC("")
41
+ uncompressed = LZ4::uncompress(compressed)
42
+ assert_empty("", uncompressed)
43
+ end
44
+
45
+ LOOP_COUNT.times do |t|
46
+ len = t + 1
47
+ text = "a" * len
48
+
49
+ should "text of #{len} \"a\"'s" do
50
+ compressed = LZ4::compressHC(text)
51
+ uncompressed = LZ4::uncompress(compressed)
52
+ assert_equal(text, uncompressed)
53
+ end
54
+ end
55
+
56
+ LOOP_COUNT.times do |t|
57
+ len = t + 1
58
+ text = @@random.bytes(len)
59
+
60
+ should "random text of #{len} bytes" do
61
+ compressed = LZ4::compressHC(text)
62
+ uncompressed = LZ4::uncompress(compressed)
63
+ assert_equal(text, uncompressed)
64
+ end
65
+ end
15
66
  end
16
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lz4-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-04 00:00:00.000000000 Z
12
+ date: 2012-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  segments:
118
118
  - 0
119
- hash: 492549213
119
+ hash: -379957487
120
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  none: false
122
122
  requirements: