2009-03-29 07:30:54 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2010-09-09 18:19:35 +00:00
|
|
|
# Copyright (C) 2006-2009 Yoann GUILLOT
|
2009-03-29 07:30:54 +00:00
|
|
|
#
|
|
|
|
# Licence is LGPL, see LICENCE in the top-level directory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IO
|
|
|
|
def hexdump(ctx={})
|
2013-08-30 21:28:33 +00:00
|
|
|
ctx[:noend] = true
|
|
|
|
while buf = read(512) and not buf.empty?
|
|
|
|
buf.hexdump(ctx)
|
|
|
|
end
|
|
|
|
ctx.delete :noend
|
|
|
|
''.hexdump(ctx)
|
2009-03-29 07:30:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class String
|
|
|
|
def hexdump(ctx={})
|
2013-08-30 21:28:33 +00:00
|
|
|
fmt = ctx[:fmt] ||= ['c', 'd', 'a']
|
|
|
|
ctx[:pos] ||= 0
|
|
|
|
ctx[:linelen] ||= 16
|
|
|
|
scan(/.{1,#{ctx[:linelen]}}/m) { |s|
|
|
|
|
if s != ctx[:lastline]
|
|
|
|
ctx[:lastdup] = false
|
|
|
|
print '%04x ' % ctx[:pos]
|
|
|
|
print s.unpack('C*').map { |b| '%02x' % b }.join(' ').ljust(3*16-1) + ' ' if fmt.include? 'c'
|
|
|
|
print s.unpack('v*').map { |b| '%04x' % b }.join(' ').ljust(5*8-1) + ' ' if fmt.include? 'w'
|
|
|
|
print s.unpack('L*').map { |b| '%08x' % b }.join(' ').ljust(9*4-1) + ' ' if fmt.include? 'd'
|
|
|
|
print s.tr("\0-\x1f\x7f-\xff", '.') if fmt.include? 'a'
|
|
|
|
puts
|
|
|
|
elsif not ctx[:lastdup]
|
|
|
|
ctx[:lastdup] = true
|
|
|
|
puts '*'
|
|
|
|
end
|
|
|
|
ctx[:lastline] = s
|
|
|
|
ctx[:pos] += s.length
|
|
|
|
}
|
|
|
|
puts '%04x' % ctx[:pos] if not ctx[:noend]
|
2011-04-06 17:40:01 +00:00
|
|
|
rescue Errno::EPIPE
|
2013-08-30 21:28:33 +00:00
|
|
|
exit
|
2009-03-29 07:30:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
2013-08-30 21:28:33 +00:00
|
|
|
fmt = []
|
|
|
|
fmt << 'c' if ARGV.delete '-C'
|
|
|
|
fmt << 'w' if ARGV.delete '-W'
|
|
|
|
fmt << 'd' if ARGV.delete '-D'
|
|
|
|
fmt << 'a' if ARGV.delete '-A'
|
|
|
|
fmt = ['c', 'd', 'a'] if ARGV.delete '-a'
|
|
|
|
File.open(ARGV.first, 'rb').hexdump(:fmt => fmt)
|
2009-03-29 07:30:54 +00:00
|
|
|
end
|