Class: Ronin::Support::Binary::CTypes::ScalarType Private

Inherits:
Type
  • Object
show all
Defined in:
lib/ronin/support/binary/ctypes/scalar_type.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.

Base class for all scalar types.

Since:

  • 1.0.0

Direct Known Subclasses

CharType, FloatType, IntType, UIntType

Instance Attribute Summary collapse

Attributes inherited from Type

#pack_string

Instance Method Summary collapse

Methods inherited from Type

#[], #uninitialized_value

Constructor Details

#initialize(size:, alignment: size, endian:, signed:, **kwargs) ⇒ ScalarType

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 scalar type.

Parameters:

  • size (1, 2, 4, 8)

    The scalar type's size in bytes.

  • alignment (Integer, nil) (defaults to: size)

    Optional custom alignment for the scalar type.

  • endian (:little, :big, nil)

    The endianness of the scalar type. nil indicates the type has no endianness.

  • signed (Boolean)

    Indicates whether the scalar type is signed or unsigned.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :pack_string (String)

    The String for Array#pack or String#unpack.

Since:

  • 1.0.0



76
77
78
79
80
81
82
83
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 76

def initialize(size: , alignment: size, endian: , signed: , **kwargs)
  super(**kwargs)

  @endian    = endian
  @size      = size
  @alignment = alignment
  @signed    = signed
end

Instance Attribute Details

#alignmentInteger (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 alignment in bytes for the scalar type.

Returns:

Since:

  • 1.0.0



42
43
44
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 42

def alignment
  @alignment
end

#endian:little, ... (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 endian-ness of the type.

Returns:

  • (:little, :big, nil)

Since:

  • 1.0.0



47
48
49
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 47

def endian
  @endian
end

#signedBoolean (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.

Indicates whether the type is signed.

Returns:

  • (Boolean)

Since:

  • 1.0.0



52
53
54
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 52

def signed
  @signed
end

#size1, ... (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 size in bytes of the type.

Returns:

  • (1, 2, 4, 8)

Since:

  • 1.0.0



37
38
39
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 37

def size
  @size
end

Instance Method Details

#align(new_alignment) ⇒ ScalarType

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 copy of the scalar type with a different #alignment.

Parameters:

  • new_alignment (Integer)

    The new alignment for the new scalar type.

Returns:

Since:

  • 1.0.0



112
113
114
115
116
117
118
119
120
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 112

def align(new_alignment)
  self.class.new(
    size:        @size,
    alignment:   new_alignment,
    endian:      @endian,
    signed:      @signed,
    pack_string: @pack_string
  )
end

#dequeue_value(values) ⇒ Integer, ...

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.

Dequeues a scalar value from the flat list of values.

Parameters:

  • values (Array)

    The flat array of values.

Returns:

Since:

  • 1.0.0



192
193
194
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 192

def dequeue_value(values)
  values.shift
end

#enqueue_value(values, value) ⇒ 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.

Enqueues a scalar value onto the flat list of values.

Parameters:

  • values (Array)

    The flat array of values.

  • value (Integer, Float, String, nil)

    The scalar value to enqueue.

Since:

  • 1.0.0



177
178
179
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 177

def enqueue_value(values,value)
  values.push(value)
end

#pack(value) ⇒ String

Packs the value into the scalar type's binary format.

Parameters:

Returns:

  • (String)

    The packed binary data.

Raises:

Since:

  • 1.0.0



136
137
138
139
140
141
142
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 136

def pack(value)
  if @pack_string
    [value].pack(@pack_string)
  else
    raise(NotImplementedError,"#{self.class} does not define a #pack_string")
  end
end

#signed?Boolean

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.

Whether the scalar type is signed.

Returns:

  • (Boolean)

Since:

  • 1.0.0



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

def signed?
  @signed
end

#unpack(data) ⇒ Integer, ...

Unpacks the binary data.

Parameters:

  • data (String)

    The binary data to unpack.

Returns:

Raises:

Since:

  • 1.0.0



158
159
160
161
162
163
164
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 158

def unpack(data)
  if @pack_string
    data.unpack1(@pack_string)
  else
    raise(NotImplementedError,"#{self.class} does not define a #pack_string")
  end
end

#unsigned?Boolean

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.

Whether the scalar type is unsigned.

Returns:

  • (Boolean)

Since:

  • 1.0.0



99
100
101
# File 'lib/ronin/support/binary/ctypes/scalar_type.rb', line 99

def unsigned?
  !@signed
end