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
|
# File 'lib/epitools/hexdump.rb', line 29
def self.dump(data, **options)
base_offset = options[:base_offset] || 0
color = options[:color].nil? ? true : options[:color]
highlight = options[:highlight]
lines = data.scan(/.{1,16}/m)
max_offset = (base_offset + data.size) / 16 * 16
max_offset_width = max_offset.to_s.size
max_hex_width = 3 * 16 + 1
lines.each_with_index do |line,n|
offset = base_offset + n*16
bytes = line.unpack("C*")
hex = bytes.map { |c| "%0.2x" % c }.insert(8, '').join(' ')
plain = bytes.map do |c|
if ASCII_PRINTABLE.include?(c)
c = c.chr
else
color ? '<9>.</9>' : '.'
end
end.join('')
puts "<11>#{offset.to_s.ljust(max_offset_width)}<3>: <14>#{hex.ljust(max_hex_width)} <8>|<15>#{plain}<8>|".colorize(:strip => !color)
end
end
|