base45_lite 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/base45_lite.rb +71 -0
  3. metadata +72 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1910b7a1ffbeac3439e25a8a31272cf52ef1e9036c6429ea7e789f522018c417
4
+ data.tar.gz: 31dcc6f11dfc98a4e2b327d06f9f35dc63c72ba1e1f0cbed01d84c80735726bb
5
+ SHA512:
6
+ metadata.gz: 207781b24d294bd4805826a029e39e498efeee3d7ebb2e9c0a72bb62469ed525fe8b00a3b276b43b020d09c3b845f280585a3e8da174a25da2e3ead078cbc8dd
7
+ data.tar.gz: 9803f051edb2daad7491c008bda25e0c6361909e347719d70f4d43a6e3a5ac1f02126bc07456f5975ed7740eb678e7c03dfb48629b3051d8cbeffa8a3433f4f4
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2022 Weihang Jian <https://tonytonyjan.net>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ # An implementation of draft-faltstrom-base45-10, see
24
+ # https://datatracker.ietf.org/doc/draft-faltstrom-base45/
25
+ module Base45Lite
26
+ class Error < ::StandardError; end
27
+ class OverflowError < Error; end
28
+ class InvalidCharacterError < Error; end
29
+ class ForbiddenLengthError < Error; end
30
+
31
+ MAX_UINT18 = 2**16 - 1
32
+ SQUARED_45 = 45**2
33
+ MAPPING = [
34
+ *'0'..'9',
35
+ *'A'..'Z',
36
+ ' ', '$', '%', '*', '+', '-', '.', '/', ':'
37
+ ].map!.with_index { |x, i| [i, x] }.to_h.freeze
38
+ REVERSE_MAPPING = MAPPING.invert.freeze
39
+
40
+ def self.encode(input)
41
+ sequence = []
42
+ input.unpack('n*').map! do |uint16|
43
+ i, c = uint16.divmod(45)
44
+ i, d = i.divmod(45)
45
+ _, e = i.divmod(45)
46
+ sequence.push(c, d, e)
47
+ end
48
+ if input.bytesize.odd?
49
+ i, c = input.getbyte(-1).divmod(45)
50
+ _, d = i.divmod(45)
51
+ sequence.push(c, d)
52
+ end
53
+ sequence.map!{ MAPPING[_1] }.join
54
+ end
55
+
56
+ def self.decode(input)
57
+ input
58
+ .chars.map! { REVERSE_MAPPING[_1] || raise(InvalidCharacterError) }
59
+ .each_slice(3).map do |slice|
60
+ c, d, e = slice
61
+ raise ForbiddenLengthError if d.nil?
62
+
63
+ sum = c + d * 45
64
+ sum += e * SQUARED_45 if e
65
+ raise OverflowError if sum > MAX_UINT18
66
+
67
+ sum
68
+ end
69
+ .pack((input.length % 3).zero? ? 'n*' : "n#{input.length / 3}C")
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base45_lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Weihang Jian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-02 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: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.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: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: base45_lite is a Ruby implementation of Base45 data encoding
42
+ email:
43
+ - tonytonyjan@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/base45_lite.rb
49
+ homepage: https://github.com/tonytonyjan/base45_lite
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.3.7
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Base45 encoder/decoder
72
+ test_files: []