base32 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +47 -0
- data/Rakefile +81 -0
- data/config/environment.rb +27 -0
- data/ext/decoder.c +112 -0
- data/ext/decoder.h +31 -0
- data/ext/encoder.c +105 -0
- data/ext/encoder.h +32 -0
- data/ext/ext.c +75 -0
- data/ext/extconf.rb +22 -0
- data/test/base32_test.rb +73 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= base32
|
2
|
+
|
3
|
+
For Version: 0.1.0
|
4
|
+
|
5
|
+
This package contains base32, a ruby extension for encoding and decoding
|
6
|
+
in base32 per RFC 3548.
|
7
|
+
|
8
|
+
== Download
|
9
|
+
|
10
|
+
The latest version of base32 can be found at
|
11
|
+
|
12
|
+
http://rubyforge.org/frs/?group_id=3938
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
=== Normal Installation
|
17
|
+
|
18
|
+
You can install base32 with the following command from the distribution
|
19
|
+
directory.
|
20
|
+
|
21
|
+
% rake install
|
22
|
+
|
23
|
+
=== Gem Installation
|
24
|
+
|
25
|
+
Download and install base32 with the following command.
|
26
|
+
|
27
|
+
% gem install --remote base32
|
28
|
+
|
29
|
+
=== Running the Test Suite
|
30
|
+
|
31
|
+
If you want to run the automated tests for base32, issue this command from the
|
32
|
+
distribution directory.
|
33
|
+
|
34
|
+
% rake test:all
|
35
|
+
|
36
|
+
== References
|
37
|
+
|
38
|
+
* RFC 3548: http://www.faqs.org/rfcs/rfc3548.html
|
39
|
+
|
40
|
+
== Simple Example
|
41
|
+
|
42
|
+
require "base32"
|
43
|
+
|
44
|
+
encoded = Base32.encode("chunky bacon!") #==> "MNUHK3TLPEQGEYLDN5XCC==="
|
45
|
+
decoded = Base32.decode(encoded) #==> "chunky bacon!"
|
46
|
+
|
47
|
+
puts %Q{"#{decoded}" is "#{encoded}" in base32}
|
data/Rakefile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Copyright (c) 2007 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 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", "ext/**/*.o"
|
30
|
+
CLOBBER.include "ext/Makefile", "ext/base32", "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,bundle}"]
|
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.0"
|
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 => [: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
|
+
|
63
|
+
extension_source = FileList["ext/**/*.c", "ext/**/*.h"]
|
64
|
+
|
65
|
+
file "ext/base32.bundle" => ["ext/Makefile", *extension_source] do
|
66
|
+
cd "ext" do
|
67
|
+
sh "make"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
file "ext/Makefile" => "ext/extconf.rb" do
|
72
|
+
cd "ext" do
|
73
|
+
ruby "extconf.rb"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
task :install => :extension do
|
78
|
+
cd "ext" do
|
79
|
+
sh "make install"
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Copyright (c) 2007 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
|
+
unless defined? BASE32_ROOT
|
22
|
+
root_path = File.join(File.dirname(__FILE__), '..')
|
23
|
+
BASE32_ROOT = File.expand_path(root_path)
|
24
|
+
end
|
25
|
+
|
26
|
+
$LOAD_PATH.unshift File.join(BASE32_ROOT, 'lib')
|
27
|
+
$LOAD_PATH.unshift File.join(BASE32_ROOT, 'ext')
|
data/ext/decoder.c
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
/* Copyright (c) 2007 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 "ruby.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
|
+
size_t base32_decode_buffer_size (const size_t encodedTextLength)
|
52
|
+
{
|
53
|
+
if (encodedTextLength == 0 || encodedTextLength % 8 != 0)
|
54
|
+
return 0;
|
55
|
+
return encodedTextLength * 8 / 5;
|
56
|
+
}
|
57
|
+
|
58
|
+
size_t
|
59
|
+
base32_decode (uint8_t *output, const size_t outputLength, const uint8_t *input, const size_t inputLength)
|
60
|
+
{
|
61
|
+
if (outputLength == 0 || inputLength == 0 || inputLength % 8 != 0)
|
62
|
+
return 0;
|
63
|
+
|
64
|
+
size_t bytes = 0;
|
65
|
+
uint8_t currentByte = 0;
|
66
|
+
unsigned offset;
|
67
|
+
for (offset = 0; offset < inputLength && bytes < outputLength; offset += 8)
|
68
|
+
{
|
69
|
+
output[bytes] = decode_bits (input[offset + 0]) << 3;
|
70
|
+
currentByte = decode_bits (input[offset + 1]);
|
71
|
+
output[bytes] += currentByte >> 2;
|
72
|
+
output[bytes + 1] = (currentByte & 0x03) << 6;
|
73
|
+
|
74
|
+
if (input[offset + 2] == '=')
|
75
|
+
return bytes + 1;
|
76
|
+
else
|
77
|
+
bytes++;
|
78
|
+
|
79
|
+
output[bytes] += decode_bits (input[offset + 2]) << 1;
|
80
|
+
currentByte = decode_bits (input[offset + 3]);
|
81
|
+
output[bytes] += currentByte >> 4;
|
82
|
+
output[bytes + 1] = currentByte << 4;
|
83
|
+
|
84
|
+
if (input[offset + 4] == '=')
|
85
|
+
return bytes + 1;
|
86
|
+
else
|
87
|
+
bytes++;
|
88
|
+
|
89
|
+
currentByte = decode_bits (input[offset + 4]);
|
90
|
+
output[bytes] += currentByte >> 1;
|
91
|
+
output[bytes + 1] = currentByte << 7;
|
92
|
+
|
93
|
+
if (input[offset + 5] == '=')
|
94
|
+
return bytes + 1;
|
95
|
+
else
|
96
|
+
bytes++;
|
97
|
+
|
98
|
+
output[bytes] += decode_bits (input[offset + 5]) << 2;
|
99
|
+
currentByte = decode_bits (input[offset + 6]);
|
100
|
+
output[bytes] += currentByte >> 3;
|
101
|
+
output[bytes + 1] = (currentByte & 0x07) << 5;
|
102
|
+
|
103
|
+
if (input[offset + 7] == '=')
|
104
|
+
return bytes + 1;
|
105
|
+
else
|
106
|
+
bytes++;
|
107
|
+
|
108
|
+
output[bytes] += decode_bits (input[offset + 7]) & 0x1F;
|
109
|
+
bytes++;
|
110
|
+
}
|
111
|
+
return bytes;
|
112
|
+
}
|
data/ext/decoder.h
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
/* Copyright (c) 2007 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
|
+
|
27
|
+
inline size_t base32_decoder_buffer_size (const size_t encodedTextLength);
|
28
|
+
|
29
|
+
size_t base32_decode (uint8_t *output, const size_t outputLength,
|
30
|
+
const uint8_t *input, const size_t inputLength);
|
31
|
+
#endif
|
data/ext/encoder.c
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
/* Copyright (c) 2007 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 base32_encoder_last_quintent (const size_t bytes)
|
25
|
+
{
|
26
|
+
int quintets = bytes * 8 / 5;
|
27
|
+
int remainder = bytes % 5;
|
28
|
+
return remainder == 0 ? quintets : quintets + 1;
|
29
|
+
}
|
30
|
+
|
31
|
+
inline size_t base32_encoder_output_padding_size (const size_t bytes)
|
32
|
+
{
|
33
|
+
unsigned remainder = bytes % 5;
|
34
|
+
return remainder == 0 ? 0 : (5 - remainder) * 8 / 5;
|
35
|
+
}
|
36
|
+
|
37
|
+
inline size_t base32_encoder_buffer_size (const size_t bytes)
|
38
|
+
{
|
39
|
+
return base32_encoder_last_quintent (bytes) +
|
40
|
+
base32_encoder_output_padding_size (bytes);
|
41
|
+
}
|
42
|
+
|
43
|
+
static unsigned
|
44
|
+
base32_encoder_encode_bits (int position, const uint8_t *buffer)
|
45
|
+
{
|
46
|
+
unsigned offset = position / 8 * 5;
|
47
|
+
switch (position % 8)
|
48
|
+
{
|
49
|
+
case 0:
|
50
|
+
return
|
51
|
+
((buffer[offset] & 0xF8) >> 3);
|
52
|
+
|
53
|
+
case 1:
|
54
|
+
return
|
55
|
+
((buffer[offset] & 0x07) << 2) +
|
56
|
+
((buffer[offset + 1] & 0xC0) >> 6);
|
57
|
+
|
58
|
+
case 2:
|
59
|
+
return
|
60
|
+
((buffer[offset + 1] & 0x3E) >> 1);
|
61
|
+
|
62
|
+
case 3:
|
63
|
+
return
|
64
|
+
((buffer[offset + 1] & 0x01) << 4) +
|
65
|
+
((buffer[offset + 2] & 0xF0) >> 4);
|
66
|
+
|
67
|
+
case 4:
|
68
|
+
return
|
69
|
+
((buffer[offset + 2] & 0x0F) << 1) +
|
70
|
+
((buffer[offset + 3] & 0x80) >> 7);
|
71
|
+
|
72
|
+
case 5:
|
73
|
+
return
|
74
|
+
((buffer[offset + 3] & 0x7C) >> 2);
|
75
|
+
|
76
|
+
case 6:
|
77
|
+
return
|
78
|
+
((buffer[offset + 3] & 0x03) << 3) +
|
79
|
+
((buffer[offset + 4] & 0xE0) >> 5);
|
80
|
+
|
81
|
+
case 7:
|
82
|
+
return
|
83
|
+
buffer[offset + 4] & 0x1F;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
static uint8_t
|
88
|
+
base32_encoder_encode_at_position (unsigned position, const uint8_t *buffer)
|
89
|
+
{
|
90
|
+
const char *table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
91
|
+
unsigned index = base32_encoder_encode_bits (position, buffer);
|
92
|
+
return table[index];
|
93
|
+
}
|
94
|
+
|
95
|
+
void
|
96
|
+
base32_encode (uint8_t *output, const size_t outputLength,
|
97
|
+
const uint8_t *input, const size_t inputLength)
|
98
|
+
{
|
99
|
+
unsigned i;
|
100
|
+
unsigned quintets = base32_encoder_last_quintent(inputLength);
|
101
|
+
for (i = 0; i < quintets; i++)
|
102
|
+
output[i] = base32_encoder_encode_at_position (i, input);
|
103
|
+
for (i = quintets; i < outputLength; i++)
|
104
|
+
output[i] = '=';
|
105
|
+
}
|
data/ext/encoder.h
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
/* Copyright (c) 2007 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
|
+
|
27
|
+
size_t base32_encoder_buffer_size (const size_t bytes);
|
28
|
+
|
29
|
+
void base32_encode (uint8_t *output, const size_t outputLength,
|
30
|
+
const uint8_t *input, const size_t inputLength);
|
31
|
+
|
32
|
+
#endif
|
data/ext/ext.c
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
/* Copyright (c) 2007 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 "ruby.h"
|
23
|
+
#include "decoder.h"
|
24
|
+
#include "encoder.h"
|
25
|
+
|
26
|
+
/*
|
27
|
+
* call-seq:
|
28
|
+
* Base32.decode(encoded_string) -> string
|
29
|
+
*
|
30
|
+
* Decodes a string that is encoded in base32. Will throw an ArgumentError if
|
31
|
+
* it cannot successfully decode it.
|
32
|
+
*/
|
33
|
+
static VALUE
|
34
|
+
b32_decode (VALUE self, VALUE value)
|
35
|
+
{
|
36
|
+
value = StringValue (value);
|
37
|
+
if (RSTRING (value)->len == 0)
|
38
|
+
return value;
|
39
|
+
|
40
|
+
VALUE result = rb_str_new (0, base32_decode_buffer_size (RSTRING (value)->len));
|
41
|
+
size_t length = base32_decode ((uint8_t *) RSTRING (result)->ptr, RSTRING (result)->len,
|
42
|
+
(uint8_t *) RSTRING (value)->ptr, RSTRING (value)->len);
|
43
|
+
if (length == 0)
|
44
|
+
rb_raise(rb_eRuntimeError, "Value provided not base32 encoded");
|
45
|
+
|
46
|
+
RSTRING (result)->len = length;
|
47
|
+
return result;
|
48
|
+
}
|
49
|
+
|
50
|
+
/*
|
51
|
+
* call-seq:
|
52
|
+
* Base32.encode(string) -> encoded_string
|
53
|
+
*
|
54
|
+
* Encodes a string in base32.
|
55
|
+
*/
|
56
|
+
static VALUE
|
57
|
+
b32_encode (VALUE self, VALUE value)
|
58
|
+
{
|
59
|
+
value = StringValue(value);
|
60
|
+
|
61
|
+
VALUE result = rb_str_new (0, base32_encoder_buffer_size (RSTRING (value)->len));
|
62
|
+
base32_encode ((uint8_t *) RSTRING (result)->ptr, RSTRING (result)->len,
|
63
|
+
(uint8_t *) RSTRING (value)->ptr, RSTRING (value)->len);
|
64
|
+
|
65
|
+
return result;
|
66
|
+
}
|
67
|
+
|
68
|
+
VALUE mBase32;
|
69
|
+
|
70
|
+
void Init_base32 ()
|
71
|
+
{
|
72
|
+
mBase32 = rb_define_module ("Base32");
|
73
|
+
rb_define_module_function(mBase32, "decode", b32_decode, 1);
|
74
|
+
rb_define_module_function(mBase32, "encode", b32_encode, 1);
|
75
|
+
}
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright (c) 2007 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')
|
data/test/base32_test.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Copyright (c) 2007 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 'test/unit'
|
22
|
+
require 'base32'
|
23
|
+
|
24
|
+
class TestBase32 < Test::Unit::TestCase
|
25
|
+
def assert_decoding(encoded, plain)
|
26
|
+
assert_equal(plain, Base32.decode(encoded))
|
27
|
+
end
|
28
|
+
|
29
|
+
def assert_encoding(encoded, plain)
|
30
|
+
assert_equal(encoded, Base32.encode(plain))
|
31
|
+
end
|
32
|
+
|
33
|
+
def assert_encode_and_decode(encoded, plain)
|
34
|
+
assert_encoding(encoded, plain)
|
35
|
+
assert_decoding(encoded, plain)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_empty_string
|
39
|
+
assert_encode_and_decode('', '')
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_a
|
43
|
+
assert_encode_and_decode('ME======', 'a')
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_12345
|
47
|
+
assert_encode_and_decode('GEZDGNBV', '12345')
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_abcde
|
51
|
+
assert_encode_and_decode('MFRGGZDF', 'abcde')
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_constitution_preamble
|
55
|
+
plaintext =<<-EOT
|
56
|
+
We the people of the United States, in order to form a more perfect union,
|
57
|
+
establish justice, insure domestic tranquility, provide for the common
|
58
|
+
defense, promote the general welfare, and secure the blessings of liberty
|
59
|
+
to ourselves and our posterity, do ordain and establish this Constitution
|
60
|
+
for the United States of America.
|
61
|
+
EOT
|
62
|
+
encoded = %W(
|
63
|
+
EAQCAIBAEBLWKIDUNBSSA4DFN5YGYZJAN5TCA5DIMUQFK3TJORSWIICTORQXIZLTFQQGS3RA
|
64
|
+
N5ZGIZLSEB2G6IDGN5ZG2IDBEBWW64TFEBYGK4TGMVRXIIDVNZUW63RMBIQCAIBAEAQGK43U
|
65
|
+
MFRGY2LTNAQGU5LTORUWGZJMEBUW443VOJSSAZDPNVSXG5DJMMQHI4TBNZYXK2LMNF2HSLBA
|
66
|
+
OBZG65TJMRSSAZTPOIQHI2DFEBRW63LNN5XAUIBAEAQCAIDEMVTGK3TTMUWCA4DSN5WW65DF
|
67
|
+
EB2GQZJAM5SW4ZLSMFWCA53FNRTGC4TFFQQGC3TEEBZWKY3VOJSSA5DIMUQGE3DFONZWS3TH
|
68
|
+
OMQG6ZRANRUWEZLSOR4QUIBAEAQCAIDUN4QG65LSONSWY5TFOMQGC3TEEBXXK4RAOBXXG5DF
|
69
|
+
OJUXI6JMEBSG6IDPOJSGC2LOEBQW4ZBAMVZXIYLCNRUXG2BAORUGS4ZAINXW443UNF2HK5DJ
|
70
|
+
N5XAUIBAEAQCAIDGN5ZCA5DIMUQFK3TJORSWIICTORQXIZLTEBXWMICBNVSXE2LDMEXAU===).join
|
71
|
+
assert_encode_and_decode(encoded, plaintext)
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: base32
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-06-28 00:00:00 -05:00
|
8
|
+
summary: Ruby extension for base32 encoding and decoding
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
- ext
|
12
|
+
email: samuel@thoughtlocker.net
|
13
|
+
homepage: http://base32.rubyforge.org
|
14
|
+
rubyforge_project:
|
15
|
+
description:
|
16
|
+
autorequire:
|
17
|
+
default_executable:
|
18
|
+
bindir: bin
|
19
|
+
has_rdoc: true
|
20
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
signing_key:
|
28
|
+
cert_chain:
|
29
|
+
post_install_message:
|
30
|
+
authors:
|
31
|
+
- Samuel Tesla
|
32
|
+
files:
|
33
|
+
- Rakefile
|
34
|
+
- config/environment.rb
|
35
|
+
- test/base32_test.rb
|
36
|
+
- ext/decoder.c
|
37
|
+
- ext/encoder.c
|
38
|
+
- ext/ext.c
|
39
|
+
- ext/decoder.h
|
40
|
+
- ext/encoder.h
|
41
|
+
- ext/extconf.rb
|
42
|
+
- README
|
43
|
+
test_files: []
|
44
|
+
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
extra_rdoc_files:
|
48
|
+
- README
|
49
|
+
executables: []
|
50
|
+
|
51
|
+
extensions:
|
52
|
+
- ext/extconf.rb
|
53
|
+
requirements:
|
54
|
+
- none
|
55
|
+
dependencies: []
|
56
|
+
|