module Ronin::CLI::BinaryTemplate

Module for commands that uses [Ronin::Support::Binary::Template].

[Ronin::Support::Binary::Template]: ronin-rb.dev/docs/ronin-support/Ronin/Support/Binary/Template.html

@since 2.1.0

Public Class Methods

included(command) click to toggle source

Adds the ‘–endian`, `–arch`, and `–os` options to the command class including {BinaryTemplate}.

@param [Class<Command>] command

The command class including {BinaryTemplate}.
# File lib/ronin/cli/binary_template.rb, line 38
def self.included(command)
  command.option :endian, short: '-E',
                          value: {
                            type: [:little, :big, :net]
                          },
                          desc: 'Sets the endianness'

  command.option :arch, short: '-A',
                        value: {
                          type: [
                            :x86, :x86_64,
                            :ppc, :ppc64,
                            :mips, :mips_le, :mips_be,
                            :mips64, :mips64_le, :mips64_be,
                            :arm, :arm_le, :arm_be,
                            :arm64, :arm64_le, :arm64_be
                          ]
                        },
                        desc: 'Sets the architecture'

  command.option :os, short: '-O',
                      value: {
                        type: [
                          :linux,
                          :macos,
                          :windows,
                          :android,
                          :apple_ios,
                          :bsd,
                          :freebsd,
                          :openbsd,
                          :netbsd
                        ]
                      },
                      desc: 'Sets the OS'
end

Public Instance Methods

build_template(types) click to toggle source

Builds a binary template for the given types and for the optional ‘–endian`, `–arch`, and `–os` options.

@param [Array<Symbol, (Symbol, Integer)>] types

The types for each field in the binary template.

@return [Ronin::Support::Binary::Template]

The binary template.
# File lib/ronin/cli/binary_template.rb, line 114
def build_template(types)
  Support::Binary::Template.new(types, endian: options[:endian],
                                       arch:   options[:arch],
                                       os:     options[:os])
rescue ArgumentError => error
  print_error(error.message)
  exit(1)
end
parse_type(string) click to toggle source

Parses a type string.

@param [String] string

The raw type signature to parse.

@return [Symbol, (Symbol, Integer)]

The parsed type signature.
# File lib/ronin/cli/binary_template.rb, line 84
def parse_type(string)
  unless (match = string.match(/\A(?<type>[a-z0-9_]+)(?:\[(?<array_size>\d*)\])?\z/))
    print_error "invalid type: #{string}"
    exit(-1)
  end

  type       = match[:type].to_sym
  array_size = match[:array_size]

  if array_size && array_size.empty?
    # unbounded array
    type = (type..)
  elsif array_size
    # sized array
    type = [type, array_size.to_i]
  end

  return type
end