Class: Resedit::ExeHeader

Inherits:
Changeable show all
Defined in:
lib/resedit/classes/exefile.rb

Direct Known Subclasses

BWHeader, LEHeader, MZHeader

Constant Summary collapse

BLK =
0x200
PARA =
0x10
HDRDESCR =
[:Magic]
HDRUNPACK =
"v*"

Constants inherited from Changeable

Changeable::COL_CHANGED, Changeable::COL_ORIGINAL, Changeable::HOW_CHANGED, Changeable::HOW_ORIGINAL, Changeable::LOG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Changeable

#addData, #bytes, #changed?, #colStr, #colVal, #curcol, #dbgdump, #debug, #fix, #getChanges, #getData, #hex, #hexify, #insert, #loadChanges, #parseHow, #revert, #saveChanges, #saveData, #setData, #size, #undo, #unhexify

Constructor Details

#initialize(exe, file, fsize) ⇒ ExeHeader

Returns a new instance of ExeHeader.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/resedit/classes/exefile.rb', line 24

def initialize(exe, file, fsize)
    raise "Not EXE file" if fsize < self.class::HSIZE
    @exe = exe
    super(file, self.class::HSIZE)
    @hdrtbl = nil
    @_infoOrig = loadInfo()
    @_info = nil
    @info = @_infoOrig
    @relocFix = 0
    raise "Not EXE file" if self.class::MAGIC != @info[:Magic] && (!self.class::MAGIC.is_a?(Array) || !self.class::MAGIC.include?(@info[:Magic]))
    loadTables(file)
    loadTail(file)
end

Instance Attribute Details

#exeObject (readonly)

Returns the value of attribute exe.



22
23
24
# File 'lib/resedit/classes/exefile.rb', line 22

def exe
  @exe
end

#infoObject (readonly)

Returns the value of attribute info.



22
23
24
# File 'lib/resedit/classes/exefile.rb', line 22

def info
  @info
end

Instance Method Details

#_headerTableObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/resedit/classes/exefile.rb', line 66

def _headerTable()
    return @hdrtbl if @hdrtbl
    @hdrtbl = []
    sz = ["acC","vnsS","VNlL","qQ"]
    ofs = 0
    len = self.class::HDRDESCR.length
    self.class::HDRUNPACK.scan(/[a-zA-Z][\d*]*/).each{|s|
        size = sz.index{|v| v.include?(s[0])}
        raise "Unsupported header descr #{s}" if size==nil
        size = 1 << size
        cnt = s.length>1 && s[1]!='*' ? s[1..-1].to_i : 1
        if s[0]=='a'
            cnt = self.class::HSIZE - ofs if s[1]=='*'
            @hdrtbl += [[ofs, cnt, s]]
            ofs += cnt
            next
        end
        cnt = len - @hdrtbl.length if s[1]=='*'
        cnt.times{
            @hdrtbl += [[ofs, size, s[0]]]
            ofs += size
        }
    }
    raise "Header descr unmatch #{@hdrtbl} #{self.class::HDRDESCR}" if @hdrtbl.length!=len
    return @hdrtbl
end

#addReloc(ofs, value) ⇒ Object



124
# File 'lib/resedit/classes/exefile.rb', line 124

def addReloc(ofs, value); raise "NotImplemented" end

#change(ofs, bytes) ⇒ Object



49
50
51
52
# File 'lib/resedit/classes/exefile.rb', line 49

def change(ofs, bytes)
    super(ofs, bytes)
    @_info = nil if (ofs < self.class::HSIZE)
end

#entry(size) ⇒ Object



123
# File 'lib/resedit/classes/exefile.rb', line 123

def entry(size); raise "NotImplemented" end

#fieldOffset(field) ⇒ Object



93
# File 'lib/resedit/classes/exefile.rb', line 93

def fieldOffset(field); return _headerTable()[self.class::HDRDESCR.index(field)][0] end

#fieldSize(field) ⇒ Object



94
# File 'lib/resedit/classes/exefile.rb', line 94

def fieldSize(field); return _headerTable()[self.class::HDRDESCR.index(field)][1] end

#fileSizeObject



121
# File 'lib/resedit/classes/exefile.rb', line 121

def fileSize(); raise "NotImplemented"  end

#headerSizeObject



108
# File 'lib/resedit/classes/exefile.rb', line 108

def headerSize(); self.class::HSIZE end

#loadInfoObject



55
56
57
58
59
# File 'lib/resedit/classes/exefile.rb', line 55

def loadInfo()
    v = getData(0, self.class::HSIZE).unpack(self.class::HDRUNPACK)
    ret = self.class::HDRDESCR.map.with_index { |x, i| [x, v[i]] }.to_h
    return ret
end

#loadTables(file) ⇒ Object



61
# File 'lib/resedit/classes/exefile.rb', line 61

def loadTables(file); end

#loadTail(file) ⇒ Object



62
63
64
# File 'lib/resedit/classes/exefile.rb', line 62

def loadTail(file);
    addData(file, headerSize() - self.class::HSIZE)
end

#mode(how) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/resedit/classes/exefile.rb', line 38

def mode(how)
    super(how)
    if @mode == HOW_ORIGINAL
        @info = @_infoOrig
    else
        @_info = loadInfo() if  !@_info
        @info = @_info
    end
end


110
111
112
113
114
115
116
117
118
# File 'lib/resedit/classes/exefile.rb', line 110

def print(what, how)
    return super(what, how) if what!="header"
    mode(parseHow(how))
    @info.each{|k,v|
        printf("%20s:\t%s\n", k.to_s, colStr(v, changed?(fieldOffset(k),fieldSize(k))))
    }
    puts
    return true
end

#setBodySize(size) ⇒ Object



106
# File 'lib/resedit/classes/exefile.rb', line 106

def setBodySize(size); setFileSize(size + headerSize()) end

#setFileSize(size) ⇒ Object



122
# File 'lib/resedit/classes/exefile.rb', line 122

def setFileSize(size); raise "NotImplemented" end

#setHeaderSize(size) ⇒ Object



120
# File 'lib/resedit/classes/exefile.rb', line 120

def setHeaderSize(size); raise "NotImplemented" end

#setInfo(field, values) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/resedit/classes/exefile.rb', line 96

def setInfo(field, values)
    raise "Unknown header field #{field}" if !self.class::HDRDESCR.include?(field)
    tbl = _headerTable()
    values = [values] if !values.is_a?(Array)
    ofs = 0
    idx = self.class::HDRDESCR.index(field)
    pack = tbl[idx, values.length].map{|v| v[2]}.join('')
    change(tbl[idx][0], values.pack(pack))
end