Class: Ronin::Support::Binary::CTypes::EnumType

Inherits:
Type
  • Object
show all
Defined in:
lib/ronin/support/binary/ctypes/enum_type.rb

Overview

Base class for all enum types.

Instance Attribute Summary collapse

Attributes inherited from Type

#pack_string

Instance Method Summary collapse

Methods inherited from Type

#[]

Constructor Details

#initialize(int_type, mapping) ⇒ EnumType

Initializes the enum type.

Parameters:

  • int_type (IntType)

    The underlying int type for the enum.

  • mapping (Hash{Symbol => Integer})

    The mapping of Symbols to Integer values for the enum.



54
55
56
57
58
59
60
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 54

def initialize(int_type,mapping)
  super(pack_string: nil)

  @int_type        = int_type
  @mapping         = mapping
  @reverse_mapping = @mapping.invert
end

Instance Attribute Details

#int_typeIntType (readonly)

The underlying integer type.

Returns:



33
34
35
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 33

def int_type
  @int_type
end

#mappingHash{Symbol => Integer} (readonly)

The enum's mapping of symbols to their integer values.

Returns:



38
39
40
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 38

def mapping
  @mapping
end

#reverse_mappingHash{Integer => Symbol} (readonly)

The reverse mapping of the enum's integers to their symbol values.

Returns:



43
44
45
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 43

def reverse_mapping
  @reverse_mapping
end

Instance Method Details

#align(new_alignment) ⇒ EnumType

Creates a copy of the enum type with different alignment.

Parameters:

  • new_alignment (Integer)

    The new alignment for the enum type.

Returns:



103
104
105
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 103

def align(new_alignment)
  self.class.new(@int_type.align(new_alignment),@mapping)
end

#alignmentInteger

The alignment of the enum type.

Returns:



90
91
92
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 90

def alignment
  @int_type.alignment
end

#dequeue_value(values) ⇒ Symbol, Integer

Dequeues a value from the flat list of values.

Parameters:

  • values (Array)

    The flat array of values.

Returns:

  • (Symbol, Integer)

    The dequeued value.



173
174
175
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 173

def dequeue_value(values)
  @int_type.dequeue_value(values)
end

#enqueue_value(values, value) ⇒ Object

Enqueues a value onto the flat list of values.

Parameters:

  • values (Array)

    The flat array of values.

  • value (Symbol, Integer)

    The value to enqueue.



160
161
162
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 160

def enqueue_value(values,value)
  @int_type.enqueue_value(values,value)
end

#pack(value) ⇒ String

Packs an enum value.

Parameters:

  • value (Symbol, Integer)

    The enum value. Can be either a Symbol from the enum or an Integer.

Returns:

  • (String)

    The packed enum value.

Raises:

  • (ArgumentError)

    The enum value either was not a Symbol or an Integer, or was a Symbol value not in #mapping.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 121

def pack(value)
  int = case value
        when Integer then value
        when Symbol
          @mapping.fetch(value) do
            raise(ArgumentError,"invalid enum value: #{value.inspect}")
          end
        else
          raise(ArgumentError,"enum value must be a Symbol or an Integer: #{value.inspect}")
        end

  @int_type.pack(int)
end

#sizeInteger

The size of the enum type.

Returns:



80
81
82
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 80

def size
  @int_type.size
end

#uninitialized_valueSymbol, Integer

The uninitinalized value for the enum.

Returns:

  • (Symbol, Integer)

    The enum Symbol which maps to 0 or 0.



68
69
70
71
72
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 68

def uninitialized_value
  default_value = @int_type.uninitialized_value

  @reverse_mapping.fetch(default_value,default_value)
end

#unpack(data) ⇒ Symbol, Integer

Unpacks a previously packed enum value.

Parameters:

  • data (String)

    The packed enum value.

Returns:



145
146
147
148
149
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 145

def unpack(data)
  int = @int_type.unpack(data)

  return @reverse_mapping.fetch(int,int)
end