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
|
# File 'lib/zpng/hexdump.rb', line 11
def dump data, h = {}
offset = h[:offset] || 0
add = h[:add] || 0
size = h[:size] || (data.size-offset)
tail = h[:tail] || "\n"
width = h[:width] || 0x10
h[:show_offset] = true unless h.key?(:show_offset)
h[:dedup] = true unless h.key?(:dedup)
size = data.size-offset if size+offset > data.size
r = ''; prevhex = ''; c = nil; prevdup = false
while true
ascii = ''; hex = ''
width.times do |i|
hex << ' ' if i%8==0 && i>0
if c = ((size > 0) && data[offset+i])
hex << "%02x " % c.ord
ascii << ((32..126).include?(c.ord) ? c : '.')
else
hex << ' '
ascii << ' '
end
size-=1
end
if h[:dedup] && hex == prevhex
row = "*"
yield(row, offset+add, ascii) if block_given?
r << row << "\n" unless prevdup
prevdup = true
else
row = (h[:show_offset] ? ("%08x: " % (offset + add)) : '') + hex + ' |' + ascii + "|"
yield(row, offset+add, ascii) if block_given?
r << row << "\n"
prevdup = false
end
offset += width
prevhex = hex
break if size <= 0
end
if h[:show_offset] && prevdup
row = "%08x: " % (offset + add)
yield(row) if block_given?
r << row
end
r.chomp + tail
end
|