cobhan 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: 309e7e9e4dc297cca788ad4a722c4b5e686e21f071f1faff64d9256ca5eec267
4
+ data.tar.gz: 3a2f819a9bb2d1d67ea99e3739cadfeb80a27354a2deebd007e57b366d21ebc4
5
+ SHA512:
6
+ metadata.gz: 91c3c544862b16a4b4a41312bcd3ce5208f75f9fc738e3940bada7c7fac08f899e5cb3fbadd4731ab512e34adcb5a0b0cfd11e9560ef51aa82200a48660d04ef
7
+ data.tar.gz: 9498b1799ec6834e1659f2183b1d05b9a5e0bbdaa53856facd7200228e3e285b27639124d8edc29e5aeb66f967d2e20a2b8e0aa35e4803d48aaa68b0ecb93d79
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-01
4
+
5
+ - Initial release
@@ -0,0 +1,77 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies within all project spaces, and it also applies when
49
+ an individual is representing the project or its community in public spaces.
50
+ Examples of representing a project or community include using an official
51
+ project e-mail address, posting via an official social media account, or acting
52
+ as an appointed representative at an online or offline event. Representation of
53
+ a project may be further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at oss@godaddy.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
77
+
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,128 @@
1
+ # Contributing
2
+
3
+ Everyone is welcome to contribute to GoDaddy's Open Source Software.
4
+ Contributing doesn’t just mean submitting pull requests. To get involved,
5
+ you can report or triage bugs, and participate in discussions on the
6
+ evolution of each project.
7
+
8
+ No matter how you want to get involved, we ask that you first learn what’s
9
+ expected of anyone who participates in the project by reading the Contribution
10
+ Guidelines and our [Code of Conduct][coc].
11
+
12
+ **Please Note:** GitHub is for bug reports and contributions primarily -
13
+ if you have a support question head over to [GoDaddy's Open Source
14
+ Software Slack channel][slack]. You can request an invite
15
+ [here][invite].
16
+
17
+ ## Answering Questions
18
+
19
+ One of the most important and immediate ways you can support this project is
20
+ to answer questions on [Slack][slack] or [Github][issues]. Whether you’re
21
+ helping a newcomer understand a feature or troubleshooting an edge case with a
22
+ seasoned developer, your knowledge and experience with a programming language
23
+ can go a long way to help others.
24
+
25
+ ## Reporting Bugs
26
+
27
+ **Do not report potential security vulnerabilities here. Refer to
28
+ [SECURITY.md](./SECURITY.md) for more details about the process of reporting
29
+ security vulnerabilities.**
30
+
31
+ Before submitting a ticket, please search our [Issue Tracker][issues] to make
32
+ sure it does not already exist and have a simple replication of the behavior. If
33
+ the issue is isolated to one of the dependencies of this project, please create
34
+ a Github issue in that project. All dependencies should be open source software
35
+ and can be found on Github.
36
+
37
+ Submit a ticket for your issue, assuming one does not already exist:
38
+
39
+ - Create it on the project's [issue Tracker][issues].
40
+ - Clearly describe the issue by following the template layout
41
+ - Make sure to include steps to reproduce the bug.
42
+ - A reproducible (unit) test could be helpful in solving the bug.
43
+ - Describe the environment that (re)produced the problem.
44
+
45
+ ## Triaging bugs or contributing code
46
+
47
+ If you're triaging a bug, first make sure that you can reproduce it. Once a bug
48
+ can be reproduced, reduce it to the smallest amount of code possible. Reasoning
49
+ about a sample or unit test that reproduces a bug in just a few lines of code
50
+ is easier than reasoning about a longer sample.
51
+
52
+ From a practical perspective, contributions are as simple as:
53
+
54
+ 1. Fork and clone the repo, [see Github's instructions if you need help.][fork]
55
+ 1. Create a branch for your PR with `git checkout -b pr/your-branch-name`
56
+ 1. Make changes on the branch of your forked repository.
57
+ 1. When committing, reference your issue (if present) and include a note about
58
+ the fix.
59
+ 1. Please also add/update unit tests for your changes.
60
+ 1. Push the changes to your fork and submit a pull request to the 'main
61
+ development branch' branch of the projects' repository.
62
+
63
+ If you are interested in making a large change and feel unsure about its overall
64
+ effect, start with opening an Issue in the project's [Issue Tracker][issues]
65
+ with a high-level proposal and discuss it with the core contributors through
66
+ Github comments or in [Slack][slack]. After reaching a consensus with core
67
+ contributors about the change, discuss the best way to go about implementing it.
68
+
69
+ > Tip: Keep your main branch pointing at the original repository and make
70
+ > pull requests from branches on your fork. To do this, run:
71
+ >
72
+ > ```sh
73
+ > git remote add upstream https://github.com/godaddy/cobhan-ruby.git
74
+ > git fetch upstream
75
+ > git branch --set-upstream-to=upstream/main main
76
+ > ```
77
+ >
78
+ > This will add the original repository as a "remote" called "upstream," Then
79
+ > fetch the git information from that remote, then set your local main
80
+ > branch to use the upstream main branch whenever you run git pull. Then you
81
+ > can make all of your pull request branches based on this main branch.
82
+ > Whenever you want to update your version of main, do a regular git pull.
83
+
84
+ ## Code Review
85
+
86
+ Any open source project relies heavily on code review to improve software
87
+ quality. All significant changes, by all developers, must be reviewed before
88
+ they are committed to the repository. Code reviews are conducted on GitHub
89
+ through comments on pull requests or commits. The developer responsible for a
90
+ code change is also responsible for making all necessary review-related changes.
91
+
92
+ Sometimes code reviews will take longer than you would hope for, especially for
93
+ larger features. Here are some accepted ways to speed up review times for your
94
+ patches:
95
+
96
+ - Review other people’s changes. If you help out, others will more likely be
97
+ willing to do the same for you.
98
+ - Split your change into multiple smaller changes. The smaller your change,
99
+ the higher the probability that somebody will take a quick look at it.
100
+ - Mention the change on [Slack][slack]. If it is urgent, provide reasons why it
101
+ is important to get this change landed. Remember that you are asking for valuable
102
+ time from other professional developers.
103
+
104
+ **Note that anyone is welcome to review and give feedback on a change, but only
105
+ people with commit access to the repository can approve it.**
106
+
107
+ ## Attribution of Changes
108
+
109
+ When contributors submit a change to this project, after that change is
110
+ approved, other developers with commit access may commit it for the author. When
111
+ doing so, it is important to retain correct attribution of the contribution.
112
+ Generally speaking, Git handles attribution automatically.
113
+
114
+ ## Code Style and Documentation
115
+
116
+ Ensure that your contribution follows the standards set by the project's style
117
+ guide with respect to patterns, naming, documentation and testing.
118
+
119
+ # Additional Resources
120
+
121
+ - [General GitHub Documentation](https://help.github.com/)
122
+ - [GitHub Pull Request documentation](https://help.github.com/send-pull-requests/)
123
+
124
+ [issues]: https://github.com/godaddy/cobhan-ruby/issues/
125
+ [coc]: ./CODE_OF_CONDUCT.md
126
+ [slack]: https://godaddy-oss.slack.com/
127
+ [fork]: https://help.github.com/en/articles/fork-a-repo
128
+ [invite]: https://godaddy-oss-slack.herokuapp.com
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cobhan.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cobhan (0.1.0)
5
+ ffi (~> 1.15.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.0)
12
+ ffi (1.15.5)
13
+ parallel (1.21.0)
14
+ parser (3.1.1.0)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.1.1)
17
+ rake (13.0.6)
18
+ regexp_parser (2.2.1)
19
+ rexml (3.2.5)
20
+ rspec (3.11.0)
21
+ rspec-core (~> 3.11.0)
22
+ rspec-expectations (~> 3.11.0)
23
+ rspec-mocks (~> 3.11.0)
24
+ rspec-core (3.11.0)
25
+ rspec-support (~> 3.11.0)
26
+ rspec-expectations (3.11.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.11.0)
29
+ rspec-mocks (3.11.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.11.0)
32
+ rspec-support (3.11.0)
33
+ rubocop (1.25.1)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.1.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml
39
+ rubocop-ast (>= 1.15.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 1.4.0, < 3.0)
42
+ rubocop-ast (1.16.0)
43
+ parser (>= 3.1.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.1.0)
46
+
47
+ PLATFORMS
48
+ x86_64-darwin-20
49
+
50
+ DEPENDENCIES
51
+ cobhan!
52
+ rake (~> 13.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 1.21)
55
+
56
+ BUNDLED WITH
57
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 GoDaddy Operating Company, LLC.
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.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Cobhan
2
+
3
+ Cobhan FFI is a proof of concept system for enabling shared code to be written in Rust or Go and consumed from all major languages/platforms in a safe and effective way, using easy helper functions to manage any unsafe data marshaling.
4
+
5
+ ## Types
6
+
7
+ * Supported types
8
+ * int32 - 32bit signed integer
9
+ * int64 - 64bit signed integer
10
+ * float64 - double precision 64bit IEEE 754 floating point
11
+ * Cobhan buffer - length delimited 8bit buffer (no null delimiters)
12
+ * utf-8 encoded string
13
+ * JSON
14
+ * binary data
15
+ * Cobhan buffer details
16
+ * Callers provide the output buffer allocation and capacity
17
+ * Called functions can transparently return larger values via temporary files
18
+ * **Modern [tmpfs](https://en.wikipedia.org/wiki/Tmpfs) is entirely memory backed**
19
+ * Return values
20
+ * Functions that return scalar values can return the value directly
21
+ * Functions *can* use special case and return maximum positive or maximum negative or zero values to represent error or overflow conditions
22
+ * Functions *can* allow scalar values to wrap
23
+ * Functions should document their overflow / underflow behavior
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'cobhan'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ ```bash
36
+ bundle install
37
+ ```
38
+
39
+ Or install it yourself as:
40
+
41
+ ```bash
42
+ gem install cobhan
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ By running the demo, it will automatically build the sample [libcobhandemo](spec/support/libcobhandemo/libcobhandemo.go) binary.
48
+
49
+ ```bash
50
+ bundle exec ruby demo/demo.rb
51
+ ```
52
+
53
+ Once the binary is built, to rebuilt it, you'll have to cleanup the `tmp` dir.
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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).
60
+
61
+ ### Running specs
62
+
63
+ ```bash
64
+ bundle exec rspec spec
65
+ ```
66
+
67
+ ### Running specs inside vagrant
68
+
69
+ ```bash
70
+ vagrant up
71
+ vagrant ssh
72
+ cd /vagrant/
73
+ bundle install
74
+ bundle exec rspec spec
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/godaddy/cobhan-ruby.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
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]
data/SECURITY.md ADDED
@@ -0,0 +1,19 @@
1
+ # Reporting Security Issues
2
+
3
+ We take security very seriously at GoDaddy. We appreciate your efforts to
4
+ responsibly disclose your findings, and will make every effort to acknowledge
5
+ your contributions.
6
+
7
+ ## Where should I report security issues?
8
+
9
+ In order to give the community time to respond and upgrade, we strongly urge you
10
+ report all security issues privately.
11
+
12
+ To report a security issue in one of our Open Source projects email us directly
13
+ at **oss@godaddy.com** and include the word "SECURITY" in the subject line.
14
+
15
+ This mail is delivered to our Open Source Security team.
16
+
17
+ After the initial reply to your report, the team will keep you informed of the
18
+ progress being made towards a fix and announcement, and may ask for additional
19
+ information or guidance.
data/Vagrantfile ADDED
@@ -0,0 +1,31 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ $script = <<-SCRIPT
5
+ set -e
6
+
7
+ echo "Install RVM"
8
+ gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
9
+ # If key add does not work, see https://rvm.io/rvm/security for alternatives.
10
+ curl -sSL https://get.rvm.io | bash -s stable
11
+ source /home/vagrant/.rvm/scripts/rvm
12
+
13
+ RUBY_VERSION=3.1.0
14
+ echo "Install Ruby $RUBY_VERSION"
15
+ rvm install $RUBY_VERSION
16
+
17
+ echo "Install git"
18
+ sudo yum install git -y
19
+
20
+ echo "Install docker"
21
+ sudo yum install -y yum-utils
22
+ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
23
+ sudo yum install docker-ce -y
24
+ sudo usermod -aG docker $USER
25
+ sudo systemctl restart docker.service
26
+ SCRIPT
27
+
28
+ Vagrant.configure("2") do |config|
29
+ config.vm.box = "centos/7"
30
+ config.vm.provision "shell", inline: $script, privileged: false
31
+ end
data/cobhan.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cobhan/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cobhan"
7
+ spec.version = Cobhan::VERSION
8
+ spec.authors = ["GoDaddy"]
9
+ spec.email = ["oss@godaddy.com"]
10
+
11
+ spec.summary = "Ruby wrapper library for the Cobhan FFI system"
12
+ spec.description = "Cobhan FFI is a proof of concept system for enabling shared code to be written in Rust or Go and consumed from all major languages/platforms in a safe and effective way, using easy helper functions to manage any unsafe data marshaling."
13
+
14
+ spec.summary = 'Wrapper sample for Golang c-shared build'
15
+ spec.description = 'Wrapper sample for Golang c-shared build implementation'
16
+
17
+ spec.homepage = "https://github.com/godaddy/cobhan-ruby"
18
+ spec.license = "MIT"
19
+ spec.required_ruby_version = ">= 2.5.0"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/godaddy/cobhan-ruby"
23
+ spec.metadata["changelog_uri"] = "https://github.com/godaddy/cobhan-ruby/blob/main/CHANGELOG.md"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|demo|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
30
+ end
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency 'ffi', '~> 1.15.4'
37
+
38
+ spec.add_development_dependency 'rspec', '~> 3.10.0'
39
+ spec.add_development_dependency 'rubocop', '~> 1.7'
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cobhan
4
+ VERSION = "0.1.0"
5
+ end
data/lib/cobhan.rb ADDED
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cobhan/version"
4
+
5
+ require 'ffi'
6
+
7
+ module Cobhan
8
+ UnsupportedPlatformError = Class.new(StandardError)
9
+
10
+ include FFI::Library
11
+
12
+ EXTS = { 'linux' => 'so', 'darwin' => 'dylib', 'windows' => 'dll' }.freeze
13
+ CPU_ARCHS = { 'x86_64' => 'x64', 'aarch64' => 'arm64' }.freeze
14
+
15
+ SIZEOF_INT32 = 32 / 8
16
+ BUFFER_HEADER_SIZE = SIZEOF_INT32 * 2
17
+ MINIMUM_ALLOCATION = 1024
18
+
19
+ def library_file_name(name)
20
+ ext = EXTS[FFI::Platform::OS]
21
+ raise UnsupportedPlatformError, "Unsupported OS: #{FFI::Platform::OS}" unless ext
22
+
23
+ cpu_arch = CPU_ARCHS[FFI::Platform::ARCH]
24
+ raise UnsupportedPlatformError, "Unsupported CPU: #{FFI::Platform::ARCH}" unless cpu_arch
25
+
26
+ "#{name}-#{cpu_arch}.#{ext}"
27
+ end
28
+
29
+ def load_library(lib_root_path, name, functions)
30
+ # To load other libs that depend on relative paths, chdir to lib path dir.
31
+ Dir.chdir(lib_root_path) do
32
+ ffi_lib File.join(lib_root_path, library_file_name(name))
33
+ end
34
+
35
+ functions.each do |function|
36
+ attach_function(*function)
37
+ end
38
+ end
39
+
40
+ def string_to_cbuffer(input)
41
+ buffer_ptr = FFI::MemoryPointer.new(1, BUFFER_HEADER_SIZE + input.bytesize, false)
42
+ buffer_ptr.put_int32(0, input.bytesize)
43
+ buffer_ptr.put_int32(SIZEOF_INT32, 0) # Reserved - must be zero
44
+ buffer_ptr.put_bytes(BUFFER_HEADER_SIZE, input)
45
+ buffer_ptr
46
+ end
47
+
48
+ def cbuffer_to_string(buffer)
49
+ length = buffer.get_int32(0)
50
+ if length >= 0
51
+ buffer.get_bytes(BUFFER_HEADER_SIZE, length)
52
+ else
53
+ temp_to_string(buffer, length)
54
+ end
55
+ end
56
+
57
+ def temp_to_string(buffer, length)
58
+ length = 0 - length
59
+ filename = buffer.get_bytes(BUFFER_HEADER_SIZE, length)
60
+ # Read file with name in payload, and replace payload
61
+ bytes = IO.binread(filename)
62
+ File.delete(filename)
63
+ bytes
64
+ end
65
+
66
+ def allocate_cbuffer(size)
67
+ size = [size, MINIMUM_ALLOCATION].max
68
+ buffer_ptr = FFI::MemoryPointer.new(1, BUFFER_HEADER_SIZE + size, false)
69
+ buffer_ptr.put_int32(0, size)
70
+ buffer_ptr.put_int32(SIZEOF_INT32, 0) # Reserved - must be zero
71
+ buffer_ptr
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cobhan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - GoDaddy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.15.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.15.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.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: 3.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ description: Wrapper sample for Golang c-shared build implementation
56
+ email:
57
+ - oss@godaddy.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - ".ruby-version"
65
+ - CHANGELOG.md
66
+ - CODE_OF_CONDUCT.md
67
+ - CONTRIBUTING.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - SECURITY.md
74
+ - Vagrantfile
75
+ - cobhan.gemspec
76
+ - lib/cobhan.rb
77
+ - lib/cobhan/version.rb
78
+ homepage: https://github.com/godaddy/cobhan-ruby
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ homepage_uri: https://github.com/godaddy/cobhan-ruby
83
+ source_code_uri: https://github.com/godaddy/cobhan-ruby
84
+ changelog_uri: https://github.com/godaddy/cobhan-ruby/blob/main/CHANGELOG.md
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.5.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.3.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Wrapper sample for Golang c-shared build
104
+ test_files: []