Sha256: 561082ca21f5bf05746aee8c98bab0b57dbae74b538d26bbe3e3174617a2d331

Contents?: true

Size: 1.99 KB

Versions: 31

Compression:

Stored size: 1.99 KB

Contents

%w[rubygems colorize].each{|r| require r}

ASCII_PRINTABLE = (33..126)

=begin
000352c0  ed 33 8c 85 6e cc f6 f7  72 79 1c e3 3a b4 c2 c6  |.3..n...ry..:...|
000352d0  c8 8d d6 ee 3e 68 a1 a5  ae b2 b7 97 a4 1d 5f a7  |....>h........_.|
000352e0  d8 7d 28 db f6 8a e7 8a  7b 8d 0b bd 35 7d 25 3c  |.}(.....{...5}%<|
000352f0  8b 3c c8 9d ec 04 85 54  92 a0 f7 a8 ed cf 05 7d  |.<.....T.......}|
00035300  b5 e3 9e 35 f0 79 9f 51  74 e3 60 ee 0f 03 8e 3f  |...5.y.Qt.`....?|
00035310  05 5b 91 87 e6 48 48 ee  a3 77 ae ad 5e 2a 56 a2  |.[...HH..w..^*V.|
00035320  b6 96 86 f3 3c 92 b3 c8  62 4a 6f 96 10 5c 9c bb  |....<...bJo..\..|
=end

# whoops!
# 48:  d2 b1 6d 31 3e 67 e1 88  99 8b 4b 34 1d 61 05 15  |..m1g....K4.a..|
#

def hexdump(data, options={})
  base_offset   = options[:base_offset] || 0
  color         = options[:color].nil? ? true : options[:color] 
  highlight     = options[:highlight]

  p options
  p color
  
  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
  
  p [max_offset, max_offset_width]
  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('')

    if color
      outstring = "<light_cyan>%s<cyan>:  <light_yellow>%s <default>|<light_white>%s<default>|" % [offset.to_s.ljust(max_offset_width), hex.ljust(max_hex_width), plain]
      outstring = outstring.colorize
    else
      outstring = "%s:  %s |%s|" % [offset.to_s.ljust(max_offset_width), hex.ljust(max_hex_width), plain]
    end
    puts outstring
  end      
end


if $0 == __FILE__
  data = (0..64).map{ rand(255).chr }.join('')
  hexdump(data)
  puts
  hexdump(data, :color=>false)
  puts
  
  data = "1234567890"*10
  hexdump(data)
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
epitools-0.4.15 lib/epitools/hexdump.rb
epitools-0.4.14 lib/epitools/hexdump.rb
epitools-0.4.13 lib/epitools/hexdump.rb
epitools-0.4.10 lib/epitools/hexdump.rb
epitools-0.4.9 lib/epitools/hexdump.rb
epitools-0.4.8 lib/epitools/hexdump.rb
epitools-0.4.7 lib/epitools/hexdump.rb
epitools-0.4.6 lib/epitools/hexdump.rb
epitools-0.4.5 lib/epitools/hexdump.rb
epitools-0.4.4 lib/epitools/hexdump.rb
epitools-0.4.3 lib/epitools/hexdump.rb
epitools-0.4.2 lib/epitools/hexdump.rb
epitools-0.4.1 lib/epitools/hexdump.rb
epitools-0.4.0 lib/epitools/hexdump.rb
epitools-0.3.4 lib/epitools/hexdump.rb
epitools-0.3.3 lib/epitools/hexdump.rb
epitools-0.3.2 lib/epitools/hexdump.rb
epitools-0.3.1 lib/epitools/hexdump.rb
epitools-0.3.0 lib/epitools/hexdump.rb
epitools-0.2.1 lib/epitools/hexdump.rb