Module: CTypes

Extended by:
Helpers
Defined in:
lib/ctypes.rb,
lib/ctypes/int.rb,
lib/ctypes/pad.rb,
lib/ctypes/enum.rb,
lib/ctypes/type.rb,
lib/ctypes/array.rb,
lib/ctypes/union.rb,
lib/ctypes/bitmap.rb,
lib/ctypes/string.rb,
lib/ctypes/struct.rb,
lib/ctypes/helpers.rb,
lib/ctypes/version.rb,
lib/ctypes/bitfield.rb,
lib/ctypes/exporter.rb,
lib/ctypes/importers.rb,
lib/ctypes/terminated.rb,
lib/ctypes/enum/builder.rb,
lib/ctypes/union/builder.rb,
lib/ctypes/struct/builder.rb,
lib/ctypes/bitfield/builder.rb,
lib/ctypes/importers/castxml.rb,
lib/ctypes/missing_bytes_error.rb,
lib/ctypes/pretty_print_helpers.rb,
lib/ctypes/importers/castxml/loader.rb

Overview

SPDX-FileCopyrightText: 2025 Cisco SPDX-License-Identifier: MIT

Defined Under Namespace

Modules: Helpers, Importers, PrettyPrintHelpers, Type Classes: Array, Bitfield, Bitmap, Enum, Error, Exporter, Int, MissingBytesError, Pad, String, Struct, Terminated, TerminatorNotFoundError, TruncatedValueError, Union, UnknownAttributeError, UnknownFieldError, UnknownMemberError

Constant Summary collapse

Endian =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Dry::Types["coercible.symbol"].enum(*%i[big little])
UInt8 =

base type for unsiged 8-bit integers

Int.new(bits: 8, signed: false, format: "C", desc: "uint8")
UInt16 =

base type for unsiged 16-bit integers

Int.new(bits: 16, signed: false, format: "S", desc: "uint16")
UInt32 =

base type for unsiged 32-bit integers

Int.new(bits: 32, signed: false, format: "L", desc: "uint32")
UInt64 =

base type for unsiged 64-bit integers

Int.new(bits: 64, signed: false, format: "Q", desc: "uint64")
Int8 =

base type for siged 8-bit integers

Int.new(bits: 8, signed: true, format: "c", desc: "int8")
Int16 =

base type for siged 16-bit integers

Int.new(bits: 16, signed: true, format: "s", desc: "int16")
Int32 =

base type for siged 32-bit integers

Int.new(bits: 32, signed: true, format: "l", desc: "int32")
Int64 =

base type for siged 64-bit integers

Int.new(bits: 64, signed: true, format: "q", desc: "int64")
VERSION =
"0.3.0"

Class Method Summary collapse

Methods included from Helpers

array, bitfield, bitmap, enum, string, struct, union

Class Method Details

.default_endianObject

get the default endian for the system; defaults to native endian



67
68
69
# File 'lib/ctypes.rb', line 67

def self.default_endian
  @endian ||= host_endian
end

.default_endian=(value) ⇒ Object

set the endian for any datatype that does not have an explicit endian set

Examples:

big endian

CTypes.default_endian = :big
t = CTypes::UInt32
t.pack(0xdeadbeef)                  # => "\xde\xad\xbe\xef"
t.pack(0xdeadbeef, endian: :little) # => "\xef\xbe\xad\xde"

# create a type that overrides the default endian
l = CTypes::UInt32.with_endian(:little)
l.pack(0xdeadbeef)                  # => "\xef\xbe\xad\xde"

little endian

CTypes.default_endian = :little
t = CTypes::UInt32
t.pack(0xdeadbeef)                  # => "\xef\xbe\xad\xde"
t.pack(0xdeadbeef, endian: :big)    # => "\xde\xad\xbe\xef"

Parameters:

  • value (Symbol)

    endian, :big or little



62
63
64
# File 'lib/ctypes.rb', line 62

def self.default_endian=(value)
  @endian = Endian[value]
end

.host_endianObject

get the endian of the system this code is running on



72
73
74
# File 'lib/ctypes.rb', line 72

def self.host_endian
  @host_endian ||= ("\xde\xad".unpack1("S") == 0xDEAD) ? :big : :little
end

.type_lookupObject

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.



97
98
99
# File 'lib/ctypes.rb', line 97

def self.type_lookup # :nodoc:
  @type_lookup && @type_lookup[-1]
end

.using_type_lookup(lookup) ⇒ Object

set a unknown type lookup method to use in the layout blocks of ‘Ctypes::Struct` and `CTypes::Union`.

Note: the current implementation is not thread-safe.

Examples:

@my_types = { id_t: uint32 }
my_struct = CTypes.using_type_lookup(->(n) { @my_types[n] }) do
  struct do
    attribute id, id_t
  end
end


88
89
90
91
92
93
94
# File 'lib/ctypes.rb', line 88

def self.using_type_lookup(lookup)
  @type_lookup ||= []
  @type_lookup.push(lookup)
  yield
ensure
  @type_lookup.pop
end