1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 | #!/bin/bash /home/richmit/bin/ruby
# -*- Mode:Ruby; Coding:us-ascii-unix; fill-column:158 -*-
################################################################################################################################################################
##
# @file hexDump.rb
# @author Mitch Richling <https://www.mitchr.me/>
# @brief Hex dump files in a wide, color format on ANSI/VT100 terms.@EOL
# @keywords hex dump color ANSI VT100
# @std Ruby2.0
# @copyright
# @parblock
# Copyright (c) 1996-2016, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# @endparblock
################################################################################################################################################################
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
require 'optparse'
require 'optparse/time'
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
# Set defaults and process command line arguments
blkNchr = false
titleLines = 20
skipSame = true
maxBytes = 0
skipBytes = 0
doColor = true
bpf = "%02x"
blkPerLine = 4
bpfSpec = 0
bytePerBlk = 8
opts = OptionParser.new do |opts|
opts.banner = "Usage: hexDump.rb [options]"
opts.separator ""
opts.separator "Options:"
opts.on("-h", "--help", "Show this message") { puts opts; exit }
opts.on("-n INT", "Print only n bytes") { |v| maxBytes = v.to_i }
opts.on("-p INT", "Skip first n bytes") { |v| skipBytes = v.to_i }
opts.on("-w INT", "Blocks per line") { |v| blkPerLine = v.to_i }
opts.on("-b INT", "Bytes per block") { |v| bytePerBlk = v.to_i }
opts.on("-t INT", "Title every INT lines") { |v| titleLines = v.to_i }
opts.on("-c", "--cbreaks", "Blocks in character data") { |v| blkNchr = true }
opts.separator " -1 no titles"
opts.separator " 0 title on first & last lines"
opts.on("-o", "--oct", "Print in Octal") { |v| bpf = "%03o"; bpfSpec += 1 }
opts.on("-d", "--dec", "Print in Decimal") { |v| bpf = "%03d"; bpfSpec += 1 }
opts.on("-a", "--all", "Display duplicate lines") { |v| skipSame = false }
opts.on("-m", "--nocolor", "No color output") { |v| doColor = false }
opts.separator ""
opts.separator "Display a hex dump just the way I like it -- wide, with color!"
opts.separator "Color is achieved via ANSI/VT100 escape sequences."
opts.separator "Use 'less -S -R' if you want a pager."
opts.separator "Use --nocolor for terminals with poor color support."
opts.separator ""
end
opts.parse!(ARGV)
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
if(maxBytes < 0) then
puts("hexDump.rb: ERROR: -n argument must be a NON-NEGATIVE INTEGER.")
exit
end
if(skipBytes < 0) then
puts("hexDump.rb: ERROR: -p argument must be a NON-NEGATIVE INTEGER.")
exit
end
if(blkPerLine <= 0) then
puts("hexDump.rb: ERROR: -w argument must be a POSITIVE INTEGER.")
exit
end
if(bytePerBlk <= 0) then
puts("hexDump.rb: ERROR: -b argument must be a POSITIVE INTEGER.")
exit
end
if((titleLines>=0) && (blkPerLine*bytePerBlk > 256)) then
puts("hexDump.rb: ERROR: Can not print more than 256 bytes per line with titles turned on.")
exit
end
if(bpfSpec > 1) then
puts("hexDump.rb: ERROR: Only one occurrence of the -o and -d arguments may be used.")
exit
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
$stdin.binmode
inFile = $stdin
if ( !(ARGV[0].nil?)) then
inFile = open(ARGV[0], 'rb')
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
hdw = (1 + (bpf=="%02x" ? 2 : 3)) * bytePerBlk * blkPerLine + blkPerLine
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
def prtTitle(blkNchr, blkPerLine, bytePerBlk, bpf)
printf("%10s :", "")
0.upto(blkPerLine*bytePerBlk-1) do |i|
if ((i % bytePerBlk)==0) then
printf(" ")
end
printf("#{bpf} ", i)
end
printf(" : ")
clw = bytePerBlk - (bpf=="%02x" ? 2 : 3) + (blkNchr ? 1 : 0)
0.upto(blkPerLine*bytePerBlk-1) do |i|
if ((i % bytePerBlk)==0) then
printf("#{bpf}%#{clw}s", i, '')
end
end
printf("\n")
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
def printLine(sameCount, cLineAdd, cLineHex, cLineStr, hdw)
if (sameCount > 0) then
printf("* (%d duplicate lines suppressed)\n", sameCount)
end
if ( cLineAdd >= 0) then
printf("%010x : %-#{hdw}s : %s\n", cLineAdd, cLineHex, cLineStr)
end
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
if (titleLines >= 0) then
prtTitle(blkNchr, blkPerLine, bytePerBlk, bpf)
end
colCtr = colPrt = colOvr = [ '', '' ]
if (doColor) then
colCtr = [ "\e[0;45m", "\e[0m" ]
colPrt = [ "\e[0m", "\e[0m" ]
colOvr = [ "\e[0;46m", "\e[0m" ]
end
numLinesOut = 0
blockCount = 0
byteCount = 0
cLineStr = ''
cLineAdd = -1
cLineHex = ''
lastLineData = nil
sameCount = 0
if(skipBytes > 0) then
inFile.seek(skipBytes)
end
inFile.each_byte do |b|
if(cLineAdd < 0) then
cLineAdd = byteCount + skipBytes
cLineHex=''
cLineStr=''
end
byteCount += 1
if (b<32) then
cLineStr += colCtr[0] + (b+64).chr + colCtr[1]
elsif (b>126) then
cLineStr += colOvr[0] + '.' + colOvr[1]
else
cLineStr += colPrt[0] + b.chr + colPrt[1]
end
if (blkNchr && ((byteCount % bytePerBlk)==0)) then
cLineStr += " "
end
cLineHex += sprintf("#{bpf} ", b)
if ( (maxBytes > 0) && (byteCount >= maxBytes) ) then
break
end
if ((byteCount % bytePerBlk)==0) then
blockCount += 1
if ((blockCount % blkPerLine)==0) then
curLineData = cLineHex + cLineStr
if (skipSame && (lastLineData == curLineData)) then
sameCount += 1
else
printLine(sameCount, cLineAdd, cLineHex, cLineStr, hdw)
sameCount = 0
lastLineData = curLineData;
numLinesOut += 1;
if ((titleLines>0) && ((numLinesOut % titleLines) == 0)) then
prtTitle(blkNchr, blkPerLine, bytePerBlk, bpf)
end
end
cLineAdd = -1
else
cLineHex += sprintf(" ")
end
end
end
printLine(sameCount, cLineAdd, cLineHex, cLineStr, hdw)
if (titleLines >= 0) then
prtTitle(blkNchr, blkPerLine, bytePerBlk, bpf)
end
printf("\n")
inFile.close()
|