Module: ZHexdump
- Included in:
- String
- Defined in:
- lib/zhexdump.rb,
lib/zhexdump/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.defaults ⇒ Object
Returns the value of attribute defaults.
Class Method Summary collapse
- ._get_param(h, key, default) ⇒ Object
- .dump(data, h = {}) ⇒ Object (also: hexdump)
Instance Method Summary collapse
-
#hexdump(options = {}, &block) ⇒ Object
default hexdump to STDOUT (unless options overridden).
-
#to_hexdump(options = {}, &block) ⇒ Object
default hexdump to string (unless options overridden).
Class Attribute Details
.defaults ⇒ Object
Returns the value of attribute defaults.
25 26 27 |
# File 'lib/zhexdump.rb', line 25 def defaults @defaults end |
Class Method Details
._get_param(h, key, default) ⇒ Object
27 28 29 |
# File 'lib/zhexdump.rb', line 27 def _get_param(h, key, default) h[key] || defaults[key] || default end |
.dump(data, h = {}) ⇒ Object Also known as: hexdump
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 |
# File 'lib/zhexdump.rb', line 31 def dump data, h = {} offset = h.fetch(:offset, 0) dedup = h.fetch(:dedup, true) show_offset = h.fetch(:show_offset, h.fetch(:show_addr, true)) if offset == false show_offset = false offset = 0 end add = _get_param(h, :add, 0) size = _get_param(h, :size, data.size-offset) tail = _get_param(h, :tail, "\n") width = _get_param(h, :width, 16) output = _get_param(h, :output, $stdout) indent = _get_param(h, :indent, 0) offset_format = _get_param(h, :offset_format, "%08x: ") indent = ' ' * indent size = data.size-offset if size+offset > data.size prevhex = ''; c = nil; prevdup = false; start = offset while true ascii = ''; hex = '' width.times do |i| hex << ' ' if i%8==0 && i>0 if c = ((size > 0) && data[offset+i]) ord = c.ord hex << "%02x " % ord ascii << (ord == 0 ? ' ' : ((32..126).include?(ord) ? c : '.')) else hex << ' ' ascii << ' ' end size-=1 end if dedup && hex == prevhex row = indent + "*" yield(row, offset+add, ascii) if block_given? unless prevdup output << "\n" if offset > start output << row end prevdup = true else row = indent + (show_offset ? (offset_format % (offset + add)) : '') + hex yield(row, offset+add, ascii) if block_given? row << ' |' + ascii + "|" output << "\n" if offset > start output << row prevdup = false end offset += width prevhex = hex break if size <= 0 end if show_offset && prevdup row = indent + (offset_format % (offset + add)) yield(row) if block_given? output << "\n" << row end output << tail end |