hexdump 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.
- data/.document +3 -0
- data/.gemtest +0 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +10 -0
- data/LICENSE.txt +20 -0
- data/README.md +113 -0
- data/Rakefile +38 -0
- data/benchmarks/hexdump.rb +35 -0
- data/gemspec.yml +14 -0
- data/hexdump.gemspec +15 -0
- data/lib/hexdump.rb +2 -0
- data/lib/hexdump/extensions.rb +4 -0
- data/lib/hexdump/extensions/file.rb +22 -0
- data/lib/hexdump/extensions/io.rb +7 -0
- data/lib/hexdump/extensions/string.rb +7 -0
- data/lib/hexdump/extensions/string_io.rb +9 -0
- data/lib/hexdump/hexdump.rb +139 -0
- data/spec/extensions_spec.rb +20 -0
- data/spec/hexdump_spec.rb +171 -0
- data/spec/spec_helper.rb +2 -0
- metadata +112 -0
data/.document
ADDED
data/.gemtest
ADDED
|
File without changes
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--colour --format documentation
|
data/.yardopts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--markup markdown --title "hexdump Documentation" --protected
|
data/ChangeLog.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
### 0.1.0 / 2011-03-05
|
|
2
|
+
|
|
3
|
+
* Initial release:
|
|
4
|
+
* Can hexdump any Object supporting the `each_byte` method.
|
|
5
|
+
* Can send the hexdump output to any Object supporting the `<<` method.
|
|
6
|
+
* Can yield each line of hexdump, instead of printing the output.
|
|
7
|
+
* Supports printing hexadecimal, decimal, octal and binary bytes.
|
|
8
|
+
* Makes {String}, {StringIO}, {IO}, {File} objects hexdumpable.
|
|
9
|
+
* Fast-ish.
|
|
10
|
+
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2011 Hal Brodigan
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# hexdump
|
|
2
|
+
|
|
3
|
+
* [Homepage](http://github.com/postmoderm/hexdump)
|
|
4
|
+
* [Issues](http://github.com/postmoderm/hexdump/issues)
|
|
5
|
+
* [Documentation](http://rubydoc.info/gems/hexdump/frames)
|
|
6
|
+
* [Email](mailto:postmodern.mod3 at gmail.com)
|
|
7
|
+
|
|
8
|
+
## Description
|
|
9
|
+
|
|
10
|
+
Simple and Fast hexdumping for Ruby.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
* Can hexdump any Object supporting the `each_byte` method.
|
|
15
|
+
* Can send the hexdump output to any Object supporting the `<<` method.
|
|
16
|
+
* Can yield each line of hexdump, instead of printing the output.
|
|
17
|
+
* Supports printing ASCII, hexadecimal, decimal, octal and binary bytes.
|
|
18
|
+
* Makes {String}, {StringIO}, {IO}, {File} objects hexdumpable.
|
|
19
|
+
* Fast-ish.
|
|
20
|
+
|
|
21
|
+
## Benchmarks
|
|
22
|
+
|
|
23
|
+
Benchmarks show {Hexdump.dump} processing 2.4M of data.
|
|
24
|
+
|
|
25
|
+
### Ruby 1.9.2-p180
|
|
26
|
+
|
|
27
|
+
user system total real
|
|
28
|
+
hexdump (block) 7.740000 0.030000 7.770000 ( 8.138029)
|
|
29
|
+
hexdump 9.590000 0.050000 9.640000 ( 10.178203)
|
|
30
|
+
hexdump width=256 (block) 7.280000 0.020000 7.300000 ( 7.534507)
|
|
31
|
+
hexdump width=256 8.130000 0.030000 8.160000 ( 8.342448)
|
|
32
|
+
hexdump ascii=true (block) 7.740000 0.030000 7.770000 ( 7.958550)
|
|
33
|
+
hexdump ascii=true 9.550000 0.050000 9.600000 ( 9.803758)
|
|
34
|
+
|
|
35
|
+
### Ruby 1.8.7-p334
|
|
36
|
+
|
|
37
|
+
user system total real
|
|
38
|
+
hexdump (block) 10.520000 0.010000 10.530000 ( 10.692901)
|
|
39
|
+
hexdump 11.580000 0.010000 11.590000 ( 11.873978)
|
|
40
|
+
hexdump width=256 (block) 9.960000 0.110000 10.070000 ( 11.592033)
|
|
41
|
+
hexdump width=256 10.660000 0.010000 10.670000 ( 10.987417)
|
|
42
|
+
hexdump ascii=true (block) 10.620000 0.010000 10.630000 ( 10.899925)
|
|
43
|
+
hexdump ascii=true 11.590000 0.030000 11.620000 ( 12.765259)
|
|
44
|
+
|
|
45
|
+
### Jruby 1.5.6
|
|
46
|
+
|
|
47
|
+
user system total real
|
|
48
|
+
hexdump (block) 6.690000 0.000000 6.690000 ( 6.517000)
|
|
49
|
+
hexdump 8.234000 0.000000 8.234000 ( 8.234000)
|
|
50
|
+
hexdump width=256 (block) 4.488000 0.000000 4.488000 ( 4.488000)
|
|
51
|
+
hexdump width=256 5.462000 0.000000 5.462000 ( 5.462000)
|
|
52
|
+
hexdump ascii=true (block) 4.456000 0.000000 4.456000 ( 4.456000)
|
|
53
|
+
hexdump ascii=true 5.039000 0.000000 5.039000 ( 5.039000)
|
|
54
|
+
|
|
55
|
+
### Rubinius 1.2.3
|
|
56
|
+
|
|
57
|
+
user system total real
|
|
58
|
+
hexdump (block) 10.013478 0.018997 10.032475 ( 11.148450)
|
|
59
|
+
hexdump 13.153001 0.015997 13.168998 ( 13.740888)
|
|
60
|
+
hexdump width=256 (block) 8.845656 0.008999 8.854655 ( 9.022673)
|
|
61
|
+
hexdump width=256 9.894496 0.008999 9.903495 ( 10.121070)
|
|
62
|
+
hexdump ascii=true (block) 9.576544 0.021996 9.598540 ( 9.810846)
|
|
63
|
+
hexdump ascii=true 13.088011 0.015998 13.104009 ( 13.390532)
|
|
64
|
+
|
|
65
|
+
## Examples
|
|
66
|
+
|
|
67
|
+
require 'hexdump'
|
|
68
|
+
|
|
69
|
+
data = "hello\x00"
|
|
70
|
+
|
|
71
|
+
Hexdump.dump(data)
|
|
72
|
+
# 00000000 68 65 6c 6c 6f 00 |hello.|
|
|
73
|
+
|
|
74
|
+
data.hexdump
|
|
75
|
+
# 00000000 68 65 6c 6c 6f 00 |hello.|
|
|
76
|
+
|
|
77
|
+
# iterate over the hexdump lines
|
|
78
|
+
data.hexdump do |index,hex,printable|
|
|
79
|
+
index # => 0
|
|
80
|
+
hex # => ["68", "65", "6c", "6c", "6f", "00"]
|
|
81
|
+
printable # => ["h", "e", "l", "l", "o", "."]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# configure the width of the hexdump
|
|
85
|
+
Hexdump.dump('A' * 30, :width => 10)
|
|
86
|
+
# 00000000 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
|
|
87
|
+
# 0000000a 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
|
|
88
|
+
# 00000014 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
|
|
89
|
+
|
|
90
|
+
Hexdump.dump(data, :ascii => true)
|
|
91
|
+
# 00000000 h e l l o 00 |hello.|
|
|
92
|
+
|
|
93
|
+
Hexdump.dump(data, :base => 16)
|
|
94
|
+
# 00000000 68 65 6c 6c 6f 00 |hello.|
|
|
95
|
+
|
|
96
|
+
Hexdump.dump(data, :base => :decimal)
|
|
97
|
+
# 00000000 104 101 108 108 111 0 |hello.|
|
|
98
|
+
|
|
99
|
+
Hexdump.dump(data, :base => :octal)
|
|
100
|
+
# 00000000 0150 0145 0154 0154 0157 0000 |hello.|
|
|
101
|
+
|
|
102
|
+
Hexdump.dump(data, :base => :binary)
|
|
103
|
+
# 00000000 01101000 01100101 01101100 01101100 01101111 00000000 |hello.|
|
|
104
|
+
|
|
105
|
+
## Install
|
|
106
|
+
|
|
107
|
+
$ gem install hexdump
|
|
108
|
+
|
|
109
|
+
## Copyright
|
|
110
|
+
|
|
111
|
+
Copyright (c) 2011 Hal Brodigan
|
|
112
|
+
|
|
113
|
+
See {file:LICENSE.txt} for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
gem 'ore-tasks', '~> 0.4'
|
|
6
|
+
require 'ore/tasks'
|
|
7
|
+
|
|
8
|
+
Ore::Tasks.new
|
|
9
|
+
rescue LoadError => e
|
|
10
|
+
STDERR.puts e.message
|
|
11
|
+
STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
begin
|
|
15
|
+
gem 'rspec', '~> 2.4'
|
|
16
|
+
require 'rspec/core/rake_task'
|
|
17
|
+
|
|
18
|
+
RSpec::Core::RakeTask.new
|
|
19
|
+
rescue LoadError => e
|
|
20
|
+
task :spec do
|
|
21
|
+
abort "Please run `gem install rspec` to install RSpec."
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
task :test => :spec
|
|
26
|
+
task :default => :spec
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
gem 'yard', '~> 0.6.0'
|
|
30
|
+
require 'yard'
|
|
31
|
+
|
|
32
|
+
YARD::Rake::YardocTask.new
|
|
33
|
+
rescue LoadError => e
|
|
34
|
+
task :yard do
|
|
35
|
+
abort "Please run `gem install yard` to install YARD."
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
task :doc => :yard
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
|
|
4
|
+
|
|
5
|
+
require 'hexdump'
|
|
6
|
+
require 'benchmark'
|
|
7
|
+
|
|
8
|
+
DATA = ((0..255).map { |b| b.chr }.join) * 10000
|
|
9
|
+
OUTPUT = Class.new { def <<(data); end }.new
|
|
10
|
+
|
|
11
|
+
Benchmark.bm(26) do |b|
|
|
12
|
+
b.report('hexdump (block)') do
|
|
13
|
+
Hexdump.dump(DATA) { |index,hex,print| }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
b.report('hexdump') do
|
|
17
|
+
Hexdump.dump(DATA, :output => OUTPUT)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
b.report('hexdump width=256 (block)') do
|
|
21
|
+
Hexdump.dump(DATA, :width => 256) { |index,hex,print| }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
b.report('hexdump width=256') do
|
|
25
|
+
Hexdump.dump(DATA, :width => 256, :output => OUTPUT)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
b.report('hexdump ascii=true (block)') do
|
|
29
|
+
Hexdump.dump(DATA, :ascci => true) { |index,hex,print| }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
b.report('hexdump ascii=true') do
|
|
33
|
+
Hexdump.dump(DATA, :ascci => true, :output => OUTPUT)
|
|
34
|
+
end
|
|
35
|
+
end
|
data/gemspec.yml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
name: hexdump
|
|
2
|
+
version: 0.1.0
|
|
3
|
+
summary: Simple and Fast hexdumping for Ruby.
|
|
4
|
+
description: Simple and Fast hexdumping for Ruby.
|
|
5
|
+
license: MIT
|
|
6
|
+
authors: hal
|
|
7
|
+
email: postmodern.mod3@gmail.com
|
|
8
|
+
homepage: http://github.com/postmodern/hexdump
|
|
9
|
+
has_yard: true
|
|
10
|
+
|
|
11
|
+
development_dependencies:
|
|
12
|
+
ore-tasks: ~> 0.4
|
|
13
|
+
rspec: ~> 2.4
|
|
14
|
+
yard: ~> 0.6.0
|
data/hexdump.gemspec
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
begin
|
|
4
|
+
Ore::Specification.new do |gemspec|
|
|
5
|
+
# custom logic here
|
|
6
|
+
end
|
|
7
|
+
rescue NameError
|
|
8
|
+
begin
|
|
9
|
+
require 'ore/specification'
|
|
10
|
+
retry
|
|
11
|
+
rescue LoadError
|
|
12
|
+
STDERR.puts "The '#{__FILE__}' file requires Ore."
|
|
13
|
+
STDERR.puts "Run `gem install ore-core` to install Ore."
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/hexdump.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'hexdump/hexdump'
|
|
2
|
+
|
|
3
|
+
class File
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
# Hexdumps the contents of a file.
|
|
7
|
+
#
|
|
8
|
+
# @param [String] path
|
|
9
|
+
# The path of the file.
|
|
10
|
+
#
|
|
11
|
+
# @param [Hash] options
|
|
12
|
+
# Additional options.
|
|
13
|
+
#
|
|
14
|
+
# @see Hexdump.dump
|
|
15
|
+
#
|
|
16
|
+
def self.hexdump(path,options={},&block)
|
|
17
|
+
self.open(path,'rb') do |file|
|
|
18
|
+
Hexdump.dump(file,options,&block)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Provides the {Hexdump.dump} method and can add hexdumping to other classes
|
|
3
|
+
# by including the {Hexdump} module.
|
|
4
|
+
#
|
|
5
|
+
# class AbstractData
|
|
6
|
+
#
|
|
7
|
+
# include Hexdump
|
|
8
|
+
#
|
|
9
|
+
# def each_byte
|
|
10
|
+
# # ...
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# data = AbstractData.new
|
|
16
|
+
# data.hexdump
|
|
17
|
+
#
|
|
18
|
+
module Hexdump
|
|
19
|
+
#
|
|
20
|
+
# Hexdumps the given data.
|
|
21
|
+
#
|
|
22
|
+
# @param [#each_byte] data
|
|
23
|
+
# The data to be hexdumped.
|
|
24
|
+
#
|
|
25
|
+
# @param [Hash] options
|
|
26
|
+
# Additional options.
|
|
27
|
+
#
|
|
28
|
+
# @option options [Integer] :width (16)
|
|
29
|
+
# The number of bytes to dump for each line.
|
|
30
|
+
#
|
|
31
|
+
# @option options [Symbol, Integer] :base (:hexadecimal)
|
|
32
|
+
# The base to print bytes in. Supported bases include, `:hexadecimal`,
|
|
33
|
+
# `:hex`, `16, `:decimal`, `:dec`, `10, `:octal`, `:oct`, `8`,
|
|
34
|
+
# `:binary`, `:bin` and `2`.
|
|
35
|
+
#
|
|
36
|
+
# @option options [Boolean] :ascii (false)
|
|
37
|
+
# Print ascii characters when possible.
|
|
38
|
+
#
|
|
39
|
+
# @option options [#<<] :output (STDOUT)
|
|
40
|
+
# The output to print the hexdump to.
|
|
41
|
+
#
|
|
42
|
+
# @yield [index,hex_segment,print_segment]
|
|
43
|
+
# The given block will be passed the hexdump break-down of each segment.
|
|
44
|
+
#
|
|
45
|
+
# @yieldparam [Integer] index
|
|
46
|
+
# The index of the hexdumped segment.
|
|
47
|
+
#
|
|
48
|
+
# @yieldparam [Array<String>] hex_segment
|
|
49
|
+
# The hexadecimal-byte representation of the segment.
|
|
50
|
+
#
|
|
51
|
+
# @yieldparam [Array<String>] print_segment
|
|
52
|
+
# The print-character representation of the segment.
|
|
53
|
+
#
|
|
54
|
+
# @return [nil]
|
|
55
|
+
#
|
|
56
|
+
# @raise [ArgumentError]
|
|
57
|
+
# The given data does not define the `#each_byte` method, or
|
|
58
|
+
# the `:output` value does not support the `#<<` method.
|
|
59
|
+
#
|
|
60
|
+
def Hexdump.dump(data,options={})
|
|
61
|
+
unless data.respond_to?(:each_byte)
|
|
62
|
+
raise(ArgumentError,"the data to hexdump must define #each_byte")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
output = options.fetch(:output,STDOUT)
|
|
66
|
+
|
|
67
|
+
unless output.respond_to?(:<<)
|
|
68
|
+
raise(ArgumentError,":output must support the #<< method")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
width = options.fetch(:width,16)
|
|
72
|
+
base = options.fetch(:base,:hexadecimal)
|
|
73
|
+
ascii = options.fetch(:ascii,false)
|
|
74
|
+
byte_width, byte_format = case base
|
|
75
|
+
when :hexadecimal, :hex, 16
|
|
76
|
+
[2, "%.2x"]
|
|
77
|
+
when :decimal, :dec, 10
|
|
78
|
+
[3, "%3.d"]
|
|
79
|
+
when :octal, :oct, 8
|
|
80
|
+
[4, "0%.3o"]
|
|
81
|
+
when :binary, :bin, 2
|
|
82
|
+
[8, "%.8b"]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
hex_byte = lambda { |byte|
|
|
86
|
+
if (ascii && (byte >= 0x20 && byte <= 0x7e))
|
|
87
|
+
byte.chr
|
|
88
|
+
else
|
|
89
|
+
byte_format % byte
|
|
90
|
+
end
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
print_byte = lambda { |byte|
|
|
94
|
+
if (byte >= 0x20 && byte <= 0x7e)
|
|
95
|
+
byte.chr
|
|
96
|
+
else
|
|
97
|
+
'.'
|
|
98
|
+
end
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
index = 0
|
|
102
|
+
|
|
103
|
+
hex_segment_width = ((width * byte_width) + (width - 1))
|
|
104
|
+
line_format = "%.8x %-#{hex_segment_width}s |%s|\n"
|
|
105
|
+
|
|
106
|
+
data.each_byte.each_slice(width) do |bytes|
|
|
107
|
+
hex_segment = bytes.map(&hex_byte)
|
|
108
|
+
print_segment = bytes.map(&print_byte)
|
|
109
|
+
|
|
110
|
+
if block_given?
|
|
111
|
+
yield(index,hex_segment,print_segment)
|
|
112
|
+
else
|
|
113
|
+
output << sprintf(
|
|
114
|
+
line_format,
|
|
115
|
+
index,
|
|
116
|
+
hex_segment.join(' '),
|
|
117
|
+
print_segment.join
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
index += width
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# flush the hexdump buffer
|
|
125
|
+
return nil
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
#
|
|
129
|
+
# Hexdumps the object.
|
|
130
|
+
#
|
|
131
|
+
# @param [Hash] options
|
|
132
|
+
# Additional options.
|
|
133
|
+
#
|
|
134
|
+
# @see Hexdump.dump
|
|
135
|
+
#
|
|
136
|
+
def hexdump(options={},&block)
|
|
137
|
+
Hexdump.dump(self,options,&block)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'hexdump/extensions'
|
|
3
|
+
|
|
4
|
+
describe "Hexdump extensions" do
|
|
5
|
+
it "should include Hexdump into String" do
|
|
6
|
+
String.should include(Hexdump)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should include Hexdump into StringIO" do
|
|
10
|
+
StringIO.should include(Hexdump)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should include Hexdump into IO" do
|
|
14
|
+
IO.should include(Hexdump)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "should define File.hexdump" do
|
|
18
|
+
File.should respond_to(:hexdump)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'hexdump'
|
|
3
|
+
|
|
4
|
+
describe Hexdump do
|
|
5
|
+
let(:bytes) { [104, 101, 108, 108, 111] }
|
|
6
|
+
let(:hex_chars) { ['68', '65', '6c', '6c', '6f'] }
|
|
7
|
+
let(:decimal_chars) { ['104', '101', '108', '108', '111'] }
|
|
8
|
+
let(:octal_chars) { ['0150', '0145', '0154', '0154', '0157'] }
|
|
9
|
+
let(:binary_chars) { ['01101000', '01100101', '01101100', '01101100', '01101111'] }
|
|
10
|
+
let(:print_chars) { ['h', 'e', 'l', 'l', 'o'] }
|
|
11
|
+
let(:data) { print_chars.join }
|
|
12
|
+
|
|
13
|
+
describe "dump" do
|
|
14
|
+
it "should check if the data defines '#each_byte'" do
|
|
15
|
+
lambda {
|
|
16
|
+
subject.dump(Object.new)
|
|
17
|
+
}.should raise_error(ArgumentError)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should check if the :output supports the '#<<' method" do
|
|
21
|
+
lambda {
|
|
22
|
+
subject.dump(data, :output => Object.new)
|
|
23
|
+
}.should raise_error(ArgumentError)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should append each line of the hexdump to the output" do
|
|
27
|
+
lines = []
|
|
28
|
+
subject.dump(data, :output => lines)
|
|
29
|
+
|
|
30
|
+
lines.length.should == 1
|
|
31
|
+
lines[0].should include(hex_chars.join(' '))
|
|
32
|
+
lines[0].should include(print_chars.join)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "should yield the parts of each hexdump line to the given block" do
|
|
36
|
+
lines = []
|
|
37
|
+
|
|
38
|
+
subject.dump(data) do |index,hex,print|
|
|
39
|
+
lines << [index, hex, print]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
lines.length.should == 1
|
|
43
|
+
lines[0][0].should == 0
|
|
44
|
+
lines[0][1].should == hex_chars
|
|
45
|
+
lines[0][2].should == print_chars
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "should provide the index within the data for each line" do
|
|
49
|
+
indices = []
|
|
50
|
+
|
|
51
|
+
subject.dump('A' * 100, :width => 10) do |index,hex,print|
|
|
52
|
+
indices << index
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
indices.should == [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should allow configuring the width, in bytes, of each line" do
|
|
59
|
+
widths = []
|
|
60
|
+
|
|
61
|
+
subject.dump('A' * 100, :width => 10) do |index,hex,print|
|
|
62
|
+
widths << hex.length
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
widths.should == ([10] * 10)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "should hexdump the remaining bytes" do
|
|
69
|
+
chars = (['B'] * 4)
|
|
70
|
+
string = chars.join
|
|
71
|
+
leading = ('A' * 100)
|
|
72
|
+
remainder = nil
|
|
73
|
+
|
|
74
|
+
subject.dump(leading + string, :width => 10) do |index,hex,print|
|
|
75
|
+
remainder = print
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
remainder.should == chars
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should provide the hexadecimal characters for each line" do
|
|
82
|
+
chars = []
|
|
83
|
+
|
|
84
|
+
subject.dump(data * 100, :width => 10) do |index,hex,print|
|
|
85
|
+
chars += hex
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
chars.should == (hex_chars * 100)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "should allow printing ASCII characters in place of hex characters" do
|
|
92
|
+
chars = []
|
|
93
|
+
|
|
94
|
+
subject.dump(data, :ascii => true) do |index,hex,print|
|
|
95
|
+
chars += hex
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
chars.should == print_chars
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it "should provide the print characters for each line" do
|
|
102
|
+
chars = []
|
|
103
|
+
|
|
104
|
+
subject.dump(data * 100, :width => 10) do |index,hex,print|
|
|
105
|
+
chars += print
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
chars.should == (print_chars * 100)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
it "should map unprintable characters to '.'" do
|
|
112
|
+
unprintable = ((0x00..0x1f).map(&:chr) + (0x7f..0xff).map(&:chr)).join
|
|
113
|
+
chars = []
|
|
114
|
+
|
|
115
|
+
subject.dump(unprintable) do |index,hex,print|
|
|
116
|
+
chars += print
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
chars.should == (['.'] * unprintable.length)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "should support dumping bytes in decimal format" do
|
|
123
|
+
chars = []
|
|
124
|
+
|
|
125
|
+
subject.dump(data, :base => :decimal) do |index,hex,print|
|
|
126
|
+
chars += hex
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
chars.should == decimal_chars
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
it "should support dumping bytes in octal format" do
|
|
133
|
+
chars = []
|
|
134
|
+
|
|
135
|
+
subject.dump(data, :base => :octal) do |index,hex,print|
|
|
136
|
+
chars += hex
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
chars.should == octal_chars
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it "should support dumping bytes in binary format" do
|
|
143
|
+
chars = []
|
|
144
|
+
|
|
145
|
+
subject.dump(data, :base => :binary) do |index,hex,print|
|
|
146
|
+
chars += hex
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
chars.should == binary_chars
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
describe "#hexdump" do
|
|
154
|
+
subject do
|
|
155
|
+
obj = Object.new.extend(Hexdump)
|
|
156
|
+
obj.stub!(:each_byte).and_return(bytes.enum_for(:each))
|
|
157
|
+
|
|
158
|
+
obj
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
it "should hexdump the object" do
|
|
162
|
+
chars = []
|
|
163
|
+
|
|
164
|
+
subject.hexdump do |index,hex,print|
|
|
165
|
+
chars += hex
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
chars.should == hex_chars
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hexdump
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease:
|
|
5
|
+
version: 0.1.0
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- hal
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2011-03-05 00:00:00 -08:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies:
|
|
16
|
+
- !ruby/object:Gem::Dependency
|
|
17
|
+
name: ore-tasks
|
|
18
|
+
prerelease: false
|
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
20
|
+
none: false
|
|
21
|
+
requirements:
|
|
22
|
+
- - ~>
|
|
23
|
+
- !ruby/object:Gem::Version
|
|
24
|
+
version: "0.4"
|
|
25
|
+
type: :development
|
|
26
|
+
version_requirements: *id001
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
prerelease: false
|
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
31
|
+
none: false
|
|
32
|
+
requirements:
|
|
33
|
+
- - ~>
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: "2.4"
|
|
36
|
+
type: :development
|
|
37
|
+
version_requirements: *id002
|
|
38
|
+
- !ruby/object:Gem::Dependency
|
|
39
|
+
name: yard
|
|
40
|
+
prerelease: false
|
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ~>
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 0.6.0
|
|
47
|
+
type: :development
|
|
48
|
+
version_requirements: *id003
|
|
49
|
+
description: Simple and Fast hexdumping for Ruby.
|
|
50
|
+
email:
|
|
51
|
+
- postmodern.mod3@gmail.com
|
|
52
|
+
executables: []
|
|
53
|
+
|
|
54
|
+
extensions: []
|
|
55
|
+
|
|
56
|
+
extra_rdoc_files:
|
|
57
|
+
- README.md
|
|
58
|
+
- ChangeLog.md
|
|
59
|
+
- LICENSE.txt
|
|
60
|
+
files:
|
|
61
|
+
- .document
|
|
62
|
+
- .gemtest
|
|
63
|
+
- .rspec
|
|
64
|
+
- .yardopts
|
|
65
|
+
- ChangeLog.md
|
|
66
|
+
- LICENSE.txt
|
|
67
|
+
- README.md
|
|
68
|
+
- Rakefile
|
|
69
|
+
- benchmarks/hexdump.rb
|
|
70
|
+
- gemspec.yml
|
|
71
|
+
- hexdump.gemspec
|
|
72
|
+
- lib/hexdump.rb
|
|
73
|
+
- lib/hexdump/extensions.rb
|
|
74
|
+
- lib/hexdump/extensions/file.rb
|
|
75
|
+
- lib/hexdump/extensions/io.rb
|
|
76
|
+
- lib/hexdump/extensions/string.rb
|
|
77
|
+
- lib/hexdump/extensions/string_io.rb
|
|
78
|
+
- lib/hexdump/hexdump.rb
|
|
79
|
+
- spec/extensions_spec.rb
|
|
80
|
+
- spec/hexdump_spec.rb
|
|
81
|
+
- spec/spec_helper.rb
|
|
82
|
+
has_rdoc: yard
|
|
83
|
+
homepage: http://github.com/postmodern/hexdump
|
|
84
|
+
licenses:
|
|
85
|
+
- MIT
|
|
86
|
+
post_install_message:
|
|
87
|
+
rdoc_options: []
|
|
88
|
+
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
none: false
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: "0"
|
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
|
+
none: false
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: "0"
|
|
103
|
+
requirements: []
|
|
104
|
+
|
|
105
|
+
rubyforge_project: hexdump
|
|
106
|
+
rubygems_version: 1.5.2
|
|
107
|
+
signing_key:
|
|
108
|
+
specification_version: 3
|
|
109
|
+
summary: Simple and Fast hexdumping for Ruby.
|
|
110
|
+
test_files:
|
|
111
|
+
- spec/hexdump_spec.rb
|
|
112
|
+
- spec/extensions_spec.rb
|