Class: Ronin::Support::Binary::Union

Inherits:
Struct show all
Defined in:
lib/ronin/support/binary/union.rb

Overview

Note:

This class provides lazy memory mapped access to an underlying

Generic Binary Union class.

buffer. This means values are decoded/encoded each time they are read or written to.

Examples

Defining Fields

class MyUnion < Ronin::Support::Binary::Union

  member :c, :char
  member :i, :int16
  member :u, :uint32

end

Initializing Fields

union   = MyUnion.new
union.i = -1

union = MyUnion.new(i: -1)

Inheriting Unions

class MyUnion < Ronin::Support::Binary::Union

  member :c, :char
  member :i, :int16
  member :u, :uint32

end

class MyUnion2 < MyUnion

  member :f, :float32
  member :d, :float64

end

union   = MyUnion.new
union.i = -1

union2   = MyUnion.new
union2.i = -1
union2.d = 1.123

Packing Unions

class MyUnion < Ronin::Support::Binary::Union

  member :c, :char
  member :i, :int16
  member :u, :uint32

end

union = MyUnion.new
union.u = 0x11111111
union.i = -1
union.pack
# => "\xFF\xFF\x11\x11"

Unpacking Unions

class MyUnion < Ronin::Support::Binary::Union

  member :c, :char
  member :i, :int32
  member :u, :uint32

end

union = MyUnion.unpack("\xFF\xFF\x11\x11")
union.c
# => "\xFF"
union.i
# => -1
union.u
# => 286392319

Array Fields

class MyUnion < Ronin::Support::Binary::Union

  member :c, :char
  member :i, :int16
  member :u, :uint32
  member :nums, [:uint8, 10]

end

union = MyUnion.new
union.nums = [0x01, 0x02, 0x03, 0x04]
union.pack
# => "\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00"

Unbounded Array Fields

class MyUnion < Ronin::Support::Binary::Union

  member :c, :char
  member :i, :int16
  member :u, :uint32
  member :payload, (:uint8..)

end

union = MyUnion.new
union.payload = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
union.pack
# => "\x01\x02\x03\x04\x05\x06\a\b"

Default Endianness

class MyUnion < Ronin::Support::Binary::Union

  endian :big

  member :c, :char
  member :i, :int32
  member :u, :uint32

end

union = MyUnion.new(u: 0xaabbccdd)
union.pack
# => "\xAA\xBB\xCC\xDD"

Instance Attribute Summary

Attributes inherited from Struct

#type

Attributes included from CTypes::Mixin

#arch, #endian, #os, #type_resolver, #type_system

Attributes inherited from Memory

#string

Method Summary

Methods inherited from Struct

#[], #[]=, align, alignment, #each, has_member?, #initialize, member, members, pack, padding, platform, read_from, size, #to_a, #to_h, translate, type, type_resolver, type_system, unpack

Methods included from CTypes::Mixin

#initialize_type_system

Methods inherited from Memory

#+, #[], #[]=, #byteslice, #clear, #copy_from, #copy_to, #initialize, #pack, #read_from, #size

Constructor Details

This class inherits a constructor from Ronin::Support::Binary::Struct