class Ronin::Support::Binary::CTypes::ScalarType
Base class for all scalar types.
@api private
@since 1.0.0
Attributes
The alignment in bytes for the scalar type.
@return [Integer]
The endian-ness of the type.
@return [:little, :big, nil]
Indicates whether the type is signed.
@return [Boolean]
The size in bytes of the type.
@return [1, 2, 4, 8]
Public Class Methods
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`.
# 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
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
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
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
Whether the scalar type is signed.
@return [Boolean]
# File lib/ronin/support/binary/ctypes/scalar_type.rb, line 90 def signed? @signed end
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
Whether the scalar type is unsigned.
@return [Boolean]
# File lib/ronin/support/binary/ctypes/scalar_type.rb, line 99 def unsigned? !@signed end