base91 0.0.1
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 +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/base91.gemspec +22 -0
- data/lib/base91.rb +195 -0
- data/lib/base91/version.rb +3 -0
- data/spec/base91_spec.rb +18 -0
- data/spec/spec_helper.rb +3 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e4f94eb1cbb3f644b511f124537b58f2f29cb4c
|
4
|
+
data.tar.gz: 86dbca2a9b348559e842da3fb61af9b2f2f892f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc88bec086b2891c0a62ed79251c073cc5f35a71a4fb1c6c392955c535b2b3ad00a38d2b1e37a6f7800545e61c07a85e795de0b30359b877ca2eeef055d0e642
|
7
|
+
data.tar.gz: b0313dd6d6316efc3f2caa02460236723b870b2ad104328ea7f78cdc30da2fc504da9a027b2d73ece433336b8f4a0d005c894931170545f809afc701f4ecac5b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 sivsushruth
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Base91
|
2
|
+
|
3
|
+
Gem to encode and decode data using basE91
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'base91'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install base91
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
```ruby
|
23
|
+
require 'base91'
|
24
|
+
Base91.encode("base91 is much better than basE64") #=> [D7ghoHBt#6tF9}iNBMf>[!eKUd/<P:m~FH<<w#XD
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( https://github.com/[my-github-username]/base91/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/base91.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'base91/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "base91"
|
8
|
+
spec.version = Base91::VERSION
|
9
|
+
spec.authors = ["sivsushruth"]
|
10
|
+
spec.email = ["sivsushruth@gmail.com"]
|
11
|
+
spec.summary = "basE91 encoder and decoder"
|
12
|
+
spec.homepage = "https://github.com/sivsushruth/base91"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
22
|
+
end
|
data/lib/base91.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
require "base91/version"
|
2
|
+
|
3
|
+
module Base91
|
4
|
+
|
5
|
+
AVERAGE_ENCODING_RATIO = 1.2297
|
6
|
+
ENCODING_TABLE = [
|
7
|
+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
8
|
+
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
9
|
+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
10
|
+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
11
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', '%',
|
12
|
+
'&', '(', ')', '*', '+', ',', '.', '/', ':', ';','<', '=',
|
13
|
+
'>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~',
|
14
|
+
'"'
|
15
|
+
]
|
16
|
+
DECODING_TABLE = [
|
17
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
18
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
19
|
+
91, 62, 90, 63, 64, 65, 66, 91, 67, 68, 69, 70, 71, 91, 72, 73,
|
20
|
+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 74, 75, 76, 77, 78, 79,
|
21
|
+
80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
22
|
+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 81, 91, 82, 83, 84,
|
23
|
+
85, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
24
|
+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 86, 87, 88, 89, 91,
|
25
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
26
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
27
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
28
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
29
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
30
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
31
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
|
32
|
+
91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91
|
33
|
+
]
|
34
|
+
|
35
|
+
def self.encode data
|
36
|
+
len = data.length
|
37
|
+
output = ''
|
38
|
+
ebq = 0
|
39
|
+
en = 0
|
40
|
+
ev = 0
|
41
|
+
j = 0
|
42
|
+
byte = 0
|
43
|
+
lenj = 0
|
44
|
+
|
45
|
+
if data.is_a? String
|
46
|
+
(0..len-1).each do |i|
|
47
|
+
byte = data[i].ord
|
48
|
+
if byte < 128
|
49
|
+
lenj = 1
|
50
|
+
elsif byte > 127 && byte < 2048
|
51
|
+
lenj = 2
|
52
|
+
else
|
53
|
+
lenj = 3
|
54
|
+
end
|
55
|
+
for j in 0..lenj-1
|
56
|
+
if lenj == 1
|
57
|
+
ebq = ebq | byte << en
|
58
|
+
elsif lenj == 2
|
59
|
+
if j == 0
|
60
|
+
ebq = ebq | (((byte >> 6) | 192)) << en
|
61
|
+
else
|
62
|
+
ebq = ebq | ((byte & 63) | 128) << en
|
63
|
+
end
|
64
|
+
else
|
65
|
+
if j == 0
|
66
|
+
ebq = ebq | (((byte >> 12) | 224) << en)
|
67
|
+
elsif j == 1
|
68
|
+
ebq = ebq | ((((byte >> 6) & 63) | 128) << en)
|
69
|
+
else
|
70
|
+
ebq = ebq | (((byte & 63) | 128) << en)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
en+=8
|
75
|
+
if en > 13
|
76
|
+
ev = ebq & 8191
|
77
|
+
if ev > 88
|
78
|
+
ebq = ebq >> 13
|
79
|
+
en-=13
|
80
|
+
else
|
81
|
+
ev = ebq & 16383
|
82
|
+
ebq = ebq >> 14
|
83
|
+
ebq-=14
|
84
|
+
end
|
85
|
+
output << "#{ENCODING_TABLE[ev%91]}#{ENCODING_TABLE[(ev/91) | 0]}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
else
|
89
|
+
(0..len-1).each do |i|
|
90
|
+
ebq = ebq | (data[i] & 255) << en
|
91
|
+
en += 8
|
92
|
+
if en > 13
|
93
|
+
ev = ebq & 8191
|
94
|
+
if ev > 88
|
95
|
+
ebq = eqb >> 13
|
96
|
+
en -= 13
|
97
|
+
else
|
98
|
+
ev = ebq & 16383
|
99
|
+
ebq = ebq >> 14
|
100
|
+
en -= 14
|
101
|
+
end
|
102
|
+
output << "#{ENCODING_TABLE[ev%91]}#{ENCODING_TABLE[(ev/91) | 0]}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
if en > 0
|
108
|
+
output+= ENCODING_TABLE[ebq % 91]
|
109
|
+
if en > 7 || ebq > 90
|
110
|
+
output << ENCODING_TABLE[(ebq / 91) | 0]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
return output
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.decode data
|
117
|
+
len = data.length
|
118
|
+
estimated_size = (len / AVERAGE_ENCODING_RATIO).to_i | 0
|
119
|
+
dbq = 0
|
120
|
+
dn = 0
|
121
|
+
dv = -1
|
122
|
+
i = 0
|
123
|
+
o = -1
|
124
|
+
byte = 0
|
125
|
+
output = Array.new(estimated_size) ##check here
|
126
|
+
if data.is_a? String
|
127
|
+
(0..len-1).each do |i|
|
128
|
+
byte = data[i].ord
|
129
|
+
if DECODING_TABLE[byte] != 91
|
130
|
+
if dv == -1
|
131
|
+
dv = DECODING_TABLE[byte]
|
132
|
+
else
|
133
|
+
dv+=(DECODING_TABLE[byte] * 91)
|
134
|
+
dbq = dbq | (dv << dn)
|
135
|
+
if dv & 8191 > 88
|
136
|
+
dn+=13
|
137
|
+
else
|
138
|
+
dn+=14
|
139
|
+
end
|
140
|
+
loop do
|
141
|
+
if ((o+=1) > estimated_size)
|
142
|
+
output << (dbq & 0xFF)
|
143
|
+
else
|
144
|
+
output[o] = dbq & 0xFF
|
145
|
+
end
|
146
|
+
dbq = dbq >> 8
|
147
|
+
dn-=8
|
148
|
+
break if dn<=7
|
149
|
+
end
|
150
|
+
dv = -1
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
else
|
155
|
+
(0..len-1).each do |i|
|
156
|
+
byte = data[i]
|
157
|
+
if DECODING_TABLE[byte] != 91
|
158
|
+
if dv == -1
|
159
|
+
dv =DECODING_TABLE[byte]
|
160
|
+
else
|
161
|
+
dv += (DECODING_TABLE[byte] * 91)
|
162
|
+
dbq = dbq | dv << dn
|
163
|
+
if (dv & 8191) > 88
|
164
|
+
dn += 13
|
165
|
+
else
|
166
|
+
dn += 14
|
167
|
+
end
|
168
|
+
loop do
|
169
|
+
if ((o+=1) > estimated_size)
|
170
|
+
output << (dbq & 0xFF)
|
171
|
+
else
|
172
|
+
output[o] = (dbq & 0xFF)
|
173
|
+
end
|
174
|
+
dbq = dbq >> 8
|
175
|
+
dn-=8
|
176
|
+
break if dn<=7
|
177
|
+
end
|
178
|
+
dv = -1
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
if dv != -1
|
184
|
+
if (o+=1) >= estimated_size
|
185
|
+
output << (dbq | dv << dn)
|
186
|
+
else
|
187
|
+
output[o] = (dbq | dv << dn)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
if o > -1 && o < estimated_size -1
|
191
|
+
output = output[0..o+1]
|
192
|
+
end
|
193
|
+
return output.map(&:chr).join("")
|
194
|
+
end
|
195
|
+
end
|
data/spec/base91_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe Base91 do
|
3
|
+
it "encodes string" do
|
4
|
+
expect(Base91.encode("base91 is much better than basE64")).to eq("[D7ghoHBt#6tF9}iNBMf>[!eKUd/<P:m~FH<<w#XD")
|
5
|
+
end
|
6
|
+
|
7
|
+
it "encodes string with all standard characters" do
|
8
|
+
expect(Base91.encode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,./;'[]-=<>?:\"{}_+!@#\{$%^&*\(\)\}")).to eq("#G(Ic,5ph#77&xrmlrjgs@DZ7UB>xQGrfG^F%w_o%5qOdwQbFrzd[5eYAP;gMP+fQztEml0o[2;(p*1QfUve]f>){3_[XjLs\"f^C.YTZwG}1=BrOiB")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "decodes data to produce original string" do
|
12
|
+
expect(Base91.decode("[D7ghoHBt#6tF9}iNBMf>[!eKUd/<P:m~FH<<w#XD")).to eq("base91 is much better than basE64")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "encodes data to produce string with all standard characters" do
|
16
|
+
expect(Base91.decode("#G(Ic,5ph#77&xrmlrjgs@DZ7UB>xQGrfG^F%w_o%5qOdwQbFrzd[5eYAP;gMP+fQztEml0o[2;(p*1QfUve]f>){3_[XjLs\"f^C.YTZwG}1=BrOiB")).to eq("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,./;'[]-=<>?:\"{}_+!@#\{$%^&*\(\)\}")
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: base91
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sivsushruth
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- sivsushruth@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- base91.gemspec
|
68
|
+
- lib/base91.rb
|
69
|
+
- lib/base91/version.rb
|
70
|
+
- spec/base91_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: https://github.com/sivsushruth/base91
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.4
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: basE91 encoder and decoder
|
96
|
+
test_files:
|
97
|
+
- spec/base91_spec.rb
|
98
|
+
- spec/spec_helper.rb
|