xxd 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9fb517531102225914b72ff4ad1adbd194d0d1b753e070c94de7eba141869b4a
4
+ data.tar.gz: 1dc807ace3b39c29785357468c6152084c0ef9b6b7a111001d1d368138008e1d
5
+ SHA512:
6
+ metadata.gz: bf6597a2b374ad8ded3f83c5cdfb7741c4a6ae87ac0e82478ac78af2340ddfc4edfdefef12527f84deceec2867daa5832d4643f8658c9377a402aa35958c4aae
7
+ data.tar.gz: b7540ae0b06371817304097fcc40cfe35b861cf684e77d9b8757c9bdc4317b56dbeb38446a87c894d131e821442e9e82952309e61bd284d689127278aa72d14d
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in xxd.gemspec
6
+ gemspec
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xxd (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ method_source (0.9.2)
11
+ minitest (5.11.3)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.17)
22
+ minitest (~> 5.0)
23
+ pry
24
+ rake (~> 10.0)
25
+ xxd!
26
+
27
+ BUNDLED WITH
28
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Lee Hambley
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,91 @@
1
+ # Xxd
2
+
3
+ I put this Gem together one night to help make it easier to deal with binary blobs in Ruby specs/assertions.
4
+
5
+ I found some libraries that can dump hex into a nice table with offsets and the printable ASCII bytes, and some
6
+ which could parse the same, but nothing that could do both without pulling in a load of dependencies.
7
+
8
+ This takes the dumbest possible approach to natively, and naïvely implement the default functionality of `xxd` from GNU.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'xxd'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install xxd
25
+
26
+ ## Usage
27
+
28
+ There are two methods `Xxd.dump` and `Xxd.parse`. There are no configuration options. Given a string the gem will
29
+ print it as a hex table in this style:
30
+
31
+ 00000000: 2159 a651 f852 132c 5f83 1259 9acd d837 !Y.Q.R.,_..Y...7
32
+ 00000010: 0340 44d2 e08f 6979 46ee 40dc d6a0 bfa3 .@D...iyF.@.....
33
+ 00000020: 0c63 a246 1eae 5ddd 9a59 9fa2 f14d d57c .c.F..]..Y...M.|
34
+ 00000030: f13f 8aa5 b7dd 7384 c620 133f f43c 190f .?....s.. .?.<..
35
+ 00000040: a89a 3f85 f958 be10 f8dd d7b5 a5d6 907f ..?..X..........
36
+ 00000050: 78ee 3b93 8fb5 5c51 51ed eb71 e23c 7c75 x.;...\QQ..q.<|u
37
+ 00000060: bde0 2e05 ....
38
+
39
+ If the input is too large, all bets are off. The logic for the left column is weak.
40
+
41
+ Usage from Ruby looks like this:
42
+
43
+ $ irb -Ilib -rxxd
44
+ irb(main):004:0> message = "this is a message consisting only of printable ascii characters"
45
+ => "this is a message consisting only of printable ascii characters"
46
+ irb(main):005:0> puts Xxd.dump(message)
47
+ 00000000: 7468 6973 2069 7320 6120 6d65 7373 6167 this is a messag
48
+ 00000010: 6520 636f 6e73 6973 7469 6e67 206f 6e6c e consisting onl
49
+ 00000020: 7920 6f66 2070 7269 6e74 6162 6c65 2061 y of printable a
50
+ 00000030: 7363 6969 2063 6861 7261 6374 6572 73 scii characters
51
+ => nil
52
+
53
+ Printing pure ASCII isn't very interesting, so binary output is handled thusly:
54
+
55
+ irb(main):006:0> message = File.open("/dev/urandom").read(200)
56
+ => "\xD6-\xBD\xC9x\xFA4\x8Dxz\x1C\xC2,:\xDF\x1FFY&\xE4UV\x9A}\x9Dv\xB9\xE3\xF2\xAAz\xF5\x9Cg\x86\x1DX\x85\xD7caQ\xE0\xBC\xBA\x04\x154|\xE3\xBBXX\xFFi\x9Ah\x16Bl\xE6\x9D\xFA\f\b\x88skaH\xD1\xAC~a\x9D\x1E\xA9\x7F\xAA\x7F\x0Er\x8C\x05_\xEF\x9C\xAD<\x1CF\xC4\xF1\xE0\xDE\xE2\xB1\xC4H\x10M\"Mu\xCD\xCD\x18W\x03\xD0<j\xE3\x0Et5e\xD8d\xC7\xE7\xD6\xFCO\xA7\xDF\x18\xE8\xC5t:iKG`\xDAG\x157\xA1~\xA6\x95{\x97\x93t\xFC\x8Bz'Z\xC7\xDEH\x04\x1D\xD7\xB32>\xB0\xA3\xBB\xFB\xD2\x9E\xEA5\xAA\xDA\x8C\xEF\xF6B|\xF7JU5\xDE4\xB6$\xBB\x88\xBD|\x92\x8E?\x04\xAC\x8A\xE3\x1F \xF6\x99\r"
57
+ irb(main):007:0> puts Xxd.dump(message)
58
+ 00000000: d62d bdc9 78fa 348d 787a 1cc2 2c3a df1f .-..x.4.xz..,:..
59
+ 00000010: 4659 26e4 5556 9a7d 9d76 b9e3 f2aa 7af5 FY&.UV.}.v....z.
60
+ 00000020: 9c67 861d 5885 d763 6151 e0bc ba04 1534 .g..X..caQ.....4
61
+ 00000030: 7ce3 bb58 58ff 699a 6816 426c e69d fa0c |..XX.i.h.Bl....
62
+ 00000040: 0888 736b 6148 d1ac 7e61 9d1e a97f aa7f ..skaH..~a......
63
+ 00000050: 0e72 8c05 5fef 9cad 3c1c 46c4 f1e0 dee2 .r.._...<.F.....
64
+ 00000060: b1c4 4810 4d22 4d75 cdcd 1857 03d0 3c6a ..H.M"Mu...W..<j
65
+ 00000070: e30e 7435 65d8 64c7 e7d6 fc4f a7df 18e8 ..t5e.d....O....
66
+ 00000080: c574 3a69 4b47 60da 4715 37a1 7ea6 957b .t:iKG`.G.7.~..{
67
+ 00000090: 9793 74fc 8b7a 275a c7de 4804 1dd7 b332 ..t..z'Z..H....2
68
+ 000000a0: 3eb0 a3bb fbd2 9eea 35aa da8c eff6 427c >.......5.....B|
69
+ 000000b0: f74a 5535 de34 b624 bb88 bd7c 928e 3f04 .JU5.4.$...|..?.
70
+ 000000c0: ac8a e31f 20f6 990d .... ...
71
+ => nil
72
+
73
+ The original message can be recovered thusly:
74
+
75
+ irb(main):012:0> Xxd.parse(Xxd.dump(message))
76
+ => "\xD6-\xBD\xC9x\xFA4\x8Dxz\x1C\xC2,:\xDF\x1FFY&\xE4UV\x9A}\x9Dv\xB9\xE3\xF2\xAAz\xF5\x9Cg\x86\x1DX\x85\xD7caQ\xE0\xBC\xBA\x04\x154|\xE3\xBBXX\xFFi\x9Ah\x16Bl\xE6\x9D\xFA\f\b\x88skaH\xD1\xAC~a\x9D\x1E\xA9\x7F\xAA\x7F\x0Er\x8C\x05_\xEF\x9C\xAD<\x1CF\xC4\xF1\xE0\xDE\xE2\xB1\xC4H\x10M\"Mu\xCD\xCD\x18W\x03\xD0<j\xE3\x0Et5e\xD8d\xC7\xE7\xD6\xFCO\xA7\xDF\x18\xE8\xC5t:iKG`\xDAG\x157\xA1~\xA6\x95{\x97\x93t\xFC\x8Bz'Z\xC7\xDEH\x04\x1D\xD7\xB32>\xB0\xA3\xBB\xFB\xD2\x9E\xEA5\xAA\xDA\x8C\xEF\xF6B|\xF7JU5\xDE4\xB6$\xBB\x88\xBD|\x92\x8E?\x04\xAC\x8A\xE3\x1F \xF6\x99\r"
77
+
78
+
79
+ ## Development
80
+
81
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
82
+
83
+ 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).
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xxd.
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "xxd"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task :dump do
12
+ fixture = File.binread(File.join(File.dirname(__FILE__), "test/random-data-from-urandom"))
13
+ puts Xxd.dump(fixture)
14
+ end
15
+
16
+ task :default => :dump
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xxd"
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,27 @@
1
+ require "xxd/version"
2
+
3
+ module Xxd
4
+ class Error < StandardError; end
5
+
6
+ def dump(input)
7
+ io = StringIO.new
8
+ res = input.bytes.each_slice(2).each_slice(8).each_with_index do |row, i|
9
+ io.write format(
10
+ "%07x0: %-40s %-16s\n",
11
+ i,
12
+ row.map { |pair| pair.map { |b| b.to_s(16).rjust(2, "0") }.join }.join(" "),
13
+ row.flat_map { |pair| pair.map { |b| (b >= 32 && b < 127 ? b.chr : ".") } }.flatten.join
14
+ )
15
+ end
16
+ io.string
17
+ end
18
+ module_function :dump
19
+
20
+ def parse(input)
21
+ res = input.lines.flat_map do |line|
22
+ line[10..48].gsub(" ", "").scan(/../).map { |hb| hb.to_i(16) }
23
+ end.pack("c*")
24
+ end
25
+ module_function :parse
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ module Xxd
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "xxd/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xxd"
8
+ spec.version = Xxd::VERSION
9
+ spec.authors = ["Lee Hambley"]
10
+ spec.email = ["lee.hambley@gmail.com"]
11
+
12
+ spec.summary = %q{simple naïve implementation of xxd in both directions for Ruby}
13
+ spec.homepage = "https://github.com/leehambley/xxd"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/leehambley/xxd"
23
+ spec.metadata["changelog_uri"] = "https://github.com/leehambley/xxd/blob/master/CHANGELOG"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "minitest", "~> 5.0"
41
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xxd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lee Hambley
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '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: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - lee.hambley@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/xxd.rb
72
+ - lib/xxd/version.rb
73
+ - xxd.gemspec
74
+ homepage: https://github.com/leehambley/xxd
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/leehambley/xxd
79
+ source_code_uri: https://github.com/leehambley/xxd
80
+ changelog_uri: https://github.com/leehambley/xxd/blob/master/CHANGELOG
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: simple naïve implementation of xxd in both directions for Ruby
100
+ test_files: []