Module: Pwnlib::Constants

Extended by:
Context
Defined in:
lib/pwnlib/constants/constants.rb,
lib/pwnlib/constants/constant.rb

Overview

Module containing constants.

Examples:

context.arch = 'amd64'
Pwnlib::Constants.SYS_read
#=> Constant('SYS_read', 0x0)

Defined Under Namespace

Classes: Constant, ConstantBuilder

Class Method Summary collapse

Class Method Details

.eval(str) ⇒ Constant

Eval for Constants.

Examples:

Constants.eval('O_CREAT')
#=> Constant('(O_CREAT)', 0x40)
Constants.eval('O_CREAT | O_APPEND')
#=> Constant('(O_CREAT | O_APPEND)', 0x440)
Constants.eval('meow')
# Pwnlib::Errors::ConstantNotFoundError: Undefined constant(s): meow

Parameters:

  • str (String)

    The string to be evaluated.

Returns:

Raises:



52
53
54
55
56
57
58
59
60
61
# File 'lib/pwnlib/constants/constants.rb', line 52

def eval(str)
  return str unless str.instance_of?(String)

  begin
    val = calculator.evaluate!(str.strip).to_i
  rescue Dentaku::UnboundVariableError => e
    raise ::Pwnlib::Errors::ConstantNotFoundError, "Undefined constant(s): #{e.unbound_variables.join(', ')}"
  end
  ::Pwnlib::Constants::Constant.new("(#{str})", val)
end

.method_missing(method, *args, &block) ⇒ Constant

To support getting constants like Pwnlib::Constants.SYS_read.

Returns:

Raises:

  • (NoMethodError)


24
25
26
# File 'lib/pwnlib/constants/constants.rb', line 24

def method_missing(method, *args, &block)
  args.empty? && block.nil? && get_constant(method) || super
end

.respond_to_missing?(method, _include_all) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pwnlib/constants/constants.rb', line 29

def respond_to_missing?(method, _include_all)
  !get_constant(method).nil?
end