Class: TurboRex::CStruct::CStructBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/turborex/cstruct.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arch = 'x86') ⇒ CStructBuilder

Returns a new instance of CStructBuilder.



312
313
314
315
316
# File 'lib/turborex/cstruct.rb', line 312

def initialize(arch = 'x86')
  @s = ::Rex::Struct2::CStructTemplate.new
  @arch = arch
  define_variable_length_type
end

Instance Attribute Details

#sObject (readonly)

Returns the value of attribute s.



310
311
312
# File 'lib/turborex/cstruct.rb', line 310

def s
  @s
end

#struct_nameObject (readonly)

Returns the value of attribute struct_name.



309
310
311
# File 'lib/turborex/cstruct.rb', line 309

def struct_name
  @struct_name
end

Class Method Details

.create_method(method_name, &block) ⇒ Object



448
449
450
# File 'lib/turborex/cstruct.rb', line 448

def self.create_method(method_name, &block)
  define_method(method_name, &block)
end

Instance Method Details

#__int64(field, init_value = 0, endian = 'v') ⇒ Object



354
355
356
# 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) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
# 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) ⇒ Object



416
417
418
# 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) ⇒ Object



442
443
444
445
446
# File 'lib/turborex/cstruct.rb', line 442

def build(name)
  set_struct_name(name)

  self
end

#char(field, init_value = 0) ⇒ Object Also known as: CCHAR, CHAR, INT8



318
319
320
# File 'lib/turborex/cstruct.rb', line 318

def char(field, init_value = 0)
  add_object('int8', field, init_value)
end

#define_variable_length_typeObject



420
421
422
423
# 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') ⇒ Object Also known as: DWORD



346
347
348
# File 'lib/turborex/cstruct.rb', line 346

def dword(field, init_value = 0, endian = 'v')
  add_object('uint32' + endian, field, init_value)
end

#from_str(s) ⇒ Object



460
461
462
463
464
# 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') ⇒ Object Also known as: BOOL, HFILE, INT, INT32, LONG32, LONG



334
335
336
# File 'lib/turborex/cstruct.rb', line 334

def int(field, init_value = 0, endian = 'v')
  add_object('int32' + endian, field, init_value)
end

#int64(field, init_value = 0, endian = 'v') ⇒ Object Also known as: INT64, LONG64



350
351
352
# File 'lib/turborex/cstruct.rb', line 350

def int64(field, init_value = 0, endian = 'v')
  add_object('int64' + endian, field, init_value)
end

#make(opts = {}) ⇒ Object



456
457
458
# 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') ⇒ Object Also known as: PVOID



362
363
364
365
366
367
368
369
# 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

#set_struct_name(name) ⇒ Object



452
453
454
# File 'lib/turborex/cstruct.rb', line 452

def set_struct_name(name)
  @struct_name = name
end

#short(field, init_value = 0, endian = 'v') ⇒ Object Also known as: SHORT, INT16

‘v’ is little-endian, ‘n’ is big-endian



326
327
328
# 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

#struct(&block) ⇒ Object



425
426
427
# File 'lib/turborex/cstruct.rb', line 425

def struct(&block)
  yield
end

#uchar(field, init_value = 0) ⇒ Object Also known as: BYTE, UCHAR, UINT8



322
323
324
# File 'lib/turborex/cstruct.rb', line 322

def uchar(field, init_value = 0)
  add_object('uint8', field, init_value)
end

#uint(field, init_value = 0, endian = 'v') ⇒ Object Also known as: DWORD32, UINT, UINT32, ULONG, ULONG32



338
339
340
# File 'lib/turborex/cstruct.rb', line 338

def uint(field, init_value = 0, endian = 'v')
  add_object('uint32' + endian, field, init_value)
end

#uint64(field, init_value = 0, endian = 'v') ⇒ Object Also known as: QWORD, DWORDLONG, DWORD64, UINT64, ULONG64



358
359
360
# File 'lib/turborex/cstruct.rb', line 358

def uint64(field, init_value = 0, endian = 'v')
  add_object('uint64' + endian, field, init_value)
end

#ushort(field, init_value = 0, endian = 'v') ⇒ Object Also known as: UINT16, USHORT



330
331
332
# File 'lib/turborex/cstruct.rb', line 330

def ushort(field, init_value = 0, endian = 'v')
  add_object('uint16' + endian, field, init_value)
end

#word(field, init_value = 0, endian = 'v') ⇒ Object Also known as: ATOM, WORD



342
343
344
# File 'lib/turborex/cstruct.rb', line 342

def word(field, init_value = 0, endian = 'v')
  add_object('uint16' + endian, field, init_value)
end