zhexdump 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tmp.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zhexdump.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrey "Zed" Zaikin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,122 @@
1
+ # ZHexdump
2
+
3
+ A highly flexible hexdump implementation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zhexdump'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zhexdump
18
+
19
+ ## Usage
20
+
21
+ ### Simple dump (to STDOUT)
22
+ ```ruby
23
+ ZHexdump.dump "abc123"*5
24
+
25
+ # output:
26
+ 00000000: 61 62 63 31 32 33 61 62 63 31 32 33 61 62 63 31 |abc123abc123abc1|
27
+ 00000010: 32 33 61 62 63 31 32 33 61 62 63 31 32 33 |23abc123abc123 |
28
+ ```
29
+
30
+ ### Dump to string
31
+ ```ruby
32
+ s = ''
33
+ ZHexdump.dump "abc123", :output => s
34
+ puts "START\n#{s}END"
35
+
36
+ # output:
37
+ START
38
+ 00000000: 61 62 63 31 32 33 |abc123 |
39
+ END
40
+ ```
41
+
42
+ ### Custom width
43
+ ```ruby
44
+ ZHexdump.dump "abc123"*2, :width => 3
45
+
46
+ # output:
47
+ 00000000: 61 62 63 |abc|
48
+ 00000003: 31 32 33 |123|
49
+ 00000006: 61 62 63 |abc|
50
+ 00000009: 31 32 33 |123|
51
+ ```
52
+
53
+ ### Dumping only part of data
54
+ ```ruby
55
+ ZHexdump.dump "0123456789abcdef", :size => 5, :offset => 3
56
+
57
+ # output:
58
+ 00000003: 33 34 35 36 37 |34567 |
59
+ ```
60
+
61
+ ### Hide offset
62
+ ```ruby
63
+ ZHexdump.dump "abc123", :offset => false
64
+
65
+ # output:
66
+ 61 62 63 31 32 33 |abc123 |
67
+ ```
68
+
69
+ ### Add to offset
70
+ ```ruby
71
+ ZHexdump.dump "abc123", :add => 0x1234
72
+
73
+ # output:
74
+ 00001234: 61 62 63 31 32 33 |abc123 |
75
+ ```
76
+
77
+ ### Duplicate rows hiding enabled (default)
78
+ ```ruby
79
+ ZHexdump.dump "0123456789abcdef"*5
80
+
81
+ # output:
82
+ 00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|
83
+ *
84
+ 00000050:
85
+ ```
86
+
87
+ ### Duplicate rows hiding disabled
88
+ ```ruby
89
+ ZHexdump.dump "0123456789abcdef"*5, :dedup => false
90
+
91
+ # output:
92
+ 00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|
93
+ 00000010: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|
94
+ 00000020: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|
95
+ 00000030: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|
96
+ 00000040: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|
97
+ ```
98
+
99
+ ### Tail comment
100
+ ```ruby
101
+ ZHexdump.dump "abc123", :tail => " comment here"
102
+
103
+ # output:
104
+ 00000000: 61 62 63 31 32 33 |abc123 | comment here
105
+ ```
106
+
107
+ ### Row preprocessing
108
+ ```ruby
109
+ lineno = 1
110
+ ZHexdump.dump "abc123"*10 do |row, pos, ascii|
111
+ row.gsub!(/ 3[123]/, " ..")
112
+ row.insert 0, " (line ##{lineno}) "
113
+ ascii.tr! '123',"_"
114
+ lineno += 1
115
+ end
116
+
117
+ # output:
118
+ (line #1) 00000000: 61 62 63 .. .. .. 61 62 63 .. .. .. 61 62 63 .. |abc___abc___abc_|
119
+ (line #2) 00000010: .. .. 61 62 63 .. .. .. 61 62 63 .. .. .. 61 62 |__abc___abc___ab|
120
+ (line #3) 00000020: 63 .. .. .. 61 62 63 .. .. .. 61 62 63 .. .. .. |c___abc___abc___|
121
+ (line #4) 00000030: 61 62 63 .. .. .. 61 62 63 .. .. .. |abc___abc___ |
122
+ ```
@@ -0,0 +1,77 @@
1
+ # ZHexdump
2
+
3
+ A very flexible hexdump implementation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zhexdump'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zhexdump
18
+
19
+ ## Usage
20
+
21
+ ### Simple dump (to STDOUT)
22
+ ```ruby
23
+ ZHexdump.dump "abc123"*5
24
+ ```
25
+
26
+ ### Dump to string
27
+ ```ruby
28
+ s = ''
29
+ ZHexdump.dump "abc123", :output => s
30
+ puts "START\n#{s}END"
31
+ ```
32
+
33
+ ### Custom width
34
+ ```ruby
35
+ ZHexdump.dump "abc123"*2, :width => 3
36
+ ```
37
+
38
+ ### Dumping only part of data
39
+ ```ruby
40
+ ZHexdump.dump "0123456789abcdef", :size => 5, :offset => 3
41
+ ```
42
+
43
+ ### Hide offset
44
+ ```ruby
45
+ ZHexdump.dump "abc123", :offset => false
46
+ ```
47
+
48
+ ### Add to offset
49
+ ```ruby
50
+ ZHexdump.dump "abc123", :add => 0x1234
51
+ ```
52
+
53
+ ### Duplicate rows hiding enabled (default)
54
+ ```ruby
55
+ ZHexdump.dump "0123456789abcdef"*5
56
+ ```
57
+
58
+ ### Duplicate rows hiding disabled
59
+ ```ruby
60
+ ZHexdump.dump "0123456789abcdef"*5, :dedup => false
61
+ ```
62
+
63
+ ### Tail comment
64
+ ```ruby
65
+ ZHexdump.dump "abc123", :tail => " comment here"
66
+ ```
67
+
68
+ ### Row preprocessing
69
+ ```ruby
70
+ lineno = 1
71
+ ZHexdump.dump "abc123"*10 do |row, pos, ascii|
72
+ row.gsub!(/ 3[123]/, " ..")
73
+ row.insert 0, " (line ##{lineno}) "
74
+ ascii.tr! '123',"_"
75
+ lineno += 1
76
+ end
77
+ ```
@@ -0,0 +1,32 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "run specs"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+
9
+ desc "build readme"
10
+ task :readme do
11
+ tpl = File.read('README.md.tpl')
12
+ result = tpl.gsub(/^### ([^~`\n]+?)\n```ruby(.+?)^```/m) do |x|
13
+ title, code = $1, $2
14
+
15
+ File.open("tmp.rb", "w:utf-8") do |f|
16
+ # f.puts "#coding: utf-8"
17
+ # f.puts "$:.unshift('../lib')"
18
+ f.puts "require 'zhexdump'"
19
+ # f.puts "srand 0"
20
+ f.puts code
21
+ end
22
+
23
+ puts "[.] #{title} .. "
24
+ out = `ruby -Ilib tmp.rb`
25
+ exit unless $?.success?
26
+
27
+ x.sub code, code+"\n # output:\n"+out.split("\n").map{|x| " #{x}"}.join("\n")+"\n"
28
+ end
29
+ File.unlink("tmp.rb")
30
+ File.open('README.md','w'){ |f| f << result }
31
+ #puts result
32
+ end
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path("zhexdump/version", File.dirname(__FILE__))
3
+
4
+ module ZHexdump
5
+
6
+ # two methods for using when module ZHexdump is included in some class,
7
+ # f.ex. String:
8
+
9
+ # default hexdump to STDOUT (unless options[:output] overridden)
10
+ def hexdump options = {}, &block
11
+ ZHexdump.dump self, options, &block
12
+ end
13
+
14
+ # default hexdump to string (unless options[:output] overridden)
15
+ def to_hexdump options = {}, &block
16
+ r = ''
17
+ options[:output] ||= r
18
+ ZHexdump.dump self, options, &block
19
+ r
20
+ end
21
+
22
+ class << self
23
+ def dump data, h = {}
24
+ offset = h.fetch(:offset, 0)
25
+ dedup = h.fetch(:dedup, true)
26
+ show_offset = h.fetch(:show_offset, h.fetch(:show_addr, true))
27
+
28
+ if offset == false
29
+ show_offset = false
30
+ offset = 0
31
+ end
32
+
33
+ add = h[:add] || 0
34
+ size = h[:size] || (data.size-offset)
35
+ tail = h[:tail] || "\n"
36
+ width = h[:width] || 0x10 # row width, in bytes
37
+ output = h[:output] || $>
38
+
39
+ size = data.size-offset if size+offset > data.size
40
+
41
+ prevhex = ''; c = nil; prevdup = false; start = offset
42
+ while true
43
+ ascii = ''; hex = ''
44
+ width.times do |i|
45
+ hex << ' ' if i%8==0 && i>0
46
+ if c = ((size > 0) && data[offset+i])
47
+ hex << "%02x " % c.ord
48
+ ascii << ((32..126).include?(c.ord) ? c : '.')
49
+ else
50
+ hex << ' '
51
+ ascii << ' '
52
+ end
53
+ size-=1
54
+ end
55
+
56
+ if dedup && hex == prevhex
57
+ row = "*"
58
+ yield(row, offset+add, ascii) if block_given?
59
+ unless prevdup
60
+ output << "\n" if offset > start
61
+ output << row
62
+ end
63
+ prevdup = true
64
+ else
65
+ row = (show_offset ? ("%08x: " % (offset + add)) : '') + hex
66
+ yield(row, offset+add, ascii) if block_given?
67
+ row << ' |' + ascii + "|"
68
+ output << "\n" if offset > start
69
+ output << row
70
+ prevdup = false
71
+ end
72
+
73
+ offset += width
74
+ prevhex = hex
75
+ break if size <= 0
76
+ end
77
+ if show_offset && prevdup
78
+ row = "%08x: " % (offset + add)
79
+ yield(row) if block_given?
80
+ output << "\n" << row
81
+ end
82
+ output << tail
83
+ end # dump
84
+
85
+ alias :hexdump :dump
86
+ end # class << self
87
+ end # module ZHexdump
88
+
89
+ Zhexdump = ZHexdump
90
+
91
+ if $0 == __FILE__
92
+ h = {}
93
+ case ARGV.size
94
+ when 0
95
+ puts "gimme fname [offset] [size]"
96
+ exit
97
+ when 1
98
+ fname = ARGV[0]
99
+ when 2
100
+ fname = ARGV[0]
101
+ h[:offset] = ARGV[1].to_i
102
+ when 3
103
+ fname = ARGV[0]
104
+ h[:offset] = ARGV[1].to_i
105
+ h[:size] = ARGV[2].to_i
106
+ end
107
+ File.open(fname,"rb") do |f|
108
+ f.seek h[:offset] if h[:offset]
109
+ @data = f.read(h[:size])
110
+ end
111
+ puts ZHexdump.dump(@data)
112
+ end
113
+
@@ -0,0 +1,3 @@
1
+ module ZHexdump
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ describe ZHexdump do
5
+ it "dumps to string" do
6
+ s = ''
7
+ ZHexdump.dump "foo", :output => s
8
+ s.should == "00000000: 66 6f 6f |foo |\n"
9
+ end
10
+
11
+ it "dumps to stdout" do
12
+ io = StringIO.new
13
+ begin
14
+ saved_stdout, $> = $>, io
15
+ ZHexdump.dump "foo"
16
+ ensure
17
+ $> = saved_stdout
18
+ end
19
+
20
+ io.rewind
21
+ io.read.should == "00000000: 66 6f 6f |foo |\n"
22
+ end
23
+
24
+ it "dumps w/o addr if show_addr=false" do
25
+ s = ''
26
+ ZHexdump.dump "foo", :output => s, :show_addr => false
27
+ s.should == "66 6f 6f |foo |\n"
28
+ end
29
+
30
+ it "dumps w/o addr if show_offset=false" do
31
+ s = ''
32
+ ZHexdump.dump "foo", :output => s, :show_offset => false
33
+ s.should == "66 6f 6f |foo |\n"
34
+ end
35
+
36
+ it "dumps w/o addr if offset=false" do
37
+ s = ''
38
+ ZHexdump.dump "foo", :output => s, :offset => false
39
+ s.should == "66 6f 6f |foo |\n"
40
+ end
41
+
42
+ it "respects :size" do
43
+ data = 'foo'*100
44
+ s = ''
45
+ ZHexdump.dump data, :output => s, :size => 3
46
+ s.should == "00000000: 66 6f 6f |foo |\n"
47
+ end
48
+
49
+ it "respects :offset" do
50
+ data = 'foobar'*100
51
+ s = ''
52
+ ZHexdump.dump data, :output => s, :offset => 2, :size => 3
53
+ s.should == "00000002: 6f 62 61 |oba |\n"
54
+ end
55
+
56
+ it "adds :add to offset shown" do
57
+ data = 'foo'*100
58
+ s = ''
59
+ ZHexdump.dump data, :output => s, :size => 3, :add => 0x1234
60
+ s.should == "00001234: 66 6f 6f |foo |\n"
61
+ end
62
+
63
+ it "shows tail" do
64
+ data = 'foo'
65
+ s = ''
66
+ ZHexdump.dump data, :output => s, :tail => 'tail'
67
+ s.should == "00000000: 66 6f 6f |foo |tail"
68
+ end
69
+
70
+ it "shows tail only on last line" do
71
+ data = 'foo'*6
72
+ s = ''
73
+ ZHexdump.dump data, :output => s, :tail => 'tail'
74
+ s.should == <<-EOF.split("\n").map(&:strip).join("\n")
75
+ 00000000: 66 6f 6f 66 6f 6f 66 6f 6f 66 6f 6f 66 6f 6f 66 |foofoofoofoofoof|
76
+ 00000010: 6f 6f |oo |tail
77
+ EOF
78
+ end
79
+
80
+ it "no dedup: should have exactly 0x10 lines" do
81
+ s = ''
82
+ ZHexdump.dump "x"*0x100, :output => s, :dedup => false
83
+ s.count("\n").should == 0x10
84
+ end
85
+
86
+ it "default dedup: should have exactly 2 lines" do
87
+ s = ''
88
+ ZHexdump.dump "x"*0x100, :output => s
89
+ s.count("\n").should == 3
90
+ s.strip.should == <<-EOF.split("\n").map(&:strip).join("\n")
91
+ 00000000: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 |xxxxxxxxxxxxxxxx|
92
+ *
93
+ 00000100:
94
+ EOF
95
+ end
96
+
97
+ it "column width=4, no offset, each row prepend" do
98
+ data = "ABCDEFGHIJKLMNOPQRST"
99
+ s = ''
100
+ ZHexdump.dump(data, :output => s, :width => 4, :show_offset => false) do |row, offset|
101
+ row.insert(0,"color %4s: " % "##{(offset/4)}")
102
+ end
103
+ s.should == <<-EOF.split("\n").map(&:strip).join("\n") + "\n"
104
+ color #0: 41 42 43 44 |ABCD|
105
+ color #1: 45 46 47 48 |EFGH|
106
+ color #2: 49 4a 4b 4c |IJKL|
107
+ color #3: 4d 4e 4f 50 |MNOP|
108
+ color #4: 51 52 53 54 |QRST|
109
+ EOF
110
+ end
111
+
112
+ it "column width=3, no offset, each row prepend" do
113
+ data = "ABCDEFGHI"
114
+ s = ''
115
+ ZHexdump.dump(data, :output => s, :width => 3, :show_offset => false) do |row, offset|
116
+ row.insert(0,"color %4s: " % "##{(offset/3)}")
117
+ end
118
+ s.should == <<-EOF.split("\n").map(&:strip).join("\n") + "\n"
119
+ color #0: 41 42 43 |ABC|
120
+ color #1: 44 45 46 |DEF|
121
+ color #2: 47 48 49 |GHI|
122
+ EOF
123
+ end
124
+
125
+ it "prepends spaces before offset" do
126
+ data = 'foo'
127
+ s = ''
128
+ ZHexdump.dump(data, :output => s) do |x|
129
+ x.insert(0," ")
130
+ end
131
+ s.should == " 00000000: 66 6f 6f |foo |\n"
132
+ end
133
+
134
+ it "modifies ascii" do
135
+ data = 'foo'
136
+ s = ''
137
+ ZHexdump.dump(data, :output => s) do |row, offset, ascii|
138
+ ascii.sub! 'foo', 'xxx'
139
+ end
140
+ s.should == "00000000: 66 6f 6f |xxx |\n"
141
+ end
142
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/zhexdump.rb')
2
+
3
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ #config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ #config.order = 'random'
14
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zhexdump/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "zhexdump"
8
+ gem.version = ZHexdump::VERSION
9
+ gem.authors = ["Andrey \"Zed\" Zaikin"]
10
+ gem.email = ["zed.0xff@gmail.com"]
11
+ gem.summary = %q{A highly flexible hexdump implementation.}
12
+ gem.description = gem.summary
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zhexdump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrey "Zed" Zaikin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A highly flexible hexdump implementation.
31
+ email:
32
+ - zed.0xff@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - README.md.tpl
43
+ - Rakefile
44
+ - lib/zhexdump.rb
45
+ - lib/zhexdump/version.rb
46
+ - spec/hexdump_spec.rb
47
+ - spec/spec_helper.rb
48
+ - zhexdump.gemspec
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A highly flexible hexdump implementation.
73
+ test_files:
74
+ - spec/hexdump_spec.rb
75
+ - spec/spec_helper.rb