base32 0.1.3 → 0.3.4
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 +7 -0
- data/.gitignore +5 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +31 -0
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/{README → README.md} +14 -12
- data/Rakefile +14 -83
- data/base32.gemspec +22 -0
- data/lib/base32.rb +72 -0
- data/lib/base32/version.rb +3 -0
- data/test/base32_test.rb +73 -26
- metadata +79 -59
- data/ext/decoder.c +0 -113
- data/ext/decoder.h +0 -32
- data/ext/encoder.c +0 -108
- data/ext/encoder.h +0 -33
- data/ext/ext.c +0 -107
- data/ext/extconf.rb +0 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e5d47bd83119651fb595fcf9fb64e9fe5ae77ec8f7638b7f44958176d8d9af1
|
4
|
+
data.tar.gz: bcb82ae555d025fa55e7f3f551607eb1273df459b15bf68d42bf6d005e9cd04b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1413d5c64515ccd175348256975f71e99a63e07f64689139eeae3f02c7162673c23e6858761d1d4f1a9c18d6ec8730a71fd80dd5e935c58952878995b7274bc
|
7
|
+
data.tar.gz: 3d860ab09a8db3c0ea94ea2305481eb8a47a2795393249ec70d555181f747efdbc1f3977ed9072ea8cbf4a3c809ab122836b0c15375fc3b2828c5293f8d853f8
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# CHANGELOG
|
2
|
+
|
3
|
+
## 0.3.4
|
4
|
+
- PR #7: Updated README formatting (contributer: codehs)
|
5
|
+
- PR #8: Include license in gemspec (contributer: flavio)
|
6
|
+
- PR #9: Detect invalid characters when decoding (contributer: voondo)
|
7
|
+
- PR #10: Properly initialize character table (contributer: pwnall)
|
8
|
+
- PR #11: Move version.rb into lib/base32 (contributer: pravi)
|
9
|
+
- PR #12: Fix deprecation warnings (contributer: kenchan)
|
10
|
+
- PR #13: Fix build errors (contributor: kenchan)
|
11
|
+
|
12
|
+
## 0.3.2
|
13
|
+
|
14
|
+
- PR #5: Make padding optional for `random_base32` (contributer: lukesteensen)
|
15
|
+
|
16
|
+
## 0.3.1
|
17
|
+
|
18
|
+
- PR #4: Fix compatibility with older JRubies (contributor: localshred)
|
19
|
+
|
20
|
+
## 0.3.0
|
21
|
+
|
22
|
+
- Add `Base32::random_base32` function
|
23
|
+
- Add test cases for hexadecimal conversion
|
24
|
+
- Upgrade tests to use Minitest (successor to Test::Unit)
|
25
|
+
- Add Travis and CodeClimate badges to README
|
26
|
+
- Add Gemfile and gemspec files, using `gem-release` gem to release
|
27
|
+
- Track version as a Ruby constant
|
28
|
+
|
29
|
+
## 0.2.0 and prior
|
30
|
+
|
31
|
+
- Support Base32::encode/decode methods
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2007-2011 Samuel Tesla
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/{README → README.md}
RENAMED
@@ -1,47 +1,49 @@
|
|
1
|
-
|
1
|
+
# base32
|
2
2
|
|
3
3
|
For Version: 0.1.3
|
4
4
|
|
5
|
-
This package contains base32, a
|
5
|
+
This package contains base32, a Ruby extension for encoding and decoding
|
6
6
|
in base32 per RFC 3548.
|
7
7
|
|
8
|
-
|
8
|
+
## Download
|
9
9
|
|
10
10
|
The latest version of base32 can be found at
|
11
11
|
|
12
12
|
http://rubyforge.org/frs/?group_id=3938
|
13
13
|
|
14
|
-
|
14
|
+
## Installation
|
15
15
|
|
16
|
-
|
16
|
+
### Normal Installation
|
17
17
|
|
18
18
|
You can install base32 with the following command from the distribution
|
19
19
|
directory.
|
20
20
|
|
21
|
-
|
21
|
+
` rake install`
|
22
22
|
|
23
|
-
|
23
|
+
### Gem Installation
|
24
24
|
|
25
25
|
Download and install base32 with the following command.
|
26
26
|
|
27
|
-
|
27
|
+
` gem install --remote base32`
|
28
28
|
|
29
|
-
|
29
|
+
### Running the Test Suite
|
30
30
|
|
31
31
|
If you want to run the automated tests for base32, issue this command from the
|
32
32
|
distribution directory.
|
33
33
|
|
34
|
-
|
34
|
+
` rake test:all`
|
35
35
|
|
36
|
-
|
36
|
+
## References
|
37
37
|
|
38
38
|
* RFC 3548: http://www.faqs.org/rfcs/rfc3548.html
|
39
39
|
|
40
|
-
|
40
|
+
## Simple Example
|
41
41
|
|
42
|
+
```rb
|
42
43
|
require "base32"
|
43
44
|
|
44
45
|
encoded = Base32.encode("chunky bacon!") #==> "MNUHK3TLPEQGEYLDN5XCC==="
|
45
46
|
decoded = Base32.decode(encoded) #==> "chunky bacon!"
|
46
47
|
|
47
48
|
puts %Q{"#{decoded}" is "#{encoded}" in base32}
|
49
|
+
```
|
data/Rakefile
CHANGED
@@ -1,88 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
|
21
|
-
require File.join(File.dirname(__FILE__), 'config', 'environment.rb')
|
22
|
-
require 'rake/clean'
|
23
|
-
require 'rake/gempackagetask'
|
24
|
-
require 'rake/testtask'
|
25
|
-
require 'rubygems'
|
26
|
-
|
27
|
-
task :default => ['test:all']
|
28
|
-
|
29
|
-
CLEAN.include "ext/**/*.{bundle,o,so}"
|
30
|
-
CLOBBER.include "ext/Makefile", "ext/mkmf.log", "pkg/**/*"
|
31
|
-
|
32
|
-
gemspec = Gem::Specification.new do |s|
|
33
|
-
s.author = "Samuel Tesla"
|
34
|
-
s.email = "samuel@thoughtlocker.net"
|
35
|
-
s.extensions = ["ext/extconf.rb"]
|
36
|
-
s.extra_rdoc_files = ["README"]
|
37
|
-
s.files = FileList["Rakefile", "{config,test}/**/*", "ext/*.{c,h,rb}"]
|
38
|
-
s.has_rdoc = true
|
39
|
-
s.homepage = "http://base32.rubyforge.org"
|
40
|
-
s.name = 'base32'
|
41
|
-
s.require_paths << 'ext'
|
42
|
-
s.requirements << 'none'
|
43
|
-
s.summary = "Ruby extension for base32 encoding and decoding"
|
44
|
-
s.version = "0.1.3"
|
45
|
-
end
|
46
|
-
|
47
|
-
Rake::GemPackageTask.new(gemspec) do |pkg|
|
48
|
-
pkg.need_tar = true
|
49
|
-
end
|
50
|
-
|
51
|
-
namespace :test do
|
52
|
-
Rake::TestTask.new :all => [:test_extension] do |t|
|
53
|
-
t.libs << 'test'
|
54
|
-
t.libs << 'ext'
|
55
|
-
t.pattern = 'test/**/*_test.rb'
|
56
|
-
t.verbose = true
|
57
|
-
end
|
58
|
-
Rake::Task['test:all'].comment = 'Run all of the tests'
|
59
|
-
end
|
60
|
-
|
61
|
-
task :extension => "ext/base32.bundle"
|
62
|
-
task :test_extension do
|
63
|
-
sh 'rake TEST=1 ext/base32.bundle'
|
1
|
+
begin
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup(:default, :development)
|
4
|
+
rescue Bundler::BundlerError => e
|
5
|
+
$stderr.puts e.message
|
6
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
7
|
+
exit e.status_code
|
64
8
|
end
|
65
9
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
cd "ext" do
|
70
|
-
if ENV['TEST']
|
71
|
-
sh "make cflags=\"-D TEST\""
|
72
|
-
else
|
73
|
-
sh "make"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
10
|
+
require 'bundler/gem_tasks'
|
11
|
+
require 'rubygems'
|
12
|
+
require 'rake/testtask'
|
77
13
|
|
78
|
-
|
79
|
-
cd "ext" do
|
80
|
-
ruby "extconf.rb"
|
81
|
-
end
|
82
|
-
end
|
14
|
+
task :default => :test
|
83
15
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
end
|
16
|
+
Rake::TestTask.new do |t|
|
17
|
+
t.libs << 'test'
|
18
|
+
t.test_files = FileList['test/**/*_test.rb']
|
88
19
|
end
|
data/base32.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$:.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'base32/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'base32'
|
6
|
+
s.version = Base32::VERSION
|
7
|
+
s.authors = ['Samuel Tesla']
|
8
|
+
s.email = 'samuel.tesla@gmail.com'
|
9
|
+
s.summary = 'Ruby extension for base32 encoding and decoding'
|
10
|
+
s.licenses = ['MIT']
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split($/)
|
13
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
14
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
15
|
+
s.require_paths = ['lib']
|
16
|
+
|
17
|
+
s.post_install_message = File.read('UPGRADING') if File.exist?('UPGRADING')
|
18
|
+
|
19
|
+
s.add_development_dependency 'rake'
|
20
|
+
s.add_development_dependency 'minitest'
|
21
|
+
s.add_development_dependency 'gem-release'
|
22
|
+
end
|
data/lib/base32.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
# Module for encoding and decoding in Base32 per RFC 3548
|
4
|
+
module Base32
|
5
|
+
TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.freeze
|
6
|
+
@table = TABLE
|
7
|
+
|
8
|
+
class <<self
|
9
|
+
attr_reader :table
|
10
|
+
end
|
11
|
+
|
12
|
+
class Chunk
|
13
|
+
def initialize(bytes)
|
14
|
+
@bytes = bytes
|
15
|
+
end
|
16
|
+
|
17
|
+
def decode
|
18
|
+
bytes = @bytes.take_while {|c| c != 61} # strip padding
|
19
|
+
n = (bytes.length * 5.0 / 8.0).floor
|
20
|
+
p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
|
21
|
+
c = bytes.inject(0) do |m,o|
|
22
|
+
i = Base32.table.index(o.chr)
|
23
|
+
raise ArgumentError, "invalid character '#{o.chr}'" if i.nil?
|
24
|
+
(m << 5) + i
|
25
|
+
end >> p
|
26
|
+
(0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr}
|
27
|
+
end
|
28
|
+
|
29
|
+
def encode
|
30
|
+
n = (@bytes.length * 8.0 / 5.0).ceil
|
31
|
+
p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0
|
32
|
+
c = @bytes.inject(0) {|m,o| (m << 8) + o} << p
|
33
|
+
[(0..n-1).to_a.reverse.collect {|i| Base32.table[(c >> i * 5) & 0x1f].chr},
|
34
|
+
("=" * (8-n))]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.chunks(str, size)
|
39
|
+
result = []
|
40
|
+
bytes = str.bytes
|
41
|
+
while bytes.any? do
|
42
|
+
result << Chunk.new(bytes.take(size))
|
43
|
+
bytes = bytes.drop(size)
|
44
|
+
end
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.encode(str)
|
49
|
+
chunks(str, 5).collect(&:encode).flatten.join
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.decode(str)
|
53
|
+
chunks(str, 8).collect(&:decode).flatten.join
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.random_base32(length=16, padding=true)
|
57
|
+
random = ''
|
58
|
+
OpenSSL::Random.random_bytes(length).each_byte do |b|
|
59
|
+
random << self.table[b % 32]
|
60
|
+
end
|
61
|
+
padding ? random.ljust((length / 8.0).ceil * 8, '=') : random
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.table=(table)
|
65
|
+
raise ArgumentError, "Table must have 32 unique characters" unless self.table_valid?(table)
|
66
|
+
@table = table
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.table_valid?(table)
|
70
|
+
table.bytes.to_a.size == 32 && table.bytes.to_a.uniq.size == 32
|
71
|
+
end
|
72
|
+
end
|
data/test/base32_test.rb
CHANGED
@@ -1,37 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
|
21
|
-
require 'test/unit'
|
22
|
-
require 'base32'
|
23
|
-
|
24
|
-
class TestBase32 < Test::Unit::TestCase
|
1
|
+
gem 'minitest'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/base32.rb'
|
4
|
+
|
5
|
+
class TestBase32 < Minitest::Test
|
25
6
|
def assert_decoding(encoded, plain)
|
26
7
|
decoded = Base32.decode(encoded)
|
27
8
|
assert_equal(plain, decoded)
|
28
|
-
assert_equal(decoded.size, Base32Test.strlen(decoded))
|
29
9
|
end
|
30
10
|
|
31
11
|
def assert_encoding(encoded, plain)
|
32
12
|
actual = Base32.encode(plain)
|
33
13
|
assert_equal(encoded, actual)
|
34
|
-
assert_equal(actual.size, Base32Test.strlen(actual))
|
35
14
|
end
|
36
15
|
|
37
16
|
def assert_encode_and_decode(encoded, plain)
|
@@ -39,6 +18,11 @@ class TestBase32 < Test::Unit::TestCase
|
|
39
18
|
assert_decoding(encoded, plain)
|
40
19
|
end
|
41
20
|
|
21
|
+
def assert_hex_encode_and_decode(encoded, hex)
|
22
|
+
plain = [hex].pack('H*')
|
23
|
+
assert_encode_and_decode(encoded, plain)
|
24
|
+
end
|
25
|
+
|
42
26
|
def test_empty_string
|
43
27
|
assert_encode_and_decode('', '')
|
44
28
|
end
|
@@ -74,4 +58,67 @@ class TestBase32 < Test::Unit::TestCase
|
|
74
58
|
N5XAUIBAEAQCAIDGN5ZCA5DIMUQFK3TJORSWIICTORQXIZLTEBXWMICBNVSXE2LDMEXAU===).join
|
75
59
|
assert_encode_and_decode(encoded, plaintext)
|
76
60
|
end
|
61
|
+
|
62
|
+
def test_hex_byte_encoding
|
63
|
+
assert_hex_encode_and_decode('FA======', '28')
|
64
|
+
assert_hex_encode_and_decode('2Y======', 'd6')
|
65
|
+
assert_hex_encode_and_decode('234A====', 'd6f8')
|
66
|
+
assert_hex_encode_and_decode('234AA===', 'd6f800')
|
67
|
+
assert_hex_encode_and_decode('234BA===', 'd6f810')
|
68
|
+
assert_hex_encode_and_decode('234BCDA=', 'd6f8110c')
|
69
|
+
assert_hex_encode_and_decode('234BCDEA', 'd6f8110c80')
|
70
|
+
assert_hex_encode_and_decode('234BCDEFGA======', 'd6f8110c8530')
|
71
|
+
assert_hex_encode_and_decode('234BCDEFG234BCDEFE======', 'd6f8110c8536b7c0886429')
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_random_base32
|
75
|
+
assert_equal(16, Base32.random_base32.length)
|
76
|
+
assert_match(/^[A-Z2-7]+$/, Base32.random_base32)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_random_base32_length
|
80
|
+
assert_equal(32, Base32.random_base32(32).length)
|
81
|
+
assert_equal(40, Base32.random_base32(40).length)
|
82
|
+
assert_equal(32, Base32.random_base32(29).length)
|
83
|
+
assert_match(/^[A-Z2-7]{1}={7}$/, Base32.random_base32(1))
|
84
|
+
assert_match(/^[A-Z2-7]{29}={3}$/, Base32.random_base32(29))
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_random_base32_padding
|
88
|
+
assert_equal(32, Base32.random_base32(32, false).length)
|
89
|
+
assert_equal(40, Base32.random_base32(40, false).length)
|
90
|
+
assert_equal(29, Base32.random_base32(29, false).length)
|
91
|
+
assert_match(/^[A-Z2-7]{1}$/, Base32.random_base32(1, false))
|
92
|
+
assert_match(/^[A-Z2-7]{29}$/, Base32.random_base32(29, false))
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_assign_new_table
|
96
|
+
new_table = 'abcdefghijklmnopqrstuvwxyz234567'
|
97
|
+
Base32.table = new_table
|
98
|
+
assert_equal(new_table, Base32.table)
|
99
|
+
Base32.table = Base32::TABLE # so as not to ruin other tests
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_check_table_length
|
103
|
+
assert_raises(ArgumentError) { Base32.table = ('a' * 31) }
|
104
|
+
assert_raises(ArgumentError) { Base32.table = ('a' * 32) }
|
105
|
+
assert_raises(ArgumentError) { Base32.table = ('a' * 33) }
|
106
|
+
assert_raises(ArgumentError) { Base32.table = ('abcdefghijklmnopqrstuvwxyz234567' * 2) }
|
107
|
+
Base32.table = Base32::TABLE # so as not to ruin other tests
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_encode_decode_with_alternate_table
|
111
|
+
Base32.table = 'abcdefghijklmnopqrstuvwxyz234567'
|
112
|
+
assert_hex_encode_and_decode('fa======', '28')
|
113
|
+
assert_hex_encode_and_decode('2y======', 'd6')
|
114
|
+
assert_hex_encode_and_decode('234a====', 'd6f8')
|
115
|
+
assert_hex_encode_and_decode('234aa===', 'd6f800')
|
116
|
+
assert_hex_encode_and_decode('234ba===', 'd6f810')
|
117
|
+
assert_hex_encode_and_decode('234bcda=', 'd6f8110c')
|
118
|
+
assert_hex_encode_and_decode('234bcdea', 'd6f8110c80')
|
119
|
+
assert_hex_encode_and_decode('234bcdefga======', 'd6f8110c8530')
|
120
|
+
assert_hex_encode_and_decode('234bcdefg234bcdefe======', 'd6f8110c8536b7c0886429')
|
121
|
+
Base32.table = Base32::TABLE # so as not to ruin other tests
|
122
|
+
end
|
123
|
+
|
77
124
|
end
|
metadata
CHANGED
@@ -1,77 +1,97 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: base32
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 3
|
10
|
-
version: 0.1.3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.4
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Samuel Tesla
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
11
|
+
date: 2020-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: gem-release
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
22
55
|
description:
|
23
|
-
email: samuel@
|
56
|
+
email: samuel.tesla@gmail.com
|
24
57
|
executables: []
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
-
|
30
|
-
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- ".travis.yml"
|
63
|
+
- CHANGELOG.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
31
67
|
- Rakefile
|
68
|
+
- base32.gemspec
|
32
69
|
- config/environment.rb
|
70
|
+
- lib/base32.rb
|
71
|
+
- lib/base32/version.rb
|
33
72
|
- test/base32_test.rb
|
34
|
-
|
35
|
-
|
36
|
-
-
|
37
|
-
|
38
|
-
- ext/encoder.h
|
39
|
-
- ext/extconf.rb
|
40
|
-
- README
|
41
|
-
has_rdoc: true
|
42
|
-
homepage: http://base32.rubyforge.org
|
43
|
-
licenses: []
|
44
|
-
|
73
|
+
homepage:
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
45
77
|
post_install_message:
|
46
78
|
rdoc_options: []
|
47
|
-
|
48
|
-
require_paths:
|
79
|
+
require_paths:
|
49
80
|
- lib
|
50
|
-
|
51
|
-
|
52
|
-
none: false
|
53
|
-
requirements:
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
54
83
|
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
version: "0"
|
60
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
|
-
requirements:
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
63
88
|
- - ">="
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
version: "0"
|
69
|
-
requirements:
|
70
|
-
- none
|
71
|
-
rubyforge_project:
|
72
|
-
rubygems_version: 1.5.0
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.1.2
|
73
93
|
signing_key:
|
74
|
-
specification_version:
|
94
|
+
specification_version: 4
|
75
95
|
summary: Ruby extension for base32 encoding and decoding
|
76
|
-
test_files:
|
77
|
-
|
96
|
+
test_files:
|
97
|
+
- test/base32_test.rb
|
data/ext/decoder.c
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007-2011 Samuel Tesla
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
* THE SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include "decoder.h"
|
23
|
-
|
24
|
-
static inline uint8_t
|
25
|
-
decode_bits (const uint8_t bits)
|
26
|
-
{
|
27
|
-
uint8_t table[] = {
|
28
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
29
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
30
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
31
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
|
32
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04,
|
33
|
-
0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
|
34
|
-
0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
|
35
|
-
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
|
36
|
-
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF,
|
37
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
38
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
39
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
40
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
41
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
42
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
43
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
44
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
45
|
-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
46
|
-
0xFF, 0xFF, 0xFF, 0xFF
|
47
|
-
};
|
48
|
-
return table[bits];
|
49
|
-
}
|
50
|
-
|
51
|
-
inline size_t
|
52
|
-
base32_decode_buffer_size (const size_t encodedTextLength)
|
53
|
-
{
|
54
|
-
if (encodedTextLength == 0 || encodedTextLength % 8 != 0)
|
55
|
-
return 0;
|
56
|
-
return encodedTextLength * 5 / 8;
|
57
|
-
}
|
58
|
-
|
59
|
-
size_t
|
60
|
-
base32_decode (uint8_t *output, const size_t outputLength, const uint8_t *input, const size_t inputLength)
|
61
|
-
{
|
62
|
-
if (outputLength == 0 || inputLength == 0 || inputLength % 8 != 0)
|
63
|
-
return 0;
|
64
|
-
|
65
|
-
size_t bytes = 0;
|
66
|
-
uint8_t currentByte = 0;
|
67
|
-
unsigned offset;
|
68
|
-
for (offset = 0; offset < inputLength && bytes < outputLength; offset += 8)
|
69
|
-
{
|
70
|
-
output[bytes] = decode_bits (input[offset + 0]) << 3;
|
71
|
-
currentByte = decode_bits (input[offset + 1]);
|
72
|
-
output[bytes] += currentByte >> 2;
|
73
|
-
output[bytes + 1] = (currentByte & 0x03) << 6;
|
74
|
-
|
75
|
-
if (input[offset + 2] == '=')
|
76
|
-
return bytes + 1;
|
77
|
-
else
|
78
|
-
bytes++;
|
79
|
-
|
80
|
-
output[bytes] += decode_bits (input[offset + 2]) << 1;
|
81
|
-
currentByte = decode_bits (input[offset + 3]);
|
82
|
-
output[bytes] += currentByte >> 4;
|
83
|
-
output[bytes + 1] = currentByte << 4;
|
84
|
-
|
85
|
-
if (input[offset + 4] == '=')
|
86
|
-
return bytes + 1;
|
87
|
-
else
|
88
|
-
bytes++;
|
89
|
-
|
90
|
-
currentByte = decode_bits (input[offset + 4]);
|
91
|
-
output[bytes] += currentByte >> 1;
|
92
|
-
output[bytes + 1] = currentByte << 7;
|
93
|
-
|
94
|
-
if (input[offset + 5] == '=')
|
95
|
-
return bytes + 1;
|
96
|
-
else
|
97
|
-
bytes++;
|
98
|
-
|
99
|
-
output[bytes] += decode_bits (input[offset + 5]) << 2;
|
100
|
-
currentByte = decode_bits (input[offset + 6]);
|
101
|
-
output[bytes] += currentByte >> 3;
|
102
|
-
output[bytes + 1] = (currentByte & 0x07) << 5;
|
103
|
-
|
104
|
-
if (input[offset + 7] == '=')
|
105
|
-
return bytes + 1;
|
106
|
-
else
|
107
|
-
bytes++;
|
108
|
-
|
109
|
-
output[bytes] += decode_bits (input[offset + 7]) & 0x1F;
|
110
|
-
bytes++;
|
111
|
-
}
|
112
|
-
return bytes;
|
113
|
-
}
|
data/ext/decoder.h
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007-2011 Samuel Tesla
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
* THE SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#ifndef DECODER_H
|
23
|
-
#define DECODER_H
|
24
|
-
|
25
|
-
#include <stdlib.h>
|
26
|
-
#include <stdint.h>
|
27
|
-
|
28
|
-
inline size_t base32_decoder_buffer_size (const size_t encodedTextLength);
|
29
|
-
|
30
|
-
size_t base32_decode (uint8_t *output, const size_t outputLength,
|
31
|
-
const uint8_t *input, const size_t inputLength);
|
32
|
-
#endif
|
data/ext/encoder.c
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007-2011 Samuel Tesla
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
* THE SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include "encoder.h"
|
23
|
-
|
24
|
-
inline size_t
|
25
|
-
base32_encoder_last_quintent (const size_t bytes)
|
26
|
-
{
|
27
|
-
int quintets = bytes * 8 / 5;
|
28
|
-
int remainder = bytes % 5;
|
29
|
-
return remainder == 0 ? quintets : quintets + 1;
|
30
|
-
}
|
31
|
-
|
32
|
-
inline size_t
|
33
|
-
base32_encoder_output_padding_size (const size_t bytes)
|
34
|
-
{
|
35
|
-
unsigned remainder = bytes % 5;
|
36
|
-
return remainder == 0 ? 0 : (5 - remainder) * 8 / 5;
|
37
|
-
}
|
38
|
-
|
39
|
-
inline size_t
|
40
|
-
base32_encoder_buffer_size (const size_t bytes)
|
41
|
-
{
|
42
|
-
return base32_encoder_last_quintent (bytes) +
|
43
|
-
base32_encoder_output_padding_size (bytes);
|
44
|
-
}
|
45
|
-
|
46
|
-
static unsigned
|
47
|
-
base32_encoder_encode_bits (int position, const uint8_t *buffer)
|
48
|
-
{
|
49
|
-
unsigned offset = position / 8 * 5;
|
50
|
-
switch (position % 8)
|
51
|
-
{
|
52
|
-
case 0:
|
53
|
-
return
|
54
|
-
((buffer[offset] & 0xF8) >> 3);
|
55
|
-
|
56
|
-
case 1:
|
57
|
-
return
|
58
|
-
((buffer[offset] & 0x07) << 2) +
|
59
|
-
((buffer[offset + 1] & 0xC0) >> 6);
|
60
|
-
|
61
|
-
case 2:
|
62
|
-
return
|
63
|
-
((buffer[offset + 1] & 0x3E) >> 1);
|
64
|
-
|
65
|
-
case 3:
|
66
|
-
return
|
67
|
-
((buffer[offset + 1] & 0x01) << 4) +
|
68
|
-
((buffer[offset + 2] & 0xF0) >> 4);
|
69
|
-
|
70
|
-
case 4:
|
71
|
-
return
|
72
|
-
((buffer[offset + 2] & 0x0F) << 1) +
|
73
|
-
((buffer[offset + 3] & 0x80) >> 7);
|
74
|
-
|
75
|
-
case 5:
|
76
|
-
return
|
77
|
-
((buffer[offset + 3] & 0x7C) >> 2);
|
78
|
-
|
79
|
-
case 6:
|
80
|
-
return
|
81
|
-
((buffer[offset + 3] & 0x03) << 3) +
|
82
|
-
((buffer[offset + 4] & 0xE0) >> 5);
|
83
|
-
|
84
|
-
case 7:
|
85
|
-
return
|
86
|
-
buffer[offset + 4] & 0x1F;
|
87
|
-
}
|
88
|
-
}
|
89
|
-
|
90
|
-
static inline uint8_t
|
91
|
-
base32_encoder_encode_at_position (unsigned position, const uint8_t *buffer)
|
92
|
-
{
|
93
|
-
const char *table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
94
|
-
unsigned index = base32_encoder_encode_bits (position, buffer);
|
95
|
-
return table[index];
|
96
|
-
}
|
97
|
-
|
98
|
-
void
|
99
|
-
base32_encode (uint8_t *output, const size_t outputLength,
|
100
|
-
const uint8_t *input, const size_t inputLength)
|
101
|
-
{
|
102
|
-
unsigned i;
|
103
|
-
unsigned quintets = base32_encoder_last_quintent(inputLength);
|
104
|
-
for (i = 0; i < quintets; i++)
|
105
|
-
output[i] = base32_encoder_encode_at_position (i, input);
|
106
|
-
for (i = quintets; i < outputLength; i++)
|
107
|
-
output[i] = '=';
|
108
|
-
}
|
data/ext/encoder.h
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007-2011 Samuel Tesla
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
* THE SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#ifndef ENCODER_H
|
23
|
-
#define ENCODER_H
|
24
|
-
|
25
|
-
#include <stdlib.h>
|
26
|
-
#include <stdint.h>
|
27
|
-
|
28
|
-
inline size_t base32_encoder_buffer_size (const size_t bytes);
|
29
|
-
|
30
|
-
void base32_encode (uint8_t *output, const size_t outputLength,
|
31
|
-
const uint8_t *input, const size_t inputLength);
|
32
|
-
|
33
|
-
#endif
|
data/ext/ext.c
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007-2011 Samuel Tesla
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
* THE SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include <stdlib.h>
|
23
|
-
#include "ruby.h"
|
24
|
-
#include "decoder.h"
|
25
|
-
#include "encoder.h"
|
26
|
-
|
27
|
-
/*
|
28
|
-
* call-seq:
|
29
|
-
* Base32.decode(encoded_string) -> string
|
30
|
-
*
|
31
|
-
* Decodes a string that is encoded in base32. Will throw an ArgumentError if
|
32
|
-
* it cannot successfully decode it.
|
33
|
-
*/
|
34
|
-
static VALUE
|
35
|
-
b32_decode (VALUE self, VALUE value)
|
36
|
-
{
|
37
|
-
value = StringValue (value);
|
38
|
-
if (RSTRING_LEN (value) == 0)
|
39
|
-
return value;
|
40
|
-
|
41
|
-
size_t buflen = base32_decode_buffer_size (RSTRING_LEN (value));
|
42
|
-
char *buffer = (char *) malloc (buflen);
|
43
|
-
#ifdef TEST
|
44
|
-
memset(buffer, 0xff, buflen);
|
45
|
-
#else
|
46
|
-
memset(buffer, 0x00, buflen);
|
47
|
-
#endif
|
48
|
-
|
49
|
-
size_t length = base32_decode ((uint8_t *) buffer, buflen,
|
50
|
-
(uint8_t *) RSTRING_PTR (value), RSTRING_LEN (value));
|
51
|
-
|
52
|
-
if (length == 0) {
|
53
|
-
free(buffer);
|
54
|
-
rb_raise(rb_eRuntimeError, "Value provided not base32 encoded");
|
55
|
-
}
|
56
|
-
|
57
|
-
VALUE result = rb_str_new (0, length);
|
58
|
-
memcpy(RSTRING_PTR (result), buffer, length);
|
59
|
-
free(buffer);
|
60
|
-
return result;
|
61
|
-
}
|
62
|
-
|
63
|
-
/*
|
64
|
-
* call-seq:
|
65
|
-
* Base32.encode(string) -> encoded_string
|
66
|
-
*
|
67
|
-
* Encodes a string in base32.
|
68
|
-
*/
|
69
|
-
static VALUE
|
70
|
-
b32_encode (VALUE self, VALUE value)
|
71
|
-
{
|
72
|
-
value = StringValue(value);
|
73
|
-
|
74
|
-
VALUE result = rb_str_new (0, base32_encoder_buffer_size (RSTRING_LEN (value)));
|
75
|
-
#ifdef TEST
|
76
|
-
memset(RSTRING_PTR (result), 0xff, RSTRING_LEN (result));
|
77
|
-
#endif
|
78
|
-
base32_encode ((uint8_t *) RSTRING_PTR (result), RSTRING_LEN (result),
|
79
|
-
(uint8_t *) RSTRING_PTR (value), RSTRING_LEN (value));
|
80
|
-
|
81
|
-
return result;
|
82
|
-
}
|
83
|
-
|
84
|
-
#ifdef TEST
|
85
|
-
static VALUE
|
86
|
-
b32_test_strlen (VALUE self, VALUE value)
|
87
|
-
{
|
88
|
-
return UINT2NUM (strlen (RSTRING_PTR (value)));
|
89
|
-
}
|
90
|
-
#endif
|
91
|
-
|
92
|
-
|
93
|
-
VALUE mBase32;
|
94
|
-
#ifdef TEST
|
95
|
-
VALUE mBase32Test;
|
96
|
-
#endif
|
97
|
-
|
98
|
-
void Init_base32 ()
|
99
|
-
{
|
100
|
-
mBase32 = rb_define_module ("Base32");
|
101
|
-
rb_define_module_function(mBase32, "decode", b32_decode, 1);
|
102
|
-
rb_define_module_function(mBase32, "encode", b32_encode, 1);
|
103
|
-
#ifdef TEST
|
104
|
-
mBase32Test = rb_define_module ("Base32Test");
|
105
|
-
rb_define_module_function(mBase32Test, "strlen", b32_test_strlen, 1);
|
106
|
-
#endif
|
107
|
-
}
|
data/ext/extconf.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# Copyright (c) 2007-2011 Samuel Tesla
|
2
|
-
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
|
21
|
-
require 'mkmf'
|
22
|
-
create_makefile('base32')
|