Module: Hexdump

Included in:
IO, String, StringIO
Defined in:
lib/hexdump/hexdump.rb

Overview

Provides the Hexdump.dump method and can add hexdumping to other classes by including the Hexdump module.

class AbstractData

  include Hexdump

  def each_byte
    # ...
  end

end

data = AbstractData.new
data.hexdump

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump(data, options = {}) {|index, hex_segment, print_segment| ... } ⇒ nil

Hexdumps the given data.

Parameters:

  • data (#each_byte)

    The data to be hexdumped.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :width (Integer) — default: 16

    The number of bytes to dump for each line.

  • :base (Symbol, Integer) — default: :hexadecimal

    The base to print bytes in. Supported bases include, :hexadecimal, :hex, 16,:decimal,:dec,10, :octal, :oct, 8, :binary, :bin and 2.

  • :ascii (Boolean) — default: false

    Print ascii characters when possible.

  • :output (#<<) — default: STDOUT

    The output to print the hexdump to.

Yields:

  • (index, hex_segment, print_segment)

    The given block will be passed the hexdump break-down of each segment.

Yield Parameters:

  • index (Integer)

    The index of the hexdumped segment.

  • hex_segment (Array<String>)

    The hexadecimal-byte representation of the segment.

  • print_segment (Array<String>)

    The print-character representation of the segment.

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    The given data does not define the #each_byte method, or the :output value does not support the #<< method.



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hexdump/hexdump.rb', line 60

def Hexdump.dump(data,options={})
  unless data.respond_to?(:each_byte)
    raise(ArgumentError,"the data to hexdump must define #each_byte")
  end

  output = options.fetch(:output,STDOUT)

  unless output.respond_to?(:<<)
    raise(ArgumentError,":output must support the #<< method")
  end

  width = options.fetch(:width,16)
  base = options.fetch(:base,:hexadecimal)
  ascii = options.fetch(:ascii,false)
  byte_width, byte_format = case base
                            when :hexadecimal, :hex, 16
                              [2, "%.2x"]
                            when :decimal, :dec, 10
                              [3, "%3.d"]
                            when :octal, :oct, 8
                              [4, "0%.3o"]
                            when :binary, :bin, 2
                              [8, "%.8b"]
                            end

  hex_byte = lambda { |byte|
    if (ascii && (byte >= 0x20 && byte <= 0x7e))
      byte.chr
    else
      byte_format % byte
    end
  }

  print_byte = lambda { |byte|
    if (byte >= 0x20 && byte <= 0x7e)
      byte.chr
    else
      '.'
    end
  }

  index = 0

  hex_segment_width = ((width * byte_width) + (width - 1))
  line_format = "%.8x  %-#{hex_segment_width}s  |%s|\n"

  data.each_byte.each_slice(width) do |bytes|
    hex_segment = bytes.map(&hex_byte)
    print_segment = bytes.map(&print_byte)

    if block_given?
      yield(index,hex_segment,print_segment)
    else
      output << sprintf(
        line_format,
        index,
        hex_segment.join(' '),
        print_segment.join
      )
    end

    index += width
  end

  # flush the hexdump buffer
  return nil
end

Instance Method Details

#hexdump(options = {}, &block) ⇒ Object

Hexdumps the object.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

See Also:



136
137
138
# File 'lib/hexdump/hexdump.rb', line 136

def hexdump(options={},&block)
  Hexdump.dump(self,options,&block)
end