ohsnap 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +7 -0
- data/ext/ohsnap/compat.h +87 -0
- data/ext/ohsnap/ext.c +71 -0
- data/ext/ohsnap/extconf.rb +55 -0
- data/lib/ohsnap.rb +6 -0
- data/lib/ohsnap/version.rb +3 -0
- data/ohsnap.gemspec +24 -0
- data/test/ohsnap_test.rb +21 -0
- data/test/test_helper.rb +12 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Brian Lopez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Ohsnap
|
2
|
+
|
3
|
+
Ruby library for the Snappy compression algorithm
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ohsnap'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ohsnap
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
compressed_data = Ohsnap.compress(data)
|
23
|
+
```
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
data = Ohsnap.decompress(compressed_data)
|
27
|
+
```
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/ext/ohsnap/compat.h
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
/*
|
2
|
+
* The snappy-c code is under the same license as the original snappy source
|
3
|
+
*
|
4
|
+
* Copyright 2011 Intel Corporation All Rights Reserved.
|
5
|
+
*
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
7
|
+
* modification, are permitted provided that the following conditions are
|
8
|
+
* met:
|
9
|
+
*
|
10
|
+
* * Redistributions of source code must retain the above copyright
|
11
|
+
* notice, this list of conditions and the following disclaimer.
|
12
|
+
* * Redistributions in binary form must reproduce the above
|
13
|
+
* copyright notice, this list of conditions and the following disclaimer
|
14
|
+
* in the documentation and/or other materials provided with the
|
15
|
+
* distribution.
|
16
|
+
* * Neither the name of Intel Corporation nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived from
|
18
|
+
* this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
#if defined(HAVE_BYTESWAP_H)
|
34
|
+
#include <byteswap.h>
|
35
|
+
#if !defined(le32toh) || !defined(htole32)
|
36
|
+
#if BYTE_ORDER == LITTLE_ENDIAN
|
37
|
+
#define htole16(x) bswap_16(x)
|
38
|
+
#define le32toh(x) (x)
|
39
|
+
#else
|
40
|
+
#define htole16(x) (x)
|
41
|
+
#define le32toh(x) bswap_32(x)
|
42
|
+
#endif
|
43
|
+
#endif
|
44
|
+
#elif defined(HAVE_COREFOUNDATION_COREFOUNDATION_H)
|
45
|
+
#include <CoreFoundation/CoreFoundation.h>
|
46
|
+
#define htole16(x) CFSwapInt16HostToLittle(x)
|
47
|
+
#define le32toh(x) CFSwapInt32LittleToHost(x)
|
48
|
+
#endif
|
49
|
+
|
50
|
+
#include <stdlib.h>
|
51
|
+
#include <assert.h>
|
52
|
+
#include <string.h>
|
53
|
+
#include <errno.h>
|
54
|
+
#include <stdbool.h>
|
55
|
+
#include <limits.h>
|
56
|
+
#include <sys/uio.h>
|
57
|
+
|
58
|
+
typedef unsigned char u8;
|
59
|
+
typedef unsigned short u16;
|
60
|
+
typedef unsigned u32;
|
61
|
+
typedef unsigned long long u64;
|
62
|
+
|
63
|
+
#define BUG_ON(x) assert(!(x))
|
64
|
+
|
65
|
+
#define get_unaligned(x) (*(x))
|
66
|
+
#define get_unaligned_le32(x) (le32toh(*(u32 *)(x)))
|
67
|
+
#define put_unaligned(v,x) (*(x) = (v))
|
68
|
+
#define put_unaligned_le16(v,x) (*(u16 *)(x) = htole16(v))
|
69
|
+
|
70
|
+
#define vmalloc(x) malloc(x)
|
71
|
+
#define vfree(x) free(x)
|
72
|
+
|
73
|
+
#define EXPORT_SYMBOL(x)
|
74
|
+
|
75
|
+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
|
76
|
+
|
77
|
+
#define likely(x) __builtin_expect((x), 1)
|
78
|
+
#define unlikely(x) __builtin_expect((x), 0)
|
79
|
+
|
80
|
+
#define min_t(t,x,y) ((x) < (y) ? (x) : (y))
|
81
|
+
#define max_t(t,x,y) ((x) > (y) ? (x) : (y))
|
82
|
+
|
83
|
+
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
84
|
+
#define __LITTLE_ENDIAN__ 1
|
85
|
+
#endif
|
86
|
+
|
87
|
+
#define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
|
data/ext/ohsnap/ext.c
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "snappy-c.h"
|
3
|
+
|
4
|
+
static VALUE rb_mOhsnap_compress(VALUE self, VALUE rb_data) {
|
5
|
+
snappy_status err;
|
6
|
+
const char *data;
|
7
|
+
char *out;
|
8
|
+
size_t out_len=0;
|
9
|
+
size_t data_len;
|
10
|
+
VALUE rb_out;
|
11
|
+
|
12
|
+
Check_Type(rb_data, T_STRING);
|
13
|
+
|
14
|
+
rb_out = Qnil;
|
15
|
+
data = RSTRING_PTR(rb_data);
|
16
|
+
data_len = RSTRING_LEN(rb_data);
|
17
|
+
|
18
|
+
out_len = snappy_max_compressed_length(data_len);
|
19
|
+
out = (char*)xmalloc(out_len);
|
20
|
+
|
21
|
+
err = snappy_compress(data, data_len, out, &out_len);
|
22
|
+
|
23
|
+
if (err != SNAPPY_OK) {
|
24
|
+
xfree(out);
|
25
|
+
rb_raise(rb_eIOError, "There was an error compressing the data");
|
26
|
+
}
|
27
|
+
|
28
|
+
rb_out = rb_str_new(out, out_len);
|
29
|
+
xfree(out);
|
30
|
+
|
31
|
+
return rb_out;
|
32
|
+
}
|
33
|
+
|
34
|
+
static VALUE rb_mOhsnap_decompress(VALUE self, VALUE rb_compressed_data) {
|
35
|
+
snappy_status err;
|
36
|
+
const char *compressed_data;
|
37
|
+
size_t compressed_data_len=0;
|
38
|
+
char *out;
|
39
|
+
size_t out_len=0;
|
40
|
+
VALUE rb_out;
|
41
|
+
|
42
|
+
Check_Type(rb_compressed_data, T_STRING);
|
43
|
+
|
44
|
+
rb_out = Qnil;
|
45
|
+
compressed_data = RSTRING_PTR(rb_compressed_data);
|
46
|
+
compressed_data_len = RSTRING_LEN(rb_compressed_data);
|
47
|
+
|
48
|
+
err = snappy_uncompressed_length(compressed_data, compressed_data_len, &out_len);
|
49
|
+
if (err != SNAPPY_OK) {
|
50
|
+
rb_raise(rb_eArgError, "Unable to determine uncompressed length, input is corrupt");
|
51
|
+
}
|
52
|
+
|
53
|
+
out = (char*)xmalloc(out_len);
|
54
|
+
err = snappy_uncompress(compressed_data, compressed_data_len, out, &out_len);
|
55
|
+
if (err != SNAPPY_OK) {
|
56
|
+
xfree(out);
|
57
|
+
rb_raise(rb_eIOError, "There was an error uncompressing the data");
|
58
|
+
}
|
59
|
+
|
60
|
+
rb_out = rb_str_new(out, out_len);
|
61
|
+
xfree(out);
|
62
|
+
|
63
|
+
return rb_out;
|
64
|
+
}
|
65
|
+
|
66
|
+
void Init_ohsnap() {
|
67
|
+
VALUE rb_mOhsnap = rb_define_module("Ohsnap");
|
68
|
+
|
69
|
+
rb_define_method(rb_mOhsnap, "compress", (VALUE(*)(ANYARGS))rb_mOhsnap_compress, 1);
|
70
|
+
rb_define_method(rb_mOhsnap, "decompress", (VALUE(*)(ANYARGS))rb_mOhsnap_decompress, 1);
|
71
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
def sys(cmd)
|
4
|
+
puts " -- #{cmd}"
|
5
|
+
unless ret = xsystem(cmd)
|
6
|
+
raise "#{cmd} failed, please report issue on http://github.com/brianmario/ohsnap"
|
7
|
+
end
|
8
|
+
ret
|
9
|
+
end
|
10
|
+
|
11
|
+
if `which make`.strip.empty?
|
12
|
+
STDERR.puts "\n\n"
|
13
|
+
STDERR.puts "***************************************************************************************"
|
14
|
+
STDERR.puts "*************** make required (apt-get install make build-essential) =( ***************"
|
15
|
+
STDERR.puts "***************************************************************************************"
|
16
|
+
exit(1)
|
17
|
+
end
|
18
|
+
|
19
|
+
snappy_ver = "snappy-1.0.5"
|
20
|
+
src = File.basename("#{snappy_ver}.tar.gz")
|
21
|
+
dir = File.basename(src, '.tar.gz')
|
22
|
+
|
23
|
+
CWD = File.expand_path(File.dirname(__FILE__))
|
24
|
+
Dir.chdir("#{CWD}/src") do
|
25
|
+
FileUtils.rm_rf(dir) if File.exists?(dir)
|
26
|
+
|
27
|
+
sys("tar zxvf #{src}")
|
28
|
+
Dir.chdir(dir) do
|
29
|
+
sys("./configure --prefix=#{CWD}/dst/ --disable-shared --enable-static --with-pic")
|
30
|
+
sys("make install")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
FileUtils.cp "#{CWD}/dst/lib/libsnappy.a", "#{CWD}/libsnappy_ext.a"
|
35
|
+
|
36
|
+
$INCFLAGS[0,0] = " -I#{CWD}/dst/include "
|
37
|
+
$LDFLAGS << " -L#{CWD} "
|
38
|
+
|
39
|
+
dir_config 'snappy'
|
40
|
+
unless have_library 'snappy_ext' and have_header 'snappy-c.h'
|
41
|
+
STDERR.puts "\n\n"
|
42
|
+
STDERR.puts "***************************************************************************************"
|
43
|
+
STDERR.puts "********* error compiling and linking libsnappy. please report issue on github ********"
|
44
|
+
STDERR.puts "***************************************************************************************"
|
45
|
+
exit(1)
|
46
|
+
end
|
47
|
+
|
48
|
+
segments = CONFIG['LDSHARED'].split(' ')
|
49
|
+
segments[0] = "$(CXX)"
|
50
|
+
CONFIG['LDSHARED'] = segments.join(' ')
|
51
|
+
|
52
|
+
$CFLAGS << ' -Wall'
|
53
|
+
$CFLAGS << ' -Wextra -O0 -ggdb3' if ENV['DEBUG']
|
54
|
+
|
55
|
+
create_makefile("ohsnap/ohsnap")
|
data/lib/ohsnap.rb
ADDED
data/ohsnap.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ohsnap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ohsnap"
|
8
|
+
gem.version = Ohsnap::VERSION
|
9
|
+
gem.authors = ["Brian Lopez"]
|
10
|
+
gem.email = ["seniorlopez@gmail.com"]
|
11
|
+
gem.description = %q{Ruby library for the Snappy compression algorithm}
|
12
|
+
gem.summary = %q{Ruby library for the Snappy compression algorithm}
|
13
|
+
gem.homepage = "https://github.com/brianmario/ohsnap"
|
14
|
+
gem.extensions = ["ext/ohsnap/extconf.rb"]
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# tests
|
22
|
+
gem.add_development_dependency 'rake-compiler', ">= 0.8.1"
|
23
|
+
gem.add_development_dependency 'minitest', ">= 4.3.0"
|
24
|
+
end
|
data/test/ohsnap_test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
require 'ohsnap'
|
4
|
+
|
5
|
+
class OhsnapTest < MiniTest::Unit::TestCase
|
6
|
+
TEXT = "a"*1024*1024
|
7
|
+
|
8
|
+
def test_compression
|
9
|
+
compressed = Ohsnap.compress(TEXT)
|
10
|
+
|
11
|
+
assert TEXT.bytesize > compressed.bytesize
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_decompression
|
15
|
+
compressed = Ohsnap.compress(TEXT)
|
16
|
+
decompressed = Ohsnap.decompress(compressed)
|
17
|
+
|
18
|
+
assert TEXT.bytesize == decompressed.bytesize
|
19
|
+
assert_equal TEXT, decompressed
|
20
|
+
end
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Basic test environment.
|
2
|
+
|
3
|
+
# blah fuck this
|
4
|
+
require 'rubygems' if !defined?(Gem)
|
5
|
+
require 'bundler/setup'
|
6
|
+
|
7
|
+
# bring in minitest
|
8
|
+
require 'minitest/autorun'
|
9
|
+
|
10
|
+
# put lib and test dirs directly on load path
|
11
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
12
|
+
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ohsnap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Lopez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake-compiler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.1
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.8.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 4.3.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 4.3.0
|
46
|
+
description: Ruby library for the Snappy compression algorithm
|
47
|
+
email:
|
48
|
+
- seniorlopez@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions:
|
51
|
+
- ext/ohsnap/extconf.rb
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- MIT-LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- ext/ohsnap/compat.h
|
60
|
+
- ext/ohsnap/ext.c
|
61
|
+
- ext/ohsnap/extconf.rb
|
62
|
+
- ext/ohsnap/src/snappy-1.0.5.tar.gz
|
63
|
+
- lib/ohsnap.rb
|
64
|
+
- lib/ohsnap/version.rb
|
65
|
+
- ohsnap.gemspec
|
66
|
+
- test/ohsnap_test.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
homepage: https://github.com/brianmario/ohsnap
|
69
|
+
licenses: []
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.23
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Ruby library for the Snappy compression algorithm
|
92
|
+
test_files:
|
93
|
+
- test/ohsnap_test.rb
|
94
|
+
- test/test_helper.rb
|
95
|
+
has_rdoc:
|