hexdump 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +3 -5
- data/ChangeLog.md +11 -0
- data/LICENSE.txt +1 -1
- data/README.md +99 -78
- data/benchmark.rb +47 -0
- data/gemspec.yml +8 -2
- data/hexdump.gemspec +1 -0
- data/lib/hexdump.rb +1 -1
- data/lib/hexdump/core_ext.rb +4 -0
- data/lib/hexdump/{extensions → core_ext}/file.rb +0 -0
- data/lib/hexdump/{extensions → core_ext}/io.rb +0 -0
- data/lib/hexdump/{extensions → core_ext}/string.rb +0 -0
- data/lib/hexdump/{extensions → core_ext}/string_io.rb +0 -0
- data/lib/hexdump/dumper.rb +61 -91
- data/lib/hexdump/extensions.rb +2 -4
- data/lib/hexdump/hexdump.rb +12 -21
- data/lib/hexdump/version.rb +1 -1
- data/spec/{extensions_spec.rb → core_ext_spec.rb} +2 -2
- data/spec/dumper_spec.rb +176 -107
- data/spec/hexdump_spec.rb +0 -1
- metadata +17 -12
- data/benchmarks/hexdump.rb +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc17566453285686c384ffb48945dfa526b3d4ea24a3edec8b193540ec40fead
|
4
|
+
data.tar.gz: cfd9d433a9d11d85b43cd98a46e4b8c1aa67dcc572bd0ebfd7771469bbc315ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcc397a847ff0d4b9cfef884d6c8c97ed344cbded5242cfeccd2e6e61dc755a4dfb3ac20ddfc65d66f1428dce3ba36ab46858c9c2fdb2e43905b73c4f39e0c10
|
7
|
+
data.tar.gz: 3917cbf496621c7ddd8e9aec840be04b1836145f082fa0eefe016438cba570cda39013becd7a07fefdb8e4a0c1e9a13b17690f238da468ed528792c043e1c1f4
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
name: CI
|
2
2
|
|
3
|
-
on:
|
4
|
-
push:
|
5
|
-
branches: [ master ]
|
6
|
-
pull_request:
|
7
|
-
branches: ['**']
|
3
|
+
on: [ push, pull_request ]
|
8
4
|
|
9
5
|
jobs:
|
10
6
|
tests:
|
@@ -17,7 +13,9 @@ jobs:
|
|
17
13
|
- 2.5
|
18
14
|
- 2.6
|
19
15
|
- 2.7
|
16
|
+
- 3.0
|
20
17
|
- jruby
|
18
|
+
- truffleruby
|
21
19
|
name: Ruby ${{ matrix.ruby }}
|
22
20
|
steps:
|
23
21
|
- uses: actions/checkout@v2
|
data/ChangeLog.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
### 0.3.0 / 2021-04-10
|
2
|
+
|
3
|
+
* Require Ruby >= 2.0.0.
|
4
|
+
* Added support for Ruby 3.0.
|
5
|
+
* {Hexdump.dump}, {Hexdump#dump}, and {Hexdump::Dumper#initialize} now accept
|
6
|
+
keyword arguments.
|
7
|
+
* {Hexdump::Dumper#each} now returns the total number of bytes read.
|
8
|
+
* {Hexdump::Dumper#dump} now prints the final number of bytes read on the last
|
9
|
+
line.
|
10
|
+
* Micro-optimizations to improve performance on JRuby and TruffleRuby.
|
11
|
+
|
1
12
|
### 0.2.4 / 2020-09-26
|
2
13
|
|
3
14
|
* Default the `:output` option of {Hexdump.dump} to `$stdout` instead of
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
# hexdump
|
1
|
+
# hexdump.rb
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
[![CI](https://github.com/postmodern/hexdump.rb/actions/workflows/ruby.yml/badge.svg)](https://github.com/postmodern/hexdump.rb/actions/workflows/ruby.yml)
|
4
|
+
|
5
|
+
* [Source](https://github.com/postmodern/hexdump.rb)
|
6
|
+
* [Issues](https://github.com/postmodern/hexdump.rb/issues)
|
5
7
|
* [Documentation](http://rubydoc.info/gems/hexdump/frames)
|
6
8
|
* [Email](mailto:postmodern.mod3 at gmail.com)
|
7
9
|
|
@@ -15,8 +17,9 @@ Simple and Fast hexdumping for Ruby.
|
|
15
17
|
* Can send the hexdump output to any Object supporting the `<<` method.
|
16
18
|
* Can yield each line of hexdump, instead of printing the output.
|
17
19
|
* Supports printing ASCII, hexadecimal, decimal, octal and binary bytes.
|
18
|
-
* Supports hexdumping
|
19
|
-
|
20
|
+
* Supports hexdumping bytes (8bit), words (16bit), double-words (32bit), and
|
21
|
+
quad-words (64bit).
|
22
|
+
* Supports Little Endian and Big Endian modes.
|
20
23
|
* Makes {String}, {StringIO}, {IO}, {File} objects hexdumpable.
|
21
24
|
* Fast-ish.
|
22
25
|
|
@@ -28,9 +31,11 @@ Simple and Fast hexdumping for Ruby.
|
|
28
31
|
|
29
32
|
Hexdump.dump(data)
|
30
33
|
# 00000000 68 65 6c 6c 6f 00 |hello.|
|
34
|
+
# 00000006
|
31
35
|
|
32
36
|
data.hexdump
|
33
37
|
# 00000000 68 65 6c 6c 6f 00 |hello.|
|
38
|
+
# 00000006
|
34
39
|
|
35
40
|
File.open('dump.txt','w') do |file|
|
36
41
|
data.hexdump(:output => file)
|
@@ -42,31 +47,39 @@ Simple and Fast hexdumping for Ruby.
|
|
42
47
|
hex # => ["68", "65", "6c", "6c", "6f", "00"]
|
43
48
|
printable # => ["h", "e", "l", "l", "o", "."]
|
44
49
|
end
|
50
|
+
# => 6
|
45
51
|
|
46
52
|
# configure the width of the hexdump
|
47
|
-
Hexdump.dump('A' * 30, :
|
53
|
+
Hexdump.dump('A' * 30, width: 10)
|
48
54
|
# 00000000 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
|
49
55
|
# 0000000a 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
|
50
56
|
# 00000014 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
|
57
|
+
# 0000001e
|
51
58
|
|
52
|
-
Hexdump.dump(data, :
|
59
|
+
Hexdump.dump(data, ascii: true)
|
53
60
|
# 00000000 h e l l o 00 |hello.|
|
61
|
+
# 00000006
|
54
62
|
|
55
|
-
Hexdump.dump(data, :
|
63
|
+
Hexdump.dump(data, base: 16)
|
56
64
|
# 00000000 68 65 6c 6c 6f 00 |hello.|
|
65
|
+
# 00000006
|
57
66
|
|
58
|
-
Hexdump.dump(data, :
|
67
|
+
Hexdump.dump(data, base: :decimal)
|
59
68
|
# 00000000 104 101 108 108 111 0 |hello.|
|
69
|
+
# 00000006
|
60
70
|
|
61
|
-
Hexdump.dump(data, :
|
71
|
+
Hexdump.dump(data, base: :octal)
|
62
72
|
# 00000000 0150 0145 0154 0154 0157 0000 |hello.|
|
73
|
+
# 00000006
|
63
74
|
|
64
|
-
Hexdump.dump(data, :
|
75
|
+
Hexdump.dump(data, base: :binary)
|
65
76
|
# 00000000 01101000 01100101 01101100 01101100 01101111 00000000 |hello.|
|
77
|
+
# 00000006
|
66
78
|
|
67
|
-
("ABC" * 10).hexdump(:
|
79
|
+
("ABC" * 10).hexdump(word_size: 2)
|
68
80
|
# 00000000 4241 4143 4342 4241 4143 4342 4241 4143 |䉁䅃䍂䉁䅃䍂䉁䅃|
|
69
81
|
# 00000010 4342 4241 4143 4342 4241 4143 4342 |䍂䉁䅃䍂䉁䅃䍂|
|
82
|
+
# 0000001e
|
70
83
|
|
71
84
|
## Install
|
72
85
|
|
@@ -74,74 +87,82 @@ Simple and Fast hexdumping for Ruby.
|
|
74
87
|
|
75
88
|
## Benchmarks
|
76
89
|
|
77
|
-
Benchmarks show {Hexdump.dump} processing
|
78
|
-
|
79
|
-
### Ruby
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
90
|
+
Benchmarks show {Hexdump.dump} processing 25M of data.
|
91
|
+
|
92
|
+
### Ruby 2.7.3
|
93
|
+
|
94
|
+
```
|
95
|
+
user system total real
|
96
|
+
Hexdump.dump (output) 10.283433 0.000748 10.284181 ( 10.328899)
|
97
|
+
Hexdump.dump width=256 (output) 8.803228 0.005973 8.809201 ( 8.838375)
|
98
|
+
Hexdump.dump ascii=true (output) 10.740975 0.001903 10.742878 ( 10.779777)
|
99
|
+
Hexdump.dump word_size=2 (output) 15.163195 0.000989 15.164184 ( 15.220481)
|
100
|
+
Hexdump.dump word_size=4 (output) 14.279406 0.003840 14.283246 ( 14.345357)
|
101
|
+
Hexdump.dump word_size=8 (output) 7.715803 0.002879 7.718682 ( 7.746389)
|
102
|
+
Hexdump.dump (block) 5.543268 0.000980 5.544248 ( 5.561494)
|
103
|
+
Hexdump.dump width=256 (block) 5.438946 0.000000 5.438946 ( 5.455742)
|
104
|
+
Hexdump.dump ascii=true (block) 6.082787 0.000924 6.083711 ( 6.106234)
|
105
|
+
Hexdump.dump word_size=2 (block) 11.439610 0.000983 11.440593 ( 11.483788)
|
106
|
+
Hexdump.dump word_size=4 (block) 11.111633 0.000954 11.112587 ( 11.158416)
|
107
|
+
Hexdump.dump word_size=8 (block) 5.397569 0.002896 5.400465 ( 5.426971)
|
108
|
+
```
|
109
|
+
|
110
|
+
### Ruby 3.0.1
|
111
|
+
|
112
|
+
```
|
113
|
+
user system total real
|
114
|
+
Hexdump.dump (output) 12.064022 0.001165 12.065187 ( 12.118272)
|
115
|
+
Hexdump.dump width=256 (output) 10.228743 0.009920 10.238663 ( 10.279783)
|
116
|
+
Hexdump.dump ascii=true (output) 12.532913 0.000000 12.532913 ( 12.582665)
|
117
|
+
Hexdump.dump word_size=2 (output) 17.685782 0.000000 17.685782 ( 17.770686)
|
118
|
+
Hexdump.dump word_size=4 (output) 15.835564 0.000000 15.835564 ( 15.917552)
|
119
|
+
Hexdump.dump word_size=8 (output) 8.436831 0.000000 8.436831 ( 8.473445)
|
120
|
+
Hexdump.dump (block) 6.482589 0.000000 6.482589 ( 6.504816)
|
121
|
+
Hexdump.dump width=256 (block) 6.360828 0.000000 6.360828 ( 6.383705)
|
122
|
+
Hexdump.dump ascii=true (block) 6.911868 0.000000 6.911868 ( 6.936795)
|
123
|
+
Hexdump.dump word_size=2 (block) 13.120488 0.000000 13.120488 ( 13.179957)
|
124
|
+
Hexdump.dump word_size=4 (block) 12.349516 0.000000 12.349516 ( 12.412972)
|
125
|
+
Hexdump.dump word_size=8 (block) 5.814830 0.000000 5.814830 ( 5.837822)
|
126
|
+
```
|
127
|
+
|
128
|
+
### JRuby 9.2.16.0
|
129
|
+
|
130
|
+
```
|
131
|
+
user system total real
|
132
|
+
Hexdump.dump (output) 13.090000 0.240000 13.330000 ( 11.226466)
|
133
|
+
Hexdump.dump width=256 (output) 9.350000 0.030000 9.380000 ( 9.165070)
|
134
|
+
Hexdump.dump ascii=true (output) 10.910000 0.050000 10.960000 ( 10.665791)
|
135
|
+
Hexdump.dump word_size=2 (output) 13.760000 0.150000 13.910000 ( 12.268307)
|
136
|
+
Hexdump.dump word_size=4 (output) 11.940000 0.090000 12.030000 ( 11.107564)
|
137
|
+
Hexdump.dump word_size=8 (output) 8.170000 0.040000 8.210000 ( 7.419708)
|
138
|
+
Hexdump.dump (block) 7.840000 0.020000 7.860000 ( 7.777749)
|
139
|
+
Hexdump.dump width=256 (block) 7.540000 0.000000 7.540000 ( 7.466315)
|
140
|
+
Hexdump.dump ascii=true (block) 7.680000 0.010000 7.690000 ( 7.622393)
|
141
|
+
Hexdump.dump word_size=2 (block) 9.830000 0.020000 9.850000 ( 9.693596)
|
142
|
+
Hexdump.dump word_size=4 (block) 9.010000 0.020000 9.030000 ( 8.998687)
|
143
|
+
Hexdump.dump word_size=8 (block) 5.740000 0.030000 5.770000 ( 5.709127)
|
144
|
+
```
|
145
|
+
|
146
|
+
### TruffleRuby 21.0.0
|
147
|
+
|
148
|
+
```
|
149
|
+
user system total real
|
150
|
+
Hexdump.dump (output) 25.818995 0.855689 26.674684 ( 22.376015)
|
151
|
+
Hexdump.dump width=256 (output) 20.489077 0.125966 20.615043 ( 18.301748)
|
152
|
+
Hexdump.dump ascii=true (output) 25.214678 0.098018 25.312696 ( 21.714985)
|
153
|
+
Hexdump.dump word_size=2 (output) 28.380387 0.192277 28.572664 ( 23.736887)
|
154
|
+
Hexdump.dump word_size=4 (output) 31.348977 0.134854 31.483831 ( 27.710968)
|
155
|
+
Hexdump.dump word_size=8 (output) 18.850093 0.100256 18.950349 ( 13.921720)
|
156
|
+
Hexdump.dump (block) 7.792878 0.050542 7.843420 ( 6.003789)
|
157
|
+
Hexdump.dump width=256 (block) 6.526531 0.015898 6.542429 ( 5.777169)
|
158
|
+
Hexdump.dump ascii=true (block) 7.425399 0.030799 7.456198 ( 5.705369)
|
159
|
+
Hexdump.dump word_size=2 (block) 12.629775 0.028653 12.658428 ( 11.115049)
|
160
|
+
Hexdump.dump word_size=4 (block) 20.372094 0.010807 20.382901 ( 19.758073)
|
161
|
+
Hexdump.dump word_size=8 (block) 8.828653 0.010889 8.839542 ( 8.017241)
|
162
|
+
```
|
142
163
|
|
143
164
|
## Copyright
|
144
165
|
|
145
|
-
Copyright (c) 2011-
|
166
|
+
Copyright (c) 2011-2021 Hal Brodigan
|
146
167
|
|
147
168
|
See {file:LICENSE.txt} for details.
|
data/benchmark.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.expand_path('../lib',__FILE__))
|
4
|
+
|
5
|
+
require 'hexdump'
|
6
|
+
require 'benchmark'
|
7
|
+
|
8
|
+
DATA = ((0..255).map { |b| b.chr }.join) * (1024 * 100)
|
9
|
+
OUTPUT = Class.new { def <<(data); end }.new
|
10
|
+
|
11
|
+
Benchmark.bm(33) do |b|
|
12
|
+
b.report('Hexdump.dump (output)') do
|
13
|
+
Hexdump.dump(DATA, :output => OUTPUT)
|
14
|
+
end
|
15
|
+
|
16
|
+
b.report('Hexdump.dump width=256 (output)') do
|
17
|
+
Hexdump.dump(DATA, width: 256, output: OUTPUT)
|
18
|
+
end
|
19
|
+
|
20
|
+
b.report('Hexdump.dump ascii=true (output)') do
|
21
|
+
Hexdump.dump(DATA, ascii: true, output: OUTPUT)
|
22
|
+
end
|
23
|
+
|
24
|
+
[2, 4, 8].each do |word_size|
|
25
|
+
b.report("Hexdump.dump word_size=#{word_size} (output)") do
|
26
|
+
Hexdump.dump(DATA, word_size: word_size, output: OUTPUT)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
b.report('Hexdump.dump (block)') do
|
31
|
+
Hexdump.dump(DATA) { |index,hex,print| }
|
32
|
+
end
|
33
|
+
|
34
|
+
b.report('Hexdump.dump width=256 (block)') do
|
35
|
+
Hexdump.dump(DATA, width: 256) { |index,hex,print| }
|
36
|
+
end
|
37
|
+
|
38
|
+
b.report('Hexdump.dump ascii=true (block)') do
|
39
|
+
Hexdump.dump(DATA, ascii: true) { |index,hex,print| }
|
40
|
+
end
|
41
|
+
|
42
|
+
[2, 4, 8].each do |word_size|
|
43
|
+
b.report("Hexdump.dump word_size=#{word_size} (block)") do
|
44
|
+
Hexdump.dump(DATA, word_size: word_size) { |index,hex,print| }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/gemspec.yml
CHANGED
@@ -4,10 +4,16 @@ description: Simple and Fast hexdumping for Ruby.
|
|
4
4
|
license: MIT
|
5
5
|
authors: Postmodern
|
6
6
|
email: postmodern.mod3@gmail.com
|
7
|
-
homepage: https://github.com/postmodern/hexdump#readme
|
7
|
+
homepage: https://github.com/postmodern/hexdump.rb#readme
|
8
8
|
has_yard: true
|
9
9
|
|
10
|
-
|
10
|
+
metadata:
|
11
|
+
documentation_uri: https://rubydoc.info/gems/hexdump
|
12
|
+
source_code_uri: https://github.com/postmodern/hexdump.rb
|
13
|
+
bug_tracker_uri: https://github.com/postmodern/hexdump.rb/issues
|
14
|
+
changelog_uri: https://github.com/postmodern/hexdump.rb/blob/master/ChangeLog.md
|
15
|
+
|
16
|
+
required_ruby_version: ">= 2.0.0"
|
11
17
|
|
12
18
|
development_dependencies:
|
13
19
|
bundler: ~> 2.0
|
data/hexdump.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.authors = Array(gemspec['authors'])
|
21
21
|
gem.email = gemspec['email']
|
22
22
|
gem.homepage = gemspec['homepage']
|
23
|
+
gem.metadata = gemspec['metadata'] if gemspec['metadata']
|
23
24
|
|
24
25
|
glob = lambda { |patterns| gem.files & Dir[*patterns] }
|
25
26
|
|
data/lib/hexdump.rb
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/lib/hexdump/dumper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hexdump
|
2
4
|
#
|
3
5
|
# Handles the parsing of data and formatting of the hexdump.
|
@@ -8,28 +10,28 @@ module Hexdump
|
|
8
10
|
|
9
11
|
# Widths for formatted numbers
|
10
12
|
WIDTHS = {
|
11
|
-
:
|
12
|
-
:
|
13
|
+
hexadecimal: ->(word_size) { word_size * 2 },
|
14
|
+
decimal: {
|
13
15
|
1 => 3,
|
14
16
|
2 => 5,
|
15
17
|
4 => 10,
|
16
18
|
8 => 20
|
17
19
|
},
|
18
|
-
:
|
20
|
+
octal: {
|
19
21
|
1 => 3,
|
20
22
|
2 => 6,
|
21
23
|
4 => 11,
|
22
24
|
8 => 22
|
23
25
|
},
|
24
|
-
:
|
26
|
+
binary: ->(word_size) { word_size * 8 }
|
25
27
|
}
|
26
28
|
|
27
29
|
# Format Strings for the various bases
|
28
30
|
FORMATS = {
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
31
|
+
hexadecimal: ->(width) { "%.#{width}x" },
|
32
|
+
decimal: ->(width) { "%#{width}.d" },
|
33
|
+
octal: ->(width) { "%.#{width}o" },
|
34
|
+
binary: ->(width) { "%.#{width}b" }
|
33
35
|
}
|
34
36
|
|
35
37
|
# Character to represent unprintable characters
|
@@ -154,25 +156,22 @@ module Hexdump
|
|
154
156
|
#
|
155
157
|
# Creates a new Hexdump dumper.
|
156
158
|
#
|
157
|
-
# @param [
|
158
|
-
# Additional options.
|
159
|
-
#
|
160
|
-
# @option options [Integer] :width (16)
|
159
|
+
# @param [Integer] width (16)
|
161
160
|
# The number of bytes to dump for each line.
|
162
161
|
#
|
163
|
-
# @
|
162
|
+
# @param [Integer] endian (:little)
|
164
163
|
# The endianness that the bytes are organized in. Supported endianness
|
165
164
|
# include `:little` and `:big`.
|
166
165
|
#
|
167
|
-
# @
|
166
|
+
# @param [Integer] word_size (1)
|
168
167
|
# The number of bytes within a word.
|
169
168
|
#
|
170
|
-
# @
|
169
|
+
# @param [Symbol, Integer] base (:hexadecimal)
|
171
170
|
# The base to print bytes in. Supported bases include, `:hexadecimal`,
|
172
171
|
# `:hex`, `16, `:decimal`, `:dec`, `10, `:octal`, `:oct`, `8`,
|
173
172
|
# `:binary`, `:bin` and `2`.
|
174
173
|
#
|
175
|
-
# @
|
174
|
+
# @param [Boolean] ascii (false)
|
176
175
|
# Print ascii characters when possible.
|
177
176
|
#
|
178
177
|
# @raise [ArgumentError]
|
@@ -180,36 +179,33 @@ module Hexdump
|
|
180
179
|
#
|
181
180
|
# @since 0.2.0
|
182
181
|
#
|
183
|
-
def initialize(
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
when :
|
192
|
-
|
193
|
-
when
|
194
|
-
|
182
|
+
def initialize(width: 16,
|
183
|
+
endian: :little,
|
184
|
+
word_size: 1,
|
185
|
+
base: :hexadecimal,
|
186
|
+
ascii: false)
|
187
|
+
|
188
|
+
@base = case base
|
189
|
+
when :hexadecimal, :hex, 16 then :hexadecimal
|
190
|
+
when :decimal, :dec, 10 then :decimal
|
191
|
+
when :octal, :oct, 8 then :octal
|
192
|
+
when :binary, :bin, 2 then :binary
|
193
|
+
when nil then :hexadecimal
|
195
194
|
else
|
196
|
-
raise(ArgumentError,"unknown base #{
|
195
|
+
raise(ArgumentError,"unknown base #{base.inspect}")
|
197
196
|
end
|
198
197
|
|
199
|
-
@word_size =
|
200
|
-
@endian = case
|
201
|
-
when 'little', :little
|
202
|
-
|
203
|
-
when
|
204
|
-
:big
|
205
|
-
when nil
|
206
|
-
:little
|
198
|
+
@word_size = word_size
|
199
|
+
@endian = case endian
|
200
|
+
when 'little', :little then :little
|
201
|
+
when 'big', :big then :big
|
202
|
+
when nil then :little
|
207
203
|
else
|
208
|
-
raise(ArgumentError,"unknown endian: #{
|
204
|
+
raise(ArgumentError,"unknown endian: #{endian.inspect}")
|
209
205
|
end
|
210
206
|
|
211
|
-
@width = (
|
212
|
-
@ascii =
|
207
|
+
@width = (width / @word_size)
|
208
|
+
@ascii = ascii
|
213
209
|
|
214
210
|
@format_width = (WIDTHS[@base][@word_size] || 1)
|
215
211
|
@format = FORMATS[@base][@format_width]
|
@@ -252,20 +248,16 @@ module Hexdump
|
|
252
248
|
word = 0
|
253
249
|
count = 0
|
254
250
|
|
255
|
-
init_shift = if @endian == :big
|
256
|
-
|
257
|
-
else
|
258
|
-
0
|
251
|
+
init_shift = if @endian == :big then ((@word_size - 1) * 8)
|
252
|
+
else 0
|
259
253
|
end
|
260
254
|
shift = init_shift
|
261
255
|
|
262
256
|
data.each_byte do |b|
|
263
257
|
word |= (b << shift)
|
264
258
|
|
265
|
-
if @endian == :big
|
266
|
-
|
267
|
-
else
|
268
|
-
shift += 8
|
259
|
+
if @endian == :big then shift -= 8
|
260
|
+
else shift += 8
|
269
261
|
end
|
270
262
|
|
271
263
|
count += 1
|
@@ -305,7 +297,8 @@ module Hexdump
|
|
305
297
|
# @yieldparam [Array<String>] printable
|
306
298
|
# The printable representation of the segment.
|
307
299
|
#
|
308
|
-
# @return [Enumerator]
|
300
|
+
# @return [Integer, Enumerator]
|
301
|
+
# If a block is given, then the final number of bytes read is returned.
|
309
302
|
# If no block is given, an Enumerator will be returned.
|
310
303
|
#
|
311
304
|
# @since 0.2.0
|
@@ -316,21 +309,18 @@ module Hexdump
|
|
316
309
|
index = 0
|
317
310
|
count = 0
|
318
311
|
|
319
|
-
numeric
|
320
|
-
printable =
|
312
|
+
numeric = Array.new(@width)
|
313
|
+
printable = Array.new(@width)
|
321
314
|
|
322
315
|
each_word(data) do |word|
|
323
|
-
numeric
|
324
|
-
printable
|
316
|
+
numeric[count] = format_numeric(word)
|
317
|
+
printable[count] = format_printable(word)
|
325
318
|
|
326
319
|
count += 1
|
327
320
|
|
328
321
|
if count >= @width
|
329
322
|
yield index, numeric, printable
|
330
323
|
|
331
|
-
numeric.clear
|
332
|
-
printable.clear
|
333
|
-
|
334
324
|
index += (@width * @word_size)
|
335
325
|
count = 0
|
336
326
|
end
|
@@ -338,8 +328,12 @@ module Hexdump
|
|
338
328
|
|
339
329
|
if count > 0
|
340
330
|
# yield the remaining data
|
341
|
-
yield index, numeric, printable
|
331
|
+
yield index, numeric[0,count], printable[0,count]
|
332
|
+
|
333
|
+
index += (count * @word_size)
|
342
334
|
end
|
335
|
+
|
336
|
+
return index
|
343
337
|
end
|
344
338
|
|
345
339
|
#
|
@@ -364,39 +358,17 @@ module Hexdump
|
|
364
358
|
end
|
365
359
|
|
366
360
|
bytes_segment_width = ((@width * @format_width) + @width)
|
367
|
-
|
368
|
-
|
369
|
-
index = 0
|
370
|
-
count = 0
|
371
|
-
|
372
|
-
numeric = ''
|
373
|
-
printable = ''
|
374
|
-
|
375
|
-
each_word(data) do |word|
|
376
|
-
numeric << format_numeric(word) << ' '
|
377
|
-
printable << format_printable(word)
|
361
|
+
index_format = "%.8x"
|
362
|
+
line_format = "#{index_format} %-#{bytes_segment_width}s |%s|#{$/}"
|
378
363
|
|
379
|
-
|
380
|
-
|
381
|
-
if count >= @width
|
382
|
-
output << sprintf(line_format,index,numeric,printable)
|
383
|
-
|
384
|
-
numeric = ''
|
385
|
-
printable = ''
|
386
|
-
|
387
|
-
index += (@width * @word_size)
|
388
|
-
count = 0
|
389
|
-
end
|
364
|
+
length = each(data) do |index,numeric,printable|
|
365
|
+
output << sprintf(line_format,index,numeric.join(' '),printable.join)
|
390
366
|
end
|
391
367
|
|
392
|
-
|
393
|
-
|
394
|
-
output << sprintf(line_format,index,numeric,printable)
|
395
|
-
end
|
368
|
+
output << sprintf("#{index_format}#{$/}",length)
|
369
|
+
return nil
|
396
370
|
end
|
397
371
|
|
398
|
-
protected
|
399
|
-
|
400
372
|
#
|
401
373
|
# Converts the word into a numeric String.
|
402
374
|
#
|
@@ -434,12 +406,10 @@ module Hexdump
|
|
434
406
|
def format_printable(word)
|
435
407
|
if @word_size == 1
|
436
408
|
PRINTABLE[word]
|
437
|
-
elsif
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
UNPRINTABLE
|
442
|
-
end
|
409
|
+
elsif word >= 0 && word <= 0x7fffffff
|
410
|
+
# XXX: https://github.com/jruby/jruby/issues/6652
|
411
|
+
char = word.chr(Encoding::UTF_8) rescue nil
|
412
|
+
char || UNPRINTABLE
|
443
413
|
else
|
444
414
|
UNPRINTABLE
|
445
415
|
end
|