base85 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 328dd90db168fa41b46bfe68fbee1fecf63547fa31cdae25a9405753c58cb8f3
4
+ data.tar.gz: 631040e522c72ec2191b709d024334f98a0c1caf1a71067ae3c63563d4eedc6b
5
+ SHA512:
6
+ metadata.gz: 74fd0e3d3a76477ae6598e6665499150b3c874dc2e052d9797dc884a82a4736724a28647a16f7873cf502d8893f733b22b0cafb544097090de1ece7d81f0c6f2
7
+ data.tar.gz: 7d34cdcd2606ad5ed01f95875c4b1b3d183af6855dd0fda9f543fe179c63ac0d556f4ac500765f8b5cfd55bf3eacb762aece0f87bb29991d5721c42273ae220b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ plugins:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.1
8
+ NewCops: enable
9
+
10
+ Layout/LineLength:
11
+ Max: 120
12
+
13
+ RSpec/DescribedClass:
14
+ Enabled: false
15
+
16
+ Style/RaiseArgs:
17
+ Enabled: false
18
+
19
+ Style/StringLiterals:
20
+ Enabled: true
21
+ EnforcedStyle: double_quotes
22
+
23
+ Style/StringLiteralsInInterpolation:
24
+ Enabled: true
25
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-14
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Base85
2
+
3
+ Base85 is a pure Ruby gem to encode/decode data using Base85 encoding.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ bundle add base85
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ gem install base85
14
+
15
+ ## Usage
16
+
17
+ To encode a string:
18
+
19
+ ```ruby
20
+ require "base85"
21
+
22
+ Base85.encode("test1") #=> "FCfN80`"
23
+ ```
24
+
25
+ To decode a string:
26
+
27
+ ```ruby
28
+ require "base85"
29
+
30
+ Base85.decode("!!!!!") #=> "\u0000\u0000\u0000\u0000"
31
+ ```
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bundle exec bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on Codeberg at <https://codeberg.org/sd77/base85>.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Base85
4
+ # Standard base85 encoding. This encoding uses '!' to 'u' ASCII characters to encode data.
5
+ # @author sd77
6
+ module Standard
7
+ # ASCII value for character '!'
8
+ BASE_ASCII_VALUE = "!".ord
9
+ # @private
10
+ LUT = (0..4).map { |count| 85**(4 - count) }.freeze
11
+
12
+ # @private
13
+ # Encode a single word
14
+ # @param [Integer] word
15
+ # @param [Integer] byte_count byte count of word. Must be in range 1..4
16
+ def self.encode_word(word, byte_count: 4)
17
+ encoded = "~" * 5
18
+ 5.times do |i|
19
+ word, r = word.divmod(85)
20
+ encoded[-i - 1] = (BASE_ASCII_VALUE + r).chr
21
+ end
22
+ encoded[..byte_count]
23
+ end
24
+
25
+ # Encode data using standard base85 encoding
26
+ # @param [String] data
27
+ # @return [String]
28
+ def self.encode(data)
29
+ result = +""
30
+ data.unpack("N*").each do |word|
31
+ result << encode_word(word)
32
+ end
33
+
34
+ last_count = data.length % 4
35
+ return result if last_count.zero?
36
+
37
+ last_chars = data[-last_count..] << ("\x00" * (4 - last_count))
38
+ result << encode_word(last_chars.unpack1("N"), byte_count: last_count)
39
+ end
40
+
41
+ # Decode data using standard base85 encoding
42
+ # @param [String] data
43
+ # @return [String]
44
+ # @raise [DecodeError] unknown character or invalid tuple
45
+ def self.decode(data)
46
+ result = +""
47
+ word = 0
48
+ count = 0
49
+ data.each_char do |c|
50
+ case c
51
+ when "!".."u" # rubocop:disable Lint/MixedCaseRange
52
+ idx = c.ord - BASE_ASCII_VALUE
53
+ else
54
+ raise DecodeError, "unknown character '#{c}'"
55
+ end
56
+
57
+ word += idx * LUT[count]
58
+ count += 1
59
+
60
+ if (count == 5) && (word > 0xffffffff)
61
+ raise DecodeError, "invalid tuple"
62
+ elsif count == 5
63
+ wordbuf = " " * 4
64
+ 3.downto(0) do |i|
65
+ wordbuf.setbyte(i, word & 0xff)
66
+ word >>= 8
67
+ end
68
+ result << wordbuf
69
+ count = 0
70
+ word = 0
71
+ end
72
+ end
73
+
74
+ return result if count.zero?
75
+
76
+ count -= 1
77
+ word += LUT[count]
78
+ result << ((word >> 24) & 0xff).chr if count >= 1
79
+ result << ((word >> 16) & 0xff).chr if count >= 2
80
+ result << ((word >> 8) & 0xff).chr if count == 3
81
+ result
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Base85
4
+ VERSION = "0.1.0"
5
+ end
data/lib/base85.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base85/version"
4
+ require_relative "base85/standard"
5
+
6
+ # Provide methods to encode/decode Base85 formatted data
7
+ # @author sd77
8
+ module Base85
9
+ # Base error for Base85
10
+ class Error < StandardError; end
11
+
12
+ # Error while decoding
13
+ class DecodeError < Error; end
14
+
15
+ # Unknown alphabet exception
16
+ class UnknownAlphabetError < Error
17
+ def initialize(name)
18
+ super
19
+ @name = name
20
+ end
21
+
22
+ def message
23
+ "Uknown alphabet '#{@name}'"
24
+ end
25
+ end
26
+
27
+ # Supported Base85 alphabets
28
+ SUPPORTED_ALPHABETS = %i[standard z85 rfc1924].freeze
29
+
30
+ def self.encode(data, alphabet: :standard)
31
+ case alphabet
32
+ when :standard
33
+ Standard.encode(data)
34
+ else
35
+ raise UnknownAlphabetError.new(alphabet)
36
+ end
37
+ end
38
+
39
+ def self.decode(data, alphabet: :standard)
40
+ case alphabet
41
+ when :standard
42
+ Standard.decode(data)
43
+ else
44
+ raise UnknownAlphabetError.new(alphabet)
45
+ end
46
+ end
47
+ end
data/sig/base85.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Base85
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base85
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sd77
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-06-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Encode and decode base85 data. Multiple alphabets are supported (standard,
14
+ Z85 or rfc1924)
15
+ email:
16
+ - sd@ld77.eu
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/base85.rb
28
+ - lib/base85/standard.rb
29
+ - lib/base85/version.rb
30
+ - sig/base85.rbs
31
+ homepage: https://codeberg.org/sd77/base85
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ allowed_push_host: https://rubygems.org
36
+ homepage_uri: https://codeberg.org/sd77/base85
37
+ source_code_uri: https://codeberg.org/sd77/base85
38
+ changelog_uri: https://codeberg.org/sd77/base85/src/branch/main/CHANGELOG.md
39
+ rubygems_mfa_required: 'true'
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.1.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.3.15
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Base85 encoding and decoding gem
59
+ test_files: []