Class: Ronin::Binary::Hexdump::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/binary/hexdump/parser.rb

Overview

Since:

  • 0.5.0

Constant Summary collapse

BASES =

Bases for various encodings

Since:

  • 0.5.0

{
  :binary         => 2,
  :octal          => 8,
  :octal_bytes    => 8,
  :octal_shorts   => 8,
  :octal_ints     => 8,
  :octal_quads    => 8,
  :decimal        => 10,
  :decimal_bytes  => 10,
  :decimal_shorts => 10,
  :decimal_ints   => 10,
  :decimal_quads  => 10,
  :hex            => 16,
  :hex_bytes      => 16,
  :hex_shorts     => 16,
  :hex_ints       => 16,
  :hex_quads      => 16,
  :named_chars    => 16,
  :floats         => 10,
  :doubles        => 10
}
WORD_SIZES =

Word-sizes for various encodings

Since:

  • 0.5.0

{
  :binary         => 1,
  :octal_bytes    => 1,
  :decimal_bytes  => 1,
  :hex_bytes      => 1,
  :hex_chars      => 1,
  :named_chars    => 1,
  :octal_shorts   => 2,
  :decimal_shorts => 2,
  :hex_shorts     => 2,
  :octal_ints     => 4,
  :decimal_ints   => 4,
  :hex_ints       => 4,
  :octal_quads    => 8,
  :decimal_quads  => 8,
  :hex_quads      => 8,
  :floats         => 4,
  :doubles        => 8
}
CHARS =

Visible characters

Since:

  • 0.5.0

Hash[Chars::VISIBLE.chars.sort.zip(Chars::VISIBLE.bytes.sort)]
ESCAPED_CHARS =

Escaped characters

Since:

  • 0.5.0

{
  '\0' => 0x00,
  '\a' => 0x07,
  '\b' => 0x08,
  '\t' => 0x09,
  '\n' => 0x0a,
  '\v' => 0x0b,
  '\f' => 0x0c,
  '\r' => 0x0d
}
NAMED_CHARS =

od named characters

Since:

  • 0.5.0

{
  'nul' => 0x00,
  'soh' => 0x01,
  'stx' => 0x02,
  'etx' => 0x03,
  'eot' => 0x04,
  'enq' => 0x05,
  'ack' => 0x06,
  'bel' => 0x07,
  'bs'  => 0x08,
  'ht'  => 0x09,
  'lf'  => 0x0a,
  'nl'  => 0x0a,
  'vt'  => 0x0b,
  'ff'  => 0x0c,
  'cr'  => 0x0d,
  'so'  => 0x0e,
  'si'  => 0x0f,
  'dle' => 0x10,
  'dc1' => 0x11,
  'dc2' => 0x12,
  'dc3' => 0x13,
  'dc4' => 0x14,
  'nak' => 0x15,
  'syn' => 0x16,
  'etb' => 0x17,
  'can' => 0x18,
  'em'  => 0x19,
  'sub' => 0x1a,
  'esc' => 0x1b,
  'fs'  => 0x1c,
  'gs'  => 0x1d,
  'rs'  => 0x1e,
  'us'  => 0x1f,
  'sp'  => 0x20,
  'del' => 0x7f
}
FORMATS =

Array#pack codes for various types/endianness/word-sizes

Since:

  • 0.5.0

{
  :integer => {
    :little => {
      1 => 'C',
      2 => (RUBY_VERSION > '1.9.' ? 'S<' : 'v'),
      4 => (RUBY_VERSION > '1.9.' ? 'L<' : 'V'),
      8 => 'Q<' # Ruby 1.9 only
    },

    :big => {
      1 => 'C',
      2 => (RUBY_VERSION > '1.9.' ? 'S>' : 'n'),
      4 => (RUBY_VERSION > '1.9.' ? 'L>' : 'N'),
      8 => 'Q>' # Ruby 1.9 only
    }
  },

  :float => {
    :little => {
      4 => 'e',
      8 => 'E'
    },

    :big => {
      4 => 'g',
      8 => 'G'
    }
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Initializes the hexdump parser.

Parameters:

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

    Additional options.

Options Hash (options):

  • :format (Symbol)

    The expected format of the hexdump. Must be either :od or :hexdump.

  • :encoding (Symbol)

    Denotes the encoding used for the bytes within the hexdump. Must be one of the following:

    • :binary
    • :octal
    • :octal_bytes
    • :octal_shorts
    • :octal_ints
    • :octal_quads (Ruby 1.9 only)
    • :decimal
    • :decimal_bytes
    • :decimal_shorts
    • :decimal_ints
    • :decimal_quads (Ruby 1.9 only)
    • :hex
    • :hex_chars
    • :hex_bytes
    • :hex_shorts
    • :hex_ints
    • :hex_quads (Ruby 1.9 only)
    • :named_chars
    • :floats
    • :doubles
  • :endian (:little, :big, :network) — default: :little

    The endianness of the words.

Since:

  • 0.5.0


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ronin/binary/hexdump/parser.rb', line 135

def initialize(options={})
  @format   = options[:format]
  @encoding = options[:encoding]

  @type = case @encoding
          when :floats, :doubles then :float
          else                        :integer
          end
  @endian = options.fetch(:endian,:little)

  case @format
  when :od
    @address_base = 8
    @base         = 8
    @word_size    = 2
  when :hexdump
    @address_base = 16
    @base         = 16
    @word_size    = 2
  else
    @address_base = 16
    @base         = 16
    @word_size    = 1
  end

  if @encoding
    @base      = BASES.fetch(options[:encoding])
    @word_size = WORD_SIZES.fetch(options[:encoding])
  end

  @chars = case @encoding
           when :hex_chars   then CHARS.merge(ESCAPED_CHARS)
           when :named_chars then CHARS.merge(NAMED_CHARS)
           end
end

Instance Attribute Details

#address_baseObject (readonly)

The base of all addresses to parse

Since:

  • 0.5.0


92
93
94
# File 'lib/ronin/binary/hexdump/parser.rb', line 92

def address_base
  @address_base
end

#baseObject (readonly)

The base of all words to parse

Since:

  • 0.5.0


95
96
97
# File 'lib/ronin/binary/hexdump/parser.rb', line 95

def base
  @base
end

#encodingObject (readonly)

The encoding of the hexdump data

Since:

  • 0.5.0


80
81
82
# File 'lib/ronin/binary/hexdump/parser.rb', line 80

def encoding
  @encoding
end

#endianObject (readonly)

The endianness of data to parse (:little, :big, :network)

Since:

  • 0.5.0


86
87
88
# File 'lib/ronin/binary/hexdump/parser.rb', line 86

def endian
  @endian
end

#formatObject (readonly)

The format to parse (:hexdump / :od)

Since:

  • 0.5.0


77
78
79
# File 'lib/ronin/binary/hexdump/parser.rb', line 77

def format
  @format
end

#typeObject (readonly)

The type of data to parse (:integer / :float)

Since:

  • 0.5.0


83
84
85
# File 'lib/ronin/binary/hexdump/parser.rb', line 83

def type
  @type
end

#word_sizeObject (readonly)

The size of words to parse

Since:

  • 0.5.0


89
90
91
# File 'lib/ronin/binary/hexdump/parser.rb', line 89

def word_size
  @word_size
end

Instance Method Details

#pack(values) ⇒ String (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Packs a segment back into bytes.

Parameters:

Returns:

  • (String)

    The packed segment.

Since:

  • 0.5.0


396
397
398
# File 'lib/ronin/binary/hexdump/parser.rb', line 396

def pack(values)
  values.pack(FORMATS[@type][@endian][@word_size] * values.length)
end

#parse(hexdump) ⇒ String

Parses a hexdump.

Parameters:

  • hexdump (#each_line)

    The hexdump output.

Returns:

  • (String)

    The raw-data from the hexdump.

Since:

  • 0.5.0


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ronin/binary/hexdump/parser.rb', line 180

def parse(hexdump)
  current_addr = last_addr = first_addr = nil
  repeated = false

  segment = ''
  buffer  = ''

  hexdump.each_line do |line|
    if @format == :hexdump
      line = line.gsub(/\s+\|.+\|\s*$/,'')
    end

    words = line.split

    if words.first == '*'
      repeated = true
    elsif words.length > 0
      current_addr = parse_address(words.shift)
      first_addr ||= current_addr

      if repeated
        (((current_addr - last_addr) / segment.length) - 1).times do
          buffer << segment
        end

        repeated = false
      end

      segment = pack(words.map { |word| parse_word(word) })

      buffer   << segment
      last_addr = current_addr
    end
  end

  return buffer[0,last_addr - first_addr]
end

#parse_address(address) ⇒ Integer (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses an address.

Parameters:

  • address (String)

    The text of the address.

Returns:

  • (Integer)

    The parsed address.

Since:

  • 0.5.0


320
321
322
# File 'lib/ronin/binary/hexdump/parser.rb', line 320

def parse_address(address)
  address.to_i(@address_base)
end

#parse_char(char) ⇒ Integer (protected)

Parses a character or hex-byte.

Parameters:

  • char (String)

    The character to parse.

Returns:

Since:

  • 0.5.0


333
334
335
336
337
# File 'lib/ronin/binary/hexdump/parser.rb', line 333

def parse_char(char)
  @chars.fetch(char) do |hex_byte|
    hex_byte.to_i(16)
  end
end

#parse_float(float) ⇒ Float (protected)

Parses a float.

Parameters:

  • float (String)

    The text of the float.

Returns:

  • (Float)

    The parsed float.

Since:

  • 0.5.0


365
366
367
# File 'lib/ronin/binary/hexdump/parser.rb', line 365

def parse_float(float)
  float.to_f
end

#parse_int(int) ⇒ Integer (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses an Integer.

Parameters:

  • int (String)

    The text of the Integer.

Returns:

Since:

  • 0.5.0


350
351
352
353
354
# File 'lib/ronin/binary/hexdump/parser.rb', line 350

def parse_int(int)
  if @chars then parse_char(int)
  else           int.to_i(@base)
  end
end

#parse_word(word) ⇒ Integer, Float (protected)

Parses a word within a segment.

Parameters:

  • word (String)

    The word to parse.

Returns:

Since:

  • 0.5.0


378
379
380
381
382
383
# File 'lib/ronin/binary/hexdump/parser.rb', line 378

def parse_word(word)
  case @type
  when :integer then parse_int(word)
  when :float   then parse_float(word)
  end
end