byteman 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f97c79daebb65b447178abe2eb39be01d98583d0f813cd60a92cd2b6172d4f31
4
+ data.tar.gz: a700e4f59a8621fb2c6b9289e42db7ce4715cf897ae487dc2b0ca9021be0d3f5
5
+ SHA512:
6
+ metadata.gz: 11117098c87ef66779236692bb2c8dcda4e2225fffe56afbdb606a6467a943e3fab2d77f53cceec083e10878b4913c1b649ef619b2f605d7a1d16f9865e37f9c
7
+ data.tar.gz: 054cda5e6c527b0ef81032eedb80efd68c50636ffab2fbe68b9153838585b33c55c938fe94cb6e16458d367aa44b2a16302674e30932db55b33c83a670bbc01a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
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, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ 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 both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ 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 micah.shute@gmail.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://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in byteman.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "pry", "~> 0.13.1", group: :development
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ byteman (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ diff-lcs (1.4.2)
11
+ method_source (1.0.0)
12
+ pry (0.13.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rake (12.3.3)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.2)
21
+ rspec-support (~> 3.9.3)
22
+ rspec-expectations (3.9.2)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ byteman!
35
+ pry (~> 0.13.1)
36
+ rake (~> 12.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 micahshute
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.
@@ -0,0 +1,72 @@
1
+ # Byteman
2
+
3
+ Simple gem to allow manipulation of data into other forms. Specifically, transform data between byte buffers (integer arrays with every number representing a byte ie an integer from 0-255), hexdigest strings, hex strings, and integers.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'byteman'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install byteman
20
+
21
+ ## Usage
22
+
23
+ See spec for most examples. Usage is relatively simple. For example, the `.hex` method can transform hexdigest strings, byte arrays, and integers into its corresponding hex string. For example:
24
+
25
+ ```ruby
26
+ Byteman.hex(123456) #=> "\x01\xE2@"
27
+ Byteman.hex([92, 123, 1, 209]) #=> "\\{\x01\xD1"
28
+ ```
29
+
30
+ You can pad your numbers to a specific byte length or bit length using the `.pad` method:
31
+
32
+ ```ruby
33
+ Byteman.pad(num: [43,223], len: 4) #=> [0,0,43, 223]
34
+ Byteman.pad(num: 1234, len: 4) #=> "\x00\x00\x04\xD2"
35
+ Byteman.pad(num: "\xE3\x09", len: 8) #=> "\x00\x00\x00\x00\x00\x00\xE3\x09"
36
+ Byteman.pad(num: 42, len: 8, type: :bits) #=> "00101010"
37
+ Byteman.pad(num: "11001", len: 16, type: :bits) #=> "0000000000011001"
38
+ ```
39
+ And there are more methods to facilitate easy manipulation of bytestreams. All are documented with rdoc and are tested in the spec folder. Some of these methods are:
40
+
41
+ `.digest2buf`, which turns a hexdigest string into a byte buffer:
42
+ ```ruby
43
+ Byteman.digest2buf("68656c6c6f20776f726c64") #=> [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
44
+ ```
45
+
46
+ `.digest2int`, which turns a hexdigest string into an integer:
47
+ ```ruby
48
+ Byteman.digest2int("168656c6c6f20776f726c64") #=> 435692254137895873546447972
49
+
50
+ ```
51
+
52
+ And similar methods such as `.hexdigest`, `.buf2int`, etc.
53
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/byteman. 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/[USERNAME]/byteman/blob/master/CODE_OF_CONDUCT.md).
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
69
+
70
+ ## Code of Conduct
71
+
72
+ Everyone interacting in the Byteman project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/byteman/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
6
+ desc "opens interactive console for this program"
7
+ task :console do
8
+ require 'pry'
9
+ require_relative('./lib/byteman')
10
+ Pry.start
11
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "byteman"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/byteman/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "byteman"
5
+ spec.version = Byteman::VERSION
6
+ spec.authors = ["micahshute"]
7
+ spec.email = ["micah.shute@gmail.com"]
8
+
9
+ spec.summary = %q{Easily manipulate and transform data into different forms (e.g. byte strings, byte array buffers, hexdigests)}
10
+ spec.description = %q{Allows simple transformation of data into hex strings, hexdigest strings, integer byte arrays, etc. as well as padding your data to a certain byte length}
11
+ spec.homepage = "https://github.com/micahshute/byteman"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/micahshute/byteman"
19
+ spec.metadata["changelog_uri"] = "https://github.com/micahshute/byteman"
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('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,196 @@
1
+ require "byteman/version"
2
+
3
+ ##
4
+ # A simple module with methods that allow easy manipulation of data into other forms.
5
+ # Specifically focused on bytes, digests, and integer byte arrays (buffers)
6
+
7
+
8
+ module Byteman
9
+ ##
10
+ # A simple module with methods to allow you to easily manipulate
11
+ # data into different forms, such as strings and numbers to their hex digests,
12
+ # and back again, digests, numbers, and byte buffers into their bytes,
13
+ # padding
14
+
15
+
16
+ # pad args
17
+ #
18
+ # num: (named) [Array or Integer or String] Accepts an array of integers representing bytes (0-255), or an integer of any size (less than the pad size), or a byte string
19
+ #
20
+ # len: (named) [Integer] The number of bytes you want to pad this number to
21
+ #
22
+ # type: (named) [Symbol] (default: :bytes) Determines if you are padding bytes or bits. Defaults to :bytes, also accepts :bits
23
+ #
24
+ # Note that this pads 0s to the front of the number, and returns a byte string if an integer or string is the input arg, and returns an array buffer if an array buffer is entered
25
+ #
26
+ # Unexpected results will occur if the number is greater than the number of total padded byte length
27
+ def self.pad(num: nil, len: , type: :bytes, lsb: false)
28
+ raise ArgumentError.new("Type must be :bytes or :bits") if type != :bytes && type != :bits
29
+ if type == :bytes
30
+ if num.is_a?(Array)
31
+ overflow = len - num.length
32
+ if overflow == 0
33
+ return num
34
+ elsif overflow < 0
35
+ return num[(num.length - len)..-1]
36
+ else
37
+ if lsb
38
+ return num + ([0] * overflow)
39
+ else
40
+ return [0] * overflow + num
41
+ end
42
+ end
43
+ elsif num.is_a?(Integer)
44
+ hex(pad(num: int2buf(num), len: len, lsb: lsb))
45
+ elsif num.is_a?(String)
46
+ pad(num: num.unpack("C*"), len: len, lsb: lsb).pack("C*")
47
+ else
48
+ raise ArgumentError.new("Num must be a Array, Integer, or ByteString")
49
+ end
50
+ else
51
+ if num.is_a?(Integer)
52
+ bin_num = num.to_s(2)
53
+ elsif num.is_a?(String) && num.split('').all?{ |n| n == "0" || n == "1"}
54
+ bin_num = num
55
+ else
56
+ raise ArgumentError("If performing bit padding, the input must be an Integer or a string of bits")
57
+ end
58
+ overflow = len - bin_num.length
59
+ if overflow == 0
60
+ return bin_num
61
+ elsif overflow < 0
62
+ return bin_num[(bin_num.length - len)..-1]
63
+ else
64
+ if lsb
65
+ padded = bin_num + "0" * overflow
66
+ return padded
67
+ else
68
+ padded = "0" * overflow + bin_num
69
+ return padded
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+
76
+ # str2digest args
77
+ #
78
+ # str: [String] the string whose bytes will be turned into a hexdigest representation of the data
79
+ #
80
+ # Returns a hexdigest string
81
+ def self.str2digest(str)
82
+ str.bytes.map do |b|
83
+ hexdigest(b)
84
+ end.join('')
85
+ end
86
+
87
+ # digest2buf args
88
+ #
89
+ # dig: [String] a string hexdigest that will be turned into an array of the represented bytes (the array will be half the length of the number of characters in the digest)
90
+ #
91
+ # Returns an integer array representing the bytes of the hexdigest
92
+ def self.digest2buf(dig)
93
+ hex2buf(hex(dig))
94
+ end
95
+
96
+ # hex2buf args
97
+ #
98
+ # hex: [String] hex string
99
+ #
100
+ # Returns an array of Integers, each representing a byte of data
101
+ def self.hex2buf(hex)
102
+ hex.unpack("C*")
103
+ end
104
+
105
+ # digest2int args
106
+ #
107
+ # dig: [String] hexdigest string
108
+ #
109
+ # Returns a byte String half the length of the hexdigest
110
+ def self.digest2int(dig)
111
+ dig.to_i(16)
112
+ end
113
+
114
+ # hex args
115
+ #
116
+ # input: [Integer or String or Array] accepts any Integer, or a hexdigest String or an integer array (byte buffer) with all numbers between 0-255 inclusive
117
+ #
118
+ # Returns a byte String.
119
+ #
120
+ # Note -> Automatically pads a 0 nibble to the front of any odd-lengthed hexdigest
121
+ def self.hex(input)
122
+ if input.is_a?(Integer)
123
+ hex(hexdigest(input))
124
+ elsif input.is_a?(String)
125
+ hd = input.length.odd? ? "0#{input}" : input
126
+ [hd].pack("H*")
127
+ elsif input.is_a?(Array)
128
+ input.pack("C*")
129
+ else
130
+ raise ArgumentError.new("Input must be a string, number, or byte array buffer")
131
+ end
132
+ end
133
+
134
+ # hexdigest args
135
+ #
136
+ # arg: [Integer or String or Array] accepts any Integer or String or Array of Integers from 0-255
137
+ #
138
+ # Returns a hexdigest string
139
+ def self.hexdigest(arg)
140
+ if arg.is_a?(Integer)
141
+ dig = arg.to_s(16)
142
+ dig.length.odd? ? "0#{dig}" : dig
143
+ elsif arg.is_a?(Array) && arg.all?{|a| a.between?(0,255)}
144
+ buf2digest(arg)
145
+ elsif arg.is_a?(String)
146
+ arg.unpack("H*").first
147
+ else
148
+ raise ArgumentError.new("Input must be an integer, Array of integers between 0-255, or String")
149
+ end
150
+ end
151
+
152
+ # buf2digest args
153
+ #
154
+ # barr: [Array] accepts an integer array (ints between 0-255 inclusive)
155
+ #
156
+ # Returns a hexdigest string
157
+ def self.buf2digest(barr)
158
+ barr.pack("C*").unpack("H*").first
159
+ end
160
+
161
+ # buf2hex args
162
+ #
163
+ # barr: [Array] accepts an integer array (ints between 0-255 inclusive)
164
+ #
165
+ # Returns a byte String
166
+ def self.buf2hex(barr)
167
+ barr.pack("C*")
168
+ end
169
+
170
+ # buf2int args
171
+ #
172
+ # barr: [Array] accepts an integer array (ints between 0-255 inclusive)
173
+ #
174
+ # Returns the Integer represeted by the byte array buffer
175
+ def self.buf2int(barr)
176
+ buf2digest(barr).to_i(16)
177
+ end
178
+
179
+ # int2buf args
180
+ #
181
+ # num: [Integer] any integer
182
+ #
183
+ # Returns an array of Integers between 0-255 inclusive, representing the byte buffer array
184
+ def self.int2buf(num)
185
+ hex(num).bytes
186
+ end
187
+
188
+ # hex2int args
189
+ #
190
+ # hex: [String]
191
+ #
192
+ # Returns the integer represented by the inputted hex string
193
+ def self.hex2int(hex)
194
+ hex.unpack("H*").first.to_i(16)
195
+ end
196
+ end
@@ -0,0 +1,3 @@
1
+ module Byteman
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: byteman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - micahshute
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Allows simple transformation of data into hex strings, hexdigest strings,
14
+ integer byte arrays, etc. as well as padding your data to a certain byte length
15
+ email:
16
+ - micah.shute@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - byteman.gemspec
33
+ - lib/byteman.rb
34
+ - lib/byteman/version.rb
35
+ homepage: https://github.com/micahshute/byteman
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/micahshute/byteman
40
+ source_code_uri: https://github.com/micahshute/byteman
41
+ changelog_uri: https://github.com/micahshute/byteman
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.3.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Easily manipulate and transform data into different forms (e.g. byte strings,
61
+ byte array buffers, hexdigests)
62
+ test_files: []