base32 0.1.3 → 0.2.0
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.
- data/Rakefile +5 -40
- data/lib/base32.rb +43 -0
- data/test/base32_test.rb +0 -2
- metadata +11 -28
- 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
data/Rakefile
CHANGED
@@ -26,22 +26,17 @@ require 'rubygems'
|
|
26
26
|
|
27
27
|
task :default => ['test:all']
|
28
28
|
|
29
|
-
CLEAN.include "ext/**/*.{bundle,o,so}"
|
30
|
-
CLOBBER.include "ext/Makefile", "ext/mkmf.log", "pkg/**/*"
|
31
|
-
|
32
29
|
gemspec = Gem::Specification.new do |s|
|
33
30
|
s.author = "Samuel Tesla"
|
34
|
-
s.email = "samuel@
|
35
|
-
s.extensions = ["ext/extconf.rb"]
|
31
|
+
s.email = "samuel.tesla@gmail.com"
|
36
32
|
s.extra_rdoc_files = ["README"]
|
37
|
-
s.files = FileList["Rakefile", "{config,test}/**/*"
|
33
|
+
s.files = FileList["Rakefile", "{config,lib,test}/**/*"]
|
38
34
|
s.has_rdoc = true
|
39
|
-
s.homepage = "http://base32.rubyforge.org"
|
40
35
|
s.name = 'base32'
|
41
|
-
s.require_paths << '
|
36
|
+
s.require_paths << 'lib'
|
42
37
|
s.requirements << 'none'
|
43
38
|
s.summary = "Ruby extension for base32 encoding and decoding"
|
44
|
-
s.version = "0.
|
39
|
+
s.version = "0.2.0"
|
45
40
|
end
|
46
41
|
|
47
42
|
Rake::GemPackageTask.new(gemspec) do |pkg|
|
@@ -49,40 +44,10 @@ Rake::GemPackageTask.new(gemspec) do |pkg|
|
|
49
44
|
end
|
50
45
|
|
51
46
|
namespace :test do
|
52
|
-
Rake::TestTask.new :all
|
47
|
+
Rake::TestTask.new :all do |t|
|
53
48
|
t.libs << 'test'
|
54
|
-
t.libs << 'ext'
|
55
49
|
t.pattern = 'test/**/*_test.rb'
|
56
50
|
t.verbose = true
|
57
51
|
end
|
58
52
|
Rake::Task['test:all'].comment = 'Run all of the tests'
|
59
53
|
end
|
60
|
-
|
61
|
-
task :extension => "ext/base32.bundle"
|
62
|
-
task :test_extension do
|
63
|
-
sh 'rake TEST=1 ext/base32.bundle'
|
64
|
-
end
|
65
|
-
|
66
|
-
extension_source = FileList["ext/**/*.c", "ext/**/*.h"]
|
67
|
-
|
68
|
-
file "ext/base32.bundle" => ["ext/Makefile", *extension_source] do
|
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
|
77
|
-
|
78
|
-
file "ext/Makefile" => "ext/extconf.rb" do
|
79
|
-
cd "ext" do
|
80
|
-
ruby "extconf.rb"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
task :install => :extension do
|
85
|
-
cd "ext" do
|
86
|
-
sh "make install"
|
87
|
-
end
|
88
|
-
end
|
data/lib/base32.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Base32
|
2
|
+
TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
3
|
+
|
4
|
+
class Chunk
|
5
|
+
def initialize(bytes)
|
6
|
+
@bytes = bytes
|
7
|
+
end
|
8
|
+
|
9
|
+
def decode
|
10
|
+
bytes = @bytes.take_while {|c| c != 61} # strip padding
|
11
|
+
n = (bytes.length * 5.0 / 8.0).floor
|
12
|
+
p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
|
13
|
+
c = bytes.inject(0) {|m,o| (m << 5) + TABLE.index(o.chr)} >> p
|
14
|
+
(0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr}
|
15
|
+
end
|
16
|
+
|
17
|
+
def encode
|
18
|
+
n = (@bytes.length * 8.0 / 5.0).ceil
|
19
|
+
p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0
|
20
|
+
c = @bytes.inject(0) {|m,o| (m << 8) + o} << p
|
21
|
+
[(0..n-1).to_a.reverse.collect {|i| TABLE[(c >> i * 5) & 0x1f].chr},
|
22
|
+
("=" * (8-n))]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.chunks(str, size)
|
27
|
+
result = []
|
28
|
+
bytes = str.bytes
|
29
|
+
while bytes.any? do
|
30
|
+
result << Chunk.new(bytes.take(size))
|
31
|
+
bytes = bytes.drop(size)
|
32
|
+
end
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.encode(str)
|
37
|
+
chunks(str, 5).collect(&:encode).flatten.join
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.decode(str)
|
41
|
+
chunks(str, 8).collect(&:decode).flatten.join
|
42
|
+
end
|
43
|
+
end
|
data/test/base32_test.rb
CHANGED
@@ -25,13 +25,11 @@ class TestBase32 < Test::Unit::TestCase
|
|
25
25
|
def assert_decoding(encoded, plain)
|
26
26
|
decoded = Base32.decode(encoded)
|
27
27
|
assert_equal(plain, decoded)
|
28
|
-
assert_equal(decoded.size, Base32Test.strlen(decoded))
|
29
28
|
end
|
30
29
|
|
31
30
|
def assert_encoding(encoded, plain)
|
32
31
|
actual = Base32.encode(plain)
|
33
32
|
assert_equal(encoded, actual)
|
34
|
-
assert_equal(actual.size, Base32Test.strlen(actual))
|
35
33
|
end
|
36
34
|
|
37
35
|
def assert_encode_and_decode(encoded, plain)
|
metadata
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base32
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 3
|
10
|
-
version: 0.1.3
|
4
|
+
version: 0.2.0
|
11
5
|
platform: ruby
|
12
6
|
authors:
|
13
7
|
- Samuel Tesla
|
@@ -15,31 +9,26 @@ autorequire:
|
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
11
|
|
18
|
-
date: 2011-
|
12
|
+
date: 2011-08-12 00:00:00 -05:00
|
19
13
|
default_executable:
|
20
14
|
dependencies: []
|
21
15
|
|
22
16
|
description:
|
23
|
-
email: samuel@
|
17
|
+
email: samuel.tesla@gmail.com
|
24
18
|
executables: []
|
25
19
|
|
26
|
-
extensions:
|
27
|
-
|
20
|
+
extensions: []
|
21
|
+
|
28
22
|
extra_rdoc_files:
|
29
23
|
- README
|
30
24
|
files:
|
31
25
|
- Rakefile
|
32
26
|
- config/environment.rb
|
27
|
+
- lib/base32.rb
|
33
28
|
- test/base32_test.rb
|
34
|
-
- ext/decoder.c
|
35
|
-
- ext/encoder.c
|
36
|
-
- ext/ext.c
|
37
|
-
- ext/decoder.h
|
38
|
-
- ext/encoder.h
|
39
|
-
- ext/extconf.rb
|
40
29
|
- README
|
41
30
|
has_rdoc: true
|
42
|
-
homepage:
|
31
|
+
homepage:
|
43
32
|
licenses: []
|
44
33
|
|
45
34
|
post_install_message:
|
@@ -47,29 +36,23 @@ rdoc_options: []
|
|
47
36
|
|
48
37
|
require_paths:
|
49
38
|
- lib
|
50
|
-
-
|
39
|
+
- lib
|
51
40
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
41
|
requirements:
|
54
42
|
- - ">="
|
55
43
|
- !ruby/object:Gem::Version
|
56
|
-
hash: 3
|
57
|
-
segments:
|
58
|
-
- 0
|
59
44
|
version: "0"
|
45
|
+
version:
|
60
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
47
|
requirements:
|
63
48
|
- - ">="
|
64
49
|
- !ruby/object:Gem::Version
|
65
|
-
hash: 3
|
66
|
-
segments:
|
67
|
-
- 0
|
68
50
|
version: "0"
|
51
|
+
version:
|
69
52
|
requirements:
|
70
53
|
- none
|
71
54
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.5
|
55
|
+
rubygems_version: 1.3.5
|
73
56
|
signing_key:
|
74
57
|
specification_version: 3
|
75
58
|
summary: Ruby extension for base32 encoding and decoding
|
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')
|