ob64 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d1111a285be1731afda3a4b3040eb9150109a6022eaea13d007004e5d51dcfe
4
+ data.tar.gz: e3a298f4286ac95e6656cd0ba0fdebefa1e7f180eec6f4ca0b3f7def4de64122
5
+ SHA512:
6
+ metadata.gz: b5992c541b3d46c846bb3e22f8e7e01aed89b8a8760ce3ca401263ebfebbd9130cfba69eacee46e288fdba44ed5319ee1086ff55e9e0b0b6791254def2577632
7
+ data.tar.gz: 3e77098bab5a772fdab9cf3cc0d987ee18e7a64748dd3fda9bc970cc29b15e48f2fa6b2f24d1ba9b8bb4e41c24bea9fac4041834214d638a17bf0b7c2776fe5b
data/.byebug_history ADDED
@@ -0,0 +1,72 @@
1
+ continue
2
+ aa == String.new(bb, encoding: 'ASCII-8BIT')
3
+ String.new(bb, encoding: 'ASCII').encoding
4
+ bb.encoding
5
+ aa.encoding
6
+ aa == String.new(bb, encoding: 'ASCII')
7
+ aa == String.new(bb, encoding: 'ASCII)
8
+ aa == String.new(bb)
9
+ continue
10
+ aa = String.new(bb)
11
+ bb.dup.force_encoding(Encoding::ASCII_8BIT) == aa
12
+ bb.dup.force_encoding(Encoding::ASCII_8BIT).encoding
13
+ bb.dup.force_encoding Encoding::ASCII_8BIT
14
+ bb.force_encoding Encoding::ASCII_8BIT
15
+ bb.encode Encoding::ASCII_8BIT
16
+ aa.encode Encoding::UTF_8
17
+ aa.encode Encoding::UTF-8
18
+ bb.encode Encoding::ASCII_8BIT
19
+ Encoding.constants
20
+ Encoding
21
+ bb.encoding
22
+ aa.encoding
23
+ bb.class
24
+ aa.class
25
+ aa.casecmp? 'ad'
26
+ aa.casecmp? bb
27
+ bb.bytes
28
+ aa.bytes
29
+ bb.bytesize
30
+ aa.bytesize
31
+ bb.size
32
+ aa.size
33
+ aa.match? bb
34
+ aa.match?
35
+ aa == bb
36
+ aa
37
+ a
38
+ bb
39
+ continue
40
+ b
41
+ a
42
+ exit
43
+ eixt
44
+ continue
45
+ disable-byebug
46
+ continue
47
+ missing_padding
48
+ n
49
+ continue
50
+ n
51
+ missing_padding
52
+ string
53
+ n
54
+ string
55
+ continue
56
+ string
57
+ continue
58
+ string
59
+ continue
60
+ exit
61
+ missing_padding
62
+ n
63
+ string
64
+ continue
65
+ __decode(string)
66
+ string
67
+ n
68
+ string
69
+ missing_padding
70
+ next
71
+ string
72
+ continue
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ PATH_add bin
@@ -0,0 +1,18 @@
1
+ name: Test
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ ruby: ['2.5', '2.6', '2.7', '3.0']
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ bundler-cache: false
18
+ - run: bundle exec rake spec
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+ Gemfile.lock
15
+
16
+ # rspec failure tracking
17
+ .rspec_status
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/libbase64"]
2
+ path = vendor/libbase64
3
+ url = https://github.com/jcmfernandes/base64-cmake
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,10 @@
1
+ fix: true
2
+ parallel: true
3
+ format: progress
4
+
5
+ ignore:
6
+ - 'benchmark.rb'
7
+ - 'ext/**/*'
8
+ - 'vendor/**/*'
9
+ - 'spec/**/*':
10
+ - Layout/AlignHash
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --title "Ob64 API Documentation"
2
+ --readme README.md
3
+ --charset utf-8
4
+ -
5
+ CHANGELOG.md
6
+ LICENSE
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-21
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joao@hopin.to. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "benchmark-ips"
8
+ gem "byebug"
9
+ gem "guard"
10
+ gem "guard-rspec"
11
+ gem "rake", "~> 13.0"
12
+ gem "rake-compiler"
13
+ gem "rspec", "~> 3.0"
14
+ gem "standard"
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ guard :rspec, cmd: "bin/rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+
11
+ # Ruby files
12
+ ruby = dsl.ruby
13
+ dsl.watch_spec_files_for(ruby.lib_files)
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Joao Fernandes
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,89 @@
1
+ # Ob64 gem
2
+
3
+ A *fast* Base64 encoder and decoder as a Ruby gem.
4
+
5
+ How fast? Try it yourself, execute:
6
+
7
+ $ rake benchmark
8
+
9
+ When compared to Ruby's own `Base64` module, expect a 4x speedup in encoding, and a 2x speedup in decoding. YMMV.
10
+
11
+ ## Acknowledgements
12
+
13
+ Ob64 uses `libbase64` under the hood. Originally developed by [aklomp](https://github.com/aklomp), here we started from [BurningEnlightenment](https://github.com/BurningEnlightenment)'s fork. `libbase64` takes advantage of SIMD (AVX2, NEON, AArch64/NEON, SSSE3, SSE4.1, SSE4.2, AVX) acceleration available on x86, x86-64, and ARM systems.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'ob64'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle install
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install ob64
30
+
31
+ Requires CMake and a C99 compiler.
32
+
33
+ **WARNING:** Only tested on Linux and x86-64.
34
+
35
+ ## Usage
36
+
37
+ ```ruby
38
+ require "ob64"
39
+
40
+ bin1 = "This is data!"
41
+ string = Ob64.encode(bin1)
42
+
43
+ # string = "VGhpcyBpcyBkYXRhIQ=="
44
+
45
+ bin2 = Ob64.decode(string)
46
+ puts "Same? #{bin1 == bin2}"
47
+ # "Same? true"
48
+ ```
49
+
50
+ If you want to replace Ruby's own `Base64` module with Ob64 **(NOT RECOMMENDED!)**:
51
+
52
+ ```ruby
53
+ require "ob64/core_ext"
54
+ # It does NOT replace:
55
+ # - Base64.encode64
56
+ # - Base64.decode64
57
+ ```
58
+
59
+ See [YARD documentation](http://www.rubydoc.info/gems/ob64).
60
+
61
+ ## Releases
62
+
63
+ See [{file:CHANGELOG.md}](CHANGELOG.md).
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ 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).
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jcmfernandes/ob64. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jcmfernandes/ob64/blob/master/CODE_OF_CONDUCT.md).
74
+
75
+ ## TODO
76
+
77
+ * URL-safe encoding/decoding using separate alphabet/tables
78
+ * Streaming interface
79
+ * RFC 2045 (MIME) encoding/decoding
80
+ * Make the OpenMP threshold configurable (and enable OpenMP acceleration)
81
+ * Windows support
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
86
+
87
+ ## Code of Conduct
88
+
89
+ Everyone interacting in the Ob64 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jcmfernandes/ob64/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "standard/rake"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ require "rake/extensiontask"
10
+
11
+ task build: :compile
12
+
13
+ Rake::ExtensionTask.new("ob64_ext") do |ext|
14
+ ext.ext_dir = "ext/ob64"
15
+ end
16
+
17
+ desc "Run benchmark"
18
+ task :benchmark do
19
+ load "#{__dir__}/benchmark.rb"
20
+ end
21
+
22
+ task default: %i[clobber compile spec]
data/benchmark.rb ADDED
@@ -0,0 +1,79 @@
1
+ require "benchmark/ips"
2
+ require "securerandom"
3
+
4
+ require "ob64"
5
+ require "base64"
6
+
7
+ def to_size(bytes)
8
+ bytes >= 2**20 ? "#{bytes / 2**20} MB" : "#{bytes / 2**10} kB"
9
+ end
10
+
11
+ def each_block
12
+ [
13
+ [1, :kb],
14
+ [4, :kb],
15
+ [64, :kb],
16
+ [1, :mb],
17
+ [10, :mb],
18
+ [64, :mb]
19
+ ].each do |(n, unit)|
20
+ yield(n * 2**({kb: 10, mb: 20}.fetch(unit)))
21
+ end
22
+ end
23
+
24
+ def benchmark(setup_block)
25
+ each_block do |size|
26
+ setup_block.call(size)
27
+ GC.start
28
+ puts "\n\n#{"=" * 60}\nbenchmark with block size #{to_size(size)}"
29
+ yield
30
+ end
31
+ end
32
+
33
+ def encode_benchmark(&block)
34
+ benchmark(
35
+ lambda { |size| $unencoded = File.open("/dev/urandom") { |f| f.read(size) } },
36
+ &block
37
+ )
38
+ end
39
+
40
+ def decode_benchmark(&block)
41
+ benchmark(
42
+ lambda { |size| $encoded = SecureRandom.base64(size) },
43
+ &block
44
+ )
45
+ end
46
+
47
+ encode_benchmark do
48
+ Benchmark.ips do |x|
49
+ x.time = 5
50
+ x.warmup = 2
51
+
52
+ x.report("base64 .strict_encode64") do
53
+ Base64.strict_encode64($unencoded)
54
+ end
55
+
56
+ x.report("ob64 .encode") do
57
+ Ob64.encode($unencoded)
58
+ end
59
+
60
+ x.compare!
61
+ end
62
+ end
63
+
64
+ decode_benchmark do
65
+ Benchmark.ips do |x|
66
+ x.time = 5
67
+ x.warmup = 2
68
+
69
+ x.report("base64 .strict_decode64") do
70
+ Base64.strict_decode64($encoded)
71
+ end
72
+
73
+ x.report("ob64 .decode") do
74
+ Ob64.encode($encoded)
75
+ end
76
+
77
+ x.compare!
78
+ end
79
+ end
data/bin/_guard-core ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application '_guard-core' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("guard", "_guard-core")
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ob64"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/guard ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'guard' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("guard", "guard")
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/standardrb ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'standardrb' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("standard", "standardrb")
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ def sys(cmd)
6
+ puts " -- #{cmd}"
7
+ unless ret = xsystem(cmd)
8
+ raise "ERROR: '#{cmd}' failed"
9
+ end
10
+ ret
11
+ end
12
+
13
+ def run_cmake(args)
14
+ pid = Process.spawn("cmake #{args}")
15
+ Process.waitpid(pid)
16
+ end
17
+
18
+ if !find_executable("cmake")
19
+ abort "ERROR: CMake is required to build ob64"
20
+ end
21
+
22
+ MAKE = find_executable("make") || find_executable("gmake")
23
+ if !MAKE
24
+ abort "ERROR: make is required to build ob64"
25
+ end
26
+
27
+ CWD = __dir__
28
+ LIBBASE64_DIR = File.join(CWD, "..", "..", "vendor", "libbase64")
29
+
30
+ Dir.chdir(LIBBASE64_DIR) do
31
+ Dir.mkdir("build") unless Dir.exist?("build")
32
+
33
+ Dir.chdir("build") do
34
+ run_cmake(".. -DBUILD_SHARED_LIBS=true -DOPENMP=false")
35
+ sys(MAKE)
36
+ end
37
+
38
+ $DEFLIBPATH.unshift("#{LIBBASE64_DIR}/build/bin")
39
+ dir_config("base64", "#{LIBBASE64_DIR}/include", "#{LIBBASE64_DIR}/build/bin")
40
+ end
41
+
42
+ unless have_library("base64") && have_header("libbase64.h")
43
+ abort "ERROR: Failed to build libbase64"
44
+ end
45
+
46
+ dir_config("ob64_ext")
47
+ create_makefile("ob64_ext")
@@ -0,0 +1 @@
1
+ #include "../../vendor/libbase64/include/libbase64.h"
data/ext/ob64/ob64.h ADDED
@@ -0,0 +1,7 @@
1
+ #ifndef OB64_H
2
+ #define OB64_H 1
3
+
4
+ #include "libbase64.h"
5
+ #include "ruby.h"
6
+
7
+ #endif /* OB64_H */
@@ -0,0 +1,81 @@
1
+ #include <stdbool.h>
2
+ #include "ob64.h"
3
+
4
+ VALUE mOb64 = Qnil;
5
+ VALUE mOb64_LibBase64 = Qnil;
6
+
7
+ VALUE Ob64_encode_string(VALUE self, VALUE bin);
8
+ VALUE Ob64_decode_string(VALUE self, VALUE string);
9
+ VALUE Ob64_encoded_length_of(VALUE self, VALUE bin, VALUE withPadding);
10
+ VALUE Ob64_decoded_length_of(VALUE self, VALUE string);
11
+
12
+ size_t __encoded_length_of(VALUE string, bool withPadding);
13
+ size_t __decoded_length_of(VALUE string);
14
+
15
+ void
16
+ Init_ob64_ext(void)
17
+ {
18
+ mOb64 = rb_define_module("Ob64");
19
+ mOb64_LibBase64 = rb_define_module_under(mOb64, "LibBase64");
20
+
21
+ rb_define_private_method(mOb64_LibBase64, "__encode", Ob64_encode_string, 1);
22
+ rb_define_private_method(mOb64_LibBase64, "__decode", Ob64_decode_string, 1);
23
+ rb_define_private_method(mOb64_LibBase64, "__encoded_length_of", Ob64_encoded_length_of, 2);
24
+ rb_define_private_method(mOb64_LibBase64, "__decoded_length_of", Ob64_decoded_length_of, 1);
25
+ }
26
+
27
+ VALUE Ob64_encode_string(VALUE self, VALUE bin)
28
+ {
29
+ VALUE result = rb_str_buf_new(__encoded_length_of(bin, true));
30
+ size_t nout;
31
+
32
+ base64_encode(StringValuePtr(bin), RSTRING_LEN(bin), StringValuePtr(result), &nout, 0);
33
+ rb_str_set_len(result, nout);
34
+
35
+ return result;
36
+ }
37
+
38
+ VALUE Ob64_decode_string(VALUE self, VALUE string)
39
+ {
40
+ VALUE result = rb_str_buf_new(__decoded_length_of(string));
41
+ size_t nout;
42
+
43
+ if (!base64_decode(StringValuePtr(string), RSTRING_LEN(string), StringValuePtr(result), &nout, 0)) {
44
+ rb_raise(rb_eArgError, "invalid base64");
45
+ }
46
+ rb_str_set_len(result, nout);
47
+
48
+ return result;
49
+ }
50
+
51
+ VALUE Ob64_encoded_length_of(VALUE self, VALUE bin, VALUE withPadding)
52
+ {
53
+ return ULONG2NUM((unsigned long)__encoded_length_of(bin, withPadding != Qnil && withPadding != Qfalse ? true : false));
54
+ }
55
+
56
+ VALUE Ob64_decoded_length_of(VALUE self, VALUE string)
57
+ {
58
+ return ULONG2NUM((unsigned long)__decoded_length_of(string));
59
+ }
60
+
61
+ size_t __encoded_length_of(VALUE bin, bool withPadding)
62
+ {
63
+ if (withPadding) {
64
+ return (RSTRING_LEN(bin) + 2) / 3 * 4;
65
+ } else {
66
+ return ((RSTRING_LEN(bin) * 8) + 5) / 6;
67
+ }
68
+ }
69
+
70
+ size_t __decoded_length_of(VALUE string)
71
+ {
72
+ size_t string_len = RSTRING_LEN(string);
73
+ if (string_len < 2) return 0;
74
+
75
+ size_t padding = 0;
76
+ char* string_ptr = StringValuePtr(string);
77
+ if (string_ptr[string_len - 1] == '=') padding++;
78
+ if (string_ptr[string_len - 2] == '=') padding++;
79
+
80
+ return (3 * (string_len - padding)) / 4;
81
+ }
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ob64"
4
+ require "base64"
5
+
6
+ module Ob64
7
+ module CoreExt
8
+ def strict_encode64(bin)
9
+ Ob64.strictier_encode(bin)
10
+ end
11
+
12
+ def strict_decode64(str)
13
+ Ob64.strictier_decode(str)
14
+ end
15
+
16
+ def urlsafe_encode64(bin, padding: true)
17
+ Ob64.urlsafe_encode(bin, padding: padding)
18
+ end
19
+
20
+ def urlsafe_decode64(str)
21
+ Ob64.urlsafe_decode(str)
22
+ end
23
+ end
24
+
25
+ ::Base64.prepend(CoreExt)
26
+ ::Base64.singleton_class.prepend(CoreExt)
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ob64
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ob64.rb ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ob64/version"
4
+ require_relative "ob64_ext"
5
+
6
+ module Ob64
7
+ include LibBase64
8
+ extend LibBase64
9
+
10
+ module_function
11
+
12
+ def encode(bin)
13
+ __encode(bin)
14
+ end
15
+
16
+ def decode(string)
17
+ __decode(string)
18
+ end
19
+
20
+ def urlsafe_encode(bin, padding: true)
21
+ string = __encode(bin)
22
+ string.chomp!("==") || string.chomp!("=") unless padding
23
+ string.tr!("+/", "-_")
24
+ string
25
+ end
26
+
27
+ def urlsafe_decode(string)
28
+ if !string.end_with?("=") && string.length % 4 != 0
29
+ string = string.ljust((string.length + 3) & ~3, "=")
30
+ string.tr!("-_", "+/")
31
+ else
32
+ string = string.tr("-_", "+/")
33
+ end
34
+ __decode(string)
35
+ end
36
+
37
+ def encoded_length_of(bin, padding: true)
38
+ __encoded_length_of(bin, padding)
39
+ end
40
+
41
+ def decoded_length_of(string)
42
+ __decoded_length_of(string)
43
+ end
44
+ end
data/ob64.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require_relative "lib/ob64/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ob64"
8
+ spec.version = Ob64::VERSION
9
+ spec.authors = ["João Fernandes"]
10
+ spec.email = ["joao.fernandes@ist.utl.pt"]
11
+ spec.homepage = "https://github.com/jcmfernandes/ob64"
12
+
13
+ spec.summary = "A fast Base64 encoder and decoder."
14
+ spec.description = "A fast Base64 encoder and decoder that makes use of SIMD extensions."
15
+
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.require_paths = ["lib"]
27
+ spec.extensions = ["ext/ob64/extconf.rb"]
28
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ob64
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - João Fernandes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A fast Base64 encoder and decoder that makes use of SIMD extensions.
14
+ email:
15
+ - joao.fernandes@ist.utl.pt
16
+ executables: []
17
+ extensions:
18
+ - ext/ob64/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".byebug_history"
22
+ - ".envrc"
23
+ - ".github/workflows/main.yml"
24
+ - ".gitignore"
25
+ - ".gitmodules"
26
+ - ".rspec"
27
+ - ".standard.yml"
28
+ - ".yardopts"
29
+ - CHANGELOG.md
30
+ - CODE_OF_CONDUCT.md
31
+ - Gemfile
32
+ - Guardfile
33
+ - LICENSE.txt
34
+ - README.md
35
+ - Rakefile
36
+ - benchmark.rb
37
+ - bin/_guard-core
38
+ - bin/console
39
+ - bin/guard
40
+ - bin/rspec
41
+ - bin/setup
42
+ - bin/standardrb
43
+ - ext/ob64/extconf.rb
44
+ - ext/ob64/libbase64.h
45
+ - ext/ob64/ob64.h
46
+ - ext/ob64/ob64_ext.c
47
+ - lib/ob64.rb
48
+ - lib/ob64/core_ext.rb
49
+ - lib/ob64/version.rb
50
+ - ob64.gemspec
51
+ homepage: https://github.com/jcmfernandes/ob64
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/jcmfernandes/ob64
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.5.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.4
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: A fast Base64 encoder and decoder.
75
+ test_files: []