Module: Ronin::Support::Binary::CTypes

Includes:
Native
Defined in:
lib/ronin/support/binary/ctypes.rb,
lib/ronin/support/binary/ctypes/os.rb,
lib/ronin/support/binary/ctypes/type.rb,
lib/ronin/support/binary/ctypes/mixin.rb,
lib/ronin/support/binary/ctypes/native.rb,
lib/ronin/support/binary/ctypes/os/bsd.rb,
lib/ronin/support/binary/ctypes/network.rb,
lib/ronin/support/binary/ctypes/os/unix.rb,
lib/ronin/support/binary/ctypes/arch/arm.rb,
lib/ronin/support/binary/ctypes/arch/ppc.rb,
lib/ronin/support/binary/ctypes/arch/x86.rb,
lib/ronin/support/binary/ctypes/int_type.rb,
lib/ronin/support/binary/ctypes/os/linux.rb,
lib/ronin/support/binary/ctypes/os/macos.rb,
lib/ronin/support/binary/ctypes/arch/mips.rb,
lib/ronin/support/binary/ctypes/char_type.rb,
lib/ronin/support/binary/ctypes/enum_type.rb,
lib/ronin/support/binary/ctypes/int8_type.rb,
lib/ronin/support/binary/ctypes/os/netbsd.rb,
lib/ronin/support/binary/ctypes/uint_type.rb,
lib/ronin/support/binary/ctypes/arch/arm64.rb,
lib/ronin/support/binary/ctypes/arch/ppc64.rb,
lib/ronin/support/binary/ctypes/array_type.rb,
lib/ronin/support/binary/ctypes/big_endian.rb,
lib/ronin/support/binary/ctypes/char_types.rb,
lib/ronin/support/binary/ctypes/float_type.rb,
lib/ronin/support/binary/ctypes/int16_type.rb,
lib/ronin/support/binary/ctypes/int32_type.rb,
lib/ronin/support/binary/ctypes/int64_type.rb,
lib/ronin/support/binary/ctypes/os/freebsd.rb,
lib/ronin/support/binary/ctypes/os/openbsd.rb,
lib/ronin/support/binary/ctypes/os/windows.rb,
lib/ronin/support/binary/ctypes/uint8_type.rb,
lib/ronin/support/binary/ctypes/union_type.rb,
lib/ronin/support/binary/ctypes/arch/mips64.rb,
lib/ronin/support/binary/ctypes/arch/x86_64.rb,
lib/ronin/support/binary/ctypes/object_type.rb,
lib/ronin/support/binary/ctypes/scalar_type.rb,
lib/ronin/support/binary/ctypes/string_type.rb,
lib/ronin/support/binary/ctypes/struct_type.rb,
lib/ronin/support/binary/ctypes/uint16_type.rb,
lib/ronin/support/binary/ctypes/uint32_type.rb,
lib/ronin/support/binary/ctypes/uint64_type.rb,
lib/ronin/support/binary/ctypes/float32_type.rb,
lib/ronin/support/binary/ctypes/float64_type.rb,
lib/ronin/support/binary/ctypes/little_endian.rb,
lib/ronin/support/binary/ctypes/type_resolver.rb,
lib/ronin/support/binary/ctypes/aggregate_type.rb,
lib/ronin/support/binary/ctypes/array_object_type.rb,
lib/ronin/support/binary/ctypes/union_object_type.rb,
lib/ronin/support/binary/ctypes/struct_object_type.rb,
lib/ronin/support/binary/ctypes/arch/arm/big_endian.rb,
lib/ronin/support/binary/ctypes/unbounded_array_type.rb,
lib/ronin/support/binary/ctypes/arch/arm64/big_endian.rb,
lib/ronin/support/binary/ctypes/arch/mips/little_endian.rb,
lib/ronin/support/binary/ctypes/arch/mips64/little_endian.rb

Overview

Provides a complete virtual C type system, implemented purely in Ruby. The type objects are then used by Template, Buffer, Stream, Array, Struct, and Union to encode and decoded any C binary data.

Supported Types

  • Scalar Types:
    • Character Types:
    • char (ex: 'c')
    • string (ex: "abc")
    • Signed Integer Types:
    • int8 (-127 - 127)
    • int16 (-32768 to 32767)
    • int32 (-2147483648 - 2147483648)
    • int64 (-9223372036854775808 - 9223372036854775808)
    • Unsigned Integer Types:
    • uint8 (0x00 - 0xff)
    • uint16 (0x0000 - 0xffff)
    • uint32 (0x00000000 - 0xffffffff)
    • uint64 (0x0000000000000000 - 0xffffffffffffffff)
    • Floating Point Types:
    • float (-3.40282347E+38F - 3.40282347E+38F)
    • double (-1.79769313486231570E+308 - 1.79769313486231570E+308)
  • Aggregate Types:
  • Object Types:

Supported Endian-ness

Supported Architectures

Supported Operating Systems (OS)

Defined Under Namespace

Modules: Arch, BigEndian, CharTypes, LittleEndian, Mixin, Native Classes: AggregateType, ArrayObjectType, ArrayType, CharType, EnumType, Float32Type, Float64Type, FloatType, Int16Type, Int32Type, Int64Type, Int8Type, IntType, OS, ObjectType, ScalarType, StringType, StructObjectType, StructType, Type, TypeResolver, UInt16Type, UInt32Type, UInt64Type, UInt8Type, UIntType, UnboundedArrayType, UnionObjectType, UnionType

Constant Summary collapse

INT16_LE =

little-endian types

LittleEndian::INT16
INT32_LE =
LittleEndian::INT32
INT64_LE =
LittleEndian::INT64
UINT16_LE =
LittleEndian::UINT16
UINT32_LE =
LittleEndian::UINT32
UINT64_LE =
LittleEndian::UINT64
WORD_LE =
LittleEndian::WORD
DWORD_LE =
LittleEndian::DWORD
QWORD_LE =
LittleEndian::QWORD
MACHINE_WORD_LE =
LittleEndian::MACHINE_WORD
POINTER_LE =
LittleEndian::POINTER
FLOAT32_LE =
LittleEndian::FLOAT32
FLOAT64_LE =
LittleEndian::FLOAT64
FLOAT_LE =
LittleEndian::FLOAT
DOUBLE_LE =
LittleEndian::DOUBLE
INT16_BE =

big-endian types

BigEndian::INT16
INT32_BE =
BigEndian::INT32
INT64_BE =
BigEndian::INT64
UINT16_BE =
BigEndian::UINT16
UINT32_BE =
BigEndian::UINT32
UINT64_BE =
BigEndian::UINT64
WORD_BE =
BigEndian::WORD
DWORD_BE =
BigEndian::DWORD
QWORD_BE =
BigEndian::QWORD
MACHINE_WORD_BE =
BigEndian::MACHINE_WORD
POINTER_BE =
BigEndian::POINTER
FLOAT32_BE =
BigEndian::FLOAT32
FLOAT64_BE =
BigEndian::FLOAT64
FLOAT_BE =
BigEndian::FLOAT
DOUBLE_BE =
BigEndian::DOUBLE
INT16_NE =

network byte-order types

Network::INT16
INT32_NE =
Network::INT32
INT64_NE =
Network::INT64
UINT16_NE =
Network::UINT16
UINT32_NE =
Network::UINT32
UINT64_NE =
Network::UINT64
WORD_NE =
Network::WORD
DWORD_NE =
Network::DWORD
QWORD_NE =
Network::QWORD
MACHINE_WORD_NE =
Network::MACHINE_WORD
POINTER_NE =
Network::POINTER
FLOAT32_NE =
Network::FLOAT32
FLOAT64_NE =
Network::FLOAT64
FLOAT_NE =
Network::FLOAT
DOUBLE_NE =
Network::DOUBLE
INT16_NET =
Network::INT16
INT32_NET =
Network::INT32
INT64_NET =
Network::INT64
UINT16_NET =
Network::UINT16
UINT32_NET =
Network::UINT32
UINT64_NET =
Network::UINT64
WORD_NET =
Network::WORD
DWORD_NET =
Network::DWORD
QWORD_NET =
Network::QWORD
MACHINE_WORD_NET =
Network::MACHINE_WORD
POINTER_NET =
Network::POINTER
FLOAT32_NET =
Network::FLOAT32
FLOAT64_NET =
Network::FLOAT64
FLOAT_NET =
Network::FLOAT
DOUBLE_NET =
Network::DOUBLE
TYPES =

All types (native, little-endian, big-endian, and network byte-order).

Native::TYPES.merge(
  # little-endian types
  int16_le: LittleEndian::INT16,
  int32_le: LittleEndian::INT32,
  int64_le: LittleEndian::INT64,

  short_le:     LittleEndian::INT16,
  int_le:       LittleEndian::INT32,
  long_le:      LittleEndian::LONG,
  long_long_le: LittleEndian::INT64,

  uint16_le: LittleEndian::UINT16,
  uint32_le: LittleEndian::UINT32,
  uint64_le: LittleEndian::UINT64,

  ushort_le:     LittleEndian::UINT16,
  uint_le:       LittleEndian::UINT32,
  ulong_le:      LittleEndian::ULONG,
  ulong_long_le: LittleEndian::UINT64,

  word_le:  WORD_LE,
  dword_le: DWORD_LE,
  qword_le: QWORD_LE,

  machine_word_le: LittleEndian::MACHINE_WORD,
  pointer_le:      LittleEndian::POINTER,

  float32_le: LittleEndian::FLOAT32,
  float64_le: LittleEndian::FLOAT64,

  float_le:   LittleEndian::FLOAT,
  double_le:  LittleEndian::DOUBLE,

  # big-endian types
  int16_be: BigEndian::INT16,
  int32_be: BigEndian::INT32,
  int64_be: BigEndian::INT64,

  short_be:     BigEndian::INT16,
  int_be:       BigEndian::INT32,
  long_be:      BigEndian::LONG,
  long_long_be: BigEndian::INT64,

  uint16_be: BigEndian::UINT16,
  uint32_be: BigEndian::UINT32,
  uint64_be: BigEndian::UINT64,

  ushort_be:     BigEndian::UINT16,
  uint_be:       BigEndian::UINT32,
  ulong_be:      BigEndian::ULONG,
  ulong_long_be: BigEndian::UINT64,

  word_be:  WORD_BE,
  dword_be: DWORD_BE,
  qword_be: QWORD_BE,

  machine_word_be: BigEndian::MACHINE_WORD,
  pointer_be:      BigEndian::POINTER,

  float32_be: BigEndian::FLOAT32,
  float64_be: BigEndian::FLOAT64,

  float_be:   BigEndian::FLOAT,
  double_be:  BigEndian::DOUBLE,

  # network byte-order types
  int16_ne: Network::INT16,
  int32_ne: Network::INT32,
  int64_ne: Network::INT64,

  short_ne:     Network::INT16,
  int_ne:       Network::INT32,
  long_ne:      Network::LONG,
  long_long_ne: Network::INT64,

  uint16_ne: Network::UINT16,
  uint32_ne: Network::UINT32,
  uint64_ne: Network::UINT64,

  ushort_ne:     Network::UINT16,
  uint_ne:       Network::UINT32,
  ulong_ne:      Network::ULONG,
  ulong_long_ne: Network::UINT64,

  word_ne:  WORD_NE,
  dword_ne: DWORD_NE,
  qword_ne: QWORD_NE,

  machine_word_ne: Network::MACHINE_WORD,
  pointer_ne:      Network::POINTER,

  float32_ne: Network::FLOAT32,
  float64_ne: Network::FLOAT64,

  float_ne:   Network::FLOAT,
  double_ne:  Network::DOUBLE,

  # `_net` -> `_ne` aliases
  int16_net: Network::INT16,
  int32_net: Network::INT32,
  int64_net: Network::INT64,

  short_net:     Network::INT16,
  int_net:       Network::INT32,
  long_net:      Network::LONG,
  long_long_net: Network::INT64,

  uint16_net: Network::UINT16,
  uint32_net: Network::UINT32,
  uint64_net: Network::UINT64,

  ushort_net:     Network::UINT16,
  uint_net:       Network::UINT32,
  ulong_net:      Network::ULONG,
  ulong_long_net: Network::UINT64,

  word_net:  WORD_NET,
  dword_net: DWORD_NET,
  qword_net: QWORD_NET,

  machine_word_net: Network::MACHINE_WORD,
  pointer_net:      Network::POINTER,

  float32_net: Network::FLOAT32,
  float64_net: Network::FLOAT64,

  float_net:   Network::FLOAT,
  double_net:  Network::DOUBLE
)
ENDIAN =

Represents the different endian type systems.

{
  little: LittleEndian,
  big:    BigEndian,
  net:    Network,

  nil => self
}
ARCHES =

The supported architectures.

{
  x86: Arch::X86,

  x86_64: Arch::X86_64,
  ia64:   Arch::X86_64,
  amd64:  Arch::X86_64,

  ppc:   Arch::PPC,
  ppc64: Arch::PPC64,

  mips:    Arch::MIPS,
  mips_le: Arch::MIPS::LittleEndian,
  mips_be: Arch::MIPS, # MIPS is big-endian by default

  mips64:    Arch::MIPS64,
  mips64_le: Arch::MIPS64::LittleEndian,
  mips64_be: Arch::MIPS64, # MIPS is big-endian by default

  arm:      Arch::ARM,
  arm_le:   Arch::ARM,
  arm_be:   Arch::ARM::BigEndian,

  arm64:    Arch::ARM64,
  arm64_le: Arch::ARM64, # ARM is little-endian by default
  arm64_be: Arch::ARM64::BigEndian
}
OSES =

The supported Operating Systems.

{
  unix:  OS::UNIX,

  bsd:     OS::BSD,
  freebsd: OS::FreeBSD,
  openbsd: OS::OpenBSD,
  netbsd:  OS::NetBSD,

  linux:   OS::Linux,
  macos:   OS::MacOS,
  windows: OS::Windows
}
Network =
BigEndian

Constants included from Native

Native::ADDRESS_SIZE, Native::BYTE, Native::DOUBLE, Native::DWORD, Native::FLOAT, Native::FLOAT32, Native::FLOAT64, Native::INT, Native::INT16, Native::INT32, Native::INT64, Native::INT8, Native::LONG, Native::LONG_LONG, Native::MACHINE_WORD, Native::POINTER, Native::QWORD, Native::SHORT, Native::UINT, Native::UINT16, Native::UINT32, Native::UINT64, Native::UINT8, Native::ULONG, Native::ULONG_LONG, Native::USHORT, Native::WORD

Constants included from CharTypes

CharTypes::CHAR, CharTypes::STRING, CharTypes::UCHAR

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Type

Fetches the type from TYPES.

Parameters:

  • name (Symbol)

    The type name to lookup.

Returns:

Raises:

  • (ArgumentError)

    The type name was unknown.



341
342
343
344
345
# File 'lib/ronin/support/binary/ctypes.rb', line 341

def self.[](name)
  TYPES.fetch(name) do
    raise(ArgumentError,"unknown type: #{name.inspect}")
  end
end

.platform(arch: nil, endian: nil, os: nil) ⇒ CTypes, ...

Returns the types module/object for the given endianness, architecture, and/or Operating System (OS).

The architecture name to lookup.

The Operating System name to lookup.

The types module.

Parameters:

  • endian (:little, :big, :net, nil) (defaults to: nil)

    The endianness.

  • arch (:x86, :x86_64, :ppc, :ppc64, :mips, :mips_le, :mips_be, :mips64, :mips64_le, :mips64_be, :arm, :arm_le, :arm_be, :arm64, :arm64_le, :arm64_be) (defaults to: nil)
  • os (:linux, :macos, :windows, :bsd, :freebsd, :openbsd, :netbsd) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)

    The endian was unknown, the architecture name was unknown, or the os name was unknown.



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/ronin/support/binary/ctypes.rb', line 440

def self.platform(arch: nil, endian: nil, os: nil)
  types = if arch
            ARCHES.fetch(arch) do
              raise(ArgumentError,"unknown architecture: #{arch.inspect}")
            end
          else
            ENDIAN.fetch(endian) do
              raise(ArgumentError,"unknown endian: #{endian.inspect}")
            end
          end

  if os
    types = OSES.fetch(os) {
      raise(ArgumentError,"unknown OS: #{os.inspect}")
    }.new(types)
  end

  return types
end