fb64 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/ext/fb64/extconf.rb +39 -0
  3. data/ext/fb64/fb64.c +60 -0
  4. data/lib/test.rb +25 -0
  5. metadata +89 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3b23ef4e6b769da8464691da756a7962e8565e9
4
+ data.tar.gz: 176e0797980176e401967c117e2f12b39b1cf897
5
+ SHA512:
6
+ metadata.gz: adb232cc875683f5390f6b9846414a1616ab653af55b33e11eca8fa35ad0d2f3047043ffbd19bfa733cfc251cc328d8ca13649373639a91cbaf07cc9a6771c6b
7
+ data.tar.gz: e4396969adcf56be51066a56e7eccfcecdafd6817406ffdaba689e05741bf2c2d51bb67afc4a5d2ec633811a8168a11616ce1cdb3e010c0f2749161d82ad2f55
@@ -0,0 +1,39 @@
1
+ require 'mkmf'
2
+
3
+
4
+ def windows?
5
+ RbConfig::CONFIG['target_os'] =~ /mingw32|mswin/
6
+ end
7
+
8
+ def solaris?
9
+ RbConfig::CONFIG['target_os'] =~ /solaris/
10
+ end
11
+
12
+ def darwin?
13
+ RbConfig::CONFIG['target_os'] =~ /darwin/
14
+ end
15
+
16
+ def openbsd?
17
+ RbConfig::CONFIG['target_os'] =~ /openbsd/
18
+ end
19
+
20
+ def nix?
21
+ ! (windows? || solaris? || darwin?)
22
+ end
23
+
24
+ extension_name = 'fb64'
25
+ dir_config 'libbase64'
26
+ library_name = if darwin?
27
+ 'libbase64.o'
28
+ elsif windows?
29
+ raise "Sorry, your platform is not supported"
30
+ else
31
+ 'libbase64.so' # TODO verify
32
+ end
33
+
34
+ if have_library(library_name) && have_header('libbase64.h')
35
+ dir_config(extension_name)
36
+ create_makefile(extension_name)
37
+ end
38
+
39
+ # rake clean clobber compile -- --with-libbase64-dir=/Users/jcook/open_source/base64
@@ -0,0 +1,60 @@
1
+ #include <stdlib.h>
2
+ #include <libbase64.h>
3
+ #include <ruby.h>
4
+ #include <ruby/encoding.h>
5
+
6
+ static VALUE m_FB64;
7
+ static VALUE e_DecodeError;
8
+
9
+ static VALUE
10
+ fb64_encode64(int argc, VALUE* argv, VALUE self)
11
+ {
12
+ VALUE input;
13
+ rb_scan_args(argc, argv, "1", &input);
14
+
15
+ if (TYPE(input) != T_STRING) {
16
+ rb_raise(rb_eTypeError, "expected a String");
17
+ }
18
+
19
+ const char* src = StringValueCStr(input);
20
+ size_t srclen = RSTRING_LEN( (VALUE)input);
21
+ size_t outlen = ((srclen * 4) / 3) + 1;
22
+ char out[outlen];
23
+
24
+ base64_encode(src, srclen, out, &outlen, 0);
25
+
26
+ return rb_utf8_str_new(out, outlen);
27
+ }
28
+
29
+ static VALUE
30
+ fb64_decode64(int argc, VALUE *argv, VALUE self)
31
+ {
32
+ VALUE input;
33
+ rb_scan_args(argc, argv, "1", &input);
34
+
35
+ if (TYPE(input) != T_STRING) {
36
+ rb_raise(rb_eTypeError, "expected a String");
37
+ }
38
+
39
+ const char* src = StringValueCStr(input);
40
+ size_t srclen = RSTRING_LEN( (VALUE)input);
41
+ size_t outlen = ((srclen * 3) / 4) + 1;
42
+ char out[outlen];
43
+
44
+ int result;
45
+ result = base64_decode(src, srclen, out, &outlen, 0);
46
+ if (result != 1) {
47
+ rb_raise(e_DecodeError, "Error during decode");
48
+ }
49
+ return rb_utf8_str_new(out, outlen);
50
+ }
51
+
52
+
53
+ void
54
+ Init_fb64() {
55
+ m_FB64 = rb_define_module("FB64");
56
+
57
+ e_DecodeError = rb_define_class_under(m_FB64, "DecodeError", rb_eStandardError);
58
+ rb_define_module_function(m_FB64, "encode64", fb64_encode64, -1);
59
+ rb_define_module_function(m_FB64, "decode64", fb64_decode64, -1);
60
+ }
@@ -0,0 +1,25 @@
1
+ require_relative "./fb64"
2
+ require 'base64'
3
+ require 'benchmark/ips'
4
+
5
+
6
+ puts FB64.base64_encode("hello world")
7
+ puts Base64.encode64("hello world")
8
+ str = File.read("/Users/jcook/Downloads/war-and-peace.txt")
9
+
10
+ encoded = FB64.base64_encode(str)
11
+
12
+ Benchmark.ips do |b|
13
+ b.time = 10
14
+ b.report("FB64.base64_decode") { FB64.base64_decode(encoded) }
15
+ b.report("Base64.decode") { Base64.decode64(encoded) }
16
+ b.compare!
17
+ end
18
+
19
+ Benchmark.ips do |b|
20
+ b.time = 10
21
+ b.report("FB64.base64_encode") { FB64.base64_encode(str) }
22
+ b.report("Base64.encode") { Base64.encode64(str) }
23
+ b.compare!
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fb64
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - James Cook
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: benchmark-ips
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ description:
56
+ email:
57
+ executables: []
58
+ extensions:
59
+ - ext/fb64/extconf.rb
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ext/fb64/extconf.rb
63
+ - ext/fb64/fb64.c
64
+ - lib/test.rb
65
+ homepage: https://github.com/jamescook/fb64
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '2'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.6.13
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Fast Base64 C extension
89
+ test_files: []