class TurboRex::CStruct::CStructBuilder

Attributes

s[R]
struct_name[R]

Public Class Methods

create_method(method_name, &block) click to toggle source
# File lib/turborex/cstruct.rb, line 448
def self.create_method(method_name, &block)
  define_method(method_name, &block)
end
new(arch = 'x86') click to toggle source
# File lib/turborex/cstruct.rb, line 312
def initialize(arch = 'x86')
  @s = ::Rex::Struct2::CStructTemplate.new
  @arch = arch
  define_variable_length_type
end

Public Instance Methods

ATOM(field, init_value = 0, endian = 'v')
Alias for: word
BOOL(field, init_value = 0, endian = 'v')
Alias for: int
BOOLEAN(field, init_value = 0)
Alias for: BYTE
BYTE(field, init_value = 0)
Also aliased as: BOOLEAN
Alias for: uchar
CCHAR(field, init_value = 0)
Alias for: char
CHAR(field, init_value = 0)
Alias for: char
COLORREF(field, init_value = 0, endian = 'v')
Alias for: DWORD
DWORD(field, init_value = 0, endian = 'v')
Also aliased as: COLORREF, LCID, LCTYPE, LGRPID
Alias for: dword
DWORD32(field, init_value = 0, endian = 'v')
Alias for: uint
DWORD64(field, init_value = 0, endian = 'v')
Alias for: uint64
DWORDLONG(field, init_value = 0, endian = 'v')
Alias for: uint64
HANDLE(field, init_value = 0, endian = 'v')
Alias for: PVOID
HFILE(field, init_value = 0, endian = 'v')
Alias for: int
INT(field, init_value = 0, endian = 'v')
Alias for: int
INT16(field, init_value = 0, endian = 'v')
Alias for: short
INT32(field, init_value = 0, endian = 'v')
Alias for: int
INT64(field, init_value = 0, endian = 'v')
Alias for: int64
INT8(field, init_value = 0)
Alias for: char
LANGID(field, init_value = 0, endian = 'v')
Alias for: WORD
LCID(field, init_value = 0, endian = 'v')
Alias for: DWORD
LCTYPE(field, init_value = 0, endian = 'v')
Alias for: DWORD
LGRPID(field, init_value = 0, endian = 'v')
Alias for: DWORD
LONG(field, init_value = 0, endian = 'v')
Alias for: int
LONG32(field, init_value = 0, endian = 'v')
Alias for: int
LONG64(field, init_value = 0, endian = 'v')
Alias for: int64
PVOID(field, init_value = 0, endian = 'v')
Also aliased as: HANDLE
Alias for: pvoid
QWORD(field, init_value = 0, endian = 'v')
Alias for: uint64
SHORT(field, init_value = 0, endian = 'v')
Alias for: short
UCHAR(field, init_value = 0)
Alias for: uchar
UINT(field, init_value = 0, endian = 'v')
Alias for: uint
UINT16(field, init_value = 0, endian = 'v')
Alias for: ushort
UINT32(field, init_value = 0, endian = 'v')
Alias for: uint
UINT64(field, init_value = 0, endian = 'v')
Alias for: uint64
UINT8(field, init_value = 0)
Alias for: uchar
ULONG(field, init_value = 0, endian = 'v')
Alias for: uint
ULONG32(field, init_value = 0, endian = 'v')
Alias for: uint
ULONG64(field, init_value = 0, endian = 'v')
Alias for: uint64
USHORT(field, init_value = 0, endian = 'v')
Alias for: ushort
WORD(field, init_value = 0, endian = 'v')
Also aliased as: LANGID
Alias for: word
__int64(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 354
def __int64(field, init_value = 0, endian = 'v')
  add_object('int64' + endian, field, init_value)
end
add_object(type, field, init_value) click to toggle source
# File lib/turborex/cstruct.rb, line 429
def add_object(type, field, init_value)
  if field.count == 1
    @s.template << [type, field.to_s, init_value]
  else
    struct_temp = ::Rex::Struct2::CStructTemplate.new
    field.count.times do |i|
      struct_temp.template << [type, field.to_s + '_' + i.to_s, init_value]
    end

    @s.template << ['template', field.to_s, struct_temp]
  end
end
alias_singleton_method(alias_sym, method) click to toggle source
# File lib/turborex/cstruct.rb, line 416
def alias_singleton_method(alias_sym, method)
  self.singleton_class.send(:alias_method, alias_sym, method)
end
build(name) click to toggle source
# File lib/turborex/cstruct.rb, line 442
def build(name)
  set_struct_name(name)

  self
end
char(field, init_value = 0) click to toggle source
# File lib/turborex/cstruct.rb, line 318
def char(field, init_value = 0)
  add_object('int8', field, init_value)
end
Also aliased as: CCHAR, CHAR, INT8
define_variable_length_type() click to toggle source
# File lib/turborex/cstruct.rb, line 420
def define_variable_length_type
  arch_32? ? alias_singleton_method(:ULONG_PTR, :uint) : alias_singleton_method(:ULONG_PTR, :uint64)
  arch_32? ? alias_singleton_method(:ULONG_PTR_T, :uint) : alias_singleton_method(:ULONG_PTR_T, :uint64)
end
dword(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 346
def dword(field, init_value = 0, endian = 'v')
  add_object('uint32' + endian, field, init_value)
end
Also aliased as: DWORD
from_str(s) click to toggle source
# File lib/turborex/cstruct.rb, line 460
def from_str(s)
  struct = @s.make_struct
  struct.from_s(s)
  struct
end
int(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 334
def int(field, init_value = 0, endian = 'v')
  add_object('int32' + endian, field, init_value)
end
Also aliased as: BOOL, HFILE, INT, INT32, LONG32, LONG
int64(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 350
def int64(field, init_value = 0, endian = 'v')
  add_object('int64' + endian, field, init_value)
end
Also aliased as: INT64, LONG64
make(opts = {}) click to toggle source
# File lib/turborex/cstruct.rb, line 456
def make(opts = {})
  @s.make_struct(opts[:pack], opts[:align])
end
pvoid(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 362
def pvoid(field, init_value = 0, endian = 'v')
  case @arch
  when 'x86'
    add_object('uint32' + endian, field, init_value)
  when 'x64'
    add_object('uint64' + endian, field, init_value)
  end
end
Also aliased as: PVOID
set_struct_name(name) click to toggle source
# File lib/turborex/cstruct.rb, line 452
def set_struct_name(name)
  @struct_name = name
end
short(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 326
def short(field, init_value = 0, endian = 'v') #  'v' is little-endian, 'n' is big-endian
  add_object('int16' + endian, field, init_value)
end
Also aliased as: SHORT, INT16
struct() { || ... } click to toggle source
# File lib/turborex/cstruct.rb, line 425
def struct(&block)
  yield
end
uchar(field, init_value = 0) click to toggle source
# File lib/turborex/cstruct.rb, line 322
def uchar(field, init_value = 0)
  add_object('uint8', field, init_value)
end
Also aliased as: BYTE, UCHAR, UINT8
uint(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 338
def uint(field, init_value = 0, endian = 'v')
  add_object('uint32' + endian, field, init_value)
end
Also aliased as: DWORD32, UINT, UINT32, ULONG, ULONG32
uint64(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 358
def uint64(field, init_value = 0, endian = 'v')
  add_object('uint64' + endian, field, init_value)
end
Also aliased as: QWORD, DWORDLONG, DWORD64, UINT64, ULONG64
ushort(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 330
def ushort(field, init_value = 0, endian = 'v')
  add_object('uint16' + endian, field, init_value)
end
Also aliased as: UINT16, USHORT
word(field, init_value = 0, endian = 'v') click to toggle source
# File lib/turborex/cstruct.rb, line 342
def word(field, init_value = 0, endian = 'v')
  add_object('uint16' + endian, field, init_value)
end
Also aliased as: ATOM, WORD

Private Instance Methods

arch_32?() click to toggle source
# File lib/turborex/cstruct.rb, line 468
def arch_32?
  @arch == 'x86'
end
arch_64?() click to toggle source
# File lib/turborex/cstruct.rb, line 472
def arch_64?
  !arch_32?
end