class Ronin::Support::Binary::CTypes::ScalarType

Base class for all scalar types.

@api private

@since 1.0.0

Attributes

alignment[R]

The alignment in bytes for the scalar type.

@return [Integer]

endian[R]

The endian-ness of the type.

@return [:little, :big, nil]

signed[R]

Indicates whether the type is signed.

@return [Boolean]

size[R]

The size in bytes of the type.

@return [1, 2, 4, 8]

Public Class Methods

new(size: , alignment: size, endian: , signed: , **kwargs) click to toggle source

Initializes the scalar type.

@param [1, 2, 4, 8] size

The scalar type's size in bytes.

@param [Integer, nil] alignment

Optional custom alignment for the scalar type.

@param [:little, :big, nil] endian

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

@param [Boolean] signed

Indicates whether the scalar type is signed or unsigned.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [String] :pack_string

The String for `Array#pack` or `String#unpack`.
Calls superclass method
# 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

Public Instance Methods

align(new_alignment) click to toggle source

Creates a copy of the scalar type with a different {#alignment}.

@param [Integer] new_alignment

The new alignment for the new scalar type.

@return [ScalarType]

The new scalar type.
# 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) click to toggle source

Dequeues a scalar value from the flat list of values.

@param [Array] values

The flat array of values.

@return [Integer, Float, String, nil]

The dequeued scalar value.

@api private

# File lib/ronin/support/binary/ctypes/scalar_type.rb, line 192
def dequeue_value(values)
  values.shift
end
enqueue_value(values,value) click to toggle source

Enqueues a scalar value onto the flat list of values.

@param [Array] values

The flat array of values.

@param [Integer, Float, String, nil] value

The scalar value to enqueue.

@api private

# File lib/ronin/support/binary/ctypes/scalar_type.rb, line 177
def enqueue_value(values,value)
  values.push(value)
end
pack(value) click to toggle source

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

@param [Integer, Float, String] value

The value to pack.

@return [String]

The packed binary data.

@raise [NotImplementedError]

{#pack_string} was not set.

@api public

# 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?() click to toggle source

Whether the scalar type is signed.

@return [Boolean]

# File lib/ronin/support/binary/ctypes/scalar_type.rb, line 90
def signed?
  @signed
end
unpack(data) click to toggle source

Unpacks the binary data.

@param [String] data

The binary data to unpack.

@return [Integer, Float, String, nil]

The unpacked value.

@raise [NotImplementedError]

{#pack_string} was not set.

@api public

# 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?() click to toggle source

Whether the scalar type is unsigned.

@return [Boolean]

# File lib/ronin/support/binary/ctypes/scalar_type.rb, line 99
def unsigned?
  !@signed
end