Class: Ronin::CLI::Commands::Hexdump Private

Inherits:
FileProcessorCommand show all
Defined in:
lib/ronin/cli/commands/hexdump.rb

Overview

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

Hexdumps data in a variety of encodings and formats.

Usage

ronin hexdump [options] [FILE ...]

Options

-t int8|uint8|char|uchar|byte|int16|int16_le|int16_be|int16_ne|uint16|uint16_le|uint16_be|uint16_ne|short|short_le|short_be|short_ne|ushort|ushort_le|ushort_be|ushort_ne|int32|int32_le|int32_be|int32_ne|uint32|uint32_le|uint32_be|uint32_ne|int|long|long_le|long_be|long_ne|uint|ulong|ulong_le|ulong_be|ulong_ne|int64|int64_le|int64_be|int64_ne|uint64|uint64_le|uint64_be|uint64_ne|long_long|long_long_le|long_long_be|long_long_ne|ulong_long|ulong_long_le|ulong_long_be|ulong_long_ne|float|float_le|float_be|float_ne|double|double_le|double_be|double_ne, --type The binary data type to decode the data as (Default: byte) -O, --offset INDEX Offset within the data to start hexdumping at -L, --length LEN Length of data to hexdump -Z, --zero-pad Enables zero-padding the input data -c, --columns WIDTH The number of bytes to hexdump per line -g, --group-columns WIDTH Groups columns together -G, --group-chars WIDTH|type Group characters into columns -r, --[no-]repeating Allows repeating lines in hexdump output -b, --base 2|8|10|16 Base to print numbers in -B, --index-base 2|8|10|16 Base to print the index addresses in -I, --index-offset INT Starting number for the index addresses -C, --[no-]chars-column Enables/disables the characters column -E, --encoding ascii|utf8 Encoding to display the characters in (Default: ascii) --style-index STYLE ANSI styles the index column --style-numeric STYLE ANSI styles the numeric columns --style-chars STYLE ANSI styles the characters column --highlight-index PATTERN:STYLE Applies ANSI highlighting to the index column --highlight-numeric PATTERN:STYLE Applies ANSI highlighting to the numeric column --highlight-chars PATTERN:STYLE Applies ANSI highlighting to the characters column -h, --help Print help information

Arguments

[FILE] Optional file to hexdump

Since:

  • 2.0.0

Constant Summary collapse

TYPES =

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

Supported types for the -t,--type option.

Since:

  • 2.0.0

[
  :int8,
  :uint8,
  :char,
  :uchar,
  :byte, # default
  :int16,
  :int16_le,
  :int16_be,
  :int16_ne,
  :uint16,
  :uint16_le,
  :uint16_be,
  :uint16_ne,
  :short,
  :short_le,
  :short_be,
  :short_ne,
  :ushort,
  :ushort_le,
  :ushort_be,
  :ushort_ne,
  :int32,
  :int32_le,
  :int32_be,
  :int32_ne,
  :uint32,
  :uint32_le,
  :uint32_be,
  :uint32_ne,
  :int,
  :long,
  :long_le,
  :long_be,
  :long_ne,
  :uint,
  :ulong,
  :ulong_le,
  :ulong_be,
  :ulong_ne,
  :int64,
  :int64_le,
  :int64_be,
  :int64_ne,
  :uint64,
  :uint64_le,
  :uint64_be,
  :uint64_ne,
  :long_long,
  :long_long_le,
  :long_long_be,
  :long_long_ne,
  :ulong_long,
  :ulong_long_le,
  :ulong_long_be,
  :ulong_long_ne,
  :float,
  :float_le,
  :float_be,
  :float_ne,
  :double,
  :double_le,
  :double_be,
  :double_ne
]
BASES =

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

Mapping of supported values for the -b,--base option.

Since:

  • 2.0.0

{'2' => 2, '8' => 8, '10' => 10, '16' => 16}
STYLES =

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

Mapping of style names to Symbols.

Since:

  • 2.0.0

{
  # font styling
  'bold'      => :bold,
  'faint'     => :faint,
  'italic'    => :italic,
  'underline' => :underline,
  'invert'    => :invert,
  'strike'    => :strike,

  # foreground colors
  'black'   => :black,
  'red'     => :red,
  'green'   => :green,
  'yellow'  => :yellow,
  'blue'    => :blue,
  'magenta' => :magenta,
  'cyan'    => :cyan,
  'white'   => :white,

  # background colors
  'on_black'   => :on_black,
  'on_red'     => :on_red,
  'on_green'   => :on_green,
  'on_yellow'  => :on_yellow,
  'on_blue'    => :on_blue,
  'on_magenta' => :on_magenta,
  'on_cyan'    => :on_cyan,
  'on_white'   => :on_white
}
HEXDUMP_OPTIONS =

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

List of command options that directly map to the keyword arguments of Hexdump.hexdump.

Since:

  • 2.0.0

[
  :type,
  :format,
  :offset,
  :length,
  :zero_pad,
  :columns,
  :group_columns,
  :group_chars,
  :repeating,
  :base,
  :index_base,
  :index_offset,
  :chars_column,
  :encoding
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FileProcessorCommand

#process_file

Constructor Details

#initialize(**kwargs) ⇒ Hexdump

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.

Initializes the hexdump command.

Since:

  • 2.0.0



270
271
272
273
274
275
276
# File 'lib/ronin/cli/commands/hexdump.rb', line 270

def initialize(**kwargs)
  super(**kwargs)

  @highlight_index   = {}
  @highlight_numeric = {}
  @highlight_chars   = {}
end

Instance Attribute Details

#highlight_charsArray<(Regexp, Array<Symbol>)>, Array<(String, Array<Symbol>)> (readonly)

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.

The highlighting rules to apply to the characters column.

Returns:

  • (Array<(Regexp, Array<Symbol>)>, Array<(String, Array<Symbol>)>)

Since:

  • 2.0.0



265
266
267
# File 'lib/ronin/cli/commands/hexdump.rb', line 265

def highlight_chars
  @highlight_chars
end

#highlight_indexArray<(Regexp, Array<Symbol>)>, Array<(String, Array<Symbol>)> (readonly)

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.

The highlighting rules to apply to the index column.

Returns:

  • (Array<(Regexp, Array<Symbol>)>, Array<(String, Array<Symbol>)>)

Since:

  • 2.0.0



253
254
255
# File 'lib/ronin/cli/commands/hexdump.rb', line 253

def highlight_index
  @highlight_index
end

#highlight_numericArray<(Regexp, Array<Symbol>)>, Array<(String, Array<Symbol>)> (readonly)

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.

The highlighting rules to apply to the numeric column.

Returns:

  • (Array<(Regexp, Array<Symbol>)>, Array<(String, Array<Symbol>)>)

Since:

  • 2.0.0



259
260
261
# File 'lib/ronin/cli/commands/hexdump.rb', line 259

def highlight_numeric
  @highlight_numeric
end

Instance Method Details

#hexdump_highlights_kwargsHash{Symbol => Object}

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.

The hexdump highlights: keyword arguments.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 2.0.0



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/ronin/cli/commands/hexdump.rb', line 492

def hexdump_highlights_kwargs
  highlights = {}

  unless @highlight_index.empty?
    highlights[:index] = @highlight_index
  end

  unless @highlight_numeric.empty?
    highlights[:numeric] = @highlight_numeric
  end

  unless @highlight_chars.empty?
    highlights[:chars] = @highlight_chars
  end

  return highlights
end

#hexdump_kwargsHash{Symbol => Object}

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.

Creates a keyword arguments Hash of all command options that will be directly passed to Hexdump.hexdump.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 2.0.0



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/ronin/cli/commands/hexdump.rb', line 442

def hexdump_kwargs
  kwargs = {}

  HEXDUMP_OPTIONS.each do |key|
    kwargs[key] = options[key] if options.has_key?(key)
  end

  if options.has_key?(:style_index)   ||
     options.has_key?(:style_numeric) ||
     options.has_key?(:style_chars)
    kwargs[:style] = hexdump_style_kwargs
  end

  if !@highlight_index.empty?   ||
     !@highlight_numeric.empty? ||
     !@highlight_chars.empty?
    kwargs[:highlights] = hexdump_highlights_kwargs
  end

  return kwargs
end

#hexdump_style_kwargsHash{Symbol => Object}

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.

The hexdump style: keyword arguments.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 2.0.0



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/ronin/cli/commands/hexdump.rb', line 469

def hexdump_style_kwargs
  style = {}

  if (index_style = options[:style_index])
    style[:index] = index_style
  end

  if (numeric_style = options[:style_numeric])
    style[:numeric] = numeric_style
  end

  if (chars_style = options[:style_chars])
    style[:chars] = chars_style
  end

  return style
end

#open_file(file) {|file| ... } ⇒ File?

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.

Opens the file in binary mode.

Yields:

  • (file)

    If a block is given, the newly opened file will be yielded. Once the block returns the file will automatically be closed.

Yield Parameters:

  • file (File)

    The newly opened file.

Returns:

  • (File, nil)

    If no block is given, the newly opened file object will be returned. If no block was given, then nil will be returned.

Since:

  • 2.0.0



304
305
306
# File 'lib/ronin/cli/commands/hexdump.rb', line 304

def open_file(file,&block)
  File.open(file,'rb',&block)
end

#parse_group_chars(value) ⇒ Integer, :type

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 the value passed to the -G,--group-chars option.

Parameters:

  • value (String)

    The -G,--group-chars argument value.

Returns:

  • (Integer, :type)

    The parsed integer or :type if the type argument was given.

Since:

  • 2.0.0



327
328
329
330
331
332
333
334
# File 'lib/ronin/cli/commands/hexdump.rb', line 327

def parse_group_chars(value)
  case value
  when 'type'  then :type
  when /^\d+$/ then value.to_i
  else
    raise(OptionParser::InvalidArgument,"invalid value: #{value}")
  end
end

#parse_highlight(value) ⇒ (Regexp, Array<Symbol>), (String, Array<Symbol>)

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 a highlight rule of the form /REGEXP/:STYLE or STRING:STYLE.

Parameters:

  • value (String)

    The raw string value to parse.

Returns:

  • ((Regexp, Array<Symbol>), (String, Array<Symbol>))

    The Regexp or String pattern to match and the style rules to apply to it.

Since:

  • 2.0.0



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/ronin/cli/commands/hexdump.rb', line 395

def parse_highlight(value)
  if value.start_with?('/')
    unless (index = value.rindex('/:'))
      raise(OptionParser::InvalidArgument,"argument must be of the form /REGEXP/:STYLE but was: #{value}")
    end

    regexp = Regexp.new(value[1...index])
    style  = parse_style(value[(index + 2)..])

    return [regexp, style]
  else
    unless (index = value.rindex(':'))
      raise(OptionParser::InvalidArgument,"argument must be of the form STRING:STYLE but was: #{value}")
    end

    pattern = value[0...index]
    style   = parse_style(value[(index + 1)..])

    return [pattern, style]
  end
end

#parse_style(value) ⇒ Array<Symbol>

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 a style string.

Parameters:

  • value (String)

    The comma-separated list of style names.

Returns:

  • (Array<Symbol>)

    The array of parsed style names.

Since:

  • 2.0.0



376
377
378
379
380
381
382
# File 'lib/ronin/cli/commands/hexdump.rb', line 376

def parse_style(value)
  value.split(/\s*,\s*/).map do |style_name|
    STYLES.fetch(style_name) do
      raise(OptionParser::InvalidArgument,"unknown style name: #{style_name}")
    end
  end
end

#process_input(input) ⇒ Object

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.

Hexdumps the input stream.

Parameters:

  • input (IO, StringIO)

    The input stream to hexdump.

Since:

  • 2.0.0



314
315
316
# File 'lib/ronin/cli/commands/hexdump.rb', line 314

def process_input(input)
  @hexdump.hexdump(input)
end

#run(*files) ⇒ Object

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.

Runs the ronin hexdump command.

Parameters:

  • files (Array<String>)

    Additional files to hexdump.

Since:

  • 2.0.0



284
285
286
287
288
# File 'lib/ronin/cli/commands/hexdump.rb', line 284

def run(*files)
  @hexdump = ::Hexdump::Hexdump.new(**hexdump_kwargs)

  super(*files)
end