Last active
May 5, 2022 02:14
-
-
Save tonytonyjan/5eefdfbe7a79cd676e75c138466e921d to your computer and use it in GitHub Desktop.
A Ruby implementation of Base45 Data Encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# Copyright (c) 2022 Weihang Jian <https://tonytonyjan.net> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# An implementation of draft-faltstrom-base45-10, see | |
# https://datatracker.ietf.org/doc/draft-faltstrom-base45/ | |
module Base45 | |
class Error < ::StandardError; end | |
class OverflowError < Error; end | |
class InvalidCharacterError < Error; end | |
class ForbiddenLengthError < Error; end | |
MAX_UINT18 = 2**16 - 1 | |
SQUARED_45 = 45**2 | |
MAPPING = [ | |
*'0'..'9', | |
*'A'..'Z', | |
' ', '$', '%', '*', '+', '-', '.', '/', ':' | |
].map!.with_index { |x, i| [i, x] }.to_h.freeze | |
REVERSE_MAPPING = MAPPING.invert.freeze | |
def self.encode(input) | |
sequence = [] | |
input.unpack('n*').map! do |uint16| | |
i, c = uint16.divmod(45) | |
i, d = i.divmod(45) | |
_, e = i.divmod(45) | |
sequence.push(c, d, e) | |
end | |
if input.bytesize.odd? | |
i, c = input.getbyte(-1).divmod(45) | |
_, d = i.divmod(45) | |
sequence.push(c, d) | |
end | |
sequence.map!{ MAPPING[_1] }.join | |
end | |
def self.decode(input) | |
input | |
.chars.map! { REVERSE_MAPPING[_1] || raise(InvalidCharacterError) } | |
.each_slice(3).map do |slice| | |
c, d, e = slice | |
raise ForbiddenLengthError if d.nil? | |
sum = c + d * 45 | |
sum += e * SQUARED_45 if e | |
raise OverflowError if sum > MAX_UINT18 | |
sum | |
end | |
.pack((input.length % 3).zero? ? 'n*' : "n#{input.length / 3}C") | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
require 'minitest/autorun' | |
class Base45Test < Minitest::Test | |
parallelize_me! | |
def test_encode | |
assert_equal 'BB8', Base45.encode('AB') | |
assert_equal '%69 VD92EX0', Base45.encode('Hello!!') | |
assert_equal 'UJCLQE7W581', Base45.encode('base-45') | |
end | |
def test_decode | |
assert_equal 'ietf!', Base45.decode('QED8WEX0') | |
assert_equal 'AB', Base45.decode('BB8') | |
assert_equal 'Hello!!', Base45.decode('%69 VD92EX0') | |
assert_equal 'base-45', Base45.decode('UJCLQE7W581') | |
end | |
def test_raise_invalid_character_error | |
assert_raises(Base45::InvalidCharacterError) { Base45.decode('^{}[]!&') } | |
end | |
def test_raise_overflow_error | |
assert_raises(Base45::OverflowError) { Base45.decode('///') } | |
end | |
def test_raise_forbidden_length_error | |
assert_raises(Base45::ForbiddenLengthError) { Base45.decode('ABCD') } | |
end | |
end | |
end |
I always get an overflow error with the standard EU-DCC 2D barcode output in Base45. Any clues?
Hi, can you give instructions to reproduce the issue? Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I always get an overflow error with the standard EU-DCC 2D barcode output in Base45. Any clues?