Class: Exerb::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/exerb/executable.rb

Overview

#

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ Executable

Returns a new instance of Executable.



23
24
25
26
# File 'lib/exerb/executable.rb', line 23

def initialize(core)
  @core = core
  @rsrc = Exerb::Resource.new_from_pe_binary(@core)
end

Instance Attribute Details

#coreObject (readonly)

Returns the value of attribute core.



28
29
30
# File 'lib/exerb/executable.rb', line 28

def core
  @core
end

#rsrcObject

Returns the value of attribute rsrc.



29
30
31
# File 'lib/exerb/executable.rb', line 29

def rsrc
  @rsrc
end

Class Method Details

.read(input) ⇒ Object



31
32
33
# File 'lib/exerb/executable.rb', line 31

def self.read(input)
  return self.new(Exerb::Utility.read(input))
end

Instance Method Details

#packObject

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/exerb/executable.rb', line 35

def pack
  pe  = Exerb::Win32::PeFile.new_from_binary(@core)
  fh  = pe.nt_headers.file_header
  oh  = pe.nt_headers.optional_header
  rsh = pe.sections.last
  raise(Exerb::ExerbError, "the last section must be resource section") unless rsh.name == '.rsrc'

  packed_rsrc  = @rsrc.pack(rsh.virtual_address)
  aligned_rsrc = Exerb::Utility.alignment(packed_rsrc, oh.file_alignment)
  old_resource_size = Exerb::Utility.align_value(rsh.virtual_size, oh.section_alignment)
  new_resource_size = Exerb::Utility.align_value(packed_rsrc.size, oh.section_alignment)

  @core[rsh.pointer_to_raw_data, rsh.size_of_raw_data] = aligned_rsrc

  fh.time_date_stamp                 = Time.now.to_i
  oh.size_of_initialized_data        = oh.size_of_initialized_data - rsh.size_of_raw_data + aligned_rsrc.size
  oh.size_of_image                   = oh.size_of_image            - old_resource_size + new_resource_size
  oh.resource_directory_virtual_size = packed_rsrc.size
  rsh.virtual_size                   = packed_rsrc.size
  rsh.size_of_raw_data               = aligned_rsrc.size

  fh.update(@core)
  oh.update(@core)
  rsh.update(@core)

  return Exerb::Utility.alignment16(@core)
end

#write(output) ⇒ Object



63
64
65
# File 'lib/exerb/executable.rb', line 63

def write(output)
  Exerb::Utility.write(output, self.pack, 0755)
end